1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=instcombine,lower-constant-intrinsics -S < %s | FileCheck %s
4 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-unknown-linux-gnu"
7 declare dso_local i32 @posix_memalign(ptr noundef, i64 noundef, i64 noundef)
8 declare i64 @llvm.objectsize.i64.p0(ptr, i1 immarg, i1 immarg, i1 immarg)
10 ; Check posix_memalign call with proper handlig of return value
11 define dso_local i64 @check_posix_memalign(i32 noundef %n) local_unnamed_addr {
12 ; CHECK-LABEL: @check_posix_memalign(
14 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
15 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
16 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
17 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COND_FALSE:%.*]], label [[EXIT:%.*]]
19 ; CHECK-NEXT: br label [[EXIT]]
21 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[ENTRY:%.*]] ], [ 10, [[COND_FALSE]] ]
22 ; CHECK-NEXT: ret i64 [[COND]]
26 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
27 %tobool = icmp ne i32 %call, 0
28 br i1 %tobool, label %exit, label %cond.false
31 %val = load ptr, ptr %obj
32 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
36 %cond = phi i64 [ -2, %entry ], [ %objsize, %cond.false ]
42 ; Same test case as above but with idiomatic NULL initialization
43 define dso_local i64 @check_posix_memalign_null() {
44 ; CHECK-LABEL: @check_posix_memalign_null(
46 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
47 ; CHECK-NEXT: store ptr null, ptr [[OBJ]], align 8
48 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
49 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
50 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COND_FALSE:%.*]], label [[EXIT:%.*]]
52 ; CHECK-NEXT: br label [[EXIT]]
54 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[ENTRY:%.*]] ], [ 10, [[COND_FALSE]] ]
55 ; CHECK-NEXT: ret i64 [[COND]]
59 store ptr null, ptr %obj
60 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
61 %tobool = icmp ne i32 %call, 0
62 br i1 %tobool, label %exit, label %cond.false
65 %val = load ptr, ptr %obj
66 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
70 %cond = phi i64 [ -2, %entry ], [ %objsize, %cond.false ]
74 ; Using argument storage instead of local storage for the allocated pointer.
75 define dso_local i64 @check_posix_memalign_arg(ptr noalias noundef %obj) {
76 ; CHECK-LABEL: @check_posix_memalign_arg(
78 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef [[OBJ:%.*]], i64 noundef 8, i64 noundef 10)
79 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
80 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COND_FALSE:%.*]], label [[EXIT:%.*]]
82 ; CHECK-NEXT: br label [[EXIT]]
84 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[ENTRY:%.*]] ], [ 10, [[COND_FALSE]] ]
85 ; CHECK-NEXT: ret i64 [[COND]]
88 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
89 %tobool = icmp ne i32 %call, 0
90 br i1 %tobool, label %exit, label %cond.false
93 %val = load ptr, ptr %obj
94 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
98 %cond = phi i64 [ -2, %entry ], [ %objsize, %cond.false ]
103 ; posix_memalign can fail, in that case no object_size can be guessed.
104 define dso_local i64 @check_posix_memalign_unchecked() {
105 ; CHECK-LABEL: @check_posix_memalign_unchecked(
107 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
108 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
109 ; CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr [[OBJ]], align 8
110 ; CHECK-NEXT: ret i64 -1
114 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
115 %val = load ptr, ptr %obj
116 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
120 ; Checks that bo upon posix_memalign failure behaves correctly
121 define dso_local i64 @check_posix_memalign_inverted_cond() {
122 ; CHECK-LABEL: @check_posix_memalign_inverted_cond(
124 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
125 ; CHECK-NEXT: store ptr null, ptr [[OBJ]], align 8
126 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
127 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[CALL]], 0
128 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[EXIT:%.*]], label [[COND_FALSE:%.*]]
130 ; CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr [[OBJ]], align 8
131 ; CHECK-NEXT: br label [[EXIT]]
133 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[ENTRY:%.*]] ], [ -1, [[COND_FALSE]] ]
134 ; CHECK-NEXT: ret i64 [[COND]]
138 store ptr null, ptr %obj
139 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
140 %tobool = icmp eq i32 %call, 0
141 br i1 %tobool, label %exit, label %cond.false
144 %val = load ptr, ptr %obj
145 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
149 %cond = phi i64 [ -2, %entry ], [ %objsize, %cond.false ]
153 ; Check posix_memalign call with a runtime condition check
154 define dso_local i64 @check_posix_memalign_runtime_cond(i32 noundef %n) local_unnamed_addr {
155 ; CHECK-LABEL: @check_posix_memalign_runtime_cond(
157 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
158 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
159 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], [[N:%.*]]
160 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COND_FALSE:%.*]], label [[EXIT:%.*]]
162 ; CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr [[OBJ]], align 8
163 ; CHECK-NEXT: br label [[EXIT]]
165 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[ENTRY:%.*]] ], [ -1, [[COND_FALSE]] ]
166 ; CHECK-NEXT: ret i64 [[COND]]
170 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
171 %tobool = icmp ne i32 %call, %n
172 br i1 %tobool, label %exit, label %cond.false
175 %val = load ptr, ptr %obj
176 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
180 %cond = phi i64 [ -2, %entry ], [ %objsize, %cond.false ]
185 ; Check posix_memalign call with two different paths leading to the same alloc.
186 define dso_local i64 @check_posix_memalign_diamond() local_unnamed_addr {
187 ; CHECK-LABEL: @check_posix_memalign_diamond(
189 ; CHECK-NEXT: [[OBJ:%.*]] = alloca ptr, align 8
190 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @posix_memalign(ptr noundef nonnull [[OBJ]], i64 noundef 8, i64 noundef 10)
191 ; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[CALL]], 0
192 ; CHECK-NEXT: br i1 [[TOBOOL_NOT]], label [[COND_FALSE:%.*]], label [[COND_TRUE:%.*]]
194 ; CHECK-NEXT: br label [[EXIT:%.*]]
196 ; CHECK-NEXT: br label [[EXIT]]
198 ; CHECK-NEXT: [[COND:%.*]] = phi i64 [ -2, [[COND_TRUE]] ], [ 10, [[COND_FALSE]] ]
199 ; CHECK-NEXT: ret i64 [[COND]]
203 %call = call i32 @posix_memalign(ptr noundef %obj, i64 noundef 8, i64 noundef 10)
204 %tobool = icmp ne i32 %call, 0
205 br i1 %tobool, label %cond.true, label %cond.false
211 %val = load ptr, ptr %obj
212 %objsize = call i64 @llvm.objectsize.i64.p0(ptr %val, i1 false, i1 true, i1 false)
216 %cond = phi i64 [ -2, %cond.true ], [ %objsize, %cond.false ]