Attributor.cpp 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321
  1. //===- Attributor.cpp - Module-wide attribute deduction -------------------===//
  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. // This file implements an interprocedural pass that deduces and/or propagates
  10. // attributes. This is done in an abstract interpretation style fixpoint
  11. // iteration. See the Attributor.h file comment and the class descriptions in
  12. // that file for more information.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/IPO/Attributor.h"
  16. #include "llvm/ADT/GraphTraits.h"
  17. #include "llvm/ADT/PointerIntPair.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/ADT/TinyPtrVector.h"
  21. #include "llvm/Analysis/InlineCost.h"
  22. #include "llvm/Analysis/LazyValueInfo.h"
  23. #include "llvm/Analysis/MemoryBuiltins.h"
  24. #include "llvm/Analysis/MemorySSAUpdater.h"
  25. #include "llvm/Analysis/MustExecute.h"
  26. #include "llvm/Analysis/ValueTracking.h"
  27. #include "llvm/IR/Attributes.h"
  28. #include "llvm/IR/Constant.h"
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/GlobalValue.h"
  31. #include "llvm/IR/GlobalVariable.h"
  32. #include "llvm/IR/IRBuilder.h"
  33. #include "llvm/IR/Instruction.h"
  34. #include "llvm/IR/Instructions.h"
  35. #include "llvm/IR/IntrinsicInst.h"
  36. #include "llvm/IR/NoFolder.h"
  37. #include "llvm/IR/ValueHandle.h"
  38. #include "llvm/IR/Verifier.h"
  39. #include "llvm/InitializePasses.h"
  40. #include "llvm/Support/Casting.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/DebugCounter.h"
  44. #include "llvm/Support/FileSystem.h"
  45. #include "llvm/Support/GraphWriter.h"
  46. #include "llvm/Support/raw_ostream.h"
  47. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  48. #include "llvm/Transforms/Utils/Cloning.h"
  49. #include "llvm/Transforms/Utils/Local.h"
  50. #include <cassert>
  51. #include <string>
  52. using namespace llvm;
  53. #define DEBUG_TYPE "attributor"
  54. DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",
  55. "Determine what attributes are manifested in the IR");
  56. STATISTIC(NumFnDeleted, "Number of function deleted");
  57. STATISTIC(NumFnWithExactDefinition,
  58. "Number of functions with exact definitions");
  59. STATISTIC(NumFnWithoutExactDefinition,
  60. "Number of functions without exact definitions");
  61. STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created");
  62. STATISTIC(NumAttributesTimedOut,
  63. "Number of abstract attributes timed out before fixpoint");
  64. STATISTIC(NumAttributesValidFixpoint,
  65. "Number of abstract attributes in a valid fixpoint state");
  66. STATISTIC(NumAttributesManifested,
  67. "Number of abstract attributes manifested in IR");
  68. // TODO: Determine a good default value.
  69. //
  70. // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
  71. // (when run with the first 5 abstract attributes). The results also indicate
  72. // that we never reach 32 iterations but always find a fixpoint sooner.
  73. //
  74. // This will become more evolved once we perform two interleaved fixpoint
  75. // iterations: bottom-up and top-down.
  76. static cl::opt<unsigned>
  77. SetFixpointIterations("attributor-max-iterations", cl::Hidden,
  78. cl::desc("Maximal number of fixpoint iterations."),
  79. cl::init(32));
  80. static cl::opt<unsigned, true> MaxInitializationChainLengthX(
  81. "attributor-max-initialization-chain-length", cl::Hidden,
  82. cl::desc(
  83. "Maximal number of chained initializations (to avoid stack overflows)"),
  84. cl::location(MaxInitializationChainLength), cl::init(1024));
  85. unsigned llvm::MaxInitializationChainLength;
  86. static cl::opt<bool> VerifyMaxFixpointIterations(
  87. "attributor-max-iterations-verify", cl::Hidden,
  88. cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
  89. cl::init(false));
  90. static cl::opt<bool> AnnotateDeclarationCallSites(
  91. "attributor-annotate-decl-cs", cl::Hidden,
  92. cl::desc("Annotate call sites of function declarations."), cl::init(false));
  93. static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
  94. cl::init(true), cl::Hidden);
  95. static cl::opt<bool>
  96. AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden,
  97. cl::desc("Allow the Attributor to create shallow "
  98. "wrappers for non-exact definitions."),
  99. cl::init(false));
  100. static cl::opt<bool>
  101. AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden,
  102. cl::desc("Allow the Attributor to use IP information "
  103. "derived from non-exact functions via cloning"),
  104. cl::init(false));
  105. // These options can only used for debug builds.
  106. #ifndef NDEBUG
  107. static cl::list<std::string>
  108. SeedAllowList("attributor-seed-allow-list", cl::Hidden,
  109. cl::desc("Comma seperated list of attribute names that are "
  110. "allowed to be seeded."),
  111. cl::ZeroOrMore, cl::CommaSeparated);
  112. static cl::list<std::string> FunctionSeedAllowList(
  113. "attributor-function-seed-allow-list", cl::Hidden,
  114. cl::desc("Comma seperated list of function names that are "
  115. "allowed to be seeded."),
  116. cl::ZeroOrMore, cl::CommaSeparated);
  117. #endif
  118. static cl::opt<bool>
  119. DumpDepGraph("attributor-dump-dep-graph", cl::Hidden,
  120. cl::desc("Dump the dependency graph to dot files."),
  121. cl::init(false));
  122. static cl::opt<std::string> DepGraphDotFileNamePrefix(
  123. "attributor-depgraph-dot-filename-prefix", cl::Hidden,
  124. cl::desc("The prefix used for the CallGraph dot file names."));
  125. static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
  126. cl::desc("View the dependency graph."),
  127. cl::init(false));
  128. static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
  129. cl::desc("Print attribute dependencies"),
  130. cl::init(false));
  131. static cl::opt<bool> EnableCallSiteSpecific(
  132. "attributor-enable-call-site-specific-deduction", cl::Hidden,
  133. cl::desc("Allow the Attributor to do call site specific analysis"),
  134. cl::init(false));
  135. static cl::opt<bool>
  136. PrintCallGraph("attributor-print-call-graph", cl::Hidden,
  137. cl::desc("Print Attributor's internal call graph"),
  138. cl::init(false));
  139. static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads",
  140. cl::Hidden,
  141. cl::desc("Try to simplify all loads."),
  142. cl::init(true));
  143. /// Logic operators for the change status enum class.
  144. ///
  145. ///{
  146. ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) {
  147. return L == ChangeStatus::CHANGED ? L : R;
  148. }
  149. ChangeStatus &llvm::operator|=(ChangeStatus &L, ChangeStatus R) {
  150. L = L | R;
  151. return L;
  152. }
  153. ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) {
  154. return L == ChangeStatus::UNCHANGED ? L : R;
  155. }
  156. ChangeStatus &llvm::operator&=(ChangeStatus &L, ChangeStatus R) {
  157. L = L & R;
  158. return L;
  159. }
  160. ///}
  161. bool AA::isNoSyncInst(Attributor &A, const Instruction &I,
  162. const AbstractAttribute &QueryingAA) {
  163. // We are looking for volatile instructions or non-relaxed atomics.
  164. if (const auto *CB = dyn_cast<CallBase>(&I)) {
  165. if (CB->hasFnAttr(Attribute::NoSync))
  166. return true;
  167. // Non-convergent and readnone imply nosync.
  168. if (!CB->isConvergent() && !CB->mayReadOrWriteMemory())
  169. return true;
  170. if (AANoSync::isNoSyncIntrinsic(&I))
  171. return true;
  172. const auto &NoSyncAA = A.getAAFor<AANoSync>(
  173. QueryingAA, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  174. return NoSyncAA.isAssumedNoSync();
  175. }
  176. if (!I.mayReadOrWriteMemory())
  177. return true;
  178. return !I.isVolatile() && !AANoSync::isNonRelaxedAtomic(&I);
  179. }
  180. bool AA::isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
  181. const Value &V) {
  182. if (auto *C = dyn_cast<Constant>(&V))
  183. return !C->isThreadDependent();
  184. // TODO: Inspect and cache more complex instructions.
  185. if (auto *CB = dyn_cast<CallBase>(&V))
  186. return CB->getNumOperands() == 0 && !CB->mayHaveSideEffects() &&
  187. !CB->mayReadFromMemory();
  188. const Function *Scope = nullptr;
  189. if (auto *I = dyn_cast<Instruction>(&V))
  190. Scope = I->getFunction();
  191. if (auto *A = dyn_cast<Argument>(&V))
  192. Scope = A->getParent();
  193. if (!Scope)
  194. return false;
  195. auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  196. QueryingAA, IRPosition::function(*Scope), DepClassTy::OPTIONAL);
  197. return NoRecurseAA.isAssumedNoRecurse();
  198. }
  199. Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty,
  200. const TargetLibraryInfo *TLI) {
  201. if (isa<AllocaInst>(Obj))
  202. return UndefValue::get(&Ty);
  203. if (isAllocationFn(&Obj, TLI))
  204. return getInitialValueOfAllocation(&cast<CallBase>(Obj), TLI, &Ty);
  205. auto *GV = dyn_cast<GlobalVariable>(&Obj);
  206. if (!GV || !GV->hasLocalLinkage())
  207. return nullptr;
  208. if (!GV->hasInitializer())
  209. return UndefValue::get(&Ty);
  210. return dyn_cast_or_null<Constant>(getWithType(*GV->getInitializer(), Ty));
  211. }
  212. bool AA::isValidInScope(const Value &V, const Function *Scope) {
  213. if (isa<Constant>(V))
  214. return true;
  215. if (auto *I = dyn_cast<Instruction>(&V))
  216. return I->getFunction() == Scope;
  217. if (auto *A = dyn_cast<Argument>(&V))
  218. return A->getParent() == Scope;
  219. return false;
  220. }
  221. bool AA::isValidAtPosition(const Value &V, const Instruction &CtxI,
  222. InformationCache &InfoCache) {
  223. if (isa<Constant>(V))
  224. return true;
  225. const Function *Scope = CtxI.getFunction();
  226. if (auto *A = dyn_cast<Argument>(&V))
  227. return A->getParent() == Scope;
  228. if (auto *I = dyn_cast<Instruction>(&V))
  229. if (I->getFunction() == Scope) {
  230. const DominatorTree *DT =
  231. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Scope);
  232. return DT && DT->dominates(I, &CtxI);
  233. }
  234. return false;
  235. }
  236. Value *AA::getWithType(Value &V, Type &Ty) {
  237. if (V.getType() == &Ty)
  238. return &V;
  239. if (isa<PoisonValue>(V))
  240. return PoisonValue::get(&Ty);
  241. if (isa<UndefValue>(V))
  242. return UndefValue::get(&Ty);
  243. if (auto *C = dyn_cast<Constant>(&V)) {
  244. if (C->isNullValue())
  245. return Constant::getNullValue(&Ty);
  246. if (C->getType()->isPointerTy() && Ty.isPointerTy())
  247. return ConstantExpr::getPointerCast(C, &Ty);
  248. if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
  249. if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
  250. return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
  251. if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
  252. return ConstantExpr::getFPTrunc(C, &Ty, /* OnlyIfReduced */ true);
  253. }
  254. }
  255. return nullptr;
  256. }
  257. Optional<Value *>
  258. AA::combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
  259. const Optional<Value *> &B, Type *Ty) {
  260. if (A == B)
  261. return A;
  262. if (!B.hasValue())
  263. return A;
  264. if (*B == nullptr)
  265. return nullptr;
  266. if (!A.hasValue())
  267. return Ty ? getWithType(**B, *Ty) : nullptr;
  268. if (*A == nullptr)
  269. return nullptr;
  270. if (!Ty)
  271. Ty = (*A)->getType();
  272. if (isa_and_nonnull<UndefValue>(*A))
  273. return getWithType(**B, *Ty);
  274. if (isa<UndefValue>(*B))
  275. return A;
  276. if (*A && *B && *A == getWithType(**B, *Ty))
  277. return A;
  278. return nullptr;
  279. }
  280. bool AA::getPotentialCopiesOfStoredValue(
  281. Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
  282. const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation) {
  283. Value &Ptr = *SI.getPointerOperand();
  284. SmallVector<Value *, 8> Objects;
  285. if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, QueryingAA, &SI,
  286. UsedAssumedInformation)) {
  287. LLVM_DEBUG(
  288. dbgs() << "Underlying objects stored into could not be determined\n";);
  289. return false;
  290. }
  291. SmallVector<const AAPointerInfo *> PIs;
  292. SmallVector<Value *> NewCopies;
  293. for (Value *Obj : Objects) {
  294. LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n");
  295. if (isa<UndefValue>(Obj))
  296. continue;
  297. if (isa<ConstantPointerNull>(Obj)) {
  298. // A null pointer access can be undefined but any offset from null may
  299. // be OK. We do not try to optimize the latter.
  300. if (!NullPointerIsDefined(SI.getFunction(),
  301. Ptr.getType()->getPointerAddressSpace()) &&
  302. A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation) ==
  303. Obj)
  304. continue;
  305. LLVM_DEBUG(
  306. dbgs() << "Underlying object is a valid nullptr, giving up.\n";);
  307. return false;
  308. }
  309. if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj) &&
  310. !isNoAliasCall(Obj)) {
  311. LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << *Obj
  312. << "\n";);
  313. return false;
  314. }
  315. if (auto *GV = dyn_cast<GlobalVariable>(Obj))
  316. if (!GV->hasLocalLinkage()) {
  317. LLVM_DEBUG(dbgs() << "Underlying object is global with external "
  318. "linkage, not supported yet: "
  319. << *Obj << "\n";);
  320. return false;
  321. }
  322. auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
  323. if (!Acc.isRead())
  324. return true;
  325. auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
  326. if (!LI) {
  327. LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "
  328. "instruction not supported yet: "
  329. << *Acc.getRemoteInst() << "\n";);
  330. return false;
  331. }
  332. NewCopies.push_back(LI);
  333. return true;
  334. };
  335. auto &PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(*Obj),
  336. DepClassTy::NONE);
  337. if (!PI.forallInterferingAccesses(SI, CheckAccess)) {
  338. LLVM_DEBUG(
  339. dbgs()
  340. << "Failed to verify all interfering accesses for underlying object: "
  341. << *Obj << "\n");
  342. return false;
  343. }
  344. PIs.push_back(&PI);
  345. }
  346. for (auto *PI : PIs) {
  347. if (!PI->getState().isAtFixpoint())
  348. UsedAssumedInformation = true;
  349. A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
  350. }
  351. PotentialCopies.insert(NewCopies.begin(), NewCopies.end());
  352. return true;
  353. }
  354. static bool isAssumedReadOnlyOrReadNone(Attributor &A, const IRPosition &IRP,
  355. const AbstractAttribute &QueryingAA,
  356. bool RequireReadNone, bool &IsKnown) {
  357. IRPosition::Kind Kind = IRP.getPositionKind();
  358. if (Kind == IRPosition::IRP_FUNCTION || Kind == IRPosition::IRP_CALL_SITE) {
  359. const auto &MemLocAA =
  360. A.getAAFor<AAMemoryLocation>(QueryingAA, IRP, DepClassTy::NONE);
  361. if (MemLocAA.isAssumedReadNone()) {
  362. IsKnown = MemLocAA.isKnownReadNone();
  363. if (!IsKnown)
  364. A.recordDependence(MemLocAA, QueryingAA, DepClassTy::OPTIONAL);
  365. return true;
  366. }
  367. }
  368. const auto &MemBehaviorAA =
  369. A.getAAFor<AAMemoryBehavior>(QueryingAA, IRP, DepClassTy::NONE);
  370. if (MemBehaviorAA.isAssumedReadNone() ||
  371. (!RequireReadNone && MemBehaviorAA.isAssumedReadOnly())) {
  372. IsKnown = RequireReadNone ? MemBehaviorAA.isKnownReadNone()
  373. : MemBehaviorAA.isKnownReadOnly();
  374. if (!IsKnown)
  375. A.recordDependence(MemBehaviorAA, QueryingAA, DepClassTy::OPTIONAL);
  376. return true;
  377. }
  378. return false;
  379. }
  380. bool AA::isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
  381. const AbstractAttribute &QueryingAA, bool &IsKnown) {
  382. return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
  383. /* RequireReadNone */ false, IsKnown);
  384. }
  385. bool AA::isAssumedReadNone(Attributor &A, const IRPosition &IRP,
  386. const AbstractAttribute &QueryingAA, bool &IsKnown) {
  387. return isAssumedReadOnlyOrReadNone(A, IRP, QueryingAA,
  388. /* RequireReadNone */ true, IsKnown);
  389. }
  390. static bool
  391. isPotentiallyReachable(Attributor &A, const Instruction &FromI,
  392. const Instruction *ToI, const Function &ToFn,
  393. const AbstractAttribute &QueryingAA,
  394. std::function<bool(const Function &F)> GoBackwardsCB) {
  395. LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable @" << ToFn.getName()
  396. << " from " << FromI << " [GBCB: " << bool(GoBackwardsCB)
  397. << "]\n");
  398. SmallPtrSet<const Instruction *, 8> Visited;
  399. SmallVector<const Instruction *> Worklist;
  400. Worklist.push_back(&FromI);
  401. while (!Worklist.empty()) {
  402. const Instruction *CurFromI = Worklist.pop_back_val();
  403. if (!Visited.insert(CurFromI).second)
  404. continue;
  405. const Function *FromFn = CurFromI->getFunction();
  406. if (FromFn == &ToFn) {
  407. if (!ToI)
  408. return true;
  409. LLVM_DEBUG(dbgs() << "[AA] check " << *ToI << " from " << *CurFromI
  410. << " intraprocedurally\n");
  411. const auto &ReachabilityAA = A.getAAFor<AAReachability>(
  412. QueryingAA, IRPosition::function(ToFn), DepClassTy::OPTIONAL);
  413. bool Result = ReachabilityAA.isAssumedReachable(A, *CurFromI, *ToI);
  414. LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " "
  415. << (Result ? "can potentially " : "cannot ") << "reach "
  416. << *ToI << " [Intra]\n");
  417. if (Result)
  418. return true;
  419. continue;
  420. }
  421. // TODO: If we can go arbitrarily backwards we will eventually reach an
  422. // entry point that can reach ToI. Only once this takes a set of blocks
  423. // through which we cannot go, or once we track internal functions not
  424. // accessible from the outside, it makes sense to perform backwards analysis
  425. // in the absence of a GoBackwardsCB.
  426. if (!GoBackwardsCB) {
  427. LLVM_DEBUG(dbgs() << "[AA] check @" << ToFn.getName() << " from "
  428. << *CurFromI << " is not checked backwards, abort\n");
  429. return true;
  430. }
  431. // Check if the current instruction is already known to reach the ToFn.
  432. const auto &FnReachabilityAA = A.getAAFor<AAFunctionReachability>(
  433. QueryingAA, IRPosition::function(*FromFn), DepClassTy::OPTIONAL);
  434. bool Result = FnReachabilityAA.instructionCanReach(
  435. A, *CurFromI, ToFn, /* UseBackwards */ false);
  436. LLVM_DEBUG(dbgs() << "[AA] " << *CurFromI << " in @" << FromFn->getName()
  437. << " " << (Result ? "can potentially " : "cannot ")
  438. << "reach @" << ToFn.getName() << " [FromFn]\n");
  439. if (Result)
  440. return true;
  441. // If we do not go backwards from the FromFn we are done here and so far we
  442. // could not find a way to reach ToFn/ToI.
  443. if (!GoBackwardsCB(*FromFn))
  444. continue;
  445. LLVM_DEBUG(dbgs() << "Stepping backwards to the call sites of @"
  446. << FromFn->getName() << "\n");
  447. auto CheckCallSite = [&](AbstractCallSite ACS) {
  448. CallBase *CB = ACS.getInstruction();
  449. if (!CB)
  450. return false;
  451. if (isa<InvokeInst>(CB))
  452. return false;
  453. Instruction *Inst = CB->getNextNonDebugInstruction();
  454. Worklist.push_back(Inst);
  455. return true;
  456. };
  457. bool UsedAssumedInformation = false;
  458. Result = !A.checkForAllCallSites(CheckCallSite, *FromFn,
  459. /* RequireAllCallSites */ true,
  460. &QueryingAA, UsedAssumedInformation);
  461. if (Result) {
  462. LLVM_DEBUG(dbgs() << "[AA] stepping back to call sites from " << *CurFromI
  463. << " in @" << FromFn->getName()
  464. << " failed, give up\n");
  465. return true;
  466. }
  467. LLVM_DEBUG(dbgs() << "[AA] stepped back to call sites from " << *CurFromI
  468. << " in @" << FromFn->getName()
  469. << " worklist size is: " << Worklist.size() << "\n");
  470. }
  471. return false;
  472. }
  473. bool AA::isPotentiallyReachable(
  474. Attributor &A, const Instruction &FromI, const Instruction &ToI,
  475. const AbstractAttribute &QueryingAA,
  476. std::function<bool(const Function &F)> GoBackwardsCB) {
  477. LLVM_DEBUG(dbgs() << "[AA] isPotentiallyReachable " << ToI << " from "
  478. << FromI << " [GBCB: " << bool(GoBackwardsCB) << "]\n");
  479. const Function *ToFn = ToI.getFunction();
  480. return ::isPotentiallyReachable(A, FromI, &ToI, *ToFn, QueryingAA,
  481. GoBackwardsCB);
  482. }
  483. bool AA::isPotentiallyReachable(
  484. Attributor &A, const Instruction &FromI, const Function &ToFn,
  485. const AbstractAttribute &QueryingAA,
  486. std::function<bool(const Function &F)> GoBackwardsCB) {
  487. return ::isPotentiallyReachable(A, FromI, /* ToI */ nullptr, ToFn, QueryingAA,
  488. GoBackwardsCB);
  489. }
  490. /// Return true if \p New is equal or worse than \p Old.
  491. static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
  492. if (!Old.isIntAttribute())
  493. return true;
  494. return Old.getValueAsInt() >= New.getValueAsInt();
  495. }
  496. /// Return true if the information provided by \p Attr was added to the
  497. /// attribute list \p Attrs. This is only the case if it was not already present
  498. /// in \p Attrs at the position describe by \p PK and \p AttrIdx.
  499. static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
  500. AttributeList &Attrs, int AttrIdx,
  501. bool ForceReplace = false) {
  502. if (Attr.isEnumAttribute()) {
  503. Attribute::AttrKind Kind = Attr.getKindAsEnum();
  504. if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
  505. if (!ForceReplace &&
  506. isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
  507. return false;
  508. Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
  509. return true;
  510. }
  511. if (Attr.isStringAttribute()) {
  512. StringRef Kind = Attr.getKindAsString();
  513. if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
  514. if (!ForceReplace &&
  515. isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
  516. return false;
  517. Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
  518. return true;
  519. }
  520. if (Attr.isIntAttribute()) {
  521. Attribute::AttrKind Kind = Attr.getKindAsEnum();
  522. if (Attrs.hasAttributeAtIndex(AttrIdx, Kind))
  523. if (!ForceReplace &&
  524. isEqualOrWorse(Attr, Attrs.getAttributeAtIndex(AttrIdx, Kind)))
  525. return false;
  526. Attrs = Attrs.removeAttributeAtIndex(Ctx, AttrIdx, Kind);
  527. Attrs = Attrs.addAttributeAtIndex(Ctx, AttrIdx, Attr);
  528. return true;
  529. }
  530. llvm_unreachable("Expected enum or string attribute!");
  531. }
  532. Argument *IRPosition::getAssociatedArgument() const {
  533. if (getPositionKind() == IRP_ARGUMENT)
  534. return cast<Argument>(&getAnchorValue());
  535. // Not an Argument and no argument number means this is not a call site
  536. // argument, thus we cannot find a callback argument to return.
  537. int ArgNo = getCallSiteArgNo();
  538. if (ArgNo < 0)
  539. return nullptr;
  540. // Use abstract call sites to make the connection between the call site
  541. // values and the ones in callbacks. If a callback was found that makes use
  542. // of the underlying call site operand, we want the corresponding callback
  543. // callee argument and not the direct callee argument.
  544. Optional<Argument *> CBCandidateArg;
  545. SmallVector<const Use *, 4> CallbackUses;
  546. const auto &CB = cast<CallBase>(getAnchorValue());
  547. AbstractCallSite::getCallbackUses(CB, CallbackUses);
  548. for (const Use *U : CallbackUses) {
  549. AbstractCallSite ACS(U);
  550. assert(ACS && ACS.isCallbackCall());
  551. if (!ACS.getCalledFunction())
  552. continue;
  553. for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
  554. // Test if the underlying call site operand is argument number u of the
  555. // callback callee.
  556. if (ACS.getCallArgOperandNo(u) != ArgNo)
  557. continue;
  558. assert(ACS.getCalledFunction()->arg_size() > u &&
  559. "ACS mapped into var-args arguments!");
  560. if (CBCandidateArg.hasValue()) {
  561. CBCandidateArg = nullptr;
  562. break;
  563. }
  564. CBCandidateArg = ACS.getCalledFunction()->getArg(u);
  565. }
  566. }
  567. // If we found a unique callback candidate argument, return it.
  568. if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
  569. return CBCandidateArg.getValue();
  570. // If no callbacks were found, or none used the underlying call site operand
  571. // exclusively, use the direct callee argument if available.
  572. const Function *Callee = CB.getCalledFunction();
  573. if (Callee && Callee->arg_size() > unsigned(ArgNo))
  574. return Callee->getArg(ArgNo);
  575. return nullptr;
  576. }
  577. ChangeStatus AbstractAttribute::update(Attributor &A) {
  578. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  579. if (getState().isAtFixpoint())
  580. return HasChanged;
  581. LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
  582. HasChanged = updateImpl(A);
  583. LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
  584. << "\n");
  585. return HasChanged;
  586. }
  587. ChangeStatus
  588. IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
  589. const ArrayRef<Attribute> &DeducedAttrs,
  590. bool ForceReplace) {
  591. Function *ScopeFn = IRP.getAnchorScope();
  592. IRPosition::Kind PK = IRP.getPositionKind();
  593. // In the following some generic code that will manifest attributes in
  594. // DeducedAttrs if they improve the current IR. Due to the different
  595. // annotation positions we use the underlying AttributeList interface.
  596. AttributeList Attrs;
  597. switch (PK) {
  598. case IRPosition::IRP_INVALID:
  599. case IRPosition::IRP_FLOAT:
  600. return ChangeStatus::UNCHANGED;
  601. case IRPosition::IRP_ARGUMENT:
  602. case IRPosition::IRP_FUNCTION:
  603. case IRPosition::IRP_RETURNED:
  604. Attrs = ScopeFn->getAttributes();
  605. break;
  606. case IRPosition::IRP_CALL_SITE:
  607. case IRPosition::IRP_CALL_SITE_RETURNED:
  608. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  609. Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes();
  610. break;
  611. }
  612. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  613. LLVMContext &Ctx = IRP.getAnchorValue().getContext();
  614. for (const Attribute &Attr : DeducedAttrs) {
  615. if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx(), ForceReplace))
  616. continue;
  617. HasChanged = ChangeStatus::CHANGED;
  618. }
  619. if (HasChanged == ChangeStatus::UNCHANGED)
  620. return HasChanged;
  621. switch (PK) {
  622. case IRPosition::IRP_ARGUMENT:
  623. case IRPosition::IRP_FUNCTION:
  624. case IRPosition::IRP_RETURNED:
  625. ScopeFn->setAttributes(Attrs);
  626. break;
  627. case IRPosition::IRP_CALL_SITE:
  628. case IRPosition::IRP_CALL_SITE_RETURNED:
  629. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  630. cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs);
  631. break;
  632. case IRPosition::IRP_INVALID:
  633. case IRPosition::IRP_FLOAT:
  634. break;
  635. }
  636. return HasChanged;
  637. }
  638. const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey());
  639. const IRPosition
  640. IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey());
  641. SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
  642. IRPositions.emplace_back(IRP);
  643. // Helper to determine if operand bundles on a call site are benin or
  644. // potentially problematic. We handle only llvm.assume for now.
  645. auto CanIgnoreOperandBundles = [](const CallBase &CB) {
  646. return (isa<IntrinsicInst>(CB) &&
  647. cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
  648. };
  649. const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
  650. switch (IRP.getPositionKind()) {
  651. case IRPosition::IRP_INVALID:
  652. case IRPosition::IRP_FLOAT:
  653. case IRPosition::IRP_FUNCTION:
  654. return;
  655. case IRPosition::IRP_ARGUMENT:
  656. case IRPosition::IRP_RETURNED:
  657. IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
  658. return;
  659. case IRPosition::IRP_CALL_SITE:
  660. assert(CB && "Expected call site!");
  661. // TODO: We need to look at the operand bundles similar to the redirection
  662. // in CallBase.
  663. if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
  664. if (const Function *Callee = CB->getCalledFunction())
  665. IRPositions.emplace_back(IRPosition::function(*Callee));
  666. return;
  667. case IRPosition::IRP_CALL_SITE_RETURNED:
  668. assert(CB && "Expected call site!");
  669. // TODO: We need to look at the operand bundles similar to the redirection
  670. // in CallBase.
  671. if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
  672. if (const Function *Callee = CB->getCalledFunction()) {
  673. IRPositions.emplace_back(IRPosition::returned(*Callee));
  674. IRPositions.emplace_back(IRPosition::function(*Callee));
  675. for (const Argument &Arg : Callee->args())
  676. if (Arg.hasReturnedAttr()) {
  677. IRPositions.emplace_back(
  678. IRPosition::callsite_argument(*CB, Arg.getArgNo()));
  679. IRPositions.emplace_back(
  680. IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
  681. IRPositions.emplace_back(IRPosition::argument(Arg));
  682. }
  683. }
  684. }
  685. IRPositions.emplace_back(IRPosition::callsite_function(*CB));
  686. return;
  687. case IRPosition::IRP_CALL_SITE_ARGUMENT: {
  688. assert(CB && "Expected call site!");
  689. // TODO: We need to look at the operand bundles similar to the redirection
  690. // in CallBase.
  691. if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
  692. const Function *Callee = CB->getCalledFunction();
  693. if (Callee) {
  694. if (Argument *Arg = IRP.getAssociatedArgument())
  695. IRPositions.emplace_back(IRPosition::argument(*Arg));
  696. IRPositions.emplace_back(IRPosition::function(*Callee));
  697. }
  698. }
  699. IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
  700. return;
  701. }
  702. }
  703. }
  704. bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
  705. bool IgnoreSubsumingPositions, Attributor *A) const {
  706. SmallVector<Attribute, 4> Attrs;
  707. for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
  708. for (Attribute::AttrKind AK : AKs)
  709. if (EquivIRP.getAttrsFromIRAttr(AK, Attrs))
  710. return true;
  711. // The first position returned by the SubsumingPositionIterator is
  712. // always the position itself. If we ignore subsuming positions we
  713. // are done after the first iteration.
  714. if (IgnoreSubsumingPositions)
  715. break;
  716. }
  717. if (A)
  718. for (Attribute::AttrKind AK : AKs)
  719. if (getAttrsFromAssumes(AK, Attrs, *A))
  720. return true;
  721. return false;
  722. }
  723. void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
  724. SmallVectorImpl<Attribute> &Attrs,
  725. bool IgnoreSubsumingPositions, Attributor *A) const {
  726. for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
  727. for (Attribute::AttrKind AK : AKs)
  728. EquivIRP.getAttrsFromIRAttr(AK, Attrs);
  729. // The first position returned by the SubsumingPositionIterator is
  730. // always the position itself. If we ignore subsuming positions we
  731. // are done after the first iteration.
  732. if (IgnoreSubsumingPositions)
  733. break;
  734. }
  735. if (A)
  736. for (Attribute::AttrKind AK : AKs)
  737. getAttrsFromAssumes(AK, Attrs, *A);
  738. }
  739. bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
  740. SmallVectorImpl<Attribute> &Attrs) const {
  741. if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
  742. return false;
  743. AttributeList AttrList;
  744. if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
  745. AttrList = CB->getAttributes();
  746. else
  747. AttrList = getAssociatedFunction()->getAttributes();
  748. bool HasAttr = AttrList.hasAttributeAtIndex(getAttrIdx(), AK);
  749. if (HasAttr)
  750. Attrs.push_back(AttrList.getAttributeAtIndex(getAttrIdx(), AK));
  751. return HasAttr;
  752. }
  753. bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK,
  754. SmallVectorImpl<Attribute> &Attrs,
  755. Attributor &A) const {
  756. assert(getPositionKind() != IRP_INVALID && "Did expect a valid position!");
  757. Value &AssociatedValue = getAssociatedValue();
  758. const Assume2KnowledgeMap &A2K =
  759. A.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
  760. // Check if we found any potential assume use, if not we don't need to create
  761. // explorer iterators.
  762. if (A2K.empty())
  763. return false;
  764. LLVMContext &Ctx = AssociatedValue.getContext();
  765. unsigned AttrsSize = Attrs.size();
  766. MustBeExecutedContextExplorer &Explorer =
  767. A.getInfoCache().getMustBeExecutedContextExplorer();
  768. auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI());
  769. for (auto &It : A2K)
  770. if (Explorer.findInContextOf(It.first, EIt, EEnd))
  771. Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
  772. return AttrsSize != Attrs.size();
  773. }
  774. void IRPosition::verify() {
  775. #ifdef EXPENSIVE_CHECKS
  776. switch (getPositionKind()) {
  777. case IRP_INVALID:
  778. assert((CBContext == nullptr) &&
  779. "Invalid position must not have CallBaseContext!");
  780. assert(!Enc.getOpaqueValue() &&
  781. "Expected a nullptr for an invalid position!");
  782. return;
  783. case IRP_FLOAT:
  784. assert((!isa<Argument>(&getAssociatedValue())) &&
  785. "Expected specialized kind for argument values!");
  786. return;
  787. case IRP_RETURNED:
  788. assert(isa<Function>(getAsValuePtr()) &&
  789. "Expected function for a 'returned' position!");
  790. assert(getAsValuePtr() == &getAssociatedValue() &&
  791. "Associated value mismatch!");
  792. return;
  793. case IRP_CALL_SITE_RETURNED:
  794. assert((CBContext == nullptr) &&
  795. "'call site returned' position must not have CallBaseContext!");
  796. assert((isa<CallBase>(getAsValuePtr())) &&
  797. "Expected call base for 'call site returned' position!");
  798. assert(getAsValuePtr() == &getAssociatedValue() &&
  799. "Associated value mismatch!");
  800. return;
  801. case IRP_CALL_SITE:
  802. assert((CBContext == nullptr) &&
  803. "'call site function' position must not have CallBaseContext!");
  804. assert((isa<CallBase>(getAsValuePtr())) &&
  805. "Expected call base for 'call site function' position!");
  806. assert(getAsValuePtr() == &getAssociatedValue() &&
  807. "Associated value mismatch!");
  808. return;
  809. case IRP_FUNCTION:
  810. assert(isa<Function>(getAsValuePtr()) &&
  811. "Expected function for a 'function' position!");
  812. assert(getAsValuePtr() == &getAssociatedValue() &&
  813. "Associated value mismatch!");
  814. return;
  815. case IRP_ARGUMENT:
  816. assert(isa<Argument>(getAsValuePtr()) &&
  817. "Expected argument for a 'argument' position!");
  818. assert(getAsValuePtr() == &getAssociatedValue() &&
  819. "Associated value mismatch!");
  820. return;
  821. case IRP_CALL_SITE_ARGUMENT: {
  822. assert((CBContext == nullptr) &&
  823. "'call site argument' position must not have CallBaseContext!");
  824. Use *U = getAsUsePtr();
  825. (void)U; // Silence unused variable warning.
  826. assert(U && "Expected use for a 'call site argument' position!");
  827. assert(isa<CallBase>(U->getUser()) &&
  828. "Expected call base user for a 'call site argument' position!");
  829. assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&
  830. "Expected call base argument operand for a 'call site argument' "
  831. "position");
  832. assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==
  833. unsigned(getCallSiteArgNo()) &&
  834. "Argument number mismatch!");
  835. assert(U->get() == &getAssociatedValue() && "Associated value mismatch!");
  836. return;
  837. }
  838. }
  839. #endif
  840. }
  841. Optional<Constant *>
  842. Attributor::getAssumedConstant(const IRPosition &IRP,
  843. const AbstractAttribute &AA,
  844. bool &UsedAssumedInformation) {
  845. // First check all callbacks provided by outside AAs. If any of them returns
  846. // a non-null value that is different from the associated value, or None, we
  847. // assume it's simpliied.
  848. for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
  849. Optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
  850. if (!SimplifiedV.hasValue())
  851. return llvm::None;
  852. if (isa_and_nonnull<Constant>(*SimplifiedV))
  853. return cast<Constant>(*SimplifiedV);
  854. return nullptr;
  855. }
  856. const auto &ValueSimplifyAA =
  857. getAAFor<AAValueSimplify>(AA, IRP, DepClassTy::NONE);
  858. Optional<Value *> SimplifiedV =
  859. ValueSimplifyAA.getAssumedSimplifiedValue(*this);
  860. bool IsKnown = ValueSimplifyAA.isAtFixpoint();
  861. UsedAssumedInformation |= !IsKnown;
  862. if (!SimplifiedV.hasValue()) {
  863. recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
  864. return llvm::None;
  865. }
  866. if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
  867. recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
  868. return UndefValue::get(IRP.getAssociatedType());
  869. }
  870. Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue());
  871. if (CI)
  872. CI = dyn_cast_or_null<Constant>(
  873. AA::getWithType(*CI, *IRP.getAssociatedType()));
  874. if (CI)
  875. recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
  876. return CI;
  877. }
  878. Optional<Value *>
  879. Attributor::getAssumedSimplified(const IRPosition &IRP,
  880. const AbstractAttribute *AA,
  881. bool &UsedAssumedInformation) {
  882. // First check all callbacks provided by outside AAs. If any of them returns
  883. // a non-null value that is different from the associated value, or None, we
  884. // assume it's simpliied.
  885. for (auto &CB : SimplificationCallbacks.lookup(IRP))
  886. return CB(IRP, AA, UsedAssumedInformation);
  887. // If no high-level/outside simplification occured, use AAValueSimplify.
  888. const auto &ValueSimplifyAA =
  889. getOrCreateAAFor<AAValueSimplify>(IRP, AA, DepClassTy::NONE);
  890. Optional<Value *> SimplifiedV =
  891. ValueSimplifyAA.getAssumedSimplifiedValue(*this);
  892. bool IsKnown = ValueSimplifyAA.isAtFixpoint();
  893. UsedAssumedInformation |= !IsKnown;
  894. if (!SimplifiedV.hasValue()) {
  895. if (AA)
  896. recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL);
  897. return llvm::None;
  898. }
  899. if (*SimplifiedV == nullptr)
  900. return const_cast<Value *>(&IRP.getAssociatedValue());
  901. if (Value *SimpleV =
  902. AA::getWithType(**SimplifiedV, *IRP.getAssociatedType())) {
  903. if (AA)
  904. recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL);
  905. return SimpleV;
  906. }
  907. return const_cast<Value *>(&IRP.getAssociatedValue());
  908. }
  909. Optional<Value *> Attributor::translateArgumentToCallSiteContent(
  910. Optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
  911. bool &UsedAssumedInformation) {
  912. if (!V.hasValue())
  913. return V;
  914. if (*V == nullptr || isa<Constant>(*V))
  915. return V;
  916. if (auto *Arg = dyn_cast<Argument>(*V))
  917. if (CB.getCalledFunction() == Arg->getParent())
  918. if (!Arg->hasPointeeInMemoryValueAttr())
  919. return getAssumedSimplified(
  920. IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
  921. UsedAssumedInformation);
  922. return nullptr;
  923. }
  924. Attributor::~Attributor() {
  925. // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
  926. // thus we cannot delete them. We can, and want to, destruct them though.
  927. for (auto &DepAA : DG.SyntheticRoot.Deps) {
  928. AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
  929. AA->~AbstractAttribute();
  930. }
  931. }
  932. bool Attributor::isAssumedDead(const AbstractAttribute &AA,
  933. const AAIsDead *FnLivenessAA,
  934. bool &UsedAssumedInformation,
  935. bool CheckBBLivenessOnly, DepClassTy DepClass) {
  936. const IRPosition &IRP = AA.getIRPosition();
  937. if (!Functions.count(IRP.getAnchorScope()))
  938. return false;
  939. return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
  940. CheckBBLivenessOnly, DepClass);
  941. }
  942. bool Attributor::isAssumedDead(const Use &U,
  943. const AbstractAttribute *QueryingAA,
  944. const AAIsDead *FnLivenessAA,
  945. bool &UsedAssumedInformation,
  946. bool CheckBBLivenessOnly, DepClassTy DepClass) {
  947. Instruction *UserI = dyn_cast<Instruction>(U.getUser());
  948. if (!UserI)
  949. return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
  950. UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
  951. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  952. // For call site argument uses we can check if the argument is
  953. // unused/dead.
  954. if (CB->isArgOperand(&U)) {
  955. const IRPosition &CSArgPos =
  956. IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
  957. return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
  958. UsedAssumedInformation, CheckBBLivenessOnly,
  959. DepClass);
  960. }
  961. } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
  962. const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
  963. return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
  964. UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
  965. } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
  966. BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
  967. return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
  968. UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
  969. }
  970. return isAssumedDead(IRPosition::inst(*UserI), QueryingAA, FnLivenessAA,
  971. UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
  972. }
  973. bool Attributor::isAssumedDead(const Instruction &I,
  974. const AbstractAttribute *QueryingAA,
  975. const AAIsDead *FnLivenessAA,
  976. bool &UsedAssumedInformation,
  977. bool CheckBBLivenessOnly, DepClassTy DepClass) {
  978. const IRPosition::CallBaseContext *CBCtx =
  979. QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
  980. if (ManifestAddedBlocks.contains(I.getParent()))
  981. return false;
  982. if (!FnLivenessAA)
  983. FnLivenessAA =
  984. lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction(), CBCtx),
  985. QueryingAA, DepClassTy::NONE);
  986. // If we have a context instruction and a liveness AA we use it.
  987. if (FnLivenessAA &&
  988. FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
  989. (CheckBBLivenessOnly ? FnLivenessAA->isAssumedDead(I.getParent())
  990. : FnLivenessAA->isAssumedDead(&I))) {
  991. if (QueryingAA)
  992. recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
  993. if (!FnLivenessAA->isKnownDead(&I))
  994. UsedAssumedInformation = true;
  995. return true;
  996. }
  997. if (CheckBBLivenessOnly)
  998. return false;
  999. const IRPosition IRP = IRPosition::inst(I, CBCtx);
  1000. const AAIsDead &IsDeadAA =
  1001. getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
  1002. // Don't check liveness for AAIsDead.
  1003. if (QueryingAA == &IsDeadAA)
  1004. return false;
  1005. if (IsDeadAA.isAssumedDead()) {
  1006. if (QueryingAA)
  1007. recordDependence(IsDeadAA, *QueryingAA, DepClass);
  1008. if (!IsDeadAA.isKnownDead())
  1009. UsedAssumedInformation = true;
  1010. return true;
  1011. }
  1012. return false;
  1013. }
  1014. bool Attributor::isAssumedDead(const IRPosition &IRP,
  1015. const AbstractAttribute *QueryingAA,
  1016. const AAIsDead *FnLivenessAA,
  1017. bool &UsedAssumedInformation,
  1018. bool CheckBBLivenessOnly, DepClassTy DepClass) {
  1019. Instruction *CtxI = IRP.getCtxI();
  1020. if (CtxI &&
  1021. isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
  1022. /* CheckBBLivenessOnly */ true,
  1023. CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
  1024. return true;
  1025. if (CheckBBLivenessOnly)
  1026. return false;
  1027. // If we haven't succeeded we query the specific liveness info for the IRP.
  1028. const AAIsDead *IsDeadAA;
  1029. if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
  1030. IsDeadAA = &getOrCreateAAFor<AAIsDead>(
  1031. IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
  1032. QueryingAA, DepClassTy::NONE);
  1033. else
  1034. IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
  1035. // Don't check liveness for AAIsDead.
  1036. if (QueryingAA == IsDeadAA)
  1037. return false;
  1038. if (IsDeadAA->isAssumedDead()) {
  1039. if (QueryingAA)
  1040. recordDependence(*IsDeadAA, *QueryingAA, DepClass);
  1041. if (!IsDeadAA->isKnownDead())
  1042. UsedAssumedInformation = true;
  1043. return true;
  1044. }
  1045. return false;
  1046. }
  1047. bool Attributor::isAssumedDead(const BasicBlock &BB,
  1048. const AbstractAttribute *QueryingAA,
  1049. const AAIsDead *FnLivenessAA,
  1050. DepClassTy DepClass) {
  1051. if (!FnLivenessAA)
  1052. FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*BB.getParent()),
  1053. QueryingAA, DepClassTy::NONE);
  1054. if (FnLivenessAA->isAssumedDead(&BB)) {
  1055. if (QueryingAA)
  1056. recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
  1057. return true;
  1058. }
  1059. return false;
  1060. }
  1061. bool Attributor::checkForAllUses(
  1062. function_ref<bool(const Use &, bool &)> Pred,
  1063. const AbstractAttribute &QueryingAA, const Value &V,
  1064. bool CheckBBLivenessOnly, DepClassTy LivenessDepClass,
  1065. function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB) {
  1066. // Check the trivial case first as it catches void values.
  1067. if (V.use_empty())
  1068. return true;
  1069. const IRPosition &IRP = QueryingAA.getIRPosition();
  1070. SmallVector<const Use *, 16> Worklist;
  1071. SmallPtrSet<const Use *, 16> Visited;
  1072. for (const Use &U : V.uses())
  1073. Worklist.push_back(&U);
  1074. LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
  1075. << " initial uses to check\n");
  1076. const Function *ScopeFn = IRP.getAnchorScope();
  1077. const auto *LivenessAA =
  1078. ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
  1079. DepClassTy::NONE)
  1080. : nullptr;
  1081. while (!Worklist.empty()) {
  1082. const Use *U = Worklist.pop_back_val();
  1083. if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second)
  1084. continue;
  1085. LLVM_DEBUG({
  1086. if (auto *Fn = dyn_cast<Function>(U->getUser()))
  1087. dbgs() << "[Attributor] Check use: " << **U << " in " << Fn->getName()
  1088. << "\n";
  1089. else
  1090. dbgs() << "[Attributor] Check use: " << **U << " in " << *U->getUser()
  1091. << "\n";
  1092. });
  1093. bool UsedAssumedInformation = false;
  1094. if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
  1095. CheckBBLivenessOnly, LivenessDepClass)) {
  1096. LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
  1097. continue;
  1098. }
  1099. if (U->getUser()->isDroppable()) {
  1100. LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n");
  1101. continue;
  1102. }
  1103. if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
  1104. if (&SI->getOperandUse(0) == U) {
  1105. if (!Visited.insert(U).second)
  1106. continue;
  1107. SmallSetVector<Value *, 4> PotentialCopies;
  1108. if (AA::getPotentialCopiesOfStoredValue(*this, *SI, PotentialCopies,
  1109. QueryingAA,
  1110. UsedAssumedInformation)) {
  1111. LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with "
  1112. << PotentialCopies.size()
  1113. << " potential copies instead!\n");
  1114. for (Value *PotentialCopy : PotentialCopies)
  1115. for (const Use &CopyUse : PotentialCopy->uses()) {
  1116. if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) {
  1117. LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
  1118. "rejected by the equivalence call back: "
  1119. << *CopyUse << "!\n");
  1120. return false;
  1121. }
  1122. Worklist.push_back(&CopyUse);
  1123. }
  1124. continue;
  1125. }
  1126. }
  1127. }
  1128. bool Follow = false;
  1129. if (!Pred(*U, Follow))
  1130. return false;
  1131. if (!Follow)
  1132. continue;
  1133. for (const Use &UU : U->getUser()->uses())
  1134. Worklist.push_back(&UU);
  1135. }
  1136. return true;
  1137. }
  1138. bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
  1139. const AbstractAttribute &QueryingAA,
  1140. bool RequireAllCallSites,
  1141. bool &UsedAssumedInformation) {
  1142. // We can try to determine information from
  1143. // the call sites. However, this is only possible all call sites are known,
  1144. // hence the function has internal linkage.
  1145. const IRPosition &IRP = QueryingAA.getIRPosition();
  1146. const Function *AssociatedFunction = IRP.getAssociatedFunction();
  1147. if (!AssociatedFunction) {
  1148. LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
  1149. << "\n");
  1150. return false;
  1151. }
  1152. return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
  1153. &QueryingAA, UsedAssumedInformation);
  1154. }
  1155. bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
  1156. const Function &Fn,
  1157. bool RequireAllCallSites,
  1158. const AbstractAttribute *QueryingAA,
  1159. bool &UsedAssumedInformation) {
  1160. if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
  1161. LLVM_DEBUG(
  1162. dbgs()
  1163. << "[Attributor] Function " << Fn.getName()
  1164. << " has no internal linkage, hence not all call sites are known\n");
  1165. return false;
  1166. }
  1167. SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
  1168. for (unsigned u = 0; u < Uses.size(); ++u) {
  1169. const Use &U = *Uses[u];
  1170. LLVM_DEBUG({
  1171. if (auto *Fn = dyn_cast<Function>(U))
  1172. dbgs() << "[Attributor] Check use: " << Fn->getName() << " in "
  1173. << *U.getUser() << "\n";
  1174. else
  1175. dbgs() << "[Attributor] Check use: " << *U << " in " << *U.getUser()
  1176. << "\n";
  1177. });
  1178. if (isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
  1179. /* CheckBBLivenessOnly */ true)) {
  1180. LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
  1181. continue;
  1182. }
  1183. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
  1184. if (CE->isCast() && CE->getType()->isPointerTy() &&
  1185. CE->getType()->getPointerElementType()->isFunctionTy()) {
  1186. LLVM_DEBUG(
  1187. dbgs() << "[Attributor] Use, is constant cast expression, add "
  1188. << CE->getNumUses()
  1189. << " uses of that expression instead!\n");
  1190. for (const Use &CEU : CE->uses())
  1191. Uses.push_back(&CEU);
  1192. continue;
  1193. }
  1194. }
  1195. AbstractCallSite ACS(&U);
  1196. if (!ACS) {
  1197. LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
  1198. << " has non call site use " << *U.get() << " in "
  1199. << *U.getUser() << "\n");
  1200. // BlockAddress users are allowed.
  1201. if (isa<BlockAddress>(U.getUser()))
  1202. continue;
  1203. return false;
  1204. }
  1205. const Use *EffectiveUse =
  1206. ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
  1207. if (!ACS.isCallee(EffectiveUse)) {
  1208. if (!RequireAllCallSites) {
  1209. LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
  1210. << " is not a call of " << Fn.getName()
  1211. << ", skip use\n");
  1212. continue;
  1213. }
  1214. LLVM_DEBUG(dbgs() << "[Attributor] User " << *EffectiveUse->getUser()
  1215. << " is an invalid use of " << Fn.getName() << "\n");
  1216. return false;
  1217. }
  1218. // Make sure the arguments that can be matched between the call site and the
  1219. // callee argee on their type. It is unlikely they do not and it doesn't
  1220. // make sense for all attributes to know/care about this.
  1221. assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
  1222. unsigned MinArgsParams =
  1223. std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
  1224. for (unsigned u = 0; u < MinArgsParams; ++u) {
  1225. Value *CSArgOp = ACS.getCallArgOperand(u);
  1226. if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
  1227. LLVM_DEBUG(
  1228. dbgs() << "[Attributor] Call site / callee argument type mismatch ["
  1229. << u << "@" << Fn.getName() << ": "
  1230. << *Fn.getArg(u)->getType() << " vs. "
  1231. << *ACS.getCallArgOperand(u)->getType() << "\n");
  1232. return false;
  1233. }
  1234. }
  1235. if (Pred(ACS))
  1236. continue;
  1237. LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
  1238. << *ACS.getInstruction() << "\n");
  1239. return false;
  1240. }
  1241. return true;
  1242. }
  1243. bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
  1244. // TODO: Maintain a cache of Values that are
  1245. // on the pathway from a Argument to a Instruction that would effect the
  1246. // liveness/return state etc.
  1247. return EnableCallSiteSpecific;
  1248. }
  1249. bool Attributor::checkForAllReturnedValuesAndReturnInsts(
  1250. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
  1251. const AbstractAttribute &QueryingAA) {
  1252. const IRPosition &IRP = QueryingAA.getIRPosition();
  1253. // Since we need to provide return instructions we have to have an exact
  1254. // definition.
  1255. const Function *AssociatedFunction = IRP.getAssociatedFunction();
  1256. if (!AssociatedFunction)
  1257. return false;
  1258. // If this is a call site query we use the call site specific return values
  1259. // and liveness information.
  1260. // TODO: use the function scope once we have call site AAReturnedValues.
  1261. const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
  1262. const auto &AARetVal =
  1263. getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
  1264. if (!AARetVal.getState().isValidState())
  1265. return false;
  1266. return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
  1267. }
  1268. bool Attributor::checkForAllReturnedValues(
  1269. function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) {
  1270. const IRPosition &IRP = QueryingAA.getIRPosition();
  1271. const Function *AssociatedFunction = IRP.getAssociatedFunction();
  1272. if (!AssociatedFunction)
  1273. return false;
  1274. // TODO: use the function scope once we have call site AAReturnedValues.
  1275. const IRPosition &QueryIRP = IRPosition::function(
  1276. *AssociatedFunction, QueryingAA.getCallBaseContext());
  1277. const auto &AARetVal =
  1278. getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
  1279. if (!AARetVal.getState().isValidState())
  1280. return false;
  1281. return AARetVal.checkForAllReturnedValuesAndReturnInsts(
  1282. [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
  1283. return Pred(RV);
  1284. });
  1285. }
  1286. static bool checkForAllInstructionsImpl(
  1287. Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
  1288. function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
  1289. const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes,
  1290. bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
  1291. bool CheckPotentiallyDead = false) {
  1292. for (unsigned Opcode : Opcodes) {
  1293. // Check if we have instructions with this opcode at all first.
  1294. auto *Insts = OpcodeInstMap.lookup(Opcode);
  1295. if (!Insts)
  1296. continue;
  1297. for (Instruction *I : *Insts) {
  1298. // Skip dead instructions.
  1299. if (A && !CheckPotentiallyDead &&
  1300. A->isAssumedDead(IRPosition::inst(*I), QueryingAA, LivenessAA,
  1301. UsedAssumedInformation, CheckBBLivenessOnly)) {
  1302. LLVM_DEBUG(dbgs() << "[Attributor] Instruction " << *I
  1303. << " is potentially dead, skip!\n";);
  1304. continue;
  1305. }
  1306. if (!Pred(*I))
  1307. return false;
  1308. }
  1309. }
  1310. return true;
  1311. }
  1312. bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
  1313. const AbstractAttribute &QueryingAA,
  1314. const ArrayRef<unsigned> &Opcodes,
  1315. bool &UsedAssumedInformation,
  1316. bool CheckBBLivenessOnly,
  1317. bool CheckPotentiallyDead) {
  1318. const IRPosition &IRP = QueryingAA.getIRPosition();
  1319. // Since we need to provide instructions we have to have an exact definition.
  1320. const Function *AssociatedFunction = IRP.getAssociatedFunction();
  1321. if (!AssociatedFunction)
  1322. return false;
  1323. if (AssociatedFunction->isDeclaration())
  1324. return false;
  1325. // TODO: use the function scope once we have call site AAReturnedValues.
  1326. const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
  1327. const auto *LivenessAA =
  1328. (CheckBBLivenessOnly || CheckPotentiallyDead)
  1329. ? nullptr
  1330. : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE));
  1331. auto &OpcodeInstMap =
  1332. InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
  1333. if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
  1334. LivenessAA, Opcodes, UsedAssumedInformation,
  1335. CheckBBLivenessOnly, CheckPotentiallyDead))
  1336. return false;
  1337. return true;
  1338. }
  1339. bool Attributor::checkForAllReadWriteInstructions(
  1340. function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
  1341. bool &UsedAssumedInformation) {
  1342. const Function *AssociatedFunction =
  1343. QueryingAA.getIRPosition().getAssociatedFunction();
  1344. if (!AssociatedFunction)
  1345. return false;
  1346. // TODO: use the function scope once we have call site AAReturnedValues.
  1347. const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
  1348. const auto &LivenessAA =
  1349. getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
  1350. for (Instruction *I :
  1351. InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
  1352. // Skip dead instructions.
  1353. if (isAssumedDead(IRPosition::inst(*I), &QueryingAA, &LivenessAA,
  1354. UsedAssumedInformation))
  1355. continue;
  1356. if (!Pred(*I))
  1357. return false;
  1358. }
  1359. return true;
  1360. }
  1361. void Attributor::runTillFixpoint() {
  1362. TimeTraceScope TimeScope("Attributor::runTillFixpoint");
  1363. LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
  1364. << DG.SyntheticRoot.Deps.size()
  1365. << " abstract attributes.\n");
  1366. // Now that all abstract attributes are collected and initialized we start
  1367. // the abstract analysis.
  1368. unsigned IterationCounter = 1;
  1369. unsigned MaxFixedPointIterations;
  1370. if (MaxFixpointIterations)
  1371. MaxFixedPointIterations = MaxFixpointIterations.getValue();
  1372. else
  1373. MaxFixedPointIterations = SetFixpointIterations;
  1374. SmallVector<AbstractAttribute *, 32> ChangedAAs;
  1375. SetVector<AbstractAttribute *> Worklist, InvalidAAs;
  1376. Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end());
  1377. do {
  1378. // Remember the size to determine new attributes.
  1379. size_t NumAAs = DG.SyntheticRoot.Deps.size();
  1380. LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
  1381. << ", Worklist size: " << Worklist.size() << "\n");
  1382. // For invalid AAs we can fix dependent AAs that have a required dependence,
  1383. // thereby folding long dependence chains in a single step without the need
  1384. // to run updates.
  1385. for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
  1386. AbstractAttribute *InvalidAA = InvalidAAs[u];
  1387. // Check the dependences to fast track invalidation.
  1388. LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
  1389. << InvalidAA->Deps.size()
  1390. << " required & optional dependences\n");
  1391. while (!InvalidAA->Deps.empty()) {
  1392. const auto &Dep = InvalidAA->Deps.back();
  1393. InvalidAA->Deps.pop_back();
  1394. AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer());
  1395. if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) {
  1396. LLVM_DEBUG(dbgs() << " - recompute: " << *DepAA);
  1397. Worklist.insert(DepAA);
  1398. continue;
  1399. }
  1400. LLVM_DEBUG(dbgs() << " - invalidate: " << *DepAA);
  1401. DepAA->getState().indicatePessimisticFixpoint();
  1402. assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!");
  1403. if (!DepAA->getState().isValidState())
  1404. InvalidAAs.insert(DepAA);
  1405. else
  1406. ChangedAAs.push_back(DepAA);
  1407. }
  1408. }
  1409. // Add all abstract attributes that are potentially dependent on one that
  1410. // changed to the work list.
  1411. for (AbstractAttribute *ChangedAA : ChangedAAs)
  1412. while (!ChangedAA->Deps.empty()) {
  1413. Worklist.insert(
  1414. cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
  1415. ChangedAA->Deps.pop_back();
  1416. }
  1417. LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
  1418. << ", Worklist+Dependent size: " << Worklist.size()
  1419. << "\n");
  1420. // Reset the changed and invalid set.
  1421. ChangedAAs.clear();
  1422. InvalidAAs.clear();
  1423. // Update all abstract attribute in the work list and record the ones that
  1424. // changed.
  1425. for (AbstractAttribute *AA : Worklist) {
  1426. const auto &AAState = AA->getState();
  1427. if (!AAState.isAtFixpoint())
  1428. if (updateAA(*AA) == ChangeStatus::CHANGED)
  1429. ChangedAAs.push_back(AA);
  1430. // Use the InvalidAAs vector to propagate invalid states fast transitively
  1431. // without requiring updates.
  1432. if (!AAState.isValidState())
  1433. InvalidAAs.insert(AA);
  1434. }
  1435. // Add attributes to the changed set if they have been created in the last
  1436. // iteration.
  1437. ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
  1438. DG.SyntheticRoot.end());
  1439. // Reset the work list and repopulate with the changed abstract attributes.
  1440. // Note that dependent ones are added above.
  1441. Worklist.clear();
  1442. Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
  1443. Worklist.insert(QueryAAsAwaitingUpdate.begin(),
  1444. QueryAAsAwaitingUpdate.end());
  1445. QueryAAsAwaitingUpdate.clear();
  1446. } while (!Worklist.empty() && (IterationCounter++ < MaxFixedPointIterations ||
  1447. VerifyMaxFixpointIterations));
  1448. if (IterationCounter > MaxFixedPointIterations && !Worklist.empty()) {
  1449. auto Remark = [&](OptimizationRemarkMissed ORM) {
  1450. return ORM << "Attributor did not reach a fixpoint after "
  1451. << ore::NV("Iterations", MaxFixedPointIterations)
  1452. << " iterations.";
  1453. };
  1454. Function *F = Worklist.front()->getIRPosition().getAssociatedFunction();
  1455. emitRemark<OptimizationRemarkMissed>(F, "FixedPoint", Remark);
  1456. }
  1457. LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
  1458. << IterationCounter << "/" << MaxFixpointIterations
  1459. << " iterations\n");
  1460. // Reset abstract arguments not settled in a sound fixpoint by now. This
  1461. // happens when we stopped the fixpoint iteration early. Note that only the
  1462. // ones marked as "changed" *and* the ones transitively depending on them
  1463. // need to be reverted to a pessimistic state. Others might not be in a
  1464. // fixpoint state but we can use the optimistic results for them anyway.
  1465. SmallPtrSet<AbstractAttribute *, 32> Visited;
  1466. for (unsigned u = 0; u < ChangedAAs.size(); u++) {
  1467. AbstractAttribute *ChangedAA = ChangedAAs[u];
  1468. if (!Visited.insert(ChangedAA).second)
  1469. continue;
  1470. AbstractState &State = ChangedAA->getState();
  1471. if (!State.isAtFixpoint()) {
  1472. State.indicatePessimisticFixpoint();
  1473. NumAttributesTimedOut++;
  1474. }
  1475. while (!ChangedAA->Deps.empty()) {
  1476. ChangedAAs.push_back(
  1477. cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
  1478. ChangedAA->Deps.pop_back();
  1479. }
  1480. }
  1481. LLVM_DEBUG({
  1482. if (!Visited.empty())
  1483. dbgs() << "\n[Attributor] Finalized " << Visited.size()
  1484. << " abstract attributes.\n";
  1485. });
  1486. if (VerifyMaxFixpointIterations &&
  1487. IterationCounter != MaxFixedPointIterations) {
  1488. errs() << "\n[Attributor] Fixpoint iteration done after: "
  1489. << IterationCounter << "/" << MaxFixedPointIterations
  1490. << " iterations\n";
  1491. llvm_unreachable("The fixpoint was not reached with exactly the number of "
  1492. "specified iterations!");
  1493. }
  1494. }
  1495. void Attributor::registerForUpdate(AbstractAttribute &AA) {
  1496. assert(AA.isQueryAA() &&
  1497. "Non-query AAs should not be required to register for updates!");
  1498. QueryAAsAwaitingUpdate.insert(&AA);
  1499. }
  1500. ChangeStatus Attributor::manifestAttributes() {
  1501. TimeTraceScope TimeScope("Attributor::manifestAttributes");
  1502. size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
  1503. unsigned NumManifested = 0;
  1504. unsigned NumAtFixpoint = 0;
  1505. ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
  1506. for (auto &DepAA : DG.SyntheticRoot.Deps) {
  1507. AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
  1508. AbstractState &State = AA->getState();
  1509. // If there is not already a fixpoint reached, we can now take the
  1510. // optimistic state. This is correct because we enforced a pessimistic one
  1511. // on abstract attributes that were transitively dependent on a changed one
  1512. // already above.
  1513. if (!State.isAtFixpoint())
  1514. State.indicateOptimisticFixpoint();
  1515. // We must not manifest Attributes that use Callbase info.
  1516. if (AA->hasCallBaseContext())
  1517. continue;
  1518. // If the state is invalid, we do not try to manifest it.
  1519. if (!State.isValidState())
  1520. continue;
  1521. // Skip dead code.
  1522. bool UsedAssumedInformation = false;
  1523. if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
  1524. /* CheckBBLivenessOnly */ true))
  1525. continue;
  1526. // Check if the manifest debug counter that allows skipping manifestation of
  1527. // AAs
  1528. if (!DebugCounter::shouldExecute(ManifestDBGCounter))
  1529. continue;
  1530. // Manifest the state and record if we changed the IR.
  1531. ChangeStatus LocalChange = AA->manifest(*this);
  1532. if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
  1533. AA->trackStatistics();
  1534. LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
  1535. << "\n");
  1536. ManifestChange = ManifestChange | LocalChange;
  1537. NumAtFixpoint++;
  1538. NumManifested += (LocalChange == ChangeStatus::CHANGED);
  1539. }
  1540. (void)NumManifested;
  1541. (void)NumAtFixpoint;
  1542. LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
  1543. << " arguments while " << NumAtFixpoint
  1544. << " were in a valid fixpoint state\n");
  1545. NumAttributesManifested += NumManifested;
  1546. NumAttributesValidFixpoint += NumAtFixpoint;
  1547. (void)NumFinalAAs;
  1548. if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
  1549. for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u)
  1550. errs() << "Unexpected abstract attribute: "
  1551. << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
  1552. << " :: "
  1553. << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
  1554. ->getIRPosition()
  1555. .getAssociatedValue()
  1556. << "\n";
  1557. llvm_unreachable("Expected the final number of abstract attributes to "
  1558. "remain unchanged!");
  1559. }
  1560. return ManifestChange;
  1561. }
  1562. void Attributor::identifyDeadInternalFunctions() {
  1563. // Early exit if we don't intend to delete functions.
  1564. if (!DeleteFns)
  1565. return;
  1566. // Identify dead internal functions and delete them. This happens outside
  1567. // the other fixpoint analysis as we might treat potentially dead functions
  1568. // as live to lower the number of iterations. If they happen to be dead, the
  1569. // below fixpoint loop will identify and eliminate them.
  1570. SmallVector<Function *, 8> InternalFns;
  1571. for (Function *F : Functions)
  1572. if (F->hasLocalLinkage())
  1573. InternalFns.push_back(F);
  1574. SmallPtrSet<Function *, 8> LiveInternalFns;
  1575. bool FoundLiveInternal = true;
  1576. while (FoundLiveInternal) {
  1577. FoundLiveInternal = false;
  1578. for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
  1579. Function *F = InternalFns[u];
  1580. if (!F)
  1581. continue;
  1582. bool UsedAssumedInformation = false;
  1583. if (checkForAllCallSites(
  1584. [&](AbstractCallSite ACS) {
  1585. Function *Callee = ACS.getInstruction()->getFunction();
  1586. return ToBeDeletedFunctions.count(Callee) ||
  1587. (Functions.count(Callee) && Callee->hasLocalLinkage() &&
  1588. !LiveInternalFns.count(Callee));
  1589. },
  1590. *F, true, nullptr, UsedAssumedInformation)) {
  1591. continue;
  1592. }
  1593. LiveInternalFns.insert(F);
  1594. InternalFns[u] = nullptr;
  1595. FoundLiveInternal = true;
  1596. }
  1597. }
  1598. for (unsigned u = 0, e = InternalFns.size(); u < e; ++u)
  1599. if (Function *F = InternalFns[u])
  1600. ToBeDeletedFunctions.insert(F);
  1601. }
  1602. ChangeStatus Attributor::cleanupIR() {
  1603. TimeTraceScope TimeScope("Attributor::cleanupIR");
  1604. // Delete stuff at the end to avoid invalid references and a nice order.
  1605. LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "
  1606. << ToBeDeletedFunctions.size() << " functions and "
  1607. << ToBeDeletedBlocks.size() << " blocks and "
  1608. << ToBeDeletedInsts.size() << " instructions and "
  1609. << ToBeChangedValues.size() << " values and "
  1610. << ToBeChangedUses.size() << " uses. "
  1611. << "Preserve manifest added " << ManifestAddedBlocks.size()
  1612. << " blocks\n");
  1613. SmallVector<WeakTrackingVH, 32> DeadInsts;
  1614. SmallVector<Instruction *, 32> TerminatorsToFold;
  1615. auto ReplaceUse = [&](Use *U, Value *NewV) {
  1616. Value *OldV = U->get();
  1617. // If we plan to replace NewV we need to update it at this point.
  1618. do {
  1619. const auto &Entry = ToBeChangedValues.lookup(NewV);
  1620. if (!Entry.first)
  1621. break;
  1622. NewV = Entry.first;
  1623. } while (true);
  1624. // Do not replace uses in returns if the value is a must-tail call we will
  1625. // not delete.
  1626. if (auto *RI = dyn_cast<ReturnInst>(U->getUser())) {
  1627. if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
  1628. if (CI->isMustTailCall() &&
  1629. (!ToBeDeletedInsts.count(CI) || !isRunOn(*CI->getCaller())))
  1630. return;
  1631. // If we rewrite a return and the new value is not an argument, strip the
  1632. // `returned` attribute as it is wrong now.
  1633. if (!isa<Argument>(NewV))
  1634. for (auto &Arg : RI->getFunction()->args())
  1635. Arg.removeAttr(Attribute::Returned);
  1636. }
  1637. // Do not perform call graph altering changes outside the SCC.
  1638. if (auto *CB = dyn_cast<CallBase>(U->getUser()))
  1639. if (CB->isCallee(U) && !isRunOn(*CB->getCaller()))
  1640. return;
  1641. LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
  1642. << " instead of " << *OldV << "\n");
  1643. U->set(NewV);
  1644. if (Instruction *I = dyn_cast<Instruction>(OldV)) {
  1645. CGModifiedFunctions.insert(I->getFunction());
  1646. if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
  1647. isInstructionTriviallyDead(I))
  1648. DeadInsts.push_back(I);
  1649. }
  1650. if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
  1651. auto *CB = cast<CallBase>(U->getUser());
  1652. if (CB->isArgOperand(U)) {
  1653. unsigned Idx = CB->getArgOperandNo(U);
  1654. CB->removeParamAttr(Idx, Attribute::NoUndef);
  1655. Function *Fn = CB->getCalledFunction();
  1656. if (Fn && Fn->arg_size() > Idx)
  1657. Fn->removeParamAttr(Idx, Attribute::NoUndef);
  1658. }
  1659. }
  1660. if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
  1661. Instruction *UserI = cast<Instruction>(U->getUser());
  1662. if (isa<UndefValue>(NewV)) {
  1663. ToBeChangedToUnreachableInsts.insert(UserI);
  1664. } else {
  1665. TerminatorsToFold.push_back(UserI);
  1666. }
  1667. }
  1668. };
  1669. for (auto &It : ToBeChangedUses) {
  1670. Use *U = It.first;
  1671. Value *NewV = It.second;
  1672. ReplaceUse(U, NewV);
  1673. }
  1674. SmallVector<Use *, 4> Uses;
  1675. for (auto &It : ToBeChangedValues) {
  1676. Value *OldV = It.first;
  1677. auto &Entry = It.second;
  1678. Value *NewV = Entry.first;
  1679. Uses.clear();
  1680. for (auto &U : OldV->uses())
  1681. if (Entry.second || !U.getUser()->isDroppable())
  1682. Uses.push_back(&U);
  1683. for (Use *U : Uses)
  1684. ReplaceUse(U, NewV);
  1685. }
  1686. for (auto &V : InvokeWithDeadSuccessor)
  1687. if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
  1688. assert(isRunOn(*II->getFunction()) &&
  1689. "Cannot replace an invoke outside the current SCC!");
  1690. bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
  1691. bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
  1692. bool Invoke2CallAllowed =
  1693. !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction());
  1694. assert((UnwindBBIsDead || NormalBBIsDead) &&
  1695. "Invoke does not have dead successors!");
  1696. BasicBlock *BB = II->getParent();
  1697. BasicBlock *NormalDestBB = II->getNormalDest();
  1698. if (UnwindBBIsDead) {
  1699. Instruction *NormalNextIP = &NormalDestBB->front();
  1700. if (Invoke2CallAllowed) {
  1701. changeToCall(II);
  1702. NormalNextIP = BB->getTerminator();
  1703. }
  1704. if (NormalBBIsDead)
  1705. ToBeChangedToUnreachableInsts.insert(NormalNextIP);
  1706. } else {
  1707. assert(NormalBBIsDead && "Broken invariant!");
  1708. if (!NormalDestBB->getUniquePredecessor())
  1709. NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
  1710. ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
  1711. }
  1712. }
  1713. for (Instruction *I : TerminatorsToFold) {
  1714. if (!isRunOn(*I->getFunction()))
  1715. continue;
  1716. CGModifiedFunctions.insert(I->getFunction());
  1717. ConstantFoldTerminator(I->getParent());
  1718. }
  1719. for (auto &V : ToBeChangedToUnreachableInsts)
  1720. if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
  1721. if (!isRunOn(*I->getFunction()))
  1722. continue;
  1723. CGModifiedFunctions.insert(I->getFunction());
  1724. changeToUnreachable(I);
  1725. }
  1726. for (auto &V : ToBeDeletedInsts) {
  1727. if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
  1728. if (auto *CB = dyn_cast<CallBase>(I)) {
  1729. if (!isRunOn(*I->getFunction()))
  1730. continue;
  1731. if (!isa<IntrinsicInst>(CB))
  1732. CGUpdater.removeCallSite(*CB);
  1733. }
  1734. I->dropDroppableUses();
  1735. CGModifiedFunctions.insert(I->getFunction());
  1736. if (!I->getType()->isVoidTy())
  1737. I->replaceAllUsesWith(UndefValue::get(I->getType()));
  1738. if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
  1739. DeadInsts.push_back(I);
  1740. else
  1741. I->eraseFromParent();
  1742. }
  1743. }
  1744. llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) {
  1745. return !I || !isRunOn(*cast<Instruction>(I)->getFunction());
  1746. });
  1747. LLVM_DEBUG({
  1748. dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";
  1749. for (auto &I : DeadInsts)
  1750. if (I)
  1751. dbgs() << " - " << *I << "\n";
  1752. });
  1753. RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
  1754. if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
  1755. SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
  1756. ToBeDeletedBBs.reserve(NumDeadBlocks);
  1757. for (BasicBlock *BB : ToBeDeletedBlocks) {
  1758. assert(isRunOn(*BB->getParent()) &&
  1759. "Cannot delete a block outside the current SCC!");
  1760. CGModifiedFunctions.insert(BB->getParent());
  1761. // Do not delete BBs added during manifests of AAs.
  1762. if (ManifestAddedBlocks.contains(BB))
  1763. continue;
  1764. ToBeDeletedBBs.push_back(BB);
  1765. }
  1766. // Actually we do not delete the blocks but squash them into a single
  1767. // unreachable but untangling branches that jump here is something we need
  1768. // to do in a more generic way.
  1769. detachDeadBlocks(ToBeDeletedBBs, nullptr);
  1770. }
  1771. identifyDeadInternalFunctions();
  1772. // Rewrite the functions as requested during manifest.
  1773. ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
  1774. for (Function *Fn : CGModifiedFunctions)
  1775. if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
  1776. CGUpdater.reanalyzeFunction(*Fn);
  1777. for (Function *Fn : ToBeDeletedFunctions) {
  1778. if (!Functions.count(Fn))
  1779. continue;
  1780. CGUpdater.removeFunction(*Fn);
  1781. }
  1782. if (!ToBeChangedUses.empty())
  1783. ManifestChange = ChangeStatus::CHANGED;
  1784. if (!ToBeChangedToUnreachableInsts.empty())
  1785. ManifestChange = ChangeStatus::CHANGED;
  1786. if (!ToBeDeletedFunctions.empty())
  1787. ManifestChange = ChangeStatus::CHANGED;
  1788. if (!ToBeDeletedBlocks.empty())
  1789. ManifestChange = ChangeStatus::CHANGED;
  1790. if (!ToBeDeletedInsts.empty())
  1791. ManifestChange = ChangeStatus::CHANGED;
  1792. if (!InvokeWithDeadSuccessor.empty())
  1793. ManifestChange = ChangeStatus::CHANGED;
  1794. if (!DeadInsts.empty())
  1795. ManifestChange = ChangeStatus::CHANGED;
  1796. NumFnDeleted += ToBeDeletedFunctions.size();
  1797. LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()
  1798. << " functions after manifest.\n");
  1799. #ifdef EXPENSIVE_CHECKS
  1800. for (Function *F : Functions) {
  1801. if (ToBeDeletedFunctions.count(F))
  1802. continue;
  1803. assert(!verifyFunction(*F, &errs()) && "Module verification failed!");
  1804. }
  1805. #endif
  1806. return ManifestChange;
  1807. }
  1808. ChangeStatus Attributor::run() {
  1809. TimeTraceScope TimeScope("Attributor::run");
  1810. AttributorCallGraph ACallGraph(*this);
  1811. if (PrintCallGraph)
  1812. ACallGraph.populateAll();
  1813. Phase = AttributorPhase::UPDATE;
  1814. runTillFixpoint();
  1815. // dump graphs on demand
  1816. if (DumpDepGraph)
  1817. DG.dumpGraph();
  1818. if (ViewDepGraph)
  1819. DG.viewGraph();
  1820. if (PrintDependencies)
  1821. DG.print();
  1822. Phase = AttributorPhase::MANIFEST;
  1823. ChangeStatus ManifestChange = manifestAttributes();
  1824. Phase = AttributorPhase::CLEANUP;
  1825. ChangeStatus CleanupChange = cleanupIR();
  1826. if (PrintCallGraph)
  1827. ACallGraph.print();
  1828. return ManifestChange | CleanupChange;
  1829. }
  1830. ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
  1831. TimeTraceScope TimeScope(
  1832. AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) +
  1833. "::updateAA");
  1834. assert(Phase == AttributorPhase::UPDATE &&
  1835. "We can update AA only in the update stage!");
  1836. // Use a new dependence vector for this update.
  1837. DependenceVector DV;
  1838. DependenceStack.push_back(&DV);
  1839. auto &AAState = AA.getState();
  1840. ChangeStatus CS = ChangeStatus::UNCHANGED;
  1841. bool UsedAssumedInformation = false;
  1842. if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
  1843. /* CheckBBLivenessOnly */ true))
  1844. CS = AA.update(*this);
  1845. if (!AA.isQueryAA() && DV.empty()) {
  1846. // If the attribute did not query any non-fix information, the state
  1847. // will not change and we can indicate that right away.
  1848. AAState.indicateOptimisticFixpoint();
  1849. }
  1850. if (!AAState.isAtFixpoint())
  1851. rememberDependences();
  1852. // Verify the stack was used properly, that is we pop the dependence vector we
  1853. // put there earlier.
  1854. DependenceVector *PoppedDV = DependenceStack.pop_back_val();
  1855. (void)PoppedDV;
  1856. assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!");
  1857. return CS;
  1858. }
  1859. void Attributor::createShallowWrapper(Function &F) {
  1860. assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!");
  1861. Module &M = *F.getParent();
  1862. LLVMContext &Ctx = M.getContext();
  1863. FunctionType *FnTy = F.getFunctionType();
  1864. Function *Wrapper =
  1865. Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
  1866. F.setName(""); // set the inside function anonymous
  1867. M.getFunctionList().insert(F.getIterator(), Wrapper);
  1868. F.setLinkage(GlobalValue::InternalLinkage);
  1869. F.replaceAllUsesWith(Wrapper);
  1870. assert(F.use_empty() && "Uses remained after wrapper was created!");
  1871. // Move the COMDAT section to the wrapper.
  1872. // TODO: Check if we need to keep it for F as well.
  1873. Wrapper->setComdat(F.getComdat());
  1874. F.setComdat(nullptr);
  1875. // Copy all metadata and attributes but keep them on F as well.
  1876. SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
  1877. F.getAllMetadata(MDs);
  1878. for (auto MDIt : MDs)
  1879. Wrapper->addMetadata(MDIt.first, *MDIt.second);
  1880. Wrapper->setAttributes(F.getAttributes());
  1881. // Create the call in the wrapper.
  1882. BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
  1883. SmallVector<Value *, 8> Args;
  1884. Argument *FArgIt = F.arg_begin();
  1885. for (Argument &Arg : Wrapper->args()) {
  1886. Args.push_back(&Arg);
  1887. Arg.setName((FArgIt++)->getName());
  1888. }
  1889. CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
  1890. CI->setTailCall(true);
  1891. CI->addFnAttr(Attribute::NoInline);
  1892. ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
  1893. NumFnShallowWrappersCreated++;
  1894. }
  1895. bool Attributor::isInternalizable(Function &F) {
  1896. if (F.isDeclaration() || F.hasLocalLinkage() ||
  1897. GlobalValue::isInterposableLinkage(F.getLinkage()))
  1898. return false;
  1899. return true;
  1900. }
  1901. Function *Attributor::internalizeFunction(Function &F, bool Force) {
  1902. if (!AllowDeepWrapper && !Force)
  1903. return nullptr;
  1904. if (!isInternalizable(F))
  1905. return nullptr;
  1906. SmallPtrSet<Function *, 2> FnSet = {&F};
  1907. DenseMap<Function *, Function *> InternalizedFns;
  1908. internalizeFunctions(FnSet, InternalizedFns);
  1909. return InternalizedFns[&F];
  1910. }
  1911. bool Attributor::internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
  1912. DenseMap<Function *, Function *> &FnMap) {
  1913. for (Function *F : FnSet)
  1914. if (!Attributor::isInternalizable(*F))
  1915. return false;
  1916. FnMap.clear();
  1917. // Generate the internalized version of each function.
  1918. for (Function *F : FnSet) {
  1919. Module &M = *F->getParent();
  1920. FunctionType *FnTy = F->getFunctionType();
  1921. // Create a copy of the current function
  1922. Function *Copied =
  1923. Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
  1924. F->getName() + ".internalized");
  1925. ValueToValueMapTy VMap;
  1926. auto *NewFArgIt = Copied->arg_begin();
  1927. for (auto &Arg : F->args()) {
  1928. auto ArgName = Arg.getName();
  1929. NewFArgIt->setName(ArgName);
  1930. VMap[&Arg] = &(*NewFArgIt++);
  1931. }
  1932. SmallVector<ReturnInst *, 8> Returns;
  1933. // Copy the body of the original function to the new one
  1934. CloneFunctionInto(Copied, F, VMap,
  1935. CloneFunctionChangeType::LocalChangesOnly, Returns);
  1936. // Set the linakage and visibility late as CloneFunctionInto has some
  1937. // implicit requirements.
  1938. Copied->setVisibility(GlobalValue::DefaultVisibility);
  1939. Copied->setLinkage(GlobalValue::PrivateLinkage);
  1940. // Copy metadata
  1941. SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
  1942. F->getAllMetadata(MDs);
  1943. for (auto MDIt : MDs)
  1944. if (!Copied->hasMetadata())
  1945. Copied->addMetadata(MDIt.first, *MDIt.second);
  1946. M.getFunctionList().insert(F->getIterator(), Copied);
  1947. Copied->setDSOLocal(true);
  1948. FnMap[F] = Copied;
  1949. }
  1950. // Replace all uses of the old function with the new internalized function
  1951. // unless the caller is a function that was just internalized.
  1952. for (Function *F : FnSet) {
  1953. auto &InternalizedFn = FnMap[F];
  1954. auto IsNotInternalized = [&](Use &U) -> bool {
  1955. if (auto *CB = dyn_cast<CallBase>(U.getUser()))
  1956. return !FnMap.lookup(CB->getCaller());
  1957. return false;
  1958. };
  1959. F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
  1960. }
  1961. return true;
  1962. }
  1963. bool Attributor::isValidFunctionSignatureRewrite(
  1964. Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
  1965. if (!RewriteSignatures)
  1966. return false;
  1967. Function *Fn = Arg.getParent();
  1968. auto CallSiteCanBeChanged = [Fn](AbstractCallSite ACS) {
  1969. // Forbid the call site to cast the function return type. If we need to
  1970. // rewrite these functions we need to re-create a cast for the new call site
  1971. // (if the old had uses).
  1972. if (!ACS.getCalledFunction() ||
  1973. ACS.getInstruction()->getType() !=
  1974. ACS.getCalledFunction()->getReturnType())
  1975. return false;
  1976. if (ACS.getCalledOperand()->getType() != Fn->getType())
  1977. return false;
  1978. // Forbid must-tail calls for now.
  1979. return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
  1980. };
  1981. // Avoid var-arg functions for now.
  1982. if (Fn->isVarArg()) {
  1983. LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
  1984. return false;
  1985. }
  1986. // Avoid functions with complicated argument passing semantics.
  1987. AttributeList FnAttributeList = Fn->getAttributes();
  1988. if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
  1989. FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
  1990. FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
  1991. FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
  1992. LLVM_DEBUG(
  1993. dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
  1994. return false;
  1995. }
  1996. // Avoid callbacks for now.
  1997. bool UsedAssumedInformation = false;
  1998. if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
  1999. UsedAssumedInformation)) {
  2000. LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
  2001. return false;
  2002. }
  2003. auto InstPred = [](Instruction &I) {
  2004. if (auto *CI = dyn_cast<CallInst>(&I))
  2005. return !CI->isMustTailCall();
  2006. return true;
  2007. };
  2008. // Forbid must-tail calls for now.
  2009. // TODO:
  2010. auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
  2011. if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
  2012. nullptr, {Instruction::Call},
  2013. UsedAssumedInformation)) {
  2014. LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
  2015. return false;
  2016. }
  2017. return true;
  2018. }
  2019. bool Attributor::registerFunctionSignatureRewrite(
  2020. Argument &Arg, ArrayRef<Type *> ReplacementTypes,
  2021. ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
  2022. ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
  2023. LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
  2024. << Arg.getParent()->getName() << " with "
  2025. << ReplacementTypes.size() << " replacements\n");
  2026. assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
  2027. "Cannot register an invalid rewrite");
  2028. Function *Fn = Arg.getParent();
  2029. SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
  2030. ArgumentReplacementMap[Fn];
  2031. if (ARIs.empty())
  2032. ARIs.resize(Fn->arg_size());
  2033. // If we have a replacement already with less than or equal new arguments,
  2034. // ignore this request.
  2035. std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
  2036. if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
  2037. LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
  2038. return false;
  2039. }
  2040. // If we have a replacement already but we like the new one better, delete
  2041. // the old.
  2042. ARI.reset();
  2043. LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
  2044. << Arg.getParent()->getName() << " with "
  2045. << ReplacementTypes.size() << " replacements\n");
  2046. // Remember the replacement.
  2047. ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
  2048. std::move(CalleeRepairCB),
  2049. std::move(ACSRepairCB)));
  2050. return true;
  2051. }
  2052. bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
  2053. bool Result = true;
  2054. #ifndef NDEBUG
  2055. if (SeedAllowList.size() != 0)
  2056. Result = llvm::is_contained(SeedAllowList, AA.getName());
  2057. Function *Fn = AA.getAnchorScope();
  2058. if (FunctionSeedAllowList.size() != 0 && Fn)
  2059. Result &= llvm::is_contained(FunctionSeedAllowList, Fn->getName());
  2060. #endif
  2061. return Result;
  2062. }
  2063. ChangeStatus Attributor::rewriteFunctionSignatures(
  2064. SmallPtrSetImpl<Function *> &ModifiedFns) {
  2065. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  2066. for (auto &It : ArgumentReplacementMap) {
  2067. Function *OldFn = It.getFirst();
  2068. // Deleted functions do not require rewrites.
  2069. if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
  2070. continue;
  2071. const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
  2072. It.getSecond();
  2073. assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
  2074. SmallVector<Type *, 16> NewArgumentTypes;
  2075. SmallVector<AttributeSet, 16> NewArgumentAttributes;
  2076. // Collect replacement argument types and copy over existing attributes.
  2077. AttributeList OldFnAttributeList = OldFn->getAttributes();
  2078. for (Argument &Arg : OldFn->args()) {
  2079. if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
  2080. ARIs[Arg.getArgNo()]) {
  2081. NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
  2082. ARI->ReplacementTypes.end());
  2083. NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
  2084. AttributeSet());
  2085. } else {
  2086. NewArgumentTypes.push_back(Arg.getType());
  2087. NewArgumentAttributes.push_back(
  2088. OldFnAttributeList.getParamAttrs(Arg.getArgNo()));
  2089. }
  2090. }
  2091. FunctionType *OldFnTy = OldFn->getFunctionType();
  2092. Type *RetTy = OldFnTy->getReturnType();
  2093. // Construct the new function type using the new arguments types.
  2094. FunctionType *NewFnTy =
  2095. FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
  2096. LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
  2097. << "' from " << *OldFn->getFunctionType() << " to "
  2098. << *NewFnTy << "\n");
  2099. // Create the new function body and insert it into the module.
  2100. Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
  2101. OldFn->getAddressSpace(), "");
  2102. Functions.insert(NewFn);
  2103. OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
  2104. NewFn->takeName(OldFn);
  2105. NewFn->copyAttributesFrom(OldFn);
  2106. // Patch the pointer to LLVM function in debug info descriptor.
  2107. NewFn->setSubprogram(OldFn->getSubprogram());
  2108. OldFn->setSubprogram(nullptr);
  2109. // Recompute the parameter attributes list based on the new arguments for
  2110. // the function.
  2111. LLVMContext &Ctx = OldFn->getContext();
  2112. NewFn->setAttributes(AttributeList::get(
  2113. Ctx, OldFnAttributeList.getFnAttrs(), OldFnAttributeList.getRetAttrs(),
  2114. NewArgumentAttributes));
  2115. // Since we have now created the new function, splice the body of the old
  2116. // function right into the new function, leaving the old rotting hulk of the
  2117. // function empty.
  2118. NewFn->getBasicBlockList().splice(NewFn->begin(),
  2119. OldFn->getBasicBlockList());
  2120. // Fixup block addresses to reference new function.
  2121. SmallVector<BlockAddress *, 8u> BlockAddresses;
  2122. for (User *U : OldFn->users())
  2123. if (auto *BA = dyn_cast<BlockAddress>(U))
  2124. BlockAddresses.push_back(BA);
  2125. for (auto *BA : BlockAddresses)
  2126. BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock()));
  2127. // Set of all "call-like" instructions that invoke the old function mapped
  2128. // to their new replacements.
  2129. SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
  2130. // Callback to create a new "call-like" instruction for a given one.
  2131. auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
  2132. CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
  2133. const AttributeList &OldCallAttributeList = OldCB->getAttributes();
  2134. // Collect the new argument operands for the replacement call site.
  2135. SmallVector<Value *, 16> NewArgOperands;
  2136. SmallVector<AttributeSet, 16> NewArgOperandAttributes;
  2137. for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
  2138. unsigned NewFirstArgNum = NewArgOperands.size();
  2139. (void)NewFirstArgNum; // only used inside assert.
  2140. if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
  2141. ARIs[OldArgNum]) {
  2142. if (ARI->ACSRepairCB)
  2143. ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
  2144. assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
  2145. NewArgOperands.size() &&
  2146. "ACS repair callback did not provide as many operand as new "
  2147. "types were registered!");
  2148. // TODO: Exose the attribute set to the ACS repair callback
  2149. NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
  2150. AttributeSet());
  2151. } else {
  2152. NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
  2153. NewArgOperandAttributes.push_back(
  2154. OldCallAttributeList.getParamAttrs(OldArgNum));
  2155. }
  2156. }
  2157. assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
  2158. "Mismatch # argument operands vs. # argument operand attributes!");
  2159. assert(NewArgOperands.size() == NewFn->arg_size() &&
  2160. "Mismatch # argument operands vs. # function arguments!");
  2161. SmallVector<OperandBundleDef, 4> OperandBundleDefs;
  2162. OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
  2163. // Create a new call or invoke instruction to replace the old one.
  2164. CallBase *NewCB;
  2165. if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
  2166. NewCB =
  2167. InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
  2168. NewArgOperands, OperandBundleDefs, "", OldCB);
  2169. } else {
  2170. auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
  2171. "", OldCB);
  2172. NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
  2173. NewCB = NewCI;
  2174. }
  2175. // Copy over various properties and the new attributes.
  2176. NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
  2177. NewCB->setCallingConv(OldCB->getCallingConv());
  2178. NewCB->takeName(OldCB);
  2179. NewCB->setAttributes(AttributeList::get(
  2180. Ctx, OldCallAttributeList.getFnAttrs(),
  2181. OldCallAttributeList.getRetAttrs(), NewArgOperandAttributes));
  2182. CallSitePairs.push_back({OldCB, NewCB});
  2183. return true;
  2184. };
  2185. // Use the CallSiteReplacementCreator to create replacement call sites.
  2186. bool UsedAssumedInformation = false;
  2187. bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
  2188. true, nullptr, UsedAssumedInformation);
  2189. (void)Success;
  2190. assert(Success && "Assumed call site replacement to succeed!");
  2191. // Rewire the arguments.
  2192. Argument *OldFnArgIt = OldFn->arg_begin();
  2193. Argument *NewFnArgIt = NewFn->arg_begin();
  2194. for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
  2195. ++OldArgNum, ++OldFnArgIt) {
  2196. if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
  2197. ARIs[OldArgNum]) {
  2198. if (ARI->CalleeRepairCB)
  2199. ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
  2200. NewFnArgIt += ARI->ReplacementTypes.size();
  2201. } else {
  2202. NewFnArgIt->takeName(&*OldFnArgIt);
  2203. OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
  2204. ++NewFnArgIt;
  2205. }
  2206. }
  2207. // Eliminate the instructions *after* we visited all of them.
  2208. for (auto &CallSitePair : CallSitePairs) {
  2209. CallBase &OldCB = *CallSitePair.first;
  2210. CallBase &NewCB = *CallSitePair.second;
  2211. assert(OldCB.getType() == NewCB.getType() &&
  2212. "Cannot handle call sites with different types!");
  2213. ModifiedFns.insert(OldCB.getFunction());
  2214. CGUpdater.replaceCallSite(OldCB, NewCB);
  2215. OldCB.replaceAllUsesWith(&NewCB);
  2216. OldCB.eraseFromParent();
  2217. }
  2218. // Replace the function in the call graph (if any).
  2219. CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
  2220. // If the old function was modified and needed to be reanalyzed, the new one
  2221. // does now.
  2222. if (ModifiedFns.erase(OldFn))
  2223. ModifiedFns.insert(NewFn);
  2224. Changed = ChangeStatus::CHANGED;
  2225. }
  2226. return Changed;
  2227. }
  2228. void InformationCache::initializeInformationCache(const Function &CF,
  2229. FunctionInfo &FI) {
  2230. // As we do not modify the function here we can remove the const
  2231. // withouth breaking implicit assumptions. At the end of the day, we could
  2232. // initialize the cache eagerly which would look the same to the users.
  2233. Function &F = const_cast<Function &>(CF);
  2234. // Walk all instructions to find interesting instructions that might be
  2235. // queried by abstract attributes during their initialization or update.
  2236. // This has to happen before we create attributes.
  2237. for (Instruction &I : instructions(&F)) {
  2238. bool IsInterestingOpcode = false;
  2239. // To allow easy access to all instructions in a function with a given
  2240. // opcode we store them in the InfoCache. As not all opcodes are interesting
  2241. // to concrete attributes we only cache the ones that are as identified in
  2242. // the following switch.
  2243. // Note: There are no concrete attributes now so this is initially empty.
  2244. switch (I.getOpcode()) {
  2245. default:
  2246. assert(!isa<CallBase>(&I) &&
  2247. "New call base instruction type needs to be known in the "
  2248. "Attributor.");
  2249. break;
  2250. case Instruction::Call:
  2251. // Calls are interesting on their own, additionally:
  2252. // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
  2253. // For `must-tail` calls we remember the caller and callee.
  2254. if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
  2255. fillMapFromAssume(*Assume, KnowledgeMap);
  2256. } else if (cast<CallInst>(I).isMustTailCall()) {
  2257. FI.ContainsMustTailCall = true;
  2258. if (const Function *Callee = cast<CallInst>(I).getCalledFunction())
  2259. getFunctionInfo(*Callee).CalledViaMustTail = true;
  2260. }
  2261. LLVM_FALLTHROUGH;
  2262. case Instruction::CallBr:
  2263. case Instruction::Invoke:
  2264. case Instruction::CleanupRet:
  2265. case Instruction::CatchSwitch:
  2266. case Instruction::AtomicRMW:
  2267. case Instruction::AtomicCmpXchg:
  2268. case Instruction::Br:
  2269. case Instruction::Resume:
  2270. case Instruction::Ret:
  2271. case Instruction::Load:
  2272. // The alignment of a pointer is interesting for loads.
  2273. case Instruction::Store:
  2274. // The alignment of a pointer is interesting for stores.
  2275. case Instruction::Alloca:
  2276. case Instruction::AddrSpaceCast:
  2277. IsInterestingOpcode = true;
  2278. }
  2279. if (IsInterestingOpcode) {
  2280. auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
  2281. if (!Insts)
  2282. Insts = new (Allocator) InstructionVectorTy();
  2283. Insts->push_back(&I);
  2284. }
  2285. if (I.mayReadOrWriteMemory())
  2286. FI.RWInsts.push_back(&I);
  2287. }
  2288. if (F.hasFnAttribute(Attribute::AlwaysInline) &&
  2289. isInlineViable(F).isSuccess())
  2290. InlineableFunctions.insert(&F);
  2291. }
  2292. AAResults *InformationCache::getAAResultsForFunction(const Function &F) {
  2293. return AG.getAnalysis<AAManager>(F);
  2294. }
  2295. InformationCache::FunctionInfo::~FunctionInfo() {
  2296. // The instruction vectors are allocated using a BumpPtrAllocator, we need to
  2297. // manually destroy them.
  2298. for (auto &It : OpcodeInstMap)
  2299. It.getSecond()->~InstructionVectorTy();
  2300. }
  2301. void Attributor::recordDependence(const AbstractAttribute &FromAA,
  2302. const AbstractAttribute &ToAA,
  2303. DepClassTy DepClass) {
  2304. if (DepClass == DepClassTy::NONE)
  2305. return;
  2306. // If we are outside of an update, thus before the actual fixpoint iteration
  2307. // started (= when we create AAs), we do not track dependences because we will
  2308. // put all AAs into the initial worklist anyway.
  2309. if (DependenceStack.empty())
  2310. return;
  2311. if (FromAA.getState().isAtFixpoint())
  2312. return;
  2313. DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
  2314. }
  2315. void Attributor::rememberDependences() {
  2316. assert(!DependenceStack.empty() && "No dependences to remember!");
  2317. for (DepInfo &DI : *DependenceStack.back()) {
  2318. assert((DI.DepClass == DepClassTy::REQUIRED ||
  2319. DI.DepClass == DepClassTy::OPTIONAL) &&
  2320. "Expected required or optional dependence (1 bit)!");
  2321. auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
  2322. DepAAs.push_back(AbstractAttribute::DepTy(
  2323. const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
  2324. }
  2325. }
  2326. void Attributor::identifyDefaultAbstractAttributes(Function &F) {
  2327. if (!VisitedFunctions.insert(&F).second)
  2328. return;
  2329. if (F.isDeclaration())
  2330. return;
  2331. // In non-module runs we need to look at the call sites of a function to
  2332. // determine if it is part of a must-tail call edge. This will influence what
  2333. // attributes we can derive.
  2334. InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
  2335. if (!isModulePass() && !FI.CalledViaMustTail) {
  2336. for (const Use &U : F.uses())
  2337. if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
  2338. if (CB->isCallee(&U) && CB->isMustTailCall())
  2339. FI.CalledViaMustTail = true;
  2340. }
  2341. IRPosition FPos = IRPosition::function(F);
  2342. // Check for dead BasicBlocks in every function.
  2343. // We need dead instruction detection because we do not want to deal with
  2344. // broken IR in which SSA rules do not apply.
  2345. getOrCreateAAFor<AAIsDead>(FPos);
  2346. // Every function might be "will-return".
  2347. getOrCreateAAFor<AAWillReturn>(FPos);
  2348. // Every function might contain instructions that cause "undefined behavior".
  2349. getOrCreateAAFor<AAUndefinedBehavior>(FPos);
  2350. // Every function can be nounwind.
  2351. getOrCreateAAFor<AANoUnwind>(FPos);
  2352. // Every function might be marked "nosync"
  2353. getOrCreateAAFor<AANoSync>(FPos);
  2354. // Every function might be "no-free".
  2355. getOrCreateAAFor<AANoFree>(FPos);
  2356. // Every function might be "no-return".
  2357. getOrCreateAAFor<AANoReturn>(FPos);
  2358. // Every function might be "no-recurse".
  2359. getOrCreateAAFor<AANoRecurse>(FPos);
  2360. // Every function might be "readnone/readonly/writeonly/...".
  2361. getOrCreateAAFor<AAMemoryBehavior>(FPos);
  2362. // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
  2363. getOrCreateAAFor<AAMemoryLocation>(FPos);
  2364. // Every function can track active assumptions.
  2365. getOrCreateAAFor<AAAssumptionInfo>(FPos);
  2366. // Every function might be applicable for Heap-To-Stack conversion.
  2367. if (EnableHeapToStack)
  2368. getOrCreateAAFor<AAHeapToStack>(FPos);
  2369. // Return attributes are only appropriate if the return type is non void.
  2370. Type *ReturnType = F.getReturnType();
  2371. if (!ReturnType->isVoidTy()) {
  2372. // Argument attribute "returned" --- Create only one per function even
  2373. // though it is an argument attribute.
  2374. getOrCreateAAFor<AAReturnedValues>(FPos);
  2375. IRPosition RetPos = IRPosition::returned(F);
  2376. // Every returned value might be dead.
  2377. getOrCreateAAFor<AAIsDead>(RetPos);
  2378. // Every function might be simplified.
  2379. getOrCreateAAFor<AAValueSimplify>(RetPos);
  2380. // Every returned value might be marked noundef.
  2381. getOrCreateAAFor<AANoUndef>(RetPos);
  2382. if (ReturnType->isPointerTy()) {
  2383. // Every function with pointer return type might be marked align.
  2384. getOrCreateAAFor<AAAlign>(RetPos);
  2385. // Every function with pointer return type might be marked nonnull.
  2386. getOrCreateAAFor<AANonNull>(RetPos);
  2387. // Every function with pointer return type might be marked noalias.
  2388. getOrCreateAAFor<AANoAlias>(RetPos);
  2389. // Every function with pointer return type might be marked
  2390. // dereferenceable.
  2391. getOrCreateAAFor<AADereferenceable>(RetPos);
  2392. }
  2393. }
  2394. for (Argument &Arg : F.args()) {
  2395. IRPosition ArgPos = IRPosition::argument(Arg);
  2396. // Every argument might be simplified. We have to go through the Attributor
  2397. // interface though as outside AAs can register custom simplification
  2398. // callbacks.
  2399. bool UsedAssumedInformation = false;
  2400. getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation);
  2401. // Every argument might be dead.
  2402. getOrCreateAAFor<AAIsDead>(ArgPos);
  2403. // Every argument might be marked noundef.
  2404. getOrCreateAAFor<AANoUndef>(ArgPos);
  2405. if (Arg.getType()->isPointerTy()) {
  2406. // Every argument with pointer type might be marked nonnull.
  2407. getOrCreateAAFor<AANonNull>(ArgPos);
  2408. // Every argument with pointer type might be marked noalias.
  2409. getOrCreateAAFor<AANoAlias>(ArgPos);
  2410. // Every argument with pointer type might be marked dereferenceable.
  2411. getOrCreateAAFor<AADereferenceable>(ArgPos);
  2412. // Every argument with pointer type might be marked align.
  2413. getOrCreateAAFor<AAAlign>(ArgPos);
  2414. // Every argument with pointer type might be marked nocapture.
  2415. getOrCreateAAFor<AANoCapture>(ArgPos);
  2416. // Every argument with pointer type might be marked
  2417. // "readnone/readonly/writeonly/..."
  2418. getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
  2419. // Every argument with pointer type might be marked nofree.
  2420. getOrCreateAAFor<AANoFree>(ArgPos);
  2421. // Every argument with pointer type might be privatizable (or promotable)
  2422. getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
  2423. }
  2424. }
  2425. auto CallSitePred = [&](Instruction &I) -> bool {
  2426. auto &CB = cast<CallBase>(I);
  2427. IRPosition CBInstPos = IRPosition::inst(CB);
  2428. IRPosition CBFnPos = IRPosition::callsite_function(CB);
  2429. // Call sites might be dead if they do not have side effects and no live
  2430. // users. The return value might be dead if there are no live users.
  2431. getOrCreateAAFor<AAIsDead>(CBInstPos);
  2432. Function *Callee = CB.getCalledFunction();
  2433. // TODO: Even if the callee is not known now we might be able to simplify
  2434. // the call/callee.
  2435. if (!Callee)
  2436. return true;
  2437. // Every call site can track active assumptions.
  2438. getOrCreateAAFor<AAAssumptionInfo>(CBFnPos);
  2439. // Skip declarations except if annotations on their call sites were
  2440. // explicitly requested.
  2441. if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
  2442. !Callee->hasMetadata(LLVMContext::MD_callback))
  2443. return true;
  2444. if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
  2445. IRPosition CBRetPos = IRPosition::callsite_returned(CB);
  2446. getOrCreateAAFor<AAValueSimplify>(CBRetPos);
  2447. }
  2448. for (int I = 0, E = CB.arg_size(); I < E; ++I) {
  2449. IRPosition CBArgPos = IRPosition::callsite_argument(CB, I);
  2450. // Every call site argument might be dead.
  2451. getOrCreateAAFor<AAIsDead>(CBArgPos);
  2452. // Call site argument might be simplified. We have to go through the
  2453. // Attributor interface though as outside AAs can register custom
  2454. // simplification callbacks.
  2455. bool UsedAssumedInformation = false;
  2456. getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation);
  2457. // Every call site argument might be marked "noundef".
  2458. getOrCreateAAFor<AANoUndef>(CBArgPos);
  2459. if (!CB.getArgOperand(I)->getType()->isPointerTy())
  2460. continue;
  2461. // Call site argument attribute "non-null".
  2462. getOrCreateAAFor<AANonNull>(CBArgPos);
  2463. // Call site argument attribute "nocapture".
  2464. getOrCreateAAFor<AANoCapture>(CBArgPos);
  2465. // Call site argument attribute "no-alias".
  2466. getOrCreateAAFor<AANoAlias>(CBArgPos);
  2467. // Call site argument attribute "dereferenceable".
  2468. getOrCreateAAFor<AADereferenceable>(CBArgPos);
  2469. // Call site argument attribute "align".
  2470. getOrCreateAAFor<AAAlign>(CBArgPos);
  2471. // Call site argument attribute
  2472. // "readnone/readonly/writeonly/..."
  2473. getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);
  2474. // Call site argument attribute "nofree".
  2475. getOrCreateAAFor<AANoFree>(CBArgPos);
  2476. }
  2477. return true;
  2478. };
  2479. auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
  2480. bool Success;
  2481. bool UsedAssumedInformation = false;
  2482. Success = checkForAllInstructionsImpl(
  2483. nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
  2484. {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
  2485. (unsigned)Instruction::Call},
  2486. UsedAssumedInformation);
  2487. (void)Success;
  2488. assert(Success && "Expected the check call to be successful!");
  2489. auto LoadStorePred = [&](Instruction &I) -> bool {
  2490. if (isa<LoadInst>(I)) {
  2491. getOrCreateAAFor<AAAlign>(
  2492. IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
  2493. if (SimplifyAllLoads)
  2494. getOrCreateAAFor<AAValueSimplify>(IRPosition::value(I));
  2495. } else
  2496. getOrCreateAAFor<AAAlign>(
  2497. IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
  2498. return true;
  2499. };
  2500. Success = checkForAllInstructionsImpl(
  2501. nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
  2502. {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
  2503. UsedAssumedInformation);
  2504. (void)Success;
  2505. assert(Success && "Expected the check call to be successful!");
  2506. }
  2507. /// Helpers to ease debugging through output streams and print calls.
  2508. ///
  2509. ///{
  2510. raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
  2511. return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
  2512. }
  2513. raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
  2514. switch (AP) {
  2515. case IRPosition::IRP_INVALID:
  2516. return OS << "inv";
  2517. case IRPosition::IRP_FLOAT:
  2518. return OS << "flt";
  2519. case IRPosition::IRP_RETURNED:
  2520. return OS << "fn_ret";
  2521. case IRPosition::IRP_CALL_SITE_RETURNED:
  2522. return OS << "cs_ret";
  2523. case IRPosition::IRP_FUNCTION:
  2524. return OS << "fn";
  2525. case IRPosition::IRP_CALL_SITE:
  2526. return OS << "cs";
  2527. case IRPosition::IRP_ARGUMENT:
  2528. return OS << "arg";
  2529. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  2530. return OS << "cs_arg";
  2531. }
  2532. llvm_unreachable("Unknown attribute position!");
  2533. }
  2534. raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
  2535. const Value &AV = Pos.getAssociatedValue();
  2536. OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
  2537. << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
  2538. if (Pos.hasCallBaseContext())
  2539. OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
  2540. return OS << "}";
  2541. }
  2542. raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
  2543. OS << "range-state(" << S.getBitWidth() << ")<";
  2544. S.getKnown().print(OS);
  2545. OS << " / ";
  2546. S.getAssumed().print(OS);
  2547. OS << ">";
  2548. return OS << static_cast<const AbstractState &>(S);
  2549. }
  2550. raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
  2551. return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
  2552. }
  2553. raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
  2554. AA.print(OS);
  2555. return OS;
  2556. }
  2557. raw_ostream &llvm::operator<<(raw_ostream &OS,
  2558. const PotentialConstantIntValuesState &S) {
  2559. OS << "set-state(< {";
  2560. if (!S.isValidState())
  2561. OS << "full-set";
  2562. else {
  2563. for (auto &it : S.getAssumedSet())
  2564. OS << it << ", ";
  2565. if (S.undefIsContained())
  2566. OS << "undef ";
  2567. }
  2568. OS << "} >)";
  2569. return OS;
  2570. }
  2571. void AbstractAttribute::print(raw_ostream &OS) const {
  2572. OS << "[";
  2573. OS << getName();
  2574. OS << "] for CtxI ";
  2575. if (auto *I = getCtxI()) {
  2576. OS << "'";
  2577. I->print(OS);
  2578. OS << "'";
  2579. } else
  2580. OS << "<<null inst>>";
  2581. OS << " at position " << getIRPosition() << " with state " << getAsStr()
  2582. << '\n';
  2583. }
  2584. void AbstractAttribute::printWithDeps(raw_ostream &OS) const {
  2585. print(OS);
  2586. for (const auto &DepAA : Deps) {
  2587. auto *AA = DepAA.getPointer();
  2588. OS << " updates ";
  2589. AA->print(OS);
  2590. }
  2591. OS << '\n';
  2592. }
  2593. raw_ostream &llvm::operator<<(raw_ostream &OS,
  2594. const AAPointerInfo::Access &Acc) {
  2595. OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
  2596. if (Acc.getLocalInst() != Acc.getRemoteInst())
  2597. OS << " via " << *Acc.getLocalInst();
  2598. if (Acc.getContent().hasValue())
  2599. OS << " [" << *Acc.getContent() << "]";
  2600. return OS;
  2601. }
  2602. ///}
  2603. /// ----------------------------------------------------------------------------
  2604. /// Pass (Manager) Boilerplate
  2605. /// ----------------------------------------------------------------------------
  2606. static bool runAttributorOnFunctions(InformationCache &InfoCache,
  2607. SetVector<Function *> &Functions,
  2608. AnalysisGetter &AG,
  2609. CallGraphUpdater &CGUpdater,
  2610. bool DeleteFns) {
  2611. if (Functions.empty())
  2612. return false;
  2613. LLVM_DEBUG({
  2614. dbgs() << "[Attributor] Run on module with " << Functions.size()
  2615. << " functions:\n";
  2616. for (Function *Fn : Functions)
  2617. dbgs() << " - " << Fn->getName() << "\n";
  2618. });
  2619. // Create an Attributor and initially empty information cache that is filled
  2620. // while we identify default attribute opportunities.
  2621. Attributor A(Functions, InfoCache, CGUpdater, /* Allowed */ nullptr,
  2622. DeleteFns);
  2623. // Create shallow wrappers for all functions that are not IPO amendable
  2624. if (AllowShallowWrappers)
  2625. for (Function *F : Functions)
  2626. if (!A.isFunctionIPOAmendable(*F))
  2627. Attributor::createShallowWrapper(*F);
  2628. // Internalize non-exact functions
  2629. // TODO: for now we eagerly internalize functions without calculating the
  2630. // cost, we need a cost interface to determine whether internalizing
  2631. // a function is "benefitial"
  2632. if (AllowDeepWrapper) {
  2633. unsigned FunSize = Functions.size();
  2634. for (unsigned u = 0; u < FunSize; u++) {
  2635. Function *F = Functions[u];
  2636. if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() &&
  2637. !GlobalValue::isInterposableLinkage(F->getLinkage())) {
  2638. Function *NewF = Attributor::internalizeFunction(*F);
  2639. assert(NewF && "Could not internalize function.");
  2640. Functions.insert(NewF);
  2641. // Update call graph
  2642. CGUpdater.replaceFunctionWith(*F, *NewF);
  2643. for (const Use &U : NewF->uses())
  2644. if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
  2645. auto *CallerF = CB->getCaller();
  2646. CGUpdater.reanalyzeFunction(*CallerF);
  2647. }
  2648. }
  2649. }
  2650. }
  2651. for (Function *F : Functions) {
  2652. if (F->hasExactDefinition())
  2653. NumFnWithExactDefinition++;
  2654. else
  2655. NumFnWithoutExactDefinition++;
  2656. // We look at internal functions only on-demand but if any use is not a
  2657. // direct call or outside the current set of analyzed functions, we have
  2658. // to do it eagerly.
  2659. if (F->hasLocalLinkage()) {
  2660. if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
  2661. const auto *CB = dyn_cast<CallBase>(U.getUser());
  2662. return CB && CB->isCallee(&U) &&
  2663. Functions.count(const_cast<Function *>(CB->getCaller()));
  2664. }))
  2665. continue;
  2666. }
  2667. // Populate the Attributor with abstract attribute opportunities in the
  2668. // function and the information cache with IR information.
  2669. A.identifyDefaultAbstractAttributes(*F);
  2670. }
  2671. ChangeStatus Changed = A.run();
  2672. LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
  2673. << " functions, result: " << Changed << ".\n");
  2674. return Changed == ChangeStatus::CHANGED;
  2675. }
  2676. void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
  2677. void AADepGraph::dumpGraph() {
  2678. static std::atomic<int> CallTimes;
  2679. std::string Prefix;
  2680. if (!DepGraphDotFileNamePrefix.empty())
  2681. Prefix = DepGraphDotFileNamePrefix;
  2682. else
  2683. Prefix = "dep_graph";
  2684. std::string Filename =
  2685. Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
  2686. outs() << "Dependency graph dump to " << Filename << ".\n";
  2687. std::error_code EC;
  2688. raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
  2689. if (!EC)
  2690. llvm::WriteGraph(File, this);
  2691. CallTimes++;
  2692. }
  2693. void AADepGraph::print() {
  2694. for (auto DepAA : SyntheticRoot.Deps)
  2695. cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
  2696. }
  2697. PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
  2698. FunctionAnalysisManager &FAM =
  2699. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  2700. AnalysisGetter AG(FAM);
  2701. SetVector<Function *> Functions;
  2702. for (Function &F : M)
  2703. Functions.insert(&F);
  2704. CallGraphUpdater CGUpdater;
  2705. BumpPtrAllocator Allocator;
  2706. InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
  2707. if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
  2708. /* DeleteFns */ true)) {
  2709. // FIXME: Think about passes we will preserve and add them here.
  2710. return PreservedAnalyses::none();
  2711. }
  2712. return PreservedAnalyses::all();
  2713. }
  2714. PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
  2715. CGSCCAnalysisManager &AM,
  2716. LazyCallGraph &CG,
  2717. CGSCCUpdateResult &UR) {
  2718. FunctionAnalysisManager &FAM =
  2719. AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
  2720. AnalysisGetter AG(FAM);
  2721. SetVector<Function *> Functions;
  2722. for (LazyCallGraph::Node &N : C)
  2723. Functions.insert(&N.getFunction());
  2724. if (Functions.empty())
  2725. return PreservedAnalyses::all();
  2726. Module &M = *Functions.back()->getParent();
  2727. CallGraphUpdater CGUpdater;
  2728. CGUpdater.initialize(CG, C, AM, UR);
  2729. BumpPtrAllocator Allocator;
  2730. InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
  2731. if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
  2732. /* DeleteFns */ false)) {
  2733. // FIXME: Think about passes we will preserve and add them here.
  2734. PreservedAnalyses PA;
  2735. PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
  2736. return PA;
  2737. }
  2738. return PreservedAnalyses::all();
  2739. }
  2740. namespace llvm {
  2741. template <> struct GraphTraits<AADepGraphNode *> {
  2742. using NodeRef = AADepGraphNode *;
  2743. using DepTy = PointerIntPair<AADepGraphNode *, 1>;
  2744. using EdgeRef = PointerIntPair<AADepGraphNode *, 1>;
  2745. static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; }
  2746. static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); }
  2747. using ChildIteratorType =
  2748. mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
  2749. using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator;
  2750. static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); }
  2751. static ChildIteratorType child_end(NodeRef N) { return N->child_end(); }
  2752. };
  2753. template <>
  2754. struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> {
  2755. static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
  2756. using nodes_iterator =
  2757. mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
  2758. static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
  2759. static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
  2760. };
  2761. template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
  2762. DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
  2763. static std::string getNodeLabel(const AADepGraphNode *Node,
  2764. const AADepGraph *DG) {
  2765. std::string AAString;
  2766. raw_string_ostream O(AAString);
  2767. Node->print(O);
  2768. return AAString;
  2769. }
  2770. };
  2771. } // end namespace llvm
  2772. namespace {
  2773. struct AttributorLegacyPass : public ModulePass {
  2774. static char ID;
  2775. AttributorLegacyPass() : ModulePass(ID) {
  2776. initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
  2777. }
  2778. bool runOnModule(Module &M) override {
  2779. if (skipModule(M))
  2780. return false;
  2781. AnalysisGetter AG;
  2782. SetVector<Function *> Functions;
  2783. for (Function &F : M)
  2784. Functions.insert(&F);
  2785. CallGraphUpdater CGUpdater;
  2786. BumpPtrAllocator Allocator;
  2787. InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
  2788. return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
  2789. /* DeleteFns*/ true);
  2790. }
  2791. void getAnalysisUsage(AnalysisUsage &AU) const override {
  2792. // FIXME: Think about passes we will preserve and add them here.
  2793. AU.addRequired<TargetLibraryInfoWrapperPass>();
  2794. }
  2795. };
  2796. struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
  2797. static char ID;
  2798. AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
  2799. initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
  2800. }
  2801. bool runOnSCC(CallGraphSCC &SCC) override {
  2802. if (skipSCC(SCC))
  2803. return false;
  2804. SetVector<Function *> Functions;
  2805. for (CallGraphNode *CGN : SCC)
  2806. if (Function *Fn = CGN->getFunction())
  2807. if (!Fn->isDeclaration())
  2808. Functions.insert(Fn);
  2809. if (Functions.empty())
  2810. return false;
  2811. AnalysisGetter AG;
  2812. CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
  2813. CallGraphUpdater CGUpdater;
  2814. CGUpdater.initialize(CG, SCC);
  2815. Module &M = *Functions.back()->getParent();
  2816. BumpPtrAllocator Allocator;
  2817. InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
  2818. return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
  2819. /* DeleteFns */ false);
  2820. }
  2821. void getAnalysisUsage(AnalysisUsage &AU) const override {
  2822. // FIXME: Think about passes we will preserve and add them here.
  2823. AU.addRequired<TargetLibraryInfoWrapperPass>();
  2824. CallGraphSCCPass::getAnalysisUsage(AU);
  2825. }
  2826. };
  2827. } // end anonymous namespace
  2828. Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
  2829. Pass *llvm::createAttributorCGSCCLegacyPass() {
  2830. return new AttributorCGSCCLegacyPass();
  2831. }
  2832. char AttributorLegacyPass::ID = 0;
  2833. char AttributorCGSCCLegacyPass::ID = 0;
  2834. INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
  2835. "Deduce and propagate attributes", false, false)
  2836. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  2837. INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
  2838. "Deduce and propagate attributes", false, false)
  2839. INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
  2840. "Deduce and propagate attributes (CGSCC pass)", false,
  2841. false)
  2842. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  2843. INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
  2844. INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
  2845. "Deduce and propagate attributes (CGSCC pass)", false,
  2846. false)