Skip to content

Commit 0b6d7bc

Browse files
committedApr 3, 2016
[CodeGenPrepare] Avoid sinking soft-FP comparisons
Sinking comparisons in CGP can undo the job of hoisting them done earlier by LICM, and soft-FP makes this an expensive mistake. A common pattern that produces floating point comparisons uniform over a loop is an explicit check for division by zero. If the divisor is hoisted out of the loop, the comparison can also be, but hoisting the function that unwinds is never legal, since it may cause side effects in the loop body prior to the unwinding to not be executed. Differential Revision: http://reviews.llvm.org/D18744 llvm-svn: 265264
1 parent 20d1d4f commit 0b6d7bc

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed
 

‎llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,14 @@ static bool CombineUAddWithOverflow(CmpInst *CI) {
855855
/// lose; some adjustment may be wanted there.
856856
///
857857
/// Return true if any changes are made.
858-
static bool SinkCmpExpression(CmpInst *CI) {
858+
static bool SinkCmpExpression(CmpInst *CI, const TargetLowering &TLI) {
859859
BasicBlock *DefBB = CI->getParent();
860860

861-
/// Only insert a cmp in each block once.
861+
// Avoid sinking soft-FP comparisons, since this can move them into a loop.
862+
if (TLI.useSoftFloat() && isa<FCmpInst>(CI))
863+
return false;
864+
865+
// Only insert a cmp in each block once.
862866
DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
863867

864868
bool MadeChange = false;
@@ -906,8 +910,8 @@ static bool SinkCmpExpression(CmpInst *CI) {
906910
return MadeChange;
907911
}
908912

909-
static bool OptimizeCmpExpression(CmpInst *CI) {
910-
if (SinkCmpExpression(CI))
913+
static bool OptimizeCmpExpression(CmpInst *CI, const TargetLowering &TLI) {
914+
if (SinkCmpExpression(CI, TLI))
911915
return true;
912916

913917
if (CombineUAddWithOverflow(CI))
@@ -5173,7 +5177,7 @@ bool CodeGenPrepare::optimizeInst(Instruction *I, bool& ModifiedDT) {
51735177

51745178
if (CmpInst *CI = dyn_cast<CmpInst>(I))
51755179
if (!TLI || !TLI->hasMultipleConditionRegisters())
5176-
return OptimizeCmpExpression(CI);
5180+
return OptimizeCmpExpression(CI, *TLI);
51775181

51785182
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
51795183
stripInvariantGroupMetadata(*LI);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: opt %s -codegenprepare -mattr=+soft-float -S | FileCheck %s -check-prefix=CHECK -check-prefix=SOFTFP
2+
; RUN: opt %s -codegenprepare -mattr=-soft-float -S | FileCheck %s -check-prefix=CHECK -check-prefix=HARDFP
3+
4+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
; CHECK-LABEL: @foo
8+
; CHECK: entry:
9+
; SOFTFP: fcmp
10+
; HARDFP-NOT: fcmp
11+
; CHECK: body:
12+
; SOFTFP-NOT: fcmp
13+
; HARDFP: fcmp
14+
define void @foo(float %a, float %b) {
15+
entry:
16+
%c = fcmp oeq float %a, %b
17+
br label %head
18+
head:
19+
%IND = phi i32 [ 0, %entry ], [ %IND.new, %body1 ]
20+
%CMP = icmp slt i32 %IND, 1250
21+
br i1 %CMP, label %body, label %tail
22+
body:
23+
br i1 %c, label %body1, label %tail
24+
body1:
25+
%IND.new = add i32 %IND, 1
26+
br label %head
27+
tail:
28+
ret void
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.