Changing basic blocks

Hello guys,

I plan to do some pre register allocation optimizations.
I already know my way around LLVM but I could use some more experience.

I need a way to reorder instructions inside MachineBasicBlocks or MachineFunctions.
I’ve been searching for it but I have not found an example in the code yet.

Can someone point me where I should look for?

ty,

I need a way to reorder instructions inside MachineBasicBlocks or
MachineFunctions.
I've been searching for it but I have not found an example in the code yet.

For MachineBasicBlocks, check out this doc:
http://llvm.org/doxygen/classllvm_1_1MachineBasicBlock.html

I believe its basically the same as basic blocks in that you can manipulate the instruction list.

Otherwise, you need to write a MachineFunction Pass:
http://llvm.org/docs/WritingAnLLVMPass.html#MachineFunctionPass

-Tanya

Hi Tanya and everybody,

Ty for your support.

I too believe it should not be complicated.
But I was not being able to do it.

For instance, I tried to run this code below:

BB->push_back(&(BB->front()));
BB->pop_front();

But it did not work (kinda obvious why).

Nor this:

BB->push_back(BB->begin());
BB->pop_front();

But also did not work. It seams the same instruction may not be duplicated in the base block.

Finally, after some thinking (and tinkering), this worked like a charm:

MachineInstr* mi = BB->remove(BB->begin());
BB->push_back(mi);

=D

But, is there a better way to do it?
And for inserting a NOP ? Is there a simple way?

Ty again.

2007/8/8, Tanya M. Lattner <tonic@nondot.org>:

My suggestion for NOP's:

     create a method "addNop()" in the MRegisterInfo class, and make it concrete on the targets that you are planning to use. For instance, in the x86, the opcode for NOP is 0x90 (le'me just use this opportunity to thank Anton for telling me the opcode :slight_smile:
     use the method "copyRegToReg" as an example of how to add further methods into MRegisterInfo.
     The bad thing is that your version of LLVM will be off with the distributed version. Well, because more people have had this necessity of adding Nop's into the code, maybe the LLVM guys could consider adding this method officially to the MRegisterInfo class... and also a "swapRegs()" method into that class. It is very useful in register allocation.

best,

Fernando

But also did not work. It seams the same instruction may not be duplicated
in the base block.

That is correct. You must remove it before moving it.

But, is there a better way to do it?

I don't believe so. 2 lines of code are not that much though.

-Tanya

This is a good way to do a single instruction. You can also use the splice method, which allows you to move around ranges of instructions in constant time. It works the same was as std::list::splice.

-Chris

For adding the nop:

TII->insertNoop(*BB, BB->end());

2007/8/9, Chris Lattner <sabre@nondot.org>:

Hi all,

A week agoo I sent an email about reordering instruction in basicblocks.
And … I was able to do it. I guess so, at least.

Now when compiling my test program, llc dumps:

This doesn't do what you think. This line:

std::vector<MachineInstr*> positionmap(total);

Creates a vector with "total" null elements in it, it doesn't reserve space. This means that this loop:
   for(int i = 0; i< total; ++i)
     BB->push_back(positionmap[i]);

Inserts all these nulls into the BB. I'm surprised this doesn't explode.

-Chris

Yup,
You are right. That exploded.
I missed some lines in between.
there was a .reserve(total) in the actual code.
But, there is some side effect I still could not find.

2007/8/16, Chris Lattner <sabre@nondot.org>:

Just for the update, I found my way around like this:
rts is my optimizer.

for (int i = 0; i < rts.total_emmited(); ++i)
{
MachineInstr * mi = rts.emmited_instruction(i);
MachineInstr* last = BB->remove(mi);
BB->push_back(last);
}

2007/8/16, Emílio Wuerges <wuerges@gmail.com>: