Home | History | Annotate | Download | only in IndVarSimplify
      1 ; RUN: opt < %s -indvars -S | FileCheck %s
      2 
      3 ; These tests ensure that we can compute the trip count of various forms of
      4 ; loops.  If the trip count of the loop is computable, then we will know what
      5 ; the exit value of the loop will be for some value, allowing us to substitute
      6 ; it directly into users outside of the loop, making the loop dead.
      7 
      8 ; CHECK: @linear_setne
      9 ; CHECK: ret i32 100
     10 
     11 define i32 @linear_setne() {
     12 entry:
     13 	br label %loop
     14 
     15 loop:		; preds = %loop, %entry
     16 	%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]		; <i32> [#uses=3]
     17 	%i.next = add i32 %i, 1		; <i32> [#uses=1]
     18 	%c = icmp ne i32 %i, 100		; <i1> [#uses=1]
     19 	br i1 %c, label %loop, label %loopexit
     20 
     21 loopexit:		; preds = %loop
     22 	ret i32 %i
     23 }
     24 
     25 ; CHECK: @linear_setne_2
     26 ; CHECK: ret i32 100
     27 
     28 define i32 @linear_setne_2() {
     29 entry:
     30 	br label %loop
     31 
     32 loop:		; preds = %loop, %entry
     33 	%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]		; <i32> [#uses=3]
     34 	%i.next = add i32 %i, 2		; <i32> [#uses=1]
     35 	%c = icmp ne i32 %i, 100		; <i1> [#uses=1]
     36 	br i1 %c, label %loop, label %loopexit
     37 
     38 loopexit:		; preds = %loop
     39 	ret i32 %i
     40 }
     41 
     42 ; CHECK: @linear_setne_overflow
     43 ; CHECK: ret i32 0
     44 
     45 define i32 @linear_setne_overflow() {
     46 entry:
     47 	br label %loop
     48 
     49 loop:		; preds = %loop, %entry
     50 	%i = phi i32 [ 1024, %entry ], [ %i.next, %loop ]		; <i32> [#uses=3]
     51 	%i.next = add i32 %i, 1024		; <i32> [#uses=1]
     52 	%c = icmp ne i32 %i, 0		; <i1> [#uses=1]
     53 	br i1 %c, label %loop, label %loopexit
     54 
     55 loopexit:		; preds = %loop
     56 	ret i32 %i
     57 }
     58 
     59 ; CHECK: @linear_setlt
     60 ; CHECK: ret i32 100
     61 
     62 define i32 @linear_setlt() {
     63 entry:
     64 	br label %loop
     65 
     66 loop:		; preds = %loop, %entry
     67 	%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]		; <i32> [#uses=3]
     68 	%i.next = add i32 %i, 1		; <i32> [#uses=1]
     69 	%c = icmp slt i32 %i, 100		; <i1> [#uses=1]
     70 	br i1 %c, label %loop, label %loopexit
     71 
     72 loopexit:		; preds = %loop
     73 	ret i32 %i
     74 }
     75 
     76 ; CHECK: @quadratic_setlt
     77 ; CHECK: ret i32 34
     78 
     79 define i32 @quadratic_setlt() {
     80 entry:
     81 	br label %loop
     82 
     83 loop:		; preds = %loop, %entry
     84 	%i = phi i32 [ 7, %entry ], [ %i.next, %loop ]		; <i32> [#uses=4]
     85 	%i.next = add i32 %i, 3		; <i32> [#uses=1]
     86 	%i2 = mul i32 %i, %i		; <i32> [#uses=1]
     87 	%c = icmp slt i32 %i2, 1000		; <i1> [#uses=1]
     88 	br i1 %c, label %loop, label %loopexit
     89 
     90 loopexit:		; preds = %loop
     91 	ret i32 %i
     92 }
     93 
     94 ; CHECK: @chained
     95 ; CHECK: ret i32 200
     96 
     97 define i32 @chained() {
     98 entry:
     99 	br label %loop
    100 
    101 loop:		; preds = %loop, %entry
    102 	%i = phi i32 [ 0, %entry ], [ %i.next, %loop ]		; <i32> [#uses=3]
    103 	%i.next = add i32 %i, 1		; <i32> [#uses=1]
    104 	%c = icmp ne i32 %i, 100		; <i1> [#uses=1]
    105 	br i1 %c, label %loop, label %loopexit
    106 
    107 loopexit:		; preds = %loop
    108 	br label %loop2
    109 
    110 loop2:		; preds = %loop2, %loopexit
    111 	%j = phi i32 [ %i, %loopexit ], [ %j.next, %loop2 ]		; <i32> [#uses=3]
    112 	%j.next = add i32 %j, 1		; <i32> [#uses=1]
    113 	%c2 = icmp ne i32 %j, 200		; <i1> [#uses=1]
    114 	br i1 %c2, label %loop2, label %loopexit2
    115 
    116 loopexit2:		; preds = %loop2
    117 	ret i32 %j
    118 }
    119 
    120 ; CHECK: @chained4
    121 ; CHECK: ret i32 400
    122 
    123 define i32 @chained4() {
    124 entry:
    125   br label %loop
    126 
    127 loop:                                             ; preds = %loop, %entry
    128   %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]  ; <i32> [#uses=3]
    129   %i.next = add i32 %i, 1                         ; <i32> [#uses=1]
    130   %c = icmp ne i32 %i.next, 100                   ; <i1> [#uses=1]
    131   br i1 %c, label %loop, label %loopexit
    132 
    133 loopexit:                                         ; preds = %loop
    134   br label %loop2
    135 
    136 loop2:                                            ; preds = %loop2, %loopexit
    137   %j = phi i32 [ %i.next, %loopexit ], [ %j.next, %loop2 ] ; <i32> [#uses=3]
    138   %j.next = add i32 %j, 1                         ; <i32> [#uses=1]
    139   %c2 = icmp ne i32 %j.next, 200                  ; <i1> [#uses=1]
    140   br i1 %c2, label %loop2, label %loopexit2
    141 
    142 loopexit2:                                        ; preds = %loop
    143   br label %loop8
    144 
    145 loop8:                                            ; preds = %loop2, %loopexit
    146   %k = phi i32 [ %j.next, %loopexit2 ], [ %k.next, %loop8 ] ; <i32> [#uses=3]
    147   %k.next = add i32 %k, 1                         ; <i32> [#uses=1]
    148   %c8 = icmp ne i32 %k.next, 300                  ; <i1> [#uses=1]
    149   br i1 %c8, label %loop8, label %loopexit8
    150 
    151 loopexit8:                                        ; preds = %loop2
    152   br label %loop9
    153 
    154 loop9:                                            ; preds = %loop2, %loopexit
    155   %l = phi i32 [ %k.next, %loopexit8 ], [ %l.next, %loop9 ] ; <i32> [#uses=3]
    156   %l.next = add i32 %l, 1                         ; <i32> [#uses=1]
    157   %c9 = icmp ne i32 %l.next, 400                  ; <i1> [#uses=1]
    158   br i1 %c9, label %loop9, label %loopexit9
    159 
    160 loopexit9:                                        ; preds = %loop2
    161   ret i32 %l.next
    162 }
    163