llvm.org GIT mirror llvm / a040965
teach DSE to use GetPointerBaseWithConstantOffset to analyze may-aliasing stores that partially overlap with different base pointers. This implements PR6043 and the non-variable part of PR8657 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120485 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 8 years ago
3 changed file(s) with 73 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
101101 /// base and offset to the caller.
102102 Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
103103 const TargetData &TD);
104 static inline const Value *
105 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
106 const TargetData &TD) {
107 return GetPointerBaseWithConstantOffset(const_cast(Ptr), Offset,TD);
108 }
104109
105110 /// GetConstantStringInfo - This function computes the length of a
106111 /// null-terminated C string pointed to by V. If successful, it returns true
2727 #include "llvm/Analysis/Dominators.h"
2828 #include "llvm/Analysis/MemoryBuiltins.h"
2929 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
30 #include "llvm/Analysis/ValueTracking.h"
3031 #include "llvm/Target/TargetData.h"
3132 #include "llvm/Transforms/Utils/Local.h"
3233 using namespace llvm;
259260 static bool isCompleteOverwrite(const AliasAnalysis::Location &Later,
260261 const AliasAnalysis::Location &Earlier,
261262 AliasAnalysis &AA) {
262 const Value *P1 = Later.Ptr->stripPointerCasts();
263 const Value *P2 = Earlier.Ptr->stripPointerCasts();
264
265 // Make sure that the start pointers are the same.
266 if (P1 != P2)
263 const Value *P1 = Earlier.Ptr->stripPointerCasts();
264 const Value *P2 = Later.Ptr->stripPointerCasts();
265
266 // If the start pointers are the same, we just have to compare sizes to see if
267 // the later store was larger than the earlier store.
268 if (P1 == P2) {
269 // If we don't know the sizes of either access, then we can't do a
270 // comparison.
271 if (Later.Size == AliasAnalysis::UnknownSize ||
272 Earlier.Size == AliasAnalysis::UnknownSize) {
273 // If we have no TargetData information around, then the size of the store
274 // is inferrable from the pointee type. If they are the same type, then
275 // we know that the store is safe.
276 if (AA.getTargetData() == 0)
277 return Later.Ptr->getType() == Earlier.Ptr->getType();
278 return false;
279 }
280
281 // Make sure that the Later size is >= the Earlier size.
282 if (Later.Size < Earlier.Size)
283 return false;
284 return true;
285 }
286
287 // Otherwise, we have to have size information, and the later store has to be
288 // larger than the earlier one.
289 if (Later.Size == AliasAnalysis::UnknownSize ||
290 Earlier.Size == AliasAnalysis::UnknownSize ||
291 Later.Size <= Earlier.Size ||
292 AA.getTargetData() == 0)
267293 return false;
268
269 // If we don't know the sizes of either access, then we can't do a comparison.
270 if (Later.Size == AliasAnalysis::UnknownSize ||
271 Earlier.Size == AliasAnalysis::UnknownSize) {
272 // If we have no TargetData information around, then the size of the store
273 // is inferrable from the pointee type. If they are the same type, then we
274 // know that the store is safe.
275 if (AA.getTargetData() == 0)
276 return Later.Ptr->getType() == Earlier.Ptr->getType();
294
295 const TargetData &TD = *AA.getTargetData();
296
297 // Okay, we have stores to two completely different pointers. Try to
298 // decompose the pointer into a "base + constant_offset" form. If the base
299 // pointers are equal, then we can reason about the two stores.
300 int64_t Off1 = 0, Off2 = 0;
301 const Value *BP1 = GetPointerBaseWithConstantOffset(P1, Off1, TD);
302 const Value *BP2 = GetPointerBaseWithConstantOffset(P2, Off2, TD);
303
304 // If the base pointers still differ, we have two completely different stores.
305 if (BP1 != BP2)
277306 return false;
278 }
279
280 // Make sure that the Later size is >= the Earlier size.
281 if (Later.Size < Earlier.Size)
307
308 // Otherwise, we might have a situation like:
309 // store i16 -> P + 1 Byte
310 // store i32 -> P
311 // In this case, we see if the later store completely overlaps all bytes
312 // stored by the previous store.
313 if (Off1 < Off2 || // Earlier starts before Later.
314 Off1+Earlier.Size > Off2+Later.Size) // Earlier goes beyond Later.
282315 return false;
283
316 // Otherwise, we have complete overlap.
284317 return true;
285318 }
286319
3535 %tmp.7 = zext i1 %tmp.6 to i32
3636 ret i32 %tmp.7
3737 }
38
39 ; PR6043
40 define void @test4(i8* %P) {
41 ; CHECK: @test4
42 ; CHECK-NEXT: bitcast
43 ; CHECK-NEXT: store double
44
45 store i8 19, i8* %P ;; dead
46 %A = getelementptr i8* %P, i32 3
47
48 store i8 42, i8* %A ;; dead
49
50 %Q = bitcast i8* %P to double*
51 store double 0.0, double* %Q
52 ret void
53 }