llvm.org GIT mirror llvm / b401e3b
Teach DeadStoreElimination to eliminate exit-block stores with phi addresses. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156558 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 7 years ago
4 changed file(s) with 68 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
150150 return GetUnderlyingObject(const_cast(V), TD, MaxLookup);
151151 }
152152
153 /// GetUnderlyingObjects - This method is similar to GetUnderlyingObject
154 /// except that it can look through phi and select instructions and return
155 /// multiple objects.
156 void GetUnderlyingObjects(Value *V,
157 SmallVectorImpl &Objects,
158 const TargetData *TD = 0,
159 unsigned MaxLookup = 6);
160
153161 /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
154162 /// are lifetime markers.
155163 bool onlyUsedByLifetimeMarkers(const Value *V);
17951795 return V;
17961796 }
17971797
1798 void
1799 llvm::GetUnderlyingObjects(Value *V,
1800 SmallVectorImpl &Objects,
1801 const TargetData *TD,
1802 unsigned MaxLookup) {
1803 SmallPtrSet Visited;
1804 SmallVector Worklist;
1805 Worklist.push_back(V);
1806 do {
1807 Value *P = Worklist.pop_back_val();
1808 P = GetUnderlyingObject(P, TD, MaxLookup);
1809
1810 if (!Visited.insert(P))
1811 continue;
1812
1813 if (SelectInst *SI = dyn_cast(P)) {
1814 Worklist.push_back(SI->getTrueValue());
1815 Worklist.push_back(SI->getFalseValue());
1816 continue;
1817 }
1818
1819 if (PHINode *PN = dyn_cast(P)) {
1820 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1821 Worklist.push_back(PN->getIncomingValue(i));
1822 continue;
1823 }
1824
1825 Objects.push_back(P);
1826 } while (!Worklist.empty());
1827 }
1828
17981829 /// onlyUsedByLifetimeMarkers - Return true if the only users of this pointer
17991830 /// are lifetime markers.
18001831 ///
730730 // If we find a store, check to see if it points into a dead stack value.
731731 if (hasMemoryWrite(BBI) && isRemovable(BBI)) {
732732 // See through pointer-to-pointer bitcasts
733 Value *Pointer = GetUnderlyingObject(getStoredPointerOperand(BBI));
733 SmallVector Pointers;
734 GetUnderlyingObjects(getStoredPointerOperand(BBI), Pointers);
734735
735736 // Stores to stack values are valid candidates for removal.
736 if (DeadStackObjects.count(Pointer)) {
737 bool AllDead = true;
738 for (SmallVectorImpl::iterator I = Pointers.begin(),
739 E = Pointers.end(); I != E; ++I)
740 if (!DeadStackObjects.count(*I)) {
741 AllDead = false;
742 break;
743 }
744
745 if (AllDead) {
737746 Instruction *Dead = BBI++;
738747
739748 DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
740 << *Dead << "\n Object: " << *Pointer << '\n');
749 << *Dead << "\n Objects: ";
750 for (SmallVectorImpl::iterator I = Pointers.begin(),
751 E = Pointers.end(); I != E; ++I) {
752 dbgs() << **I;
753 if (llvm::next(I) != E)
754 dbgs() << ", ";
755 }
756 dbgs() << '\n');
741757
742758 // DCE instructions only used to calculate that store.
743759 DeleteDeadInstruction(Dead, *MD, &DeadStackObjects);
265265 ; CHECK-NEXT: ret void
266266 ret void
267267 }
268
269 ; CHECK: @test22(
270 define void @test22(i1 %i, i32 %k, i32 %m) nounwind {
271 %k.addr = alloca i32
272 %m.addr = alloca i32
273 %k.addr.m.addr = select i1 %i, i32* %k.addr, i32* %m.addr
274 store i32 0, i32* %k.addr.m.addr, align 4
275 ; CHECK-NEXT: ret void
276 ret void
277 }