@@ -255,7 +255,13 @@ AArch64ConditionOptimizer::CmpInfo AArch64ConditionOptimizer::adjustCmp(
255
255
const int OldImm = (int )CmpMI->getOperand (2 ).getImm ();
256
256
const int NewImm = std::abs (OldImm + Correction);
257
257
258
- // Handle +0 -> -1 and -0 -> +1 (CMN with 0 immediate) transitions by
258
+ // Handle cmn 1 -> cmp 0, because we prefer CMP 0 over cmn 0.
259
+ if (OldImm == 1 && Negative && Correction == -1 ) {
260
+ // If we are adjusting from -1 to 0, we need to change the opcode.
261
+ Opc = getComplementOpc (Opc);
262
+ }
263
+
264
+ // Handle +0 -> -1 and -0 -> +1 (CMN with 0 immediate.) transitions by
259
265
// adjusting compare instruction opcode.
260
266
if (OldImm == 0 && ((Negative && Correction == 1 ) ||
261
267
(!Negative && Correction == -1 ))) {
@@ -380,8 +386,8 @@ bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
380
386
continue ;
381
387
}
382
388
383
- const int HeadImm = (int )HeadCmpMI->getOperand (2 ).getImm ();
384
- const int TrueImm = (int )TrueCmpMI->getOperand (2 ).getImm ();
389
+ int HeadImm = (int )HeadCmpMI->getOperand (2 ).getImm ();
390
+ int TrueImm = (int )TrueCmpMI->getOperand (2 ).getImm ();
385
391
386
392
LLVM_DEBUG (dbgs () << " Head branch:\n " );
387
393
LLVM_DEBUG (dbgs () << " \t condition: " << AArch64CC::getCondCodeName (HeadCmp)
@@ -393,6 +399,14 @@ bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
393
399
<< ' \n ' );
394
400
LLVM_DEBUG (dbgs () << " \t immediate: " << TrueImm << ' \n ' );
395
401
402
+ unsigned Opc = HeadCmpMI->getOpcode ();
403
+ if (Opc == AArch64::ADDSWri || Opc == AArch64::ADDSXri)
404
+ HeadImm = -HeadImm;
405
+
406
+ Opc = TrueCmpMI->getOpcode ();
407
+ if (Opc == AArch64::ADDSWri || Opc == AArch64::ADDSXri)
408
+ TrueImm = -TrueImm;
409
+
396
410
if (((HeadCmp == AArch64CC::GT && TrueCmp == AArch64CC::LT) ||
397
411
(HeadCmp == AArch64CC::LT && TrueCmp == AArch64CC::GT)) &&
398
412
std::abs (TrueImm - HeadImm) == 2 ) {
@@ -434,6 +448,8 @@ bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
434
448
adjustHeadCond = !adjustHeadCond;
435
449
}
436
450
451
+ TrueImm = std::abs (TrueImm);
452
+ HeadImm = std::abs (HeadImm);
437
453
if (adjustHeadCond) {
438
454
Changed |= adjustTo (HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
439
455
} else {
0 commit comments