@@ -428,6 +428,13 @@ InstructionCost ARMTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
428
428
if (Opcode == Instruction::GetElementPtr && Idx != 0 )
429
429
return 0 ;
430
430
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
+
431
438
if (Opcode == Instruction::And) {
432
439
// UXTB/UXTH
433
440
if (Imm == 255 || Imm == 65535 )
@@ -437,19 +444,38 @@ InstructionCost ARMTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
437
444
getIntImmCost (~Imm, Ty, CostKind));
438
445
}
439
446
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 ;
441
461
// Conversion to SUB is free, and means we can use -Imm instead.
442
462
return std::min (getIntImmCost (Imm, Ty, CostKind),
443
463
getIntImmCost (-Imm, Ty, CostKind));
464
+ }
444
465
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 )
453
479
return 0 ;
454
480
}
455
481
0 commit comments