DependenceAnalysis.cpp 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201
  1. //===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===//
  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. // DependenceAnalysis is an LLVM pass that analyses dependences between memory
  10. // accesses. Currently, it is an (incomplete) implementation of the approach
  11. // described in
  12. //
  13. // Practical Dependence Testing
  14. // Goff, Kennedy, Tseng
  15. // PLDI 1991
  16. //
  17. // There's a single entry point that analyzes the dependence between a pair
  18. // of memory references in a function, returning either NULL, for no dependence,
  19. // or a more-or-less detailed description of the dependence between them.
  20. //
  21. // Currently, the implementation cannot propagate constraints between
  22. // coupled RDIV subscripts and lacks a multi-subscript MIV test.
  23. // Both of these are conservative weaknesses;
  24. // that is, not a source of correctness problems.
  25. //
  26. // Since Clang linearizes some array subscripts, the dependence
  27. // analysis is using SCEV->delinearize to recover the representation of multiple
  28. // subscripts, and thus avoid the more expensive and less precise MIV tests. The
  29. // delinearization is controlled by the flag -da-delinearize.
  30. //
  31. // We should pay some careful attention to the possibility of integer overflow
  32. // in the implementation of the various tests. This could happen with Add,
  33. // Subtract, or Multiply, with both APInt's and SCEV's.
  34. //
  35. // Some non-linear subscript pairs can be handled by the GCD test
  36. // (and perhaps other tests).
  37. // Should explore how often these things occur.
  38. //
  39. // Finally, it seems like certain test cases expose weaknesses in the SCEV
  40. // simplification, especially in the handling of sign and zero extensions.
  41. // It could be useful to spend time exploring these.
  42. //
  43. // Please note that this is work in progress and the interface is subject to
  44. // change.
  45. //
  46. //===----------------------------------------------------------------------===//
  47. // //
  48. // In memory of Ken Kennedy, 1945 - 2007 //
  49. // //
  50. //===----------------------------------------------------------------------===//
  51. #include "llvm/Analysis/DependenceAnalysis.h"
  52. #include "llvm/ADT/Statistic.h"
  53. #include "llvm/Analysis/AliasAnalysis.h"
  54. #include "llvm/Analysis/Delinearization.h"
  55. #include "llvm/Analysis/LoopInfo.h"
  56. #include "llvm/Analysis/ScalarEvolution.h"
  57. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  58. #include "llvm/Analysis/ValueTracking.h"
  59. #include "llvm/IR/InstIterator.h"
  60. #include "llvm/IR/Module.h"
  61. #include "llvm/InitializePasses.h"
  62. #include "llvm/Support/CommandLine.h"
  63. #include "llvm/Support/Debug.h"
  64. #include "llvm/Support/ErrorHandling.h"
  65. #include "llvm/Support/raw_ostream.h"
  66. using namespace llvm;
  67. #define DEBUG_TYPE "da"
  68. //===----------------------------------------------------------------------===//
  69. // statistics
  70. STATISTIC(TotalArrayPairs, "Array pairs tested");
  71. STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs");
  72. STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs");
  73. STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs");
  74. STATISTIC(ZIVapplications, "ZIV applications");
  75. STATISTIC(ZIVindependence, "ZIV independence");
  76. STATISTIC(StrongSIVapplications, "Strong SIV applications");
  77. STATISTIC(StrongSIVsuccesses, "Strong SIV successes");
  78. STATISTIC(StrongSIVindependence, "Strong SIV independence");
  79. STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications");
  80. STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes");
  81. STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence");
  82. STATISTIC(ExactSIVapplications, "Exact SIV applications");
  83. STATISTIC(ExactSIVsuccesses, "Exact SIV successes");
  84. STATISTIC(ExactSIVindependence, "Exact SIV independence");
  85. STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications");
  86. STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes");
  87. STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence");
  88. STATISTIC(ExactRDIVapplications, "Exact RDIV applications");
  89. STATISTIC(ExactRDIVindependence, "Exact RDIV independence");
  90. STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications");
  91. STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence");
  92. STATISTIC(DeltaApplications, "Delta applications");
  93. STATISTIC(DeltaSuccesses, "Delta successes");
  94. STATISTIC(DeltaIndependence, "Delta independence");
  95. STATISTIC(DeltaPropagations, "Delta propagations");
  96. STATISTIC(GCDapplications, "GCD applications");
  97. STATISTIC(GCDsuccesses, "GCD successes");
  98. STATISTIC(GCDindependence, "GCD independence");
  99. STATISTIC(BanerjeeApplications, "Banerjee applications");
  100. STATISTIC(BanerjeeIndependence, "Banerjee independence");
  101. STATISTIC(BanerjeeSuccesses, "Banerjee successes");
  102. static cl::opt<bool>
  103. Delinearize("da-delinearize", cl::init(true), cl::Hidden,
  104. cl::desc("Try to delinearize array references."));
  105. static cl::opt<bool> DisableDelinearizationChecks(
  106. "da-disable-delinearization-checks", cl::Hidden,
  107. cl::desc(
  108. "Disable checks that try to statically verify validity of "
  109. "delinearized subscripts. Enabling this option may result in incorrect "
  110. "dependence vectors for languages that allow the subscript of one "
  111. "dimension to underflow or overflow into another dimension."));
  112. static cl::opt<unsigned> MIVMaxLevelThreshold(
  113. "da-miv-max-level-threshold", cl::init(7), cl::Hidden,
  114. cl::desc("Maximum depth allowed for the recursive algorithm used to "
  115. "explore MIV direction vectors."));
  116. //===----------------------------------------------------------------------===//
  117. // basics
  118. DependenceAnalysis::Result
  119. DependenceAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
  120. auto &AA = FAM.getResult<AAManager>(F);
  121. auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
  122. auto &LI = FAM.getResult<LoopAnalysis>(F);
  123. return DependenceInfo(&F, &AA, &SE, &LI);
  124. }
  125. AnalysisKey DependenceAnalysis::Key;
  126. INITIALIZE_PASS_BEGIN(DependenceAnalysisWrapperPass, "da",
  127. "Dependence Analysis", true, true)
  128. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  129. INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
  130. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  131. INITIALIZE_PASS_END(DependenceAnalysisWrapperPass, "da", "Dependence Analysis",
  132. true, true)
  133. char DependenceAnalysisWrapperPass::ID = 0;
  134. DependenceAnalysisWrapperPass::DependenceAnalysisWrapperPass()
  135. : FunctionPass(ID) {
  136. initializeDependenceAnalysisWrapperPassPass(*PassRegistry::getPassRegistry());
  137. }
  138. FunctionPass *llvm::createDependenceAnalysisWrapperPass() {
  139. return new DependenceAnalysisWrapperPass();
  140. }
  141. bool DependenceAnalysisWrapperPass::runOnFunction(Function &F) {
  142. auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
  143. auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  144. auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  145. info.reset(new DependenceInfo(&F, &AA, &SE, &LI));
  146. return false;
  147. }
  148. DependenceInfo &DependenceAnalysisWrapperPass::getDI() const { return *info; }
  149. void DependenceAnalysisWrapperPass::releaseMemory() { info.reset(); }
  150. void DependenceAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  151. AU.setPreservesAll();
  152. AU.addRequiredTransitive<AAResultsWrapperPass>();
  153. AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
  154. AU.addRequiredTransitive<LoopInfoWrapperPass>();
  155. }
  156. // Used to test the dependence analyzer.
  157. // Looks through the function, noting instructions that may access memory.
  158. // Calls depends() on every possible pair and prints out the result.
  159. // Ignores all other instructions.
  160. static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA,
  161. ScalarEvolution &SE, bool NormalizeResults) {
  162. auto *F = DA->getFunction();
  163. for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); SrcI != SrcE;
  164. ++SrcI) {
  165. if (SrcI->mayReadOrWriteMemory()) {
  166. for (inst_iterator DstI = SrcI, DstE = inst_end(F);
  167. DstI != DstE; ++DstI) {
  168. if (DstI->mayReadOrWriteMemory()) {
  169. OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n";
  170. OS << " da analyze - ";
  171. if (auto D = DA->depends(&*SrcI, &*DstI, true)) {
  172. // Normalize negative direction vectors if required by clients.
  173. if (NormalizeResults && D->normalize(&SE))
  174. OS << "normalized - ";
  175. D->dump(OS);
  176. for (unsigned Level = 1; Level <= D->getLevels(); Level++) {
  177. if (D->isSplitable(Level)) {
  178. OS << " da analyze - split level = " << Level;
  179. OS << ", iteration = " << *DA->getSplitIteration(*D, Level);
  180. OS << "!\n";
  181. }
  182. }
  183. }
  184. else
  185. OS << "none!\n";
  186. }
  187. }
  188. }
  189. }
  190. }
  191. void DependenceAnalysisWrapperPass::print(raw_ostream &OS,
  192. const Module *) const {
  193. dumpExampleDependence(OS, info.get(),
  194. getAnalysis<ScalarEvolutionWrapperPass>().getSE(), false);
  195. }
  196. PreservedAnalyses
  197. DependenceAnalysisPrinterPass::run(Function &F, FunctionAnalysisManager &FAM) {
  198. OS << "'Dependence Analysis' for function '" << F.getName() << "':\n";
  199. dumpExampleDependence(OS, &FAM.getResult<DependenceAnalysis>(F),
  200. FAM.getResult<ScalarEvolutionAnalysis>(F),
  201. NormalizeResults);
  202. return PreservedAnalyses::all();
  203. }
  204. //===----------------------------------------------------------------------===//
  205. // Dependence methods
  206. // Returns true if this is an input dependence.
  207. bool Dependence::isInput() const {
  208. return Src->mayReadFromMemory() && Dst->mayReadFromMemory();
  209. }
  210. // Returns true if this is an output dependence.
  211. bool Dependence::isOutput() const {
  212. return Src->mayWriteToMemory() && Dst->mayWriteToMemory();
  213. }
  214. // Returns true if this is an flow (aka true) dependence.
  215. bool Dependence::isFlow() const {
  216. return Src->mayWriteToMemory() && Dst->mayReadFromMemory();
  217. }
  218. // Returns true if this is an anti dependence.
  219. bool Dependence::isAnti() const {
  220. return Src->mayReadFromMemory() && Dst->mayWriteToMemory();
  221. }
  222. // Returns true if a particular level is scalar; that is,
  223. // if no subscript in the source or destination mention the induction
  224. // variable associated with the loop at this level.
  225. // Leave this out of line, so it will serve as a virtual method anchor
  226. bool Dependence::isScalar(unsigned level) const {
  227. return false;
  228. }
  229. //===----------------------------------------------------------------------===//
  230. // FullDependence methods
  231. FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
  232. bool PossiblyLoopIndependent,
  233. unsigned CommonLevels)
  234. : Dependence(Source, Destination), Levels(CommonLevels),
  235. LoopIndependent(PossiblyLoopIndependent) {
  236. Consistent = true;
  237. if (CommonLevels)
  238. DV = std::make_unique<DVEntry[]>(CommonLevels);
  239. }
  240. // FIXME: in some cases the meaning of a negative direction vector
  241. // may not be straightforward, e.g.,
  242. // for (int i = 0; i < 32; ++i) {
  243. // Src: A[i] = ...;
  244. // Dst: use(A[31 - i]);
  245. // }
  246. // The dependency is
  247. // flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
  248. // anti { Dst[i] -> Src[31 - i] : when i < 16 },
  249. // -- hence a [<>].
  250. // As long as a dependence result contains '>' ('<>', '<=>', "*"), it
  251. // means that a reversed/normalized dependence needs to be considered
  252. // as well. Nevertheless, current isDirectionNegative() only returns
  253. // true with a '>' or '>=' dependency for ease of canonicalizing the
  254. // dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
  255. bool FullDependence::isDirectionNegative() const {
  256. for (unsigned Level = 1; Level <= Levels; ++Level) {
  257. unsigned char Direction = DV[Level - 1].Direction;
  258. if (Direction == Dependence::DVEntry::EQ)
  259. continue;
  260. if (Direction == Dependence::DVEntry::GT ||
  261. Direction == Dependence::DVEntry::GE)
  262. return true;
  263. return false;
  264. }
  265. return false;
  266. }
  267. bool FullDependence::normalize(ScalarEvolution *SE) {
  268. if (!isDirectionNegative())
  269. return false;
  270. LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
  271. dump(dbgs()););
  272. std::swap(Src, Dst);
  273. for (unsigned Level = 1; Level <= Levels; ++Level) {
  274. unsigned char Direction = DV[Level - 1].Direction;
  275. // Reverse the direction vector, this means LT becomes GT
  276. // and GT becomes LT.
  277. unsigned char RevDirection = Direction & Dependence::DVEntry::EQ;
  278. if (Direction & Dependence::DVEntry::LT)
  279. RevDirection |= Dependence::DVEntry::GT;
  280. if (Direction & Dependence::DVEntry::GT)
  281. RevDirection |= Dependence::DVEntry::LT;
  282. DV[Level - 1].Direction = RevDirection;
  283. // Reverse the dependence distance as well.
  284. if (DV[Level - 1].Distance != nullptr)
  285. DV[Level - 1].Distance =
  286. SE->getNegativeSCEV(DV[Level - 1].Distance);
  287. }
  288. LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
  289. dump(dbgs()););
  290. return true;
  291. }
  292. // The rest are simple getters that hide the implementation.
  293. // getDirection - Returns the direction associated with a particular level.
  294. unsigned FullDependence::getDirection(unsigned Level) const {
  295. assert(0 < Level && Level <= Levels && "Level out of range");
  296. return DV[Level - 1].Direction;
  297. }
  298. // Returns the distance (or NULL) associated with a particular level.
  299. const SCEV *FullDependence::getDistance(unsigned Level) const {
  300. assert(0 < Level && Level <= Levels && "Level out of range");
  301. return DV[Level - 1].Distance;
  302. }
  303. // Returns true if a particular level is scalar; that is,
  304. // if no subscript in the source or destination mention the induction
  305. // variable associated with the loop at this level.
  306. bool FullDependence::isScalar(unsigned Level) const {
  307. assert(0 < Level && Level <= Levels && "Level out of range");
  308. return DV[Level - 1].Scalar;
  309. }
  310. // Returns true if peeling the first iteration from this loop
  311. // will break this dependence.
  312. bool FullDependence::isPeelFirst(unsigned Level) const {
  313. assert(0 < Level && Level <= Levels && "Level out of range");
  314. return DV[Level - 1].PeelFirst;
  315. }
  316. // Returns true if peeling the last iteration from this loop
  317. // will break this dependence.
  318. bool FullDependence::isPeelLast(unsigned Level) const {
  319. assert(0 < Level && Level <= Levels && "Level out of range");
  320. return DV[Level - 1].PeelLast;
  321. }
  322. // Returns true if splitting this loop will break the dependence.
  323. bool FullDependence::isSplitable(unsigned Level) const {
  324. assert(0 < Level && Level <= Levels && "Level out of range");
  325. return DV[Level - 1].Splitable;
  326. }
  327. //===----------------------------------------------------------------------===//
  328. // DependenceInfo::Constraint methods
  329. // If constraint is a point <X, Y>, returns X.
  330. // Otherwise assert.
  331. const SCEV *DependenceInfo::Constraint::getX() const {
  332. assert(Kind == Point && "Kind should be Point");
  333. return A;
  334. }
  335. // If constraint is a point <X, Y>, returns Y.
  336. // Otherwise assert.
  337. const SCEV *DependenceInfo::Constraint::getY() const {
  338. assert(Kind == Point && "Kind should be Point");
  339. return B;
  340. }
  341. // If constraint is a line AX + BY = C, returns A.
  342. // Otherwise assert.
  343. const SCEV *DependenceInfo::Constraint::getA() const {
  344. assert((Kind == Line || Kind == Distance) &&
  345. "Kind should be Line (or Distance)");
  346. return A;
  347. }
  348. // If constraint is a line AX + BY = C, returns B.
  349. // Otherwise assert.
  350. const SCEV *DependenceInfo::Constraint::getB() const {
  351. assert((Kind == Line || Kind == Distance) &&
  352. "Kind should be Line (or Distance)");
  353. return B;
  354. }
  355. // If constraint is a line AX + BY = C, returns C.
  356. // Otherwise assert.
  357. const SCEV *DependenceInfo::Constraint::getC() const {
  358. assert((Kind == Line || Kind == Distance) &&
  359. "Kind should be Line (or Distance)");
  360. return C;
  361. }
  362. // If constraint is a distance, returns D.
  363. // Otherwise assert.
  364. const SCEV *DependenceInfo::Constraint::getD() const {
  365. assert(Kind == Distance && "Kind should be Distance");
  366. return SE->getNegativeSCEV(C);
  367. }
  368. // Returns the loop associated with this constraint.
  369. const Loop *DependenceInfo::Constraint::getAssociatedLoop() const {
  370. assert((Kind == Distance || Kind == Line || Kind == Point) &&
  371. "Kind should be Distance, Line, or Point");
  372. return AssociatedLoop;
  373. }
  374. void DependenceInfo::Constraint::setPoint(const SCEV *X, const SCEV *Y,
  375. const Loop *CurLoop) {
  376. Kind = Point;
  377. A = X;
  378. B = Y;
  379. AssociatedLoop = CurLoop;
  380. }
  381. void DependenceInfo::Constraint::setLine(const SCEV *AA, const SCEV *BB,
  382. const SCEV *CC, const Loop *CurLoop) {
  383. Kind = Line;
  384. A = AA;
  385. B = BB;
  386. C = CC;
  387. AssociatedLoop = CurLoop;
  388. }
  389. void DependenceInfo::Constraint::setDistance(const SCEV *D,
  390. const Loop *CurLoop) {
  391. Kind = Distance;
  392. A = SE->getOne(D->getType());
  393. B = SE->getNegativeSCEV(A);
  394. C = SE->getNegativeSCEV(D);
  395. AssociatedLoop = CurLoop;
  396. }
  397. void DependenceInfo::Constraint::setEmpty() { Kind = Empty; }
  398. void DependenceInfo::Constraint::setAny(ScalarEvolution *NewSE) {
  399. SE = NewSE;
  400. Kind = Any;
  401. }
  402. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  403. // For debugging purposes. Dumps the constraint out to OS.
  404. LLVM_DUMP_METHOD void DependenceInfo::Constraint::dump(raw_ostream &OS) const {
  405. if (isEmpty())
  406. OS << " Empty\n";
  407. else if (isAny())
  408. OS << " Any\n";
  409. else if (isPoint())
  410. OS << " Point is <" << *getX() << ", " << *getY() << ">\n";
  411. else if (isDistance())
  412. OS << " Distance is " << *getD() <<
  413. " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n";
  414. else if (isLine())
  415. OS << " Line is " << *getA() << "*X + " <<
  416. *getB() << "*Y = " << *getC() << "\n";
  417. else
  418. llvm_unreachable("unknown constraint type in Constraint::dump");
  419. }
  420. #endif
  421. // Updates X with the intersection
  422. // of the Constraints X and Y. Returns true if X has changed.
  423. // Corresponds to Figure 4 from the paper
  424. //
  425. // Practical Dependence Testing
  426. // Goff, Kennedy, Tseng
  427. // PLDI 1991
  428. bool DependenceInfo::intersectConstraints(Constraint *X, const Constraint *Y) {
  429. ++DeltaApplications;
  430. LLVM_DEBUG(dbgs() << "\tintersect constraints\n");
  431. LLVM_DEBUG(dbgs() << "\t X ="; X->dump(dbgs()));
  432. LLVM_DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs()));
  433. assert(!Y->isPoint() && "Y must not be a Point");
  434. if (X->isAny()) {
  435. if (Y->isAny())
  436. return false;
  437. *X = *Y;
  438. return true;
  439. }
  440. if (X->isEmpty())
  441. return false;
  442. if (Y->isEmpty()) {
  443. X->setEmpty();
  444. return true;
  445. }
  446. if (X->isDistance() && Y->isDistance()) {
  447. LLVM_DEBUG(dbgs() << "\t intersect 2 distances\n");
  448. if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD()))
  449. return false;
  450. if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) {
  451. X->setEmpty();
  452. ++DeltaSuccesses;
  453. return true;
  454. }
  455. // Hmmm, interesting situation.
  456. // I guess if either is constant, keep it and ignore the other.
  457. if (isa<SCEVConstant>(Y->getD())) {
  458. *X = *Y;
  459. return true;
  460. }
  461. return false;
  462. }
  463. // At this point, the pseudo-code in Figure 4 of the paper
  464. // checks if (X->isPoint() && Y->isPoint()).
  465. // This case can't occur in our implementation,
  466. // since a Point can only arise as the result of intersecting
  467. // two Line constraints, and the right-hand value, Y, is never
  468. // the result of an intersection.
  469. assert(!(X->isPoint() && Y->isPoint()) &&
  470. "We shouldn't ever see X->isPoint() && Y->isPoint()");
  471. if (X->isLine() && Y->isLine()) {
  472. LLVM_DEBUG(dbgs() << "\t intersect 2 lines\n");
  473. const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB());
  474. const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA());
  475. if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) {
  476. // slopes are equal, so lines are parallel
  477. LLVM_DEBUG(dbgs() << "\t\tsame slope\n");
  478. Prod1 = SE->getMulExpr(X->getC(), Y->getB());
  479. Prod2 = SE->getMulExpr(X->getB(), Y->getC());
  480. if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2))
  481. return false;
  482. if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
  483. X->setEmpty();
  484. ++DeltaSuccesses;
  485. return true;
  486. }
  487. return false;
  488. }
  489. if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) {
  490. // slopes differ, so lines intersect
  491. LLVM_DEBUG(dbgs() << "\t\tdifferent slopes\n");
  492. const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB());
  493. const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA());
  494. const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB());
  495. const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA());
  496. const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB());
  497. const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB());
  498. const SCEVConstant *C1A2_C2A1 =
  499. dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1));
  500. const SCEVConstant *C1B2_C2B1 =
  501. dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1));
  502. const SCEVConstant *A1B2_A2B1 =
  503. dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1));
  504. const SCEVConstant *A2B1_A1B2 =
  505. dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2));
  506. if (!C1B2_C2B1 || !C1A2_C2A1 ||
  507. !A1B2_A2B1 || !A2B1_A1B2)
  508. return false;
  509. APInt Xtop = C1B2_C2B1->getAPInt();
  510. APInt Xbot = A1B2_A2B1->getAPInt();
  511. APInt Ytop = C1A2_C2A1->getAPInt();
  512. APInt Ybot = A2B1_A1B2->getAPInt();
  513. LLVM_DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n");
  514. LLVM_DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n");
  515. LLVM_DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n");
  516. LLVM_DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n");
  517. APInt Xq = Xtop; // these need to be initialized, even
  518. APInt Xr = Xtop; // though they're just going to be overwritten
  519. APInt::sdivrem(Xtop, Xbot, Xq, Xr);
  520. APInt Yq = Ytop;
  521. APInt Yr = Ytop;
  522. APInt::sdivrem(Ytop, Ybot, Yq, Yr);
  523. if (Xr != 0 || Yr != 0) {
  524. X->setEmpty();
  525. ++DeltaSuccesses;
  526. return true;
  527. }
  528. LLVM_DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n");
  529. if (Xq.slt(0) || Yq.slt(0)) {
  530. X->setEmpty();
  531. ++DeltaSuccesses;
  532. return true;
  533. }
  534. if (const SCEVConstant *CUB =
  535. collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) {
  536. const APInt &UpperBound = CUB->getAPInt();
  537. LLVM_DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n");
  538. if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) {
  539. X->setEmpty();
  540. ++DeltaSuccesses;
  541. return true;
  542. }
  543. }
  544. X->setPoint(SE->getConstant(Xq),
  545. SE->getConstant(Yq),
  546. X->getAssociatedLoop());
  547. ++DeltaSuccesses;
  548. return true;
  549. }
  550. return false;
  551. }
  552. // if (X->isLine() && Y->isPoint()) This case can't occur.
  553. assert(!(X->isLine() && Y->isPoint()) && "This case should never occur");
  554. if (X->isPoint() && Y->isLine()) {
  555. LLVM_DEBUG(dbgs() << "\t intersect Point and Line\n");
  556. const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX());
  557. const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY());
  558. const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1);
  559. if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC()))
  560. return false;
  561. if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) {
  562. X->setEmpty();
  563. ++DeltaSuccesses;
  564. return true;
  565. }
  566. return false;
  567. }
  568. llvm_unreachable("shouldn't reach the end of Constraint intersection");
  569. return false;
  570. }
  571. //===----------------------------------------------------------------------===//
  572. // DependenceInfo methods
  573. // For debugging purposes. Dumps a dependence to OS.
  574. void Dependence::dump(raw_ostream &OS) const {
  575. bool Splitable = false;
  576. if (isConfused())
  577. OS << "confused";
  578. else {
  579. if (isConsistent())
  580. OS << "consistent ";
  581. if (isFlow())
  582. OS << "flow";
  583. else if (isOutput())
  584. OS << "output";
  585. else if (isAnti())
  586. OS << "anti";
  587. else if (isInput())
  588. OS << "input";
  589. unsigned Levels = getLevels();
  590. OS << " [";
  591. for (unsigned II = 1; II <= Levels; ++II) {
  592. if (isSplitable(II))
  593. Splitable = true;
  594. if (isPeelFirst(II))
  595. OS << 'p';
  596. const SCEV *Distance = getDistance(II);
  597. if (Distance)
  598. OS << *Distance;
  599. else if (isScalar(II))
  600. OS << "S";
  601. else {
  602. unsigned Direction = getDirection(II);
  603. if (Direction == DVEntry::ALL)
  604. OS << "*";
  605. else {
  606. if (Direction & DVEntry::LT)
  607. OS << "<";
  608. if (Direction & DVEntry::EQ)
  609. OS << "=";
  610. if (Direction & DVEntry::GT)
  611. OS << ">";
  612. }
  613. }
  614. if (isPeelLast(II))
  615. OS << 'p';
  616. if (II < Levels)
  617. OS << " ";
  618. }
  619. if (isLoopIndependent())
  620. OS << "|<";
  621. OS << "]";
  622. if (Splitable)
  623. OS << " splitable";
  624. }
  625. OS << "!\n";
  626. }
  627. // Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their
  628. // underlaying objects. If LocA and LocB are known to not alias (for any reason:
  629. // tbaa, non-overlapping regions etc), then it is known there is no dependecy.
  630. // Otherwise the underlying objects are checked to see if they point to
  631. // different identifiable objects.
  632. static AliasResult underlyingObjectsAlias(AAResults *AA,
  633. const DataLayout &DL,
  634. const MemoryLocation &LocA,
  635. const MemoryLocation &LocB) {
  636. // Check the original locations (minus size) for noalias, which can happen for
  637. // tbaa, incompatible underlying object locations, etc.
  638. MemoryLocation LocAS =
  639. MemoryLocation::getBeforeOrAfter(LocA.Ptr, LocA.AATags);
  640. MemoryLocation LocBS =
  641. MemoryLocation::getBeforeOrAfter(LocB.Ptr, LocB.AATags);
  642. if (AA->isNoAlias(LocAS, LocBS))
  643. return AliasResult::NoAlias;
  644. // Check the underlying objects are the same
  645. const Value *AObj = getUnderlyingObject(LocA.Ptr);
  646. const Value *BObj = getUnderlyingObject(LocB.Ptr);
  647. // If the underlying objects are the same, they must alias
  648. if (AObj == BObj)
  649. return AliasResult::MustAlias;
  650. // We may have hit the recursion limit for underlying objects, or have
  651. // underlying objects where we don't know they will alias.
  652. if (!isIdentifiedObject(AObj) || !isIdentifiedObject(BObj))
  653. return AliasResult::MayAlias;
  654. // Otherwise we know the objects are different and both identified objects so
  655. // must not alias.
  656. return AliasResult::NoAlias;
  657. }
  658. // Returns true if the load or store can be analyzed. Atomic and volatile
  659. // operations have properties which this analysis does not understand.
  660. static
  661. bool isLoadOrStore(const Instruction *I) {
  662. if (const LoadInst *LI = dyn_cast<LoadInst>(I))
  663. return LI->isUnordered();
  664. else if (const StoreInst *SI = dyn_cast<StoreInst>(I))
  665. return SI->isUnordered();
  666. return false;
  667. }
  668. // Examines the loop nesting of the Src and Dst
  669. // instructions and establishes their shared loops. Sets the variables
  670. // CommonLevels, SrcLevels, and MaxLevels.
  671. // The source and destination instructions needn't be contained in the same
  672. // loop. The routine establishNestingLevels finds the level of most deeply
  673. // nested loop that contains them both, CommonLevels. An instruction that's
  674. // not contained in a loop is at level = 0. MaxLevels is equal to the level
  675. // of the source plus the level of the destination, minus CommonLevels.
  676. // This lets us allocate vectors MaxLevels in length, with room for every
  677. // distinct loop referenced in both the source and destination subscripts.
  678. // The variable SrcLevels is the nesting depth of the source instruction.
  679. // It's used to help calculate distinct loops referenced by the destination.
  680. // Here's the map from loops to levels:
  681. // 0 - unused
  682. // 1 - outermost common loop
  683. // ... - other common loops
  684. // CommonLevels - innermost common loop
  685. // ... - loops containing Src but not Dst
  686. // SrcLevels - innermost loop containing Src but not Dst
  687. // ... - loops containing Dst but not Src
  688. // MaxLevels - innermost loops containing Dst but not Src
  689. // Consider the follow code fragment:
  690. // for (a = ...) {
  691. // for (b = ...) {
  692. // for (c = ...) {
  693. // for (d = ...) {
  694. // A[] = ...;
  695. // }
  696. // }
  697. // for (e = ...) {
  698. // for (f = ...) {
  699. // for (g = ...) {
  700. // ... = A[];
  701. // }
  702. // }
  703. // }
  704. // }
  705. // }
  706. // If we're looking at the possibility of a dependence between the store
  707. // to A (the Src) and the load from A (the Dst), we'll note that they
  708. // have 2 loops in common, so CommonLevels will equal 2 and the direction
  709. // vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7.
  710. // A map from loop names to loop numbers would look like
  711. // a - 1
  712. // b - 2 = CommonLevels
  713. // c - 3
  714. // d - 4 = SrcLevels
  715. // e - 5
  716. // f - 6
  717. // g - 7 = MaxLevels
  718. void DependenceInfo::establishNestingLevels(const Instruction *Src,
  719. const Instruction *Dst) {
  720. const BasicBlock *SrcBlock = Src->getParent();
  721. const BasicBlock *DstBlock = Dst->getParent();
  722. unsigned SrcLevel = LI->getLoopDepth(SrcBlock);
  723. unsigned DstLevel = LI->getLoopDepth(DstBlock);
  724. const Loop *SrcLoop = LI->getLoopFor(SrcBlock);
  725. const Loop *DstLoop = LI->getLoopFor(DstBlock);
  726. SrcLevels = SrcLevel;
  727. MaxLevels = SrcLevel + DstLevel;
  728. while (SrcLevel > DstLevel) {
  729. SrcLoop = SrcLoop->getParentLoop();
  730. SrcLevel--;
  731. }
  732. while (DstLevel > SrcLevel) {
  733. DstLoop = DstLoop->getParentLoop();
  734. DstLevel--;
  735. }
  736. while (SrcLoop != DstLoop) {
  737. SrcLoop = SrcLoop->getParentLoop();
  738. DstLoop = DstLoop->getParentLoop();
  739. SrcLevel--;
  740. }
  741. CommonLevels = SrcLevel;
  742. MaxLevels -= CommonLevels;
  743. }
  744. // Given one of the loops containing the source, return
  745. // its level index in our numbering scheme.
  746. unsigned DependenceInfo::mapSrcLoop(const Loop *SrcLoop) const {
  747. return SrcLoop->getLoopDepth();
  748. }
  749. // Given one of the loops containing the destination,
  750. // return its level index in our numbering scheme.
  751. unsigned DependenceInfo::mapDstLoop(const Loop *DstLoop) const {
  752. unsigned D = DstLoop->getLoopDepth();
  753. if (D > CommonLevels)
  754. // This tries to make sure that we assign unique numbers to src and dst when
  755. // the memory accesses reside in different loops that have the same depth.
  756. return D - CommonLevels + SrcLevels;
  757. else
  758. return D;
  759. }
  760. // Returns true if Expression is loop invariant in LoopNest.
  761. bool DependenceInfo::isLoopInvariant(const SCEV *Expression,
  762. const Loop *LoopNest) const {
  763. // Unlike ScalarEvolution::isLoopInvariant() we consider an access outside of
  764. // any loop as invariant, because we only consier expression evaluation at a
  765. // specific position (where the array access takes place), and not across the
  766. // entire function.
  767. if (!LoopNest)
  768. return true;
  769. // If the expression is invariant in the outermost loop of the loop nest, it
  770. // is invariant anywhere in the loop nest.
  771. return SE->isLoopInvariant(Expression, LoopNest->getOutermostLoop());
  772. }
  773. // Finds the set of loops from the LoopNest that
  774. // have a level <= CommonLevels and are referred to by the SCEV Expression.
  775. void DependenceInfo::collectCommonLoops(const SCEV *Expression,
  776. const Loop *LoopNest,
  777. SmallBitVector &Loops) const {
  778. while (LoopNest) {
  779. unsigned Level = LoopNest->getLoopDepth();
  780. if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest))
  781. Loops.set(Level);
  782. LoopNest = LoopNest->getParentLoop();
  783. }
  784. }
  785. void DependenceInfo::unifySubscriptType(ArrayRef<Subscript *> Pairs) {
  786. unsigned widestWidthSeen = 0;
  787. Type *widestType;
  788. // Go through each pair and find the widest bit to which we need
  789. // to extend all of them.
  790. for (Subscript *Pair : Pairs) {
  791. const SCEV *Src = Pair->Src;
  792. const SCEV *Dst = Pair->Dst;
  793. IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
  794. IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
  795. if (SrcTy == nullptr || DstTy == nullptr) {
  796. assert(SrcTy == DstTy && "This function only unify integer types and "
  797. "expect Src and Dst share the same type "
  798. "otherwise.");
  799. continue;
  800. }
  801. if (SrcTy->getBitWidth() > widestWidthSeen) {
  802. widestWidthSeen = SrcTy->getBitWidth();
  803. widestType = SrcTy;
  804. }
  805. if (DstTy->getBitWidth() > widestWidthSeen) {
  806. widestWidthSeen = DstTy->getBitWidth();
  807. widestType = DstTy;
  808. }
  809. }
  810. assert(widestWidthSeen > 0);
  811. // Now extend each pair to the widest seen.
  812. for (Subscript *Pair : Pairs) {
  813. const SCEV *Src = Pair->Src;
  814. const SCEV *Dst = Pair->Dst;
  815. IntegerType *SrcTy = dyn_cast<IntegerType>(Src->getType());
  816. IntegerType *DstTy = dyn_cast<IntegerType>(Dst->getType());
  817. if (SrcTy == nullptr || DstTy == nullptr) {
  818. assert(SrcTy == DstTy && "This function only unify integer types and "
  819. "expect Src and Dst share the same type "
  820. "otherwise.");
  821. continue;
  822. }
  823. if (SrcTy->getBitWidth() < widestWidthSeen)
  824. // Sign-extend Src to widestType
  825. Pair->Src = SE->getSignExtendExpr(Src, widestType);
  826. if (DstTy->getBitWidth() < widestWidthSeen) {
  827. // Sign-extend Dst to widestType
  828. Pair->Dst = SE->getSignExtendExpr(Dst, widestType);
  829. }
  830. }
  831. }
  832. // removeMatchingExtensions - Examines a subscript pair.
  833. // If the source and destination are identically sign (or zero)
  834. // extended, it strips off the extension in an effect to simplify
  835. // the actual analysis.
  836. void DependenceInfo::removeMatchingExtensions(Subscript *Pair) {
  837. const SCEV *Src = Pair->Src;
  838. const SCEV *Dst = Pair->Dst;
  839. if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) ||
  840. (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) {
  841. const SCEVIntegralCastExpr *SrcCast = cast<SCEVIntegralCastExpr>(Src);
  842. const SCEVIntegralCastExpr *DstCast = cast<SCEVIntegralCastExpr>(Dst);
  843. const SCEV *SrcCastOp = SrcCast->getOperand();
  844. const SCEV *DstCastOp = DstCast->getOperand();
  845. if (SrcCastOp->getType() == DstCastOp->getType()) {
  846. Pair->Src = SrcCastOp;
  847. Pair->Dst = DstCastOp;
  848. }
  849. }
  850. }
  851. // Examine the scev and return true iff it's affine.
  852. // Collect any loops mentioned in the set of "Loops".
  853. bool DependenceInfo::checkSubscript(const SCEV *Expr, const Loop *LoopNest,
  854. SmallBitVector &Loops, bool IsSrc) {
  855. const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
  856. if (!AddRec)
  857. return isLoopInvariant(Expr, LoopNest);
  858. // The AddRec must depend on one of the containing loops. Otherwise,
  859. // mapSrcLoop and mapDstLoop return indices outside the intended range. This
  860. // can happen when a subscript in one loop references an IV from a sibling
  861. // loop that could not be replaced with a concrete exit value by
  862. // getSCEVAtScope.
  863. const Loop *L = LoopNest;
  864. while (L && AddRec->getLoop() != L)
  865. L = L->getParentLoop();
  866. if (!L)
  867. return false;
  868. const SCEV *Start = AddRec->getStart();
  869. const SCEV *Step = AddRec->getStepRecurrence(*SE);
  870. const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
  871. if (!isa<SCEVCouldNotCompute>(UB)) {
  872. if (SE->getTypeSizeInBits(Start->getType()) <
  873. SE->getTypeSizeInBits(UB->getType())) {
  874. if (!AddRec->getNoWrapFlags())
  875. return false;
  876. }
  877. }
  878. if (!isLoopInvariant(Step, LoopNest))
  879. return false;
  880. if (IsSrc)
  881. Loops.set(mapSrcLoop(AddRec->getLoop()));
  882. else
  883. Loops.set(mapDstLoop(AddRec->getLoop()));
  884. return checkSubscript(Start, LoopNest, Loops, IsSrc);
  885. }
  886. // Examine the scev and return true iff it's linear.
  887. // Collect any loops mentioned in the set of "Loops".
  888. bool DependenceInfo::checkSrcSubscript(const SCEV *Src, const Loop *LoopNest,
  889. SmallBitVector &Loops) {
  890. return checkSubscript(Src, LoopNest, Loops, true);
  891. }
  892. // Examine the scev and return true iff it's linear.
  893. // Collect any loops mentioned in the set of "Loops".
  894. bool DependenceInfo::checkDstSubscript(const SCEV *Dst, const Loop *LoopNest,
  895. SmallBitVector &Loops) {
  896. return checkSubscript(Dst, LoopNest, Loops, false);
  897. }
  898. // Examines the subscript pair (the Src and Dst SCEVs)
  899. // and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear.
  900. // Collects the associated loops in a set.
  901. DependenceInfo::Subscript::ClassificationKind
  902. DependenceInfo::classifyPair(const SCEV *Src, const Loop *SrcLoopNest,
  903. const SCEV *Dst, const Loop *DstLoopNest,
  904. SmallBitVector &Loops) {
  905. SmallBitVector SrcLoops(MaxLevels + 1);
  906. SmallBitVector DstLoops(MaxLevels + 1);
  907. if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops))
  908. return Subscript::NonLinear;
  909. if (!checkDstSubscript(Dst, DstLoopNest, DstLoops))
  910. return Subscript::NonLinear;
  911. Loops = SrcLoops;
  912. Loops |= DstLoops;
  913. unsigned N = Loops.count();
  914. if (N == 0)
  915. return Subscript::ZIV;
  916. if (N == 1)
  917. return Subscript::SIV;
  918. if (N == 2 && (SrcLoops.count() == 0 ||
  919. DstLoops.count() == 0 ||
  920. (SrcLoops.count() == 1 && DstLoops.count() == 1)))
  921. return Subscript::RDIV;
  922. return Subscript::MIV;
  923. }
  924. // A wrapper around SCEV::isKnownPredicate.
  925. // Looks for cases where we're interested in comparing for equality.
  926. // If both X and Y have been identically sign or zero extended,
  927. // it strips off the (confusing) extensions before invoking
  928. // SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package
  929. // will be similarly updated.
  930. //
  931. // If SCEV::isKnownPredicate can't prove the predicate,
  932. // we try simple subtraction, which seems to help in some cases
  933. // involving symbolics.
  934. bool DependenceInfo::isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *X,
  935. const SCEV *Y) const {
  936. if (Pred == CmpInst::ICMP_EQ ||
  937. Pred == CmpInst::ICMP_NE) {
  938. if ((isa<SCEVSignExtendExpr>(X) &&
  939. isa<SCEVSignExtendExpr>(Y)) ||
  940. (isa<SCEVZeroExtendExpr>(X) &&
  941. isa<SCEVZeroExtendExpr>(Y))) {
  942. const SCEVIntegralCastExpr *CX = cast<SCEVIntegralCastExpr>(X);
  943. const SCEVIntegralCastExpr *CY = cast<SCEVIntegralCastExpr>(Y);
  944. const SCEV *Xop = CX->getOperand();
  945. const SCEV *Yop = CY->getOperand();
  946. if (Xop->getType() == Yop->getType()) {
  947. X = Xop;
  948. Y = Yop;
  949. }
  950. }
  951. }
  952. if (SE->isKnownPredicate(Pred, X, Y))
  953. return true;
  954. // If SE->isKnownPredicate can't prove the condition,
  955. // we try the brute-force approach of subtracting
  956. // and testing the difference.
  957. // By testing with SE->isKnownPredicate first, we avoid
  958. // the possibility of overflow when the arguments are constants.
  959. const SCEV *Delta = SE->getMinusSCEV(X, Y);
  960. switch (Pred) {
  961. case CmpInst::ICMP_EQ:
  962. return Delta->isZero();
  963. case CmpInst::ICMP_NE:
  964. return SE->isKnownNonZero(Delta);
  965. case CmpInst::ICMP_SGE:
  966. return SE->isKnownNonNegative(Delta);
  967. case CmpInst::ICMP_SLE:
  968. return SE->isKnownNonPositive(Delta);
  969. case CmpInst::ICMP_SGT:
  970. return SE->isKnownPositive(Delta);
  971. case CmpInst::ICMP_SLT:
  972. return SE->isKnownNegative(Delta);
  973. default:
  974. llvm_unreachable("unexpected predicate in isKnownPredicate");
  975. }
  976. }
  977. /// Compare to see if S is less than Size, using isKnownNegative(S - max(Size, 1))
  978. /// with some extra checking if S is an AddRec and we can prove less-than using
  979. /// the loop bounds.
  980. bool DependenceInfo::isKnownLessThan(const SCEV *S, const SCEV *Size) const {
  981. // First unify to the same type
  982. auto *SType = dyn_cast<IntegerType>(S->getType());
  983. auto *SizeType = dyn_cast<IntegerType>(Size->getType());
  984. if (!SType || !SizeType)
  985. return false;
  986. Type *MaxType =
  987. (SType->getBitWidth() >= SizeType->getBitWidth()) ? SType : SizeType;
  988. S = SE->getTruncateOrZeroExtend(S, MaxType);
  989. Size = SE->getTruncateOrZeroExtend(Size, MaxType);
  990. // Special check for addrecs using BE taken count
  991. const SCEV *Bound = SE->getMinusSCEV(S, Size);
  992. if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Bound)) {
  993. if (AddRec->isAffine()) {
  994. const SCEV *BECount = SE->getBackedgeTakenCount(AddRec->getLoop());
  995. if (!isa<SCEVCouldNotCompute>(BECount)) {
  996. const SCEV *Limit = AddRec->evaluateAtIteration(BECount, *SE);
  997. if (SE->isKnownNegative(Limit))
  998. return true;
  999. }
  1000. }
  1001. }
  1002. // Check using normal isKnownNegative
  1003. const SCEV *LimitedBound =
  1004. SE->getMinusSCEV(S, SE->getSMaxExpr(Size, SE->getOne(Size->getType())));
  1005. return SE->isKnownNegative(LimitedBound);
  1006. }
  1007. bool DependenceInfo::isKnownNonNegative(const SCEV *S, const Value *Ptr) const {
  1008. bool Inbounds = false;
  1009. if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(Ptr))
  1010. Inbounds = SrcGEP->isInBounds();
  1011. if (Inbounds) {
  1012. if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
  1013. if (AddRec->isAffine()) {
  1014. // We know S is for Ptr, the operand on a load/store, so doesn't wrap.
  1015. // If both parts are NonNegative, the end result will be NonNegative
  1016. if (SE->isKnownNonNegative(AddRec->getStart()) &&
  1017. SE->isKnownNonNegative(AddRec->getOperand(1)))
  1018. return true;
  1019. }
  1020. }
  1021. }
  1022. return SE->isKnownNonNegative(S);
  1023. }
  1024. // All subscripts are all the same type.
  1025. // Loop bound may be smaller (e.g., a char).
  1026. // Should zero extend loop bound, since it's always >= 0.
  1027. // This routine collects upper bound and extends or truncates if needed.
  1028. // Truncating is safe when subscripts are known not to wrap. Cases without
  1029. // nowrap flags should have been rejected earlier.
  1030. // Return null if no bound available.
  1031. const SCEV *DependenceInfo::collectUpperBound(const Loop *L, Type *T) const {
  1032. if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
  1033. const SCEV *UB = SE->getBackedgeTakenCount(L);
  1034. return SE->getTruncateOrZeroExtend(UB, T);
  1035. }
  1036. return nullptr;
  1037. }
  1038. // Calls collectUpperBound(), then attempts to cast it to SCEVConstant.
  1039. // If the cast fails, returns NULL.
  1040. const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
  1041. Type *T) const {
  1042. if (const SCEV *UB = collectUpperBound(L, T))
  1043. return dyn_cast<SCEVConstant>(UB);
  1044. return nullptr;
  1045. }
  1046. // testZIV -
  1047. // When we have a pair of subscripts of the form [c1] and [c2],
  1048. // where c1 and c2 are both loop invariant, we attack it using
  1049. // the ZIV test. Basically, we test by comparing the two values,
  1050. // but there are actually three possible results:
  1051. // 1) the values are equal, so there's a dependence
  1052. // 2) the values are different, so there's no dependence
  1053. // 3) the values might be equal, so we have to assume a dependence.
  1054. //
  1055. // Return true if dependence disproved.
  1056. bool DependenceInfo::testZIV(const SCEV *Src, const SCEV *Dst,
  1057. FullDependence &Result) const {
  1058. LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
  1059. LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
  1060. ++ZIVapplications;
  1061. if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) {
  1062. LLVM_DEBUG(dbgs() << " provably dependent\n");
  1063. return false; // provably dependent
  1064. }
  1065. if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) {
  1066. LLVM_DEBUG(dbgs() << " provably independent\n");
  1067. ++ZIVindependence;
  1068. return true; // provably independent
  1069. }
  1070. LLVM_DEBUG(dbgs() << " possibly dependent\n");
  1071. Result.Consistent = false;
  1072. return false; // possibly dependent
  1073. }
  1074. // strongSIVtest -
  1075. // From the paper, Practical Dependence Testing, Section 4.2.1
  1076. //
  1077. // When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i],
  1078. // where i is an induction variable, c1 and c2 are loop invariant,
  1079. // and a is a constant, we can solve it exactly using the Strong SIV test.
  1080. //
  1081. // Can prove independence. Failing that, can compute distance (and direction).
  1082. // In the presence of symbolic terms, we can sometimes make progress.
  1083. //
  1084. // If there's a dependence,
  1085. //
  1086. // c1 + a*i = c2 + a*i'
  1087. //
  1088. // The dependence distance is
  1089. //
  1090. // d = i' - i = (c1 - c2)/a
  1091. //
  1092. // A dependence only exists if d is an integer and abs(d) <= U, where U is the
  1093. // loop's upper bound. If a dependence exists, the dependence direction is
  1094. // defined as
  1095. //
  1096. // { < if d > 0
  1097. // direction = { = if d = 0
  1098. // { > if d < 0
  1099. //
  1100. // Return true if dependence disproved.
  1101. bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
  1102. const SCEV *DstConst, const Loop *CurLoop,
  1103. unsigned Level, FullDependence &Result,
  1104. Constraint &NewConstraint) const {
  1105. LLVM_DEBUG(dbgs() << "\tStrong SIV test\n");
  1106. LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff);
  1107. LLVM_DEBUG(dbgs() << ", " << *Coeff->getType() << "\n");
  1108. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst);
  1109. LLVM_DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n");
  1110. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst);
  1111. LLVM_DEBUG(dbgs() << ", " << *DstConst->getType() << "\n");
  1112. ++StrongSIVapplications;
  1113. assert(0 < Level && Level <= CommonLevels && "level out of range");
  1114. Level--;
  1115. const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
  1116. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
  1117. LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
  1118. // check that |Delta| < iteration count
  1119. if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
  1120. LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
  1121. LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
  1122. const SCEV *AbsDelta =
  1123. SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
  1124. const SCEV *AbsCoeff =
  1125. SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
  1126. const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
  1127. if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
  1128. // Distance greater than trip count - no dependence
  1129. ++StrongSIVindependence;
  1130. ++StrongSIVsuccesses;
  1131. return true;
  1132. }
  1133. }
  1134. // Can we compute distance?
  1135. if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) {
  1136. APInt ConstDelta = cast<SCEVConstant>(Delta)->getAPInt();
  1137. APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getAPInt();
  1138. APInt Distance = ConstDelta; // these need to be initialized
  1139. APInt Remainder = ConstDelta;
  1140. APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder);
  1141. LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
  1142. LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
  1143. // Make sure Coeff divides Delta exactly
  1144. if (Remainder != 0) {
  1145. // Coeff doesn't divide Distance, no dependence
  1146. ++StrongSIVindependence;
  1147. ++StrongSIVsuccesses;
  1148. return true;
  1149. }
  1150. Result.DV[Level].Distance = SE->getConstant(Distance);
  1151. NewConstraint.setDistance(SE->getConstant(Distance), CurLoop);
  1152. if (Distance.sgt(0))
  1153. Result.DV[Level].Direction &= Dependence::DVEntry::LT;
  1154. else if (Distance.slt(0))
  1155. Result.DV[Level].Direction &= Dependence::DVEntry::GT;
  1156. else
  1157. Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
  1158. ++StrongSIVsuccesses;
  1159. }
  1160. else if (Delta->isZero()) {
  1161. // since 0/X == 0
  1162. Result.DV[Level].Distance = Delta;
  1163. NewConstraint.setDistance(Delta, CurLoop);
  1164. Result.DV[Level].Direction &= Dependence::DVEntry::EQ;
  1165. ++StrongSIVsuccesses;
  1166. }
  1167. else {
  1168. if (Coeff->isOne()) {
  1169. LLVM_DEBUG(dbgs() << "\t Distance = " << *Delta << "\n");
  1170. Result.DV[Level].Distance = Delta; // since X/1 == X
  1171. NewConstraint.setDistance(Delta, CurLoop);
  1172. }
  1173. else {
  1174. Result.Consistent = false;
  1175. NewConstraint.setLine(Coeff,
  1176. SE->getNegativeSCEV(Coeff),
  1177. SE->getNegativeSCEV(Delta), CurLoop);
  1178. }
  1179. // maybe we can get a useful direction
  1180. bool DeltaMaybeZero = !SE->isKnownNonZero(Delta);
  1181. bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta);
  1182. bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta);
  1183. bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff);
  1184. bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff);
  1185. // The double negatives above are confusing.
  1186. // It helps to read !SE->isKnownNonZero(Delta)
  1187. // as "Delta might be Zero"
  1188. unsigned NewDirection = Dependence::DVEntry::NONE;
  1189. if ((DeltaMaybePositive && CoeffMaybePositive) ||
  1190. (DeltaMaybeNegative && CoeffMaybeNegative))
  1191. NewDirection = Dependence::DVEntry::LT;
  1192. if (DeltaMaybeZero)
  1193. NewDirection |= Dependence::DVEntry::EQ;
  1194. if ((DeltaMaybeNegative && CoeffMaybePositive) ||
  1195. (DeltaMaybePositive && CoeffMaybeNegative))
  1196. NewDirection |= Dependence::DVEntry::GT;
  1197. if (NewDirection < Result.DV[Level].Direction)
  1198. ++StrongSIVsuccesses;
  1199. Result.DV[Level].Direction &= NewDirection;
  1200. }
  1201. return false;
  1202. }
  1203. // weakCrossingSIVtest -
  1204. // From the paper, Practical Dependence Testing, Section 4.2.2
  1205. //
  1206. // When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i],
  1207. // where i is an induction variable, c1 and c2 are loop invariant,
  1208. // and a is a constant, we can solve it exactly using the
  1209. // Weak-Crossing SIV test.
  1210. //
  1211. // Given c1 + a*i = c2 - a*i', we can look for the intersection of
  1212. // the two lines, where i = i', yielding
  1213. //
  1214. // c1 + a*i = c2 - a*i
  1215. // 2a*i = c2 - c1
  1216. // i = (c2 - c1)/2a
  1217. //
  1218. // If i < 0, there is no dependence.
  1219. // If i > upperbound, there is no dependence.
  1220. // If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0.
  1221. // If i = upperbound, there's a dependence with distance = 0.
  1222. // If i is integral, there's a dependence (all directions).
  1223. // If the non-integer part = 1/2, there's a dependence (<> directions).
  1224. // Otherwise, there's no dependence.
  1225. //
  1226. // Can prove independence. Failing that,
  1227. // can sometimes refine the directions.
  1228. // Can determine iteration for splitting.
  1229. //
  1230. // Return true if dependence disproved.
  1231. bool DependenceInfo::weakCrossingSIVtest(
  1232. const SCEV *Coeff, const SCEV *SrcConst, const SCEV *DstConst,
  1233. const Loop *CurLoop, unsigned Level, FullDependence &Result,
  1234. Constraint &NewConstraint, const SCEV *&SplitIter) const {
  1235. LLVM_DEBUG(dbgs() << "\tWeak-Crossing SIV test\n");
  1236. LLVM_DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n");
  1237. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
  1238. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
  1239. ++WeakCrossingSIVapplications;
  1240. assert(0 < Level && Level <= CommonLevels && "Level out of range");
  1241. Level--;
  1242. Result.Consistent = false;
  1243. const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
  1244. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1245. NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop);
  1246. if (Delta->isZero()) {
  1247. Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
  1248. Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
  1249. ++WeakCrossingSIVsuccesses;
  1250. if (!Result.DV[Level].Direction) {
  1251. ++WeakCrossingSIVindependence;
  1252. return true;
  1253. }
  1254. Result.DV[Level].Distance = Delta; // = 0
  1255. return false;
  1256. }
  1257. const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff);
  1258. if (!ConstCoeff)
  1259. return false;
  1260. Result.DV[Level].Splitable = true;
  1261. if (SE->isKnownNegative(ConstCoeff)) {
  1262. ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff));
  1263. assert(ConstCoeff &&
  1264. "dynamic cast of negative of ConstCoeff should yield constant");
  1265. Delta = SE->getNegativeSCEV(Delta);
  1266. }
  1267. assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive");
  1268. // compute SplitIter for use by DependenceInfo::getSplitIteration()
  1269. SplitIter = SE->getUDivExpr(
  1270. SE->getSMaxExpr(SE->getZero(Delta->getType()), Delta),
  1271. SE->getMulExpr(SE->getConstant(Delta->getType(), 2), ConstCoeff));
  1272. LLVM_DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n");
  1273. const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
  1274. if (!ConstDelta)
  1275. return false;
  1276. // We're certain that ConstCoeff > 0; therefore,
  1277. // if Delta < 0, then no dependence.
  1278. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1279. LLVM_DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n");
  1280. if (SE->isKnownNegative(Delta)) {
  1281. // No dependence, Delta < 0
  1282. ++WeakCrossingSIVindependence;
  1283. ++WeakCrossingSIVsuccesses;
  1284. return true;
  1285. }
  1286. // We're certain that Delta > 0 and ConstCoeff > 0.
  1287. // Check Delta/(2*ConstCoeff) against upper loop bound
  1288. if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
  1289. LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
  1290. const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2);
  1291. const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound),
  1292. ConstantTwo);
  1293. LLVM_DEBUG(dbgs() << "\t ML = " << *ML << "\n");
  1294. if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) {
  1295. // Delta too big, no dependence
  1296. ++WeakCrossingSIVindependence;
  1297. ++WeakCrossingSIVsuccesses;
  1298. return true;
  1299. }
  1300. if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) {
  1301. // i = i' = UB
  1302. Result.DV[Level].Direction &= ~Dependence::DVEntry::LT;
  1303. Result.DV[Level].Direction &= ~Dependence::DVEntry::GT;
  1304. ++WeakCrossingSIVsuccesses;
  1305. if (!Result.DV[Level].Direction) {
  1306. ++WeakCrossingSIVindependence;
  1307. return true;
  1308. }
  1309. Result.DV[Level].Splitable = false;
  1310. Result.DV[Level].Distance = SE->getZero(Delta->getType());
  1311. return false;
  1312. }
  1313. }
  1314. // check that Coeff divides Delta
  1315. APInt APDelta = ConstDelta->getAPInt();
  1316. APInt APCoeff = ConstCoeff->getAPInt();
  1317. APInt Distance = APDelta; // these need to be initialzed
  1318. APInt Remainder = APDelta;
  1319. APInt::sdivrem(APDelta, APCoeff, Distance, Remainder);
  1320. LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
  1321. if (Remainder != 0) {
  1322. // Coeff doesn't divide Delta, no dependence
  1323. ++WeakCrossingSIVindependence;
  1324. ++WeakCrossingSIVsuccesses;
  1325. return true;
  1326. }
  1327. LLVM_DEBUG(dbgs() << "\t Distance = " << Distance << "\n");
  1328. // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible
  1329. APInt Two = APInt(Distance.getBitWidth(), 2, true);
  1330. Remainder = Distance.srem(Two);
  1331. LLVM_DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n");
  1332. if (Remainder != 0) {
  1333. // Equal direction isn't possible
  1334. Result.DV[Level].Direction &= ~Dependence::DVEntry::EQ;
  1335. ++WeakCrossingSIVsuccesses;
  1336. }
  1337. return false;
  1338. }
  1339. // Kirch's algorithm, from
  1340. //
  1341. // Optimizing Supercompilers for Supercomputers
  1342. // Michael Wolfe
  1343. // MIT Press, 1989
  1344. //
  1345. // Program 2.1, page 29.
  1346. // Computes the GCD of AM and BM.
  1347. // Also finds a solution to the equation ax - by = gcd(a, b).
  1348. // Returns true if dependence disproved; i.e., gcd does not divide Delta.
  1349. static bool findGCD(unsigned Bits, const APInt &AM, const APInt &BM,
  1350. const APInt &Delta, APInt &G, APInt &X, APInt &Y) {
  1351. APInt A0(Bits, 1, true), A1(Bits, 0, true);
  1352. APInt B0(Bits, 0, true), B1(Bits, 1, true);
  1353. APInt G0 = AM.abs();
  1354. APInt G1 = BM.abs();
  1355. APInt Q = G0; // these need to be initialized
  1356. APInt R = G0;
  1357. APInt::sdivrem(G0, G1, Q, R);
  1358. while (R != 0) {
  1359. APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2;
  1360. APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2;
  1361. G0 = G1; G1 = R;
  1362. APInt::sdivrem(G0, G1, Q, R);
  1363. }
  1364. G = G1;
  1365. LLVM_DEBUG(dbgs() << "\t GCD = " << G << "\n");
  1366. X = AM.slt(0) ? -A1 : A1;
  1367. Y = BM.slt(0) ? B1 : -B1;
  1368. // make sure gcd divides Delta
  1369. R = Delta.srem(G);
  1370. if (R != 0)
  1371. return true; // gcd doesn't divide Delta, no dependence
  1372. Q = Delta.sdiv(G);
  1373. return false;
  1374. }
  1375. static APInt floorOfQuotient(const APInt &A, const APInt &B) {
  1376. APInt Q = A; // these need to be initialized
  1377. APInt R = A;
  1378. APInt::sdivrem(A, B, Q, R);
  1379. if (R == 0)
  1380. return Q;
  1381. if ((A.sgt(0) && B.sgt(0)) ||
  1382. (A.slt(0) && B.slt(0)))
  1383. return Q;
  1384. else
  1385. return Q - 1;
  1386. }
  1387. static APInt ceilingOfQuotient(const APInt &A, const APInt &B) {
  1388. APInt Q = A; // these need to be initialized
  1389. APInt R = A;
  1390. APInt::sdivrem(A, B, Q, R);
  1391. if (R == 0)
  1392. return Q;
  1393. if ((A.sgt(0) && B.sgt(0)) ||
  1394. (A.slt(0) && B.slt(0)))
  1395. return Q + 1;
  1396. else
  1397. return Q;
  1398. }
  1399. // exactSIVtest -
  1400. // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i],
  1401. // where i is an induction variable, c1 and c2 are loop invariant, and a1
  1402. // and a2 are constant, we can solve it exactly using an algorithm developed
  1403. // by Banerjee and Wolfe. See Algorithm 6.2.1 (case 2.5) in:
  1404. //
  1405. // Dependence Analysis for Supercomputing
  1406. // Utpal Banerjee
  1407. // Kluwer Academic Publishers, 1988
  1408. //
  1409. // It's slower than the specialized tests (strong SIV, weak-zero SIV, etc),
  1410. // so use them if possible. They're also a bit better with symbolics and,
  1411. // in the case of the strong SIV test, can compute Distances.
  1412. //
  1413. // Return true if dependence disproved.
  1414. //
  1415. // This is a modified version of the original Banerjee algorithm. The original
  1416. // only tested whether Dst depends on Src. This algorithm extends that and
  1417. // returns all the dependencies that exist between Dst and Src.
  1418. bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
  1419. const SCEV *SrcConst, const SCEV *DstConst,
  1420. const Loop *CurLoop, unsigned Level,
  1421. FullDependence &Result,
  1422. Constraint &NewConstraint) const {
  1423. LLVM_DEBUG(dbgs() << "\tExact SIV test\n");
  1424. LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
  1425. LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
  1426. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
  1427. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
  1428. ++ExactSIVapplications;
  1429. assert(0 < Level && Level <= CommonLevels && "Level out of range");
  1430. Level--;
  1431. Result.Consistent = false;
  1432. const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
  1433. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1434. NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), Delta,
  1435. CurLoop);
  1436. const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
  1437. const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
  1438. const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
  1439. if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
  1440. return false;
  1441. // find gcd
  1442. APInt G, X, Y;
  1443. APInt AM = ConstSrcCoeff->getAPInt();
  1444. APInt BM = ConstDstCoeff->getAPInt();
  1445. APInt CM = ConstDelta->getAPInt();
  1446. unsigned Bits = AM.getBitWidth();
  1447. if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
  1448. // gcd doesn't divide Delta, no dependence
  1449. ++ExactSIVindependence;
  1450. ++ExactSIVsuccesses;
  1451. return true;
  1452. }
  1453. LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
  1454. // since SCEV construction normalizes, LM = 0
  1455. APInt UM(Bits, 1, true);
  1456. bool UMValid = false;
  1457. // UM is perhaps unavailable, let's check
  1458. if (const SCEVConstant *CUB =
  1459. collectConstantUpperBound(CurLoop, Delta->getType())) {
  1460. UM = CUB->getAPInt();
  1461. LLVM_DEBUG(dbgs() << "\t UM = " << UM << "\n");
  1462. UMValid = true;
  1463. }
  1464. APInt TU(APInt::getSignedMaxValue(Bits));
  1465. APInt TL(APInt::getSignedMinValue(Bits));
  1466. APInt TC = CM.sdiv(G);
  1467. APInt TX = X * TC;
  1468. APInt TY = Y * TC;
  1469. LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
  1470. LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
  1471. LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
  1472. SmallVector<APInt, 2> TLVec, TUVec;
  1473. APInt TB = BM.sdiv(G);
  1474. if (TB.sgt(0)) {
  1475. TLVec.push_back(ceilingOfQuotient(-TX, TB));
  1476. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1477. // New bound check - modification to Banerjee's e3 check
  1478. if (UMValid) {
  1479. TUVec.push_back(floorOfQuotient(UM - TX, TB));
  1480. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1481. }
  1482. } else {
  1483. TUVec.push_back(floorOfQuotient(-TX, TB));
  1484. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1485. // New bound check - modification to Banerjee's e3 check
  1486. if (UMValid) {
  1487. TLVec.push_back(ceilingOfQuotient(UM - TX, TB));
  1488. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1489. }
  1490. }
  1491. APInt TA = AM.sdiv(G);
  1492. if (TA.sgt(0)) {
  1493. if (UMValid) {
  1494. TUVec.push_back(floorOfQuotient(UM - TY, TA));
  1495. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1496. }
  1497. // New bound check - modification to Banerjee's e3 check
  1498. TLVec.push_back(ceilingOfQuotient(-TY, TA));
  1499. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1500. } else {
  1501. if (UMValid) {
  1502. TLVec.push_back(ceilingOfQuotient(UM - TY, TA));
  1503. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1504. }
  1505. // New bound check - modification to Banerjee's e3 check
  1506. TUVec.push_back(floorOfQuotient(-TY, TA));
  1507. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1508. }
  1509. LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
  1510. LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
  1511. if (TLVec.empty() || TUVec.empty())
  1512. return false;
  1513. TL = APIntOps::smax(TLVec.front(), TLVec.back());
  1514. TU = APIntOps::smin(TUVec.front(), TUVec.back());
  1515. LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
  1516. LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
  1517. if (TL.sgt(TU)) {
  1518. ++ExactSIVindependence;
  1519. ++ExactSIVsuccesses;
  1520. return true;
  1521. }
  1522. // explore directions
  1523. unsigned NewDirection = Dependence::DVEntry::NONE;
  1524. APInt LowerDistance, UpperDistance;
  1525. if (TA.sgt(TB)) {
  1526. LowerDistance = (TY - TX) + (TA - TB) * TL;
  1527. UpperDistance = (TY - TX) + (TA - TB) * TU;
  1528. } else {
  1529. LowerDistance = (TY - TX) + (TA - TB) * TU;
  1530. UpperDistance = (TY - TX) + (TA - TB) * TL;
  1531. }
  1532. LLVM_DEBUG(dbgs() << "\t LowerDistance = " << LowerDistance << "\n");
  1533. LLVM_DEBUG(dbgs() << "\t UpperDistance = " << UpperDistance << "\n");
  1534. APInt Zero(Bits, 0, true);
  1535. if (LowerDistance.sle(Zero) && UpperDistance.sge(Zero)) {
  1536. NewDirection |= Dependence::DVEntry::EQ;
  1537. ++ExactSIVsuccesses;
  1538. }
  1539. if (LowerDistance.slt(0)) {
  1540. NewDirection |= Dependence::DVEntry::GT;
  1541. ++ExactSIVsuccesses;
  1542. }
  1543. if (UpperDistance.sgt(0)) {
  1544. NewDirection |= Dependence::DVEntry::LT;
  1545. ++ExactSIVsuccesses;
  1546. }
  1547. // finished
  1548. Result.DV[Level].Direction &= NewDirection;
  1549. if (Result.DV[Level].Direction == Dependence::DVEntry::NONE)
  1550. ++ExactSIVindependence;
  1551. LLVM_DEBUG(dbgs() << "\t Result = ");
  1552. LLVM_DEBUG(Result.dump(dbgs()));
  1553. return Result.DV[Level].Direction == Dependence::DVEntry::NONE;
  1554. }
  1555. // Return true if the divisor evenly divides the dividend.
  1556. static
  1557. bool isRemainderZero(const SCEVConstant *Dividend,
  1558. const SCEVConstant *Divisor) {
  1559. const APInt &ConstDividend = Dividend->getAPInt();
  1560. const APInt &ConstDivisor = Divisor->getAPInt();
  1561. return ConstDividend.srem(ConstDivisor) == 0;
  1562. }
  1563. // weakZeroSrcSIVtest -
  1564. // From the paper, Practical Dependence Testing, Section 4.2.2
  1565. //
  1566. // When we have a pair of subscripts of the form [c1] and [c2 + a*i],
  1567. // where i is an induction variable, c1 and c2 are loop invariant,
  1568. // and a is a constant, we can solve it exactly using the
  1569. // Weak-Zero SIV test.
  1570. //
  1571. // Given
  1572. //
  1573. // c1 = c2 + a*i
  1574. //
  1575. // we get
  1576. //
  1577. // (c1 - c2)/a = i
  1578. //
  1579. // If i is not an integer, there's no dependence.
  1580. // If i < 0 or > UB, there's no dependence.
  1581. // If i = 0, the direction is >= and peeling the
  1582. // 1st iteration will break the dependence.
  1583. // If i = UB, the direction is <= and peeling the
  1584. // last iteration will break the dependence.
  1585. // Otherwise, the direction is *.
  1586. //
  1587. // Can prove independence. Failing that, we can sometimes refine
  1588. // the directions. Can sometimes show that first or last
  1589. // iteration carries all the dependences (so worth peeling).
  1590. //
  1591. // (see also weakZeroDstSIVtest)
  1592. //
  1593. // Return true if dependence disproved.
  1594. bool DependenceInfo::weakZeroSrcSIVtest(const SCEV *DstCoeff,
  1595. const SCEV *SrcConst,
  1596. const SCEV *DstConst,
  1597. const Loop *CurLoop, unsigned Level,
  1598. FullDependence &Result,
  1599. Constraint &NewConstraint) const {
  1600. // For the WeakSIV test, it's possible the loop isn't common to
  1601. // the Src and Dst loops. If it isn't, then there's no need to
  1602. // record a direction.
  1603. LLVM_DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n");
  1604. LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n");
  1605. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
  1606. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
  1607. ++WeakZeroSIVapplications;
  1608. assert(0 < Level && Level <= MaxLevels && "Level out of range");
  1609. Level--;
  1610. Result.Consistent = false;
  1611. const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
  1612. NewConstraint.setLine(SE->getZero(Delta->getType()), DstCoeff, Delta,
  1613. CurLoop);
  1614. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1615. if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) {
  1616. if (Level < CommonLevels) {
  1617. Result.DV[Level].Direction &= Dependence::DVEntry::GE;
  1618. Result.DV[Level].PeelFirst = true;
  1619. ++WeakZeroSIVsuccesses;
  1620. }
  1621. return false; // dependences caused by first iteration
  1622. }
  1623. const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
  1624. if (!ConstCoeff)
  1625. return false;
  1626. const SCEV *AbsCoeff =
  1627. SE->isKnownNegative(ConstCoeff) ?
  1628. SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
  1629. const SCEV *NewDelta =
  1630. SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
  1631. // check that Delta/SrcCoeff < iteration count
  1632. // really check NewDelta < count*AbsCoeff
  1633. if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
  1634. LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
  1635. const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
  1636. if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
  1637. ++WeakZeroSIVindependence;
  1638. ++WeakZeroSIVsuccesses;
  1639. return true;
  1640. }
  1641. if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
  1642. // dependences caused by last iteration
  1643. if (Level < CommonLevels) {
  1644. Result.DV[Level].Direction &= Dependence::DVEntry::LE;
  1645. Result.DV[Level].PeelLast = true;
  1646. ++WeakZeroSIVsuccesses;
  1647. }
  1648. return false;
  1649. }
  1650. }
  1651. // check that Delta/SrcCoeff >= 0
  1652. // really check that NewDelta >= 0
  1653. if (SE->isKnownNegative(NewDelta)) {
  1654. // No dependence, newDelta < 0
  1655. ++WeakZeroSIVindependence;
  1656. ++WeakZeroSIVsuccesses;
  1657. return true;
  1658. }
  1659. // if SrcCoeff doesn't divide Delta, then no dependence
  1660. if (isa<SCEVConstant>(Delta) &&
  1661. !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
  1662. ++WeakZeroSIVindependence;
  1663. ++WeakZeroSIVsuccesses;
  1664. return true;
  1665. }
  1666. return false;
  1667. }
  1668. // weakZeroDstSIVtest -
  1669. // From the paper, Practical Dependence Testing, Section 4.2.2
  1670. //
  1671. // When we have a pair of subscripts of the form [c1 + a*i] and [c2],
  1672. // where i is an induction variable, c1 and c2 are loop invariant,
  1673. // and a is a constant, we can solve it exactly using the
  1674. // Weak-Zero SIV test.
  1675. //
  1676. // Given
  1677. //
  1678. // c1 + a*i = c2
  1679. //
  1680. // we get
  1681. //
  1682. // i = (c2 - c1)/a
  1683. //
  1684. // If i is not an integer, there's no dependence.
  1685. // If i < 0 or > UB, there's no dependence.
  1686. // If i = 0, the direction is <= and peeling the
  1687. // 1st iteration will break the dependence.
  1688. // If i = UB, the direction is >= and peeling the
  1689. // last iteration will break the dependence.
  1690. // Otherwise, the direction is *.
  1691. //
  1692. // Can prove independence. Failing that, we can sometimes refine
  1693. // the directions. Can sometimes show that first or last
  1694. // iteration carries all the dependences (so worth peeling).
  1695. //
  1696. // (see also weakZeroSrcSIVtest)
  1697. //
  1698. // Return true if dependence disproved.
  1699. bool DependenceInfo::weakZeroDstSIVtest(const SCEV *SrcCoeff,
  1700. const SCEV *SrcConst,
  1701. const SCEV *DstConst,
  1702. const Loop *CurLoop, unsigned Level,
  1703. FullDependence &Result,
  1704. Constraint &NewConstraint) const {
  1705. // For the WeakSIV test, it's possible the loop isn't common to the
  1706. // Src and Dst loops. If it isn't, then there's no need to record a direction.
  1707. LLVM_DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n");
  1708. LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n");
  1709. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
  1710. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
  1711. ++WeakZeroSIVapplications;
  1712. assert(0 < Level && Level <= SrcLevels && "Level out of range");
  1713. Level--;
  1714. Result.Consistent = false;
  1715. const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
  1716. NewConstraint.setLine(SrcCoeff, SE->getZero(Delta->getType()), Delta,
  1717. CurLoop);
  1718. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1719. if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) {
  1720. if (Level < CommonLevels) {
  1721. Result.DV[Level].Direction &= Dependence::DVEntry::LE;
  1722. Result.DV[Level].PeelFirst = true;
  1723. ++WeakZeroSIVsuccesses;
  1724. }
  1725. return false; // dependences caused by first iteration
  1726. }
  1727. const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
  1728. if (!ConstCoeff)
  1729. return false;
  1730. const SCEV *AbsCoeff =
  1731. SE->isKnownNegative(ConstCoeff) ?
  1732. SE->getNegativeSCEV(ConstCoeff) : ConstCoeff;
  1733. const SCEV *NewDelta =
  1734. SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta;
  1735. // check that Delta/SrcCoeff < iteration count
  1736. // really check NewDelta < count*AbsCoeff
  1737. if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) {
  1738. LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n");
  1739. const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound);
  1740. if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) {
  1741. ++WeakZeroSIVindependence;
  1742. ++WeakZeroSIVsuccesses;
  1743. return true;
  1744. }
  1745. if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) {
  1746. // dependences caused by last iteration
  1747. if (Level < CommonLevels) {
  1748. Result.DV[Level].Direction &= Dependence::DVEntry::GE;
  1749. Result.DV[Level].PeelLast = true;
  1750. ++WeakZeroSIVsuccesses;
  1751. }
  1752. return false;
  1753. }
  1754. }
  1755. // check that Delta/SrcCoeff >= 0
  1756. // really check that NewDelta >= 0
  1757. if (SE->isKnownNegative(NewDelta)) {
  1758. // No dependence, newDelta < 0
  1759. ++WeakZeroSIVindependence;
  1760. ++WeakZeroSIVsuccesses;
  1761. return true;
  1762. }
  1763. // if SrcCoeff doesn't divide Delta, then no dependence
  1764. if (isa<SCEVConstant>(Delta) &&
  1765. !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) {
  1766. ++WeakZeroSIVindependence;
  1767. ++WeakZeroSIVsuccesses;
  1768. return true;
  1769. }
  1770. return false;
  1771. }
  1772. // exactRDIVtest - Tests the RDIV subscript pair for dependence.
  1773. // Things of the form [c1 + a*i] and [c2 + b*j],
  1774. // where i and j are induction variable, c1 and c2 are loop invariant,
  1775. // and a and b are constants.
  1776. // Returns true if any possible dependence is disproved.
  1777. // Marks the result as inconsistent.
  1778. // Works in some cases that symbolicRDIVtest doesn't, and vice versa.
  1779. bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
  1780. const SCEV *SrcConst, const SCEV *DstConst,
  1781. const Loop *SrcLoop, const Loop *DstLoop,
  1782. FullDependence &Result) const {
  1783. LLVM_DEBUG(dbgs() << "\tExact RDIV test\n");
  1784. LLVM_DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n");
  1785. LLVM_DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n");
  1786. LLVM_DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n");
  1787. LLVM_DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n");
  1788. ++ExactRDIVapplications;
  1789. Result.Consistent = false;
  1790. const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
  1791. LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
  1792. const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta);
  1793. const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff);
  1794. const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff);
  1795. if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff)
  1796. return false;
  1797. // find gcd
  1798. APInt G, X, Y;
  1799. APInt AM = ConstSrcCoeff->getAPInt();
  1800. APInt BM = ConstDstCoeff->getAPInt();
  1801. APInt CM = ConstDelta->getAPInt();
  1802. unsigned Bits = AM.getBitWidth();
  1803. if (findGCD(Bits, AM, BM, CM, G, X, Y)) {
  1804. // gcd doesn't divide Delta, no dependence
  1805. ++ExactRDIVindependence;
  1806. return true;
  1807. }
  1808. LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n");
  1809. // since SCEV construction seems to normalize, LM = 0
  1810. APInt SrcUM(Bits, 1, true);
  1811. bool SrcUMvalid = false;
  1812. // SrcUM is perhaps unavailable, let's check
  1813. if (const SCEVConstant *UpperBound =
  1814. collectConstantUpperBound(SrcLoop, Delta->getType())) {
  1815. SrcUM = UpperBound->getAPInt();
  1816. LLVM_DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n");
  1817. SrcUMvalid = true;
  1818. }
  1819. APInt DstUM(Bits, 1, true);
  1820. bool DstUMvalid = false;
  1821. // UM is perhaps unavailable, let's check
  1822. if (const SCEVConstant *UpperBound =
  1823. collectConstantUpperBound(DstLoop, Delta->getType())) {
  1824. DstUM = UpperBound->getAPInt();
  1825. LLVM_DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n");
  1826. DstUMvalid = true;
  1827. }
  1828. APInt TU(APInt::getSignedMaxValue(Bits));
  1829. APInt TL(APInt::getSignedMinValue(Bits));
  1830. APInt TC = CM.sdiv(G);
  1831. APInt TX = X * TC;
  1832. APInt TY = Y * TC;
  1833. LLVM_DEBUG(dbgs() << "\t TC = " << TC << "\n");
  1834. LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n");
  1835. LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n");
  1836. SmallVector<APInt, 2> TLVec, TUVec;
  1837. APInt TB = BM.sdiv(G);
  1838. if (TB.sgt(0)) {
  1839. TLVec.push_back(ceilingOfQuotient(-TX, TB));
  1840. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1841. if (SrcUMvalid) {
  1842. TUVec.push_back(floorOfQuotient(SrcUM - TX, TB));
  1843. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1844. }
  1845. } else {
  1846. TUVec.push_back(floorOfQuotient(-TX, TB));
  1847. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1848. if (SrcUMvalid) {
  1849. TLVec.push_back(ceilingOfQuotient(SrcUM - TX, TB));
  1850. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1851. }
  1852. }
  1853. APInt TA = AM.sdiv(G);
  1854. if (TA.sgt(0)) {
  1855. TLVec.push_back(ceilingOfQuotient(-TY, TA));
  1856. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1857. if (DstUMvalid) {
  1858. TUVec.push_back(floorOfQuotient(DstUM - TY, TA));
  1859. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1860. }
  1861. } else {
  1862. TUVec.push_back(floorOfQuotient(-TY, TA));
  1863. LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n");
  1864. if (DstUMvalid) {
  1865. TLVec.push_back(ceilingOfQuotient(DstUM - TY, TA));
  1866. LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n");
  1867. }
  1868. }
  1869. if (TLVec.empty() || TUVec.empty())
  1870. return false;
  1871. LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n");
  1872. LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n");
  1873. TL = APIntOps::smax(TLVec.front(), TLVec.back());
  1874. TU = APIntOps::smin(TUVec.front(), TUVec.back());
  1875. LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n");
  1876. LLVM_DEBUG(dbgs() << "\t TU = " << TU << "\n");
  1877. if (TL.sgt(TU))
  1878. ++ExactRDIVindependence;
  1879. return TL.sgt(TU);
  1880. }
  1881. // symbolicRDIVtest -
  1882. // In Section 4.5 of the Practical Dependence Testing paper,the authors
  1883. // introduce a special case of Banerjee's Inequalities (also called the
  1884. // Extreme-Value Test) that can handle some of the SIV and RDIV cases,
  1885. // particularly cases with symbolics. Since it's only able to disprove
  1886. // dependence (not compute distances or directions), we'll use it as a
  1887. // fall back for the other tests.
  1888. //
  1889. // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
  1890. // where i and j are induction variables and c1 and c2 are loop invariants,
  1891. // we can use the symbolic tests to disprove some dependences, serving as a
  1892. // backup for the RDIV test. Note that i and j can be the same variable,
  1893. // letting this test serve as a backup for the various SIV tests.
  1894. //
  1895. // For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some
  1896. // 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized)
  1897. // loop bounds for the i and j loops, respectively. So, ...
  1898. //
  1899. // c1 + a1*i = c2 + a2*j
  1900. // a1*i - a2*j = c2 - c1
  1901. //
  1902. // To test for a dependence, we compute c2 - c1 and make sure it's in the
  1903. // range of the maximum and minimum possible values of a1*i - a2*j.
  1904. // Considering the signs of a1 and a2, we have 4 possible cases:
  1905. //
  1906. // 1) If a1 >= 0 and a2 >= 0, then
  1907. // a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0
  1908. // -a2*N2 <= c2 - c1 <= a1*N1
  1909. //
  1910. // 2) If a1 >= 0 and a2 <= 0, then
  1911. // a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2
  1912. // 0 <= c2 - c1 <= a1*N1 - a2*N2
  1913. //
  1914. // 3) If a1 <= 0 and a2 >= 0, then
  1915. // a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0
  1916. // a1*N1 - a2*N2 <= c2 - c1 <= 0
  1917. //
  1918. // 4) If a1 <= 0 and a2 <= 0, then
  1919. // a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2
  1920. // a1*N1 <= c2 - c1 <= -a2*N2
  1921. //
  1922. // return true if dependence disproved
  1923. bool DependenceInfo::symbolicRDIVtest(const SCEV *A1, const SCEV *A2,
  1924. const SCEV *C1, const SCEV *C2,
  1925. const Loop *Loop1,
  1926. const Loop *Loop2) const {
  1927. ++SymbolicRDIVapplications;
  1928. LLVM_DEBUG(dbgs() << "\ttry symbolic RDIV test\n");
  1929. LLVM_DEBUG(dbgs() << "\t A1 = " << *A1);
  1930. LLVM_DEBUG(dbgs() << ", type = " << *A1->getType() << "\n");
  1931. LLVM_DEBUG(dbgs() << "\t A2 = " << *A2 << "\n");
  1932. LLVM_DEBUG(dbgs() << "\t C1 = " << *C1 << "\n");
  1933. LLVM_DEBUG(dbgs() << "\t C2 = " << *C2 << "\n");
  1934. const SCEV *N1 = collectUpperBound(Loop1, A1->getType());
  1935. const SCEV *N2 = collectUpperBound(Loop2, A1->getType());
  1936. LLVM_DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n");
  1937. LLVM_DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n");
  1938. const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1);
  1939. const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2);
  1940. LLVM_DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n");
  1941. LLVM_DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n");
  1942. if (SE->isKnownNonNegative(A1)) {
  1943. if (SE->isKnownNonNegative(A2)) {
  1944. // A1 >= 0 && A2 >= 0
  1945. if (N1) {
  1946. // make sure that c2 - c1 <= a1*N1
  1947. const SCEV *A1N1 = SE->getMulExpr(A1, N1);
  1948. LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
  1949. if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) {
  1950. ++SymbolicRDIVindependence;
  1951. return true;
  1952. }
  1953. }
  1954. if (N2) {
  1955. // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2
  1956. const SCEV *A2N2 = SE->getMulExpr(A2, N2);
  1957. LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
  1958. if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) {
  1959. ++SymbolicRDIVindependence;
  1960. return true;
  1961. }
  1962. }
  1963. }
  1964. else if (SE->isKnownNonPositive(A2)) {
  1965. // a1 >= 0 && a2 <= 0
  1966. if (N1 && N2) {
  1967. // make sure that c2 - c1 <= a1*N1 - a2*N2
  1968. const SCEV *A1N1 = SE->getMulExpr(A1, N1);
  1969. const SCEV *A2N2 = SE->getMulExpr(A2, N2);
  1970. const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
  1971. LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
  1972. if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) {
  1973. ++SymbolicRDIVindependence;
  1974. return true;
  1975. }
  1976. }
  1977. // make sure that 0 <= c2 - c1
  1978. if (SE->isKnownNegative(C2_C1)) {
  1979. ++SymbolicRDIVindependence;
  1980. return true;
  1981. }
  1982. }
  1983. }
  1984. else if (SE->isKnownNonPositive(A1)) {
  1985. if (SE->isKnownNonNegative(A2)) {
  1986. // a1 <= 0 && a2 >= 0
  1987. if (N1 && N2) {
  1988. // make sure that a1*N1 - a2*N2 <= c2 - c1
  1989. const SCEV *A1N1 = SE->getMulExpr(A1, N1);
  1990. const SCEV *A2N2 = SE->getMulExpr(A2, N2);
  1991. const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2);
  1992. LLVM_DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n");
  1993. if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) {
  1994. ++SymbolicRDIVindependence;
  1995. return true;
  1996. }
  1997. }
  1998. // make sure that c2 - c1 <= 0
  1999. if (SE->isKnownPositive(C2_C1)) {
  2000. ++SymbolicRDIVindependence;
  2001. return true;
  2002. }
  2003. }
  2004. else if (SE->isKnownNonPositive(A2)) {
  2005. // a1 <= 0 && a2 <= 0
  2006. if (N1) {
  2007. // make sure that a1*N1 <= c2 - c1
  2008. const SCEV *A1N1 = SE->getMulExpr(A1, N1);
  2009. LLVM_DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n");
  2010. if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) {
  2011. ++SymbolicRDIVindependence;
  2012. return true;
  2013. }
  2014. }
  2015. if (N2) {
  2016. // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2
  2017. const SCEV *A2N2 = SE->getMulExpr(A2, N2);
  2018. LLVM_DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n");
  2019. if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) {
  2020. ++SymbolicRDIVindependence;
  2021. return true;
  2022. }
  2023. }
  2024. }
  2025. }
  2026. return false;
  2027. }
  2028. // testSIV -
  2029. // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i]
  2030. // where i is an induction variable, c1 and c2 are loop invariant, and a1 and
  2031. // a2 are constant, we attack it with an SIV test. While they can all be
  2032. // solved with the Exact SIV test, it's worthwhile to use simpler tests when
  2033. // they apply; they're cheaper and sometimes more precise.
  2034. //
  2035. // Return true if dependence disproved.
  2036. bool DependenceInfo::testSIV(const SCEV *Src, const SCEV *Dst, unsigned &Level,
  2037. FullDependence &Result, Constraint &NewConstraint,
  2038. const SCEV *&SplitIter) const {
  2039. LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
  2040. LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
  2041. const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
  2042. const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
  2043. if (SrcAddRec && DstAddRec) {
  2044. const SCEV *SrcConst = SrcAddRec->getStart();
  2045. const SCEV *DstConst = DstAddRec->getStart();
  2046. const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
  2047. const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
  2048. const Loop *CurLoop = SrcAddRec->getLoop();
  2049. assert(CurLoop == DstAddRec->getLoop() &&
  2050. "both loops in SIV should be same");
  2051. Level = mapSrcLoop(CurLoop);
  2052. bool disproven;
  2053. if (SrcCoeff == DstCoeff)
  2054. disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
  2055. Level, Result, NewConstraint);
  2056. else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff))
  2057. disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
  2058. Level, Result, NewConstraint, SplitIter);
  2059. else
  2060. disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop,
  2061. Level, Result, NewConstraint);
  2062. return disproven ||
  2063. gcdMIVtest(Src, Dst, Result) ||
  2064. symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop);
  2065. }
  2066. if (SrcAddRec) {
  2067. const SCEV *SrcConst = SrcAddRec->getStart();
  2068. const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
  2069. const SCEV *DstConst = Dst;
  2070. const Loop *CurLoop = SrcAddRec->getLoop();
  2071. Level = mapSrcLoop(CurLoop);
  2072. return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop,
  2073. Level, Result, NewConstraint) ||
  2074. gcdMIVtest(Src, Dst, Result);
  2075. }
  2076. if (DstAddRec) {
  2077. const SCEV *DstConst = DstAddRec->getStart();
  2078. const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE);
  2079. const SCEV *SrcConst = Src;
  2080. const Loop *CurLoop = DstAddRec->getLoop();
  2081. Level = mapDstLoop(CurLoop);
  2082. return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst,
  2083. CurLoop, Level, Result, NewConstraint) ||
  2084. gcdMIVtest(Src, Dst, Result);
  2085. }
  2086. llvm_unreachable("SIV test expected at least one AddRec");
  2087. return false;
  2088. }
  2089. // testRDIV -
  2090. // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j]
  2091. // where i and j are induction variables, c1 and c2 are loop invariant,
  2092. // and a1 and a2 are constant, we can solve it exactly with an easy adaptation
  2093. // of the Exact SIV test, the Restricted Double Index Variable (RDIV) test.
  2094. // It doesn't make sense to talk about distance or direction in this case,
  2095. // so there's no point in making special versions of the Strong SIV test or
  2096. // the Weak-crossing SIV test.
  2097. //
  2098. // With minor algebra, this test can also be used for things like
  2099. // [c1 + a1*i + a2*j][c2].
  2100. //
  2101. // Return true if dependence disproved.
  2102. bool DependenceInfo::testRDIV(const SCEV *Src, const SCEV *Dst,
  2103. FullDependence &Result) const {
  2104. // we have 3 possible situations here:
  2105. // 1) [a*i + b] and [c*j + d]
  2106. // 2) [a*i + c*j + b] and [d]
  2107. // 3) [b] and [a*i + c*j + d]
  2108. // We need to find what we've got and get organized
  2109. const SCEV *SrcConst, *DstConst;
  2110. const SCEV *SrcCoeff, *DstCoeff;
  2111. const Loop *SrcLoop, *DstLoop;
  2112. LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
  2113. LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
  2114. const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src);
  2115. const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst);
  2116. if (SrcAddRec && DstAddRec) {
  2117. SrcConst = SrcAddRec->getStart();
  2118. SrcCoeff = SrcAddRec->getStepRecurrence(*SE);
  2119. SrcLoop = SrcAddRec->getLoop();
  2120. DstConst = DstAddRec->getStart();
  2121. DstCoeff = DstAddRec->getStepRecurrence(*SE);
  2122. DstLoop = DstAddRec->getLoop();
  2123. }
  2124. else if (SrcAddRec) {
  2125. if (const SCEVAddRecExpr *tmpAddRec =
  2126. dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) {
  2127. SrcConst = tmpAddRec->getStart();
  2128. SrcCoeff = tmpAddRec->getStepRecurrence(*SE);
  2129. SrcLoop = tmpAddRec->getLoop();
  2130. DstConst = Dst;
  2131. DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE));
  2132. DstLoop = SrcAddRec->getLoop();
  2133. }
  2134. else
  2135. llvm_unreachable("RDIV reached by surprising SCEVs");
  2136. }
  2137. else if (DstAddRec) {
  2138. if (const SCEVAddRecExpr *tmpAddRec =
  2139. dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) {
  2140. DstConst = tmpAddRec->getStart();
  2141. DstCoeff = tmpAddRec->getStepRecurrence(*SE);
  2142. DstLoop = tmpAddRec->getLoop();
  2143. SrcConst = Src;
  2144. SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE));
  2145. SrcLoop = DstAddRec->getLoop();
  2146. }
  2147. else
  2148. llvm_unreachable("RDIV reached by surprising SCEVs");
  2149. }
  2150. else
  2151. llvm_unreachable("RDIV expected at least one AddRec");
  2152. return exactRDIVtest(SrcCoeff, DstCoeff,
  2153. SrcConst, DstConst,
  2154. SrcLoop, DstLoop,
  2155. Result) ||
  2156. gcdMIVtest(Src, Dst, Result) ||
  2157. symbolicRDIVtest(SrcCoeff, DstCoeff,
  2158. SrcConst, DstConst,
  2159. SrcLoop, DstLoop);
  2160. }
  2161. // Tests the single-subscript MIV pair (Src and Dst) for dependence.
  2162. // Return true if dependence disproved.
  2163. // Can sometimes refine direction vectors.
  2164. bool DependenceInfo::testMIV(const SCEV *Src, const SCEV *Dst,
  2165. const SmallBitVector &Loops,
  2166. FullDependence &Result) const {
  2167. LLVM_DEBUG(dbgs() << " src = " << *Src << "\n");
  2168. LLVM_DEBUG(dbgs() << " dst = " << *Dst << "\n");
  2169. Result.Consistent = false;
  2170. return gcdMIVtest(Src, Dst, Result) ||
  2171. banerjeeMIVtest(Src, Dst, Loops, Result);
  2172. }
  2173. // Given a product, e.g., 10*X*Y, returns the first constant operand,
  2174. // in this case 10. If there is no constant part, returns NULL.
  2175. static
  2176. const SCEVConstant *getConstantPart(const SCEV *Expr) {
  2177. if (const auto *Constant = dyn_cast<SCEVConstant>(Expr))
  2178. return Constant;
  2179. else if (const auto *Product = dyn_cast<SCEVMulExpr>(Expr))
  2180. if (const auto *Constant = dyn_cast<SCEVConstant>(Product->getOperand(0)))
  2181. return Constant;
  2182. return nullptr;
  2183. }
  2184. //===----------------------------------------------------------------------===//
  2185. // gcdMIVtest -
  2186. // Tests an MIV subscript pair for dependence.
  2187. // Returns true if any possible dependence is disproved.
  2188. // Marks the result as inconsistent.
  2189. // Can sometimes disprove the equal direction for 1 or more loops,
  2190. // as discussed in Michael Wolfe's book,
  2191. // High Performance Compilers for Parallel Computing, page 235.
  2192. //
  2193. // We spend some effort (code!) to handle cases like
  2194. // [10*i + 5*N*j + 15*M + 6], where i and j are induction variables,
  2195. // but M and N are just loop-invariant variables.
  2196. // This should help us handle linearized subscripts;
  2197. // also makes this test a useful backup to the various SIV tests.
  2198. //
  2199. // It occurs to me that the presence of loop-invariant variables
  2200. // changes the nature of the test from "greatest common divisor"
  2201. // to "a common divisor".
  2202. bool DependenceInfo::gcdMIVtest(const SCEV *Src, const SCEV *Dst,
  2203. FullDependence &Result) const {
  2204. LLVM_DEBUG(dbgs() << "starting gcd\n");
  2205. ++GCDapplications;
  2206. unsigned BitWidth = SE->getTypeSizeInBits(Src->getType());
  2207. APInt RunningGCD = APInt::getZero(BitWidth);
  2208. // Examine Src coefficients.
  2209. // Compute running GCD and record source constant.
  2210. // Because we're looking for the constant at the end of the chain,
  2211. // we can't quit the loop just because the GCD == 1.
  2212. const SCEV *Coefficients = Src;
  2213. while (const SCEVAddRecExpr *AddRec =
  2214. dyn_cast<SCEVAddRecExpr>(Coefficients)) {
  2215. const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
  2216. // If the coefficient is the product of a constant and other stuff,
  2217. // we can use the constant in the GCD computation.
  2218. const auto *Constant = getConstantPart(Coeff);
  2219. if (!Constant)
  2220. return false;
  2221. APInt ConstCoeff = Constant->getAPInt();
  2222. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
  2223. Coefficients = AddRec->getStart();
  2224. }
  2225. const SCEV *SrcConst = Coefficients;
  2226. // Examine Dst coefficients.
  2227. // Compute running GCD and record destination constant.
  2228. // Because we're looking for the constant at the end of the chain,
  2229. // we can't quit the loop just because the GCD == 1.
  2230. Coefficients = Dst;
  2231. while (const SCEVAddRecExpr *AddRec =
  2232. dyn_cast<SCEVAddRecExpr>(Coefficients)) {
  2233. const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
  2234. // If the coefficient is the product of a constant and other stuff,
  2235. // we can use the constant in the GCD computation.
  2236. const auto *Constant = getConstantPart(Coeff);
  2237. if (!Constant)
  2238. return false;
  2239. APInt ConstCoeff = Constant->getAPInt();
  2240. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
  2241. Coefficients = AddRec->getStart();
  2242. }
  2243. const SCEV *DstConst = Coefficients;
  2244. APInt ExtraGCD = APInt::getZero(BitWidth);
  2245. const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
  2246. LLVM_DEBUG(dbgs() << " Delta = " << *Delta << "\n");
  2247. const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta);
  2248. if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) {
  2249. // If Delta is a sum of products, we may be able to make further progress.
  2250. for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) {
  2251. const SCEV *Operand = Sum->getOperand(Op);
  2252. if (isa<SCEVConstant>(Operand)) {
  2253. assert(!Constant && "Surprised to find multiple constants");
  2254. Constant = cast<SCEVConstant>(Operand);
  2255. }
  2256. else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) {
  2257. // Search for constant operand to participate in GCD;
  2258. // If none found; return false.
  2259. const SCEVConstant *ConstOp = getConstantPart(Product);
  2260. if (!ConstOp)
  2261. return false;
  2262. APInt ConstOpValue = ConstOp->getAPInt();
  2263. ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD,
  2264. ConstOpValue.abs());
  2265. }
  2266. else
  2267. return false;
  2268. }
  2269. }
  2270. if (!Constant)
  2271. return false;
  2272. APInt ConstDelta = cast<SCEVConstant>(Constant)->getAPInt();
  2273. LLVM_DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n");
  2274. if (ConstDelta == 0)
  2275. return false;
  2276. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD);
  2277. LLVM_DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n");
  2278. APInt Remainder = ConstDelta.srem(RunningGCD);
  2279. if (Remainder != 0) {
  2280. ++GCDindependence;
  2281. return true;
  2282. }
  2283. // Try to disprove equal directions.
  2284. // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1],
  2285. // the code above can't disprove the dependence because the GCD = 1.
  2286. // So we consider what happen if i = i' and what happens if j = j'.
  2287. // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1],
  2288. // which is infeasible, so we can disallow the = direction for the i level.
  2289. // Setting j = j' doesn't help matters, so we end up with a direction vector
  2290. // of [<>, *]
  2291. //
  2292. // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5],
  2293. // we need to remember that the constant part is 5 and the RunningGCD should
  2294. // be initialized to ExtraGCD = 30.
  2295. LLVM_DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n');
  2296. bool Improved = false;
  2297. Coefficients = Src;
  2298. while (const SCEVAddRecExpr *AddRec =
  2299. dyn_cast<SCEVAddRecExpr>(Coefficients)) {
  2300. Coefficients = AddRec->getStart();
  2301. const Loop *CurLoop = AddRec->getLoop();
  2302. RunningGCD = ExtraGCD;
  2303. const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE);
  2304. const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff);
  2305. const SCEV *Inner = Src;
  2306. while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
  2307. AddRec = cast<SCEVAddRecExpr>(Inner);
  2308. const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
  2309. if (CurLoop == AddRec->getLoop())
  2310. ; // SrcCoeff == Coeff
  2311. else {
  2312. // If the coefficient is the product of a constant and other stuff,
  2313. // we can use the constant in the GCD computation.
  2314. Constant = getConstantPart(Coeff);
  2315. if (!Constant)
  2316. return false;
  2317. APInt ConstCoeff = Constant->getAPInt();
  2318. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
  2319. }
  2320. Inner = AddRec->getStart();
  2321. }
  2322. Inner = Dst;
  2323. while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) {
  2324. AddRec = cast<SCEVAddRecExpr>(Inner);
  2325. const SCEV *Coeff = AddRec->getStepRecurrence(*SE);
  2326. if (CurLoop == AddRec->getLoop())
  2327. DstCoeff = Coeff;
  2328. else {
  2329. // If the coefficient is the product of a constant and other stuff,
  2330. // we can use the constant in the GCD computation.
  2331. Constant = getConstantPart(Coeff);
  2332. if (!Constant)
  2333. return false;
  2334. APInt ConstCoeff = Constant->getAPInt();
  2335. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
  2336. }
  2337. Inner = AddRec->getStart();
  2338. }
  2339. Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff);
  2340. // If the coefficient is the product of a constant and other stuff,
  2341. // we can use the constant in the GCD computation.
  2342. Constant = getConstantPart(Delta);
  2343. if (!Constant)
  2344. // The difference of the two coefficients might not be a product
  2345. // or constant, in which case we give up on this direction.
  2346. continue;
  2347. APInt ConstCoeff = Constant->getAPInt();
  2348. RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs());
  2349. LLVM_DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n");
  2350. if (RunningGCD != 0) {
  2351. Remainder = ConstDelta.srem(RunningGCD);
  2352. LLVM_DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n");
  2353. if (Remainder != 0) {
  2354. unsigned Level = mapSrcLoop(CurLoop);
  2355. Result.DV[Level - 1].Direction &= ~Dependence::DVEntry::EQ;
  2356. Improved = true;
  2357. }
  2358. }
  2359. }
  2360. if (Improved)
  2361. ++GCDsuccesses;
  2362. LLVM_DEBUG(dbgs() << "all done\n");
  2363. return false;
  2364. }
  2365. //===----------------------------------------------------------------------===//
  2366. // banerjeeMIVtest -
  2367. // Use Banerjee's Inequalities to test an MIV subscript pair.
  2368. // (Wolfe, in the race-car book, calls this the Extreme Value Test.)
  2369. // Generally follows the discussion in Section 2.5.2 of
  2370. //
  2371. // Optimizing Supercompilers for Supercomputers
  2372. // Michael Wolfe
  2373. //
  2374. // The inequalities given on page 25 are simplified in that loops are
  2375. // normalized so that the lower bound is always 0 and the stride is always 1.
  2376. // For example, Wolfe gives
  2377. //
  2378. // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
  2379. //
  2380. // where A_k is the coefficient of the kth index in the source subscript,
  2381. // B_k is the coefficient of the kth index in the destination subscript,
  2382. // U_k is the upper bound of the kth index, L_k is the lower bound of the Kth
  2383. // index, and N_k is the stride of the kth index. Since all loops are normalized
  2384. // by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the
  2385. // equation to
  2386. //
  2387. // LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1
  2388. // = (A^-_k - B_k)^- (U_k - 1) - B_k
  2389. //
  2390. // Similar simplifications are possible for the other equations.
  2391. //
  2392. // When we can't determine the number of iterations for a loop,
  2393. // we use NULL as an indicator for the worst case, infinity.
  2394. // When computing the upper bound, NULL denotes +inf;
  2395. // for the lower bound, NULL denotes -inf.
  2396. //
  2397. // Return true if dependence disproved.
  2398. bool DependenceInfo::banerjeeMIVtest(const SCEV *Src, const SCEV *Dst,
  2399. const SmallBitVector &Loops,
  2400. FullDependence &Result) const {
  2401. LLVM_DEBUG(dbgs() << "starting Banerjee\n");
  2402. ++BanerjeeApplications;
  2403. LLVM_DEBUG(dbgs() << " Src = " << *Src << '\n');
  2404. const SCEV *A0;
  2405. CoefficientInfo *A = collectCoeffInfo(Src, true, A0);
  2406. LLVM_DEBUG(dbgs() << " Dst = " << *Dst << '\n');
  2407. const SCEV *B0;
  2408. CoefficientInfo *B = collectCoeffInfo(Dst, false, B0);
  2409. BoundInfo *Bound = new BoundInfo[MaxLevels + 1];
  2410. const SCEV *Delta = SE->getMinusSCEV(B0, A0);
  2411. LLVM_DEBUG(dbgs() << "\tDelta = " << *Delta << '\n');
  2412. // Compute bounds for all the * directions.
  2413. LLVM_DEBUG(dbgs() << "\tBounds[*]\n");
  2414. for (unsigned K = 1; K <= MaxLevels; ++K) {
  2415. Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations;
  2416. Bound[K].Direction = Dependence::DVEntry::ALL;
  2417. Bound[K].DirSet = Dependence::DVEntry::NONE;
  2418. findBoundsALL(A, B, Bound, K);
  2419. #ifndef NDEBUG
  2420. LLVM_DEBUG(dbgs() << "\t " << K << '\t');
  2421. if (Bound[K].Lower[Dependence::DVEntry::ALL])
  2422. LLVM_DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t');
  2423. else
  2424. LLVM_DEBUG(dbgs() << "-inf\t");
  2425. if (Bound[K].Upper[Dependence::DVEntry::ALL])
  2426. LLVM_DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n');
  2427. else
  2428. LLVM_DEBUG(dbgs() << "+inf\n");
  2429. #endif
  2430. }
  2431. // Test the *, *, *, ... case.
  2432. bool Disproved = false;
  2433. if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) {
  2434. // Explore the direction vector hierarchy.
  2435. unsigned DepthExpanded = 0;
  2436. unsigned NewDeps = exploreDirections(1, A, B, Bound,
  2437. Loops, DepthExpanded, Delta);
  2438. if (NewDeps > 0) {
  2439. bool Improved = false;
  2440. for (unsigned K = 1; K <= CommonLevels; ++K) {
  2441. if (Loops[K]) {
  2442. unsigned Old = Result.DV[K - 1].Direction;
  2443. Result.DV[K - 1].Direction = Old & Bound[K].DirSet;
  2444. Improved |= Old != Result.DV[K - 1].Direction;
  2445. if (!Result.DV[K - 1].Direction) {
  2446. Improved = false;
  2447. Disproved = true;
  2448. break;
  2449. }
  2450. }
  2451. }
  2452. if (Improved)
  2453. ++BanerjeeSuccesses;
  2454. }
  2455. else {
  2456. ++BanerjeeIndependence;
  2457. Disproved = true;
  2458. }
  2459. }
  2460. else {
  2461. ++BanerjeeIndependence;
  2462. Disproved = true;
  2463. }
  2464. delete [] Bound;
  2465. delete [] A;
  2466. delete [] B;
  2467. return Disproved;
  2468. }
  2469. // Hierarchically expands the direction vector
  2470. // search space, combining the directions of discovered dependences
  2471. // in the DirSet field of Bound. Returns the number of distinct
  2472. // dependences discovered. If the dependence is disproved,
  2473. // it will return 0.
  2474. unsigned DependenceInfo::exploreDirections(unsigned Level, CoefficientInfo *A,
  2475. CoefficientInfo *B, BoundInfo *Bound,
  2476. const SmallBitVector &Loops,
  2477. unsigned &DepthExpanded,
  2478. const SCEV *Delta) const {
  2479. // This algorithm has worst case complexity of O(3^n), where 'n' is the number
  2480. // of common loop levels. To avoid excessive compile-time, pessimize all the
  2481. // results and immediately return when the number of common levels is beyond
  2482. // the given threshold.
  2483. if (CommonLevels > MIVMaxLevelThreshold) {
  2484. LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
  2485. "direction exploration is terminated.\n");
  2486. for (unsigned K = 1; K <= CommonLevels; ++K)
  2487. if (Loops[K])
  2488. Bound[K].DirSet = Dependence::DVEntry::ALL;
  2489. return 1;
  2490. }
  2491. if (Level > CommonLevels) {
  2492. // record result
  2493. LLVM_DEBUG(dbgs() << "\t[");
  2494. for (unsigned K = 1; K <= CommonLevels; ++K) {
  2495. if (Loops[K]) {
  2496. Bound[K].DirSet |= Bound[K].Direction;
  2497. #ifndef NDEBUG
  2498. switch (Bound[K].Direction) {
  2499. case Dependence::DVEntry::LT:
  2500. LLVM_DEBUG(dbgs() << " <");
  2501. break;
  2502. case Dependence::DVEntry::EQ:
  2503. LLVM_DEBUG(dbgs() << " =");
  2504. break;
  2505. case Dependence::DVEntry::GT:
  2506. LLVM_DEBUG(dbgs() << " >");
  2507. break;
  2508. case Dependence::DVEntry::ALL:
  2509. LLVM_DEBUG(dbgs() << " *");
  2510. break;
  2511. default:
  2512. llvm_unreachable("unexpected Bound[K].Direction");
  2513. }
  2514. #endif
  2515. }
  2516. }
  2517. LLVM_DEBUG(dbgs() << " ]\n");
  2518. return 1;
  2519. }
  2520. if (Loops[Level]) {
  2521. if (Level > DepthExpanded) {
  2522. DepthExpanded = Level;
  2523. // compute bounds for <, =, > at current level
  2524. findBoundsLT(A, B, Bound, Level);
  2525. findBoundsGT(A, B, Bound, Level);
  2526. findBoundsEQ(A, B, Bound, Level);
  2527. #ifndef NDEBUG
  2528. LLVM_DEBUG(dbgs() << "\tBound for level = " << Level << '\n');
  2529. LLVM_DEBUG(dbgs() << "\t <\t");
  2530. if (Bound[Level].Lower[Dependence::DVEntry::LT])
  2531. LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT]
  2532. << '\t');
  2533. else
  2534. LLVM_DEBUG(dbgs() << "-inf\t");
  2535. if (Bound[Level].Upper[Dependence::DVEntry::LT])
  2536. LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT]
  2537. << '\n');
  2538. else
  2539. LLVM_DEBUG(dbgs() << "+inf\n");
  2540. LLVM_DEBUG(dbgs() << "\t =\t");
  2541. if (Bound[Level].Lower[Dependence::DVEntry::EQ])
  2542. LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ]
  2543. << '\t');
  2544. else
  2545. LLVM_DEBUG(dbgs() << "-inf\t");
  2546. if (Bound[Level].Upper[Dependence::DVEntry::EQ])
  2547. LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ]
  2548. << '\n');
  2549. else
  2550. LLVM_DEBUG(dbgs() << "+inf\n");
  2551. LLVM_DEBUG(dbgs() << "\t >\t");
  2552. if (Bound[Level].Lower[Dependence::DVEntry::GT])
  2553. LLVM_DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT]
  2554. << '\t');
  2555. else
  2556. LLVM_DEBUG(dbgs() << "-inf\t");
  2557. if (Bound[Level].Upper[Dependence::DVEntry::GT])
  2558. LLVM_DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT]
  2559. << '\n');
  2560. else
  2561. LLVM_DEBUG(dbgs() << "+inf\n");
  2562. #endif
  2563. }
  2564. unsigned NewDeps = 0;
  2565. // test bounds for <, *, *, ...
  2566. if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta))
  2567. NewDeps += exploreDirections(Level + 1, A, B, Bound,
  2568. Loops, DepthExpanded, Delta);
  2569. // Test bounds for =, *, *, ...
  2570. if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta))
  2571. NewDeps += exploreDirections(Level + 1, A, B, Bound,
  2572. Loops, DepthExpanded, Delta);
  2573. // test bounds for >, *, *, ...
  2574. if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta))
  2575. NewDeps += exploreDirections(Level + 1, A, B, Bound,
  2576. Loops, DepthExpanded, Delta);
  2577. Bound[Level].Direction = Dependence::DVEntry::ALL;
  2578. return NewDeps;
  2579. }
  2580. else
  2581. return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta);
  2582. }
  2583. // Returns true iff the current bounds are plausible.
  2584. bool DependenceInfo::testBounds(unsigned char DirKind, unsigned Level,
  2585. BoundInfo *Bound, const SCEV *Delta) const {
  2586. Bound[Level].Direction = DirKind;
  2587. if (const SCEV *LowerBound = getLowerBound(Bound))
  2588. if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta))
  2589. return false;
  2590. if (const SCEV *UpperBound = getUpperBound(Bound))
  2591. if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound))
  2592. return false;
  2593. return true;
  2594. }
  2595. // Computes the upper and lower bounds for level K
  2596. // using the * direction. Records them in Bound.
  2597. // Wolfe gives the equations
  2598. //
  2599. // LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k
  2600. // UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k
  2601. //
  2602. // Since we normalize loops, we can simplify these equations to
  2603. //
  2604. // LB^*_k = (A^-_k - B^+_k)U_k
  2605. // UB^*_k = (A^+_k - B^-_k)U_k
  2606. //
  2607. // We must be careful to handle the case where the upper bound is unknown.
  2608. // Note that the lower bound is always <= 0
  2609. // and the upper bound is always >= 0.
  2610. void DependenceInfo::findBoundsALL(CoefficientInfo *A, CoefficientInfo *B,
  2611. BoundInfo *Bound, unsigned K) const {
  2612. Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity.
  2613. Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity.
  2614. if (Bound[K].Iterations) {
  2615. Bound[K].Lower[Dependence::DVEntry::ALL] =
  2616. SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart),
  2617. Bound[K].Iterations);
  2618. Bound[K].Upper[Dependence::DVEntry::ALL] =
  2619. SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart),
  2620. Bound[K].Iterations);
  2621. }
  2622. else {
  2623. // If the difference is 0, we won't need to know the number of iterations.
  2624. if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart))
  2625. Bound[K].Lower[Dependence::DVEntry::ALL] =
  2626. SE->getZero(A[K].Coeff->getType());
  2627. if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart))
  2628. Bound[K].Upper[Dependence::DVEntry::ALL] =
  2629. SE->getZero(A[K].Coeff->getType());
  2630. }
  2631. }
  2632. // Computes the upper and lower bounds for level K
  2633. // using the = direction. Records them in Bound.
  2634. // Wolfe gives the equations
  2635. //
  2636. // LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k
  2637. // UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k
  2638. //
  2639. // Since we normalize loops, we can simplify these equations to
  2640. //
  2641. // LB^=_k = (A_k - B_k)^- U_k
  2642. // UB^=_k = (A_k - B_k)^+ U_k
  2643. //
  2644. // We must be careful to handle the case where the upper bound is unknown.
  2645. // Note that the lower bound is always <= 0
  2646. // and the upper bound is always >= 0.
  2647. void DependenceInfo::findBoundsEQ(CoefficientInfo *A, CoefficientInfo *B,
  2648. BoundInfo *Bound, unsigned K) const {
  2649. Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity.
  2650. Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity.
  2651. if (Bound[K].Iterations) {
  2652. const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
  2653. const SCEV *NegativePart = getNegativePart(Delta);
  2654. Bound[K].Lower[Dependence::DVEntry::EQ] =
  2655. SE->getMulExpr(NegativePart, Bound[K].Iterations);
  2656. const SCEV *PositivePart = getPositivePart(Delta);
  2657. Bound[K].Upper[Dependence::DVEntry::EQ] =
  2658. SE->getMulExpr(PositivePart, Bound[K].Iterations);
  2659. }
  2660. else {
  2661. // If the positive/negative part of the difference is 0,
  2662. // we won't need to know the number of iterations.
  2663. const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff);
  2664. const SCEV *NegativePart = getNegativePart(Delta);
  2665. if (NegativePart->isZero())
  2666. Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero
  2667. const SCEV *PositivePart = getPositivePart(Delta);
  2668. if (PositivePart->isZero())
  2669. Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero
  2670. }
  2671. }
  2672. // Computes the upper and lower bounds for level K
  2673. // using the < direction. Records them in Bound.
  2674. // Wolfe gives the equations
  2675. //
  2676. // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
  2677. // UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k
  2678. //
  2679. // Since we normalize loops, we can simplify these equations to
  2680. //
  2681. // LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k
  2682. // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k
  2683. //
  2684. // We must be careful to handle the case where the upper bound is unknown.
  2685. void DependenceInfo::findBoundsLT(CoefficientInfo *A, CoefficientInfo *B,
  2686. BoundInfo *Bound, unsigned K) const {
  2687. Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity.
  2688. Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity.
  2689. if (Bound[K].Iterations) {
  2690. const SCEV *Iter_1 = SE->getMinusSCEV(
  2691. Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
  2692. const SCEV *NegPart =
  2693. getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
  2694. Bound[K].Lower[Dependence::DVEntry::LT] =
  2695. SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff);
  2696. const SCEV *PosPart =
  2697. getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
  2698. Bound[K].Upper[Dependence::DVEntry::LT] =
  2699. SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff);
  2700. }
  2701. else {
  2702. // If the positive/negative part of the difference is 0,
  2703. // we won't need to know the number of iterations.
  2704. const SCEV *NegPart =
  2705. getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff));
  2706. if (NegPart->isZero())
  2707. Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
  2708. const SCEV *PosPart =
  2709. getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff));
  2710. if (PosPart->isZero())
  2711. Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff);
  2712. }
  2713. }
  2714. // Computes the upper and lower bounds for level K
  2715. // using the > direction. Records them in Bound.
  2716. // Wolfe gives the equations
  2717. //
  2718. // LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
  2719. // UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k
  2720. //
  2721. // Since we normalize loops, we can simplify these equations to
  2722. //
  2723. // LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k
  2724. // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k
  2725. //
  2726. // We must be careful to handle the case where the upper bound is unknown.
  2727. void DependenceInfo::findBoundsGT(CoefficientInfo *A, CoefficientInfo *B,
  2728. BoundInfo *Bound, unsigned K) const {
  2729. Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity.
  2730. Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity.
  2731. if (Bound[K].Iterations) {
  2732. const SCEV *Iter_1 = SE->getMinusSCEV(
  2733. Bound[K].Iterations, SE->getOne(Bound[K].Iterations->getType()));
  2734. const SCEV *NegPart =
  2735. getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
  2736. Bound[K].Lower[Dependence::DVEntry::GT] =
  2737. SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff);
  2738. const SCEV *PosPart =
  2739. getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
  2740. Bound[K].Upper[Dependence::DVEntry::GT] =
  2741. SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff);
  2742. }
  2743. else {
  2744. // If the positive/negative part of the difference is 0,
  2745. // we won't need to know the number of iterations.
  2746. const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart));
  2747. if (NegPart->isZero())
  2748. Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff;
  2749. const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart));
  2750. if (PosPart->isZero())
  2751. Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff;
  2752. }
  2753. }
  2754. // X^+ = max(X, 0)
  2755. const SCEV *DependenceInfo::getPositivePart(const SCEV *X) const {
  2756. return SE->getSMaxExpr(X, SE->getZero(X->getType()));
  2757. }
  2758. // X^- = min(X, 0)
  2759. const SCEV *DependenceInfo::getNegativePart(const SCEV *X) const {
  2760. return SE->getSMinExpr(X, SE->getZero(X->getType()));
  2761. }
  2762. // Walks through the subscript,
  2763. // collecting each coefficient, the associated loop bounds,
  2764. // and recording its positive and negative parts for later use.
  2765. DependenceInfo::CoefficientInfo *
  2766. DependenceInfo::collectCoeffInfo(const SCEV *Subscript, bool SrcFlag,
  2767. const SCEV *&Constant) const {
  2768. const SCEV *Zero = SE->getZero(Subscript->getType());
  2769. CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1];
  2770. for (unsigned K = 1; K <= MaxLevels; ++K) {
  2771. CI[K].Coeff = Zero;
  2772. CI[K].PosPart = Zero;
  2773. CI[K].NegPart = Zero;
  2774. CI[K].Iterations = nullptr;
  2775. }
  2776. while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) {
  2777. const Loop *L = AddRec->getLoop();
  2778. unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L);
  2779. CI[K].Coeff = AddRec->getStepRecurrence(*SE);
  2780. CI[K].PosPart = getPositivePart(CI[K].Coeff);
  2781. CI[K].NegPart = getNegativePart(CI[K].Coeff);
  2782. CI[K].Iterations = collectUpperBound(L, Subscript->getType());
  2783. Subscript = AddRec->getStart();
  2784. }
  2785. Constant = Subscript;
  2786. #ifndef NDEBUG
  2787. LLVM_DEBUG(dbgs() << "\tCoefficient Info\n");
  2788. for (unsigned K = 1; K <= MaxLevels; ++K) {
  2789. LLVM_DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff);
  2790. LLVM_DEBUG(dbgs() << "\tPos Part = ");
  2791. LLVM_DEBUG(dbgs() << *CI[K].PosPart);
  2792. LLVM_DEBUG(dbgs() << "\tNeg Part = ");
  2793. LLVM_DEBUG(dbgs() << *CI[K].NegPart);
  2794. LLVM_DEBUG(dbgs() << "\tUpper Bound = ");
  2795. if (CI[K].Iterations)
  2796. LLVM_DEBUG(dbgs() << *CI[K].Iterations);
  2797. else
  2798. LLVM_DEBUG(dbgs() << "+inf");
  2799. LLVM_DEBUG(dbgs() << '\n');
  2800. }
  2801. LLVM_DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n');
  2802. #endif
  2803. return CI;
  2804. }
  2805. // Looks through all the bounds info and
  2806. // computes the lower bound given the current direction settings
  2807. // at each level. If the lower bound for any level is -inf,
  2808. // the result is -inf.
  2809. const SCEV *DependenceInfo::getLowerBound(BoundInfo *Bound) const {
  2810. const SCEV *Sum = Bound[1].Lower[Bound[1].Direction];
  2811. for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
  2812. if (Bound[K].Lower[Bound[K].Direction])
  2813. Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]);
  2814. else
  2815. Sum = nullptr;
  2816. }
  2817. return Sum;
  2818. }
  2819. // Looks through all the bounds info and
  2820. // computes the upper bound given the current direction settings
  2821. // at each level. If the upper bound at any level is +inf,
  2822. // the result is +inf.
  2823. const SCEV *DependenceInfo::getUpperBound(BoundInfo *Bound) const {
  2824. const SCEV *Sum = Bound[1].Upper[Bound[1].Direction];
  2825. for (unsigned K = 2; Sum && K <= MaxLevels; ++K) {
  2826. if (Bound[K].Upper[Bound[K].Direction])
  2827. Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]);
  2828. else
  2829. Sum = nullptr;
  2830. }
  2831. return Sum;
  2832. }
  2833. //===----------------------------------------------------------------------===//
  2834. // Constraint manipulation for Delta test.
  2835. // Given a linear SCEV,
  2836. // return the coefficient (the step)
  2837. // corresponding to the specified loop.
  2838. // If there isn't one, return 0.
  2839. // For example, given a*i + b*j + c*k, finding the coefficient
  2840. // corresponding to the j loop would yield b.
  2841. const SCEV *DependenceInfo::findCoefficient(const SCEV *Expr,
  2842. const Loop *TargetLoop) const {
  2843. const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
  2844. if (!AddRec)
  2845. return SE->getZero(Expr->getType());
  2846. if (AddRec->getLoop() == TargetLoop)
  2847. return AddRec->getStepRecurrence(*SE);
  2848. return findCoefficient(AddRec->getStart(), TargetLoop);
  2849. }
  2850. // Given a linear SCEV,
  2851. // return the SCEV given by zeroing out the coefficient
  2852. // corresponding to the specified loop.
  2853. // For example, given a*i + b*j + c*k, zeroing the coefficient
  2854. // corresponding to the j loop would yield a*i + c*k.
  2855. const SCEV *DependenceInfo::zeroCoefficient(const SCEV *Expr,
  2856. const Loop *TargetLoop) const {
  2857. const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
  2858. if (!AddRec)
  2859. return Expr; // ignore
  2860. if (AddRec->getLoop() == TargetLoop)
  2861. return AddRec->getStart();
  2862. return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop),
  2863. AddRec->getStepRecurrence(*SE),
  2864. AddRec->getLoop(),
  2865. AddRec->getNoWrapFlags());
  2866. }
  2867. // Given a linear SCEV Expr,
  2868. // return the SCEV given by adding some Value to the
  2869. // coefficient corresponding to the specified TargetLoop.
  2870. // For example, given a*i + b*j + c*k, adding 1 to the coefficient
  2871. // corresponding to the j loop would yield a*i + (b+1)*j + c*k.
  2872. const SCEV *DependenceInfo::addToCoefficient(const SCEV *Expr,
  2873. const Loop *TargetLoop,
  2874. const SCEV *Value) const {
  2875. const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
  2876. if (!AddRec) // create a new addRec
  2877. return SE->getAddRecExpr(Expr,
  2878. Value,
  2879. TargetLoop,
  2880. SCEV::FlagAnyWrap); // Worst case, with no info.
  2881. if (AddRec->getLoop() == TargetLoop) {
  2882. const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value);
  2883. if (Sum->isZero())
  2884. return AddRec->getStart();
  2885. return SE->getAddRecExpr(AddRec->getStart(),
  2886. Sum,
  2887. AddRec->getLoop(),
  2888. AddRec->getNoWrapFlags());
  2889. }
  2890. if (SE->isLoopInvariant(AddRec, TargetLoop))
  2891. return SE->getAddRecExpr(AddRec, Value, TargetLoop, SCEV::FlagAnyWrap);
  2892. return SE->getAddRecExpr(
  2893. addToCoefficient(AddRec->getStart(), TargetLoop, Value),
  2894. AddRec->getStepRecurrence(*SE), AddRec->getLoop(),
  2895. AddRec->getNoWrapFlags());
  2896. }
  2897. // Review the constraints, looking for opportunities
  2898. // to simplify a subscript pair (Src and Dst).
  2899. // Return true if some simplification occurs.
  2900. // If the simplification isn't exact (that is, if it is conservative
  2901. // in terms of dependence), set consistent to false.
  2902. // Corresponds to Figure 5 from the paper
  2903. //
  2904. // Practical Dependence Testing
  2905. // Goff, Kennedy, Tseng
  2906. // PLDI 1991
  2907. bool DependenceInfo::propagate(const SCEV *&Src, const SCEV *&Dst,
  2908. SmallBitVector &Loops,
  2909. SmallVectorImpl<Constraint> &Constraints,
  2910. bool &Consistent) {
  2911. bool Result = false;
  2912. for (unsigned LI : Loops.set_bits()) {
  2913. LLVM_DEBUG(dbgs() << "\t Constraint[" << LI << "] is");
  2914. LLVM_DEBUG(Constraints[LI].dump(dbgs()));
  2915. if (Constraints[LI].isDistance())
  2916. Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent);
  2917. else if (Constraints[LI].isLine())
  2918. Result |= propagateLine(Src, Dst, Constraints[LI], Consistent);
  2919. else if (Constraints[LI].isPoint())
  2920. Result |= propagatePoint(Src, Dst, Constraints[LI]);
  2921. }
  2922. return Result;
  2923. }
  2924. // Attempt to propagate a distance
  2925. // constraint into a subscript pair (Src and Dst).
  2926. // Return true if some simplification occurs.
  2927. // If the simplification isn't exact (that is, if it is conservative
  2928. // in terms of dependence), set consistent to false.
  2929. bool DependenceInfo::propagateDistance(const SCEV *&Src, const SCEV *&Dst,
  2930. Constraint &CurConstraint,
  2931. bool &Consistent) {
  2932. const Loop *CurLoop = CurConstraint.getAssociatedLoop();
  2933. LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
  2934. const SCEV *A_K = findCoefficient(Src, CurLoop);
  2935. if (A_K->isZero())
  2936. return false;
  2937. const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD());
  2938. Src = SE->getMinusSCEV(Src, DA_K);
  2939. Src = zeroCoefficient(Src, CurLoop);
  2940. LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
  2941. LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
  2942. Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K));
  2943. LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
  2944. if (!findCoefficient(Dst, CurLoop)->isZero())
  2945. Consistent = false;
  2946. return true;
  2947. }
  2948. // Attempt to propagate a line
  2949. // constraint into a subscript pair (Src and Dst).
  2950. // Return true if some simplification occurs.
  2951. // If the simplification isn't exact (that is, if it is conservative
  2952. // in terms of dependence), set consistent to false.
  2953. bool DependenceInfo::propagateLine(const SCEV *&Src, const SCEV *&Dst,
  2954. Constraint &CurConstraint,
  2955. bool &Consistent) {
  2956. const Loop *CurLoop = CurConstraint.getAssociatedLoop();
  2957. const SCEV *A = CurConstraint.getA();
  2958. const SCEV *B = CurConstraint.getB();
  2959. const SCEV *C = CurConstraint.getC();
  2960. LLVM_DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C
  2961. << "\n");
  2962. LLVM_DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n");
  2963. LLVM_DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n");
  2964. if (A->isZero()) {
  2965. const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B);
  2966. const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
  2967. if (!Bconst || !Cconst) return false;
  2968. APInt Beta = Bconst->getAPInt();
  2969. APInt Charlie = Cconst->getAPInt();
  2970. APInt CdivB = Charlie.sdiv(Beta);
  2971. assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B");
  2972. const SCEV *AP_K = findCoefficient(Dst, CurLoop);
  2973. // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
  2974. Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB)));
  2975. Dst = zeroCoefficient(Dst, CurLoop);
  2976. if (!findCoefficient(Src, CurLoop)->isZero())
  2977. Consistent = false;
  2978. }
  2979. else if (B->isZero()) {
  2980. const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
  2981. const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
  2982. if (!Aconst || !Cconst) return false;
  2983. APInt Alpha = Aconst->getAPInt();
  2984. APInt Charlie = Cconst->getAPInt();
  2985. APInt CdivA = Charlie.sdiv(Alpha);
  2986. assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
  2987. const SCEV *A_K = findCoefficient(Src, CurLoop);
  2988. Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
  2989. Src = zeroCoefficient(Src, CurLoop);
  2990. if (!findCoefficient(Dst, CurLoop)->isZero())
  2991. Consistent = false;
  2992. }
  2993. else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) {
  2994. const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A);
  2995. const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C);
  2996. if (!Aconst || !Cconst) return false;
  2997. APInt Alpha = Aconst->getAPInt();
  2998. APInt Charlie = Cconst->getAPInt();
  2999. APInt CdivA = Charlie.sdiv(Alpha);
  3000. assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A");
  3001. const SCEV *A_K = findCoefficient(Src, CurLoop);
  3002. Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA)));
  3003. Src = zeroCoefficient(Src, CurLoop);
  3004. Dst = addToCoefficient(Dst, CurLoop, A_K);
  3005. if (!findCoefficient(Dst, CurLoop)->isZero())
  3006. Consistent = false;
  3007. }
  3008. else {
  3009. // paper is incorrect here, or perhaps just misleading
  3010. const SCEV *A_K = findCoefficient(Src, CurLoop);
  3011. Src = SE->getMulExpr(Src, A);
  3012. Dst = SE->getMulExpr(Dst, A);
  3013. Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C));
  3014. Src = zeroCoefficient(Src, CurLoop);
  3015. Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B));
  3016. if (!findCoefficient(Dst, CurLoop)->isZero())
  3017. Consistent = false;
  3018. }
  3019. LLVM_DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n");
  3020. LLVM_DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n");
  3021. return true;
  3022. }
  3023. // Attempt to propagate a point
  3024. // constraint into a subscript pair (Src and Dst).
  3025. // Return true if some simplification occurs.
  3026. bool DependenceInfo::propagatePoint(const SCEV *&Src, const SCEV *&Dst,
  3027. Constraint &CurConstraint) {
  3028. const Loop *CurLoop = CurConstraint.getAssociatedLoop();
  3029. const SCEV *A_K = findCoefficient(Src, CurLoop);
  3030. const SCEV *AP_K = findCoefficient(Dst, CurLoop);
  3031. const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX());
  3032. const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY());
  3033. LLVM_DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n");
  3034. Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K));
  3035. Src = zeroCoefficient(Src, CurLoop);
  3036. LLVM_DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n");
  3037. LLVM_DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n");
  3038. Dst = zeroCoefficient(Dst, CurLoop);
  3039. LLVM_DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n");
  3040. return true;
  3041. }
  3042. // Update direction vector entry based on the current constraint.
  3043. void DependenceInfo::updateDirection(Dependence::DVEntry &Level,
  3044. const Constraint &CurConstraint) const {
  3045. LLVM_DEBUG(dbgs() << "\tUpdate direction, constraint =");
  3046. LLVM_DEBUG(CurConstraint.dump(dbgs()));
  3047. if (CurConstraint.isAny())
  3048. ; // use defaults
  3049. else if (CurConstraint.isDistance()) {
  3050. // this one is consistent, the others aren't
  3051. Level.Scalar = false;
  3052. Level.Distance = CurConstraint.getD();
  3053. unsigned NewDirection = Dependence::DVEntry::NONE;
  3054. if (!SE->isKnownNonZero(Level.Distance)) // if may be zero
  3055. NewDirection = Dependence::DVEntry::EQ;
  3056. if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive
  3057. NewDirection |= Dependence::DVEntry::LT;
  3058. if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative
  3059. NewDirection |= Dependence::DVEntry::GT;
  3060. Level.Direction &= NewDirection;
  3061. }
  3062. else if (CurConstraint.isLine()) {
  3063. Level.Scalar = false;
  3064. Level.Distance = nullptr;
  3065. // direction should be accurate
  3066. }
  3067. else if (CurConstraint.isPoint()) {
  3068. Level.Scalar = false;
  3069. Level.Distance = nullptr;
  3070. unsigned NewDirection = Dependence::DVEntry::NONE;
  3071. if (!isKnownPredicate(CmpInst::ICMP_NE,
  3072. CurConstraint.getY(),
  3073. CurConstraint.getX()))
  3074. // if X may be = Y
  3075. NewDirection |= Dependence::DVEntry::EQ;
  3076. if (!isKnownPredicate(CmpInst::ICMP_SLE,
  3077. CurConstraint.getY(),
  3078. CurConstraint.getX()))
  3079. // if Y may be > X
  3080. NewDirection |= Dependence::DVEntry::LT;
  3081. if (!isKnownPredicate(CmpInst::ICMP_SGE,
  3082. CurConstraint.getY(),
  3083. CurConstraint.getX()))
  3084. // if Y may be < X
  3085. NewDirection |= Dependence::DVEntry::GT;
  3086. Level.Direction &= NewDirection;
  3087. }
  3088. else
  3089. llvm_unreachable("constraint has unexpected kind");
  3090. }
  3091. /// Check if we can delinearize the subscripts. If the SCEVs representing the
  3092. /// source and destination array references are recurrences on a nested loop,
  3093. /// this function flattens the nested recurrences into separate recurrences
  3094. /// for each loop level.
  3095. bool DependenceInfo::tryDelinearize(Instruction *Src, Instruction *Dst,
  3096. SmallVectorImpl<Subscript> &Pair) {
  3097. assert(isLoadOrStore(Src) && "instruction is not load or store");
  3098. assert(isLoadOrStore(Dst) && "instruction is not load or store");
  3099. Value *SrcPtr = getLoadStorePointerOperand(Src);
  3100. Value *DstPtr = getLoadStorePointerOperand(Dst);
  3101. Loop *SrcLoop = LI->getLoopFor(Src->getParent());
  3102. Loop *DstLoop = LI->getLoopFor(Dst->getParent());
  3103. const SCEV *SrcAccessFn = SE->getSCEVAtScope(SrcPtr, SrcLoop);
  3104. const SCEV *DstAccessFn = SE->getSCEVAtScope(DstPtr, DstLoop);
  3105. const SCEVUnknown *SrcBase =
  3106. dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
  3107. const SCEVUnknown *DstBase =
  3108. dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
  3109. if (!SrcBase || !DstBase || SrcBase != DstBase)
  3110. return false;
  3111. SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
  3112. if (!tryDelinearizeFixedSize(Src, Dst, SrcAccessFn, DstAccessFn,
  3113. SrcSubscripts, DstSubscripts) &&
  3114. !tryDelinearizeParametricSize(Src, Dst, SrcAccessFn, DstAccessFn,
  3115. SrcSubscripts, DstSubscripts))
  3116. return false;
  3117. int Size = SrcSubscripts.size();
  3118. LLVM_DEBUG({
  3119. dbgs() << "\nSrcSubscripts: ";
  3120. for (int I = 0; I < Size; I++)
  3121. dbgs() << *SrcSubscripts[I];
  3122. dbgs() << "\nDstSubscripts: ";
  3123. for (int I = 0; I < Size; I++)
  3124. dbgs() << *DstSubscripts[I];
  3125. });
  3126. // The delinearization transforms a single-subscript MIV dependence test into
  3127. // a multi-subscript SIV dependence test that is easier to compute. So we
  3128. // resize Pair to contain as many pairs of subscripts as the delinearization
  3129. // has found, and then initialize the pairs following the delinearization.
  3130. Pair.resize(Size);
  3131. for (int I = 0; I < Size; ++I) {
  3132. Pair[I].Src = SrcSubscripts[I];
  3133. Pair[I].Dst = DstSubscripts[I];
  3134. unifySubscriptType(&Pair[I]);
  3135. }
  3136. return true;
  3137. }
  3138. /// Try to delinearize \p SrcAccessFn and \p DstAccessFn if the underlying
  3139. /// arrays accessed are fixed-size arrays. Return true if delinearization was
  3140. /// successful.
  3141. bool DependenceInfo::tryDelinearizeFixedSize(
  3142. Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
  3143. const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
  3144. SmallVectorImpl<const SCEV *> &DstSubscripts) {
  3145. LLVM_DEBUG({
  3146. const SCEVUnknown *SrcBase =
  3147. dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
  3148. const SCEVUnknown *DstBase =
  3149. dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
  3150. assert(SrcBase && DstBase && SrcBase == DstBase &&
  3151. "expected src and dst scev unknowns to be equal");
  3152. });
  3153. SmallVector<int, 4> SrcSizes;
  3154. SmallVector<int, 4> DstSizes;
  3155. if (!tryDelinearizeFixedSizeImpl(SE, Src, SrcAccessFn, SrcSubscripts,
  3156. SrcSizes) ||
  3157. !tryDelinearizeFixedSizeImpl(SE, Dst, DstAccessFn, DstSubscripts,
  3158. DstSizes))
  3159. return false;
  3160. // Check that the two size arrays are non-empty and equal in length and
  3161. // value.
  3162. if (SrcSizes.size() != DstSizes.size() ||
  3163. !std::equal(SrcSizes.begin(), SrcSizes.end(), DstSizes.begin())) {
  3164. SrcSubscripts.clear();
  3165. DstSubscripts.clear();
  3166. return false;
  3167. }
  3168. assert(SrcSubscripts.size() == DstSubscripts.size() &&
  3169. "Expected equal number of entries in the list of SrcSubscripts and "
  3170. "DstSubscripts.");
  3171. Value *SrcPtr = getLoadStorePointerOperand(Src);
  3172. Value *DstPtr = getLoadStorePointerOperand(Dst);
  3173. // In general we cannot safely assume that the subscripts recovered from GEPs
  3174. // are in the range of values defined for their corresponding array
  3175. // dimensions. For example some C language usage/interpretation make it
  3176. // impossible to verify this at compile-time. As such we can only delinearize
  3177. // iff the subscripts are positive and are less than the range of the
  3178. // dimension.
  3179. if (!DisableDelinearizationChecks) {
  3180. auto AllIndiciesInRange = [&](SmallVector<int, 4> &DimensionSizes,
  3181. SmallVectorImpl<const SCEV *> &Subscripts,
  3182. Value *Ptr) {
  3183. size_t SSize = Subscripts.size();
  3184. for (size_t I = 1; I < SSize; ++I) {
  3185. const SCEV *S = Subscripts[I];
  3186. if (!isKnownNonNegative(S, Ptr))
  3187. return false;
  3188. if (auto *SType = dyn_cast<IntegerType>(S->getType())) {
  3189. const SCEV *Range = SE->getConstant(
  3190. ConstantInt::get(SType, DimensionSizes[I - 1], false));
  3191. if (!isKnownLessThan(S, Range))
  3192. return false;
  3193. }
  3194. }
  3195. return true;
  3196. };
  3197. if (!AllIndiciesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
  3198. !AllIndiciesInRange(DstSizes, DstSubscripts, DstPtr)) {
  3199. SrcSubscripts.clear();
  3200. DstSubscripts.clear();
  3201. return false;
  3202. }
  3203. }
  3204. LLVM_DEBUG({
  3205. dbgs() << "Delinearized subscripts of fixed-size array\n"
  3206. << "SrcGEP:" << *SrcPtr << "\n"
  3207. << "DstGEP:" << *DstPtr << "\n";
  3208. });
  3209. return true;
  3210. }
  3211. bool DependenceInfo::tryDelinearizeParametricSize(
  3212. Instruction *Src, Instruction *Dst, const SCEV *SrcAccessFn,
  3213. const SCEV *DstAccessFn, SmallVectorImpl<const SCEV *> &SrcSubscripts,
  3214. SmallVectorImpl<const SCEV *> &DstSubscripts) {
  3215. Value *SrcPtr = getLoadStorePointerOperand(Src);
  3216. Value *DstPtr = getLoadStorePointerOperand(Dst);
  3217. const SCEVUnknown *SrcBase =
  3218. dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcAccessFn));
  3219. const SCEVUnknown *DstBase =
  3220. dyn_cast<SCEVUnknown>(SE->getPointerBase(DstAccessFn));
  3221. assert(SrcBase && DstBase && SrcBase == DstBase &&
  3222. "expected src and dst scev unknowns to be equal");
  3223. const SCEV *ElementSize = SE->getElementSize(Src);
  3224. if (ElementSize != SE->getElementSize(Dst))
  3225. return false;
  3226. const SCEV *SrcSCEV = SE->getMinusSCEV(SrcAccessFn, SrcBase);
  3227. const SCEV *DstSCEV = SE->getMinusSCEV(DstAccessFn, DstBase);
  3228. const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
  3229. const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
  3230. if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
  3231. return false;
  3232. // First step: collect parametric terms in both array references.
  3233. SmallVector<const SCEV *, 4> Terms;
  3234. collectParametricTerms(*SE, SrcAR, Terms);
  3235. collectParametricTerms(*SE, DstAR, Terms);
  3236. // Second step: find subscript sizes.
  3237. SmallVector<const SCEV *, 4> Sizes;
  3238. findArrayDimensions(*SE, Terms, Sizes, ElementSize);
  3239. // Third step: compute the access functions for each subscript.
  3240. computeAccessFunctions(*SE, SrcAR, SrcSubscripts, Sizes);
  3241. computeAccessFunctions(*SE, DstAR, DstSubscripts, Sizes);
  3242. // Fail when there is only a subscript: that's a linearized access function.
  3243. if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 ||
  3244. SrcSubscripts.size() != DstSubscripts.size())
  3245. return false;
  3246. size_t Size = SrcSubscripts.size();
  3247. // Statically check that the array bounds are in-range. The first subscript we
  3248. // don't have a size for and it cannot overflow into another subscript, so is
  3249. // always safe. The others need to be 0 <= subscript[i] < bound, for both src
  3250. // and dst.
  3251. // FIXME: It may be better to record these sizes and add them as constraints
  3252. // to the dependency checks.
  3253. if (!DisableDelinearizationChecks)
  3254. for (size_t I = 1; I < Size; ++I) {
  3255. if (!isKnownNonNegative(SrcSubscripts[I], SrcPtr))
  3256. return false;
  3257. if (!isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]))
  3258. return false;
  3259. if (!isKnownNonNegative(DstSubscripts[I], DstPtr))
  3260. return false;
  3261. if (!isKnownLessThan(DstSubscripts[I], Sizes[I - 1]))
  3262. return false;
  3263. }
  3264. return true;
  3265. }
  3266. //===----------------------------------------------------------------------===//
  3267. #ifndef NDEBUG
  3268. // For debugging purposes, dump a small bit vector to dbgs().
  3269. static void dumpSmallBitVector(SmallBitVector &BV) {
  3270. dbgs() << "{";
  3271. for (unsigned VI : BV.set_bits()) {
  3272. dbgs() << VI;
  3273. if (BV.find_next(VI) >= 0)
  3274. dbgs() << ' ';
  3275. }
  3276. dbgs() << "}\n";
  3277. }
  3278. #endif
  3279. bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA,
  3280. FunctionAnalysisManager::Invalidator &Inv) {
  3281. // Check if the analysis itself has been invalidated.
  3282. auto PAC = PA.getChecker<DependenceAnalysis>();
  3283. if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
  3284. return true;
  3285. // Check transitive dependencies.
  3286. return Inv.invalidate<AAManager>(F, PA) ||
  3287. Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
  3288. Inv.invalidate<LoopAnalysis>(F, PA);
  3289. }
  3290. // depends -
  3291. // Returns NULL if there is no dependence.
  3292. // Otherwise, return a Dependence with as many details as possible.
  3293. // Corresponds to Section 3.1 in the paper
  3294. //
  3295. // Practical Dependence Testing
  3296. // Goff, Kennedy, Tseng
  3297. // PLDI 1991
  3298. //
  3299. // Care is required to keep the routine below, getSplitIteration(),
  3300. // up to date with respect to this routine.
  3301. std::unique_ptr<Dependence>
  3302. DependenceInfo::depends(Instruction *Src, Instruction *Dst,
  3303. bool PossiblyLoopIndependent) {
  3304. if (Src == Dst)
  3305. PossiblyLoopIndependent = false;
  3306. if (!(Src->mayReadOrWriteMemory() && Dst->mayReadOrWriteMemory()))
  3307. // if both instructions don't reference memory, there's no dependence
  3308. return nullptr;
  3309. if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) {
  3310. // can only analyze simple loads and stores, i.e., no calls, invokes, etc.
  3311. LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n");
  3312. return std::make_unique<Dependence>(Src, Dst);
  3313. }
  3314. assert(isLoadOrStore(Src) && "instruction is not load or store");
  3315. assert(isLoadOrStore(Dst) && "instruction is not load or store");
  3316. Value *SrcPtr = getLoadStorePointerOperand(Src);
  3317. Value *DstPtr = getLoadStorePointerOperand(Dst);
  3318. switch (underlyingObjectsAlias(AA, F->getParent()->getDataLayout(),
  3319. MemoryLocation::get(Dst),
  3320. MemoryLocation::get(Src))) {
  3321. case AliasResult::MayAlias:
  3322. case AliasResult::PartialAlias:
  3323. // cannot analyse objects if we don't understand their aliasing.
  3324. LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n");
  3325. return std::make_unique<Dependence>(Src, Dst);
  3326. case AliasResult::NoAlias:
  3327. // If the objects noalias, they are distinct, accesses are independent.
  3328. LLVM_DEBUG(dbgs() << "no alias\n");
  3329. return nullptr;
  3330. case AliasResult::MustAlias:
  3331. break; // The underlying objects alias; test accesses for dependence.
  3332. }
  3333. // establish loop nesting levels
  3334. establishNestingLevels(Src, Dst);
  3335. LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
  3336. LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
  3337. FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
  3338. ++TotalArrayPairs;
  3339. unsigned Pairs = 1;
  3340. SmallVector<Subscript, 2> Pair(Pairs);
  3341. const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
  3342. const SCEV *DstSCEV = SE->getSCEV(DstPtr);
  3343. LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
  3344. LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n");
  3345. if (SE->getPointerBase(SrcSCEV) != SE->getPointerBase(DstSCEV)) {
  3346. // If two pointers have different bases, trying to analyze indexes won't
  3347. // work; we can't compare them to each other. This can happen, for example,
  3348. // if one is produced by an LCSSA PHI node.
  3349. //
  3350. // We check this upfront so we don't crash in cases where getMinusSCEV()
  3351. // returns a SCEVCouldNotCompute.
  3352. LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
  3353. return std::make_unique<Dependence>(Src, Dst);
  3354. }
  3355. Pair[0].Src = SrcSCEV;
  3356. Pair[0].Dst = DstSCEV;
  3357. if (Delinearize) {
  3358. if (tryDelinearize(Src, Dst, Pair)) {
  3359. LLVM_DEBUG(dbgs() << " delinearized\n");
  3360. Pairs = Pair.size();
  3361. }
  3362. }
  3363. for (unsigned P = 0; P < Pairs; ++P) {
  3364. Pair[P].Loops.resize(MaxLevels + 1);
  3365. Pair[P].GroupLoops.resize(MaxLevels + 1);
  3366. Pair[P].Group.resize(Pairs);
  3367. removeMatchingExtensions(&Pair[P]);
  3368. Pair[P].Classification =
  3369. classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
  3370. Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
  3371. Pair[P].Loops);
  3372. Pair[P].GroupLoops = Pair[P].Loops;
  3373. Pair[P].Group.set(P);
  3374. LLVM_DEBUG(dbgs() << " subscript " << P << "\n");
  3375. LLVM_DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n");
  3376. LLVM_DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n");
  3377. LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
  3378. LLVM_DEBUG(dbgs() << "\tloops = ");
  3379. LLVM_DEBUG(dumpSmallBitVector(Pair[P].Loops));
  3380. }
  3381. SmallBitVector Separable(Pairs);
  3382. SmallBitVector Coupled(Pairs);
  3383. // Partition subscripts into separable and minimally-coupled groups
  3384. // Algorithm in paper is algorithmically better;
  3385. // this may be faster in practice. Check someday.
  3386. //
  3387. // Here's an example of how it works. Consider this code:
  3388. //
  3389. // for (i = ...) {
  3390. // for (j = ...) {
  3391. // for (k = ...) {
  3392. // for (l = ...) {
  3393. // for (m = ...) {
  3394. // A[i][j][k][m] = ...;
  3395. // ... = A[0][j][l][i + j];
  3396. // }
  3397. // }
  3398. // }
  3399. // }
  3400. // }
  3401. //
  3402. // There are 4 subscripts here:
  3403. // 0 [i] and [0]
  3404. // 1 [j] and [j]
  3405. // 2 [k] and [l]
  3406. // 3 [m] and [i + j]
  3407. //
  3408. // We've already classified each subscript pair as ZIV, SIV, etc.,
  3409. // and collected all the loops mentioned by pair P in Pair[P].Loops.
  3410. // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops
  3411. // and set Pair[P].Group = {P}.
  3412. //
  3413. // Src Dst Classification Loops GroupLoops Group
  3414. // 0 [i] [0] SIV {1} {1} {0}
  3415. // 1 [j] [j] SIV {2} {2} {1}
  3416. // 2 [k] [l] RDIV {3,4} {3,4} {2}
  3417. // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3}
  3418. //
  3419. // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ.
  3420. // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc.
  3421. //
  3422. // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty.
  3423. // Next, 0 and 2. Again, the intersection of their GroupLoops is empty.
  3424. // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty,
  3425. // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added
  3426. // to either Separable or Coupled).
  3427. //
  3428. // Next, we consider 1 and 2. The intersection of the GroupLoops is empty.
  3429. // Next, 1 and 3. The intersection of their GroupLoops = {2}, not empty,
  3430. // so Pair[3].Group = {0, 1, 3} and Done = false.
  3431. //
  3432. // Next, we compare 2 against 3. The intersection of the GroupLoops is empty.
  3433. // Since Done remains true, we add 2 to the set of Separable pairs.
  3434. //
  3435. // Finally, we consider 3. There's nothing to compare it with,
  3436. // so Done remains true and we add it to the Coupled set.
  3437. // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}.
  3438. //
  3439. // In the end, we've got 1 separable subscript and 1 coupled group.
  3440. for (unsigned SI = 0; SI < Pairs; ++SI) {
  3441. if (Pair[SI].Classification == Subscript::NonLinear) {
  3442. // ignore these, but collect loops for later
  3443. ++NonlinearSubscriptPairs;
  3444. collectCommonLoops(Pair[SI].Src,
  3445. LI->getLoopFor(Src->getParent()),
  3446. Pair[SI].Loops);
  3447. collectCommonLoops(Pair[SI].Dst,
  3448. LI->getLoopFor(Dst->getParent()),
  3449. Pair[SI].Loops);
  3450. Result.Consistent = false;
  3451. } else if (Pair[SI].Classification == Subscript::ZIV) {
  3452. // always separable
  3453. Separable.set(SI);
  3454. }
  3455. else {
  3456. // SIV, RDIV, or MIV, so check for coupled group
  3457. bool Done = true;
  3458. for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
  3459. SmallBitVector Intersection = Pair[SI].GroupLoops;
  3460. Intersection &= Pair[SJ].GroupLoops;
  3461. if (Intersection.any()) {
  3462. // accumulate set of all the loops in group
  3463. Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
  3464. // accumulate set of all subscripts in group
  3465. Pair[SJ].Group |= Pair[SI].Group;
  3466. Done = false;
  3467. }
  3468. }
  3469. if (Done) {
  3470. if (Pair[SI].Group.count() == 1) {
  3471. Separable.set(SI);
  3472. ++SeparableSubscriptPairs;
  3473. }
  3474. else {
  3475. Coupled.set(SI);
  3476. ++CoupledSubscriptPairs;
  3477. }
  3478. }
  3479. }
  3480. }
  3481. LLVM_DEBUG(dbgs() << " Separable = ");
  3482. LLVM_DEBUG(dumpSmallBitVector(Separable));
  3483. LLVM_DEBUG(dbgs() << " Coupled = ");
  3484. LLVM_DEBUG(dumpSmallBitVector(Coupled));
  3485. Constraint NewConstraint;
  3486. NewConstraint.setAny(SE);
  3487. // test separable subscripts
  3488. for (unsigned SI : Separable.set_bits()) {
  3489. LLVM_DEBUG(dbgs() << "testing subscript " << SI);
  3490. switch (Pair[SI].Classification) {
  3491. case Subscript::ZIV:
  3492. LLVM_DEBUG(dbgs() << ", ZIV\n");
  3493. if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result))
  3494. return nullptr;
  3495. break;
  3496. case Subscript::SIV: {
  3497. LLVM_DEBUG(dbgs() << ", SIV\n");
  3498. unsigned Level;
  3499. const SCEV *SplitIter = nullptr;
  3500. if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, Result, NewConstraint,
  3501. SplitIter))
  3502. return nullptr;
  3503. break;
  3504. }
  3505. case Subscript::RDIV:
  3506. LLVM_DEBUG(dbgs() << ", RDIV\n");
  3507. if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result))
  3508. return nullptr;
  3509. break;
  3510. case Subscript::MIV:
  3511. LLVM_DEBUG(dbgs() << ", MIV\n");
  3512. if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result))
  3513. return nullptr;
  3514. break;
  3515. default:
  3516. llvm_unreachable("subscript has unexpected classification");
  3517. }
  3518. }
  3519. if (Coupled.count()) {
  3520. // test coupled subscript groups
  3521. LLVM_DEBUG(dbgs() << "starting on coupled subscripts\n");
  3522. LLVM_DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n");
  3523. SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
  3524. for (unsigned II = 0; II <= MaxLevels; ++II)
  3525. Constraints[II].setAny(SE);
  3526. for (unsigned SI : Coupled.set_bits()) {
  3527. LLVM_DEBUG(dbgs() << "testing subscript group " << SI << " { ");
  3528. SmallBitVector Group(Pair[SI].Group);
  3529. SmallBitVector Sivs(Pairs);
  3530. SmallBitVector Mivs(Pairs);
  3531. SmallBitVector ConstrainedLevels(MaxLevels + 1);
  3532. SmallVector<Subscript *, 4> PairsInGroup;
  3533. for (unsigned SJ : Group.set_bits()) {
  3534. LLVM_DEBUG(dbgs() << SJ << " ");
  3535. if (Pair[SJ].Classification == Subscript::SIV)
  3536. Sivs.set(SJ);
  3537. else
  3538. Mivs.set(SJ);
  3539. PairsInGroup.push_back(&Pair[SJ]);
  3540. }
  3541. unifySubscriptType(PairsInGroup);
  3542. LLVM_DEBUG(dbgs() << "}\n");
  3543. while (Sivs.any()) {
  3544. bool Changed = false;
  3545. for (unsigned SJ : Sivs.set_bits()) {
  3546. LLVM_DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n");
  3547. // SJ is an SIV subscript that's part of the current coupled group
  3548. unsigned Level;
  3549. const SCEV *SplitIter = nullptr;
  3550. LLVM_DEBUG(dbgs() << "SIV\n");
  3551. if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
  3552. SplitIter))
  3553. return nullptr;
  3554. ConstrainedLevels.set(Level);
  3555. if (intersectConstraints(&Constraints[Level], &NewConstraint)) {
  3556. if (Constraints[Level].isEmpty()) {
  3557. ++DeltaIndependence;
  3558. return nullptr;
  3559. }
  3560. Changed = true;
  3561. }
  3562. Sivs.reset(SJ);
  3563. }
  3564. if (Changed) {
  3565. // propagate, possibly creating new SIVs and ZIVs
  3566. LLVM_DEBUG(dbgs() << " propagating\n");
  3567. LLVM_DEBUG(dbgs() << "\tMivs = ");
  3568. LLVM_DEBUG(dumpSmallBitVector(Mivs));
  3569. for (unsigned SJ : Mivs.set_bits()) {
  3570. // SJ is an MIV subscript that's part of the current coupled group
  3571. LLVM_DEBUG(dbgs() << "\tSJ = " << SJ << "\n");
  3572. if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops,
  3573. Constraints, Result.Consistent)) {
  3574. LLVM_DEBUG(dbgs() << "\t Changed\n");
  3575. ++DeltaPropagations;
  3576. Pair[SJ].Classification =
  3577. classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
  3578. Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
  3579. Pair[SJ].Loops);
  3580. switch (Pair[SJ].Classification) {
  3581. case Subscript::ZIV:
  3582. LLVM_DEBUG(dbgs() << "ZIV\n");
  3583. if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
  3584. return nullptr;
  3585. Mivs.reset(SJ);
  3586. break;
  3587. case Subscript::SIV:
  3588. Sivs.set(SJ);
  3589. Mivs.reset(SJ);
  3590. break;
  3591. case Subscript::RDIV:
  3592. case Subscript::MIV:
  3593. break;
  3594. default:
  3595. llvm_unreachable("bad subscript classification");
  3596. }
  3597. }
  3598. }
  3599. }
  3600. }
  3601. // test & propagate remaining RDIVs
  3602. for (unsigned SJ : Mivs.set_bits()) {
  3603. if (Pair[SJ].Classification == Subscript::RDIV) {
  3604. LLVM_DEBUG(dbgs() << "RDIV test\n");
  3605. if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result))
  3606. return nullptr;
  3607. // I don't yet understand how to propagate RDIV results
  3608. Mivs.reset(SJ);
  3609. }
  3610. }
  3611. // test remaining MIVs
  3612. // This code is temporary.
  3613. // Better to somehow test all remaining subscripts simultaneously.
  3614. for (unsigned SJ : Mivs.set_bits()) {
  3615. if (Pair[SJ].Classification == Subscript::MIV) {
  3616. LLVM_DEBUG(dbgs() << "MIV test\n");
  3617. if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result))
  3618. return nullptr;
  3619. }
  3620. else
  3621. llvm_unreachable("expected only MIV subscripts at this point");
  3622. }
  3623. // update Result.DV from constraint vector
  3624. LLVM_DEBUG(dbgs() << " updating\n");
  3625. for (unsigned SJ : ConstrainedLevels.set_bits()) {
  3626. if (SJ > CommonLevels)
  3627. break;
  3628. updateDirection(Result.DV[SJ - 1], Constraints[SJ]);
  3629. if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE)
  3630. return nullptr;
  3631. }
  3632. }
  3633. }
  3634. // Make sure the Scalar flags are set correctly.
  3635. SmallBitVector CompleteLoops(MaxLevels + 1);
  3636. for (unsigned SI = 0; SI < Pairs; ++SI)
  3637. CompleteLoops |= Pair[SI].Loops;
  3638. for (unsigned II = 1; II <= CommonLevels; ++II)
  3639. if (CompleteLoops[II])
  3640. Result.DV[II - 1].Scalar = false;
  3641. if (PossiblyLoopIndependent) {
  3642. // Make sure the LoopIndependent flag is set correctly.
  3643. // All directions must include equal, otherwise no
  3644. // loop-independent dependence is possible.
  3645. for (unsigned II = 1; II <= CommonLevels; ++II) {
  3646. if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) {
  3647. Result.LoopIndependent = false;
  3648. break;
  3649. }
  3650. }
  3651. }
  3652. else {
  3653. // On the other hand, if all directions are equal and there's no
  3654. // loop-independent dependence possible, then no dependence exists.
  3655. bool AllEqual = true;
  3656. for (unsigned II = 1; II <= CommonLevels; ++II) {
  3657. if (Result.getDirection(II) != Dependence::DVEntry::EQ) {
  3658. AllEqual = false;
  3659. break;
  3660. }
  3661. }
  3662. if (AllEqual)
  3663. return nullptr;
  3664. }
  3665. return std::make_unique<FullDependence>(std::move(Result));
  3666. }
  3667. //===----------------------------------------------------------------------===//
  3668. // getSplitIteration -
  3669. // Rather than spend rarely-used space recording the splitting iteration
  3670. // during the Weak-Crossing SIV test, we re-compute it on demand.
  3671. // The re-computation is basically a repeat of the entire dependence test,
  3672. // though simplified since we know that the dependence exists.
  3673. // It's tedious, since we must go through all propagations, etc.
  3674. //
  3675. // Care is required to keep this code up to date with respect to the routine
  3676. // above, depends().
  3677. //
  3678. // Generally, the dependence analyzer will be used to build
  3679. // a dependence graph for a function (basically a map from instructions
  3680. // to dependences). Looking for cycles in the graph shows us loops
  3681. // that cannot be trivially vectorized/parallelized.
  3682. //
  3683. // We can try to improve the situation by examining all the dependences
  3684. // that make up the cycle, looking for ones we can break.
  3685. // Sometimes, peeling the first or last iteration of a loop will break
  3686. // dependences, and we've got flags for those possibilities.
  3687. // Sometimes, splitting a loop at some other iteration will do the trick,
  3688. // and we've got a flag for that case. Rather than waste the space to
  3689. // record the exact iteration (since we rarely know), we provide
  3690. // a method that calculates the iteration. It's a drag that it must work
  3691. // from scratch, but wonderful in that it's possible.
  3692. //
  3693. // Here's an example:
  3694. //
  3695. // for (i = 0; i < 10; i++)
  3696. // A[i] = ...
  3697. // ... = A[11 - i]
  3698. //
  3699. // There's a loop-carried flow dependence from the store to the load,
  3700. // found by the weak-crossing SIV test. The dependence will have a flag,
  3701. // indicating that the dependence can be broken by splitting the loop.
  3702. // Calling getSplitIteration will return 5.
  3703. // Splitting the loop breaks the dependence, like so:
  3704. //
  3705. // for (i = 0; i <= 5; i++)
  3706. // A[i] = ...
  3707. // ... = A[11 - i]
  3708. // for (i = 6; i < 10; i++)
  3709. // A[i] = ...
  3710. // ... = A[11 - i]
  3711. //
  3712. // breaks the dependence and allows us to vectorize/parallelize
  3713. // both loops.
  3714. const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
  3715. unsigned SplitLevel) {
  3716. assert(Dep.isSplitable(SplitLevel) &&
  3717. "Dep should be splitable at SplitLevel");
  3718. Instruction *Src = Dep.getSrc();
  3719. Instruction *Dst = Dep.getDst();
  3720. assert(Src->mayReadFromMemory() || Src->mayWriteToMemory());
  3721. assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory());
  3722. assert(isLoadOrStore(Src));
  3723. assert(isLoadOrStore(Dst));
  3724. Value *SrcPtr = getLoadStorePointerOperand(Src);
  3725. Value *DstPtr = getLoadStorePointerOperand(Dst);
  3726. assert(underlyingObjectsAlias(
  3727. AA, F->getParent()->getDataLayout(), MemoryLocation::get(Dst),
  3728. MemoryLocation::get(Src)) == AliasResult::MustAlias);
  3729. // establish loop nesting levels
  3730. establishNestingLevels(Src, Dst);
  3731. FullDependence Result(Src, Dst, false, CommonLevels);
  3732. unsigned Pairs = 1;
  3733. SmallVector<Subscript, 2> Pair(Pairs);
  3734. const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
  3735. const SCEV *DstSCEV = SE->getSCEV(DstPtr);
  3736. Pair[0].Src = SrcSCEV;
  3737. Pair[0].Dst = DstSCEV;
  3738. if (Delinearize) {
  3739. if (tryDelinearize(Src, Dst, Pair)) {
  3740. LLVM_DEBUG(dbgs() << " delinearized\n");
  3741. Pairs = Pair.size();
  3742. }
  3743. }
  3744. for (unsigned P = 0; P < Pairs; ++P) {
  3745. Pair[P].Loops.resize(MaxLevels + 1);
  3746. Pair[P].GroupLoops.resize(MaxLevels + 1);
  3747. Pair[P].Group.resize(Pairs);
  3748. removeMatchingExtensions(&Pair[P]);
  3749. Pair[P].Classification =
  3750. classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()),
  3751. Pair[P].Dst, LI->getLoopFor(Dst->getParent()),
  3752. Pair[P].Loops);
  3753. Pair[P].GroupLoops = Pair[P].Loops;
  3754. Pair[P].Group.set(P);
  3755. }
  3756. SmallBitVector Separable(Pairs);
  3757. SmallBitVector Coupled(Pairs);
  3758. // partition subscripts into separable and minimally-coupled groups
  3759. for (unsigned SI = 0; SI < Pairs; ++SI) {
  3760. if (Pair[SI].Classification == Subscript::NonLinear) {
  3761. // ignore these, but collect loops for later
  3762. collectCommonLoops(Pair[SI].Src,
  3763. LI->getLoopFor(Src->getParent()),
  3764. Pair[SI].Loops);
  3765. collectCommonLoops(Pair[SI].Dst,
  3766. LI->getLoopFor(Dst->getParent()),
  3767. Pair[SI].Loops);
  3768. Result.Consistent = false;
  3769. }
  3770. else if (Pair[SI].Classification == Subscript::ZIV)
  3771. Separable.set(SI);
  3772. else {
  3773. // SIV, RDIV, or MIV, so check for coupled group
  3774. bool Done = true;
  3775. for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) {
  3776. SmallBitVector Intersection = Pair[SI].GroupLoops;
  3777. Intersection &= Pair[SJ].GroupLoops;
  3778. if (Intersection.any()) {
  3779. // accumulate set of all the loops in group
  3780. Pair[SJ].GroupLoops |= Pair[SI].GroupLoops;
  3781. // accumulate set of all subscripts in group
  3782. Pair[SJ].Group |= Pair[SI].Group;
  3783. Done = false;
  3784. }
  3785. }
  3786. if (Done) {
  3787. if (Pair[SI].Group.count() == 1)
  3788. Separable.set(SI);
  3789. else
  3790. Coupled.set(SI);
  3791. }
  3792. }
  3793. }
  3794. Constraint NewConstraint;
  3795. NewConstraint.setAny(SE);
  3796. // test separable subscripts
  3797. for (unsigned SI : Separable.set_bits()) {
  3798. switch (Pair[SI].Classification) {
  3799. case Subscript::SIV: {
  3800. unsigned Level;
  3801. const SCEV *SplitIter = nullptr;
  3802. (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level,
  3803. Result, NewConstraint, SplitIter);
  3804. if (Level == SplitLevel) {
  3805. assert(SplitIter != nullptr);
  3806. return SplitIter;
  3807. }
  3808. break;
  3809. }
  3810. case Subscript::ZIV:
  3811. case Subscript::RDIV:
  3812. case Subscript::MIV:
  3813. break;
  3814. default:
  3815. llvm_unreachable("subscript has unexpected classification");
  3816. }
  3817. }
  3818. if (Coupled.count()) {
  3819. // test coupled subscript groups
  3820. SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
  3821. for (unsigned II = 0; II <= MaxLevels; ++II)
  3822. Constraints[II].setAny(SE);
  3823. for (unsigned SI : Coupled.set_bits()) {
  3824. SmallBitVector Group(Pair[SI].Group);
  3825. SmallBitVector Sivs(Pairs);
  3826. SmallBitVector Mivs(Pairs);
  3827. SmallBitVector ConstrainedLevels(MaxLevels + 1);
  3828. for (unsigned SJ : Group.set_bits()) {
  3829. if (Pair[SJ].Classification == Subscript::SIV)
  3830. Sivs.set(SJ);
  3831. else
  3832. Mivs.set(SJ);
  3833. }
  3834. while (Sivs.any()) {
  3835. bool Changed = false;
  3836. for (unsigned SJ : Sivs.set_bits()) {
  3837. // SJ is an SIV subscript that's part of the current coupled group
  3838. unsigned Level;
  3839. const SCEV *SplitIter = nullptr;
  3840. (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
  3841. Result, NewConstraint, SplitIter);
  3842. if (Level == SplitLevel && SplitIter)
  3843. return SplitIter;
  3844. ConstrainedLevels.set(Level);
  3845. if (intersectConstraints(&Constraints[Level], &NewConstraint))
  3846. Changed = true;
  3847. Sivs.reset(SJ);
  3848. }
  3849. if (Changed) {
  3850. // propagate, possibly creating new SIVs and ZIVs
  3851. for (unsigned SJ : Mivs.set_bits()) {
  3852. // SJ is an MIV subscript that's part of the current coupled group
  3853. if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
  3854. Pair[SJ].Loops, Constraints, Result.Consistent)) {
  3855. Pair[SJ].Classification =
  3856. classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
  3857. Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
  3858. Pair[SJ].Loops);
  3859. switch (Pair[SJ].Classification) {
  3860. case Subscript::ZIV:
  3861. Mivs.reset(SJ);
  3862. break;
  3863. case Subscript::SIV:
  3864. Sivs.set(SJ);
  3865. Mivs.reset(SJ);
  3866. break;
  3867. case Subscript::RDIV:
  3868. case Subscript::MIV:
  3869. break;
  3870. default:
  3871. llvm_unreachable("bad subscript classification");
  3872. }
  3873. }
  3874. }
  3875. }
  3876. }
  3877. }
  3878. }
  3879. llvm_unreachable("somehow reached end of routine");
  3880. return nullptr;
  3881. }