Skip to content

Sequential checked_add and saturating_add of constants don't get combined together #52203

Closed
@scottmcm

Description

@scottmcm
pub fn test_saturating(a: u32) -> u32 {
    a.saturating_add(7).saturating_add(13)
}

gets compiled to

playground::test_saturating:
	add	edi, 7
	mov	eax, -1
	cmovb	edi, eax
	add	edi, 13
	cmovae	eax, edi
	ret

but ideally it would get compiled to a.saturating_add(20), aka

playground::test_saturating:
	add	edi, 20
	mov	eax, -1
	cmovae	eax, edi
	ret

Ditto for checked_add.

Playground repro: https://play.rust-lang.org/?gist=eeea31ea85491e38bdd6d456b9f6e47f&version=nightly&mode=release&edition=2015

Found looking at step_by, which would like .next(); .nth(step) to collapse nicely for ranges (#52065).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.WG-llvmWorking group: LLVM backend code generation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions