https://gcc.gnu.org/g:982250b230967776f0da708a1572b78a38561e08
commit r14-9609-g982250b230967776f0da708a1572b78a38561e08
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Fri Mar 22 09:22:04 2024 +0100

    bitint: Some bitint store fixes [PR114405]
    
    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.
    
    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/torture/bitint-66.c: New test.

Diff:
---
 gcc/gimple-lower-bitint.cc               |  12 +-
 gcc/testsuite/gcc.dg/torture/bitint-66.c | 187 +++++++++++++++++++++++++++++++
 2 files changed, 193 insertions(+), 6 deletions(-)

diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc
index 1ce13ca2579..8c268005dfd 100644
--- a/gcc/gimple-lower-bitint.cc
+++ b/gcc/gimple-lower-bitint.cc
@@ -2737,7 +2737,7 @@ bitint_large_huge::lower_mergeable_stmt (gimple *stmt, 
tree_code &cmp_code,
                  && 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 (gimple *stmt, 
tree_code &cmp_code,
          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 (gimple *stmt, 
tree_code &cmp_code,
   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)
diff --git a/gcc/testsuite/gcc.dg/torture/bitint-66.c 
b/gcc/testsuite/gcc.dg/torture/bitint-66.c
new file mode 100644
index 00000000000..8fb88d216d7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/bitint-66.c
@@ -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 = 
350158003380505374986530089755747774884710871132956781945995047863711187105859447644843198218140686774905278646556854711912708123763151870063629176165769271113028654479830157397075438943018123276131343038411208269067907997079455719403536797179924566182284755285475105868977492335261807627967153385764741622084822475795014727515642442806664677590983061305914705596990843549102948821927236518415923939267571803051959500247745642200250305632929473547817592991949664987268764796880772389214831109940540275108471440422817532903868269218913490896525332128476375392036639442918647357541723528908344268626321269611639850469720253013990186845667407859963267220766594009418937889601227496239164856014153600022746753770302452284312756435898866680852929503992761788218035829079769340187112322935747029882048971893909556633907818109695235550134927356869571529053653703092754092636708853379140877900495842013951097549196974650844255172249805330877818509804670098128567306960742979
 
1082863937720363133946467525524010564287075567637758084732017172141047455206427992617580550815928960027813965779184943695557298023811509978571508728024962099182393100276770938801496977501453221290529191707994544455914915588553786944320399814952892186491327305493650491301298739096872276143632469207421142657782533061515800356121739815674406797038013250088617313837500401028668180111843683715716935600017461138566379508596358574860422159527540615757911933142849871431645450287870714448554104685176379505506971638045465087788808209915530760495658556575009545684472633970773573545730681863019275796086282000890703269610699088064368287729705477856097268128947567682212096027403292419009729989265019637473801546274078976865649211970688846186860064103084074813635036821812133507954727582393374679472037664928409287598204318128431435855712870798426055735715466237867792817819073526868349271401785462996715301423115737788509106444783120106351065029738591580428731993950662845143572120836405243362743498872
 
9866237807202969467835254010038134458240638537208900261071474543301726397124628778009813885844516116875814757951712204725755866073002681855066543296947579858825103198060954359453660770400428464367183262945659741442129886496782750203291436130782872835833210931632788692244249384390726841766304668343729453316125242663035845457840953337688285031237708574070672758470607188306503291550138232031426679939617451739589817112669177611683906458764129736114273328542188257601463679042157104326452743228219904706515617554527404048486259443988771977512551077861016524071064966970245017842256022606252061533848052435944977075011683626924927864280265758204665377061594059017099850979465876532419313486240717486906824159353605563308622625101457788519462541166177560281487799503798108588440825426607303000287409968420712049604991695790921639810867480138128306445418986912743800521671354614532833872265863112799715535687739478987679787018336970919695660390682264636150577566966764721802539998608991596596546556960
 
4176417580699783098866150550657026870536456931249954783324541468003598940150893787833260369611478503176058963483137266255537810619233787186876504200493831639902992204620149011509757239763439907907398093565472391698731232032570784856352037222051876132946725857067124760426775901106390300691138378884154380054843441277687809210852749356198741739489897158401938968492203923681045185140314091790525245850705960662770018199109905535975087865402745200429810225431794266922841157688473007603197986027827195027114799778418742799302568163144523663310246454657851708009679849157702743020563732885898484835862711028160583538286021548486945129848858717909421620824272380055813765567540357900911555134257410554711661438345253199869522263180092262194016646538428602556804480706644075136724584497820747390113654733582296509887484724906293707440223914666253611130544126125746781342160328565568636422654143984119352738041080449937832692492623109163432789176981386117140611123933738971853888821465711867947693009146
 
3178003337002690745535000910099139823423054400570722563991119900152106055649435326973441892697654388302016781496592193153509671682645826225018963774859237607386519281005435261305120732747461933793476114666342613493511850341382550408242433297726217919022744997017191325484219401730950463287898590618616621469214043342953600907571361052872189444798676746889688487876545361065162269234116893837366568397027458386544938279882315085140350973067705314070862559548324563961874057421065353019862485927613113378349503352280269641302910155942633905005858493263181592221041394619951035085286544681240310627671393958192954678546051113759341905606030625021702928581180301398061680184130469472201143575876617796204970166461351258318204926076114454486325646718621383006085182968388375257726507868938567461716414603543509258824731832273352356728384543225612083768664794478007858026541770192313312136051160221909476492627753435480704032785944771947100224584478232849203935960345908318143622209766900777455475673126
 
8016051487933312972616137658853650317461494196070488717404975633548685934461815467879673043738028501662385765726339447319024260930739951505693992826178343374149857545719100281434407604507469945111545032437656099424412573336369079168862003094855689600545933448699263081128774135084325432935970025160634165357145443378006220662160445647943128195352561425127099417647196668384335109349031134696463855906874865675419780741981139645587274839419864230821395469418683636935930834609291353227775312716176266653441965834697927579160177680186708970308315749650687662459599387055171808726719664673659254693742629742210875019198336374705508371903093542410995253013501601426016328214864707009534388699971539616972918925748729111792476147132099151408067415671771268758562336472333173561377424856607016076217345813200209837622293261269296049102357129259291847303310788402335473055104793925018108266365566334144957977011022080954274483513076181095572139673622797279076831219272711108256081425991375813518135442665
 
798185129409104195642582574005698068543758814799878628816260575002894250723437469293380754329373751478069967151946701724550270952583590048204731437620446357611597703063140065615278014456977266371201795995646459704425842307781189527028486070466105764495249910844961272734411722090576328496690678921549811732110098855139371357058264972132635004768261508328436120677484741464226299067993312922923316398805257936717699519222213192533192746600829048907097046462066206043987729487350920474035219484795840791105847762753227958460953129122508970162044661061074090561192446511631511339514682638233698374095743691902757648070625949939841339410289027899541368321851192767145693326883564251857041615916159616490433637711319076039315762942426225728417924096927156791836747462094823404471357021596544260573448601630171546391583358297381573516226814145190492133449221202013592319137021uwb;
+  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
+}

Reply via email to