GVN.cpp 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120
  1. //===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This pass performs global value numbering to eliminate fully redundant
  10. // instructions. It also performs simple dead load elimination.
  11. //
  12. // Note that this pass does the value numbering itself; it does not use the
  13. // ValueNumbering analysis passes.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Scalar/GVN.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/DepthFirstIterator.h"
  19. #include "llvm/ADT/Hashing.h"
  20. #include "llvm/ADT/MapVector.h"
  21. #include "llvm/ADT/PointerIntPair.h"
  22. #include "llvm/ADT/PostOrderIterator.h"
  23. #include "llvm/ADT/STLExtras.h"
  24. #include "llvm/ADT/SetVector.h"
  25. #include "llvm/ADT/SmallPtrSet.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/Statistic.h"
  28. #include "llvm/Analysis/AliasAnalysis.h"
  29. #include "llvm/Analysis/AssumeBundleQueries.h"
  30. #include "llvm/Analysis/AssumptionCache.h"
  31. #include "llvm/Analysis/CFG.h"
  32. #include "llvm/Analysis/DomTreeUpdater.h"
  33. #include "llvm/Analysis/GlobalsModRef.h"
  34. #include "llvm/Analysis/InstructionSimplify.h"
  35. #include "llvm/Analysis/LoopInfo.h"
  36. #include "llvm/Analysis/MemoryBuiltins.h"
  37. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  38. #include "llvm/Analysis/MemorySSA.h"
  39. #include "llvm/Analysis/MemorySSAUpdater.h"
  40. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  41. #include "llvm/Analysis/PHITransAddr.h"
  42. #include "llvm/Analysis/TargetLibraryInfo.h"
  43. #include "llvm/Analysis/ValueTracking.h"
  44. #include "llvm/Config/llvm-config.h"
  45. #include "llvm/IR/Attributes.h"
  46. #include "llvm/IR/BasicBlock.h"
  47. #include "llvm/IR/Constant.h"
  48. #include "llvm/IR/Constants.h"
  49. #include "llvm/IR/DataLayout.h"
  50. #include "llvm/IR/DebugLoc.h"
  51. #include "llvm/IR/Dominators.h"
  52. #include "llvm/IR/Function.h"
  53. #include "llvm/IR/InstrTypes.h"
  54. #include "llvm/IR/Instruction.h"
  55. #include "llvm/IR/Instructions.h"
  56. #include "llvm/IR/IntrinsicInst.h"
  57. #include "llvm/IR/Intrinsics.h"
  58. #include "llvm/IR/LLVMContext.h"
  59. #include "llvm/IR/Metadata.h"
  60. #include "llvm/IR/Module.h"
  61. #include "llvm/IR/Operator.h"
  62. #include "llvm/IR/PassManager.h"
  63. #include "llvm/IR/PatternMatch.h"
  64. #include "llvm/IR/Type.h"
  65. #include "llvm/IR/Use.h"
  66. #include "llvm/IR/Value.h"
  67. #include "llvm/InitializePasses.h"
  68. #include "llvm/Pass.h"
  69. #include "llvm/Support/Casting.h"
  70. #include "llvm/Support/CommandLine.h"
  71. #include "llvm/Support/Compiler.h"
  72. #include "llvm/Support/Debug.h"
  73. #include "llvm/Support/raw_ostream.h"
  74. #include "llvm/Transforms/Utils.h"
  75. #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
  76. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  77. #include "llvm/Transforms/Utils/Local.h"
  78. #include "llvm/Transforms/Utils/SSAUpdater.h"
  79. #include "llvm/Transforms/Utils/VNCoercion.h"
  80. #include <algorithm>
  81. #include <cassert>
  82. #include <cstdint>
  83. #include <utility>
  84. using namespace llvm;
  85. using namespace llvm::gvn;
  86. using namespace llvm::VNCoercion;
  87. using namespace PatternMatch;
  88. #define DEBUG_TYPE "gvn"
  89. STATISTIC(NumGVNInstr, "Number of instructions deleted");
  90. STATISTIC(NumGVNLoad, "Number of loads deleted");
  91. STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
  92. STATISTIC(NumGVNBlocks, "Number of blocks merged");
  93. STATISTIC(NumGVNSimpl, "Number of instructions simplified");
  94. STATISTIC(NumGVNEqProp, "Number of equalities propagated");
  95. STATISTIC(NumPRELoad, "Number of loads PRE'd");
  96. STATISTIC(NumPRELoopLoad, "Number of loop loads PRE'd");
  97. STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax,
  98. "Number of blocks speculated as available in "
  99. "IsValueFullyAvailableInBlock(), max");
  100. STATISTIC(MaxBBSpeculationCutoffReachedTimes,
  101. "Number of times we we reached gvn-max-block-speculations cut-off "
  102. "preventing further exploration");
  103. static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden);
  104. static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
  105. static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
  106. cl::init(true));
  107. static cl::opt<bool>
  108. GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre",
  109. cl::init(true));
  110. static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
  111. static cl::opt<uint32_t> MaxNumDeps(
  112. "gvn-max-num-deps", cl::Hidden, cl::init(100), cl::ZeroOrMore,
  113. cl::desc("Max number of dependences to attempt Load PRE (default = 100)"));
  114. // This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat.
  115. static cl::opt<uint32_t> MaxBBSpeculations(
  116. "gvn-max-block-speculations", cl::Hidden, cl::init(600), cl::ZeroOrMore,
  117. cl::desc("Max number of blocks we're willing to speculate on (and recurse "
  118. "into) when deducing if a value is fully available or not in GVN "
  119. "(default = 600)"));
  120. struct llvm::GVNPass::Expression {
  121. uint32_t opcode;
  122. bool commutative = false;
  123. Type *type = nullptr;
  124. SmallVector<uint32_t, 4> varargs;
  125. Expression(uint32_t o = ~2U) : opcode(o) {}
  126. bool operator==(const Expression &other) const {
  127. if (opcode != other.opcode)
  128. return false;
  129. if (opcode == ~0U || opcode == ~1U)
  130. return true;
  131. if (type != other.type)
  132. return false;
  133. if (varargs != other.varargs)
  134. return false;
  135. return true;
  136. }
  137. friend hash_code hash_value(const Expression &Value) {
  138. return hash_combine(
  139. Value.opcode, Value.type,
  140. hash_combine_range(Value.varargs.begin(), Value.varargs.end()));
  141. }
  142. };
  143. namespace llvm {
  144. template <> struct DenseMapInfo<GVNPass::Expression> {
  145. static inline GVNPass::Expression getEmptyKey() { return ~0U; }
  146. static inline GVNPass::Expression getTombstoneKey() { return ~1U; }
  147. static unsigned getHashValue(const GVNPass::Expression &e) {
  148. using llvm::hash_value;
  149. return static_cast<unsigned>(hash_value(e));
  150. }
  151. static bool isEqual(const GVNPass::Expression &LHS,
  152. const GVNPass::Expression &RHS) {
  153. return LHS == RHS;
  154. }
  155. };
  156. } // end namespace llvm
  157. /// Represents a particular available value that we know how to materialize.
  158. /// Materialization of an AvailableValue never fails. An AvailableValue is
  159. /// implicitly associated with a rematerialization point which is the
  160. /// location of the instruction from which it was formed.
  161. struct llvm::gvn::AvailableValue {
  162. enum ValType {
  163. SimpleVal, // A simple offsetted value that is accessed.
  164. LoadVal, // A value produced by a load.
  165. MemIntrin, // A memory intrinsic which is loaded from.
  166. UndefVal // A UndefValue representing a value from dead block (which
  167. // is not yet physically removed from the CFG).
  168. };
  169. /// V - The value that is live out of the block.
  170. PointerIntPair<Value *, 2, ValType> Val;
  171. /// Offset - The byte offset in Val that is interesting for the load query.
  172. unsigned Offset = 0;
  173. static AvailableValue get(Value *V, unsigned Offset = 0) {
  174. AvailableValue Res;
  175. Res.Val.setPointer(V);
  176. Res.Val.setInt(SimpleVal);
  177. Res.Offset = Offset;
  178. return Res;
  179. }
  180. static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) {
  181. AvailableValue Res;
  182. Res.Val.setPointer(MI);
  183. Res.Val.setInt(MemIntrin);
  184. Res.Offset = Offset;
  185. return Res;
  186. }
  187. static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) {
  188. AvailableValue Res;
  189. Res.Val.setPointer(Load);
  190. Res.Val.setInt(LoadVal);
  191. Res.Offset = Offset;
  192. return Res;
  193. }
  194. static AvailableValue getUndef() {
  195. AvailableValue Res;
  196. Res.Val.setPointer(nullptr);
  197. Res.Val.setInt(UndefVal);
  198. Res.Offset = 0;
  199. return Res;
  200. }
  201. bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
  202. bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
  203. bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
  204. bool isUndefValue() const { return Val.getInt() == UndefVal; }
  205. Value *getSimpleValue() const {
  206. assert(isSimpleValue() && "Wrong accessor");
  207. return Val.getPointer();
  208. }
  209. LoadInst *getCoercedLoadValue() const {
  210. assert(isCoercedLoadValue() && "Wrong accessor");
  211. return cast<LoadInst>(Val.getPointer());
  212. }
  213. MemIntrinsic *getMemIntrinValue() const {
  214. assert(isMemIntrinValue() && "Wrong accessor");
  215. return cast<MemIntrinsic>(Val.getPointer());
  216. }
  217. /// Emit code at the specified insertion point to adjust the value defined
  218. /// here to the specified type. This handles various coercion cases.
  219. Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt,
  220. GVNPass &gvn) const;
  221. };
  222. /// Represents an AvailableValue which can be rematerialized at the end of
  223. /// the associated BasicBlock.
  224. struct llvm::gvn::AvailableValueInBlock {
  225. /// BB - The basic block in question.
  226. BasicBlock *BB = nullptr;
  227. /// AV - The actual available value
  228. AvailableValue AV;
  229. static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) {
  230. AvailableValueInBlock Res;
  231. Res.BB = BB;
  232. Res.AV = std::move(AV);
  233. return Res;
  234. }
  235. static AvailableValueInBlock get(BasicBlock *BB, Value *V,
  236. unsigned Offset = 0) {
  237. return get(BB, AvailableValue::get(V, Offset));
  238. }
  239. static AvailableValueInBlock getUndef(BasicBlock *BB) {
  240. return get(BB, AvailableValue::getUndef());
  241. }
  242. /// Emit code at the end of this block to adjust the value defined here to
  243. /// the specified type. This handles various coercion cases.
  244. Value *MaterializeAdjustedValue(LoadInst *Load, GVNPass &gvn) const {
  245. return AV.MaterializeAdjustedValue(Load, BB->getTerminator(), gvn);
  246. }
  247. };
  248. //===----------------------------------------------------------------------===//
  249. // ValueTable Internal Functions
  250. //===----------------------------------------------------------------------===//
  251. GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) {
  252. Expression e;
  253. e.type = I->getType();
  254. e.opcode = I->getOpcode();
  255. if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) {
  256. // gc.relocate is 'special' call: its second and third operands are
  257. // not real values, but indices into statepoint's argument list.
  258. // Use the refered to values for purposes of identity.
  259. e.varargs.push_back(lookupOrAdd(GCR->getOperand(0)));
  260. e.varargs.push_back(lookupOrAdd(GCR->getBasePtr()));
  261. e.varargs.push_back(lookupOrAdd(GCR->getDerivedPtr()));
  262. } else {
  263. for (Use &Op : I->operands())
  264. e.varargs.push_back(lookupOrAdd(Op));
  265. }
  266. if (I->isCommutative()) {
  267. // Ensure that commutative instructions that only differ by a permutation
  268. // of their operands get the same value number by sorting the operand value
  269. // numbers. Since commutative operands are the 1st two operands it is more
  270. // efficient to sort by hand rather than using, say, std::sort.
  271. assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!");
  272. if (e.varargs[0] > e.varargs[1])
  273. std::swap(e.varargs[0], e.varargs[1]);
  274. e.commutative = true;
  275. }
  276. if (auto *C = dyn_cast<CmpInst>(I)) {
  277. // Sort the operand value numbers so x<y and y>x get the same value number.
  278. CmpInst::Predicate Predicate = C->getPredicate();
  279. if (e.varargs[0] > e.varargs[1]) {
  280. std::swap(e.varargs[0], e.varargs[1]);
  281. Predicate = CmpInst::getSwappedPredicate(Predicate);
  282. }
  283. e.opcode = (C->getOpcode() << 8) | Predicate;
  284. e.commutative = true;
  285. } else if (auto *E = dyn_cast<InsertValueInst>(I)) {
  286. e.varargs.append(E->idx_begin(), E->idx_end());
  287. } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
  288. ArrayRef<int> ShuffleMask = SVI->getShuffleMask();
  289. e.varargs.append(ShuffleMask.begin(), ShuffleMask.end());
  290. }
  291. return e;
  292. }
  293. GVNPass::Expression GVNPass::ValueTable::createCmpExpr(
  294. unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) {
  295. assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
  296. "Not a comparison!");
  297. Expression e;
  298. e.type = CmpInst::makeCmpResultType(LHS->getType());
  299. e.varargs.push_back(lookupOrAdd(LHS));
  300. e.varargs.push_back(lookupOrAdd(RHS));
  301. // Sort the operand value numbers so x<y and y>x get the same value number.
  302. if (e.varargs[0] > e.varargs[1]) {
  303. std::swap(e.varargs[0], e.varargs[1]);
  304. Predicate = CmpInst::getSwappedPredicate(Predicate);
  305. }
  306. e.opcode = (Opcode << 8) | Predicate;
  307. e.commutative = true;
  308. return e;
  309. }
  310. GVNPass::Expression
  311. GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) {
  312. assert(EI && "Not an ExtractValueInst?");
  313. Expression e;
  314. e.type = EI->getType();
  315. e.opcode = 0;
  316. WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
  317. if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
  318. // EI is an extract from one of our with.overflow intrinsics. Synthesize
  319. // a semantically equivalent expression instead of an extract value
  320. // expression.
  321. e.opcode = WO->getBinaryOp();
  322. e.varargs.push_back(lookupOrAdd(WO->getLHS()));
  323. e.varargs.push_back(lookupOrAdd(WO->getRHS()));
  324. return e;
  325. }
  326. // Not a recognised intrinsic. Fall back to producing an extract value
  327. // expression.
  328. e.opcode = EI->getOpcode();
  329. for (Use &Op : EI->operands())
  330. e.varargs.push_back(lookupOrAdd(Op));
  331. append_range(e.varargs, EI->indices());
  332. return e;
  333. }
  334. //===----------------------------------------------------------------------===//
  335. // ValueTable External Functions
  336. //===----------------------------------------------------------------------===//
  337. GVNPass::ValueTable::ValueTable() = default;
  338. GVNPass::ValueTable::ValueTable(const ValueTable &) = default;
  339. GVNPass::ValueTable::ValueTable(ValueTable &&) = default;
  340. GVNPass::ValueTable::~ValueTable() = default;
  341. GVNPass::ValueTable &
  342. GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default;
  343. /// add - Insert a value into the table with a specified value number.
  344. void GVNPass::ValueTable::add(Value *V, uint32_t num) {
  345. valueNumbering.insert(std::make_pair(V, num));
  346. if (PHINode *PN = dyn_cast<PHINode>(V))
  347. NumberingPhi[num] = PN;
  348. }
  349. uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) {
  350. if (AA->doesNotAccessMemory(C)) {
  351. Expression exp = createExpr(C);
  352. uint32_t e = assignExpNewValueNum(exp).first;
  353. valueNumbering[C] = e;
  354. return e;
  355. } else if (MD && AA->onlyReadsMemory(C)) {
  356. Expression exp = createExpr(C);
  357. auto ValNum = assignExpNewValueNum(exp);
  358. if (ValNum.second) {
  359. valueNumbering[C] = ValNum.first;
  360. return ValNum.first;
  361. }
  362. MemDepResult local_dep = MD->getDependency(C);
  363. if (!local_dep.isDef() && !local_dep.isNonLocal()) {
  364. valueNumbering[C] = nextValueNumber;
  365. return nextValueNumber++;
  366. }
  367. if (local_dep.isDef()) {
  368. // For masked load/store intrinsics, the local_dep may actully be
  369. // a normal load or store instruction.
  370. CallInst *local_cdep = dyn_cast<CallInst>(local_dep.getInst());
  371. if (!local_cdep || local_cdep->arg_size() != C->arg_size()) {
  372. valueNumbering[C] = nextValueNumber;
  373. return nextValueNumber++;
  374. }
  375. for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
  376. uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
  377. uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i));
  378. if (c_vn != cd_vn) {
  379. valueNumbering[C] = nextValueNumber;
  380. return nextValueNumber++;
  381. }
  382. }
  383. uint32_t v = lookupOrAdd(local_cdep);
  384. valueNumbering[C] = v;
  385. return v;
  386. }
  387. // Non-local case.
  388. const MemoryDependenceResults::NonLocalDepInfo &deps =
  389. MD->getNonLocalCallDependency(C);
  390. // FIXME: Move the checking logic to MemDep!
  391. CallInst* cdep = nullptr;
  392. // Check to see if we have a single dominating call instruction that is
  393. // identical to C.
  394. for (unsigned i = 0, e = deps.size(); i != e; ++i) {
  395. const NonLocalDepEntry *I = &deps[i];
  396. if (I->getResult().isNonLocal())
  397. continue;
  398. // We don't handle non-definitions. If we already have a call, reject
  399. // instruction dependencies.
  400. if (!I->getResult().isDef() || cdep != nullptr) {
  401. cdep = nullptr;
  402. break;
  403. }
  404. CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
  405. // FIXME: All duplicated with non-local case.
  406. if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
  407. cdep = NonLocalDepCall;
  408. continue;
  409. }
  410. cdep = nullptr;
  411. break;
  412. }
  413. if (!cdep) {
  414. valueNumbering[C] = nextValueNumber;
  415. return nextValueNumber++;
  416. }
  417. if (cdep->arg_size() != C->arg_size()) {
  418. valueNumbering[C] = nextValueNumber;
  419. return nextValueNumber++;
  420. }
  421. for (unsigned i = 0, e = C->arg_size(); i < e; ++i) {
  422. uint32_t c_vn = lookupOrAdd(C->getArgOperand(i));
  423. uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i));
  424. if (c_vn != cd_vn) {
  425. valueNumbering[C] = nextValueNumber;
  426. return nextValueNumber++;
  427. }
  428. }
  429. uint32_t v = lookupOrAdd(cdep);
  430. valueNumbering[C] = v;
  431. return v;
  432. } else {
  433. valueNumbering[C] = nextValueNumber;
  434. return nextValueNumber++;
  435. }
  436. }
  437. /// Returns true if a value number exists for the specified value.
  438. bool GVNPass::ValueTable::exists(Value *V) const {
  439. return valueNumbering.count(V) != 0;
  440. }
  441. /// lookup_or_add - Returns the value number for the specified value, assigning
  442. /// it a new number if it did not have one before.
  443. uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) {
  444. DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
  445. if (VI != valueNumbering.end())
  446. return VI->second;
  447. if (!isa<Instruction>(V)) {
  448. valueNumbering[V] = nextValueNumber;
  449. return nextValueNumber++;
  450. }
  451. Instruction* I = cast<Instruction>(V);
  452. Expression exp;
  453. switch (I->getOpcode()) {
  454. case Instruction::Call:
  455. return lookupOrAddCall(cast<CallInst>(I));
  456. case Instruction::FNeg:
  457. case Instruction::Add:
  458. case Instruction::FAdd:
  459. case Instruction::Sub:
  460. case Instruction::FSub:
  461. case Instruction::Mul:
  462. case Instruction::FMul:
  463. case Instruction::UDiv:
  464. case Instruction::SDiv:
  465. case Instruction::FDiv:
  466. case Instruction::URem:
  467. case Instruction::SRem:
  468. case Instruction::FRem:
  469. case Instruction::Shl:
  470. case Instruction::LShr:
  471. case Instruction::AShr:
  472. case Instruction::And:
  473. case Instruction::Or:
  474. case Instruction::Xor:
  475. case Instruction::ICmp:
  476. case Instruction::FCmp:
  477. case Instruction::Trunc:
  478. case Instruction::ZExt:
  479. case Instruction::SExt:
  480. case Instruction::FPToUI:
  481. case Instruction::FPToSI:
  482. case Instruction::UIToFP:
  483. case Instruction::SIToFP:
  484. case Instruction::FPTrunc:
  485. case Instruction::FPExt:
  486. case Instruction::PtrToInt:
  487. case Instruction::IntToPtr:
  488. case Instruction::AddrSpaceCast:
  489. case Instruction::BitCast:
  490. case Instruction::Select:
  491. case Instruction::Freeze:
  492. case Instruction::ExtractElement:
  493. case Instruction::InsertElement:
  494. case Instruction::ShuffleVector:
  495. case Instruction::InsertValue:
  496. case Instruction::GetElementPtr:
  497. exp = createExpr(I);
  498. break;
  499. case Instruction::ExtractValue:
  500. exp = createExtractvalueExpr(cast<ExtractValueInst>(I));
  501. break;
  502. case Instruction::PHI:
  503. valueNumbering[V] = nextValueNumber;
  504. NumberingPhi[nextValueNumber] = cast<PHINode>(V);
  505. return nextValueNumber++;
  506. default:
  507. valueNumbering[V] = nextValueNumber;
  508. return nextValueNumber++;
  509. }
  510. uint32_t e = assignExpNewValueNum(exp).first;
  511. valueNumbering[V] = e;
  512. return e;
  513. }
  514. /// Returns the value number of the specified value. Fails if
  515. /// the value has not yet been numbered.
  516. uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const {
  517. DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
  518. if (Verify) {
  519. assert(VI != valueNumbering.end() && "Value not numbered?");
  520. return VI->second;
  521. }
  522. return (VI != valueNumbering.end()) ? VI->second : 0;
  523. }
  524. /// Returns the value number of the given comparison,
  525. /// assigning it a new number if it did not have one before. Useful when
  526. /// we deduced the result of a comparison, but don't immediately have an
  527. /// instruction realizing that comparison to hand.
  528. uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode,
  529. CmpInst::Predicate Predicate,
  530. Value *LHS, Value *RHS) {
  531. Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS);
  532. return assignExpNewValueNum(exp).first;
  533. }
  534. /// Remove all entries from the ValueTable.
  535. void GVNPass::ValueTable::clear() {
  536. valueNumbering.clear();
  537. expressionNumbering.clear();
  538. NumberingPhi.clear();
  539. PhiTranslateTable.clear();
  540. nextValueNumber = 1;
  541. Expressions.clear();
  542. ExprIdx.clear();
  543. nextExprNumber = 0;
  544. }
  545. /// Remove a value from the value numbering.
  546. void GVNPass::ValueTable::erase(Value *V) {
  547. uint32_t Num = valueNumbering.lookup(V);
  548. valueNumbering.erase(V);
  549. // If V is PHINode, V <--> value number is an one-to-one mapping.
  550. if (isa<PHINode>(V))
  551. NumberingPhi.erase(Num);
  552. }
  553. /// verifyRemoved - Verify that the value is removed from all internal data
  554. /// structures.
  555. void GVNPass::ValueTable::verifyRemoved(const Value *V) const {
  556. for (DenseMap<Value*, uint32_t>::const_iterator
  557. I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
  558. assert(I->first != V && "Inst still occurs in value numbering map!");
  559. }
  560. }
  561. //===----------------------------------------------------------------------===//
  562. // GVN Pass
  563. //===----------------------------------------------------------------------===//
  564. bool GVNPass::isPREEnabled() const {
  565. return Options.AllowPRE.getValueOr(GVNEnablePRE);
  566. }
  567. bool GVNPass::isLoadPREEnabled() const {
  568. return Options.AllowLoadPRE.getValueOr(GVNEnableLoadPRE);
  569. }
  570. bool GVNPass::isLoadInLoopPREEnabled() const {
  571. return Options.AllowLoadInLoopPRE.getValueOr(GVNEnableLoadInLoopPRE);
  572. }
  573. bool GVNPass::isLoadPRESplitBackedgeEnabled() const {
  574. return Options.AllowLoadPRESplitBackedge.getValueOr(
  575. GVNEnableSplitBackedgeInLoadPRE);
  576. }
  577. bool GVNPass::isMemDepEnabled() const {
  578. return Options.AllowMemDep.getValueOr(GVNEnableMemDep);
  579. }
  580. PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) {
  581. // FIXME: The order of evaluation of these 'getResult' calls is very
  582. // significant! Re-ordering these variables will cause GVN when run alone to
  583. // be less effective! We should fix memdep and basic-aa to not exhibit this
  584. // behavior, but until then don't change the order here.
  585. auto &AC = AM.getResult<AssumptionAnalysis>(F);
  586. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  587. auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
  588. auto &AA = AM.getResult<AAManager>(F);
  589. auto *MemDep =
  590. isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr;
  591. auto *LI = AM.getCachedResult<LoopAnalysis>(F);
  592. auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F);
  593. auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
  594. bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE,
  595. MSSA ? &MSSA->getMSSA() : nullptr);
  596. if (!Changed)
  597. return PreservedAnalyses::all();
  598. PreservedAnalyses PA;
  599. PA.preserve<DominatorTreeAnalysis>();
  600. PA.preserve<TargetLibraryAnalysis>();
  601. if (MSSA)
  602. PA.preserve<MemorySSAAnalysis>();
  603. if (LI)
  604. PA.preserve<LoopAnalysis>();
  605. return PA;
  606. }
  607. void GVNPass::printPipeline(
  608. raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
  609. static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline(
  610. OS, MapClassName2PassName);
  611. OS << "<";
  612. if (Options.AllowPRE != None)
  613. OS << (Options.AllowPRE.getValue() ? "" : "no-") << "pre;";
  614. if (Options.AllowLoadPRE != None)
  615. OS << (Options.AllowLoadPRE.getValue() ? "" : "no-") << "load-pre;";
  616. if (Options.AllowLoadPRESplitBackedge != None)
  617. OS << (Options.AllowLoadPRESplitBackedge.getValue() ? "" : "no-")
  618. << "split-backedge-load-pre;";
  619. if (Options.AllowMemDep != None)
  620. OS << (Options.AllowMemDep.getValue() ? "" : "no-") << "memdep";
  621. OS << ">";
  622. }
  623. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  624. LLVM_DUMP_METHOD void GVNPass::dump(DenseMap<uint32_t, Value *> &d) const {
  625. errs() << "{\n";
  626. for (auto &I : d) {
  627. errs() << I.first << "\n";
  628. I.second->dump();
  629. }
  630. errs() << "}\n";
  631. }
  632. #endif
  633. enum class AvailabilityState : char {
  634. /// We know the block *is not* fully available. This is a fixpoint.
  635. Unavailable = 0,
  636. /// We know the block *is* fully available. This is a fixpoint.
  637. Available = 1,
  638. /// We do not know whether the block is fully available or not,
  639. /// but we are currently speculating that it will be.
  640. /// If it would have turned out that the block was, in fact, not fully
  641. /// available, this would have been cleaned up into an Unavailable.
  642. SpeculativelyAvailable = 2,
  643. };
  644. /// Return true if we can prove that the value
  645. /// we're analyzing is fully available in the specified block. As we go, keep
  646. /// track of which blocks we know are fully alive in FullyAvailableBlocks. This
  647. /// map is actually a tri-state map with the following values:
  648. /// 0) we know the block *is not* fully available.
  649. /// 1) we know the block *is* fully available.
  650. /// 2) we do not know whether the block is fully available or not, but we are
  651. /// currently speculating that it will be.
  652. static bool IsValueFullyAvailableInBlock(
  653. BasicBlock *BB,
  654. DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) {
  655. SmallVector<BasicBlock *, 32> Worklist;
  656. Optional<BasicBlock *> UnavailableBB;
  657. // The number of times we didn't find an entry for a block in a map and
  658. // optimistically inserted an entry marking block as speculatively available.
  659. unsigned NumNewNewSpeculativelyAvailableBBs = 0;
  660. #ifndef NDEBUG
  661. SmallSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs;
  662. SmallVector<BasicBlock *, 32> AvailableBBs;
  663. #endif
  664. Worklist.emplace_back(BB);
  665. while (!Worklist.empty()) {
  666. BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first!
  667. // Optimistically assume that the block is Speculatively Available and check
  668. // to see if we already know about this block in one lookup.
  669. std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV =
  670. FullyAvailableBlocks.try_emplace(
  671. CurrBB, AvailabilityState::SpeculativelyAvailable);
  672. AvailabilityState &State = IV.first->second;
  673. // Did the entry already exist for this block?
  674. if (!IV.second) {
  675. if (State == AvailabilityState::Unavailable) {
  676. UnavailableBB = CurrBB;
  677. break; // Backpropagate unavailability info.
  678. }
  679. #ifndef NDEBUG
  680. AvailableBBs.emplace_back(CurrBB);
  681. #endif
  682. continue; // Don't recurse further, but continue processing worklist.
  683. }
  684. // No entry found for block.
  685. ++NumNewNewSpeculativelyAvailableBBs;
  686. bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations;
  687. // If we have exhausted our budget, mark this block as unavailable.
  688. // Also, if this block has no predecessors, the value isn't live-in here.
  689. if (OutOfBudget || pred_empty(CurrBB)) {
  690. MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget;
  691. State = AvailabilityState::Unavailable;
  692. UnavailableBB = CurrBB;
  693. break; // Backpropagate unavailability info.
  694. }
  695. // Tentatively consider this block as speculatively available.
  696. #ifndef NDEBUG
  697. NewSpeculativelyAvailableBBs.insert(CurrBB);
  698. #endif
  699. // And further recurse into block's predecessors, in depth-first order!
  700. Worklist.append(pred_begin(CurrBB), pred_end(CurrBB));
  701. }
  702. #if LLVM_ENABLE_STATS
  703. IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax(
  704. NumNewNewSpeculativelyAvailableBBs);
  705. #endif
  706. // If the block isn't marked as fixpoint yet
  707. // (the Unavailable and Available states are fixpoints)
  708. auto MarkAsFixpointAndEnqueueSuccessors =
  709. [&](BasicBlock *BB, AvailabilityState FixpointState) {
  710. auto It = FullyAvailableBlocks.find(BB);
  711. if (It == FullyAvailableBlocks.end())
  712. return; // Never queried this block, leave as-is.
  713. switch (AvailabilityState &State = It->second) {
  714. case AvailabilityState::Unavailable:
  715. case AvailabilityState::Available:
  716. return; // Don't backpropagate further, continue processing worklist.
  717. case AvailabilityState::SpeculativelyAvailable: // Fix it!
  718. State = FixpointState;
  719. #ifndef NDEBUG
  720. assert(NewSpeculativelyAvailableBBs.erase(BB) &&
  721. "Found a speculatively available successor leftover?");
  722. #endif
  723. // Queue successors for further processing.
  724. Worklist.append(succ_begin(BB), succ_end(BB));
  725. return;
  726. }
  727. };
  728. if (UnavailableBB) {
  729. // Okay, we have encountered an unavailable block.
  730. // Mark speculatively available blocks reachable from UnavailableBB as
  731. // unavailable as well. Paths are terminated when they reach blocks not in
  732. // FullyAvailableBlocks or they are not marked as speculatively available.
  733. Worklist.clear();
  734. Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB));
  735. while (!Worklist.empty())
  736. MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
  737. AvailabilityState::Unavailable);
  738. }
  739. #ifndef NDEBUG
  740. Worklist.clear();
  741. for (BasicBlock *AvailableBB : AvailableBBs)
  742. Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB));
  743. while (!Worklist.empty())
  744. MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(),
  745. AvailabilityState::Available);
  746. assert(NewSpeculativelyAvailableBBs.empty() &&
  747. "Must have fixed all the new speculatively available blocks.");
  748. #endif
  749. return !UnavailableBB;
  750. }
  751. /// Given a set of loads specified by ValuesPerBlock,
  752. /// construct SSA form, allowing us to eliminate Load. This returns the value
  753. /// that should be used at Load's definition site.
  754. static Value *
  755. ConstructSSAForLoadSet(LoadInst *Load,
  756. SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
  757. GVNPass &gvn) {
  758. // Check for the fully redundant, dominating load case. In this case, we can
  759. // just use the dominating value directly.
  760. if (ValuesPerBlock.size() == 1 &&
  761. gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
  762. Load->getParent())) {
  763. assert(!ValuesPerBlock[0].AV.isUndefValue() &&
  764. "Dead BB dominate this block");
  765. return ValuesPerBlock[0].MaterializeAdjustedValue(Load, gvn);
  766. }
  767. // Otherwise, we have to construct SSA form.
  768. SmallVector<PHINode*, 8> NewPHIs;
  769. SSAUpdater SSAUpdate(&NewPHIs);
  770. SSAUpdate.Initialize(Load->getType(), Load->getName());
  771. for (const AvailableValueInBlock &AV : ValuesPerBlock) {
  772. BasicBlock *BB = AV.BB;
  773. if (AV.AV.isUndefValue())
  774. continue;
  775. if (SSAUpdate.HasValueForBlock(BB))
  776. continue;
  777. // If the value is the load that we will be eliminating, and the block it's
  778. // available in is the block that the load is in, then don't add it as
  779. // SSAUpdater will resolve the value to the relevant phi which may let it
  780. // avoid phi construction entirely if there's actually only one value.
  781. if (BB == Load->getParent() &&
  782. ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) ||
  783. (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load)))
  784. continue;
  785. SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load, gvn));
  786. }
  787. // Perform PHI construction.
  788. return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent());
  789. }
  790. Value *AvailableValue::MaterializeAdjustedValue(LoadInst *Load,
  791. Instruction *InsertPt,
  792. GVNPass &gvn) const {
  793. Value *Res;
  794. Type *LoadTy = Load->getType();
  795. const DataLayout &DL = Load->getModule()->getDataLayout();
  796. if (isSimpleValue()) {
  797. Res = getSimpleValue();
  798. if (Res->getType() != LoadTy) {
  799. Res = getStoreValueForLoad(Res, Offset, LoadTy, InsertPt, DL);
  800. LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset
  801. << " " << *getSimpleValue() << '\n'
  802. << *Res << '\n'
  803. << "\n\n\n");
  804. }
  805. } else if (isCoercedLoadValue()) {
  806. LoadInst *CoercedLoad = getCoercedLoadValue();
  807. if (CoercedLoad->getType() == LoadTy && Offset == 0) {
  808. Res = CoercedLoad;
  809. } else {
  810. Res = getLoadValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt, DL);
  811. // We would like to use gvn.markInstructionForDeletion here, but we can't
  812. // because the load is already memoized into the leader map table that GVN
  813. // tracks. It is potentially possible to remove the load from the table,
  814. // but then there all of the operations based on it would need to be
  815. // rehashed. Just leave the dead load around.
  816. gvn.getMemDep().removeInstruction(CoercedLoad);
  817. LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset
  818. << " " << *getCoercedLoadValue() << '\n'
  819. << *Res << '\n'
  820. << "\n\n\n");
  821. }
  822. } else if (isMemIntrinValue()) {
  823. Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy,
  824. InsertPt, DL);
  825. LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
  826. << " " << *getMemIntrinValue() << '\n'
  827. << *Res << '\n'
  828. << "\n\n\n");
  829. } else {
  830. llvm_unreachable("Should not materialize value from dead block");
  831. }
  832. assert(Res && "failed to materialize?");
  833. return Res;
  834. }
  835. static bool isLifetimeStart(const Instruction *Inst) {
  836. if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
  837. return II->getIntrinsicID() == Intrinsic::lifetime_start;
  838. return false;
  839. }
  840. /// Assuming To can be reached from both From and Between, does Between lie on
  841. /// every path from From to To?
  842. static bool liesBetween(const Instruction *From, Instruction *Between,
  843. const Instruction *To, DominatorTree *DT) {
  844. if (From->getParent() == Between->getParent())
  845. return DT->dominates(From, Between);
  846. SmallSet<BasicBlock *, 1> Exclusion;
  847. Exclusion.insert(Between->getParent());
  848. return !isPotentiallyReachable(From, To, &Exclusion, DT);
  849. }
  850. /// Try to locate the three instruction involved in a missed
  851. /// load-elimination case that is due to an intervening store.
  852. static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo,
  853. DominatorTree *DT,
  854. OptimizationRemarkEmitter *ORE) {
  855. using namespace ore;
  856. User *OtherAccess = nullptr;
  857. OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", Load);
  858. R << "load of type " << NV("Type", Load->getType()) << " not eliminated"
  859. << setExtraArgs();
  860. for (auto *U : Load->getPointerOperand()->users()) {
  861. if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
  862. cast<Instruction>(U)->getFunction() == Load->getFunction() &&
  863. DT->dominates(cast<Instruction>(U), Load)) {
  864. // Use the most immediately dominating value
  865. if (OtherAccess) {
  866. if (DT->dominates(cast<Instruction>(OtherAccess), cast<Instruction>(U)))
  867. OtherAccess = U;
  868. else
  869. assert(DT->dominates(cast<Instruction>(U),
  870. cast<Instruction>(OtherAccess)));
  871. } else
  872. OtherAccess = U;
  873. }
  874. }
  875. if (!OtherAccess) {
  876. // There is no dominating use, check if we can find a closest non-dominating
  877. // use that lies between any other potentially available use and Load.
  878. for (auto *U : Load->getPointerOperand()->users()) {
  879. if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U)) &&
  880. cast<Instruction>(U)->getFunction() == Load->getFunction() &&
  881. isPotentiallyReachable(cast<Instruction>(U), Load, nullptr, DT)) {
  882. if (OtherAccess) {
  883. if (liesBetween(cast<Instruction>(OtherAccess), cast<Instruction>(U),
  884. Load, DT)) {
  885. OtherAccess = U;
  886. } else if (!liesBetween(cast<Instruction>(U),
  887. cast<Instruction>(OtherAccess), Load, DT)) {
  888. // These uses are both partially available at Load were it not for
  889. // the clobber, but neither lies strictly after the other.
  890. OtherAccess = nullptr;
  891. break;
  892. } // else: keep current OtherAccess since it lies between U and Load
  893. } else {
  894. OtherAccess = U;
  895. }
  896. }
  897. }
  898. }
  899. if (OtherAccess)
  900. R << " in favor of " << NV("OtherAccess", OtherAccess);
  901. R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst());
  902. ORE->emit(R);
  903. }
  904. bool GVNPass::AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo,
  905. Value *Address, AvailableValue &Res) {
  906. assert((DepInfo.isDef() || DepInfo.isClobber()) &&
  907. "expected a local dependence");
  908. assert(Load->isUnordered() && "rules below are incorrect for ordered access");
  909. const DataLayout &DL = Load->getModule()->getDataLayout();
  910. Instruction *DepInst = DepInfo.getInst();
  911. if (DepInfo.isClobber()) {
  912. // If the dependence is to a store that writes to a superset of the bits
  913. // read by the load, we can extract the bits we need for the load from the
  914. // stored value.
  915. if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
  916. // Can't forward from non-atomic to atomic without violating memory model.
  917. if (Address && Load->isAtomic() <= DepSI->isAtomic()) {
  918. int Offset =
  919. analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL);
  920. if (Offset != -1) {
  921. Res = AvailableValue::get(DepSI->getValueOperand(), Offset);
  922. return true;
  923. }
  924. }
  925. }
  926. // Check to see if we have something like this:
  927. // load i32* P
  928. // load i8* (P+1)
  929. // if we have this, replace the later with an extraction from the former.
  930. if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) {
  931. // If this is a clobber and L is the first instruction in its block, then
  932. // we have the first instruction in the entry block.
  933. // Can't forward from non-atomic to atomic without violating memory model.
  934. if (DepLoad != Load && Address &&
  935. Load->isAtomic() <= DepLoad->isAtomic()) {
  936. Type *LoadType = Load->getType();
  937. int Offset = -1;
  938. // If MD reported clobber, check it was nested.
  939. if (DepInfo.isClobber() &&
  940. canCoerceMustAliasedValueToLoad(DepLoad, LoadType, DL)) {
  941. const auto ClobberOff = MD->getClobberOffset(DepLoad);
  942. // GVN has no deal with a negative offset.
  943. Offset = (ClobberOff == None || ClobberOff.getValue() < 0)
  944. ? -1
  945. : ClobberOff.getValue();
  946. }
  947. if (Offset == -1)
  948. Offset =
  949. analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL);
  950. if (Offset != -1) {
  951. Res = AvailableValue::getLoad(DepLoad, Offset);
  952. return true;
  953. }
  954. }
  955. }
  956. // If the clobbering value is a memset/memcpy/memmove, see if we can
  957. // forward a value on from it.
  958. if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
  959. if (Address && !Load->isAtomic()) {
  960. int Offset = analyzeLoadFromClobberingMemInst(Load->getType(), Address,
  961. DepMI, DL);
  962. if (Offset != -1) {
  963. Res = AvailableValue::getMI(DepMI, Offset);
  964. return true;
  965. }
  966. }
  967. }
  968. // Nothing known about this clobber, have to be conservative
  969. LLVM_DEBUG(
  970. // fast print dep, using operator<< on instruction is too slow.
  971. dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
  972. dbgs() << " is clobbered by " << *DepInst << '\n';);
  973. if (ORE->allowExtraAnalysis(DEBUG_TYPE))
  974. reportMayClobberedLoad(Load, DepInfo, DT, ORE);
  975. return false;
  976. }
  977. assert(DepInfo.isDef() && "follows from above");
  978. // Loading the alloca -> undef.
  979. // Loading immediately after lifetime begin -> undef.
  980. if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst)) {
  981. Res = AvailableValue::get(UndefValue::get(Load->getType()));
  982. return true;
  983. }
  984. if (isAllocationFn(DepInst, TLI))
  985. if (auto *InitVal = getInitialValueOfAllocation(cast<CallBase>(DepInst),
  986. TLI, Load->getType())) {
  987. Res = AvailableValue::get(InitVal);
  988. return true;
  989. }
  990. if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
  991. // Reject loads and stores that are to the same address but are of
  992. // different types if we have to. If the stored value is convertable to
  993. // the loaded value, we can reuse it.
  994. if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(),
  995. DL))
  996. return false;
  997. // Can't forward from non-atomic to atomic without violating memory model.
  998. if (S->isAtomic() < Load->isAtomic())
  999. return false;
  1000. Res = AvailableValue::get(S->getValueOperand());
  1001. return true;
  1002. }
  1003. if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
  1004. // If the types mismatch and we can't handle it, reject reuse of the load.
  1005. // If the stored value is larger or equal to the loaded value, we can reuse
  1006. // it.
  1007. if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(), DL))
  1008. return false;
  1009. // Can't forward from non-atomic to atomic without violating memory model.
  1010. if (LD->isAtomic() < Load->isAtomic())
  1011. return false;
  1012. Res = AvailableValue::getLoad(LD);
  1013. return true;
  1014. }
  1015. // Unknown def - must be conservative
  1016. LLVM_DEBUG(
  1017. // fast print dep, using operator<< on instruction is too slow.
  1018. dbgs() << "GVN: load "; Load->printAsOperand(dbgs());
  1019. dbgs() << " has unknown def " << *DepInst << '\n';);
  1020. return false;
  1021. }
  1022. void GVNPass::AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps,
  1023. AvailValInBlkVect &ValuesPerBlock,
  1024. UnavailBlkVect &UnavailableBlocks) {
  1025. // Filter out useless results (non-locals, etc). Keep track of the blocks
  1026. // where we have a value available in repl, also keep track of whether we see
  1027. // dependencies that produce an unknown value for the load (such as a call
  1028. // that could potentially clobber the load).
  1029. unsigned NumDeps = Deps.size();
  1030. for (unsigned i = 0, e = NumDeps; i != e; ++i) {
  1031. BasicBlock *DepBB = Deps[i].getBB();
  1032. MemDepResult DepInfo = Deps[i].getResult();
  1033. if (DeadBlocks.count(DepBB)) {
  1034. // Dead dependent mem-op disguise as a load evaluating the same value
  1035. // as the load in question.
  1036. ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
  1037. continue;
  1038. }
  1039. if (!DepInfo.isDef() && !DepInfo.isClobber()) {
  1040. UnavailableBlocks.push_back(DepBB);
  1041. continue;
  1042. }
  1043. // The address being loaded in this non-local block may not be the same as
  1044. // the pointer operand of the load if PHI translation occurs. Make sure
  1045. // to consider the right address.
  1046. Value *Address = Deps[i].getAddress();
  1047. AvailableValue AV;
  1048. if (AnalyzeLoadAvailability(Load, DepInfo, Address, AV)) {
  1049. // subtlety: because we know this was a non-local dependency, we know
  1050. // it's safe to materialize anywhere between the instruction within
  1051. // DepInfo and the end of it's block.
  1052. ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
  1053. std::move(AV)));
  1054. } else {
  1055. UnavailableBlocks.push_back(DepBB);
  1056. }
  1057. }
  1058. assert(NumDeps == ValuesPerBlock.size() + UnavailableBlocks.size() &&
  1059. "post condition violation");
  1060. }
  1061. void GVNPass::eliminatePartiallyRedundantLoad(
  1062. LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
  1063. MapVector<BasicBlock *, Value *> &AvailableLoads) {
  1064. for (const auto &AvailableLoad : AvailableLoads) {
  1065. BasicBlock *UnavailableBlock = AvailableLoad.first;
  1066. Value *LoadPtr = AvailableLoad.second;
  1067. auto *NewLoad =
  1068. new LoadInst(Load->getType(), LoadPtr, Load->getName() + ".pre",
  1069. Load->isVolatile(), Load->getAlign(), Load->getOrdering(),
  1070. Load->getSyncScopeID(), UnavailableBlock->getTerminator());
  1071. NewLoad->setDebugLoc(Load->getDebugLoc());
  1072. if (MSSAU) {
  1073. auto *MSSA = MSSAU->getMemorySSA();
  1074. // Get the defining access of the original load or use the load if it is a
  1075. // MemoryDef (e.g. because it is volatile). The inserted loads are
  1076. // guaranteed to load from the same definition.
  1077. auto *LoadAcc = MSSA->getMemoryAccess(Load);
  1078. auto *DefiningAcc =
  1079. isa<MemoryDef>(LoadAcc) ? LoadAcc : LoadAcc->getDefiningAccess();
  1080. auto *NewAccess = MSSAU->createMemoryAccessInBB(
  1081. NewLoad, DefiningAcc, NewLoad->getParent(),
  1082. MemorySSA::BeforeTerminator);
  1083. if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess))
  1084. MSSAU->insertDef(NewDef, /*RenameUses=*/true);
  1085. else
  1086. MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true);
  1087. }
  1088. // Transfer the old load's AA tags to the new load.
  1089. AAMDNodes Tags = Load->getAAMetadata();
  1090. if (Tags)
  1091. NewLoad->setAAMetadata(Tags);
  1092. if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load))
  1093. NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD);
  1094. if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group))
  1095. NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD);
  1096. if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range))
  1097. NewLoad->setMetadata(LLVMContext::MD_range, RangeMD);
  1098. if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group))
  1099. if (LI &&
  1100. LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock))
  1101. NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD);
  1102. // We do not propagate the old load's debug location, because the new
  1103. // load now lives in a different BB, and we want to avoid a jumpy line
  1104. // table.
  1105. // FIXME: How do we retain source locations without causing poor debugging
  1106. // behavior?
  1107. // Add the newly created load.
  1108. ValuesPerBlock.push_back(
  1109. AvailableValueInBlock::get(UnavailableBlock, NewLoad));
  1110. MD->invalidateCachedPointerInfo(LoadPtr);
  1111. LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
  1112. }
  1113. // Perform PHI construction.
  1114. Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
  1115. Load->replaceAllUsesWith(V);
  1116. if (isa<PHINode>(V))
  1117. V->takeName(Load);
  1118. if (Instruction *I = dyn_cast<Instruction>(V))
  1119. I->setDebugLoc(Load->getDebugLoc());
  1120. if (V->getType()->isPtrOrPtrVectorTy())
  1121. MD->invalidateCachedPointerInfo(V);
  1122. markInstructionForDeletion(Load);
  1123. ORE->emit([&]() {
  1124. return OptimizationRemark(DEBUG_TYPE, "LoadPRE", Load)
  1125. << "load eliminated by PRE";
  1126. });
  1127. }
  1128. bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock,
  1129. UnavailBlkVect &UnavailableBlocks) {
  1130. // Okay, we have *some* definitions of the value. This means that the value
  1131. // is available in some of our (transitive) predecessors. Lets think about
  1132. // doing PRE of this load. This will involve inserting a new load into the
  1133. // predecessor when it's not available. We could do this in general, but
  1134. // prefer to not increase code size. As such, we only do this when we know
  1135. // that we only have to insert *one* load (which means we're basically moving
  1136. // the load, not inserting a new one).
  1137. SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(),
  1138. UnavailableBlocks.end());
  1139. // Let's find the first basic block with more than one predecessor. Walk
  1140. // backwards through predecessors if needed.
  1141. BasicBlock *LoadBB = Load->getParent();
  1142. BasicBlock *TmpBB = LoadBB;
  1143. // Check that there is no implicit control flow instructions above our load in
  1144. // its block. If there is an instruction that doesn't always pass the
  1145. // execution to the following instruction, then moving through it may become
  1146. // invalid. For example:
  1147. //
  1148. // int arr[LEN];
  1149. // int index = ???;
  1150. // ...
  1151. // guard(0 <= index && index < LEN);
  1152. // use(arr[index]);
  1153. //
  1154. // It is illegal to move the array access to any point above the guard,
  1155. // because if the index is out of bounds we should deoptimize rather than
  1156. // access the array.
  1157. // Check that there is no guard in this block above our instruction.
  1158. bool MustEnsureSafetyOfSpeculativeExecution =
  1159. ICF->isDominatedByICFIFromSameBlock(Load);
  1160. while (TmpBB->getSinglePredecessor()) {
  1161. TmpBB = TmpBB->getSinglePredecessor();
  1162. if (TmpBB == LoadBB) // Infinite (unreachable) loop.
  1163. return false;
  1164. if (Blockers.count(TmpBB))
  1165. return false;
  1166. // If any of these blocks has more than one successor (i.e. if the edge we
  1167. // just traversed was critical), then there are other paths through this
  1168. // block along which the load may not be anticipated. Hoisting the load
  1169. // above this block would be adding the load to execution paths along
  1170. // which it was not previously executed.
  1171. if (TmpBB->getTerminator()->getNumSuccessors() != 1)
  1172. return false;
  1173. // Check that there is no implicit control flow in a block above.
  1174. MustEnsureSafetyOfSpeculativeExecution =
  1175. MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB);
  1176. }
  1177. assert(TmpBB);
  1178. LoadBB = TmpBB;
  1179. // Check to see how many predecessors have the loaded value fully
  1180. // available.
  1181. MapVector<BasicBlock *, Value *> PredLoads;
  1182. DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks;
  1183. for (const AvailableValueInBlock &AV : ValuesPerBlock)
  1184. FullyAvailableBlocks[AV.BB] = AvailabilityState::Available;
  1185. for (BasicBlock *UnavailableBB : UnavailableBlocks)
  1186. FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable;
  1187. SmallVector<BasicBlock *, 4> CriticalEdgePred;
  1188. for (BasicBlock *Pred : predecessors(LoadBB)) {
  1189. // If any predecessor block is an EH pad that does not allow non-PHI
  1190. // instructions before the terminator, we can't PRE the load.
  1191. if (Pred->getTerminator()->isEHPad()) {
  1192. LLVM_DEBUG(
  1193. dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '"
  1194. << Pred->getName() << "': " << *Load << '\n');
  1195. return false;
  1196. }
  1197. if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) {
  1198. continue;
  1199. }
  1200. if (Pred->getTerminator()->getNumSuccessors() != 1) {
  1201. if (isa<IndirectBrInst>(Pred->getTerminator())) {
  1202. LLVM_DEBUG(
  1203. dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
  1204. << Pred->getName() << "': " << *Load << '\n');
  1205. return false;
  1206. }
  1207. // FIXME: Can we support the fallthrough edge?
  1208. if (isa<CallBrInst>(Pred->getTerminator())) {
  1209. LLVM_DEBUG(
  1210. dbgs() << "COULD NOT PRE LOAD BECAUSE OF CALLBR CRITICAL EDGE '"
  1211. << Pred->getName() << "': " << *Load << '\n');
  1212. return false;
  1213. }
  1214. if (LoadBB->isEHPad()) {
  1215. LLVM_DEBUG(
  1216. dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '"
  1217. << Pred->getName() << "': " << *Load << '\n');
  1218. return false;
  1219. }
  1220. // Do not split backedge as it will break the canonical loop form.
  1221. if (!isLoadPRESplitBackedgeEnabled())
  1222. if (DT->dominates(LoadBB, Pred)) {
  1223. LLVM_DEBUG(
  1224. dbgs()
  1225. << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '"
  1226. << Pred->getName() << "': " << *Load << '\n');
  1227. return false;
  1228. }
  1229. CriticalEdgePred.push_back(Pred);
  1230. } else {
  1231. // Only add the predecessors that will not be split for now.
  1232. PredLoads[Pred] = nullptr;
  1233. }
  1234. }
  1235. // Decide whether PRE is profitable for this load.
  1236. unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
  1237. assert(NumUnavailablePreds != 0 &&
  1238. "Fully available value should already be eliminated!");
  1239. // If this load is unavailable in multiple predecessors, reject it.
  1240. // FIXME: If we could restructure the CFG, we could make a common pred with
  1241. // all the preds that don't have an available Load and insert a new load into
  1242. // that one block.
  1243. if (NumUnavailablePreds != 1)
  1244. return false;
  1245. // Now we know where we will insert load. We must ensure that it is safe
  1246. // to speculatively execute the load at that points.
  1247. if (MustEnsureSafetyOfSpeculativeExecution) {
  1248. if (CriticalEdgePred.size())
  1249. if (!isSafeToSpeculativelyExecute(Load, LoadBB->getFirstNonPHI(), DT))
  1250. return false;
  1251. for (auto &PL : PredLoads)
  1252. if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), DT))
  1253. return false;
  1254. }
  1255. // Split critical edges, and update the unavailable predecessors accordingly.
  1256. for (BasicBlock *OrigPred : CriticalEdgePred) {
  1257. BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
  1258. assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
  1259. PredLoads[NewPred] = nullptr;
  1260. LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
  1261. << LoadBB->getName() << '\n');
  1262. }
  1263. // Check if the load can safely be moved to all the unavailable predecessors.
  1264. bool CanDoPRE = true;
  1265. const DataLayout &DL = Load->getModule()->getDataLayout();
  1266. SmallVector<Instruction*, 8> NewInsts;
  1267. for (auto &PredLoad : PredLoads) {
  1268. BasicBlock *UnavailablePred = PredLoad.first;
  1269. // Do PHI translation to get its value in the predecessor if necessary. The
  1270. // returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
  1271. // We do the translation for each edge we skipped by going from Load's block
  1272. // to LoadBB, otherwise we might miss pieces needing translation.
  1273. // If all preds have a single successor, then we know it is safe to insert
  1274. // the load on the pred (?!?), so we can insert code to materialize the
  1275. // pointer if it is not available.
  1276. Value *LoadPtr = Load->getPointerOperand();
  1277. BasicBlock *Cur = Load->getParent();
  1278. while (Cur != LoadBB) {
  1279. PHITransAddr Address(LoadPtr, DL, AC);
  1280. LoadPtr = Address.PHITranslateWithInsertion(
  1281. Cur, Cur->getSinglePredecessor(), *DT, NewInsts);
  1282. if (!LoadPtr) {
  1283. CanDoPRE = false;
  1284. break;
  1285. }
  1286. Cur = Cur->getSinglePredecessor();
  1287. }
  1288. if (LoadPtr) {
  1289. PHITransAddr Address(LoadPtr, DL, AC);
  1290. LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred, *DT,
  1291. NewInsts);
  1292. }
  1293. // If we couldn't find or insert a computation of this phi translated value,
  1294. // we fail PRE.
  1295. if (!LoadPtr) {
  1296. LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
  1297. << *Load->getPointerOperand() << "\n");
  1298. CanDoPRE = false;
  1299. break;
  1300. }
  1301. PredLoad.second = LoadPtr;
  1302. }
  1303. if (!CanDoPRE) {
  1304. while (!NewInsts.empty()) {
  1305. // Erase instructions generated by the failed PHI translation before
  1306. // trying to number them. PHI translation might insert instructions
  1307. // in basic blocks other than the current one, and we delete them
  1308. // directly, as markInstructionForDeletion only allows removing from the
  1309. // current basic block.
  1310. NewInsts.pop_back_val()->eraseFromParent();
  1311. }
  1312. // HINT: Don't revert the edge-splitting as following transformation may
  1313. // also need to split these critical edges.
  1314. return !CriticalEdgePred.empty();
  1315. }
  1316. // Okay, we can eliminate this load by inserting a reload in the predecessor
  1317. // and using PHI construction to get the value in the other predecessors, do
  1318. // it.
  1319. LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n');
  1320. LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size()
  1321. << " INSTS: " << *NewInsts.back()
  1322. << '\n');
  1323. // Assign value numbers to the new instructions.
  1324. for (Instruction *I : NewInsts) {
  1325. // Instructions that have been inserted in predecessor(s) to materialize
  1326. // the load address do not retain their original debug locations. Doing
  1327. // so could lead to confusing (but correct) source attributions.
  1328. I->updateLocationAfterHoist();
  1329. // FIXME: We really _ought_ to insert these value numbers into their
  1330. // parent's availability map. However, in doing so, we risk getting into
  1331. // ordering issues. If a block hasn't been processed yet, we would be
  1332. // marking a value as AVAIL-IN, which isn't what we intend.
  1333. VN.lookupOrAdd(I);
  1334. }
  1335. eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads);
  1336. ++NumPRELoad;
  1337. return true;
  1338. }
  1339. bool GVNPass::performLoopLoadPRE(LoadInst *Load,
  1340. AvailValInBlkVect &ValuesPerBlock,
  1341. UnavailBlkVect &UnavailableBlocks) {
  1342. if (!LI)
  1343. return false;
  1344. const Loop *L = LI->getLoopFor(Load->getParent());
  1345. // TODO: Generalize to other loop blocks that dominate the latch.
  1346. if (!L || L->getHeader() != Load->getParent())
  1347. return false;
  1348. BasicBlock *Preheader = L->getLoopPreheader();
  1349. BasicBlock *Latch = L->getLoopLatch();
  1350. if (!Preheader || !Latch)
  1351. return false;
  1352. Value *LoadPtr = Load->getPointerOperand();
  1353. // Must be available in preheader.
  1354. if (!L->isLoopInvariant(LoadPtr))
  1355. return false;
  1356. // We plan to hoist the load to preheader without introducing a new fault.
  1357. // In order to do it, we need to prove that we cannot side-exit the loop
  1358. // once loop header is first entered before execution of the load.
  1359. if (ICF->isDominatedByICFIFromSameBlock(Load))
  1360. return false;
  1361. BasicBlock *LoopBlock = nullptr;
  1362. for (auto *Blocker : UnavailableBlocks) {
  1363. // Blockers from outside the loop are handled in preheader.
  1364. if (!L->contains(Blocker))
  1365. continue;
  1366. // Only allow one loop block. Loop header is not less frequently executed
  1367. // than each loop block, and likely it is much more frequently executed. But
  1368. // in case of multiple loop blocks, we need extra information (such as block
  1369. // frequency info) to understand whether it is profitable to PRE into
  1370. // multiple loop blocks.
  1371. if (LoopBlock)
  1372. return false;
  1373. // Do not sink into inner loops. This may be non-profitable.
  1374. if (L != LI->getLoopFor(Blocker))
  1375. return false;
  1376. // Blocks that dominate the latch execute on every single iteration, maybe
  1377. // except the last one. So PREing into these blocks doesn't make much sense
  1378. // in most cases. But the blocks that do not necessarily execute on each
  1379. // iteration are sometimes much colder than the header, and this is when
  1380. // PRE is potentially profitable.
  1381. if (DT->dominates(Blocker, Latch))
  1382. return false;
  1383. // Make sure that the terminator itself doesn't clobber.
  1384. if (Blocker->getTerminator()->mayWriteToMemory())
  1385. return false;
  1386. LoopBlock = Blocker;
  1387. }
  1388. if (!LoopBlock)
  1389. return false;
  1390. // Make sure the memory at this pointer cannot be freed, therefore we can
  1391. // safely reload from it after clobber.
  1392. if (LoadPtr->canBeFreed())
  1393. return false;
  1394. // TODO: Support critical edge splitting if blocker has more than 1 successor.
  1395. MapVector<BasicBlock *, Value *> AvailableLoads;
  1396. AvailableLoads[LoopBlock] = LoadPtr;
  1397. AvailableLoads[Preheader] = LoadPtr;
  1398. LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n');
  1399. eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads);
  1400. ++NumPRELoopLoad;
  1401. return true;
  1402. }
  1403. static void reportLoadElim(LoadInst *Load, Value *AvailableValue,
  1404. OptimizationRemarkEmitter *ORE) {
  1405. using namespace ore;
  1406. ORE->emit([&]() {
  1407. return OptimizationRemark(DEBUG_TYPE, "LoadElim", Load)
  1408. << "load of type " << NV("Type", Load->getType()) << " eliminated"
  1409. << setExtraArgs() << " in favor of "
  1410. << NV("InfavorOfValue", AvailableValue);
  1411. });
  1412. }
  1413. /// Attempt to eliminate a load whose dependencies are
  1414. /// non-local by performing PHI construction.
  1415. bool GVNPass::processNonLocalLoad(LoadInst *Load) {
  1416. // non-local speculations are not allowed under asan.
  1417. if (Load->getParent()->getParent()->hasFnAttribute(
  1418. Attribute::SanitizeAddress) ||
  1419. Load->getParent()->getParent()->hasFnAttribute(
  1420. Attribute::SanitizeHWAddress))
  1421. return false;
  1422. // Step 1: Find the non-local dependencies of the load.
  1423. LoadDepVect Deps;
  1424. MD->getNonLocalPointerDependency(Load, Deps);
  1425. // If we had to process more than one hundred blocks to find the
  1426. // dependencies, this load isn't worth worrying about. Optimizing
  1427. // it will be too expensive.
  1428. unsigned NumDeps = Deps.size();
  1429. if (NumDeps > MaxNumDeps)
  1430. return false;
  1431. // If we had a phi translation failure, we'll have a single entry which is a
  1432. // clobber in the current block. Reject this early.
  1433. if (NumDeps == 1 &&
  1434. !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
  1435. LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs());
  1436. dbgs() << " has unknown dependencies\n";);
  1437. return false;
  1438. }
  1439. bool Changed = false;
  1440. // If this load follows a GEP, see if we can PRE the indices before analyzing.
  1441. if (GetElementPtrInst *GEP =
  1442. dyn_cast<GetElementPtrInst>(Load->getOperand(0))) {
  1443. for (Use &U : GEP->indices())
  1444. if (Instruction *I = dyn_cast<Instruction>(U.get()))
  1445. Changed |= performScalarPRE(I);
  1446. }
  1447. // Step 2: Analyze the availability of the load
  1448. AvailValInBlkVect ValuesPerBlock;
  1449. UnavailBlkVect UnavailableBlocks;
  1450. AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks);
  1451. // If we have no predecessors that produce a known value for this load, exit
  1452. // early.
  1453. if (ValuesPerBlock.empty())
  1454. return Changed;
  1455. // Step 3: Eliminate fully redundancy.
  1456. //
  1457. // If all of the instructions we depend on produce a known value for this
  1458. // load, then it is fully redundant and we can use PHI insertion to compute
  1459. // its value. Insert PHIs and remove the fully redundant value now.
  1460. if (UnavailableBlocks.empty()) {
  1461. LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n');
  1462. // Perform PHI construction.
  1463. Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this);
  1464. Load->replaceAllUsesWith(V);
  1465. if (isa<PHINode>(V))
  1466. V->takeName(Load);
  1467. if (Instruction *I = dyn_cast<Instruction>(V))
  1468. // If instruction I has debug info, then we should not update it.
  1469. // Also, if I has a null DebugLoc, then it is still potentially incorrect
  1470. // to propagate Load's DebugLoc because Load may not post-dominate I.
  1471. if (Load->getDebugLoc() && Load->getParent() == I->getParent())
  1472. I->setDebugLoc(Load->getDebugLoc());
  1473. if (V->getType()->isPtrOrPtrVectorTy())
  1474. MD->invalidateCachedPointerInfo(V);
  1475. markInstructionForDeletion(Load);
  1476. ++NumGVNLoad;
  1477. reportLoadElim(Load, V, ORE);
  1478. return true;
  1479. }
  1480. // Step 4: Eliminate partial redundancy.
  1481. if (!isPREEnabled() || !isLoadPREEnabled())
  1482. return Changed;
  1483. if (!isLoadInLoopPREEnabled() && LI && LI->getLoopFor(Load->getParent()))
  1484. return Changed;
  1485. if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) ||
  1486. PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks))
  1487. return true;
  1488. return Changed;
  1489. }
  1490. static bool impliesEquivalanceIfTrue(CmpInst* Cmp) {
  1491. if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ)
  1492. return true;
  1493. // Floating point comparisons can be equal, but not equivalent. Cases:
  1494. // NaNs for unordered operators
  1495. // +0.0 vs 0.0 for all operators
  1496. if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ ||
  1497. (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ &&
  1498. Cmp->getFastMathFlags().noNaNs())) {
  1499. Value *LHS = Cmp->getOperand(0);
  1500. Value *RHS = Cmp->getOperand(1);
  1501. // If we can prove either side non-zero, then equality must imply
  1502. // equivalence.
  1503. // FIXME: We should do this optimization if 'no signed zeros' is
  1504. // applicable via an instruction-level fast-math-flag or some other
  1505. // indicator that relaxed FP semantics are being used.
  1506. if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
  1507. return true;
  1508. if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
  1509. return true;;
  1510. // TODO: Handle vector floating point constants
  1511. }
  1512. return false;
  1513. }
  1514. static bool impliesEquivalanceIfFalse(CmpInst* Cmp) {
  1515. if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE)
  1516. return true;
  1517. // Floating point comparisons can be equal, but not equivelent. Cases:
  1518. // NaNs for unordered operators
  1519. // +0.0 vs 0.0 for all operators
  1520. if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE &&
  1521. Cmp->getFastMathFlags().noNaNs()) ||
  1522. Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) {
  1523. Value *LHS = Cmp->getOperand(0);
  1524. Value *RHS = Cmp->getOperand(1);
  1525. // If we can prove either side non-zero, then equality must imply
  1526. // equivalence.
  1527. // FIXME: We should do this optimization if 'no signed zeros' is
  1528. // applicable via an instruction-level fast-math-flag or some other
  1529. // indicator that relaxed FP semantics are being used.
  1530. if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero())
  1531. return true;
  1532. if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero())
  1533. return true;;
  1534. // TODO: Handle vector floating point constants
  1535. }
  1536. return false;
  1537. }
  1538. static bool hasUsersIn(Value *V, BasicBlock *BB) {
  1539. for (User *U : V->users())
  1540. if (isa<Instruction>(U) &&
  1541. cast<Instruction>(U)->getParent() == BB)
  1542. return true;
  1543. return false;
  1544. }
  1545. bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
  1546. Value *V = IntrinsicI->getArgOperand(0);
  1547. if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) {
  1548. if (Cond->isZero()) {
  1549. Type *Int8Ty = Type::getInt8Ty(V->getContext());
  1550. // Insert a new store to null instruction before the load to indicate that
  1551. // this code is not reachable. FIXME: We could insert unreachable
  1552. // instruction directly because we can modify the CFG.
  1553. auto *NewS = new StoreInst(PoisonValue::get(Int8Ty),
  1554. Constant::getNullValue(Int8Ty->getPointerTo()),
  1555. IntrinsicI);
  1556. if (MSSAU) {
  1557. const MemoryUseOrDef *FirstNonDom = nullptr;
  1558. const auto *AL =
  1559. MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent());
  1560. // If there are accesses in the current basic block, find the first one
  1561. // that does not come before NewS. The new memory access is inserted
  1562. // after the found access or before the terminator if no such access is
  1563. // found.
  1564. if (AL) {
  1565. for (auto &Acc : *AL) {
  1566. if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc))
  1567. if (!Current->getMemoryInst()->comesBefore(NewS)) {
  1568. FirstNonDom = Current;
  1569. break;
  1570. }
  1571. }
  1572. }
  1573. // This added store is to null, so it will never executed and we can
  1574. // just use the LiveOnEntry def as defining access.
  1575. auto *NewDef =
  1576. FirstNonDom ? MSSAU->createMemoryAccessBefore(
  1577. NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
  1578. const_cast<MemoryUseOrDef *>(FirstNonDom))
  1579. : MSSAU->createMemoryAccessInBB(
  1580. NewS, MSSAU->getMemorySSA()->getLiveOnEntryDef(),
  1581. NewS->getParent(), MemorySSA::BeforeTerminator);
  1582. MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false);
  1583. }
  1584. }
  1585. if (isAssumeWithEmptyBundle(*IntrinsicI))
  1586. markInstructionForDeletion(IntrinsicI);
  1587. return false;
  1588. } else if (isa<Constant>(V)) {
  1589. // If it's not false, and constant, it must evaluate to true. This means our
  1590. // assume is assume(true), and thus, pointless, and we don't want to do
  1591. // anything more here.
  1592. return false;
  1593. }
  1594. Constant *True = ConstantInt::getTrue(V->getContext());
  1595. bool Changed = false;
  1596. for (BasicBlock *Successor : successors(IntrinsicI->getParent())) {
  1597. BasicBlockEdge Edge(IntrinsicI->getParent(), Successor);
  1598. // This property is only true in dominated successors, propagateEquality
  1599. // will check dominance for us.
  1600. Changed |= propagateEquality(V, True, Edge, false);
  1601. }
  1602. // We can replace assume value with true, which covers cases like this:
  1603. // call void @llvm.assume(i1 %cmp)
  1604. // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true
  1605. ReplaceOperandsWithMap[V] = True;
  1606. // Similarly, after assume(!NotV) we know that NotV == false.
  1607. Value *NotV;
  1608. if (match(V, m_Not(m_Value(NotV))))
  1609. ReplaceOperandsWithMap[NotV] = ConstantInt::getFalse(V->getContext());
  1610. // If we find an equality fact, canonicalize all dominated uses in this block
  1611. // to one of the two values. We heuristically choice the "oldest" of the
  1612. // two where age is determined by value number. (Note that propagateEquality
  1613. // above handles the cross block case.)
  1614. //
  1615. // Key case to cover are:
  1616. // 1)
  1617. // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen
  1618. // call void @llvm.assume(i1 %cmp)
  1619. // ret float %0 ; will change it to ret float 3.000000e+00
  1620. // 2)
  1621. // %load = load float, float* %addr
  1622. // %cmp = fcmp oeq float %load, %0
  1623. // call void @llvm.assume(i1 %cmp)
  1624. // ret float %load ; will change it to ret float %0
  1625. if (auto *CmpI = dyn_cast<CmpInst>(V)) {
  1626. if (impliesEquivalanceIfTrue(CmpI)) {
  1627. Value *CmpLHS = CmpI->getOperand(0);
  1628. Value *CmpRHS = CmpI->getOperand(1);
  1629. // Heuristically pick the better replacement -- the choice of heuristic
  1630. // isn't terribly important here, but the fact we canonicalize on some
  1631. // replacement is for exposing other simplifications.
  1632. // TODO: pull this out as a helper function and reuse w/existing
  1633. // (slightly different) logic.
  1634. if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS))
  1635. std::swap(CmpLHS, CmpRHS);
  1636. if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))
  1637. std::swap(CmpLHS, CmpRHS);
  1638. if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) ||
  1639. (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) {
  1640. // Move the 'oldest' value to the right-hand side, using the value
  1641. // number as a proxy for age.
  1642. uint32_t LVN = VN.lookupOrAdd(CmpLHS);
  1643. uint32_t RVN = VN.lookupOrAdd(CmpRHS);
  1644. if (LVN < RVN)
  1645. std::swap(CmpLHS, CmpRHS);
  1646. }
  1647. // Handle degenerate case where we either haven't pruned a dead path or a
  1648. // removed a trivial assume yet.
  1649. if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS))
  1650. return Changed;
  1651. LLVM_DEBUG(dbgs() << "Replacing dominated uses of "
  1652. << *CmpLHS << " with "
  1653. << *CmpRHS << " in block "
  1654. << IntrinsicI->getParent()->getName() << "\n");
  1655. // Setup the replacement map - this handles uses within the same block
  1656. if (hasUsersIn(CmpLHS, IntrinsicI->getParent()))
  1657. ReplaceOperandsWithMap[CmpLHS] = CmpRHS;
  1658. // NOTE: The non-block local cases are handled by the call to
  1659. // propagateEquality above; this block is just about handling the block
  1660. // local cases. TODO: There's a bunch of logic in propagateEqualiy which
  1661. // isn't duplicated for the block local case, can we share it somehow?
  1662. }
  1663. }
  1664. return Changed;
  1665. }
  1666. static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
  1667. patchReplacementInstruction(I, Repl);
  1668. I->replaceAllUsesWith(Repl);
  1669. }
  1670. /// Attempt to eliminate a load, first by eliminating it
  1671. /// locally, and then attempting non-local elimination if that fails.
  1672. bool GVNPass::processLoad(LoadInst *L) {
  1673. if (!MD)
  1674. return false;
  1675. // This code hasn't been audited for ordered or volatile memory access
  1676. if (!L->isUnordered())
  1677. return false;
  1678. if (L->use_empty()) {
  1679. markInstructionForDeletion(L);
  1680. return true;
  1681. }
  1682. // ... to a pointer that has been loaded from before...
  1683. MemDepResult Dep = MD->getDependency(L);
  1684. // If it is defined in another block, try harder.
  1685. if (Dep.isNonLocal())
  1686. return processNonLocalLoad(L);
  1687. // Only handle the local case below
  1688. if (!Dep.isDef() && !Dep.isClobber()) {
  1689. // This might be a NonFuncLocal or an Unknown
  1690. LLVM_DEBUG(
  1691. // fast print dep, using operator<< on instruction is too slow.
  1692. dbgs() << "GVN: load "; L->printAsOperand(dbgs());
  1693. dbgs() << " has unknown dependence\n";);
  1694. return false;
  1695. }
  1696. AvailableValue AV;
  1697. if (AnalyzeLoadAvailability(L, Dep, L->getPointerOperand(), AV)) {
  1698. Value *AvailableValue = AV.MaterializeAdjustedValue(L, L, *this);
  1699. // Replace the load!
  1700. patchAndReplaceAllUsesWith(L, AvailableValue);
  1701. markInstructionForDeletion(L);
  1702. if (MSSAU)
  1703. MSSAU->removeMemoryAccess(L);
  1704. ++NumGVNLoad;
  1705. reportLoadElim(L, AvailableValue, ORE);
  1706. // Tell MDA to reexamine the reused pointer since we might have more
  1707. // information after forwarding it.
  1708. if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy())
  1709. MD->invalidateCachedPointerInfo(AvailableValue);
  1710. return true;
  1711. }
  1712. return false;
  1713. }
  1714. /// Return a pair the first field showing the value number of \p Exp and the
  1715. /// second field showing whether it is a value number newly created.
  1716. std::pair<uint32_t, bool>
  1717. GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) {
  1718. uint32_t &e = expressionNumbering[Exp];
  1719. bool CreateNewValNum = !e;
  1720. if (CreateNewValNum) {
  1721. Expressions.push_back(Exp);
  1722. if (ExprIdx.size() < nextValueNumber + 1)
  1723. ExprIdx.resize(nextValueNumber * 2);
  1724. e = nextValueNumber;
  1725. ExprIdx[nextValueNumber++] = nextExprNumber++;
  1726. }
  1727. return {e, CreateNewValNum};
  1728. }
  1729. /// Return whether all the values related with the same \p num are
  1730. /// defined in \p BB.
  1731. bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB,
  1732. GVNPass &Gvn) {
  1733. LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
  1734. while (Vals && Vals->BB == BB)
  1735. Vals = Vals->Next;
  1736. return !Vals;
  1737. }
  1738. /// Wrap phiTranslateImpl to provide caching functionality.
  1739. uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred,
  1740. const BasicBlock *PhiBlock,
  1741. uint32_t Num, GVNPass &Gvn) {
  1742. auto FindRes = PhiTranslateTable.find({Num, Pred});
  1743. if (FindRes != PhiTranslateTable.end())
  1744. return FindRes->second;
  1745. uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn);
  1746. PhiTranslateTable.insert({{Num, Pred}, NewNum});
  1747. return NewNum;
  1748. }
  1749. // Return true if the value number \p Num and NewNum have equal value.
  1750. // Return false if the result is unknown.
  1751. bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum,
  1752. const BasicBlock *Pred,
  1753. const BasicBlock *PhiBlock,
  1754. GVNPass &Gvn) {
  1755. CallInst *Call = nullptr;
  1756. LeaderTableEntry *Vals = &Gvn.LeaderTable[Num];
  1757. while (Vals) {
  1758. Call = dyn_cast<CallInst>(Vals->Val);
  1759. if (Call && Call->getParent() == PhiBlock)
  1760. break;
  1761. Vals = Vals->Next;
  1762. }
  1763. if (AA->doesNotAccessMemory(Call))
  1764. return true;
  1765. if (!MD || !AA->onlyReadsMemory(Call))
  1766. return false;
  1767. MemDepResult local_dep = MD->getDependency(Call);
  1768. if (!local_dep.isNonLocal())
  1769. return false;
  1770. const MemoryDependenceResults::NonLocalDepInfo &deps =
  1771. MD->getNonLocalCallDependency(Call);
  1772. // Check to see if the Call has no function local clobber.
  1773. for (const NonLocalDepEntry &D : deps) {
  1774. if (D.getResult().isNonFuncLocal())
  1775. return true;
  1776. }
  1777. return false;
  1778. }
  1779. /// Translate value number \p Num using phis, so that it has the values of
  1780. /// the phis in BB.
  1781. uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred,
  1782. const BasicBlock *PhiBlock,
  1783. uint32_t Num, GVNPass &Gvn) {
  1784. if (PHINode *PN = NumberingPhi[Num]) {
  1785. for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
  1786. if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred)
  1787. if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false))
  1788. return TransVal;
  1789. }
  1790. return Num;
  1791. }
  1792. // If there is any value related with Num is defined in a BB other than
  1793. // PhiBlock, it cannot depend on a phi in PhiBlock without going through
  1794. // a backedge. We can do an early exit in that case to save compile time.
  1795. if (!areAllValsInBB(Num, PhiBlock, Gvn))
  1796. return Num;
  1797. if (Num >= ExprIdx.size() || ExprIdx[Num] == 0)
  1798. return Num;
  1799. Expression Exp = Expressions[ExprIdx[Num]];
  1800. for (unsigned i = 0; i < Exp.varargs.size(); i++) {
  1801. // For InsertValue and ExtractValue, some varargs are index numbers
  1802. // instead of value numbers. Those index numbers should not be
  1803. // translated.
  1804. if ((i > 1 && Exp.opcode == Instruction::InsertValue) ||
  1805. (i > 0 && Exp.opcode == Instruction::ExtractValue) ||
  1806. (i > 1 && Exp.opcode == Instruction::ShuffleVector))
  1807. continue;
  1808. Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn);
  1809. }
  1810. if (Exp.commutative) {
  1811. assert(Exp.varargs.size() >= 2 && "Unsupported commutative instruction!");
  1812. if (Exp.varargs[0] > Exp.varargs[1]) {
  1813. std::swap(Exp.varargs[0], Exp.varargs[1]);
  1814. uint32_t Opcode = Exp.opcode >> 8;
  1815. if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)
  1816. Exp.opcode = (Opcode << 8) |
  1817. CmpInst::getSwappedPredicate(
  1818. static_cast<CmpInst::Predicate>(Exp.opcode & 255));
  1819. }
  1820. }
  1821. if (uint32_t NewNum = expressionNumbering[Exp]) {
  1822. if (Exp.opcode == Instruction::Call && NewNum != Num)
  1823. return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num;
  1824. return NewNum;
  1825. }
  1826. return Num;
  1827. }
  1828. /// Erase stale entry from phiTranslate cache so phiTranslate can be computed
  1829. /// again.
  1830. void GVNPass::ValueTable::eraseTranslateCacheEntry(
  1831. uint32_t Num, const BasicBlock &CurrBlock) {
  1832. for (const BasicBlock *Pred : predecessors(&CurrBlock))
  1833. PhiTranslateTable.erase({Num, Pred});
  1834. }
  1835. // In order to find a leader for a given value number at a
  1836. // specific basic block, we first obtain the list of all Values for that number,
  1837. // and then scan the list to find one whose block dominates the block in
  1838. // question. This is fast because dominator tree queries consist of only
  1839. // a few comparisons of DFS numbers.
  1840. Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t num) {
  1841. LeaderTableEntry Vals = LeaderTable[num];
  1842. if (!Vals.Val) return nullptr;
  1843. Value *Val = nullptr;
  1844. if (DT->dominates(Vals.BB, BB)) {
  1845. Val = Vals.Val;
  1846. if (isa<Constant>(Val)) return Val;
  1847. }
  1848. LeaderTableEntry* Next = Vals.Next;
  1849. while (Next) {
  1850. if (DT->dominates(Next->BB, BB)) {
  1851. if (isa<Constant>(Next->Val)) return Next->Val;
  1852. if (!Val) Val = Next->Val;
  1853. }
  1854. Next = Next->Next;
  1855. }
  1856. return Val;
  1857. }
  1858. /// There is an edge from 'Src' to 'Dst'. Return
  1859. /// true if every path from the entry block to 'Dst' passes via this edge. In
  1860. /// particular 'Dst' must not be reachable via another edge from 'Src'.
  1861. static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
  1862. DominatorTree *DT) {
  1863. // While in theory it is interesting to consider the case in which Dst has
  1864. // more than one predecessor, because Dst might be part of a loop which is
  1865. // only reachable from Src, in practice it is pointless since at the time
  1866. // GVN runs all such loops have preheaders, which means that Dst will have
  1867. // been changed to have only one predecessor, namely Src.
  1868. const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
  1869. assert((!Pred || Pred == E.getStart()) &&
  1870. "No edge between these basic blocks!");
  1871. return Pred != nullptr;
  1872. }
  1873. void GVNPass::assignBlockRPONumber(Function &F) {
  1874. BlockRPONumber.clear();
  1875. uint32_t NextBlockNumber = 1;
  1876. ReversePostOrderTraversal<Function *> RPOT(&F);
  1877. for (BasicBlock *BB : RPOT)
  1878. BlockRPONumber[BB] = NextBlockNumber++;
  1879. InvalidBlockRPONumbers = false;
  1880. }
  1881. bool GVNPass::replaceOperandsForInBlockEquality(Instruction *Instr) const {
  1882. bool Changed = false;
  1883. for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) {
  1884. Value *Operand = Instr->getOperand(OpNum);
  1885. auto it = ReplaceOperandsWithMap.find(Operand);
  1886. if (it != ReplaceOperandsWithMap.end()) {
  1887. LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with "
  1888. << *it->second << " in instruction " << *Instr << '\n');
  1889. Instr->setOperand(OpNum, it->second);
  1890. Changed = true;
  1891. }
  1892. }
  1893. return Changed;
  1894. }
  1895. /// The given values are known to be equal in every block
  1896. /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
  1897. /// 'RHS' everywhere in the scope. Returns whether a change was made.
  1898. /// If DominatesByEdge is false, then it means that we will propagate the RHS
  1899. /// value starting from the end of Root.Start.
  1900. bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
  1901. const BasicBlockEdge &Root,
  1902. bool DominatesByEdge) {
  1903. SmallVector<std::pair<Value*, Value*>, 4> Worklist;
  1904. Worklist.push_back(std::make_pair(LHS, RHS));
  1905. bool Changed = false;
  1906. // For speed, compute a conservative fast approximation to
  1907. // DT->dominates(Root, Root.getEnd());
  1908. const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
  1909. while (!Worklist.empty()) {
  1910. std::pair<Value*, Value*> Item = Worklist.pop_back_val();
  1911. LHS = Item.first; RHS = Item.second;
  1912. if (LHS == RHS)
  1913. continue;
  1914. assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
  1915. // Don't try to propagate equalities between constants.
  1916. if (isa<Constant>(LHS) && isa<Constant>(RHS))
  1917. continue;
  1918. // Prefer a constant on the right-hand side, or an Argument if no constants.
  1919. if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
  1920. std::swap(LHS, RHS);
  1921. assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
  1922. // If there is no obvious reason to prefer the left-hand side over the
  1923. // right-hand side, ensure the longest lived term is on the right-hand side,
  1924. // so the shortest lived term will be replaced by the longest lived.
  1925. // This tends to expose more simplifications.
  1926. uint32_t LVN = VN.lookupOrAdd(LHS);
  1927. if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
  1928. (isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
  1929. // Move the 'oldest' value to the right-hand side, using the value number
  1930. // as a proxy for age.
  1931. uint32_t RVN = VN.lookupOrAdd(RHS);
  1932. if (LVN < RVN) {
  1933. std::swap(LHS, RHS);
  1934. LVN = RVN;
  1935. }
  1936. }
  1937. // If value numbering later sees that an instruction in the scope is equal
  1938. // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
  1939. // the invariant that instructions only occur in the leader table for their
  1940. // own value number (this is used by removeFromLeaderTable), do not do this
  1941. // if RHS is an instruction (if an instruction in the scope is morphed into
  1942. // LHS then it will be turned into RHS by the next GVN iteration anyway, so
  1943. // using the leader table is about compiling faster, not optimizing better).
  1944. // The leader table only tracks basic blocks, not edges. Only add to if we
  1945. // have the simple case where the edge dominates the end.
  1946. if (RootDominatesEnd && !isa<Instruction>(RHS))
  1947. addToLeaderTable(LVN, RHS, Root.getEnd());
  1948. // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
  1949. // LHS always has at least one use that is not dominated by Root, this will
  1950. // never do anything if LHS has only one use.
  1951. if (!LHS->hasOneUse()) {
  1952. unsigned NumReplacements =
  1953. DominatesByEdge
  1954. ? replaceDominatedUsesWith(LHS, RHS, *DT, Root)
  1955. : replaceDominatedUsesWith(LHS, RHS, *DT, Root.getStart());
  1956. Changed |= NumReplacements > 0;
  1957. NumGVNEqProp += NumReplacements;
  1958. // Cached information for anything that uses LHS will be invalid.
  1959. if (MD)
  1960. MD->invalidateCachedPointerInfo(LHS);
  1961. }
  1962. // Now try to deduce additional equalities from this one. For example, if
  1963. // the known equality was "(A != B)" == "false" then it follows that A and B
  1964. // are equal in the scope. Only boolean equalities with an explicit true or
  1965. // false RHS are currently supported.
  1966. if (!RHS->getType()->isIntegerTy(1))
  1967. // Not a boolean equality - bail out.
  1968. continue;
  1969. ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
  1970. if (!CI)
  1971. // RHS neither 'true' nor 'false' - bail out.
  1972. continue;
  1973. // Whether RHS equals 'true'. Otherwise it equals 'false'.
  1974. bool isKnownTrue = CI->isMinusOne();
  1975. bool isKnownFalse = !isKnownTrue;
  1976. // If "A && B" is known true then both A and B are known true. If "A || B"
  1977. // is known false then both A and B are known false.
  1978. Value *A, *B;
  1979. if ((isKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) ||
  1980. (isKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) {
  1981. Worklist.push_back(std::make_pair(A, RHS));
  1982. Worklist.push_back(std::make_pair(B, RHS));
  1983. continue;
  1984. }
  1985. // If we are propagating an equality like "(A == B)" == "true" then also
  1986. // propagate the equality A == B. When propagating a comparison such as
  1987. // "(A >= B)" == "true", replace all instances of "A < B" with "false".
  1988. if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
  1989. Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
  1990. // If "A == B" is known true, or "A != B" is known false, then replace
  1991. // A with B everywhere in the scope. For floating point operations, we
  1992. // have to be careful since equality does not always imply equivalance.
  1993. if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) ||
  1994. (isKnownFalse && impliesEquivalanceIfFalse(Cmp)))
  1995. Worklist.push_back(std::make_pair(Op0, Op1));
  1996. // If "A >= B" is known true, replace "A < B" with false everywhere.
  1997. CmpInst::Predicate NotPred = Cmp->getInversePredicate();
  1998. Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
  1999. // Since we don't have the instruction "A < B" immediately to hand, work
  2000. // out the value number that it would have and use that to find an
  2001. // appropriate instruction (if any).
  2002. uint32_t NextNum = VN.getNextUnusedValueNumber();
  2003. uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1);
  2004. // If the number we were assigned was brand new then there is no point in
  2005. // looking for an instruction realizing it: there cannot be one!
  2006. if (Num < NextNum) {
  2007. Value *NotCmp = findLeader(Root.getEnd(), Num);
  2008. if (NotCmp && isa<Instruction>(NotCmp)) {
  2009. unsigned NumReplacements =
  2010. DominatesByEdge
  2011. ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root)
  2012. : replaceDominatedUsesWith(NotCmp, NotVal, *DT,
  2013. Root.getStart());
  2014. Changed |= NumReplacements > 0;
  2015. NumGVNEqProp += NumReplacements;
  2016. // Cached information for anything that uses NotCmp will be invalid.
  2017. if (MD)
  2018. MD->invalidateCachedPointerInfo(NotCmp);
  2019. }
  2020. }
  2021. // Ensure that any instruction in scope that gets the "A < B" value number
  2022. // is replaced with false.
  2023. // The leader table only tracks basic blocks, not edges. Only add to if we
  2024. // have the simple case where the edge dominates the end.
  2025. if (RootDominatesEnd)
  2026. addToLeaderTable(Num, NotVal, Root.getEnd());
  2027. continue;
  2028. }
  2029. }
  2030. return Changed;
  2031. }
  2032. /// When calculating availability, handle an instruction
  2033. /// by inserting it into the appropriate sets
  2034. bool GVNPass::processInstruction(Instruction *I) {
  2035. // Ignore dbg info intrinsics.
  2036. if (isa<DbgInfoIntrinsic>(I))
  2037. return false;
  2038. // If the instruction can be easily simplified then do so now in preference
  2039. // to value numbering it. Value numbering often exposes redundancies, for
  2040. // example if it determines that %y is equal to %x then the instruction
  2041. // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
  2042. const DataLayout &DL = I->getModule()->getDataLayout();
  2043. if (Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC})) {
  2044. bool Changed = false;
  2045. if (!I->use_empty()) {
  2046. // Simplification can cause a special instruction to become not special.
  2047. // For example, devirtualization to a willreturn function.
  2048. ICF->removeUsersOf(I);
  2049. I->replaceAllUsesWith(V);
  2050. Changed = true;
  2051. }
  2052. if (isInstructionTriviallyDead(I, TLI)) {
  2053. markInstructionForDeletion(I);
  2054. Changed = true;
  2055. }
  2056. if (Changed) {
  2057. if (MD && V->getType()->isPtrOrPtrVectorTy())
  2058. MD->invalidateCachedPointerInfo(V);
  2059. ++NumGVNSimpl;
  2060. return true;
  2061. }
  2062. }
  2063. if (auto *Assume = dyn_cast<AssumeInst>(I))
  2064. return processAssumeIntrinsic(Assume);
  2065. if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
  2066. if (processLoad(Load))
  2067. return true;
  2068. unsigned Num = VN.lookupOrAdd(Load);
  2069. addToLeaderTable(Num, Load, Load->getParent());
  2070. return false;
  2071. }
  2072. // For conditional branches, we can perform simple conditional propagation on
  2073. // the condition value itself.
  2074. if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
  2075. if (!BI->isConditional())
  2076. return false;
  2077. if (isa<Constant>(BI->getCondition()))
  2078. return processFoldableCondBr(BI);
  2079. Value *BranchCond = BI->getCondition();
  2080. BasicBlock *TrueSucc = BI->getSuccessor(0);
  2081. BasicBlock *FalseSucc = BI->getSuccessor(1);
  2082. // Avoid multiple edges early.
  2083. if (TrueSucc == FalseSucc)
  2084. return false;
  2085. BasicBlock *Parent = BI->getParent();
  2086. bool Changed = false;
  2087. Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
  2088. BasicBlockEdge TrueE(Parent, TrueSucc);
  2089. Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true);
  2090. Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
  2091. BasicBlockEdge FalseE(Parent, FalseSucc);
  2092. Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true);
  2093. return Changed;
  2094. }
  2095. // For switches, propagate the case values into the case destinations.
  2096. if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
  2097. Value *SwitchCond = SI->getCondition();
  2098. BasicBlock *Parent = SI->getParent();
  2099. bool Changed = false;
  2100. // Remember how many outgoing edges there are to every successor.
  2101. SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
  2102. for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
  2103. ++SwitchEdges[SI->getSuccessor(i)];
  2104. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  2105. i != e; ++i) {
  2106. BasicBlock *Dst = i->getCaseSuccessor();
  2107. // If there is only a single edge, propagate the case value into it.
  2108. if (SwitchEdges.lookup(Dst) == 1) {
  2109. BasicBlockEdge E(Parent, Dst);
  2110. Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true);
  2111. }
  2112. }
  2113. return Changed;
  2114. }
  2115. // Instructions with void type don't return a value, so there's
  2116. // no point in trying to find redundancies in them.
  2117. if (I->getType()->isVoidTy())
  2118. return false;
  2119. uint32_t NextNum = VN.getNextUnusedValueNumber();
  2120. unsigned Num = VN.lookupOrAdd(I);
  2121. // Allocations are always uniquely numbered, so we can save time and memory
  2122. // by fast failing them.
  2123. if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) {
  2124. addToLeaderTable(Num, I, I->getParent());
  2125. return false;
  2126. }
  2127. // If the number we were assigned was a brand new VN, then we don't
  2128. // need to do a lookup to see if the number already exists
  2129. // somewhere in the domtree: it can't!
  2130. if (Num >= NextNum) {
  2131. addToLeaderTable(Num, I, I->getParent());
  2132. return false;
  2133. }
  2134. // Perform fast-path value-number based elimination of values inherited from
  2135. // dominators.
  2136. Value *Repl = findLeader(I->getParent(), Num);
  2137. if (!Repl) {
  2138. // Failure, just remember this instance for future use.
  2139. addToLeaderTable(Num, I, I->getParent());
  2140. return false;
  2141. } else if (Repl == I) {
  2142. // If I was the result of a shortcut PRE, it might already be in the table
  2143. // and the best replacement for itself. Nothing to do.
  2144. return false;
  2145. }
  2146. // Remove it!
  2147. patchAndReplaceAllUsesWith(I, Repl);
  2148. if (MD && Repl->getType()->isPtrOrPtrVectorTy())
  2149. MD->invalidateCachedPointerInfo(Repl);
  2150. markInstructionForDeletion(I);
  2151. return true;
  2152. }
  2153. /// runOnFunction - This is the main transformation entry point for a function.
  2154. bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
  2155. const TargetLibraryInfo &RunTLI, AAResults &RunAA,
  2156. MemoryDependenceResults *RunMD, LoopInfo *LI,
  2157. OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) {
  2158. AC = &RunAC;
  2159. DT = &RunDT;
  2160. VN.setDomTree(DT);
  2161. TLI = &RunTLI;
  2162. VN.setAliasAnalysis(&RunAA);
  2163. MD = RunMD;
  2164. ImplicitControlFlowTracking ImplicitCFT;
  2165. ICF = &ImplicitCFT;
  2166. this->LI = LI;
  2167. VN.setMemDep(MD);
  2168. ORE = RunORE;
  2169. InvalidBlockRPONumbers = true;
  2170. MemorySSAUpdater Updater(MSSA);
  2171. MSSAU = MSSA ? &Updater : nullptr;
  2172. bool Changed = false;
  2173. bool ShouldContinue = true;
  2174. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
  2175. // Merge unconditional branches, allowing PRE to catch more
  2176. // optimization opportunities.
  2177. for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
  2178. bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, LI, MSSAU, MD);
  2179. if (removedBlock)
  2180. ++NumGVNBlocks;
  2181. Changed |= removedBlock;
  2182. }
  2183. unsigned Iteration = 0;
  2184. while (ShouldContinue) {
  2185. LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
  2186. ShouldContinue = iterateOnFunction(F);
  2187. Changed |= ShouldContinue;
  2188. ++Iteration;
  2189. }
  2190. if (isPREEnabled()) {
  2191. // Fabricate val-num for dead-code in order to suppress assertion in
  2192. // performPRE().
  2193. assignValNumForDeadCode();
  2194. bool PREChanged = true;
  2195. while (PREChanged) {
  2196. PREChanged = performPRE(F);
  2197. Changed |= PREChanged;
  2198. }
  2199. }
  2200. // FIXME: Should perform GVN again after PRE does something. PRE can move
  2201. // computations into blocks where they become fully redundant. Note that
  2202. // we can't do this until PRE's critical edge splitting updates memdep.
  2203. // Actually, when this happens, we should just fully integrate PRE into GVN.
  2204. cleanupGlobalSets();
  2205. // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
  2206. // iteration.
  2207. DeadBlocks.clear();
  2208. if (MSSA && VerifyMemorySSA)
  2209. MSSA->verifyMemorySSA();
  2210. return Changed;
  2211. }
  2212. bool GVNPass::processBlock(BasicBlock *BB) {
  2213. // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
  2214. // (and incrementing BI before processing an instruction).
  2215. assert(InstrsToErase.empty() &&
  2216. "We expect InstrsToErase to be empty across iterations");
  2217. if (DeadBlocks.count(BB))
  2218. return false;
  2219. // Clearing map before every BB because it can be used only for single BB.
  2220. ReplaceOperandsWithMap.clear();
  2221. bool ChangedFunction = false;
  2222. // Since we may not have visited the input blocks of the phis, we can't
  2223. // use our normal hash approach for phis. Instead, simply look for
  2224. // obvious duplicates. The first pass of GVN will tend to create
  2225. // identical phis, and the second or later passes can eliminate them.
  2226. ChangedFunction |= EliminateDuplicatePHINodes(BB);
  2227. for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
  2228. BI != BE;) {
  2229. if (!ReplaceOperandsWithMap.empty())
  2230. ChangedFunction |= replaceOperandsForInBlockEquality(&*BI);
  2231. ChangedFunction |= processInstruction(&*BI);
  2232. if (InstrsToErase.empty()) {
  2233. ++BI;
  2234. continue;
  2235. }
  2236. // If we need some instructions deleted, do it now.
  2237. NumGVNInstr += InstrsToErase.size();
  2238. // Avoid iterator invalidation.
  2239. bool AtStart = BI == BB->begin();
  2240. if (!AtStart)
  2241. --BI;
  2242. for (auto *I : InstrsToErase) {
  2243. assert(I->getParent() == BB && "Removing instruction from wrong block?");
  2244. LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n');
  2245. salvageKnowledge(I, AC);
  2246. salvageDebugInfo(*I);
  2247. if (MD) MD->removeInstruction(I);
  2248. if (MSSAU)
  2249. MSSAU->removeMemoryAccess(I);
  2250. LLVM_DEBUG(verifyRemoved(I));
  2251. ICF->removeInstruction(I);
  2252. I->eraseFromParent();
  2253. }
  2254. InstrsToErase.clear();
  2255. if (AtStart)
  2256. BI = BB->begin();
  2257. else
  2258. ++BI;
  2259. }
  2260. return ChangedFunction;
  2261. }
  2262. // Instantiate an expression in a predecessor that lacked it.
  2263. bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred,
  2264. BasicBlock *Curr, unsigned int ValNo) {
  2265. // Because we are going top-down through the block, all value numbers
  2266. // will be available in the predecessor by the time we need them. Any
  2267. // that weren't originally present will have been instantiated earlier
  2268. // in this loop.
  2269. bool success = true;
  2270. for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) {
  2271. Value *Op = Instr->getOperand(i);
  2272. if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
  2273. continue;
  2274. // This could be a newly inserted instruction, in which case, we won't
  2275. // find a value number, and should give up before we hurt ourselves.
  2276. // FIXME: Rewrite the infrastructure to let it easier to value number
  2277. // and process newly inserted instructions.
  2278. if (!VN.exists(Op)) {
  2279. success = false;
  2280. break;
  2281. }
  2282. uint32_t TValNo =
  2283. VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this);
  2284. if (Value *V = findLeader(Pred, TValNo)) {
  2285. Instr->setOperand(i, V);
  2286. } else {
  2287. success = false;
  2288. break;
  2289. }
  2290. }
  2291. // Fail out if we encounter an operand that is not available in
  2292. // the PRE predecessor. This is typically because of loads which
  2293. // are not value numbered precisely.
  2294. if (!success)
  2295. return false;
  2296. Instr->insertBefore(Pred->getTerminator());
  2297. Instr->setName(Instr->getName() + ".pre");
  2298. Instr->setDebugLoc(Instr->getDebugLoc());
  2299. ICF->insertInstructionTo(Instr, Pred);
  2300. unsigned Num = VN.lookupOrAdd(Instr);
  2301. VN.add(Instr, Num);
  2302. // Update the availability map to include the new instruction.
  2303. addToLeaderTable(Num, Instr, Pred);
  2304. return true;
  2305. }
  2306. bool GVNPass::performScalarPRE(Instruction *CurInst) {
  2307. if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() ||
  2308. isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
  2309. CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
  2310. isa<DbgInfoIntrinsic>(CurInst))
  2311. return false;
  2312. // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
  2313. // sinking the compare again, and it would force the code generator to
  2314. // move the i1 from processor flags or predicate registers into a general
  2315. // purpose register.
  2316. if (isa<CmpInst>(CurInst))
  2317. return false;
  2318. // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from
  2319. // sinking the addressing mode computation back to its uses. Extending the
  2320. // GEP's live range increases the register pressure, and therefore it can
  2321. // introduce unnecessary spills.
  2322. //
  2323. // This doesn't prevent Load PRE. PHI translation will make the GEP available
  2324. // to the load by moving it to the predecessor block if necessary.
  2325. if (isa<GetElementPtrInst>(CurInst))
  2326. return false;
  2327. if (auto *CallB = dyn_cast<CallBase>(CurInst)) {
  2328. // We don't currently value number ANY inline asm calls.
  2329. if (CallB->isInlineAsm())
  2330. return false;
  2331. // Don't do PRE on convergent calls.
  2332. if (CallB->isConvergent())
  2333. return false;
  2334. }
  2335. uint32_t ValNo = VN.lookup(CurInst);
  2336. // Look for the predecessors for PRE opportunities. We're
  2337. // only trying to solve the basic diamond case, where
  2338. // a value is computed in the successor and one predecessor,
  2339. // but not the other. We also explicitly disallow cases
  2340. // where the successor is its own predecessor, because they're
  2341. // more complicated to get right.
  2342. unsigned NumWith = 0;
  2343. unsigned NumWithout = 0;
  2344. BasicBlock *PREPred = nullptr;
  2345. BasicBlock *CurrentBlock = CurInst->getParent();
  2346. // Update the RPO numbers for this function.
  2347. if (InvalidBlockRPONumbers)
  2348. assignBlockRPONumber(*CurrentBlock->getParent());
  2349. SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap;
  2350. for (BasicBlock *P : predecessors(CurrentBlock)) {
  2351. // We're not interested in PRE where blocks with predecessors that are
  2352. // not reachable.
  2353. if (!DT->isReachableFromEntry(P)) {
  2354. NumWithout = 2;
  2355. break;
  2356. }
  2357. // It is not safe to do PRE when P->CurrentBlock is a loop backedge, and
  2358. // when CurInst has operand defined in CurrentBlock (so it may be defined
  2359. // by phi in the loop header).
  2360. assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) &&
  2361. "Invalid BlockRPONumber map.");
  2362. if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock] &&
  2363. llvm::any_of(CurInst->operands(), [&](const Use &U) {
  2364. if (auto *Inst = dyn_cast<Instruction>(U.get()))
  2365. return Inst->getParent() == CurrentBlock;
  2366. return false;
  2367. })) {
  2368. NumWithout = 2;
  2369. break;
  2370. }
  2371. uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this);
  2372. Value *predV = findLeader(P, TValNo);
  2373. if (!predV) {
  2374. predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
  2375. PREPred = P;
  2376. ++NumWithout;
  2377. } else if (predV == CurInst) {
  2378. /* CurInst dominates this predecessor. */
  2379. NumWithout = 2;
  2380. break;
  2381. } else {
  2382. predMap.push_back(std::make_pair(predV, P));
  2383. ++NumWith;
  2384. }
  2385. }
  2386. // Don't do PRE when it might increase code size, i.e. when
  2387. // we would need to insert instructions in more than one pred.
  2388. if (NumWithout > 1 || NumWith == 0)
  2389. return false;
  2390. // We may have a case where all predecessors have the instruction,
  2391. // and we just need to insert a phi node. Otherwise, perform
  2392. // insertion.
  2393. Instruction *PREInstr = nullptr;
  2394. if (NumWithout != 0) {
  2395. if (!isSafeToSpeculativelyExecute(CurInst)) {
  2396. // It is only valid to insert a new instruction if the current instruction
  2397. // is always executed. An instruction with implicit control flow could
  2398. // prevent us from doing it. If we cannot speculate the execution, then
  2399. // PRE should be prohibited.
  2400. if (ICF->isDominatedByICFIFromSameBlock(CurInst))
  2401. return false;
  2402. }
  2403. // Don't do PRE across indirect branch.
  2404. if (isa<IndirectBrInst>(PREPred->getTerminator()))
  2405. return false;
  2406. // Don't do PRE across callbr.
  2407. // FIXME: Can we do this across the fallthrough edge?
  2408. if (isa<CallBrInst>(PREPred->getTerminator()))
  2409. return false;
  2410. // We can't do PRE safely on a critical edge, so instead we schedule
  2411. // the edge to be split and perform the PRE the next time we iterate
  2412. // on the function.
  2413. unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
  2414. if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
  2415. toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
  2416. return false;
  2417. }
  2418. // We need to insert somewhere, so let's give it a shot
  2419. PREInstr = CurInst->clone();
  2420. if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) {
  2421. // If we failed insertion, make sure we remove the instruction.
  2422. LLVM_DEBUG(verifyRemoved(PREInstr));
  2423. PREInstr->deleteValue();
  2424. return false;
  2425. }
  2426. }
  2427. // Either we should have filled in the PRE instruction, or we should
  2428. // not have needed insertions.
  2429. assert(PREInstr != nullptr || NumWithout == 0);
  2430. ++NumGVNPRE;
  2431. // Create a PHI to make the value available in this block.
  2432. PHINode *Phi =
  2433. PHINode::Create(CurInst->getType(), predMap.size(),
  2434. CurInst->getName() + ".pre-phi", &CurrentBlock->front());
  2435. for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
  2436. if (Value *V = predMap[i].first) {
  2437. // If we use an existing value in this phi, we have to patch the original
  2438. // value because the phi will be used to replace a later value.
  2439. patchReplacementInstruction(CurInst, V);
  2440. Phi->addIncoming(V, predMap[i].second);
  2441. } else
  2442. Phi->addIncoming(PREInstr, PREPred);
  2443. }
  2444. VN.add(Phi, ValNo);
  2445. // After creating a new PHI for ValNo, the phi translate result for ValNo will
  2446. // be changed, so erase the related stale entries in phi translate cache.
  2447. VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock);
  2448. addToLeaderTable(ValNo, Phi, CurrentBlock);
  2449. Phi->setDebugLoc(CurInst->getDebugLoc());
  2450. CurInst->replaceAllUsesWith(Phi);
  2451. if (MD && Phi->getType()->isPtrOrPtrVectorTy())
  2452. MD->invalidateCachedPointerInfo(Phi);
  2453. VN.erase(CurInst);
  2454. removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
  2455. LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
  2456. if (MD)
  2457. MD->removeInstruction(CurInst);
  2458. if (MSSAU)
  2459. MSSAU->removeMemoryAccess(CurInst);
  2460. LLVM_DEBUG(verifyRemoved(CurInst));
  2461. // FIXME: Intended to be markInstructionForDeletion(CurInst), but it causes
  2462. // some assertion failures.
  2463. ICF->removeInstruction(CurInst);
  2464. CurInst->eraseFromParent();
  2465. ++NumGVNInstr;
  2466. return true;
  2467. }
  2468. /// Perform a purely local form of PRE that looks for diamond
  2469. /// control flow patterns and attempts to perform simple PRE at the join point.
  2470. bool GVNPass::performPRE(Function &F) {
  2471. bool Changed = false;
  2472. for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
  2473. // Nothing to PRE in the entry block.
  2474. if (CurrentBlock == &F.getEntryBlock())
  2475. continue;
  2476. // Don't perform PRE on an EH pad.
  2477. if (CurrentBlock->isEHPad())
  2478. continue;
  2479. for (BasicBlock::iterator BI = CurrentBlock->begin(),
  2480. BE = CurrentBlock->end();
  2481. BI != BE;) {
  2482. Instruction *CurInst = &*BI++;
  2483. Changed |= performScalarPRE(CurInst);
  2484. }
  2485. }
  2486. if (splitCriticalEdges())
  2487. Changed = true;
  2488. return Changed;
  2489. }
  2490. /// Split the critical edge connecting the given two blocks, and return
  2491. /// the block inserted to the critical edge.
  2492. BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
  2493. // GVN does not require loop-simplify, do not try to preserve it if it is not
  2494. // possible.
  2495. BasicBlock *BB = SplitCriticalEdge(
  2496. Pred, Succ,
  2497. CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify());
  2498. if (BB) {
  2499. if (MD)
  2500. MD->invalidateCachedPredecessors();
  2501. InvalidBlockRPONumbers = true;
  2502. }
  2503. return BB;
  2504. }
  2505. /// Split critical edges found during the previous
  2506. /// iteration that may enable further optimization.
  2507. bool GVNPass::splitCriticalEdges() {
  2508. if (toSplit.empty())
  2509. return false;
  2510. bool Changed = false;
  2511. do {
  2512. std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val();
  2513. Changed |= SplitCriticalEdge(Edge.first, Edge.second,
  2514. CriticalEdgeSplittingOptions(DT, LI, MSSAU)) !=
  2515. nullptr;
  2516. } while (!toSplit.empty());
  2517. if (Changed) {
  2518. if (MD)
  2519. MD->invalidateCachedPredecessors();
  2520. InvalidBlockRPONumbers = true;
  2521. }
  2522. return Changed;
  2523. }
  2524. /// Executes one iteration of GVN
  2525. bool GVNPass::iterateOnFunction(Function &F) {
  2526. cleanupGlobalSets();
  2527. // Top-down walk of the dominator tree
  2528. bool Changed = false;
  2529. // Needed for value numbering with phi construction to work.
  2530. // RPOT walks the graph in its constructor and will not be invalidated during
  2531. // processBlock.
  2532. ReversePostOrderTraversal<Function *> RPOT(&F);
  2533. for (BasicBlock *BB : RPOT)
  2534. Changed |= processBlock(BB);
  2535. return Changed;
  2536. }
  2537. void GVNPass::cleanupGlobalSets() {
  2538. VN.clear();
  2539. LeaderTable.clear();
  2540. BlockRPONumber.clear();
  2541. TableAllocator.Reset();
  2542. ICF->clear();
  2543. InvalidBlockRPONumbers = true;
  2544. }
  2545. /// Verify that the specified instruction does not occur in our
  2546. /// internal data structures.
  2547. void GVNPass::verifyRemoved(const Instruction *Inst) const {
  2548. VN.verifyRemoved(Inst);
  2549. // Walk through the value number scope to make sure the instruction isn't
  2550. // ferreted away in it.
  2551. for (const auto &I : LeaderTable) {
  2552. const LeaderTableEntry *Node = &I.second;
  2553. assert(Node->Val != Inst && "Inst still in value numbering scope!");
  2554. while (Node->Next) {
  2555. Node = Node->Next;
  2556. assert(Node->Val != Inst && "Inst still in value numbering scope!");
  2557. }
  2558. }
  2559. }
  2560. /// BB is declared dead, which implied other blocks become dead as well. This
  2561. /// function is to add all these blocks to "DeadBlocks". For the dead blocks'
  2562. /// live successors, update their phi nodes by replacing the operands
  2563. /// corresponding to dead blocks with UndefVal.
  2564. void GVNPass::addDeadBlock(BasicBlock *BB) {
  2565. SmallVector<BasicBlock *, 4> NewDead;
  2566. SmallSetVector<BasicBlock *, 4> DF;
  2567. NewDead.push_back(BB);
  2568. while (!NewDead.empty()) {
  2569. BasicBlock *D = NewDead.pop_back_val();
  2570. if (DeadBlocks.count(D))
  2571. continue;
  2572. // All blocks dominated by D are dead.
  2573. SmallVector<BasicBlock *, 8> Dom;
  2574. DT->getDescendants(D, Dom);
  2575. DeadBlocks.insert(Dom.begin(), Dom.end());
  2576. // Figure out the dominance-frontier(D).
  2577. for (BasicBlock *B : Dom) {
  2578. for (BasicBlock *S : successors(B)) {
  2579. if (DeadBlocks.count(S))
  2580. continue;
  2581. bool AllPredDead = true;
  2582. for (BasicBlock *P : predecessors(S))
  2583. if (!DeadBlocks.count(P)) {
  2584. AllPredDead = false;
  2585. break;
  2586. }
  2587. if (!AllPredDead) {
  2588. // S could be proved dead later on. That is why we don't update phi
  2589. // operands at this moment.
  2590. DF.insert(S);
  2591. } else {
  2592. // While S is not dominated by D, it is dead by now. This could take
  2593. // place if S already have a dead predecessor before D is declared
  2594. // dead.
  2595. NewDead.push_back(S);
  2596. }
  2597. }
  2598. }
  2599. }
  2600. // For the dead blocks' live successors, update their phi nodes by replacing
  2601. // the operands corresponding to dead blocks with UndefVal.
  2602. for (BasicBlock *B : DF) {
  2603. if (DeadBlocks.count(B))
  2604. continue;
  2605. // First, split the critical edges. This might also create additional blocks
  2606. // to preserve LoopSimplify form and adjust edges accordingly.
  2607. SmallVector<BasicBlock *, 4> Preds(predecessors(B));
  2608. for (BasicBlock *P : Preds) {
  2609. if (!DeadBlocks.count(P))
  2610. continue;
  2611. if (llvm::is_contained(successors(P), B) &&
  2612. isCriticalEdge(P->getTerminator(), B)) {
  2613. if (BasicBlock *S = splitCriticalEdges(P, B))
  2614. DeadBlocks.insert(P = S);
  2615. }
  2616. }
  2617. // Now poison the incoming values from the dead predecessors.
  2618. for (BasicBlock *P : predecessors(B)) {
  2619. if (!DeadBlocks.count(P))
  2620. continue;
  2621. for (PHINode &Phi : B->phis()) {
  2622. Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType()));
  2623. if (MD)
  2624. MD->invalidateCachedPointerInfo(&Phi);
  2625. }
  2626. }
  2627. }
  2628. }
  2629. // If the given branch is recognized as a foldable branch (i.e. conditional
  2630. // branch with constant condition), it will perform following analyses and
  2631. // transformation.
  2632. // 1) If the dead out-coming edge is a critical-edge, split it. Let
  2633. // R be the target of the dead out-coming edge.
  2634. // 1) Identify the set of dead blocks implied by the branch's dead outcoming
  2635. // edge. The result of this step will be {X| X is dominated by R}
  2636. // 2) Identify those blocks which haves at least one dead predecessor. The
  2637. // result of this step will be dominance-frontier(R).
  2638. // 3) Update the PHIs in DF(R) by replacing the operands corresponding to
  2639. // dead blocks with "UndefVal" in an hope these PHIs will optimized away.
  2640. //
  2641. // Return true iff *NEW* dead code are found.
  2642. bool GVNPass::processFoldableCondBr(BranchInst *BI) {
  2643. if (!BI || BI->isUnconditional())
  2644. return false;
  2645. // If a branch has two identical successors, we cannot declare either dead.
  2646. if (BI->getSuccessor(0) == BI->getSuccessor(1))
  2647. return false;
  2648. ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
  2649. if (!Cond)
  2650. return false;
  2651. BasicBlock *DeadRoot =
  2652. Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0);
  2653. if (DeadBlocks.count(DeadRoot))
  2654. return false;
  2655. if (!DeadRoot->getSinglePredecessor())
  2656. DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
  2657. addDeadBlock(DeadRoot);
  2658. return true;
  2659. }
  2660. // performPRE() will trigger assert if it comes across an instruction without
  2661. // associated val-num. As it normally has far more live instructions than dead
  2662. // instructions, it makes more sense just to "fabricate" a val-number for the
  2663. // dead code than checking if instruction involved is dead or not.
  2664. void GVNPass::assignValNumForDeadCode() {
  2665. for (BasicBlock *BB : DeadBlocks) {
  2666. for (Instruction &Inst : *BB) {
  2667. unsigned ValNum = VN.lookupOrAdd(&Inst);
  2668. addToLeaderTable(ValNum, &Inst, BB);
  2669. }
  2670. }
  2671. }
  2672. class llvm::gvn::GVNLegacyPass : public FunctionPass {
  2673. public:
  2674. static char ID; // Pass identification, replacement for typeid
  2675. explicit GVNLegacyPass(bool NoMemDepAnalysis = !GVNEnableMemDep)
  2676. : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) {
  2677. initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry());
  2678. }
  2679. bool runOnFunction(Function &F) override {
  2680. if (skipFunction(F))
  2681. return false;
  2682. auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
  2683. auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
  2684. return Impl.runImpl(
  2685. F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
  2686. getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
  2687. getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
  2688. getAnalysis<AAResultsWrapperPass>().getAAResults(),
  2689. Impl.isMemDepEnabled()
  2690. ? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
  2691. : nullptr,
  2692. LIWP ? &LIWP->getLoopInfo() : nullptr,
  2693. &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
  2694. MSSAWP ? &MSSAWP->getMSSA() : nullptr);
  2695. }
  2696. void getAnalysisUsage(AnalysisUsage &AU) const override {
  2697. AU.addRequired<AssumptionCacheTracker>();
  2698. AU.addRequired<DominatorTreeWrapperPass>();
  2699. AU.addRequired<TargetLibraryInfoWrapperPass>();
  2700. AU.addRequired<LoopInfoWrapperPass>();
  2701. if (Impl.isMemDepEnabled())
  2702. AU.addRequired<MemoryDependenceWrapperPass>();
  2703. AU.addRequired<AAResultsWrapperPass>();
  2704. AU.addPreserved<DominatorTreeWrapperPass>();
  2705. AU.addPreserved<GlobalsAAWrapperPass>();
  2706. AU.addPreserved<TargetLibraryInfoWrapperPass>();
  2707. AU.addPreserved<LoopInfoWrapperPass>();
  2708. AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
  2709. AU.addPreserved<MemorySSAWrapperPass>();
  2710. }
  2711. private:
  2712. GVNPass Impl;
  2713. };
  2714. char GVNLegacyPass::ID = 0;
  2715. INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
  2716. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  2717. INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
  2718. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  2719. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  2720. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  2721. INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
  2722. INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
  2723. INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false)
  2724. // The public interface to this file...
  2725. FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) {
  2726. return new GVNLegacyPass(NoMemDepAnalysis);
  2727. }