llvm.org GIT mirror llvm / 8976e59
fix PR5436 by making the 'simple' case of SRoA not promote out of range array indexes. The "complex" case of SRoA still handles them, and correctly. This fixes a weirdness where we'd correctly avoid transforming A[0][42] if the 42 was too large, but we'd only do it if it was one gep, not two separate ones. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@90007 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 69 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
468468 case Instruction::GetElementPtr: {
469469 GetElementPtrInst *GEP = cast(User);
470470 bool AreAllZeroIndices = isFirstElt;
471 if (GEP->getNumOperands() > 1) {
472 if (!isa(GEP->getOperand(1)) ||
473 !cast(GEP->getOperand(1))->isZero())
474 // Using pointer arithmetic to navigate the array.
475 return MarkUnsafe(Info);
476
477 if (AreAllZeroIndices)
478 AreAllZeroIndices = GEP->hasAllZeroIndices();
479 }
471 if (GEP->getNumOperands() > 1 &&
472 (!isa(GEP->getOperand(1)) ||
473 !cast(GEP->getOperand(1))->isZero()))
474 // Using pointer arithmetic to navigate the array.
475 return MarkUnsafe(Info);
476
477 // Verify that any array subscripts are in range.
478 for (gep_type_iterator GEPIt = gep_type_begin(GEP),
479 E = gep_type_end(GEP); GEPIt != E; ++GEPIt) {
480 // Ignore struct elements, no extra checking needed for these.
481 if (isa(*GEPIt))
482 continue;
483
484 // This GEP indexes an array. Verify that this is an in-range
485 // constant integer. Specifically, consider A[0][i]. We cannot know that
486 // the user isn't doing invalid things like allowing i to index an
487 // out-of-range subscript that accesses A[1]. Because of this, we have
488 // to reject SROA of any accesses into structs where any of the
489 // components are variables.
490 ConstantInt *IdxVal = dyn_cast(GEPIt.getOperand());
491 if (!IdxVal) return MarkUnsafe(Info);
492
493 // Are all indices still zero?
494 AreAllZeroIndices &= IdxVal->isZero();
495
496 if (const ArrayType *AT = dyn_cast(*GEPIt)) {
497 if (IdxVal->getZExtValue() >= AT->getNumElements())
498 return MarkUnsafe(Info);
499 } else if (const VectorType *VT = dyn_cast(*GEPIt)) {
500 if (IdxVal->getZExtValue() >= VT->getNumElements())
501 return MarkUnsafe(Info);
502 }
503 }
504
505
480506 isSafeElementUse(GEP, AreAllZeroIndices, AI, Info);
481507 if (Info.isUnsafe) return;
482508 break;
0 ; RUN: opt < %s -scalarrepl -S | FileCheck %s
1
2 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
3 target triple = "i386-pc-linux-gnu"
4
15
26 ; PR3466
37 ; Off end of array, don't transform.
48 define i32 @test1() {
59 ; CHECK: @test1
6 ; CHECK: %X = alloca
10 ; CHECK-NOT: = alloca
711 %X = alloca [4 x i32]
812 %Y = getelementptr [4 x i32]* %X, i64 0, i64 6 ; [#uses=2]
913 store i32 0, i32* %Y
1620 define i32 @test2() nounwind {
1721 entry:
1822 ; CHECK: @test2
19 ; CHECK: %yx2.i = alloca
23 ; CHECK-NOT: = alloca
2024 %yx2.i = alloca float, align 4 ; [#uses=1]
2125 %yx26.i = bitcast float* %yx2.i to i64* ; [#uses=1]
2226 %0 = load i64* %yx26.i, align 8 ; [#uses=0]
2327 unreachable
2428 }
29
30 %base = type { i32, [0 x i8] }
31 %padded = type { %base, [1 x i32] }
32
33 ; PR5436
34 define void @test3() {
35 entry:
36 ; CHECK: @test3
37 ; CHECK-NOT: = alloca
38 ; CHECK: store i64
39 %var_1 = alloca %padded, align 8 ; <%padded*> [#uses=3]
40 %0 = getelementptr inbounds %padded* %var_1, i32 0, i32 0 ; <%base*> [#uses=2]
41
42 %p2 = getelementptr inbounds %base* %0, i32 0, i32 1, i32 0 ; [#uses=1]
43 store i8 72, i8* %p2, align 1
44
45 ; 72 -> a[0].
46
47 %callret = call %padded *@test3f() ; [#uses=2]
48 %callretcast = bitcast %padded* %callret to i8* ; [#uses=1]
49 %var_11 = bitcast %padded* %var_1 to i8* ; [#uses=1]
50 call void @llvm.memcpy.i32(i8* %callretcast, i8* %var_11, i32 8, i32 4)
51 ret void
52 }
53
54 declare void @llvm.memcpy.i32(i8* nocapture, i8* nocapture, i32, i32) nounwind
55
56 declare %padded* @test3f()