public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Safe transposition of logical and operands
@ 2023-09-17 19:33 Paul Floyd
  2023-09-17 20:51 ` Jonathan Wakely
  2023-09-18 10:30 ` Andreas Schwab
  0 siblings, 2 replies; 14+ messages in thread
From: Paul Floyd @ 2023-09-17 19:33 UTC (permalink / raw)
  To: gcc

Hi

I'm looking at a Valgrind issue. Full details here

https://bugs.kde.org/show_bug.cgi?id=472329

This code

void foo(std::optional<int64_t> f) {
   std::cout << (f ? *f : 0) << std::endl;
   std::set<int64_t> test;
   test.emplace(0);
   auto it{test.begin()};
   while (it != test.end()) {
     int64_t b{*it};
// Valgrind error on next line
     if (f && *f != b) {
[snip]

int main() { foo(std::nullopt); }

generates (using g++ 12.2 on FreeBSD 13.2 amd64)

	movq	40(%rbx), %rax
Error on next line:
	cmpq	%r14, %rax	# f, SR.252
	je	.L47
	cmpb	$0, -97(%rbp)	#, %sfp
	jne	.L69

Unless I'm completely mistaken cmpq %r14, %rax is the quadword 
comparison of *f != b and cmpb	$0, -97(%rbp) is the 
std::optional::operator bool() comparison with 0. That means that g++ 
has transposed the order of evaluation.


The first cmpq/je generates a

==49599== Conditional jump or move depends on uninitialised value(s)

Valgrind has machinery to detect transformations like this and to 
convert them back into bit accurate bitwise ands.

However, Valgrind thinks that these transpositions can't be done when 
there are potentially trapping memory loads like *f.

So who is right? Is this a safe transformation?

Regards
Paul

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-09-19  5:03 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-17 19:33 Safe transposition of logical and operands Paul Floyd
2023-09-17 20:51 ` Jonathan Wakely
2023-09-18  7:03   ` Paul Floyd
2023-09-18  7:23     ` Jonathan Wakely
2023-09-18  8:00       ` Richard Biener
2023-09-18 14:46         ` Floyd, Paul
2023-09-18 14:55           ` Richard Biener
2023-09-18 17:56             ` Paul Floyd
2023-09-18 19:09               ` Martin Uecker
2023-09-18 20:15                 ` Paul Floyd
2023-09-18 20:52                   ` Martin Uecker
2023-09-19  5:03                     ` Paul Floyd
2023-09-18  9:36       ` Jonathan Wakely
2023-09-18 10:30 ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).