GlobalOpt.cpp 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084
  1. //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass transforms simple global variables that never have their address
  11. // taken. If obviously true, it marks read/write globals as constant, deletes
  12. // variables only stored to, etc.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/IPO.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/Analysis/ConstantFolding.h"
  23. #include "llvm/Analysis/MemoryBuiltins.h"
  24. #include "llvm/Analysis/TargetLibraryInfo.h"
  25. #include "llvm/IR/CallSite.h"
  26. #include "llvm/IR/CallingConv.h"
  27. #include "llvm/IR/Constants.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/DerivedTypes.h"
  30. #include "llvm/IR/GetElementPtrTypeIterator.h"
  31. #include "llvm/IR/Instructions.h"
  32. #include "llvm/IR/IntrinsicInst.h"
  33. #include "llvm/IR/Module.h"
  34. #include "llvm/IR/Operator.h"
  35. #include "llvm/IR/ValueHandle.h"
  36. #include "llvm/Pass.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/ErrorHandling.h"
  39. #include "llvm/Support/MathExtras.h"
  40. #include "llvm/Support/raw_ostream.h"
  41. #include "llvm/Transforms/Utils/CtorUtils.h"
  42. #include "llvm/Transforms/Utils/GlobalStatus.h"
  43. #include "llvm/Transforms/Utils/ModuleUtils.h"
  44. #include <algorithm>
  45. #include <deque>
  46. using namespace llvm;
  47. #define DEBUG_TYPE "globalopt"
  48. STATISTIC(NumMarked , "Number of globals marked constant");
  49. STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
  50. STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
  51. STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
  52. STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
  53. STATISTIC(NumDeleted , "Number of globals deleted");
  54. STATISTIC(NumFnDeleted , "Number of functions deleted");
  55. STATISTIC(NumGlobUses , "Number of global uses devirtualized");
  56. STATISTIC(NumLocalized , "Number of globals localized");
  57. STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
  58. STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
  59. STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
  60. STATISTIC(NumNestRemoved , "Number of nest attributes removed");
  61. STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
  62. STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
  63. STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
  64. namespace {
  65. struct GlobalOpt : public ModulePass {
  66. void getAnalysisUsage(AnalysisUsage &AU) const override {
  67. AU.addRequired<TargetLibraryInfoWrapperPass>();
  68. }
  69. static char ID; // Pass identification, replacement for typeid
  70. GlobalOpt() : ModulePass(ID) {
  71. initializeGlobalOptPass(*PassRegistry::getPassRegistry());
  72. }
  73. bool runOnModule(Module &M) override;
  74. private:
  75. bool OptimizeFunctions(Module &M);
  76. bool OptimizeGlobalVars(Module &M);
  77. bool OptimizeGlobalAliases(Module &M);
  78. bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
  79. bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI,
  80. const GlobalStatus &GS);
  81. bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
  82. TargetLibraryInfo *TLI;
  83. SmallSet<const Comdat *, 8> NotDiscardableComdats;
  84. };
  85. }
  86. char GlobalOpt::ID = 0;
  87. INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
  88. "Global Variable Optimizer", false, false)
  89. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  90. INITIALIZE_PASS_END(GlobalOpt, "globalopt",
  91. "Global Variable Optimizer", false, false)
  92. ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
  93. /// isLeakCheckerRoot - Is this global variable possibly used by a leak checker
  94. /// as a root? If so, we might not really want to eliminate the stores to it.
  95. static bool isLeakCheckerRoot(GlobalVariable *GV) {
  96. // A global variable is a root if it is a pointer, or could plausibly contain
  97. // a pointer. There are two challenges; one is that we could have a struct
  98. // the has an inner member which is a pointer. We recurse through the type to
  99. // detect these (up to a point). The other is that we may actually be a union
  100. // of a pointer and another type, and so our LLVM type is an integer which
  101. // gets converted into a pointer, or our type is an [i8 x #] with a pointer
  102. // potentially contained here.
  103. if (GV->hasPrivateLinkage())
  104. return false;
  105. SmallVector<Type *, 4> Types;
  106. Types.push_back(cast<PointerType>(GV->getType())->getElementType());
  107. unsigned Limit = 20;
  108. do {
  109. Type *Ty = Types.pop_back_val();
  110. switch (Ty->getTypeID()) {
  111. default: break;
  112. case Type::PointerTyID: return true;
  113. case Type::ArrayTyID:
  114. case Type::VectorTyID: {
  115. SequentialType *STy = cast<SequentialType>(Ty);
  116. Types.push_back(STy->getElementType());
  117. break;
  118. }
  119. case Type::StructTyID: {
  120. StructType *STy = cast<StructType>(Ty);
  121. if (STy->isOpaque()) return true;
  122. for (StructType::element_iterator I = STy->element_begin(),
  123. E = STy->element_end(); I != E; ++I) {
  124. Type *InnerTy = *I;
  125. if (isa<PointerType>(InnerTy)) return true;
  126. if (isa<CompositeType>(InnerTy))
  127. Types.push_back(InnerTy);
  128. }
  129. break;
  130. }
  131. }
  132. if (--Limit == 0) return true;
  133. } while (!Types.empty());
  134. return false;
  135. }
  136. /// Given a value that is stored to a global but never read, determine whether
  137. /// it's safe to remove the store and the chain of computation that feeds the
  138. /// store.
  139. static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) {
  140. do {
  141. if (isa<Constant>(V))
  142. return true;
  143. if (!V->hasOneUse())
  144. return false;
  145. if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
  146. isa<GlobalValue>(V))
  147. return false;
  148. if (isAllocationFn(V, TLI))
  149. return true;
  150. Instruction *I = cast<Instruction>(V);
  151. if (I->mayHaveSideEffects())
  152. return false;
  153. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
  154. if (!GEP->hasAllConstantIndices())
  155. return false;
  156. } else if (I->getNumOperands() != 1) {
  157. return false;
  158. }
  159. V = I->getOperand(0);
  160. } while (1);
  161. }
  162. /// CleanupPointerRootUsers - This GV is a pointer root. Loop over all users
  163. /// of the global and clean up any that obviously don't assign the global a
  164. /// value that isn't dynamically allocated.
  165. ///
  166. static bool CleanupPointerRootUsers(GlobalVariable *GV,
  167. const TargetLibraryInfo *TLI) {
  168. // A brief explanation of leak checkers. The goal is to find bugs where
  169. // pointers are forgotten, causing an accumulating growth in memory
  170. // usage over time. The common strategy for leak checkers is to whitelist the
  171. // memory pointed to by globals at exit. This is popular because it also
  172. // solves another problem where the main thread of a C++ program may shut down
  173. // before other threads that are still expecting to use those globals. To
  174. // handle that case, we expect the program may create a singleton and never
  175. // destroy it.
  176. bool Changed = false;
  177. // If Dead[n].first is the only use of a malloc result, we can delete its
  178. // chain of computation and the store to the global in Dead[n].second.
  179. SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;
  180. // Constants can't be pointers to dynamically allocated memory.
  181. for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end();
  182. UI != E;) {
  183. User *U = *UI++;
  184. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  185. Value *V = SI->getValueOperand();
  186. if (isa<Constant>(V)) {
  187. Changed = true;
  188. SI->eraseFromParent();
  189. } else if (Instruction *I = dyn_cast<Instruction>(V)) {
  190. if (I->hasOneUse())
  191. Dead.push_back(std::make_pair(I, SI));
  192. }
  193. } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
  194. if (isa<Constant>(MSI->getValue())) {
  195. Changed = true;
  196. MSI->eraseFromParent();
  197. } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
  198. if (I->hasOneUse())
  199. Dead.push_back(std::make_pair(I, MSI));
  200. }
  201. } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
  202. GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
  203. if (MemSrc && MemSrc->isConstant()) {
  204. Changed = true;
  205. MTI->eraseFromParent();
  206. } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
  207. if (I->hasOneUse())
  208. Dead.push_back(std::make_pair(I, MTI));
  209. }
  210. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
  211. if (CE->use_empty()) {
  212. CE->destroyConstant();
  213. Changed = true;
  214. }
  215. } else if (Constant *C = dyn_cast<Constant>(U)) {
  216. if (isSafeToDestroyConstant(C)) {
  217. C->destroyConstant();
  218. // This could have invalidated UI, start over from scratch.
  219. Dead.clear();
  220. CleanupPointerRootUsers(GV, TLI);
  221. return true;
  222. }
  223. }
  224. }
  225. for (int i = 0, e = Dead.size(); i != e; ++i) {
  226. if (IsSafeComputationToRemove(Dead[i].first, TLI)) {
  227. Dead[i].second->eraseFromParent();
  228. Instruction *I = Dead[i].first;
  229. do {
  230. if (isAllocationFn(I, TLI))
  231. break;
  232. Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
  233. if (!J)
  234. break;
  235. I->eraseFromParent();
  236. I = J;
  237. } while (1);
  238. I->eraseFromParent();
  239. }
  240. }
  241. return Changed;
  242. }
  243. /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
  244. /// users of the global, cleaning up the obvious ones. This is largely just a
  245. /// quick scan over the use list to clean up the easy and obvious cruft. This
  246. /// returns true if it made a change.
  247. static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
  248. const DataLayout &DL,
  249. TargetLibraryInfo *TLI) {
  250. bool Changed = false;
  251. // Note that we need to use a weak value handle for the worklist items. When
  252. // we delete a constant array, we may also be holding pointer to one of its
  253. // elements (or an element of one of its elements if we're dealing with an
  254. // array of arrays) in the worklist.
  255. SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
  256. while (!WorkList.empty()) {
  257. Value *UV = WorkList.pop_back_val();
  258. if (!UV)
  259. continue;
  260. User *U = cast<User>(UV);
  261. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  262. if (Init) {
  263. // Replace the load with the initializer.
  264. LI->replaceAllUsesWith(Init);
  265. LI->eraseFromParent();
  266. Changed = true;
  267. }
  268. } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  269. // Store must be unreachable or storing Init into the global.
  270. SI->eraseFromParent();
  271. Changed = true;
  272. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
  273. if (CE->getOpcode() == Instruction::GetElementPtr) {
  274. Constant *SubInit = nullptr;
  275. if (Init)
  276. SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
  277. Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, TLI);
  278. } else if ((CE->getOpcode() == Instruction::BitCast &&
  279. CE->getType()->isPointerTy()) ||
  280. CE->getOpcode() == Instruction::AddrSpaceCast) {
  281. // Pointer cast, delete any stores and memsets to the global.
  282. Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, TLI);
  283. }
  284. if (CE->use_empty()) {
  285. CE->destroyConstant();
  286. Changed = true;
  287. }
  288. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
  289. // Do not transform "gepinst (gep constexpr (GV))" here, because forming
  290. // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
  291. // and will invalidate our notion of what Init is.
  292. Constant *SubInit = nullptr;
  293. if (!isa<ConstantExpr>(GEP->getOperand(0))) {
  294. ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(
  295. ConstantFoldInstruction(GEP, DL, TLI));
  296. if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
  297. SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
  298. // If the initializer is an all-null value and we have an inbounds GEP,
  299. // we already know what the result of any load from that GEP is.
  300. // TODO: Handle splats.
  301. if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
  302. SubInit = Constant::getNullValue(GEP->getType()->getElementType());
  303. }
  304. Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, TLI);
  305. if (GEP->use_empty()) {
  306. GEP->eraseFromParent();
  307. Changed = true;
  308. }
  309. } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
  310. if (MI->getRawDest() == V) {
  311. MI->eraseFromParent();
  312. Changed = true;
  313. }
  314. } else if (Constant *C = dyn_cast<Constant>(U)) {
  315. // If we have a chain of dead constantexprs or other things dangling from
  316. // us, and if they are all dead, nuke them without remorse.
  317. if (isSafeToDestroyConstant(C)) {
  318. C->destroyConstant();
  319. CleanupConstantGlobalUsers(V, Init, DL, TLI);
  320. return true;
  321. }
  322. }
  323. }
  324. return Changed;
  325. }
  326. /// isSafeSROAElementUse - Return true if the specified instruction is a safe
  327. /// user of a derived expression from a global that we want to SROA.
  328. static bool isSafeSROAElementUse(Value *V) {
  329. // We might have a dead and dangling constant hanging off of here.
  330. if (Constant *C = dyn_cast<Constant>(V))
  331. return isSafeToDestroyConstant(C);
  332. Instruction *I = dyn_cast<Instruction>(V);
  333. if (!I) return false;
  334. // Loads are ok.
  335. if (isa<LoadInst>(I)) return true;
  336. // Stores *to* the pointer are ok.
  337. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  338. return SI->getOperand(0) != V;
  339. // Otherwise, it must be a GEP.
  340. GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
  341. if (!GEPI) return false;
  342. if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
  343. !cast<Constant>(GEPI->getOperand(1))->isNullValue())
  344. return false;
  345. for (User *U : GEPI->users())
  346. if (!isSafeSROAElementUse(U))
  347. return false;
  348. return true;
  349. }
  350. /// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
  351. /// Look at it and its uses and decide whether it is safe to SROA this global.
  352. ///
  353. static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
  354. // The user of the global must be a GEP Inst or a ConstantExpr GEP.
  355. if (!isa<GetElementPtrInst>(U) &&
  356. (!isa<ConstantExpr>(U) ||
  357. cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
  358. return false;
  359. // Check to see if this ConstantExpr GEP is SRA'able. In particular, we
  360. // don't like < 3 operand CE's, and we don't like non-constant integer
  361. // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some
  362. // value of C.
  363. if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
  364. !cast<Constant>(U->getOperand(1))->isNullValue() ||
  365. !isa<ConstantInt>(U->getOperand(2)))
  366. return false;
  367. gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
  368. ++GEPI; // Skip over the pointer index.
  369. // If this is a use of an array allocation, do a bit more checking for sanity.
  370. if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
  371. uint64_t NumElements = AT->getNumElements();
  372. ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
  373. // Check to make sure that index falls within the array. If not,
  374. // something funny is going on, so we won't do the optimization.
  375. //
  376. if (Idx->getZExtValue() >= NumElements)
  377. return false;
  378. // We cannot scalar repl this level of the array unless any array
  379. // sub-indices are in-range constants. In particular, consider:
  380. // A[0][i]. We cannot know that the user isn't doing invalid things like
  381. // allowing i to index an out-of-range subscript that accesses A[1].
  382. //
  383. // Scalar replacing *just* the outer index of the array is probably not
  384. // going to be a win anyway, so just give up.
  385. for (++GEPI; // Skip array index.
  386. GEPI != E;
  387. ++GEPI) {
  388. uint64_t NumElements;
  389. if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI))
  390. NumElements = SubArrayTy->getNumElements();
  391. else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI))
  392. NumElements = SubVectorTy->getNumElements();
  393. else {
  394. assert((*GEPI)->isStructTy() &&
  395. "Indexed GEP type is not array, vector, or struct!");
  396. continue;
  397. }
  398. ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
  399. if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
  400. return false;
  401. }
  402. }
  403. for (User *UU : U->users())
  404. if (!isSafeSROAElementUse(UU))
  405. return false;
  406. return true;
  407. }
  408. /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
  409. /// is safe for us to perform this transformation.
  410. ///
  411. static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
  412. for (User *U : GV->users())
  413. if (!IsUserOfGlobalSafeForSRA(U, GV))
  414. return false;
  415. return true;
  416. }
  417. /// SRAGlobal - Perform scalar replacement of aggregates on the specified global
  418. /// variable. This opens the door for other optimizations by exposing the
  419. /// behavior of the program in a more fine-grained way. We have determined that
  420. /// this transformation is safe already. We return the first global variable we
  421. /// insert so that the caller can reprocess it.
  422. static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
  423. // Make sure this global only has simple uses that we can SRA.
  424. if (!GlobalUsersSafeToSRA(GV))
  425. return nullptr;
  426. assert(GV->hasLocalLinkage() && !GV->isConstant());
  427. Constant *Init = GV->getInitializer();
  428. Type *Ty = Init->getType();
  429. std::vector<GlobalVariable*> NewGlobals;
  430. Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
  431. // Get the alignment of the global, either explicit or target-specific.
  432. unsigned StartAlignment = GV->getAlignment();
  433. if (StartAlignment == 0)
  434. StartAlignment = DL.getABITypeAlignment(GV->getType());
  435. if (StructType *STy = dyn_cast<StructType>(Ty)) {
  436. NewGlobals.reserve(STy->getNumElements());
  437. const StructLayout &Layout = *DL.getStructLayout(STy);
  438. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  439. Constant *In = Init->getAggregateElement(i);
  440. assert(In && "Couldn't get element of initializer?");
  441. GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
  442. GlobalVariable::InternalLinkage,
  443. In, GV->getName()+"."+Twine(i),
  444. GV->getThreadLocalMode(),
  445. GV->getType()->getAddressSpace());
  446. Globals.insert(GV, NGV);
  447. NewGlobals.push_back(NGV);
  448. // Calculate the known alignment of the field. If the original aggregate
  449. // had 256 byte alignment for example, something might depend on that:
  450. // propagate info to each field.
  451. uint64_t FieldOffset = Layout.getElementOffset(i);
  452. unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
  453. if (NewAlign > DL.getABITypeAlignment(STy->getElementType(i)))
  454. NGV->setAlignment(NewAlign);
  455. }
  456. } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
  457. unsigned NumElements = 0;
  458. if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
  459. NumElements = ATy->getNumElements();
  460. else
  461. NumElements = cast<VectorType>(STy)->getNumElements();
  462. if (NumElements > 16 && GV->hasNUsesOrMore(16))
  463. return nullptr; // It's not worth it.
  464. NewGlobals.reserve(NumElements);
  465. uint64_t EltSize = DL.getTypeAllocSize(STy->getElementType());
  466. unsigned EltAlign = DL.getABITypeAlignment(STy->getElementType());
  467. for (unsigned i = 0, e = NumElements; i != e; ++i) {
  468. Constant *In = Init->getAggregateElement(i);
  469. assert(In && "Couldn't get element of initializer?");
  470. GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
  471. GlobalVariable::InternalLinkage,
  472. In, GV->getName()+"."+Twine(i),
  473. GV->getThreadLocalMode(),
  474. GV->getType()->getAddressSpace());
  475. Globals.insert(GV, NGV);
  476. NewGlobals.push_back(NGV);
  477. // Calculate the known alignment of the field. If the original aggregate
  478. // had 256 byte alignment for example, something might depend on that:
  479. // propagate info to each field.
  480. unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
  481. if (NewAlign > EltAlign)
  482. NGV->setAlignment(NewAlign);
  483. }
  484. }
  485. if (NewGlobals.empty())
  486. return nullptr;
  487. DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV);
  488. Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
  489. // Loop over all of the uses of the global, replacing the constantexpr geps,
  490. // with smaller constantexpr geps or direct references.
  491. while (!GV->use_empty()) {
  492. User *GEP = GV->user_back();
  493. assert(((isa<ConstantExpr>(GEP) &&
  494. cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
  495. isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
  496. // Ignore the 1th operand, which has to be zero or else the program is quite
  497. // broken (undefined). Get the 2nd operand, which is the structure or array
  498. // index.
  499. unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
  500. if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
  501. Value *NewPtr = NewGlobals[Val];
  502. Type *NewTy = NewGlobals[Val]->getValueType();
  503. // Form a shorter GEP if needed.
  504. if (GEP->getNumOperands() > 3) {
  505. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
  506. SmallVector<Constant*, 8> Idxs;
  507. Idxs.push_back(NullInt);
  508. for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
  509. Idxs.push_back(CE->getOperand(i));
  510. NewPtr =
  511. ConstantExpr::getGetElementPtr(NewTy, cast<Constant>(NewPtr), Idxs);
  512. } else {
  513. GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
  514. SmallVector<Value*, 8> Idxs;
  515. Idxs.push_back(NullInt);
  516. for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
  517. Idxs.push_back(GEPI->getOperand(i));
  518. NewPtr = GetElementPtrInst::Create(
  519. NewTy, NewPtr, Idxs, GEPI->getName() + "." + Twine(Val), GEPI);
  520. }
  521. }
  522. GEP->replaceAllUsesWith(NewPtr);
  523. if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
  524. GEPI->eraseFromParent();
  525. else
  526. cast<ConstantExpr>(GEP)->destroyConstant();
  527. }
  528. // Delete the old global, now that it is dead.
  529. Globals.erase(GV);
  530. ++NumSRA;
  531. // Loop over the new globals array deleting any globals that are obviously
  532. // dead. This can arise due to scalarization of a structure or an array that
  533. // has elements that are dead.
  534. unsigned FirstGlobal = 0;
  535. for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
  536. if (NewGlobals[i]->use_empty()) {
  537. Globals.erase(NewGlobals[i]);
  538. if (FirstGlobal == i) ++FirstGlobal;
  539. }
  540. return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : nullptr;
  541. }
  542. /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
  543. /// value will trap if the value is dynamically null. PHIs keeps track of any
  544. /// phi nodes we've seen to avoid reprocessing them.
  545. static bool AllUsesOfValueWillTrapIfNull(const Value *V,
  546. SmallPtrSetImpl<const PHINode*> &PHIs) {
  547. for (const User *U : V->users())
  548. if (isa<LoadInst>(U)) {
  549. // Will trap.
  550. } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
  551. if (SI->getOperand(0) == V) {
  552. //cerr << "NONTRAPPING USE: " << *U;
  553. return false; // Storing the value.
  554. }
  555. } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
  556. if (CI->getCalledValue() != V) {
  557. //cerr << "NONTRAPPING USE: " << *U;
  558. return false; // Not calling the ptr
  559. }
  560. } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
  561. if (II->getCalledValue() != V) {
  562. //cerr << "NONTRAPPING USE: " << *U;
  563. return false; // Not calling the ptr
  564. }
  565. } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
  566. if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
  567. } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
  568. if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
  569. } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
  570. // If we've already seen this phi node, ignore it, it has already been
  571. // checked.
  572. if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
  573. return false;
  574. } else if (isa<ICmpInst>(U) &&
  575. isa<ConstantPointerNull>(U->getOperand(1))) {
  576. // Ignore icmp X, null
  577. } else {
  578. //cerr << "NONTRAPPING USE: " << *U;
  579. return false;
  580. }
  581. return true;
  582. }
  583. /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
  584. /// from GV will trap if the loaded value is null. Note that this also permits
  585. /// comparisons of the loaded value against null, as a special case.
  586. static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
  587. for (const User *U : GV->users())
  588. if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
  589. SmallPtrSet<const PHINode*, 8> PHIs;
  590. if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
  591. return false;
  592. } else if (isa<StoreInst>(U)) {
  593. // Ignore stores to the global.
  594. } else {
  595. // We don't know or understand this user, bail out.
  596. //cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
  597. return false;
  598. }
  599. return true;
  600. }
  601. static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
  602. bool Changed = false;
  603. for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
  604. Instruction *I = cast<Instruction>(*UI++);
  605. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  606. LI->setOperand(0, NewV);
  607. Changed = true;
  608. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  609. if (SI->getOperand(1) == V) {
  610. SI->setOperand(1, NewV);
  611. Changed = true;
  612. }
  613. } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
  614. CallSite CS(I);
  615. if (CS.getCalledValue() == V) {
  616. // Calling through the pointer! Turn into a direct call, but be careful
  617. // that the pointer is not also being passed as an argument.
  618. CS.setCalledFunction(NewV);
  619. Changed = true;
  620. bool PassedAsArg = false;
  621. for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
  622. if (CS.getArgument(i) == V) {
  623. PassedAsArg = true;
  624. CS.setArgument(i, NewV);
  625. }
  626. if (PassedAsArg) {
  627. // Being passed as an argument also. Be careful to not invalidate UI!
  628. UI = V->user_begin();
  629. }
  630. }
  631. } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
  632. Changed |= OptimizeAwayTrappingUsesOfValue(CI,
  633. ConstantExpr::getCast(CI->getOpcode(),
  634. NewV, CI->getType()));
  635. if (CI->use_empty()) {
  636. Changed = true;
  637. CI->eraseFromParent();
  638. }
  639. } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
  640. // Should handle GEP here.
  641. SmallVector<Constant*, 8> Idxs;
  642. Idxs.reserve(GEPI->getNumOperands()-1);
  643. for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
  644. i != e; ++i)
  645. if (Constant *C = dyn_cast<Constant>(*i))
  646. Idxs.push_back(C);
  647. else
  648. break;
  649. if (Idxs.size() == GEPI->getNumOperands()-1)
  650. Changed |= OptimizeAwayTrappingUsesOfValue(
  651. GEPI, ConstantExpr::getGetElementPtr(nullptr, NewV, Idxs));
  652. if (GEPI->use_empty()) {
  653. Changed = true;
  654. GEPI->eraseFromParent();
  655. }
  656. }
  657. }
  658. return Changed;
  659. }
  660. /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
  661. /// value stored into it. If there are uses of the loaded value that would trap
  662. /// if the loaded value is dynamically null, then we know that they cannot be
  663. /// reachable with a null optimize away the load.
  664. static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
  665. const DataLayout &DL,
  666. TargetLibraryInfo *TLI) {
  667. bool Changed = false;
  668. // Keep track of whether we are able to remove all the uses of the global
  669. // other than the store that defines it.
  670. bool AllNonStoreUsesGone = true;
  671. // Replace all uses of loads with uses of uses of the stored value.
  672. for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){
  673. User *GlobalUser = *GUI++;
  674. if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
  675. Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
  676. // If we were able to delete all uses of the loads
  677. if (LI->use_empty()) {
  678. LI->eraseFromParent();
  679. Changed = true;
  680. } else {
  681. AllNonStoreUsesGone = false;
  682. }
  683. } else if (isa<StoreInst>(GlobalUser)) {
  684. // Ignore the store that stores "LV" to the global.
  685. assert(GlobalUser->getOperand(1) == GV &&
  686. "Must be storing *to* the global");
  687. } else {
  688. AllNonStoreUsesGone = false;
  689. // If we get here we could have other crazy uses that are transitively
  690. // loaded.
  691. assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
  692. isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
  693. isa<BitCastInst>(GlobalUser) ||
  694. isa<GetElementPtrInst>(GlobalUser)) &&
  695. "Only expect load and stores!");
  696. }
  697. }
  698. if (Changed) {
  699. DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
  700. ++NumGlobUses;
  701. }
  702. // If we nuked all of the loads, then none of the stores are needed either,
  703. // nor is the global.
  704. if (AllNonStoreUsesGone) {
  705. if (isLeakCheckerRoot(GV)) {
  706. Changed |= CleanupPointerRootUsers(GV, TLI);
  707. } else {
  708. Changed = true;
  709. CleanupConstantGlobalUsers(GV, nullptr, DL, TLI);
  710. }
  711. if (GV->use_empty()) {
  712. DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
  713. Changed = true;
  714. GV->eraseFromParent();
  715. ++NumDeleted;
  716. }
  717. }
  718. return Changed;
  719. }
  720. /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
  721. /// instructions that are foldable.
  722. static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
  723. TargetLibraryInfo *TLI) {
  724. for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
  725. if (Instruction *I = dyn_cast<Instruction>(*UI++))
  726. if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
  727. I->replaceAllUsesWith(NewC);
  728. // Advance UI to the next non-I use to avoid invalidating it!
  729. // Instructions could multiply use V.
  730. while (UI != E && *UI == I)
  731. ++UI;
  732. I->eraseFromParent();
  733. }
  734. }
  735. /// OptimizeGlobalAddressOfMalloc - This function takes the specified global
  736. /// variable, and transforms the program as if it always contained the result of
  737. /// the specified malloc. Because it is always the result of the specified
  738. /// malloc, there is no reason to actually DO the malloc. Instead, turn the
  739. /// malloc into a global, and any loads of GV as uses of the new global.
  740. static GlobalVariable *
  741. OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, CallInst *CI, Type *AllocTy,
  742. ConstantInt *NElements, const DataLayout &DL,
  743. TargetLibraryInfo *TLI) {
  744. DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
  745. Type *GlobalType;
  746. if (NElements->getZExtValue() == 1)
  747. GlobalType = AllocTy;
  748. else
  749. // If we have an array allocation, the global variable is of an array.
  750. GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
  751. // Create the new global variable. The contents of the malloc'd memory is
  752. // undefined, so initialize with an undef value.
  753. GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
  754. GlobalType, false,
  755. GlobalValue::InternalLinkage,
  756. UndefValue::get(GlobalType),
  757. GV->getName()+".body",
  758. GV,
  759. GV->getThreadLocalMode());
  760. // If there are bitcast users of the malloc (which is typical, usually we have
  761. // a malloc + bitcast) then replace them with uses of the new global. Update
  762. // other users to use the global as well.
  763. BitCastInst *TheBC = nullptr;
  764. while (!CI->use_empty()) {
  765. Instruction *User = cast<Instruction>(CI->user_back());
  766. if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
  767. if (BCI->getType() == NewGV->getType()) {
  768. BCI->replaceAllUsesWith(NewGV);
  769. BCI->eraseFromParent();
  770. } else {
  771. BCI->setOperand(0, NewGV);
  772. }
  773. } else {
  774. if (!TheBC)
  775. TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
  776. User->replaceUsesOfWith(CI, TheBC);
  777. }
  778. }
  779. Constant *RepValue = NewGV;
  780. if (NewGV->getType() != GV->getType()->getElementType())
  781. RepValue = ConstantExpr::getBitCast(RepValue,
  782. GV->getType()->getElementType());
  783. // If there is a comparison against null, we will insert a global bool to
  784. // keep track of whether the global was initialized yet or not.
  785. GlobalVariable *InitBool =
  786. new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
  787. GlobalValue::InternalLinkage,
  788. ConstantInt::getFalse(GV->getContext()),
  789. GV->getName()+".init", GV->getThreadLocalMode());
  790. bool InitBoolUsed = false;
  791. // Loop over all uses of GV, processing them in turn.
  792. while (!GV->use_empty()) {
  793. if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
  794. // The global is initialized when the store to it occurs.
  795. new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
  796. SI->getOrdering(), SI->getSynchScope(), SI);
  797. SI->eraseFromParent();
  798. continue;
  799. }
  800. LoadInst *LI = cast<LoadInst>(GV->user_back());
  801. while (!LI->use_empty()) {
  802. Use &LoadUse = *LI->use_begin();
  803. ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
  804. if (!ICI) {
  805. LoadUse = RepValue;
  806. continue;
  807. }
  808. // Replace the cmp X, 0 with a use of the bool value.
  809. // Sink the load to where the compare was, if atomic rules allow us to.
  810. Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
  811. LI->getOrdering(), LI->getSynchScope(),
  812. LI->isUnordered() ? (Instruction*)ICI : LI);
  813. InitBoolUsed = true;
  814. switch (ICI->getPredicate()) {
  815. default: llvm_unreachable("Unknown ICmp Predicate!");
  816. case ICmpInst::ICMP_ULT:
  817. case ICmpInst::ICMP_SLT: // X < null -> always false
  818. LV = ConstantInt::getFalse(GV->getContext());
  819. break;
  820. case ICmpInst::ICMP_ULE:
  821. case ICmpInst::ICMP_SLE:
  822. case ICmpInst::ICMP_EQ:
  823. LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
  824. break;
  825. case ICmpInst::ICMP_NE:
  826. case ICmpInst::ICMP_UGE:
  827. case ICmpInst::ICMP_SGE:
  828. case ICmpInst::ICMP_UGT:
  829. case ICmpInst::ICMP_SGT:
  830. break; // no change.
  831. }
  832. ICI->replaceAllUsesWith(LV);
  833. ICI->eraseFromParent();
  834. }
  835. LI->eraseFromParent();
  836. }
  837. // If the initialization boolean was used, insert it, otherwise delete it.
  838. if (!InitBoolUsed) {
  839. while (!InitBool->use_empty()) // Delete initializations
  840. cast<StoreInst>(InitBool->user_back())->eraseFromParent();
  841. delete InitBool;
  842. } else
  843. GV->getParent()->getGlobalList().insert(GV, InitBool);
  844. // Now the GV is dead, nuke it and the malloc..
  845. GV->eraseFromParent();
  846. CI->eraseFromParent();
  847. // To further other optimizations, loop over all users of NewGV and try to
  848. // constant prop them. This will promote GEP instructions with constant
  849. // indices into GEP constant-exprs, which will allow global-opt to hack on it.
  850. ConstantPropUsersOf(NewGV, DL, TLI);
  851. if (RepValue != NewGV)
  852. ConstantPropUsersOf(RepValue, DL, TLI);
  853. return NewGV;
  854. }
  855. /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
  856. /// to make sure that there are no complex uses of V. We permit simple things
  857. /// like dereferencing the pointer, but not storing through the address, unless
  858. /// it is to the specified global.
  859. static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
  860. const GlobalVariable *GV,
  861. SmallPtrSetImpl<const PHINode*> &PHIs) {
  862. for (const User *U : V->users()) {
  863. const Instruction *Inst = cast<Instruction>(U);
  864. if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
  865. continue; // Fine, ignore.
  866. }
  867. if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
  868. if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
  869. return false; // Storing the pointer itself... bad.
  870. continue; // Otherwise, storing through it, or storing into GV... fine.
  871. }
  872. // Must index into the array and into the struct.
  873. if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
  874. if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
  875. return false;
  876. continue;
  877. }
  878. if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
  879. // PHIs are ok if all uses are ok. Don't infinitely recurse through PHI
  880. // cycles.
  881. if (PHIs.insert(PN).second)
  882. if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs))
  883. return false;
  884. continue;
  885. }
  886. if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
  887. if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
  888. return false;
  889. continue;
  890. }
  891. return false;
  892. }
  893. return true;
  894. }
  895. /// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV
  896. /// somewhere. Transform all uses of the allocation into loads from the
  897. /// global and uses of the resultant pointer. Further, delete the store into
  898. /// GV. This assumes that these value pass the
  899. /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
  900. static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
  901. GlobalVariable *GV) {
  902. while (!Alloc->use_empty()) {
  903. Instruction *U = cast<Instruction>(*Alloc->user_begin());
  904. Instruction *InsertPt = U;
  905. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  906. // If this is the store of the allocation into the global, remove it.
  907. if (SI->getOperand(1) == GV) {
  908. SI->eraseFromParent();
  909. continue;
  910. }
  911. } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
  912. // Insert the load in the corresponding predecessor, not right before the
  913. // PHI.
  914. InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator();
  915. } else if (isa<BitCastInst>(U)) {
  916. // Must be bitcast between the malloc and store to initialize the global.
  917. ReplaceUsesOfMallocWithGlobal(U, GV);
  918. U->eraseFromParent();
  919. continue;
  920. } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
  921. // If this is a "GEP bitcast" and the user is a store to the global, then
  922. // just process it as a bitcast.
  923. if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
  924. if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back()))
  925. if (SI->getOperand(1) == GV) {
  926. // Must be bitcast GEP between the malloc and store to initialize
  927. // the global.
  928. ReplaceUsesOfMallocWithGlobal(GEPI, GV);
  929. GEPI->eraseFromParent();
  930. continue;
  931. }
  932. }
  933. // Insert a load from the global, and use it instead of the malloc.
  934. Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
  935. U->replaceUsesOfWith(Alloc, NL);
  936. }
  937. }
  938. /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi
  939. /// of a load) are simple enough to perform heap SRA on. This permits GEP's
  940. /// that index through the array and struct field, icmps of null, and PHIs.
  941. static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
  942. SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs,
  943. SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) {
  944. // We permit two users of the load: setcc comparing against the null
  945. // pointer, and a getelementptr of a specific form.
  946. for (const User *U : V->users()) {
  947. const Instruction *UI = cast<Instruction>(U);
  948. // Comparison against null is ok.
  949. if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) {
  950. if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
  951. return false;
  952. continue;
  953. }
  954. // getelementptr is also ok, but only a simple form.
  955. if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) {
  956. // Must index into the array and into the struct.
  957. if (GEPI->getNumOperands() < 3)
  958. return false;
  959. // Otherwise the GEP is ok.
  960. continue;
  961. }
  962. if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
  963. if (!LoadUsingPHIsPerLoad.insert(PN).second)
  964. // This means some phi nodes are dependent on each other.
  965. // Avoid infinite looping!
  966. return false;
  967. if (!LoadUsingPHIs.insert(PN).second)
  968. // If we have already analyzed this PHI, then it is safe.
  969. continue;
  970. // Make sure all uses of the PHI are simple enough to transform.
  971. if (!LoadUsesSimpleEnoughForHeapSRA(PN,
  972. LoadUsingPHIs, LoadUsingPHIsPerLoad))
  973. return false;
  974. continue;
  975. }
  976. // Otherwise we don't know what this is, not ok.
  977. return false;
  978. }
  979. return true;
  980. }
  981. /// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from
  982. /// GV are simple enough to perform HeapSRA, return true.
  983. static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
  984. Instruction *StoredVal) {
  985. SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
  986. SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
  987. for (const User *U : GV->users())
  988. if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
  989. if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
  990. LoadUsingPHIsPerLoad))
  991. return false;
  992. LoadUsingPHIsPerLoad.clear();
  993. }
  994. // If we reach here, we know that all uses of the loads and transitive uses
  995. // (through PHI nodes) are simple enough to transform. However, we don't know
  996. // that all inputs the to the PHI nodes are in the same equivalence sets.
  997. // Check to verify that all operands of the PHIs are either PHIS that can be
  998. // transformed, loads from GV, or MI itself.
  999. for (const PHINode *PN : LoadUsingPHIs) {
  1000. for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
  1001. Value *InVal = PN->getIncomingValue(op);
  1002. // PHI of the stored value itself is ok.
  1003. if (InVal == StoredVal) continue;
  1004. if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
  1005. // One of the PHIs in our set is (optimistically) ok.
  1006. if (LoadUsingPHIs.count(InPN))
  1007. continue;
  1008. return false;
  1009. }
  1010. // Load from GV is ok.
  1011. if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
  1012. if (LI->getOperand(0) == GV)
  1013. continue;
  1014. // UNDEF? NULL?
  1015. // Anything else is rejected.
  1016. return false;
  1017. }
  1018. }
  1019. return true;
  1020. }
  1021. static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
  1022. DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
  1023. std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
  1024. std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
  1025. if (FieldNo >= FieldVals.size())
  1026. FieldVals.resize(FieldNo+1);
  1027. // If we already have this value, just reuse the previously scalarized
  1028. // version.
  1029. if (Value *FieldVal = FieldVals[FieldNo])
  1030. return FieldVal;
  1031. // Depending on what instruction this is, we have several cases.
  1032. Value *Result;
  1033. if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
  1034. // This is a scalarized version of the load from the global. Just create
  1035. // a new Load of the scalarized global.
  1036. Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
  1037. InsertedScalarizedValues,
  1038. PHIsToRewrite),
  1039. LI->getName()+".f"+Twine(FieldNo), LI);
  1040. } else {
  1041. PHINode *PN = cast<PHINode>(V);
  1042. // PN's type is pointer to struct. Make a new PHI of pointer to struct
  1043. // field.
  1044. PointerType *PTy = cast<PointerType>(PN->getType());
  1045. StructType *ST = cast<StructType>(PTy->getElementType());
  1046. unsigned AS = PTy->getAddressSpace();
  1047. PHINode *NewPN =
  1048. PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS),
  1049. PN->getNumIncomingValues(),
  1050. PN->getName()+".f"+Twine(FieldNo), PN);
  1051. Result = NewPN;
  1052. PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
  1053. }
  1054. return FieldVals[FieldNo] = Result;
  1055. }
  1056. /// RewriteHeapSROALoadUser - Given a load instruction and a value derived from
  1057. /// the load, rewrite the derived value to use the HeapSRoA'd load.
  1058. static void RewriteHeapSROALoadUser(Instruction *LoadUser,
  1059. DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
  1060. std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
  1061. // If this is a comparison against null, handle it.
  1062. if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
  1063. assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
  1064. // If we have a setcc of the loaded pointer, we can use a setcc of any
  1065. // field.
  1066. Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
  1067. InsertedScalarizedValues, PHIsToRewrite);
  1068. Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
  1069. Constant::getNullValue(NPtr->getType()),
  1070. SCI->getName());
  1071. SCI->replaceAllUsesWith(New);
  1072. SCI->eraseFromParent();
  1073. return;
  1074. }
  1075. // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
  1076. if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
  1077. assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
  1078. && "Unexpected GEPI!");
  1079. // Load the pointer for this field.
  1080. unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
  1081. Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
  1082. InsertedScalarizedValues, PHIsToRewrite);
  1083. // Create the new GEP idx vector.
  1084. SmallVector<Value*, 8> GEPIdx;
  1085. GEPIdx.push_back(GEPI->getOperand(1));
  1086. GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
  1087. Value *NGEPI = GetElementPtrInst::Create(GEPI->getResultElementType(), NewPtr, GEPIdx,
  1088. GEPI->getName(), GEPI);
  1089. GEPI->replaceAllUsesWith(NGEPI);
  1090. GEPI->eraseFromParent();
  1091. return;
  1092. }
  1093. // Recursively transform the users of PHI nodes. This will lazily create the
  1094. // PHIs that are needed for individual elements. Keep track of what PHIs we
  1095. // see in InsertedScalarizedValues so that we don't get infinite loops (very
  1096. // antisocial). If the PHI is already in InsertedScalarizedValues, it has
  1097. // already been seen first by another load, so its uses have already been
  1098. // processed.
  1099. PHINode *PN = cast<PHINode>(LoadUser);
  1100. if (!InsertedScalarizedValues.insert(std::make_pair(PN,
  1101. std::vector<Value*>())).second)
  1102. return;
  1103. // If this is the first time we've seen this PHI, recursively process all
  1104. // users.
  1105. for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
  1106. Instruction *User = cast<Instruction>(*UI++);
  1107. RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
  1108. }
  1109. }
  1110. /// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr
  1111. /// is a value loaded from the global. Eliminate all uses of Ptr, making them
  1112. /// use FieldGlobals instead. All uses of loaded values satisfy
  1113. /// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
  1114. static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
  1115. DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
  1116. std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
  1117. for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) {
  1118. Instruction *User = cast<Instruction>(*UI++);
  1119. RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
  1120. }
  1121. if (Load->use_empty()) {
  1122. Load->eraseFromParent();
  1123. InsertedScalarizedValues.erase(Load);
  1124. }
  1125. }
  1126. /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
  1127. /// it up into multiple allocations of arrays of the fields.
  1128. static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
  1129. Value *NElems, const DataLayout &DL,
  1130. const TargetLibraryInfo *TLI) {
  1131. DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
  1132. Type *MAT = getMallocAllocatedType(CI, TLI);
  1133. StructType *STy = cast<StructType>(MAT);
  1134. // There is guaranteed to be at least one use of the malloc (storing
  1135. // it into GV). If there are other uses, change them to be uses of
  1136. // the global to simplify later code. This also deletes the store
  1137. // into GV.
  1138. ReplaceUsesOfMallocWithGlobal(CI, GV);
  1139. // Okay, at this point, there are no users of the malloc. Insert N
  1140. // new mallocs at the same place as CI, and N globals.
  1141. std::vector<Value*> FieldGlobals;
  1142. std::vector<Value*> FieldMallocs;
  1143. unsigned AS = GV->getType()->getPointerAddressSpace();
  1144. for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
  1145. Type *FieldTy = STy->getElementType(FieldNo);
  1146. PointerType *PFieldTy = PointerType::get(FieldTy, AS);
  1147. GlobalVariable *NGV =
  1148. new GlobalVariable(*GV->getParent(),
  1149. PFieldTy, false, GlobalValue::InternalLinkage,
  1150. Constant::getNullValue(PFieldTy),
  1151. GV->getName() + ".f" + Twine(FieldNo), GV,
  1152. GV->getThreadLocalMode());
  1153. FieldGlobals.push_back(NGV);
  1154. unsigned TypeSize = DL.getTypeAllocSize(FieldTy);
  1155. if (StructType *ST = dyn_cast<StructType>(FieldTy))
  1156. TypeSize = DL.getStructLayout(ST)->getSizeInBytes();
  1157. Type *IntPtrTy = DL.getIntPtrType(CI->getType());
  1158. Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
  1159. ConstantInt::get(IntPtrTy, TypeSize),
  1160. NElems, nullptr,
  1161. CI->getName() + ".f" + Twine(FieldNo));
  1162. FieldMallocs.push_back(NMI);
  1163. new StoreInst(NMI, NGV, CI);
  1164. }
  1165. // The tricky aspect of this transformation is handling the case when malloc
  1166. // fails. In the original code, malloc failing would set the result pointer
  1167. // of malloc to null. In this case, some mallocs could succeed and others
  1168. // could fail. As such, we emit code that looks like this:
  1169. // F0 = malloc(field0)
  1170. // F1 = malloc(field1)
  1171. // F2 = malloc(field2)
  1172. // if (F0 == 0 || F1 == 0 || F2 == 0) {
  1173. // if (F0) { free(F0); F0 = 0; }
  1174. // if (F1) { free(F1); F1 = 0; }
  1175. // if (F2) { free(F2); F2 = 0; }
  1176. // }
  1177. // The malloc can also fail if its argument is too large.
  1178. Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
  1179. Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
  1180. ConstantZero, "isneg");
  1181. for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
  1182. Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
  1183. Constant::getNullValue(FieldMallocs[i]->getType()),
  1184. "isnull");
  1185. RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
  1186. }
  1187. // Split the basic block at the old malloc.
  1188. BasicBlock *OrigBB = CI->getParent();
  1189. BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
  1190. // Create the block to check the first condition. Put all these blocks at the
  1191. // end of the function as they are unlikely to be executed.
  1192. BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
  1193. "malloc_ret_null",
  1194. OrigBB->getParent());
  1195. // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
  1196. // branch on RunningOr.
  1197. OrigBB->getTerminator()->eraseFromParent();
  1198. BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
  1199. // Within the NullPtrBlock, we need to emit a comparison and branch for each
  1200. // pointer, because some may be null while others are not.
  1201. for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
  1202. Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
  1203. Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
  1204. Constant::getNullValue(GVVal->getType()));
  1205. BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
  1206. OrigBB->getParent());
  1207. BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
  1208. OrigBB->getParent());
  1209. Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
  1210. Cmp, NullPtrBlock);
  1211. // Fill in FreeBlock.
  1212. CallInst::CreateFree(GVVal, BI);
  1213. new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
  1214. FreeBlock);
  1215. BranchInst::Create(NextBlock, FreeBlock);
  1216. NullPtrBlock = NextBlock;
  1217. }
  1218. BranchInst::Create(ContBB, NullPtrBlock);
  1219. // CI is no longer needed, remove it.
  1220. CI->eraseFromParent();
  1221. /// InsertedScalarizedLoads - As we process loads, if we can't immediately
  1222. /// update all uses of the load, keep track of what scalarized loads are
  1223. /// inserted for a given load.
  1224. DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
  1225. InsertedScalarizedValues[GV] = FieldGlobals;
  1226. std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
  1227. // Okay, the malloc site is completely handled. All of the uses of GV are now
  1228. // loads, and all uses of those loads are simple. Rewrite them to use loads
  1229. // of the per-field globals instead.
  1230. for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) {
  1231. Instruction *User = cast<Instruction>(*UI++);
  1232. if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
  1233. RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
  1234. continue;
  1235. }
  1236. // Must be a store of null.
  1237. StoreInst *SI = cast<StoreInst>(User);
  1238. assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
  1239. "Unexpected heap-sra user!");
  1240. // Insert a store of null into each global.
  1241. for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
  1242. PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType());
  1243. Constant *Null = Constant::getNullValue(PT->getElementType());
  1244. new StoreInst(Null, FieldGlobals[i], SI);
  1245. }
  1246. // Erase the original store.
  1247. SI->eraseFromParent();
  1248. }
  1249. // While we have PHIs that are interesting to rewrite, do it.
  1250. while (!PHIsToRewrite.empty()) {
  1251. PHINode *PN = PHIsToRewrite.back().first;
  1252. unsigned FieldNo = PHIsToRewrite.back().second;
  1253. PHIsToRewrite.pop_back();
  1254. PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
  1255. assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
  1256. // Add all the incoming values. This can materialize more phis.
  1257. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  1258. Value *InVal = PN->getIncomingValue(i);
  1259. InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
  1260. PHIsToRewrite);
  1261. FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
  1262. }
  1263. }
  1264. // Drop all inter-phi links and any loads that made it this far.
  1265. for (DenseMap<Value*, std::vector<Value*> >::iterator
  1266. I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
  1267. I != E; ++I) {
  1268. if (PHINode *PN = dyn_cast<PHINode>(I->first))
  1269. PN->dropAllReferences();
  1270. else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
  1271. LI->dropAllReferences();
  1272. }
  1273. // Delete all the phis and loads now that inter-references are dead.
  1274. for (DenseMap<Value*, std::vector<Value*> >::iterator
  1275. I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
  1276. I != E; ++I) {
  1277. if (PHINode *PN = dyn_cast<PHINode>(I->first))
  1278. PN->eraseFromParent();
  1279. else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
  1280. LI->eraseFromParent();
  1281. }
  1282. // The old global is now dead, remove it.
  1283. GV->eraseFromParent();
  1284. ++NumHeapSRA;
  1285. return cast<GlobalVariable>(FieldGlobals[0]);
  1286. }
  1287. /// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a
  1288. /// pointer global variable with a single value stored it that is a malloc or
  1289. /// cast of malloc.
  1290. static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, CallInst *CI,
  1291. Type *AllocTy,
  1292. AtomicOrdering Ordering,
  1293. Module::global_iterator &GVI,
  1294. const DataLayout &DL,
  1295. TargetLibraryInfo *TLI) {
  1296. // If this is a malloc of an abstract type, don't touch it.
  1297. if (!AllocTy->isSized())
  1298. return false;
  1299. // We can't optimize this global unless all uses of it are *known* to be
  1300. // of the malloc value, not of the null initializer value (consider a use
  1301. // that compares the global's value against zero to see if the malloc has
  1302. // been reached). To do this, we check to see if all uses of the global
  1303. // would trap if the global were null: this proves that they must all
  1304. // happen after the malloc.
  1305. if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
  1306. return false;
  1307. // We can't optimize this if the malloc itself is used in a complex way,
  1308. // for example, being stored into multiple globals. This allows the
  1309. // malloc to be stored into the specified global, loaded icmp'd, and
  1310. // GEP'd. These are all things we could transform to using the global
  1311. // for.
  1312. SmallPtrSet<const PHINode*, 8> PHIs;
  1313. if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
  1314. return false;
  1315. // If we have a global that is only initialized with a fixed size malloc,
  1316. // transform the program to use global memory instead of malloc'd memory.
  1317. // This eliminates dynamic allocation, avoids an indirection accessing the
  1318. // data, and exposes the resultant global to further GlobalOpt.
  1319. // We cannot optimize the malloc if we cannot determine malloc array size.
  1320. Value *NElems = getMallocArraySize(CI, DL, TLI, true);
  1321. if (!NElems)
  1322. return false;
  1323. if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
  1324. // Restrict this transformation to only working on small allocations
  1325. // (2048 bytes currently), as we don't want to introduce a 16M global or
  1326. // something.
  1327. if (NElements->getZExtValue() * DL.getTypeAllocSize(AllocTy) < 2048) {
  1328. GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI);
  1329. return true;
  1330. }
  1331. // If the allocation is an array of structures, consider transforming this
  1332. // into multiple malloc'd arrays, one for each field. This is basically
  1333. // SRoA for malloc'd memory.
  1334. if (Ordering != NotAtomic)
  1335. return false;
  1336. // If this is an allocation of a fixed size array of structs, analyze as a
  1337. // variable size array. malloc [100 x struct],1 -> malloc struct, 100
  1338. if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
  1339. if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
  1340. AllocTy = AT->getElementType();
  1341. StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
  1342. if (!AllocSTy)
  1343. return false;
  1344. // This the structure has an unreasonable number of fields, leave it
  1345. // alone.
  1346. if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
  1347. AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
  1348. // If this is a fixed size array, transform the Malloc to be an alloc of
  1349. // structs. malloc [100 x struct],1 -> malloc struct, 100
  1350. if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
  1351. Type *IntPtrTy = DL.getIntPtrType(CI->getType());
  1352. unsigned TypeSize = DL.getStructLayout(AllocSTy)->getSizeInBytes();
  1353. Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
  1354. Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
  1355. Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
  1356. AllocSize, NumElements,
  1357. nullptr, CI->getName());
  1358. Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
  1359. CI->replaceAllUsesWith(Cast);
  1360. CI->eraseFromParent();
  1361. if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
  1362. CI = cast<CallInst>(BCI->getOperand(0));
  1363. else
  1364. CI = cast<CallInst>(Malloc);
  1365. }
  1366. GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true),
  1367. DL, TLI);
  1368. return true;
  1369. }
  1370. return false;
  1371. }
  1372. // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
  1373. // that only one value (besides its initializer) is ever stored to the global.
  1374. static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
  1375. AtomicOrdering Ordering,
  1376. Module::global_iterator &GVI,
  1377. const DataLayout &DL,
  1378. TargetLibraryInfo *TLI) {
  1379. // Ignore no-op GEPs and bitcasts.
  1380. StoredOnceVal = StoredOnceVal->stripPointerCasts();
  1381. // If we are dealing with a pointer global that is initialized to null and
  1382. // only has one (non-null) value stored into it, then we can optimize any
  1383. // users of the loaded value (often calls and loads) that would trap if the
  1384. // value was null.
  1385. if (GV->getInitializer()->getType()->isPointerTy() &&
  1386. GV->getInitializer()->isNullValue()) {
  1387. if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
  1388. if (GV->getInitializer()->getType() != SOVC->getType())
  1389. SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
  1390. // Optimize away any trapping uses of the loaded value.
  1391. if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, TLI))
  1392. return true;
  1393. } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
  1394. Type *MallocType = getMallocAllocatedType(CI, TLI);
  1395. if (MallocType &&
  1396. TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI,
  1397. DL, TLI))
  1398. return true;
  1399. }
  1400. }
  1401. return false;
  1402. }
  1403. /// TryToShrinkGlobalToBoolean - At this point, we have learned that the only
  1404. /// two values ever stored into GV are its initializer and OtherVal. See if we
  1405. /// can shrink the global into a boolean and select between the two values
  1406. /// whenever it is used. This exposes the values to other scalar optimizations.
  1407. static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
  1408. Type *GVElType = GV->getType()->getElementType();
  1409. // If GVElType is already i1, it is already shrunk. If the type of the GV is
  1410. // an FP value, pointer or vector, don't do this optimization because a select
  1411. // between them is very expensive and unlikely to lead to later
  1412. // simplification. In these cases, we typically end up with "cond ? v1 : v2"
  1413. // where v1 and v2 both require constant pool loads, a big loss.
  1414. if (GVElType == Type::getInt1Ty(GV->getContext()) ||
  1415. GVElType->isFloatingPointTy() ||
  1416. GVElType->isPointerTy() || GVElType->isVectorTy())
  1417. return false;
  1418. // Walk the use list of the global seeing if all the uses are load or store.
  1419. // If there is anything else, bail out.
  1420. for (User *U : GV->users())
  1421. if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
  1422. return false;
  1423. DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV);
  1424. // Create the new global, initializing it to false.
  1425. GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
  1426. false,
  1427. GlobalValue::InternalLinkage,
  1428. ConstantInt::getFalse(GV->getContext()),
  1429. GV->getName()+".b",
  1430. GV->getThreadLocalMode(),
  1431. GV->getType()->getAddressSpace());
  1432. GV->getParent()->getGlobalList().insert(GV, NewGV);
  1433. Constant *InitVal = GV->getInitializer();
  1434. assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
  1435. "No reason to shrink to bool!");
  1436. // If initialized to zero and storing one into the global, we can use a cast
  1437. // instead of a select to synthesize the desired value.
  1438. bool IsOneZero = false;
  1439. if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
  1440. IsOneZero = InitVal->isNullValue() && CI->isOne();
  1441. while (!GV->use_empty()) {
  1442. Instruction *UI = cast<Instruction>(GV->user_back());
  1443. if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
  1444. // Change the store into a boolean store.
  1445. bool StoringOther = SI->getOperand(0) == OtherVal;
  1446. // Only do this if we weren't storing a loaded value.
  1447. Value *StoreVal;
  1448. if (StoringOther || SI->getOperand(0) == InitVal) {
  1449. StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
  1450. StoringOther);
  1451. } else {
  1452. // Otherwise, we are storing a previously loaded copy. To do this,
  1453. // change the copy from copying the original value to just copying the
  1454. // bool.
  1455. Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
  1456. // If we've already replaced the input, StoredVal will be a cast or
  1457. // select instruction. If not, it will be a load of the original
  1458. // global.
  1459. if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
  1460. assert(LI->getOperand(0) == GV && "Not a copy!");
  1461. // Insert a new load, to preserve the saved value.
  1462. StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
  1463. LI->getOrdering(), LI->getSynchScope(), LI);
  1464. } else {
  1465. assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
  1466. "This is not a form that we understand!");
  1467. StoreVal = StoredVal->getOperand(0);
  1468. assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
  1469. }
  1470. }
  1471. new StoreInst(StoreVal, NewGV, false, 0,
  1472. SI->getOrdering(), SI->getSynchScope(), SI);
  1473. } else {
  1474. // Change the load into a load of bool then a select.
  1475. LoadInst *LI = cast<LoadInst>(UI);
  1476. LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
  1477. LI->getOrdering(), LI->getSynchScope(), LI);
  1478. Value *NSI;
  1479. if (IsOneZero)
  1480. NSI = new ZExtInst(NLI, LI->getType(), "", LI);
  1481. else
  1482. NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
  1483. NSI->takeName(LI);
  1484. LI->replaceAllUsesWith(NSI);
  1485. }
  1486. UI->eraseFromParent();
  1487. }
  1488. // Retain the name of the old global variable. People who are debugging their
  1489. // programs may expect these variables to be named the same.
  1490. NewGV->takeName(GV);
  1491. GV->eraseFromParent();
  1492. return true;
  1493. }
  1494. /// ProcessGlobal - Analyze the specified global variable and optimize it if
  1495. /// possible. If we make a change, return true.
  1496. bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
  1497. Module::global_iterator &GVI) {
  1498. // Do more involved optimizations if the global is internal.
  1499. GV->removeDeadConstantUsers();
  1500. if (GV->use_empty()) {
  1501. DEBUG(dbgs() << "GLOBAL DEAD: " << *GV);
  1502. GV->eraseFromParent();
  1503. ++NumDeleted;
  1504. return true;
  1505. }
  1506. if (!GV->hasLocalLinkage())
  1507. return false;
  1508. GlobalStatus GS;
  1509. if (GlobalStatus::analyzeGlobal(GV, GS))
  1510. return false;
  1511. if (!GS.IsCompared && !GV->hasUnnamedAddr()) {
  1512. GV->setUnnamedAddr(true);
  1513. NumUnnamed++;
  1514. }
  1515. if (GV->isConstant() || !GV->hasInitializer())
  1516. return false;
  1517. return ProcessInternalGlobal(GV, GVI, GS);
  1518. }
  1519. /// ProcessInternalGlobal - Analyze the specified global variable and optimize
  1520. /// it if possible. If we make a change, return true.
  1521. bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
  1522. Module::global_iterator &GVI,
  1523. const GlobalStatus &GS) {
  1524. auto &DL = GV->getParent()->getDataLayout();
  1525. // If this is a first class global and has only one accessing function
  1526. // and this function is main (which we know is not recursive), we replace
  1527. // the global with a local alloca in this function.
  1528. //
  1529. // NOTE: It doesn't make sense to promote non-single-value types since we
  1530. // are just replacing static memory to stack memory.
  1531. //
  1532. // If the global is in different address space, don't bring it to stack.
  1533. if (!GS.HasMultipleAccessingFunctions &&
  1534. GS.AccessingFunction && !GS.HasNonInstructionUser &&
  1535. GV->getType()->getElementType()->isSingleValueType() &&
  1536. GS.AccessingFunction->getName() == "main" &&
  1537. GS.AccessingFunction->hasExternalLinkage() &&
  1538. GV->getType()->getAddressSpace() == 0) {
  1539. DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV);
  1540. Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
  1541. ->getEntryBlock().begin());
  1542. Type *ElemTy = GV->getType()->getElementType();
  1543. // FIXME: Pass Global's alignment when globals have alignment
  1544. AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
  1545. GV->getName(), &FirstI);
  1546. if (!isa<UndefValue>(GV->getInitializer()))
  1547. new StoreInst(GV->getInitializer(), Alloca, &FirstI);
  1548. GV->replaceAllUsesWith(Alloca);
  1549. GV->eraseFromParent();
  1550. ++NumLocalized;
  1551. return true;
  1552. }
  1553. // If the global is never loaded (but may be stored to), it is dead.
  1554. // Delete it now.
  1555. if (!GS.IsLoaded) {
  1556. DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV);
  1557. bool Changed;
  1558. if (isLeakCheckerRoot(GV)) {
  1559. // Delete any constant stores to the global.
  1560. Changed = CleanupPointerRootUsers(GV, TLI);
  1561. } else {
  1562. // Delete any stores we can find to the global. We may not be able to
  1563. // make it completely dead though.
  1564. Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
  1565. }
  1566. // If the global is dead now, delete it.
  1567. if (GV->use_empty()) {
  1568. GV->eraseFromParent();
  1569. ++NumDeleted;
  1570. Changed = true;
  1571. }
  1572. return Changed;
  1573. } else if (GS.StoredType <= GlobalStatus::InitializerStored) {
  1574. DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
  1575. GV->setConstant(true);
  1576. // Clean up any obviously simplifiable users now.
  1577. CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
  1578. // If the global is dead now, just nuke it.
  1579. if (GV->use_empty()) {
  1580. DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
  1581. << "all users and delete global!\n");
  1582. GV->eraseFromParent();
  1583. ++NumDeleted;
  1584. }
  1585. ++NumMarked;
  1586. return true;
  1587. } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
  1588. const DataLayout &DL = GV->getParent()->getDataLayout();
  1589. if (GlobalVariable *FirstNewGV = SRAGlobal(GV, DL)) {
  1590. GVI = FirstNewGV; // Don't skip the newly produced globals!
  1591. return true;
  1592. }
  1593. } else if (GS.StoredType == GlobalStatus::StoredOnce) {
  1594. // If the initial value for the global was an undef value, and if only
  1595. // one other value was stored into it, we can just change the
  1596. // initializer to be the stored value, then delete all stores to the
  1597. // global. This allows us to mark it constant.
  1598. if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
  1599. if (isa<UndefValue>(GV->getInitializer())) {
  1600. // Change the initial value here.
  1601. GV->setInitializer(SOVConstant);
  1602. // Clean up any obviously simplifiable users now.
  1603. CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
  1604. if (GV->use_empty()) {
  1605. DEBUG(dbgs() << " *** Substituting initializer allowed us to "
  1606. << "simplify all users and delete global!\n");
  1607. GV->eraseFromParent();
  1608. ++NumDeleted;
  1609. } else {
  1610. GVI = GV;
  1611. }
  1612. ++NumSubstitute;
  1613. return true;
  1614. }
  1615. // Try to optimize globals based on the knowledge that only one value
  1616. // (besides its initializer) is ever stored to the global.
  1617. if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI,
  1618. DL, TLI))
  1619. return true;
  1620. // Otherwise, if the global was not a boolean, we can shrink it to be a
  1621. // boolean.
  1622. if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) {
  1623. if (GS.Ordering == NotAtomic) {
  1624. if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
  1625. ++NumShrunkToBool;
  1626. return true;
  1627. }
  1628. }
  1629. }
  1630. }
  1631. return false;
  1632. }
  1633. /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
  1634. /// function, changing them to FastCC.
  1635. static void ChangeCalleesToFastCall(Function *F) {
  1636. for (User *U : F->users()) {
  1637. if (isa<BlockAddress>(U))
  1638. continue;
  1639. CallSite CS(cast<Instruction>(U));
  1640. CS.setCallingConv(CallingConv::Fast);
  1641. }
  1642. }
  1643. static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) {
  1644. for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
  1645. unsigned Index = Attrs.getSlotIndex(i);
  1646. if (!Attrs.getSlotAttributes(i).hasAttribute(Index, Attribute::Nest))
  1647. continue;
  1648. // There can be only one.
  1649. return Attrs.removeAttribute(C, Index, Attribute::Nest);
  1650. }
  1651. return Attrs;
  1652. }
  1653. static void RemoveNestAttribute(Function *F) {
  1654. F->setAttributes(StripNest(F->getContext(), F->getAttributes()));
  1655. for (User *U : F->users()) {
  1656. if (isa<BlockAddress>(U))
  1657. continue;
  1658. CallSite CS(cast<Instruction>(U));
  1659. CS.setAttributes(StripNest(F->getContext(), CS.getAttributes()));
  1660. }
  1661. }
  1662. /// Return true if this is a calling convention that we'd like to change. The
  1663. /// idea here is that we don't want to mess with the convention if the user
  1664. /// explicitly requested something with performance implications like coldcc,
  1665. /// GHC, or anyregcc.
  1666. static bool isProfitableToMakeFastCC(Function *F) {
  1667. CallingConv::ID CC = F->getCallingConv();
  1668. // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
  1669. return CC == CallingConv::C || CC == CallingConv::X86_ThisCall;
  1670. }
  1671. bool GlobalOpt::OptimizeFunctions(Module &M) {
  1672. bool Changed = false;
  1673. // Optimize functions.
  1674. for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
  1675. Function *F = FI++;
  1676. // Functions without names cannot be referenced outside this module.
  1677. if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
  1678. F->setLinkage(GlobalValue::InternalLinkage);
  1679. const Comdat *C = F->getComdat();
  1680. bool inComdat = C && NotDiscardableComdats.count(C);
  1681. F->removeDeadConstantUsers();
  1682. if ((!inComdat || F->hasLocalLinkage()) && F->isDefTriviallyDead()) {
  1683. F->eraseFromParent();
  1684. Changed = true;
  1685. ++NumFnDeleted;
  1686. } else if (F->hasLocalLinkage()) {
  1687. if (isProfitableToMakeFastCC(F) && !F->isVarArg() &&
  1688. !F->hasAddressTaken()) {
  1689. // If this function has a calling convention worth changing, is not a
  1690. // varargs function, and is only called directly, promote it to use the
  1691. // Fast calling convention.
  1692. F->setCallingConv(CallingConv::Fast);
  1693. ChangeCalleesToFastCall(F);
  1694. ++NumFastCallFns;
  1695. Changed = true;
  1696. }
  1697. if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) &&
  1698. !F->hasAddressTaken()) {
  1699. // The function is not used by a trampoline intrinsic, so it is safe
  1700. // to remove the 'nest' attribute.
  1701. RemoveNestAttribute(F);
  1702. ++NumNestRemoved;
  1703. Changed = true;
  1704. }
  1705. }
  1706. }
  1707. return Changed;
  1708. }
  1709. bool GlobalOpt::OptimizeGlobalVars(Module &M) {
  1710. bool Changed = false;
  1711. for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
  1712. GVI != E; ) {
  1713. GlobalVariable *GV = GVI++;
  1714. // Global variables without names cannot be referenced outside this module.
  1715. if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage())
  1716. GV->setLinkage(GlobalValue::InternalLinkage);
  1717. // Simplify the initializer.
  1718. if (GV->hasInitializer())
  1719. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
  1720. auto &DL = M.getDataLayout();
  1721. Constant *New = ConstantFoldConstantExpression(CE, DL, TLI);
  1722. if (New && New != CE)
  1723. GV->setInitializer(New);
  1724. }
  1725. if (GV->isDiscardableIfUnused()) {
  1726. if (const Comdat *C = GV->getComdat())
  1727. if (NotDiscardableComdats.count(C) && !GV->hasLocalLinkage())
  1728. continue;
  1729. Changed |= ProcessGlobal(GV, GVI);
  1730. }
  1731. }
  1732. return Changed;
  1733. }
  1734. static inline bool
  1735. isSimpleEnoughValueToCommit(Constant *C,
  1736. SmallPtrSetImpl<Constant *> &SimpleConstants,
  1737. const DataLayout &DL);
  1738. /// isSimpleEnoughValueToCommit - Return true if the specified constant can be
  1739. /// handled by the code generator. We don't want to generate something like:
  1740. /// void *X = &X/42;
  1741. /// because the code generator doesn't have a relocation that can handle that.
  1742. ///
  1743. /// This function should be called if C was not found (but just got inserted)
  1744. /// in SimpleConstants to avoid having to rescan the same constants all the
  1745. /// time.
  1746. static bool
  1747. isSimpleEnoughValueToCommitHelper(Constant *C,
  1748. SmallPtrSetImpl<Constant *> &SimpleConstants,
  1749. const DataLayout &DL) {
  1750. // Simple global addresses are supported, do not allow dllimport or
  1751. // thread-local globals.
  1752. if (auto *GV = dyn_cast<GlobalValue>(C))
  1753. return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
  1754. // Simple integer, undef, constant aggregate zero, etc are all supported.
  1755. if (C->getNumOperands() == 0 || isa<BlockAddress>(C))
  1756. return true;
  1757. // Aggregate values are safe if all their elements are.
  1758. if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
  1759. isa<ConstantVector>(C)) {
  1760. for (Value *Op : C->operands())
  1761. if (!isSimpleEnoughValueToCommit(cast<Constant>(Op), SimpleConstants, DL))
  1762. return false;
  1763. return true;
  1764. }
  1765. // We don't know exactly what relocations are allowed in constant expressions,
  1766. // so we allow &global+constantoffset, which is safe and uniformly supported
  1767. // across targets.
  1768. ConstantExpr *CE = cast<ConstantExpr>(C);
  1769. switch (CE->getOpcode()) {
  1770. case Instruction::BitCast:
  1771. // Bitcast is fine if the casted value is fine.
  1772. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  1773. case Instruction::IntToPtr:
  1774. case Instruction::PtrToInt:
  1775. // int <=> ptr is fine if the int type is the same size as the
  1776. // pointer type.
  1777. if (DL.getTypeSizeInBits(CE->getType()) !=
  1778. DL.getTypeSizeInBits(CE->getOperand(0)->getType()))
  1779. return false;
  1780. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  1781. // GEP is fine if it is simple + constant offset.
  1782. case Instruction::GetElementPtr:
  1783. for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
  1784. if (!isa<ConstantInt>(CE->getOperand(i)))
  1785. return false;
  1786. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  1787. case Instruction::Add:
  1788. // We allow simple+cst.
  1789. if (!isa<ConstantInt>(CE->getOperand(1)))
  1790. return false;
  1791. return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
  1792. }
  1793. return false;
  1794. }
  1795. static inline bool
  1796. isSimpleEnoughValueToCommit(Constant *C,
  1797. SmallPtrSetImpl<Constant *> &SimpleConstants,
  1798. const DataLayout &DL) {
  1799. // If we already checked this constant, we win.
  1800. if (!SimpleConstants.insert(C).second)
  1801. return true;
  1802. // Check the constant.
  1803. return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, DL);
  1804. }
  1805. /// isSimpleEnoughPointerToCommit - Return true if this constant is simple
  1806. /// enough for us to understand. In particular, if it is a cast to anything
  1807. /// other than from one pointer type to another pointer type, we punt.
  1808. /// We basically just support direct accesses to globals and GEP's of
  1809. /// globals. This should be kept up to date with CommitValueTo.
  1810. static bool isSimpleEnoughPointerToCommit(Constant *C) {
  1811. // Conservatively, avoid aggregate types. This is because we don't
  1812. // want to worry about them partially overlapping other stores.
  1813. if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
  1814. return false;
  1815. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
  1816. // Do not allow weak/*_odr/linkonce linkage or external globals.
  1817. return GV->hasUniqueInitializer();
  1818. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  1819. // Handle a constantexpr gep.
  1820. if (CE->getOpcode() == Instruction::GetElementPtr &&
  1821. isa<GlobalVariable>(CE->getOperand(0)) &&
  1822. cast<GEPOperator>(CE)->isInBounds()) {
  1823. GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
  1824. // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
  1825. // external globals.
  1826. if (!GV->hasUniqueInitializer())
  1827. return false;
  1828. // The first index must be zero.
  1829. ConstantInt *CI = dyn_cast<ConstantInt>(*std::next(CE->op_begin()));
  1830. if (!CI || !CI->isZero()) return false;
  1831. // The remaining indices must be compile-time known integers within the
  1832. // notional bounds of the corresponding static array types.
  1833. if (!CE->isGEPWithNoNotionalOverIndexing())
  1834. return false;
  1835. return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
  1836. // A constantexpr bitcast from a pointer to another pointer is a no-op,
  1837. // and we know how to evaluate it by moving the bitcast from the pointer
  1838. // operand to the value operand.
  1839. } else if (CE->getOpcode() == Instruction::BitCast &&
  1840. isa<GlobalVariable>(CE->getOperand(0))) {
  1841. // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
  1842. // external globals.
  1843. return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
  1844. }
  1845. }
  1846. return false;
  1847. }
  1848. /// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global
  1849. /// initializer. This returns 'Init' modified to reflect 'Val' stored into it.
  1850. /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
  1851. static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
  1852. ConstantExpr *Addr, unsigned OpNo) {
  1853. // Base case of the recursion.
  1854. if (OpNo == Addr->getNumOperands()) {
  1855. assert(Val->getType() == Init->getType() && "Type mismatch!");
  1856. return Val;
  1857. }
  1858. SmallVector<Constant*, 32> Elts;
  1859. if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
  1860. // Break up the constant into its elements.
  1861. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  1862. Elts.push_back(Init->getAggregateElement(i));
  1863. // Replace the element that we are supposed to.
  1864. ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
  1865. unsigned Idx = CU->getZExtValue();
  1866. assert(Idx < STy->getNumElements() && "Struct index out of range!");
  1867. Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
  1868. // Return the modified struct.
  1869. return ConstantStruct::get(STy, Elts);
  1870. }
  1871. ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
  1872. SequentialType *InitTy = cast<SequentialType>(Init->getType());
  1873. uint64_t NumElts;
  1874. if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy))
  1875. NumElts = ATy->getNumElements();
  1876. else
  1877. NumElts = InitTy->getVectorNumElements();
  1878. // Break up the array into elements.
  1879. for (uint64_t i = 0, e = NumElts; i != e; ++i)
  1880. Elts.push_back(Init->getAggregateElement(i));
  1881. assert(CI->getZExtValue() < NumElts);
  1882. Elts[CI->getZExtValue()] =
  1883. EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
  1884. if (Init->getType()->isArrayTy())
  1885. return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
  1886. return ConstantVector::get(Elts);
  1887. }
  1888. /// CommitValueTo - We have decided that Addr (which satisfies the predicate
  1889. /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
  1890. static void CommitValueTo(Constant *Val, Constant *Addr) {
  1891. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
  1892. assert(GV->hasInitializer());
  1893. GV->setInitializer(Val);
  1894. return;
  1895. }
  1896. ConstantExpr *CE = cast<ConstantExpr>(Addr);
  1897. GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
  1898. GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
  1899. }
  1900. namespace {
  1901. /// Evaluator - This class evaluates LLVM IR, producing the Constant
  1902. /// representing each SSA instruction. Changes to global variables are stored
  1903. /// in a mapping that can be iterated over after the evaluation is complete.
  1904. /// Once an evaluation call fails, the evaluation object should not be reused.
  1905. class Evaluator {
  1906. public:
  1907. Evaluator(const DataLayout &DL, const TargetLibraryInfo *TLI)
  1908. : DL(DL), TLI(TLI) {
  1909. ValueStack.emplace_back();
  1910. }
  1911. ~Evaluator() {
  1912. for (auto &Tmp : AllocaTmps)
  1913. // If there are still users of the alloca, the program is doing something
  1914. // silly, e.g. storing the address of the alloca somewhere and using it
  1915. // later. Since this is undefined, we'll just make it be null.
  1916. if (!Tmp->use_empty())
  1917. Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
  1918. }
  1919. /// EvaluateFunction - Evaluate a call to function F, returning true if
  1920. /// successful, false if we can't evaluate it. ActualArgs contains the formal
  1921. /// arguments for the function.
  1922. bool EvaluateFunction(Function *F, Constant *&RetVal,
  1923. const SmallVectorImpl<Constant*> &ActualArgs);
  1924. /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
  1925. /// successful, false if we can't evaluate it. NewBB returns the next BB that
  1926. /// control flows into, or null upon return.
  1927. bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
  1928. Constant *getVal(Value *V) {
  1929. if (Constant *CV = dyn_cast<Constant>(V)) return CV;
  1930. Constant *R = ValueStack.back().lookup(V);
  1931. assert(R && "Reference to an uncomputed value!");
  1932. return R;
  1933. }
  1934. void setVal(Value *V, Constant *C) {
  1935. ValueStack.back()[V] = C;
  1936. }
  1937. const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
  1938. return MutatedMemory;
  1939. }
  1940. const SmallPtrSetImpl<GlobalVariable*> &getInvariants() const {
  1941. return Invariants;
  1942. }
  1943. private:
  1944. Constant *ComputeLoadResult(Constant *P);
  1945. /// ValueStack - As we compute SSA register values, we store their contents
  1946. /// here. The back of the deque contains the current function and the stack
  1947. /// contains the values in the calling frames.
  1948. std::deque<DenseMap<Value*, Constant*>> ValueStack;
  1949. /// CallStack - This is used to detect recursion. In pathological situations
  1950. /// we could hit exponential behavior, but at least there is nothing
  1951. /// unbounded.
  1952. SmallVector<Function*, 4> CallStack;
  1953. /// MutatedMemory - For each store we execute, we update this map. Loads
  1954. /// check this to get the most up-to-date value. If evaluation is successful,
  1955. /// this state is committed to the process.
  1956. DenseMap<Constant*, Constant*> MutatedMemory;
  1957. /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
  1958. /// to represent its body. This vector is needed so we can delete the
  1959. /// temporary globals when we are done.
  1960. SmallVector<std::unique_ptr<GlobalVariable>, 32> AllocaTmps;
  1961. /// Invariants - These global variables have been marked invariant by the
  1962. /// static constructor.
  1963. SmallPtrSet<GlobalVariable*, 8> Invariants;
  1964. /// SimpleConstants - These are constants we have checked and know to be
  1965. /// simple enough to live in a static initializer of a global.
  1966. SmallPtrSet<Constant*, 8> SimpleConstants;
  1967. const DataLayout &DL;
  1968. const TargetLibraryInfo *TLI;
  1969. };
  1970. } // anonymous namespace
  1971. /// ComputeLoadResult - Return the value that would be computed by a load from
  1972. /// P after the stores reflected by 'memory' have been performed. If we can't
  1973. /// decide, return null.
  1974. Constant *Evaluator::ComputeLoadResult(Constant *P) {
  1975. // If this memory location has been recently stored, use the stored value: it
  1976. // is the most up-to-date.
  1977. DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
  1978. if (I != MutatedMemory.end()) return I->second;
  1979. // Access it.
  1980. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
  1981. if (GV->hasDefinitiveInitializer())
  1982. return GV->getInitializer();
  1983. return nullptr;
  1984. }
  1985. // Handle a constantexpr getelementptr.
  1986. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
  1987. if (CE->getOpcode() == Instruction::GetElementPtr &&
  1988. isa<GlobalVariable>(CE->getOperand(0))) {
  1989. GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
  1990. if (GV->hasDefinitiveInitializer())
  1991. return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
  1992. }
  1993. return nullptr; // don't know how to evaluate.
  1994. }
  1995. /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
  1996. /// successful, false if we can't evaluate it. NewBB returns the next BB that
  1997. /// control flows into, or null upon return.
  1998. bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
  1999. BasicBlock *&NextBB) {
  2000. // This is the main evaluation loop.
  2001. while (1) {
  2002. Constant *InstResult = nullptr;
  2003. DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
  2004. if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
  2005. if (!SI->isSimple()) {
  2006. DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
  2007. return false; // no volatile/atomic accesses.
  2008. }
  2009. Constant *Ptr = getVal(SI->getOperand(1));
  2010. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
  2011. DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
  2012. Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
  2013. DEBUG(dbgs() << "; To: " << *Ptr << "\n");
  2014. }
  2015. if (!isSimpleEnoughPointerToCommit(Ptr)) {
  2016. // If this is too complex for us to commit, reject it.
  2017. DEBUG(dbgs() << "Pointer is too complex for us to evaluate store.");
  2018. return false;
  2019. }
  2020. Constant *Val = getVal(SI->getOperand(0));
  2021. // If this might be too difficult for the backend to handle (e.g. the addr
  2022. // of one global variable divided by another) then we can't commit it.
  2023. if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
  2024. DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val
  2025. << "\n");
  2026. return false;
  2027. }
  2028. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
  2029. if (CE->getOpcode() == Instruction::BitCast) {
  2030. DEBUG(dbgs() << "Attempting to resolve bitcast on constant ptr.\n");
  2031. // If we're evaluating a store through a bitcast, then we need
  2032. // to pull the bitcast off the pointer type and push it onto the
  2033. // stored value.
  2034. Ptr = CE->getOperand(0);
  2035. Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
  2036. // In order to push the bitcast onto the stored value, a bitcast
  2037. // from NewTy to Val's type must be legal. If it's not, we can try
  2038. // introspecting NewTy to find a legal conversion.
  2039. while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) {
  2040. // If NewTy is a struct, we can convert the pointer to the struct
  2041. // into a pointer to its first member.
  2042. // FIXME: This could be extended to support arrays as well.
  2043. if (StructType *STy = dyn_cast<StructType>(NewTy)) {
  2044. NewTy = STy->getTypeAtIndex(0U);
  2045. IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
  2046. Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
  2047. Constant * const IdxList[] = {IdxZero, IdxZero};
  2048. Ptr = ConstantExpr::getGetElementPtr(nullptr, Ptr, IdxList);
  2049. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
  2050. Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
  2051. // If we can't improve the situation by introspecting NewTy,
  2052. // we have to give up.
  2053. } else {
  2054. DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
  2055. "evaluate.\n");
  2056. return false;
  2057. }
  2058. }
  2059. // If we found compatible types, go ahead and push the bitcast
  2060. // onto the stored value.
  2061. Val = ConstantExpr::getBitCast(Val, NewTy);
  2062. DEBUG(dbgs() << "Evaluated bitcast: " << *Val << "\n");
  2063. }
  2064. }
  2065. MutatedMemory[Ptr] = Val;
  2066. } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
  2067. InstResult = ConstantExpr::get(BO->getOpcode(),
  2068. getVal(BO->getOperand(0)),
  2069. getVal(BO->getOperand(1)));
  2070. DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: " << *InstResult
  2071. << "\n");
  2072. } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
  2073. InstResult = ConstantExpr::getCompare(CI->getPredicate(),
  2074. getVal(CI->getOperand(0)),
  2075. getVal(CI->getOperand(1)));
  2076. DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
  2077. << "\n");
  2078. } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
  2079. InstResult = ConstantExpr::getCast(CI->getOpcode(),
  2080. getVal(CI->getOperand(0)),
  2081. CI->getType());
  2082. DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
  2083. << "\n");
  2084. } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
  2085. InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
  2086. getVal(SI->getOperand(1)),
  2087. getVal(SI->getOperand(2)));
  2088. DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
  2089. << "\n");
  2090. } else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
  2091. InstResult = ConstantExpr::getExtractValue(
  2092. getVal(EVI->getAggregateOperand()), EVI->getIndices());
  2093. DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " << *InstResult
  2094. << "\n");
  2095. } else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
  2096. InstResult = ConstantExpr::getInsertValue(
  2097. getVal(IVI->getAggregateOperand()),
  2098. getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
  2099. DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " << *InstResult
  2100. << "\n");
  2101. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
  2102. Constant *P = getVal(GEP->getOperand(0));
  2103. SmallVector<Constant*, 8> GEPOps;
  2104. for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
  2105. i != e; ++i)
  2106. GEPOps.push_back(getVal(*i));
  2107. InstResult =
  2108. ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
  2109. cast<GEPOperator>(GEP)->isInBounds());
  2110. DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult
  2111. << "\n");
  2112. } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
  2113. if (!LI->isSimple()) {
  2114. DEBUG(dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
  2115. return false; // no volatile/atomic accesses.
  2116. }
  2117. Constant *Ptr = getVal(LI->getOperand(0));
  2118. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
  2119. Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
  2120. DEBUG(dbgs() << "Found a constant pointer expression, constant "
  2121. "folding: " << *Ptr << "\n");
  2122. }
  2123. InstResult = ComputeLoadResult(Ptr);
  2124. if (!InstResult) {
  2125. DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load."
  2126. "\n");
  2127. return false; // Could not evaluate load.
  2128. }
  2129. DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
  2130. } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
  2131. if (AI->isArrayAllocation()) {
  2132. DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
  2133. return false; // Cannot handle array allocs.
  2134. }
  2135. Type *Ty = AI->getType()->getElementType();
  2136. AllocaTmps.push_back(
  2137. make_unique<GlobalVariable>(Ty, false, GlobalValue::InternalLinkage,
  2138. UndefValue::get(Ty), AI->getName()));
  2139. InstResult = AllocaTmps.back().get();
  2140. DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
  2141. } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
  2142. CallSite CS(CurInst);
  2143. // Debug info can safely be ignored here.
  2144. if (isa<DbgInfoIntrinsic>(CS.getInstruction())) {
  2145. DEBUG(dbgs() << "Ignoring debug info.\n");
  2146. ++CurInst;
  2147. continue;
  2148. }
  2149. // Cannot handle inline asm.
  2150. if (isa<InlineAsm>(CS.getCalledValue())) {
  2151. DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
  2152. return false;
  2153. }
  2154. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
  2155. if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
  2156. if (MSI->isVolatile()) {
  2157. DEBUG(dbgs() << "Can not optimize a volatile memset " <<
  2158. "intrinsic.\n");
  2159. return false;
  2160. }
  2161. Constant *Ptr = getVal(MSI->getDest());
  2162. Constant *Val = getVal(MSI->getValue());
  2163. Constant *DestVal = ComputeLoadResult(getVal(Ptr));
  2164. if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
  2165. // This memset is a no-op.
  2166. DEBUG(dbgs() << "Ignoring no-op memset.\n");
  2167. ++CurInst;
  2168. continue;
  2169. }
  2170. }
  2171. if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
  2172. II->getIntrinsicID() == Intrinsic::lifetime_end) {
  2173. DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
  2174. ++CurInst;
  2175. continue;
  2176. }
  2177. if (II->getIntrinsicID() == Intrinsic::invariant_start) {
  2178. // We don't insert an entry into Values, as it doesn't have a
  2179. // meaningful return value.
  2180. if (!II->use_empty()) {
  2181. DEBUG(dbgs() << "Found unused invariant_start. Can't evaluate.\n");
  2182. return false;
  2183. }
  2184. ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
  2185. Value *PtrArg = getVal(II->getArgOperand(1));
  2186. Value *Ptr = PtrArg->stripPointerCasts();
  2187. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
  2188. Type *ElemTy = cast<PointerType>(GV->getType())->getElementType();
  2189. if (!Size->isAllOnesValue() &&
  2190. Size->getValue().getLimitedValue() >=
  2191. DL.getTypeStoreSize(ElemTy)) {
  2192. Invariants.insert(GV);
  2193. DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV
  2194. << "\n");
  2195. } else {
  2196. DEBUG(dbgs() << "Found a global var, but can not treat it as an "
  2197. "invariant.\n");
  2198. }
  2199. }
  2200. // Continue even if we do nothing.
  2201. ++CurInst;
  2202. continue;
  2203. }
  2204. DEBUG(dbgs() << "Unknown intrinsic. Can not evaluate.\n");
  2205. return false;
  2206. }
  2207. // Resolve function pointers.
  2208. Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
  2209. if (!Callee || Callee->mayBeOverridden()) {
  2210. DEBUG(dbgs() << "Can not resolve function pointer.\n");
  2211. return false; // Cannot resolve.
  2212. }
  2213. SmallVector<Constant*, 8> Formals;
  2214. for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
  2215. Formals.push_back(getVal(*i));
  2216. if (Callee->isDeclaration()) {
  2217. // If this is a function we can constant fold, do it.
  2218. if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
  2219. InstResult = C;
  2220. DEBUG(dbgs() << "Constant folded function call. Result: " <<
  2221. *InstResult << "\n");
  2222. } else {
  2223. DEBUG(dbgs() << "Can not constant fold function call.\n");
  2224. return false;
  2225. }
  2226. } else {
  2227. if (Callee->getFunctionType()->isVarArg()) {
  2228. DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
  2229. return false;
  2230. }
  2231. Constant *RetVal = nullptr;
  2232. // Execute the call, if successful, use the return value.
  2233. ValueStack.emplace_back();
  2234. if (!EvaluateFunction(Callee, RetVal, Formals)) {
  2235. DEBUG(dbgs() << "Failed to evaluate function.\n");
  2236. return false;
  2237. }
  2238. ValueStack.pop_back();
  2239. InstResult = RetVal;
  2240. if (InstResult) {
  2241. DEBUG(dbgs() << "Successfully evaluated function. Result: " <<
  2242. InstResult << "\n\n");
  2243. } else {
  2244. DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n");
  2245. }
  2246. }
  2247. } else if (isa<TerminatorInst>(CurInst)) {
  2248. DEBUG(dbgs() << "Found a terminator instruction.\n");
  2249. if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
  2250. if (BI->isUnconditional()) {
  2251. NextBB = BI->getSuccessor(0);
  2252. } else {
  2253. ConstantInt *Cond =
  2254. dyn_cast<ConstantInt>(getVal(BI->getCondition()));
  2255. if (!Cond) return false; // Cannot determine.
  2256. NextBB = BI->getSuccessor(!Cond->getZExtValue());
  2257. }
  2258. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
  2259. ConstantInt *Val =
  2260. dyn_cast<ConstantInt>(getVal(SI->getCondition()));
  2261. if (!Val) return false; // Cannot determine.
  2262. NextBB = SI->findCaseValue(Val).getCaseSuccessor();
  2263. } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
  2264. Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
  2265. if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
  2266. NextBB = BA->getBasicBlock();
  2267. else
  2268. return false; // Cannot determine.
  2269. } else if (isa<ReturnInst>(CurInst)) {
  2270. NextBB = nullptr;
  2271. } else {
  2272. // invoke, unwind, resume, unreachable.
  2273. DEBUG(dbgs() << "Can not handle terminator.");
  2274. return false; // Cannot handle this terminator.
  2275. }
  2276. // We succeeded at evaluating this block!
  2277. DEBUG(dbgs() << "Successfully evaluated block.\n");
  2278. return true;
  2279. } else {
  2280. // Did not know how to evaluate this!
  2281. DEBUG(dbgs() << "Failed to evaluate block due to unhandled instruction."
  2282. "\n");
  2283. return false;
  2284. }
  2285. if (!CurInst->use_empty()) {
  2286. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
  2287. InstResult = ConstantFoldConstantExpression(CE, DL, TLI);
  2288. setVal(CurInst, InstResult);
  2289. }
  2290. // If we just processed an invoke, we finished evaluating the block.
  2291. if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
  2292. NextBB = II->getNormalDest();
  2293. DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
  2294. return true;
  2295. }
  2296. // Advance program counter.
  2297. ++CurInst;
  2298. }
  2299. }
  2300. /// EvaluateFunction - Evaluate a call to function F, returning true if
  2301. /// successful, false if we can't evaluate it. ActualArgs contains the formal
  2302. /// arguments for the function.
  2303. bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
  2304. const SmallVectorImpl<Constant*> &ActualArgs) {
  2305. // Check to see if this function is already executing (recursion). If so,
  2306. // bail out. TODO: we might want to accept limited recursion.
  2307. if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
  2308. return false;
  2309. CallStack.push_back(F);
  2310. // Initialize arguments to the incoming values specified.
  2311. unsigned ArgNo = 0;
  2312. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
  2313. ++AI, ++ArgNo)
  2314. setVal(AI, ActualArgs[ArgNo]);
  2315. // ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
  2316. // we can only evaluate any one basic block at most once. This set keeps
  2317. // track of what we have executed so we can detect recursive cases etc.
  2318. SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
  2319. // CurBB - The current basic block we're evaluating.
  2320. BasicBlock *CurBB = F->begin();
  2321. BasicBlock::iterator CurInst = CurBB->begin();
  2322. while (1) {
  2323. BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
  2324. DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
  2325. if (!EvaluateBlock(CurInst, NextBB))
  2326. return false;
  2327. if (!NextBB) {
  2328. // Successfully running until there's no next block means that we found
  2329. // the return. Fill it the return value and pop the call stack.
  2330. ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
  2331. if (RI->getNumOperands())
  2332. RetVal = getVal(RI->getOperand(0));
  2333. CallStack.pop_back();
  2334. return true;
  2335. }
  2336. // Okay, we succeeded in evaluating this control flow. See if we have
  2337. // executed the new block before. If so, we have a looping function,
  2338. // which we cannot evaluate in reasonable time.
  2339. if (!ExecutedBlocks.insert(NextBB).second)
  2340. return false; // looped!
  2341. // Okay, we have never been in this block before. Check to see if there
  2342. // are any PHI nodes. If so, evaluate them with information about where
  2343. // we came from.
  2344. PHINode *PN = nullptr;
  2345. for (CurInst = NextBB->begin();
  2346. (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
  2347. setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
  2348. // Advance to the next block.
  2349. CurBB = NextBB;
  2350. }
  2351. }
  2352. /// EvaluateStaticConstructor - Evaluate static constructors in the function, if
  2353. /// we can. Return true if we can, false otherwise.
  2354. static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,
  2355. const TargetLibraryInfo *TLI) {
  2356. // Call the function.
  2357. Evaluator Eval(DL, TLI);
  2358. Constant *RetValDummy;
  2359. bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
  2360. SmallVector<Constant*, 0>());
  2361. if (EvalSuccess) {
  2362. ++NumCtorsEvaluated;
  2363. // We succeeded at evaluation: commit the result.
  2364. DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
  2365. << F->getName() << "' to " << Eval.getMutatedMemory().size()
  2366. << " stores.\n");
  2367. for (DenseMap<Constant*, Constant*>::const_iterator I =
  2368. Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
  2369. I != E; ++I)
  2370. CommitValueTo(I->second, I->first);
  2371. for (GlobalVariable *GV : Eval.getInvariants())
  2372. GV->setConstant(true);
  2373. }
  2374. return EvalSuccess;
  2375. }
  2376. // HLSL Change: changed calling convention to __cdecl
  2377. static int __cdecl compareNames(Constant *const *A, Constant *const *B) {
  2378. return (*A)->getName().compare((*B)->getName());
  2379. }
  2380. static void setUsedInitializer(GlobalVariable &V,
  2381. const SmallPtrSet<GlobalValue *, 8> &Init) {
  2382. if (Init.empty()) {
  2383. V.eraseFromParent();
  2384. return;
  2385. }
  2386. // Type of pointer to the array of pointers.
  2387. PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
  2388. SmallVector<llvm::Constant *, 8> UsedArray;
  2389. for (GlobalValue *GV : Init) {
  2390. Constant *Cast
  2391. = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
  2392. UsedArray.push_back(Cast);
  2393. }
  2394. // Sort to get deterministic order.
  2395. array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
  2396. ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
  2397. Module *M = V.getParent();
  2398. V.removeFromParent();
  2399. GlobalVariable *NV =
  2400. new GlobalVariable(*M, ATy, false, llvm::GlobalValue::AppendingLinkage,
  2401. llvm::ConstantArray::get(ATy, UsedArray), "");
  2402. NV->takeName(&V);
  2403. NV->setSection("llvm.metadata");
  2404. delete &V;
  2405. }
  2406. namespace {
  2407. /// \brief An easy to access representation of llvm.used and llvm.compiler.used.
  2408. class LLVMUsed {
  2409. SmallPtrSet<GlobalValue *, 8> Used;
  2410. SmallPtrSet<GlobalValue *, 8> CompilerUsed;
  2411. GlobalVariable *UsedV;
  2412. GlobalVariable *CompilerUsedV;
  2413. public:
  2414. LLVMUsed(Module &M) {
  2415. UsedV = collectUsedGlobalVariables(M, Used, false);
  2416. CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
  2417. }
  2418. typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator;
  2419. typedef iterator_range<iterator> used_iterator_range;
  2420. iterator usedBegin() { return Used.begin(); }
  2421. iterator usedEnd() { return Used.end(); }
  2422. used_iterator_range used() {
  2423. return used_iterator_range(usedBegin(), usedEnd());
  2424. }
  2425. iterator compilerUsedBegin() { return CompilerUsed.begin(); }
  2426. iterator compilerUsedEnd() { return CompilerUsed.end(); }
  2427. used_iterator_range compilerUsed() {
  2428. return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
  2429. }
  2430. bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
  2431. bool compilerUsedCount(GlobalValue *GV) const {
  2432. return CompilerUsed.count(GV);
  2433. }
  2434. bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
  2435. bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
  2436. bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
  2437. bool compilerUsedInsert(GlobalValue *GV) {
  2438. return CompilerUsed.insert(GV).second;
  2439. }
  2440. void syncVariablesAndSets() {
  2441. if (UsedV)
  2442. setUsedInitializer(*UsedV, Used);
  2443. if (CompilerUsedV)
  2444. setUsedInitializer(*CompilerUsedV, CompilerUsed);
  2445. }
  2446. };
  2447. }
  2448. static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
  2449. if (GA.use_empty()) // No use at all.
  2450. return false;
  2451. assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
  2452. "We should have removed the duplicated "
  2453. "element from llvm.compiler.used");
  2454. if (!GA.hasOneUse())
  2455. // Strictly more than one use. So at least one is not in llvm.used and
  2456. // llvm.compiler.used.
  2457. return true;
  2458. // Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
  2459. return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
  2460. }
  2461. static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
  2462. const LLVMUsed &U) {
  2463. unsigned N = 2;
  2464. assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
  2465. "We should have removed the duplicated "
  2466. "element from llvm.compiler.used");
  2467. if (U.usedCount(&V) || U.compilerUsedCount(&V))
  2468. ++N;
  2469. return V.hasNUsesOrMore(N);
  2470. }
  2471. static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
  2472. if (!GA.hasLocalLinkage())
  2473. return true;
  2474. return U.usedCount(&GA) || U.compilerUsedCount(&GA);
  2475. }
  2476. static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
  2477. bool &RenameTarget) {
  2478. RenameTarget = false;
  2479. bool Ret = false;
  2480. if (hasUseOtherThanLLVMUsed(GA, U))
  2481. Ret = true;
  2482. // If the alias is externally visible, we may still be able to simplify it.
  2483. if (!mayHaveOtherReferences(GA, U))
  2484. return Ret;
  2485. // If the aliasee has internal linkage, give it the name and linkage
  2486. // of the alias, and delete the alias. This turns:
  2487. // define internal ... @f(...)
  2488. // @a = alias ... @f
  2489. // into:
  2490. // define ... @a(...)
  2491. Constant *Aliasee = GA.getAliasee();
  2492. GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
  2493. if (!Target->hasLocalLinkage())
  2494. return Ret;
  2495. // Do not perform the transform if multiple aliases potentially target the
  2496. // aliasee. This check also ensures that it is safe to replace the section
  2497. // and other attributes of the aliasee with those of the alias.
  2498. if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
  2499. return Ret;
  2500. RenameTarget = true;
  2501. return true;
  2502. }
  2503. bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
  2504. bool Changed = false;
  2505. LLVMUsed Used(M);
  2506. for (GlobalValue *GV : Used.used())
  2507. Used.compilerUsedErase(GV);
  2508. for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
  2509. I != E;) {
  2510. Module::alias_iterator J = I++;
  2511. // Aliases without names cannot be referenced outside this module.
  2512. if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage())
  2513. J->setLinkage(GlobalValue::InternalLinkage);
  2514. // If the aliasee may change at link time, nothing can be done - bail out.
  2515. if (J->mayBeOverridden())
  2516. continue;
  2517. Constant *Aliasee = J->getAliasee();
  2518. GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
  2519. // We can't trivially replace the alias with the aliasee if the aliasee is
  2520. // non-trivial in some way.
  2521. // TODO: Try to handle non-zero GEPs of local aliasees.
  2522. if (!Target)
  2523. continue;
  2524. Target->removeDeadConstantUsers();
  2525. // Make all users of the alias use the aliasee instead.
  2526. bool RenameTarget;
  2527. if (!hasUsesToReplace(*J, Used, RenameTarget))
  2528. continue;
  2529. J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
  2530. ++NumAliasesResolved;
  2531. Changed = true;
  2532. if (RenameTarget) {
  2533. // Give the aliasee the name, linkage and other attributes of the alias.
  2534. Target->takeName(J);
  2535. Target->setLinkage(J->getLinkage());
  2536. Target->setVisibility(J->getVisibility());
  2537. Target->setDLLStorageClass(J->getDLLStorageClass());
  2538. if (Used.usedErase(J))
  2539. Used.usedInsert(Target);
  2540. if (Used.compilerUsedErase(J))
  2541. Used.compilerUsedInsert(Target);
  2542. } else if (mayHaveOtherReferences(*J, Used))
  2543. continue;
  2544. // Delete the alias.
  2545. M.getAliasList().erase(J);
  2546. ++NumAliasesRemoved;
  2547. Changed = true;
  2548. }
  2549. Used.syncVariablesAndSets();
  2550. return Changed;
  2551. }
  2552. static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
  2553. if (!TLI->has(LibFunc::cxa_atexit))
  2554. return nullptr;
  2555. Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
  2556. if (!Fn)
  2557. return nullptr;
  2558. FunctionType *FTy = Fn->getFunctionType();
  2559. // Checking that the function has the right return type, the right number of
  2560. // parameters and that they all have pointer types should be enough.
  2561. if (!FTy->getReturnType()->isIntegerTy() ||
  2562. FTy->getNumParams() != 3 ||
  2563. !FTy->getParamType(0)->isPointerTy() ||
  2564. !FTy->getParamType(1)->isPointerTy() ||
  2565. !FTy->getParamType(2)->isPointerTy())
  2566. return nullptr;
  2567. return Fn;
  2568. }
  2569. /// cxxDtorIsEmpty - Returns whether the given function is an empty C++
  2570. /// destructor and can therefore be eliminated.
  2571. /// Note that we assume that other optimization passes have already simplified
  2572. /// the code so we only look for a function with a single basic block, where
  2573. /// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
  2574. /// other side-effect free instructions.
  2575. static bool cxxDtorIsEmpty(const Function &Fn,
  2576. SmallPtrSet<const Function *, 8> &CalledFunctions) {
  2577. // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
  2578. // nounwind, but that doesn't seem worth doing.
  2579. if (Fn.isDeclaration())
  2580. return false;
  2581. if (++Fn.begin() != Fn.end())
  2582. return false;
  2583. const BasicBlock &EntryBlock = Fn.getEntryBlock();
  2584. for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
  2585. I != E; ++I) {
  2586. if (const CallInst *CI = dyn_cast<CallInst>(I)) {
  2587. // Ignore debug intrinsics.
  2588. if (isa<DbgInfoIntrinsic>(CI))
  2589. continue;
  2590. const Function *CalledFn = CI->getCalledFunction();
  2591. if (!CalledFn)
  2592. return false;
  2593. SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
  2594. // Don't treat recursive functions as empty.
  2595. if (!NewCalledFunctions.insert(CalledFn).second)
  2596. return false;
  2597. if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
  2598. return false;
  2599. } else if (isa<ReturnInst>(*I))
  2600. return true; // We're done.
  2601. else if (I->mayHaveSideEffects())
  2602. return false; // Destructor with side effects, bail.
  2603. }
  2604. return false;
  2605. }
  2606. bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
  2607. /// Itanium C++ ABI p3.3.5:
  2608. ///
  2609. /// After constructing a global (or local static) object, that will require
  2610. /// destruction on exit, a termination function is registered as follows:
  2611. ///
  2612. /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
  2613. ///
  2614. /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
  2615. /// call f(p) when DSO d is unloaded, before all such termination calls
  2616. /// registered before this one. It returns zero if registration is
  2617. /// successful, nonzero on failure.
  2618. // This pass will look for calls to __cxa_atexit where the function is trivial
  2619. // and remove them.
  2620. bool Changed = false;
  2621. for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
  2622. I != E;) {
  2623. // We're only interested in calls. Theoretically, we could handle invoke
  2624. // instructions as well, but neither llvm-gcc nor clang generate invokes
  2625. // to __cxa_atexit.
  2626. CallInst *CI = dyn_cast<CallInst>(*I++);
  2627. if (!CI)
  2628. continue;
  2629. Function *DtorFn =
  2630. dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
  2631. if (!DtorFn)
  2632. continue;
  2633. SmallPtrSet<const Function *, 8> CalledFunctions;
  2634. if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
  2635. continue;
  2636. // Just remove the call.
  2637. CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
  2638. CI->eraseFromParent();
  2639. ++NumCXXDtorsRemoved;
  2640. Changed |= true;
  2641. }
  2642. return Changed;
  2643. }
  2644. bool GlobalOpt::runOnModule(Module &M) {
  2645. bool Changed = false;
  2646. auto &DL = M.getDataLayout();
  2647. TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  2648. bool LocalChange = true;
  2649. while (LocalChange) {
  2650. LocalChange = false;
  2651. NotDiscardableComdats.clear();
  2652. for (const GlobalVariable &GV : M.globals())
  2653. if (const Comdat *C = GV.getComdat())
  2654. if (!GV.isDiscardableIfUnused() || !GV.use_empty())
  2655. NotDiscardableComdats.insert(C);
  2656. for (Function &F : M)
  2657. if (const Comdat *C = F.getComdat())
  2658. if (!F.isDefTriviallyDead())
  2659. NotDiscardableComdats.insert(C);
  2660. for (GlobalAlias &GA : M.aliases())
  2661. if (const Comdat *C = GA.getComdat())
  2662. if (!GA.isDiscardableIfUnused() || !GA.use_empty())
  2663. NotDiscardableComdats.insert(C);
  2664. // Delete functions that are trivially dead, ccc -> fastcc
  2665. LocalChange |= OptimizeFunctions(M);
  2666. // Optimize global_ctors list.
  2667. LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
  2668. return EvaluateStaticConstructor(F, DL, TLI);
  2669. });
  2670. // Optimize non-address-taken globals.
  2671. LocalChange |= OptimizeGlobalVars(M);
  2672. // Resolve aliases, when possible.
  2673. LocalChange |= OptimizeGlobalAliases(M);
  2674. // Try to remove trivial global destructors if they are not removed
  2675. // already.
  2676. Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
  2677. if (CXAAtExitFn)
  2678. LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
  2679. Changed |= LocalChange;
  2680. }
  2681. // TODO: Move all global ctors functions to the end of the module for code
  2682. // layout.
  2683. return Changed;
  2684. }