Skip to content

Commit c0c5ab3

Browse files
fhahnzmodem
authored andcommitted
[LV] Fix predication for branches with matching true and false succs.
Currently due to the edge caching, we create wrong predicates for branches with matching true and false successors. We will cache the condition for the edge from the true successor, and then lookup the same edge (src and dst are the same) for the edge to the false successor. If both successors match, the condition should always be true. At the moment, we cannot really create constant VPValues, but we can just create a true condition as X | !X. Later passes will clean that up. Fixes PR44488. Reviewers: rengolin, hsaito, fhahn, Ayal, dorit, gilr Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D73079 (cherry picked from commit f14f2a8)
1 parent 0242096 commit c0c5ab3

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6716,7 +6716,7 @@ VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock *Src, BasicBlock *Dst,
67166716
BranchInst *BI = dyn_cast<BranchInst>(Src->getTerminator());
67176717
assert(BI && "Unexpected terminator found");
67186718

6719-
if (!BI->isConditional())
6719+
if (!BI->isConditional() || BI->getSuccessor(0) == BI->getSuccessor(1))
67206720
return EdgeMaskCache[Edge] = SrcMask;
67216721

67226722
VPValue *EdgeMask = Plan->getVPValue(BI->getCondition());
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
; RUN: opt < %s -force-vector-width=2 -force-vector-interleave=1 -loop-vectorize -S | FileCheck %s
2+
3+
; Test case for PR44488. Checks that the correct predicates are created for
4+
; branches where true and false successors are equal. See the checks involving
5+
; CMP1 and CMP2.
6+
7+
@v_38 = global i16 12061, align 1
8+
@v_39 = global i16 11333, align 1
9+
10+
define i16 @test_true_and_false_branch_equal() {
11+
; CHECK-LABEL: @test_true_and_false_branch_equal(
12+
; CHECK-LABEL: vector.body:
13+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %vector.ph ], [ [[INDEX_NEXT:%.*]], [[PRED_SREM_CONTINUE2:%.*]] ]
14+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[INDEX]] to i16
15+
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i16 99, [[TMP0]]
16+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i16> undef, i16 [[OFFSET_IDX]], i32 0
17+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i16> [[BROADCAST_SPLATINSERT]], <2 x i16> undef, <2 x i32> zeroinitializer
18+
; CHECK-NEXT: [[INDUCTION:%.*]] = add <2 x i16> [[BROADCAST_SPLAT]], <i16 0, i16 1>
19+
; CHECK-NEXT: [[TMP1:%.*]] = add i16 [[OFFSET_IDX]], 0
20+
; CHECK-NEXT: [[TMP2:%.*]] = load i16, i16* @v_38, align 1
21+
; CHECK-NEXT: [[TMP3:%.*]] = load i16, i16* @v_38, align 1
22+
; CHECK-NEXT: [[TMP4:%.*]] = insertelement <2 x i16> undef, i16 [[TMP2]], i32 0
23+
; CHECK-NEXT: [[TMP5:%.*]] = insertelement <2 x i16> [[TMP4]], i16 [[TMP3]], i32 1
24+
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq <2 x i16> [[TMP5]], <i16 32767, i16 32767>
25+
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq <2 x i16> [[TMP5]], zeroinitializer
26+
; CHECK-NEXT: [[NOT_CMP2:%.*]] = xor <2 x i1> [[CMP2]], <i1 true, i1 true>
27+
; CHECK-NEXT: [[PRED1:%.*]] = extractelement <2 x i1> [[NOT_CMP2]], i32 0
28+
; CHECK-NEXT: br i1 [[PRED1]], label [[PRED_SREM_IF:%.*]], label [[PRED_SREM_CONTINUE:%.*]]
29+
; CHECK: pred.srem.if:
30+
; CHECK-NEXT: [[TMP14:%.*]] = srem i16 5786, [[TMP2]]
31+
; CHECK-NEXT: [[TMP15:%.*]] = insertelement <2 x i16> undef, i16 [[TMP14]], i32 0
32+
; CHECK-NEXT: br label [[PRED_SREM_CONTINUE]]
33+
; CHECK: pred.srem.continue:
34+
; CHECK-NEXT: [[TMP16:%.*]] = phi <2 x i16> [ undef, %vector.body ], [ [[TMP15]], [[PRED_SREM_IF]] ]
35+
; CHECK-NEXT: [[PRED2:%.*]] = extractelement <2 x i1> [[NOT_CMP2]], i32 1
36+
; CHECK-NEXT: br i1 [[PRED2]], label [[PRED_SREM_IF1:%.*]], label [[PRED_SREM_CONTINUE2]]
37+
; CHECK: pred.srem.if1:
38+
; CHECK-NEXT: [[TMP18:%.*]] = srem i16 5786, [[TMP3]]
39+
; CHECK-NEXT: [[TMP19:%.*]] = insertelement <2 x i16> [[TMP16]], i16 [[TMP18]], i32 1
40+
; CHECK-NEXT: br label [[PRED_SREM_CONTINUE2]]
41+
; CHECK: pred.srem.continue2:
42+
; CHECK-NEXT: [[TMP20:%.*]] = phi <2 x i16> [ [[TMP16]], [[PRED_SREM_CONTINUE]] ], [ [[TMP19]], [[PRED_SREM_IF1]] ]
43+
; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[CMP2]], <2 x i16> <i16 5786, i16 5786>, <2 x i16> [[TMP20]]
44+
; CHECK-NEXT: [[TMP22:%.*]] = extractelement <2 x i16> [[PREDPHI]], i32 0
45+
; CHECK-NEXT: store i16 [[TMP22]], i16* @v_39, align 1
46+
; CHECK-NEXT: [[TMP23:%.*]] = extractelement <2 x i16> [[PREDPHI]], i32 1
47+
; CHECK-NEXT: store i16 [[TMP23]], i16* @v_39, align 1
48+
; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 2
49+
; CHECK-NEXT: [[TMP24:%.*]] = icmp eq i32 [[INDEX_NEXT]], 12
50+
; CHECK-NEXT: br i1 [[TMP24]], label %middle.block, label %vector.body, !llvm.loop !0
51+
;
52+
entry:
53+
br label %for.body
54+
55+
for.body: ; preds = %entry, %for.latch
56+
%i.07 = phi i16 [ 99, %entry ], [ %inc7, %for.latch ]
57+
%lv = load i16, i16* @v_38, align 1
58+
%cmp1 = icmp eq i16 %lv, 32767
59+
br i1 %cmp1, label %cond.end, label %cond.end
60+
61+
cond.end: ; preds = %for.body, %for.body
62+
%cmp2 = icmp eq i16 %lv, 0
63+
br i1 %cmp2, label %for.latch, label %cond.false4
64+
65+
cond.false4: ; preds = %cond.end
66+
%rem = srem i16 5786, %lv
67+
br label %for.latch
68+
69+
for.latch: ; preds = %cond.end, %cond.false4
70+
%cond6 = phi i16 [ %rem, %cond.false4 ], [ 5786, %cond.end ]
71+
store i16 %cond6, i16* @v_39, align 1
72+
%inc7 = add nsw i16 %i.07, 1
73+
%cmp = icmp slt i16 %inc7, 111
74+
br i1 %cmp, label %for.body, label %exit
75+
76+
exit: ; preds = %for.latch
77+
%rv = load i16, i16* @v_39, align 1
78+
ret i16 %rv
79+
}

0 commit comments

Comments
 (0)