Skip to content

Commit

Permalink
[InstCombine] Preserve all gep flags in gep of exact div fold
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jun 19, 2024
1 parent 7811330 commit 60984f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 3 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2941,10 +2941,9 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
match(GEP.getOperand(1),
m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
Builder.getInt8Ty(), GEP.getPointerOperand(), V);
NewGEP->setIsInBounds(GEP.isInBounds());
return NewGEP;
return GetElementPtrInst::Create(Builder.getInt8Ty(),
GEP.getPointerOperand(), V,
GEP.getNoWrapFlags());
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/Transforms/InstCombine/getelementptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,16 @@ define ptr @gep_sdiv_inbounds(ptr %p, i64 %off) {
ret ptr %ptr
}

define ptr @gep_sdiv_nuw(ptr %p, i64 %off) {
; CHECK-LABEL: @gep_sdiv_nuw(
; CHECK-NEXT: [[PTR:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
; CHECK-NEXT: ret ptr [[PTR]]
;
%index = sdiv exact i64 %off, 7
%ptr = getelementptr nuw %struct.C, ptr %p, i64 %index
ret ptr %ptr
}

define ptr @gep_ashr(ptr %p, i64 %off) {
; CHECK-LABEL: @gep_ashr(
; CHECK-NEXT: [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
Expand Down

0 comments on commit 60984f5

Please sign in to comment.