llvm.org GIT mirror llvm / ae10dd2
Merge isObjectPointerWithTrustworthySize with getPointerSize. Use it when looking at the size of the pointee. Fixes PR11390! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@144773 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 7 years ago
2 changed file(s) with 61 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
273273 }
274274 }
275275
276 static uint64_t getPointerSize(Value *V, AliasAnalysis &AA) {
276 static uint64_t getPointerSize(const Value *V, AliasAnalysis &AA) {
277277 const TargetData *TD = AA.getTargetData();
278278
279 if (CallInst *CI = dyn_cast(V)) {
280 assert(isMalloc(CI) && "Expected Malloc call!");
281 if (ConstantInt *C = dyn_cast(CI->getArgOperand(0)))
279 if (const CallInst *CI = extractMallocCall(V)) {
280 if (const ConstantInt *C = dyn_cast(CI->getArgOperand(0)))
282281 return C->getZExtValue();
283 return AliasAnalysis::UnknownSize;
284282 }
285283
286284 if (TD == 0)
287285 return AliasAnalysis::UnknownSize;
288286
289 if (AllocaInst *A = dyn_cast(V)) {
287 if (const AllocaInst *A = dyn_cast(V)) {
290288 // Get size information for the alloca
291 if (ConstantInt *C = dyn_cast(A->getArraySize()))
289 if (const ConstantInt *C = dyn_cast(A->getArraySize()))
292290 return C->getZExtValue() * TD->getTypeAllocSize(A->getAllocatedType());
293 return AliasAnalysis::UnknownSize;
294 }
295
296 assert(isa(V) && "Expected AllocaInst, malloc call or Argument!");
297 PointerType *PT = cast(V->getType());
298 return TD->getTypeAllocSize(PT->getElementType());
299 }
300
301 /// isObjectPointerWithTrustworthySize - Return true if the specified Value* is
302 /// pointing to an object with a pointer size we can trust.
303 static bool isObjectPointerWithTrustworthySize(const Value *V) {
304 if (const AllocaInst *AI = dyn_cast(V))
305 return !AI->isArrayAllocation();
306 if (const GlobalVariable *GV = dyn_cast(V))
307 return !GV->mayBeOverridden();
308 if (const Argument *A = dyn_cast(V))
309 return A->hasByValAttr();
310 if (isMalloc(V))
311 return true;
312 return false;
291 }
292
293 if (const Argument *A = dyn_cast(V)) {
294 if (A->hasByValAttr())
295 if (PointerType *PT = dyn_cast(A->getType()))
296 return TD->getTypeAllocSize(PT->getElementType());
297 }
298
299 if (const GlobalVariable *GV = dyn_cast(V)) {
300 if (!GV->mayBeOverridden())
301 return TD->getTypeAllocSize(GV->getType()->getElementType());
302 }
303
304 return AliasAnalysis::UnknownSize;
313305 }
314306
315307 namespace {
328320 static OverwriteResult isOverwrite(const AliasAnalysis::Location &Later,
329321 const AliasAnalysis::Location &Earlier,
330322 AliasAnalysis &AA,
331 int64_t& EarlierOff,
332 int64_t& LaterOff) {
323 int64_t &EarlierOff,
324 int64_t &LaterOff) {
333325 const Value *P1 = Earlier.Ptr->stripPointerCasts();
334326 const Value *P2 = Later.Ptr->stripPointerCasts();
335327
376368 return OverwriteUnknown;
377369
378370 // If the "Later" store is to a recognizable object, get its size.
379 if (isObjectPointerWithTrustworthySize(UO2)) {
380 uint64_t ObjectSize =
381 TD.getTypeAllocSize(cast(UO2->getType())->getElementType());
371 uint64_t ObjectSize = getPointerSize(UO2, AA);
372 if (ObjectSize != AliasAnalysis::UnknownSize)
382373 if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size)
383374 return OverwriteComplete;
384 }
385375
386376 // Okay, we have stores to two completely different pointers. Try to
387377 // decompose the pointer into a "base + constant_offset" form. If the base
0 ; RUN: opt -basicaa -dse -S -o - | FileCheck %s
1 ; PR11390
2 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 define fastcc void @cat_domain(i8* nocapture %name, i8* nocapture %domain, i8**
6 nocapture %s) nounwind uwtable {
7 entry:
8 %call = tail call i64 @strlen(i8* %name) nounwind readonly
9 %call1 = tail call i64 @strlen(i8* %domain) nounwind readonly
10 %add = add i64 %call, 1
11 %add2 = add i64 %add, %call1
12 %add3 = add i64 %add2, 1
13 %call4 = tail call noalias i8* @malloc(i64 %add3) nounwind
14 store i8* %call4, i8** %s, align 8
15 %tobool = icmp eq i8* %call4, null
16 br i1 %tobool, label %return, label %if.end
17
18 if.end: ; preds = %entry
19 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %call4, i8* %name, i64 %call, i32 1, i1 false)
20 %arrayidx = getelementptr inbounds i8* %call4, i64 %call
21 store i8 46, i8* %arrayidx, align 1
22 ; CHECK: store i8 46
23 %add.ptr5 = getelementptr inbounds i8* %call4, i64 %add
24 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %add.ptr5, i8* %domain, i64 %call1, i32 1, i1 false)
25 %arrayidx8 = getelementptr inbounds i8* %call4, i64 %add2
26 store i8 0, i8* %arrayidx8, align 1
27 br label %return
28
29 return: ; preds = %if.end, %entry
30 ret void
31 }
32
33 declare i64 @strlen(i8* nocapture) nounwind readonly
34
35 declare noalias i8* @malloc(i64) nounwind
36
37 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind