Skip to content

Commit 26d4afc

Browse files
authored
[InstCombine] Fold gep of exact unsigned division (#82334)
Extend the transform added in #76458 to also handle unsigned division. X exact/ Y * Y == X holds independently of whether the division is signed or unsigned. Proofs: https://alive2.llvm.org/ce/z/wFd5Ec
1 parent f122268 commit 26d4afc

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

Diff for: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2615,10 +2615,10 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
26152615
Value *V;
26162616
if ((has_single_bit(TyAllocSize) &&
26172617
match(GEP.getOperand(1),
2618-
m_Exact(m_AShr(m_Value(V),
2619-
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
2618+
m_Exact(m_Shr(m_Value(V),
2619+
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
26202620
match(GEP.getOperand(1),
2621-
m_Exact(m_SDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
2621+
m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
26222622
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
26232623
Builder.getInt8Ty(), GEP.getPointerOperand(), V);
26242624
NewGEP->setIsInBounds(GEP.isInBounds());

Diff for: llvm/test/Transforms/InstCombine/getelementptr.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -1477,8 +1477,7 @@ define ptr @gep_sdiv(ptr %p, i64 %off) {
14771477

14781478
define ptr @gep_udiv(ptr %p, i64 %off) {
14791479
; CHECK-LABEL: @gep_udiv(
1480-
; CHECK-NEXT: [[INDEX:%.*]] = udiv exact i64 [[OFF:%.*]], 7
1481-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr [[STRUCT_C:%.*]], ptr [[P:%.*]], i64 [[INDEX]]
1480+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
14821481
; CHECK-NEXT: ret ptr [[PTR]]
14831482
;
14841483
%index = udiv exact i64 %off, 7
@@ -1518,8 +1517,7 @@ define ptr @gep_ashr(ptr %p, i64 %off) {
15181517

15191518
define ptr @gep_lshr(ptr %p, i64 %off) {
15201519
; CHECK-LABEL: @gep_lshr(
1521-
; CHECK-NEXT: [[INDEX:%.*]] = lshr exact i64 [[OFF:%.*]], 2
1522-
; CHECK-NEXT: [[PTR:%.*]] = getelementptr i32, ptr [[P:%.*]], i64 [[INDEX]]
1520+
; CHECK-NEXT: [[PTR:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[OFF:%.*]]
15231521
; CHECK-NEXT: ret ptr [[PTR]]
15241522
;
15251523
%index = lshr exact i64 %off, 2

0 commit comments

Comments
 (0)