Mahesha S via llvm-dev
2019-Jan-22 13:22 UTC
[llvm-dev] Question about global variable type
Hi, Let's say, we want to insert a global variable to a module, that, we can accomplish as shown in the below code snippet. However, if I compare the original type with the type after the call to getOrInsertGlobal(), the two pointers are different as shown. Is it a correct behavior? If so, what is the right way to get the type of global variable so that it is same as original type? PS: I am using LLVM versoin 8 ------------------ // Get type of global variable to be inserted auto varType = IntegerType::get(M.getContext(), 32); outs() << "varType = " << varType << "\n"; // Insert newly created variable to current module auto globalVar = M.getOrInsertGlobal("myVar", varType); outs() << "varType = " << globalVar->getType() << "\n"; ------------------ ------------------ varType = 0x5567b71a22b8 varType = 0x5567b71ce1b0 ------------------ -- Thanks, Mahesha -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190122/7a3b5d7f/attachment.html>
Tim Northover via llvm-dev
2019-Jan-22 13:33 UTC
[llvm-dev] Question about global variable type
On Tue, 22 Jan 2019 at 13:22, Mahesha S via llvm-dev <llvm-dev at lists.llvm.org> wrote:> auto globalVar = M.getOrInsertGlobal("myVar", varType);Globals (as an instance of Value) have a type that's a pointer to the allocated storage. So in this case globalVar->getType() is actually "i32*" rather than i32. GlobalValue::getValueType will return the underlying type. You could use Type::dump to see this kind of thing more quickly in future. Cheers. Tim.