Home | History | Annotate | Download | only in Inline
      1 ; RUN: opt -inline < %s -S -o - -inline-threshold=10 | FileCheck %s
      2 
      3 target datalayout = "p:32:32-p1:64:64-p2:16:16-n16:32:64"
      4 
      5 define i32 @outer1() {
      6 ; CHECK-LABEL: @outer1(
      7 ; CHECK-NOT: call i32
      8 ; CHECK: ret i32
      9 
     10   %ptr = alloca i32
     11   %ptr1 = getelementptr inbounds i32, i32* %ptr, i32 0
     12   %ptr2 = getelementptr inbounds i32, i32* %ptr, i32 42
     13   %result = call i32 @inner1(i32* %ptr1, i32* %ptr2)
     14   ret i32 %result
     15 }
     16 
     17 define i32 @inner1(i32* %begin, i32* %end) {
     18   call void @extern()
     19   %begin.i = ptrtoint i32* %begin to i32
     20   %end.i = ptrtoint i32* %end to i32
     21   %distance = sub i32 %end.i, %begin.i
     22   %icmp = icmp sle i32 %distance, 42
     23   br i1 %icmp, label %then, label %else
     24 
     25 then:
     26   ret i32 3
     27 
     28 else:
     29   %t = load i32, i32* %begin
     30   ret i32 %t
     31 }
     32 
     33 define i32 @outer1_as1(i32 addrspace(1)* %ptr) {
     34 ; CHECK-LABEL: @outer1_as1(
     35 ; CHECK-NOT: call
     36 ; CHECK: ret i32
     37   %ptr1 = getelementptr inbounds i32, i32 addrspace(1)* %ptr, i32 0
     38   %ptr2 = getelementptr inbounds i32, i32 addrspace(1)* %ptr, i32 42
     39   %result = call i32 @inner1_as1(i32 addrspace(1)* %ptr1, i32 addrspace(1)* %ptr2)
     40   ret i32 %result
     41 }
     42 
     43 ; Make sure that the address space's larger size makes the ptrtoints
     44 ; not no-ops preventing inlining
     45 define i32 @inner1_as1(i32 addrspace(1)* %begin, i32 addrspace(1)* %end) {
     46   %begin.i = ptrtoint i32 addrspace(1)* %begin to i32
     47   %end.i = ptrtoint i32 addrspace(1)* %end to i32
     48   %distance = sub i32 %end.i, %begin.i
     49   %icmp = icmp sle i32 %distance, 42
     50   br i1 %icmp, label %then, label %else
     51 
     52 then:
     53   ret i32 3
     54 
     55 else:
     56   %t = load i32, i32 addrspace(1)* %begin
     57   ret i32 %t
     58 }
     59 
     60 define i32 @outer2(i32* %ptr) {
     61 ; Test that an inbounds GEP disables this -- it isn't safe in general as
     62 ; wrapping changes the behavior of lessthan and greaterthan comparisons.
     63 ; CHECK-LABEL: @outer2(
     64 ; CHECK: call i32 @inner2
     65 ; CHECK: ret i32
     66 
     67   %ptr1 = getelementptr i32, i32* %ptr, i32 0
     68   %ptr2 = getelementptr i32, i32* %ptr, i32 42
     69   %result = call i32 @inner2(i32* %ptr1, i32* %ptr2)
     70   ret i32 %result
     71 }
     72 
     73 define i32 @inner2(i32* %begin, i32* %end) {
     74   call void @extern()
     75   %begin.i = ptrtoint i32* %begin to i32
     76   %end.i = ptrtoint i32* %end to i32
     77   %distance = sub i32 %end.i, %begin.i
     78   %icmp = icmp sle i32 %distance, 42
     79   br i1 %icmp, label %then, label %else
     80 
     81 then:
     82   ret i32 3
     83 
     84 else:
     85   %t = load i32, i32* %begin
     86   ret i32 %t
     87 }
     88 
     89 define i32 @outer3(i16* addrspace(1)* %ptr) {
     90 ; CHECK-LABEL: @outer3(
     91 ; CHECK-NOT: call i32
     92 ; CHECK: ret i32 3
     93 ; CHECK-LABEL: @inner3(
     94   %result = call i32 @inner3(i16* addrspace(1)* %ptr)
     95   ret i32 %result
     96 }
     97 
     98 define i32 @inner3(i16* addrspace(1)* %ptr) {
     99   call void @extern()
    100   %ptr.i = ptrtoint i16* addrspace(1)* %ptr to i64
    101   %distance = sub i64 %ptr.i, %ptr.i
    102   %icmp = icmp eq i64 %distance, 0
    103   br i1 %icmp, label %then, label %else
    104 
    105 then:
    106   ret i32 3
    107 
    108 else:
    109   ret i32 5
    110 }
    111 
    112 
    113 ; The inttoptrs are free since it is a smaller integer to a larger
    114 ; pointer size
    115 define i32 @inttoptr_free_cost(i32 %a, i32 %b, i32 %c) {
    116   call void @extern()
    117   %p1 = inttoptr i32 %a to i32 addrspace(1)*
    118   %p2 = inttoptr i32 %b to i32 addrspace(1)*
    119   %p3 = inttoptr i32 %c to i32 addrspace(1)*
    120   %t1 = load i32, i32 addrspace(1)* %p1
    121   %t2 = load i32, i32 addrspace(1)* %p2
    122   %t3 = load i32, i32 addrspace(1)* %p3
    123   %s = add i32 %t1, %t2
    124   %s1 = add i32 %s, %t3
    125   ret i32 %s1
    126 }
    127 
    128 define i32 @inttoptr_free_cost_user(i32 %begin, i32 %end) {
    129 ; CHECK-LABEL: @inttoptr_free_cost_user(
    130 ; CHECK-NOT: call i32
    131   %x = call i32 @inttoptr_free_cost(i32 %begin, i32 %end, i32 9)
    132   ret i32 %x
    133 }
    134 
    135 ; The inttoptrs have a cost since it is a larger integer to a smaller
    136 ; pointer size
    137 define i32 @inttoptr_cost_smaller_ptr(i32 %a, i32 %b, i32 %c) {
    138   call void @extern()
    139   %p1 = inttoptr i32 %a to i32 addrspace(2)*
    140   %p2 = inttoptr i32 %b to i32 addrspace(2)*
    141   %p3 = inttoptr i32 %c to i32 addrspace(2)*
    142   %t1 = load i32, i32 addrspace(2)* %p1
    143   %t2 = load i32, i32 addrspace(2)* %p2
    144   %t3 = load i32, i32 addrspace(2)* %p3
    145   %s = add i32 %t1, %t2
    146   %s1 = add i32 %s, %t3
    147   ret i32 %s1
    148 }
    149 
    150 define i32 @inttoptr_cost_smaller_ptr_user(i32 %begin, i32 %end) {
    151 ; CHECK-LABEL: @inttoptr_cost_smaller_ptr_user(
    152 ; CHECK: call i32
    153   %x = call i32 @inttoptr_cost_smaller_ptr(i32 %begin, i32 %end, i32 9)
    154   ret i32 %x
    155 }
    156 
    157 declare void @extern()