1; RUN: opt < %s -instcombine -S | FileCheck %s 2; RUN: opt -debugify -instcombine -S < %s | FileCheck %s -check-prefix DBGINFO 3 4target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32" 5 6define i32 @mul(i32 %x, i32 %y) { 7; CHECK-LABEL: @mul( 8; CHECK-NEXT: [[C:%.*]] = mul i32 [[X:%.*]], [[Y:%.*]] 9; CHECK-NEXT: [[D:%.*]] = and i32 [[C]], 255 10; CHECK-NEXT: ret i32 [[D]] 11 12; Test that when zext is evaluated in different type 13; we preserve the debug information in the resulting 14; instruction. 15; DBGINFO-LABEL: @mul( 16; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 %x, {{.*}} !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)) 17; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 %y, {{.*}} !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)) 18; DBGINFO-NEXT: [[C:%.*]] = mul i32 {{.*}} 19; DBGINFO-NEXT: [[D:%.*]] = and i32 {{.*}} 20; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[C]] 21; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i32 [[D]] 22 23 %A = trunc i32 %x to i8 24 %B = trunc i32 %y to i8 25 %C = mul i8 %A, %B 26 %D = zext i8 %C to i32 27 ret i32 %D 28} 29 30define i32 @select1(i1 %cond, i32 %x, i32 %y, i32 %z) { 31; CHECK-LABEL: @select1( 32; CHECK-NEXT: [[D:%.*]] = add i32 [[X:%.*]], [[Y:%.*]] 33; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i32 [[Z:%.*]], i32 [[D]] 34; CHECK-NEXT: [[F:%.*]] = and i32 [[E]], 255 35; CHECK-NEXT: ret i32 [[F]] 36; 37 %A = trunc i32 %x to i8 38 %B = trunc i32 %y to i8 39 %C = trunc i32 %z to i8 40 %D = add i8 %A, %B 41 %E = select i1 %cond, i8 %C, i8 %D 42 %F = zext i8 %E to i32 43 ret i32 %F 44} 45 46define i8 @select2(i1 %cond, i8 %x, i8 %y, i8 %z) { 47; CHECK-LABEL: @select2( 48; CHECK-NEXT: [[D:%.*]] = add i8 [[X:%.*]], [[Y:%.*]] 49; CHECK-NEXT: [[E:%.*]] = select i1 [[COND:%.*]], i8 [[Z:%.*]], i8 [[D]] 50; CHECK-NEXT: ret i8 [[E]] 51; 52 %A = zext i8 %x to i32 53 %B = zext i8 %y to i32 54 %C = zext i8 %z to i32 55 %D = add i32 %A, %B 56 %E = select i1 %cond, i32 %C, i32 %D 57 %F = trunc i32 %E to i8 58 ret i8 %F 59} 60 61; The next 3 tests could be handled in instcombine, but evaluating values 62; with multiple uses may be very slow. Let some other pass deal with it. 63 64define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) { 65; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst( 66; CHECK-NEXT: [[Z:%.*]] = zext i32 [[X:%.*]] to i64 67; CHECK-NEXT: [[A:%.*]] = add nuw nsw i64 [[Z]], 15 68; CHECK-NEXT: [[M:%.*]] = mul i64 [[A]], [[A]] 69; CHECK-NEXT: [[T:%.*]] = trunc i64 [[M]] to i32 70; CHECK-NEXT: ret i32 [[T]] 71; 72 %z = zext i32 %x to i64 73 %a = add nsw nuw i64 %z, 15 74 %m = mul i64 %a, %a 75 %t = trunc i64 %m to i32 76 ret i32 %t 77} 78 79define i32 @eval_zext_multi_use_in_one_inst(i32 %x) { 80; CHECK-LABEL: @eval_zext_multi_use_in_one_inst( 81; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16 82; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 5 83; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]] 84; CHECK-NEXT: [[R:%.*]] = zext i16 [[M]] to i32 85; CHECK-NEXT: ret i32 [[R]] 86; 87 %t = trunc i32 %x to i16 88 %a = and i16 %t, 5 89 %m = mul nuw nsw i16 %a, %a 90 %r = zext i16 %m to i32 91 ret i32 %r 92} 93 94define i32 @eval_sext_multi_use_in_one_inst(i32 %x) { 95; CHECK-LABEL: @eval_sext_multi_use_in_one_inst( 96; CHECK-NEXT: [[T:%.*]] = trunc i32 [[X:%.*]] to i16 97; CHECK-NEXT: [[A:%.*]] = and i16 [[T]], 14 98; CHECK-NEXT: [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]] 99; CHECK-NEXT: [[O:%.*]] = or i16 [[M]], -32768 100; CHECK-NEXT: [[R:%.*]] = sext i16 [[O]] to i32 101; CHECK-NEXT: ret i32 [[R]] 102; 103 %t = trunc i32 %x to i16 104 %a = and i16 %t, 14 105 %m = mul nuw nsw i16 %a, %a 106 %o = or i16 %m, 32768 107 %r = sext i16 %o to i32 108 ret i32 %r 109} 110 111; If we have a transform to shrink the above 3 cases, make sure it's not 112; also trying to look through multiple uses in this test and crashing. 113 114define void @PR36225(i32 %a, i32 %b) { 115; CHECK-LABEL: @PR36225( 116; CHECK-NEXT: entry: 117; CHECK-NEXT: br label [[WHILE_BODY:%.*]] 118; CHECK: while.body: 119; CHECK-NEXT: br i1 undef, label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]] 120; CHECK: for.body3.us: 121; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[B:%.*]], 0 122; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4 123; CHECK-NEXT: switch i3 undef, label [[EXIT:%.*]] [ 124; CHECK-NEXT: i3 0, label [[FOR_END:%.*]] 125; CHECK-NEXT: i3 -1, label [[FOR_END]] 126; CHECK-NEXT: ] 127; CHECK: for.body3: 128; CHECK-NEXT: switch i3 undef, label [[EXIT]] [ 129; CHECK-NEXT: i3 0, label [[FOR_END]] 130; CHECK-NEXT: i3 -1, label [[FOR_END]] 131; CHECK-NEXT: ] 132; CHECK: for.end: 133; CHECK-NEXT: [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ] 134; CHECK-NEXT: [[TMP0:%.*]] = zext i8 [[H]] to i32 135; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[A:%.*]] 136; CHECK-NEXT: br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]] 137; CHECK: exit2: 138; CHECK-NEXT: unreachable 139; CHECK: exit: 140; CHECK-NEXT: unreachable 141; 142entry: 143 br label %while.body 144 145while.body: 146 %tobool = icmp eq i32 %b, 0 147 br i1 undef, label %for.body3.us, label %for.body3 148 149for.body3.us: 150 %spec.select = select i1 %tobool, i8 0, i8 4 151 switch i3 undef, label %exit [ 152 i3 0, label %for.end 153 i3 -1, label %for.end 154 ] 155 156for.body3: 157 switch i3 undef, label %exit [ 158 i3 0, label %for.end 159 i3 -1, label %for.end 160 ] 161 162for.end: 163 %h = phi i8 [ %spec.select, %for.body3.us ], [ %spec.select, %for.body3.us ], [ 0, %for.body3 ], [ 0, %for.body3 ] 164 %conv = sext i8 %h to i32 165 %cmp = icmp sgt i32 %a, %conv 166 br i1 %cmp, label %exit, label %exit2 167 168exit2: 169 unreachable 170 171exit: 172 unreachable 173} 174 175; Check that we don't drop debug info when a zext is removed. 176define i1 @foo(i1 zeroext %b) { 177; DBGINFO-LABEL: @foo( 178; DBGINFO-NEXT: call void @llvm.dbg.value(metadata i1 %b, {{.*}} !DIExpression(DW_OP_LLVM_convert, 1, DW_ATE_unsigned, DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_stack_value)) 179; DBGINFO-NEXT: ret i1 %b 180 181 %frombool = zext i1 %b to i8 182 ret i1 %b 183} 184