Skip to content

Commit 296c637

Browse files
committed
Update costs for ARM
Things that can be encoded in an immediate should not have a cost.
1 parent 2158036 commit 296c637

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,13 @@ InstructionCost ARMTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
428428
if (Opcode == Instruction::GetElementPtr && Idx != 0)
429429
return 0;
430430

431+
if ((Opcode == Instruction::Shl || Opcode == Instruction::LShr ||
432+
Opcode == Instruction::AShr) &&
433+
Idx == 1) {
434+
// Shifts are free (are we really going to get a shift of more than 64)?
435+
return 0;
436+
}
437+
431438
if (Opcode == Instruction::And) {
432439
// UXTB/UXTH
433440
if (Imm == 255 || Imm == 65535)
@@ -437,19 +444,38 @@ InstructionCost ARMTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
437444
getIntImmCost(~Imm, Ty, CostKind));
438445
}
439446

440-
if (Opcode == Instruction::Add)
447+
if (Opcode == Instruction::Add || Opcode == Instruction::Sub) {
448+
int64_t ImmVal = Imm.getSExtValue();
449+
if (!ST->isThumb())
450+
if (ARM_AM::getSOImmVal((uint32_t)ImmVal) != -1 ||
451+
ARM_AM::getSOImmVal(-(uint32_t)ImmVal) != -1)
452+
return 0;
453+
if (ST->isThumb2())
454+
if (ARM_AM::getT2SOImmVal((uint32_t)ImmVal) != -1 ||
455+
ARM_AM::getT2SOImmVal(-(uint32_t)ImmVal) != -1)
456+
return 0;
457+
// Thumb1 doesn't have cmn, and only 8-bit immediates.
458+
ImmVal = ImmVal < 0 ? -ImmVal : ImmVal;
459+
if (ImmVal >= 0 && ImmVal <= 255)
460+
return 0;
441461
// Conversion to SUB is free, and means we can use -Imm instead.
442462
return std::min(getIntImmCost(Imm, Ty, CostKind),
443463
getIntImmCost(-Imm, Ty, CostKind));
464+
}
444465

445-
if (Opcode == Instruction::ICmp && Imm.isNegative() &&
446-
Ty->getIntegerBitWidth() == 32) {
447-
int64_t NegImm = -Imm.getSExtValue();
448-
if (ST->isThumb2() && NegImm < 1<<12)
449-
// icmp X, #-C -> cmn X, #C
450-
return 0;
451-
if (ST->isThumb() && NegImm < 1<<8)
452-
// icmp X, #-C -> adds X, #C
466+
if (Opcode == Instruction::ICmp && Ty->getIntegerBitWidth() == 32) {
467+
int64_t ImmVal = Imm.getSExtValue();
468+
if (!ST->isThumb())
469+
if (ARM_AM::getSOImmVal((uint32_t)ImmVal) != -1 ||
470+
ARM_AM::getSOImmVal(-(uint32_t)ImmVal) != -1)
471+
return 0;
472+
if (ST->isThumb2())
473+
if (ARM_AM::getT2SOImmVal((uint32_t)ImmVal) != -1 ||
474+
ARM_AM::getT2SOImmVal(-(uint32_t)ImmVal) != -1)
475+
return 0;
476+
// Thumb1 doesn't have cmn, and only 8-bit immediates.
477+
ImmVal = ImmVal < 0 ? -ImmVal : ImmVal;
478+
if (ImmVal >= 0 && ImmVal <= 255)
453479
return 0;
454480
}
455481

llvm/test/CodeGen/ARM/ssat.ll

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,28 +390,27 @@ entry:
390390
define i32 @no_sat_incorrect_constant(i32 %x) #0 {
391391
; V4T-LABEL: no_sat_incorrect_constant:
392392
; V4T: @ %bb.0: @ %entry
393-
; V4T-NEXT: mov r1, #1065353216
393+
; V4T-NEXT: ldr r2, .LCPI11_0
394394
; V4T-NEXT: cmn r0, #8388608
395-
; V4T-NEXT: orr r1, r1, #-1073741824
396-
; V4T-NEXT: mov r2, r0
397-
; V4T-NEXT: orrlt r2, r1, #1
398-
; V4T-NEXT: ldr r1, .LCPI11_0
395+
; V4T-NEXT: movge r2, r0
396+
; V4T-NEXT: ldr r1, .LCPI11_1
399397
; V4T-NEXT: cmp r0, #8388608
400398
; V4T-NEXT: movlt r1, r2
401399
; V4T-NEXT: mov r0, r1
402400
; V4T-NEXT: bx lr
403401
; V4T-NEXT: .p2align 2
404402
; V4T-NEXT: @ %bb.1:
405403
; V4T-NEXT: .LCPI11_0:
404+
; V4T-NEXT: .long 4286578689 @ 0xff800001
405+
; V4T-NEXT: .LCPI11_1:
406406
; V4T-NEXT: .long 8388607 @ 0x7fffff
407407
;
408408
; V6T2-LABEL: no_sat_incorrect_constant:
409409
; V6T2: @ %bb.0: @ %entry
410-
; V6T2-NEXT: movw r2, #0
411410
; V6T2-NEXT: cmn r0, #8388608
412411
; V6T2-NEXT: mov r1, r0
413-
; V6T2-NEXT: movt r2, #65408
414-
; V6T2-NEXT: orrlt r1, r2, #1
412+
; V6T2-NEXT: movwlt r1, #1
413+
; V6T2-NEXT: movtlt r1, #65408
415414
; V6T2-NEXT: cmp r0, #8388608
416415
; V6T2-NEXT: movwge r1, #65535
417416
; V6T2-NEXT: movtge r1, #127

0 commit comments

Comments
 (0)