Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit db059db

Browse files
committed
AArch64: be careful of large immediates when optimising cmps.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243492 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 62df004 commit db059db

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/Target/AArch64/AArch64ConditionOptimizer.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,18 @@ MachineInstr *AArch64ConditionOptimizer::findSuitableCompare(
154154
// cmn is an alias for adds with a dead destination register.
155155
case AArch64::ADDSWri:
156156
case AArch64::ADDSXri:
157-
if (MRI->use_empty(I->getOperand(0).getReg()))
158-
return I;
159-
160-
DEBUG(dbgs() << "Destination of cmp is not dead, " << *I << '\n');
161-
return nullptr;
157+
if (!I->getOperand(2).isImm()) {
158+
DEBUG(dbgs() << "Immediate of cmp is symbolic, " << *I << '\n');
159+
return nullptr;
160+
} else if (I->getOperand(2).getImm() << I->getOperand(3).getImm() >=
161+
0xfff) {
162+
DEBUG(dbgs() << "Immediate of cmp may be out of range, " << *I << '\n');
163+
return nullptr;
164+
} else if (!MRI->use_empty(I->getOperand(0).getReg())) {
165+
DEBUG(dbgs() << "Destination of cmp is not dead, " << *I << '\n');
166+
return nullptr;
167+
}
168+
return I;
162169

163170
// Prevent false positive case like:
164171
// cmp w19, #0

test/CodeGen/AArch64/combine-comparisons-by-cse.ll

+26
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,32 @@ return: ; preds = %land.lhs.true, %con
403403
ret i32 %retval.0
404404
}
405405

406+
define void @cmp_shifted(i32 %in, i32 %lhs, i32 %rhs) {
407+
; CHECK-LABEL: cmp_shifted:
408+
; CHECK: cmp w0, #1
409+
; [...]
410+
; CHECK: cmp w0, #2, lsl #12
411+
412+
%tst_low = icmp sgt i32 %in, 0
413+
br i1 %tst_low, label %true, label %false
414+
415+
true:
416+
call i32 @zoo(i32 128)
417+
ret void
418+
419+
false:
420+
%tst = icmp sgt i32 %in, 8191
421+
br i1 %tst, label %truer, label %falser
422+
423+
truer:
424+
call i32 @zoo(i32 42)
425+
ret void
426+
427+
falser:
428+
call i32 @zoo(i32 1)
429+
ret void
430+
}
431+
406432
declare i32 @zoo(i32)
407433

408434
declare double @yoo(i32)

0 commit comments

Comments
 (0)