1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 19:12:56 +02:00

[InstCombine] Infer inbounds on geps of allocas

llvm-svn: 277950
This commit is contained in:
David Majnemer 2016-08-07 07:58:00 +00:00
parent 9af3384b86
commit 18c9d7e47c
2 changed files with 22 additions and 3 deletions

View File

@ -1899,6 +1899,25 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
}
}
if (!GEP.isInBounds()) {
unsigned PtrWidth =
DL.getPointerSizeInBits(PtrOp->getType()->getPointerAddressSpace());
APInt BasePtrOffset(PtrWidth, 0);
Value *UnderlyingPtrOp =
PtrOp->stripAndAccumulateInBoundsConstantOffsets(DL,
BasePtrOffset);
if (auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
BasePtrOffset.isNonNegative()) {
APInt AllocSize(PtrWidth, DL.getTypeAllocSize(AI->getAllocatedType()));
if (BasePtrOffset.ule(AllocSize)) {
return GetElementPtrInst::CreateInBounds(
PtrOp, makeArrayRef(Ops).slice(1), GEP.getName());
}
}
}
}
return nullptr;
}

View File

@ -367,7 +367,7 @@ define i32 @test21() {
%rval = load i32, i32* %pbobel
ret i32 %rval
; CHECK-LABEL: @test21(
; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0
; CHECK: getelementptr inbounds %intstruct, %intstruct* %pbob1, i64 0, i32 0
}
@ -541,8 +541,8 @@ define i8* @test32(i8* %v) {
%G = load i8*, i8** %F
ret i8* %G
; CHECK-LABEL: @test32(
; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
; CHECK: %D = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
; CHECK: %F = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
}
; PR3290