1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -instsimplify -S | FileCheck %s 3 4; %ret = add nuw i8 %x, C 5; nuw means no unsigned wrap, from -1 to 0. 6; So if C is -1, %x can only be 0, and the result is always -1. 7 8define i8 @add_nuw (i8 %x) { 9; CHECK-LABEL: @add_nuw( 10; CHECK-NEXT: ret i8 -1 11; 12 %ret = add nuw i8 %x, -1 13 ; nuw here means that %x can only be 0 14 ret i8 %ret 15} 16 17define i8 @add_nuw_nsw (i8 %x) { 18; CHECK-LABEL: @add_nuw_nsw( 19; CHECK-NEXT: ret i8 -1 20; 21 %ret = add nuw nsw i8 %x, -1 22 ; nuw here means that %x can only be 0 23 ret i8 %ret 24} 25 26define i8 @add_nuw_commute (i8 %x) { 27; CHECK-LABEL: @add_nuw_commute( 28; CHECK-NEXT: ret i8 -1 29; 30 %ret = add nuw i8 -1, %x ; swapped 31 ; nuw here means that %x can only be 0 32 ret i8 %ret 33} 34 35; ============================================================================ ; 36; Positive tests with value range known 37; ============================================================================ ; 38 39declare void @llvm.assume(i1 %cond); 40 41define i8 @knownbits_allones(i8 %x, i8 %y) { 42; CHECK-LABEL: @knownbits_allones( 43; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[Y:%.*]], -2 44; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]]) 45; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[Y]] 46; CHECK-NEXT: ret i8 [[RET]] 47; 48 %cmp = icmp slt i8 %y, 254 49 tail call void @llvm.assume(i1 %cmp) 50 %ret = add nuw i8 %x, %y 51 ret i8 %ret 52} 53 54; ============================================================================ ; 55; Vectors 56; ============================================================================ ; 57 58define <2 x i8> @add_vec(<2 x i8> %x) { 59; CHECK-LABEL: @add_vec( 60; CHECK-NEXT: ret <2 x i8> <i8 -1, i8 -1> 61; 62 %ret = add nuw <2 x i8> %x, <i8 -1, i8 -1> 63 ret <2 x i8> %ret 64} 65 66define <3 x i8> @add_vec_undef(<3 x i8> %x) { 67; CHECK-LABEL: @add_vec_undef( 68; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 undef, i8 -1> 69; 70 %ret = add nuw <3 x i8> %x, <i8 -1, i8 undef, i8 -1> 71 ret <3 x i8> %ret 72} 73 74; ============================================================================ ; 75; Negative tests. Should not be folded. 76; ============================================================================ ; 77 78define i8 @bad_add (i8 %x) { 79; CHECK-LABEL: @bad_add( 80; CHECK-NEXT: [[RET:%.*]] = add i8 [[X:%.*]], -1 81; CHECK-NEXT: ret i8 [[RET]] 82; 83 %ret = add i8 %x, -1 ; need nuw 84 ret i8 %ret 85} 86 87define i8 @bad_add_nsw (i8 %x) { 88; CHECK-LABEL: @bad_add_nsw( 89; CHECK-NEXT: [[RET:%.*]] = add nsw i8 [[X:%.*]], -1 90; CHECK-NEXT: ret i8 [[RET]] 91; 92 %ret = add nsw i8 %x, -1 ; need nuw 93 ret i8 %ret 94} 95 96; Second `add` operand is not `-1` constant 97 98define i8 @bad_add0(i8 %x, i8 %addop2) { 99; CHECK-LABEL: @bad_add0( 100; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[ADDOP2:%.*]] 101; CHECK-NEXT: ret i8 [[RET]] 102; 103 %ret = add nuw i8 %x, %addop2 104 ret i8 %ret 105} 106 107; Bad constant 108 109define i8 @bad_add1(i8 %x) { 110; CHECK-LABEL: @bad_add1( 111; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], 1 112; CHECK-NEXT: ret i8 [[RET]] 113; 114 %ret = add nuw i8 %x, 1 ; not -1 115 ret i8 %ret 116} 117 118define <2 x i8> @bad_add_vec_nonsplat(<2 x i8> %x) { 119; CHECK-LABEL: @bad_add_vec_nonsplat( 120; CHECK-NEXT: [[RET:%.*]] = add nuw <2 x i8> [[X:%.*]], <i8 -1, i8 1> 121; CHECK-NEXT: ret <2 x i8> [[RET]] 122; 123 %ret = add nuw <2 x i8> %x, <i8 -1, i8 1> 124 ret <2 x i8> %ret 125} 126 127; Bad known bits 128 129define i8 @bad_knownbits(i8 %x, i8 %y) { 130; CHECK-LABEL: @bad_knownbits( 131; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], -3 132; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]]) 133; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X]], [[Y:%.*]] 134; CHECK-NEXT: ret i8 [[RET]] 135; 136 %cmp = icmp slt i8 %x, 253 137 tail call void @llvm.assume(i1 %cmp) 138 %ret = add nuw i8 %x, %y 139 ret i8 %ret 140} 141