298 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes2; RUN: opt -S -passes=argpromotion < %s | FileCheck %s3 4; In the following tests, the call to @callee may invalidate ptr %test_c and so5; prohibit removing loads of %test_c following the call, preventing Argument6; Promotion of %test_c in the general case.7 8; This is called by @caller_ptr_args, from which we cannot prove anything about9; whether %test_c may alias %p and so we cannot promote %test_c.10;11define internal i32 @test_cannot_promote_1(ptr %p, ptr nocapture readonly %test_c) {12; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_113; CHECK-SAME: (ptr [[P:%.*]], ptr readonly captures(none) [[TEST_C:%.*]]) {14; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 415; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])16; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 417; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]18; CHECK-NEXT: ret i32 [[SUM]]19;20 %res = call i32 @callee(ptr %p, ptr %test_c)21 22 %ltest_c = load i32, ptr %test_c23 24 %sum = add i32 %ltest_c, %res25 26 ret i32 %sum27}28 29; This is called by multiple callers, from which we can see that %test_c may30; alias %p and so we cannot promote %test_c.31;32define internal i32 @test_cannot_promote_2(ptr %p, ptr nocapture readonly %test_c) {33; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_234; CHECK-SAME: (ptr [[P:%.*]], ptr readonly captures(none) [[TEST_C:%.*]]) {35; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 436; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])37; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 438; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]39; CHECK-NEXT: ret i32 [[SUM]]40;41 %res = call i32 @callee(ptr %p, ptr %test_c)42 43 %ltest_c = load i32, ptr %test_c44 45 %sum = add i32 %ltest_c, %res46 47 ret i32 %sum48}49 50; This is called by @caller_safe_args_1, but also from @caller_aliased_args, so51; we cannot promote %test_c.52;53define internal i32 @test_cannot_promote_3(ptr %p, ptr nocapture readonly %test_c) {54; CHECK-LABEL: define {{[^@]+}}@test_cannot_promote_355; CHECK-SAME: (ptr [[P:%.*]], ptr readonly captures(none) [[TEST_C:%.*]]) {56; CHECK-NEXT: [[TEST_C_VAL:%.*]] = load i32, ptr [[TEST_C]], align 457; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_VAL]])58; CHECK-NEXT: [[LTEST_C:%.*]] = load i32, ptr [[TEST_C]], align 459; CHECK-NEXT: [[SUM:%.*]] = add i32 [[LTEST_C]], [[RES]]60; CHECK-NEXT: ret i32 [[SUM]]61;62 %res = call i32 @callee(ptr %p, ptr %test_c)63 64 %ltest_c = load i32, ptr %test_c65 66 %sum = add i32 %ltest_c, %res67 68 ret i32 %sum69}70 71; This is called only by @caller_safe_args_1, from which we can prove that72; %test_c does not alias %p for any Call to the function, so we can promote it.73;74define internal i32 @test_can_promote_1(ptr %p, ptr nocapture readonly %test_c) {75; CHECK-LABEL: define {{[^@]+}}@test_can_promote_176; CHECK-SAME: (ptr [[P:%.*]], i32 [[TEST_C_0_VAL:%.*]]) {77; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_0_VAL]])78; CHECK-NEXT: [[SUM:%.*]] = add i32 [[TEST_C_0_VAL]], [[RES]]79; CHECK-NEXT: ret i32 [[SUM]]80;81 %res = call i32 @callee(ptr %p, ptr %test_c)82 83 %ltest_c = load i32, ptr %test_c84 85 %sum = add i32 %ltest_c, %res86 87 ret i32 %sum88}89 90; This is called by multiple callers, from which we can prove that %test_c does91; not alias %p for any Call to the function, so we can promote it.92;93define internal i32 @test_can_promote_2(ptr %p, ptr nocapture readonly %test_c) {94; CHECK-LABEL: define {{[^@]+}}@test_can_promote_295; CHECK-SAME: (ptr [[P:%.*]], i32 [[TEST_C_0_VAL:%.*]]) {96; CHECK-NEXT: [[RES:%.*]] = call i32 @callee(ptr [[P]], i32 [[TEST_C_0_VAL]])97; CHECK-NEXT: [[SUM:%.*]] = add i32 [[TEST_C_0_VAL]], [[RES]]98; CHECK-NEXT: ret i32 [[SUM]]99;100 %res = call i32 @callee(ptr %p, ptr %test_c)101 102 %ltest_c = load i32, ptr %test_c103 104 %sum = add i32 %ltest_c, %res105 106 ret i32 %sum107}108 109; Called by @test_XXX110define internal i32 @callee(ptr %p, ptr nocapture readonly %callee_c) {111; CHECK-LABEL: define {{[^@]+}}@callee112; CHECK-SAME: (ptr [[P:%.*]], i32 [[CALLEE_C_0_VAL:%.*]]) {113; CHECK-NEXT: [[A:%.*]] = load i32, ptr [[P]], align 4114; CHECK-NEXT: [[SUM:%.*]] = add i32 [[A]], [[CALLEE_C_0_VAL]]115; CHECK-NEXT: store i32 [[SUM]], ptr [[P]], align 4116; CHECK-NEXT: ret i32 [[SUM]]117;118 %a = load i32, ptr %p119 120 %lcallee_c = load i32, ptr %callee_c121 122 %sum = add i32 %a, %lcallee_c123 124 store i32 %sum, ptr %p125 126 ret i32 %sum127}128 129; Calls @test_cannot_promote_1130define i32 @caller_ptr_args(i64 %n, ptr %p1, ptr %p2) {131; CHECK-LABEL: define {{[^@]+}}@caller_ptr_args132; CHECK-SAME: (i64 [[N:%.*]], ptr [[P1:%.*]], ptr [[P2:%.*]]) {133; CHECK-NEXT: call void @memset(ptr [[P1]], i64 0, i64 [[N]])134; CHECK-NEXT: store i32 5, ptr [[P2]], align 4135; CHECK-NEXT: [[RES:%.*]] = call i32 @test_cannot_promote_1(ptr [[P1]], ptr [[P2]])136; CHECK-NEXT: ret i32 [[RES]]137;138 call void @memset(ptr %p1, i64 0, i64 %n)139 140 store i32 5, ptr %p2141 142 %res = call i32 @test_cannot_promote_1(ptr %p1, ptr %p2)143 144 ret i32 %res145}146 147; Calls @test_cannot_promote_2148; Calls @test_cannot_promote_3149define i32 @caller_aliased_args() {150; CHECK-LABEL: define {{[^@]+}}@caller_aliased_args() {151; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4152; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4153; CHECK-NEXT: [[RES1:%.*]] = call i32 @test_cannot_promote_2(ptr [[CALLER_C]], ptr [[CALLER_C]])154; CHECK-NEXT: [[RES2:%.*]] = call i32 @test_cannot_promote_3(ptr [[CALLER_C]], ptr [[CALLER_C]])155; CHECK-NEXT: [[RES:%.*]] = add i32 [[RES1]], [[RES2]]156; CHECK-NEXT: ret i32 [[RES]]157;158 %caller_c = alloca i32159 store i32 5, ptr %caller_c160 161 %res1 = call i32 @test_cannot_promote_2(ptr %caller_c, ptr %caller_c)162 %res2 = call i32 @test_cannot_promote_3(ptr %caller_c, ptr %caller_c)163 164 %res = add i32 %res1, %res2165 166 ret i32 %res167}168 169; Calls @test_cannot_promote_3170; Calls @test_can_promote_1171; Calls @test_can_promote_2172define i32 @caller_safe_args_1(i64 %n) {173; CHECK-LABEL: define {{[^@]+}}@caller_safe_args_1174; CHECK-SAME: (i64 [[N:%.*]]) {175; CHECK-NEXT: [[P:%.*]] = alloca [5 x double], i64 [[N]], align 8176; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])177; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4178; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4179; CHECK-NEXT: [[RES1:%.*]] = call i32 @test_cannot_promote_3(ptr [[P]], ptr [[CALLER_C]])180; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4181; CHECK-NEXT: [[RES2:%.*]] = call i32 @test_can_promote_1(ptr [[P]], i32 [[CALLER_C_VAL]])182; CHECK-NEXT: [[CALLER_C_VAL1:%.*]] = load i32, ptr [[CALLER_C]], align 4183; CHECK-NEXT: [[RES3:%.*]] = call i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL1]])184; CHECK-NEXT: [[RES12:%.*]] = add i32 [[RES1]], [[RES2]]185; CHECK-NEXT: [[RES:%.*]] = add i32 [[RES12]], [[RES3]]186; CHECK-NEXT: ret i32 [[RES]]187;188 %p = alloca [5 x double], i64 %n189 call void @memset(ptr %p, i64 0, i64 %n)190 191 %caller_c = alloca i32192 store i32 5, ptr %caller_c193 194 %res1 = call i32 @test_cannot_promote_3(ptr %p, ptr %caller_c)195 %res2 = call i32 @test_can_promote_1(ptr %p, ptr %caller_c)196 %res3 = call i32 @test_can_promote_2(ptr %p, ptr %caller_c)197 198 %res12 = add i32 %res1, %res2199 %res = add i32 %res12, %res3200 201 ret i32 %res202}203 204; Calls @test_can_promote_2205define i32 @caller_safe_args_2(i64 %n, ptr %p) {206; CHECK-LABEL: define {{[^@]+}}@caller_safe_args_2207; CHECK-SAME: (i64 [[N:%.*]], ptr [[P:%.*]]) {208; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])209; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4210; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4211; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4212; CHECK-NEXT: [[RES:%.*]] = call i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL]])213; CHECK-NEXT: ret i32 [[RES]]214;215 call void @memset(ptr %p, i64 0, i64 %n)216 217 %caller_c = alloca i32218 store i32 5, ptr %caller_c219 220 %res = call i32 @test_can_promote_2(ptr %p, ptr %caller_c)221 222 ret i32 %res223}224 225; Invokes @test_cannot_promote_2226define i32 @caller_invoke_aliased_args() personality ptr @__gxx_personality_v0 {227; CHECK-LABEL: define {{[^@]+}}@caller_invoke_aliased_args() personality ptr @__gxx_personality_v0 {228; CHECK-NEXT: entry:229; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4230; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4231; CHECK-NEXT: [[RES:%.*]] = invoke i32 @test_cannot_promote_2(ptr [[CALLER_C]], ptr [[CALLER_C]])232; CHECK-NEXT: to label [[OUT:%.*]] unwind label [[CPAD:%.*]]233; CHECK: out:234; CHECK-NEXT: ret i32 [[RES]]235; CHECK: cpad:236; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }237; CHECK-NEXT: catch ptr @_ZTIi238; CHECK-NEXT: ret i32 -1239;240entry:241 %caller_c = alloca i32242 store i32 5, ptr %caller_c243 244 %res = invoke i32 @test_cannot_promote_2(ptr %caller_c, ptr %caller_c)245 to label %out unwind label %cpad246 247out:248 ret i32 %res249 250cpad:251 %exn = landingpad { ptr, i32 }252 catch ptr @_ZTIi253 ret i32 -1254}255 256; Invokes @test_can_promote_2257define i32 @caller_invoke_safe_args(i64 %n) personality ptr @__gxx_personality_v0 {258; CHECK-LABEL: define {{[^@]+}}@caller_invoke_safe_args259; CHECK-SAME: (i64 [[N:%.*]]) personality ptr @__gxx_personality_v0 {260; CHECK-NEXT: entry:261; CHECK-NEXT: [[P:%.*]] = alloca [5 x double], i64 [[N]], align 8262; CHECK-NEXT: call void @memset(ptr [[P]], i64 0, i64 [[N]])263; CHECK-NEXT: [[CALLER_C:%.*]] = alloca i32, align 4264; CHECK-NEXT: store i32 5, ptr [[CALLER_C]], align 4265; CHECK-NEXT: [[CALLER_C_VAL:%.*]] = load i32, ptr [[CALLER_C]], align 4266; CHECK-NEXT: [[RES:%.*]] = invoke i32 @test_can_promote_2(ptr [[P]], i32 [[CALLER_C_VAL]])267; CHECK-NEXT: to label [[OUT:%.*]] unwind label [[CPAD:%.*]]268; CHECK: out:269; CHECK-NEXT: ret i32 [[RES]]270; CHECK: cpad:271; CHECK-NEXT: [[EXN:%.*]] = landingpad { ptr, i32 }272; CHECK-NEXT: catch ptr @_ZTIi273; CHECK-NEXT: ret i32 -1274;275entry:276 %p = alloca [5 x double], i64 %n277 call void @memset(ptr %p, i64 0, i64 %n)278 279 %caller_c = alloca i32280 store i32 5, ptr %caller_c281 282 %res = invoke i32 @test_can_promote_2(ptr %p, ptr %caller_c)283 to label %out unwind label %cpad284 285out:286 ret i32 %res287 288cpad:289 %exn = landingpad { ptr, i32 }290 catch ptr @_ZTIi291 ret i32 -1292}293 294declare void @memset(ptr, i64, i64)295declare i32 @__gxx_personality_v0(...)296 297@_ZTIi = external constant ptr298