With the tip of the 4.2 branch, the following program returns 1.  Mainline
returns 0.  Is this defined behavior?  I could not find anything on the
subject.

struct S {
  unsigned b4:4;
  unsigned b6:6;
} s;

int main(void){
  s.b6 = 31;
  s.b4 = s.b6;
  s.b6 = s.b4;
  return s.b6 == 15 ? 0 : 1;
}


before FRE (-fdump-tree-ccp):

;; Function main (main)

main ()
{
  short unsigned int D.1882;
  short unsigned int D.1881;
  int D.1880;
  <unnamed type> D.1879;
  <unnamed type> D.1878;
  <unnamed type> D.1877;
  <unnamed type> D.1876;

<bb 2>:
  s.b6 = 31;
  D.1876_3 = s.b6;
  D.1877_4 = (<unnamed type>) D.1876_3;
  s.b4 = D.1877_4;
  D.1878_7 = s.b4;
  D.1879_8 = (<unnamed type>) D.1878_7;
  s.b6 = D.1879_8;
  D.1881_10 = BIT_FIELD_REF <s, 16, 0>;
  D.1882_11 = D.1881_10 & 1008;
  D.1880_12 = D.1882_11 != 240;
  return D.1880_12;

}


after FRE (-fdump-tree-fre):

;; Function main (main)

main ()
{
  short unsigned int D.1882;
  short unsigned int D.1881;
  int D.1880;
  <unnamed type> D.1879;
  <unnamed type> D.1878;
  <unnamed type> D.1877;
  <unnamed type> D.1876;

<bb 2>:
  s.b6 = 31;
  D.1876_3 = 31;
  D.1877_4 = (<unnamed type>) D.1876_3;
  s.b4 = D.1877_4;
  D.1878_7 = D.1877_4;
  D.1879_8 = 31;
  s.b6 = D.1879_8;
  D.1881_10 = BIT_FIELD_REF <s, 16, 0>;
  D.1882_11 = D.1881_10 & 1008;
  D.1880_12 = D.1882_11 != 240;
  return D.1880_12;

}


D.1879_8 was replaced by 31, ignoring the fact that the value should have been
truncated to 15 when assigned to s.b4.


-- 
           Summary: FRE ignores bit-field truncation
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: TabonyEE at austin dot rr dot com
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31136

Reply via email to