[Bug middle-end/114289] Non-optimal assembly for accessing bit-fields in packed structs

2024-03-08 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114289

--- Comment #3 from Dan Stahlke  ---
Variants that generate more reasonable results:

struct foo {
int x:32;
} __attribute__((packed));

struct foo {
int x:16;
} __attribute__((packed));

struct foo {
int x:31;
};

struct foo {
int x:31;
} __attribute__((packed, aligned(4)));

[Bug tree-optimization/114289] Non-optimal assembly for accessing bit-fields in packed structs

2024-03-08 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114289

Dan Stahlke  changed:

   What|Removed |Added

 Target|x86_64-linux-gnu|
   Keywords|missed-optimization |
  Component|middle-end  |tree-optimization

--- Comment #1 from Dan Stahlke  ---
Variants that generate more reasonable results:

struct foo {
int x:32;
} __attribute__((packed));

struct foo {
int x:16;
} __attribute__((packed));

struct foo {
int x:31;
};

struct foo {
int x:31;
} __attribute__((packed, aligned(4)));

[Bug tree-optimization/114289] New: Non-optimal assembly for accessing bit-fields in packed structs

2024-03-08 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114289

Bug ID: 114289
   Summary: Non-optimal assembly for accessing bit-fields in
packed structs
   Product: gcc
   Version: 13.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: tree-optimization
  Assignee: unassigned at gcc dot gnu.org
  Reporter: dan at stahlke dot org
  Target Milestone: ---

// gcc -O3 -g -Wall -march=haswell -mavx2
// https://godbolt.org/z/Yfb9dnYx4

struct foo {
int x:31;
} __attribute__((packed));

int fx(struct foo const *o) { return o->x; }

Disassembly:

movzx   eax, BYTE PTR [rdi+1]
movzx   edx, BYTE PTR [rdi]
sal rax, 8
or  rax, rdx
movzx   edx, BYTE PTR [rdi+2]
sal rdx, 16
or  rdx, rax
movzx   eax, BYTE PTR [rdi+3]
and eax, 127
sal rax, 24
or  rax, rdx
sal rax, 33
sar rax, 33
ret

Compare to clang:

mov eax, dword ptr [rdi]
add eax, eax
sar eax
ret

[Bug tree-optimization/105329] [12/13 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6

2022-09-30 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329

--- Comment #25 from Dan Stahlke  ---
The test case I just posted appears in the bug 105651 discussion.  So maybe or
maybe not related to the present discussion.

[Bug tree-optimization/105329] [12/13 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6

2022-09-30 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329

Dan Stahlke  changed:

   What|Removed |Added

 CC||dan at stahlke dot org

--- Comment #24 from Dan Stahlke  ---
Here is another test case, broken on 12.2 with "-std=c++20 -Wrestrict -O3" but
working on trunk and working on 12.2 with -O2 rather than -O3.

#include 
#include  // std::ignore

int main()
{
std::string s = "b";
std::ignore = "a" + std::move(s);
}

[Bug c++/106893] New: auto deduces wrong type for function pointer

2022-09-08 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106893

Bug ID: 106893
   Summary: auto deduces wrong type for function pointer
   Product: gcc
   Version: 12.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: dan at stahlke dot org
  Target Milestone: ---

This works in 11.3.0 but not in 12.2.0.

struct IntegerCoordinate {
int x() const;
int y() const;
};

template 
struct CoordTraits
{
static auto GetX(T const ) { return p.x(); }
static auto GetY(T const ) { return p.y(); }
using Ordinate = decltype(GetX(T{}));
};

template 
struct Node {
static constexpr auto GetX = Traits::GetX; // works because of the `using
Ordinate = ...`
static constexpr auto GetY = Traits::GetY; // compilation error
// This compiles
//static constexpr auto GetX = ::GetX;
//static constexpr auto GetY = ::GetY;
// This compiles
//using fptr = int(*)(IntegerCoordinate const &);
//static constexpr fptr GetX = Traits::GetX;
//static constexpr fptr GetY = Traits::GetY;

// Not needed for minimal test case
auto x(auto const ) const { return GetX(p); }
auto y(auto const ) const { return GetY(p); }
};

int main() {
Node> node;
// Not needed for minimal test case
node.x(IntegerCoordinate{});
node.y(IntegerCoordinate{});
}


$ g++ -std=c++20 gcc-function-ptr-bug.cpp -c -o gcc-function-ptr-bug.o

gcc-function-ptr-bug.cpp: In instantiation of ‘constexpr auto (* const
Node >::GetY)(const IntegerCoordinate&)’:
gcc-function-ptr-bug.cpp:17:27:   required from ‘struct
Node >’
gcc-function-ptr-bug.cpp:32:42:   required from here
gcc-function-ptr-bug.cpp:17:27: error: invalid conversion from ‘int (*)(const
IntegerCoordinate&)’ to ‘auto (*)(const IntegerCoordinate&)’ [-fpermissive]
   17 | static constexpr auto GetY = Traits::GetY; // compilation error
  |   ^~~~
  |   |
  |   int (*)(const IntegerCoordinate&)
gcc-function-ptr-bug.cpp: In instantiation of ‘auto Node::y(const
auto:2&) const [with auto:2 = IntegerCoordinate; Traits =
CoordTraits]’:
gcc-function-ptr-bug.cpp:35:11:   required from here
gcc-function-ptr-bug.cpp:28:46: error: use of
‘Node >::GetY’ before deduction of ‘auto’
   28 | auto y(auto const ) const { return GetY(p); }
  |  ^~~

[Bug c++/102970] New: stable_sort uninitialized value with -funroll-loops -fno-tree-vectorize

2021-10-27 Thread dan at stahlke dot org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102970

Bug ID: 102970
   Summary: stable_sort uninitialized value with -funroll-loops
-fno-tree-vectorize
   Product: gcc
   Version: 11.2.0
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c++
  Assignee: unassigned at gcc dot gnu.org
  Reporter: dan at stahlke dot org
  Target Milestone: ---

Created attachment 51682
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51682=edit
demonstration source code

The attached file, compiled with 11.2.0, using the command line in the comment,
causes std::stable_sort to access uninitialized memory.  In fact, it already
goes wrong by the time it passes the inputs to the first comparison operation. 
The problem goes away if the copy constructor of the box struct is defaulted
(see the "#if").

It only happens with "-funroll-loops -fno-tree-vectorize".

Here is a Godbolt: https://godbolt.org/z/6PsdPj6q3