llvm.org GIT mirror llvm / 8df7c39
IR: Don't constant fold GEP bitcasts between different address spaces PR15262 reported a bug where the following instruction: i8 getelementptr inbounds i8* bitcast ([4 x i8] addrspace(12)* @buf to i8*), i32 2 was getting folded into: addrspace(12)* getelementptr inbounds ([4 x i8] addrspace(12)* @buf, i32 0, i32 2) This caused instcombine to crash because the original instruction and the folded instruction have different types. The issue was fixed by disallowing bitcasts between different address spaces to be folded away. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@176156 91177308-0d34-0410-b5e6-96231b3b80d8 Meador Inge 6 years ago
2 changed file(s) with 42 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
19701970 }
19711971 }
19721972
1973 // Implement folding of:
1974 // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
1975 // i64 0, i64 0)
1976 // To: i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
1973 // Attempt to fold casts to the same type away. For example, folding:
19771974 //
1975 // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
1976 // i64 0, i64 0)
1977 // into:
1978 //
1979 // i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
1980 //
1981 // Don't fold if the cast is changing address spaces.
19781982 if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
1979 if (PointerType *SPT =
1980 dyn_cast(CE->getOperand(0)->getType()))
1981 if (ArrayType *SAT = dyn_cast(SPT->getElementType()))
1982 if (ArrayType *CAT =
1983 dyn_cast(cast(C->getType())->getElementType()))
1984 if (CAT->getElementType() == SAT->getElementType())
1985 return
1986 ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0),
1987 Idxs, inBounds);
1983 PointerType *SrcPtrTy =
1984 dyn_cast(CE->getOperand(0)->getType());
1985 PointerType *DstPtrTy = dyn_cast(CE->getType());
1986 if (SrcPtrTy && DstPtrTy) {
1987 ArrayType *SrcArrayTy =
1988 dyn_cast(SrcPtrTy->getElementType());
1989 ArrayType *DstArrayTy =
1990 dyn_cast(DstPtrTy->getElementType());
1991 if (SrcArrayTy && DstArrayTy
1992 && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
1993 && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
1994 return ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0),
1995 Idxs, inBounds);
1996 }
19881997 }
19891998 }
19901999
446446 ret i32* %t
447447 }
448448
449 ; PR15262 - Check GEP folding with casts between address spaces.
450
451 @p0 = global [4 x i8] zeroinitializer, align 1
452 @p12 = addrspace(12) global [4 x i8] zeroinitializer, align 1
453
454 define i8* @different_addrspace() nounwind noinline {
455 ; OPT: different_addrspace
456 %p = getelementptr inbounds i8* bitcast ([4 x i8] addrspace(12)* @p12 to i8*),
457 i32 2
458 ret i8* %p
459 ; OPT: ret i8* getelementptr (i8* bitcast ([4 x i8] addrspace(12)* @p12 to i8*), i32 2)
460 }
461
462 define i8* @same_addrspace() nounwind noinline {
463 ; OPT: same_addrspace
464 %p = getelementptr inbounds i8* bitcast ([4 x i8] * @p0 to i8*), i32 2
465 ret i8* %p
466 ; OPT: ret i8* getelementptr inbounds ([4 x i8]* @p0, i32 0, i32 2)
467 }
468
449469 ; CHECK: attributes #0 = { nounwind }