How to extract loop body into a new function?

Hi All,

I am having trouble extracting loop body into a new function. The ExtractLoop() or ExtractBasicBlock() extracts the entire loop along with the header into a new function. All I want is to extract the body of the loop into a new function(without the header).
Is this possible?


It worked with ExtractBasicBlock()

False Alarm!!

Still don't know how to do it!

I am trying to write a transformation pass to extract a loop body into a function.
For example:
The Loop in question is:
  for (i2 = 0; i2 < LOOP_SIZE; i2++) {
      A[B[i2]] = 2 * B[i2];

The IR for which is:
bb13: ; preds = %bb13, %bb
        %i2.0.reg2mem.0 = phi i32 [ 0, %bb ], [ %indvar.next62, %bb13 ] ; <i32> [#uses=2]
        %tmp15 = getelementptr [25 x i32]* %B, i32 0, i32 %i2.0.reg2mem.0 ; <i32*> [#uses=1]
        %tmp16 = load i32* %tmp15, align 4 ; <i32> [#uses=2]
        %tmp20 = shl i32 %tmp16, 1 ; <i32> [#uses=1]
        %tmp21 = getelementptr [25 x i32]* %A, i32 0, i32 %tmp16 ; <i32*> [#uses=1]
        store i32 %tmp20, i32* %tmp21, align 4
        %indvar.next62 = add i32 %i2.0.reg2mem.0, 1 ; <i32> [#uses=2]
        %exitcond63 = icmp eq i32 %indvar.next62, 25 ; <i1> [#uses=1]
        br i1 %exitcond63, label %bb30, label %bb13

I want to be able to extract:
      A[B[i2]] = 2 * B[i2];
into a function

myfunc() {
      A[B[i2]] = 2 * B[i2];

How can I do this?

Any hints??


Try ExtractCodeRegion or walk loop body and extract each basic block using ExtractBasicBlock.