Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c3eb3c7

Browse files
majnemereddyb
authored andcommitted
[InstSimplify] Fold gep (gep V, C), (xor V, -1) to C-1
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@278779 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent bd3e05c commit c3eb3c7

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

lib/Analysis/InstructionSimplify.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3597,7 +3597,6 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
35973597
}
35983598
}
35993599

3600-
// gep (gep V, C), (sub 0, V) -> C
36013600
if (Q.DL.getTypeAllocSize(LastType) == 1 &&
36023601
all_of(Ops.slice(1).drop_back(1),
36033602
[](Value *Idx) { return match(Idx, m_Zero()); })) {
@@ -3609,11 +3608,18 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
36093608
Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL,
36103609
BasePtrOffset);
36113610

3611+
// gep (gep V, C), (sub 0, V) -> C
36123612
if (match(Ops.back(),
36133613
m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) {
36143614
auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
36153615
return ConstantExpr::getIntToPtr(CI, GEPTy);
36163616
}
3617+
// gep (gep V, C), (xor V, -1) -> C-1
3618+
if (match(Ops.back(),
3619+
m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes()))) {
3620+
auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
3621+
return ConstantExpr::getIntToPtr(CI, GEPTy);
3622+
}
36173623
}
36183624
}
36193625

test/Transforms/InstSimplify/compare.ll

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,6 @@ define i1 @gep17() {
218218
; CHECK-NEXT: ret i1 true
219219
}
220220

221-
define i32 @gep18() {
222-
; CHECK-LABEL: @gep18(
223-
%alloca = alloca i32, align 4 ; alloca + 0
224-
%gep = getelementptr inbounds i32, i32* %alloca, i32 1 ; alloca + 4
225-
%bc = bitcast i32* %gep to [4 x i8]* ; alloca + 4
226-
%pti = ptrtoint i32* %alloca to i32 ; alloca
227-
%sub = sub i32 0, %pti ; -alloca
228-
%add = getelementptr [4 x i8], [4 x i8]* %bc, i32 0, i32 %sub ; alloca + 4 - alloca == 4
229-
%add_to_int = ptrtoint i8* %add to i32 ; 4
230-
ret i32 %add_to_int ; 4
231-
; CHECK-NEXT: ret i32 4
232-
}
233-
234221
define i1 @zext(i32 %x) {
235222
; CHECK-LABEL: @zext(
236223
%e1 = zext i32 %x to i64

0 commit comments

Comments
 (0)