GlobalValues appear in their own use lists?

In the following IR module:

define i8 @foo() #0 {
entry:
%call0 = call i8 @bar()
ret i8 %call0
}

declare i8 @bar() #1

@bar() gets marked as its own user in top-of-tree LLVM. I patched the Verifier to check it (but didn’t commit the patch):

Index: lib/IR/Verifier.cpp

In the following IR module:

define i8 @foo() #0 {
entry:
%call0 = call i8 @bar()
ret i8 %call0
}

declare i8 @bar() #1

@bar() gets marked as its own user in top-of-tree LLVM. I patched the Verifier to check it (but didn’t commit the patch):

Index: lib/IR/Verifier.cpp

— lib/IR/Verifier.cpp (revision 203468)
+++ lib/IR/Verifier.cpp (working copy)
@@ -360,6 +360,11 @@
“Global is external, but doesn’t have external or weak linkage!”,
&GV);

  • for (Value::const_use_iterator UI = GV.use_begin(), UE = GV.use_end();
  • UI != UE; ++UI) {
  • Assert1(*UI != &GV, “Global values cannot be their own uses!”, &GV);
  • }

Assert1(!GV.hasAppendingLinkage() || isa(GV),
“Only global variables can have appending linkage!”, &GV);

Is it ever reasonable for a global value to be its own use?

No, this is never possible. The only globals that have uses are GlobalVariables and Aliases, and both of them require that the operand have a different type than the global itself (one level of pointer is removed). Since LLVM 2.0, it isn’t possible to have a "pointer to itself” type anymore.

If not, can I commit this patch?

Sure, but note that it won’t apply to mainline. use iterators got reworked to be C++’11ified and got renamed in the process.

This really causes problems for LLDB in various parts of our logic, including infinite loops when following use chains, but a major issue is that we try to strip certain global values out of the module, and if they’re their own uses then that doesn’t work because we can never eliminate the last use.

Are you sure that they are direct uses like this? If is possible to have global variables that use themselves through constant expressions. A C example would be something like:

extern void *G;
void *G = &G;

which compiles to:

@G = global i8* bitcast (i8** @G to i8*), align 8

-Chris

Chris,

this is a patch against top-of-tree. Maybe I wrote something wrong in the patch?

$ svn info

Last Changed Author: chapuni
Last Changed Rev: 203523
Last Changed Date: 2014-03-10 17:34:38 -0700 (Mon, 10 Mar 2014)

$ svn diff
Index: lib/IR/Verifier.cpp

As Chris said, use_iterator walks over the Use objects now, not over the
User objects.

Sadly, a Use *implicitly converts* to the used value. IE, directly back to
the Value you called 'use_begin()' on. That is why this assert is firing. =[

If you want to walk the Users which Use an object (rather than the Use
nodes that connect them) use the newly minted Value::user_iterator (or a
range based for loop over GV.users()).

Chandler, the implicit conversion is an extremely scary thing now in the presence of both use_iterator and user_iterator. Can you remove the implicit conversion? If not, I think we have to get rid of one or the other, or rename them to be less similar.

-Chris

Ohhh, I see!

Thanks Chandler and Chris, that makes more sense now. All I have to do, then, is fix some of LLDB’s logic that is now using the implicit conversion.

Sean