[llvm] [EarlyIfConversion] Fix the logic to determine predictable branch. (PR #92405)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 16:09:41 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-powerpc

Author: Mikhail Gudim (mgudim)

<details>
<summary>Changes</summary>

Branch is considered predictable when ALL of the operands used to evaluate condition are loop-invariant.

---
Full diff: https://github.com/llvm/llvm-project/pull/92405.diff


4 Files Affected:

- (modified) llvm/lib/CodeGen/EarlyIfConversion.cpp (+1-1) 
- (modified) llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir (+18-35) 
- (modified) llvm/test/CodeGen/PowerPC/expand-foldable-isel.ll (+28-29) 
- (modified) llvm/test/CodeGen/PowerPC/loop-instr-form-non-inc.ll (+3-8) 


``````````diff
diff --git a/llvm/lib/CodeGen/EarlyIfConversion.cpp b/llvm/lib/CodeGen/EarlyIfConversion.cpp
index 2a7bee1618deb..2f8729fcbb945 100644
--- a/llvm/lib/CodeGen/EarlyIfConversion.cpp
+++ b/llvm/lib/CodeGen/EarlyIfConversion.cpp
@@ -879,7 +879,7 @@ bool EarlyIfConverter::shouldConvertIf() {
   // from a loop-invariant address predictable; we were unable to prove that it
   // doesn't alias any of the memory-writes in the loop, but it is likely to
   // read to same value multiple times.
-  if (CurrentLoop && any_of(IfConv.Cond, [&](MachineOperand &MO) {
+  if (CurrentLoop && all_of(IfConv.Cond, [&](MachineOperand &MO) {
         if (!MO.isReg() || !MO.isUse())
           return false;
         Register Reg = MO.getReg();
diff --git a/llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir b/llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir
index 425a23214871d..2c3670ab236b5 100644
--- a/llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir
+++ b/llvm/test/CodeGen/AArch64/early-ifcvt-likely-predictable.mir
@@ -33,35 +33,27 @@ body:             |
   ; CHECK-NEXT:   [[COPY3:%[0-9]+]]:gpr64common = COPY $x0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
-  ; CHECK-NEXT:   successors: %bb.3(0x30000000), %bb.2(0x50000000)
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   [[LDRBBui:%[0-9]+]]:gpr32 = LDRBBui [[COPY3]], 0 :: (load (s8))
+  ; CHECK-NEXT:   [[LDRBBui:%[0-9]+]]:gpr32common = LDRBBui [[COPY3]], 0 :: (load (s8))
   ; CHECK-NEXT:   [[COPY4:%[0-9]+]]:gpr32all = COPY $wzr
-  ; CHECK-NEXT:   [[COPY5:%[0-9]+]]:gpr32all = COPY [[COPY4]]
-  ; CHECK-NEXT:   CBZW killed [[LDRBBui]], %bb.3
-  ; CHECK-NEXT:   B %bb.2
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.2:
-  ; CHECK-NEXT:   successors: %bb.3(0x80000000)
-  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[COPY5:%[0-9]+]]:gpr32 = COPY [[COPY4]]
   ; CHECK-NEXT:   [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[COPY1]], 4080, 12, implicit-def $nzcv
   ; CHECK-NEXT:   [[MOVi32imm:%[0-9]+]]:gpr32 = MOVi32imm 16711680
   ; CHECK-NEXT:   [[CSELWr:%[0-9]+]]:gpr32common = CSELWr [[COPY1]], killed [[MOVi32imm]], 11, implicit $nzcv
   ; CHECK-NEXT:   [[SUBSWri1:%[0-9]+]]:gpr32 = SUBSWri [[CSELWr]], 0, 0, implicit-def $nzcv
   ; CHECK-NEXT:   [[COPY6:%[0-9]+]]:gpr32 = COPY $wzr
   ; CHECK-NEXT:   [[CSELWr1:%[0-9]+]]:gpr32 = CSELWr [[CSELWr]], [[COPY6]], 12, implicit $nzcv
-  ; CHECK-NEXT:   [[COPY7:%[0-9]+]]:gpr32all = COPY [[CSELWr1]]
+  ; CHECK-NEXT:   [[COPY7:%[0-9]+]]:gpr32 = COPY [[CSELWr1]]
   ; CHECK-NEXT:   [[SUBSWri2:%[0-9]+]]:gpr32 = SUBSWri [[COPY1]], 0, 0, implicit-def $nzcv
   ; CHECK-NEXT:   [[CSELWr2:%[0-9]+]]:gpr32 = CSELWr [[COPY1]], [[COPY6]], 12, implicit $nzcv
-  ; CHECK-NEXT:   [[COPY8:%[0-9]+]]:gpr32all = COPY [[CSELWr2]]
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.3:
-  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:gpr32 = PHI [[COPY5]], %bb.1, [[COPY7]], %bb.2
-  ; CHECK-NEXT:   [[PHI1:%[0-9]+]]:gpr32 = PHI [[COPY5]], %bb.1, [[COPY8]], %bb.2
-  ; CHECK-NEXT:   STRBBui [[PHI1]], [[COPY2]], 0 :: (store (s8))
-  ; CHECK-NEXT:   STRBBui [[PHI]], [[COPY]], 0 :: (store (s8))
+  ; CHECK-NEXT:   [[COPY8:%[0-9]+]]:gpr32 = COPY [[CSELWr2]]
+  ; CHECK-NEXT:   $wzr = SUBSWri [[LDRBBui]], 0, 0, implicit-def $nzcv
+  ; CHECK-NEXT:   [[CSELWr3:%[0-9]+]]:gpr32 = CSELWr [[COPY5]], [[COPY7]], 0, implicit $nzcv
+  ; CHECK-NEXT:   $wzr = SUBSWri [[LDRBBui]], 0, 0, implicit-def $nzcv
+  ; CHECK-NEXT:   [[CSELWr4:%[0-9]+]]:gpr32 = CSELWr [[COPY5]], [[COPY8]], 0, implicit $nzcv
+  ; CHECK-NEXT:   STRBBui [[CSELWr4]], [[COPY2]], 0 :: (store (s8))
+  ; CHECK-NEXT:   STRBBui [[CSELWr3]], [[COPY]], 0 :: (store (s8))
   ; CHECK-NEXT:   B %bb.1
   bb.0:
     liveins: $x0, $x1, $w2, $x3
@@ -116,26 +108,17 @@ body:             |
   ; CHECK-NEXT:   [[COPY3:%[0-9]+]]:gpr64common = COPY $x0
   ; CHECK-NEXT: {{  $}}
   ; CHECK-NEXT: bb.1:
-  ; CHECK-NEXT:   successors: %bb.3(0x30000000), %bb.2(0x50000000)
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
   ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   [[LDRBBui:%[0-9]+]]:gpr32 = LDRBBui [[COPY3]], 0 :: (load (s8))
+  ; CHECK-NEXT:   [[LDRBBui:%[0-9]+]]:gpr32common = LDRBBui [[COPY3]], 0 :: (load (s8))
   ; CHECK-NEXT:   [[COPY4:%[0-9]+]]:gpr32all = COPY $wzr
   ; CHECK-NEXT:   [[COPY5:%[0-9]+]]:gpr32all = COPY [[COPY4]]
-  ; CHECK-NEXT:   [[COPY6:%[0-9]+]]:gpr32all = COPY [[LDRBBui]]
-  ; CHECK-NEXT:   CBZW killed [[LDRBBui]], %bb.3
-  ; CHECK-NEXT:   B %bb.2
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.2:
-  ; CHECK-NEXT:   successors: %bb.3(0x80000000)
-  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[COPY6:%[0-9]+]]:gpr32 = COPY [[LDRBBui]]
   ; CHECK-NEXT:   [[SUBSWri:%[0-9]+]]:gpr32 = SUBSWri [[COPY1]], 4080, 12, implicit-def $nzcv
-  ; CHECK-NEXT:   [[COPY7:%[0-9]+]]:gpr32all = COPY [[SUBSWri]]
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT: bb.3:
-  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
-  ; CHECK-NEXT: {{  $}}
-  ; CHECK-NEXT:   [[PHI:%[0-9]+]]:gpr32 = PHI [[COPY6]], %bb.1, [[COPY7]], %bb.2
-  ; CHECK-NEXT:   STRBBui [[PHI]], [[COPY]], 0 :: (store (s8))
+  ; CHECK-NEXT:   [[COPY7:%[0-9]+]]:gpr32 = COPY [[SUBSWri]]
+  ; CHECK-NEXT:   $wzr = SUBSWri [[LDRBBui]], 0, 0, implicit-def $nzcv
+  ; CHECK-NEXT:   [[CSELWr:%[0-9]+]]:gpr32 = CSELWr [[COPY6]], [[COPY7]], 0, implicit $nzcv
+  ; CHECK-NEXT:   STRBBui [[CSELWr]], [[COPY]], 0 :: (store (s8))
   ; CHECK-NEXT:   B %bb.1
   bb.0:
     liveins: $x0, $x1, $w2, $x3
diff --git a/llvm/test/CodeGen/PowerPC/expand-foldable-isel.ll b/llvm/test/CodeGen/PowerPC/expand-foldable-isel.ll
index 8da7519fa6dc7..38445336a7e1c 100644
--- a/llvm/test/CodeGen/PowerPC/expand-foldable-isel.ll
+++ b/llvm/test/CodeGen/PowerPC/expand-foldable-isel.ll
@@ -36,29 +36,24 @@ define void @_ZN3pov6ot_insEPPNS_14ot_node_structEPNS_15ot_block_structEPNS_12ot
 ; CHECK-GEN-ISEL-TRUE-NEXT:    std r0, 80(r1)
 ; CHECK-GEN-ISEL-TRUE-NEXT:    # implicit-def: $x3
 ; CHECK-GEN-ISEL-TRUE-NEXT:    # implicit-def: $r29
-; CHECK-GEN-ISEL-TRUE-NEXT:    b .LBB0_2
 ; CHECK-GEN-ISEL-TRUE-NEXT:    .p2align 4
-; CHECK-GEN-ISEL-TRUE-NEXT:  .LBB0_1: # %cond.false21.i156
-; CHECK-GEN-ISEL-TRUE-NEXT:    #
-; CHECK-GEN-ISEL-TRUE-NEXT:    addi r4, r29, 1
-; CHECK-GEN-ISEL-TRUE-NEXT:    srawi r4, r4, 1
-; CHECK-GEN-ISEL-TRUE-NEXT:    addze r29, r4
-; CHECK-GEN-ISEL-TRUE-NEXT:  .LBB0_2: # %while.cond11
+; CHECK-GEN-ISEL-TRUE-NEXT:  .LBB0_1: # %while.cond11
 ; CHECK-GEN-ISEL-TRUE-NEXT:    #
 ; CHECK-GEN-ISEL-TRUE-NEXT:    lwz r4, 0(r3)
 ; CHECK-GEN-ISEL-TRUE-NEXT:    cmplwi r4, 0
-; CHECK-GEN-ISEL-TRUE-NEXT:    beq cr0, .LBB0_5
-; CHECK-GEN-ISEL-TRUE-NEXT:  # %bb.3: # %while.body21
+; CHECK-GEN-ISEL-TRUE-NEXT:    beq cr0, .LBB0_3
+; CHECK-GEN-ISEL-TRUE-NEXT:  # %bb.2: # %while.body21
 ; CHECK-GEN-ISEL-TRUE-NEXT:    #
 ; CHECK-GEN-ISEL-TRUE-NEXT:    bl ZN3pov10pov_callocEmmPKciS1_pov
 ; CHECK-GEN-ISEL-TRUE-NEXT:    nop
+; CHECK-GEN-ISEL-TRUE-NEXT:    addi r4, r29, 1
+; CHECK-GEN-ISEL-TRUE-NEXT:    srwi r5, r29, 1
+; CHECK-GEN-ISEL-TRUE-NEXT:    srawi r4, r4, 1
 ; CHECK-GEN-ISEL-TRUE-NEXT:    std r3, 0(r3)
-; CHECK-GEN-ISEL-TRUE-NEXT:    bc 12, 4*cr5+lt, .LBB0_1
-; CHECK-GEN-ISEL-TRUE-NEXT:  # %bb.4: # %cond.true18.i153
-; CHECK-GEN-ISEL-TRUE-NEXT:    #
-; CHECK-GEN-ISEL-TRUE-NEXT:    srwi r29, r29, 1
-; CHECK-GEN-ISEL-TRUE-NEXT:    b .LBB0_2
-; CHECK-GEN-ISEL-TRUE-NEXT:  .LBB0_5: # %lor.rhs
+; CHECK-GEN-ISEL-TRUE-NEXT:    addze r4, r4
+; CHECK-GEN-ISEL-TRUE-NEXT:    isel r29, r4, r5, 4*cr5+lt
+; CHECK-GEN-ISEL-TRUE-NEXT:    b .LBB0_1
+; CHECK-GEN-ISEL-TRUE-NEXT:  .LBB0_3: # %lor.rhs
 ; CHECK-GEN-ISEL-TRUE-NEXT:    std r30, 16(r3)
 ; CHECK-GEN-ISEL-TRUE-NEXT:    addi r1, r1, 64
 ; CHECK-GEN-ISEL-TRUE-NEXT:    ld r0, 16(r1)
@@ -81,29 +76,33 @@ define void @_ZN3pov6ot_insEPPNS_14ot_node_structEPNS_15ot_block_structEPNS_12ot
 ; CHECK-NEXT:    std r0, 80(r1)
 ; CHECK-NEXT:    # implicit-def: $x3
 ; CHECK-NEXT:    # implicit-def: $r29
-; CHECK-NEXT:    b .LBB0_2
 ; CHECK-NEXT:    .p2align 4
-; CHECK-NEXT:  .LBB0_1: # %cond.false21.i156
-; CHECK-NEXT:    #
-; CHECK-NEXT:    addi r4, r29, 1
-; CHECK-NEXT:    srawi r4, r4, 1
-; CHECK-NEXT:    addze r29, r4
-; CHECK-NEXT:  .LBB0_2: # %while.cond11
+; CHECK-NEXT:  .LBB0_1: # %while.cond11
 ; CHECK-NEXT:    #
 ; CHECK-NEXT:    lwz r4, 0(r3)
 ; CHECK-NEXT:    cmplwi r4, 0
-; CHECK-NEXT:    beq cr0, .LBB0_5
-; CHECK-NEXT:  # %bb.3: # %while.body21
+; CHECK-NEXT:    beq cr0, .LBB0_6
+; CHECK-NEXT:  # %bb.2: # %while.body21
 ; CHECK-NEXT:    #
 ; CHECK-NEXT:    bl ZN3pov10pov_callocEmmPKciS1_pov
 ; CHECK-NEXT:    nop
+; CHECK-NEXT:    addi r4, r29, 1
+; CHECK-NEXT:    srwi r5, r29, 1
+; CHECK-NEXT:    srawi r4, r4, 1
 ; CHECK-NEXT:    std r3, 0(r3)
-; CHECK-NEXT:    bc 12, 4*cr5+lt, .LBB0_1
-; CHECK-NEXT:  # %bb.4: # %cond.true18.i153
+; CHECK-NEXT:    addze r4, r4
+; CHECK-NEXT:    bc 12, 4*cr5+lt, .LBB0_4
+; CHECK-NEXT:  # %bb.3: # %while.body21
+; CHECK-NEXT:    #
+; CHECK-NEXT:    ori r29, r5, 0
+; CHECK-NEXT:    b .LBB0_5
+; CHECK-NEXT:  .LBB0_4: # %while.body21
+; CHECK-NEXT:    #
+; CHECK-NEXT:    addi r29, r4, 0
+; CHECK-NEXT:  .LBB0_5: # %while.body21
 ; CHECK-NEXT:    #
-; CHECK-NEXT:    srwi r29, r29, 1
-; CHECK-NEXT:    b .LBB0_2
-; CHECK-NEXT:  .LBB0_5: # %lor.rhs
+; CHECK-NEXT:    b .LBB0_1
+; CHECK-NEXT:  .LBB0_6: # %lor.rhs
 ; CHECK-NEXT:    std r30, 16(r3)
 ; CHECK-NEXT:    addi r1, r1, 64
 ; CHECK-NEXT:    ld r0, 16(r1)
diff --git a/llvm/test/CodeGen/PowerPC/loop-instr-form-non-inc.ll b/llvm/test/CodeGen/PowerPC/loop-instr-form-non-inc.ll
index e87d6392c4c7b..93697ec06be2c 100644
--- a/llvm/test/CodeGen/PowerPC/loop-instr-form-non-inc.ll
+++ b/llvm/test/CodeGen/PowerPC/loop-instr-form-non-inc.ll
@@ -11,18 +11,13 @@ define dso_local void @test_no_inc(i32 signext %a) local_unnamed_addr nounwind a
 ; CHECK-NEXT:    li 7, 0
 ; CHECK-NEXT:    andc 4, 3, 4
 ; CHECK-NEXT:    addi 5, 4, 1
-; CHECK-NEXT:    b .LBB0_2
 ; CHECK-NEXT:    .p2align 5
-; CHECK-NEXT:  .LBB0_1: # %for.cond.cleanup
+; CHECK-NEXT:  .LBB0_1: # %for.cond
 ; CHECK-NEXT:    #
+; CHECK-NEXT:    add 8, 3, 6
 ; CHECK-NEXT:    stb 7, 0(5)
 ; CHECK-NEXT:    add 5, 5, 4
-; CHECK-NEXT:  .LBB0_2: # %for.cond
-; CHECK-NEXT:    #
-; CHECK-NEXT:    bc 4, 1, .LBB0_1
-; CHECK-NEXT:  # %bb.3: # %for.body.preheader
-; CHECK-NEXT:    #
-; CHECK-NEXT:    add 6, 3, 6
+; CHECK-NEXT:    iselgt 6, 8, 6
 ; CHECK-NEXT:    b .LBB0_1
 entry:
   %cmp10 = icmp sgt i32 %a, 0

``````````

</details>


https://github.com/llvm/llvm-project/pull/92405


More information about the llvm-commits mailing list