1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -codegenprepare -S < %s | FileCheck %s 3; RUN: opt -debugify -codegenprepare -S < %s | FileCheck %s -check-prefix=DEBUG 4 5target triple = "x86_64-unknown-unknown" 6 7; Nothing to sink and convert here. 8 9define i32 @no_sink(double %a, double* %b, i32 %x, i32 %y) { 10; CHECK-LABEL: @no_sink( 11; CHECK-NEXT: entry: 12; CHECK-NEXT: [[LOAD:%.*]] = load double, double* [[B:%.*]], align 8 13; CHECK-NEXT: [[CMP:%.*]] = fcmp olt double [[LOAD]], [[A:%.*]] 14; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[X:%.*]], i32 [[Y:%.*]] 15; CHECK-NEXT: ret i32 [[SEL]] 16; 17entry: 18 %load = load double, double* %b, align 8 19 %cmp = fcmp olt double %load, %a 20 %sel = select i1 %cmp, i32 %x, i32 %y 21 ret i32 %sel 22} 23 24 25; An 'fdiv' is expensive, so sink it rather than speculatively execute it. 26 27define float @fdiv_true_sink(float %a, float %b) { 28; CHECK-LABEL: @fdiv_true_sink( 29; CHECK-NEXT: entry: 30; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]] 31; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 1.000000e+00 32; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]] 33; CHECK: select.true.sink: 34; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]] 35; CHECK-NEXT: br label [[SELECT_END]] 36; CHECK: select.end: 37; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ] 38; CHECK-NEXT: ret float [[SEL]] 39; 40; DEBUG-LABEL: @fdiv_true_sink( 41; DEBUG-NEXT: entry: 42; DEBUG-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]] 43; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 1.000000e+00, !dbg !24 44; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]] 45; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_END:%.*]], !dbg 46; DEBUG: select.true.sink: 47; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]] 48; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]] 49; DEBUG-NEXT: br label [[SELECT_END]], !dbg 50; DEBUG: select.end: 51; DEBUG-NEXT: [[SEL:%.*]] = phi float [ [[DIV]], [[SELECT_TRUE_SINK]] ], [ 2.000000e+00, [[ENTRY:%.*]] ], !dbg 52; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]] 53; DEBUG-NEXT: ret float [[SEL]] 54; 55entry: 56 %div = fdiv float %a, %b 57 %cmp = fcmp ogt float %a, 1.0 58 %sel = select i1 %cmp, float %div, float 2.0 59 ret float %sel 60} 61 62define float @fdiv_false_sink(float %a, float %b) { 63; CHECK-LABEL: @fdiv_false_sink( 64; CHECK-NEXT: entry: 65; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]] 66; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 3.000000e+00 67; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]] 68; CHECK: select.false.sink: 69; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]] 70; CHECK-NEXT: br label [[SELECT_END]] 71; CHECK: select.end: 72; CHECK-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ] 73; CHECK-NEXT: ret float [[SEL]] 74; 75; DEBUG-LABEL: @fdiv_false_sink( 76; DEBUG-NEXT: entry: 77; DEBUG-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]] 78; DEBUG-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 3.000000e+00, !dbg !33 79; DEBUG-NEXT: call void @llvm.dbg.value(metadata i1 [[CMP]] 80; DEBUG-NEXT: br i1 [[CMP]], label [[SELECT_END:%.*]], label [[SELECT_FALSE_SINK:%.*]], !dbg 81; DEBUG: select.false.sink: 82; DEBUG-NEXT: [[DIV:%.*]] = fdiv float [[A]], [[B:%.*]] 83; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[DIV]] 84; DEBUG-NEXT: br label [[SELECT_END]], !dbg 85; DEBUG: select.end: 86; DEBUG-NEXT: [[SEL:%.*]] = phi float [ 4.000000e+00, [[ENTRY:%.*]] ], [ [[DIV]], [[SELECT_FALSE_SINK]] ], !dbg 87; DEBUG-NEXT: call void @llvm.dbg.value(metadata float [[SEL]] 88; DEBUG-NEXT: ret float [[SEL]], !dbg 89; 90entry: 91 %div = fdiv float %a, %b 92 %cmp = fcmp ogt float %a, 3.0 93 %sel = select i1 %cmp, float 4.0, float %div 94 ret float %sel 95} 96 97define float @fdiv_both_sink(float %a, float %b) { 98; CHECK-LABEL: @fdiv_both_sink( 99; CHECK-NEXT: entry: 100; CHECK-NEXT: [[SEL_FR:%.*]] = freeze float [[A:%.*]] 101; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[SEL_FR]], 5.000000e+00 102; CHECK-NEXT: br i1 [[CMP]], label [[SELECT_TRUE_SINK:%.*]], label [[SELECT_FALSE_SINK:%.*]] 103; CHECK: select.true.sink: 104; CHECK-NEXT: [[DIV1:%.*]] = fdiv float [[A]], [[B:%.*]] 105; CHECK-NEXT: br label [[SELECT_END:%.*]] 106; CHECK: select.false.sink: 107; CHECK-NEXT: [[DIV2:%.*]] = fdiv float [[B]], [[A]] 108; CHECK-NEXT: br label [[SELECT_END]] 109; CHECK: select.end: 110; CHECK-NEXT: [[SEL:%.*]] = phi float [ [[DIV1]], [[SELECT_TRUE_SINK]] ], [ [[DIV2]], [[SELECT_FALSE_SINK]] ] 111; CHECK-NEXT: ret float [[SEL]] 112; 113entry: 114 %div1 = fdiv float %a, %b 115 %div2 = fdiv float %b, %a 116 %cmp = fcmp ogt float %a, 5.0 117 %sel = select i1 %cmp, float %div1, float %div2 118 ret float %sel 119} 120 121; But if the select is marked unpredictable, then don't turn it into a branch. 122 123define float @unpredictable_select(float %a, float %b) { 124; CHECK-LABEL: @unpredictable_select( 125; CHECK-NEXT: entry: 126; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]] 127; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00 128; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[DIV]], float 2.000000e+00, !unpredictable !0 129; CHECK-NEXT: ret float [[SEL]] 130; 131entry: 132 %div = fdiv float %a, %b 133 %cmp = fcmp ogt float %a, 1.0 134 %sel = select i1 %cmp, float %div, float 2.0, !unpredictable !0 135 ret float %sel 136} 137 138!0 = !{} 139 140; An 'fadd' is not too expensive, so it's ok to speculate. 141 142define float @fadd_no_sink(float %a, float %b) { 143; CHECK-LABEL: @fadd_no_sink( 144; CHECK-NEXT: [[ADD:%.*]] = fadd float [[A:%.*]], [[B:%.*]] 145; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float 6.000000e+00, [[A]] 146; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 7.000000e+00 147; CHECK-NEXT: ret float [[SEL]] 148; 149 %add = fadd float %a, %b 150 %cmp = fcmp ogt float 6.0, %a 151 %sel = select i1 %cmp, float %add, float 7.0 152 ret float %sel 153} 154 155; Possible enhancement: sinkability is only calculated with the direct 156; operand of the select, so we don't try to sink this. The fdiv cost is not 157; taken into account. 158 159define float @fdiv_no_sink(float %a, float %b) { 160; CHECK-LABEL: @fdiv_no_sink( 161; CHECK-NEXT: entry: 162; CHECK-NEXT: [[DIV:%.*]] = fdiv float [[A:%.*]], [[B:%.*]] 163; CHECK-NEXT: [[ADD:%.*]] = fadd float [[DIV]], [[B]] 164; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[A]], 1.000000e+00 165; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], float [[ADD]], float 8.000000e+00 166; CHECK-NEXT: ret float [[SEL]] 167; 168entry: 169 %div = fdiv float %a, %b 170 %add = fadd float %div, %b 171 %cmp = fcmp ogt float %a, 1.0 172 %sel = select i1 %cmp, float %add, float 8.0 173 ret float %sel 174} 175 176; Do not transform the CFG if the select operands may have side effects. 177 178declare i64* @bar(i32, i32, i32) 179declare i64* @baz(i32, i32, i32) 180 181define i64* @calls_no_sink(i32 %in) { 182; CHECK-LABEL: @calls_no_sink( 183; CHECK-NEXT: [[CALL1:%.*]] = call i64* @bar(i32 1, i32 2, i32 3) 184; CHECK-NEXT: [[CALL2:%.*]] = call i64* @baz(i32 1, i32 2, i32 3) 185; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[IN:%.*]], 0 186; CHECK-NEXT: [[SEL:%.*]] = select i1 [[TOBOOL]], i64* [[CALL1]], i64* [[CALL2]] 187; CHECK-NEXT: ret i64* [[SEL]] 188; 189 %call1 = call i64* @bar(i32 1, i32 2, i32 3) 190 %call2 = call i64* @baz(i32 1, i32 2, i32 3) 191 %tobool = icmp ne i32 %in, 0 192 %sel = select i1 %tobool, i64* %call1, i64* %call2 193 ret i64* %sel 194} 195 196define i32 @sdiv_no_sink(i32 %a, i32 %b) { 197; CHECK-LABEL: @sdiv_no_sink( 198; CHECK-NEXT: [[DIV1:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]] 199; CHECK-NEXT: [[DIV2:%.*]] = sdiv i32 [[B]], [[A]] 200; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A]], 5 201; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 [[DIV1]], i32 [[DIV2]] 202; CHECK-NEXT: ret i32 [[SEL]] 203; 204 %div1 = sdiv i32 %a, %b 205 %div2 = sdiv i32 %b, %a 206 %cmp = icmp sgt i32 %a, 5 207 %sel = select i1 %cmp, i32 %div1, i32 %div2 208 ret i32 %sel 209} 210 211