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
FYI, in case you're wondering about the validity of this approach, generating alloca instructions and relying on -mem2reg is what the C/C++ front ends do. It is a little more straight forward to code than manually worrying about the PHI nodes. Either approach will work, take your pick :) Reid. On Sat, 2006-04-15 at 09:13 -0500, Misha Brukman wrote:> 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. >-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060415/d094d6e2/attachment.sig>
I also suffer from the same C++ allergy notably shoot in foot with dynamic grammers and overloading.. creates gasey code feeling..especially in the backend.. :)cheers! Simon Burton <simon at arrowtheory.com> wrote: On Sat, 15 Apr 2006 07:47:00 +0200 Oscar Fuentes 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 _______________________________________________ LLVM Developers mailing list LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev Send instant messages to your online friends http://uk.messenger.yahoo.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20060418/fbd87b68/attachment.html>