Local.cpp 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518
  1. //===- Local.cpp - Functions to perform local transformations -------------===//
  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 family of functions perform various local transformations to the
  10. // program.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/Local.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/DenseSet.h"
  18. #include "llvm/ADT/Hashing.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/Analysis/AssumeBundleQueries.h"
  25. #include "llvm/Analysis/ConstantFolding.h"
  26. #include "llvm/Analysis/DomTreeUpdater.h"
  27. #include "llvm/Analysis/EHPersonalities.h"
  28. #include "llvm/Analysis/InstructionSimplify.h"
  29. #include "llvm/Analysis/MemoryBuiltins.h"
  30. #include "llvm/Analysis/MemorySSAUpdater.h"
  31. #include "llvm/Analysis/TargetLibraryInfo.h"
  32. #include "llvm/Analysis/ValueTracking.h"
  33. #include "llvm/Analysis/VectorUtils.h"
  34. #include "llvm/BinaryFormat/Dwarf.h"
  35. #include "llvm/IR/Argument.h"
  36. #include "llvm/IR/Attributes.h"
  37. #include "llvm/IR/BasicBlock.h"
  38. #include "llvm/IR/CFG.h"
  39. #include "llvm/IR/Constant.h"
  40. #include "llvm/IR/ConstantRange.h"
  41. #include "llvm/IR/Constants.h"
  42. #include "llvm/IR/DIBuilder.h"
  43. #include "llvm/IR/DataLayout.h"
  44. #include "llvm/IR/DebugInfo.h"
  45. #include "llvm/IR/DebugInfoMetadata.h"
  46. #include "llvm/IR/DebugLoc.h"
  47. #include "llvm/IR/DerivedTypes.h"
  48. #include "llvm/IR/Dominators.h"
  49. #include "llvm/IR/Function.h"
  50. #include "llvm/IR/GetElementPtrTypeIterator.h"
  51. #include "llvm/IR/GlobalObject.h"
  52. #include "llvm/IR/IRBuilder.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/IntrinsicsWebAssembly.h"
  59. #include "llvm/IR/LLVMContext.h"
  60. #include "llvm/IR/MDBuilder.h"
  61. #include "llvm/IR/Metadata.h"
  62. #include "llvm/IR/Module.h"
  63. #include "llvm/IR/PatternMatch.h"
  64. #include "llvm/IR/ProfDataUtils.h"
  65. #include "llvm/IR/Type.h"
  66. #include "llvm/IR/Use.h"
  67. #include "llvm/IR/User.h"
  68. #include "llvm/IR/Value.h"
  69. #include "llvm/IR/ValueHandle.h"
  70. #include "llvm/Support/Casting.h"
  71. #include "llvm/Support/Debug.h"
  72. #include "llvm/Support/ErrorHandling.h"
  73. #include "llvm/Support/KnownBits.h"
  74. #include "llvm/Support/raw_ostream.h"
  75. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  76. #include "llvm/Transforms/Utils/ValueMapper.h"
  77. #include <algorithm>
  78. #include <cassert>
  79. #include <cstdint>
  80. #include <iterator>
  81. #include <map>
  82. #include <optional>
  83. #include <utility>
  84. using namespace llvm;
  85. using namespace llvm::PatternMatch;
  86. #define DEBUG_TYPE "local"
  87. STATISTIC(NumRemoved, "Number of unreachable basic blocks removed");
  88. STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd");
  89. static cl::opt<bool> PHICSEDebugHash(
  90. "phicse-debug-hash",
  91. #ifdef EXPENSIVE_CHECKS
  92. cl::init(true),
  93. #else
  94. cl::init(false),
  95. #endif
  96. cl::Hidden,
  97. cl::desc("Perform extra assertion checking to verify that PHINodes's hash "
  98. "function is well-behaved w.r.t. its isEqual predicate"));
  99. static cl::opt<unsigned> PHICSENumPHISmallSize(
  100. "phicse-num-phi-smallsize", cl::init(32), cl::Hidden,
  101. cl::desc(
  102. "When the basic block contains not more than this number of PHI nodes, "
  103. "perform a (faster!) exhaustive search instead of set-driven one."));
  104. // Max recursion depth for collectBitParts used when detecting bswap and
  105. // bitreverse idioms.
  106. static const unsigned BitPartRecursionMaxDepth = 48;
  107. //===----------------------------------------------------------------------===//
  108. // Local constant propagation.
  109. //
  110. /// ConstantFoldTerminator - If a terminator instruction is predicated on a
  111. /// constant value, convert it into an unconditional branch to the constant
  112. /// destination. This is a nontrivial operation because the successors of this
  113. /// basic block must have their PHI nodes updated.
  114. /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
  115. /// conditions and indirectbr addresses this might make dead if
  116. /// DeleteDeadConditions is true.
  117. bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
  118. const TargetLibraryInfo *TLI,
  119. DomTreeUpdater *DTU) {
  120. Instruction *T = BB->getTerminator();
  121. IRBuilder<> Builder(T);
  122. // Branch - See if we are conditional jumping on constant
  123. if (auto *BI = dyn_cast<BranchInst>(T)) {
  124. if (BI->isUnconditional()) return false; // Can't optimize uncond branch
  125. BasicBlock *Dest1 = BI->getSuccessor(0);
  126. BasicBlock *Dest2 = BI->getSuccessor(1);
  127. if (Dest2 == Dest1) { // Conditional branch to same location?
  128. // This branch matches something like this:
  129. // br bool %cond, label %Dest, label %Dest
  130. // and changes it into: br label %Dest
  131. // Let the basic block know that we are letting go of one copy of it.
  132. assert(BI->getParent() && "Terminator not inserted in block!");
  133. Dest1->removePredecessor(BI->getParent());
  134. // Replace the conditional branch with an unconditional one.
  135. BranchInst *NewBI = Builder.CreateBr(Dest1);
  136. // Transfer the metadata to the new branch instruction.
  137. NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,
  138. LLVMContext::MD_annotation});
  139. Value *Cond = BI->getCondition();
  140. BI->eraseFromParent();
  141. if (DeleteDeadConditions)
  142. RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
  143. return true;
  144. }
  145. if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
  146. // Are we branching on constant?
  147. // YES. Change to unconditional branch...
  148. BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
  149. BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
  150. // Let the basic block know that we are letting go of it. Based on this,
  151. // it will adjust it's PHI nodes.
  152. OldDest->removePredecessor(BB);
  153. // Replace the conditional branch with an unconditional one.
  154. BranchInst *NewBI = Builder.CreateBr(Destination);
  155. // Transfer the metadata to the new branch instruction.
  156. NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,
  157. LLVMContext::MD_annotation});
  158. BI->eraseFromParent();
  159. if (DTU)
  160. DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}});
  161. return true;
  162. }
  163. return false;
  164. }
  165. if (auto *SI = dyn_cast<SwitchInst>(T)) {
  166. // If we are switching on a constant, we can convert the switch to an
  167. // unconditional branch.
  168. auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
  169. BasicBlock *DefaultDest = SI->getDefaultDest();
  170. BasicBlock *TheOnlyDest = DefaultDest;
  171. // If the default is unreachable, ignore it when searching for TheOnlyDest.
  172. if (isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()) &&
  173. SI->getNumCases() > 0) {
  174. TheOnlyDest = SI->case_begin()->getCaseSuccessor();
  175. }
  176. bool Changed = false;
  177. // Figure out which case it goes to.
  178. for (auto i = SI->case_begin(), e = SI->case_end(); i != e;) {
  179. // Found case matching a constant operand?
  180. if (i->getCaseValue() == CI) {
  181. TheOnlyDest = i->getCaseSuccessor();
  182. break;
  183. }
  184. // Check to see if this branch is going to the same place as the default
  185. // dest. If so, eliminate it as an explicit compare.
  186. if (i->getCaseSuccessor() == DefaultDest) {
  187. MDNode *MD = getValidBranchWeightMDNode(*SI);
  188. unsigned NCases = SI->getNumCases();
  189. // Fold the case metadata into the default if there will be any branches
  190. // left, unless the metadata doesn't match the switch.
  191. if (NCases > 1 && MD) {
  192. // Collect branch weights into a vector.
  193. SmallVector<uint32_t, 8> Weights;
  194. extractBranchWeights(MD, Weights);
  195. // Merge weight of this case to the default weight.
  196. unsigned idx = i->getCaseIndex();
  197. // TODO: Add overflow check.
  198. Weights[0] += Weights[idx+1];
  199. // Remove weight for this case.
  200. std::swap(Weights[idx+1], Weights.back());
  201. Weights.pop_back();
  202. SI->setMetadata(LLVMContext::MD_prof,
  203. MDBuilder(BB->getContext()).
  204. createBranchWeights(Weights));
  205. }
  206. // Remove this entry.
  207. BasicBlock *ParentBB = SI->getParent();
  208. DefaultDest->removePredecessor(ParentBB);
  209. i = SI->removeCase(i);
  210. e = SI->case_end();
  211. // Removing this case may have made the condition constant. In that
  212. // case, update CI and restart iteration through the cases.
  213. if (auto *NewCI = dyn_cast<ConstantInt>(SI->getCondition())) {
  214. CI = NewCI;
  215. i = SI->case_begin();
  216. }
  217. Changed = true;
  218. continue;
  219. }
  220. // Otherwise, check to see if the switch only branches to one destination.
  221. // We do this by reseting "TheOnlyDest" to null when we find two non-equal
  222. // destinations.
  223. if (i->getCaseSuccessor() != TheOnlyDest)
  224. TheOnlyDest = nullptr;
  225. // Increment this iterator as we haven't removed the case.
  226. ++i;
  227. }
  228. if (CI && !TheOnlyDest) {
  229. // Branching on a constant, but not any of the cases, go to the default
  230. // successor.
  231. TheOnlyDest = SI->getDefaultDest();
  232. }
  233. // If we found a single destination that we can fold the switch into, do so
  234. // now.
  235. if (TheOnlyDest) {
  236. // Insert the new branch.
  237. Builder.CreateBr(TheOnlyDest);
  238. BasicBlock *BB = SI->getParent();
  239. SmallSet<BasicBlock *, 8> RemovedSuccessors;
  240. // Remove entries from PHI nodes which we no longer branch to...
  241. BasicBlock *SuccToKeep = TheOnlyDest;
  242. for (BasicBlock *Succ : successors(SI)) {
  243. if (DTU && Succ != TheOnlyDest)
  244. RemovedSuccessors.insert(Succ);
  245. // Found case matching a constant operand?
  246. if (Succ == SuccToKeep) {
  247. SuccToKeep = nullptr; // Don't modify the first branch to TheOnlyDest
  248. } else {
  249. Succ->removePredecessor(BB);
  250. }
  251. }
  252. // Delete the old switch.
  253. Value *Cond = SI->getCondition();
  254. SI->eraseFromParent();
  255. if (DeleteDeadConditions)
  256. RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
  257. if (DTU) {
  258. std::vector<DominatorTree::UpdateType> Updates;
  259. Updates.reserve(RemovedSuccessors.size());
  260. for (auto *RemovedSuccessor : RemovedSuccessors)
  261. Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
  262. DTU->applyUpdates(Updates);
  263. }
  264. return true;
  265. }
  266. if (SI->getNumCases() == 1) {
  267. // Otherwise, we can fold this switch into a conditional branch
  268. // instruction if it has only one non-default destination.
  269. auto FirstCase = *SI->case_begin();
  270. Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
  271. FirstCase.getCaseValue(), "cond");
  272. // Insert the new branch.
  273. BranchInst *NewBr = Builder.CreateCondBr(Cond,
  274. FirstCase.getCaseSuccessor(),
  275. SI->getDefaultDest());
  276. SmallVector<uint32_t> Weights;
  277. if (extractBranchWeights(*SI, Weights) && Weights.size() == 2) {
  278. uint32_t DefWeight = Weights[0];
  279. uint32_t CaseWeight = Weights[1];
  280. // The TrueWeight should be the weight for the single case of SI.
  281. NewBr->setMetadata(LLVMContext::MD_prof,
  282. MDBuilder(BB->getContext())
  283. .createBranchWeights(CaseWeight, DefWeight));
  284. }
  285. // Update make.implicit metadata to the newly-created conditional branch.
  286. MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit);
  287. if (MakeImplicitMD)
  288. NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD);
  289. // Delete the old switch.
  290. SI->eraseFromParent();
  291. return true;
  292. }
  293. return Changed;
  294. }
  295. if (auto *IBI = dyn_cast<IndirectBrInst>(T)) {
  296. // indirectbr blockaddress(@F, @BB) -> br label @BB
  297. if (auto *BA =
  298. dyn_cast<BlockAddress>(IBI->getAddress()->stripPointerCasts())) {
  299. BasicBlock *TheOnlyDest = BA->getBasicBlock();
  300. SmallSet<BasicBlock *, 8> RemovedSuccessors;
  301. // Insert the new branch.
  302. Builder.CreateBr(TheOnlyDest);
  303. BasicBlock *SuccToKeep = TheOnlyDest;
  304. for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
  305. BasicBlock *DestBB = IBI->getDestination(i);
  306. if (DTU && DestBB != TheOnlyDest)
  307. RemovedSuccessors.insert(DestBB);
  308. if (IBI->getDestination(i) == SuccToKeep) {
  309. SuccToKeep = nullptr;
  310. } else {
  311. DestBB->removePredecessor(BB);
  312. }
  313. }
  314. Value *Address = IBI->getAddress();
  315. IBI->eraseFromParent();
  316. if (DeleteDeadConditions)
  317. // Delete pointer cast instructions.
  318. RecursivelyDeleteTriviallyDeadInstructions(Address, TLI);
  319. // Also zap the blockaddress constant if there are no users remaining,
  320. // otherwise the destination is still marked as having its address taken.
  321. if (BA->use_empty())
  322. BA->destroyConstant();
  323. // If we didn't find our destination in the IBI successor list, then we
  324. // have undefined behavior. Replace the unconditional branch with an
  325. // 'unreachable' instruction.
  326. if (SuccToKeep) {
  327. BB->getTerminator()->eraseFromParent();
  328. new UnreachableInst(BB->getContext(), BB);
  329. }
  330. if (DTU) {
  331. std::vector<DominatorTree::UpdateType> Updates;
  332. Updates.reserve(RemovedSuccessors.size());
  333. for (auto *RemovedSuccessor : RemovedSuccessors)
  334. Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});
  335. DTU->applyUpdates(Updates);
  336. }
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. //===----------------------------------------------------------------------===//
  343. // Local dead code elimination.
  344. //
  345. /// isInstructionTriviallyDead - Return true if the result produced by the
  346. /// instruction is not used, and the instruction has no side effects.
  347. ///
  348. bool llvm::isInstructionTriviallyDead(Instruction *I,
  349. const TargetLibraryInfo *TLI) {
  350. if (!I->use_empty())
  351. return false;
  352. return wouldInstructionBeTriviallyDead(I, TLI);
  353. }
  354. bool llvm::wouldInstructionBeTriviallyDeadOnUnusedPaths(
  355. Instruction *I, const TargetLibraryInfo *TLI) {
  356. // Instructions that are "markers" and have implied meaning on code around
  357. // them (without explicit uses), are not dead on unused paths.
  358. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
  359. if (II->getIntrinsicID() == Intrinsic::stacksave ||
  360. II->getIntrinsicID() == Intrinsic::launder_invariant_group ||
  361. II->isLifetimeStartOrEnd())
  362. return false;
  363. return wouldInstructionBeTriviallyDead(I, TLI);
  364. }
  365. bool llvm::wouldInstructionBeTriviallyDead(Instruction *I,
  366. const TargetLibraryInfo *TLI) {
  367. if (I->isTerminator())
  368. return false;
  369. // We don't want the landingpad-like instructions removed by anything this
  370. // general.
  371. if (I->isEHPad())
  372. return false;
  373. // We don't want debug info removed by anything this general, unless
  374. // debug info is empty.
  375. if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
  376. if (DDI->getAddress())
  377. return false;
  378. return true;
  379. }
  380. if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
  381. if (DVI->hasArgList() || DVI->getValue(0))
  382. return false;
  383. return true;
  384. }
  385. if (DbgLabelInst *DLI = dyn_cast<DbgLabelInst>(I)) {
  386. if (DLI->getLabel())
  387. return false;
  388. return true;
  389. }
  390. if (auto *CB = dyn_cast<CallBase>(I))
  391. if (isRemovableAlloc(CB, TLI))
  392. return true;
  393. if (!I->willReturn()) {
  394. auto *II = dyn_cast<IntrinsicInst>(I);
  395. if (!II)
  396. return false;
  397. // TODO: These intrinsics are not safe to remove, because this may remove
  398. // a well-defined trap.
  399. switch (II->getIntrinsicID()) {
  400. case Intrinsic::wasm_trunc_signed:
  401. case Intrinsic::wasm_trunc_unsigned:
  402. case Intrinsic::ptrauth_auth:
  403. case Intrinsic::ptrauth_resign:
  404. return true;
  405. default:
  406. return false;
  407. }
  408. }
  409. if (!I->mayHaveSideEffects())
  410. return true;
  411. // Special case intrinsics that "may have side effects" but can be deleted
  412. // when dead.
  413. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  414. // Safe to delete llvm.stacksave and launder.invariant.group if dead.
  415. if (II->getIntrinsicID() == Intrinsic::stacksave ||
  416. II->getIntrinsicID() == Intrinsic::launder_invariant_group)
  417. return true;
  418. if (II->isLifetimeStartOrEnd()) {
  419. auto *Arg = II->getArgOperand(1);
  420. // Lifetime intrinsics are dead when their right-hand is undef.
  421. if (isa<UndefValue>(Arg))
  422. return true;
  423. // If the right-hand is an alloc, global, or argument and the only uses
  424. // are lifetime intrinsics then the intrinsics are dead.
  425. if (isa<AllocaInst>(Arg) || isa<GlobalValue>(Arg) || isa<Argument>(Arg))
  426. return llvm::all_of(Arg->uses(), [](Use &Use) {
  427. if (IntrinsicInst *IntrinsicUse =
  428. dyn_cast<IntrinsicInst>(Use.getUser()))
  429. return IntrinsicUse->isLifetimeStartOrEnd();
  430. return false;
  431. });
  432. return false;
  433. }
  434. // Assumptions are dead if their condition is trivially true. Guards on
  435. // true are operationally no-ops. In the future we can consider more
  436. // sophisticated tradeoffs for guards considering potential for check
  437. // widening, but for now we keep things simple.
  438. if ((II->getIntrinsicID() == Intrinsic::assume &&
  439. isAssumeWithEmptyBundle(cast<AssumeInst>(*II))) ||
  440. II->getIntrinsicID() == Intrinsic::experimental_guard) {
  441. if (ConstantInt *Cond = dyn_cast<ConstantInt>(II->getArgOperand(0)))
  442. return !Cond->isZero();
  443. return false;
  444. }
  445. if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I)) {
  446. std::optional<fp::ExceptionBehavior> ExBehavior =
  447. FPI->getExceptionBehavior();
  448. return *ExBehavior != fp::ebStrict;
  449. }
  450. }
  451. if (auto *Call = dyn_cast<CallBase>(I)) {
  452. if (Value *FreedOp = getFreedOperand(Call, TLI))
  453. if (Constant *C = dyn_cast<Constant>(FreedOp))
  454. return C->isNullValue() || isa<UndefValue>(C);
  455. if (isMathLibCallNoop(Call, TLI))
  456. return true;
  457. }
  458. // Non-volatile atomic loads from constants can be removed.
  459. if (auto *LI = dyn_cast<LoadInst>(I))
  460. if (auto *GV = dyn_cast<GlobalVariable>(
  461. LI->getPointerOperand()->stripPointerCasts()))
  462. if (!LI->isVolatile() && GV->isConstant())
  463. return true;
  464. return false;
  465. }
  466. /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
  467. /// trivially dead instruction, delete it. If that makes any of its operands
  468. /// trivially dead, delete them too, recursively. Return true if any
  469. /// instructions were deleted.
  470. bool llvm::RecursivelyDeleteTriviallyDeadInstructions(
  471. Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU,
  472. std::function<void(Value *)> AboutToDeleteCallback) {
  473. Instruction *I = dyn_cast<Instruction>(V);
  474. if (!I || !isInstructionTriviallyDead(I, TLI))
  475. return false;
  476. SmallVector<WeakTrackingVH, 16> DeadInsts;
  477. DeadInsts.push_back(I);
  478. RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU,
  479. AboutToDeleteCallback);
  480. return true;
  481. }
  482. bool llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive(
  483. SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
  484. MemorySSAUpdater *MSSAU,
  485. std::function<void(Value *)> AboutToDeleteCallback) {
  486. unsigned S = 0, E = DeadInsts.size(), Alive = 0;
  487. for (; S != E; ++S) {
  488. auto *I = dyn_cast<Instruction>(DeadInsts[S]);
  489. if (!I || !isInstructionTriviallyDead(I)) {
  490. DeadInsts[S] = nullptr;
  491. ++Alive;
  492. }
  493. }
  494. if (Alive == E)
  495. return false;
  496. RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU,
  497. AboutToDeleteCallback);
  498. return true;
  499. }
  500. void llvm::RecursivelyDeleteTriviallyDeadInstructions(
  501. SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
  502. MemorySSAUpdater *MSSAU,
  503. std::function<void(Value *)> AboutToDeleteCallback) {
  504. // Process the dead instruction list until empty.
  505. while (!DeadInsts.empty()) {
  506. Value *V = DeadInsts.pop_back_val();
  507. Instruction *I = cast_or_null<Instruction>(V);
  508. if (!I)
  509. continue;
  510. assert(isInstructionTriviallyDead(I, TLI) &&
  511. "Live instruction found in dead worklist!");
  512. assert(I->use_empty() && "Instructions with uses are not dead.");
  513. // Don't lose the debug info while deleting the instructions.
  514. salvageDebugInfo(*I);
  515. if (AboutToDeleteCallback)
  516. AboutToDeleteCallback(I);
  517. // Null out all of the instruction's operands to see if any operand becomes
  518. // dead as we go.
  519. for (Use &OpU : I->operands()) {
  520. Value *OpV = OpU.get();
  521. OpU.set(nullptr);
  522. if (!OpV->use_empty())
  523. continue;
  524. // If the operand is an instruction that became dead as we nulled out the
  525. // operand, and if it is 'trivially' dead, delete it in a future loop
  526. // iteration.
  527. if (Instruction *OpI = dyn_cast<Instruction>(OpV))
  528. if (isInstructionTriviallyDead(OpI, TLI))
  529. DeadInsts.push_back(OpI);
  530. }
  531. if (MSSAU)
  532. MSSAU->removeMemoryAccess(I);
  533. I->eraseFromParent();
  534. }
  535. }
  536. bool llvm::replaceDbgUsesWithUndef(Instruction *I) {
  537. SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
  538. findDbgUsers(DbgUsers, I);
  539. for (auto *DII : DbgUsers)
  540. DII->setKillLocation();
  541. return !DbgUsers.empty();
  542. }
  543. /// areAllUsesEqual - Check whether the uses of a value are all the same.
  544. /// This is similar to Instruction::hasOneUse() except this will also return
  545. /// true when there are no uses or multiple uses that all refer to the same
  546. /// value.
  547. static bool areAllUsesEqual(Instruction *I) {
  548. Value::user_iterator UI = I->user_begin();
  549. Value::user_iterator UE = I->user_end();
  550. if (UI == UE)
  551. return true;
  552. User *TheUse = *UI;
  553. for (++UI; UI != UE; ++UI) {
  554. if (*UI != TheUse)
  555. return false;
  556. }
  557. return true;
  558. }
  559. /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
  560. /// dead PHI node, due to being a def-use chain of single-use nodes that
  561. /// either forms a cycle or is terminated by a trivially dead instruction,
  562. /// delete it. If that makes any of its operands trivially dead, delete them
  563. /// too, recursively. Return true if a change was made.
  564. bool llvm::RecursivelyDeleteDeadPHINode(PHINode *PN,
  565. const TargetLibraryInfo *TLI,
  566. llvm::MemorySSAUpdater *MSSAU) {
  567. SmallPtrSet<Instruction*, 4> Visited;
  568. for (Instruction *I = PN; areAllUsesEqual(I) && !I->mayHaveSideEffects();
  569. I = cast<Instruction>(*I->user_begin())) {
  570. if (I->use_empty())
  571. return RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
  572. // If we find an instruction more than once, we're on a cycle that
  573. // won't prove fruitful.
  574. if (!Visited.insert(I).second) {
  575. // Break the cycle and delete the instruction and its operands.
  576. I->replaceAllUsesWith(PoisonValue::get(I->getType()));
  577. (void)RecursivelyDeleteTriviallyDeadInstructions(I, TLI, MSSAU);
  578. return true;
  579. }
  580. }
  581. return false;
  582. }
  583. static bool
  584. simplifyAndDCEInstruction(Instruction *I,
  585. SmallSetVector<Instruction *, 16> &WorkList,
  586. const DataLayout &DL,
  587. const TargetLibraryInfo *TLI) {
  588. if (isInstructionTriviallyDead(I, TLI)) {
  589. salvageDebugInfo(*I);
  590. // Null out all of the instruction's operands to see if any operand becomes
  591. // dead as we go.
  592. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
  593. Value *OpV = I->getOperand(i);
  594. I->setOperand(i, nullptr);
  595. if (!OpV->use_empty() || I == OpV)
  596. continue;
  597. // If the operand is an instruction that became dead as we nulled out the
  598. // operand, and if it is 'trivially' dead, delete it in a future loop
  599. // iteration.
  600. if (Instruction *OpI = dyn_cast<Instruction>(OpV))
  601. if (isInstructionTriviallyDead(OpI, TLI))
  602. WorkList.insert(OpI);
  603. }
  604. I->eraseFromParent();
  605. return true;
  606. }
  607. if (Value *SimpleV = simplifyInstruction(I, DL)) {
  608. // Add the users to the worklist. CAREFUL: an instruction can use itself,
  609. // in the case of a phi node.
  610. for (User *U : I->users()) {
  611. if (U != I) {
  612. WorkList.insert(cast<Instruction>(U));
  613. }
  614. }
  615. // Replace the instruction with its simplified value.
  616. bool Changed = false;
  617. if (!I->use_empty()) {
  618. I->replaceAllUsesWith(SimpleV);
  619. Changed = true;
  620. }
  621. if (isInstructionTriviallyDead(I, TLI)) {
  622. I->eraseFromParent();
  623. Changed = true;
  624. }
  625. return Changed;
  626. }
  627. return false;
  628. }
  629. /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
  630. /// simplify any instructions in it and recursively delete dead instructions.
  631. ///
  632. /// This returns true if it changed the code, note that it can delete
  633. /// instructions in other blocks as well in this block.
  634. bool llvm::SimplifyInstructionsInBlock(BasicBlock *BB,
  635. const TargetLibraryInfo *TLI) {
  636. bool MadeChange = false;
  637. const DataLayout &DL = BB->getModule()->getDataLayout();
  638. #ifndef NDEBUG
  639. // In debug builds, ensure that the terminator of the block is never replaced
  640. // or deleted by these simplifications. The idea of simplification is that it
  641. // cannot introduce new instructions, and there is no way to replace the
  642. // terminator of a block without introducing a new instruction.
  643. AssertingVH<Instruction> TerminatorVH(&BB->back());
  644. #endif
  645. SmallSetVector<Instruction *, 16> WorkList;
  646. // Iterate over the original function, only adding insts to the worklist
  647. // if they actually need to be revisited. This avoids having to pre-init
  648. // the worklist with the entire function's worth of instructions.
  649. for (BasicBlock::iterator BI = BB->begin(), E = std::prev(BB->end());
  650. BI != E;) {
  651. assert(!BI->isTerminator());
  652. Instruction *I = &*BI;
  653. ++BI;
  654. // We're visiting this instruction now, so make sure it's not in the
  655. // worklist from an earlier visit.
  656. if (!WorkList.count(I))
  657. MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
  658. }
  659. while (!WorkList.empty()) {
  660. Instruction *I = WorkList.pop_back_val();
  661. MadeChange |= simplifyAndDCEInstruction(I, WorkList, DL, TLI);
  662. }
  663. return MadeChange;
  664. }
  665. //===----------------------------------------------------------------------===//
  666. // Control Flow Graph Restructuring.
  667. //
  668. void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB,
  669. DomTreeUpdater *DTU) {
  670. // If BB has single-entry PHI nodes, fold them.
  671. while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
  672. Value *NewVal = PN->getIncomingValue(0);
  673. // Replace self referencing PHI with poison, it must be dead.
  674. if (NewVal == PN) NewVal = PoisonValue::get(PN->getType());
  675. PN->replaceAllUsesWith(NewVal);
  676. PN->eraseFromParent();
  677. }
  678. BasicBlock *PredBB = DestBB->getSinglePredecessor();
  679. assert(PredBB && "Block doesn't have a single predecessor!");
  680. bool ReplaceEntryBB = PredBB->isEntryBlock();
  681. // DTU updates: Collect all the edges that enter
  682. // PredBB. These dominator edges will be redirected to DestBB.
  683. SmallVector<DominatorTree::UpdateType, 32> Updates;
  684. if (DTU) {
  685. // To avoid processing the same predecessor more than once.
  686. SmallPtrSet<BasicBlock *, 2> SeenPreds;
  687. Updates.reserve(Updates.size() + 2 * pred_size(PredBB) + 1);
  688. for (BasicBlock *PredOfPredBB : predecessors(PredBB))
  689. // This predecessor of PredBB may already have DestBB as a successor.
  690. if (PredOfPredBB != PredBB)
  691. if (SeenPreds.insert(PredOfPredBB).second)
  692. Updates.push_back({DominatorTree::Insert, PredOfPredBB, DestBB});
  693. SeenPreds.clear();
  694. for (BasicBlock *PredOfPredBB : predecessors(PredBB))
  695. if (SeenPreds.insert(PredOfPredBB).second)
  696. Updates.push_back({DominatorTree::Delete, PredOfPredBB, PredBB});
  697. Updates.push_back({DominatorTree::Delete, PredBB, DestBB});
  698. }
  699. // Zap anything that took the address of DestBB. Not doing this will give the
  700. // address an invalid value.
  701. if (DestBB->hasAddressTaken()) {
  702. BlockAddress *BA = BlockAddress::get(DestBB);
  703. Constant *Replacement =
  704. ConstantInt::get(Type::getInt32Ty(BA->getContext()), 1);
  705. BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
  706. BA->getType()));
  707. BA->destroyConstant();
  708. }
  709. // Anything that branched to PredBB now branches to DestBB.
  710. PredBB->replaceAllUsesWith(DestBB);
  711. // Splice all the instructions from PredBB to DestBB.
  712. PredBB->getTerminator()->eraseFromParent();
  713. DestBB->splice(DestBB->begin(), PredBB);
  714. new UnreachableInst(PredBB->getContext(), PredBB);
  715. // If the PredBB is the entry block of the function, move DestBB up to
  716. // become the entry block after we erase PredBB.
  717. if (ReplaceEntryBB)
  718. DestBB->moveAfter(PredBB);
  719. if (DTU) {
  720. assert(PredBB->size() == 1 &&
  721. isa<UnreachableInst>(PredBB->getTerminator()) &&
  722. "The successor list of PredBB isn't empty before "
  723. "applying corresponding DTU updates.");
  724. DTU->applyUpdatesPermissive(Updates);
  725. DTU->deleteBB(PredBB);
  726. // Recalculation of DomTree is needed when updating a forward DomTree and
  727. // the Entry BB is replaced.
  728. if (ReplaceEntryBB && DTU->hasDomTree()) {
  729. // The entry block was removed and there is no external interface for
  730. // the dominator tree to be notified of this change. In this corner-case
  731. // we recalculate the entire tree.
  732. DTU->recalculate(*(DestBB->getParent()));
  733. }
  734. }
  735. else {
  736. PredBB->eraseFromParent(); // Nuke BB if DTU is nullptr.
  737. }
  738. }
  739. /// Return true if we can choose one of these values to use in place of the
  740. /// other. Note that we will always choose the non-undef value to keep.
  741. static bool CanMergeValues(Value *First, Value *Second) {
  742. return First == Second || isa<UndefValue>(First) || isa<UndefValue>(Second);
  743. }
  744. /// Return true if we can fold BB, an almost-empty BB ending in an unconditional
  745. /// branch to Succ, into Succ.
  746. ///
  747. /// Assumption: Succ is the single successor for BB.
  748. static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
  749. assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");
  750. LLVM_DEBUG(dbgs() << "Looking to fold " << BB->getName() << " into "
  751. << Succ->getName() << "\n");
  752. // Shortcut, if there is only a single predecessor it must be BB and merging
  753. // is always safe
  754. if (Succ->getSinglePredecessor()) return true;
  755. // Make a list of the predecessors of BB
  756. SmallPtrSet<BasicBlock*, 16> BBPreds(pred_begin(BB), pred_end(BB));
  757. // Look at all the phi nodes in Succ, to see if they present a conflict when
  758. // merging these blocks
  759. for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
  760. PHINode *PN = cast<PHINode>(I);
  761. // If the incoming value from BB is again a PHINode in
  762. // BB which has the same incoming value for *PI as PN does, we can
  763. // merge the phi nodes and then the blocks can still be merged
  764. PHINode *BBPN = dyn_cast<PHINode>(PN->getIncomingValueForBlock(BB));
  765. if (BBPN && BBPN->getParent() == BB) {
  766. for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
  767. BasicBlock *IBB = PN->getIncomingBlock(PI);
  768. if (BBPreds.count(IBB) &&
  769. !CanMergeValues(BBPN->getIncomingValueForBlock(IBB),
  770. PN->getIncomingValue(PI))) {
  771. LLVM_DEBUG(dbgs()
  772. << "Can't fold, phi node " << PN->getName() << " in "
  773. << Succ->getName() << " is conflicting with "
  774. << BBPN->getName() << " with regard to common predecessor "
  775. << IBB->getName() << "\n");
  776. return false;
  777. }
  778. }
  779. } else {
  780. Value* Val = PN->getIncomingValueForBlock(BB);
  781. for (unsigned PI = 0, PE = PN->getNumIncomingValues(); PI != PE; ++PI) {
  782. // See if the incoming value for the common predecessor is equal to the
  783. // one for BB, in which case this phi node will not prevent the merging
  784. // of the block.
  785. BasicBlock *IBB = PN->getIncomingBlock(PI);
  786. if (BBPreds.count(IBB) &&
  787. !CanMergeValues(Val, PN->getIncomingValue(PI))) {
  788. LLVM_DEBUG(dbgs() << "Can't fold, phi node " << PN->getName()
  789. << " in " << Succ->getName()
  790. << " is conflicting with regard to common "
  791. << "predecessor " << IBB->getName() << "\n");
  792. return false;
  793. }
  794. }
  795. }
  796. }
  797. return true;
  798. }
  799. using PredBlockVector = SmallVector<BasicBlock *, 16>;
  800. using IncomingValueMap = DenseMap<BasicBlock *, Value *>;
  801. /// Determines the value to use as the phi node input for a block.
  802. ///
  803. /// Select between \p OldVal any value that we know flows from \p BB
  804. /// to a particular phi on the basis of which one (if either) is not
  805. /// undef. Update IncomingValues based on the selected value.
  806. ///
  807. /// \param OldVal The value we are considering selecting.
  808. /// \param BB The block that the value flows in from.
  809. /// \param IncomingValues A map from block-to-value for other phi inputs
  810. /// that we have examined.
  811. ///
  812. /// \returns the selected value.
  813. static Value *selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB,
  814. IncomingValueMap &IncomingValues) {
  815. if (!isa<UndefValue>(OldVal)) {
  816. assert((!IncomingValues.count(BB) ||
  817. IncomingValues.find(BB)->second == OldVal) &&
  818. "Expected OldVal to match incoming value from BB!");
  819. IncomingValues.insert(std::make_pair(BB, OldVal));
  820. return OldVal;
  821. }
  822. IncomingValueMap::const_iterator It = IncomingValues.find(BB);
  823. if (It != IncomingValues.end()) return It->second;
  824. return OldVal;
  825. }
  826. /// Create a map from block to value for the operands of a
  827. /// given phi.
  828. ///
  829. /// Create a map from block to value for each non-undef value flowing
  830. /// into \p PN.
  831. ///
  832. /// \param PN The phi we are collecting the map for.
  833. /// \param IncomingValues [out] The map from block to value for this phi.
  834. static void gatherIncomingValuesToPhi(PHINode *PN,
  835. IncomingValueMap &IncomingValues) {
  836. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  837. BasicBlock *BB = PN->getIncomingBlock(i);
  838. Value *V = PN->getIncomingValue(i);
  839. if (!isa<UndefValue>(V))
  840. IncomingValues.insert(std::make_pair(BB, V));
  841. }
  842. }
  843. /// Replace the incoming undef values to a phi with the values
  844. /// from a block-to-value map.
  845. ///
  846. /// \param PN The phi we are replacing the undefs in.
  847. /// \param IncomingValues A map from block to value.
  848. static void replaceUndefValuesInPhi(PHINode *PN,
  849. const IncomingValueMap &IncomingValues) {
  850. SmallVector<unsigned> TrueUndefOps;
  851. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  852. Value *V = PN->getIncomingValue(i);
  853. if (!isa<UndefValue>(V)) continue;
  854. BasicBlock *BB = PN->getIncomingBlock(i);
  855. IncomingValueMap::const_iterator It = IncomingValues.find(BB);
  856. // Keep track of undef/poison incoming values. Those must match, so we fix
  857. // them up below if needed.
  858. // Note: this is conservatively correct, but we could try harder and group
  859. // the undef values per incoming basic block.
  860. if (It == IncomingValues.end()) {
  861. TrueUndefOps.push_back(i);
  862. continue;
  863. }
  864. // There is a defined value for this incoming block, so map this undef
  865. // incoming value to the defined value.
  866. PN->setIncomingValue(i, It->second);
  867. }
  868. // If there are both undef and poison values incoming, then convert those
  869. // values to undef. It is invalid to have different values for the same
  870. // incoming block.
  871. unsigned PoisonCount = count_if(TrueUndefOps, [&](unsigned i) {
  872. return isa<PoisonValue>(PN->getIncomingValue(i));
  873. });
  874. if (PoisonCount != 0 && PoisonCount != TrueUndefOps.size()) {
  875. for (unsigned i : TrueUndefOps)
  876. PN->setIncomingValue(i, UndefValue::get(PN->getType()));
  877. }
  878. }
  879. /// Replace a value flowing from a block to a phi with
  880. /// potentially multiple instances of that value flowing from the
  881. /// block's predecessors to the phi.
  882. ///
  883. /// \param BB The block with the value flowing into the phi.
  884. /// \param BBPreds The predecessors of BB.
  885. /// \param PN The phi that we are updating.
  886. static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB,
  887. const PredBlockVector &BBPreds,
  888. PHINode *PN) {
  889. Value *OldVal = PN->removeIncomingValue(BB, false);
  890. assert(OldVal && "No entry in PHI for Pred BB!");
  891. IncomingValueMap IncomingValues;
  892. // We are merging two blocks - BB, and the block containing PN - and
  893. // as a result we need to redirect edges from the predecessors of BB
  894. // to go to the block containing PN, and update PN
  895. // accordingly. Since we allow merging blocks in the case where the
  896. // predecessor and successor blocks both share some predecessors,
  897. // and where some of those common predecessors might have undef
  898. // values flowing into PN, we want to rewrite those values to be
  899. // consistent with the non-undef values.
  900. gatherIncomingValuesToPhi(PN, IncomingValues);
  901. // If this incoming value is one of the PHI nodes in BB, the new entries
  902. // in the PHI node are the entries from the old PHI.
  903. if (isa<PHINode>(OldVal) && cast<PHINode>(OldVal)->getParent() == BB) {
  904. PHINode *OldValPN = cast<PHINode>(OldVal);
  905. for (unsigned i = 0, e = OldValPN->getNumIncomingValues(); i != e; ++i) {
  906. // Note that, since we are merging phi nodes and BB and Succ might
  907. // have common predecessors, we could end up with a phi node with
  908. // identical incoming branches. This will be cleaned up later (and
  909. // will trigger asserts if we try to clean it up now, without also
  910. // simplifying the corresponding conditional branch).
  911. BasicBlock *PredBB = OldValPN->getIncomingBlock(i);
  912. Value *PredVal = OldValPN->getIncomingValue(i);
  913. Value *Selected = selectIncomingValueForBlock(PredVal, PredBB,
  914. IncomingValues);
  915. // And add a new incoming value for this predecessor for the
  916. // newly retargeted branch.
  917. PN->addIncoming(Selected, PredBB);
  918. }
  919. } else {
  920. for (unsigned i = 0, e = BBPreds.size(); i != e; ++i) {
  921. // Update existing incoming values in PN for this
  922. // predecessor of BB.
  923. BasicBlock *PredBB = BBPreds[i];
  924. Value *Selected = selectIncomingValueForBlock(OldVal, PredBB,
  925. IncomingValues);
  926. // And add a new incoming value for this predecessor for the
  927. // newly retargeted branch.
  928. PN->addIncoming(Selected, PredBB);
  929. }
  930. }
  931. replaceUndefValuesInPhi(PN, IncomingValues);
  932. }
  933. bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
  934. DomTreeUpdater *DTU) {
  935. assert(BB != &BB->getParent()->getEntryBlock() &&
  936. "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
  937. // We can't eliminate infinite loops.
  938. BasicBlock *Succ = cast<BranchInst>(BB->getTerminator())->getSuccessor(0);
  939. if (BB == Succ) return false;
  940. // Check to see if merging these blocks would cause conflicts for any of the
  941. // phi nodes in BB or Succ. If not, we can safely merge.
  942. if (!CanPropagatePredecessorsForPHIs(BB, Succ)) return false;
  943. // Check for cases where Succ has multiple predecessors and a PHI node in BB
  944. // has uses which will not disappear when the PHI nodes are merged. It is
  945. // possible to handle such cases, but difficult: it requires checking whether
  946. // BB dominates Succ, which is non-trivial to calculate in the case where
  947. // Succ has multiple predecessors. Also, it requires checking whether
  948. // constructing the necessary self-referential PHI node doesn't introduce any
  949. // conflicts; this isn't too difficult, but the previous code for doing this
  950. // was incorrect.
  951. //
  952. // Note that if this check finds a live use, BB dominates Succ, so BB is
  953. // something like a loop pre-header (or rarely, a part of an irreducible CFG);
  954. // folding the branch isn't profitable in that case anyway.
  955. if (!Succ->getSinglePredecessor()) {
  956. BasicBlock::iterator BBI = BB->begin();
  957. while (isa<PHINode>(*BBI)) {
  958. for (Use &U : BBI->uses()) {
  959. if (PHINode* PN = dyn_cast<PHINode>(U.getUser())) {
  960. if (PN->getIncomingBlock(U) != BB)
  961. return false;
  962. } else {
  963. return false;
  964. }
  965. }
  966. ++BBI;
  967. }
  968. }
  969. // 'BB' and 'BB->Pred' are loop latches, bail out to presrve inner loop
  970. // metadata.
  971. //
  972. // FIXME: This is a stop-gap solution to preserve inner-loop metadata given
  973. // current status (that loop metadata is implemented as metadata attached to
  974. // the branch instruction in the loop latch block). To quote from review
  975. // comments, "the current representation of loop metadata (using a loop latch
  976. // terminator attachment) is known to be fundamentally broken. Loop latches
  977. // are not uniquely associated with loops (both in that a latch can be part of
  978. // multiple loops and a loop may have multiple latches). Loop headers are. The
  979. // solution to this problem is also known: Add support for basic block
  980. // metadata, and attach loop metadata to the loop header."
  981. //
  982. // Why bail out:
  983. // In this case, we expect 'BB' is the latch for outer-loop and 'BB->Pred' is
  984. // the latch for inner-loop (see reason below), so bail out to prerserve
  985. // inner-loop metadata rather than eliminating 'BB' and attaching its metadata
  986. // to this inner-loop.
  987. // - The reason we believe 'BB' and 'BB->Pred' have different inner-most
  988. // loops: assuming 'BB' and 'BB->Pred' are from the same inner-most loop L,
  989. // then 'BB' is the header and latch of 'L' and thereby 'L' must consist of
  990. // one self-looping basic block, which is contradictory with the assumption.
  991. //
  992. // To illustrate how inner-loop metadata is dropped:
  993. //
  994. // CFG Before
  995. //
  996. // BB is while.cond.exit, attached with loop metdata md2.
  997. // BB->Pred is for.body, attached with loop metadata md1.
  998. //
  999. // entry
  1000. // |
  1001. // v
  1002. // ---> while.cond -------------> while.end
  1003. // | |
  1004. // | v
  1005. // | while.body
  1006. // | |
  1007. // | v
  1008. // | for.body <---- (md1)
  1009. // | | |______|
  1010. // | v
  1011. // | while.cond.exit (md2)
  1012. // | |
  1013. // |_______|
  1014. //
  1015. // CFG After
  1016. //
  1017. // while.cond1 is the merge of while.cond.exit and while.cond above.
  1018. // for.body is attached with md2, and md1 is dropped.
  1019. // If LoopSimplify runs later (as a part of loop pass), it could create
  1020. // dedicated exits for inner-loop (essentially adding `while.cond.exit`
  1021. // back), but won't it won't see 'md1' nor restore it for the inner-loop.
  1022. //
  1023. // entry
  1024. // |
  1025. // v
  1026. // ---> while.cond1 -------------> while.end
  1027. // | |
  1028. // | v
  1029. // | while.body
  1030. // | |
  1031. // | v
  1032. // | for.body <---- (md2)
  1033. // |_______| |______|
  1034. if (Instruction *TI = BB->getTerminator())
  1035. if (TI->hasMetadata(LLVMContext::MD_loop))
  1036. for (BasicBlock *Pred : predecessors(BB))
  1037. if (Instruction *PredTI = Pred->getTerminator())
  1038. if (PredTI->hasMetadata(LLVMContext::MD_loop))
  1039. return false;
  1040. LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
  1041. SmallVector<DominatorTree::UpdateType, 32> Updates;
  1042. if (DTU) {
  1043. // To avoid processing the same predecessor more than once.
  1044. SmallPtrSet<BasicBlock *, 8> SeenPreds;
  1045. // All predecessors of BB will be moved to Succ.
  1046. SmallPtrSet<BasicBlock *, 8> PredsOfSucc(pred_begin(Succ), pred_end(Succ));
  1047. Updates.reserve(Updates.size() + 2 * pred_size(BB) + 1);
  1048. for (auto *PredOfBB : predecessors(BB))
  1049. // This predecessor of BB may already have Succ as a successor.
  1050. if (!PredsOfSucc.contains(PredOfBB))
  1051. if (SeenPreds.insert(PredOfBB).second)
  1052. Updates.push_back({DominatorTree::Insert, PredOfBB, Succ});
  1053. SeenPreds.clear();
  1054. for (auto *PredOfBB : predecessors(BB))
  1055. if (SeenPreds.insert(PredOfBB).second)
  1056. Updates.push_back({DominatorTree::Delete, PredOfBB, BB});
  1057. Updates.push_back({DominatorTree::Delete, BB, Succ});
  1058. }
  1059. if (isa<PHINode>(Succ->begin())) {
  1060. // If there is more than one pred of succ, and there are PHI nodes in
  1061. // the successor, then we need to add incoming edges for the PHI nodes
  1062. //
  1063. const PredBlockVector BBPreds(predecessors(BB));
  1064. // Loop over all of the PHI nodes in the successor of BB.
  1065. for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
  1066. PHINode *PN = cast<PHINode>(I);
  1067. redirectValuesFromPredecessorsToPhi(BB, BBPreds, PN);
  1068. }
  1069. }
  1070. if (Succ->getSinglePredecessor()) {
  1071. // BB is the only predecessor of Succ, so Succ will end up with exactly
  1072. // the same predecessors BB had.
  1073. // Copy over any phi, debug or lifetime instruction.
  1074. BB->getTerminator()->eraseFromParent();
  1075. Succ->splice(Succ->getFirstNonPHI()->getIterator(), BB);
  1076. } else {
  1077. while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
  1078. // We explicitly check for such uses in CanPropagatePredecessorsForPHIs.
  1079. assert(PN->use_empty() && "There shouldn't be any uses here!");
  1080. PN->eraseFromParent();
  1081. }
  1082. }
  1083. // If the unconditional branch we replaced contains llvm.loop metadata, we
  1084. // add the metadata to the branch instructions in the predecessors.
  1085. unsigned LoopMDKind = BB->getContext().getMDKindID("llvm.loop");
  1086. Instruction *TI = BB->getTerminator();
  1087. if (TI)
  1088. if (MDNode *LoopMD = TI->getMetadata(LoopMDKind))
  1089. for (BasicBlock *Pred : predecessors(BB))
  1090. Pred->getTerminator()->setMetadata(LoopMDKind, LoopMD);
  1091. // Everything that jumped to BB now goes to Succ.
  1092. BB->replaceAllUsesWith(Succ);
  1093. if (!Succ->hasName()) Succ->takeName(BB);
  1094. // Clear the successor list of BB to match updates applying to DTU later.
  1095. if (BB->getTerminator())
  1096. BB->back().eraseFromParent();
  1097. new UnreachableInst(BB->getContext(), BB);
  1098. assert(succ_empty(BB) && "The successor list of BB isn't empty before "
  1099. "applying corresponding DTU updates.");
  1100. if (DTU)
  1101. DTU->applyUpdates(Updates);
  1102. DeleteDeadBlock(BB, DTU);
  1103. return true;
  1104. }
  1105. static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB) {
  1106. // This implementation doesn't currently consider undef operands
  1107. // specially. Theoretically, two phis which are identical except for
  1108. // one having an undef where the other doesn't could be collapsed.
  1109. bool Changed = false;
  1110. // Examine each PHI.
  1111. // Note that increment of I must *NOT* be in the iteration_expression, since
  1112. // we don't want to immediately advance when we restart from the beginning.
  1113. for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I);) {
  1114. ++I;
  1115. // Is there an identical PHI node in this basic block?
  1116. // Note that we only look in the upper square's triangle,
  1117. // we already checked that the lower triangle PHI's aren't identical.
  1118. for (auto J = I; PHINode *DuplicatePN = dyn_cast<PHINode>(J); ++J) {
  1119. if (!DuplicatePN->isIdenticalToWhenDefined(PN))
  1120. continue;
  1121. // A duplicate. Replace this PHI with the base PHI.
  1122. ++NumPHICSEs;
  1123. DuplicatePN->replaceAllUsesWith(PN);
  1124. DuplicatePN->eraseFromParent();
  1125. Changed = true;
  1126. // The RAUW can change PHIs that we already visited.
  1127. I = BB->begin();
  1128. break; // Start over from the beginning.
  1129. }
  1130. }
  1131. return Changed;
  1132. }
  1133. static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB) {
  1134. // This implementation doesn't currently consider undef operands
  1135. // specially. Theoretically, two phis which are identical except for
  1136. // one having an undef where the other doesn't could be collapsed.
  1137. struct PHIDenseMapInfo {
  1138. static PHINode *getEmptyKey() {
  1139. return DenseMapInfo<PHINode *>::getEmptyKey();
  1140. }
  1141. static PHINode *getTombstoneKey() {
  1142. return DenseMapInfo<PHINode *>::getTombstoneKey();
  1143. }
  1144. static bool isSentinel(PHINode *PN) {
  1145. return PN == getEmptyKey() || PN == getTombstoneKey();
  1146. }
  1147. // WARNING: this logic must be kept in sync with
  1148. // Instruction::isIdenticalToWhenDefined()!
  1149. static unsigned getHashValueImpl(PHINode *PN) {
  1150. // Compute a hash value on the operands. Instcombine will likely have
  1151. // sorted them, which helps expose duplicates, but we have to check all
  1152. // the operands to be safe in case instcombine hasn't run.
  1153. return static_cast<unsigned>(hash_combine(
  1154. hash_combine_range(PN->value_op_begin(), PN->value_op_end()),
  1155. hash_combine_range(PN->block_begin(), PN->block_end())));
  1156. }
  1157. static unsigned getHashValue(PHINode *PN) {
  1158. #ifndef NDEBUG
  1159. // If -phicse-debug-hash was specified, return a constant -- this
  1160. // will force all hashing to collide, so we'll exhaustively search
  1161. // the table for a match, and the assertion in isEqual will fire if
  1162. // there's a bug causing equal keys to hash differently.
  1163. if (PHICSEDebugHash)
  1164. return 0;
  1165. #endif
  1166. return getHashValueImpl(PN);
  1167. }
  1168. static bool isEqualImpl(PHINode *LHS, PHINode *RHS) {
  1169. if (isSentinel(LHS) || isSentinel(RHS))
  1170. return LHS == RHS;
  1171. return LHS->isIdenticalTo(RHS);
  1172. }
  1173. static bool isEqual(PHINode *LHS, PHINode *RHS) {
  1174. // These comparisons are nontrivial, so assert that equality implies
  1175. // hash equality (DenseMap demands this as an invariant).
  1176. bool Result = isEqualImpl(LHS, RHS);
  1177. assert(!Result || (isSentinel(LHS) && LHS == RHS) ||
  1178. getHashValueImpl(LHS) == getHashValueImpl(RHS));
  1179. return Result;
  1180. }
  1181. };
  1182. // Set of unique PHINodes.
  1183. DenseSet<PHINode *, PHIDenseMapInfo> PHISet;
  1184. PHISet.reserve(4 * PHICSENumPHISmallSize);
  1185. // Examine each PHI.
  1186. bool Changed = false;
  1187. for (auto I = BB->begin(); PHINode *PN = dyn_cast<PHINode>(I++);) {
  1188. auto Inserted = PHISet.insert(PN);
  1189. if (!Inserted.second) {
  1190. // A duplicate. Replace this PHI with its duplicate.
  1191. ++NumPHICSEs;
  1192. PN->replaceAllUsesWith(*Inserted.first);
  1193. PN->eraseFromParent();
  1194. Changed = true;
  1195. // The RAUW can change PHIs that we already visited. Start over from the
  1196. // beginning.
  1197. PHISet.clear();
  1198. I = BB->begin();
  1199. }
  1200. }
  1201. return Changed;
  1202. }
  1203. bool llvm::EliminateDuplicatePHINodes(BasicBlock *BB) {
  1204. if (
  1205. #ifndef NDEBUG
  1206. !PHICSEDebugHash &&
  1207. #endif
  1208. hasNItemsOrLess(BB->phis(), PHICSENumPHISmallSize))
  1209. return EliminateDuplicatePHINodesNaiveImpl(BB);
  1210. return EliminateDuplicatePHINodesSetBasedImpl(BB);
  1211. }
  1212. /// If the specified pointer points to an object that we control, try to modify
  1213. /// the object's alignment to PrefAlign. Returns a minimum known alignment of
  1214. /// the value after the operation, which may be lower than PrefAlign.
  1215. ///
  1216. /// Increating value alignment isn't often possible though. If alignment is
  1217. /// important, a more reliable approach is to simply align all global variables
  1218. /// and allocation instructions to their preferred alignment from the beginning.
  1219. static Align tryEnforceAlignment(Value *V, Align PrefAlign,
  1220. const DataLayout &DL) {
  1221. V = V->stripPointerCasts();
  1222. if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
  1223. // TODO: Ideally, this function would not be called if PrefAlign is smaller
  1224. // than the current alignment, as the known bits calculation should have
  1225. // already taken it into account. However, this is not always the case,
  1226. // as computeKnownBits() has a depth limit, while stripPointerCasts()
  1227. // doesn't.
  1228. Align CurrentAlign = AI->getAlign();
  1229. if (PrefAlign <= CurrentAlign)
  1230. return CurrentAlign;
  1231. // If the preferred alignment is greater than the natural stack alignment
  1232. // then don't round up. This avoids dynamic stack realignment.
  1233. if (DL.exceedsNaturalStackAlignment(PrefAlign))
  1234. return CurrentAlign;
  1235. AI->setAlignment(PrefAlign);
  1236. return PrefAlign;
  1237. }
  1238. if (auto *GO = dyn_cast<GlobalObject>(V)) {
  1239. // TODO: as above, this shouldn't be necessary.
  1240. Align CurrentAlign = GO->getPointerAlignment(DL);
  1241. if (PrefAlign <= CurrentAlign)
  1242. return CurrentAlign;
  1243. // If there is a large requested alignment and we can, bump up the alignment
  1244. // of the global. If the memory we set aside for the global may not be the
  1245. // memory used by the final program then it is impossible for us to reliably
  1246. // enforce the preferred alignment.
  1247. if (!GO->canIncreaseAlignment())
  1248. return CurrentAlign;
  1249. GO->setAlignment(PrefAlign);
  1250. return PrefAlign;
  1251. }
  1252. return Align(1);
  1253. }
  1254. Align llvm::getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign,
  1255. const DataLayout &DL,
  1256. const Instruction *CxtI,
  1257. AssumptionCache *AC,
  1258. const DominatorTree *DT) {
  1259. assert(V->getType()->isPointerTy() &&
  1260. "getOrEnforceKnownAlignment expects a pointer!");
  1261. KnownBits Known = computeKnownBits(V, DL, 0, AC, CxtI, DT);
  1262. unsigned TrailZ = Known.countMinTrailingZeros();
  1263. // Avoid trouble with ridiculously large TrailZ values, such as
  1264. // those computed from a null pointer.
  1265. // LLVM doesn't support alignments larger than (1 << MaxAlignmentExponent).
  1266. TrailZ = std::min(TrailZ, +Value::MaxAlignmentExponent);
  1267. Align Alignment = Align(1ull << std::min(Known.getBitWidth() - 1, TrailZ));
  1268. if (PrefAlign && *PrefAlign > Alignment)
  1269. Alignment = std::max(Alignment, tryEnforceAlignment(V, *PrefAlign, DL));
  1270. // We don't need to make any adjustment.
  1271. return Alignment;
  1272. }
  1273. ///===---------------------------------------------------------------------===//
  1274. /// Dbg Intrinsic utilities
  1275. ///
  1276. /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
  1277. static bool PhiHasDebugValue(DILocalVariable *DIVar,
  1278. DIExpression *DIExpr,
  1279. PHINode *APN) {
  1280. // Since we can't guarantee that the original dbg.declare intrinsic
  1281. // is removed by LowerDbgDeclare(), we need to make sure that we are
  1282. // not inserting the same dbg.value intrinsic over and over.
  1283. SmallVector<DbgValueInst *, 1> DbgValues;
  1284. findDbgValues(DbgValues, APN);
  1285. for (auto *DVI : DbgValues) {
  1286. assert(is_contained(DVI->getValues(), APN));
  1287. if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr))
  1288. return true;
  1289. }
  1290. return false;
  1291. }
  1292. /// Check if the alloc size of \p ValTy is large enough to cover the variable
  1293. /// (or fragment of the variable) described by \p DII.
  1294. ///
  1295. /// This is primarily intended as a helper for the different
  1296. /// ConvertDebugDeclareToDebugValue functions. The dbg.declare/dbg.addr that is
  1297. /// converted describes an alloca'd variable, so we need to use the
  1298. /// alloc size of the value when doing the comparison. E.g. an i1 value will be
  1299. /// identified as covering an n-bit fragment, if the store size of i1 is at
  1300. /// least n bits.
  1301. static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) {
  1302. const DataLayout &DL = DII->getModule()->getDataLayout();
  1303. TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);
  1304. if (std::optional<uint64_t> FragmentSize = DII->getFragmentSizeInBits()) {
  1305. assert(!ValueSize.isScalable() &&
  1306. "Fragments don't work on scalable types.");
  1307. return ValueSize.getFixedValue() >= *FragmentSize;
  1308. }
  1309. // We can't always calculate the size of the DI variable (e.g. if it is a
  1310. // VLA). Try to use the size of the alloca that the dbg intrinsic describes
  1311. // intead.
  1312. if (DII->isAddressOfVariable()) {
  1313. // DII should have exactly 1 location when it is an address.
  1314. assert(DII->getNumVariableLocationOps() == 1 &&
  1315. "address of variable must have exactly 1 location operand.");
  1316. if (auto *AI =
  1317. dyn_cast_or_null<AllocaInst>(DII->getVariableLocationOp(0))) {
  1318. if (std::optional<TypeSize> FragmentSize =
  1319. AI->getAllocationSizeInBits(DL)) {
  1320. return TypeSize::isKnownGE(ValueSize, *FragmentSize);
  1321. }
  1322. }
  1323. }
  1324. // Could not determine size of variable. Conservatively return false.
  1325. return false;
  1326. }
  1327. /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
  1328. /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
  1329. void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
  1330. StoreInst *SI, DIBuilder &Builder) {
  1331. assert(DII->isAddressOfVariable() || isa<DbgAssignIntrinsic>(DII));
  1332. auto *DIVar = DII->getVariable();
  1333. assert(DIVar && "Missing variable");
  1334. auto *DIExpr = DII->getExpression();
  1335. Value *DV = SI->getValueOperand();
  1336. DebugLoc NewLoc = getDebugValueLoc(DII);
  1337. if (!valueCoversEntireFragment(DV->getType(), DII)) {
  1338. // FIXME: If storing to a part of the variable described by the dbg.declare,
  1339. // then we want to insert a dbg.value for the corresponding fragment.
  1340. LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
  1341. << *DII << '\n');
  1342. // For now, when there is a store to parts of the variable (but we do not
  1343. // know which part) we insert an dbg.value intrinsic to indicate that we
  1344. // know nothing about the variable's content.
  1345. DV = UndefValue::get(DV->getType());
  1346. Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI);
  1347. return;
  1348. }
  1349. Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI);
  1350. }
  1351. /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
  1352. /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
  1353. void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
  1354. LoadInst *LI, DIBuilder &Builder) {
  1355. auto *DIVar = DII->getVariable();
  1356. auto *DIExpr = DII->getExpression();
  1357. assert(DIVar && "Missing variable");
  1358. if (!valueCoversEntireFragment(LI->getType(), DII)) {
  1359. // FIXME: If only referring to a part of the variable described by the
  1360. // dbg.declare, then we want to insert a dbg.value for the corresponding
  1361. // fragment.
  1362. LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
  1363. << *DII << '\n');
  1364. return;
  1365. }
  1366. DebugLoc NewLoc = getDebugValueLoc(DII);
  1367. // We are now tracking the loaded value instead of the address. In the
  1368. // future if multi-location support is added to the IR, it might be
  1369. // preferable to keep tracking both the loaded value and the original
  1370. // address in case the alloca can not be elided.
  1371. Instruction *DbgValue = Builder.insertDbgValueIntrinsic(
  1372. LI, DIVar, DIExpr, NewLoc, (Instruction *)nullptr);
  1373. DbgValue->insertAfter(LI);
  1374. }
  1375. /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
  1376. /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
  1377. void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
  1378. PHINode *APN, DIBuilder &Builder) {
  1379. auto *DIVar = DII->getVariable();
  1380. auto *DIExpr = DII->getExpression();
  1381. assert(DIVar && "Missing variable");
  1382. if (PhiHasDebugValue(DIVar, DIExpr, APN))
  1383. return;
  1384. if (!valueCoversEntireFragment(APN->getType(), DII)) {
  1385. // FIXME: If only referring to a part of the variable described by the
  1386. // dbg.declare, then we want to insert a dbg.value for the corresponding
  1387. // fragment.
  1388. LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "
  1389. << *DII << '\n');
  1390. return;
  1391. }
  1392. BasicBlock *BB = APN->getParent();
  1393. auto InsertionPt = BB->getFirstInsertionPt();
  1394. DebugLoc NewLoc = getDebugValueLoc(DII);
  1395. // The block may be a catchswitch block, which does not have a valid
  1396. // insertion point.
  1397. // FIXME: Insert dbg.value markers in the successors when appropriate.
  1398. if (InsertionPt != BB->end())
  1399. Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, NewLoc, &*InsertionPt);
  1400. }
  1401. /// Determine whether this alloca is either a VLA or an array.
  1402. static bool isArray(AllocaInst *AI) {
  1403. return AI->isArrayAllocation() ||
  1404. (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
  1405. }
  1406. /// Determine whether this alloca is a structure.
  1407. static bool isStructure(AllocaInst *AI) {
  1408. return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
  1409. }
  1410. /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
  1411. /// of llvm.dbg.value intrinsics.
  1412. bool llvm::LowerDbgDeclare(Function &F) {
  1413. bool Changed = false;
  1414. DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
  1415. SmallVector<DbgDeclareInst *, 4> Dbgs;
  1416. for (auto &FI : F)
  1417. for (Instruction &BI : FI)
  1418. if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
  1419. Dbgs.push_back(DDI);
  1420. if (Dbgs.empty())
  1421. return Changed;
  1422. for (auto &I : Dbgs) {
  1423. DbgDeclareInst *DDI = I;
  1424. AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
  1425. // If this is an alloca for a scalar variable, insert a dbg.value
  1426. // at each load and store to the alloca and erase the dbg.declare.
  1427. // The dbg.values allow tracking a variable even if it is not
  1428. // stored on the stack, while the dbg.declare can only describe
  1429. // the stack slot (and at a lexical-scope granularity). Later
  1430. // passes will attempt to elide the stack slot.
  1431. if (!AI || isArray(AI) || isStructure(AI))
  1432. continue;
  1433. // A volatile load/store means that the alloca can't be elided anyway.
  1434. if (llvm::any_of(AI->users(), [](User *U) -> bool {
  1435. if (LoadInst *LI = dyn_cast<LoadInst>(U))
  1436. return LI->isVolatile();
  1437. if (StoreInst *SI = dyn_cast<StoreInst>(U))
  1438. return SI->isVolatile();
  1439. return false;
  1440. }))
  1441. continue;
  1442. SmallVector<const Value *, 8> WorkList;
  1443. WorkList.push_back(AI);
  1444. while (!WorkList.empty()) {
  1445. const Value *V = WorkList.pop_back_val();
  1446. for (const auto &AIUse : V->uses()) {
  1447. User *U = AIUse.getUser();
  1448. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  1449. if (AIUse.getOperandNo() == 1)
  1450. ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
  1451. } else if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  1452. ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
  1453. } else if (CallInst *CI = dyn_cast<CallInst>(U)) {
  1454. // This is a call by-value or some other instruction that takes a
  1455. // pointer to the variable. Insert a *value* intrinsic that describes
  1456. // the variable by dereferencing the alloca.
  1457. if (!CI->isLifetimeStartOrEnd()) {
  1458. DebugLoc NewLoc = getDebugValueLoc(DDI);
  1459. auto *DerefExpr =
  1460. DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref);
  1461. DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr,
  1462. NewLoc, CI);
  1463. }
  1464. } else if (BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
  1465. if (BI->getType()->isPointerTy())
  1466. WorkList.push_back(BI);
  1467. }
  1468. }
  1469. }
  1470. DDI->eraseFromParent();
  1471. Changed = true;
  1472. }
  1473. if (Changed)
  1474. for (BasicBlock &BB : F)
  1475. RemoveRedundantDbgInstrs(&BB);
  1476. return Changed;
  1477. }
  1478. /// Propagate dbg.value intrinsics through the newly inserted PHIs.
  1479. void llvm::insertDebugValuesForPHIs(BasicBlock *BB,
  1480. SmallVectorImpl<PHINode *> &InsertedPHIs) {
  1481. assert(BB && "No BasicBlock to clone dbg.value(s) from.");
  1482. if (InsertedPHIs.size() == 0)
  1483. return;
  1484. // Map existing PHI nodes to their dbg.values.
  1485. ValueToValueMapTy DbgValueMap;
  1486. for (auto &I : *BB) {
  1487. if (auto DbgII = dyn_cast<DbgVariableIntrinsic>(&I)) {
  1488. for (Value *V : DbgII->location_ops())
  1489. if (auto *Loc = dyn_cast_or_null<PHINode>(V))
  1490. DbgValueMap.insert({Loc, DbgII});
  1491. }
  1492. }
  1493. if (DbgValueMap.size() == 0)
  1494. return;
  1495. // Map a pair of the destination BB and old dbg.value to the new dbg.value,
  1496. // so that if a dbg.value is being rewritten to use more than one of the
  1497. // inserted PHIs in the same destination BB, we can update the same dbg.value
  1498. // with all the new PHIs instead of creating one copy for each.
  1499. MapVector<std::pair<BasicBlock *, DbgVariableIntrinsic *>,
  1500. DbgVariableIntrinsic *>
  1501. NewDbgValueMap;
  1502. // Then iterate through the new PHIs and look to see if they use one of the
  1503. // previously mapped PHIs. If so, create a new dbg.value intrinsic that will
  1504. // propagate the info through the new PHI. If we use more than one new PHI in
  1505. // a single destination BB with the same old dbg.value, merge the updates so
  1506. // that we get a single new dbg.value with all the new PHIs.
  1507. for (auto *PHI : InsertedPHIs) {
  1508. BasicBlock *Parent = PHI->getParent();
  1509. // Avoid inserting an intrinsic into an EH block.
  1510. if (Parent->getFirstNonPHI()->isEHPad())
  1511. continue;
  1512. for (auto *VI : PHI->operand_values()) {
  1513. auto V = DbgValueMap.find(VI);
  1514. if (V != DbgValueMap.end()) {
  1515. auto *DbgII = cast<DbgVariableIntrinsic>(V->second);
  1516. auto NewDI = NewDbgValueMap.find({Parent, DbgII});
  1517. if (NewDI == NewDbgValueMap.end()) {
  1518. auto *NewDbgII = cast<DbgVariableIntrinsic>(DbgII->clone());
  1519. NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first;
  1520. }
  1521. DbgVariableIntrinsic *NewDbgII = NewDI->second;
  1522. // If PHI contains VI as an operand more than once, we may
  1523. // replaced it in NewDbgII; confirm that it is present.
  1524. if (is_contained(NewDbgII->location_ops(), VI))
  1525. NewDbgII->replaceVariableLocationOp(VI, PHI);
  1526. }
  1527. }
  1528. }
  1529. // Insert thew new dbg.values into their destination blocks.
  1530. for (auto DI : NewDbgValueMap) {
  1531. BasicBlock *Parent = DI.first.first;
  1532. auto *NewDbgII = DI.second;
  1533. auto InsertionPt = Parent->getFirstInsertionPt();
  1534. assert(InsertionPt != Parent->end() && "Ill-formed basic block");
  1535. NewDbgII->insertBefore(&*InsertionPt);
  1536. }
  1537. }
  1538. bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
  1539. DIBuilder &Builder, uint8_t DIExprFlags,
  1540. int Offset) {
  1541. auto DbgAddrs = FindDbgAddrUses(Address);
  1542. for (DbgVariableIntrinsic *DII : DbgAddrs) {
  1543. const DebugLoc &Loc = DII->getDebugLoc();
  1544. auto *DIVar = DII->getVariable();
  1545. auto *DIExpr = DII->getExpression();
  1546. assert(DIVar && "Missing variable");
  1547. DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
  1548. // Insert llvm.dbg.declare immediately before DII, and remove old
  1549. // llvm.dbg.declare.
  1550. Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, DII);
  1551. DII->eraseFromParent();
  1552. }
  1553. return !DbgAddrs.empty();
  1554. }
  1555. static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress,
  1556. DIBuilder &Builder, int Offset) {
  1557. const DebugLoc &Loc = DVI->getDebugLoc();
  1558. auto *DIVar = DVI->getVariable();
  1559. auto *DIExpr = DVI->getExpression();
  1560. assert(DIVar && "Missing variable");
  1561. // This is an alloca-based llvm.dbg.value. The first thing it should do with
  1562. // the alloca pointer is dereference it. Otherwise we don't know how to handle
  1563. // it and give up.
  1564. if (!DIExpr || DIExpr->getNumElements() < 1 ||
  1565. DIExpr->getElement(0) != dwarf::DW_OP_deref)
  1566. return;
  1567. // Insert the offset before the first deref.
  1568. // We could just change the offset argument of dbg.value, but it's unsigned...
  1569. if (Offset)
  1570. DIExpr = DIExpression::prepend(DIExpr, 0, Offset);
  1571. Builder.insertDbgValueIntrinsic(NewAddress, DIVar, DIExpr, Loc, DVI);
  1572. DVI->eraseFromParent();
  1573. }
  1574. void llvm::replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
  1575. DIBuilder &Builder, int Offset) {
  1576. if (auto *L = LocalAsMetadata::getIfExists(AI))
  1577. if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
  1578. for (Use &U : llvm::make_early_inc_range(MDV->uses()))
  1579. if (auto *DVI = dyn_cast<DbgValueInst>(U.getUser()))
  1580. replaceOneDbgValueForAlloca(DVI, NewAllocaAddress, Builder, Offset);
  1581. }
  1582. /// Where possible to salvage debug information for \p I do so.
  1583. /// If not possible mark undef.
  1584. void llvm::salvageDebugInfo(Instruction &I) {
  1585. SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
  1586. findDbgUsers(DbgUsers, &I);
  1587. salvageDebugInfoForDbgValues(I, DbgUsers);
  1588. }
  1589. /// Salvage the address component of \p DAI.
  1590. static void salvageDbgAssignAddress(DbgAssignIntrinsic *DAI) {
  1591. Instruction *I = dyn_cast<Instruction>(DAI->getAddress());
  1592. // Only instructions can be salvaged at the moment.
  1593. if (!I)
  1594. return;
  1595. assert(!DAI->getAddressExpression()->getFragmentInfo().has_value() &&
  1596. "address-expression shouldn't have fragment info");
  1597. // The address component of a dbg.assign cannot be variadic.
  1598. uint64_t CurrentLocOps = 0;
  1599. SmallVector<Value *, 4> AdditionalValues;
  1600. SmallVector<uint64_t, 16> Ops;
  1601. Value *NewV = salvageDebugInfoImpl(*I, CurrentLocOps, Ops, AdditionalValues);
  1602. // Check if the salvage failed.
  1603. if (!NewV)
  1604. return;
  1605. DIExpression *SalvagedExpr = DIExpression::appendOpsToArg(
  1606. DAI->getAddressExpression(), Ops, 0, /*StackValue=*/false);
  1607. assert(!SalvagedExpr->getFragmentInfo().has_value() &&
  1608. "address-expression shouldn't have fragment info");
  1609. // Salvage succeeds if no additional values are required.
  1610. if (AdditionalValues.empty()) {
  1611. DAI->setAddress(NewV);
  1612. DAI->setAddressExpression(SalvagedExpr);
  1613. } else {
  1614. DAI->setKillAddress();
  1615. }
  1616. }
  1617. void llvm::salvageDebugInfoForDbgValues(
  1618. Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers) {
  1619. // These are arbitrary chosen limits on the maximum number of values and the
  1620. // maximum size of a debug expression we can salvage up to, used for
  1621. // performance reasons.
  1622. const unsigned MaxDebugArgs = 16;
  1623. const unsigned MaxExpressionSize = 128;
  1624. bool Salvaged = false;
  1625. for (auto *DII : DbgUsers) {
  1626. if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DII)) {
  1627. if (DAI->getAddress() == &I) {
  1628. salvageDbgAssignAddress(DAI);
  1629. Salvaged = true;
  1630. }
  1631. if (DAI->getValue() != &I)
  1632. continue;
  1633. }
  1634. // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they
  1635. // are implicitly pointing out the value as a DWARF memory location
  1636. // description.
  1637. bool StackValue = isa<DbgValueInst>(DII);
  1638. auto DIILocation = DII->location_ops();
  1639. assert(
  1640. is_contained(DIILocation, &I) &&
  1641. "DbgVariableIntrinsic must use salvaged instruction as its location");
  1642. SmallVector<Value *, 4> AdditionalValues;
  1643. // `I` may appear more than once in DII's location ops, and each use of `I`
  1644. // must be updated in the DIExpression and potentially have additional
  1645. // values added; thus we call salvageDebugInfoImpl for each `I` instance in
  1646. // DIILocation.
  1647. Value *Op0 = nullptr;
  1648. DIExpression *SalvagedExpr = DII->getExpression();
  1649. auto LocItr = find(DIILocation, &I);
  1650. while (SalvagedExpr && LocItr != DIILocation.end()) {
  1651. SmallVector<uint64_t, 16> Ops;
  1652. unsigned LocNo = std::distance(DIILocation.begin(), LocItr);
  1653. uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
  1654. Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
  1655. if (!Op0)
  1656. break;
  1657. SalvagedExpr =
  1658. DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
  1659. LocItr = std::find(++LocItr, DIILocation.end(), &I);
  1660. }
  1661. // salvageDebugInfoImpl should fail on examining the first element of
  1662. // DbgUsers, or none of them.
  1663. if (!Op0)
  1664. break;
  1665. DII->replaceVariableLocationOp(&I, Op0);
  1666. bool IsValidSalvageExpr = SalvagedExpr->getNumElements() <= MaxExpressionSize;
  1667. if (AdditionalValues.empty() && IsValidSalvageExpr) {
  1668. DII->setExpression(SalvagedExpr);
  1669. } else if (isa<DbgValueInst>(DII) && !isa<DbgAssignIntrinsic>(DII) &&
  1670. IsValidSalvageExpr &&
  1671. DII->getNumVariableLocationOps() + AdditionalValues.size() <=
  1672. MaxDebugArgs) {
  1673. DII->addVariableLocationOps(AdditionalValues, SalvagedExpr);
  1674. } else {
  1675. // Do not salvage using DIArgList for dbg.addr/dbg.declare, as it is
  1676. // not currently supported in those instructions. Do not salvage using
  1677. // DIArgList for dbg.assign yet. FIXME: support this.
  1678. // Also do not salvage if the resulting DIArgList would contain an
  1679. // unreasonably large number of values.
  1680. DII->setKillLocation();
  1681. }
  1682. LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n');
  1683. Salvaged = true;
  1684. }
  1685. if (Salvaged)
  1686. return;
  1687. for (auto *DII : DbgUsers)
  1688. DII->setKillLocation();
  1689. }
  1690. Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
  1691. uint64_t CurrentLocOps,
  1692. SmallVectorImpl<uint64_t> &Opcodes,
  1693. SmallVectorImpl<Value *> &AdditionalValues) {
  1694. unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
  1695. // Rewrite a GEP into a DIExpression.
  1696. MapVector<Value *, APInt> VariableOffsets;
  1697. APInt ConstantOffset(BitWidth, 0);
  1698. if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
  1699. return nullptr;
  1700. if (!VariableOffsets.empty() && !CurrentLocOps) {
  1701. Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0});
  1702. CurrentLocOps = 1;
  1703. }
  1704. for (auto Offset : VariableOffsets) {
  1705. AdditionalValues.push_back(Offset.first);
  1706. assert(Offset.second.isStrictlyPositive() &&
  1707. "Expected strictly positive multiplier for offset.");
  1708. Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps++, dwarf::DW_OP_constu,
  1709. Offset.second.getZExtValue(), dwarf::DW_OP_mul,
  1710. dwarf::DW_OP_plus});
  1711. }
  1712. DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue());
  1713. return GEP->getOperand(0);
  1714. }
  1715. uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) {
  1716. switch (Opcode) {
  1717. case Instruction::Add:
  1718. return dwarf::DW_OP_plus;
  1719. case Instruction::Sub:
  1720. return dwarf::DW_OP_minus;
  1721. case Instruction::Mul:
  1722. return dwarf::DW_OP_mul;
  1723. case Instruction::SDiv:
  1724. return dwarf::DW_OP_div;
  1725. case Instruction::SRem:
  1726. return dwarf::DW_OP_mod;
  1727. case Instruction::Or:
  1728. return dwarf::DW_OP_or;
  1729. case Instruction::And:
  1730. return dwarf::DW_OP_and;
  1731. case Instruction::Xor:
  1732. return dwarf::DW_OP_xor;
  1733. case Instruction::Shl:
  1734. return dwarf::DW_OP_shl;
  1735. case Instruction::LShr:
  1736. return dwarf::DW_OP_shr;
  1737. case Instruction::AShr:
  1738. return dwarf::DW_OP_shra;
  1739. default:
  1740. // TODO: Salvage from each kind of binop we know about.
  1741. return 0;
  1742. }
  1743. }
  1744. Value *getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
  1745. SmallVectorImpl<uint64_t> &Opcodes,
  1746. SmallVectorImpl<Value *> &AdditionalValues) {
  1747. // Handle binary operations with constant integer operands as a special case.
  1748. auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1));
  1749. // Values wider than 64 bits cannot be represented within a DIExpression.
  1750. if (ConstInt && ConstInt->getBitWidth() > 64)
  1751. return nullptr;
  1752. Instruction::BinaryOps BinOpcode = BI->getOpcode();
  1753. // Push any Constant Int operand onto the expression stack.
  1754. if (ConstInt) {
  1755. uint64_t Val = ConstInt->getSExtValue();
  1756. // Add or Sub Instructions with a constant operand can potentially be
  1757. // simplified.
  1758. if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) {
  1759. uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val);
  1760. DIExpression::appendOffset(Opcodes, Offset);
  1761. return BI->getOperand(0);
  1762. }
  1763. Opcodes.append({dwarf::DW_OP_constu, Val});
  1764. } else {
  1765. if (!CurrentLocOps) {
  1766. Opcodes.append({dwarf::DW_OP_LLVM_arg, 0});
  1767. CurrentLocOps = 1;
  1768. }
  1769. Opcodes.append({dwarf::DW_OP_LLVM_arg, CurrentLocOps});
  1770. AdditionalValues.push_back(BI->getOperand(1));
  1771. }
  1772. // Add salvaged binary operator to expression stack, if it has a valid
  1773. // representation in a DIExpression.
  1774. uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode);
  1775. if (!DwarfBinOp)
  1776. return nullptr;
  1777. Opcodes.push_back(DwarfBinOp);
  1778. return BI->getOperand(0);
  1779. }
  1780. Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
  1781. SmallVectorImpl<uint64_t> &Ops,
  1782. SmallVectorImpl<Value *> &AdditionalValues) {
  1783. auto &M = *I.getModule();
  1784. auto &DL = M.getDataLayout();
  1785. if (auto *CI = dyn_cast<CastInst>(&I)) {
  1786. Value *FromValue = CI->getOperand(0);
  1787. // No-op casts are irrelevant for debug info.
  1788. if (CI->isNoopCast(DL)) {
  1789. return FromValue;
  1790. }
  1791. Type *Type = CI->getType();
  1792. if (Type->isPointerTy())
  1793. Type = DL.getIntPtrType(Type);
  1794. // Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
  1795. if (Type->isVectorTy() ||
  1796. !(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I) ||
  1797. isa<IntToPtrInst>(&I) || isa<PtrToIntInst>(&I)))
  1798. return nullptr;
  1799. llvm::Type *FromType = FromValue->getType();
  1800. if (FromType->isPointerTy())
  1801. FromType = DL.getIntPtrType(FromType);
  1802. unsigned FromTypeBitSize = FromType->getScalarSizeInBits();
  1803. unsigned ToTypeBitSize = Type->getScalarSizeInBits();
  1804. auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
  1805. isa<SExtInst>(&I));
  1806. Ops.append(ExtOps.begin(), ExtOps.end());
  1807. return FromValue;
  1808. }
  1809. if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
  1810. return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
  1811. if (auto *BI = dyn_cast<BinaryOperator>(&I))
  1812. return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
  1813. // *Not* to do: we should not attempt to salvage load instructions,
  1814. // because the validity and lifetime of a dbg.value containing
  1815. // DW_OP_deref becomes difficult to analyze. See PR40628 for examples.
  1816. return nullptr;
  1817. }
  1818. /// A replacement for a dbg.value expression.
  1819. using DbgValReplacement = std::optional<DIExpression *>;
  1820. /// Point debug users of \p From to \p To using exprs given by \p RewriteExpr,
  1821. /// possibly moving/undefing users to prevent use-before-def. Returns true if
  1822. /// changes are made.
  1823. static bool rewriteDebugUsers(
  1824. Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT,
  1825. function_ref<DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr) {
  1826. // Find debug users of From.
  1827. SmallVector<DbgVariableIntrinsic *, 1> Users;
  1828. findDbgUsers(Users, &From);
  1829. if (Users.empty())
  1830. return false;
  1831. // Prevent use-before-def of To.
  1832. bool Changed = false;
  1833. SmallPtrSet<DbgVariableIntrinsic *, 1> UndefOrSalvage;
  1834. if (isa<Instruction>(&To)) {
  1835. bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint;
  1836. for (auto *DII : Users) {
  1837. // It's common to see a debug user between From and DomPoint. Move it
  1838. // after DomPoint to preserve the variable update without any reordering.
  1839. if (DomPointAfterFrom && DII->getNextNonDebugInstruction() == &DomPoint) {
  1840. LLVM_DEBUG(dbgs() << "MOVE: " << *DII << '\n');
  1841. DII->moveAfter(&DomPoint);
  1842. Changed = true;
  1843. // Users which otherwise aren't dominated by the replacement value must
  1844. // be salvaged or deleted.
  1845. } else if (!DT.dominates(&DomPoint, DII)) {
  1846. UndefOrSalvage.insert(DII);
  1847. }
  1848. }
  1849. }
  1850. // Update debug users without use-before-def risk.
  1851. for (auto *DII : Users) {
  1852. if (UndefOrSalvage.count(DII))
  1853. continue;
  1854. DbgValReplacement DVR = RewriteExpr(*DII);
  1855. if (!DVR)
  1856. continue;
  1857. DII->replaceVariableLocationOp(&From, &To);
  1858. DII->setExpression(*DVR);
  1859. LLVM_DEBUG(dbgs() << "REWRITE: " << *DII << '\n');
  1860. Changed = true;
  1861. }
  1862. if (!UndefOrSalvage.empty()) {
  1863. // Try to salvage the remaining debug users.
  1864. salvageDebugInfo(From);
  1865. Changed = true;
  1866. }
  1867. return Changed;
  1868. }
  1869. /// Check if a bitcast between a value of type \p FromTy to type \p ToTy would
  1870. /// losslessly preserve the bits and semantics of the value. This predicate is
  1871. /// symmetric, i.e swapping \p FromTy and \p ToTy should give the same result.
  1872. ///
  1873. /// Note that Type::canLosslesslyBitCastTo is not suitable here because it
  1874. /// allows semantically unequivalent bitcasts, such as <2 x i64> -> <4 x i32>,
  1875. /// and also does not allow lossless pointer <-> integer conversions.
  1876. static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy,
  1877. Type *ToTy) {
  1878. // Trivially compatible types.
  1879. if (FromTy == ToTy)
  1880. return true;
  1881. // Handle compatible pointer <-> integer conversions.
  1882. if (FromTy->isIntOrPtrTy() && ToTy->isIntOrPtrTy()) {
  1883. bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy);
  1884. bool LosslessConversion = !DL.isNonIntegralPointerType(FromTy) &&
  1885. !DL.isNonIntegralPointerType(ToTy);
  1886. return SameSize && LosslessConversion;
  1887. }
  1888. // TODO: This is not exhaustive.
  1889. return false;
  1890. }
  1891. bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To,
  1892. Instruction &DomPoint, DominatorTree &DT) {
  1893. // Exit early if From has no debug users.
  1894. if (!From.isUsedByMetadata())
  1895. return false;
  1896. assert(&From != &To && "Can't replace something with itself");
  1897. Type *FromTy = From.getType();
  1898. Type *ToTy = To.getType();
  1899. auto Identity = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
  1900. return DII.getExpression();
  1901. };
  1902. // Handle no-op conversions.
  1903. Module &M = *From.getModule();
  1904. const DataLayout &DL = M.getDataLayout();
  1905. if (isBitCastSemanticsPreserving(DL, FromTy, ToTy))
  1906. return rewriteDebugUsers(From, To, DomPoint, DT, Identity);
  1907. // Handle integer-to-integer widening and narrowing.
  1908. // FIXME: Use DW_OP_convert when it's available everywhere.
  1909. if (FromTy->isIntegerTy() && ToTy->isIntegerTy()) {
  1910. uint64_t FromBits = FromTy->getPrimitiveSizeInBits();
  1911. uint64_t ToBits = ToTy->getPrimitiveSizeInBits();
  1912. assert(FromBits != ToBits && "Unexpected no-op conversion");
  1913. // When the width of the result grows, assume that a debugger will only
  1914. // access the low `FromBits` bits when inspecting the source variable.
  1915. if (FromBits < ToBits)
  1916. return rewriteDebugUsers(From, To, DomPoint, DT, Identity);
  1917. // The width of the result has shrunk. Use sign/zero extension to describe
  1918. // the source variable's high bits.
  1919. auto SignOrZeroExt = [&](DbgVariableIntrinsic &DII) -> DbgValReplacement {
  1920. DILocalVariable *Var = DII.getVariable();
  1921. // Without knowing signedness, sign/zero extension isn't possible.
  1922. auto Signedness = Var->getSignedness();
  1923. if (!Signedness)
  1924. return std::nullopt;
  1925. bool Signed = *Signedness == DIBasicType::Signedness::Signed;
  1926. return DIExpression::appendExt(DII.getExpression(), ToBits, FromBits,
  1927. Signed);
  1928. };
  1929. return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt);
  1930. }
  1931. // TODO: Floating-point conversions, vectors.
  1932. return false;
  1933. }
  1934. std::pair<unsigned, unsigned>
  1935. llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
  1936. unsigned NumDeadInst = 0;
  1937. unsigned NumDeadDbgInst = 0;
  1938. // Delete the instructions backwards, as it has a reduced likelihood of
  1939. // having to update as many def-use and use-def chains.
  1940. Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
  1941. while (EndInst != &BB->front()) {
  1942. // Delete the next to last instruction.
  1943. Instruction *Inst = &*--EndInst->getIterator();
  1944. if (!Inst->use_empty() && !Inst->getType()->isTokenTy())
  1945. Inst->replaceAllUsesWith(PoisonValue::get(Inst->getType()));
  1946. if (Inst->isEHPad() || Inst->getType()->isTokenTy()) {
  1947. EndInst = Inst;
  1948. continue;
  1949. }
  1950. if (isa<DbgInfoIntrinsic>(Inst))
  1951. ++NumDeadDbgInst;
  1952. else
  1953. ++NumDeadInst;
  1954. Inst->eraseFromParent();
  1955. }
  1956. return {NumDeadInst, NumDeadDbgInst};
  1957. }
  1958. unsigned llvm::changeToUnreachable(Instruction *I, bool PreserveLCSSA,
  1959. DomTreeUpdater *DTU,
  1960. MemorySSAUpdater *MSSAU) {
  1961. BasicBlock *BB = I->getParent();
  1962. if (MSSAU)
  1963. MSSAU->changeToUnreachable(I);
  1964. SmallSet<BasicBlock *, 8> UniqueSuccessors;
  1965. // Loop over all of the successors, removing BB's entry from any PHI
  1966. // nodes.
  1967. for (BasicBlock *Successor : successors(BB)) {
  1968. Successor->removePredecessor(BB, PreserveLCSSA);
  1969. if (DTU)
  1970. UniqueSuccessors.insert(Successor);
  1971. }
  1972. auto *UI = new UnreachableInst(I->getContext(), I);
  1973. UI->setDebugLoc(I->getDebugLoc());
  1974. // All instructions after this are dead.
  1975. unsigned NumInstrsRemoved = 0;
  1976. BasicBlock::iterator BBI = I->getIterator(), BBE = BB->end();
  1977. while (BBI != BBE) {
  1978. if (!BBI->use_empty())
  1979. BBI->replaceAllUsesWith(PoisonValue::get(BBI->getType()));
  1980. BBI++->eraseFromParent();
  1981. ++NumInstrsRemoved;
  1982. }
  1983. if (DTU) {
  1984. SmallVector<DominatorTree::UpdateType, 8> Updates;
  1985. Updates.reserve(UniqueSuccessors.size());
  1986. for (BasicBlock *UniqueSuccessor : UniqueSuccessors)
  1987. Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor});
  1988. DTU->applyUpdates(Updates);
  1989. }
  1990. return NumInstrsRemoved;
  1991. }
  1992. CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) {
  1993. SmallVector<Value *, 8> Args(II->args());
  1994. SmallVector<OperandBundleDef, 1> OpBundles;
  1995. II->getOperandBundlesAsDefs(OpBundles);
  1996. CallInst *NewCall = CallInst::Create(II->getFunctionType(),
  1997. II->getCalledOperand(), Args, OpBundles);
  1998. NewCall->setCallingConv(II->getCallingConv());
  1999. NewCall->setAttributes(II->getAttributes());
  2000. NewCall->setDebugLoc(II->getDebugLoc());
  2001. NewCall->copyMetadata(*II);
  2002. // If the invoke had profile metadata, try converting them for CallInst.
  2003. uint64_t TotalWeight;
  2004. if (NewCall->extractProfTotalWeight(TotalWeight)) {
  2005. // Set the total weight if it fits into i32, otherwise reset.
  2006. MDBuilder MDB(NewCall->getContext());
  2007. auto NewWeights = uint32_t(TotalWeight) != TotalWeight
  2008. ? nullptr
  2009. : MDB.createBranchWeights({uint32_t(TotalWeight)});
  2010. NewCall->setMetadata(LLVMContext::MD_prof, NewWeights);
  2011. }
  2012. return NewCall;
  2013. }
  2014. // changeToCall - Convert the specified invoke into a normal call.
  2015. CallInst *llvm::changeToCall(InvokeInst *II, DomTreeUpdater *DTU) {
  2016. CallInst *NewCall = createCallMatchingInvoke(II);
  2017. NewCall->takeName(II);
  2018. NewCall->insertBefore(II);
  2019. II->replaceAllUsesWith(NewCall);
  2020. // Follow the call by a branch to the normal destination.
  2021. BasicBlock *NormalDestBB = II->getNormalDest();
  2022. BranchInst::Create(NormalDestBB, II);
  2023. // Update PHI nodes in the unwind destination
  2024. BasicBlock *BB = II->getParent();
  2025. BasicBlock *UnwindDestBB = II->getUnwindDest();
  2026. UnwindDestBB->removePredecessor(BB);
  2027. II->eraseFromParent();
  2028. if (DTU)
  2029. DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
  2030. return NewCall;
  2031. }
  2032. BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
  2033. BasicBlock *UnwindEdge,
  2034. DomTreeUpdater *DTU) {
  2035. BasicBlock *BB = CI->getParent();
  2036. // Convert this function call into an invoke instruction. First, split the
  2037. // basic block.
  2038. BasicBlock *Split = SplitBlock(BB, CI, DTU, /*LI=*/nullptr, /*MSSAU*/ nullptr,
  2039. CI->getName() + ".noexc");
  2040. // Delete the unconditional branch inserted by SplitBlock
  2041. BB->back().eraseFromParent();
  2042. // Create the new invoke instruction.
  2043. SmallVector<Value *, 8> InvokeArgs(CI->args());
  2044. SmallVector<OperandBundleDef, 1> OpBundles;
  2045. CI->getOperandBundlesAsDefs(OpBundles);
  2046. // Note: we're round tripping operand bundles through memory here, and that
  2047. // can potentially be avoided with a cleverer API design that we do not have
  2048. // as of this time.
  2049. InvokeInst *II =
  2050. InvokeInst::Create(CI->getFunctionType(), CI->getCalledOperand(), Split,
  2051. UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB);
  2052. II->setDebugLoc(CI->getDebugLoc());
  2053. II->setCallingConv(CI->getCallingConv());
  2054. II->setAttributes(CI->getAttributes());
  2055. II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));
  2056. if (DTU)
  2057. DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});
  2058. // Make sure that anything using the call now uses the invoke! This also
  2059. // updates the CallGraph if present, because it uses a WeakTrackingVH.
  2060. CI->replaceAllUsesWith(II);
  2061. // Delete the original call
  2062. Split->front().eraseFromParent();
  2063. return Split;
  2064. }
  2065. static bool markAliveBlocks(Function &F,
  2066. SmallPtrSetImpl<BasicBlock *> &Reachable,
  2067. DomTreeUpdater *DTU = nullptr) {
  2068. SmallVector<BasicBlock*, 128> Worklist;
  2069. BasicBlock *BB = &F.front();
  2070. Worklist.push_back(BB);
  2071. Reachable.insert(BB);
  2072. bool Changed = false;
  2073. do {
  2074. BB = Worklist.pop_back_val();
  2075. // Do a quick scan of the basic block, turning any obviously unreachable
  2076. // instructions into LLVM unreachable insts. The instruction combining pass
  2077. // canonicalizes unreachable insts into stores to null or undef.
  2078. for (Instruction &I : *BB) {
  2079. if (auto *CI = dyn_cast<CallInst>(&I)) {
  2080. Value *Callee = CI->getCalledOperand();
  2081. // Handle intrinsic calls.
  2082. if (Function *F = dyn_cast<Function>(Callee)) {
  2083. auto IntrinsicID = F->getIntrinsicID();
  2084. // Assumptions that are known to be false are equivalent to
  2085. // unreachable. Also, if the condition is undefined, then we make the
  2086. // choice most beneficial to the optimizer, and choose that to also be
  2087. // unreachable.
  2088. if (IntrinsicID == Intrinsic::assume) {
  2089. if (match(CI->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
  2090. // Don't insert a call to llvm.trap right before the unreachable.
  2091. changeToUnreachable(CI, false, DTU);
  2092. Changed = true;
  2093. break;
  2094. }
  2095. } else if (IntrinsicID == Intrinsic::experimental_guard) {
  2096. // A call to the guard intrinsic bails out of the current
  2097. // compilation unit if the predicate passed to it is false. If the
  2098. // predicate is a constant false, then we know the guard will bail
  2099. // out of the current compile unconditionally, so all code following
  2100. // it is dead.
  2101. //
  2102. // Note: unlike in llvm.assume, it is not "obviously profitable" for
  2103. // guards to treat `undef` as `false` since a guard on `undef` can
  2104. // still be useful for widening.
  2105. if (match(CI->getArgOperand(0), m_Zero()))
  2106. if (!isa<UnreachableInst>(CI->getNextNode())) {
  2107. changeToUnreachable(CI->getNextNode(), false, DTU);
  2108. Changed = true;
  2109. break;
  2110. }
  2111. }
  2112. } else if ((isa<ConstantPointerNull>(Callee) &&
  2113. !NullPointerIsDefined(CI->getFunction(),
  2114. cast<PointerType>(Callee->getType())
  2115. ->getAddressSpace())) ||
  2116. isa<UndefValue>(Callee)) {
  2117. changeToUnreachable(CI, false, DTU);
  2118. Changed = true;
  2119. break;
  2120. }
  2121. if (CI->doesNotReturn() && !CI->isMustTailCall()) {
  2122. // If we found a call to a no-return function, insert an unreachable
  2123. // instruction after it. Make sure there isn't *already* one there
  2124. // though.
  2125. if (!isa<UnreachableInst>(CI->getNextNode())) {
  2126. // Don't insert a call to llvm.trap right before the unreachable.
  2127. changeToUnreachable(CI->getNextNode(), false, DTU);
  2128. Changed = true;
  2129. }
  2130. break;
  2131. }
  2132. } else if (auto *SI = dyn_cast<StoreInst>(&I)) {
  2133. // Store to undef and store to null are undefined and used to signal
  2134. // that they should be changed to unreachable by passes that can't
  2135. // modify the CFG.
  2136. // Don't touch volatile stores.
  2137. if (SI->isVolatile()) continue;
  2138. Value *Ptr = SI->getOperand(1);
  2139. if (isa<UndefValue>(Ptr) ||
  2140. (isa<ConstantPointerNull>(Ptr) &&
  2141. !NullPointerIsDefined(SI->getFunction(),
  2142. SI->getPointerAddressSpace()))) {
  2143. changeToUnreachable(SI, false, DTU);
  2144. Changed = true;
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. Instruction *Terminator = BB->getTerminator();
  2150. if (auto *II = dyn_cast<InvokeInst>(Terminator)) {
  2151. // Turn invokes that call 'nounwind' functions into ordinary calls.
  2152. Value *Callee = II->getCalledOperand();
  2153. if ((isa<ConstantPointerNull>(Callee) &&
  2154. !NullPointerIsDefined(BB->getParent())) ||
  2155. isa<UndefValue>(Callee)) {
  2156. changeToUnreachable(II, false, DTU);
  2157. Changed = true;
  2158. } else {
  2159. if (II->doesNotReturn() &&
  2160. !isa<UnreachableInst>(II->getNormalDest()->front())) {
  2161. // If we found an invoke of a no-return function,
  2162. // create a new empty basic block with an `unreachable` terminator,
  2163. // and set it as the normal destination for the invoke,
  2164. // unless that is already the case.
  2165. // Note that the original normal destination could have other uses.
  2166. BasicBlock *OrigNormalDest = II->getNormalDest();
  2167. OrigNormalDest->removePredecessor(II->getParent());
  2168. LLVMContext &Ctx = II->getContext();
  2169. BasicBlock *UnreachableNormalDest = BasicBlock::Create(
  2170. Ctx, OrigNormalDest->getName() + ".unreachable",
  2171. II->getFunction(), OrigNormalDest);
  2172. new UnreachableInst(Ctx, UnreachableNormalDest);
  2173. II->setNormalDest(UnreachableNormalDest);
  2174. if (DTU)
  2175. DTU->applyUpdates(
  2176. {{DominatorTree::Delete, BB, OrigNormalDest},
  2177. {DominatorTree::Insert, BB, UnreachableNormalDest}});
  2178. Changed = true;
  2179. }
  2180. if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
  2181. if (II->use_empty() && !II->mayHaveSideEffects()) {
  2182. // jump to the normal destination branch.
  2183. BasicBlock *NormalDestBB = II->getNormalDest();
  2184. BasicBlock *UnwindDestBB = II->getUnwindDest();
  2185. BranchInst::Create(NormalDestBB, II);
  2186. UnwindDestBB->removePredecessor(II->getParent());
  2187. II->eraseFromParent();
  2188. if (DTU)
  2189. DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});
  2190. } else
  2191. changeToCall(II, DTU);
  2192. Changed = true;
  2193. }
  2194. }
  2195. } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Terminator)) {
  2196. // Remove catchpads which cannot be reached.
  2197. struct CatchPadDenseMapInfo {
  2198. static CatchPadInst *getEmptyKey() {
  2199. return DenseMapInfo<CatchPadInst *>::getEmptyKey();
  2200. }
  2201. static CatchPadInst *getTombstoneKey() {
  2202. return DenseMapInfo<CatchPadInst *>::getTombstoneKey();
  2203. }
  2204. static unsigned getHashValue(CatchPadInst *CatchPad) {
  2205. return static_cast<unsigned>(hash_combine_range(
  2206. CatchPad->value_op_begin(), CatchPad->value_op_end()));
  2207. }
  2208. static bool isEqual(CatchPadInst *LHS, CatchPadInst *RHS) {
  2209. if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||
  2210. RHS == getEmptyKey() || RHS == getTombstoneKey())
  2211. return LHS == RHS;
  2212. return LHS->isIdenticalTo(RHS);
  2213. }
  2214. };
  2215. SmallDenseMap<BasicBlock *, int, 8> NumPerSuccessorCases;
  2216. // Set of unique CatchPads.
  2217. SmallDenseMap<CatchPadInst *, detail::DenseSetEmpty, 4,
  2218. CatchPadDenseMapInfo, detail::DenseSetPair<CatchPadInst *>>
  2219. HandlerSet;
  2220. detail::DenseSetEmpty Empty;
  2221. for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(),
  2222. E = CatchSwitch->handler_end();
  2223. I != E; ++I) {
  2224. BasicBlock *HandlerBB = *I;
  2225. if (DTU)
  2226. ++NumPerSuccessorCases[HandlerBB];
  2227. auto *CatchPad = cast<CatchPadInst>(HandlerBB->getFirstNonPHI());
  2228. if (!HandlerSet.insert({CatchPad, Empty}).second) {
  2229. if (DTU)
  2230. --NumPerSuccessorCases[HandlerBB];
  2231. CatchSwitch->removeHandler(I);
  2232. --I;
  2233. --E;
  2234. Changed = true;
  2235. }
  2236. }
  2237. if (DTU) {
  2238. std::vector<DominatorTree::UpdateType> Updates;
  2239. for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)
  2240. if (I.second == 0)
  2241. Updates.push_back({DominatorTree::Delete, BB, I.first});
  2242. DTU->applyUpdates(Updates);
  2243. }
  2244. }
  2245. Changed |= ConstantFoldTerminator(BB, true, nullptr, DTU);
  2246. for (BasicBlock *Successor : successors(BB))
  2247. if (Reachable.insert(Successor).second)
  2248. Worklist.push_back(Successor);
  2249. } while (!Worklist.empty());
  2250. return Changed;
  2251. }
  2252. Instruction *llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) {
  2253. Instruction *TI = BB->getTerminator();
  2254. if (auto *II = dyn_cast<InvokeInst>(TI))
  2255. return changeToCall(II, DTU);
  2256. Instruction *NewTI;
  2257. BasicBlock *UnwindDest;
  2258. if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
  2259. NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI);
  2260. UnwindDest = CRI->getUnwindDest();
  2261. } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) {
  2262. auto *NewCatchSwitch = CatchSwitchInst::Create(
  2263. CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),
  2264. CatchSwitch->getName(), CatchSwitch);
  2265. for (BasicBlock *PadBB : CatchSwitch->handlers())
  2266. NewCatchSwitch->addHandler(PadBB);
  2267. NewTI = NewCatchSwitch;
  2268. UnwindDest = CatchSwitch->getUnwindDest();
  2269. } else {
  2270. llvm_unreachable("Could not find unwind successor");
  2271. }
  2272. NewTI->takeName(TI);
  2273. NewTI->setDebugLoc(TI->getDebugLoc());
  2274. UnwindDest->removePredecessor(BB);
  2275. TI->replaceAllUsesWith(NewTI);
  2276. TI->eraseFromParent();
  2277. if (DTU)
  2278. DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}});
  2279. return NewTI;
  2280. }
  2281. /// removeUnreachableBlocks - Remove blocks that are not reachable, even
  2282. /// if they are in a dead cycle. Return true if a change was made, false
  2283. /// otherwise.
  2284. bool llvm::removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
  2285. MemorySSAUpdater *MSSAU) {
  2286. SmallPtrSet<BasicBlock *, 16> Reachable;
  2287. bool Changed = markAliveBlocks(F, Reachable, DTU);
  2288. // If there are unreachable blocks in the CFG...
  2289. if (Reachable.size() == F.size())
  2290. return Changed;
  2291. assert(Reachable.size() < F.size());
  2292. // Are there any blocks left to actually delete?
  2293. SmallSetVector<BasicBlock *, 8> BlocksToRemove;
  2294. for (BasicBlock &BB : F) {
  2295. // Skip reachable basic blocks
  2296. if (Reachable.count(&BB))
  2297. continue;
  2298. // Skip already-deleted blocks
  2299. if (DTU && DTU->isBBPendingDeletion(&BB))
  2300. continue;
  2301. BlocksToRemove.insert(&BB);
  2302. }
  2303. if (BlocksToRemove.empty())
  2304. return Changed;
  2305. Changed = true;
  2306. NumRemoved += BlocksToRemove.size();
  2307. if (MSSAU)
  2308. MSSAU->removeBlocks(BlocksToRemove);
  2309. DeleteDeadBlocks(BlocksToRemove.takeVector(), DTU);
  2310. return Changed;
  2311. }
  2312. void llvm::combineMetadata(Instruction *K, const Instruction *J,
  2313. ArrayRef<unsigned> KnownIDs, bool DoesKMove) {
  2314. SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
  2315. K->dropUnknownNonDebugMetadata(KnownIDs);
  2316. K->getAllMetadataOtherThanDebugLoc(Metadata);
  2317. for (const auto &MD : Metadata) {
  2318. unsigned Kind = MD.first;
  2319. MDNode *JMD = J->getMetadata(Kind);
  2320. MDNode *KMD = MD.second;
  2321. switch (Kind) {
  2322. default:
  2323. K->setMetadata(Kind, nullptr); // Remove unknown metadata
  2324. break;
  2325. case LLVMContext::MD_dbg:
  2326. llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");
  2327. case LLVMContext::MD_DIAssignID:
  2328. K->mergeDIAssignID(J);
  2329. break;
  2330. case LLVMContext::MD_tbaa:
  2331. K->setMetadata(Kind, MDNode::getMostGenericTBAA(JMD, KMD));
  2332. break;
  2333. case LLVMContext::MD_alias_scope:
  2334. K->setMetadata(Kind, MDNode::getMostGenericAliasScope(JMD, KMD));
  2335. break;
  2336. case LLVMContext::MD_noalias:
  2337. case LLVMContext::MD_mem_parallel_loop_access:
  2338. K->setMetadata(Kind, MDNode::intersect(JMD, KMD));
  2339. break;
  2340. case LLVMContext::MD_access_group:
  2341. K->setMetadata(LLVMContext::MD_access_group,
  2342. intersectAccessGroups(K, J));
  2343. break;
  2344. case LLVMContext::MD_range:
  2345. // If K does move, use most generic range. Otherwise keep the range of
  2346. // K.
  2347. if (DoesKMove)
  2348. // FIXME: If K does move, we should drop the range info and nonnull.
  2349. // Currently this function is used with DoesKMove in passes
  2350. // doing hoisting/sinking and the current behavior of using the
  2351. // most generic range is correct in those cases.
  2352. K->setMetadata(Kind, MDNode::getMostGenericRange(JMD, KMD));
  2353. break;
  2354. case LLVMContext::MD_fpmath:
  2355. K->setMetadata(Kind, MDNode::getMostGenericFPMath(JMD, KMD));
  2356. break;
  2357. case LLVMContext::MD_invariant_load:
  2358. // Only set the !invariant.load if it is present in both instructions.
  2359. K->setMetadata(Kind, JMD);
  2360. break;
  2361. case LLVMContext::MD_nonnull:
  2362. // If K does move, keep nonull if it is present in both instructions.
  2363. if (DoesKMove)
  2364. K->setMetadata(Kind, JMD);
  2365. break;
  2366. case LLVMContext::MD_invariant_group:
  2367. // Preserve !invariant.group in K.
  2368. break;
  2369. case LLVMContext::MD_align:
  2370. K->setMetadata(Kind,
  2371. MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
  2372. break;
  2373. case LLVMContext::MD_dereferenceable:
  2374. case LLVMContext::MD_dereferenceable_or_null:
  2375. K->setMetadata(Kind,
  2376. MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
  2377. break;
  2378. case LLVMContext::MD_preserve_access_index:
  2379. // Preserve !preserve.access.index in K.
  2380. break;
  2381. }
  2382. }
  2383. // Set !invariant.group from J if J has it. If both instructions have it
  2384. // then we will just pick it from J - even when they are different.
  2385. // Also make sure that K is load or store - f.e. combining bitcast with load
  2386. // could produce bitcast with invariant.group metadata, which is invalid.
  2387. // FIXME: we should try to preserve both invariant.group md if they are
  2388. // different, but right now instruction can only have one invariant.group.
  2389. if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group))
  2390. if (isa<LoadInst>(K) || isa<StoreInst>(K))
  2391. K->setMetadata(LLVMContext::MD_invariant_group, JMD);
  2392. }
  2393. void llvm::combineMetadataForCSE(Instruction *K, const Instruction *J,
  2394. bool KDominatesJ) {
  2395. unsigned KnownIDs[] = {
  2396. LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
  2397. LLVMContext::MD_noalias, LLVMContext::MD_range,
  2398. LLVMContext::MD_invariant_load, LLVMContext::MD_nonnull,
  2399. LLVMContext::MD_invariant_group, LLVMContext::MD_align,
  2400. LLVMContext::MD_dereferenceable,
  2401. LLVMContext::MD_dereferenceable_or_null,
  2402. LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index};
  2403. combineMetadata(K, J, KnownIDs, KDominatesJ);
  2404. }
  2405. void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) {
  2406. SmallVector<std::pair<unsigned, MDNode *>, 8> MD;
  2407. Source.getAllMetadata(MD);
  2408. MDBuilder MDB(Dest.getContext());
  2409. Type *NewType = Dest.getType();
  2410. const DataLayout &DL = Source.getModule()->getDataLayout();
  2411. for (const auto &MDPair : MD) {
  2412. unsigned ID = MDPair.first;
  2413. MDNode *N = MDPair.second;
  2414. // Note, essentially every kind of metadata should be preserved here! This
  2415. // routine is supposed to clone a load instruction changing *only its type*.
  2416. // The only metadata it makes sense to drop is metadata which is invalidated
  2417. // when the pointer type changes. This should essentially never be the case
  2418. // in LLVM, but we explicitly switch over only known metadata to be
  2419. // conservatively correct. If you are adding metadata to LLVM which pertains
  2420. // to loads, you almost certainly want to add it here.
  2421. switch (ID) {
  2422. case LLVMContext::MD_dbg:
  2423. case LLVMContext::MD_tbaa:
  2424. case LLVMContext::MD_prof:
  2425. case LLVMContext::MD_fpmath:
  2426. case LLVMContext::MD_tbaa_struct:
  2427. case LLVMContext::MD_invariant_load:
  2428. case LLVMContext::MD_alias_scope:
  2429. case LLVMContext::MD_noalias:
  2430. case LLVMContext::MD_nontemporal:
  2431. case LLVMContext::MD_mem_parallel_loop_access:
  2432. case LLVMContext::MD_access_group:
  2433. case LLVMContext::MD_noundef:
  2434. // All of these directly apply.
  2435. Dest.setMetadata(ID, N);
  2436. break;
  2437. case LLVMContext::MD_nonnull:
  2438. copyNonnullMetadata(Source, N, Dest);
  2439. break;
  2440. case LLVMContext::MD_align:
  2441. case LLVMContext::MD_dereferenceable:
  2442. case LLVMContext::MD_dereferenceable_or_null:
  2443. // These only directly apply if the new type is also a pointer.
  2444. if (NewType->isPointerTy())
  2445. Dest.setMetadata(ID, N);
  2446. break;
  2447. case LLVMContext::MD_range:
  2448. copyRangeMetadata(DL, Source, N, Dest);
  2449. break;
  2450. }
  2451. }
  2452. }
  2453. void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {
  2454. auto *ReplInst = dyn_cast<Instruction>(Repl);
  2455. if (!ReplInst)
  2456. return;
  2457. // Patch the replacement so that it is not more restrictive than the value
  2458. // being replaced.
  2459. // Note that if 'I' is a load being replaced by some operation,
  2460. // for example, by an arithmetic operation, then andIRFlags()
  2461. // would just erase all math flags from the original arithmetic
  2462. // operation, which is clearly not wanted and not needed.
  2463. if (!isa<LoadInst>(I))
  2464. ReplInst->andIRFlags(I);
  2465. // FIXME: If both the original and replacement value are part of the
  2466. // same control-flow region (meaning that the execution of one
  2467. // guarantees the execution of the other), then we can combine the
  2468. // noalias scopes here and do better than the general conservative
  2469. // answer used in combineMetadata().
  2470. // In general, GVN unifies expressions over different control-flow
  2471. // regions, and so we need a conservative combination of the noalias
  2472. // scopes.
  2473. static const unsigned KnownIDs[] = {
  2474. LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
  2475. LLVMContext::MD_noalias, LLVMContext::MD_range,
  2476. LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
  2477. LLVMContext::MD_invariant_group, LLVMContext::MD_nonnull,
  2478. LLVMContext::MD_access_group, LLVMContext::MD_preserve_access_index};
  2479. combineMetadata(ReplInst, I, KnownIDs, false);
  2480. }
  2481. template <typename RootType, typename DominatesFn>
  2482. static unsigned replaceDominatedUsesWith(Value *From, Value *To,
  2483. const RootType &Root,
  2484. const DominatesFn &Dominates) {
  2485. assert(From->getType() == To->getType());
  2486. unsigned Count = 0;
  2487. for (Use &U : llvm::make_early_inc_range(From->uses())) {
  2488. if (!Dominates(Root, U))
  2489. continue;
  2490. U.set(To);
  2491. LLVM_DEBUG(dbgs() << "Replace dominated use of '" << From->getName()
  2492. << "' as " << *To << " in " << *U << "\n");
  2493. ++Count;
  2494. }
  2495. return Count;
  2496. }
  2497. unsigned llvm::replaceNonLocalUsesWith(Instruction *From, Value *To) {
  2498. assert(From->getType() == To->getType());
  2499. auto *BB = From->getParent();
  2500. unsigned Count = 0;
  2501. for (Use &U : llvm::make_early_inc_range(From->uses())) {
  2502. auto *I = cast<Instruction>(U.getUser());
  2503. if (I->getParent() == BB)
  2504. continue;
  2505. U.set(To);
  2506. ++Count;
  2507. }
  2508. return Count;
  2509. }
  2510. unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
  2511. DominatorTree &DT,
  2512. const BasicBlockEdge &Root) {
  2513. auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) {
  2514. return DT.dominates(Root, U);
  2515. };
  2516. return ::replaceDominatedUsesWith(From, To, Root, Dominates);
  2517. }
  2518. unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To,
  2519. DominatorTree &DT,
  2520. const BasicBlock *BB) {
  2521. auto Dominates = [&DT](const BasicBlock *BB, const Use &U) {
  2522. return DT.dominates(BB, U);
  2523. };
  2524. return ::replaceDominatedUsesWith(From, To, BB, Dominates);
  2525. }
  2526. bool llvm::callsGCLeafFunction(const CallBase *Call,
  2527. const TargetLibraryInfo &TLI) {
  2528. // Check if the function is specifically marked as a gc leaf function.
  2529. if (Call->hasFnAttr("gc-leaf-function"))
  2530. return true;
  2531. if (const Function *F = Call->getCalledFunction()) {
  2532. if (F->hasFnAttribute("gc-leaf-function"))
  2533. return true;
  2534. if (auto IID = F->getIntrinsicID()) {
  2535. // Most LLVM intrinsics do not take safepoints.
  2536. return IID != Intrinsic::experimental_gc_statepoint &&
  2537. IID != Intrinsic::experimental_deoptimize &&
  2538. IID != Intrinsic::memcpy_element_unordered_atomic &&
  2539. IID != Intrinsic::memmove_element_unordered_atomic;
  2540. }
  2541. }
  2542. // Lib calls can be materialized by some passes, and won't be
  2543. // marked as 'gc-leaf-function.' All available Libcalls are
  2544. // GC-leaf.
  2545. LibFunc LF;
  2546. if (TLI.getLibFunc(*Call, LF)) {
  2547. return TLI.has(LF);
  2548. }
  2549. return false;
  2550. }
  2551. void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N,
  2552. LoadInst &NewLI) {
  2553. auto *NewTy = NewLI.getType();
  2554. // This only directly applies if the new type is also a pointer.
  2555. if (NewTy->isPointerTy()) {
  2556. NewLI.setMetadata(LLVMContext::MD_nonnull, N);
  2557. return;
  2558. }
  2559. // The only other translation we can do is to integral loads with !range
  2560. // metadata.
  2561. if (!NewTy->isIntegerTy())
  2562. return;
  2563. MDBuilder MDB(NewLI.getContext());
  2564. const Value *Ptr = OldLI.getPointerOperand();
  2565. auto *ITy = cast<IntegerType>(NewTy);
  2566. auto *NullInt = ConstantExpr::getPtrToInt(
  2567. ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
  2568. auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
  2569. NewLI.setMetadata(LLVMContext::MD_range,
  2570. MDB.createRange(NonNullInt, NullInt));
  2571. }
  2572. void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI,
  2573. MDNode *N, LoadInst &NewLI) {
  2574. auto *NewTy = NewLI.getType();
  2575. // Simply copy the metadata if the type did not change.
  2576. if (NewTy == OldLI.getType()) {
  2577. NewLI.setMetadata(LLVMContext::MD_range, N);
  2578. return;
  2579. }
  2580. // Give up unless it is converted to a pointer where there is a single very
  2581. // valuable mapping we can do reliably.
  2582. // FIXME: It would be nice to propagate this in more ways, but the type
  2583. // conversions make it hard.
  2584. if (!NewTy->isPointerTy())
  2585. return;
  2586. unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);
  2587. if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
  2588. MDNode *NN = MDNode::get(OldLI.getContext(), std::nullopt);
  2589. NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
  2590. }
  2591. }
  2592. void llvm::dropDebugUsers(Instruction &I) {
  2593. SmallVector<DbgVariableIntrinsic *, 1> DbgUsers;
  2594. findDbgUsers(DbgUsers, &I);
  2595. for (auto *DII : DbgUsers)
  2596. DII->eraseFromParent();
  2597. }
  2598. void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt,
  2599. BasicBlock *BB) {
  2600. // Since we are moving the instructions out of its basic block, we do not
  2601. // retain their original debug locations (DILocations) and debug intrinsic
  2602. // instructions.
  2603. //
  2604. // Doing so would degrade the debugging experience and adversely affect the
  2605. // accuracy of profiling information.
  2606. //
  2607. // Currently, when hoisting the instructions, we take the following actions:
  2608. // - Remove their debug intrinsic instructions.
  2609. // - Set their debug locations to the values from the insertion point.
  2610. //
  2611. // As per PR39141 (comment #8), the more fundamental reason why the dbg.values
  2612. // need to be deleted, is because there will not be any instructions with a
  2613. // DILocation in either branch left after performing the transformation. We
  2614. // can only insert a dbg.value after the two branches are joined again.
  2615. //
  2616. // See PR38762, PR39243 for more details.
  2617. //
  2618. // TODO: Extend llvm.dbg.value to take more than one SSA Value (PR39141) to
  2619. // encode predicated DIExpressions that yield different results on different
  2620. // code paths.
  2621. for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;) {
  2622. Instruction *I = &*II;
  2623. I->dropUndefImplyingAttrsAndUnknownMetadata();
  2624. if (I->isUsedByMetadata())
  2625. dropDebugUsers(*I);
  2626. if (I->isDebugOrPseudoInst()) {
  2627. // Remove DbgInfo and pseudo probe Intrinsics.
  2628. II = I->eraseFromParent();
  2629. continue;
  2630. }
  2631. I->setDebugLoc(InsertPt->getDebugLoc());
  2632. ++II;
  2633. }
  2634. DomBlock->splice(InsertPt->getIterator(), BB, BB->begin(),
  2635. BB->getTerminator()->getIterator());
  2636. }
  2637. namespace {
  2638. /// A potential constituent of a bitreverse or bswap expression. See
  2639. /// collectBitParts for a fuller explanation.
  2640. struct BitPart {
  2641. BitPart(Value *P, unsigned BW) : Provider(P) {
  2642. Provenance.resize(BW);
  2643. }
  2644. /// The Value that this is a bitreverse/bswap of.
  2645. Value *Provider;
  2646. /// The "provenance" of each bit. Provenance[A] = B means that bit A
  2647. /// in Provider becomes bit B in the result of this expression.
  2648. SmallVector<int8_t, 32> Provenance; // int8_t means max size is i128.
  2649. enum { Unset = -1 };
  2650. };
  2651. } // end anonymous namespace
  2652. /// Analyze the specified subexpression and see if it is capable of providing
  2653. /// pieces of a bswap or bitreverse. The subexpression provides a potential
  2654. /// piece of a bswap or bitreverse if it can be proved that each non-zero bit in
  2655. /// the output of the expression came from a corresponding bit in some other
  2656. /// value. This function is recursive, and the end result is a mapping of
  2657. /// bitnumber to bitnumber. It is the caller's responsibility to validate that
  2658. /// the bitnumber to bitnumber mapping is correct for a bswap or bitreverse.
  2659. ///
  2660. /// For example, if the current subexpression if "(shl i32 %X, 24)" then we know
  2661. /// that the expression deposits the low byte of %X into the high byte of the
  2662. /// result and that all other bits are zero. This expression is accepted and a
  2663. /// BitPart is returned with Provider set to %X and Provenance[24-31] set to
  2664. /// [0-7].
  2665. ///
  2666. /// For vector types, all analysis is performed at the per-element level. No
  2667. /// cross-element analysis is supported (shuffle/insertion/reduction), and all
  2668. /// constant masks must be splatted across all elements.
  2669. ///
  2670. /// To avoid revisiting values, the BitPart results are memoized into the
  2671. /// provided map. To avoid unnecessary copying of BitParts, BitParts are
  2672. /// constructed in-place in the \c BPS map. Because of this \c BPS needs to
  2673. /// store BitParts objects, not pointers. As we need the concept of a nullptr
  2674. /// BitParts (Value has been analyzed and the analysis failed), we an Optional
  2675. /// type instead to provide the same functionality.
  2676. ///
  2677. /// Because we pass around references into \c BPS, we must use a container that
  2678. /// does not invalidate internal references (std::map instead of DenseMap).
  2679. static const std::optional<BitPart> &
  2680. collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
  2681. std::map<Value *, std::optional<BitPart>> &BPS, int Depth,
  2682. bool &FoundRoot) {
  2683. auto I = BPS.find(V);
  2684. if (I != BPS.end())
  2685. return I->second;
  2686. auto &Result = BPS[V] = std::nullopt;
  2687. auto BitWidth = V->getType()->getScalarSizeInBits();
  2688. // Can't do integer/elements > 128 bits.
  2689. if (BitWidth > 128)
  2690. return Result;
  2691. // Prevent stack overflow by limiting the recursion depth
  2692. if (Depth == BitPartRecursionMaxDepth) {
  2693. LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
  2694. return Result;
  2695. }
  2696. if (auto *I = dyn_cast<Instruction>(V)) {
  2697. Value *X, *Y;
  2698. const APInt *C;
  2699. // If this is an or instruction, it may be an inner node of the bswap.
  2700. if (match(V, m_Or(m_Value(X), m_Value(Y)))) {
  2701. // Check we have both sources and they are from the same provider.
  2702. const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2703. Depth + 1, FoundRoot);
  2704. if (!A || !A->Provider)
  2705. return Result;
  2706. const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
  2707. Depth + 1, FoundRoot);
  2708. if (!B || A->Provider != B->Provider)
  2709. return Result;
  2710. // Try and merge the two together.
  2711. Result = BitPart(A->Provider, BitWidth);
  2712. for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) {
  2713. if (A->Provenance[BitIdx] != BitPart::Unset &&
  2714. B->Provenance[BitIdx] != BitPart::Unset &&
  2715. A->Provenance[BitIdx] != B->Provenance[BitIdx])
  2716. return Result = std::nullopt;
  2717. if (A->Provenance[BitIdx] == BitPart::Unset)
  2718. Result->Provenance[BitIdx] = B->Provenance[BitIdx];
  2719. else
  2720. Result->Provenance[BitIdx] = A->Provenance[BitIdx];
  2721. }
  2722. return Result;
  2723. }
  2724. // If this is a logical shift by a constant, recurse then shift the result.
  2725. if (match(V, m_LogicalShift(m_Value(X), m_APInt(C)))) {
  2726. const APInt &BitShift = *C;
  2727. // Ensure the shift amount is defined.
  2728. if (BitShift.uge(BitWidth))
  2729. return Result;
  2730. // For bswap-only, limit shift amounts to whole bytes, for an early exit.
  2731. if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0)
  2732. return Result;
  2733. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2734. Depth + 1, FoundRoot);
  2735. if (!Res)
  2736. return Result;
  2737. Result = Res;
  2738. // Perform the "shift" on BitProvenance.
  2739. auto &P = Result->Provenance;
  2740. if (I->getOpcode() == Instruction::Shl) {
  2741. P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end());
  2742. P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset);
  2743. } else {
  2744. P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue()));
  2745. P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset);
  2746. }
  2747. return Result;
  2748. }
  2749. // If this is a logical 'and' with a mask that clears bits, recurse then
  2750. // unset the appropriate bits.
  2751. if (match(V, m_And(m_Value(X), m_APInt(C)))) {
  2752. const APInt &AndMask = *C;
  2753. // Check that the mask allows a multiple of 8 bits for a bswap, for an
  2754. // early exit.
  2755. unsigned NumMaskedBits = AndMask.countPopulation();
  2756. if (!MatchBitReversals && (NumMaskedBits % 8) != 0)
  2757. return Result;
  2758. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2759. Depth + 1, FoundRoot);
  2760. if (!Res)
  2761. return Result;
  2762. Result = Res;
  2763. for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
  2764. // If the AndMask is zero for this bit, clear the bit.
  2765. if (AndMask[BitIdx] == 0)
  2766. Result->Provenance[BitIdx] = BitPart::Unset;
  2767. return Result;
  2768. }
  2769. // If this is a zext instruction zero extend the result.
  2770. if (match(V, m_ZExt(m_Value(X)))) {
  2771. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2772. Depth + 1, FoundRoot);
  2773. if (!Res)
  2774. return Result;
  2775. Result = BitPart(Res->Provider, BitWidth);
  2776. auto NarrowBitWidth = X->getType()->getScalarSizeInBits();
  2777. for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx)
  2778. Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
  2779. for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx)
  2780. Result->Provenance[BitIdx] = BitPart::Unset;
  2781. return Result;
  2782. }
  2783. // If this is a truncate instruction, extract the lower bits.
  2784. if (match(V, m_Trunc(m_Value(X)))) {
  2785. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2786. Depth + 1, FoundRoot);
  2787. if (!Res)
  2788. return Result;
  2789. Result = BitPart(Res->Provider, BitWidth);
  2790. for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
  2791. Result->Provenance[BitIdx] = Res->Provenance[BitIdx];
  2792. return Result;
  2793. }
  2794. // BITREVERSE - most likely due to us previous matching a partial
  2795. // bitreverse.
  2796. if (match(V, m_BitReverse(m_Value(X)))) {
  2797. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2798. Depth + 1, FoundRoot);
  2799. if (!Res)
  2800. return Result;
  2801. Result = BitPart(Res->Provider, BitWidth);
  2802. for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
  2803. Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx];
  2804. return Result;
  2805. }
  2806. // BSWAP - most likely due to us previous matching a partial bswap.
  2807. if (match(V, m_BSwap(m_Value(X)))) {
  2808. const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2809. Depth + 1, FoundRoot);
  2810. if (!Res)
  2811. return Result;
  2812. unsigned ByteWidth = BitWidth / 8;
  2813. Result = BitPart(Res->Provider, BitWidth);
  2814. for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) {
  2815. unsigned ByteBitOfs = ByteIdx * 8;
  2816. for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx)
  2817. Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] =
  2818. Res->Provenance[ByteBitOfs + BitIdx];
  2819. }
  2820. return Result;
  2821. }
  2822. // Funnel 'double' shifts take 3 operands, 2 inputs and the shift
  2823. // amount (modulo).
  2824. // fshl(X,Y,Z): (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
  2825. // fshr(X,Y,Z): (X << (BW - (Z % BW))) | (Y >> (Z % BW))
  2826. if (match(V, m_FShl(m_Value(X), m_Value(Y), m_APInt(C))) ||
  2827. match(V, m_FShr(m_Value(X), m_Value(Y), m_APInt(C)))) {
  2828. // We can treat fshr as a fshl by flipping the modulo amount.
  2829. unsigned ModAmt = C->urem(BitWidth);
  2830. if (cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fshr)
  2831. ModAmt = BitWidth - ModAmt;
  2832. // For bswap-only, limit shift amounts to whole bytes, for an early exit.
  2833. if (!MatchBitReversals && (ModAmt % 8) != 0)
  2834. return Result;
  2835. // Check we have both sources and they are from the same provider.
  2836. const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,
  2837. Depth + 1, FoundRoot);
  2838. if (!LHS || !LHS->Provider)
  2839. return Result;
  2840. const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,
  2841. Depth + 1, FoundRoot);
  2842. if (!RHS || LHS->Provider != RHS->Provider)
  2843. return Result;
  2844. unsigned StartBitRHS = BitWidth - ModAmt;
  2845. Result = BitPart(LHS->Provider, BitWidth);
  2846. for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx)
  2847. Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx];
  2848. for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx)
  2849. Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS];
  2850. return Result;
  2851. }
  2852. }
  2853. // If we've already found a root input value then we're never going to merge
  2854. // these back together.
  2855. if (FoundRoot)
  2856. return Result;
  2857. // Okay, we got to something that isn't a shift, 'or', 'and', etc. This must
  2858. // be the root input value to the bswap/bitreverse.
  2859. FoundRoot = true;
  2860. Result = BitPart(V, BitWidth);
  2861. for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)
  2862. Result->Provenance[BitIdx] = BitIdx;
  2863. return Result;
  2864. }
  2865. static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To,
  2866. unsigned BitWidth) {
  2867. if (From % 8 != To % 8)
  2868. return false;
  2869. // Convert from bit indices to byte indices and check for a byte reversal.
  2870. From >>= 3;
  2871. To >>= 3;
  2872. BitWidth >>= 3;
  2873. return From == BitWidth - To - 1;
  2874. }
  2875. static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To,
  2876. unsigned BitWidth) {
  2877. return From == BitWidth - To - 1;
  2878. }
  2879. bool llvm::recognizeBSwapOrBitReverseIdiom(
  2880. Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
  2881. SmallVectorImpl<Instruction *> &InsertedInsts) {
  2882. if (!match(I, m_Or(m_Value(), m_Value())) &&
  2883. !match(I, m_FShl(m_Value(), m_Value(), m_Value())) &&
  2884. !match(I, m_FShr(m_Value(), m_Value(), m_Value())))
  2885. return false;
  2886. if (!MatchBSwaps && !MatchBitReversals)
  2887. return false;
  2888. Type *ITy = I->getType();
  2889. if (!ITy->isIntOrIntVectorTy() || ITy->getScalarSizeInBits() > 128)
  2890. return false; // Can't do integer/elements > 128 bits.
  2891. // Try to find all the pieces corresponding to the bswap.
  2892. bool FoundRoot = false;
  2893. std::map<Value *, std::optional<BitPart>> BPS;
  2894. const auto &Res =
  2895. collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot);
  2896. if (!Res)
  2897. return false;
  2898. ArrayRef<int8_t> BitProvenance = Res->Provenance;
  2899. assert(all_of(BitProvenance,
  2900. [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) &&
  2901. "Illegal bit provenance index");
  2902. // If the upper bits are zero, then attempt to perform as a truncated op.
  2903. Type *DemandedTy = ITy;
  2904. if (BitProvenance.back() == BitPart::Unset) {
  2905. while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset)
  2906. BitProvenance = BitProvenance.drop_back();
  2907. if (BitProvenance.empty())
  2908. return false; // TODO - handle null value?
  2909. DemandedTy = Type::getIntNTy(I->getContext(), BitProvenance.size());
  2910. if (auto *IVecTy = dyn_cast<VectorType>(ITy))
  2911. DemandedTy = VectorType::get(DemandedTy, IVecTy);
  2912. }
  2913. // Check BitProvenance hasn't found a source larger than the result type.
  2914. unsigned DemandedBW = DemandedTy->getScalarSizeInBits();
  2915. if (DemandedBW > ITy->getScalarSizeInBits())
  2916. return false;
  2917. // Now, is the bit permutation correct for a bswap or a bitreverse? We can
  2918. // only byteswap values with an even number of bytes.
  2919. APInt DemandedMask = APInt::getAllOnes(DemandedBW);
  2920. bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0;
  2921. bool OKForBitReverse = MatchBitReversals;
  2922. for (unsigned BitIdx = 0;
  2923. (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) {
  2924. if (BitProvenance[BitIdx] == BitPart::Unset) {
  2925. DemandedMask.clearBit(BitIdx);
  2926. continue;
  2927. }
  2928. OKForBSwap &= bitTransformIsCorrectForBSwap(BitProvenance[BitIdx], BitIdx,
  2929. DemandedBW);
  2930. OKForBitReverse &= bitTransformIsCorrectForBitReverse(BitProvenance[BitIdx],
  2931. BitIdx, DemandedBW);
  2932. }
  2933. Intrinsic::ID Intrin;
  2934. if (OKForBSwap)
  2935. Intrin = Intrinsic::bswap;
  2936. else if (OKForBitReverse)
  2937. Intrin = Intrinsic::bitreverse;
  2938. else
  2939. return false;
  2940. Function *F = Intrinsic::getDeclaration(I->getModule(), Intrin, DemandedTy);
  2941. Value *Provider = Res->Provider;
  2942. // We may need to truncate the provider.
  2943. if (DemandedTy != Provider->getType()) {
  2944. auto *Trunc =
  2945. CastInst::CreateIntegerCast(Provider, DemandedTy, false, "trunc", I);
  2946. InsertedInsts.push_back(Trunc);
  2947. Provider = Trunc;
  2948. }
  2949. Instruction *Result = CallInst::Create(F, Provider, "rev", I);
  2950. InsertedInsts.push_back(Result);
  2951. if (!DemandedMask.isAllOnes()) {
  2952. auto *Mask = ConstantInt::get(DemandedTy, DemandedMask);
  2953. Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I);
  2954. InsertedInsts.push_back(Result);
  2955. }
  2956. // We may need to zeroextend back to the result type.
  2957. if (ITy != Result->getType()) {
  2958. auto *ExtInst = CastInst::CreateIntegerCast(Result, ITy, false, "zext", I);
  2959. InsertedInsts.push_back(ExtInst);
  2960. }
  2961. return true;
  2962. }
  2963. // CodeGen has special handling for some string functions that may replace
  2964. // them with target-specific intrinsics. Since that'd skip our interceptors
  2965. // in ASan/MSan/TSan/DFSan, and thus make us miss some memory accesses,
  2966. // we mark affected calls as NoBuiltin, which will disable optimization
  2967. // in CodeGen.
  2968. void llvm::maybeMarkSanitizerLibraryCallNoBuiltin(
  2969. CallInst *CI, const TargetLibraryInfo *TLI) {
  2970. Function *F = CI->getCalledFunction();
  2971. LibFunc Func;
  2972. if (F && !F->hasLocalLinkage() && F->hasName() &&
  2973. TLI->getLibFunc(F->getName(), Func) && TLI->hasOptimizedCodeGen(Func) &&
  2974. !F->doesNotAccessMemory())
  2975. CI->addFnAttr(Attribute::NoBuiltin);
  2976. }
  2977. bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) {
  2978. // We can't have a PHI with a metadata type.
  2979. if (I->getOperand(OpIdx)->getType()->isMetadataTy())
  2980. return false;
  2981. // Early exit.
  2982. if (!isa<Constant>(I->getOperand(OpIdx)))
  2983. return true;
  2984. switch (I->getOpcode()) {
  2985. default:
  2986. return true;
  2987. case Instruction::Call:
  2988. case Instruction::Invoke: {
  2989. const auto &CB = cast<CallBase>(*I);
  2990. // Can't handle inline asm. Skip it.
  2991. if (CB.isInlineAsm())
  2992. return false;
  2993. // Constant bundle operands may need to retain their constant-ness for
  2994. // correctness.
  2995. if (CB.isBundleOperand(OpIdx))
  2996. return false;
  2997. if (OpIdx < CB.arg_size()) {
  2998. // Some variadic intrinsics require constants in the variadic arguments,
  2999. // which currently aren't markable as immarg.
  3000. if (isa<IntrinsicInst>(CB) &&
  3001. OpIdx >= CB.getFunctionType()->getNumParams()) {
  3002. // This is known to be OK for stackmap.
  3003. return CB.getIntrinsicID() == Intrinsic::experimental_stackmap;
  3004. }
  3005. // gcroot is a special case, since it requires a constant argument which
  3006. // isn't also required to be a simple ConstantInt.
  3007. if (CB.getIntrinsicID() == Intrinsic::gcroot)
  3008. return false;
  3009. // Some intrinsic operands are required to be immediates.
  3010. return !CB.paramHasAttr(OpIdx, Attribute::ImmArg);
  3011. }
  3012. // It is never allowed to replace the call argument to an intrinsic, but it
  3013. // may be possible for a call.
  3014. return !isa<IntrinsicInst>(CB);
  3015. }
  3016. case Instruction::ShuffleVector:
  3017. // Shufflevector masks are constant.
  3018. return OpIdx != 2;
  3019. case Instruction::Switch:
  3020. case Instruction::ExtractValue:
  3021. // All operands apart from the first are constant.
  3022. return OpIdx == 0;
  3023. case Instruction::InsertValue:
  3024. // All operands apart from the first and the second are constant.
  3025. return OpIdx < 2;
  3026. case Instruction::Alloca:
  3027. // Static allocas (constant size in the entry block) are handled by
  3028. // prologue/epilogue insertion so they're free anyway. We definitely don't
  3029. // want to make them non-constant.
  3030. return !cast<AllocaInst>(I)->isStaticAlloca();
  3031. case Instruction::GetElementPtr:
  3032. if (OpIdx == 0)
  3033. return true;
  3034. gep_type_iterator It = gep_type_begin(I);
  3035. for (auto E = std::next(It, OpIdx); It != E; ++It)
  3036. if (It.isStruct())
  3037. return false;
  3038. return true;
  3039. }
  3040. }
  3041. Value *llvm::invertCondition(Value *Condition) {
  3042. // First: Check if it's a constant
  3043. if (Constant *C = dyn_cast<Constant>(Condition))
  3044. return ConstantExpr::getNot(C);
  3045. // Second: If the condition is already inverted, return the original value
  3046. Value *NotCondition;
  3047. if (match(Condition, m_Not(m_Value(NotCondition))))
  3048. return NotCondition;
  3049. BasicBlock *Parent = nullptr;
  3050. Instruction *Inst = dyn_cast<Instruction>(Condition);
  3051. if (Inst)
  3052. Parent = Inst->getParent();
  3053. else if (Argument *Arg = dyn_cast<Argument>(Condition))
  3054. Parent = &Arg->getParent()->getEntryBlock();
  3055. assert(Parent && "Unsupported condition to invert");
  3056. // Third: Check all the users for an invert
  3057. for (User *U : Condition->users())
  3058. if (Instruction *I = dyn_cast<Instruction>(U))
  3059. if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
  3060. return I;
  3061. // Last option: Create a new instruction
  3062. auto *Inverted =
  3063. BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv");
  3064. if (Inst && !isa<PHINode>(Inst))
  3065. Inverted->insertAfter(Inst);
  3066. else
  3067. Inverted->insertBefore(&*Parent->getFirstInsertionPt());
  3068. return Inverted;
  3069. }
  3070. bool llvm::inferAttributesFromOthers(Function &F) {
  3071. // Note: We explicitly check for attributes rather than using cover functions
  3072. // because some of the cover functions include the logic being implemented.
  3073. bool Changed = false;
  3074. // readnone + not convergent implies nosync
  3075. if (!F.hasFnAttribute(Attribute::NoSync) &&
  3076. F.doesNotAccessMemory() && !F.isConvergent()) {
  3077. F.setNoSync();
  3078. Changed = true;
  3079. }
  3080. // readonly implies nofree
  3081. if (!F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) {
  3082. F.setDoesNotFreeMemory();
  3083. Changed = true;
  3084. }
  3085. // willreturn implies mustprogress
  3086. if (!F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) {
  3087. F.setMustProgress();
  3088. Changed = true;
  3089. }
  3090. // TODO: There are a bunch of cases of restrictive memory effects we
  3091. // can infer by inspecting arguments of argmemonly-ish functions.
  3092. return Changed;
  3093. }