Skip to content

Commit 43dc285

Browse files
committedDec 29, 2015
[JumpThreading] Fix opcode bonus in getJumpThreadDuplicationCost()
The code that was meant to adjust the duplication cost based on the terminator opcode was not being executed in cases where the initial threshold was hit inside the loop. Subscribers: mcrosier, llvm-commits Differential Revision: http://reviews.llvm.org/D15536 llvm-svn: 256568
1 parent 755baa4 commit 43dc285

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed
 

‎llvm/lib/Transforms/Scalar/JumpThreading.cpp

+16-10
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,21 @@ static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB,
286286
// FIXME: THREADING will delete values that are just used to compute the
287287
// branch, so they shouldn't count against the duplication cost.
288288

289+
unsigned Bonus = 0;
290+
const TerminatorInst *BBTerm = BB->getTerminator();
291+
// Threading through a switch statement is particularly profitable. If this
292+
// block ends in a switch, decrease its cost to make it more likely to happen.
293+
if (isa<SwitchInst>(BBTerm))
294+
Bonus = 6;
295+
296+
// The same holds for indirect branches, but slightly more so.
297+
if (isa<IndirectBrInst>(BBTerm))
298+
Bonus = 8;
299+
300+
// Bump the threshold up so the early exit from the loop doesn't skip the
301+
// terminator-based Size adjustment at the end.
302+
Threshold += Bonus;
303+
289304
// Sum up the cost of each instruction until we get to the terminator. Don't
290305
// include the terminator because the copy won't include it.
291306
unsigned Size = 0;
@@ -326,16 +341,7 @@ static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB,
326341
}
327342
}
328343

329-
// Threading through a switch statement is particularly profitable. If this
330-
// block ends in a switch, decrease its cost to make it more likely to happen.
331-
if (isa<SwitchInst>(I))
332-
Size = Size > 6 ? Size-6 : 0;
333-
334-
// The same holds for indirect branches, but slightly more so.
335-
if (isa<IndirectBrInst>(I))
336-
Size = Size > 8 ? Size-8 : 0;
337-
338-
return Size;
344+
return Size > Bonus ? Size - Bonus : 0;
339345
}
340346

341347
/// FindLoopHeaders - We do not want jump threading to turn proper loop

‎llvm/test/Transforms/JumpThreading/select.ll

+30
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ L3:
9191
}
9292

9393

94+
; Jump threading of indirectbr with select as address. Test increased
95+
; duplication threshold for cases where indirectbr is being threaded
96+
; through.
97+
98+
; CHECK-LABEL: @test_indirectbr_thresh(
99+
; CHECK-NEXT: entry:
100+
; CHECK-NEXT: br i1 %cond, label %L1, label %L3
101+
; CHECK-NOT: indirectbr
102+
define void @test_indirectbr_thresh(i1 %cond, i8* %address) nounwind {
103+
entry:
104+
br i1 %cond, label %L0, label %L3
105+
L0:
106+
%indirect.goto.dest = select i1 %cond, i8* blockaddress(@test_indirectbr_thresh, %L1), i8* %address
107+
call void @quux()
108+
call void @quux()
109+
call void @quux()
110+
indirectbr i8* %indirect.goto.dest, [label %L1, label %L2, label %L3]
111+
112+
L1:
113+
call void @foo()
114+
ret void
115+
L2:
116+
call void @bar()
117+
ret void
118+
L3:
119+
call void @baz()
120+
ret void
121+
}
122+
123+
94124
; A more complicated case: the condition is a select based on a comparison.
95125

96126
; CHECK-LABEL: @test_switch_cmp(

0 commit comments

Comments
 (0)
Please sign in to comment.