Skip to content

Commit

Permalink
[Thumb1] Fix cost calculation for complemented immediates
Browse files Browse the repository at this point in the history
Materializing something like "-3" can be done as 2 instructions:
  MOV r0, #3
  MVN r0, r0

This has a cost of 2, not 3. It looks like we were already trying to detect this pattern in TII::getIntImmCost(), but were taking the complement of the zero-extended value instead of the sign-extended value which is unlikely to ever produce a number < 256.

There were no tests failing after changing this... :/

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280928 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
James Molloy committed Sep 8, 2016
1 parent d88990b commit 210e77b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Target/ARM/ARMTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
// Thumb1.
if (SImmVal >= 0 && SImmVal < 256)
return 1;
if ((~ZImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
if ((~SImmVal < 256) || ARM_AM::isThumbImmShiftedVal(ZImmVal))
return 2;
// Load from constantpool.
return 3;
Expand Down
21 changes: 21 additions & 0 deletions test/CodeGen/ARM/immcost.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: llc %s -o - -O1 -debug-only=consthoist 2>&1 | FileCheck %s
; REQUIRES: asserts

target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
target triple = "thumbv6m-apple-ios8.0.0"

declare void @g(i32)

; CHECK: Collect constant i32 -3 from call void @g(i32 -3) with cost 2
define void @f(i1 %cond) {
entry:
call void @g(i32 -3)
br i1 %cond, label %true, label %ret

true:
call void @g(i32 -3)
br label %ret

ret:
ret void
}

0 comments on commit 210e77b

Please sign in to comment.