Skip to content

Commit 4872ecf

Browse files
[LLVM][IR] Teach extractelement folds about constant ConstantInt/FP. (#116793)
1 parent d7d6fb1 commit 4872ecf

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

llvm/lib/IR/Constants.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,10 @@ Constant *Constant::getSplatValue(bool AllowPoison) const {
16991699
assert(this->getType()->isVectorTy() && "Only valid for vectors!");
17001700
if (isa<ConstantAggregateZero>(this))
17011701
return getNullValue(cast<VectorType>(getType())->getElementType());
1702+
if (auto *CI = dyn_cast<ConstantInt>(this))
1703+
return ConstantInt::get(getContext(), CI->getValue());
1704+
if (auto *CFP = dyn_cast<ConstantFP>(this))
1705+
return ConstantFP::get(getContext(), CFP->getValue());
17021706
if (const ConstantDataVector *CV = dyn_cast<ConstantDataVector>(this))
17031707
return CV->getSplatValue();
17041708
if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))

llvm/lib/IR/Instructions.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,8 +1752,17 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
17521752
if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
17531753
return true;
17541754

1755+
// NOTE: Through vector ConstantInt we have the potential to support more
1756+
// than just zero splat masks but that requires a LangRef change.
1757+
if (isa<ScalableVectorType>(MaskTy))
1758+
return false;
1759+
1760+
unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
1761+
1762+
if (const auto *CI = dyn_cast<ConstantInt>(Mask))
1763+
return !CI->uge(V1Size * 2);
1764+
17551765
if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
1756-
unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
17571766
for (Value *Op : MV->operands()) {
17581767
if (auto *CI = dyn_cast<ConstantInt>(Op)) {
17591768
if (CI->uge(V1Size*2))
@@ -1766,7 +1775,6 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
17661775
}
17671776

17681777
if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
1769-
unsigned V1Size = cast<FixedVectorType>(V1->getType())->getNumElements();
17701778
for (unsigned i = 0, e = cast<FixedVectorType>(MaskTy)->getNumElements();
17711779
i != e; ++i)
17721780
if (CDS->getElementAsInteger(i) >= V1Size*2)

llvm/test/Transforms/InstCombine/extractelement.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
; RUN: opt < %s -passes=instcombine -S -data-layout="E-n64" | FileCheck %s --check-prefixes=ANY,ANYBE,BE64
55
; RUN: opt < %s -passes=instcombine -S -data-layout="E-n128" | FileCheck %s --check-prefixes=ANY,ANYBE,BE128
66

7+
; RUN: opt < %s -passes=instcombine -S -data-layout="e-n64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ANY,ANYLE,LE64
8+
; RUN: opt < %s -passes=instcombine -S -data-layout="e-n128" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ANY,ANYLE,LE128
9+
; RUN: opt < %s -passes=instcombine -S -data-layout="E-n64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ANY,ANYBE,BE64
10+
; RUN: opt < %s -passes=instcombine -S -data-layout="E-n128" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ANY,ANYBE,BE128
11+
712
define i32 @extractelement_out_of_range(<2 x i32> %x) {
813
; ANY-LABEL: @extractelement_out_of_range(
914
; ANY-NEXT: ret i32 poison

llvm/test/Transforms/InstSimplify/extract-element.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
3+
; RUN: opt < %s -passes=instsimplify -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s
34

45
; Weird Types
56

0 commit comments

Comments
 (0)