From 7cb6809c258a02b1322762d3b6a720493b000380 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 28 Sep 2006 23:02:22 +0000 Subject: [PATCH] Another attempt at making ArgPromotion smarter. This patch no longer breaks Burg. llvm-svn: 30657 --- llvm/lib/Transforms/IPO/ArgumentPromotion.cpp | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp index 5183d43bc7c2..a3ebc65f12a3 100644 --- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp +++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp @@ -179,6 +179,53 @@ static bool AllCalleesPassInValidPointerForArgument(Argument *Arg) { return true; } +/// AccessOccursOnPath - Returns true if and only if a load or GEP instruction +/// on Pointer occurs in Path, or in every control-flow path that succeeds it. +bool AccessOccursOnPath(Value* V, BasicBlock* Start) { + std::vector Worklist; + Worklist.push_back(Start); + + std::set Visited; + + while (!Worklist.empty()) { + BasicBlock* BB = Worklist.back(); + Worklist.pop_back(); + Visited.insert(BB); + + bool ContainsAccess = false; + for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { + if (isa(I)) { + for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); OI != OE; ++OI) + if (*OI == V) { + ContainsAccess = true; + break; + } + } else if (isa(I)) { + for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); OI != OE; ++OI) + if (*OI == V) { + ContainsAccess = AccessOccursOnPath(I, I->getParent()); + break; + } + } + + if (ContainsAccess) + break; + } + + if (ContainsAccess) continue; + + TerminatorInst* TI = BB->getTerminator(); + if (isa(TI) || isa(TI)) { + for (unsigned i = 0; i < TI->getNumSuccessors(); ++i) + if (!Visited.count(TI->getSuccessor(i))) + Worklist.push_back(TI->getSuccessor(i)); + } else { + return false; + } + } + + return true; +} /// isSafeToPromoteArgument - As you might guess from the name of this method, /// it checks to see if it is both safe and useful to promote the argument. @@ -252,7 +299,8 @@ bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg) const { // of the pointer in the entry block of the function) or if we can prove that // all pointers passed in are always to legal locations (for example, no null // pointers are passed in, no pointers to free'd memory, etc). - if (!HasLoadInEntryBlock && !AllCalleesPassInValidPointerForArgument(Arg)) + if (!AccessOccursOnPath(Arg, Arg->getParent()->begin()) && + !AllCalleesPassInValidPointerForArgument(Arg)) return false; // Cannot prove that this is safe!! // Okay, now we know that the argument is only used by load instructions and