Index: llvm/lib/Target/RISCV/RISCVInstrInfo.td =================================================================== --- llvm/lib/Target/RISCV/RISCVInstrInfo.td +++ llvm/lib/Target/RISCV/RISCVInstrInfo.td @@ -996,6 +996,9 @@ // Match X > -1, the canonical form of X >= 0, to the bgez pattern. def : Pat<(brcond (XLenVT (setgt GPR:$rs1, -1)), bb:$imm12), (BGE GPR:$rs1, X0, bb:$imm12)>; +// Lower (a < 1) as (0 >= a) into the bgez pattern. +def : Pat<(brcond (XLenVT (setlt GPR:$lhs, 1)), bb:$imm12), + (BGE X0, GPR:$lhs, bb:$imm12)>; let isBarrier = 1, isBranch = 1, isTerminator = 1 in def PseudoBR : Pseudo<(outs), (ins simm21_lsb0_jal:$imm20), [(br bb:$imm20)]>, Index: llvm/test/CodeGen/RISCV/branch.ll =================================================================== --- llvm/test/CodeGen/RISCV/branch.ll +++ llvm/test/CodeGen/RISCV/branch.ll @@ -6,44 +6,47 @@ ; RV32I-LABEL: foo: ; RV32I: # %bb.0: ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: beq a3, a0, .LBB0_13 +; RV32I-NEXT: beq a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.1: # %test2 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bne a3, a0, .LBB0_13 +; RV32I-NEXT: bne a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.2: # %test3 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: blt a3, a0, .LBB0_13 +; RV32I-NEXT: blt a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.3: # %test4 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bge a3, a0, .LBB0_13 +; RV32I-NEXT: bge a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.4: # %test5 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bltu a3, a0, .LBB0_13 +; RV32I-NEXT: bltu a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.5: # %test6 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bgeu a3, a0, .LBB0_13 +; RV32I-NEXT: bgeu a3, a0, .LBB0_14 ; RV32I-NEXT: # %bb.6: # %test7 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: blt a0, a3, .LBB0_13 +; RV32I-NEXT: blt a0, a3, .LBB0_14 ; RV32I-NEXT: # %bb.7: # %test8 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bge a0, a3, .LBB0_13 +; RV32I-NEXT: bge a0, a3, .LBB0_14 ; RV32I-NEXT: # %bb.8: # %test9 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bltu a0, a3, .LBB0_13 +; RV32I-NEXT: bltu a0, a3, .LBB0_14 ; RV32I-NEXT: # %bb.9: # %test10 ; RV32I-NEXT: lw a3, 0(a1) -; RV32I-NEXT: bgeu a0, a3, .LBB0_13 +; RV32I-NEXT: bgeu a0, a3, .LBB0_14 ; RV32I-NEXT: # %bb.10: # %test11 ; RV32I-NEXT: lw a0, 0(a1) ; RV32I-NEXT: andi a0, a2, 1 -; RV32I-NEXT: bnez a0, .LBB0_13 +; RV32I-NEXT: bnez a0, .LBB0_14 ; RV32I-NEXT: # %bb.11: # %test12 ; RV32I-NEXT: lw a0, 0(a1) -; RV32I-NEXT: bgez a0, .LBB0_13 +; RV32I-NEXT: bgez a0, .LBB0_14 ; RV32I-NEXT: # %bb.12: # %test13 ; RV32I-NEXT: lw a0, 0(a1) -; RV32I-NEXT: .LBB0_13: # %end +; RV32I-NEXT: blez a0, .LBB0_14 +; RV32I-NEXT: # %bb.13: # %test14 +; RV32I-NEXT: lw a0, 0(a1) +; RV32I-NEXT: .LBB0_14: # %end ; RV32I-NEXT: ret %val1 = load volatile i32, i32* %b %tst1 = icmp eq i32 %val1, %a @@ -110,8 +113,15 @@ %tst12 = icmp sgt i32 %val12, -1 br i1 %tst12, label %end, label %test13 +; Check that we use blez (X <= 0) for (X < 1) + test13: %val13 = load volatile i32, i32* %b + %tst13 = icmp slt i32 %val13, 1 + br i1 %tst13, label %end, label %test14 + +test14: + %val14 = load volatile i32, i32* %b br label %end end: Index: llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll =================================================================== --- llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll +++ llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll @@ -29,8 +29,7 @@ ; CHECK-NEXT: lui a0, %hi(s) ; CHECK-NEXT: addi a0, a0, %lo(s) ; CHECK-NEXT: lw a1, 164(a0) -; CHECK-NEXT: addi a2, zero, 1 -; CHECK-NEXT: blt a1, a2, .LBB1_2 +; CHECK-NEXT: blez a1, .LBB1_2 ; CHECK-NEXT: # %bb.1: # %if.then ; CHECK-NEXT: addi a1, zero, 10 ; CHECK-NEXT: sw a1, 160(a0)