<div dir="ltr"><div><div>Yes, to have a true variable, you need to do an `alloca` [ideally at the beginning of the function]. So unless you start out with two constants, at least one of your values going into an the createAdd or createSub function will be the result of a createLoad from a createAlloca - and of course, the value has to be stored first. Mem2reg pass will then translate alloca's to registers where possible.<br><br></div>In short, "when in doubt alloca".<br><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 16 March 2016 at 05:57, Jeremy Lakeman via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>"do an assign" <br></div>You aren't specifying what you mean here with regard to LLVM's terminology. LLVM doesn't really have "temporaries". There are Value's, and there are pointers to the stack or heap.<br><br></div>If you want to create a new "local variable", you have to allocate stack space by creating an alloca instruction in the entry block. Then you can Store into that pointer, and Load from it.<br><br></div><div>eg;<br><br></div><div>$ cat main.c<br></div><div>int main(){<br> int i=0;<br> i+=1;<br></div><div> return 0;<br></div><div>}<br></div><div><br>$ clang -O0 -c main.c -emit-llvm && llvm-dis main.bc && cat main.ll<br>...<br></div><div>define i32 @main() #0 {<br> %i = alloca i32, align 4<br> store i32 0, i32* %i, align 4<br> %1 = load i32* %i, align 4<br> %2 = add nsw i32 %1, 1<br> store i32 %2, i32* %i, align 4<br> ret i32 0<br>}<br><br></div>But values don't have to be written to the stack. All Value* objects are assumed to be stored in a register, preserved by the back-end until they are no longer needed.<br><div><br>$ cat fib.c<br>unsigned fibonacci(unsigned n){<br> if (n == 0)<br> return 0;<br> if (n == 1)<br> return 1;<br> return fibonacci(n - 1) + fibonacci(n - 2);<br>}<br><br>$ clang -O3 -c fib.c -emit-llvm && llvm-dis fib.bc && cat fib.ll<br>...<br>define i32 @fibonacci(i32 %n) #0 {<br> switch i32 %n, label %2 [<br> i32 0, label %8<br> i32 1, label %1<br> ]<br><br>; <label>:1 ; preds = %0<br> br label %8<br><br>; <label>:2 ; preds = %0<br> %3 = add i32 %n, -1<br> %4 = tail call i32 @fibonacci(i32 %3)<br> %5 = add i32 %n, -2<br> %6 = tail call i32 @fibonacci(i32 %5)<br> %7 = add i32 %6, %4<br> ret i32 %7<br><br>; <label>:8 ; preds = %1, %0<br> %.0 = phi i32 [ 1, %1 ], [ 0, %0 ]<br> ret i32 %.0<br>}<br><br></div><div>See, not an alloca anywhere. The "temporary" value %7 is calculated from the other value's %4 & %6, without any explicit store & load from the stack.<br></div><div><br>Each line like;<br> %7 = add i32 %6, %4<br></div><div>above, represents a single (Instruction*). <br>"%7" is it's name (in this case auto-numbered because it has no explicit name). <br>"add i32 ..." is a textual representation of the specific Instruction. <br>There is no separate llvm object to represent the " = " string above.<br><br></div><div>As I said before, Instruction inherits from Value. It represents both the operation to be performed, and the "temporary" result to be passed as arguments to other Instructions.<br></div><div><br></div><div>You may also find this useful to help you understand how to use the C++ API;<br></div><div>$ llc -march=cpp fib.bc<br></div><div>$ cat fib.cpp<br><br>...<br><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><div><div><div><br></div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 16, 2016 at 3:40 PM, Paul Hancock via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div style="font-size:12pt;color:#000000;background-color:#ffffff;font-family:Calibri,Arial,Helvetica,sans-serif">
<p>I partially worked out that to do an assign I will need to manually assign a temporary first and then load data into it, which also means I'll need to set up a temporaries list in my code assembler as allocations must be done before anything else? or is
it fine to allocate a variable mid-way through a function and the compiler will manage it?<br>
<br>
With that as well, if I had a function that loads a value from a pointer and then stores this value to multiple destinations, would it be ideal to store the value in an allocated temporary? would the load be called for each store if I don't allocate a temporary
or will the load be called only the one time?<br>
<br>
- Paul<br>
</p>
<br>
<br>
<div style="color:rgb(0,0,0)">
<hr style="display:inline-block;width:98%">
<div dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>From:</b> <a href="mailto:jeremy@lakeman.me" target="_blank">jeremy@lakeman.me</a> <<a href="mailto:jeremy@lakeman.me" target="_blank">jeremy@lakeman.me</a>> on behalf of Jeremy Lakeman <<a href="mailto:Jeremy.Lakeman@gmail.com" target="_blank">Jeremy.Lakeman@gmail.com</a>><br>
<b>Sent:</b> 16 March 2016 14:24<div><div><br>
<b>To:</b> Paul Hancock<br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [llvm-dev] IRBuilder Assignment ( '=' ) operator?</div></div></font>
<div> </div>
</div><div><div>
<div>
<div dir="ltr">
<div>
<div>There is a store operation if you want the value on the stack or heap, but there is no assignment operator in IR.
<br>
</div>
CreateAdd returns an Instruction which represents both the operation and the result value. Value's are immutable and can be used as input arguments to any Instruction further down the control flow.<br>
</div>
<div><br>
</div>
The other thing you might need to research is the Phi Instruction. Which gives you a way to specify how the value in this basic block will be different based on how we reached this block in the control flow of the function.<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Wed, Mar 16, 2016 at 1:04 PM, Paul Hancock via llvm-dev
<span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
However I need the standard assignment operator so I can assign the value of a temporary to that of another temporary, or to create a new temporary from an existing one.<br>
<br>
- Paul<br>
<br>
________________________________________<br>
From: Tim Northover <<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>><br>
Sent: 16 March 2016 13:11<br>
To: Paul Hancock<br>
Cc: <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
Subject: Re: [llvm-dev] IRBuilder Assignment ( '=' ) operator?<br>
<div>
<div><br>
Hi Paul,<br>
<br>
On 15 March 2016 at 17:59, Paul Hancock via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> In my code assembly system I have the various LH-RH operators, ADD, ADDF,<br>
> SUB, etc, using CreateAdd, CreateFAdd, etc, however I cant seem to locate<br>
> the correct function/s for the assignment operator.<br>
<br>
The assignment is automatic. The pointer you get back from CreateAdd<br>
can be used directly in other instructions. When this is transcribed<br>
to the textual IR, LLVM will put in the appropriate assignments (so it<br>
won't try to assign a store to anything for example, because it<br>
doesn't produce a value that can be used elsewhere).<br>
<br>
The Name parameter you pass to CreateAdd determines what variable will<br>
be assigned, otherwise an incrementing number will be used (%1, %2,<br>
...).<br>
<br>
Cheers.<br>
<br>
Tim.<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div></div></div>
</div>
</div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>