Simple question

Hi,

I think this is a very simple question, and it must just be missing something.

I am looking for find out how to assign a constant integer value to
the variable in llvm ir.

The following returns 12, and %var2 = 12.
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
  %var1 = xor i32 0, 0
  %var2 = add i32 %var1, 12
  ret i32 %var2
}

Why can't I do?:
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
  %var2 = 12
  ret i32 %var2
}

What is the simplest way to make %var2 = 12 ?

Kind Regards

James

Hi,

I think this is a very simple question, and it must just be missing something.

I am looking for find out how to assign a constant integer value to
the variable in llvm ir.

The following returns 12, and %var2 = 12.
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
%var1 = xor i32 0, 0
%var2 = add i32 %var1, 12
ret i32 %var2
}

Why can't I do?:
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
%var2 = 12
ret i32 %var2
}

Because an assignment of a constant to a variable isn't an instruction. Or more to the point, the right-hand-side of "%var2 = 12" isn't an instruction.

What is the simplest way to make %var2 = 12 ?

I think that this should work:

define i32 @test() {
  %v = bitcast i32 42 to i32
  ret i32 %v
}

-bw

If you turn on the optimizer from clang (-O3), you will get:
   store i32 12, i32* @var1, align 4, !tbaa !0

   %var2 = 12

is not a valid LLVM IR.

Chuck

Note that for your (trivial) example, this works:

define i32 @test() {
  ret i32 42
}

That's because the 'ret' is an instruction with the argument 'i32 42'.

-bw

From: llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu]
On Behalf Of James Courtier-Dutton
Subject: [LLVMdev] Simple question

define i32 @test() nounwind readnone {
  %var2 = 12
  ret i32 %var2
}

What is the simplest way to make %var2 = 12 ?

There is no LLVM instruction for an assignment, since it's superfluous in an SSA environment.

What are you using to create the IR? In the above example, there's no need for var2; just "ret i32 12" will suffice if you're hand-coding it. If you're generating the IR programmatically, you would normally use ConstantInt::get() with appropriate parameters to create a Value object that can be used wherever needed.

- Chuck

THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.

James Courtier-Dutton <james.dutton@gmail.com> writes:

I think this is a very simple question, and it must just be missing something.

I am looking for find out how to assign a constant integer value to
the variable in llvm ir.

The following returns 12, and %var2 = 12.
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
  %var1 = xor i32 0, 0
  %var2 = add i32 %var1, 12
  ret i32 %var2
}

Why can't I do?:
; ModuleID = 't.c'
target datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

define i32 @test() nounwind readnone {
  %var2 = 12
  ret i32 %var2
}

What is the simplest way to make %var2 = 12 ?

To add to the other response, it is important to note that your %var1,
%var2 above are not variables at all, because they can't be reassigned
(i.e. they can't appear again on the left hand of a %varX = ...
expression.) They are just names for the values corresponding to the
instructions.

Ok, maybe i asked the wrong question.
Instead of using the value 12 all the way through the llvm ir text file. How do i use %friendlyName instead?
I.e. The equivalent of something like
#define FriendlyName 12
in C

You can’t. You have to use the value 12 directly.

I think something like
   %twelve = add i32 12, 0
would work.

-Krzysztof

You can make it a constant global:

@g = global i32 12, align 4

define i32 @foo() nounwind uwtable readonly ssp {
%1 = load i32* @g, align 4
ret i32 %1
}

-bw