Skip to content

Commit 257c010

Browse files
committed
Merging r354808:
------------------------------------------------------------------------ r354808 | nikic | 2019-02-25 10:54:17 -0800 (Mon, 25 Feb 2019) | 11 lines [Mips] Fix missing masking in fast-isel of br (PR40325) Fixes https://bugs.llvm.org/show_bug.cgi?id=40325 by zero extending (and x, 1) the condition before branching on it. To avoid regressing trivial cases, I'm combining emission of cmp+br sequences for the single-use + same block case (similar to what we do in x86). icmpbr1.ll still regresses due to the cross-bb usage of the condition. Differential Revision: https://reviews.llvm.org/D58576 ------------------------------------------------------------------------ llvm-svn: 358925
1 parent 1cef8c2 commit 257c010

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

llvm/lib/Target/Mips/MipsFastISel.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -954,21 +954,34 @@ bool MipsFastISel::selectBranch(const Instruction *I) {
954954
//
955955
MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
956956
MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
957-
// For now, just try the simplest case where it's fed by a compare.
957+
958+
// Fold the common case of a conditional branch with a comparison
959+
// in the same block.
960+
unsigned ZExtCondReg = 0;
958961
if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
959-
MVT CIMVT =
960-
TLI.getValueType(DL, CI->getOperand(0)->getType(), true).getSimpleVT();
961-
if (CIMVT == MVT::i1)
962+
if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
963+
ZExtCondReg = createResultReg(&Mips::GPR32RegClass);
964+
if (!emitCmp(ZExtCondReg, CI))
965+
return false;
966+
}
967+
}
968+
969+
// For the general case, we need to mask with 1.
970+
if (ZExtCondReg == 0) {
971+
unsigned CondReg = getRegForValue(BI->getCondition());
972+
if (CondReg == 0)
962973
return false;
963974

964-
unsigned CondReg = getRegForValue(CI);
965-
BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
966-
.addReg(CondReg)
967-
.addMBB(TBB);
968-
finishCondBranch(BI->getParent(), TBB, FBB);
969-
return true;
975+
ZExtCondReg = emitIntExt(MVT::i1, CondReg, MVT::i32, true);
976+
if (ZExtCondReg == 0)
977+
return false;
970978
}
971-
return false;
979+
980+
BuildMI(*BrBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::BGTZ))
981+
.addReg(ZExtCondReg)
982+
.addMBB(TBB);
983+
finishCondBranch(BI->getParent(), TBB, FBB);
984+
return true;
972985
}
973986

974987
bool MipsFastISel::selectCmp(const Instruction *I) {

llvm/test/CodeGen/Mips/Fast-ISel/icmpbr1.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ bb0:
1717
bb1:
1818
; CHECK: # %bb.1: # %bb1
1919
; CHECK-NEXT: lw $[[REG2:[0-9]+]], [[SPILL]]($sp) # 4-byte Folded Reload
20-
; CHECK-NEXT: bgtz $[[REG2]], $BB0_3
20+
; CHECK-NEXT: andi $[[REG3:[0-9]+]], $[[REG2]], 1
21+
; CHECK-NEXT: bgtz $[[REG3]], $BB0_3
2122
br i1 %2, label %bb2, label %bb3
2223
bb2:
2324
; CHECK: $BB0_3: # %bb2
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=mipsel -relocation-model=pic -O0 -mcpu=mips32 < %s | FileCheck %s
3+
4+
define void @test(i32 %x, i1* %p) nounwind {
5+
; CHECK-LABEL: test:
6+
; CHECK: # %bb.0:
7+
; CHECK-NEXT: move $1, $4
8+
; CHECK-NEXT: andi $4, $4, 1
9+
; CHECK-NEXT: sb $4, 0($5)
10+
; CHECK-NEXT: andi $1, $1, 1
11+
; CHECK-NEXT: bgtz $1, $BB0_1
12+
; CHECK-NEXT: nop
13+
; CHECK-NEXT: # %bb.1: # %foo
14+
; CHECK-NEXT: jr $ra
15+
; CHECK-NEXT: nop
16+
%y = and i32 %x, 1
17+
%c = icmp eq i32 %y, 1
18+
store i1 %c, i1* %p
19+
br i1 %c, label %foo, label %foo
20+
21+
foo:
22+
ret void
23+
}

0 commit comments

Comments
 (0)