Skip to content

Commit ca78b56

Browse files
committed
[SelectOpt] Don't treat LogicalAnd/LogicalOr as selects
A `select i1 %c, i1 true, i1 %d` is just an or and a `select i1 %c, i1 %d, i1 false` is just an and. There are better treated as such in the logic of SelectOpt, allowing the backend to optimize them to and/or directly. Differential Revision: https://reviews.llvm.org/D138490
1 parent 0cb2dd5 commit ca78b56

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,27 @@ void SelectOptimize::convertProfitableSIGroups(SelectGroups &ProfSIGroups) {
515515
}
516516
}
517517

518+
static bool isSpecialSelect(SelectInst *SI) {
519+
using namespace llvm::PatternMatch;
520+
521+
// If the select is a logical-and/logical-or then it is better treated as a
522+
// and/or by the backend.
523+
if (match(SI, m_CombineOr(m_LogicalAnd(m_Value(), m_Value()),
524+
m_LogicalOr(m_Value(), m_Value()))))
525+
return true;
526+
527+
return false;
528+
}
529+
518530
void SelectOptimize::collectSelectGroups(BasicBlock &BB,
519531
SelectGroups &SIGroups) {
520532
BasicBlock::iterator BBIt = BB.begin();
521533
while (BBIt != BB.end()) {
522534
Instruction *I = &*BBIt++;
523535
if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
536+
if (isSpecialSelect(SI))
537+
continue;
538+
524539
SelectGroup SIGroup;
525540
SIGroup.push_back(SI);
526541
while (BBIt != BB.end()) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -select-optimize -mtriple=aarch64-linux-gnu -mcpu=neoverse-v2 -S < %s | FileCheck %s
3+
4+
define i32 @test(ptr nocapture noundef readnone %x, i32 noundef %iters) {
5+
; CHECK-LABEL: @test(
6+
; CHECK-NEXT: entry:
7+
; CHECK-NEXT: [[CMP3:%.*]] = icmp sgt i32 [[ITERS:%.*]], 0
8+
; CHECK-NEXT: br i1 [[CMP3]], label [[FOR_BODY_I_PREHEADER:%.*]], label [[FOR_COND_CLEANUP:%.*]]
9+
; CHECK: for.body.i.preheader:
10+
; CHECK-NEXT: [[I_05:%.*]] = phi i32 [ [[INC:%.*]], [[INNER_LOOP_010_EXIT:%.*]] ], [ 0, [[ENTRY:%.*]] ]
11+
; CHECK-NEXT: [[RES_04:%.*]] = phi i32 [ [[OR:%.*]], [[INNER_LOOP_010_EXIT]] ], [ 0, [[ENTRY]] ]
12+
; CHECK-NEXT: br label [[FOR_BODY_I:%.*]]
13+
; CHECK: for.cond.cleanup:
14+
; CHECK-NEXT: [[RES_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[OR]], [[INNER_LOOP_010_EXIT]] ]
15+
; CHECK-NEXT: ret i32 [[RES_0_LCSSA]]
16+
; CHECK: for.body.i:
17+
; CHECK-NEXT: [[INDVARS_IV_I:%.*]] = phi i64 [ 0, [[FOR_BODY_I_PREHEADER]] ], [ [[INDVARS_IV_NEXT_I_4:%.*]], [[FOR_BODY_I]] ]
18+
; CHECK-NEXT: [[ALL_0_OFF010_I:%.*]] = phi i1 [ true, [[FOR_BODY_I_PREHEADER]] ], [ [[ALL_0_OFF0__I_4:%.*]], [[FOR_BODY_I]] ]
19+
; CHECK-NEXT: [[ANY_0_OFF09_I:%.*]] = phi i1 [ false, [[FOR_BODY_I_PREHEADER]] ], [ [[DOTANY_0_OFF0_I_4:%.*]], [[FOR_BODY_I]] ]
20+
; CHECK-NEXT: [[ARRAYIDX_I:%.*]] = getelementptr inbounds float, ptr [[X:%.*]], i64 [[INDVARS_IV_I]]
21+
; CHECK-NEXT: [[TMP0:%.*]] = load float, ptr [[ARRAYIDX_I]], align 4
22+
; CHECK-NEXT: [[CMP1_I:%.*]] = fcmp fast olt float [[TMP0]], 0.000000e+00
23+
; CHECK-NEXT: [[INDVARS_IV_NEXT_I:%.*]] = add nuw nsw i64 [[INDVARS_IV_I]], 1
24+
; CHECK-NEXT: [[ARRAYIDX_I_1:%.*]] = getelementptr inbounds float, ptr [[X]], i64 [[INDVARS_IV_NEXT_I]]
25+
; CHECK-NEXT: [[TMP1:%.*]] = load float, ptr [[ARRAYIDX_I_1]], align 4
26+
; CHECK-NEXT: [[CMP1_I_1:%.*]] = fcmp fast olt float [[TMP1]], 0.000000e+00
27+
; CHECK-NEXT: [[INDVARS_IV_NEXT_I_1:%.*]] = add nuw nsw i64 [[INDVARS_IV_I]], 2
28+
; CHECK-NEXT: [[ARRAYIDX_I_2:%.*]] = getelementptr inbounds float, ptr [[X]], i64 [[INDVARS_IV_NEXT_I_1]]
29+
; CHECK-NEXT: [[TMP2:%.*]] = load float, ptr [[ARRAYIDX_I_2]], align 4
30+
; CHECK-NEXT: [[CMP1_I_2:%.*]] = fcmp fast olt float [[TMP2]], 0.000000e+00
31+
; CHECK-NEXT: [[INDVARS_IV_NEXT_I_2:%.*]] = add nuw nsw i64 [[INDVARS_IV_I]], 3
32+
; CHECK-NEXT: [[ARRAYIDX_I_3:%.*]] = getelementptr inbounds float, ptr [[X]], i64 [[INDVARS_IV_NEXT_I_2]]
33+
; CHECK-NEXT: [[TMP3:%.*]] = load float, ptr [[ARRAYIDX_I_3]], align 4
34+
; CHECK-NEXT: [[CMP1_I_3:%.*]] = fcmp fast olt float [[TMP3]], 0.000000e+00
35+
; CHECK-NEXT: [[INDVARS_IV_NEXT_I_3:%.*]] = add nuw nsw i64 [[INDVARS_IV_I]], 4
36+
; CHECK-NEXT: [[ARRAYIDX_I_4:%.*]] = getelementptr inbounds float, ptr [[X]], i64 [[INDVARS_IV_NEXT_I_3]]
37+
; CHECK-NEXT: [[TMP4:%.*]] = load float, ptr [[ARRAYIDX_I_4]], align 4
38+
; CHECK-NEXT: [[CMP1_I_4:%.*]] = fcmp fast olt float [[TMP4]], 0.000000e+00
39+
; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[CMP1_I_4]], i1 true, i1 [[CMP1_I_3]]
40+
; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP5]], i1 true, i1 [[CMP1_I_2]]
41+
; CHECK-NEXT: [[TMP7:%.*]] = select i1 [[TMP6]], i1 true, i1 [[CMP1_I_1]]
42+
; CHECK-NEXT: [[TMP8:%.*]] = select i1 [[TMP7]], i1 true, i1 [[CMP1_I]]
43+
; CHECK-NEXT: [[DOTANY_0_OFF0_I_4]] = select i1 [[TMP8]], i1 true, i1 [[ANY_0_OFF09_I]]
44+
; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[CMP1_I_4]], i1 [[CMP1_I_3]], i1 false
45+
; CHECK-NEXT: [[TMP10:%.*]] = select i1 [[TMP9]], i1 [[CMP1_I_2]], i1 false
46+
; CHECK-NEXT: [[TMP11:%.*]] = select i1 [[TMP10]], i1 [[CMP1_I_1]], i1 false
47+
; CHECK-NEXT: [[TMP12:%.*]] = select i1 [[TMP11]], i1 [[CMP1_I]], i1 false
48+
; CHECK-NEXT: [[ALL_0_OFF0__I_4]] = select i1 [[TMP12]], i1 [[ALL_0_OFF010_I]], i1 false
49+
; CHECK-NEXT: [[INDVARS_IV_NEXT_I_4]] = add nuw nsw i64 [[INDVARS_IV_I]], 5
50+
; CHECK-NEXT: [[EXITCOND_NOT_I_4:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT_I_4]], 10000
51+
; CHECK-NEXT: br i1 [[EXITCOND_NOT_I_4]], label [[INNER_LOOP_010_EXIT]], label [[FOR_BODY_I]]
52+
; CHECK: inner_loop_010.exit:
53+
; CHECK-NEXT: [[TMP13:%.*]] = select i1 [[DOTANY_0_OFF0_I_4]], i32 2, i32 3
54+
; CHECK-NEXT: [[TMP14:%.*]] = select i1 [[ALL_0_OFF0__I_4]], i32 1, i32 [[TMP13]]
55+
; CHECK-NEXT: [[OR]] = or i32 [[TMP14]], [[RES_04]]
56+
; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I_05]], 1
57+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC]], [[ITERS]]
58+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_COND_CLEANUP]], label [[FOR_BODY_I_PREHEADER]]
59+
;
60+
entry:
61+
%cmp3 = icmp sgt i32 %iters, 0
62+
br i1 %cmp3, label %for.body.i.preheader, label %for.cond.cleanup
63+
64+
for.body.i.preheader: ; preds = %entry, %inner_loop_010.exit
65+
%i.05 = phi i32 [ %inc, %inner_loop_010.exit ], [ 0, %entry ]
66+
%res.04 = phi i32 [ %or, %inner_loop_010.exit ], [ 0, %entry ]
67+
br label %for.body.i
68+
69+
for.cond.cleanup: ; preds = %inner_loop_010.exit, %entry
70+
%res.0.lcssa = phi i32 [ 0, %entry ], [ %or, %inner_loop_010.exit ]
71+
ret i32 %res.0.lcssa
72+
73+
for.body.i: ; preds = %for.body.i, %for.body.i.preheader
74+
%indvars.iv.i = phi i64 [ 0, %for.body.i.preheader ], [ %indvars.iv.next.i.4, %for.body.i ]
75+
%all.0.off010.i = phi i1 [ true, %for.body.i.preheader ], [ %all.0.off0..i.4, %for.body.i ]
76+
%any.0.off09.i = phi i1 [ false, %for.body.i.preheader ], [ %.any.0.off0.i.4, %for.body.i ]
77+
%arrayidx.i = getelementptr inbounds float, ptr %x, i64 %indvars.iv.i
78+
%0 = load float, ptr %arrayidx.i, align 4
79+
%cmp1.i = fcmp fast olt float %0, 0.000000e+00
80+
%indvars.iv.next.i = add nuw nsw i64 %indvars.iv.i, 1
81+
%arrayidx.i.1 = getelementptr inbounds float, ptr %x, i64 %indvars.iv.next.i
82+
%1 = load float, ptr %arrayidx.i.1, align 4
83+
%cmp1.i.1 = fcmp fast olt float %1, 0.000000e+00
84+
%indvars.iv.next.i.1 = add nuw nsw i64 %indvars.iv.i, 2
85+
%arrayidx.i.2 = getelementptr inbounds float, ptr %x, i64 %indvars.iv.next.i.1
86+
%2 = load float, ptr %arrayidx.i.2, align 4
87+
%cmp1.i.2 = fcmp fast olt float %2, 0.000000e+00
88+
%indvars.iv.next.i.2 = add nuw nsw i64 %indvars.iv.i, 3
89+
%arrayidx.i.3 = getelementptr inbounds float, ptr %x, i64 %indvars.iv.next.i.2
90+
%3 = load float, ptr %arrayidx.i.3, align 4
91+
%cmp1.i.3 = fcmp fast olt float %3, 0.000000e+00
92+
%indvars.iv.next.i.3 = add nuw nsw i64 %indvars.iv.i, 4
93+
%arrayidx.i.4 = getelementptr inbounds float, ptr %x, i64 %indvars.iv.next.i.3
94+
%4 = load float, ptr %arrayidx.i.4, align 4
95+
%cmp1.i.4 = fcmp fast olt float %4, 0.000000e+00
96+
%5 = select i1 %cmp1.i.4, i1 true, i1 %cmp1.i.3
97+
%6 = select i1 %5, i1 true, i1 %cmp1.i.2
98+
%7 = select i1 %6, i1 true, i1 %cmp1.i.1
99+
%8 = select i1 %7, i1 true, i1 %cmp1.i
100+
%.any.0.off0.i.4 = select i1 %8, i1 true, i1 %any.0.off09.i
101+
%9 = select i1 %cmp1.i.4, i1 %cmp1.i.3, i1 false
102+
%10 = select i1 %9, i1 %cmp1.i.2, i1 false
103+
%11 = select i1 %10, i1 %cmp1.i.1, i1 false
104+
%12 = select i1 %11, i1 %cmp1.i, i1 false
105+
%all.0.off0..i.4 = select i1 %12, i1 %all.0.off010.i, i1 false
106+
%indvars.iv.next.i.4 = add nuw nsw i64 %indvars.iv.i, 5
107+
%exitcond.not.i.4 = icmp eq i64 %indvars.iv.next.i.4, 10000
108+
br i1 %exitcond.not.i.4, label %inner_loop_010.exit, label %for.body.i
109+
110+
inner_loop_010.exit: ; preds = %for.body.i
111+
%13 = select i1 %.any.0.off0.i.4, i32 2, i32 3
112+
%14 = select i1 %all.0.off0..i.4, i32 1, i32 %13
113+
%or = or i32 %14, %res.04
114+
%inc = add nuw nsw i32 %i.05, 1
115+
%exitcond.not = icmp eq i32 %inc, %iters
116+
br i1 %exitcond.not, label %for.cond.cleanup, label %for.body.i.preheader
117+
}

0 commit comments

Comments
 (0)