GlobalOpt.cpp 119 KB

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