Skip to content

Commit 3f85134

Browse files
author
Chen Zheng
committed
[PowerPC] implement target hook isProfitableToHoist
On Powerpc fma is faster than fadd + fmul for some types, (PPCTargetLowering::isFMAFasterThanFMulAndFAdd). we should implement target hook isProfitableToHoist to prevent simplifyCFGpass from breaking fma pattern by hoisting fmul to predecessor block. Reviewed By: nemanjai Differential Revision: https://reviews.llvm.org/D76207
1 parent f528df8 commit 3f85134

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15385,6 +15385,33 @@ bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(const Function &F,
1538515385
}
1538615386
}
1538715387

15388+
// Currently this is a copy from AArch64TargetLowering::isProfitableToHoist.
15389+
// FIXME: add more patterns which are profitable to hoist.
15390+
bool PPCTargetLowering::isProfitableToHoist(Instruction *I) const {
15391+
if (I->getOpcode() != Instruction::FMul)
15392+
return true;
15393+
15394+
if (!I->hasOneUse())
15395+
return true;
15396+
15397+
Instruction *User = I->user_back();
15398+
assert(User && "A single use instruction with no uses.");
15399+
15400+
if (User->getOpcode() != Instruction::FSub &&
15401+
User->getOpcode() == Instruction::FAdd)
15402+
return true;
15403+
15404+
const TargetOptions &Options = getTargetMachine().Options;
15405+
const Function *F = I->getFunction();
15406+
const DataLayout &DL = F->getParent()->getDataLayout();
15407+
Type *Ty = User->getOperand(0)->getType();
15408+
15409+
return !(
15410+
isFMAFasterThanFMulAndFAdd(*F, Ty) &&
15411+
isOperationLegalOrCustom(ISD::FMA, getValueType(DL, Ty)) &&
15412+
(Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath));
15413+
}
15414+
1538815415
const MCPhysReg *
1538915416
PPCTargetLowering::getScratchRegisters(CallingConv::ID) const {
1539015417
// LR is a callee-save register, but we must treat it as clobbered by any call

llvm/lib/Target/PowerPC/PPCISelLowering.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,12 @@ namespace llvm {
909909

910910
bool isFMAFasterThanFMulAndFAdd(const Function &F, Type *Ty) const override;
911911

912+
/// isProfitableToHoist - Check if it is profitable to hoist instruction
913+
/// \p I to its dominator block.
914+
/// For example, it is not profitable if \p I and it's only user can form a
915+
/// FMA instruction, because Powerpc prefers FMADD.
916+
bool isProfitableToHoist(Instruction *I) const override;
917+
912918
const MCPhysReg *getScratchRegisters(CallingConv::ID CC) const override;
913919

914920
// Should we expand the build vector with shuffles?

llvm/test/Transforms/SimplifyCFG/PowerPC/prefer-fma.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ define double @_Z3fooRdS_S_S_(double* dereferenceable(8) %x, double* dereference
1111
; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq double [[TMP0]], 0.000000e+00
1212
; CHECK-NEXT: [[TMP1:%.*]] = load double, double* [[X:%.*]], align 8
1313
; CHECK-NEXT: [[TMP2:%.*]] = load double, double* [[A:%.*]], align 8
14-
; CHECK-NEXT: [[TMP3:%.*]] = fmul fast double [[TMP1]], [[TMP2]]
1514
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
1615
; CHECK: if.then:
16+
; CHECK-NEXT: [[TMP3:%.*]] = fmul fast double [[TMP1]], [[TMP2]]
1717
; CHECK-NEXT: [[MUL:%.*]] = fadd fast double 1.000000e+00, [[TMP3]]
1818
; CHECK-NEXT: store double [[MUL]], double* [[Y]], align 8
1919
; CHECK-NEXT: br label [[IF_END:%.*]]
2020
; CHECK: if.else:
21-
; CHECK-NEXT: [[SUB1:%.*]] = fsub fast double [[TMP3]], [[TMP0]]
21+
; CHECK-NEXT: [[MUL1:%.*]] = fmul fast double [[TMP1]], [[TMP2]]
22+
; CHECK-NEXT: [[SUB1:%.*]] = fsub fast double [[MUL1]], [[TMP0]]
2223
; CHECK-NEXT: [[GEP1:%.*]] = getelementptr double, double* [[Y]], i32 1
2324
; CHECK-NEXT: store double [[SUB1]], double* [[GEP1]], align 8
2425
; CHECK-NEXT: br label [[IF_END]]

0 commit comments

Comments
 (0)