[LLVMdev] Question about writing a pass

Chris Lattner sabre at nondot.org
Sat Dec 4 11:35:17 PST 2004

On Sat, 4 Dec 2004, Zhang Qiuyu wrote:

> I got a few for writing a pass.
> 1) Is it possible to use input parameters in command line ?
>     For example, we got our own pass, ie. HELLO
>     opt -load ../../Debug/lib/libHELLO.so -HELLO < hello.bc
>     From the above command line, could we use some input parameter and we can read those parameter in Pass routine?

That's no problem at all.  Look at the docs/CommandLine.html document for
information on how to do this, and/or grep for cl::opt in other passes.

> 2) For splitting BB, the way what I did is to pick up instruction
> randomly and do splitting before this instruction. But sometime I got
> error when I run the Pass. It seems some of instruction set cannot be
> splitted, right? the instruction like 'phi..'. If so, how could we check
> this instruction to see if it can be splitted or not.

PHI nodes are special and can only be at certain places in the CFG (for
example, the number of incoming values to a PHI must match the number of
predecessors in the CFG.  All PHI nodes are guaranteed to start at the
head of a block, so just split after them.

> 3) Here is a piece of code
>   char a[]="hello world";
>   int i;
>   for(i=0;i<10;i++){
>     a[i]=a[i]+1;
>   }
> And I got IR code like the following
> no_exit:  ; preds = %strlen.entry, %no_exit
>  %indvar = phi uint [ %indvar.next, %no_exit ], [ 0, %strlen.entry ]  ; <uint> [#uses=3]
>  %i.0.0 = cast uint %indvar to int  ; <int> [#uses=1]
>  %tmp.24 = getelementptr [12 x sbyte]* %a, int 0, uint %indvar  ; <sbyte*> [#uses=2]
>  %tmp.28 = load sbyte* %tmp.24  ; <sbyte> [#uses=1]
>  %tmp.29 = add sbyte %tmp.28, 1  ; <sbyte> [#uses=1]
>  store sbyte %tmp.29, sbyte* %tmp.24
>  %inc = add int %i.0.0, 1  ; <int> [#uses=1]
>  %tmp.20 = setlt int %inc, %tmp.17  ; <bool> [#uses=1]
>  %indvar.next = add uint %indvar, 1  ; <uint> [#uses=1]
>  br bool %tmp.20, label %no_exit, label %loopexit
> So does it mean that I should insert all the IR code if I would like to
> insert a for loop in existed code. I can do it by inserting a call
> funcation. Here, I need insert for loop into the code body.

I'm not sure if I understand exactly what you mean, but if you wanted to
insert that loop into the code, you would have to construct that IR.  If
you can abstract it out into a runtime library of some sort, you could
just insert a call to the runtime.



More information about the llvm-dev mailing list