llvm.org GIT mirror llvm / 37abc48
Make Value::isDereferenceablePointer() handle unreachable code blocks. (This returns false in the event the computation feeding into the pointer is unreachable, which maybe ought to be true -- but this is at least consistent with undef->isDereferenceablePointer().) Fixes PR11825! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148671 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
2 changed file(s) with 45 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
348348
349349 /// isDereferenceablePointer - Test if this value is always a pointer to
350350 /// allocated and suitably aligned memory for a simple load or store.
351 bool Value::isDereferenceablePointer() const {
351 static bool isDereferenceablePointer(const Value *V,
352 SmallPtrSet &Visited) {
352353 // Note that it is not safe to speculate into a malloc'd region because
353354 // malloc may return null.
354355 // It's also not always safe to follow a bitcast, for example:
357358 // be handled using TargetData to check sizes and alignments though.
358359
359360 // These are obviously ok.
360 if (isa(this)) return true;
361 if (isa(V)) return true;
361362
362363 // Global variables which can't collapse to null are ok.
363 if (const GlobalVariable *GV = dyn_cast(this))
364 if (const GlobalVariable *GV = dyn_cast(V))
364365 return !GV->hasExternalWeakLinkage();
365366
366367 // byval arguments are ok.
367 if (const Argument *A = dyn_cast(this))
368 if (const Argument *A = dyn_cast(V))
368369 return A->hasByValAttr();
369
370
370371 // For GEPs, determine if the indexing lands within the allocated object.
371 if (const GEPOperator *GEP = dyn_cast(this)) {
372 if (const GEPOperator *GEP = dyn_cast(V)) {
372373 // Conservatively require that the base pointer be fully dereferenceable.
373 if (!GEP->getOperand(0)->isDereferenceablePointer())
374 if (!Visited.insert(GEP->getOperand(0)))
375 return false;
376 if (!isDereferenceablePointer(GEP->getOperand(0), Visited))
374377 return false;
375378 // Check the indices.
376379 gep_type_iterator GTI = gep_type_begin(GEP);
404407 return false;
405408 }
406409
410 /// isDereferenceablePointer - Test if this value is always a pointer to
411 /// allocated and suitably aligned memory for a simple load or store.
412 bool Value::isDereferenceablePointer() const {
413 SmallPtrSet Visited;
414 return ::isDereferenceablePointer(this, Visited);
415 }
416
407417 /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
408418 /// return the value in the PHI node corresponding to PredBB. If not, return
409419 /// ourself. This is useful if you want to know the value something has in a
0 ; RUN: opt -simplifycfg < %s -disable-output
1
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"
3 target triple = "x86_64-pc-linux-gnu"
4
5 ; PR11825
6 define void @test1() {
7 entry:
8 br label %return
9
10 while_block: ; preds = %and_if_cont2, %and_if_cont
11 %newlen = sub i32 %newlen, 1
12 %newptr = getelementptr i8* %newptr, i64 1
13 %test = icmp sgt i32 %newlen, 0
14 br i1 %test, label %and_if1, label %and_if_cont2
15
16 and_if1: ; preds = %while_block
17 %char = load i8* %newptr
18 %test2 = icmp ule i8 %char, 32
19 br label %and_if_cont2
20
21 and_if_cont2: ; preds = %and_if1, %while_block
22 %a18 = phi i1 [ %test, %while_block ], [ %test2, %and_if1 ]
23 br i1 %a18, label %while_block, label %return
24
25 return: ; preds = %and_if_cont2, %and_if_cont
26 ret void
27 }