llvm.org GIT mirror llvm / ec52f31
Merge r231984: 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/branches/release_36@236108 91177308-0d34-0410-b5e6-96231b3b80d8 Pawel Bylica 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.
33983398 Ty = StTy->getElementType(Field);
33993399 } else {
34003400 Ty = cast(Ty)->getElementType();
3401 MVT PtrTy = DAG.getTargetLoweringInfo().getPointerTy(AS);
3402 unsigned PtrSize = PtrTy.getSizeInBits();
3403 APInt ElementSize(PtrSize, DL->getTypeAllocSize(Ty));
34013404
34023405 // If this is a constant subscript, handle it quickly.
3403 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3404 if (const ConstantInt *CI = dyn_cast(Idx)) {
3405 if (CI->isZero()) continue;
3406 uint64_t Offs =
3407 DL->getTypeAllocSize(Ty)*cast(CI)->getSExtValue();
3408 SDValue OffsVal;
3409 EVT PTy = TLI.getPointerTy(AS);
3410 unsigned PtrBits = PTy.getSizeInBits();
3411 if (PtrBits < 64)
3412 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
3413 DAG.getConstant(Offs, MVT::i64));
3414 else
3415 OffsVal = DAG.getConstant(Offs, PTy);
3416
3417 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
3418 OffsVal);
3406 if (const auto *CI = dyn_cast(Idx)) {
3407 if (CI->isZero())
3408 continue;
3409 APInt Offs = ElementSize * CI->getValue().sextOrTrunc(PtrSize);
3410 SDValue OffsVal = DAG.getConstant(Offs, PtrTy);
3411 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N, OffsVal);
34193412 continue;
34203413 }
34213414
34223415 // N = N + Idx * ElementSize;
3423 APInt ElementSize =
3424 APInt(TLI.getPointerSizeInBits(AS), DL->getTypeAllocSize(Ty));
34253416 SDValue IdxN = getValue(Idx);
34263417
34273418 // 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 }