Skip to content

Commit 058309b

Browse files
committed
Re-apply r234898 and fix tests.
This commit makes LLVM not estimate branch probabilities when doing a single bit bitmask tests. The code that originally made me discover this is: if ((a & 0x1) == 0x1) { .. } In this case we don't actually have any branch probability information and should not assume to have any. LLVM transforms this into: %and = and i32 %a, 1 %tobool = icmp eq i32 %and, 0 So, in this case, the result of a bitwise and is compared against 0, but nevertheless, we should not assume to have probability information. CodeGen/ARM/2013-10-11-select-stalls.ll started failing because the changed probabilities changed the results of ARMBaseInstrInfo::isProfitableToIfCvt() and led to an Ifcvt of the diamond in the test. AFAICT, the test was never meant to test this and thus changing the test input slightly to not change the probabilities seems like the best way to preserve the meaning of the test. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@234979 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 29da637 commit 058309b

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ bool BranchProbabilityInfo::calcZeroHeuristics(BasicBlock *BB) {
379379
if (!CV)
380380
return false;
381381

382+
// If the LHS is the result of AND'ing a value with a single bit bitmask,
383+
// we don't have information about probabilities.
384+
if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0)))
385+
if (LHS->getOpcode() == Instruction::And)
386+
if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1)))
387+
if (AndRHS->getUniqueInteger().isPowerOf2())
388+
return false;
389+
382390
bool isProb;
383391
if (CV->isZero()) {
384392
switch (CI->getPredicate()) {

test/Analysis/BranchProbabilityInfo/basic.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,31 @@ exit:
212212
ret i32 %result
213213
}
214214

215+
define i32 @zero3(i32 %i, i32 %a, i32 %b) {
216+
; CHECK: Printing analysis {{.*}} for function 'zero3'
217+
entry:
218+
; AND'ing with a single bit bitmask essentially leads to a bool comparison,
219+
; meaning we don't have probability information.
220+
%and = and i32 %i, 2
221+
%tobool = icmp eq i32 %and, 0
222+
br i1 %tobool, label %then, label %else
223+
; CHECK: edge entry -> then probability is 16 / 32
224+
; CHECK: edge entry -> else probability is 16 / 32
225+
226+
then:
227+
; AND'ing with other bitmask might be something else, so we still assume the
228+
; usual probabilities.
229+
%and2 = and i32 %i, 5
230+
%tobool2 = icmp eq i32 %and2, 0
231+
br i1 %tobool2, label %else, label %exit
232+
; CHECK: edge then -> else probability is 12 / 32
233+
; CHECK: edge then -> exit probability is 20 / 32
234+
235+
else:
236+
br label %exit
237+
238+
exit:
239+
%result = phi i32 [ %a, %then ], [ %b, %else ]
240+
ret i32 %result
241+
}
242+

test/CodeGen/AArch64/arm64-call-tailcalls.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ bb: ; preds = %entry
5353

5454
define i32 @t8(i32 %x) nounwind ssp {
5555
; CHECK-LABEL: t8:
56+
; CHECK: b _c
5657
; CHECK: b _a
5758
; CHECK: b _b
58-
; CHECK: b _c
5959
%and = and i32 %x, 1
6060
%tobool = icmp eq i32 %and, 0
6161
br i1 %tobool, label %if.end, label %if.then

test/CodeGen/ARM/2013-10-11-select-stalls.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ define <16 x i8> @multiselect(i32 %avail, i8* %foo, i8* %bar) {
77
entry:
88
%vld1 = call <16 x i8> @llvm.arm.neon.vld1.v16i8(i8* %foo, i32 1)
99
%vld2 = call <16 x i8> @llvm.arm.neon.vld1.v16i8(i8* %bar, i32 1)
10-
%and = and i32 %avail, 1
10+
%and = and i32 %avail, 3
1111
%tobool = icmp eq i32 %and, 0
1212
%retv = select i1 %tobool, <16 x i8> %vld1, <16 x i8> %vld2
1313
ret <16 x i8> %retv

test/CodeGen/Mips/octeon.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ entry:
9393
; ALL-LABEL: bbit0:
9494
; OCTEON: bbit0 $4, 3, $[[BB0:BB[0-9_]+]]
9595
; MIPS64: andi $[[T0:[0-9]+]], $4, 8
96-
; MIPS64: beqz $[[T0]], $[[BB0:BB[0-9_]+]]
96+
; MIPS64: bnez $[[T0]], $[[BB0:BB[0-9_]+]]
9797
%bit = and i64 %a, 8
9898
%res = icmp eq i64 %bit, 0
9999
br i1 %res, label %endif, label %if
@@ -111,7 +111,7 @@ entry:
111111
; MIPS64: daddiu $[[T0:[0-9]+]], $zero, 1
112112
; MIPS64: dsll $[[T1:[0-9]+]], $[[T0]], 35
113113
; MIPS64: and $[[T2:[0-9]+]], $4, $[[T1]]
114-
; MIPS64: beqz $[[T2]], $[[BB0:BB[0-9_]+]]
114+
; MIPS64: bnez $[[T2]], $[[BB0:BB[0-9_]+]]
115115
%bit = and i64 %a, 34359738368
116116
%res = icmp eq i64 %bit, 0
117117
br i1 %res, label %endif, label %if

0 commit comments

Comments
 (0)