[PATCH] Prefer small negative constants in InstCombine

Tim Northover tnorthover at apple.com
Wed Jun 26 06:56:27 PDT 2013


Hi,

The attached patch adds a heuristic to ShrinkDemandedConstant so that small negative constants are preferred over large ones even if as an unsigned value the transformation would be an improvement.

The motivating case is added as a test:
define i32 @foo(i64 %in) {
  %sub = sub i64 %in, 4
  %shift = lshr i64 %sub, 2
  %res = trunc i64 %shift to i32

  ret i32 %res
}

On x86-64 this currently generates:

_foo:
	movabsq	$17179869180, %rax      ## imm = 0x3FFFFFFFC
	addq	%rdi, %rax
	shrq	$2, %rax
	ret

with an unnecessary and large movabsq. With this change the fact that we’re really subtracting 4 survives to CodeGen and we just emit

_foo:
	leaq -4(%rdi), %rax
	shrq $2, %rax
	ret

Does anyone have any objections to this change? Is it likely to cause more damage than good for later passes?

Cheers.

Tim.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: large-const.diff
Type: application/octet-stream
Size: 2942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130626/d88539ef/attachment.obj>


More information about the llvm-commits mailing list