"LLVM Essentials" book example on scalar evolution

Hi all,

I’m having trouble understanding the output of the the scalar evolution analysis on the following LLVM IR code:

$ cat scalevl.ll
define void @func() {
entry:
	br label %header
header:
	%i = phi i32 [ 1, %entry ], [ %i.next, %body ]
	%cond = icmp eq i32 %i, 10
	br i1 %cond, label %exit, label %body
body:
	%a = mul i32 %i, 5
	%b = or i32 %i, 1
	%i.next = add i32 %i, 1
	br label %header
exit:
	ret void
}

Running the pass:

opt -analyze -scalar-evolution scalevl.ll

returns the following:

Printing analysis 'Scalar Evolution Analysis' for function 'func':
Classifying expressions for: @func
  %i = phi i32 [ 1, %entry ], [ %i.next, %body ]
  -->  {1,+,1}<%header> U: [1,11) S: [1,11)		Exits: 10		LoopDispositions: { %header: Computable }
  %a = mul i32 %i, 5
  -->  {5,+,5}<%header> U: [5,51) S: [5,51)		Exits: 50		LoopDispositions: { %header: Computable }
  %b = or i32 %i, 1
  -->  %b U: [1,0) S: full-set		Exits: 11		LoopDispositions: { %header: Variant }
  %i.next = add i32 %i, 1
  -->  {2,+,1}<%header> U: [2,12) S: [2,12)		Exits: 11		LoopDispositions: { %header: Computable }
Determining loop execution counts for: @func
Loop %header: backedge-taken count is 9
Loop %header: max backedge-taken count is 9
Loop %header: Predicated backedge-taken count is 9
 Predicates:

Loop %header: Trip multiple is 10

If I understand the LLVM IR correctly, the br instruction in header should branch to the exit block when i == 10. However, in the scalar evolution analysis output, it says that %a will exit at value 50. This doesn’t seem right because i will not equal 10 inside the body block. Similarly, I don’t see how %i.next will exit at 11. Can anyone help explain this?