llvm.org GIT mirror llvm / 70992ac
Handle big index in getelementptr instruction CodeGen incorrectly ignores (assert from APInt) constant index bigger than 2^64 in getelementptr instruction. This is a test and fix for that. Patch by Paweł Bylica! Reviewed By: rnk Subscribers: majnemer, rnk, mcrosier, resistor, llvm-commits Differential Revision: http://reviews.llvm.org/D8219 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231984 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 5 years ago
3 changed file(s) with 92 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
496496 OI != E; ++OI) {
497497 const Value *Idx = *OI;
498498 if (auto *StTy = dyn_cast(Ty)) {
499 unsigned Field = cast(Idx)->getZExtValue();
499 uint64_t Field = cast(Idx)->getZExtValue();
500500 if (Field) {
501501 // N = N + Offset
502502 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
517517 if (CI->isZero())
518518 continue;
519519 // N = N + Offset
520 TotalOffs +=
521 DL.getTypeAllocSize(Ty) * cast(CI)->getSExtValue();
520 uint64_t IdxN = CI->getValue().sextOrTrunc(64).getSExtValue();
521 TotalOffs += DL.getTypeAllocSize(Ty) * IdxN;
522522 if (TotalOffs >= MaxOffs) {
523523 N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
524524 if (!N) // Unhandled operand. Halt "fast" selection and bail.
34473447 Ty = StTy->getElementType(Field);
34483448 } else {
34493449 Ty = cast(Ty)->getElementType();
3450 MVT PtrTy = DAG.getTargetLoweringInfo().getPointerTy(AS);
3451 unsigned PtrSize = PtrTy.getSizeInBits();
3452 APInt ElementSize(PtrSize, DL->getTypeAllocSize(Ty));
34503453
34513454 // If this is a constant subscript, handle it quickly.
3452 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3453 if (const ConstantInt *CI = dyn_cast(Idx)) {
3454 if (CI->isZero()) continue;
3455 uint64_t Offs =
3456 DL->getTypeAllocSize(Ty)*cast(CI)->getSExtValue();
3457 SDValue OffsVal;
3458 EVT PTy = TLI.getPointerTy(AS);
3459 unsigned PtrBits = PTy.getSizeInBits();
3460 if (PtrBits < 64)
3461 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
3462 DAG.getConstant(Offs, MVT::i64));
3463 else
3464 OffsVal = DAG.getConstant(Offs, PTy);
3465
3466 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
3467 OffsVal);
3455 if (const auto *CI = dyn_cast(Idx)) {
3456 if (CI->isZero())
3457 continue;
3458 APInt Offs = ElementSize * CI->getValue().sextOrTrunc(PtrSize);
3459 SDValue OffsVal = DAG.getConstant(Offs, PtrTy);
3460 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, OffsVal);
34683461 continue;
34693462 }
34703463
34713464 // N = N + Idx * ElementSize;
3472 APInt ElementSize =
3473 APInt(TLI.getPointerSizeInBits(AS), DL->getTypeAllocSize(Ty));
34743465 SDValue IdxN = getValue(Idx);
34753466
34763467 // If the index is smaller or larger than intptr_t, truncate or extend
0 ; RUN: llc < %s -O0 -march=x86
1 ; RUN: llc < %s -O0 -march=x86-64
2 ; RUN: llc < %s -O2 -march=x86
3 ; RUN: llc < %s -O2 -march=x86-64
4
5
6 ; Test big index trunc to pointer size:
7
8 define i8* @test_trunc65(i8* %ptr) nounwind {
9 ; CHECK-LABEL: test_trunc65
10 ; CHECK: 3
11 %d = getelementptr i8, i8* %ptr, i65 18446744073709551619 ; 2^64 + 3
12 ret i8* %d
13 }
14
15 define i8* @test_trunc128(i8* %ptr) nounwind {
16 ; CHECK-LABEL: test_trunc128
17 ; CHECK: 5
18 %d = getelementptr i8, i8* %ptr, i128 18446744073709551621 ; 2^64 + 5
19 ret i8* %d
20 }
21
22 define i8* @test_trunc160(i8* %ptr) nounwind {
23 ; CHECK-LABEL: test_trunc160
24 ; CHECK: 8
25 %d = getelementptr i8, i8* %ptr, i160 18446744073709551624 ; 2^64 + 8
26 ret i8* %d
27 }
28
29 define i8* @test_trunc256(i8* %ptr) nounwind {
30 ; CHECK-LABEL: test_trunc256
31 ; CHECK: 13
32 %d = getelementptr i8, i8* %ptr, i256 18446744073709551629 ; 2^64 + 13
33 ret i8* %d
34 }
35
36 define i8* @test_trunc2048(i8* %ptr) nounwind {
37 ; CHECK-LABEL: test_trunc2048
38 ; CHECK: 21
39 %d = getelementptr i8, i8* %ptr, i2048 18446744073709551637 ; 2^64 + 21
40 ret i8* %d
41 }
42
43
44 ; Test small index sext to pointer size
45
46 define i8* @test_sext3(i8* %ptr) nounwind {
47 ; CHECK-LABEL: test_sext3
48 ; CHECK: -3
49 %d = getelementptr i8, i8* %ptr, i3 -3
50 ret i8* %d
51 }
52
53 define i8* @test_sext5(i8* %ptr) nounwind {
54 ; CHECK-LABEL: test_sext5
55 ; CHECK: -5
56 %d = getelementptr i8, i8* %ptr, i5 -5
57 ret i8* %d
58 }
59
60 define i8* @test_sext8(i8* %ptr) nounwind {
61 ; CHECK-LABEL: test_sext8
62 ; CHECK: -8
63 %d = getelementptr i8, i8* %ptr, i8 -8
64 ret i8* %d
65 }
66
67 define i8* @test_sext13(i8* %ptr) nounwind {
68 ; CHECK-LABEL: test_sext13
69 ; CHECK: -13
70 %d = getelementptr i8, i8* %ptr, i8 -13
71 ret i8* %d
72 }
73
74 define i8* @test_sext16(i8* %ptr) nounwind {
75 ; CHECK-LABEL: test_sext16
76 ; CHECK: -21
77 %d = getelementptr i8, i8* %ptr, i8 -21
78 ret i8* %d
79 }