Home | History | Annotate | Download | only in SimplifyCFG
      1 ; Test that simplifycfg can create switch instructions from constant pointers.
      2 ;
      3 ; RUN: opt < %s -simplifycfg -S | FileCheck %s
      4 
      5 target datalayout = "e-p:64:64:64-p1:16:16:16-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"
      6 target triple = "x86_64-apple-darwin10.0.0"
      7 
      8 @.str = private constant [5 x i8] c"null\00"      ; <[5 x i8]*> [#uses=2]
      9 @.str1 = private constant [4 x i8] c"one\00"      ; <[4 x i8]*> [#uses=2]
     10 @.str2 = private constant [4 x i8] c"two\00"      ; <[4 x i8]*> [#uses=2]
     11 @.str3 = private constant [5 x i8] c"four\00"     ; <[5 x i8]*> [#uses=2]
     12 
     13 @.str_as1 = private addrspace(1) constant [5 x i8] c"null\00"      ; <[5 x i8]*> [#uses=2]
     14 @.str1_as1 = private addrspace(1) constant [4 x i8] c"one\00"      ; <[4 x i8]*> [#uses=2]
     15 @.str2_as1 = private addrspace(1) constant [4 x i8] c"two\00"      ; <[4 x i8]*> [#uses=2]
     16 @.str3_as1 = private addrspace(1) constant [5 x i8] c"four\00"     ; <[5 x i8]*> [#uses=2]
     17 
     18 declare i32 @puts(i8*)
     19 declare i32 @puts_as1(i8 addrspace(1)*)
     20 
     21 define void @f(i8* %x) nounwind ssp {
     22 ; CHECK-LABEL: @f(
     23 ; CHECK: switch i64 %magicptr
     24 ; CHECK: i64 0, label
     25 ; CHECK: i64 1, label
     26 ; CHECK: i64 2, label
     27 ; CHECK: i64 3, label
     28 ; CHECK: i64 4, label
     29 ; CHECK: }
     30 
     31 entry:
     32   %tobool = icmp eq i8* %x, null                  ; <i1> [#uses=1]
     33   br i1 %tobool, label %if.then, label %if.else
     34 
     35 if.then:                                          ; preds = %entry
     36   %call = call i32 @puts(i8* getelementptr inbounds ([5 x i8]* @.str, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
     37   br label %if.end21
     38 
     39 if.else:                                          ; preds = %entry
     40   %cmp = icmp eq i8* %x, inttoptr (i64 1 to i8*)  ; <i1> [#uses=1]
     41   br i1 %cmp, label %if.then2, label %if.else4
     42 
     43 if.then2:                                         ; preds = %if.else
     44   %call3 = call i32 @puts(i8* getelementptr inbounds ([4 x i8]* @.str1, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
     45   br label %if.end20
     46 
     47 if.else4:                                         ; preds = %if.else
     48   %cmp6 = icmp eq i8* %x, inttoptr (i64 2 to i8*) ; <i1> [#uses=1]
     49   br i1 %cmp6, label %if.then9, label %lor.lhs.false
     50 
     51 lor.lhs.false:                                    ; preds = %if.else4
     52   %cmp8 = icmp eq i8* %x, inttoptr (i64 3 to i8*) ; <i1> [#uses=1]
     53   br i1 %cmp8, label %if.then9, label %if.else11
     54 
     55 if.then9:                                         ; preds = %lor.lhs.false, %if.else4
     56   %call10 = call i32 @puts(i8* getelementptr inbounds ([4 x i8]* @.str2, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
     57   br label %if.end19
     58 
     59 if.else11:                                        ; preds = %lor.lhs.false
     60   %cmp13 = icmp eq i8* %x, inttoptr (i64 4 to i8*) ; <i1> [#uses=1]
     61   br i1 %cmp13, label %if.then14, label %if.else16
     62 
     63 if.then14:                                        ; preds = %if.else11
     64   %call15 = call i32 @puts(i8* getelementptr inbounds ([5 x i8]* @.str3, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
     65   br label %if.end
     66 
     67 if.else16:                                        ; preds = %if.else11
     68   %call18 = call i32 @puts(i8* %x) nounwind       ; <i32> [#uses=0]
     69   br label %if.end
     70 
     71 if.end:                                           ; preds = %if.else16, %if.then14
     72   br label %if.end19
     73 
     74 if.end19:                                         ; preds = %if.end, %if.then9
     75   br label %if.end20
     76 
     77 if.end20:                                         ; preds = %if.end19, %if.then2
     78   br label %if.end21
     79 
     80 if.end21:                                         ; preds = %if.end20, %if.then
     81   ret void
     82 }
     83 
     84 ; Is it useful to test a version where the ptrtoints are to the same
     85 ; size?
     86 define void @f_as1(i8 addrspace(1)* %x) nounwind ssp {
     87 ; CHECK-LABEL: @f_as1(
     88 ; CHECK: ptrtoint i8 addrspace(1)* %x to i16
     89 ; CHECK: switch i16 %magicptr
     90 ; CHECK: i16 0, label
     91 ; CHECK: i16 1, label
     92 ; CHECK: i16 2, label
     93 ; CHECK: i16 3, label
     94 ; CHECK: i16 4, label
     95 ; CHECK: }
     96 
     97 entry:
     98   %tobool = icmp eq i8 addrspace(1)* %x, null                  ; <i1> [#uses=1]
     99   br i1 %tobool, label %if.then, label %if.else
    100 
    101 if.then:                                          ; preds = %entry
    102   %call = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([5 x i8] addrspace(1)* @.str_as1, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
    103   br label %if.end21
    104 
    105 if.else:                                          ; preds = %entry
    106   %cmp = icmp eq i8 addrspace(1)* %x, inttoptr (i64 1 to i8 addrspace(1)*)  ; <i1> [#uses=1]
    107   br i1 %cmp, label %if.then2, label %if.else4
    108 
    109 if.then2:                                         ; preds = %if.else
    110   %call3 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([4 x i8] addrspace(1)* @.str1_as1, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
    111   br label %if.end20
    112 
    113 if.else4:                                         ; preds = %if.else
    114   %cmp6 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 2 to i8 addrspace(1)*) ; <i1> [#uses=1]
    115   br i1 %cmp6, label %if.then9, label %lor.lhs.false
    116 
    117 lor.lhs.false:                                    ; preds = %if.else4
    118   %cmp8 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 3 to i8 addrspace(1)*) ; <i1> [#uses=1]
    119   br i1 %cmp8, label %if.then9, label %if.else11
    120 
    121 if.then9:                                         ; preds = %lor.lhs.false, %if.else4
    122   %call10 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([4 x i8] addrspace(1)* @.str2_as1, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
    123   br label %if.end19
    124 
    125 if.else11:                                        ; preds = %lor.lhs.false
    126   %cmp13 = icmp eq i8 addrspace(1)* %x, inttoptr (i64 4 to i8 addrspace(1)*) ; <i1> [#uses=1]
    127   br i1 %cmp13, label %if.then14, label %if.else16
    128 
    129 if.then14:                                        ; preds = %if.else11
    130   %call15 = call i32 @puts_as1(i8 addrspace(1)* getelementptr inbounds ([5 x i8] addrspace(1)* @.str3_as1, i64 0, i64 0)) nounwind ; <i32> [#uses=0]
    131   br label %if.end
    132 
    133 if.else16:                                        ; preds = %if.else11
    134   %call18 = call i32 @puts_as1(i8 addrspace(1)* %x) nounwind       ; <i32> [#uses=0]
    135   br label %if.end
    136 
    137 if.end:                                           ; preds = %if.else16, %if.then14
    138   br label %if.end19
    139 
    140 if.end19:                                         ; preds = %if.end, %if.then9
    141   br label %if.end20
    142 
    143 if.end20:                                         ; preds = %if.end19, %if.then2
    144   br label %if.end21
    145 
    146 if.end21:                                         ; preds = %if.end20, %if.then
    147   ret void
    148 }
    149 
    150