[llvm-commits] Teach SCEV about {X,*,Y} recursions

Török Edwin edwintorok at gmail.com
Wed Apr 23 13:58:44 PDT 2008


Hi,

I have a patch that adds support for exponential recursions in loops.
This is useful in static analysis tools, and also for code optimization.

For example
unsigned n = 15;
unsigned p=1;
while(n-- > 0) {
                p *= a;
}

can be turned into this code (which uses only 6 multiplications)
entry:
        br label %bb4.i
bb.i:           ; preds = %bb4.i
        %tmp3.i = mul i32 %p.0.i, %a            ; <i32> [#uses=1]
        %indvar.next = add i32 %indvar.i, 1             ; <i32> [#uses=1]
        br label %bb4.i
bb4.i:          ; preds = %bb.i, %entry
        %indvar.i = phi i32 [ 0, %entry ], [ %indvar.next, %bb.i
]              ; <i32> [#uses=2]
        %p.0.i = phi i32 [ 1, %entry ], [ %tmp3.i, %bb.i ]             
; <i32> [#uses=1]
        %exitcond = icmp eq i32 %indvar.i, 15           ; <i1> [#uses=1]
        br i1 %exitcond, label %naive_pow.exit, label %bb.i
naive_pow.exit:         ; preds = %bb4.i
        %tmp = mul i32 %a, %a           ; <i32> [#uses=3]
        %tmp1 = mul i32 %a, %tmp                ; <i32> [#uses=1]
        %tmp2 = mul i32 %tmp, %tmp              ; <i32> [#uses=3]
        %tmp3 = mul i32 %tmp1, %tmp2            ; <i32> [#uses=1]
        %tmp4 = mul i32 %tmp2, %tmp2            ; <i32> [#uses=1]
        %tmp5 = mul i32 %tmp3, %tmp4            ; <i32> [#uses=1]
        br label %return
return:         ; preds = %naive_pow.exit
        ret i32 %tmp5
}

The no. of multiplications is not optimal (it would be 5 in this case)
but this algorithm works for any constant power.

The patch is almost complete (I lack only codegen for non-constant
powers), can you please review it, and send me feedback on it?

P.S.: it currently fails
test/Analysis/ScalarEvolution/mulrec_expand_bug.ll until I implement
codegen for non-constant powers, so I don't intend to commit it until it
is finished.

Best regards,
--Edwin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: scev_mulrec.patch
Type: text/x-diff
Size: 28334 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20080423/6ff10ea6/attachment.patch>


More information about the llvm-commits mailing list