Hi! The following patch fixes some bugs in the handling of stores to large/huge _BitInt bitfields.
In the first 2 hunks we are processing the most significant limb of the actual type (not necessarily limb in the storage), and so we know it is either partial or full limb, so [1, limb_prec] bits rather than [0, limb_prec - 1] bits as the code actually assumed. So, those 2 spots are fixed by making sure if tprec is a multiple of limb_prec we actually use limb_prec bits rather than 0. Otherwise, it e.g. happily could create and use 0 precision INTEGER_TYPE even when it actually should have processed 64 bits, or for non-zero bo_bit could handle just say 1 bit rather than 64 bits plus 1 bit in the last hunk spot. In the last hunk we are dealing with the extra bits in the last storage limb, and the code was e.g. happily creating 65 bit precision INTEGER_TYPE, even when we really should use 1 bit precision in that case. Also, it used a wrong offset in that case. The large testcase covers all these cases. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-03-22 Jakub Jelinek <ja...@redhat.com> PR tree-optimization/114405 * gimple-lower-bitint.cc (bitint_large_huge::lower_mergeable_stmt): Set rprec to limb_prec rather than 0 if tprec is divisible by limb_prec. In the last bf_cur handling, set rprec to (tprec + bo_bit) % limb_prec rather than tprec % limb_prec and use just rprec instead of rprec + bo_bit. For build_bit_field_ref offset, divide (tprec + bo_bit) by limb_prec rather than just tprec. * gcc.dg/bitint-103.c: New test. --- gcc/gimple-lower-bitint.cc.jj 2024-03-20 15:03:30.868068343 +0100 +++ gcc/gimple-lower-bitint.cc 2024-03-21 12:51:26.728296098 +0100 @@ -2737,7 +2737,7 @@ bitint_large_huge::lower_mergeable_stmt && tree_fits_uhwi_p (idx)) { unsigned int tprec = TYPE_PRECISION (type); - unsigned int rprec = tprec % limb_prec; + unsigned int rprec = (tprec - 1) % limb_prec + 1; if (rprec + bo_bit < (unsigned) limb_prec) { tree ftype @@ -2882,7 +2882,7 @@ bitint_large_huge::lower_mergeable_stmt if (nlhs && i == cnt - 1) { unsigned int tprec = TYPE_PRECISION (type); - unsigned int rprec = tprec % limb_prec; + unsigned int rprec = (tprec - 1) % limb_prec + 1; if (rprec + bo_bit < (unsigned) limb_prec) { tree ftype @@ -2934,11 +2934,11 @@ bitint_large_huge::lower_mergeable_stmt if (bf_cur != NULL_TREE) { unsigned int tprec = TYPE_PRECISION (type); - unsigned int rprec = tprec % limb_prec; - tree ftype = build_nonstandard_integer_type (rprec + bo_bit, 1); + unsigned int rprec = (tprec + bo_bit) % limb_prec; + tree ftype = build_nonstandard_integer_type (rprec, 1); tree bfr = build_bit_field_ref (ftype, unshare_expr (nlhs), - rprec + bo_bit, - (bo_idx + tprec / limb_prec) + rprec, + (bo_idx + (tprec + bo_bit) / limb_prec) * limb_prec); rhs1 = bf_cur; if (bf_cur != ext) --- gcc/testsuite/gcc.dg/torture/bitint-66.c.jj 2024-03-21 11:53:00.790647163 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-66.c 2024-03-21 11:52:29.296082298 +0100 @@ -0,0 +1,187 @@ +/* PR tree-optimization/114405 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 22658 +struct S1 { unsigned _BitInt(22592) b : 22592; } s1; +struct S2 { unsigned _BitInt(22656) b : 22656; } s2; +struct S3 { unsigned _BitInt(22656) a : 1; unsigned _BitInt(22656) b : 22592; } s3; +struct S4 { unsigned _BitInt(22720) a : 1; unsigned _BitInt(22720) b : 22656; } s4; +struct S5 { unsigned _BitInt(22656) a : 63; unsigned _BitInt(22656) b : 22592; } s5; +struct S6 { unsigned _BitInt(22720) a : 63; unsigned _BitInt(22720) b : 22656; } s6; +struct S7 { unsigned _BitInt(22656) a : 63; unsigned _BitInt(22656) b : 22593; } s7; +struct S8 { unsigned _BitInt(22720) a : 63; unsigned _BitInt(22720) b : 22657; } s8; +struct S9 { unsigned _BitInt(22720) a : 63; unsigned _BitInt(22720) b : 22594; } s9; +struct S10 { unsigned _BitInt(22784) a : 63; unsigned _BitInt(22784) b : 22658; } s10; + +void +f1 () +{ + s1.b -= 1; +} + +void +f2 () +{ + s2.b -= 2; +} + +void +f3 () +{ + s3.b -= 3; +} + +void +f4 () +{ + s4.b -= 4; +} + +void +f5 () +{ + s5.b -= 5; +} + +void +f6 () +{ + s6.b -= 6; +} + +void +f7 () +{ + s7.b -= 7; +} + +void +f8 () +{ + s8.b -= 8; +} + +void +f9 () +{ + s9.b -= 9; +} + +void +f10 () +{ + s10.b -= 10; +} +#endif + +int +main () +{ +#if __BITINT_MAXWIDTH__ >= 22658 + unsigned _BitInt (22658) x = 35015800338050537498653008975574777488471087113295678194599504786371118710585944764484319821814068677490527864655685471191270812376315187006362917616576927111302865447983015739707543894301812327613134303841120826906790799707945571940353679717992456618228475528547510586897749233526180762796715338576474162208482247579501472751564244280666467759098306130591470559699084354910294882192723651841592393926757180305195950024774564220025030563292947354781759299194966498726876479688077238921483110994054027510847144042281753290386826921891349089652533212847637539203663944291864735754172352890834426862632126961163985046972025301399018684566740785996326722076659400941893788960122749623916485601415360002274675377030245228431275643589886668085292950399276178821803582907976934018711232293574702988204897189390955663390781810969523555013492735686957152905365370309275409263670885337914087790049584201395109754919697465084425517224980533087781850980467009812856730696074297910828639377203631339464675255240105642870755676377580847320171721410474552064279926175805508159289600278139657791849436955572980238115099785715087280249620991823931002767709388014969775014532212905291917079945444559149155885537869443203998149528921864913273054936504913012987390968722761436324692074211426577825330615158003561217398156744067970380132500886173138375004010286681801118436837157169356000174611385663795085963585748604221595275406157579119331428498714316454502878707144485541046851763795055069716380454650877888082099155307604956585565750095456844726339707735735457306818630192757960862820008907032696106990880643682877297054778560972681289475676822120960274032924190097299892650196374738015462740789768656492119706888461868600641030840748136350368218121335079547275823933746794720376649284092875982043181284314358557128707984260557357154662378677928178190735268683492714017854629967153014231157377885091064447831201063510650297385915804287319939506628451435721208364052433627434988729866237807202969467835254010038134458240638537208900261071474543301726397124628778009813885844516116875814757951712204725755866073002681855066543296947579858825103198060954359453660770400428464367183262945659741442129886496782750203291436130782872835833210931632788692244249384390726841766304668343729453316125242663035845457840953337688285031237708574070672758470607188306503291550138232031426679939617451739589817112669177611683906458764129736114273328542188257601463679042157104326452743228219904706515617554527404048486259443988771977512551077861016524071064966970245017842256022606252061533848052435944977075011683626924927864280265758204665377061594059017099850979465876532419313486240717486906824159353605563308622625101457788519462541166177560281487799503798108588440825426607303000287409968420712049604991695790921639810867480138128306445418986912743800521671354614532833872265863112799715535687739478987679787018336970919695660390682264636150577566966764721802539998608991596596546556960417641758069978309886615055065702687053645693124995478332454146800359894015089378783326036961147850317605896348313726625553781061923378718687650420049383163990299220462014901150975723976343990790739809356547239169873123203257078485635203722205187613294672585706712476042677590110639030069113837888415438005484344127768780921085274935619874173948989715840193896849220392368104518514031409179052524585070596066277001819910990553597508786540274520042981022543179426692284115768847300760319798602782719502711479977841874279930256816314452366331024645465785170800967984915770274302056373288589848483586271102816058353828602154848694512984885871790942162082427238005581376556754035790091155513425741055471166143834525319986952226318009226219401664653842860255680448070664407513672458449782074739011365473358229650988748472490629370744022391466625361113054412612574678134216032856556863642265414398411935273804108044993783269249262310916343278917698138611714061112393373897185388882146571186794769300914631780033370026907455350009100991398234230544005707225639911199001521060556494353269734418926976543883020167814965921931535096716826458262250189637748592376073865192810054352613051207327474619337934761146663426134935118503413825504082424332977262179190227449970171913254842194017309504632878985906186166214692140433429536009075713610528721894447986767468896884878765453610651622692341168938373665683970274583865449382798823150851403509730677053140708625595483245639618740574210653530198624859276131133783495033522802696413029101559426339050058584932631815922210413946199510350852865446812403106276713939581929546785460511137593419056060306250217029285811803013980616801841304694722011435758766177962049701664613512583182049260761144544863256467186213830060851829683883752577265078689385674617164146035435092588247318322733523567283845432256120837686647944780078580265417701923133121360511602219094764926277534354807040327859447719471002245844782328492039359603459083181436222097669007774554756731268016051487933312972616137658853650317461494196070488717404975633548685934461815467879673043738028501662385765726339447319024260930739951505693992826178343374149857545719100281434407604507469945111545032437656099424412573336369079168862003094855689600545933448699263081128774135084325432935970025160634165357145443378006220662160445647943128195352561425127099417647196668384335109349031134696463855906874865675419780741981139645587274839419864230821395469418683636935930834609291353227775312716176266653441965834697927579160177680186708970308315749650687662459599387055171808726719664673659254693742629742210875019198336374705508371903093542410995253013501601426016328214864707009534388699971539616972918925748729111792476147132099151408067415671771268758562336472333173561377424856607016076217345813200209837622293261269296049102357129259291847303310788402335473055104793925018108266365566334144957977011022080954274483513076181095572139673622797279076831219272711108256081425991375813518135442665798185129409104195642582574005698068543758814799878628816260575002894250723437469293380754329373751478069967151946701724550270952583590048204731437620446357611597703063140065615278014456977266371201795995646459704425842307781189527028486070466105764495249910844961272734411722090576328496690678921549811732110098855139371357058264972132635004768261508328436120677484741464226299067993312922923316398805257936717699519222213192533192746600829048907097046462066206043987729487350920474035219484795840791105847762753227958460953129122508970162044661061074090561192446511631511339514682638233698374095743691902757648070625949939841339410289027899541368321851192767145693326883564251857041615916159616490433637711319076039315762942426225728417924096927156791836747462094823404471357021596544260573448601630171546391583358297381573516226814145190492133449221202013592319137021uwb; + unsigned _BitInt (22658) y = ~x, a; + a = x >> (22658 - 22592); + s1.b = a; + f1 (); + if (s1.b != a - 1) + __builtin_abort (); + a = y >> (22658 - 22592); + s1.b = a; + f1 (); + if (s1.b != a - 1) + __builtin_abort (); + a = x >> (22658 - 22656); + s2.b = a; + f2 (); + if (s2.b != a - 2) + __builtin_abort (); + a = y >> (22658 - 22656); + s2.b = a; + f2 (); + if (s2.b != a - 2) + __builtin_abort (); + a = x >> (22658 - 22592); + s3.b = a; + f3 (); + if (s3.b != a - 3) + __builtin_abort (); + a = y >> (22658 - 22592); + s3.b = a; + f3 (); + if (s3.b != a - 3) + __builtin_abort (); + a = x >> (22658 - 22656); + s4.b = a; + f4 (); + if (s4.b != a - 4) + __builtin_abort (); + a = y >> (22658 - 22656); + s4.b = a; + f4 (); + if (s4.b != a - 4) + __builtin_abort (); + a = x >> (22658 - 22592); + s5.b = a; + f5 (); + if (s5.b != a - 5) + __builtin_abort (); + a = y >> (22658 - 22592); + s5.b = a; + f5 (); + if (s5.b != a - 5) + __builtin_abort (); + a = x >> (22658 - 22656); + s6.b = a; + f6 (); + if (s6.b != a - 6) + __builtin_abort (); + a = y >> (22658 - 22656); + s6.b = a; + f6 (); + if (s6.b != a - 6) + __builtin_abort (); + a = x >> (22658 - 22593); + s7.b = a; + f7 (); + if (s7.b != a - 7) + __builtin_abort (); + a = y >> (22658 - 22593); + s7.b = a; + f7 (); + if (s7.b != a - 7) + __builtin_abort (); + a = x >> (22658 - 22657); + s8.b = a; + f8 (); + if (s8.b != a - 8) + __builtin_abort (); + a = y >> (22658 - 22657); + s8.b = a; + f8 (); + if (s8.b != a - 8) + __builtin_abort (); + a = x >> (22658 - 22594); + s9.b = a; + f9 (); + if (s9.b != a - 9) + __builtin_abort (); + a = y >> (22658 - 22594); + s9.b = a; + f9 (); + if (s9.b != a - 9) + __builtin_abort (); + a = x >> (22658 - 22658); + s10.b = a; + f10 (); + if (s10.b != a - 10) + __builtin_abort (); + a = y >> (22658 - 22658); + s10.b = a; + f10 (); + if (s10.b != a - 10) + __builtin_abort (); +#endif +} Jakub