LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 48559 - Optimizer does not take advantage of `-value == ~(value - 1)`
Summary: Optimizer does not take advantage of `-value == ~(value - 1)`
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: 11.0
Hardware: PC Linux
: P enhancement
Assignee: Sanjay Patel
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-19 09:38 PST by ju.orth
Modified: 2020-12-21 09:52 PST (History)
2 users (show)

See Also:
Fixed By Commit(s): 38ca7face67e


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ju.orth 2020-12-19 09:38:01 PST
Test case:

long f(long align) {
	return (align - 1) & -align;
}

long g(long align) {
	return (align - 1) & ~(align - 1);
}

int h(long align) {
	return -align == ~(align - 1);
}

Generated assembly:

f:
	leaq	-1(%rdi), %rax
	negq	%rdi
	andq	%rdi, %rax
	retq
g:
	xorl	%eax, %eax
	retq
h:
	movl	$1, %eax
	retq
Comment 1 Sanjay Patel 2020-12-20 08:18:59 PST
This is a special-case of a more general pattern that ends in a bitwise logic op and includes add/sub operands. For example:
https://alive2.llvm.org/ce/z/mxoxUz

I think these are the generalizations we should handle in instsimplify:
https://rise4fun.com/Alive/gR0
Comment 2 Sanjay Patel 2020-12-21 09:52:10 PST
https://reviews.llvm.org/rG38ca7face67e