1 ; RUN: opt < %s -bounds-checking -S | FileCheck %s 2 target 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:64-S128" 3 4 @.str = private constant [8 x i8] c"abcdefg\00" ; <[8 x i8]*> 5 6 declare noalias i8* @malloc(i64) nounwind 7 declare noalias i8* @calloc(i64, i64) nounwind 8 declare noalias i8* @realloc(i8* nocapture, i64) nounwind 9 10 ; CHECK: @f1 11 define void @f1() nounwind { 12 %1 = tail call i8* @malloc(i64 32) 13 %2 = bitcast i8* %1 to i32* 14 %idx = getelementptr inbounds i32* %2, i64 2 15 ; CHECK-NOT: trap 16 store i32 3, i32* %idx, align 4 17 ret void 18 } 19 20 ; CHECK: @f2 21 define void @f2() nounwind { 22 %1 = tail call i8* @malloc(i64 32) 23 %2 = bitcast i8* %1 to i32* 24 %idx = getelementptr inbounds i32* %2, i64 8 25 ; CHECK: trap 26 store i32 3, i32* %idx, align 4 27 ret void 28 } 29 30 ; CHECK: @f3 31 define void @f3(i64 %x) nounwind { 32 %1 = tail call i8* @calloc(i64 4, i64 %x) 33 %2 = bitcast i8* %1 to i32* 34 %idx = getelementptr inbounds i32* %2, i64 8 35 ; CHECK: mul i64 4, % 36 ; CHECK: sub i64 {{.*}}, 32 37 ; CHECK-NEXT: icmp ult i64 {{.*}}, 32 38 ; CHECK-NEXT: icmp ult i64 {{.*}}, 4 39 ; CHECK-NEXT: or i1 40 ; CHECK: trap 41 store i32 3, i32* %idx, align 4 42 ret void 43 } 44 45 ; CHECK: @f4 46 define void @f4(i64 %x) nounwind { 47 %1 = tail call i8* @realloc(i8* null, i64 %x) nounwind 48 %2 = bitcast i8* %1 to i32* 49 %idx = getelementptr inbounds i32* %2, i64 8 50 ; CHECK: trap 51 %3 = load i32* %idx, align 4 52 ret void 53 } 54 55 ; CHECK: @f5 56 define void @f5(i64 %x) nounwind { 57 %idx = getelementptr inbounds [8 x i8]* @.str, i64 0, i64 %x 58 ; CHECK: trap 59 %1 = load i8* %idx, align 4 60 ret void 61 } 62 63 ; CHECK: @f6 64 define void @f6(i64 %x) nounwind { 65 %1 = alloca i128 66 ; CHECK-NOT: trap 67 %2 = load i128* %1, align 4 68 ret void 69 } 70 71 ; CHECK: @f7 72 define void @f7(i64 %x) nounwind { 73 %1 = alloca i128, i64 %x 74 ; CHECK: mul i64 16, 75 ; CHECK: trap 76 %2 = load i128* %1, align 4 77 ret void 78 } 79 80 ; CHECK: @f8 81 define void @f8() nounwind { 82 %1 = alloca i128 83 %2 = alloca i128 84 %3 = select i1 undef, i128* %1, i128* %2 85 ; CHECK-NOT: trap 86 %4 = load i128* %3, align 4 87 ret void 88 } 89 90 ; CHECK: @f9 91 define void @f9(i128* %arg) nounwind { 92 %1 = alloca i128 93 %2 = select i1 undef, i128* %arg, i128* %1 94 ; CHECK-NOT: trap 95 %3 = load i128* %2, align 4 96 ret void 97 } 98 99 ; CHECK: @f10 100 define void @f10(i64 %x, i64 %y) nounwind { 101 %1 = alloca i128, i64 %x 102 %2 = alloca i128, i64 %y 103 %3 = select i1 undef, i128* %1, i128* %2 104 ; CHECK: select 105 ; CHECK: select 106 ; CHECK: trap 107 %4 = load i128* %3, align 4 108 ret void 109 } 110 111 ; CHECK: @f11 112 define void @f11(i128* byval %x) nounwind { 113 %1 = bitcast i128* %x to i8* 114 %2 = getelementptr inbounds i8* %1, i64 16 115 ; CHECK: br label 116 %3 = load i8* %2, align 4 117 ret void 118 } 119 120 ; CHECK: @f12 121 define i64 @f12(i64 %x, i64 %y) nounwind { 122 %1 = tail call i8* @calloc(i64 1, i64 %x) 123 ; CHECK: mul i64 %y, 8 124 %2 = bitcast i8* %1 to i64* 125 %3 = getelementptr inbounds i64* %2, i64 %y 126 %4 = load i64* %3, align 8 127 ret i64 %4 128 } 129