Congzhe 1286de408c
[LoopFusion] Optimize away Phi nodes that are sunk from the 2nd loop preheader (#176503)
Fixed issue #165087.

When we sink phis from the 2nd loop preheader to the exit block, we 
optimize it a bit further, i.e., propagate the uses of each phi node with 
its incoming value and optimize away the phis. Deleted `fixPHINodes()`
too because the phis are already optimized away and there is no point 
processing `fixPHINodes()`.
2026-01-22 16:12:12 -05:00

48 lines
2.4 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
; RUN: opt -S -passes=loop-fusion < %s 2>&1 | FileCheck %s
; When the two loops are legal to get fused, and when there are phi nodes in the second loop preheader
; that will be sunk to the exit block, replace the phi uses with the corresponding incoming value.
; This fixes llvm issue 165087.
define i8 @_Z3fn1ssb() {
; CHECK-LABEL: define i8 @_Z3fn1ssb() {
; CHECK-NEXT: [[ENTRY:.*]]:
; CHECK-NEXT: br label %[[FOR_BODY11_1:.*]]
; CHECK: [[FOR_BODY11_1]]:
; CHECK-NEXT: [[INDVARS_IV_1:%.*]] = phi i64 [ [[INDVARS_IV_NEXT_1:%.*]], %[[FOR_BODY11_1]] ], [ 2, %[[ENTRY]] ]
; CHECK-NEXT: [[INDVARS_IV_2:%.*]] = phi i64 [ [[INDVARS_IV_NEXT_2:%.*]], %[[FOR_BODY11_1]] ], [ 2, %[[ENTRY]] ]
; CHECK-NEXT: [[INDVARS_IV_NEXT_1]] = add i64 [[INDVARS_IV_1]], 1
; CHECK-NEXT: [[EXITCOND_1_NOT:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT_1]], 0
; CHECK-NEXT: [[INDVARS_IV_NEXT_2]] = add i64 [[INDVARS_IV_2]], 1
; CHECK-NEXT: [[EXITCOND_2_NOT:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT_2]], 0
; CHECK-NEXT: br i1 [[EXITCOND_2_NOT]], label %[[FOR_COND_CLEANUP6_2_LOOPEXIT:.*]], label %[[FOR_BODY11_1]]
; CHECK: [[FOR_COND_CLEANUP6_2_LOOPEXIT]]:
; CHECK-NEXT: [[DOTSROA_SPECULATED_2_LCSSA:%.*]] = phi i8 [ 0, %[[FOR_BODY11_1]] ]
; CHECK-NEXT: ret i8 [[DOTSROA_SPECULATED_2_LCSSA]]
;
entry:
br label %for.body11.1
for.body11.1: ; preds = %for.body11.1, %entry
%indvars.iv.1 = phi i64 [ %indvars.iv.next.1, %for.body11.1 ], [ 2, %entry ]
%indvars.iv.next.1 = add i64 %indvars.iv.1, 1
%exitcond.1.not = icmp eq i64 %indvars.iv.next.1, 0
br i1 %exitcond.1.not, label %for.body11.2.preheader, label %for.body11.1
for.body11.2.preheader: ; preds = %for.body11.1
%.sroa.speculated.1.lcssa = phi i8 [ 0, %for.body11.1 ]
br label %for.body11.2
for.body11.2: ; preds = %for.body11.2, %for.body11.2.preheader
%indvars.iv.2 = phi i64 [ %indvars.iv.next.2, %for.body11.2 ], [ 2, %for.body11.2.preheader ]
%indvars.iv.next.2 = add i64 %indvars.iv.2, 1
%exitcond.2.not = icmp eq i64 %indvars.iv.next.2, 0
br i1 %exitcond.2.not, label %for.cond.cleanup6.2.loopexit, label %for.body11.2
for.cond.cleanup6.2.loopexit: ; preds = %for.body11.2
%.sroa.speculated.2.lcssa = phi i8 [ %.sroa.speculated.1.lcssa, %for.body11.2 ]
ret i8 %.sroa.speculated.2.lcssa
}