Skip to content

Commit 3beda9e

Browse files
arsenmmahesh-attarde
authored andcommitted
InstCombine: Check GEP operand is available (llvm#160438)
Logic copied from the select case. Fixes llvm#160302
1 parent f42c203 commit 3beda9e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,18 @@ bool PointerReplacer::collectUsers() {
338338
if (!TryPushInstOperand(TrueInst) || !TryPushInstOperand(FalseInst))
339339
return false;
340340
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
341-
UsersToReplace.insert(GEP);
342-
PushUsersToWorklist(GEP);
341+
auto *PtrOp = dyn_cast<Instruction>(GEP->getPointerOperand());
342+
if (!PtrOp)
343+
return false;
344+
if (isAvailable(PtrOp)) {
345+
UsersToReplace.insert(GEP);
346+
PushUsersToWorklist(GEP);
347+
continue;
348+
}
349+
350+
Worklist.emplace_back(GEP);
351+
if (!TryPushInstOperand(PtrOp))
352+
return false;
343353
} else if (auto *MI = dyn_cast<MemTransferInst>(Inst)) {
344354
if (MI->isVolatile())
345355
return false;

llvm/test/Transforms/InstCombine/AMDGPU/ptr-replace-alloca.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,23 @@ bb:
109109
ret void
110110
}
111111

112+
@global = external addrspace(1) constant [16 x float], align 64
113+
114+
define float @issue160302(i1 %cond, ptr addrspace(5) %arg) {
115+
; CHECK-LABEL: define float @issue160302(
116+
; CHECK-SAME: i1 [[COND:%.*]], ptr addrspace(5) [[ARG:%.*]]) {
117+
; CHECK-NEXT: [[AGG_TMP2_I4:%.*]] = alloca [16 x float], align 64, addrspace(5)
118+
; CHECK-NEXT: [[SELECT_PTR:%.*]] = select i1 [[COND]], ptr addrspace(5) [[AGG_TMP2_I4]], ptr addrspace(5) [[ARG]]
119+
; CHECK-NEXT: [[COND_I:%.*]] = load float, ptr addrspace(5) [[SELECT_PTR]], align 4
120+
; CHECK-NEXT: ret float [[COND_I]]
121+
;
122+
%agg.tmp2.i4 = alloca [16 x float], align 64, addrspace(5)
123+
call void @llvm.memcpy.p5.p1.i64(ptr addrspace(5) %agg.tmp2.i4, ptr addrspace(1) @global, i64 0, i1 false)
124+
%m_Data.i14.i = getelementptr [16 x float], ptr addrspace(5) %agg.tmp2.i4, i32 0, i32 0
125+
%gep = getelementptr [16 x float], ptr addrspace(5) %arg, i32 0, i32 0
126+
%select.ptr = select i1 %cond, ptr addrspace(5) %m_Data.i14.i, ptr addrspace(5) %gep
127+
%cond.i = load float, ptr addrspace(5) %select.ptr, align 4
128+
ret float %cond.i
129+
}
130+
112131
declare void @llvm.memcpy.p5.p4.i64(ptr addrspace(5) noalias writeonly captures(none), ptr addrspace(4) noalias readonly captures(none), i64, i1 immarg) #0

0 commit comments

Comments
 (0)