brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · fc9823c Raw
227 lines · plain
1; This test contains extremely tricky call graph structures for the inliner to2; handle correctly. They form cycles where the inliner introduces code that is3; immediately or can eventually be transformed back into the original code. And4; each step changes the call graph and so will trigger iteration. This requires5; some out-of-band way to prevent infinitely re-inlining and re-transforming the6; code.7;8; RUN: opt < %s -passes='cgscc(inline,function(sroa,instcombine))' -inline-threshold=50 -S | FileCheck %s9 10 11; The `test1_*` collection of functions form a directly cycling pattern.12 13define void @test1_a(ptr %ptr) {14; CHECK-LABEL: define void @test1_a(15entry:16  call void @test1_b(ptr @test1_b, i1 false, i32 0)17; Inlining and simplifying this call will reliably produce the exact same call,18; over and over again. However, each inlining increments the count, and so we19; expect this test case to stop after one round of inlining with a final20; argument of '1'.21; CHECK-NOT:     call22; CHECK:         call void @test1_b(ptr nonnull @test1_b, i1 false, i32 1)23; CHECK-NOT:     call24 25  ret void26}27 28define void @test1_b(ptr %arg, i1 %flag, i32 %inline_count) {29; CHECK-LABEL: define void @test1_b(30entry:31  %a = alloca ptr32  store ptr %arg, ptr %a33; This alloca and store should remain through any optimization.34; CHECK:         %[[A:.*]] = alloca35; CHECK:         store ptr %arg, ptr %[[A]]36 37  br i1 %flag, label %bb1, label %bb238 39bb1:40  call void @test1_a(ptr %a) noinline41  br label %bb242 43bb2:44  %p = load ptr, ptr %a45  %inline_count_inc = add i32 %inline_count, 146  call void %p(ptr %arg, i1 %flag, i32 %inline_count_inc)47; And we should continue to load and call indirectly through optimization.48; CHECK:         %[[P:.*]] = load ptr, ptr %[[A]]49; CHECK:         call void %[[P]](50 51  ret void52}53 54define void @test2_a(ptr %ptr) {55; CHECK-LABEL: define void @test2_a(56entry:57  call void @test2_b(ptr @test2_b, ptr @test2_c, i1 false, i32 0)58; Inlining and simplifying this call will reliably produce the exact same call,59; but only after doing two rounds if inlining, first from @test2_b then60; @test2_c. We check the exact number of inlining rounds before we cut off to61; break the cycle by inspecting the last paramater that gets incremented with62; each inlined function body.63; CHECK-NOT:     call64; CHECK:         call void @test2_b(ptr nonnull @test2_b, ptr nonnull @test2_c, i1 false, i32 2)65; CHECK-NOT:     call66  ret void67}68 69define void @test2_b(ptr %arg1, ptr %arg2, i1 %flag, i32 %inline_count) {70; CHECK-LABEL: define void @test2_b(71entry:72  %a = alloca ptr73  store ptr %arg2, ptr %a74; This alloca and store should remain through any optimization.75; CHECK:         %[[A:.*]] = alloca76; CHECK:         store ptr %arg2, ptr %[[A]]77 78  br i1 %flag, label %bb1, label %bb279 80bb1:81  call void @test2_a(ptr %a) noinline82  br label %bb283 84bb2:85  %p = load ptr, ptr %a86  %inline_count_inc = add i32 %inline_count, 187  call void %p(ptr %arg1, ptr %arg2, i1 %flag, i32 %inline_count_inc)88; And we should continue to load and call indirectly through optimization.89; CHECK:         %[[P:.*]] = load ptr, ptr %[[A]]90; CHECK:         call void %[[P]](91 92  ret void93}94 95define void @test2_c(ptr %arg1, ptr %arg2, i1 %flag, i32 %inline_count) {96; CHECK-LABEL: define void @test2_c(97entry:98  %a = alloca ptr99  store ptr %arg1, ptr %a100; This alloca and store should remain through any optimization.101; CHECK:         %[[A:.*]] = alloca102; CHECK:         store ptr %arg1, ptr %[[A]]103 104  br i1 %flag, label %bb1, label %bb2105 106bb1:107  call void @test2_a(ptr %a) noinline108  br label %bb2109 110bb2:111  %p = load ptr, ptr %a112  %inline_count_inc = add i32 %inline_count, 1113  call void %p(ptr %arg1, ptr %arg2, i1 %flag, i32 %inline_count_inc)114; And we should continue to load and call indirectly through optimization.115; CHECK:         %[[P:.*]] = load ptr, ptr %[[A]]116; CHECK:         call void %[[P]](117 118  ret void119}120 121; Another infinite inlining case. The initial callgraph is like following:122;123;         test3_a <---> test3_b124;             |         ^125;             v         |126;         test3_c <---> test3_d127;128; For all the call edges in the call graph, only test3_c and test3_d can be129; inlined into test3_a, and no other call edge can be inlined.130;131; After test3_c is inlined into test3_a, the original call edge test3_a->test3_c132; will be removed, a new call edge will be added and the call graph becomes:133;134;            test3_a <---> test3_b135;                  \      ^136;                   v    /137;     test3_c <---> test3_d138; But test3_a, test3_b, test3_c and test3_d still belong to the same SCC.139;140; Then after test3_a->test3_d is inlined, when test3_a->test3_d is converted to141; a ref edge, the original SCC will be split into two: {test3_c, test3_d} and142; {test3_a, test3_b}, immediately after the newly added ref edge143; test3_a->test3_c will be converted to a call edge, and the two SCCs will be144; merged into the original one again. During this cycle, the original SCC will145; be added into UR.CWorklist again and this creates an infinite loop.146 147@a = global i64 0148@b = global i64 0149 150; Check test3_c is inlined into test3_a once and only once.151; CHECK-LABEL: @test3_a(152; CHECK: tail call void @test3_b()153; CHECK-NEXT: tail call void @test3_d(i32 5)154; CHECK-NEXT: %[[LD1:.*]] = load i64, ptr @a155; CHECK-NEXT: %[[ADD1:.*]] = add nsw i64 %[[LD1]], 1156; CHECK-NEXT: store i64 %[[ADD1]], ptr @a157; CHECK-NEXT: %[[LD2:.*]] = load i64, ptr @b158; CHECK-NEXT: %[[ADD2:.*]] = add nsw i64 %[[LD2]], 5159; CHECK-NEXT: store i64 %[[ADD2]], ptr @b160; CHECK-NEXT: ret void161 162; Function Attrs: noinline163define void @test3_a() #0 {164entry:165  tail call void @test3_b()166  tail call void @test3_c(i32 5)167  %t0 = load i64, ptr @b168  %add = add nsw i64 %t0, 5169  store i64 %add, ptr @b170  ret void171}172 173; Function Attrs: noinline174define void @test3_b() #0 {175entry:176  tail call void @test3_a()177  %t0 = load i64, ptr @a178  %add = add nsw i64 %t0, 2179  store i64 %add, ptr @a180  ret void181}182 183define void @test3_d(i32 %i) {184entry:185  %cmp = icmp eq i32 %i, 5186  br i1 %cmp, label %if.end, label %if.then187 188if.then:                                          ; preds = %entry189  %call = tail call i64 @random()190  %t0 = load i64, ptr @a191  %add = add nsw i64 %t0, %call192  store i64 %add, ptr @a193  br label %if.end194 195if.end:                                           ; preds = %entry, %if.then196  tail call void @test3_c(i32 %i)197  tail call void @test3_b()198  %t6 = load i64, ptr @a199  %add79 = add nsw i64 %t6, 3200  store i64 %add79, ptr @a201  ret void202}203 204define void @test3_c(i32 %i) {205entry:206  %cmp = icmp eq i32 %i, 5207  br i1 %cmp, label %if.end, label %if.then208 209if.then:                                          ; preds = %entry210  %call = tail call i64 @random()211  %t0 = load i64, ptr @a212  %add = add nsw i64 %t0, %call213  store i64 %add, ptr @a214  br label %if.end215 216if.end:                                           ; preds = %entry, %if.then217  tail call void @test3_d(i32 %i)218  %t6 = load i64, ptr @a219  %add85 = add nsw i64 %t6, 1220  store i64 %add85, ptr @a221  ret void222}223 224declare i64 @random()225 226attributes #0 = { noinline }227