llvm.org GIT mirror llvm / 12c8d5f
improve IPSCCP to be able to propagate the result of "!mayBeOverridden" function to calls of that function, regardless of whether it has local linkage or has its address taken. Not escaping should only affect whether we make an aggressive assumption about the arguments to a function, not whether we can track the result of it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85795 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 63 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
2424 #include "llvm/Instructions.h"
2525 #include "llvm/Pass.h"
2626 #include "llvm/Analysis/ConstantFolding.h"
27 #include "llvm/Analysis/MemoryBuiltins.h"
2827 #include "llvm/Analysis/ValueTracking.h"
2928 #include "llvm/Transforms/Utils/Local.h"
3029 #include "llvm/Target/TargetData.h"
226225 /// and out of the specified function (which cannot have its address taken),
227226 /// this method must be called.
228227 void AddTrackedFunction(Function *F) {
229 assert(F->hasLocalLinkage() && "Can only track internal functions!");
230228 // Add an entry, F -> undef.
231229 if (const StructType *STy = dyn_cast(F->getReturnType())) {
232230 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
379377 // instruction that was just changed state somehow. Based on this
380378 // information, we need to update the specified user of this instruction.
381379 //
382 void OperandChangedState(User *U) {
383 // Only instructions use other variable values!
384 Instruction &I = cast(*U);
385 if (BBExecutable.count(I.getParent())) // Inst is executable?
386 visit(I);
380 void OperandChangedState(Instruction *I) {
381 if (BBExecutable.count(I->getParent())) // Inst is executable?
382 visit(*I);
387383 }
388384
389385 /// RemoveFromOverdefinedPHIs - If I has any entries in the
427423 void visitLoadInst (LoadInst &I);
428424 void visitGetElementPtrInst(GetElementPtrInst &I);
429425 void visitCallInst (CallInst &I) {
430 if (isFreeCall(&I))
431 return;
432426 visitCallSite(CallSite::get(&I));
433427 }
434428 void visitInvokeInst (InvokeInst &II) {
655649 markConstant(&PN, OperandVal); // Acquire operand value
656650 }
657651
652
653
654
658655 void SCCPSolver::visitReturnInst(ReturnInst &I) {
659656 if (I.getNumOperands() == 0) return; // ret void
660657
661658 Function *F = I.getParent()->getParent();
659
662660 // If we are tracking the return value of this function, merge it in.
663 if (!F->hasLocalLinkage())
664 return;
665
666661 if (!TrackedRetVals.empty()) {
667662 DenseMap::iterator TFRVI =
668663 TrackedRetVals.find(F);
669 if (TFRVI != TrackedRetVals.end() &&
670 !TFRVI->second.isOverdefined()) {
664 if (TFRVI != TrackedRetVals.end()) {
671665 mergeInValue(TFRVI->second, F, getValueState(I.getOperand(0)));
672666 return;
673667 }
11631157 // The common case is that we aren't tracking the callee, either because we
11641158 // are not doing interprocedural analysis or the callee is indirect, or is
11651159 // external. Handle these cases first.
1166 if (F == 0 || !F->hasLocalLinkage()) {
1160 if (F == 0 || F->isDeclaration()) {
11671161 CallOverdefined:
11681162 // Void return and not tracking callee, just bail.
11691163 if (I->getType()->isVoidTy()) return;
11701164
11711165 // Otherwise, if we have a single return value case, and if the function is
11721166 // a declaration, maybe we can constant fold it.
1173 if (!isa(I->getType()) && F && F->isDeclaration() &&
1167 if (F && F->isDeclaration() && !isa(I->getType()) &&
11741168 canConstantFoldCallTo(F)) {
11751169
11761170 SmallVector Operands;
12341228 // common path above.
12351229 goto CallOverdefined;
12361230 }
1237
1231
12381232 // Finally, if this is the first call to the function hit, mark its entry
12391233 // block executable.
12401234 MarkBlockExecutable(F->begin());
12431237 CallSite::arg_iterator CAI = CS.arg_begin();
12441238 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
12451239 AI != E; ++AI, ++CAI) {
1240 // If this argument is byval, and if the function is not readonly, there
1241 // will be an implicit copy formed of the input aggregate.
12461242 if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
12471243 markOverdefined(AI);
12481244 continue;
12721268 //
12731269 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
12741270 UI != E; ++UI)
1275 OperandChangedState(*UI);
1271 if (Instruction *I = dyn_cast(*UI))
1272 OperandChangedState(I);
12761273 }
12771274
12781275 // Process the instruction work list.
12911288 if (!getValueState(I).isOverdefined())
12921289 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
12931290 UI != E; ++UI)
1294 OperandChangedState(*UI);
1291 if (Instruction *I = dyn_cast(*UI))
1292 OperandChangedState(I);
12951293 }
12961294
12971295 // Process the basic block work list.
16491647 if (F->isDeclaration())
16501648 continue;
16511649
1652 if (!F->hasLocalLinkage() || AddressIsTaken(F)) {
1653 Solver.MarkBlockExecutable(F->begin());
1654 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1655 AI != E; ++AI)
1656 Solver.markOverdefined(AI);
1657 } else {
1650 // If this is a strong or ODR definition of this function, then we can
1651 // propagate information about its result into callsites of it.
1652 if (!F->mayBeOverridden())
16581653 Solver.AddTrackedFunction(F);
1659 }
1654
1655 // If this function only has direct calls that we can see, we can track its
1656 // arguments and return value aggressively, and can assume it is not called
1657 // unless we see evidence to the contrary.
1658 if (F->hasLocalLinkage() && !AddressIsTaken(F))
1659 continue;
1660
1661 // Assume the function is called.
1662 Solver.MarkBlockExecutable(F->begin());
1663
1664 // Assume nothing about the incoming arguments.
1665 for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1666 AI != E; ++AI)
1667 Solver.markOverdefined(AI);
16601668 }
16611669
16621670 // Loop over global variables. We inform the solver about any internal global
18041812 // TODO: Process multiple value ret instructions also.
18051813 const DenseMap &RV = Solver.getTrackedRetVals();
18061814 for (DenseMap::const_iterator I = RV.begin(),
1807 E = RV.end(); I != E; ++I)
1808 if (!I->second.isOverdefined() &&
1809 !I->first->getReturnType()->isVoidTy()) {
1810 Function *F = I->first;
1811 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1812 if (ReturnInst *RI = dyn_cast(BB->getTerminator()))
1813 if (!isa(RI->getOperand(0)))
1814 RI->setOperand(0, UndefValue::get(F->getReturnType()));
1815 }
1816
1815 E = RV.end(); I != E; ++I) {
1816 Function *F = I->first;
1817 if (I->second.isOverdefined() || F->getReturnType()->isVoidTy())
1818 continue;
1819
1820 // We can only do this if we know that nothing else can call the function.
1821 if (!F->hasLocalLinkage() || AddressIsTaken(F))
1822 continue;
1823
1824 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1825 if (ReturnInst *RI = dyn_cast(BB->getTerminator()))
1826 if (!isa(RI->getOperand(0)))
1827 RI->setOperand(0, UndefValue::get(F->getReturnType()));
1828 }
1829
18171830 // If we infered constant or undef values for globals variables, we can delete
18181831 // the global and any stores that remain to it.
18191832 const DenseMap &TG = Solver.getTrackedGlobals();
133133 ret i64 %b
134134 }
135135
136
137 ;;======================== test6
138
139 define i64 @test6a() {
140 ret i64 0
141 }
142
143 define i64 @test6b() {
144 %a = call i64 @test6a()
145 ret i64 %a
146 }
147 ; CHECK: define i64 @test6b
148 ; CHECK: ret i64 0
149