Skip to content

Commit ebc589e

Browse files
nikicllvmbot
authored andcommitted
[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 (cherry picked from commit 26d4afc)
1 parent c74afe6 commit ebc589e

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2594,10 +2594,10 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
25942594
Value *V;
25952595
if ((has_single_bit(TyAllocSize) &&
25962596
match(GEP.getOperand(1),
2597-
m_Exact(m_AShr(m_Value(V),
2598-
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
2597+
m_Exact(m_Shr(m_Value(V),
2598+
m_SpecificInt(countr_zero(TyAllocSize)))))) ||
25992599
match(GEP.getOperand(1),
2600-
m_Exact(m_SDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
2600+
m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
26012601
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
26022602
Builder.getInt8Ty(), GEP.getPointerOperand(), V);
26032603
NewGEP->setIsInBounds(GEP.isInBounds());

llvm/test/Transforms/InstCombine/getelementptr.ll

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

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

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

0 commit comments

Comments
 (0)