Skip to content

Commit

Permalink
[InstCombine] GEPOperator::accumulateConstantOffset does not support …
Browse files Browse the repository at this point in the history
…scalable vectors

Avoid transforming:

 %0 = bitcast i8* %base to <vscale x 16 x i8>*
 %1 = getelementptr <vscale x 16 x i8>, <vscale x 16 x i8>* %0, i64 1

into:

 %0 = getelementptr i8, i8* %base, i64 16
 %1 = bitcast i8* %0 to <vscale x 16 x i8>*

Reviewers: efriedma, ctetreau

Reviewed By: efriedma

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D76236
  • Loading branch information
sdesmalen-arm committed Mar 18, 2020
1 parent c218664 commit ef64ba8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/IR/Operator.cpp
Expand Up @@ -45,6 +45,11 @@ bool GEPOperator::accumulateConstantOffset(const DataLayout &DL,
if (OpC->isZero())
continue;

// Scalable vectors have are multiplied by a runtime constant.
if (auto *VecTy = dyn_cast<VectorType>(GTI.getIndexedType()))
if (VecTy->isScalable())
return false;

// Handle a struct index, which adds its field offset to the pointer.
if (StructType *STy = GTI.getStructTypeOrNull()) {
unsigned ElementIdx = OpC->getZExtValue();
Expand Down
21 changes: 21 additions & 0 deletions llvm/test/Transforms/InstCombine/gep-vector.ll
Expand Up @@ -134,3 +134,24 @@ define i32 addrspace(3)* @inbounds_bitcast_vec_to_array_addrspace_matching_alloc
%gep = getelementptr inbounds [4 x i32], [4 x i32] addrspace(3)* %asc, i64 %y, i64 %z
ret i32 addrspace(3)* %gep
}

; Negative test - avoid doing bitcast on i8*, because '16' should be scaled by 'vscale'.

define i8* @test_accumulate_constant_offset_vscale_nonzero(<vscale x 16 x i1> %pg, i8* %base) {
; CHECK-LABEL: @test_accumulate_constant_offset_vscale_nonzero
; CHECK-NEXT: %bc = bitcast i8* %base to <vscale x 16 x i8>*
; CHECK-NEXT: %gep = getelementptr <vscale x 16 x i8>, <vscale x 16 x i8>* %bc, i64 1, i64 4
; CHECK-NEXT: ret i8* %gep
%bc = bitcast i8* %base to <vscale x 16 x i8>*
%gep = getelementptr <vscale x 16 x i8>, <vscale x 16 x i8>* %bc, i64 1, i64 4
ret i8* %gep
}

define i8* @test_accumulate_constant_offset_vscale_zero(<vscale x 16 x i1> %pg, i8* %base) {
; CHECK-LABEL: @test_accumulate_constant_offset_vscale_zero
; CHECK-NEXT: %[[RES:.*]] = getelementptr i8, i8* %base, i64 4
; CHECK-NEXT: ret i8* %[[RES]]
%bc = bitcast i8* %base to <vscale x 16 x i8>*
%gep = getelementptr <vscale x 16 x i8>, <vscale x 16 x i8>* %bc, i64 0, i64 4
ret i8* %gep
}

0 comments on commit ef64ba8

Please sign in to comment.