forked from OSchip/llvm-project
ARM: don't try to hoist constant RHS out of a division.
Divisions by a constant can be converted into multiplies which are usually cheaper, but this isn't possible if the constant gets separated (particularly in loops). Fix this by telling ConstantHoisting that the immediate in a DIV is cheap. I considered making the check generic, but neither AArch64 (strangely) nor x86 showed any benefit on the tests I had. llvm-svn: 266464
This commit is contained in:
parent
9cfd8cea6b
commit
903f81ba18
|
@ -47,6 +47,21 @@ int ARMTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
|
|||
return 3;
|
||||
}
|
||||
|
||||
int ARMTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
|
||||
Type *Ty) {
|
||||
// Division by a constant can be turned into multiplication, but only if we
|
||||
// know it's constant. So it's not so much that the immediate is cheap (it's
|
||||
// not), but that the alternative is worse.
|
||||
// FIXME: this is probably unneeded with GlobalISel.
|
||||
if ((Opcode == Instruction::SDiv || Opcode == Instruction::UDiv ||
|
||||
Opcode == Instruction::SRem || Opcode == Instruction::URem) &&
|
||||
Idx == 1)
|
||||
return 0;
|
||||
|
||||
return getIntImmCost(Imm, Ty);
|
||||
}
|
||||
|
||||
|
||||
int ARMTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
|
||||
int ISD = TLI->InstructionOpcodeToISD(Opcode);
|
||||
assert(ISD && "Invalid opcode");
|
||||
|
|
|
@ -64,9 +64,7 @@ public:
|
|||
using BaseT::getIntImmCost;
|
||||
int getIntImmCost(const APInt &Imm, Type *Ty);
|
||||
|
||||
int getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty) {
|
||||
return getIntImmCost(Imm, Ty);
|
||||
}
|
||||
int getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty);
|
||||
|
||||
/// @}
|
||||
|
||||
|
|
|
@ -45,3 +45,48 @@ bb2:
|
|||
default:
|
||||
ret void
|
||||
}
|
||||
|
||||
; We don't want to convert constant divides because the benefit from converting
|
||||
; them to a mul in the backend is larget than constant materialization savings.
|
||||
define void @signed_const_division(i32 %in1, i32 %in2, i32* %addr) {
|
||||
; CHECK-LABEL: @signed_const_division
|
||||
; CHECK: %res1 = sdiv i32 %l1, 1000000000
|
||||
; CHECK: %res2 = srem i32 %l2, 1000000000
|
||||
entry:
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%l1 = phi i32 [%res1, %loop], [%in1, %entry]
|
||||
%l2 = phi i32 [%res2, %loop], [%in2, %entry]
|
||||
%res1 = sdiv i32 %l1, 1000000000
|
||||
store volatile i32 %res1, i32* %addr
|
||||
%res2 = srem i32 %l2, 1000000000
|
||||
store volatile i32 %res2, i32* %addr
|
||||
%again = icmp eq i32 %res1, %res2
|
||||
br i1 %again, label %loop, label %end
|
||||
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @unsigned_const_division(i32 %in1, i32 %in2, i32* %addr) {
|
||||
; CHECK-LABEL: @unsigned_const_division
|
||||
; CHECK: %res1 = udiv i32 %l1, 1000000000
|
||||
; CHECK: %res2 = urem i32 %l2, 1000000000
|
||||
|
||||
entry:
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%l1 = phi i32 [%res1, %loop], [%in1, %entry]
|
||||
%l2 = phi i32 [%res2, %loop], [%in2, %entry]
|
||||
%res1 = udiv i32 %l1, 1000000000
|
||||
store volatile i32 %res1, i32* %addr
|
||||
%res2 = urem i32 %l2, 1000000000
|
||||
store volatile i32 %res2, i32* %addr
|
||||
%again = icmp eq i32 %res1, %res2
|
||||
br i1 %again, label %loop, label %end
|
||||
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue