Simon Burton <simon at arrowtheory.com> writes:> Hi, > > I've read over the "LLVM Language Reference Manual" > a few times, and writing some ll code, but i'm stuck at > a very basic point. How to decrement a counter variable ? > > int %count(int %n) { > EntryBlock: > %cond = seteq int %n, 0 > br bool %cond, label %Exit, label %Next > Next: > ; how to decrement n ? > %new_n = sub int %n, 1 > br label %EntryBlock > Exit: > ret int 0 > } > > I guess I could malloc a variable and use store/load. Is that the right way ? > > Also the above code is invalid: > Entry block to function must not have predecessors! > label %EntryBlock > Broken module found, compilation aborted!You need a phi node. A good way to learn how to write LLVM assembler is to use the online demo available at http://llvm.org/demo/ You feed a C/C++/Stacker program and you get the equivalent LLVM code. In this case, something like this C code is necessary: int foo(int n, int x) { while(n > x) --n; return n; } The extra stuff is for circumventing the optimizer. If you write int foo(int n) { while(n) --n; return 0; } the optimizer will reduce it to a ret int 0, which teach us nothing. An option for disabing optimizations would be very helpful for learners. BTW, Simon, is there a reason for writing LLVM assembler and not generating LLVM code directly? The later is simpler and relieves you from some nasty burdens. It doesn't require you to generate SSA conformant code, for instance. Relating to your present problem, it is "natural" to code a loop without resorting to phi nodes. -- Oscar
Oscar Fuentes <oscarfv at telefonica.net> writes: [snip]> BTW, Simon, is there a reason for writing LLVM assembler and not > generating LLVM code directly? The later is simpler and relieves you > from some nasty burdens. It doesn't require you to generate SSA > conformant code, for instance. Relating to your present problem, it is > "natural" to code a loop without resorting to phi nodes.Clarification: it is incorrect to say that generating LLVM code by instantiating LLVM instructions doesn't require to follow SSA rules. The truth is that the API is designed in such a way that you follow SSA rules without knowing about them, except for things like entries/exits of basic blocks (at least, I have not found any other issues with SSA so far). -- Oscar
Hi Simon,> I've read over the "LLVM Language Reference Manual" a few times, and > writing some ll code, but i'm stuck at a very basic point. How to > decrement a counter variable ?As Oscar pointed out, you need a phi-node. I read some of the published papers on LLVM before the Language Reference Manual and found them to be of use. You may also find Wikipedia's explanation of SSA useful, especially the key paragraph http://en.wikipedia.org/wiki/Static_single_assignment_form "Note: the phi functions are not actually implemented; instead, they're just markers for the compiler to place the value of all the variables grouped together by the phi function, in the same location in memory (or same register)." Cheers, Ralph.
Hi, I've read over the "LLVM Language Reference Manual" a few times, and writing some ll code, but i'm stuck at a very basic point. How to decrement a counter variable ? int %count(int %n) { EntryBlock: %cond = seteq int %n, 0 br bool %cond, label %Exit, label %Next Next: ; how to decrement n ? %new_n = sub int %n, 1 br label %EntryBlock Exit: ret int 0 } I guess I could malloc a variable and use store/load. Is that the right way ? Also the above code is invalid: Entry block to function must not have predecessors! label %EntryBlock Broken module found, compilation aborted! thanks for any hints, Simon. -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 02 6249 6940 http://arrowtheory.com
On Sat, Apr 15, 2006 at 07:47:00AM +0200, Oscar Fuentes wrote:> Simon Burton <simon at arrowtheory.com> writes: > > I've read over the "LLVM Language Reference Manual" > > a few times, and writing some ll code, but i'm stuck at > > a very basic point. How to decrement a counter variable ?[snip]> > I guess I could malloc a variable and use store/load. Is that the right way ?[snip]> You need a phi node.As others have pointed out, because LLVM is in SSA form, you will need to use a phi node. To see how it would be written, try using the demo script as Oscar suggested, or you can do what you suggested (I would use alloca instead of malloc, though I think either should work) and then run it through "opt -mem2reg" to convert it to the phi-node form. -- Misha Brukman :: http://misha.brukman.net
Using an alloca'd variable with loads and stores would work well too. To date this is how every llvm language front end works. You would want to avoid malloc for such a purpose generally. mem2reg is nice enough to take your local load and stores and promote them to registers with PHINodes :-) On Apr 15, 2006, at 4:43 AM, Ralph Corderoy wrote:> > Hi Simon, > >> I've read over the "LLVM Language Reference Manual" a few times, and >> writing some ll code, but i'm stuck at a very basic point. How to >> decrement a counter variable ? > > As Oscar pointed out, you need a phi-node. I read some of the > published > papers on LLVM before the Language Reference Manual and found them > to be > of use. You may also find Wikipedia's explanation of SSA useful, > especially the key paragraph > > http://en.wikipedia.org/wiki/Static_single_assignment_form > > "Note: the phi functions are not actually implemented; instead, > they're just markers for the compiler to place the value of all > the > variables grouped together by the phi function, in the same > location > in memory (or same register)." > > Cheers, > > > Ralph. > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
On Sat, 15 Apr 2006 07:47:00 +0200 Oscar Fuentes <oscarfv at telefonica.net> wrote:> > BTW, Simon, is there a reason for writing LLVM assembler and not > generating LLVM code directly?You mean write C++ code that calls the LLVM library ? I have a mild C++ allergy that I don't wish to aggravate.> The later is simpler and relieves you > from some nasty burdens.Yes, i'm finding it quite hairy! :) Simon. -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 02 6249 6940 http://arrowtheory.com