15-411: Induction Variables Jan Ho ff mann
Example Check if an array is sorted bool is_sorted(int[] A, int n) //@requires 0 <= n && n <= \length(A); { for (int i = 0; i < n-1; i++) //@loop_invariant 0 <= i; if (A[i] > A[i+1]) return false; return true; }
is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 4 ← i 1 + 1 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue : return 1 rfalse : return 0 Translation to SSA Form (without array bound checks)
is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : Basic induction variable t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 4 ← i 1 + 1 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue : return 1 rfalse : return 0 Translation to SSA Form (without array bound checks)
is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : Basic induction variable t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 Derived induction t 3 ← M [ t 2 ] variable t 4 ← i 1 + 1 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue : return 1 rfalse : return 0 Translation to SSA Form (without array bound checks)
A first optimization is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 4 ← i 1 + 1 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue :
A first optimization is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 Common subexpression t 3 ← M [ t 2 ] elimination t 4 ← i 1 + 1 applies t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue :
A first optimization is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 Common subexpression t 3 ← M [ t 2 ] elimination t 4 ← i 1 + 1 applies t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue :
A first optimization is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← 4 ∗ i 1 t 2 ← A + t 1 Common subexpression t 3 ← M [ t 2 ] elimination t 4 ← i 1 + 1 applies t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] Preserve if ( t 3 > t 7 ) goto rfalse induction variable i i 2 ← i 1 + 1 goto loop ( i 2 ) rtrue :
is sorted ( A, n ) : i 0 ← 0 goto loop ( i 0 ) loop ( i 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue e t 1 ← 4 ∗ i 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 4 ← i 1 + 1 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse se i 2 ← t 4 goto loop ( i 2 )
is sorted ( A, n ) : is sorted ( A, n ) : i 0 ← 0 i 0 ← 0 goto loop ( i 0 ) goto loop ( i 0 ) loop ( i 1 ) : loop ( i 1 ) : t 0 ← n − 1 t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue e if ( i 1 ≥ t 0 ) goto rtrue e t 1 ← 4 ∗ i 1 t 1 ← 4 ∗ i 1 t 2 ← A + t 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 3 ← M [ t 2 ] i 2 ← i 1 + 1 t 4 ← i 1 + 1 t 5 ← 4 ∗ i 2 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 6 ← A + t 5 t 7 ← M [ t 6 ] t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse e if ( t 3 > t 7 ) goto rfalse se i 2 ← t 4 goto loop ( i 2 ) goto loop ( i 2 )
Consider induction is sorted ( A, n ) : variable t1 i 0 ← 0 variable t 1 ← 4 ∗ i 1 . goto loop ( i 0 ) iteration from at a loop ( i 1 ) : Idea: compute t1 from a t 0 ← n − 1 previous iteration of t1 if ( i 1 ≥ t 0 ) goto rtrue e t 1 ← 4 ∗ i 1 Introduce new variable t 2 ← A + t 1 j = 4*i t 3 ← M [ t 2 ] i 2 ← i 1 + 1 t 5 ← 4 ∗ i 2 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse e goto loop ( i 2 )
Consider induction is sorted ( A, n ) : variable t1 i 0 ← 0 variable t 1 ← 4 ∗ i 1 . j 0 ← 4 ∗ i 0 @ ensures j 0 = 4 ∗ i 0 goto loop ( i 0 , j 0 ) iteration from at a Idea: compute t1 from a loop ( i 1 , j 1 ) : @ requires j 1 = 4 ∗ i 1 previous iteration of t1 t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue @ assert j 1 = 4 ∗ i 1 t 1 ← j 1 Introduce new variable t 2 ← A + t 1 j = 4*i t 3 ← M [ t 2 ] i 2 ← i 1 + 1 j 2 ← 4 ∗ i 2 @ ensures j 2 = 4 ∗ i 2 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 )
is sorted ( A, n ) : is sorted ( A, n ) : i 0 ← 0 i 0 ← 0 j 0 ← 4 ∗ i 0 j 0 ← 0 @ ensures j 0 = 4 ∗ i 0 goto loop ( i 0 , j 0 ) goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : loop ( i 1 , j 1 ) : @ requires j 1 = 4 ∗ i 1 t 0 ← n − 1 t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 @ assert j 1 = 4 ∗ i 1 t 1 ← j 1 t 2 ← A + t 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 3 ← M [ t 2 ] i 2 ← i 1 + 1 i 2 ← i 1 + 1 j 2 ← 4 ∗ i 2 j 2 ← j 1 + 4 @ ensures j 2 = 4 ∗ i 2 t 4 ← i 2 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 6 ← A + t 5 t 7 ← M [ t 6 ] t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 ) goto loop ( i 2 , j 2 )
is sorted ( A, n ) : is sorted ( A, n ) : i 0 ← 0 i 0 ← 0 j 0 ← 4 ∗ i 0 j 0 ← 0 @ ensures j 0 = 4 ∗ i 0 goto loop ( i 0 , j 0 ) goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : loop ( i 1 , j 1 ) : @ requires j 1 = 4 ∗ i 1 t 0 ← n − 1 t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 @ assert j 1 = 4 ∗ i 1 t 1 ← j 1 t 2 ← A + t 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 3 ← M [ t 2 ] i 2 ← i 1 + 1 i 2 ← i 1 + 1 j 2 ← 4 ∗ i 2 j 2 ← j 1 + 4 @ ensures j 2 = 4 ∗ i 2 t 4 ← i 2 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 6 ← A + t 5 t 7 ← M [ t 6 ] t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 ) goto loop ( i 2 , j 2 ) j 0 = 4 ∗ i 0 = 0
is sorted ( A, n ) : is sorted ( A, n ) : i 0 ← 0 i 0 ← 0 j 0 ← 4 ∗ i 0 j 0 ← 0 @ ensures j 0 = 4 ∗ i 0 goto loop ( i 0 , j 0 ) goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : loop ( i 1 , j 1 ) : @ requires j 1 = 4 ∗ i 1 t 0 ← n − 1 t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 @ assert j 1 = 4 ∗ i 1 t 1 ← j 1 t 2 ← A + t 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 3 ← M [ t 2 ] i 2 ← i 1 + 1 i 2 ← i 1 + 1 j 2 ← 4 ∗ i 2 j 2 ← j 1 + 4 @ ensures j 2 = 4 ∗ i 2 t 4 ← i 2 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 6 ← A + t 5 t 7 ← M [ t 6 ] t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 ) goto loop ( i 2 , j 2 ) j 2 = 4 ∗ i 2 = 4 ∗ ( i 1 + 1) = 4 ∗ i 1 + 4 = j 1 + 4 j 0 = 4 ∗ i 0 = 0
is sorted ( A, n ) : i 0 ← 0 j 0 ← 0 @ goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] i 2 ← i 1 + 1 j 2 ← j 1 + 4 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 )
is sorted ( A, n ) : i 0 ← 0 j 0 ← 0 @ goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : Loop hoisting t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] i 2 ← i 1 + 1 j 2 ← j 1 + 4 t 4 ← i 2 t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 )
is sorted ( A, n ) : i 0 ← 0 j 0 ← 0 @ goto loop ( i 0 , j 0 ) loop ( i 1 , j 1 ) : Loop hoisting t 0 ← n − 1 if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] i 2 ← i 1 + 1 j 2 ← j 1 + 4 Copy t 4 ← i 2 propagation t 5 ← 4 ∗ t 4 t 6 ← A + t 5 t 7 ← M [ t 6 ] if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 )
is sorted ( A, n ) : is sorted ( A, n ) : i 0 ← 0 i 0 ← 0 j 0 ← 0 @ j 0 ← 0 @ ensures j 0 = 4 ∗ i 0 goto loop ( i 0 , j 0 ) t 0 ← n − 1 loop ( i 1 , j 1 ) : goto loop ( i 0 , j 0 ) t 0 ← n − 1 loop ( i 1 , j 1 ) : @ requires j 1 = 4 ∗ i 1 if ( i 1 ≥ t 0 ) goto rtrue if ( i 1 ≥ t 0 ) goto rtrue t 1 ← j 1 t 2 ← A + j 1 t 2 ← A + t 1 t 3 ← M [ t 2 ] t 3 ← M [ t 2 ] i 2 ← i 1 + 1 i 2 ← i 1 + 1 j 2 ← j 1 + 4 @ ensures j 2 = 4 ∗ i 2 j 2 ← j 1 + 4 t 5 ← 4 ∗ i 2 t 4 ← i 2 t 6 ← A + t 5 t 5 ← 4 ∗ t 4 t 7 ← M [ t 6 ] t 6 ← A + t 5 if ( t 3 > t 7 ) goto rfalse t 7 ← M [ t 6 ] goto loop ( i 2 , j 2 ) if ( t 3 > t 7 ) goto rfalse goto loop ( i 2 , j 2 )
Recommend
More recommend