Duat via llvm-dev
2019-Sep-30 16:23 UTC
[llvm-dev] Global or Local variable different builder behavior
Hello, I posted this question on stackoverflow : https://stackoverflow.com/questions/58147549/global-or-local-variable-different-builder-behavior with no response, but I can't understand the different behavior based on whether the variable is global or local. Can you help me to understand? thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190930/e5d138d2/attachment.html>
Tim Northover via llvm-dev
2019-Sep-30 16:43 UTC
[llvm-dev] Global or Local variable different builder behavior
Hi Duat, On Mon, 30 Sep 2019 at 17:23, Duat via llvm-dev <llvm-dev at lists.llvm.org> wrote:> I posted this question on stackoverflow : https://stackoverflow.com/questions/58147549/global-or-local-variable-different-builder-behavior > > with no response, but I can't understand the different behavior based on whether the variable is global or local. > Can you help me to understand?LLVM has roughly[1] two kinds of Value: Constants and Instructions. Constants are things like literal constants, (addresses of) global variables, and various expressions based just on those things; they're designed to be values that can be directly calculated by the compiler and/or linker without any CPU instructions actually being executed[2]. Instructions on the other hand sit inside Functions as real entities, they produce %whatever Values and, unless optimized away, will be turned into real CPU instructions in the end. So, you were asking for "GEP something, 0, 49". If that "something" is a Constant (e.g. a GlobalVariable) then that GEP only depends on Constants so it can be a ConstantExpr too, written "getelementptr([1024 x i64], [1024 x i64]* @var, i32 0, i32 49)". That Constant is then not inserted into a block (it's not an instruction so it can't be). Instead it's written directly in any instruction that uses it, so if you actually use the GEP you might see something like: %val = load i64, i64* getelementptr([1024 x i64], [1024 x i64]* @var, i32 0, i32 49) Until you use it, it's not actually in the function anywhere though. You just have a handle when needed. On the other hand if the "something" is a local variable, then the GEP needs to be an actual instruction inside a function and the API you're using will insert it automatically. In the Constant case, you can manually create an instruction anyway, at least in C++. I'm afraid I haven't used the C API and couldn't see an obvious way there, but you probably don't want to since optimization would quickly undo it and turn it back into a Constant. Cheers. Tim. [1] There's also Arguments, representing function parameters. They behave like Instructions for these purposes. [2] But you can build pathological Constants that no linker really could calculate like 4 * @global. That tends to result in a compiler error.
Michael Kruse via llvm-dev
2019-Sep-30 16:45 UTC
[llvm-dev] Global or Local variable different builder behavior
With 'A' being a global variable, the GEP becomes a ConstantExpr (GetElementPtrConstantExpr instead of GetElementPtrInst) since all of its arguments are constant. ConstantExpr are "free-floating", i.e. not int a BasicBlock's instruction list and therefore only appear in the printout when used. Michael Am Mo., 30. Sept. 2019 um 11:23 Uhr schrieb Duat via llvm-dev <llvm-dev at lists.llvm.org>:> > Hello, > I posted this question on stackoverflow : https://stackoverflow.com/questions/58147549/global-or-local-variable-different-builder-behavior > > with no response, but I can't understand the different behavior based on whether the variable is global or local. > Can you help me to understand? > > thanks > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev