forked from OSchip/llvm-project
[InstCombine] Fold gep inbounds of null to null
Effectively, this is what we were previously already doing when the GEP was used in conjunction with a load or store, but this fold can also be applied more generally: > The only in bounds address for a null pointer in the default > address-space is the null pointer itself.
This commit is contained in:
parent
87087a02ae
commit
eb79fd3c92
|
@ -903,29 +903,14 @@ static Instruction *replaceGEPIdxWithZero(InstCombinerImpl &IC, Value *Ptr,
|
|||
}
|
||||
|
||||
static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
|
||||
if (NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()))
|
||||
return false;
|
||||
|
||||
auto *Ptr = SI.getPointerOperand();
|
||||
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
|
||||
if (GEPI->isInBounds())
|
||||
Ptr = GEPI->getOperand(0);
|
||||
return (isa<ConstantPointerNull>(Ptr) &&
|
||||
!NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()));
|
||||
return isa<ConstantPointerNull>(SI.getPointerOperand()) &&
|
||||
!NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace());
|
||||
}
|
||||
|
||||
static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
|
||||
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
|
||||
const Value *GEPI0 = GEPI->getOperand(0);
|
||||
if (isa<ConstantPointerNull>(GEPI0) && GEPI->isInBounds() &&
|
||||
!NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace()))
|
||||
return true;
|
||||
}
|
||||
if (isa<UndefValue>(Op) ||
|
||||
(isa<ConstantPointerNull>(Op) &&
|
||||
!NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace())))
|
||||
return true;
|
||||
return false;
|
||||
return isa<UndefValue>(Op) ||
|
||||
(isa<ConstantPointerNull>(Op) &&
|
||||
!NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace()));
|
||||
}
|
||||
|
||||
Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
|
||||
|
|
|
@ -1855,6 +1855,12 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
|
|||
|
||||
Value *PtrOp = GEP.getOperand(0);
|
||||
|
||||
// The only pointer that is inbounds of null is null.
|
||||
if (isa<ConstantPointerNull>(PtrOp) && GEP.isInBounds() &&
|
||||
!NullPointerIsDefined(GEP.getFunction(),
|
||||
PtrOp->getType()->getPointerAddressSpace()))
|
||||
return replaceInstUsesWith(GEP, PtrOp);
|
||||
|
||||
// Eliminate unneeded casts for indices, and replace indices which displace
|
||||
// by multiples of a zero size type with zero.
|
||||
bool MadeChange = false;
|
||||
|
|
|
@ -1239,8 +1239,7 @@ define i32* @PR45084_extra_use(i1 %cond, %struct.f** %p) {
|
|||
|
||||
define i8* @gep_null_inbounds(i64 %idx) {
|
||||
; CHECK-LABEL: @gep_null_inbounds(
|
||||
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* null, i64 [[IDX:%.*]]
|
||||
; CHECK-NEXT: ret i8* [[GEP]]
|
||||
; CHECK-NEXT: ret i8* null
|
||||
;
|
||||
%gep = getelementptr inbounds i8, i8* null, i64 %idx
|
||||
ret i8* %gep
|
||||
|
|
|
@ -25,8 +25,7 @@ define void @test2(i32* %P) {
|
|||
|
||||
define void @store_at_gep_off_null_inbounds(i64 %offset) {
|
||||
; CHECK-LABEL: @store_at_gep_off_null_inbounds(
|
||||
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds i32, i32* null, i64 [[OFFSET:%.*]]
|
||||
; CHECK-NEXT: store i32 undef, i32* [[PTR]], align 4
|
||||
; CHECK-NEXT: store i32 undef, i32* null, align 536870912
|
||||
; CHECK-NEXT: ret void
|
||||
;
|
||||
%ptr = getelementptr inbounds i32, i32 *null, i64 %offset
|
||||
|
|
Loading…
Reference in New Issue