Rob Jansen
2014-Aug-09 14:26 UTC
[LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
On Sat, Aug 9, 2014 at 6:06 AM, Tim Northover <t.p.northover at gmail.com> wrote:> Hi Rob, > > On 9 August 2014 02:03, Rob Jansen <jansen at cs.umn.edu> wrote: > > Why is the first for loop not equivalent to the second? > > In the second loop, "*ui" is an llvm::Use object. It's owned by a > User, but isn't a subclass of one. To match the first loop, you either > need to call Value::user_begin instead of use_begin, or do the cast on > "ui->getUser()". >I think this is incorrect since the use_iterator is defined as: `typedef value_use_iterator<User> use_iterator;` and indeed `ui->getUser();` is not a callable function on a User. What am I missing? -Rob -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140809/ed8317a4/attachment.html>
Rob Jansen
2014-Aug-09 14:33 UTC
[LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
To make this clearer, I've reposted the loops below, replacing dyn_cast with cast. I've also added the assertion, which triggers in the bottom loop, but not the top. -Rob ========================== for (GlobalVariable **i = Globals.begin(), **e = Globals.end(); i != e; ++i) { GlobalVariable *GV = *i; Constant *GEP = ConstantExpr::getGetElementPtr(...); GV->replaceAllUsesWith(GEP); } assert(GV->use_empty()); ========================== for (GlobalVariable **i = Globals.begin(), **e = Globals.end(); i != e; ++i) { GlobalVariable *GV = *i; Constant *GEP = ConstantExpr::getGetElementPtr(...); for (Value::use_iterator ui = GV->use_begin(); ui != GV->use_end(); ++ui) { User *u = cast < User > (*ui); u->replaceUsesOfWith(GV, GEP); } } assert(GV->use_empty()); ==========================-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140809/f7ff8c3a/attachment.html>
Tim Northover
2014-Aug-09 15:43 UTC
[LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
> I think this is incorrect since the use_iterator is defined as: > > `typedef value_use_iterator<User> use_iterator;` > > and indeed `ui->getUser();` is not a callable function on a User. What am I > missing?Ah, looks like this has changed this year. What I said applies to trunk; you may be right there about 3.4 or earlier (looks plausible from the key commit). Another problem might be that User::replaceUsesOfWith only works in limited situations if the User is a Constant. But I'd expect that to fail with an assertion (assuming your LLVM has been built with assertions enabled, of course). Do you have (preferably as simple as possible) IR module that shows the replacement failure? Cheers. Tim.
Rob Jansen
2014-Aug-09 16:42 UTC
[LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
Ahh ha! I believe that `User::replaceUsesOfWith` modifies the use list, which probably invalidated the use_iterator and caused my code to miss some of the uses. The following works great! (I'll spare you the details about what I am actually trying to do here, unless you really want to know more.) Thanks, Rob ==========================while(GV->getNumUses() > 0) { User* u = GV->use_back(); u->replaceUsesOfWith(GV, GEP); } assert(GV->use_empty()); ==========================-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140809/facdf50d/attachment.html>
Possibly Parallel Threads
- [LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
- [LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
- [LLVMdev] difference between replaceAllUsesWith and replaceUsesOfWith?
- [LLVMdev] LLVM use chains
- [LLVMdev] LLVM use chains