[llvm-dev] IRBuilder Assignment ( '=' ) operator?

mats petersson via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 16 02:53:40 PDT 2016


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.

In short, "when in doubt alloca".

--
Mats

On 16 March 2016 at 05:57, Jeremy Lakeman via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> "do an assign"
> 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.
>
> 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.
>
> eg;
>
> $ cat main.c
> int main(){
>   int i=0;
>   i+=1;
>   return 0;
> }
>
> $ clang -O0 -c main.c -emit-llvm && llvm-dis main.bc && cat main.ll
> ...
> define i32 @main() #0 {
>   %i = alloca i32, align 4
>   store i32 0, i32* %i, align 4
>   %1 = load i32* %i, align 4
>   %2 = add nsw i32 %1, 1
>   store i32 %2, i32* %i, align 4
>   ret i32 0
> }
>
> 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.
>
> $ cat fib.c
> unsigned fibonacci(unsigned n){
>   if (n == 0)
>     return 0;
>   if (n == 1)
>     return 1;
>   return fibonacci(n - 1) + fibonacci(n - 2);
> }
>
> $ clang -O3 -c fib.c -emit-llvm && llvm-dis fib.bc && cat fib.ll
> ...
> define i32 @fibonacci(i32 %n) #0 {
>   switch i32 %n, label %2 [
>     i32 0, label %8
>     i32 1, label %1
>   ]
>
> ; <label>:1                                       ; preds = %0
>   br label %8
>
> ; <label>:2                                       ; preds = %0
>   %3 = add i32 %n, -1
>   %4 = tail call i32 @fibonacci(i32 %3)
>   %5 = add i32 %n, -2
>   %6 = tail call i32 @fibonacci(i32 %5)
>   %7 = add i32 %6, %4
>   ret i32 %7
>
> ; <label>:8                                       ; preds = %1, %0
>   %.0 = phi i32 [ 1, %1 ], [ 0, %0 ]
>   ret i32 %.0
> }
>
> 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.
>
> Each line like;
>   %7 = add i32 %6, %4
> above, represents a single (Instruction*).
> "%7" is it's name (in this case auto-numbered because it has no explicit
> name).
> "add i32 ..." is a textual representation of the specific Instruction.
> There is no separate llvm object to represent the " = " string above.
>
> 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.
>
> You may also find this useful to help you understand how to use the C++
> API;
> $ llc -march=cpp fib.bc
> $ cat fib.cpp
>
> ...
>
>
>
>
>
>
>
> On Wed, Mar 16, 2016 at 3:40 PM, Paul Hancock via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
>
>> 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?
>>
>> 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?
>>
>> - Paul
>>
>>
>> ------------------------------
>> *From:* jeremy at lakeman.me <jeremy at lakeman.me> on behalf of Jeremy
>> Lakeman <Jeremy.Lakeman at gmail.com>
>> *Sent:* 16 March 2016 14:24
>>
>> *To:* Paul Hancock
>> *Cc:* llvm-dev at lists.llvm.org
>> *Subject:* Re: [llvm-dev] IRBuilder Assignment ( '=' ) operator?
>>
>> There is a store operation if you want the value on the stack or heap,
>> but there is no assignment operator in IR.
>> 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.
>>
>> 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.
>>
>> On Wed, Mar 16, 2016 at 1:04 PM, Paul Hancock via llvm-dev <
>> llvm-dev at lists.llvm.org> wrote:
>>
>>> 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.
>>>
>>> - Paul
>>>
>>> ________________________________________
>>> From: Tim Northover <t.p.northover at gmail.com>
>>> Sent: 16 March 2016 13:11
>>> To: Paul Hancock
>>> Cc: llvm-dev at lists.llvm.org
>>> Subject: Re: [llvm-dev] IRBuilder Assignment ( '=' ) operator?
>>>
>>> Hi Paul,
>>>
>>> On 15 March 2016 at 17:59, Paul Hancock via llvm-dev
>>> <llvm-dev at lists.llvm.org> wrote:
>>> > In my code assembly system I have the various LH-RH operators, ADD,
>>> ADDF,
>>> > SUB, etc, using CreateAdd, CreateFAdd, etc, however I cant seem to
>>> locate
>>> > the correct function/s for the assignment operator.
>>>
>>> The assignment is automatic. The pointer you get back from CreateAdd
>>> can be used directly in other instructions. When this is transcribed
>>> to the textual IR, LLVM will put in the appropriate assignments (so it
>>> won't try to assign a store to anything for example, because it
>>> doesn't produce a value that can be used elsewhere).
>>>
>>> The Name parameter you pass to CreateAdd determines what variable will
>>> be assigned, otherwise an incrementing number will be used (%1, %2,
>>> ...).
>>>
>>> Cheers.
>>>
>>> Tim.
>>> _______________________________________________
>>> LLVM Developers mailing list
>>> llvm-dev at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>>
>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>
>>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160316/b545eaa9/attachment-0001.html>


More information about the llvm-dev mailing list