Mercurial > hg > CbC > CbC_llvm
comparison lib/Transforms/Scalar/DeadStoreElimination.cpp @ 134:3a76565eade5 LLVM5.0.1
update 5.0.1
author | mir3636 |
---|---|
date | Sat, 17 Feb 2018 09:57:20 +0900 |
parents | 803732b1fca8 |
children | c2174574ed3a |
comparison
equal
deleted
inserted
replaced
133:c60214abe0e8 | 134:3a76565eade5 |
---|---|
113 // Before we touch this instruction, remove it from memdep! | 113 // Before we touch this instruction, remove it from memdep! |
114 do { | 114 do { |
115 Instruction *DeadInst = NowDeadInsts.pop_back_val(); | 115 Instruction *DeadInst = NowDeadInsts.pop_back_val(); |
116 ++NumFastOther; | 116 ++NumFastOther; |
117 | 117 |
118 // Try to preserve debug information attached to the dead instruction. | |
119 salvageDebugInfo(*DeadInst); | |
120 | |
118 // This instruction is dead, zap it, in stages. Start by removing it from | 121 // This instruction is dead, zap it, in stages. Start by removing it from |
119 // MemDep, which needs to know the operands and needs it to be in the | 122 // MemDep, which needs to know the operands and needs it to be in the |
120 // function. | 123 // function. |
121 MD.removeInstruction(DeadInst); | 124 MD.removeInstruction(DeadInst); |
122 | 125 |
144 *BBI = NewIter; | 147 *BBI = NewIter; |
145 } | 148 } |
146 | 149 |
147 /// Does this instruction write some memory? This only returns true for things | 150 /// Does this instruction write some memory? This only returns true for things |
148 /// that we can analyze with other helpers below. | 151 /// that we can analyze with other helpers below. |
149 static bool hasMemoryWrite(Instruction *I, const TargetLibraryInfo &TLI) { | 152 static bool hasAnalyzableMemoryWrite(Instruction *I, |
153 const TargetLibraryInfo &TLI) { | |
150 if (isa<StoreInst>(I)) | 154 if (isa<StoreInst>(I)) |
151 return true; | 155 return true; |
152 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | 156 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
153 switch (II->getIntrinsicID()) { | 157 switch (II->getIntrinsicID()) { |
154 default: | 158 default: |
178 } | 182 } |
179 | 183 |
180 /// Return a Location stored to by the specified instruction. If isRemovable | 184 /// Return a Location stored to by the specified instruction. If isRemovable |
181 /// returns true, this function and getLocForRead completely describe the memory | 185 /// returns true, this function and getLocForRead completely describe the memory |
182 /// operations for this instruction. | 186 /// operations for this instruction. |
183 static MemoryLocation getLocForWrite(Instruction *Inst, AliasAnalysis &AA) { | 187 static MemoryLocation getLocForWrite(Instruction *Inst) { |
188 | |
184 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) | 189 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
185 return MemoryLocation::get(SI); | 190 return MemoryLocation::get(SI); |
186 | 191 |
187 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) { | 192 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(Inst)) { |
188 // memcpy/memmove/memset. | 193 // memcpy/memmove/memset. |
189 MemoryLocation Loc = MemoryLocation::getForDest(MI); | 194 MemoryLocation Loc = MemoryLocation::getForDest(MI); |
190 return Loc; | 195 return Loc; |
191 } | 196 } |
192 | 197 |
193 IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst); | 198 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { |
194 if (!II) | 199 switch (II->getIntrinsicID()) { |
195 return MemoryLocation(); | 200 default: |
196 | 201 return MemoryLocation(); // Unhandled intrinsic. |
197 switch (II->getIntrinsicID()) { | 202 case Intrinsic::init_trampoline: |
198 default: | 203 return MemoryLocation(II->getArgOperand(0)); |
199 return MemoryLocation(); // Unhandled intrinsic. | 204 case Intrinsic::lifetime_end: { |
200 case Intrinsic::init_trampoline: | 205 uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); |
201 // FIXME: We don't know the size of the trampoline, so we can't really | 206 return MemoryLocation(II->getArgOperand(1), Len); |
202 // handle it here. | 207 } |
203 return MemoryLocation(II->getArgOperand(0)); | 208 } |
204 case Intrinsic::lifetime_end: { | 209 } |
205 uint64_t Len = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); | 210 if (auto CS = CallSite(Inst)) |
206 return MemoryLocation(II->getArgOperand(1), Len); | 211 // All the supported TLI functions so far happen to have dest as their |
207 } | 212 // first argument. |
208 } | 213 return MemoryLocation(CS.getArgument(0)); |
209 } | 214 return MemoryLocation(); |
210 | 215 } |
211 /// Return the location read by the specified "hasMemoryWrite" instruction if | 216 |
212 /// any. | 217 /// Return the location read by the specified "hasAnalyzableMemoryWrite" |
218 /// instruction if any. | |
213 static MemoryLocation getLocForRead(Instruction *Inst, | 219 static MemoryLocation getLocForRead(Instruction *Inst, |
214 const TargetLibraryInfo &TLI) { | 220 const TargetLibraryInfo &TLI) { |
215 assert(hasMemoryWrite(Inst, TLI) && "Unknown instruction case"); | 221 assert(hasAnalyzableMemoryWrite(Inst, TLI) && "Unknown instruction case"); |
216 | 222 |
217 // The only instructions that both read and write are the mem transfer | 223 // The only instructions that both read and write are the mem transfer |
218 // instructions (memcpy/memmove). | 224 // instructions (memcpy/memmove). |
219 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst)) | 225 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(Inst)) |
220 return MemoryLocation::getForSource(MTI); | 226 return MemoryLocation::getForSource(MTI); |
228 if (StoreInst *SI = dyn_cast<StoreInst>(I)) | 234 if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
229 return SI->isUnordered(); | 235 return SI->isUnordered(); |
230 | 236 |
231 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | 237 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
232 switch (II->getIntrinsicID()) { | 238 switch (II->getIntrinsicID()) { |
233 default: llvm_unreachable("doesn't pass 'hasMemoryWrite' predicate"); | 239 default: llvm_unreachable("doesn't pass 'hasAnalyzableMemoryWrite' predicate"); |
234 case Intrinsic::lifetime_end: | 240 case Intrinsic::lifetime_end: |
235 // Never remove dead lifetime_end's, e.g. because it is followed by a | 241 // Never remove dead lifetime_end's, e.g. because it is followed by a |
236 // free. | 242 // free. |
237 return false; | 243 return false; |
238 case Intrinsic::init_trampoline: | 244 case Intrinsic::init_trampoline: |
244 // Don't remove volatile memory intrinsics. | 250 // Don't remove volatile memory intrinsics. |
245 return !cast<MemIntrinsic>(II)->isVolatile(); | 251 return !cast<MemIntrinsic>(II)->isVolatile(); |
246 } | 252 } |
247 } | 253 } |
248 | 254 |
255 // note: only get here for calls with analyzable writes - i.e. libcalls | |
249 if (auto CS = CallSite(I)) | 256 if (auto CS = CallSite(I)) |
250 return CS.getInstruction()->use_empty(); | 257 return CS.getInstruction()->use_empty(); |
251 | 258 |
252 return false; | 259 return false; |
253 } | 260 } |
284 return II && II->getIntrinsicID() == Intrinsic::memset; | 291 return II && II->getIntrinsicID() == Intrinsic::memset; |
285 } | 292 } |
286 | 293 |
287 /// Return the pointer that is being written to. | 294 /// Return the pointer that is being written to. |
288 static Value *getStoredPointerOperand(Instruction *I) { | 295 static Value *getStoredPointerOperand(Instruction *I) { |
289 if (StoreInst *SI = dyn_cast<StoreInst>(I)) | 296 //TODO: factor this to reuse getLocForWrite |
290 return SI->getPointerOperand(); | 297 MemoryLocation Loc = getLocForWrite(I); |
291 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) | 298 assert(Loc.Ptr && |
292 return MI->getDest(); | 299 "unable to find pointer writen for analyzable instruction?"); |
293 | 300 // TODO: most APIs don't expect const Value * |
294 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | 301 return const_cast<Value*>(Loc.Ptr); |
295 switch (II->getIntrinsicID()) { | |
296 default: llvm_unreachable("Unexpected intrinsic!"); | |
297 case Intrinsic::init_trampoline: | |
298 return II->getArgOperand(0); | |
299 } | |
300 } | |
301 | |
302 CallSite CS(I); | |
303 // All the supported functions so far happen to have dest as their first | |
304 // argument. | |
305 return CS.getArgument(0); | |
306 } | 302 } |
307 | 303 |
308 static uint64_t getPointerSize(const Value *V, const DataLayout &DL, | 304 static uint64_t getPointerSize(const Value *V, const DataLayout &DL, |
309 const TargetLibraryInfo &TLI) { | 305 const TargetLibraryInfo &TLI) { |
310 uint64_t Size; | 306 uint64_t Size; |
592 // In this case we also have to look at instructions after SI. | 588 // In this case we also have to look at instructions after SI. |
593 EI = B->end(); | 589 EI = B->end(); |
594 } | 590 } |
595 for (; BI != EI; ++BI) { | 591 for (; BI != EI; ++BI) { |
596 Instruction *I = &*BI; | 592 Instruction *I = &*BI; |
597 if (I->mayWriteToMemory() && I != SecondI) { | 593 if (I->mayWriteToMemory() && I != SecondI) |
598 auto Res = AA->getModRefInfo(I, MemLoc); | 594 if (isModSet(AA->getModRefInfo(I, MemLoc))) |
599 if (Res & MRI_Mod) | |
600 return false; | 595 return false; |
601 } | |
602 } | 596 } |
603 if (B != FirstBB) { | 597 if (B != FirstBB) { |
604 assert(B != &FirstBB->getParent()->getEntryBlock() && | 598 assert(B != &FirstBB->getParent()->getEntryBlock() && |
605 "Should not hit the entry block because SI must be dominated by LI"); | 599 "Should not hit the entry block because SI must be dominated by LI"); |
606 for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) { | 600 for (auto PredI = pred_begin(B), PE = pred_end(B); PredI != PE; ++PredI) { |
650 | 644 |
651 MemDepResult Dep = | 645 MemDepResult Dep = |
652 MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB); | 646 MD->getPointerDependencyFrom(Loc, false, InstPt->getIterator(), BB); |
653 while (Dep.isDef() || Dep.isClobber()) { | 647 while (Dep.isDef() || Dep.isClobber()) { |
654 Instruction *Dependency = Dep.getInst(); | 648 Instruction *Dependency = Dep.getInst(); |
655 if (!hasMemoryWrite(Dependency, *TLI) || !isRemovable(Dependency)) | 649 if (!hasAnalyzableMemoryWrite(Dependency, *TLI) || |
650 !isRemovable(Dependency)) | |
656 break; | 651 break; |
657 | 652 |
658 Value *DepPointer = | 653 Value *DepPointer = |
659 GetUnderlyingObject(getStoredPointerOperand(Dependency), DL); | 654 GetUnderlyingObject(getStoredPointerOperand(Dependency), DL); |
660 | 655 |
754 // Scan the basic block backwards | 749 // Scan the basic block backwards |
755 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ | 750 for (BasicBlock::iterator BBI = BB.end(); BBI != BB.begin(); ){ |
756 --BBI; | 751 --BBI; |
757 | 752 |
758 // If we find a store, check to see if it points into a dead stack value. | 753 // If we find a store, check to see if it points into a dead stack value. |
759 if (hasMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) { | 754 if (hasAnalyzableMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) { |
760 // See through pointer-to-pointer bitcasts | 755 // See through pointer-to-pointer bitcasts |
761 SmallVector<Value *, 4> Pointers; | 756 SmallVector<Value *, 4> Pointers; |
762 GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL); | 757 GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL); |
763 | 758 |
764 // Stores to stack values are valid candidates for removal. | 759 // Stores to stack values are valid candidates for removal. |
820 | 815 |
821 // If the call might load from any of our allocas, then any store above | 816 // If the call might load from any of our allocas, then any store above |
822 // the call is live. | 817 // the call is live. |
823 DeadStackObjects.remove_if([&](Value *I) { | 818 DeadStackObjects.remove_if([&](Value *I) { |
824 // See if the call site touches the value. | 819 // See if the call site touches the value. |
825 ModRefInfo A = AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI)); | 820 return isRefSet(AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI))); |
826 | |
827 return A == MRI_ModRef || A == MRI_Ref; | |
828 }); | 821 }); |
829 | 822 |
830 // If all of the allocas were clobbered by the call then we're not going | 823 // If all of the allocas were clobbered by the call then we're not going |
831 // to find anything else to process. | 824 // to find anything else to process. |
832 if (DeadStackObjects.empty()) | 825 if (DeadStackObjects.empty()) |
884 // a good idea to shorten it | 877 // a good idea to shorten it |
885 // Power of 2 vector writes are probably always a bad idea to optimize | 878 // Power of 2 vector writes are probably always a bad idea to optimize |
886 // as any store/memset/memcpy is likely using vector instructions so | 879 // as any store/memset/memcpy is likely using vector instructions so |
887 // shortening it to not vector size is likely to be slower | 880 // shortening it to not vector size is likely to be slower |
888 MemIntrinsic *EarlierIntrinsic = cast<MemIntrinsic>(EarlierWrite); | 881 MemIntrinsic *EarlierIntrinsic = cast<MemIntrinsic>(EarlierWrite); |
889 unsigned EarlierWriteAlign = EarlierIntrinsic->getAlignment(); | 882 unsigned EarlierWriteAlign = EarlierIntrinsic->getDestAlignment(); |
890 if (!IsOverwriteEnd) | 883 if (!IsOverwriteEnd) |
891 LaterOffset = int64_t(LaterOffset + LaterSize); | 884 LaterOffset = int64_t(LaterOffset + LaterSize); |
892 | 885 |
893 if (!(isPowerOf2_64(LaterOffset) && EarlierWriteAlign <= LaterOffset) && | 886 if (!(isPowerOf2_64(LaterOffset) && EarlierWriteAlign <= LaterOffset) && |
894 !((EarlierWriteAlign != 0) && LaterOffset % EarlierWriteAlign == 0)) | 887 !((EarlierWriteAlign != 0) && LaterOffset % EarlierWriteAlign == 0)) |
968 const DataLayout &DL, | 961 const DataLayout &DL, |
969 InstOverlapIntervalsTy &IOL) { | 962 InstOverlapIntervalsTy &IOL) { |
970 bool Changed = false; | 963 bool Changed = false; |
971 for (auto OI : IOL) { | 964 for (auto OI : IOL) { |
972 Instruction *EarlierWrite = OI.first; | 965 Instruction *EarlierWrite = OI.first; |
973 MemoryLocation Loc = getLocForWrite(EarlierWrite, *AA); | 966 MemoryLocation Loc = getLocForWrite(EarlierWrite); |
974 assert(isRemovable(EarlierWrite) && "Expect only removable instruction"); | 967 assert(isRemovable(EarlierWrite) && "Expect only removable instruction"); |
975 assert(Loc.Size != MemoryLocation::UnknownSize && "Unexpected mem loc"); | 968 assert(Loc.Size != MemoryLocation::UnknownSize && "Unexpected mem loc"); |
976 | 969 |
977 const Value *Ptr = Loc.Ptr->stripPointerCasts(); | 970 const Value *Ptr = Loc.Ptr->stripPointerCasts(); |
978 int64_t EarlierStart = 0; | 971 int64_t EarlierStart = 0; |
1069 LastThrowingInstIndex = CurInstNumber; | 1062 LastThrowingInstIndex = CurInstNumber; |
1070 continue; | 1063 continue; |
1071 } | 1064 } |
1072 | 1065 |
1073 // Check to see if Inst writes to memory. If not, continue. | 1066 // Check to see if Inst writes to memory. If not, continue. |
1074 if (!hasMemoryWrite(Inst, *TLI)) | 1067 if (!hasAnalyzableMemoryWrite(Inst, *TLI)) |
1075 continue; | 1068 continue; |
1076 | 1069 |
1077 // eliminateNoopStore will update in iterator, if necessary. | 1070 // eliminateNoopStore will update in iterator, if necessary. |
1078 if (eliminateNoopStore(Inst, BBI, AA, MD, DL, TLI, IOL, &InstrOrdering)) { | 1071 if (eliminateNoopStore(Inst, BBI, AA, MD, DL, TLI, IOL, &InstrOrdering)) { |
1079 MadeChange = true; | 1072 MadeChange = true; |
1087 // FIXME: cross-block DSE would be fun. :) | 1080 // FIXME: cross-block DSE would be fun. :) |
1088 if (!InstDep.isDef() && !InstDep.isClobber()) | 1081 if (!InstDep.isDef() && !InstDep.isClobber()) |
1089 continue; | 1082 continue; |
1090 | 1083 |
1091 // Figure out what location is being stored to. | 1084 // Figure out what location is being stored to. |
1092 MemoryLocation Loc = getLocForWrite(Inst, *AA); | 1085 MemoryLocation Loc = getLocForWrite(Inst); |
1093 | 1086 |
1094 // If we didn't get a useful location, fail. | 1087 // If we didn't get a useful location, fail. |
1095 if (!Loc.Ptr) | 1088 if (!Loc.Ptr) |
1096 continue; | 1089 continue; |
1097 | 1090 |
1109 // away the store and we bail out. However, if we depend on something | 1102 // away the store and we bail out. However, if we depend on something |
1110 // that overwrites the memory location we *can* potentially optimize it. | 1103 // that overwrites the memory location we *can* potentially optimize it. |
1111 // | 1104 // |
1112 // Find out what memory location the dependent instruction stores. | 1105 // Find out what memory location the dependent instruction stores. |
1113 Instruction *DepWrite = InstDep.getInst(); | 1106 Instruction *DepWrite = InstDep.getInst(); |
1114 MemoryLocation DepLoc = getLocForWrite(DepWrite, *AA); | 1107 if (!hasAnalyzableMemoryWrite(DepWrite, *TLI)) |
1108 break; | |
1109 MemoryLocation DepLoc = getLocForWrite(DepWrite); | |
1115 // If we didn't get a useful location, or if it isn't a size, bail out. | 1110 // If we didn't get a useful location, or if it isn't a size, bail out. |
1116 if (!DepLoc.Ptr) | 1111 if (!DepLoc.Ptr) |
1117 break; | 1112 break; |
1118 | 1113 |
1119 // Make sure we don't look past a call which might throw. This is an | 1114 // Make sure we don't look past a call which might throw. This is an |
1178 } else if (EnablePartialStoreMerging && | 1173 } else if (EnablePartialStoreMerging && |
1179 OR == OW_PartialEarlierWithFullLater) { | 1174 OR == OW_PartialEarlierWithFullLater) { |
1180 auto *Earlier = dyn_cast<StoreInst>(DepWrite); | 1175 auto *Earlier = dyn_cast<StoreInst>(DepWrite); |
1181 auto *Later = dyn_cast<StoreInst>(Inst); | 1176 auto *Later = dyn_cast<StoreInst>(Inst); |
1182 if (Earlier && isa<ConstantInt>(Earlier->getValueOperand()) && | 1177 if (Earlier && isa<ConstantInt>(Earlier->getValueOperand()) && |
1183 Later && isa<ConstantInt>(Later->getValueOperand())) { | 1178 Later && isa<ConstantInt>(Later->getValueOperand()) && |
1179 memoryIsNotModifiedBetween(Earlier, Later, AA)) { | |
1184 // If the store we find is: | 1180 // If the store we find is: |
1185 // a) partially overwritten by the store to 'Loc' | 1181 // a) partially overwritten by the store to 'Loc' |
1186 // b) the later store is fully contained in the earlier one and | 1182 // b) the later store is fully contained in the earlier one and |
1187 // c) they both have a constant value | 1183 // c) they both have a constant value |
1188 // Merge the two stores, replacing the earlier store's value with a | 1184 // Merge the two stores, replacing the earlier store's value with a |
1253 // we can remove the first store to P even though we don't know if P and Q | 1249 // we can remove the first store to P even though we don't know if P and Q |
1254 // alias. | 1250 // alias. |
1255 if (DepWrite == &BB.front()) break; | 1251 if (DepWrite == &BB.front()) break; |
1256 | 1252 |
1257 // Can't look past this instruction if it might read 'Loc'. | 1253 // Can't look past this instruction if it might read 'Loc'. |
1258 if (AA->getModRefInfo(DepWrite, Loc) & MRI_Ref) | 1254 if (isRefSet(AA->getModRefInfo(DepWrite, Loc))) |
1259 break; | 1255 break; |
1260 | 1256 |
1261 InstDep = MD->getPointerDependencyFrom(Loc, /*isLoad=*/ false, | 1257 InstDep = MD->getPointerDependencyFrom(Loc, /*isLoad=*/ false, |
1262 DepWrite->getIterator(), &BB, | 1258 DepWrite->getIterator(), &BB, |
1263 /*QueryInst=*/ nullptr, &Limit); | 1259 /*QueryInst=*/ nullptr, &Limit); |