TargetLowering.cpp 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469
  1. //===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
  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 implements the TargetLowering class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetLowering.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/CodeGen/Analysis.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  20. #include "llvm/CodeGen/SelectionDAG.h"
  21. #include "llvm/DataLayout.h"
  22. #include "llvm/DerivedTypes.h"
  23. #include "llvm/GlobalVariable.h"
  24. #include "llvm/MC/MCAsmInfo.h"
  25. #include "llvm/MC/MCExpr.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/ErrorHandling.h"
  28. #include "llvm/Support/MathExtras.h"
  29. #include "llvm/Target/TargetLoweringObjectFile.h"
  30. #include "llvm/Target/TargetMachine.h"
  31. #include "llvm/Target/TargetRegisterInfo.h"
  32. #include <cctype>
  33. using namespace llvm;
  34. /// InitLibcallNames - Set default libcall names.
  35. ///
  36. static void InitLibcallNames(const char **Names) {
  37. Names[RTLIB::SHL_I16] = "__ashlhi3";
  38. Names[RTLIB::SHL_I32] = "__ashlsi3";
  39. Names[RTLIB::SHL_I64] = "__ashldi3";
  40. Names[RTLIB::SHL_I128] = "__ashlti3";
  41. Names[RTLIB::SRL_I16] = "__lshrhi3";
  42. Names[RTLIB::SRL_I32] = "__lshrsi3";
  43. Names[RTLIB::SRL_I64] = "__lshrdi3";
  44. Names[RTLIB::SRL_I128] = "__lshrti3";
  45. Names[RTLIB::SRA_I16] = "__ashrhi3";
  46. Names[RTLIB::SRA_I32] = "__ashrsi3";
  47. Names[RTLIB::SRA_I64] = "__ashrdi3";
  48. Names[RTLIB::SRA_I128] = "__ashrti3";
  49. Names[RTLIB::MUL_I8] = "__mulqi3";
  50. Names[RTLIB::MUL_I16] = "__mulhi3";
  51. Names[RTLIB::MUL_I32] = "__mulsi3";
  52. Names[RTLIB::MUL_I64] = "__muldi3";
  53. Names[RTLIB::MUL_I128] = "__multi3";
  54. Names[RTLIB::MULO_I32] = "__mulosi4";
  55. Names[RTLIB::MULO_I64] = "__mulodi4";
  56. Names[RTLIB::MULO_I128] = "__muloti4";
  57. Names[RTLIB::SDIV_I8] = "__divqi3";
  58. Names[RTLIB::SDIV_I16] = "__divhi3";
  59. Names[RTLIB::SDIV_I32] = "__divsi3";
  60. Names[RTLIB::SDIV_I64] = "__divdi3";
  61. Names[RTLIB::SDIV_I128] = "__divti3";
  62. Names[RTLIB::UDIV_I8] = "__udivqi3";
  63. Names[RTLIB::UDIV_I16] = "__udivhi3";
  64. Names[RTLIB::UDIV_I32] = "__udivsi3";
  65. Names[RTLIB::UDIV_I64] = "__udivdi3";
  66. Names[RTLIB::UDIV_I128] = "__udivti3";
  67. Names[RTLIB::SREM_I8] = "__modqi3";
  68. Names[RTLIB::SREM_I16] = "__modhi3";
  69. Names[RTLIB::SREM_I32] = "__modsi3";
  70. Names[RTLIB::SREM_I64] = "__moddi3";
  71. Names[RTLIB::SREM_I128] = "__modti3";
  72. Names[RTLIB::UREM_I8] = "__umodqi3";
  73. Names[RTLIB::UREM_I16] = "__umodhi3";
  74. Names[RTLIB::UREM_I32] = "__umodsi3";
  75. Names[RTLIB::UREM_I64] = "__umoddi3";
  76. Names[RTLIB::UREM_I128] = "__umodti3";
  77. // These are generally not available.
  78. Names[RTLIB::SDIVREM_I8] = 0;
  79. Names[RTLIB::SDIVREM_I16] = 0;
  80. Names[RTLIB::SDIVREM_I32] = 0;
  81. Names[RTLIB::SDIVREM_I64] = 0;
  82. Names[RTLIB::SDIVREM_I128] = 0;
  83. Names[RTLIB::UDIVREM_I8] = 0;
  84. Names[RTLIB::UDIVREM_I16] = 0;
  85. Names[RTLIB::UDIVREM_I32] = 0;
  86. Names[RTLIB::UDIVREM_I64] = 0;
  87. Names[RTLIB::UDIVREM_I128] = 0;
  88. Names[RTLIB::NEG_I32] = "__negsi2";
  89. Names[RTLIB::NEG_I64] = "__negdi2";
  90. Names[RTLIB::ADD_F32] = "__addsf3";
  91. Names[RTLIB::ADD_F64] = "__adddf3";
  92. Names[RTLIB::ADD_F80] = "__addxf3";
  93. Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
  94. Names[RTLIB::SUB_F32] = "__subsf3";
  95. Names[RTLIB::SUB_F64] = "__subdf3";
  96. Names[RTLIB::SUB_F80] = "__subxf3";
  97. Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
  98. Names[RTLIB::MUL_F32] = "__mulsf3";
  99. Names[RTLIB::MUL_F64] = "__muldf3";
  100. Names[RTLIB::MUL_F80] = "__mulxf3";
  101. Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
  102. Names[RTLIB::DIV_F32] = "__divsf3";
  103. Names[RTLIB::DIV_F64] = "__divdf3";
  104. Names[RTLIB::DIV_F80] = "__divxf3";
  105. Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
  106. Names[RTLIB::REM_F32] = "fmodf";
  107. Names[RTLIB::REM_F64] = "fmod";
  108. Names[RTLIB::REM_F80] = "fmodl";
  109. Names[RTLIB::REM_PPCF128] = "fmodl";
  110. Names[RTLIB::FMA_F32] = "fmaf";
  111. Names[RTLIB::FMA_F64] = "fma";
  112. Names[RTLIB::FMA_F80] = "fmal";
  113. Names[RTLIB::FMA_PPCF128] = "fmal";
  114. Names[RTLIB::POWI_F32] = "__powisf2";
  115. Names[RTLIB::POWI_F64] = "__powidf2";
  116. Names[RTLIB::POWI_F80] = "__powixf2";
  117. Names[RTLIB::POWI_PPCF128] = "__powitf2";
  118. Names[RTLIB::SQRT_F32] = "sqrtf";
  119. Names[RTLIB::SQRT_F64] = "sqrt";
  120. Names[RTLIB::SQRT_F80] = "sqrtl";
  121. Names[RTLIB::SQRT_PPCF128] = "sqrtl";
  122. Names[RTLIB::LOG_F32] = "logf";
  123. Names[RTLIB::LOG_F64] = "log";
  124. Names[RTLIB::LOG_F80] = "logl";
  125. Names[RTLIB::LOG_PPCF128] = "logl";
  126. Names[RTLIB::LOG2_F32] = "log2f";
  127. Names[RTLIB::LOG2_F64] = "log2";
  128. Names[RTLIB::LOG2_F80] = "log2l";
  129. Names[RTLIB::LOG2_PPCF128] = "log2l";
  130. Names[RTLIB::LOG10_F32] = "log10f";
  131. Names[RTLIB::LOG10_F64] = "log10";
  132. Names[RTLIB::LOG10_F80] = "log10l";
  133. Names[RTLIB::LOG10_PPCF128] = "log10l";
  134. Names[RTLIB::EXP_F32] = "expf";
  135. Names[RTLIB::EXP_F64] = "exp";
  136. Names[RTLIB::EXP_F80] = "expl";
  137. Names[RTLIB::EXP_PPCF128] = "expl";
  138. Names[RTLIB::EXP2_F32] = "exp2f";
  139. Names[RTLIB::EXP2_F64] = "exp2";
  140. Names[RTLIB::EXP2_F80] = "exp2l";
  141. Names[RTLIB::EXP2_PPCF128] = "exp2l";
  142. Names[RTLIB::SIN_F32] = "sinf";
  143. Names[RTLIB::SIN_F64] = "sin";
  144. Names[RTLIB::SIN_F80] = "sinl";
  145. Names[RTLIB::SIN_PPCF128] = "sinl";
  146. Names[RTLIB::COS_F32] = "cosf";
  147. Names[RTLIB::COS_F64] = "cos";
  148. Names[RTLIB::COS_F80] = "cosl";
  149. Names[RTLIB::COS_PPCF128] = "cosl";
  150. Names[RTLIB::POW_F32] = "powf";
  151. Names[RTLIB::POW_F64] = "pow";
  152. Names[RTLIB::POW_F80] = "powl";
  153. Names[RTLIB::POW_PPCF128] = "powl";
  154. Names[RTLIB::CEIL_F32] = "ceilf";
  155. Names[RTLIB::CEIL_F64] = "ceil";
  156. Names[RTLIB::CEIL_F80] = "ceill";
  157. Names[RTLIB::CEIL_PPCF128] = "ceill";
  158. Names[RTLIB::TRUNC_F32] = "truncf";
  159. Names[RTLIB::TRUNC_F64] = "trunc";
  160. Names[RTLIB::TRUNC_F80] = "truncl";
  161. Names[RTLIB::TRUNC_PPCF128] = "truncl";
  162. Names[RTLIB::RINT_F32] = "rintf";
  163. Names[RTLIB::RINT_F64] = "rint";
  164. Names[RTLIB::RINT_F80] = "rintl";
  165. Names[RTLIB::RINT_PPCF128] = "rintl";
  166. Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
  167. Names[RTLIB::NEARBYINT_F64] = "nearbyint";
  168. Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
  169. Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
  170. Names[RTLIB::FLOOR_F32] = "floorf";
  171. Names[RTLIB::FLOOR_F64] = "floor";
  172. Names[RTLIB::FLOOR_F80] = "floorl";
  173. Names[RTLIB::FLOOR_PPCF128] = "floorl";
  174. Names[RTLIB::COPYSIGN_F32] = "copysignf";
  175. Names[RTLIB::COPYSIGN_F64] = "copysign";
  176. Names[RTLIB::COPYSIGN_F80] = "copysignl";
  177. Names[RTLIB::COPYSIGN_PPCF128] = "copysignl";
  178. Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
  179. Names[RTLIB::FPEXT_F16_F32] = "__gnu_h2f_ieee";
  180. Names[RTLIB::FPROUND_F32_F16] = "__gnu_f2h_ieee";
  181. Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
  182. Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
  183. Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
  184. Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
  185. Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
  186. Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfqi";
  187. Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfhi";
  188. Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
  189. Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
  190. Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
  191. Names[RTLIB::FPTOSINT_F64_I8] = "__fixdfqi";
  192. Names[RTLIB::FPTOSINT_F64_I16] = "__fixdfhi";
  193. Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
  194. Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
  195. Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
  196. Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
  197. Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
  198. Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
  199. Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
  200. Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
  201. Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
  202. Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfqi";
  203. Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfhi";
  204. Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
  205. Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
  206. Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
  207. Names[RTLIB::FPTOUINT_F64_I8] = "__fixunsdfqi";
  208. Names[RTLIB::FPTOUINT_F64_I16] = "__fixunsdfhi";
  209. Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
  210. Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
  211. Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
  212. Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
  213. Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
  214. Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
  215. Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
  216. Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
  217. Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
  218. Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
  219. Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
  220. Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
  221. Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
  222. Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
  223. Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
  224. Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
  225. Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
  226. Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
  227. Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
  228. Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
  229. Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
  230. Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
  231. Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
  232. Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
  233. Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
  234. Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
  235. Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
  236. Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
  237. Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
  238. Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
  239. Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
  240. Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
  241. Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
  242. Names[RTLIB::OEQ_F32] = "__eqsf2";
  243. Names[RTLIB::OEQ_F64] = "__eqdf2";
  244. Names[RTLIB::UNE_F32] = "__nesf2";
  245. Names[RTLIB::UNE_F64] = "__nedf2";
  246. Names[RTLIB::OGE_F32] = "__gesf2";
  247. Names[RTLIB::OGE_F64] = "__gedf2";
  248. Names[RTLIB::OLT_F32] = "__ltsf2";
  249. Names[RTLIB::OLT_F64] = "__ltdf2";
  250. Names[RTLIB::OLE_F32] = "__lesf2";
  251. Names[RTLIB::OLE_F64] = "__ledf2";
  252. Names[RTLIB::OGT_F32] = "__gtsf2";
  253. Names[RTLIB::OGT_F64] = "__gtdf2";
  254. Names[RTLIB::UO_F32] = "__unordsf2";
  255. Names[RTLIB::UO_F64] = "__unorddf2";
  256. Names[RTLIB::O_F32] = "__unordsf2";
  257. Names[RTLIB::O_F64] = "__unorddf2";
  258. Names[RTLIB::MEMCPY] = "memcpy";
  259. Names[RTLIB::MEMMOVE] = "memmove";
  260. Names[RTLIB::MEMSET] = "memset";
  261. Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
  262. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1";
  263. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2";
  264. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4";
  265. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8";
  266. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_1] = "__sync_lock_test_and_set_1";
  267. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_2] = "__sync_lock_test_and_set_2";
  268. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_4] = "__sync_lock_test_and_set_4";
  269. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_8] = "__sync_lock_test_and_set_8";
  270. Names[RTLIB::SYNC_FETCH_AND_ADD_1] = "__sync_fetch_and_add_1";
  271. Names[RTLIB::SYNC_FETCH_AND_ADD_2] = "__sync_fetch_and_add_2";
  272. Names[RTLIB::SYNC_FETCH_AND_ADD_4] = "__sync_fetch_and_add_4";
  273. Names[RTLIB::SYNC_FETCH_AND_ADD_8] = "__sync_fetch_and_add_8";
  274. Names[RTLIB::SYNC_FETCH_AND_SUB_1] = "__sync_fetch_and_sub_1";
  275. Names[RTLIB::SYNC_FETCH_AND_SUB_2] = "__sync_fetch_and_sub_2";
  276. Names[RTLIB::SYNC_FETCH_AND_SUB_4] = "__sync_fetch_and_sub_4";
  277. Names[RTLIB::SYNC_FETCH_AND_SUB_8] = "__sync_fetch_and_sub_8";
  278. Names[RTLIB::SYNC_FETCH_AND_AND_1] = "__sync_fetch_and_and_1";
  279. Names[RTLIB::SYNC_FETCH_AND_AND_2] = "__sync_fetch_and_and_2";
  280. Names[RTLIB::SYNC_FETCH_AND_AND_4] = "__sync_fetch_and_and_4";
  281. Names[RTLIB::SYNC_FETCH_AND_AND_8] = "__sync_fetch_and_and_8";
  282. Names[RTLIB::SYNC_FETCH_AND_OR_1] = "__sync_fetch_and_or_1";
  283. Names[RTLIB::SYNC_FETCH_AND_OR_2] = "__sync_fetch_and_or_2";
  284. Names[RTLIB::SYNC_FETCH_AND_OR_4] = "__sync_fetch_and_or_4";
  285. Names[RTLIB::SYNC_FETCH_AND_OR_8] = "__sync_fetch_and_or_8";
  286. Names[RTLIB::SYNC_FETCH_AND_XOR_1] = "__sync_fetch_and_xor_1";
  287. Names[RTLIB::SYNC_FETCH_AND_XOR_2] = "__sync_fetch_and_xor_2";
  288. Names[RTLIB::SYNC_FETCH_AND_XOR_4] = "__sync_fetch_and_xor_4";
  289. Names[RTLIB::SYNC_FETCH_AND_XOR_8] = "__sync_fetch_and_xor_8";
  290. Names[RTLIB::SYNC_FETCH_AND_NAND_1] = "__sync_fetch_and_nand_1";
  291. Names[RTLIB::SYNC_FETCH_AND_NAND_2] = "__sync_fetch_and_nand_2";
  292. Names[RTLIB::SYNC_FETCH_AND_NAND_4] = "__sync_fetch_and_nand_4";
  293. Names[RTLIB::SYNC_FETCH_AND_NAND_8] = "__sync_fetch_and_nand_8";
  294. }
  295. /// InitLibcallCallingConvs - Set default libcall CallingConvs.
  296. ///
  297. static void InitLibcallCallingConvs(CallingConv::ID *CCs) {
  298. for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
  299. CCs[i] = CallingConv::C;
  300. }
  301. }
  302. /// getFPEXT - Return the FPEXT_*_* value for the given types, or
  303. /// UNKNOWN_LIBCALL if there is none.
  304. RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
  305. if (OpVT == MVT::f32) {
  306. if (RetVT == MVT::f64)
  307. return FPEXT_F32_F64;
  308. }
  309. return UNKNOWN_LIBCALL;
  310. }
  311. /// getFPROUND - Return the FPROUND_*_* value for the given types, or
  312. /// UNKNOWN_LIBCALL if there is none.
  313. RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
  314. if (RetVT == MVT::f32) {
  315. if (OpVT == MVT::f64)
  316. return FPROUND_F64_F32;
  317. if (OpVT == MVT::f80)
  318. return FPROUND_F80_F32;
  319. if (OpVT == MVT::ppcf128)
  320. return FPROUND_PPCF128_F32;
  321. } else if (RetVT == MVT::f64) {
  322. if (OpVT == MVT::f80)
  323. return FPROUND_F80_F64;
  324. if (OpVT == MVT::ppcf128)
  325. return FPROUND_PPCF128_F64;
  326. }
  327. return UNKNOWN_LIBCALL;
  328. }
  329. /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
  330. /// UNKNOWN_LIBCALL if there is none.
  331. RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
  332. if (OpVT == MVT::f32) {
  333. if (RetVT == MVT::i8)
  334. return FPTOSINT_F32_I8;
  335. if (RetVT == MVT::i16)
  336. return FPTOSINT_F32_I16;
  337. if (RetVT == MVT::i32)
  338. return FPTOSINT_F32_I32;
  339. if (RetVT == MVT::i64)
  340. return FPTOSINT_F32_I64;
  341. if (RetVT == MVT::i128)
  342. return FPTOSINT_F32_I128;
  343. } else if (OpVT == MVT::f64) {
  344. if (RetVT == MVT::i8)
  345. return FPTOSINT_F64_I8;
  346. if (RetVT == MVT::i16)
  347. return FPTOSINT_F64_I16;
  348. if (RetVT == MVT::i32)
  349. return FPTOSINT_F64_I32;
  350. if (RetVT == MVT::i64)
  351. return FPTOSINT_F64_I64;
  352. if (RetVT == MVT::i128)
  353. return FPTOSINT_F64_I128;
  354. } else if (OpVT == MVT::f80) {
  355. if (RetVT == MVT::i32)
  356. return FPTOSINT_F80_I32;
  357. if (RetVT == MVT::i64)
  358. return FPTOSINT_F80_I64;
  359. if (RetVT == MVT::i128)
  360. return FPTOSINT_F80_I128;
  361. } else if (OpVT == MVT::ppcf128) {
  362. if (RetVT == MVT::i32)
  363. return FPTOSINT_PPCF128_I32;
  364. if (RetVT == MVT::i64)
  365. return FPTOSINT_PPCF128_I64;
  366. if (RetVT == MVT::i128)
  367. return FPTOSINT_PPCF128_I128;
  368. }
  369. return UNKNOWN_LIBCALL;
  370. }
  371. /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
  372. /// UNKNOWN_LIBCALL if there is none.
  373. RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
  374. if (OpVT == MVT::f32) {
  375. if (RetVT == MVT::i8)
  376. return FPTOUINT_F32_I8;
  377. if (RetVT == MVT::i16)
  378. return FPTOUINT_F32_I16;
  379. if (RetVT == MVT::i32)
  380. return FPTOUINT_F32_I32;
  381. if (RetVT == MVT::i64)
  382. return FPTOUINT_F32_I64;
  383. if (RetVT == MVT::i128)
  384. return FPTOUINT_F32_I128;
  385. } else if (OpVT == MVT::f64) {
  386. if (RetVT == MVT::i8)
  387. return FPTOUINT_F64_I8;
  388. if (RetVT == MVT::i16)
  389. return FPTOUINT_F64_I16;
  390. if (RetVT == MVT::i32)
  391. return FPTOUINT_F64_I32;
  392. if (RetVT == MVT::i64)
  393. return FPTOUINT_F64_I64;
  394. if (RetVT == MVT::i128)
  395. return FPTOUINT_F64_I128;
  396. } else if (OpVT == MVT::f80) {
  397. if (RetVT == MVT::i32)
  398. return FPTOUINT_F80_I32;
  399. if (RetVT == MVT::i64)
  400. return FPTOUINT_F80_I64;
  401. if (RetVT == MVT::i128)
  402. return FPTOUINT_F80_I128;
  403. } else if (OpVT == MVT::ppcf128) {
  404. if (RetVT == MVT::i32)
  405. return FPTOUINT_PPCF128_I32;
  406. if (RetVT == MVT::i64)
  407. return FPTOUINT_PPCF128_I64;
  408. if (RetVT == MVT::i128)
  409. return FPTOUINT_PPCF128_I128;
  410. }
  411. return UNKNOWN_LIBCALL;
  412. }
  413. /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
  414. /// UNKNOWN_LIBCALL if there is none.
  415. RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
  416. if (OpVT == MVT::i32) {
  417. if (RetVT == MVT::f32)
  418. return SINTTOFP_I32_F32;
  419. if (RetVT == MVT::f64)
  420. return SINTTOFP_I32_F64;
  421. if (RetVT == MVT::f80)
  422. return SINTTOFP_I32_F80;
  423. if (RetVT == MVT::ppcf128)
  424. return SINTTOFP_I32_PPCF128;
  425. } else if (OpVT == MVT::i64) {
  426. if (RetVT == MVT::f32)
  427. return SINTTOFP_I64_F32;
  428. if (RetVT == MVT::f64)
  429. return SINTTOFP_I64_F64;
  430. if (RetVT == MVT::f80)
  431. return SINTTOFP_I64_F80;
  432. if (RetVT == MVT::ppcf128)
  433. return SINTTOFP_I64_PPCF128;
  434. } else if (OpVT == MVT::i128) {
  435. if (RetVT == MVT::f32)
  436. return SINTTOFP_I128_F32;
  437. if (RetVT == MVT::f64)
  438. return SINTTOFP_I128_F64;
  439. if (RetVT == MVT::f80)
  440. return SINTTOFP_I128_F80;
  441. if (RetVT == MVT::ppcf128)
  442. return SINTTOFP_I128_PPCF128;
  443. }
  444. return UNKNOWN_LIBCALL;
  445. }
  446. /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
  447. /// UNKNOWN_LIBCALL if there is none.
  448. RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
  449. if (OpVT == MVT::i32) {
  450. if (RetVT == MVT::f32)
  451. return UINTTOFP_I32_F32;
  452. if (RetVT == MVT::f64)
  453. return UINTTOFP_I32_F64;
  454. if (RetVT == MVT::f80)
  455. return UINTTOFP_I32_F80;
  456. if (RetVT == MVT::ppcf128)
  457. return UINTTOFP_I32_PPCF128;
  458. } else if (OpVT == MVT::i64) {
  459. if (RetVT == MVT::f32)
  460. return UINTTOFP_I64_F32;
  461. if (RetVT == MVT::f64)
  462. return UINTTOFP_I64_F64;
  463. if (RetVT == MVT::f80)
  464. return UINTTOFP_I64_F80;
  465. if (RetVT == MVT::ppcf128)
  466. return UINTTOFP_I64_PPCF128;
  467. } else if (OpVT == MVT::i128) {
  468. if (RetVT == MVT::f32)
  469. return UINTTOFP_I128_F32;
  470. if (RetVT == MVT::f64)
  471. return UINTTOFP_I128_F64;
  472. if (RetVT == MVT::f80)
  473. return UINTTOFP_I128_F80;
  474. if (RetVT == MVT::ppcf128)
  475. return UINTTOFP_I128_PPCF128;
  476. }
  477. return UNKNOWN_LIBCALL;
  478. }
  479. /// InitCmpLibcallCCs - Set default comparison libcall CC.
  480. ///
  481. static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
  482. memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
  483. CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
  484. CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
  485. CCs[RTLIB::UNE_F32] = ISD::SETNE;
  486. CCs[RTLIB::UNE_F64] = ISD::SETNE;
  487. CCs[RTLIB::OGE_F32] = ISD::SETGE;
  488. CCs[RTLIB::OGE_F64] = ISD::SETGE;
  489. CCs[RTLIB::OLT_F32] = ISD::SETLT;
  490. CCs[RTLIB::OLT_F64] = ISD::SETLT;
  491. CCs[RTLIB::OLE_F32] = ISD::SETLE;
  492. CCs[RTLIB::OLE_F64] = ISD::SETLE;
  493. CCs[RTLIB::OGT_F32] = ISD::SETGT;
  494. CCs[RTLIB::OGT_F64] = ISD::SETGT;
  495. CCs[RTLIB::UO_F32] = ISD::SETNE;
  496. CCs[RTLIB::UO_F64] = ISD::SETNE;
  497. CCs[RTLIB::O_F32] = ISD::SETEQ;
  498. CCs[RTLIB::O_F64] = ISD::SETEQ;
  499. }
  500. /// NOTE: The constructor takes ownership of TLOF.
  501. TargetLowering::TargetLowering(const TargetMachine &tm,
  502. const TargetLoweringObjectFile *tlof)
  503. : TM(tm), TD(TM.getDataLayout()), TLOF(*tlof) {
  504. // All operations default to being supported.
  505. memset(OpActions, 0, sizeof(OpActions));
  506. memset(LoadExtActions, 0, sizeof(LoadExtActions));
  507. memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
  508. memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
  509. memset(CondCodeActions, 0, sizeof(CondCodeActions));
  510. // Set default actions for various operations.
  511. for (unsigned VT = 0; VT != (unsigned)MVT::LAST_VALUETYPE; ++VT) {
  512. // Default all indexed load / store to expand.
  513. for (unsigned IM = (unsigned)ISD::PRE_INC;
  514. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  515. setIndexedLoadAction(IM, (MVT::SimpleValueType)VT, Expand);
  516. setIndexedStoreAction(IM, (MVT::SimpleValueType)VT, Expand);
  517. }
  518. // These operations default to expand.
  519. setOperationAction(ISD::FGETSIGN, (MVT::SimpleValueType)VT, Expand);
  520. setOperationAction(ISD::CONCAT_VECTORS, (MVT::SimpleValueType)VT, Expand);
  521. }
  522. // Most targets ignore the @llvm.prefetch intrinsic.
  523. setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
  524. // ConstantFP nodes default to expand. Targets can either change this to
  525. // Legal, in which case all fp constants are legal, or use isFPImmLegal()
  526. // to optimize expansions for certain constants.
  527. setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
  528. setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
  529. setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
  530. setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
  531. // These library functions default to expand.
  532. setOperationAction(ISD::FLOG , MVT::f16, Expand);
  533. setOperationAction(ISD::FLOG2, MVT::f16, Expand);
  534. setOperationAction(ISD::FLOG10, MVT::f16, Expand);
  535. setOperationAction(ISD::FEXP , MVT::f16, Expand);
  536. setOperationAction(ISD::FEXP2, MVT::f16, Expand);
  537. setOperationAction(ISD::FFLOOR, MVT::f16, Expand);
  538. setOperationAction(ISD::FNEARBYINT, MVT::f16, Expand);
  539. setOperationAction(ISD::FCEIL, MVT::f16, Expand);
  540. setOperationAction(ISD::FRINT, MVT::f16, Expand);
  541. setOperationAction(ISD::FTRUNC, MVT::f16, Expand);
  542. setOperationAction(ISD::FLOG , MVT::f32, Expand);
  543. setOperationAction(ISD::FLOG2, MVT::f32, Expand);
  544. setOperationAction(ISD::FLOG10, MVT::f32, Expand);
  545. setOperationAction(ISD::FEXP , MVT::f32, Expand);
  546. setOperationAction(ISD::FEXP2, MVT::f32, Expand);
  547. setOperationAction(ISD::FFLOOR, MVT::f32, Expand);
  548. setOperationAction(ISD::FNEARBYINT, MVT::f32, Expand);
  549. setOperationAction(ISD::FCEIL, MVT::f32, Expand);
  550. setOperationAction(ISD::FRINT, MVT::f32, Expand);
  551. setOperationAction(ISD::FTRUNC, MVT::f32, Expand);
  552. setOperationAction(ISD::FLOG , MVT::f64, Expand);
  553. setOperationAction(ISD::FLOG2, MVT::f64, Expand);
  554. setOperationAction(ISD::FLOG10, MVT::f64, Expand);
  555. setOperationAction(ISD::FEXP , MVT::f64, Expand);
  556. setOperationAction(ISD::FEXP2, MVT::f64, Expand);
  557. setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
  558. setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
  559. setOperationAction(ISD::FCEIL, MVT::f64, Expand);
  560. setOperationAction(ISD::FRINT, MVT::f64, Expand);
  561. setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
  562. // Default ISD::TRAP to expand (which turns it into abort).
  563. setOperationAction(ISD::TRAP, MVT::Other, Expand);
  564. // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
  565. // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
  566. //
  567. setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
  568. IsLittleEndian = TD->isLittleEndian();
  569. PointerTy = MVT::getIntegerVT(8*TD->getPointerSize(0));
  570. memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
  571. memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
  572. maxStoresPerMemset = maxStoresPerMemcpy = maxStoresPerMemmove = 8;
  573. maxStoresPerMemsetOptSize = maxStoresPerMemcpyOptSize
  574. = maxStoresPerMemmoveOptSize = 4;
  575. benefitFromCodePlacementOpt = false;
  576. UseUnderscoreSetJmp = false;
  577. UseUnderscoreLongJmp = false;
  578. SelectIsExpensive = false;
  579. IntDivIsCheap = false;
  580. Pow2DivIsCheap = false;
  581. JumpIsExpensive = false;
  582. predictableSelectIsExpensive = false;
  583. StackPointerRegisterToSaveRestore = 0;
  584. ExceptionPointerRegister = 0;
  585. ExceptionSelectorRegister = 0;
  586. BooleanContents = UndefinedBooleanContent;
  587. BooleanVectorContents = UndefinedBooleanContent;
  588. SchedPreferenceInfo = Sched::ILP;
  589. JumpBufSize = 0;
  590. JumpBufAlignment = 0;
  591. MinFunctionAlignment = 0;
  592. PrefFunctionAlignment = 0;
  593. PrefLoopAlignment = 0;
  594. MinStackArgumentAlignment = 1;
  595. ShouldFoldAtomicFences = false;
  596. InsertFencesForAtomic = false;
  597. SupportJumpTables = true;
  598. MinimumJumpTableEntries = 4;
  599. InitLibcallNames(LibcallRoutineNames);
  600. InitCmpLibcallCCs(CmpLibcallCCs);
  601. InitLibcallCallingConvs(LibcallCallingConvs);
  602. }
  603. TargetLowering::~TargetLowering() {
  604. delete &TLOF;
  605. }
  606. MVT TargetLowering::getShiftAmountTy(EVT LHSTy) const {
  607. return MVT::getIntegerVT(8*TD->getPointerSize(0));
  608. }
  609. /// canOpTrap - Returns true if the operation can trap for the value type.
  610. /// VT must be a legal type.
  611. bool TargetLowering::canOpTrap(unsigned Op, EVT VT) const {
  612. assert(isTypeLegal(VT));
  613. switch (Op) {
  614. default:
  615. return false;
  616. case ISD::FDIV:
  617. case ISD::FREM:
  618. case ISD::SDIV:
  619. case ISD::UDIV:
  620. case ISD::SREM:
  621. case ISD::UREM:
  622. return true;
  623. }
  624. }
  625. static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
  626. unsigned &NumIntermediates,
  627. EVT &RegisterVT,
  628. TargetLowering *TLI) {
  629. // Figure out the right, legal destination reg to copy into.
  630. unsigned NumElts = VT.getVectorNumElements();
  631. MVT EltTy = VT.getVectorElementType();
  632. unsigned NumVectorRegs = 1;
  633. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  634. // could break down into LHS/RHS like LegalizeDAG does.
  635. if (!isPowerOf2_32(NumElts)) {
  636. NumVectorRegs = NumElts;
  637. NumElts = 1;
  638. }
  639. // Divide the input until we get to a supported size. This will always
  640. // end with a scalar if the target doesn't support vectors.
  641. while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
  642. NumElts >>= 1;
  643. NumVectorRegs <<= 1;
  644. }
  645. NumIntermediates = NumVectorRegs;
  646. MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
  647. if (!TLI->isTypeLegal(NewVT))
  648. NewVT = EltTy;
  649. IntermediateVT = NewVT;
  650. unsigned NewVTSize = NewVT.getSizeInBits();
  651. // Convert sizes such as i33 to i64.
  652. if (!isPowerOf2_32(NewVTSize))
  653. NewVTSize = NextPowerOf2(NewVTSize);
  654. EVT DestVT = TLI->getRegisterType(NewVT);
  655. RegisterVT = DestVT;
  656. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  657. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  658. // Otherwise, promotion or legal types use the same number of registers as
  659. // the vector decimated to the appropriate level.
  660. return NumVectorRegs;
  661. }
  662. /// isLegalRC - Return true if the value types that can be represented by the
  663. /// specified register class are all legal.
  664. bool TargetLowering::isLegalRC(const TargetRegisterClass *RC) const {
  665. for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
  666. I != E; ++I) {
  667. if (isTypeLegal(*I))
  668. return true;
  669. }
  670. return false;
  671. }
  672. /// findRepresentativeClass - Return the largest legal super-reg register class
  673. /// of the register class for the specified type and its associated "cost".
  674. std::pair<const TargetRegisterClass*, uint8_t>
  675. TargetLowering::findRepresentativeClass(EVT VT) const {
  676. const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
  677. const TargetRegisterClass *RC = RegClassForVT[VT.getSimpleVT().SimpleTy];
  678. if (!RC)
  679. return std::make_pair(RC, 0);
  680. // Compute the set of all super-register classes.
  681. BitVector SuperRegRC(TRI->getNumRegClasses());
  682. for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
  683. SuperRegRC.setBitsInMask(RCI.getMask());
  684. // Find the first legal register class with the largest spill size.
  685. const TargetRegisterClass *BestRC = RC;
  686. for (int i = SuperRegRC.find_first(); i >= 0; i = SuperRegRC.find_next(i)) {
  687. const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
  688. // We want the largest possible spill size.
  689. if (SuperRC->getSize() <= BestRC->getSize())
  690. continue;
  691. if (!isLegalRC(SuperRC))
  692. continue;
  693. BestRC = SuperRC;
  694. }
  695. return std::make_pair(BestRC, 1);
  696. }
  697. /// computeRegisterProperties - Once all of the register classes are added,
  698. /// this allows us to compute derived properties we expose.
  699. void TargetLowering::computeRegisterProperties() {
  700. assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE &&
  701. "Too many value types for ValueTypeActions to hold!");
  702. // Everything defaults to needing one register.
  703. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  704. NumRegistersForVT[i] = 1;
  705. RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
  706. }
  707. // ...except isVoid, which doesn't need any registers.
  708. NumRegistersForVT[MVT::isVoid] = 0;
  709. // Find the largest integer register class.
  710. unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
  711. for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
  712. assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
  713. // Every integer value type larger than this largest register takes twice as
  714. // many registers to represent as the previous ValueType.
  715. for (unsigned ExpandedReg = LargestIntReg + 1;
  716. ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
  717. NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
  718. RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
  719. TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
  720. ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
  721. TypeExpandInteger);
  722. }
  723. // Inspect all of the ValueType's smaller than the largest integer
  724. // register to see which ones need promotion.
  725. unsigned LegalIntReg = LargestIntReg;
  726. for (unsigned IntReg = LargestIntReg - 1;
  727. IntReg >= (unsigned)MVT::i1; --IntReg) {
  728. MVT IVT = (MVT::SimpleValueType)IntReg;
  729. if (isTypeLegal(IVT)) {
  730. LegalIntReg = IntReg;
  731. } else {
  732. RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
  733. (const MVT::SimpleValueType)LegalIntReg;
  734. ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
  735. }
  736. }
  737. // ppcf128 type is really two f64's.
  738. if (!isTypeLegal(MVT::ppcf128)) {
  739. NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
  740. RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
  741. TransformToType[MVT::ppcf128] = MVT::f64;
  742. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
  743. }
  744. // Decide how to handle f64. If the target does not have native f64 support,
  745. // expand it to i64 and we will be generating soft float library calls.
  746. if (!isTypeLegal(MVT::f64)) {
  747. NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
  748. RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
  749. TransformToType[MVT::f64] = MVT::i64;
  750. ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
  751. }
  752. // Decide how to handle f32. If the target does not have native support for
  753. // f32, promote it to f64 if it is legal. Otherwise, expand it to i32.
  754. if (!isTypeLegal(MVT::f32)) {
  755. if (isTypeLegal(MVT::f64)) {
  756. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::f64];
  757. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::f64];
  758. TransformToType[MVT::f32] = MVT::f64;
  759. ValueTypeActions.setTypeAction(MVT::f32, TypePromoteInteger);
  760. } else {
  761. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
  762. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
  763. TransformToType[MVT::f32] = MVT::i32;
  764. ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
  765. }
  766. }
  767. // Loop over all of the vector value types to see which need transformations.
  768. for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
  769. i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
  770. MVT VT = (MVT::SimpleValueType)i;
  771. if (isTypeLegal(VT)) continue;
  772. // Determine if there is a legal wider type. If so, we should promote to
  773. // that wider vector type.
  774. EVT EltVT = VT.getVectorElementType();
  775. unsigned NElts = VT.getVectorNumElements();
  776. if (NElts != 1 && !shouldSplitVectorElementType(EltVT)) {
  777. bool IsLegalWiderType = false;
  778. // First try to promote the elements of integer vectors. If no legal
  779. // promotion was found, fallback to the widen-vector method.
  780. for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  781. EVT SVT = (MVT::SimpleValueType)nVT;
  782. // Promote vectors of integers to vectors with the same number
  783. // of elements, with a wider element type.
  784. if (SVT.getVectorElementType().getSizeInBits() > EltVT.getSizeInBits()
  785. && SVT.getVectorNumElements() == NElts &&
  786. isTypeLegal(SVT) && SVT.getScalarType().isInteger()) {
  787. TransformToType[i] = SVT;
  788. RegisterTypeForVT[i] = SVT;
  789. NumRegistersForVT[i] = 1;
  790. ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
  791. IsLegalWiderType = true;
  792. break;
  793. }
  794. }
  795. if (IsLegalWiderType) continue;
  796. // Try to widen the vector.
  797. for (unsigned nVT = i+1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  798. EVT SVT = (MVT::SimpleValueType)nVT;
  799. if (SVT.getVectorElementType() == EltVT &&
  800. SVT.getVectorNumElements() > NElts &&
  801. isTypeLegal(SVT)) {
  802. TransformToType[i] = SVT;
  803. RegisterTypeForVT[i] = SVT;
  804. NumRegistersForVT[i] = 1;
  805. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  806. IsLegalWiderType = true;
  807. break;
  808. }
  809. }
  810. if (IsLegalWiderType) continue;
  811. }
  812. MVT IntermediateVT;
  813. EVT RegisterVT;
  814. unsigned NumIntermediates;
  815. NumRegistersForVT[i] =
  816. getVectorTypeBreakdownMVT(VT, IntermediateVT, NumIntermediates,
  817. RegisterVT, this);
  818. RegisterTypeForVT[i] = RegisterVT;
  819. EVT NVT = VT.getPow2VectorType();
  820. if (NVT == VT) {
  821. // Type is already a power of 2. The default action is to split.
  822. TransformToType[i] = MVT::Other;
  823. unsigned NumElts = VT.getVectorNumElements();
  824. ValueTypeActions.setTypeAction(VT,
  825. NumElts > 1 ? TypeSplitVector : TypeScalarizeVector);
  826. } else {
  827. TransformToType[i] = NVT;
  828. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  829. }
  830. }
  831. // Determine the 'representative' register class for each value type.
  832. // An representative register class is the largest (meaning one which is
  833. // not a sub-register class / subreg register class) legal register class for
  834. // a group of value types. For example, on i386, i8, i16, and i32
  835. // representative would be GR32; while on x86_64 it's GR64.
  836. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  837. const TargetRegisterClass* RRC;
  838. uint8_t Cost;
  839. tie(RRC, Cost) = findRepresentativeClass((MVT::SimpleValueType)i);
  840. RepRegClassForVT[i] = RRC;
  841. RepRegClassCostForVT[i] = Cost;
  842. }
  843. }
  844. const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
  845. return NULL;
  846. }
  847. EVT TargetLowering::getSetCCResultType(EVT VT) const {
  848. assert(!VT.isVector() && "No default SetCC type for vectors!");
  849. return getPointerTy(0).SimpleTy;
  850. }
  851. MVT::SimpleValueType TargetLowering::getCmpLibcallReturnType() const {
  852. return MVT::i32; // return the default value
  853. }
  854. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  855. /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
  856. /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
  857. /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
  858. ///
  859. /// This method returns the number of registers needed, and the VT for each
  860. /// register. It also returns the VT and quantity of the intermediate values
  861. /// before they are promoted/expanded.
  862. ///
  863. unsigned TargetLowering::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
  864. EVT &IntermediateVT,
  865. unsigned &NumIntermediates,
  866. EVT &RegisterVT) const {
  867. unsigned NumElts = VT.getVectorNumElements();
  868. // If there is a wider vector type with the same element type as this one,
  869. // or a promoted vector type that has the same number of elements which
  870. // are wider, then we should convert to that legal vector type.
  871. // This handles things like <2 x float> -> <4 x float> and
  872. // <4 x i1> -> <4 x i32>.
  873. LegalizeTypeAction TA = getTypeAction(Context, VT);
  874. if (NumElts != 1 && (TA == TypeWidenVector || TA == TypePromoteInteger)) {
  875. RegisterVT = getTypeToTransformTo(Context, VT);
  876. if (isTypeLegal(RegisterVT)) {
  877. IntermediateVT = RegisterVT;
  878. NumIntermediates = 1;
  879. return 1;
  880. }
  881. }
  882. // Figure out the right, legal destination reg to copy into.
  883. EVT EltTy = VT.getVectorElementType();
  884. unsigned NumVectorRegs = 1;
  885. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  886. // could break down into LHS/RHS like LegalizeDAG does.
  887. if (!isPowerOf2_32(NumElts)) {
  888. NumVectorRegs = NumElts;
  889. NumElts = 1;
  890. }
  891. // Divide the input until we get to a supported size. This will always
  892. // end with a scalar if the target doesn't support vectors.
  893. while (NumElts > 1 && !isTypeLegal(
  894. EVT::getVectorVT(Context, EltTy, NumElts))) {
  895. NumElts >>= 1;
  896. NumVectorRegs <<= 1;
  897. }
  898. NumIntermediates = NumVectorRegs;
  899. EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
  900. if (!isTypeLegal(NewVT))
  901. NewVT = EltTy;
  902. IntermediateVT = NewVT;
  903. EVT DestVT = getRegisterType(Context, NewVT);
  904. RegisterVT = DestVT;
  905. unsigned NewVTSize = NewVT.getSizeInBits();
  906. // Convert sizes such as i33 to i64.
  907. if (!isPowerOf2_32(NewVTSize))
  908. NewVTSize = NextPowerOf2(NewVTSize);
  909. if (DestVT.bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  910. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  911. // Otherwise, promotion or legal types use the same number of registers as
  912. // the vector decimated to the appropriate level.
  913. return NumVectorRegs;
  914. }
  915. /// Get the EVTs and ArgFlags collections that represent the legalized return
  916. /// type of the given function. This does not require a DAG or a return value,
  917. /// and is suitable for use before any DAGs for the function are constructed.
  918. /// TODO: Move this out of TargetLowering.cpp.
  919. void llvm::GetReturnInfo(Type* ReturnType, Attribute attr,
  920. SmallVectorImpl<ISD::OutputArg> &Outs,
  921. const TargetLowering &TLI) {
  922. SmallVector<EVT, 4> ValueVTs;
  923. ComputeValueVTs(TLI, ReturnType, ValueVTs);
  924. unsigned NumValues = ValueVTs.size();
  925. if (NumValues == 0) return;
  926. for (unsigned j = 0, f = NumValues; j != f; ++j) {
  927. EVT VT = ValueVTs[j];
  928. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  929. if (attr.hasAttribute(Attribute::SExt))
  930. ExtendKind = ISD::SIGN_EXTEND;
  931. else if (attr.hasAttribute(Attribute::ZExt))
  932. ExtendKind = ISD::ZERO_EXTEND;
  933. // FIXME: C calling convention requires the return type to be promoted to
  934. // at least 32-bit. But this is not necessary for non-C calling
  935. // conventions. The frontend should mark functions whose return values
  936. // require promoting with signext or zeroext attributes.
  937. if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
  938. EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
  939. if (VT.bitsLT(MinVT))
  940. VT = MinVT;
  941. }
  942. unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
  943. EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
  944. // 'inreg' on function refers to return value
  945. ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
  946. if (attr.hasAttribute(Attribute::InReg))
  947. Flags.setInReg();
  948. // Propagate extension type if any
  949. if (attr.hasAttribute(Attribute::SExt))
  950. Flags.setSExt();
  951. else if (attr.hasAttribute(Attribute::ZExt))
  952. Flags.setZExt();
  953. for (unsigned i = 0; i < NumParts; ++i)
  954. Outs.push_back(ISD::OutputArg(Flags, PartVT, /*isFixed=*/true, 0, 0));
  955. }
  956. }
  957. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  958. /// function arguments in the caller parameter area. This is the actual
  959. /// alignment, not its logarithm.
  960. unsigned TargetLowering::getByValTypeAlignment(Type *Ty) const {
  961. return TD->getCallFrameTypeAlignment(Ty);
  962. }
  963. /// getJumpTableEncoding - Return the entry encoding for a jump table in the
  964. /// current function. The returned value is a member of the
  965. /// MachineJumpTableInfo::JTEntryKind enum.
  966. unsigned TargetLowering::getJumpTableEncoding() const {
  967. // In non-pic modes, just use the address of a block.
  968. if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
  969. return MachineJumpTableInfo::EK_BlockAddress;
  970. // In PIC mode, if the target supports a GPRel32 directive, use it.
  971. if (getTargetMachine().getMCAsmInfo()->getGPRel32Directive() != 0)
  972. return MachineJumpTableInfo::EK_GPRel32BlockAddress;
  973. // Otherwise, use a label difference.
  974. return MachineJumpTableInfo::EK_LabelDifference32;
  975. }
  976. SDValue TargetLowering::getPICJumpTableRelocBase(SDValue Table,
  977. SelectionDAG &DAG) const {
  978. // If our PIC model is GP relative, use the global offset table as the base.
  979. unsigned JTEncoding = getJumpTableEncoding();
  980. if ((JTEncoding == MachineJumpTableInfo::EK_GPRel64BlockAddress) ||
  981. (JTEncoding == MachineJumpTableInfo::EK_GPRel32BlockAddress))
  982. return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy(0));
  983. return Table;
  984. }
  985. /// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
  986. /// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
  987. /// MCExpr.
  988. const MCExpr *
  989. TargetLowering::getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
  990. unsigned JTI,MCContext &Ctx) const{
  991. // The normal PIC reloc base is the label at the start of the jump table.
  992. return MCSymbolRefExpr::Create(MF->getJTISymbol(JTI, Ctx), Ctx);
  993. }
  994. bool
  995. TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
  996. // Assume that everything is safe in static mode.
  997. if (getTargetMachine().getRelocationModel() == Reloc::Static)
  998. return true;
  999. // In dynamic-no-pic mode, assume that known defined values are safe.
  1000. if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC &&
  1001. GA &&
  1002. !GA->getGlobal()->isDeclaration() &&
  1003. !GA->getGlobal()->isWeakForLinker())
  1004. return true;
  1005. // Otherwise assume nothing is safe.
  1006. return false;
  1007. }
  1008. //===----------------------------------------------------------------------===//
  1009. // Optimization Methods
  1010. //===----------------------------------------------------------------------===//
  1011. /// ShrinkDemandedConstant - Check to see if the specified operand of the
  1012. /// specified instruction is a constant integer. If so, check to see if there
  1013. /// are any bits set in the constant that are not demanded. If so, shrink the
  1014. /// constant and return true.
  1015. bool TargetLowering::TargetLoweringOpt::ShrinkDemandedConstant(SDValue Op,
  1016. const APInt &Demanded) {
  1017. DebugLoc dl = Op.getDebugLoc();
  1018. // FIXME: ISD::SELECT, ISD::SELECT_CC
  1019. switch (Op.getOpcode()) {
  1020. default: break;
  1021. case ISD::XOR:
  1022. case ISD::AND:
  1023. case ISD::OR: {
  1024. ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  1025. if (!C) return false;
  1026. if (Op.getOpcode() == ISD::XOR &&
  1027. (C->getAPIntValue() | (~Demanded)).isAllOnesValue())
  1028. return false;
  1029. // if we can expand it to have all bits set, do it
  1030. if (C->getAPIntValue().intersects(~Demanded)) {
  1031. EVT VT = Op.getValueType();
  1032. SDValue New = DAG.getNode(Op.getOpcode(), dl, VT, Op.getOperand(0),
  1033. DAG.getConstant(Demanded &
  1034. C->getAPIntValue(),
  1035. VT));
  1036. return CombineTo(Op, New);
  1037. }
  1038. break;
  1039. }
  1040. }
  1041. return false;
  1042. }
  1043. /// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
  1044. /// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
  1045. /// cast, but it could be generalized for targets with other types of
  1046. /// implicit widening casts.
  1047. bool
  1048. TargetLowering::TargetLoweringOpt::ShrinkDemandedOp(SDValue Op,
  1049. unsigned BitWidth,
  1050. const APInt &Demanded,
  1051. DebugLoc dl) {
  1052. assert(Op.getNumOperands() == 2 &&
  1053. "ShrinkDemandedOp only supports binary operators!");
  1054. assert(Op.getNode()->getNumValues() == 1 &&
  1055. "ShrinkDemandedOp only supports nodes with one result!");
  1056. // Don't do this if the node has another user, which may require the
  1057. // full value.
  1058. if (!Op.getNode()->hasOneUse())
  1059. return false;
  1060. // Search for the smallest integer type with free casts to and from
  1061. // Op's type. For expedience, just check power-of-2 integer types.
  1062. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  1063. unsigned SmallVTBits = BitWidth - Demanded.countLeadingZeros();
  1064. if (!isPowerOf2_32(SmallVTBits))
  1065. SmallVTBits = NextPowerOf2(SmallVTBits);
  1066. for (; SmallVTBits < BitWidth; SmallVTBits = NextPowerOf2(SmallVTBits)) {
  1067. EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), SmallVTBits);
  1068. if (TLI.isTruncateFree(Op.getValueType(), SmallVT) &&
  1069. TLI.isZExtFree(SmallVT, Op.getValueType())) {
  1070. // We found a type with free casts.
  1071. SDValue X = DAG.getNode(Op.getOpcode(), dl, SmallVT,
  1072. DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
  1073. Op.getNode()->getOperand(0)),
  1074. DAG.getNode(ISD::TRUNCATE, dl, SmallVT,
  1075. Op.getNode()->getOperand(1)));
  1076. SDValue Z = DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), X);
  1077. return CombineTo(Op, Z);
  1078. }
  1079. }
  1080. return false;
  1081. }
  1082. /// SimplifyDemandedBits - Look at Op. At this point, we know that only the
  1083. /// DemandedMask bits of the result of Op are ever used downstream. If we can
  1084. /// use this information to simplify Op, create a new simplified DAG node and
  1085. /// return true, returning the original and new nodes in Old and New. Otherwise,
  1086. /// analyze the expression and return a mask of KnownOne and KnownZero bits for
  1087. /// the expression (used to simplify the caller). The KnownZero/One bits may
  1088. /// only be accurate for those bits in the DemandedMask.
  1089. bool TargetLowering::SimplifyDemandedBits(SDValue Op,
  1090. const APInt &DemandedMask,
  1091. APInt &KnownZero,
  1092. APInt &KnownOne,
  1093. TargetLoweringOpt &TLO,
  1094. unsigned Depth) const {
  1095. unsigned BitWidth = DemandedMask.getBitWidth();
  1096. assert(Op.getValueType().getScalarType().getSizeInBits() == BitWidth &&
  1097. "Mask size mismatches value type size!");
  1098. APInt NewMask = DemandedMask;
  1099. DebugLoc dl = Op.getDebugLoc();
  1100. // Don't know anything.
  1101. KnownZero = KnownOne = APInt(BitWidth, 0);
  1102. // Other users may use these bits.
  1103. if (!Op.getNode()->hasOneUse()) {
  1104. if (Depth != 0) {
  1105. // If not at the root, Just compute the KnownZero/KnownOne bits to
  1106. // simplify things downstream.
  1107. TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
  1108. return false;
  1109. }
  1110. // If this is the root being simplified, allow it to have multiple uses,
  1111. // just set the NewMask to all bits.
  1112. NewMask = APInt::getAllOnesValue(BitWidth);
  1113. } else if (DemandedMask == 0) {
  1114. // Not demanding any bits from Op.
  1115. if (Op.getOpcode() != ISD::UNDEF)
  1116. return TLO.CombineTo(Op, TLO.DAG.getUNDEF(Op.getValueType()));
  1117. return false;
  1118. } else if (Depth == 6) { // Limit search depth.
  1119. return false;
  1120. }
  1121. APInt KnownZero2, KnownOne2, KnownZeroOut, KnownOneOut;
  1122. switch (Op.getOpcode()) {
  1123. case ISD::Constant:
  1124. // We know all of the bits for a constant!
  1125. KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue();
  1126. KnownZero = ~KnownOne;
  1127. return false; // Don't fall through, will infinitely loop.
  1128. case ISD::AND:
  1129. // If the RHS is a constant, check to see if the LHS would be zero without
  1130. // using the bits from the RHS. Below, we use knowledge about the RHS to
  1131. // simplify the LHS, here we're using information from the LHS to simplify
  1132. // the RHS.
  1133. if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1134. APInt LHSZero, LHSOne;
  1135. // Do not increment Depth here; that can cause an infinite loop.
  1136. TLO.DAG.ComputeMaskedBits(Op.getOperand(0), LHSZero, LHSOne, Depth);
  1137. // If the LHS already has zeros where RHSC does, this and is dead.
  1138. if ((LHSZero & NewMask) == (~RHSC->getAPIntValue() & NewMask))
  1139. return TLO.CombineTo(Op, Op.getOperand(0));
  1140. // If any of the set bits in the RHS are known zero on the LHS, shrink
  1141. // the constant.
  1142. if (TLO.ShrinkDemandedConstant(Op, ~LHSZero & NewMask))
  1143. return true;
  1144. }
  1145. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  1146. KnownOne, TLO, Depth+1))
  1147. return true;
  1148. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1149. if (SimplifyDemandedBits(Op.getOperand(0), ~KnownZero & NewMask,
  1150. KnownZero2, KnownOne2, TLO, Depth+1))
  1151. return true;
  1152. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1153. // If all of the demanded bits are known one on one side, return the other.
  1154. // These bits cannot contribute to the result of the 'and'.
  1155. if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
  1156. return TLO.CombineTo(Op, Op.getOperand(0));
  1157. if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
  1158. return TLO.CombineTo(Op, Op.getOperand(1));
  1159. // If all of the demanded bits in the inputs are known zeros, return zero.
  1160. if ((NewMask & (KnownZero|KnownZero2)) == NewMask)
  1161. return TLO.CombineTo(Op, TLO.DAG.getConstant(0, Op.getValueType()));
  1162. // If the RHS is a constant, see if we can simplify it.
  1163. if (TLO.ShrinkDemandedConstant(Op, ~KnownZero2 & NewMask))
  1164. return true;
  1165. // If the operation can be done in a smaller type, do so.
  1166. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  1167. return true;
  1168. // Output known-1 bits are only known if set in both the LHS & RHS.
  1169. KnownOne &= KnownOne2;
  1170. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  1171. KnownZero |= KnownZero2;
  1172. break;
  1173. case ISD::OR:
  1174. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  1175. KnownOne, TLO, Depth+1))
  1176. return true;
  1177. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1178. if (SimplifyDemandedBits(Op.getOperand(0), ~KnownOne & NewMask,
  1179. KnownZero2, KnownOne2, TLO, Depth+1))
  1180. return true;
  1181. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1182. // If all of the demanded bits are known zero on one side, return the other.
  1183. // These bits cannot contribute to the result of the 'or'.
  1184. if ((NewMask & ~KnownOne2 & KnownZero) == (~KnownOne2 & NewMask))
  1185. return TLO.CombineTo(Op, Op.getOperand(0));
  1186. if ((NewMask & ~KnownOne & KnownZero2) == (~KnownOne & NewMask))
  1187. return TLO.CombineTo(Op, Op.getOperand(1));
  1188. // If all of the potentially set bits on one side are known to be set on
  1189. // the other side, just use the 'other' side.
  1190. if ((NewMask & ~KnownZero & KnownOne2) == (~KnownZero & NewMask))
  1191. return TLO.CombineTo(Op, Op.getOperand(0));
  1192. if ((NewMask & ~KnownZero2 & KnownOne) == (~KnownZero2 & NewMask))
  1193. return TLO.CombineTo(Op, Op.getOperand(1));
  1194. // If the RHS is a constant, see if we can simplify it.
  1195. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  1196. return true;
  1197. // If the operation can be done in a smaller type, do so.
  1198. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  1199. return true;
  1200. // Output known-0 bits are only known if clear in both the LHS & RHS.
  1201. KnownZero &= KnownZero2;
  1202. // Output known-1 are known to be set if set in either the LHS | RHS.
  1203. KnownOne |= KnownOne2;
  1204. break;
  1205. case ISD::XOR:
  1206. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero,
  1207. KnownOne, TLO, Depth+1))
  1208. return true;
  1209. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1210. if (SimplifyDemandedBits(Op.getOperand(0), NewMask, KnownZero2,
  1211. KnownOne2, TLO, Depth+1))
  1212. return true;
  1213. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1214. // If all of the demanded bits are known zero on one side, return the other.
  1215. // These bits cannot contribute to the result of the 'xor'.
  1216. if ((KnownZero & NewMask) == NewMask)
  1217. return TLO.CombineTo(Op, Op.getOperand(0));
  1218. if ((KnownZero2 & NewMask) == NewMask)
  1219. return TLO.CombineTo(Op, Op.getOperand(1));
  1220. // If the operation can be done in a smaller type, do so.
  1221. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  1222. return true;
  1223. // If all of the unknown bits are known to be zero on one side or the other
  1224. // (but not both) turn this into an *inclusive* or.
  1225. // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
  1226. if ((NewMask & ~KnownZero & ~KnownZero2) == 0)
  1227. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, Op.getValueType(),
  1228. Op.getOperand(0),
  1229. Op.getOperand(1)));
  1230. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  1231. KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
  1232. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  1233. KnownOneOut = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
  1234. // If all of the demanded bits on one side are known, and all of the set
  1235. // bits on that side are also known to be set on the other side, turn this
  1236. // into an AND, as we know the bits will be cleared.
  1237. // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
  1238. // NB: it is okay if more bits are known than are requested
  1239. if ((NewMask & (KnownZero|KnownOne)) == NewMask) { // all known on one side
  1240. if (KnownOne == KnownOne2) { // set bits are the same on both sides
  1241. EVT VT = Op.getValueType();
  1242. SDValue ANDC = TLO.DAG.getConstant(~KnownOne & NewMask, VT);
  1243. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT,
  1244. Op.getOperand(0), ANDC));
  1245. }
  1246. }
  1247. // If the RHS is a constant, see if we can simplify it.
  1248. // for XOR, we prefer to force bits to 1 if they will make a -1.
  1249. // if we can't force bits, try to shrink constant
  1250. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1251. APInt Expanded = C->getAPIntValue() | (~NewMask);
  1252. // if we can expand it to have all bits set, do it
  1253. if (Expanded.isAllOnesValue()) {
  1254. if (Expanded != C->getAPIntValue()) {
  1255. EVT VT = Op.getValueType();
  1256. SDValue New = TLO.DAG.getNode(Op.getOpcode(), dl,VT, Op.getOperand(0),
  1257. TLO.DAG.getConstant(Expanded, VT));
  1258. return TLO.CombineTo(Op, New);
  1259. }
  1260. // if it already has all the bits set, nothing to change
  1261. // but don't shrink either!
  1262. } else if (TLO.ShrinkDemandedConstant(Op, NewMask)) {
  1263. return true;
  1264. }
  1265. }
  1266. KnownZero = KnownZeroOut;
  1267. KnownOne = KnownOneOut;
  1268. break;
  1269. case ISD::SELECT:
  1270. if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero,
  1271. KnownOne, TLO, Depth+1))
  1272. return true;
  1273. if (SimplifyDemandedBits(Op.getOperand(1), NewMask, KnownZero2,
  1274. KnownOne2, TLO, Depth+1))
  1275. return true;
  1276. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1277. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1278. // If the operands are constants, see if we can simplify them.
  1279. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  1280. return true;
  1281. // Only known if known in both the LHS and RHS.
  1282. KnownOne &= KnownOne2;
  1283. KnownZero &= KnownZero2;
  1284. break;
  1285. case ISD::SELECT_CC:
  1286. if (SimplifyDemandedBits(Op.getOperand(3), NewMask, KnownZero,
  1287. KnownOne, TLO, Depth+1))
  1288. return true;
  1289. if (SimplifyDemandedBits(Op.getOperand(2), NewMask, KnownZero2,
  1290. KnownOne2, TLO, Depth+1))
  1291. return true;
  1292. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1293. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1294. // If the operands are constants, see if we can simplify them.
  1295. if (TLO.ShrinkDemandedConstant(Op, NewMask))
  1296. return true;
  1297. // Only known if known in both the LHS and RHS.
  1298. KnownOne &= KnownOne2;
  1299. KnownZero &= KnownZero2;
  1300. break;
  1301. case ISD::SHL:
  1302. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1303. unsigned ShAmt = SA->getZExtValue();
  1304. SDValue InOp = Op.getOperand(0);
  1305. // If the shift count is an invalid immediate, don't do anything.
  1306. if (ShAmt >= BitWidth)
  1307. break;
  1308. // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a
  1309. // single shift. We can do this if the bottom bits (which are shifted
  1310. // out) are never demanded.
  1311. if (InOp.getOpcode() == ISD::SRL &&
  1312. isa<ConstantSDNode>(InOp.getOperand(1))) {
  1313. if (ShAmt && (NewMask & APInt::getLowBitsSet(BitWidth, ShAmt)) == 0) {
  1314. unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
  1315. unsigned Opc = ISD::SHL;
  1316. int Diff = ShAmt-C1;
  1317. if (Diff < 0) {
  1318. Diff = -Diff;
  1319. Opc = ISD::SRL;
  1320. }
  1321. SDValue NewSA =
  1322. TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
  1323. EVT VT = Op.getValueType();
  1324. return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
  1325. InOp.getOperand(0), NewSA));
  1326. }
  1327. }
  1328. if (SimplifyDemandedBits(InOp, NewMask.lshr(ShAmt),
  1329. KnownZero, KnownOne, TLO, Depth+1))
  1330. return true;
  1331. // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits
  1332. // are not demanded. This will likely allow the anyext to be folded away.
  1333. if (InOp.getNode()->getOpcode() == ISD::ANY_EXTEND) {
  1334. SDValue InnerOp = InOp.getNode()->getOperand(0);
  1335. EVT InnerVT = InnerOp.getValueType();
  1336. unsigned InnerBits = InnerVT.getSizeInBits();
  1337. if (ShAmt < InnerBits && NewMask.lshr(InnerBits) == 0 &&
  1338. isTypeDesirableForOp(ISD::SHL, InnerVT)) {
  1339. EVT ShTy = getShiftAmountTy(InnerVT);
  1340. if (!APInt(BitWidth, ShAmt).isIntN(ShTy.getSizeInBits()))
  1341. ShTy = InnerVT;
  1342. SDValue NarrowShl =
  1343. TLO.DAG.getNode(ISD::SHL, dl, InnerVT, InnerOp,
  1344. TLO.DAG.getConstant(ShAmt, ShTy));
  1345. return
  1346. TLO.CombineTo(Op,
  1347. TLO.DAG.getNode(ISD::ANY_EXTEND, dl, Op.getValueType(),
  1348. NarrowShl));
  1349. }
  1350. }
  1351. KnownZero <<= SA->getZExtValue();
  1352. KnownOne <<= SA->getZExtValue();
  1353. // low bits known zero.
  1354. KnownZero |= APInt::getLowBitsSet(BitWidth, SA->getZExtValue());
  1355. }
  1356. break;
  1357. case ISD::SRL:
  1358. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1359. EVT VT = Op.getValueType();
  1360. unsigned ShAmt = SA->getZExtValue();
  1361. unsigned VTSize = VT.getSizeInBits();
  1362. SDValue InOp = Op.getOperand(0);
  1363. // If the shift count is an invalid immediate, don't do anything.
  1364. if (ShAmt >= BitWidth)
  1365. break;
  1366. // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a
  1367. // single shift. We can do this if the top bits (which are shifted out)
  1368. // are never demanded.
  1369. if (InOp.getOpcode() == ISD::SHL &&
  1370. isa<ConstantSDNode>(InOp.getOperand(1))) {
  1371. if (ShAmt && (NewMask & APInt::getHighBitsSet(VTSize, ShAmt)) == 0) {
  1372. unsigned C1= cast<ConstantSDNode>(InOp.getOperand(1))->getZExtValue();
  1373. unsigned Opc = ISD::SRL;
  1374. int Diff = ShAmt-C1;
  1375. if (Diff < 0) {
  1376. Diff = -Diff;
  1377. Opc = ISD::SHL;
  1378. }
  1379. SDValue NewSA =
  1380. TLO.DAG.getConstant(Diff, Op.getOperand(1).getValueType());
  1381. return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, dl, VT,
  1382. InOp.getOperand(0), NewSA));
  1383. }
  1384. }
  1385. // Compute the new bits that are at the top now.
  1386. if (SimplifyDemandedBits(InOp, (NewMask << ShAmt),
  1387. KnownZero, KnownOne, TLO, Depth+1))
  1388. return true;
  1389. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1390. KnownZero = KnownZero.lshr(ShAmt);
  1391. KnownOne = KnownOne.lshr(ShAmt);
  1392. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
  1393. KnownZero |= HighBits; // High bits known zero.
  1394. }
  1395. break;
  1396. case ISD::SRA:
  1397. // If this is an arithmetic shift right and only the low-bit is set, we can
  1398. // always convert this into a logical shr, even if the shift amount is
  1399. // variable. The low bit of the shift cannot be an input sign bit unless
  1400. // the shift amount is >= the size of the datatype, which is undefined.
  1401. if (NewMask == 1)
  1402. return TLO.CombineTo(Op,
  1403. TLO.DAG.getNode(ISD::SRL, dl, Op.getValueType(),
  1404. Op.getOperand(0), Op.getOperand(1)));
  1405. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1406. EVT VT = Op.getValueType();
  1407. unsigned ShAmt = SA->getZExtValue();
  1408. // If the shift count is an invalid immediate, don't do anything.
  1409. if (ShAmt >= BitWidth)
  1410. break;
  1411. APInt InDemandedMask = (NewMask << ShAmt);
  1412. // If any of the demanded bits are produced by the sign extension, we also
  1413. // demand the input sign bit.
  1414. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt);
  1415. if (HighBits.intersects(NewMask))
  1416. InDemandedMask |= APInt::getSignBit(VT.getScalarType().getSizeInBits());
  1417. if (SimplifyDemandedBits(Op.getOperand(0), InDemandedMask,
  1418. KnownZero, KnownOne, TLO, Depth+1))
  1419. return true;
  1420. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1421. KnownZero = KnownZero.lshr(ShAmt);
  1422. KnownOne = KnownOne.lshr(ShAmt);
  1423. // Handle the sign bit, adjusted to where it is now in the mask.
  1424. APInt SignBit = APInt::getSignBit(BitWidth).lshr(ShAmt);
  1425. // If the input sign bit is known to be zero, or if none of the top bits
  1426. // are demanded, turn this into an unsigned shift right.
  1427. if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
  1428. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
  1429. Op.getOperand(0),
  1430. Op.getOperand(1)));
  1431. } else if (KnownOne.intersects(SignBit)) { // New bits are known one.
  1432. KnownOne |= HighBits;
  1433. }
  1434. }
  1435. break;
  1436. case ISD::SIGN_EXTEND_INREG: {
  1437. EVT ExVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1438. APInt MsbMask = APInt::getHighBitsSet(BitWidth, 1);
  1439. // If we only care about the highest bit, don't bother shifting right.
  1440. if (MsbMask == DemandedMask) {
  1441. unsigned ShAmt = ExVT.getScalarType().getSizeInBits();
  1442. SDValue InOp = Op.getOperand(0);
  1443. // Compute the correct shift amount type, which must be getShiftAmountTy
  1444. // for scalar types after legalization.
  1445. EVT ShiftAmtTy = Op.getValueType();
  1446. if (TLO.LegalTypes() && !ShiftAmtTy.isVector())
  1447. ShiftAmtTy = getShiftAmountTy(ShiftAmtTy);
  1448. SDValue ShiftAmt = TLO.DAG.getConstant(BitWidth - ShAmt, ShiftAmtTy);
  1449. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
  1450. Op.getValueType(), InOp, ShiftAmt));
  1451. }
  1452. // Sign extension. Compute the demanded bits in the result that are not
  1453. // present in the input.
  1454. APInt NewBits =
  1455. APInt::getHighBitsSet(BitWidth,
  1456. BitWidth - ExVT.getScalarType().getSizeInBits());
  1457. // If none of the extended bits are demanded, eliminate the sextinreg.
  1458. if ((NewBits & NewMask) == 0)
  1459. return TLO.CombineTo(Op, Op.getOperand(0));
  1460. APInt InSignBit =
  1461. APInt::getSignBit(ExVT.getScalarType().getSizeInBits()).zext(BitWidth);
  1462. APInt InputDemandedBits =
  1463. APInt::getLowBitsSet(BitWidth,
  1464. ExVT.getScalarType().getSizeInBits()) &
  1465. NewMask;
  1466. // Since the sign extended bits are demanded, we know that the sign
  1467. // bit is demanded.
  1468. InputDemandedBits |= InSignBit;
  1469. if (SimplifyDemandedBits(Op.getOperand(0), InputDemandedBits,
  1470. KnownZero, KnownOne, TLO, Depth+1))
  1471. return true;
  1472. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1473. // If the sign bit of the input is known set or clear, then we know the
  1474. // top bits of the result.
  1475. // If the input sign bit is known zero, convert this into a zero extension.
  1476. if (KnownZero.intersects(InSignBit))
  1477. return TLO.CombineTo(Op,
  1478. TLO.DAG.getZeroExtendInReg(Op.getOperand(0),dl,ExVT));
  1479. if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
  1480. KnownOne |= NewBits;
  1481. KnownZero &= ~NewBits;
  1482. } else { // Input sign bit unknown
  1483. KnownZero &= ~NewBits;
  1484. KnownOne &= ~NewBits;
  1485. }
  1486. break;
  1487. }
  1488. case ISD::ZERO_EXTEND: {
  1489. unsigned OperandBitWidth =
  1490. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  1491. APInt InMask = NewMask.trunc(OperandBitWidth);
  1492. // If none of the top bits are demanded, convert this into an any_extend.
  1493. APInt NewBits =
  1494. APInt::getHighBitsSet(BitWidth, BitWidth - OperandBitWidth) & NewMask;
  1495. if (!NewBits.intersects(NewMask))
  1496. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
  1497. Op.getValueType(),
  1498. Op.getOperand(0)));
  1499. if (SimplifyDemandedBits(Op.getOperand(0), InMask,
  1500. KnownZero, KnownOne, TLO, Depth+1))
  1501. return true;
  1502. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1503. KnownZero = KnownZero.zext(BitWidth);
  1504. KnownOne = KnownOne.zext(BitWidth);
  1505. KnownZero |= NewBits;
  1506. break;
  1507. }
  1508. case ISD::SIGN_EXTEND: {
  1509. EVT InVT = Op.getOperand(0).getValueType();
  1510. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1511. APInt InMask = APInt::getLowBitsSet(BitWidth, InBits);
  1512. APInt InSignBit = APInt::getBitsSet(BitWidth, InBits - 1, InBits);
  1513. APInt NewBits = ~InMask & NewMask;
  1514. // If none of the top bits are demanded, convert this into an any_extend.
  1515. if (NewBits == 0)
  1516. return TLO.CombineTo(Op,TLO.DAG.getNode(ISD::ANY_EXTEND, dl,
  1517. Op.getValueType(),
  1518. Op.getOperand(0)));
  1519. // Since some of the sign extended bits are demanded, we know that the sign
  1520. // bit is demanded.
  1521. APInt InDemandedBits = InMask & NewMask;
  1522. InDemandedBits |= InSignBit;
  1523. InDemandedBits = InDemandedBits.trunc(InBits);
  1524. if (SimplifyDemandedBits(Op.getOperand(0), InDemandedBits, KnownZero,
  1525. KnownOne, TLO, Depth+1))
  1526. return true;
  1527. KnownZero = KnownZero.zext(BitWidth);
  1528. KnownOne = KnownOne.zext(BitWidth);
  1529. // If the sign bit is known zero, convert this to a zero extend.
  1530. if (KnownZero.intersects(InSignBit))
  1531. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::ZERO_EXTEND, dl,
  1532. Op.getValueType(),
  1533. Op.getOperand(0)));
  1534. // If the sign bit is known one, the top bits match.
  1535. if (KnownOne.intersects(InSignBit)) {
  1536. KnownOne |= NewBits;
  1537. assert((KnownZero & NewBits) == 0);
  1538. } else { // Otherwise, top bits aren't known.
  1539. assert((KnownOne & NewBits) == 0);
  1540. assert((KnownZero & NewBits) == 0);
  1541. }
  1542. break;
  1543. }
  1544. case ISD::ANY_EXTEND: {
  1545. unsigned OperandBitWidth =
  1546. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  1547. APInt InMask = NewMask.trunc(OperandBitWidth);
  1548. if (SimplifyDemandedBits(Op.getOperand(0), InMask,
  1549. KnownZero, KnownOne, TLO, Depth+1))
  1550. return true;
  1551. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1552. KnownZero = KnownZero.zext(BitWidth);
  1553. KnownOne = KnownOne.zext(BitWidth);
  1554. break;
  1555. }
  1556. case ISD::TRUNCATE: {
  1557. // Simplify the input, using demanded bit information, and compute the known
  1558. // zero/one bits live out.
  1559. unsigned OperandBitWidth =
  1560. Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  1561. APInt TruncMask = NewMask.zext(OperandBitWidth);
  1562. if (SimplifyDemandedBits(Op.getOperand(0), TruncMask,
  1563. KnownZero, KnownOne, TLO, Depth+1))
  1564. return true;
  1565. KnownZero = KnownZero.trunc(BitWidth);
  1566. KnownOne = KnownOne.trunc(BitWidth);
  1567. // If the input is only used by this truncate, see if we can shrink it based
  1568. // on the known demanded bits.
  1569. if (Op.getOperand(0).getNode()->hasOneUse()) {
  1570. SDValue In = Op.getOperand(0);
  1571. switch (In.getOpcode()) {
  1572. default: break;
  1573. case ISD::SRL:
  1574. // Shrink SRL by a constant if none of the high bits shifted in are
  1575. // demanded.
  1576. if (TLO.LegalTypes() &&
  1577. !isTypeDesirableForOp(ISD::SRL, Op.getValueType()))
  1578. // Do not turn (vt1 truncate (vt2 srl)) into (vt1 srl) if vt1 is
  1579. // undesirable.
  1580. break;
  1581. ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(In.getOperand(1));
  1582. if (!ShAmt)
  1583. break;
  1584. SDValue Shift = In.getOperand(1);
  1585. if (TLO.LegalTypes()) {
  1586. uint64_t ShVal = ShAmt->getZExtValue();
  1587. Shift =
  1588. TLO.DAG.getConstant(ShVal, getShiftAmountTy(Op.getValueType()));
  1589. }
  1590. APInt HighBits = APInt::getHighBitsSet(OperandBitWidth,
  1591. OperandBitWidth - BitWidth);
  1592. HighBits = HighBits.lshr(ShAmt->getZExtValue()).trunc(BitWidth);
  1593. if (ShAmt->getZExtValue() < BitWidth && !(HighBits & NewMask)) {
  1594. // None of the shifted in bits are needed. Add a truncate of the
  1595. // shift input, then shift it.
  1596. SDValue NewTrunc = TLO.DAG.getNode(ISD::TRUNCATE, dl,
  1597. Op.getValueType(),
  1598. In.getOperand(0));
  1599. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl,
  1600. Op.getValueType(),
  1601. NewTrunc,
  1602. Shift));
  1603. }
  1604. break;
  1605. }
  1606. }
  1607. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1608. break;
  1609. }
  1610. case ISD::AssertZext: {
  1611. // AssertZext demands all of the high bits, plus any of the low bits
  1612. // demanded by its users.
  1613. EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1614. APInt InMask = APInt::getLowBitsSet(BitWidth,
  1615. VT.getSizeInBits());
  1616. if (SimplifyDemandedBits(Op.getOperand(0), ~InMask | NewMask,
  1617. KnownZero, KnownOne, TLO, Depth+1))
  1618. return true;
  1619. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1620. KnownZero |= ~InMask & NewMask;
  1621. break;
  1622. }
  1623. case ISD::BITCAST:
  1624. // If this is an FP->Int bitcast and if the sign bit is the only
  1625. // thing demanded, turn this into a FGETSIGN.
  1626. if (!TLO.LegalOperations() &&
  1627. !Op.getValueType().isVector() &&
  1628. !Op.getOperand(0).getValueType().isVector() &&
  1629. NewMask == APInt::getSignBit(Op.getValueType().getSizeInBits()) &&
  1630. Op.getOperand(0).getValueType().isFloatingPoint()) {
  1631. bool OpVTLegal = isOperationLegalOrCustom(ISD::FGETSIGN, Op.getValueType());
  1632. bool i32Legal = isOperationLegalOrCustom(ISD::FGETSIGN, MVT::i32);
  1633. if ((OpVTLegal || i32Legal) && Op.getValueType().isSimple()) {
  1634. EVT Ty = OpVTLegal ? Op.getValueType() : MVT::i32;
  1635. // Make a FGETSIGN + SHL to move the sign bit into the appropriate
  1636. // place. We expect the SHL to be eliminated by other optimizations.
  1637. SDValue Sign = TLO.DAG.getNode(ISD::FGETSIGN, dl, Ty, Op.getOperand(0));
  1638. unsigned OpVTSizeInBits = Op.getValueType().getSizeInBits();
  1639. if (!OpVTLegal && OpVTSizeInBits > 32)
  1640. Sign = TLO.DAG.getNode(ISD::ZERO_EXTEND, dl, Op.getValueType(), Sign);
  1641. unsigned ShVal = Op.getValueType().getSizeInBits()-1;
  1642. SDValue ShAmt = TLO.DAG.getConstant(ShVal, Op.getValueType());
  1643. return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SHL, dl,
  1644. Op.getValueType(),
  1645. Sign, ShAmt));
  1646. }
  1647. }
  1648. break;
  1649. case ISD::ADD:
  1650. case ISD::MUL:
  1651. case ISD::SUB: {
  1652. // Add, Sub, and Mul don't demand any bits in positions beyond that
  1653. // of the highest bit demanded of them.
  1654. APInt LoMask = APInt::getLowBitsSet(BitWidth,
  1655. BitWidth - NewMask.countLeadingZeros());
  1656. if (SimplifyDemandedBits(Op.getOperand(0), LoMask, KnownZero2,
  1657. KnownOne2, TLO, Depth+1))
  1658. return true;
  1659. if (SimplifyDemandedBits(Op.getOperand(1), LoMask, KnownZero2,
  1660. KnownOne2, TLO, Depth+1))
  1661. return true;
  1662. // See if the operation should be performed at a smaller bit width.
  1663. if (TLO.ShrinkDemandedOp(Op, BitWidth, NewMask, dl))
  1664. return true;
  1665. }
  1666. // FALL THROUGH
  1667. default:
  1668. // Just use ComputeMaskedBits to compute output bits.
  1669. TLO.DAG.ComputeMaskedBits(Op, KnownZero, KnownOne, Depth);
  1670. break;
  1671. }
  1672. // If we know the value of all of the demanded bits, return this as a
  1673. // constant.
  1674. if ((NewMask & (KnownZero|KnownOne)) == NewMask)
  1675. return TLO.CombineTo(Op, TLO.DAG.getConstant(KnownOne, Op.getValueType()));
  1676. return false;
  1677. }
  1678. /// computeMaskedBitsForTargetNode - Determine which of the bits specified
  1679. /// in Mask are known to be either zero or one and return them in the
  1680. /// KnownZero/KnownOne bitsets.
  1681. void TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
  1682. APInt &KnownZero,
  1683. APInt &KnownOne,
  1684. const SelectionDAG &DAG,
  1685. unsigned Depth) const {
  1686. assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1687. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1688. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1689. Op.getOpcode() == ISD::INTRINSIC_VOID) &&
  1690. "Should use MaskedValueIsZero if you don't know whether Op"
  1691. " is a target node!");
  1692. KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0);
  1693. }
  1694. /// ComputeNumSignBitsForTargetNode - This method can be implemented by
  1695. /// targets that want to expose additional information about sign bits to the
  1696. /// DAG Combiner.
  1697. unsigned TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
  1698. unsigned Depth) const {
  1699. assert((Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1700. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1701. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1702. Op.getOpcode() == ISD::INTRINSIC_VOID) &&
  1703. "Should use ComputeNumSignBits if you don't know whether Op"
  1704. " is a target node!");
  1705. return 1;
  1706. }
  1707. /// ValueHasExactlyOneBitSet - Test if the given value is known to have exactly
  1708. /// one bit set. This differs from ComputeMaskedBits in that it doesn't need to
  1709. /// determine which bit is set.
  1710. ///
  1711. static bool ValueHasExactlyOneBitSet(SDValue Val, const SelectionDAG &DAG) {
  1712. // A left-shift of a constant one will have exactly one bit set, because
  1713. // shifting the bit off the end is undefined.
  1714. if (Val.getOpcode() == ISD::SHL)
  1715. if (ConstantSDNode *C =
  1716. dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
  1717. if (C->getAPIntValue() == 1)
  1718. return true;
  1719. // Similarly, a right-shift of a constant sign-bit will have exactly
  1720. // one bit set.
  1721. if (Val.getOpcode() == ISD::SRL)
  1722. if (ConstantSDNode *C =
  1723. dyn_cast<ConstantSDNode>(Val.getNode()->getOperand(0)))
  1724. if (C->getAPIntValue().isSignBit())
  1725. return true;
  1726. // More could be done here, though the above checks are enough
  1727. // to handle some common cases.
  1728. // Fall back to ComputeMaskedBits to catch other known cases.
  1729. EVT OpVT = Val.getValueType();
  1730. unsigned BitWidth = OpVT.getScalarType().getSizeInBits();
  1731. APInt KnownZero, KnownOne;
  1732. DAG.ComputeMaskedBits(Val, KnownZero, KnownOne);
  1733. return (KnownZero.countPopulation() == BitWidth - 1) &&
  1734. (KnownOne.countPopulation() == 1);
  1735. }
  1736. /// SimplifySetCC - Try to simplify a setcc built with the specified operands
  1737. /// and cc. If it is unable to simplify it, return a null SDValue.
  1738. SDValue
  1739. TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
  1740. ISD::CondCode Cond, bool foldBooleans,
  1741. DAGCombinerInfo &DCI, DebugLoc dl) const {
  1742. SelectionDAG &DAG = DCI.DAG;
  1743. // These setcc operations always fold.
  1744. switch (Cond) {
  1745. default: break;
  1746. case ISD::SETFALSE:
  1747. case ISD::SETFALSE2: return DAG.getConstant(0, VT);
  1748. case ISD::SETTRUE:
  1749. case ISD::SETTRUE2: return DAG.getConstant(1, VT);
  1750. }
  1751. // Ensure that the constant occurs on the RHS, and fold constant
  1752. // comparisons.
  1753. if (isa<ConstantSDNode>(N0.getNode()))
  1754. return DAG.getSetCC(dl, VT, N1, N0, ISD::getSetCCSwappedOperands(Cond));
  1755. if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
  1756. const APInt &C1 = N1C->getAPIntValue();
  1757. // If the LHS is '(srl (ctlz x), 5)', the RHS is 0/1, and this is an
  1758. // equality comparison, then we're just comparing whether X itself is
  1759. // zero.
  1760. if (N0.getOpcode() == ISD::SRL && (C1 == 0 || C1 == 1) &&
  1761. N0.getOperand(0).getOpcode() == ISD::CTLZ &&
  1762. N0.getOperand(1).getOpcode() == ISD::Constant) {
  1763. const APInt &ShAmt
  1764. = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
  1765. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  1766. ShAmt == Log2_32(N0.getValueType().getSizeInBits())) {
  1767. if ((C1 == 0) == (Cond == ISD::SETEQ)) {
  1768. // (srl (ctlz x), 5) == 0 -> X != 0
  1769. // (srl (ctlz x), 5) != 1 -> X != 0
  1770. Cond = ISD::SETNE;
  1771. } else {
  1772. // (srl (ctlz x), 5) != 0 -> X == 0
  1773. // (srl (ctlz x), 5) == 1 -> X == 0
  1774. Cond = ISD::SETEQ;
  1775. }
  1776. SDValue Zero = DAG.getConstant(0, N0.getValueType());
  1777. return DAG.getSetCC(dl, VT, N0.getOperand(0).getOperand(0),
  1778. Zero, Cond);
  1779. }
  1780. }
  1781. SDValue CTPOP = N0;
  1782. // Look through truncs that don't change the value of a ctpop.
  1783. if (N0.hasOneUse() && N0.getOpcode() == ISD::TRUNCATE)
  1784. CTPOP = N0.getOperand(0);
  1785. if (CTPOP.hasOneUse() && CTPOP.getOpcode() == ISD::CTPOP &&
  1786. (N0 == CTPOP || N0.getValueType().getSizeInBits() >
  1787. Log2_32_Ceil(CTPOP.getValueType().getSizeInBits()))) {
  1788. EVT CTVT = CTPOP.getValueType();
  1789. SDValue CTOp = CTPOP.getOperand(0);
  1790. // (ctpop x) u< 2 -> (x & x-1) == 0
  1791. // (ctpop x) u> 1 -> (x & x-1) != 0
  1792. if ((Cond == ISD::SETULT && C1 == 2) || (Cond == ISD::SETUGT && C1 == 1)){
  1793. SDValue Sub = DAG.getNode(ISD::SUB, dl, CTVT, CTOp,
  1794. DAG.getConstant(1, CTVT));
  1795. SDValue And = DAG.getNode(ISD::AND, dl, CTVT, CTOp, Sub);
  1796. ISD::CondCode CC = Cond == ISD::SETULT ? ISD::SETEQ : ISD::SETNE;
  1797. return DAG.getSetCC(dl, VT, And, DAG.getConstant(0, CTVT), CC);
  1798. }
  1799. // TODO: (ctpop x) == 1 -> x && (x & x-1) == 0 iff ctpop is illegal.
  1800. }
  1801. // (zext x) == C --> x == (trunc C)
  1802. if (DCI.isBeforeLegalize() && N0->hasOneUse() &&
  1803. (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
  1804. unsigned MinBits = N0.getValueSizeInBits();
  1805. SDValue PreZExt;
  1806. if (N0->getOpcode() == ISD::ZERO_EXTEND) {
  1807. // ZExt
  1808. MinBits = N0->getOperand(0).getValueSizeInBits();
  1809. PreZExt = N0->getOperand(0);
  1810. } else if (N0->getOpcode() == ISD::AND) {
  1811. // DAGCombine turns costly ZExts into ANDs
  1812. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0->getOperand(1)))
  1813. if ((C->getAPIntValue()+1).isPowerOf2()) {
  1814. MinBits = C->getAPIntValue().countTrailingOnes();
  1815. PreZExt = N0->getOperand(0);
  1816. }
  1817. } else if (LoadSDNode *LN0 = dyn_cast<LoadSDNode>(N0)) {
  1818. // ZEXTLOAD
  1819. if (LN0->getExtensionType() == ISD::ZEXTLOAD) {
  1820. MinBits = LN0->getMemoryVT().getSizeInBits();
  1821. PreZExt = N0;
  1822. }
  1823. }
  1824. // Make sure we're not losing bits from the constant.
  1825. if (MinBits < C1.getBitWidth() && MinBits > C1.getActiveBits()) {
  1826. EVT MinVT = EVT::getIntegerVT(*DAG.getContext(), MinBits);
  1827. if (isTypeDesirableForOp(ISD::SETCC, MinVT)) {
  1828. // Will get folded away.
  1829. SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MinVT, PreZExt);
  1830. SDValue C = DAG.getConstant(C1.trunc(MinBits), MinVT);
  1831. return DAG.getSetCC(dl, VT, Trunc, C, Cond);
  1832. }
  1833. }
  1834. }
  1835. // If the LHS is '(and load, const)', the RHS is 0,
  1836. // the test is for equality or unsigned, and all 1 bits of the const are
  1837. // in the same partial word, see if we can shorten the load.
  1838. if (DCI.isBeforeLegalize() &&
  1839. N0.getOpcode() == ISD::AND && C1 == 0 &&
  1840. N0.getNode()->hasOneUse() &&
  1841. isa<LoadSDNode>(N0.getOperand(0)) &&
  1842. N0.getOperand(0).getNode()->hasOneUse() &&
  1843. isa<ConstantSDNode>(N0.getOperand(1))) {
  1844. LoadSDNode *Lod = cast<LoadSDNode>(N0.getOperand(0));
  1845. APInt bestMask;
  1846. unsigned bestWidth = 0, bestOffset = 0;
  1847. if (!Lod->isVolatile() && Lod->isUnindexed()) {
  1848. unsigned origWidth = N0.getValueType().getSizeInBits();
  1849. unsigned maskWidth = origWidth;
  1850. // We can narrow (e.g.) 16-bit extending loads on 32-bit target to
  1851. // 8 bits, but have to be careful...
  1852. if (Lod->getExtensionType() != ISD::NON_EXTLOAD)
  1853. origWidth = Lod->getMemoryVT().getSizeInBits();
  1854. const APInt &Mask =
  1855. cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
  1856. for (unsigned width = origWidth / 2; width>=8; width /= 2) {
  1857. APInt newMask = APInt::getLowBitsSet(maskWidth, width);
  1858. for (unsigned offset=0; offset<origWidth/width; offset++) {
  1859. if ((newMask & Mask) == Mask) {
  1860. if (!TD->isLittleEndian())
  1861. bestOffset = (origWidth/width - offset - 1) * (width/8);
  1862. else
  1863. bestOffset = (uint64_t)offset * (width/8);
  1864. bestMask = Mask.lshr(offset * (width/8) * 8);
  1865. bestWidth = width;
  1866. break;
  1867. }
  1868. newMask = newMask << width;
  1869. }
  1870. }
  1871. }
  1872. if (bestWidth) {
  1873. EVT newVT = EVT::getIntegerVT(*DAG.getContext(), bestWidth);
  1874. if (newVT.isRound()) {
  1875. EVT PtrType = Lod->getOperand(1).getValueType();
  1876. SDValue Ptr = Lod->getBasePtr();
  1877. if (bestOffset != 0)
  1878. Ptr = DAG.getNode(ISD::ADD, dl, PtrType, Lod->getBasePtr(),
  1879. DAG.getConstant(bestOffset, PtrType));
  1880. unsigned NewAlign = MinAlign(Lod->getAlignment(), bestOffset);
  1881. SDValue NewLoad = DAG.getLoad(newVT, dl, Lod->getChain(), Ptr,
  1882. Lod->getPointerInfo().getWithOffset(bestOffset),
  1883. false, false, false, NewAlign);
  1884. return DAG.getSetCC(dl, VT,
  1885. DAG.getNode(ISD::AND, dl, newVT, NewLoad,
  1886. DAG.getConstant(bestMask.trunc(bestWidth),
  1887. newVT)),
  1888. DAG.getConstant(0LL, newVT), Cond);
  1889. }
  1890. }
  1891. }
  1892. // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
  1893. if (N0.getOpcode() == ISD::ZERO_EXTEND) {
  1894. unsigned InSize = N0.getOperand(0).getValueType().getSizeInBits();
  1895. // If the comparison constant has bits in the upper part, the
  1896. // zero-extended value could never match.
  1897. if (C1.intersects(APInt::getHighBitsSet(C1.getBitWidth(),
  1898. C1.getBitWidth() - InSize))) {
  1899. switch (Cond) {
  1900. case ISD::SETUGT:
  1901. case ISD::SETUGE:
  1902. case ISD::SETEQ: return DAG.getConstant(0, VT);
  1903. case ISD::SETULT:
  1904. case ISD::SETULE:
  1905. case ISD::SETNE: return DAG.getConstant(1, VT);
  1906. case ISD::SETGT:
  1907. case ISD::SETGE:
  1908. // True if the sign bit of C1 is set.
  1909. return DAG.getConstant(C1.isNegative(), VT);
  1910. case ISD::SETLT:
  1911. case ISD::SETLE:
  1912. // True if the sign bit of C1 isn't set.
  1913. return DAG.getConstant(C1.isNonNegative(), VT);
  1914. default:
  1915. break;
  1916. }
  1917. }
  1918. // Otherwise, we can perform the comparison with the low bits.
  1919. switch (Cond) {
  1920. case ISD::SETEQ:
  1921. case ISD::SETNE:
  1922. case ISD::SETUGT:
  1923. case ISD::SETUGE:
  1924. case ISD::SETULT:
  1925. case ISD::SETULE: {
  1926. EVT newVT = N0.getOperand(0).getValueType();
  1927. if (DCI.isBeforeLegalizeOps() ||
  1928. (isOperationLegal(ISD::SETCC, newVT) &&
  1929. getCondCodeAction(Cond, newVT)==Legal))
  1930. return DAG.getSetCC(dl, VT, N0.getOperand(0),
  1931. DAG.getConstant(C1.trunc(InSize), newVT),
  1932. Cond);
  1933. break;
  1934. }
  1935. default:
  1936. break; // todo, be more careful with signed comparisons
  1937. }
  1938. } else if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
  1939. (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
  1940. EVT ExtSrcTy = cast<VTSDNode>(N0.getOperand(1))->getVT();
  1941. unsigned ExtSrcTyBits = ExtSrcTy.getSizeInBits();
  1942. EVT ExtDstTy = N0.getValueType();
  1943. unsigned ExtDstTyBits = ExtDstTy.getSizeInBits();
  1944. // If the constant doesn't fit into the number of bits for the source of
  1945. // the sign extension, it is impossible for both sides to be equal.
  1946. if (C1.getMinSignedBits() > ExtSrcTyBits)
  1947. return DAG.getConstant(Cond == ISD::SETNE, VT);
  1948. SDValue ZextOp;
  1949. EVT Op0Ty = N0.getOperand(0).getValueType();
  1950. if (Op0Ty == ExtSrcTy) {
  1951. ZextOp = N0.getOperand(0);
  1952. } else {
  1953. APInt Imm = APInt::getLowBitsSet(ExtDstTyBits, ExtSrcTyBits);
  1954. ZextOp = DAG.getNode(ISD::AND, dl, Op0Ty, N0.getOperand(0),
  1955. DAG.getConstant(Imm, Op0Ty));
  1956. }
  1957. if (!DCI.isCalledByLegalizer())
  1958. DCI.AddToWorklist(ZextOp.getNode());
  1959. // Otherwise, make this a use of a zext.
  1960. return DAG.getSetCC(dl, VT, ZextOp,
  1961. DAG.getConstant(C1 & APInt::getLowBitsSet(
  1962. ExtDstTyBits,
  1963. ExtSrcTyBits),
  1964. ExtDstTy),
  1965. Cond);
  1966. } else if ((N1C->isNullValue() || N1C->getAPIntValue() == 1) &&
  1967. (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
  1968. // SETCC (SETCC), [0|1], [EQ|NE] -> SETCC
  1969. if (N0.getOpcode() == ISD::SETCC &&
  1970. isTypeLegal(VT) && VT.bitsLE(N0.getValueType())) {
  1971. bool TrueWhenTrue = (Cond == ISD::SETEQ) ^ (N1C->getAPIntValue() != 1);
  1972. if (TrueWhenTrue)
  1973. return DAG.getNode(ISD::TRUNCATE, dl, VT, N0);
  1974. // Invert the condition.
  1975. ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
  1976. CC = ISD::getSetCCInverse(CC,
  1977. N0.getOperand(0).getValueType().isInteger());
  1978. return DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC);
  1979. }
  1980. if ((N0.getOpcode() == ISD::XOR ||
  1981. (N0.getOpcode() == ISD::AND &&
  1982. N0.getOperand(0).getOpcode() == ISD::XOR &&
  1983. N0.getOperand(1) == N0.getOperand(0).getOperand(1))) &&
  1984. isa<ConstantSDNode>(N0.getOperand(1)) &&
  1985. cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue() == 1) {
  1986. // If this is (X^1) == 0/1, swap the RHS and eliminate the xor. We
  1987. // can only do this if the top bits are known zero.
  1988. unsigned BitWidth = N0.getValueSizeInBits();
  1989. if (DAG.MaskedValueIsZero(N0,
  1990. APInt::getHighBitsSet(BitWidth,
  1991. BitWidth-1))) {
  1992. // Okay, get the un-inverted input value.
  1993. SDValue Val;
  1994. if (N0.getOpcode() == ISD::XOR)
  1995. Val = N0.getOperand(0);
  1996. else {
  1997. assert(N0.getOpcode() == ISD::AND &&
  1998. N0.getOperand(0).getOpcode() == ISD::XOR);
  1999. // ((X^1)&1)^1 -> X & 1
  2000. Val = DAG.getNode(ISD::AND, dl, N0.getValueType(),
  2001. N0.getOperand(0).getOperand(0),
  2002. N0.getOperand(1));
  2003. }
  2004. return DAG.getSetCC(dl, VT, Val, N1,
  2005. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  2006. }
  2007. } else if (N1C->getAPIntValue() == 1 &&
  2008. (VT == MVT::i1 ||
  2009. getBooleanContents(false) == ZeroOrOneBooleanContent)) {
  2010. SDValue Op0 = N0;
  2011. if (Op0.getOpcode() == ISD::TRUNCATE)
  2012. Op0 = Op0.getOperand(0);
  2013. if ((Op0.getOpcode() == ISD::XOR) &&
  2014. Op0.getOperand(0).getOpcode() == ISD::SETCC &&
  2015. Op0.getOperand(1).getOpcode() == ISD::SETCC) {
  2016. // (xor (setcc), (setcc)) == / != 1 -> (setcc) != / == (setcc)
  2017. Cond = (Cond == ISD::SETEQ) ? ISD::SETNE : ISD::SETEQ;
  2018. return DAG.getSetCC(dl, VT, Op0.getOperand(0), Op0.getOperand(1),
  2019. Cond);
  2020. }
  2021. if (Op0.getOpcode() == ISD::AND &&
  2022. isa<ConstantSDNode>(Op0.getOperand(1)) &&
  2023. cast<ConstantSDNode>(Op0.getOperand(1))->getAPIntValue() == 1) {
  2024. // If this is (X&1) == / != 1, normalize it to (X&1) != / == 0.
  2025. if (Op0.getValueType().bitsGT(VT))
  2026. Op0 = DAG.getNode(ISD::AND, dl, VT,
  2027. DAG.getNode(ISD::TRUNCATE, dl, VT, Op0.getOperand(0)),
  2028. DAG.getConstant(1, VT));
  2029. else if (Op0.getValueType().bitsLT(VT))
  2030. Op0 = DAG.getNode(ISD::AND, dl, VT,
  2031. DAG.getNode(ISD::ANY_EXTEND, dl, VT, Op0.getOperand(0)),
  2032. DAG.getConstant(1, VT));
  2033. return DAG.getSetCC(dl, VT, Op0,
  2034. DAG.getConstant(0, Op0.getValueType()),
  2035. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  2036. }
  2037. if (Op0.getOpcode() == ISD::AssertZext &&
  2038. cast<VTSDNode>(Op0.getOperand(1))->getVT() == MVT::i1)
  2039. return DAG.getSetCC(dl, VT, Op0,
  2040. DAG.getConstant(0, Op0.getValueType()),
  2041. Cond == ISD::SETEQ ? ISD::SETNE : ISD::SETEQ);
  2042. }
  2043. }
  2044. APInt MinVal, MaxVal;
  2045. unsigned OperandBitSize = N1C->getValueType(0).getSizeInBits();
  2046. if (ISD::isSignedIntSetCC(Cond)) {
  2047. MinVal = APInt::getSignedMinValue(OperandBitSize);
  2048. MaxVal = APInt::getSignedMaxValue(OperandBitSize);
  2049. } else {
  2050. MinVal = APInt::getMinValue(OperandBitSize);
  2051. MaxVal = APInt::getMaxValue(OperandBitSize);
  2052. }
  2053. // Canonicalize GE/LE comparisons to use GT/LT comparisons.
  2054. if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
  2055. if (C1 == MinVal) return DAG.getConstant(1, VT); // X >= MIN --> true
  2056. // X >= C0 --> X > (C0-1)
  2057. return DAG.getSetCC(dl, VT, N0,
  2058. DAG.getConstant(C1-1, N1.getValueType()),
  2059. (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
  2060. }
  2061. if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
  2062. if (C1 == MaxVal) return DAG.getConstant(1, VT); // X <= MAX --> true
  2063. // X <= C0 --> X < (C0+1)
  2064. return DAG.getSetCC(dl, VT, N0,
  2065. DAG.getConstant(C1+1, N1.getValueType()),
  2066. (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
  2067. }
  2068. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal)
  2069. return DAG.getConstant(0, VT); // X < MIN --> false
  2070. if ((Cond == ISD::SETGE || Cond == ISD::SETUGE) && C1 == MinVal)
  2071. return DAG.getConstant(1, VT); // X >= MIN --> true
  2072. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal)
  2073. return DAG.getConstant(0, VT); // X > MAX --> false
  2074. if ((Cond == ISD::SETLE || Cond == ISD::SETULE) && C1 == MaxVal)
  2075. return DAG.getConstant(1, VT); // X <= MAX --> true
  2076. // Canonicalize setgt X, Min --> setne X, Min
  2077. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MinVal)
  2078. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
  2079. // Canonicalize setlt X, Max --> setne X, Max
  2080. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MaxVal)
  2081. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETNE);
  2082. // If we have setult X, 1, turn it into seteq X, 0
  2083. if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C1 == MinVal+1)
  2084. return DAG.getSetCC(dl, VT, N0,
  2085. DAG.getConstant(MinVal, N0.getValueType()),
  2086. ISD::SETEQ);
  2087. // If we have setugt X, Max-1, turn it into seteq X, Max
  2088. if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C1 == MaxVal-1)
  2089. return DAG.getSetCC(dl, VT, N0,
  2090. DAG.getConstant(MaxVal, N0.getValueType()),
  2091. ISD::SETEQ);
  2092. // If we have "setcc X, C0", check to see if we can shrink the immediate
  2093. // by changing cc.
  2094. // SETUGT X, SINTMAX -> SETLT X, 0
  2095. if (Cond == ISD::SETUGT &&
  2096. C1 == APInt::getSignedMaxValue(OperandBitSize))
  2097. return DAG.getSetCC(dl, VT, N0,
  2098. DAG.getConstant(0, N1.getValueType()),
  2099. ISD::SETLT);
  2100. // SETULT X, SINTMIN -> SETGT X, -1
  2101. if (Cond == ISD::SETULT &&
  2102. C1 == APInt::getSignedMinValue(OperandBitSize)) {
  2103. SDValue ConstMinusOne =
  2104. DAG.getConstant(APInt::getAllOnesValue(OperandBitSize),
  2105. N1.getValueType());
  2106. return DAG.getSetCC(dl, VT, N0, ConstMinusOne, ISD::SETGT);
  2107. }
  2108. // Fold bit comparisons when we can.
  2109. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  2110. (VT == N0.getValueType() ||
  2111. (isTypeLegal(VT) && VT.bitsLE(N0.getValueType()))) &&
  2112. N0.getOpcode() == ISD::AND)
  2113. if (ConstantSDNode *AndRHS =
  2114. dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  2115. EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
  2116. getPointerTy() : getShiftAmountTy(N0.getValueType());
  2117. if (Cond == ISD::SETNE && C1 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
  2118. // Perform the xform if the AND RHS is a single bit.
  2119. if (AndRHS->getAPIntValue().isPowerOf2()) {
  2120. return DAG.getNode(ISD::TRUNCATE, dl, VT,
  2121. DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
  2122. DAG.getConstant(AndRHS->getAPIntValue().logBase2(), ShiftTy)));
  2123. }
  2124. } else if (Cond == ISD::SETEQ && C1 == AndRHS->getAPIntValue()) {
  2125. // (X & 8) == 8 --> (X & 8) >> 3
  2126. // Perform the xform if C1 is a single bit.
  2127. if (C1.isPowerOf2()) {
  2128. return DAG.getNode(ISD::TRUNCATE, dl, VT,
  2129. DAG.getNode(ISD::SRL, dl, N0.getValueType(), N0,
  2130. DAG.getConstant(C1.logBase2(), ShiftTy)));
  2131. }
  2132. }
  2133. }
  2134. if (C1.getMinSignedBits() <= 64 &&
  2135. !isLegalICmpImmediate(C1.getSExtValue())) {
  2136. // (X & -256) == 256 -> (X >> 8) == 1
  2137. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  2138. N0.getOpcode() == ISD::AND && N0.hasOneUse()) {
  2139. if (ConstantSDNode *AndRHS =
  2140. dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  2141. const APInt &AndRHSC = AndRHS->getAPIntValue();
  2142. if ((-AndRHSC).isPowerOf2() && (AndRHSC & C1) == C1) {
  2143. unsigned ShiftBits = AndRHSC.countTrailingZeros();
  2144. EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
  2145. getPointerTy() : getShiftAmountTy(N0.getValueType());
  2146. EVT CmpTy = N0.getValueType();
  2147. SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0.getOperand(0),
  2148. DAG.getConstant(ShiftBits, ShiftTy));
  2149. SDValue CmpRHS = DAG.getConstant(C1.lshr(ShiftBits), CmpTy);
  2150. return DAG.getSetCC(dl, VT, Shift, CmpRHS, Cond);
  2151. }
  2152. }
  2153. } else if (Cond == ISD::SETULT || Cond == ISD::SETUGE ||
  2154. Cond == ISD::SETULE || Cond == ISD::SETUGT) {
  2155. bool AdjOne = (Cond == ISD::SETULE || Cond == ISD::SETUGT);
  2156. // X < 0x100000000 -> (X >> 32) < 1
  2157. // X >= 0x100000000 -> (X >> 32) >= 1
  2158. // X <= 0x0ffffffff -> (X >> 32) < 1
  2159. // X > 0x0ffffffff -> (X >> 32) >= 1
  2160. unsigned ShiftBits;
  2161. APInt NewC = C1;
  2162. ISD::CondCode NewCond = Cond;
  2163. if (AdjOne) {
  2164. ShiftBits = C1.countTrailingOnes();
  2165. NewC = NewC + 1;
  2166. NewCond = (Cond == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
  2167. } else {
  2168. ShiftBits = C1.countTrailingZeros();
  2169. }
  2170. NewC = NewC.lshr(ShiftBits);
  2171. if (ShiftBits && isLegalICmpImmediate(NewC.getSExtValue())) {
  2172. EVT ShiftTy = DCI.isBeforeLegalizeOps() ?
  2173. getPointerTy() : getShiftAmountTy(N0.getValueType());
  2174. EVT CmpTy = N0.getValueType();
  2175. SDValue Shift = DAG.getNode(ISD::SRL, dl, CmpTy, N0,
  2176. DAG.getConstant(ShiftBits, ShiftTy));
  2177. SDValue CmpRHS = DAG.getConstant(NewC, CmpTy);
  2178. return DAG.getSetCC(dl, VT, Shift, CmpRHS, NewCond);
  2179. }
  2180. }
  2181. }
  2182. }
  2183. if (isa<ConstantFPSDNode>(N0.getNode())) {
  2184. // Constant fold or commute setcc.
  2185. SDValue O = DAG.FoldSetCC(VT, N0, N1, Cond, dl);
  2186. if (O.getNode()) return O;
  2187. } else if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
  2188. // If the RHS of an FP comparison is a constant, simplify it away in
  2189. // some cases.
  2190. if (CFP->getValueAPF().isNaN()) {
  2191. // If an operand is known to be a nan, we can fold it.
  2192. switch (ISD::getUnorderedFlavor(Cond)) {
  2193. default: llvm_unreachable("Unknown flavor!");
  2194. case 0: // Known false.
  2195. return DAG.getConstant(0, VT);
  2196. case 1: // Known true.
  2197. return DAG.getConstant(1, VT);
  2198. case 2: // Undefined.
  2199. return DAG.getUNDEF(VT);
  2200. }
  2201. }
  2202. // Otherwise, we know the RHS is not a NaN. Simplify the node to drop the
  2203. // constant if knowing that the operand is non-nan is enough. We prefer to
  2204. // have SETO(x,x) instead of SETO(x, 0.0) because this avoids having to
  2205. // materialize 0.0.
  2206. if (Cond == ISD::SETO || Cond == ISD::SETUO)
  2207. return DAG.getSetCC(dl, VT, N0, N0, Cond);
  2208. // If the condition is not legal, see if we can find an equivalent one
  2209. // which is legal.
  2210. if (!isCondCodeLegal(Cond, N0.getValueType())) {
  2211. // If the comparison was an awkward floating-point == or != and one of
  2212. // the comparison operands is infinity or negative infinity, convert the
  2213. // condition to a less-awkward <= or >=.
  2214. if (CFP->getValueAPF().isInfinity()) {
  2215. if (CFP->getValueAPF().isNegative()) {
  2216. if (Cond == ISD::SETOEQ &&
  2217. isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
  2218. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLE);
  2219. if (Cond == ISD::SETUEQ &&
  2220. isCondCodeLegal(ISD::SETOLE, N0.getValueType()))
  2221. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULE);
  2222. if (Cond == ISD::SETUNE &&
  2223. isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
  2224. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGT);
  2225. if (Cond == ISD::SETONE &&
  2226. isCondCodeLegal(ISD::SETUGT, N0.getValueType()))
  2227. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGT);
  2228. } else {
  2229. if (Cond == ISD::SETOEQ &&
  2230. isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
  2231. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOGE);
  2232. if (Cond == ISD::SETUEQ &&
  2233. isCondCodeLegal(ISD::SETOGE, N0.getValueType()))
  2234. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETUGE);
  2235. if (Cond == ISD::SETUNE &&
  2236. isCondCodeLegal(ISD::SETULT, N0.getValueType()))
  2237. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETULT);
  2238. if (Cond == ISD::SETONE &&
  2239. isCondCodeLegal(ISD::SETULT, N0.getValueType()))
  2240. return DAG.getSetCC(dl, VT, N0, N1, ISD::SETOLT);
  2241. }
  2242. }
  2243. }
  2244. }
  2245. if (N0 == N1) {
  2246. // The sext(setcc()) => setcc() optimization relies on the appropriate
  2247. // constant being emitted.
  2248. uint64_t EqVal = 0;
  2249. switch (getBooleanContents(N0.getValueType().isVector())) {
  2250. case UndefinedBooleanContent:
  2251. case ZeroOrOneBooleanContent:
  2252. EqVal = ISD::isTrueWhenEqual(Cond);
  2253. break;
  2254. case ZeroOrNegativeOneBooleanContent:
  2255. EqVal = ISD::isTrueWhenEqual(Cond) ? -1 : 0;
  2256. break;
  2257. }
  2258. // We can always fold X == X for integer setcc's.
  2259. if (N0.getValueType().isInteger()) {
  2260. return DAG.getConstant(EqVal, VT);
  2261. }
  2262. unsigned UOF = ISD::getUnorderedFlavor(Cond);
  2263. if (UOF == 2) // FP operators that are undefined on NaNs.
  2264. return DAG.getConstant(EqVal, VT);
  2265. if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
  2266. return DAG.getConstant(EqVal, VT);
  2267. // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
  2268. // if it is not already.
  2269. ISD::CondCode NewCond = UOF == 0 ? ISD::SETO : ISD::SETUO;
  2270. if (NewCond != Cond && (DCI.isBeforeLegalizeOps() ||
  2271. getCondCodeAction(NewCond, N0.getValueType()) == Legal))
  2272. return DAG.getSetCC(dl, VT, N0, N1, NewCond);
  2273. }
  2274. if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
  2275. N0.getValueType().isInteger()) {
  2276. if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
  2277. N0.getOpcode() == ISD::XOR) {
  2278. // Simplify (X+Y) == (X+Z) --> Y == Z
  2279. if (N0.getOpcode() == N1.getOpcode()) {
  2280. if (N0.getOperand(0) == N1.getOperand(0))
  2281. return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(1), Cond);
  2282. if (N0.getOperand(1) == N1.getOperand(1))
  2283. return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(0), Cond);
  2284. if (DAG.isCommutativeBinOp(N0.getOpcode())) {
  2285. // If X op Y == Y op X, try other combinations.
  2286. if (N0.getOperand(0) == N1.getOperand(1))
  2287. return DAG.getSetCC(dl, VT, N0.getOperand(1), N1.getOperand(0),
  2288. Cond);
  2289. if (N0.getOperand(1) == N1.getOperand(0))
  2290. return DAG.getSetCC(dl, VT, N0.getOperand(0), N1.getOperand(1),
  2291. Cond);
  2292. }
  2293. }
  2294. // If RHS is a legal immediate value for a compare instruction, we need
  2295. // to be careful about increasing register pressure needlessly.
  2296. bool LegalRHSImm = false;
  2297. if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(N1)) {
  2298. if (ConstantSDNode *LHSR = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
  2299. // Turn (X+C1) == C2 --> X == C2-C1
  2300. if (N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse()) {
  2301. return DAG.getSetCC(dl, VT, N0.getOperand(0),
  2302. DAG.getConstant(RHSC->getAPIntValue()-
  2303. LHSR->getAPIntValue(),
  2304. N0.getValueType()), Cond);
  2305. }
  2306. // Turn (X^C1) == C2 into X == C1^C2 iff X&~C1 = 0.
  2307. if (N0.getOpcode() == ISD::XOR)
  2308. // If we know that all of the inverted bits are zero, don't bother
  2309. // performing the inversion.
  2310. if (DAG.MaskedValueIsZero(N0.getOperand(0), ~LHSR->getAPIntValue()))
  2311. return
  2312. DAG.getSetCC(dl, VT, N0.getOperand(0),
  2313. DAG.getConstant(LHSR->getAPIntValue() ^
  2314. RHSC->getAPIntValue(),
  2315. N0.getValueType()),
  2316. Cond);
  2317. }
  2318. // Turn (C1-X) == C2 --> X == C1-C2
  2319. if (ConstantSDNode *SUBC = dyn_cast<ConstantSDNode>(N0.getOperand(0))) {
  2320. if (N0.getOpcode() == ISD::SUB && N0.getNode()->hasOneUse()) {
  2321. return
  2322. DAG.getSetCC(dl, VT, N0.getOperand(1),
  2323. DAG.getConstant(SUBC->getAPIntValue() -
  2324. RHSC->getAPIntValue(),
  2325. N0.getValueType()),
  2326. Cond);
  2327. }
  2328. }
  2329. // Could RHSC fold directly into a compare?
  2330. if (RHSC->getValueType(0).getSizeInBits() <= 64)
  2331. LegalRHSImm = isLegalICmpImmediate(RHSC->getSExtValue());
  2332. }
  2333. // Simplify (X+Z) == X --> Z == 0
  2334. // Don't do this if X is an immediate that can fold into a cmp
  2335. // instruction and X+Z has other uses. It could be an induction variable
  2336. // chain, and the transform would increase register pressure.
  2337. if (!LegalRHSImm || N0.getNode()->hasOneUse()) {
  2338. if (N0.getOperand(0) == N1)
  2339. return DAG.getSetCC(dl, VT, N0.getOperand(1),
  2340. DAG.getConstant(0, N0.getValueType()), Cond);
  2341. if (N0.getOperand(1) == N1) {
  2342. if (DAG.isCommutativeBinOp(N0.getOpcode()))
  2343. return DAG.getSetCC(dl, VT, N0.getOperand(0),
  2344. DAG.getConstant(0, N0.getValueType()), Cond);
  2345. if (N0.getNode()->hasOneUse()) {
  2346. assert(N0.getOpcode() == ISD::SUB && "Unexpected operation!");
  2347. // (Z-X) == X --> Z == X<<1
  2348. SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N1,
  2349. DAG.getConstant(1, getShiftAmountTy(N1.getValueType())));
  2350. if (!DCI.isCalledByLegalizer())
  2351. DCI.AddToWorklist(SH.getNode());
  2352. return DAG.getSetCC(dl, VT, N0.getOperand(0), SH, Cond);
  2353. }
  2354. }
  2355. }
  2356. }
  2357. if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
  2358. N1.getOpcode() == ISD::XOR) {
  2359. // Simplify X == (X+Z) --> Z == 0
  2360. if (N1.getOperand(0) == N0)
  2361. return DAG.getSetCC(dl, VT, N1.getOperand(1),
  2362. DAG.getConstant(0, N1.getValueType()), Cond);
  2363. if (N1.getOperand(1) == N0) {
  2364. if (DAG.isCommutativeBinOp(N1.getOpcode()))
  2365. return DAG.getSetCC(dl, VT, N1.getOperand(0),
  2366. DAG.getConstant(0, N1.getValueType()), Cond);
  2367. if (N1.getNode()->hasOneUse()) {
  2368. assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
  2369. // X == (Z-X) --> X<<1 == Z
  2370. SDValue SH = DAG.getNode(ISD::SHL, dl, N1.getValueType(), N0,
  2371. DAG.getConstant(1, getShiftAmountTy(N0.getValueType())));
  2372. if (!DCI.isCalledByLegalizer())
  2373. DCI.AddToWorklist(SH.getNode());
  2374. return DAG.getSetCC(dl, VT, SH, N1.getOperand(0), Cond);
  2375. }
  2376. }
  2377. }
  2378. // Simplify x&y == y to x&y != 0 if y has exactly one bit set.
  2379. // Note that where y is variable and is known to have at most
  2380. // one bit set (for example, if it is z&1) we cannot do this;
  2381. // the expressions are not equivalent when y==0.
  2382. if (N0.getOpcode() == ISD::AND)
  2383. if (N0.getOperand(0) == N1 || N0.getOperand(1) == N1) {
  2384. if (ValueHasExactlyOneBitSet(N1, DAG)) {
  2385. Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
  2386. SDValue Zero = DAG.getConstant(0, N1.getValueType());
  2387. return DAG.getSetCC(dl, VT, N0, Zero, Cond);
  2388. }
  2389. }
  2390. if (N1.getOpcode() == ISD::AND)
  2391. if (N1.getOperand(0) == N0 || N1.getOperand(1) == N0) {
  2392. if (ValueHasExactlyOneBitSet(N0, DAG)) {
  2393. Cond = ISD::getSetCCInverse(Cond, /*isInteger=*/true);
  2394. SDValue Zero = DAG.getConstant(0, N0.getValueType());
  2395. return DAG.getSetCC(dl, VT, N1, Zero, Cond);
  2396. }
  2397. }
  2398. }
  2399. // Fold away ALL boolean setcc's.
  2400. SDValue Temp;
  2401. if (N0.getValueType() == MVT::i1 && foldBooleans) {
  2402. switch (Cond) {
  2403. default: llvm_unreachable("Unknown integer setcc!");
  2404. case ISD::SETEQ: // X == Y -> ~(X^Y)
  2405. Temp = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
  2406. N0 = DAG.getNOT(dl, Temp, MVT::i1);
  2407. if (!DCI.isCalledByLegalizer())
  2408. DCI.AddToWorklist(Temp.getNode());
  2409. break;
  2410. case ISD::SETNE: // X != Y --> (X^Y)
  2411. N0 = DAG.getNode(ISD::XOR, dl, MVT::i1, N0, N1);
  2412. break;
  2413. case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
  2414. case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
  2415. Temp = DAG.getNOT(dl, N0, MVT::i1);
  2416. N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N1, Temp);
  2417. if (!DCI.isCalledByLegalizer())
  2418. DCI.AddToWorklist(Temp.getNode());
  2419. break;
  2420. case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
  2421. case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
  2422. Temp = DAG.getNOT(dl, N1, MVT::i1);
  2423. N0 = DAG.getNode(ISD::AND, dl, MVT::i1, N0, Temp);
  2424. if (!DCI.isCalledByLegalizer())
  2425. DCI.AddToWorklist(Temp.getNode());
  2426. break;
  2427. case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
  2428. case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
  2429. Temp = DAG.getNOT(dl, N0, MVT::i1);
  2430. N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N1, Temp);
  2431. if (!DCI.isCalledByLegalizer())
  2432. DCI.AddToWorklist(Temp.getNode());
  2433. break;
  2434. case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
  2435. case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
  2436. Temp = DAG.getNOT(dl, N1, MVT::i1);
  2437. N0 = DAG.getNode(ISD::OR, dl, MVT::i1, N0, Temp);
  2438. break;
  2439. }
  2440. if (VT != MVT::i1) {
  2441. if (!DCI.isCalledByLegalizer())
  2442. DCI.AddToWorklist(N0.getNode());
  2443. // FIXME: If running after legalize, we probably can't do this.
  2444. N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0);
  2445. }
  2446. return N0;
  2447. }
  2448. // Could not fold it.
  2449. return SDValue();
  2450. }
  2451. /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
  2452. /// node is a GlobalAddress + offset.
  2453. bool TargetLowering::isGAPlusOffset(SDNode *N, const GlobalValue *&GA,
  2454. int64_t &Offset) const {
  2455. if (isa<GlobalAddressSDNode>(N)) {
  2456. GlobalAddressSDNode *GASD = cast<GlobalAddressSDNode>(N);
  2457. GA = GASD->getGlobal();
  2458. Offset += GASD->getOffset();
  2459. return true;
  2460. }
  2461. if (N->getOpcode() == ISD::ADD) {
  2462. SDValue N1 = N->getOperand(0);
  2463. SDValue N2 = N->getOperand(1);
  2464. if (isGAPlusOffset(N1.getNode(), GA, Offset)) {
  2465. ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
  2466. if (V) {
  2467. Offset += V->getSExtValue();
  2468. return true;
  2469. }
  2470. } else if (isGAPlusOffset(N2.getNode(), GA, Offset)) {
  2471. ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
  2472. if (V) {
  2473. Offset += V->getSExtValue();
  2474. return true;
  2475. }
  2476. }
  2477. }
  2478. return false;
  2479. }
  2480. SDValue TargetLowering::
  2481. PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const {
  2482. // Default implementation: no optimization.
  2483. return SDValue();
  2484. }
  2485. //===----------------------------------------------------------------------===//
  2486. // Inline Assembler Implementation Methods
  2487. //===----------------------------------------------------------------------===//
  2488. TargetLowering::ConstraintType
  2489. TargetLowering::getConstraintType(const std::string &Constraint) const {
  2490. if (Constraint.size() == 1) {
  2491. switch (Constraint[0]) {
  2492. default: break;
  2493. case 'r': return C_RegisterClass;
  2494. case 'm': // memory
  2495. case 'o': // offsetable
  2496. case 'V': // not offsetable
  2497. return C_Memory;
  2498. case 'i': // Simple Integer or Relocatable Constant
  2499. case 'n': // Simple Integer
  2500. case 'E': // Floating Point Constant
  2501. case 'F': // Floating Point Constant
  2502. case 's': // Relocatable Constant
  2503. case 'p': // Address.
  2504. case 'X': // Allow ANY value.
  2505. case 'I': // Target registers.
  2506. case 'J':
  2507. case 'K':
  2508. case 'L':
  2509. case 'M':
  2510. case 'N':
  2511. case 'O':
  2512. case 'P':
  2513. case '<':
  2514. case '>':
  2515. return C_Other;
  2516. }
  2517. }
  2518. if (Constraint.size() > 1 && Constraint[0] == '{' &&
  2519. Constraint[Constraint.size()-1] == '}')
  2520. return C_Register;
  2521. return C_Unknown;
  2522. }
  2523. /// LowerXConstraint - try to replace an X constraint, which matches anything,
  2524. /// with another that has more specific requirements based on the type of the
  2525. /// corresponding operand.
  2526. const char *TargetLowering::LowerXConstraint(EVT ConstraintVT) const{
  2527. if (ConstraintVT.isInteger())
  2528. return "r";
  2529. if (ConstraintVT.isFloatingPoint())
  2530. return "f"; // works for many targets
  2531. return 0;
  2532. }
  2533. /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
  2534. /// vector. If it is invalid, don't add anything to Ops.
  2535. void TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
  2536. std::string &Constraint,
  2537. std::vector<SDValue> &Ops,
  2538. SelectionDAG &DAG) const {
  2539. if (Constraint.length() > 1) return;
  2540. char ConstraintLetter = Constraint[0];
  2541. switch (ConstraintLetter) {
  2542. default: break;
  2543. case 'X': // Allows any operand; labels (basic block) use this.
  2544. if (Op.getOpcode() == ISD::BasicBlock) {
  2545. Ops.push_back(Op);
  2546. return;
  2547. }
  2548. // fall through
  2549. case 'i': // Simple Integer or Relocatable Constant
  2550. case 'n': // Simple Integer
  2551. case 's': { // Relocatable Constant
  2552. // These operands are interested in values of the form (GV+C), where C may
  2553. // be folded in as an offset of GV, or it may be explicitly added. Also, it
  2554. // is possible and fine if either GV or C are missing.
  2555. ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
  2556. GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
  2557. // If we have "(add GV, C)", pull out GV/C
  2558. if (Op.getOpcode() == ISD::ADD) {
  2559. C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
  2560. GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
  2561. if (C == 0 || GA == 0) {
  2562. C = dyn_cast<ConstantSDNode>(Op.getOperand(0));
  2563. GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(1));
  2564. }
  2565. if (C == 0 || GA == 0)
  2566. C = 0, GA = 0;
  2567. }
  2568. // If we find a valid operand, map to the TargetXXX version so that the
  2569. // value itself doesn't get selected.
  2570. if (GA) { // Either &GV or &GV+C
  2571. if (ConstraintLetter != 'n') {
  2572. int64_t Offs = GA->getOffset();
  2573. if (C) Offs += C->getZExtValue();
  2574. Ops.push_back(DAG.getTargetGlobalAddress(GA->getGlobal(),
  2575. C ? C->getDebugLoc() : DebugLoc(),
  2576. Op.getValueType(), Offs));
  2577. return;
  2578. }
  2579. }
  2580. if (C) { // just C, no GV.
  2581. // Simple constants are not allowed for 's'.
  2582. if (ConstraintLetter != 's') {
  2583. // gcc prints these as sign extended. Sign extend value to 64 bits
  2584. // now; without this it would get ZExt'd later in
  2585. // ScheduleDAGSDNodes::EmitNode, which is very generic.
  2586. Ops.push_back(DAG.getTargetConstant(C->getAPIntValue().getSExtValue(),
  2587. MVT::i64));
  2588. return;
  2589. }
  2590. }
  2591. break;
  2592. }
  2593. }
  2594. }
  2595. std::pair<unsigned, const TargetRegisterClass*> TargetLowering::
  2596. getRegForInlineAsmConstraint(const std::string &Constraint,
  2597. EVT VT) const {
  2598. if (Constraint[0] != '{')
  2599. return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
  2600. assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
  2601. // Remove the braces from around the name.
  2602. StringRef RegName(Constraint.data()+1, Constraint.size()-2);
  2603. std::pair<unsigned, const TargetRegisterClass*> R =
  2604. std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
  2605. // Figure out which register class contains this reg.
  2606. const TargetRegisterInfo *RI = TM.getRegisterInfo();
  2607. for (TargetRegisterInfo::regclass_iterator RCI = RI->regclass_begin(),
  2608. E = RI->regclass_end(); RCI != E; ++RCI) {
  2609. const TargetRegisterClass *RC = *RCI;
  2610. // If none of the value types for this register class are valid, we
  2611. // can't use it. For example, 64-bit reg classes on 32-bit targets.
  2612. if (!isLegalRC(RC))
  2613. continue;
  2614. for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
  2615. I != E; ++I) {
  2616. if (RegName.equals_lower(RI->getName(*I))) {
  2617. std::pair<unsigned, const TargetRegisterClass*> S =
  2618. std::make_pair(*I, RC);
  2619. // If this register class has the requested value type, return it,
  2620. // otherwise keep searching and return the first class found
  2621. // if no other is found which explicitly has the requested type.
  2622. if (RC->hasType(VT))
  2623. return S;
  2624. else if (!R.second)
  2625. R = S;
  2626. }
  2627. }
  2628. }
  2629. return R;
  2630. }
  2631. //===----------------------------------------------------------------------===//
  2632. // Constraint Selection.
  2633. /// isMatchingInputConstraint - Return true of this is an input operand that is
  2634. /// a matching constraint like "4".
  2635. bool TargetLowering::AsmOperandInfo::isMatchingInputConstraint() const {
  2636. assert(!ConstraintCode.empty() && "No known constraint!");
  2637. return isdigit(ConstraintCode[0]);
  2638. }
  2639. /// getMatchedOperand - If this is an input matching constraint, this method
  2640. /// returns the output operand it matches.
  2641. unsigned TargetLowering::AsmOperandInfo::getMatchedOperand() const {
  2642. assert(!ConstraintCode.empty() && "No known constraint!");
  2643. return atoi(ConstraintCode.c_str());
  2644. }
  2645. /// ParseConstraints - Split up the constraint string from the inline
  2646. /// assembly value into the specific constraints and their prefixes,
  2647. /// and also tie in the associated operand values.
  2648. /// If this returns an empty vector, and if the constraint string itself
  2649. /// isn't empty, there was an error parsing.
  2650. TargetLowering::AsmOperandInfoVector TargetLowering::ParseConstraints(
  2651. ImmutableCallSite CS) const {
  2652. /// ConstraintOperands - Information about all of the constraints.
  2653. AsmOperandInfoVector ConstraintOperands;
  2654. const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
  2655. unsigned maCount = 0; // Largest number of multiple alternative constraints.
  2656. // Do a prepass over the constraints, canonicalizing them, and building up the
  2657. // ConstraintOperands list.
  2658. InlineAsm::ConstraintInfoVector
  2659. ConstraintInfos = IA->ParseConstraints();
  2660. unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
  2661. unsigned ResNo = 0; // ResNo - The result number of the next output.
  2662. for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
  2663. ConstraintOperands.push_back(AsmOperandInfo(ConstraintInfos[i]));
  2664. AsmOperandInfo &OpInfo = ConstraintOperands.back();
  2665. // Update multiple alternative constraint count.
  2666. if (OpInfo.multipleAlternatives.size() > maCount)
  2667. maCount = OpInfo.multipleAlternatives.size();
  2668. OpInfo.ConstraintVT = MVT::Other;
  2669. // Compute the value type for each operand.
  2670. switch (OpInfo.Type) {
  2671. case InlineAsm::isOutput:
  2672. // Indirect outputs just consume an argument.
  2673. if (OpInfo.isIndirect) {
  2674. OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
  2675. break;
  2676. }
  2677. // The return value of the call is this value. As such, there is no
  2678. // corresponding argument.
  2679. assert(!CS.getType()->isVoidTy() &&
  2680. "Bad inline asm!");
  2681. if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
  2682. OpInfo.ConstraintVT = getValueType(STy->getElementType(ResNo));
  2683. } else {
  2684. assert(ResNo == 0 && "Asm only has one result!");
  2685. OpInfo.ConstraintVT = getValueType(CS.getType());
  2686. }
  2687. ++ResNo;
  2688. break;
  2689. case InlineAsm::isInput:
  2690. OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
  2691. break;
  2692. case InlineAsm::isClobber:
  2693. // Nothing to do.
  2694. break;
  2695. }
  2696. if (OpInfo.CallOperandVal) {
  2697. llvm::Type *OpTy = OpInfo.CallOperandVal->getType();
  2698. if (OpInfo.isIndirect) {
  2699. llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
  2700. if (!PtrTy)
  2701. report_fatal_error("Indirect operand for inline asm not a pointer!");
  2702. OpTy = PtrTy->getElementType();
  2703. }
  2704. // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
  2705. if (StructType *STy = dyn_cast<StructType>(OpTy))
  2706. if (STy->getNumElements() == 1)
  2707. OpTy = STy->getElementType(0);
  2708. // If OpTy is not a single value, it may be a struct/union that we
  2709. // can tile with integers.
  2710. if (!OpTy->isSingleValueType() && OpTy->isSized()) {
  2711. unsigned BitSize = TD->getTypeSizeInBits(OpTy);
  2712. switch (BitSize) {
  2713. default: break;
  2714. case 1:
  2715. case 8:
  2716. case 16:
  2717. case 32:
  2718. case 64:
  2719. case 128:
  2720. OpInfo.ConstraintVT =
  2721. EVT::getEVT(IntegerType::get(OpTy->getContext(), BitSize), true);
  2722. break;
  2723. }
  2724. } else if (PointerType *PT = dyn_cast<PointerType>(OpTy)) {
  2725. OpInfo.ConstraintVT = MVT::getIntegerVT(
  2726. 8*TD->getPointerSize(PT->getAddressSpace()));
  2727. } else {
  2728. OpInfo.ConstraintVT = EVT::getEVT(OpTy, true);
  2729. }
  2730. }
  2731. }
  2732. // If we have multiple alternative constraints, select the best alternative.
  2733. if (ConstraintInfos.size()) {
  2734. if (maCount) {
  2735. unsigned bestMAIndex = 0;
  2736. int bestWeight = -1;
  2737. // weight: -1 = invalid match, and 0 = so-so match to 5 = good match.
  2738. int weight = -1;
  2739. unsigned maIndex;
  2740. // Compute the sums of the weights for each alternative, keeping track
  2741. // of the best (highest weight) one so far.
  2742. for (maIndex = 0; maIndex < maCount; ++maIndex) {
  2743. int weightSum = 0;
  2744. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2745. cIndex != eIndex; ++cIndex) {
  2746. AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
  2747. if (OpInfo.Type == InlineAsm::isClobber)
  2748. continue;
  2749. // If this is an output operand with a matching input operand,
  2750. // look up the matching input. If their types mismatch, e.g. one
  2751. // is an integer, the other is floating point, or their sizes are
  2752. // different, flag it as an maCantMatch.
  2753. if (OpInfo.hasMatchingInput()) {
  2754. AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
  2755. if (OpInfo.ConstraintVT != Input.ConstraintVT) {
  2756. if ((OpInfo.ConstraintVT.isInteger() !=
  2757. Input.ConstraintVT.isInteger()) ||
  2758. (OpInfo.ConstraintVT.getSizeInBits() !=
  2759. Input.ConstraintVT.getSizeInBits())) {
  2760. weightSum = -1; // Can't match.
  2761. break;
  2762. }
  2763. }
  2764. }
  2765. weight = getMultipleConstraintMatchWeight(OpInfo, maIndex);
  2766. if (weight == -1) {
  2767. weightSum = -1;
  2768. break;
  2769. }
  2770. weightSum += weight;
  2771. }
  2772. // Update best.
  2773. if (weightSum > bestWeight) {
  2774. bestWeight = weightSum;
  2775. bestMAIndex = maIndex;
  2776. }
  2777. }
  2778. // Now select chosen alternative in each constraint.
  2779. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2780. cIndex != eIndex; ++cIndex) {
  2781. AsmOperandInfo& cInfo = ConstraintOperands[cIndex];
  2782. if (cInfo.Type == InlineAsm::isClobber)
  2783. continue;
  2784. cInfo.selectAlternative(bestMAIndex);
  2785. }
  2786. }
  2787. }
  2788. // Check and hook up tied operands, choose constraint code to use.
  2789. for (unsigned cIndex = 0, eIndex = ConstraintOperands.size();
  2790. cIndex != eIndex; ++cIndex) {
  2791. AsmOperandInfo& OpInfo = ConstraintOperands[cIndex];
  2792. // If this is an output operand with a matching input operand, look up the
  2793. // matching input. If their types mismatch, e.g. one is an integer, the
  2794. // other is floating point, or their sizes are different, flag it as an
  2795. // error.
  2796. if (OpInfo.hasMatchingInput()) {
  2797. AsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
  2798. if (OpInfo.ConstraintVT != Input.ConstraintVT) {
  2799. std::pair<unsigned, const TargetRegisterClass*> MatchRC =
  2800. getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
  2801. OpInfo.ConstraintVT);
  2802. std::pair<unsigned, const TargetRegisterClass*> InputRC =
  2803. getRegForInlineAsmConstraint(Input.ConstraintCode,
  2804. Input.ConstraintVT);
  2805. if ((OpInfo.ConstraintVT.isInteger() !=
  2806. Input.ConstraintVT.isInteger()) ||
  2807. (MatchRC.second != InputRC.second)) {
  2808. report_fatal_error("Unsupported asm: input constraint"
  2809. " with a matching output constraint of"
  2810. " incompatible type!");
  2811. }
  2812. }
  2813. }
  2814. }
  2815. return ConstraintOperands;
  2816. }
  2817. /// getConstraintGenerality - Return an integer indicating how general CT
  2818. /// is.
  2819. static unsigned getConstraintGenerality(TargetLowering::ConstraintType CT) {
  2820. switch (CT) {
  2821. case TargetLowering::C_Other:
  2822. case TargetLowering::C_Unknown:
  2823. return 0;
  2824. case TargetLowering::C_Register:
  2825. return 1;
  2826. case TargetLowering::C_RegisterClass:
  2827. return 2;
  2828. case TargetLowering::C_Memory:
  2829. return 3;
  2830. }
  2831. llvm_unreachable("Invalid constraint type");
  2832. }
  2833. /// Examine constraint type and operand type and determine a weight value.
  2834. /// This object must already have been set up with the operand type
  2835. /// and the current alternative constraint selected.
  2836. TargetLowering::ConstraintWeight
  2837. TargetLowering::getMultipleConstraintMatchWeight(
  2838. AsmOperandInfo &info, int maIndex) const {
  2839. InlineAsm::ConstraintCodeVector *rCodes;
  2840. if (maIndex >= (int)info.multipleAlternatives.size())
  2841. rCodes = &info.Codes;
  2842. else
  2843. rCodes = &info.multipleAlternatives[maIndex].Codes;
  2844. ConstraintWeight BestWeight = CW_Invalid;
  2845. // Loop over the options, keeping track of the most general one.
  2846. for (unsigned i = 0, e = rCodes->size(); i != e; ++i) {
  2847. ConstraintWeight weight =
  2848. getSingleConstraintMatchWeight(info, (*rCodes)[i].c_str());
  2849. if (weight > BestWeight)
  2850. BestWeight = weight;
  2851. }
  2852. return BestWeight;
  2853. }
  2854. /// Examine constraint type and operand type and determine a weight value.
  2855. /// This object must already have been set up with the operand type
  2856. /// and the current alternative constraint selected.
  2857. TargetLowering::ConstraintWeight
  2858. TargetLowering::getSingleConstraintMatchWeight(
  2859. AsmOperandInfo &info, const char *constraint) const {
  2860. ConstraintWeight weight = CW_Invalid;
  2861. Value *CallOperandVal = info.CallOperandVal;
  2862. // If we don't have a value, we can't do a match,
  2863. // but allow it at the lowest weight.
  2864. if (CallOperandVal == NULL)
  2865. return CW_Default;
  2866. // Look at the constraint type.
  2867. switch (*constraint) {
  2868. case 'i': // immediate integer.
  2869. case 'n': // immediate integer with a known value.
  2870. if (isa<ConstantInt>(CallOperandVal))
  2871. weight = CW_Constant;
  2872. break;
  2873. case 's': // non-explicit intregal immediate.
  2874. if (isa<GlobalValue>(CallOperandVal))
  2875. weight = CW_Constant;
  2876. break;
  2877. case 'E': // immediate float if host format.
  2878. case 'F': // immediate float.
  2879. if (isa<ConstantFP>(CallOperandVal))
  2880. weight = CW_Constant;
  2881. break;
  2882. case '<': // memory operand with autodecrement.
  2883. case '>': // memory operand with autoincrement.
  2884. case 'm': // memory operand.
  2885. case 'o': // offsettable memory operand
  2886. case 'V': // non-offsettable memory operand
  2887. weight = CW_Memory;
  2888. break;
  2889. case 'r': // general register.
  2890. case 'g': // general register, memory operand or immediate integer.
  2891. // note: Clang converts "g" to "imr".
  2892. if (CallOperandVal->getType()->isIntegerTy())
  2893. weight = CW_Register;
  2894. break;
  2895. case 'X': // any operand.
  2896. default:
  2897. weight = CW_Default;
  2898. break;
  2899. }
  2900. return weight;
  2901. }
  2902. /// ChooseConstraint - If there are multiple different constraints that we
  2903. /// could pick for this operand (e.g. "imr") try to pick the 'best' one.
  2904. /// This is somewhat tricky: constraints fall into four classes:
  2905. /// Other -> immediates and magic values
  2906. /// Register -> one specific register
  2907. /// RegisterClass -> a group of regs
  2908. /// Memory -> memory
  2909. /// Ideally, we would pick the most specific constraint possible: if we have
  2910. /// something that fits into a register, we would pick it. The problem here
  2911. /// is that if we have something that could either be in a register or in
  2912. /// memory that use of the register could cause selection of *other*
  2913. /// operands to fail: they might only succeed if we pick memory. Because of
  2914. /// this the heuristic we use is:
  2915. ///
  2916. /// 1) If there is an 'other' constraint, and if the operand is valid for
  2917. /// that constraint, use it. This makes us take advantage of 'i'
  2918. /// constraints when available.
  2919. /// 2) Otherwise, pick the most general constraint present. This prefers
  2920. /// 'm' over 'r', for example.
  2921. ///
  2922. static void ChooseConstraint(TargetLowering::AsmOperandInfo &OpInfo,
  2923. const TargetLowering &TLI,
  2924. SDValue Op, SelectionDAG *DAG) {
  2925. assert(OpInfo.Codes.size() > 1 && "Doesn't have multiple constraint options");
  2926. unsigned BestIdx = 0;
  2927. TargetLowering::ConstraintType BestType = TargetLowering::C_Unknown;
  2928. int BestGenerality = -1;
  2929. // Loop over the options, keeping track of the most general one.
  2930. for (unsigned i = 0, e = OpInfo.Codes.size(); i != e; ++i) {
  2931. TargetLowering::ConstraintType CType =
  2932. TLI.getConstraintType(OpInfo.Codes[i]);
  2933. // If this is an 'other' constraint, see if the operand is valid for it.
  2934. // For example, on X86 we might have an 'rI' constraint. If the operand
  2935. // is an integer in the range [0..31] we want to use I (saving a load
  2936. // of a register), otherwise we must use 'r'.
  2937. if (CType == TargetLowering::C_Other && Op.getNode()) {
  2938. assert(OpInfo.Codes[i].size() == 1 &&
  2939. "Unhandled multi-letter 'other' constraint");
  2940. std::vector<SDValue> ResultOps;
  2941. TLI.LowerAsmOperandForConstraint(Op, OpInfo.Codes[i],
  2942. ResultOps, *DAG);
  2943. if (!ResultOps.empty()) {
  2944. BestType = CType;
  2945. BestIdx = i;
  2946. break;
  2947. }
  2948. }
  2949. // Things with matching constraints can only be registers, per gcc
  2950. // documentation. This mainly affects "g" constraints.
  2951. if (CType == TargetLowering::C_Memory && OpInfo.hasMatchingInput())
  2952. continue;
  2953. // This constraint letter is more general than the previous one, use it.
  2954. int Generality = getConstraintGenerality(CType);
  2955. if (Generality > BestGenerality) {
  2956. BestType = CType;
  2957. BestIdx = i;
  2958. BestGenerality = Generality;
  2959. }
  2960. }
  2961. OpInfo.ConstraintCode = OpInfo.Codes[BestIdx];
  2962. OpInfo.ConstraintType = BestType;
  2963. }
  2964. /// ComputeConstraintToUse - Determines the constraint code and constraint
  2965. /// type to use for the specific AsmOperandInfo, setting
  2966. /// OpInfo.ConstraintCode and OpInfo.ConstraintType.
  2967. void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
  2968. SDValue Op,
  2969. SelectionDAG *DAG) const {
  2970. assert(!OpInfo.Codes.empty() && "Must have at least one constraint");
  2971. // Single-letter constraints ('r') are very common.
  2972. if (OpInfo.Codes.size() == 1) {
  2973. OpInfo.ConstraintCode = OpInfo.Codes[0];
  2974. OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
  2975. } else {
  2976. ChooseConstraint(OpInfo, *this, Op, DAG);
  2977. }
  2978. // 'X' matches anything.
  2979. if (OpInfo.ConstraintCode == "X" && OpInfo.CallOperandVal) {
  2980. // Labels and constants are handled elsewhere ('X' is the only thing
  2981. // that matches labels). For Functions, the type here is the type of
  2982. // the result, which is not what we want to look at; leave them alone.
  2983. Value *v = OpInfo.CallOperandVal;
  2984. if (isa<BasicBlock>(v) || isa<ConstantInt>(v) || isa<Function>(v)) {
  2985. OpInfo.CallOperandVal = v;
  2986. return;
  2987. }
  2988. // Otherwise, try to resolve it to something we know about by looking at
  2989. // the actual operand type.
  2990. if (const char *Repl = LowerXConstraint(OpInfo.ConstraintVT)) {
  2991. OpInfo.ConstraintCode = Repl;
  2992. OpInfo.ConstraintType = getConstraintType(OpInfo.ConstraintCode);
  2993. }
  2994. }
  2995. }
  2996. //===----------------------------------------------------------------------===//
  2997. // Loop Strength Reduction hooks
  2998. //===----------------------------------------------------------------------===//
  2999. /// isLegalAddressingMode - Return true if the addressing mode represented
  3000. /// by AM is legal for this target, for a load/store of the specified type.
  3001. bool TargetLowering::isLegalAddressingMode(const AddrMode &AM,
  3002. Type *Ty) const {
  3003. // The default implementation of this implements a conservative RISCy, r+r and
  3004. // r+i addr mode.
  3005. // Allows a sign-extended 16-bit immediate field.
  3006. if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
  3007. return false;
  3008. // No global is ever allowed as a base.
  3009. if (AM.BaseGV)
  3010. return false;
  3011. // Only support r+r,
  3012. switch (AM.Scale) {
  3013. case 0: // "r+i" or just "i", depending on HasBaseReg.
  3014. break;
  3015. case 1:
  3016. if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
  3017. return false;
  3018. // Otherwise we have r+r or r+i.
  3019. break;
  3020. case 2:
  3021. if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
  3022. return false;
  3023. // Allow 2*r as r+r.
  3024. break;
  3025. }
  3026. return true;
  3027. }
  3028. /// BuildExactDiv - Given an exact SDIV by a constant, create a multiplication
  3029. /// with the multiplicative inverse of the constant.
  3030. SDValue TargetLowering::BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
  3031. SelectionDAG &DAG) const {
  3032. ConstantSDNode *C = cast<ConstantSDNode>(Op2);
  3033. APInt d = C->getAPIntValue();
  3034. assert(d != 0 && "Division by zero!");
  3035. // Shift the value upfront if it is even, so the LSB is one.
  3036. unsigned ShAmt = d.countTrailingZeros();
  3037. if (ShAmt) {
  3038. // TODO: For UDIV use SRL instead of SRA.
  3039. SDValue Amt = DAG.getConstant(ShAmt, getShiftAmountTy(Op1.getValueType()));
  3040. Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt);
  3041. d = d.ashr(ShAmt);
  3042. }
  3043. // Calculate the multiplicative inverse, using Newton's method.
  3044. APInt t, xn = d;
  3045. while ((t = d*xn) != 1)
  3046. xn *= APInt(d.getBitWidth(), 2) - t;
  3047. Op2 = DAG.getConstant(xn, Op1.getValueType());
  3048. return DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
  3049. }
  3050. /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
  3051. /// return a DAG expression to select that will generate the same value by
  3052. /// multiplying by a magic number. See:
  3053. /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
  3054. SDValue TargetLowering::
  3055. BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
  3056. std::vector<SDNode*> *Created) const {
  3057. EVT VT = N->getValueType(0);
  3058. DebugLoc dl= N->getDebugLoc();
  3059. // Check to see if we can do this.
  3060. // FIXME: We should be more aggressive here.
  3061. if (!isTypeLegal(VT))
  3062. return SDValue();
  3063. APInt d = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
  3064. APInt::ms magics = d.magic();
  3065. // Multiply the numerator (operand 0) by the magic value
  3066. // FIXME: We should support doing a MUL in a wider type
  3067. SDValue Q;
  3068. if (IsAfterLegalization ? isOperationLegal(ISD::MULHS, VT) :
  3069. isOperationLegalOrCustom(ISD::MULHS, VT))
  3070. Q = DAG.getNode(ISD::MULHS, dl, VT, N->getOperand(0),
  3071. DAG.getConstant(magics.m, VT));
  3072. else if (IsAfterLegalization ? isOperationLegal(ISD::SMUL_LOHI, VT) :
  3073. isOperationLegalOrCustom(ISD::SMUL_LOHI, VT))
  3074. Q = SDValue(DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(VT, VT),
  3075. N->getOperand(0),
  3076. DAG.getConstant(magics.m, VT)).getNode(), 1);
  3077. else
  3078. return SDValue(); // No mulhs or equvialent
  3079. // If d > 0 and m < 0, add the numerator
  3080. if (d.isStrictlyPositive() && magics.m.isNegative()) {
  3081. Q = DAG.getNode(ISD::ADD, dl, VT, Q, N->getOperand(0));
  3082. if (Created)
  3083. Created->push_back(Q.getNode());
  3084. }
  3085. // If d < 0 and m > 0, subtract the numerator.
  3086. if (d.isNegative() && magics.m.isStrictlyPositive()) {
  3087. Q = DAG.getNode(ISD::SUB, dl, VT, Q, N->getOperand(0));
  3088. if (Created)
  3089. Created->push_back(Q.getNode());
  3090. }
  3091. // Shift right algebraic if shift value is nonzero
  3092. if (magics.s > 0) {
  3093. Q = DAG.getNode(ISD::SRA, dl, VT, Q,
  3094. DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
  3095. if (Created)
  3096. Created->push_back(Q.getNode());
  3097. }
  3098. // Extract the sign bit and add it to the quotient
  3099. SDValue T =
  3100. DAG.getNode(ISD::SRL, dl, VT, Q, DAG.getConstant(VT.getSizeInBits()-1,
  3101. getShiftAmountTy(Q.getValueType())));
  3102. if (Created)
  3103. Created->push_back(T.getNode());
  3104. return DAG.getNode(ISD::ADD, dl, VT, Q, T);
  3105. }
  3106. /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
  3107. /// return a DAG expression to select that will generate the same value by
  3108. /// multiplying by a magic number. See:
  3109. /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
  3110. SDValue TargetLowering::
  3111. BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
  3112. std::vector<SDNode*> *Created) const {
  3113. EVT VT = N->getValueType(0);
  3114. DebugLoc dl = N->getDebugLoc();
  3115. // Check to see if we can do this.
  3116. // FIXME: We should be more aggressive here.
  3117. if (!isTypeLegal(VT))
  3118. return SDValue();
  3119. // FIXME: We should use a narrower constant when the upper
  3120. // bits are known to be zero.
  3121. const APInt &N1C = cast<ConstantSDNode>(N->getOperand(1))->getAPIntValue();
  3122. APInt::mu magics = N1C.magicu();
  3123. SDValue Q = N->getOperand(0);
  3124. // If the divisor is even, we can avoid using the expensive fixup by shifting
  3125. // the divided value upfront.
  3126. if (magics.a != 0 && !N1C[0]) {
  3127. unsigned Shift = N1C.countTrailingZeros();
  3128. Q = DAG.getNode(ISD::SRL, dl, VT, Q,
  3129. DAG.getConstant(Shift, getShiftAmountTy(Q.getValueType())));
  3130. if (Created)
  3131. Created->push_back(Q.getNode());
  3132. // Get magic number for the shifted divisor.
  3133. magics = N1C.lshr(Shift).magicu(Shift);
  3134. assert(magics.a == 0 && "Should use cheap fixup now");
  3135. }
  3136. // Multiply the numerator (operand 0) by the magic value
  3137. // FIXME: We should support doing a MUL in a wider type
  3138. if (IsAfterLegalization ? isOperationLegal(ISD::MULHU, VT) :
  3139. isOperationLegalOrCustom(ISD::MULHU, VT))
  3140. Q = DAG.getNode(ISD::MULHU, dl, VT, Q, DAG.getConstant(magics.m, VT));
  3141. else if (IsAfterLegalization ? isOperationLegal(ISD::UMUL_LOHI, VT) :
  3142. isOperationLegalOrCustom(ISD::UMUL_LOHI, VT))
  3143. Q = SDValue(DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(VT, VT), Q,
  3144. DAG.getConstant(magics.m, VT)).getNode(), 1);
  3145. else
  3146. return SDValue(); // No mulhu or equvialent
  3147. if (Created)
  3148. Created->push_back(Q.getNode());
  3149. if (magics.a == 0) {
  3150. assert(magics.s < N1C.getBitWidth() &&
  3151. "We shouldn't generate an undefined shift!");
  3152. return DAG.getNode(ISD::SRL, dl, VT, Q,
  3153. DAG.getConstant(magics.s, getShiftAmountTy(Q.getValueType())));
  3154. } else {
  3155. SDValue NPQ = DAG.getNode(ISD::SUB, dl, VT, N->getOperand(0), Q);
  3156. if (Created)
  3157. Created->push_back(NPQ.getNode());
  3158. NPQ = DAG.getNode(ISD::SRL, dl, VT, NPQ,
  3159. DAG.getConstant(1, getShiftAmountTy(NPQ.getValueType())));
  3160. if (Created)
  3161. Created->push_back(NPQ.getNode());
  3162. NPQ = DAG.getNode(ISD::ADD, dl, VT, NPQ, Q);
  3163. if (Created)
  3164. Created->push_back(NPQ.getNode());
  3165. return DAG.getNode(ISD::SRL, dl, VT, NPQ,
  3166. DAG.getConstant(magics.s-1, getShiftAmountTy(NPQ.getValueType())));
  3167. }
  3168. }