BBVectorize.cpp 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262
  1. //===- BBVectorize.cpp - A Basic-Block Vectorizer -------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements a basic-block vectorization pass. The algorithm was
  11. // inspired by that used by the Vienna MAP Vectorizor by Franchetti and Kral,
  12. // et al. It works by looking for chains of pairable operations and then
  13. // pairing them.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #define BBV_NAME "bb-vectorize"
  17. #include "llvm/Transforms/Vectorize.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/DenseSet.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/SmallSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/ADT/StringExtras.h"
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/Analysis/AliasSetTracker.h"
  27. #include "llvm/Analysis/GlobalsModRef.h"
  28. #include "llvm/Analysis/ScalarEvolution.h"
  29. #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
  30. #include "llvm/Analysis/ScalarEvolutionExpressions.h"
  31. #include "llvm/Analysis/TargetLibraryInfo.h"
  32. #include "llvm/Analysis/TargetTransformInfo.h"
  33. #include "llvm/Analysis/ValueTracking.h"
  34. #include "llvm/IR/Constants.h"
  35. #include "llvm/IR/DataLayout.h"
  36. #include "llvm/IR/DerivedTypes.h"
  37. #include "llvm/IR/Dominators.h"
  38. #include "llvm/IR/Function.h"
  39. #include "llvm/IR/Instructions.h"
  40. #include "llvm/IR/IntrinsicInst.h"
  41. #include "llvm/IR/Intrinsics.h"
  42. #include "llvm/IR/LLVMContext.h"
  43. #include "llvm/IR/Metadata.h"
  44. #include "llvm/IR/Module.h"
  45. #include "llvm/IR/Type.h"
  46. #include "llvm/IR/ValueHandle.h"
  47. #include "llvm/Pass.h"
  48. #include "llvm/Support/CommandLine.h"
  49. #include "llvm/Support/Debug.h"
  50. #include "llvm/Support/raw_ostream.h"
  51. #include "llvm/Transforms/Utils/Local.h"
  52. #include <algorithm>
  53. using namespace llvm;
  54. #define DEBUG_TYPE BBV_NAME
  55. static cl::opt<bool>
  56. IgnoreTargetInfo("bb-vectorize-ignore-target-info", cl::init(false),
  57. cl::Hidden, cl::desc("Ignore target information"));
  58. static cl::opt<unsigned>
  59. ReqChainDepth("bb-vectorize-req-chain-depth", cl::init(6), cl::Hidden,
  60. cl::desc("The required chain depth for vectorization"));
  61. static cl::opt<bool>
  62. UseChainDepthWithTI("bb-vectorize-use-chain-depth", cl::init(false),
  63. cl::Hidden, cl::desc("Use the chain depth requirement with"
  64. " target information"));
  65. static cl::opt<unsigned>
  66. SearchLimit("bb-vectorize-search-limit", cl::init(400), cl::Hidden,
  67. cl::desc("The maximum search distance for instruction pairs"));
  68. static cl::opt<bool>
  69. SplatBreaksChain("bb-vectorize-splat-breaks-chain", cl::init(false), cl::Hidden,
  70. cl::desc("Replicating one element to a pair breaks the chain"));
  71. static cl::opt<unsigned>
  72. VectorBits("bb-vectorize-vector-bits", cl::init(128), cl::Hidden,
  73. cl::desc("The size of the native vector registers"));
  74. static cl::opt<unsigned>
  75. MaxIter("bb-vectorize-max-iter", cl::init(0), cl::Hidden,
  76. cl::desc("The maximum number of pairing iterations"));
  77. static cl::opt<bool>
  78. Pow2LenOnly("bb-vectorize-pow2-len-only", cl::init(false), cl::Hidden,
  79. cl::desc("Don't try to form non-2^n-length vectors"));
  80. static cl::opt<unsigned>
  81. MaxInsts("bb-vectorize-max-instr-per-group", cl::init(500), cl::Hidden,
  82. cl::desc("The maximum number of pairable instructions per group"));
  83. static cl::opt<unsigned>
  84. MaxPairs("bb-vectorize-max-pairs-per-group", cl::init(3000), cl::Hidden,
  85. cl::desc("The maximum number of candidate instruction pairs per group"));
  86. static cl::opt<unsigned>
  87. MaxCandPairsForCycleCheck("bb-vectorize-max-cycle-check-pairs", cl::init(200),
  88. cl::Hidden, cl::desc("The maximum number of candidate pairs with which to use"
  89. " a full cycle check"));
  90. static cl::opt<bool>
  91. NoBools("bb-vectorize-no-bools", cl::init(false), cl::Hidden,
  92. cl::desc("Don't try to vectorize boolean (i1) values"));
  93. static cl::opt<bool>
  94. NoInts("bb-vectorize-no-ints", cl::init(false), cl::Hidden,
  95. cl::desc("Don't try to vectorize integer values"));
  96. static cl::opt<bool>
  97. NoFloats("bb-vectorize-no-floats", cl::init(false), cl::Hidden,
  98. cl::desc("Don't try to vectorize floating-point values"));
  99. // FIXME: This should default to false once pointer vector support works.
  100. static cl::opt<bool>
  101. NoPointers("bb-vectorize-no-pointers", cl::init(/*false*/ true), cl::Hidden,
  102. cl::desc("Don't try to vectorize pointer values"));
  103. static cl::opt<bool>
  104. NoCasts("bb-vectorize-no-casts", cl::init(false), cl::Hidden,
  105. cl::desc("Don't try to vectorize casting (conversion) operations"));
  106. static cl::opt<bool>
  107. NoMath("bb-vectorize-no-math", cl::init(false), cl::Hidden,
  108. cl::desc("Don't try to vectorize floating-point math intrinsics"));
  109. static cl::opt<bool>
  110. NoBitManipulation("bb-vectorize-no-bitmanip", cl::init(false), cl::Hidden,
  111. cl::desc("Don't try to vectorize BitManipulation intrinsics"));
  112. static cl::opt<bool>
  113. NoFMA("bb-vectorize-no-fma", cl::init(false), cl::Hidden,
  114. cl::desc("Don't try to vectorize the fused-multiply-add intrinsic"));
  115. static cl::opt<bool>
  116. NoSelect("bb-vectorize-no-select", cl::init(false), cl::Hidden,
  117. cl::desc("Don't try to vectorize select instructions"));
  118. static cl::opt<bool>
  119. NoCmp("bb-vectorize-no-cmp", cl::init(false), cl::Hidden,
  120. cl::desc("Don't try to vectorize comparison instructions"));
  121. static cl::opt<bool>
  122. NoGEP("bb-vectorize-no-gep", cl::init(false), cl::Hidden,
  123. cl::desc("Don't try to vectorize getelementptr instructions"));
  124. static cl::opt<bool>
  125. NoMemOps("bb-vectorize-no-mem-ops", cl::init(false), cl::Hidden,
  126. cl::desc("Don't try to vectorize loads and stores"));
  127. static cl::opt<bool>
  128. AlignedOnly("bb-vectorize-aligned-only", cl::init(false), cl::Hidden,
  129. cl::desc("Only generate aligned loads and stores"));
  130. static cl::opt<bool>
  131. NoMemOpBoost("bb-vectorize-no-mem-op-boost",
  132. cl::init(false), cl::Hidden,
  133. cl::desc("Don't boost the chain-depth contribution of loads and stores"));
  134. static cl::opt<bool>
  135. FastDep("bb-vectorize-fast-dep", cl::init(false), cl::Hidden,
  136. cl::desc("Use a fast instruction dependency analysis"));
  137. #ifndef NDEBUG
  138. static cl::opt<bool>
  139. DebugInstructionExamination("bb-vectorize-debug-instruction-examination",
  140. cl::init(false), cl::Hidden,
  141. cl::desc("When debugging is enabled, output information on the"
  142. " instruction-examination process"));
  143. static cl::opt<bool>
  144. DebugCandidateSelection("bb-vectorize-debug-candidate-selection",
  145. cl::init(false), cl::Hidden,
  146. cl::desc("When debugging is enabled, output information on the"
  147. " candidate-selection process"));
  148. static cl::opt<bool>
  149. DebugPairSelection("bb-vectorize-debug-pair-selection",
  150. cl::init(false), cl::Hidden,
  151. cl::desc("When debugging is enabled, output information on the"
  152. " pair-selection process"));
  153. static cl::opt<bool>
  154. DebugCycleCheck("bb-vectorize-debug-cycle-check",
  155. cl::init(false), cl::Hidden,
  156. cl::desc("When debugging is enabled, output information on the"
  157. " cycle-checking process"));
  158. static cl::opt<bool>
  159. PrintAfterEveryPair("bb-vectorize-debug-print-after-every-pair",
  160. cl::init(false), cl::Hidden,
  161. cl::desc("When debugging is enabled, dump the basic block after"
  162. " every pair is fused"));
  163. #endif
  164. STATISTIC(NumFusedOps, "Number of operations fused by bb-vectorize");
  165. namespace {
  166. struct BBVectorize : public BasicBlockPass {
  167. static char ID; // Pass identification, replacement for typeid
  168. const VectorizeConfig Config;
  169. BBVectorize(const VectorizeConfig &C = VectorizeConfig())
  170. : BasicBlockPass(ID), Config(C) {
  171. initializeBBVectorizePass(*PassRegistry::getPassRegistry());
  172. }
  173. BBVectorize(Pass *P, Function &F, const VectorizeConfig &C)
  174. : BasicBlockPass(ID), Config(C) {
  175. AA = &P->getAnalysis<AAResultsWrapperPass>().getAAResults();
  176. DT = &P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  177. SE = &P->getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  178. TLI = &P->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  179. TTI = IgnoreTargetInfo
  180. ? nullptr
  181. : &P->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  182. }
  183. typedef std::pair<Value *, Value *> ValuePair;
  184. typedef std::pair<ValuePair, int> ValuePairWithCost;
  185. typedef std::pair<ValuePair, size_t> ValuePairWithDepth;
  186. typedef std::pair<ValuePair, ValuePair> VPPair; // A ValuePair pair
  187. typedef std::pair<VPPair, unsigned> VPPairWithType;
  188. AliasAnalysis *AA;
  189. DominatorTree *DT;
  190. ScalarEvolution *SE;
  191. const TargetLibraryInfo *TLI;
  192. const TargetTransformInfo *TTI;
  193. // FIXME: const correct?
  194. bool vectorizePairs(BasicBlock &BB, bool NonPow2Len = false);
  195. bool getCandidatePairs(BasicBlock &BB,
  196. BasicBlock::iterator &Start,
  197. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  198. DenseSet<ValuePair> &FixedOrderPairs,
  199. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  200. std::vector<Value *> &PairableInsts, bool NonPow2Len);
  201. // FIXME: The current implementation does not account for pairs that
  202. // are connected in multiple ways. For example:
  203. // C1 = A1 / A2; C2 = A2 / A1 (which may be both direct and a swap)
  204. enum PairConnectionType {
  205. PairConnectionDirect,
  206. PairConnectionSwap,
  207. PairConnectionSplat
  208. };
  209. void computeConnectedPairs(
  210. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  211. DenseSet<ValuePair> &CandidatePairsSet,
  212. std::vector<Value *> &PairableInsts,
  213. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  214. DenseMap<VPPair, unsigned> &PairConnectionTypes);
  215. void buildDepMap(BasicBlock &BB,
  216. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  217. std::vector<Value *> &PairableInsts,
  218. DenseSet<ValuePair> &PairableInstUsers);
  219. void choosePairs(DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  220. DenseSet<ValuePair> &CandidatePairsSet,
  221. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  222. std::vector<Value *> &PairableInsts,
  223. DenseSet<ValuePair> &FixedOrderPairs,
  224. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  225. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  226. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
  227. DenseSet<ValuePair> &PairableInstUsers,
  228. DenseMap<Value *, Value *>& ChosenPairs);
  229. void fuseChosenPairs(BasicBlock &BB,
  230. std::vector<Value *> &PairableInsts,
  231. DenseMap<Value *, Value *>& ChosenPairs,
  232. DenseSet<ValuePair> &FixedOrderPairs,
  233. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  234. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  235. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps);
  236. bool isInstVectorizable(Instruction *I, bool &IsSimpleLoadStore);
  237. bool areInstsCompatible(Instruction *I, Instruction *J,
  238. bool IsSimpleLoadStore, bool NonPow2Len,
  239. int &CostSavings, int &FixedOrder);
  240. bool trackUsesOfI(DenseSet<Value *> &Users,
  241. AliasSetTracker &WriteSet, Instruction *I,
  242. Instruction *J, bool UpdateUsers = true,
  243. DenseSet<ValuePair> *LoadMoveSetPairs = nullptr);
  244. void computePairsConnectedTo(
  245. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  246. DenseSet<ValuePair> &CandidatePairsSet,
  247. std::vector<Value *> &PairableInsts,
  248. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  249. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  250. ValuePair P);
  251. bool pairsConflict(ValuePair P, ValuePair Q,
  252. DenseSet<ValuePair> &PairableInstUsers,
  253. DenseMap<ValuePair, std::vector<ValuePair> >
  254. *PairableInstUserMap = nullptr,
  255. DenseSet<VPPair> *PairableInstUserPairSet = nullptr);
  256. bool pairWillFormCycle(ValuePair P,
  257. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUsers,
  258. DenseSet<ValuePair> &CurrentPairs);
  259. void pruneDAGFor(
  260. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  261. std::vector<Value *> &PairableInsts,
  262. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  263. DenseSet<ValuePair> &PairableInstUsers,
  264. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
  265. DenseSet<VPPair> &PairableInstUserPairSet,
  266. DenseMap<Value *, Value *> &ChosenPairs,
  267. DenseMap<ValuePair, size_t> &DAG,
  268. DenseSet<ValuePair> &PrunedDAG, ValuePair J,
  269. bool UseCycleCheck);
  270. void buildInitialDAGFor(
  271. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  272. DenseSet<ValuePair> &CandidatePairsSet,
  273. std::vector<Value *> &PairableInsts,
  274. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  275. DenseSet<ValuePair> &PairableInstUsers,
  276. DenseMap<Value *, Value *> &ChosenPairs,
  277. DenseMap<ValuePair, size_t> &DAG, ValuePair J);
  278. void findBestDAGFor(
  279. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  280. DenseSet<ValuePair> &CandidatePairsSet,
  281. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  282. std::vector<Value *> &PairableInsts,
  283. DenseSet<ValuePair> &FixedOrderPairs,
  284. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  285. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  286. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
  287. DenseSet<ValuePair> &PairableInstUsers,
  288. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
  289. DenseSet<VPPair> &PairableInstUserPairSet,
  290. DenseMap<Value *, Value *> &ChosenPairs,
  291. DenseSet<ValuePair> &BestDAG, size_t &BestMaxDepth,
  292. int &BestEffSize, Value *II, std::vector<Value *>&JJ,
  293. bool UseCycleCheck);
  294. Value *getReplacementPointerInput(LLVMContext& Context, Instruction *I,
  295. Instruction *J, unsigned o);
  296. void fillNewShuffleMask(LLVMContext& Context, Instruction *J,
  297. unsigned MaskOffset, unsigned NumInElem,
  298. unsigned NumInElem1, unsigned IdxOffset,
  299. std::vector<Constant*> &Mask);
  300. Value *getReplacementShuffleMask(LLVMContext& Context, Instruction *I,
  301. Instruction *J);
  302. bool expandIEChain(LLVMContext& Context, Instruction *I, Instruction *J,
  303. unsigned o, Value *&LOp, unsigned numElemL,
  304. Type *ArgTypeL, Type *ArgTypeR, bool IBeforeJ,
  305. unsigned IdxOff = 0);
  306. Value *getReplacementInput(LLVMContext& Context, Instruction *I,
  307. Instruction *J, unsigned o, bool IBeforeJ);
  308. void getReplacementInputsForPair(LLVMContext& Context, Instruction *I,
  309. Instruction *J, SmallVectorImpl<Value *> &ReplacedOperands,
  310. bool IBeforeJ);
  311. void replaceOutputsOfPair(LLVMContext& Context, Instruction *I,
  312. Instruction *J, Instruction *K,
  313. Instruction *&InsertionPt, Instruction *&K1,
  314. Instruction *&K2);
  315. void collectPairLoadMoveSet(BasicBlock &BB,
  316. DenseMap<Value *, Value *> &ChosenPairs,
  317. DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
  318. DenseSet<ValuePair> &LoadMoveSetPairs,
  319. Instruction *I);
  320. void collectLoadMoveSet(BasicBlock &BB,
  321. std::vector<Value *> &PairableInsts,
  322. DenseMap<Value *, Value *> &ChosenPairs,
  323. DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
  324. DenseSet<ValuePair> &LoadMoveSetPairs);
  325. bool canMoveUsesOfIAfterJ(BasicBlock &BB,
  326. DenseSet<ValuePair> &LoadMoveSetPairs,
  327. Instruction *I, Instruction *J);
  328. void moveUsesOfIAfterJ(BasicBlock &BB,
  329. DenseSet<ValuePair> &LoadMoveSetPairs,
  330. Instruction *&InsertionPt,
  331. Instruction *I, Instruction *J);
  332. bool vectorizeBB(BasicBlock &BB) {
  333. if (skipBasicBlock(BB))
  334. return false;
  335. if (!DT->isReachableFromEntry(&BB)) {
  336. DEBUG(dbgs() << "BBV: skipping unreachable " << BB.getName() <<
  337. " in " << BB.getParent()->getName() << "\n");
  338. return false;
  339. }
  340. DEBUG(if (TTI) dbgs() << "BBV: using target information\n");
  341. bool changed = false;
  342. // Iterate a sufficient number of times to merge types of size 1 bit,
  343. // then 2 bits, then 4, etc. up to half of the target vector width of the
  344. // target vector register.
  345. unsigned n = 1;
  346. for (unsigned v = 2;
  347. (TTI || v <= Config.VectorBits) &&
  348. (!Config.MaxIter || n <= Config.MaxIter);
  349. v *= 2, ++n) {
  350. DEBUG(dbgs() << "BBV: fusing loop #" << n <<
  351. " for " << BB.getName() << " in " <<
  352. BB.getParent()->getName() << "...\n");
  353. if (vectorizePairs(BB))
  354. changed = true;
  355. else
  356. break;
  357. }
  358. if (changed && !Pow2LenOnly) {
  359. ++n;
  360. for (; !Config.MaxIter || n <= Config.MaxIter; ++n) {
  361. DEBUG(dbgs() << "BBV: fusing for non-2^n-length vectors loop #: " <<
  362. n << " for " << BB.getName() << " in " <<
  363. BB.getParent()->getName() << "...\n");
  364. if (!vectorizePairs(BB, true)) break;
  365. }
  366. }
  367. DEBUG(dbgs() << "BBV: done!\n");
  368. return changed;
  369. }
  370. bool runOnBasicBlock(BasicBlock &BB) override {
  371. // OptimizeNone check deferred to vectorizeBB().
  372. AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
  373. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  374. SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  375. TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
  376. TTI = IgnoreTargetInfo
  377. ? nullptr
  378. : &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
  379. *BB.getParent());
  380. return vectorizeBB(BB);
  381. }
  382. void getAnalysisUsage(AnalysisUsage &AU) const override {
  383. BasicBlockPass::getAnalysisUsage(AU);
  384. AU.addRequired<AAResultsWrapperPass>();
  385. AU.addRequired<DominatorTreeWrapperPass>();
  386. AU.addRequired<ScalarEvolutionWrapperPass>();
  387. AU.addRequired<TargetLibraryInfoWrapperPass>();
  388. AU.addRequired<TargetTransformInfoWrapperPass>();
  389. AU.addPreserved<DominatorTreeWrapperPass>();
  390. AU.addPreserved<GlobalsAAWrapperPass>();
  391. AU.addPreserved<ScalarEvolutionWrapperPass>();
  392. AU.addPreserved<SCEVAAWrapperPass>();
  393. AU.setPreservesCFG();
  394. }
  395. static inline VectorType *getVecTypeForPair(Type *ElemTy, Type *Elem2Ty) {
  396. assert(ElemTy->getScalarType() == Elem2Ty->getScalarType() &&
  397. "Cannot form vector from incompatible scalar types");
  398. Type *STy = ElemTy->getScalarType();
  399. unsigned numElem;
  400. if (VectorType *VTy = dyn_cast<VectorType>(ElemTy)) {
  401. numElem = VTy->getNumElements();
  402. } else {
  403. numElem = 1;
  404. }
  405. if (VectorType *VTy = dyn_cast<VectorType>(Elem2Ty)) {
  406. numElem += VTy->getNumElements();
  407. } else {
  408. numElem += 1;
  409. }
  410. return VectorType::get(STy, numElem);
  411. }
  412. static inline void getInstructionTypes(Instruction *I,
  413. Type *&T1, Type *&T2) {
  414. if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  415. // For stores, it is the value type, not the pointer type that matters
  416. // because the value is what will come from a vector register.
  417. Value *IVal = SI->getValueOperand();
  418. T1 = IVal->getType();
  419. } else {
  420. T1 = I->getType();
  421. }
  422. if (CastInst *CI = dyn_cast<CastInst>(I))
  423. T2 = CI->getSrcTy();
  424. else
  425. T2 = T1;
  426. if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
  427. T2 = SI->getCondition()->getType();
  428. } else if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(I)) {
  429. T2 = SI->getOperand(0)->getType();
  430. } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
  431. T2 = CI->getOperand(0)->getType();
  432. }
  433. }
  434. // Returns the weight associated with the provided value. A chain of
  435. // candidate pairs has a length given by the sum of the weights of its
  436. // members (one weight per pair; the weight of each member of the pair
  437. // is assumed to be the same). This length is then compared to the
  438. // chain-length threshold to determine if a given chain is significant
  439. // enough to be vectorized. The length is also used in comparing
  440. // candidate chains where longer chains are considered to be better.
  441. // Note: when this function returns 0, the resulting instructions are
  442. // not actually fused.
  443. inline size_t getDepthFactor(Value *V) {
  444. // InsertElement and ExtractElement have a depth factor of zero. This is
  445. // for two reasons: First, they cannot be usefully fused. Second, because
  446. // the pass generates a lot of these, they can confuse the simple metric
  447. // used to compare the dags in the next iteration. Thus, giving them a
  448. // weight of zero allows the pass to essentially ignore them in
  449. // subsequent iterations when looking for vectorization opportunities
  450. // while still tracking dependency chains that flow through those
  451. // instructions.
  452. if (isa<InsertElementInst>(V) || isa<ExtractElementInst>(V))
  453. return 0;
  454. // Give a load or store half of the required depth so that load/store
  455. // pairs will vectorize.
  456. if (!Config.NoMemOpBoost && (isa<LoadInst>(V) || isa<StoreInst>(V)))
  457. return Config.ReqChainDepth/2;
  458. return 1;
  459. }
  460. // Returns the cost of the provided instruction using TTI.
  461. // This does not handle loads and stores.
  462. unsigned getInstrCost(unsigned Opcode, Type *T1, Type *T2,
  463. TargetTransformInfo::OperandValueKind Op1VK =
  464. TargetTransformInfo::OK_AnyValue,
  465. TargetTransformInfo::OperandValueKind Op2VK =
  466. TargetTransformInfo::OK_AnyValue) {
  467. switch (Opcode) {
  468. default: break;
  469. case Instruction::GetElementPtr:
  470. // We mark this instruction as zero-cost because scalar GEPs are usually
  471. // lowered to the instruction addressing mode. At the moment we don't
  472. // generate vector GEPs.
  473. return 0;
  474. case Instruction::Br:
  475. return TTI->getCFInstrCost(Opcode);
  476. case Instruction::PHI:
  477. return 0;
  478. case Instruction::Add:
  479. case Instruction::FAdd:
  480. case Instruction::Sub:
  481. case Instruction::FSub:
  482. case Instruction::Mul:
  483. case Instruction::FMul:
  484. case Instruction::UDiv:
  485. case Instruction::SDiv:
  486. case Instruction::FDiv:
  487. case Instruction::URem:
  488. case Instruction::SRem:
  489. case Instruction::FRem:
  490. case Instruction::Shl:
  491. case Instruction::LShr:
  492. case Instruction::AShr:
  493. case Instruction::And:
  494. case Instruction::Or:
  495. case Instruction::Xor:
  496. return TTI->getArithmeticInstrCost(Opcode, T1, Op1VK, Op2VK);
  497. case Instruction::Select:
  498. case Instruction::ICmp:
  499. case Instruction::FCmp:
  500. return TTI->getCmpSelInstrCost(Opcode, T1, T2);
  501. case Instruction::ZExt:
  502. case Instruction::SExt:
  503. case Instruction::FPToUI:
  504. case Instruction::FPToSI:
  505. case Instruction::FPExt:
  506. case Instruction::PtrToInt:
  507. case Instruction::IntToPtr:
  508. case Instruction::SIToFP:
  509. case Instruction::UIToFP:
  510. case Instruction::Trunc:
  511. case Instruction::FPTrunc:
  512. case Instruction::BitCast:
  513. case Instruction::ShuffleVector:
  514. return TTI->getCastInstrCost(Opcode, T1, T2);
  515. }
  516. return 1;
  517. }
  518. // This determines the relative offset of two loads or stores, returning
  519. // true if the offset could be determined to be some constant value.
  520. // For example, if OffsetInElmts == 1, then J accesses the memory directly
  521. // after I; if OffsetInElmts == -1 then I accesses the memory
  522. // directly after J.
  523. bool getPairPtrInfo(Instruction *I, Instruction *J,
  524. Value *&IPtr, Value *&JPtr, unsigned &IAlignment, unsigned &JAlignment,
  525. unsigned &IAddressSpace, unsigned &JAddressSpace,
  526. int64_t &OffsetInElmts, bool ComputeOffset = true) {
  527. OffsetInElmts = 0;
  528. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  529. LoadInst *LJ = cast<LoadInst>(J);
  530. IPtr = LI->getPointerOperand();
  531. JPtr = LJ->getPointerOperand();
  532. IAlignment = LI->getAlignment();
  533. JAlignment = LJ->getAlignment();
  534. IAddressSpace = LI->getPointerAddressSpace();
  535. JAddressSpace = LJ->getPointerAddressSpace();
  536. } else {
  537. StoreInst *SI = cast<StoreInst>(I), *SJ = cast<StoreInst>(J);
  538. IPtr = SI->getPointerOperand();
  539. JPtr = SJ->getPointerOperand();
  540. IAlignment = SI->getAlignment();
  541. JAlignment = SJ->getAlignment();
  542. IAddressSpace = SI->getPointerAddressSpace();
  543. JAddressSpace = SJ->getPointerAddressSpace();
  544. }
  545. if (!ComputeOffset)
  546. return true;
  547. const SCEV *IPtrSCEV = SE->getSCEV(IPtr);
  548. const SCEV *JPtrSCEV = SE->getSCEV(JPtr);
  549. // If this is a trivial offset, then we'll get something like
  550. // 1*sizeof(type). With target data, which we need anyway, this will get
  551. // constant folded into a number.
  552. const SCEV *OffsetSCEV = SE->getMinusSCEV(JPtrSCEV, IPtrSCEV);
  553. if (const SCEVConstant *ConstOffSCEV =
  554. dyn_cast<SCEVConstant>(OffsetSCEV)) {
  555. ConstantInt *IntOff = ConstOffSCEV->getValue();
  556. int64_t Offset = IntOff->getSExtValue();
  557. const DataLayout &DL = I->getModule()->getDataLayout();
  558. Type *VTy = IPtr->getType()->getPointerElementType();
  559. int64_t VTyTSS = (int64_t)DL.getTypeStoreSize(VTy);
  560. Type *VTy2 = JPtr->getType()->getPointerElementType();
  561. if (VTy != VTy2 && Offset < 0) {
  562. int64_t VTy2TSS = (int64_t)DL.getTypeStoreSize(VTy2);
  563. OffsetInElmts = Offset/VTy2TSS;
  564. return (std::abs(Offset) % VTy2TSS) == 0;
  565. }
  566. OffsetInElmts = Offset/VTyTSS;
  567. return (std::abs(Offset) % VTyTSS) == 0;
  568. }
  569. return false;
  570. }
  571. // Returns true if the provided CallInst represents an intrinsic that can
  572. // be vectorized.
  573. bool isVectorizableIntrinsic(CallInst* I) {
  574. Function *F = I->getCalledFunction();
  575. if (!F) return false;
  576. Intrinsic::ID IID = F->getIntrinsicID();
  577. if (!IID) return false;
  578. switch(IID) {
  579. default:
  580. return false;
  581. case Intrinsic::sqrt:
  582. case Intrinsic::powi:
  583. case Intrinsic::sin:
  584. case Intrinsic::cos:
  585. case Intrinsic::log:
  586. case Intrinsic::log2:
  587. case Intrinsic::log10:
  588. case Intrinsic::exp:
  589. case Intrinsic::exp2:
  590. case Intrinsic::pow:
  591. case Intrinsic::round:
  592. case Intrinsic::copysign:
  593. case Intrinsic::ceil:
  594. case Intrinsic::nearbyint:
  595. case Intrinsic::rint:
  596. case Intrinsic::trunc:
  597. case Intrinsic::floor:
  598. case Intrinsic::fabs:
  599. case Intrinsic::minnum:
  600. case Intrinsic::maxnum:
  601. return Config.VectorizeMath;
  602. case Intrinsic::bswap:
  603. case Intrinsic::ctpop:
  604. case Intrinsic::ctlz:
  605. case Intrinsic::cttz:
  606. return Config.VectorizeBitManipulations;
  607. case Intrinsic::fma:
  608. case Intrinsic::fmuladd:
  609. return Config.VectorizeFMA;
  610. }
  611. }
  612. bool isPureIEChain(InsertElementInst *IE) {
  613. InsertElementInst *IENext = IE;
  614. do {
  615. if (!isa<UndefValue>(IENext->getOperand(0)) &&
  616. !isa<InsertElementInst>(IENext->getOperand(0))) {
  617. return false;
  618. }
  619. } while ((IENext =
  620. dyn_cast<InsertElementInst>(IENext->getOperand(0))));
  621. return true;
  622. }
  623. };
  624. // This function implements one vectorization iteration on the provided
  625. // basic block. It returns true if the block is changed.
  626. bool BBVectorize::vectorizePairs(BasicBlock &BB, bool NonPow2Len) {
  627. bool ShouldContinue;
  628. BasicBlock::iterator Start = BB.getFirstInsertionPt();
  629. std::vector<Value *> AllPairableInsts;
  630. DenseMap<Value *, Value *> AllChosenPairs;
  631. DenseSet<ValuePair> AllFixedOrderPairs;
  632. DenseMap<VPPair, unsigned> AllPairConnectionTypes;
  633. DenseMap<ValuePair, std::vector<ValuePair> > AllConnectedPairs,
  634. AllConnectedPairDeps;
  635. do {
  636. std::vector<Value *> PairableInsts;
  637. DenseMap<Value *, std::vector<Value *> > CandidatePairs;
  638. DenseSet<ValuePair> FixedOrderPairs;
  639. DenseMap<ValuePair, int> CandidatePairCostSavings;
  640. ShouldContinue = getCandidatePairs(BB, Start, CandidatePairs,
  641. FixedOrderPairs,
  642. CandidatePairCostSavings,
  643. PairableInsts, NonPow2Len);
  644. if (PairableInsts.empty()) continue;
  645. // Build the candidate pair set for faster lookups.
  646. DenseSet<ValuePair> CandidatePairsSet;
  647. for (DenseMap<Value *, std::vector<Value *> >::iterator I =
  648. CandidatePairs.begin(), E = CandidatePairs.end(); I != E; ++I)
  649. for (std::vector<Value *>::iterator J = I->second.begin(),
  650. JE = I->second.end(); J != JE; ++J)
  651. CandidatePairsSet.insert(ValuePair(I->first, *J));
  652. // Now we have a map of all of the pairable instructions and we need to
  653. // select the best possible pairing. A good pairing is one such that the
  654. // users of the pair are also paired. This defines a (directed) forest
  655. // over the pairs such that two pairs are connected iff the second pair
  656. // uses the first.
  657. // Note that it only matters that both members of the second pair use some
  658. // element of the first pair (to allow for splatting).
  659. DenseMap<ValuePair, std::vector<ValuePair> > ConnectedPairs,
  660. ConnectedPairDeps;
  661. DenseMap<VPPair, unsigned> PairConnectionTypes;
  662. computeConnectedPairs(CandidatePairs, CandidatePairsSet,
  663. PairableInsts, ConnectedPairs, PairConnectionTypes);
  664. if (ConnectedPairs.empty()) continue;
  665. for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator
  666. I = ConnectedPairs.begin(), IE = ConnectedPairs.end();
  667. I != IE; ++I)
  668. for (std::vector<ValuePair>::iterator J = I->second.begin(),
  669. JE = I->second.end(); J != JE; ++J)
  670. ConnectedPairDeps[*J].push_back(I->first);
  671. // Build the pairable-instruction dependency map
  672. DenseSet<ValuePair> PairableInstUsers;
  673. buildDepMap(BB, CandidatePairs, PairableInsts, PairableInstUsers);
  674. // There is now a graph of the connected pairs. For each variable, pick
  675. // the pairing with the largest dag meeting the depth requirement on at
  676. // least one branch. Then select all pairings that are part of that dag
  677. // and remove them from the list of available pairings and pairable
  678. // variables.
  679. DenseMap<Value *, Value *> ChosenPairs;
  680. choosePairs(CandidatePairs, CandidatePairsSet,
  681. CandidatePairCostSavings,
  682. PairableInsts, FixedOrderPairs, PairConnectionTypes,
  683. ConnectedPairs, ConnectedPairDeps,
  684. PairableInstUsers, ChosenPairs);
  685. if (ChosenPairs.empty()) continue;
  686. AllPairableInsts.insert(AllPairableInsts.end(), PairableInsts.begin(),
  687. PairableInsts.end());
  688. AllChosenPairs.insert(ChosenPairs.begin(), ChosenPairs.end());
  689. // Only for the chosen pairs, propagate information on fixed-order pairs,
  690. // pair connections, and their types to the data structures used by the
  691. // pair fusion procedures.
  692. for (DenseMap<Value *, Value *>::iterator I = ChosenPairs.begin(),
  693. IE = ChosenPairs.end(); I != IE; ++I) {
  694. if (FixedOrderPairs.count(*I))
  695. AllFixedOrderPairs.insert(*I);
  696. else if (FixedOrderPairs.count(ValuePair(I->second, I->first)))
  697. AllFixedOrderPairs.insert(ValuePair(I->second, I->first));
  698. for (DenseMap<Value *, Value *>::iterator J = ChosenPairs.begin();
  699. J != IE; ++J) {
  700. DenseMap<VPPair, unsigned>::iterator K =
  701. PairConnectionTypes.find(VPPair(*I, *J));
  702. if (K != PairConnectionTypes.end()) {
  703. AllPairConnectionTypes.insert(*K);
  704. } else {
  705. K = PairConnectionTypes.find(VPPair(*J, *I));
  706. if (K != PairConnectionTypes.end())
  707. AllPairConnectionTypes.insert(*K);
  708. }
  709. }
  710. }
  711. for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator
  712. I = ConnectedPairs.begin(), IE = ConnectedPairs.end();
  713. I != IE; ++I)
  714. for (std::vector<ValuePair>::iterator J = I->second.begin(),
  715. JE = I->second.end(); J != JE; ++J)
  716. if (AllPairConnectionTypes.count(VPPair(I->first, *J))) {
  717. AllConnectedPairs[I->first].push_back(*J);
  718. AllConnectedPairDeps[*J].push_back(I->first);
  719. }
  720. } while (ShouldContinue);
  721. if (AllChosenPairs.empty()) return false;
  722. NumFusedOps += AllChosenPairs.size();
  723. // A set of pairs has now been selected. It is now necessary to replace the
  724. // paired instructions with vector instructions. For this procedure each
  725. // operand must be replaced with a vector operand. This vector is formed
  726. // by using build_vector on the old operands. The replaced values are then
  727. // replaced with a vector_extract on the result. Subsequent optimization
  728. // passes should coalesce the build/extract combinations.
  729. fuseChosenPairs(BB, AllPairableInsts, AllChosenPairs, AllFixedOrderPairs,
  730. AllPairConnectionTypes,
  731. AllConnectedPairs, AllConnectedPairDeps);
  732. // It is important to cleanup here so that future iterations of this
  733. // function have less work to do.
  734. (void)SimplifyInstructionsInBlock(&BB, TLI);
  735. return true;
  736. }
  737. // This function returns true if the provided instruction is capable of being
  738. // fused into a vector instruction. This determination is based only on the
  739. // type and other attributes of the instruction.
  740. bool BBVectorize::isInstVectorizable(Instruction *I,
  741. bool &IsSimpleLoadStore) {
  742. IsSimpleLoadStore = false;
  743. if (CallInst *C = dyn_cast<CallInst>(I)) {
  744. if (!isVectorizableIntrinsic(C))
  745. return false;
  746. } else if (LoadInst *L = dyn_cast<LoadInst>(I)) {
  747. // Vectorize simple loads if possbile:
  748. IsSimpleLoadStore = L->isSimple();
  749. if (!IsSimpleLoadStore || !Config.VectorizeMemOps)
  750. return false;
  751. } else if (StoreInst *S = dyn_cast<StoreInst>(I)) {
  752. // Vectorize simple stores if possbile:
  753. IsSimpleLoadStore = S->isSimple();
  754. if (!IsSimpleLoadStore || !Config.VectorizeMemOps)
  755. return false;
  756. } else if (CastInst *C = dyn_cast<CastInst>(I)) {
  757. // We can vectorize casts, but not casts of pointer types, etc.
  758. if (!Config.VectorizeCasts)
  759. return false;
  760. Type *SrcTy = C->getSrcTy();
  761. if (!SrcTy->isSingleValueType())
  762. return false;
  763. Type *DestTy = C->getDestTy();
  764. if (!DestTy->isSingleValueType())
  765. return false;
  766. } else if (isa<SelectInst>(I)) {
  767. if (!Config.VectorizeSelect)
  768. return false;
  769. } else if (isa<CmpInst>(I)) {
  770. if (!Config.VectorizeCmp)
  771. return false;
  772. } else if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(I)) {
  773. if (!Config.VectorizeGEP)
  774. return false;
  775. // Currently, vector GEPs exist only with one index.
  776. if (G->getNumIndices() != 1)
  777. return false;
  778. } else if (!(I->isBinaryOp() || isa<ShuffleVectorInst>(I) ||
  779. isa<ExtractElementInst>(I) || isa<InsertElementInst>(I))) {
  780. return false;
  781. }
  782. Type *T1, *T2;
  783. getInstructionTypes(I, T1, T2);
  784. // Not every type can be vectorized...
  785. if (!(VectorType::isValidElementType(T1) || T1->isVectorTy()) ||
  786. !(VectorType::isValidElementType(T2) || T2->isVectorTy()))
  787. return false;
  788. if (T1->getScalarSizeInBits() == 1) {
  789. if (!Config.VectorizeBools)
  790. return false;
  791. } else {
  792. if (!Config.VectorizeInts && T1->isIntOrIntVectorTy())
  793. return false;
  794. }
  795. if (T2->getScalarSizeInBits() == 1) {
  796. if (!Config.VectorizeBools)
  797. return false;
  798. } else {
  799. if (!Config.VectorizeInts && T2->isIntOrIntVectorTy())
  800. return false;
  801. }
  802. if (!Config.VectorizeFloats
  803. && (T1->isFPOrFPVectorTy() || T2->isFPOrFPVectorTy()))
  804. return false;
  805. // Don't vectorize target-specific types.
  806. if (T1->isX86_FP80Ty() || T1->isPPC_FP128Ty() || T1->isX86_MMXTy())
  807. return false;
  808. if (T2->isX86_FP80Ty() || T2->isPPC_FP128Ty() || T2->isX86_MMXTy())
  809. return false;
  810. if (!Config.VectorizePointers && (T1->getScalarType()->isPointerTy() ||
  811. T2->getScalarType()->isPointerTy()))
  812. return false;
  813. if (!TTI && (T1->getPrimitiveSizeInBits() >= Config.VectorBits ||
  814. T2->getPrimitiveSizeInBits() >= Config.VectorBits))
  815. return false;
  816. return true;
  817. }
  818. // This function returns true if the two provided instructions are compatible
  819. // (meaning that they can be fused into a vector instruction). This assumes
  820. // that I has already been determined to be vectorizable and that J is not
  821. // in the use dag of I.
  822. bool BBVectorize::areInstsCompatible(Instruction *I, Instruction *J,
  823. bool IsSimpleLoadStore, bool NonPow2Len,
  824. int &CostSavings, int &FixedOrder) {
  825. DEBUG(if (DebugInstructionExamination) dbgs() << "BBV: looking at " << *I <<
  826. " <-> " << *J << "\n");
  827. CostSavings = 0;
  828. FixedOrder = 0;
  829. // Loads and stores can be merged if they have different alignments,
  830. // but are otherwise the same.
  831. if (!J->isSameOperationAs(I, Instruction::CompareIgnoringAlignment |
  832. (NonPow2Len ? Instruction::CompareUsingScalarTypes : 0)))
  833. return false;
  834. Type *IT1, *IT2, *JT1, *JT2;
  835. getInstructionTypes(I, IT1, IT2);
  836. getInstructionTypes(J, JT1, JT2);
  837. unsigned MaxTypeBits = std::max(
  838. IT1->getPrimitiveSizeInBits() + JT1->getPrimitiveSizeInBits(),
  839. IT2->getPrimitiveSizeInBits() + JT2->getPrimitiveSizeInBits());
  840. if (!TTI && MaxTypeBits > Config.VectorBits)
  841. return false;
  842. // FIXME: handle addsub-type operations!
  843. if (IsSimpleLoadStore) {
  844. Value *IPtr, *JPtr;
  845. unsigned IAlignment, JAlignment, IAddressSpace, JAddressSpace;
  846. int64_t OffsetInElmts = 0;
  847. if (getPairPtrInfo(I, J, IPtr, JPtr, IAlignment, JAlignment,
  848. IAddressSpace, JAddressSpace, OffsetInElmts) &&
  849. std::abs(OffsetInElmts) == 1) {
  850. FixedOrder = (int) OffsetInElmts;
  851. unsigned BottomAlignment = IAlignment;
  852. if (OffsetInElmts < 0) BottomAlignment = JAlignment;
  853. Type *aTypeI = isa<StoreInst>(I) ?
  854. cast<StoreInst>(I)->getValueOperand()->getType() : I->getType();
  855. Type *aTypeJ = isa<StoreInst>(J) ?
  856. cast<StoreInst>(J)->getValueOperand()->getType() : J->getType();
  857. Type *VType = getVecTypeForPair(aTypeI, aTypeJ);
  858. if (Config.AlignedOnly) {
  859. // An aligned load or store is possible only if the instruction
  860. // with the lower offset has an alignment suitable for the
  861. // vector type.
  862. const DataLayout &DL = I->getModule()->getDataLayout();
  863. unsigned VecAlignment = DL.getPrefTypeAlignment(VType);
  864. if (BottomAlignment < VecAlignment)
  865. return false;
  866. }
  867. if (TTI) {
  868. unsigned ICost = TTI->getMemoryOpCost(I->getOpcode(), aTypeI,
  869. IAlignment, IAddressSpace);
  870. unsigned JCost = TTI->getMemoryOpCost(J->getOpcode(), aTypeJ,
  871. JAlignment, JAddressSpace);
  872. unsigned VCost = TTI->getMemoryOpCost(I->getOpcode(), VType,
  873. BottomAlignment,
  874. IAddressSpace);
  875. ICost += TTI->getAddressComputationCost(aTypeI);
  876. JCost += TTI->getAddressComputationCost(aTypeJ);
  877. VCost += TTI->getAddressComputationCost(VType);
  878. if (VCost > ICost + JCost)
  879. return false;
  880. // We don't want to fuse to a type that will be split, even
  881. // if the two input types will also be split and there is no other
  882. // associated cost.
  883. unsigned VParts = TTI->getNumberOfParts(VType);
  884. if (VParts > 1)
  885. return false;
  886. else if (!VParts && VCost == ICost + JCost)
  887. return false;
  888. CostSavings = ICost + JCost - VCost;
  889. }
  890. } else {
  891. return false;
  892. }
  893. } else if (TTI) {
  894. unsigned ICost = getInstrCost(I->getOpcode(), IT1, IT2);
  895. unsigned JCost = getInstrCost(J->getOpcode(), JT1, JT2);
  896. Type *VT1 = getVecTypeForPair(IT1, JT1),
  897. *VT2 = getVecTypeForPair(IT2, JT2);
  898. TargetTransformInfo::OperandValueKind Op1VK =
  899. TargetTransformInfo::OK_AnyValue;
  900. TargetTransformInfo::OperandValueKind Op2VK =
  901. TargetTransformInfo::OK_AnyValue;
  902. // On some targets (example X86) the cost of a vector shift may vary
  903. // depending on whether the second operand is a Uniform or
  904. // NonUniform Constant.
  905. switch (I->getOpcode()) {
  906. default : break;
  907. case Instruction::Shl:
  908. case Instruction::LShr:
  909. case Instruction::AShr:
  910. // If both I and J are scalar shifts by constant, then the
  911. // merged vector shift count would be either a constant splat value
  912. // or a non-uniform vector of constants.
  913. if (ConstantInt *CII = dyn_cast<ConstantInt>(I->getOperand(1))) {
  914. if (ConstantInt *CIJ = dyn_cast<ConstantInt>(J->getOperand(1)))
  915. Op2VK = CII == CIJ ? TargetTransformInfo::OK_UniformConstantValue :
  916. TargetTransformInfo::OK_NonUniformConstantValue;
  917. } else {
  918. // Check for a splat of a constant or for a non uniform vector
  919. // of constants.
  920. Value *IOp = I->getOperand(1);
  921. Value *JOp = J->getOperand(1);
  922. if ((isa<ConstantVector>(IOp) || isa<ConstantDataVector>(IOp)) &&
  923. (isa<ConstantVector>(JOp) || isa<ConstantDataVector>(JOp))) {
  924. Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
  925. Constant *SplatValue = cast<Constant>(IOp)->getSplatValue();
  926. if (SplatValue != nullptr &&
  927. SplatValue == cast<Constant>(JOp)->getSplatValue())
  928. Op2VK = TargetTransformInfo::OK_UniformConstantValue;
  929. }
  930. }
  931. }
  932. // Note that this procedure is incorrect for insert and extract element
  933. // instructions (because combining these often results in a shuffle),
  934. // but this cost is ignored (because insert and extract element
  935. // instructions are assigned a zero depth factor and are not really
  936. // fused in general).
  937. unsigned VCost = getInstrCost(I->getOpcode(), VT1, VT2, Op1VK, Op2VK);
  938. if (VCost > ICost + JCost)
  939. return false;
  940. // We don't want to fuse to a type that will be split, even
  941. // if the two input types will also be split and there is no other
  942. // associated cost.
  943. unsigned VParts1 = TTI->getNumberOfParts(VT1),
  944. VParts2 = TTI->getNumberOfParts(VT2);
  945. if (VParts1 > 1 || VParts2 > 1)
  946. return false;
  947. else if ((!VParts1 || !VParts2) && VCost == ICost + JCost)
  948. return false;
  949. CostSavings = ICost + JCost - VCost;
  950. }
  951. // The powi,ctlz,cttz intrinsics are special because only the first
  952. // argument is vectorized, the second arguments must be equal.
  953. CallInst *CI = dyn_cast<CallInst>(I);
  954. Function *FI;
  955. if (CI && (FI = CI->getCalledFunction())) {
  956. Intrinsic::ID IID = FI->getIntrinsicID();
  957. if (IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
  958. IID == Intrinsic::cttz) {
  959. Value *A1I = CI->getArgOperand(1),
  960. *A1J = cast<CallInst>(J)->getArgOperand(1);
  961. const SCEV *A1ISCEV = SE->getSCEV(A1I),
  962. *A1JSCEV = SE->getSCEV(A1J);
  963. return (A1ISCEV == A1JSCEV);
  964. }
  965. if (IID && TTI) {
  966. FastMathFlags FMFCI;
  967. if (auto *FPMOCI = dyn_cast<FPMathOperator>(CI))
  968. FMFCI = FPMOCI->getFastMathFlags();
  969. SmallVector<Type*, 4> Tys;
  970. for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i)
  971. Tys.push_back(CI->getArgOperand(i)->getType());
  972. unsigned ICost = TTI->getIntrinsicInstrCost(IID, IT1, Tys, FMFCI);
  973. Tys.clear();
  974. CallInst *CJ = cast<CallInst>(J);
  975. FastMathFlags FMFCJ;
  976. if (auto *FPMOCJ = dyn_cast<FPMathOperator>(CJ))
  977. FMFCJ = FPMOCJ->getFastMathFlags();
  978. for (unsigned i = 0, ie = CJ->getNumArgOperands(); i != ie; ++i)
  979. Tys.push_back(CJ->getArgOperand(i)->getType());
  980. unsigned JCost = TTI->getIntrinsicInstrCost(IID, JT1, Tys, FMFCJ);
  981. Tys.clear();
  982. assert(CI->getNumArgOperands() == CJ->getNumArgOperands() &&
  983. "Intrinsic argument counts differ");
  984. for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i) {
  985. if ((IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
  986. IID == Intrinsic::cttz) && i == 1)
  987. Tys.push_back(CI->getArgOperand(i)->getType());
  988. else
  989. Tys.push_back(getVecTypeForPair(CI->getArgOperand(i)->getType(),
  990. CJ->getArgOperand(i)->getType()));
  991. }
  992. FastMathFlags FMFV = FMFCI;
  993. FMFV &= FMFCJ;
  994. Type *RetTy = getVecTypeForPair(IT1, JT1);
  995. unsigned VCost = TTI->getIntrinsicInstrCost(IID, RetTy, Tys, FMFV);
  996. if (VCost > ICost + JCost)
  997. return false;
  998. // We don't want to fuse to a type that will be split, even
  999. // if the two input types will also be split and there is no other
  1000. // associated cost.
  1001. unsigned RetParts = TTI->getNumberOfParts(RetTy);
  1002. if (RetParts > 1)
  1003. return false;
  1004. else if (!RetParts && VCost == ICost + JCost)
  1005. return false;
  1006. for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i) {
  1007. if (!Tys[i]->isVectorTy())
  1008. continue;
  1009. unsigned NumParts = TTI->getNumberOfParts(Tys[i]);
  1010. if (NumParts > 1)
  1011. return false;
  1012. else if (!NumParts && VCost == ICost + JCost)
  1013. return false;
  1014. }
  1015. CostSavings = ICost + JCost - VCost;
  1016. }
  1017. }
  1018. return true;
  1019. }
  1020. // Figure out whether or not J uses I and update the users and write-set
  1021. // structures associated with I. Specifically, Users represents the set of
  1022. // instructions that depend on I. WriteSet represents the set
  1023. // of memory locations that are dependent on I. If UpdateUsers is true,
  1024. // and J uses I, then Users is updated to contain J and WriteSet is updated
  1025. // to contain any memory locations to which J writes. The function returns
  1026. // true if J uses I. By default, alias analysis is used to determine
  1027. // whether J reads from memory that overlaps with a location in WriteSet.
  1028. // If LoadMoveSet is not null, then it is a previously-computed map
  1029. // where the key is the memory-based user instruction and the value is
  1030. // the instruction to be compared with I. So, if LoadMoveSet is provided,
  1031. // then the alias analysis is not used. This is necessary because this
  1032. // function is called during the process of moving instructions during
  1033. // vectorization and the results of the alias analysis are not stable during
  1034. // that process.
  1035. bool BBVectorize::trackUsesOfI(DenseSet<Value *> &Users,
  1036. AliasSetTracker &WriteSet, Instruction *I,
  1037. Instruction *J, bool UpdateUsers,
  1038. DenseSet<ValuePair> *LoadMoveSetPairs) {
  1039. bool UsesI = false;
  1040. // This instruction may already be marked as a user due, for example, to
  1041. // being a member of a selected pair.
  1042. if (Users.count(J))
  1043. UsesI = true;
  1044. if (!UsesI)
  1045. for (User::op_iterator JU = J->op_begin(), JE = J->op_end();
  1046. JU != JE; ++JU) {
  1047. Value *V = *JU;
  1048. if (I == V || Users.count(V)) {
  1049. UsesI = true;
  1050. break;
  1051. }
  1052. }
  1053. if (!UsesI && J->mayReadFromMemory()) {
  1054. if (LoadMoveSetPairs) {
  1055. UsesI = LoadMoveSetPairs->count(ValuePair(J, I));
  1056. } else {
  1057. for (AliasSetTracker::iterator W = WriteSet.begin(),
  1058. WE = WriteSet.end(); W != WE; ++W) {
  1059. if (W->aliasesUnknownInst(J, *AA)) {
  1060. UsesI = true;
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. if (UsesI && UpdateUsers) {
  1067. if (J->mayWriteToMemory()) WriteSet.add(J);
  1068. Users.insert(J);
  1069. }
  1070. return UsesI;
  1071. }
  1072. // This function iterates over all instruction pairs in the provided
  1073. // basic block and collects all candidate pairs for vectorization.
  1074. bool BBVectorize::getCandidatePairs(BasicBlock &BB,
  1075. BasicBlock::iterator &Start,
  1076. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1077. DenseSet<ValuePair> &FixedOrderPairs,
  1078. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  1079. std::vector<Value *> &PairableInsts, bool NonPow2Len) {
  1080. size_t TotalPairs = 0;
  1081. BasicBlock::iterator E = BB.end();
  1082. if (Start == E) return false;
  1083. bool ShouldContinue = false, IAfterStart = false;
  1084. for (BasicBlock::iterator I = Start++; I != E; ++I) {
  1085. if (I == Start) IAfterStart = true;
  1086. bool IsSimpleLoadStore;
  1087. if (!isInstVectorizable(&*I, IsSimpleLoadStore))
  1088. continue;
  1089. // Look for an instruction with which to pair instruction *I...
  1090. DenseSet<Value *> Users;
  1091. AliasSetTracker WriteSet(*AA);
  1092. if (I->mayWriteToMemory())
  1093. WriteSet.add(&*I);
  1094. bool JAfterStart = IAfterStart;
  1095. BasicBlock::iterator J = std::next(I);
  1096. for (unsigned ss = 0; J != E && ss <= Config.SearchLimit; ++J, ++ss) {
  1097. if (J == Start)
  1098. JAfterStart = true;
  1099. // Determine if J uses I, if so, exit the loop.
  1100. bool UsesI = trackUsesOfI(Users, WriteSet, &*I, &*J, !Config.FastDep);
  1101. if (Config.FastDep) {
  1102. // Note: For this heuristic to be effective, independent operations
  1103. // must tend to be intermixed. This is likely to be true from some
  1104. // kinds of grouped loop unrolling (but not the generic LLVM pass),
  1105. // but otherwise may require some kind of reordering pass.
  1106. // When using fast dependency analysis,
  1107. // stop searching after first use:
  1108. if (UsesI) break;
  1109. } else {
  1110. if (UsesI) continue;
  1111. }
  1112. // J does not use I, and comes before the first use of I, so it can be
  1113. // merged with I if the instructions are compatible.
  1114. int CostSavings, FixedOrder;
  1115. if (!areInstsCompatible(&*I, &*J, IsSimpleLoadStore, NonPow2Len,
  1116. CostSavings, FixedOrder))
  1117. continue;
  1118. // J is a candidate for merging with I.
  1119. if (PairableInsts.empty() ||
  1120. PairableInsts[PairableInsts.size() - 1] != &*I) {
  1121. PairableInsts.push_back(&*I);
  1122. }
  1123. CandidatePairs[&*I].push_back(&*J);
  1124. ++TotalPairs;
  1125. if (TTI)
  1126. CandidatePairCostSavings.insert(
  1127. ValuePairWithCost(ValuePair(&*I, &*J), CostSavings));
  1128. if (FixedOrder == 1)
  1129. FixedOrderPairs.insert(ValuePair(&*I, &*J));
  1130. else if (FixedOrder == -1)
  1131. FixedOrderPairs.insert(ValuePair(&*J, &*I));
  1132. // The next call to this function must start after the last instruction
  1133. // selected during this invocation.
  1134. if (JAfterStart) {
  1135. Start = std::next(J);
  1136. IAfterStart = JAfterStart = false;
  1137. }
  1138. DEBUG(if (DebugCandidateSelection) dbgs() << "BBV: candidate pair "
  1139. << *I << " <-> " << *J << " (cost savings: " <<
  1140. CostSavings << ")\n");
  1141. // If we have already found too many pairs, break here and this function
  1142. // will be called again starting after the last instruction selected
  1143. // during this invocation.
  1144. if (PairableInsts.size() >= Config.MaxInsts ||
  1145. TotalPairs >= Config.MaxPairs) {
  1146. ShouldContinue = true;
  1147. break;
  1148. }
  1149. }
  1150. if (ShouldContinue)
  1151. break;
  1152. }
  1153. DEBUG(dbgs() << "BBV: found " << PairableInsts.size()
  1154. << " instructions with candidate pairs\n");
  1155. return ShouldContinue;
  1156. }
  1157. // Finds candidate pairs connected to the pair P = <PI, PJ>. This means that
  1158. // it looks for pairs such that both members have an input which is an
  1159. // output of PI or PJ.
  1160. void BBVectorize::computePairsConnectedTo(
  1161. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1162. DenseSet<ValuePair> &CandidatePairsSet,
  1163. std::vector<Value *> &PairableInsts,
  1164. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1165. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  1166. ValuePair P) {
  1167. StoreInst *SI, *SJ;
  1168. // For each possible pairing for this variable, look at the uses of
  1169. // the first value...
  1170. for (Value::user_iterator I = P.first->user_begin(),
  1171. E = P.first->user_end();
  1172. I != E; ++I) {
  1173. User *UI = *I;
  1174. if (isa<LoadInst>(UI)) {
  1175. // A pair cannot be connected to a load because the load only takes one
  1176. // operand (the address) and it is a scalar even after vectorization.
  1177. continue;
  1178. } else if ((SI = dyn_cast<StoreInst>(UI)) &&
  1179. P.first == SI->getPointerOperand()) {
  1180. // Similarly, a pair cannot be connected to a store through its
  1181. // pointer operand.
  1182. continue;
  1183. }
  1184. // For each use of the first variable, look for uses of the second
  1185. // variable...
  1186. for (User *UJ : P.second->users()) {
  1187. if ((SJ = dyn_cast<StoreInst>(UJ)) &&
  1188. P.second == SJ->getPointerOperand())
  1189. continue;
  1190. // Look for <I, J>:
  1191. if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
  1192. VPPair VP(P, ValuePair(UI, UJ));
  1193. ConnectedPairs[VP.first].push_back(VP.second);
  1194. PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionDirect));
  1195. }
  1196. // Look for <J, I>:
  1197. if (CandidatePairsSet.count(ValuePair(UJ, UI))) {
  1198. VPPair VP(P, ValuePair(UJ, UI));
  1199. ConnectedPairs[VP.first].push_back(VP.second);
  1200. PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSwap));
  1201. }
  1202. }
  1203. if (Config.SplatBreaksChain) continue;
  1204. // Look for cases where just the first value in the pair is used by
  1205. // both members of another pair (splatting).
  1206. for (Value::user_iterator J = P.first->user_begin(); J != E; ++J) {
  1207. User *UJ = *J;
  1208. if ((SJ = dyn_cast<StoreInst>(UJ)) &&
  1209. P.first == SJ->getPointerOperand())
  1210. continue;
  1211. if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
  1212. VPPair VP(P, ValuePair(UI, UJ));
  1213. ConnectedPairs[VP.first].push_back(VP.second);
  1214. PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSplat));
  1215. }
  1216. }
  1217. }
  1218. if (Config.SplatBreaksChain) return;
  1219. // Look for cases where just the second value in the pair is used by
  1220. // both members of another pair (splatting).
  1221. for (Value::user_iterator I = P.second->user_begin(),
  1222. E = P.second->user_end();
  1223. I != E; ++I) {
  1224. User *UI = *I;
  1225. if (isa<LoadInst>(UI))
  1226. continue;
  1227. else if ((SI = dyn_cast<StoreInst>(UI)) &&
  1228. P.second == SI->getPointerOperand())
  1229. continue;
  1230. for (Value::user_iterator J = P.second->user_begin(); J != E; ++J) {
  1231. User *UJ = *J;
  1232. if ((SJ = dyn_cast<StoreInst>(UJ)) &&
  1233. P.second == SJ->getPointerOperand())
  1234. continue;
  1235. if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
  1236. VPPair VP(P, ValuePair(UI, UJ));
  1237. ConnectedPairs[VP.first].push_back(VP.second);
  1238. PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSplat));
  1239. }
  1240. }
  1241. }
  1242. }
  1243. // This function figures out which pairs are connected. Two pairs are
  1244. // connected if some output of the first pair forms an input to both members
  1245. // of the second pair.
  1246. void BBVectorize::computeConnectedPairs(
  1247. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1248. DenseSet<ValuePair> &CandidatePairsSet,
  1249. std::vector<Value *> &PairableInsts,
  1250. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1251. DenseMap<VPPair, unsigned> &PairConnectionTypes) {
  1252. for (std::vector<Value *>::iterator PI = PairableInsts.begin(),
  1253. PE = PairableInsts.end(); PI != PE; ++PI) {
  1254. DenseMap<Value *, std::vector<Value *> >::iterator PP =
  1255. CandidatePairs.find(*PI);
  1256. if (PP == CandidatePairs.end())
  1257. continue;
  1258. for (std::vector<Value *>::iterator P = PP->second.begin(),
  1259. E = PP->second.end(); P != E; ++P)
  1260. computePairsConnectedTo(CandidatePairs, CandidatePairsSet,
  1261. PairableInsts, ConnectedPairs,
  1262. PairConnectionTypes, ValuePair(*PI, *P));
  1263. }
  1264. DEBUG(size_t TotalPairs = 0;
  1265. for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator I =
  1266. ConnectedPairs.begin(), IE = ConnectedPairs.end(); I != IE; ++I)
  1267. TotalPairs += I->second.size();
  1268. dbgs() << "BBV: found " << TotalPairs
  1269. << " pair connections.\n");
  1270. }
  1271. // This function builds a set of use tuples such that <A, B> is in the set
  1272. // if B is in the use dag of A. If B is in the use dag of A, then B
  1273. // depends on the output of A.
  1274. void BBVectorize::buildDepMap(
  1275. BasicBlock &BB,
  1276. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1277. std::vector<Value *> &PairableInsts,
  1278. DenseSet<ValuePair> &PairableInstUsers) {
  1279. DenseSet<Value *> IsInPair;
  1280. for (DenseMap<Value *, std::vector<Value *> >::iterator C =
  1281. CandidatePairs.begin(), E = CandidatePairs.end(); C != E; ++C) {
  1282. IsInPair.insert(C->first);
  1283. IsInPair.insert(C->second.begin(), C->second.end());
  1284. }
  1285. // Iterate through the basic block, recording all users of each
  1286. // pairable instruction.
  1287. BasicBlock::iterator E = BB.end(), EL =
  1288. BasicBlock::iterator(cast<Instruction>(PairableInsts.back()));
  1289. for (BasicBlock::iterator I = BB.getFirstInsertionPt(); I != E; ++I) {
  1290. if (IsInPair.find(&*I) == IsInPair.end())
  1291. continue;
  1292. DenseSet<Value *> Users;
  1293. AliasSetTracker WriteSet(*AA);
  1294. if (I->mayWriteToMemory())
  1295. WriteSet.add(&*I);
  1296. for (BasicBlock::iterator J = std::next(I); J != E; ++J) {
  1297. (void)trackUsesOfI(Users, WriteSet, &*I, &*J);
  1298. if (J == EL)
  1299. break;
  1300. }
  1301. for (DenseSet<Value *>::iterator U = Users.begin(), E = Users.end();
  1302. U != E; ++U) {
  1303. if (IsInPair.find(*U) == IsInPair.end()) continue;
  1304. PairableInstUsers.insert(ValuePair(&*I, *U));
  1305. }
  1306. if (I == EL)
  1307. break;
  1308. }
  1309. }
  1310. // Returns true if an input to pair P is an output of pair Q and also an
  1311. // input of pair Q is an output of pair P. If this is the case, then these
  1312. // two pairs cannot be simultaneously fused.
  1313. bool BBVectorize::pairsConflict(ValuePair P, ValuePair Q,
  1314. DenseSet<ValuePair> &PairableInstUsers,
  1315. DenseMap<ValuePair, std::vector<ValuePair> > *PairableInstUserMap,
  1316. DenseSet<VPPair> *PairableInstUserPairSet) {
  1317. // Two pairs are in conflict if they are mutual Users of eachother.
  1318. bool QUsesP = PairableInstUsers.count(ValuePair(P.first, Q.first)) ||
  1319. PairableInstUsers.count(ValuePair(P.first, Q.second)) ||
  1320. PairableInstUsers.count(ValuePair(P.second, Q.first)) ||
  1321. PairableInstUsers.count(ValuePair(P.second, Q.second));
  1322. bool PUsesQ = PairableInstUsers.count(ValuePair(Q.first, P.first)) ||
  1323. PairableInstUsers.count(ValuePair(Q.first, P.second)) ||
  1324. PairableInstUsers.count(ValuePair(Q.second, P.first)) ||
  1325. PairableInstUsers.count(ValuePair(Q.second, P.second));
  1326. if (PairableInstUserMap) {
  1327. // FIXME: The expensive part of the cycle check is not so much the cycle
  1328. // check itself but this edge insertion procedure. This needs some
  1329. // profiling and probably a different data structure.
  1330. if (PUsesQ) {
  1331. if (PairableInstUserPairSet->insert(VPPair(Q, P)).second)
  1332. (*PairableInstUserMap)[Q].push_back(P);
  1333. }
  1334. if (QUsesP) {
  1335. if (PairableInstUserPairSet->insert(VPPair(P, Q)).second)
  1336. (*PairableInstUserMap)[P].push_back(Q);
  1337. }
  1338. }
  1339. return (QUsesP && PUsesQ);
  1340. }
  1341. // This function walks the use graph of current pairs to see if, starting
  1342. // from P, the walk returns to P.
  1343. bool BBVectorize::pairWillFormCycle(ValuePair P,
  1344. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
  1345. DenseSet<ValuePair> &CurrentPairs) {
  1346. DEBUG(if (DebugCycleCheck)
  1347. dbgs() << "BBV: starting cycle check for : " << *P.first << " <-> "
  1348. << *P.second << "\n");
  1349. // A lookup table of visisted pairs is kept because the PairableInstUserMap
  1350. // contains non-direct associations.
  1351. DenseSet<ValuePair> Visited;
  1352. SmallVector<ValuePair, 32> Q;
  1353. // General depth-first post-order traversal:
  1354. Q.push_back(P);
  1355. do {
  1356. ValuePair QTop = Q.pop_back_val();
  1357. Visited.insert(QTop);
  1358. DEBUG(if (DebugCycleCheck)
  1359. dbgs() << "BBV: cycle check visiting: " << *QTop.first << " <-> "
  1360. << *QTop.second << "\n");
  1361. DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
  1362. PairableInstUserMap.find(QTop);
  1363. if (QQ == PairableInstUserMap.end())
  1364. continue;
  1365. for (std::vector<ValuePair>::iterator C = QQ->second.begin(),
  1366. CE = QQ->second.end(); C != CE; ++C) {
  1367. if (*C == P) {
  1368. DEBUG(dbgs()
  1369. << "BBV: rejected to prevent non-trivial cycle formation: "
  1370. << QTop.first << " <-> " << C->second << "\n");
  1371. return true;
  1372. }
  1373. if (CurrentPairs.count(*C) && !Visited.count(*C))
  1374. Q.push_back(*C);
  1375. }
  1376. } while (!Q.empty());
  1377. return false;
  1378. }
  1379. // This function builds the initial dag of connected pairs with the
  1380. // pair J at the root.
  1381. void BBVectorize::buildInitialDAGFor(
  1382. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1383. DenseSet<ValuePair> &CandidatePairsSet,
  1384. std::vector<Value *> &PairableInsts,
  1385. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1386. DenseSet<ValuePair> &PairableInstUsers,
  1387. DenseMap<Value *, Value *> &ChosenPairs,
  1388. DenseMap<ValuePair, size_t> &DAG, ValuePair J) {
  1389. // Each of these pairs is viewed as the root node of a DAG. The DAG
  1390. // is then walked (depth-first). As this happens, we keep track of
  1391. // the pairs that compose the DAG and the maximum depth of the DAG.
  1392. SmallVector<ValuePairWithDepth, 32> Q;
  1393. // General depth-first post-order traversal:
  1394. Q.push_back(ValuePairWithDepth(J, getDepthFactor(J.first)));
  1395. do {
  1396. ValuePairWithDepth QTop = Q.back();
  1397. // Push each child onto the queue:
  1398. bool MoreChildren = false;
  1399. size_t MaxChildDepth = QTop.second;
  1400. DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
  1401. ConnectedPairs.find(QTop.first);
  1402. if (QQ != ConnectedPairs.end())
  1403. for (std::vector<ValuePair>::iterator k = QQ->second.begin(),
  1404. ke = QQ->second.end(); k != ke; ++k) {
  1405. // Make sure that this child pair is still a candidate:
  1406. if (CandidatePairsSet.count(*k)) {
  1407. DenseMap<ValuePair, size_t>::iterator C = DAG.find(*k);
  1408. if (C == DAG.end()) {
  1409. size_t d = getDepthFactor(k->first);
  1410. Q.push_back(ValuePairWithDepth(*k, QTop.second+d));
  1411. MoreChildren = true;
  1412. } else {
  1413. MaxChildDepth = std::max(MaxChildDepth, C->second);
  1414. }
  1415. }
  1416. }
  1417. if (!MoreChildren) {
  1418. // Record the current pair as part of the DAG:
  1419. DAG.insert(ValuePairWithDepth(QTop.first, MaxChildDepth));
  1420. Q.pop_back();
  1421. }
  1422. } while (!Q.empty());
  1423. }
  1424. // Given some initial dag, prune it by removing conflicting pairs (pairs
  1425. // that cannot be simultaneously chosen for vectorization).
  1426. void BBVectorize::pruneDAGFor(
  1427. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1428. std::vector<Value *> &PairableInsts,
  1429. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1430. DenseSet<ValuePair> &PairableInstUsers,
  1431. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
  1432. DenseSet<VPPair> &PairableInstUserPairSet,
  1433. DenseMap<Value *, Value *> &ChosenPairs,
  1434. DenseMap<ValuePair, size_t> &DAG,
  1435. DenseSet<ValuePair> &PrunedDAG, ValuePair J,
  1436. bool UseCycleCheck) {
  1437. SmallVector<ValuePairWithDepth, 32> Q;
  1438. // General depth-first post-order traversal:
  1439. Q.push_back(ValuePairWithDepth(J, getDepthFactor(J.first)));
  1440. do {
  1441. ValuePairWithDepth QTop = Q.pop_back_val();
  1442. PrunedDAG.insert(QTop.first);
  1443. // Visit each child, pruning as necessary...
  1444. SmallVector<ValuePairWithDepth, 8> BestChildren;
  1445. DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
  1446. ConnectedPairs.find(QTop.first);
  1447. if (QQ == ConnectedPairs.end())
  1448. continue;
  1449. for (std::vector<ValuePair>::iterator K = QQ->second.begin(),
  1450. KE = QQ->second.end(); K != KE; ++K) {
  1451. DenseMap<ValuePair, size_t>::iterator C = DAG.find(*K);
  1452. if (C == DAG.end()) continue;
  1453. // This child is in the DAG, now we need to make sure it is the
  1454. // best of any conflicting children. There could be multiple
  1455. // conflicting children, so first, determine if we're keeping
  1456. // this child, then delete conflicting children as necessary.
  1457. // It is also necessary to guard against pairing-induced
  1458. // dependencies. Consider instructions a .. x .. y .. b
  1459. // such that (a,b) are to be fused and (x,y) are to be fused
  1460. // but a is an input to x and b is an output from y. This
  1461. // means that y cannot be moved after b but x must be moved
  1462. // after b for (a,b) to be fused. In other words, after
  1463. // fusing (a,b) we have y .. a/b .. x where y is an input
  1464. // to a/b and x is an output to a/b: x and y can no longer
  1465. // be legally fused. To prevent this condition, we must
  1466. // make sure that a child pair added to the DAG is not
  1467. // both an input and output of an already-selected pair.
  1468. // Pairing-induced dependencies can also form from more complicated
  1469. // cycles. The pair vs. pair conflicts are easy to check, and so
  1470. // that is done explicitly for "fast rejection", and because for
  1471. // child vs. child conflicts, we may prefer to keep the current
  1472. // pair in preference to the already-selected child.
  1473. DenseSet<ValuePair> CurrentPairs;
  1474. bool CanAdd = true;
  1475. for (SmallVectorImpl<ValuePairWithDepth>::iterator C2
  1476. = BestChildren.begin(), E2 = BestChildren.end();
  1477. C2 != E2; ++C2) {
  1478. if (C2->first.first == C->first.first ||
  1479. C2->first.first == C->first.second ||
  1480. C2->first.second == C->first.first ||
  1481. C2->first.second == C->first.second ||
  1482. pairsConflict(C2->first, C->first, PairableInstUsers,
  1483. UseCycleCheck ? &PairableInstUserMap : nullptr,
  1484. UseCycleCheck ? &PairableInstUserPairSet
  1485. : nullptr)) {
  1486. if (C2->second >= C->second) {
  1487. CanAdd = false;
  1488. break;
  1489. }
  1490. CurrentPairs.insert(C2->first);
  1491. }
  1492. }
  1493. if (!CanAdd) continue;
  1494. // Even worse, this child could conflict with another node already
  1495. // selected for the DAG. If that is the case, ignore this child.
  1496. for (DenseSet<ValuePair>::iterator T = PrunedDAG.begin(),
  1497. E2 = PrunedDAG.end(); T != E2; ++T) {
  1498. if (T->first == C->first.first ||
  1499. T->first == C->first.second ||
  1500. T->second == C->first.first ||
  1501. T->second == C->first.second ||
  1502. pairsConflict(*T, C->first, PairableInstUsers,
  1503. UseCycleCheck ? &PairableInstUserMap : nullptr,
  1504. UseCycleCheck ? &PairableInstUserPairSet
  1505. : nullptr)) {
  1506. CanAdd = false;
  1507. break;
  1508. }
  1509. CurrentPairs.insert(*T);
  1510. }
  1511. if (!CanAdd) continue;
  1512. // And check the queue too...
  1513. for (SmallVectorImpl<ValuePairWithDepth>::iterator C2 = Q.begin(),
  1514. E2 = Q.end(); C2 != E2; ++C2) {
  1515. if (C2->first.first == C->first.first ||
  1516. C2->first.first == C->first.second ||
  1517. C2->first.second == C->first.first ||
  1518. C2->first.second == C->first.second ||
  1519. pairsConflict(C2->first, C->first, PairableInstUsers,
  1520. UseCycleCheck ? &PairableInstUserMap : nullptr,
  1521. UseCycleCheck ? &PairableInstUserPairSet
  1522. : nullptr)) {
  1523. CanAdd = false;
  1524. break;
  1525. }
  1526. CurrentPairs.insert(C2->first);
  1527. }
  1528. if (!CanAdd) continue;
  1529. // Last but not least, check for a conflict with any of the
  1530. // already-chosen pairs.
  1531. for (DenseMap<Value *, Value *>::iterator C2 =
  1532. ChosenPairs.begin(), E2 = ChosenPairs.end();
  1533. C2 != E2; ++C2) {
  1534. if (pairsConflict(*C2, C->first, PairableInstUsers,
  1535. UseCycleCheck ? &PairableInstUserMap : nullptr,
  1536. UseCycleCheck ? &PairableInstUserPairSet
  1537. : nullptr)) {
  1538. CanAdd = false;
  1539. break;
  1540. }
  1541. CurrentPairs.insert(*C2);
  1542. }
  1543. if (!CanAdd) continue;
  1544. // To check for non-trivial cycles formed by the addition of the
  1545. // current pair we've formed a list of all relevant pairs, now use a
  1546. // graph walk to check for a cycle. We start from the current pair and
  1547. // walk the use dag to see if we again reach the current pair. If we
  1548. // do, then the current pair is rejected.
  1549. // FIXME: It may be more efficient to use a topological-ordering
  1550. // algorithm to improve the cycle check. This should be investigated.
  1551. if (UseCycleCheck &&
  1552. pairWillFormCycle(C->first, PairableInstUserMap, CurrentPairs))
  1553. continue;
  1554. // This child can be added, but we may have chosen it in preference
  1555. // to an already-selected child. Check for this here, and if a
  1556. // conflict is found, then remove the previously-selected child
  1557. // before adding this one in its place.
  1558. for (SmallVectorImpl<ValuePairWithDepth>::iterator C2
  1559. = BestChildren.begin(); C2 != BestChildren.end();) {
  1560. if (C2->first.first == C->first.first ||
  1561. C2->first.first == C->first.second ||
  1562. C2->first.second == C->first.first ||
  1563. C2->first.second == C->first.second ||
  1564. pairsConflict(C2->first, C->first, PairableInstUsers))
  1565. C2 = BestChildren.erase(C2);
  1566. else
  1567. ++C2;
  1568. }
  1569. BestChildren.push_back(ValuePairWithDepth(C->first, C->second));
  1570. }
  1571. for (SmallVectorImpl<ValuePairWithDepth>::iterator C
  1572. = BestChildren.begin(), E2 = BestChildren.end();
  1573. C != E2; ++C) {
  1574. size_t DepthF = getDepthFactor(C->first.first);
  1575. Q.push_back(ValuePairWithDepth(C->first, QTop.second+DepthF));
  1576. }
  1577. } while (!Q.empty());
  1578. }
  1579. // This function finds the best dag of mututally-compatible connected
  1580. // pairs, given the choice of root pairs as an iterator range.
  1581. void BBVectorize::findBestDAGFor(
  1582. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1583. DenseSet<ValuePair> &CandidatePairsSet,
  1584. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  1585. std::vector<Value *> &PairableInsts,
  1586. DenseSet<ValuePair> &FixedOrderPairs,
  1587. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  1588. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1589. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
  1590. DenseSet<ValuePair> &PairableInstUsers,
  1591. DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
  1592. DenseSet<VPPair> &PairableInstUserPairSet,
  1593. DenseMap<Value *, Value *> &ChosenPairs,
  1594. DenseSet<ValuePair> &BestDAG, size_t &BestMaxDepth,
  1595. int &BestEffSize, Value *II, std::vector<Value *>&JJ,
  1596. bool UseCycleCheck) {
  1597. for (std::vector<Value *>::iterator J = JJ.begin(), JE = JJ.end();
  1598. J != JE; ++J) {
  1599. ValuePair IJ(II, *J);
  1600. if (!CandidatePairsSet.count(IJ))
  1601. continue;
  1602. // Before going any further, make sure that this pair does not
  1603. // conflict with any already-selected pairs (see comment below
  1604. // near the DAG pruning for more details).
  1605. DenseSet<ValuePair> ChosenPairSet;
  1606. bool DoesConflict = false;
  1607. for (DenseMap<Value *, Value *>::iterator C = ChosenPairs.begin(),
  1608. E = ChosenPairs.end(); C != E; ++C) {
  1609. if (pairsConflict(*C, IJ, PairableInstUsers,
  1610. UseCycleCheck ? &PairableInstUserMap : nullptr,
  1611. UseCycleCheck ? &PairableInstUserPairSet : nullptr)) {
  1612. DoesConflict = true;
  1613. break;
  1614. }
  1615. ChosenPairSet.insert(*C);
  1616. }
  1617. if (DoesConflict) continue;
  1618. if (UseCycleCheck &&
  1619. pairWillFormCycle(IJ, PairableInstUserMap, ChosenPairSet))
  1620. continue;
  1621. DenseMap<ValuePair, size_t> DAG;
  1622. buildInitialDAGFor(CandidatePairs, CandidatePairsSet,
  1623. PairableInsts, ConnectedPairs,
  1624. PairableInstUsers, ChosenPairs, DAG, IJ);
  1625. // Because we'll keep the child with the largest depth, the largest
  1626. // depth is still the same in the unpruned DAG.
  1627. size_t MaxDepth = DAG.lookup(IJ);
  1628. DEBUG(if (DebugPairSelection) dbgs() << "BBV: found DAG for pair {"
  1629. << *IJ.first << " <-> " << *IJ.second << "} of depth " <<
  1630. MaxDepth << " and size " << DAG.size() << "\n");
  1631. // At this point the DAG has been constructed, but, may contain
  1632. // contradictory children (meaning that different children of
  1633. // some dag node may be attempting to fuse the same instruction).
  1634. // So now we walk the dag again, in the case of a conflict,
  1635. // keep only the child with the largest depth. To break a tie,
  1636. // favor the first child.
  1637. DenseSet<ValuePair> PrunedDAG;
  1638. pruneDAGFor(CandidatePairs, PairableInsts, ConnectedPairs,
  1639. PairableInstUsers, PairableInstUserMap,
  1640. PairableInstUserPairSet,
  1641. ChosenPairs, DAG, PrunedDAG, IJ, UseCycleCheck);
  1642. int EffSize = 0;
  1643. if (TTI) {
  1644. DenseSet<Value *> PrunedDAGInstrs;
  1645. for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
  1646. E = PrunedDAG.end(); S != E; ++S) {
  1647. PrunedDAGInstrs.insert(S->first);
  1648. PrunedDAGInstrs.insert(S->second);
  1649. }
  1650. // The set of pairs that have already contributed to the total cost.
  1651. DenseSet<ValuePair> IncomingPairs;
  1652. // If the cost model were perfect, this might not be necessary; but we
  1653. // need to make sure that we don't get stuck vectorizing our own
  1654. // shuffle chains.
  1655. bool HasNontrivialInsts = false;
  1656. // The node weights represent the cost savings associated with
  1657. // fusing the pair of instructions.
  1658. for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
  1659. E = PrunedDAG.end(); S != E; ++S) {
  1660. if (!isa<ShuffleVectorInst>(S->first) &&
  1661. !isa<InsertElementInst>(S->first) &&
  1662. !isa<ExtractElementInst>(S->first))
  1663. HasNontrivialInsts = true;
  1664. bool FlipOrder = false;
  1665. if (getDepthFactor(S->first)) {
  1666. int ESContrib = CandidatePairCostSavings.find(*S)->second;
  1667. DEBUG(if (DebugPairSelection) dbgs() << "\tweight {"
  1668. << *S->first << " <-> " << *S->second << "} = " <<
  1669. ESContrib << "\n");
  1670. EffSize += ESContrib;
  1671. }
  1672. // The edge weights contribute in a negative sense: they represent
  1673. // the cost of shuffles.
  1674. DenseMap<ValuePair, std::vector<ValuePair> >::iterator SS =
  1675. ConnectedPairDeps.find(*S);
  1676. if (SS != ConnectedPairDeps.end()) {
  1677. unsigned NumDepsDirect = 0, NumDepsSwap = 0;
  1678. for (std::vector<ValuePair>::iterator T = SS->second.begin(),
  1679. TE = SS->second.end(); T != TE; ++T) {
  1680. VPPair Q(*S, *T);
  1681. if (!PrunedDAG.count(Q.second))
  1682. continue;
  1683. DenseMap<VPPair, unsigned>::iterator R =
  1684. PairConnectionTypes.find(VPPair(Q.second, Q.first));
  1685. assert(R != PairConnectionTypes.end() &&
  1686. "Cannot find pair connection type");
  1687. if (R->second == PairConnectionDirect)
  1688. ++NumDepsDirect;
  1689. else if (R->second == PairConnectionSwap)
  1690. ++NumDepsSwap;
  1691. }
  1692. // If there are more swaps than direct connections, then
  1693. // the pair order will be flipped during fusion. So the real
  1694. // number of swaps is the minimum number.
  1695. FlipOrder = !FixedOrderPairs.count(*S) &&
  1696. ((NumDepsSwap > NumDepsDirect) ||
  1697. FixedOrderPairs.count(ValuePair(S->second, S->first)));
  1698. for (std::vector<ValuePair>::iterator T = SS->second.begin(),
  1699. TE = SS->second.end(); T != TE; ++T) {
  1700. VPPair Q(*S, *T);
  1701. if (!PrunedDAG.count(Q.second))
  1702. continue;
  1703. DenseMap<VPPair, unsigned>::iterator R =
  1704. PairConnectionTypes.find(VPPair(Q.second, Q.first));
  1705. assert(R != PairConnectionTypes.end() &&
  1706. "Cannot find pair connection type");
  1707. Type *Ty1 = Q.second.first->getType(),
  1708. *Ty2 = Q.second.second->getType();
  1709. Type *VTy = getVecTypeForPair(Ty1, Ty2);
  1710. if ((R->second == PairConnectionDirect && FlipOrder) ||
  1711. (R->second == PairConnectionSwap && !FlipOrder) ||
  1712. R->second == PairConnectionSplat) {
  1713. int ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
  1714. VTy, VTy);
  1715. if (VTy->getVectorNumElements() == 2) {
  1716. if (R->second == PairConnectionSplat)
  1717. ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
  1718. TargetTransformInfo::SK_Broadcast, VTy));
  1719. else
  1720. ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
  1721. TargetTransformInfo::SK_Reverse, VTy));
  1722. }
  1723. DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
  1724. *Q.second.first << " <-> " << *Q.second.second <<
  1725. "} -> {" <<
  1726. *S->first << " <-> " << *S->second << "} = " <<
  1727. ESContrib << "\n");
  1728. EffSize -= ESContrib;
  1729. }
  1730. }
  1731. }
  1732. // Compute the cost of outgoing edges. We assume that edges outgoing
  1733. // to shuffles, inserts or extracts can be merged, and so contribute
  1734. // no additional cost.
  1735. if (!S->first->getType()->isVoidTy()) {
  1736. Type *Ty1 = S->first->getType(),
  1737. *Ty2 = S->second->getType();
  1738. Type *VTy = getVecTypeForPair(Ty1, Ty2);
  1739. bool NeedsExtraction = false;
  1740. for (User *U : S->first->users()) {
  1741. if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(U)) {
  1742. // Shuffle can be folded if it has no other input
  1743. if (isa<UndefValue>(SI->getOperand(1)))
  1744. continue;
  1745. }
  1746. if (isa<ExtractElementInst>(U))
  1747. continue;
  1748. if (PrunedDAGInstrs.count(U))
  1749. continue;
  1750. NeedsExtraction = true;
  1751. break;
  1752. }
  1753. if (NeedsExtraction) {
  1754. int ESContrib;
  1755. if (Ty1->isVectorTy()) {
  1756. ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
  1757. Ty1, VTy);
  1758. ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
  1759. TargetTransformInfo::SK_ExtractSubvector, VTy, 0, Ty1));
  1760. } else
  1761. ESContrib = (int) TTI->getVectorInstrCost(
  1762. Instruction::ExtractElement, VTy, 0);
  1763. DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
  1764. *S->first << "} = " << ESContrib << "\n");
  1765. EffSize -= ESContrib;
  1766. }
  1767. NeedsExtraction = false;
  1768. for (User *U : S->second->users()) {
  1769. if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(U)) {
  1770. // Shuffle can be folded if it has no other input
  1771. if (isa<UndefValue>(SI->getOperand(1)))
  1772. continue;
  1773. }
  1774. if (isa<ExtractElementInst>(U))
  1775. continue;
  1776. if (PrunedDAGInstrs.count(U))
  1777. continue;
  1778. NeedsExtraction = true;
  1779. break;
  1780. }
  1781. if (NeedsExtraction) {
  1782. int ESContrib;
  1783. if (Ty2->isVectorTy()) {
  1784. ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
  1785. Ty2, VTy);
  1786. ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
  1787. TargetTransformInfo::SK_ExtractSubvector, VTy,
  1788. Ty1->isVectorTy() ? Ty1->getVectorNumElements() : 1, Ty2));
  1789. } else
  1790. ESContrib = (int) TTI->getVectorInstrCost(
  1791. Instruction::ExtractElement, VTy, 1);
  1792. DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
  1793. *S->second << "} = " << ESContrib << "\n");
  1794. EffSize -= ESContrib;
  1795. }
  1796. }
  1797. // Compute the cost of incoming edges.
  1798. if (!isa<LoadInst>(S->first) && !isa<StoreInst>(S->first)) {
  1799. Instruction *S1 = cast<Instruction>(S->first),
  1800. *S2 = cast<Instruction>(S->second);
  1801. for (unsigned o = 0; o < S1->getNumOperands(); ++o) {
  1802. Value *O1 = S1->getOperand(o), *O2 = S2->getOperand(o);
  1803. // Combining constants into vector constants (or small vector
  1804. // constants into larger ones are assumed free).
  1805. if (isa<Constant>(O1) && isa<Constant>(O2))
  1806. continue;
  1807. if (FlipOrder)
  1808. std::swap(O1, O2);
  1809. ValuePair VP = ValuePair(O1, O2);
  1810. ValuePair VPR = ValuePair(O2, O1);
  1811. // Internal edges are not handled here.
  1812. if (PrunedDAG.count(VP) || PrunedDAG.count(VPR))
  1813. continue;
  1814. Type *Ty1 = O1->getType(),
  1815. *Ty2 = O2->getType();
  1816. Type *VTy = getVecTypeForPair(Ty1, Ty2);
  1817. // Combining vector operations of the same type is also assumed
  1818. // folded with other operations.
  1819. if (Ty1 == Ty2) {
  1820. // If both are insert elements, then both can be widened.
  1821. InsertElementInst *IEO1 = dyn_cast<InsertElementInst>(O1),
  1822. *IEO2 = dyn_cast<InsertElementInst>(O2);
  1823. if (IEO1 && IEO2 && isPureIEChain(IEO1) && isPureIEChain(IEO2))
  1824. continue;
  1825. // If both are extract elements, and both have the same input
  1826. // type, then they can be replaced with a shuffle
  1827. ExtractElementInst *EIO1 = dyn_cast<ExtractElementInst>(O1),
  1828. *EIO2 = dyn_cast<ExtractElementInst>(O2);
  1829. if (EIO1 && EIO2 &&
  1830. EIO1->getOperand(0)->getType() ==
  1831. EIO2->getOperand(0)->getType())
  1832. continue;
  1833. // If both are a shuffle with equal operand types and only two
  1834. // unqiue operands, then they can be replaced with a single
  1835. // shuffle
  1836. ShuffleVectorInst *SIO1 = dyn_cast<ShuffleVectorInst>(O1),
  1837. *SIO2 = dyn_cast<ShuffleVectorInst>(O2);
  1838. if (SIO1 && SIO2 &&
  1839. SIO1->getOperand(0)->getType() ==
  1840. SIO2->getOperand(0)->getType()) {
  1841. SmallSet<Value *, 4> SIOps;
  1842. SIOps.insert(SIO1->getOperand(0));
  1843. SIOps.insert(SIO1->getOperand(1));
  1844. SIOps.insert(SIO2->getOperand(0));
  1845. SIOps.insert(SIO2->getOperand(1));
  1846. if (SIOps.size() <= 2)
  1847. continue;
  1848. }
  1849. }
  1850. int ESContrib;
  1851. // This pair has already been formed.
  1852. if (IncomingPairs.count(VP)) {
  1853. continue;
  1854. } else if (IncomingPairs.count(VPR)) {
  1855. ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
  1856. VTy, VTy);
  1857. if (VTy->getVectorNumElements() == 2)
  1858. ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
  1859. TargetTransformInfo::SK_Reverse, VTy));
  1860. } else if (!Ty1->isVectorTy() && !Ty2->isVectorTy()) {
  1861. ESContrib = (int) TTI->getVectorInstrCost(
  1862. Instruction::InsertElement, VTy, 0);
  1863. ESContrib += (int) TTI->getVectorInstrCost(
  1864. Instruction::InsertElement, VTy, 1);
  1865. } else if (!Ty1->isVectorTy()) {
  1866. // O1 needs to be inserted into a vector of size O2, and then
  1867. // both need to be shuffled together.
  1868. ESContrib = (int) TTI->getVectorInstrCost(
  1869. Instruction::InsertElement, Ty2, 0);
  1870. ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
  1871. VTy, Ty2);
  1872. } else if (!Ty2->isVectorTy()) {
  1873. // O2 needs to be inserted into a vector of size O1, and then
  1874. // both need to be shuffled together.
  1875. ESContrib = (int) TTI->getVectorInstrCost(
  1876. Instruction::InsertElement, Ty1, 0);
  1877. ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
  1878. VTy, Ty1);
  1879. } else {
  1880. Type *TyBig = Ty1, *TySmall = Ty2;
  1881. if (Ty2->getVectorNumElements() > Ty1->getVectorNumElements())
  1882. std::swap(TyBig, TySmall);
  1883. ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
  1884. VTy, TyBig);
  1885. if (TyBig != TySmall)
  1886. ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
  1887. TyBig, TySmall);
  1888. }
  1889. DEBUG(if (DebugPairSelection) dbgs() << "\tcost {"
  1890. << *O1 << " <-> " << *O2 << "} = " <<
  1891. ESContrib << "\n");
  1892. EffSize -= ESContrib;
  1893. IncomingPairs.insert(VP);
  1894. }
  1895. }
  1896. }
  1897. if (!HasNontrivialInsts) {
  1898. DEBUG(if (DebugPairSelection) dbgs() <<
  1899. "\tNo non-trivial instructions in DAG;"
  1900. " override to zero effective size\n");
  1901. EffSize = 0;
  1902. }
  1903. } else {
  1904. for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
  1905. E = PrunedDAG.end(); S != E; ++S)
  1906. EffSize += (int) getDepthFactor(S->first);
  1907. }
  1908. DEBUG(if (DebugPairSelection)
  1909. dbgs() << "BBV: found pruned DAG for pair {"
  1910. << *IJ.first << " <-> " << *IJ.second << "} of depth " <<
  1911. MaxDepth << " and size " << PrunedDAG.size() <<
  1912. " (effective size: " << EffSize << ")\n");
  1913. if (((TTI && !UseChainDepthWithTI) ||
  1914. MaxDepth >= Config.ReqChainDepth) &&
  1915. EffSize > 0 && EffSize > BestEffSize) {
  1916. BestMaxDepth = MaxDepth;
  1917. BestEffSize = EffSize;
  1918. BestDAG = PrunedDAG;
  1919. }
  1920. }
  1921. }
  1922. // Given the list of candidate pairs, this function selects those
  1923. // that will be fused into vector instructions.
  1924. void BBVectorize::choosePairs(
  1925. DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
  1926. DenseSet<ValuePair> &CandidatePairsSet,
  1927. DenseMap<ValuePair, int> &CandidatePairCostSavings,
  1928. std::vector<Value *> &PairableInsts,
  1929. DenseSet<ValuePair> &FixedOrderPairs,
  1930. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  1931. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  1932. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
  1933. DenseSet<ValuePair> &PairableInstUsers,
  1934. DenseMap<Value *, Value *>& ChosenPairs) {
  1935. bool UseCycleCheck =
  1936. CandidatePairsSet.size() <= Config.MaxCandPairsForCycleCheck;
  1937. DenseMap<Value *, std::vector<Value *> > CandidatePairs2;
  1938. for (DenseSet<ValuePair>::iterator I = CandidatePairsSet.begin(),
  1939. E = CandidatePairsSet.end(); I != E; ++I) {
  1940. std::vector<Value *> &JJ = CandidatePairs2[I->second];
  1941. if (JJ.empty()) JJ.reserve(32);
  1942. JJ.push_back(I->first);
  1943. }
  1944. DenseMap<ValuePair, std::vector<ValuePair> > PairableInstUserMap;
  1945. DenseSet<VPPair> PairableInstUserPairSet;
  1946. for (std::vector<Value *>::iterator I = PairableInsts.begin(),
  1947. E = PairableInsts.end(); I != E; ++I) {
  1948. // The number of possible pairings for this variable:
  1949. size_t NumChoices = CandidatePairs.lookup(*I).size();
  1950. if (!NumChoices) continue;
  1951. std::vector<Value *> &JJ = CandidatePairs[*I];
  1952. // The best pair to choose and its dag:
  1953. size_t BestMaxDepth = 0;
  1954. int BestEffSize = 0;
  1955. DenseSet<ValuePair> BestDAG;
  1956. findBestDAGFor(CandidatePairs, CandidatePairsSet,
  1957. CandidatePairCostSavings,
  1958. PairableInsts, FixedOrderPairs, PairConnectionTypes,
  1959. ConnectedPairs, ConnectedPairDeps,
  1960. PairableInstUsers, PairableInstUserMap,
  1961. PairableInstUserPairSet, ChosenPairs,
  1962. BestDAG, BestMaxDepth, BestEffSize, *I, JJ,
  1963. UseCycleCheck);
  1964. if (BestDAG.empty())
  1965. continue;
  1966. // A dag has been chosen (or not) at this point. If no dag was
  1967. // chosen, then this instruction, I, cannot be paired (and is no longer
  1968. // considered).
  1969. DEBUG(dbgs() << "BBV: selected pairs in the best DAG for: "
  1970. << *cast<Instruction>(*I) << "\n");
  1971. for (DenseSet<ValuePair>::iterator S = BestDAG.begin(),
  1972. SE2 = BestDAG.end(); S != SE2; ++S) {
  1973. // Insert the members of this dag into the list of chosen pairs.
  1974. ChosenPairs.insert(ValuePair(S->first, S->second));
  1975. DEBUG(dbgs() << "BBV: selected pair: " << *S->first << " <-> " <<
  1976. *S->second << "\n");
  1977. // Remove all candidate pairs that have values in the chosen dag.
  1978. std::vector<Value *> &KK = CandidatePairs[S->first];
  1979. for (std::vector<Value *>::iterator K = KK.begin(), KE = KK.end();
  1980. K != KE; ++K) {
  1981. if (*K == S->second)
  1982. continue;
  1983. CandidatePairsSet.erase(ValuePair(S->first, *K));
  1984. }
  1985. std::vector<Value *> &LL = CandidatePairs2[S->second];
  1986. for (std::vector<Value *>::iterator L = LL.begin(), LE = LL.end();
  1987. L != LE; ++L) {
  1988. if (*L == S->first)
  1989. continue;
  1990. CandidatePairsSet.erase(ValuePair(*L, S->second));
  1991. }
  1992. std::vector<Value *> &MM = CandidatePairs[S->second];
  1993. for (std::vector<Value *>::iterator M = MM.begin(), ME = MM.end();
  1994. M != ME; ++M) {
  1995. assert(*M != S->first && "Flipped pair in candidate list?");
  1996. CandidatePairsSet.erase(ValuePair(S->second, *M));
  1997. }
  1998. std::vector<Value *> &NN = CandidatePairs2[S->first];
  1999. for (std::vector<Value *>::iterator N = NN.begin(), NE = NN.end();
  2000. N != NE; ++N) {
  2001. assert(*N != S->second && "Flipped pair in candidate list?");
  2002. CandidatePairsSet.erase(ValuePair(*N, S->first));
  2003. }
  2004. }
  2005. }
  2006. DEBUG(dbgs() << "BBV: selected " << ChosenPairs.size() << " pairs.\n");
  2007. }
  2008. std::string getReplacementName(Instruction *I, bool IsInput, unsigned o,
  2009. unsigned n = 0) {
  2010. if (!I->hasName())
  2011. return "";
  2012. return (I->getName() + (IsInput ? ".v.i" : ".v.r") + utostr(o) +
  2013. (n > 0 ? "." + utostr(n) : "")).str();
  2014. }
  2015. // Returns the value that is to be used as the pointer input to the vector
  2016. // instruction that fuses I with J.
  2017. Value *BBVectorize::getReplacementPointerInput(LLVMContext& Context,
  2018. Instruction *I, Instruction *J, unsigned o) {
  2019. Value *IPtr, *JPtr;
  2020. unsigned IAlignment, JAlignment, IAddressSpace, JAddressSpace;
  2021. int64_t OffsetInElmts;
  2022. // Note: the analysis might fail here, that is why the pair order has
  2023. // been precomputed (OffsetInElmts must be unused here).
  2024. (void) getPairPtrInfo(I, J, IPtr, JPtr, IAlignment, JAlignment,
  2025. IAddressSpace, JAddressSpace,
  2026. OffsetInElmts, false);
  2027. // The pointer value is taken to be the one with the lowest offset.
  2028. Value *VPtr = IPtr;
  2029. Type *ArgTypeI = IPtr->getType()->getPointerElementType();
  2030. Type *ArgTypeJ = JPtr->getType()->getPointerElementType();
  2031. Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
  2032. Type *VArgPtrType
  2033. = PointerType::get(VArgType,
  2034. IPtr->getType()->getPointerAddressSpace());
  2035. return new BitCastInst(VPtr, VArgPtrType, getReplacementName(I, true, o),
  2036. /* insert before */ I);
  2037. }
  2038. void BBVectorize::fillNewShuffleMask(LLVMContext& Context, Instruction *J,
  2039. unsigned MaskOffset, unsigned NumInElem,
  2040. unsigned NumInElem1, unsigned IdxOffset,
  2041. std::vector<Constant*> &Mask) {
  2042. unsigned NumElem1 = J->getType()->getVectorNumElements();
  2043. for (unsigned v = 0; v < NumElem1; ++v) {
  2044. int m = cast<ShuffleVectorInst>(J)->getMaskValue(v);
  2045. if (m < 0) {
  2046. Mask[v+MaskOffset] = UndefValue::get(Type::getInt32Ty(Context));
  2047. } else {
  2048. unsigned mm = m + (int) IdxOffset;
  2049. if (m >= (int) NumInElem1)
  2050. mm += (int) NumInElem;
  2051. Mask[v+MaskOffset] =
  2052. ConstantInt::get(Type::getInt32Ty(Context), mm);
  2053. }
  2054. }
  2055. }
  2056. // Returns the value that is to be used as the vector-shuffle mask to the
  2057. // vector instruction that fuses I with J.
  2058. Value *BBVectorize::getReplacementShuffleMask(LLVMContext& Context,
  2059. Instruction *I, Instruction *J) {
  2060. // This is the shuffle mask. We need to append the second
  2061. // mask to the first, and the numbers need to be adjusted.
  2062. Type *ArgTypeI = I->getType();
  2063. Type *ArgTypeJ = J->getType();
  2064. Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
  2065. unsigned NumElemI = ArgTypeI->getVectorNumElements();
  2066. // Get the total number of elements in the fused vector type.
  2067. // By definition, this must equal the number of elements in
  2068. // the final mask.
  2069. unsigned NumElem = VArgType->getVectorNumElements();
  2070. std::vector<Constant*> Mask(NumElem);
  2071. Type *OpTypeI = I->getOperand(0)->getType();
  2072. unsigned NumInElemI = OpTypeI->getVectorNumElements();
  2073. Type *OpTypeJ = J->getOperand(0)->getType();
  2074. unsigned NumInElemJ = OpTypeJ->getVectorNumElements();
  2075. // The fused vector will be:
  2076. // -----------------------------------------------------
  2077. // | NumInElemI | NumInElemJ | NumInElemI | NumInElemJ |
  2078. // -----------------------------------------------------
  2079. // from which we'll extract NumElem total elements (where the first NumElemI
  2080. // of them come from the mask in I and the remainder come from the mask
  2081. // in J.
  2082. // For the mask from the first pair...
  2083. fillNewShuffleMask(Context, I, 0, NumInElemJ, NumInElemI,
  2084. 0, Mask);
  2085. // For the mask from the second pair...
  2086. fillNewShuffleMask(Context, J, NumElemI, NumInElemI, NumInElemJ,
  2087. NumInElemI, Mask);
  2088. return ConstantVector::get(Mask);
  2089. }
  2090. bool BBVectorize::expandIEChain(LLVMContext& Context, Instruction *I,
  2091. Instruction *J, unsigned o, Value *&LOp,
  2092. unsigned numElemL,
  2093. Type *ArgTypeL, Type *ArgTypeH,
  2094. bool IBeforeJ, unsigned IdxOff) {
  2095. bool ExpandedIEChain = false;
  2096. if (InsertElementInst *LIE = dyn_cast<InsertElementInst>(LOp)) {
  2097. // If we have a pure insertelement chain, then this can be rewritten
  2098. // into a chain that directly builds the larger type.
  2099. if (isPureIEChain(LIE)) {
  2100. SmallVector<Value *, 8> VectElemts(numElemL,
  2101. UndefValue::get(ArgTypeL->getScalarType()));
  2102. InsertElementInst *LIENext = LIE;
  2103. do {
  2104. unsigned Idx =
  2105. cast<ConstantInt>(LIENext->getOperand(2))->getSExtValue();
  2106. VectElemts[Idx] = LIENext->getOperand(1);
  2107. } while ((LIENext =
  2108. dyn_cast<InsertElementInst>(LIENext->getOperand(0))));
  2109. LIENext = nullptr;
  2110. Value *LIEPrev = UndefValue::get(ArgTypeH);
  2111. for (unsigned i = 0; i < numElemL; ++i) {
  2112. if (isa<UndefValue>(VectElemts[i])) continue;
  2113. LIENext = InsertElementInst::Create(LIEPrev, VectElemts[i],
  2114. ConstantInt::get(Type::getInt32Ty(Context),
  2115. i + IdxOff),
  2116. getReplacementName(IBeforeJ ? I : J,
  2117. true, o, i+1));
  2118. LIENext->insertBefore(IBeforeJ ? J : I);
  2119. LIEPrev = LIENext;
  2120. }
  2121. LOp = LIENext ? (Value*) LIENext : UndefValue::get(ArgTypeH);
  2122. ExpandedIEChain = true;
  2123. }
  2124. }
  2125. return ExpandedIEChain;
  2126. }
  2127. static unsigned getNumScalarElements(Type *Ty) {
  2128. if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
  2129. return VecTy->getNumElements();
  2130. return 1;
  2131. }
  2132. // Returns the value to be used as the specified operand of the vector
  2133. // instruction that fuses I with J.
  2134. Value *BBVectorize::getReplacementInput(LLVMContext& Context, Instruction *I,
  2135. Instruction *J, unsigned o, bool IBeforeJ) {
  2136. Value *CV0 = ConstantInt::get(Type::getInt32Ty(Context), 0);
  2137. Value *CV1 = ConstantInt::get(Type::getInt32Ty(Context), 1);
  2138. // Compute the fused vector type for this operand
  2139. Type *ArgTypeI = I->getOperand(o)->getType();
  2140. Type *ArgTypeJ = J->getOperand(o)->getType();
  2141. VectorType *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
  2142. Instruction *L = I, *H = J;
  2143. Type *ArgTypeL = ArgTypeI, *ArgTypeH = ArgTypeJ;
  2144. unsigned numElemL = getNumScalarElements(ArgTypeL);
  2145. unsigned numElemH = getNumScalarElements(ArgTypeH);
  2146. Value *LOp = L->getOperand(o);
  2147. Value *HOp = H->getOperand(o);
  2148. unsigned numElem = VArgType->getNumElements();
  2149. // First, we check if we can reuse the "original" vector outputs (if these
  2150. // exist). We might need a shuffle.
  2151. ExtractElementInst *LEE = dyn_cast<ExtractElementInst>(LOp);
  2152. ExtractElementInst *HEE = dyn_cast<ExtractElementInst>(HOp);
  2153. ShuffleVectorInst *LSV = dyn_cast<ShuffleVectorInst>(LOp);
  2154. ShuffleVectorInst *HSV = dyn_cast<ShuffleVectorInst>(HOp);
  2155. // FIXME: If we're fusing shuffle instructions, then we can't apply this
  2156. // optimization. The input vectors to the shuffle might be a different
  2157. // length from the shuffle outputs. Unfortunately, the replacement
  2158. // shuffle mask has already been formed, and the mask entries are sensitive
  2159. // to the sizes of the inputs.
  2160. bool IsSizeChangeShuffle =
  2161. isa<ShuffleVectorInst>(L) &&
  2162. (LOp->getType() != L->getType() || HOp->getType() != H->getType());
  2163. if ((LEE || LSV) && (HEE || HSV) && !IsSizeChangeShuffle) {
  2164. // We can have at most two unique vector inputs.
  2165. bool CanUseInputs = true;
  2166. Value *I1, *I2 = nullptr;
  2167. if (LEE) {
  2168. I1 = LEE->getOperand(0);
  2169. } else {
  2170. I1 = LSV->getOperand(0);
  2171. I2 = LSV->getOperand(1);
  2172. if (I2 == I1 || isa<UndefValue>(I2))
  2173. I2 = nullptr;
  2174. }
  2175. if (HEE) {
  2176. Value *I3 = HEE->getOperand(0);
  2177. if (!I2 && I3 != I1)
  2178. I2 = I3;
  2179. else if (I3 != I1 && I3 != I2)
  2180. CanUseInputs = false;
  2181. } else {
  2182. Value *I3 = HSV->getOperand(0);
  2183. if (!I2 && I3 != I1)
  2184. I2 = I3;
  2185. else if (I3 != I1 && I3 != I2)
  2186. CanUseInputs = false;
  2187. if (CanUseInputs) {
  2188. Value *I4 = HSV->getOperand(1);
  2189. if (!isa<UndefValue>(I4)) {
  2190. if (!I2 && I4 != I1)
  2191. I2 = I4;
  2192. else if (I4 != I1 && I4 != I2)
  2193. CanUseInputs = false;
  2194. }
  2195. }
  2196. }
  2197. if (CanUseInputs) {
  2198. unsigned LOpElem =
  2199. cast<Instruction>(LOp)->getOperand(0)->getType()
  2200. ->getVectorNumElements();
  2201. unsigned HOpElem =
  2202. cast<Instruction>(HOp)->getOperand(0)->getType()
  2203. ->getVectorNumElements();
  2204. // We have one or two input vectors. We need to map each index of the
  2205. // operands to the index of the original vector.
  2206. SmallVector<std::pair<int, int>, 8> II(numElem);
  2207. for (unsigned i = 0; i < numElemL; ++i) {
  2208. int Idx, INum;
  2209. if (LEE) {
  2210. Idx =
  2211. cast<ConstantInt>(LEE->getOperand(1))->getSExtValue();
  2212. INum = LEE->getOperand(0) == I1 ? 0 : 1;
  2213. } else {
  2214. Idx = LSV->getMaskValue(i);
  2215. if (Idx < (int) LOpElem) {
  2216. INum = LSV->getOperand(0) == I1 ? 0 : 1;
  2217. } else {
  2218. Idx -= LOpElem;
  2219. INum = LSV->getOperand(1) == I1 ? 0 : 1;
  2220. }
  2221. }
  2222. II[i] = std::pair<int, int>(Idx, INum);
  2223. }
  2224. for (unsigned i = 0; i < numElemH; ++i) {
  2225. int Idx, INum;
  2226. if (HEE) {
  2227. Idx =
  2228. cast<ConstantInt>(HEE->getOperand(1))->getSExtValue();
  2229. INum = HEE->getOperand(0) == I1 ? 0 : 1;
  2230. } else {
  2231. Idx = HSV->getMaskValue(i);
  2232. if (Idx < (int) HOpElem) {
  2233. INum = HSV->getOperand(0) == I1 ? 0 : 1;
  2234. } else {
  2235. Idx -= HOpElem;
  2236. INum = HSV->getOperand(1) == I1 ? 0 : 1;
  2237. }
  2238. }
  2239. II[i + numElemL] = std::pair<int, int>(Idx, INum);
  2240. }
  2241. // We now have an array which tells us from which index of which
  2242. // input vector each element of the operand comes.
  2243. VectorType *I1T = cast<VectorType>(I1->getType());
  2244. unsigned I1Elem = I1T->getNumElements();
  2245. if (!I2) {
  2246. // In this case there is only one underlying vector input. Check for
  2247. // the trivial case where we can use the input directly.
  2248. if (I1Elem == numElem) {
  2249. bool ElemInOrder = true;
  2250. for (unsigned i = 0; i < numElem; ++i) {
  2251. if (II[i].first != (int) i && II[i].first != -1) {
  2252. ElemInOrder = false;
  2253. break;
  2254. }
  2255. }
  2256. if (ElemInOrder)
  2257. return I1;
  2258. }
  2259. // A shuffle is needed.
  2260. std::vector<Constant *> Mask(numElem);
  2261. for (unsigned i = 0; i < numElem; ++i) {
  2262. int Idx = II[i].first;
  2263. if (Idx == -1)
  2264. Mask[i] = UndefValue::get(Type::getInt32Ty(Context));
  2265. else
  2266. Mask[i] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
  2267. }
  2268. Instruction *S =
  2269. new ShuffleVectorInst(I1, UndefValue::get(I1T),
  2270. ConstantVector::get(Mask),
  2271. getReplacementName(IBeforeJ ? I : J,
  2272. true, o));
  2273. S->insertBefore(IBeforeJ ? J : I);
  2274. return S;
  2275. }
  2276. VectorType *I2T = cast<VectorType>(I2->getType());
  2277. unsigned I2Elem = I2T->getNumElements();
  2278. // This input comes from two distinct vectors. The first step is to
  2279. // make sure that both vectors are the same length. If not, the
  2280. // smaller one will need to grow before they can be shuffled together.
  2281. if (I1Elem < I2Elem) {
  2282. std::vector<Constant *> Mask(I2Elem);
  2283. unsigned v = 0;
  2284. for (; v < I1Elem; ++v)
  2285. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2286. for (; v < I2Elem; ++v)
  2287. Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
  2288. Instruction *NewI1 =
  2289. new ShuffleVectorInst(I1, UndefValue::get(I1T),
  2290. ConstantVector::get(Mask),
  2291. getReplacementName(IBeforeJ ? I : J,
  2292. true, o, 1));
  2293. NewI1->insertBefore(IBeforeJ ? J : I);
  2294. I1 = NewI1;
  2295. I1Elem = I2Elem;
  2296. } else if (I1Elem > I2Elem) {
  2297. std::vector<Constant *> Mask(I1Elem);
  2298. unsigned v = 0;
  2299. for (; v < I2Elem; ++v)
  2300. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2301. for (; v < I1Elem; ++v)
  2302. Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
  2303. Instruction *NewI2 =
  2304. new ShuffleVectorInst(I2, UndefValue::get(I2T),
  2305. ConstantVector::get(Mask),
  2306. getReplacementName(IBeforeJ ? I : J,
  2307. true, o, 1));
  2308. NewI2->insertBefore(IBeforeJ ? J : I);
  2309. I2 = NewI2;
  2310. }
  2311. // Now that both I1 and I2 are the same length we can shuffle them
  2312. // together (and use the result).
  2313. std::vector<Constant *> Mask(numElem);
  2314. for (unsigned v = 0; v < numElem; ++v) {
  2315. if (II[v].first == -1) {
  2316. Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
  2317. } else {
  2318. int Idx = II[v].first + II[v].second * I1Elem;
  2319. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
  2320. }
  2321. }
  2322. Instruction *NewOp =
  2323. new ShuffleVectorInst(I1, I2, ConstantVector::get(Mask),
  2324. getReplacementName(IBeforeJ ? I : J, true, o));
  2325. NewOp->insertBefore(IBeforeJ ? J : I);
  2326. return NewOp;
  2327. }
  2328. }
  2329. Type *ArgType = ArgTypeL;
  2330. if (numElemL < numElemH) {
  2331. if (numElemL == 1 && expandIEChain(Context, I, J, o, HOp, numElemH,
  2332. ArgTypeL, VArgType, IBeforeJ, 1)) {
  2333. // This is another short-circuit case: we're combining a scalar into
  2334. // a vector that is formed by an IE chain. We've just expanded the IE
  2335. // chain, now insert the scalar and we're done.
  2336. Instruction *S = InsertElementInst::Create(HOp, LOp, CV0,
  2337. getReplacementName(IBeforeJ ? I : J, true, o));
  2338. S->insertBefore(IBeforeJ ? J : I);
  2339. return S;
  2340. } else if (!expandIEChain(Context, I, J, o, LOp, numElemL, ArgTypeL,
  2341. ArgTypeH, IBeforeJ)) {
  2342. // The two vector inputs to the shuffle must be the same length,
  2343. // so extend the smaller vector to be the same length as the larger one.
  2344. Instruction *NLOp;
  2345. if (numElemL > 1) {
  2346. std::vector<Constant *> Mask(numElemH);
  2347. unsigned v = 0;
  2348. for (; v < numElemL; ++v)
  2349. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2350. for (; v < numElemH; ++v)
  2351. Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
  2352. NLOp = new ShuffleVectorInst(LOp, UndefValue::get(ArgTypeL),
  2353. ConstantVector::get(Mask),
  2354. getReplacementName(IBeforeJ ? I : J,
  2355. true, o, 1));
  2356. } else {
  2357. NLOp = InsertElementInst::Create(UndefValue::get(ArgTypeH), LOp, CV0,
  2358. getReplacementName(IBeforeJ ? I : J,
  2359. true, o, 1));
  2360. }
  2361. NLOp->insertBefore(IBeforeJ ? J : I);
  2362. LOp = NLOp;
  2363. }
  2364. ArgType = ArgTypeH;
  2365. } else if (numElemL > numElemH) {
  2366. if (numElemH == 1 && expandIEChain(Context, I, J, o, LOp, numElemL,
  2367. ArgTypeH, VArgType, IBeforeJ)) {
  2368. Instruction *S =
  2369. InsertElementInst::Create(LOp, HOp,
  2370. ConstantInt::get(Type::getInt32Ty(Context),
  2371. numElemL),
  2372. getReplacementName(IBeforeJ ? I : J,
  2373. true, o));
  2374. S->insertBefore(IBeforeJ ? J : I);
  2375. return S;
  2376. } else if (!expandIEChain(Context, I, J, o, HOp, numElemH, ArgTypeH,
  2377. ArgTypeL, IBeforeJ)) {
  2378. Instruction *NHOp;
  2379. if (numElemH > 1) {
  2380. std::vector<Constant *> Mask(numElemL);
  2381. unsigned v = 0;
  2382. for (; v < numElemH; ++v)
  2383. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2384. for (; v < numElemL; ++v)
  2385. Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
  2386. NHOp = new ShuffleVectorInst(HOp, UndefValue::get(ArgTypeH),
  2387. ConstantVector::get(Mask),
  2388. getReplacementName(IBeforeJ ? I : J,
  2389. true, o, 1));
  2390. } else {
  2391. NHOp = InsertElementInst::Create(UndefValue::get(ArgTypeL), HOp, CV0,
  2392. getReplacementName(IBeforeJ ? I : J,
  2393. true, o, 1));
  2394. }
  2395. NHOp->insertBefore(IBeforeJ ? J : I);
  2396. HOp = NHOp;
  2397. }
  2398. }
  2399. if (ArgType->isVectorTy()) {
  2400. unsigned numElem = VArgType->getVectorNumElements();
  2401. std::vector<Constant*> Mask(numElem);
  2402. for (unsigned v = 0; v < numElem; ++v) {
  2403. unsigned Idx = v;
  2404. // If the low vector was expanded, we need to skip the extra
  2405. // undefined entries.
  2406. if (v >= numElemL && numElemH > numElemL)
  2407. Idx += (numElemH - numElemL);
  2408. Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
  2409. }
  2410. Instruction *BV = new ShuffleVectorInst(LOp, HOp,
  2411. ConstantVector::get(Mask),
  2412. getReplacementName(IBeforeJ ? I : J, true, o));
  2413. BV->insertBefore(IBeforeJ ? J : I);
  2414. return BV;
  2415. }
  2416. Instruction *BV1 = InsertElementInst::Create(
  2417. UndefValue::get(VArgType), LOp, CV0,
  2418. getReplacementName(IBeforeJ ? I : J,
  2419. true, o, 1));
  2420. BV1->insertBefore(IBeforeJ ? J : I);
  2421. Instruction *BV2 = InsertElementInst::Create(BV1, HOp, CV1,
  2422. getReplacementName(IBeforeJ ? I : J,
  2423. true, o, 2));
  2424. BV2->insertBefore(IBeforeJ ? J : I);
  2425. return BV2;
  2426. }
  2427. // This function creates an array of values that will be used as the inputs
  2428. // to the vector instruction that fuses I with J.
  2429. void BBVectorize::getReplacementInputsForPair(LLVMContext& Context,
  2430. Instruction *I, Instruction *J,
  2431. SmallVectorImpl<Value *> &ReplacedOperands,
  2432. bool IBeforeJ) {
  2433. unsigned NumOperands = I->getNumOperands();
  2434. for (unsigned p = 0, o = NumOperands-1; p < NumOperands; ++p, --o) {
  2435. // Iterate backward so that we look at the store pointer
  2436. // first and know whether or not we need to flip the inputs.
  2437. if (isa<LoadInst>(I) || (o == 1 && isa<StoreInst>(I))) {
  2438. // This is the pointer for a load/store instruction.
  2439. ReplacedOperands[o] = getReplacementPointerInput(Context, I, J, o);
  2440. continue;
  2441. } else if (isa<CallInst>(I)) {
  2442. Function *F = cast<CallInst>(I)->getCalledFunction();
  2443. Intrinsic::ID IID = F->getIntrinsicID();
  2444. if (o == NumOperands-1) {
  2445. BasicBlock &BB = *I->getParent();
  2446. Module *M = BB.getParent()->getParent();
  2447. Type *ArgTypeI = I->getType();
  2448. Type *ArgTypeJ = J->getType();
  2449. Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
  2450. ReplacedOperands[o] = Intrinsic::getDeclaration(M, IID, VArgType);
  2451. continue;
  2452. } else if ((IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
  2453. IID == Intrinsic::cttz) && o == 1) {
  2454. // The second argument of powi/ctlz/cttz is a single integer/constant
  2455. // and we've already checked that both arguments are equal.
  2456. // As a result, we just keep I's second argument.
  2457. ReplacedOperands[o] = I->getOperand(o);
  2458. continue;
  2459. }
  2460. } else if (isa<ShuffleVectorInst>(I) && o == NumOperands-1) {
  2461. ReplacedOperands[o] = getReplacementShuffleMask(Context, I, J);
  2462. continue;
  2463. }
  2464. ReplacedOperands[o] = getReplacementInput(Context, I, J, o, IBeforeJ);
  2465. }
  2466. }
  2467. // This function creates two values that represent the outputs of the
  2468. // original I and J instructions. These are generally vector shuffles
  2469. // or extracts. In many cases, these will end up being unused and, thus,
  2470. // eliminated by later passes.
  2471. void BBVectorize::replaceOutputsOfPair(LLVMContext& Context, Instruction *I,
  2472. Instruction *J, Instruction *K,
  2473. Instruction *&InsertionPt,
  2474. Instruction *&K1, Instruction *&K2) {
  2475. if (isa<StoreInst>(I))
  2476. return;
  2477. Type *IType = I->getType();
  2478. Type *JType = J->getType();
  2479. VectorType *VType = getVecTypeForPair(IType, JType);
  2480. unsigned numElem = VType->getNumElements();
  2481. unsigned numElemI = getNumScalarElements(IType);
  2482. unsigned numElemJ = getNumScalarElements(JType);
  2483. if (IType->isVectorTy()) {
  2484. std::vector<Constant *> Mask1(numElemI), Mask2(numElemI);
  2485. for (unsigned v = 0; v < numElemI; ++v) {
  2486. Mask1[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2487. Mask2[v] = ConstantInt::get(Type::getInt32Ty(Context), numElemJ + v);
  2488. }
  2489. K1 = new ShuffleVectorInst(K, UndefValue::get(VType),
  2490. ConstantVector::get(Mask1),
  2491. getReplacementName(K, false, 1));
  2492. } else {
  2493. Value *CV0 = ConstantInt::get(Type::getInt32Ty(Context), 0);
  2494. K1 = ExtractElementInst::Create(K, CV0, getReplacementName(K, false, 1));
  2495. }
  2496. if (JType->isVectorTy()) {
  2497. std::vector<Constant *> Mask1(numElemJ), Mask2(numElemJ);
  2498. for (unsigned v = 0; v < numElemJ; ++v) {
  2499. Mask1[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
  2500. Mask2[v] = ConstantInt::get(Type::getInt32Ty(Context), numElemI + v);
  2501. }
  2502. K2 = new ShuffleVectorInst(K, UndefValue::get(VType),
  2503. ConstantVector::get(Mask2),
  2504. getReplacementName(K, false, 2));
  2505. } else {
  2506. Value *CV1 = ConstantInt::get(Type::getInt32Ty(Context), numElem - 1);
  2507. K2 = ExtractElementInst::Create(K, CV1, getReplacementName(K, false, 2));
  2508. }
  2509. K1->insertAfter(K);
  2510. K2->insertAfter(K1);
  2511. InsertionPt = K2;
  2512. }
  2513. // Move all uses of the function I (including pairing-induced uses) after J.
  2514. bool BBVectorize::canMoveUsesOfIAfterJ(BasicBlock &BB,
  2515. DenseSet<ValuePair> &LoadMoveSetPairs,
  2516. Instruction *I, Instruction *J) {
  2517. // Skip to the first instruction past I.
  2518. BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
  2519. DenseSet<Value *> Users;
  2520. AliasSetTracker WriteSet(*AA);
  2521. if (I->mayWriteToMemory()) WriteSet.add(I);
  2522. for (; cast<Instruction>(L) != J; ++L)
  2523. (void)trackUsesOfI(Users, WriteSet, I, &*L, true, &LoadMoveSetPairs);
  2524. assert(cast<Instruction>(L) == J &&
  2525. "Tracking has not proceeded far enough to check for dependencies");
  2526. // If J is now in the use set of I, then trackUsesOfI will return true
  2527. // and we have a dependency cycle (and the fusing operation must abort).
  2528. return !trackUsesOfI(Users, WriteSet, I, J, true, &LoadMoveSetPairs);
  2529. }
  2530. // Move all uses of the function I (including pairing-induced uses) after J.
  2531. void BBVectorize::moveUsesOfIAfterJ(BasicBlock &BB,
  2532. DenseSet<ValuePair> &LoadMoveSetPairs,
  2533. Instruction *&InsertionPt,
  2534. Instruction *I, Instruction *J) {
  2535. // Skip to the first instruction past I.
  2536. BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
  2537. DenseSet<Value *> Users;
  2538. AliasSetTracker WriteSet(*AA);
  2539. if (I->mayWriteToMemory()) WriteSet.add(I);
  2540. for (; cast<Instruction>(L) != J;) {
  2541. if (trackUsesOfI(Users, WriteSet, I, &*L, true, &LoadMoveSetPairs)) {
  2542. // Move this instruction
  2543. Instruction *InstToMove = &*L++;
  2544. DEBUG(dbgs() << "BBV: moving: " << *InstToMove <<
  2545. " to after " << *InsertionPt << "\n");
  2546. InstToMove->removeFromParent();
  2547. InstToMove->insertAfter(InsertionPt);
  2548. InsertionPt = InstToMove;
  2549. } else {
  2550. ++L;
  2551. }
  2552. }
  2553. }
  2554. // Collect all load instruction that are in the move set of a given first
  2555. // pair member. These loads depend on the first instruction, I, and so need
  2556. // to be moved after J (the second instruction) when the pair is fused.
  2557. void BBVectorize::collectPairLoadMoveSet(BasicBlock &BB,
  2558. DenseMap<Value *, Value *> &ChosenPairs,
  2559. DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
  2560. DenseSet<ValuePair> &LoadMoveSetPairs,
  2561. Instruction *I) {
  2562. // Skip to the first instruction past I.
  2563. BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
  2564. DenseSet<Value *> Users;
  2565. AliasSetTracker WriteSet(*AA);
  2566. if (I->mayWriteToMemory()) WriteSet.add(I);
  2567. // Note: We cannot end the loop when we reach J because J could be moved
  2568. // farther down the use chain by another instruction pairing. Also, J
  2569. // could be before I if this is an inverted input.
  2570. for (BasicBlock::iterator E = BB.end(); L != E; ++L) {
  2571. if (trackUsesOfI(Users, WriteSet, I, &*L)) {
  2572. if (L->mayReadFromMemory()) {
  2573. LoadMoveSet[&*L].push_back(I);
  2574. LoadMoveSetPairs.insert(ValuePair(&*L, I));
  2575. }
  2576. }
  2577. }
  2578. }
  2579. // In cases where both load/stores and the computation of their pointers
  2580. // are chosen for vectorization, we can end up in a situation where the
  2581. // aliasing analysis starts returning different query results as the
  2582. // process of fusing instruction pairs continues. Because the algorithm
  2583. // relies on finding the same use dags here as were found earlier, we'll
  2584. // need to precompute the necessary aliasing information here and then
  2585. // manually update it during the fusion process.
  2586. void BBVectorize::collectLoadMoveSet(BasicBlock &BB,
  2587. std::vector<Value *> &PairableInsts,
  2588. DenseMap<Value *, Value *> &ChosenPairs,
  2589. DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
  2590. DenseSet<ValuePair> &LoadMoveSetPairs) {
  2591. for (std::vector<Value *>::iterator PI = PairableInsts.begin(),
  2592. PIE = PairableInsts.end(); PI != PIE; ++PI) {
  2593. DenseMap<Value *, Value *>::iterator P = ChosenPairs.find(*PI);
  2594. if (P == ChosenPairs.end()) continue;
  2595. Instruction *I = cast<Instruction>(P->first);
  2596. collectPairLoadMoveSet(BB, ChosenPairs, LoadMoveSet,
  2597. LoadMoveSetPairs, I);
  2598. }
  2599. }
  2600. // This function fuses the chosen instruction pairs into vector instructions,
  2601. // taking care preserve any needed scalar outputs and, then, it reorders the
  2602. // remaining instructions as needed (users of the first member of the pair
  2603. // need to be moved to after the location of the second member of the pair
  2604. // because the vector instruction is inserted in the location of the pair's
  2605. // second member).
  2606. void BBVectorize::fuseChosenPairs(BasicBlock &BB,
  2607. std::vector<Value *> &PairableInsts,
  2608. DenseMap<Value *, Value *> &ChosenPairs,
  2609. DenseSet<ValuePair> &FixedOrderPairs,
  2610. DenseMap<VPPair, unsigned> &PairConnectionTypes,
  2611. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
  2612. DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps) {
  2613. LLVMContext& Context = BB.getContext();
  2614. // During the vectorization process, the order of the pairs to be fused
  2615. // could be flipped. So we'll add each pair, flipped, into the ChosenPairs
  2616. // list. After a pair is fused, the flipped pair is removed from the list.
  2617. DenseSet<ValuePair> FlippedPairs;
  2618. for (DenseMap<Value *, Value *>::iterator P = ChosenPairs.begin(),
  2619. E = ChosenPairs.end(); P != E; ++P)
  2620. FlippedPairs.insert(ValuePair(P->second, P->first));
  2621. for (DenseSet<ValuePair>::iterator P = FlippedPairs.begin(),
  2622. E = FlippedPairs.end(); P != E; ++P)
  2623. ChosenPairs.insert(*P);
  2624. DenseMap<Value *, std::vector<Value *> > LoadMoveSet;
  2625. DenseSet<ValuePair> LoadMoveSetPairs;
  2626. collectLoadMoveSet(BB, PairableInsts, ChosenPairs,
  2627. LoadMoveSet, LoadMoveSetPairs);
  2628. DEBUG(dbgs() << "BBV: initial: \n" << BB << "\n");
  2629. for (BasicBlock::iterator PI = BB.getFirstInsertionPt(); PI != BB.end();) {
  2630. DenseMap<Value *, Value *>::iterator P = ChosenPairs.find(&*PI);
  2631. if (P == ChosenPairs.end()) {
  2632. ++PI;
  2633. continue;
  2634. }
  2635. if (getDepthFactor(P->first) == 0) {
  2636. // These instructions are not really fused, but are tracked as though
  2637. // they are. Any case in which it would be interesting to fuse them
  2638. // will be taken care of by InstCombine.
  2639. --NumFusedOps;
  2640. ++PI;
  2641. continue;
  2642. }
  2643. Instruction *I = cast<Instruction>(P->first),
  2644. *J = cast<Instruction>(P->second);
  2645. DEBUG(dbgs() << "BBV: fusing: " << *I <<
  2646. " <-> " << *J << "\n");
  2647. // Remove the pair and flipped pair from the list.
  2648. DenseMap<Value *, Value *>::iterator FP = ChosenPairs.find(P->second);
  2649. assert(FP != ChosenPairs.end() && "Flipped pair not found in list");
  2650. ChosenPairs.erase(FP);
  2651. ChosenPairs.erase(P);
  2652. if (!canMoveUsesOfIAfterJ(BB, LoadMoveSetPairs, I, J)) {
  2653. DEBUG(dbgs() << "BBV: fusion of: " << *I <<
  2654. " <-> " << *J <<
  2655. " aborted because of non-trivial dependency cycle\n");
  2656. --NumFusedOps;
  2657. ++PI;
  2658. continue;
  2659. }
  2660. // If the pair must have the other order, then flip it.
  2661. bool FlipPairOrder = FixedOrderPairs.count(ValuePair(J, I));
  2662. if (!FlipPairOrder && !FixedOrderPairs.count(ValuePair(I, J))) {
  2663. // This pair does not have a fixed order, and so we might want to
  2664. // flip it if that will yield fewer shuffles. We count the number
  2665. // of dependencies connected via swaps, and those directly connected,
  2666. // and flip the order if the number of swaps is greater.
  2667. bool OrigOrder = true;
  2668. DenseMap<ValuePair, std::vector<ValuePair> >::iterator IJ =
  2669. ConnectedPairDeps.find(ValuePair(I, J));
  2670. if (IJ == ConnectedPairDeps.end()) {
  2671. IJ = ConnectedPairDeps.find(ValuePair(J, I));
  2672. OrigOrder = false;
  2673. }
  2674. if (IJ != ConnectedPairDeps.end()) {
  2675. unsigned NumDepsDirect = 0, NumDepsSwap = 0;
  2676. for (std::vector<ValuePair>::iterator T = IJ->second.begin(),
  2677. TE = IJ->second.end(); T != TE; ++T) {
  2678. VPPair Q(IJ->first, *T);
  2679. DenseMap<VPPair, unsigned>::iterator R =
  2680. PairConnectionTypes.find(VPPair(Q.second, Q.first));
  2681. assert(R != PairConnectionTypes.end() &&
  2682. "Cannot find pair connection type");
  2683. if (R->second == PairConnectionDirect)
  2684. ++NumDepsDirect;
  2685. else if (R->second == PairConnectionSwap)
  2686. ++NumDepsSwap;
  2687. }
  2688. if (!OrigOrder)
  2689. std::swap(NumDepsDirect, NumDepsSwap);
  2690. if (NumDepsSwap > NumDepsDirect) {
  2691. FlipPairOrder = true;
  2692. DEBUG(dbgs() << "BBV: reordering pair: " << *I <<
  2693. " <-> " << *J << "\n");
  2694. }
  2695. }
  2696. }
  2697. Instruction *L = I, *H = J;
  2698. if (FlipPairOrder)
  2699. std::swap(H, L);
  2700. // If the pair being fused uses the opposite order from that in the pair
  2701. // connection map, then we need to flip the types.
  2702. DenseMap<ValuePair, std::vector<ValuePair> >::iterator HL =
  2703. ConnectedPairs.find(ValuePair(H, L));
  2704. if (HL != ConnectedPairs.end())
  2705. for (std::vector<ValuePair>::iterator T = HL->second.begin(),
  2706. TE = HL->second.end(); T != TE; ++T) {
  2707. VPPair Q(HL->first, *T);
  2708. DenseMap<VPPair, unsigned>::iterator R = PairConnectionTypes.find(Q);
  2709. assert(R != PairConnectionTypes.end() &&
  2710. "Cannot find pair connection type");
  2711. if (R->second == PairConnectionDirect)
  2712. R->second = PairConnectionSwap;
  2713. else if (R->second == PairConnectionSwap)
  2714. R->second = PairConnectionDirect;
  2715. }
  2716. bool LBeforeH = !FlipPairOrder;
  2717. unsigned NumOperands = I->getNumOperands();
  2718. SmallVector<Value *, 3> ReplacedOperands(NumOperands);
  2719. getReplacementInputsForPair(Context, L, H, ReplacedOperands,
  2720. LBeforeH);
  2721. // Make a copy of the original operation, change its type to the vector
  2722. // type and replace its operands with the vector operands.
  2723. Instruction *K = L->clone();
  2724. if (L->hasName())
  2725. K->takeName(L);
  2726. else if (H->hasName())
  2727. K->takeName(H);
  2728. if (auto CS = CallSite(K)) {
  2729. SmallVector<Type *, 3> Tys;
  2730. FunctionType *Old = CS.getFunctionType();
  2731. unsigned NumOld = Old->getNumParams();
  2732. assert(NumOld <= ReplacedOperands.size());
  2733. for (unsigned i = 0; i != NumOld; ++i)
  2734. Tys.push_back(ReplacedOperands[i]->getType());
  2735. CS.mutateFunctionType(
  2736. FunctionType::get(getVecTypeForPair(L->getType(), H->getType()),
  2737. Tys, Old->isVarArg()));
  2738. } else if (!isa<StoreInst>(K))
  2739. K->mutateType(getVecTypeForPair(L->getType(), H->getType()));
  2740. unsigned KnownIDs[] = {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
  2741. LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
  2742. LLVMContext::MD_invariant_group};
  2743. combineMetadata(K, H, KnownIDs);
  2744. K->intersectOptionalDataWith(H);
  2745. for (unsigned o = 0; o < NumOperands; ++o)
  2746. K->setOperand(o, ReplacedOperands[o]);
  2747. K->insertAfter(J);
  2748. // Instruction insertion point:
  2749. Instruction *InsertionPt = K;
  2750. Instruction *K1 = nullptr, *K2 = nullptr;
  2751. replaceOutputsOfPair(Context, L, H, K, InsertionPt, K1, K2);
  2752. // The use dag of the first original instruction must be moved to after
  2753. // the location of the second instruction. The entire use dag of the
  2754. // first instruction is disjoint from the input dag of the second
  2755. // (by definition), and so commutes with it.
  2756. moveUsesOfIAfterJ(BB, LoadMoveSetPairs, InsertionPt, I, J);
  2757. if (!isa<StoreInst>(I)) {
  2758. L->replaceAllUsesWith(K1);
  2759. H->replaceAllUsesWith(K2);
  2760. }
  2761. // Instructions that may read from memory may be in the load move set.
  2762. // Once an instruction is fused, we no longer need its move set, and so
  2763. // the values of the map never need to be updated. However, when a load
  2764. // is fused, we need to merge the entries from both instructions in the
  2765. // pair in case those instructions were in the move set of some other
  2766. // yet-to-be-fused pair. The loads in question are the keys of the map.
  2767. if (I->mayReadFromMemory()) {
  2768. std::vector<ValuePair> NewSetMembers;
  2769. DenseMap<Value *, std::vector<Value *> >::iterator II =
  2770. LoadMoveSet.find(I);
  2771. if (II != LoadMoveSet.end())
  2772. for (std::vector<Value *>::iterator N = II->second.begin(),
  2773. NE = II->second.end(); N != NE; ++N)
  2774. NewSetMembers.push_back(ValuePair(K, *N));
  2775. DenseMap<Value *, std::vector<Value *> >::iterator JJ =
  2776. LoadMoveSet.find(J);
  2777. if (JJ != LoadMoveSet.end())
  2778. for (std::vector<Value *>::iterator N = JJ->second.begin(),
  2779. NE = JJ->second.end(); N != NE; ++N)
  2780. NewSetMembers.push_back(ValuePair(K, *N));
  2781. for (std::vector<ValuePair>::iterator A = NewSetMembers.begin(),
  2782. AE = NewSetMembers.end(); A != AE; ++A) {
  2783. LoadMoveSet[A->first].push_back(A->second);
  2784. LoadMoveSetPairs.insert(*A);
  2785. }
  2786. }
  2787. // Before removing I, set the iterator to the next instruction.
  2788. PI = std::next(BasicBlock::iterator(I));
  2789. if (cast<Instruction>(PI) == J)
  2790. ++PI;
  2791. SE->forgetValue(I);
  2792. SE->forgetValue(J);
  2793. I->eraseFromParent();
  2794. J->eraseFromParent();
  2795. DEBUG(if (PrintAfterEveryPair) dbgs() << "BBV: block is now: \n" <<
  2796. BB << "\n");
  2797. }
  2798. DEBUG(dbgs() << "BBV: final: \n" << BB << "\n");
  2799. }
  2800. }
  2801. char BBVectorize::ID = 0;
  2802. static const char bb_vectorize_name[] = "Basic-Block Vectorization";
  2803. INITIALIZE_PASS_BEGIN(BBVectorize, BBV_NAME, bb_vectorize_name, false, false)
  2804. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  2805. INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
  2806. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  2807. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  2808. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  2809. INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
  2810. INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
  2811. INITIALIZE_PASS_DEPENDENCY(SCEVAAWrapperPass)
  2812. INITIALIZE_PASS_END(BBVectorize, BBV_NAME, bb_vectorize_name, false, false)
  2813. BasicBlockPass *llvm::createBBVectorizePass(const VectorizeConfig &C) {
  2814. return new BBVectorize(C);
  2815. }
  2816. bool
  2817. llvm::vectorizeBasicBlock(Pass *P, BasicBlock &BB, const VectorizeConfig &C) {
  2818. BBVectorize BBVectorizer(P, *BB.getParent(), C);
  2819. return BBVectorizer.vectorizeBB(BB);
  2820. }
  2821. //===----------------------------------------------------------------------===//
  2822. VectorizeConfig::VectorizeConfig() {
  2823. VectorBits = ::VectorBits;
  2824. VectorizeBools = !::NoBools;
  2825. VectorizeInts = !::NoInts;
  2826. VectorizeFloats = !::NoFloats;
  2827. VectorizePointers = !::NoPointers;
  2828. VectorizeCasts = !::NoCasts;
  2829. VectorizeMath = !::NoMath;
  2830. VectorizeBitManipulations = !::NoBitManipulation;
  2831. VectorizeFMA = !::NoFMA;
  2832. VectorizeSelect = !::NoSelect;
  2833. VectorizeCmp = !::NoCmp;
  2834. VectorizeGEP = !::NoGEP;
  2835. VectorizeMemOps = !::NoMemOps;
  2836. AlignedOnly = ::AlignedOnly;
  2837. ReqChainDepth= ::ReqChainDepth;
  2838. SearchLimit = ::SearchLimit;
  2839. MaxCandPairsForCycleCheck = ::MaxCandPairsForCycleCheck;
  2840. SplatBreaksChain = ::SplatBreaksChain;
  2841. MaxInsts = ::MaxInsts;
  2842. MaxPairs = ::MaxPairs;
  2843. MaxIter = ::MaxIter;
  2844. Pow2LenOnly = ::Pow2LenOnly;
  2845. NoMemOpBoost = ::NoMemOpBoost;
  2846. FastDep = ::FastDep;
  2847. }