return CV->getSplatValue();
if (const ConstantVector *CV = dyn_cast<ConstantVector>(this))
return CV->getSplatValue(AllowUndefs);
+
+ // Check if this is a constant expression splat of the form returned by
+ // ConstantVector::getSplat()
+ const auto *Shuf = dyn_cast<ConstantExpr>(this);
+ if (Shuf && Shuf->getOpcode() == Instruction::ShuffleVector &&
+ isa<UndefValue>(Shuf->getOperand(1))) {
+
+ const auto *IElt = dyn_cast<ConstantExpr>(Shuf->getOperand(0));
+ if (IElt && IElt->getOpcode() == Instruction::InsertElement &&
+ isa<UndefValue>(IElt->getOperand(0))) {
+
+ ArrayRef<int> Mask = Shuf->getShuffleMask();
+ Constant *SplatVal = IElt->getOperand(1);
+ ConstantInt *Index = dyn_cast<ConstantInt>(IElt->getOperand(2));
+
+ if (Index && Index->getValue() == 0 &&
+ std::all_of(Mask.begin(), Mask.end(), [](int I) { return I == 0; }))
+ return SplatVal;
+ }
+ }
+
return nullptr;
}
ret i32 %r
}
+; more complicated expressions
+
+define <vscale x 2 x i1> @cmp_le_smax_always_true(<vscale x 2 x i64> %x) {
+; CHECK-LABEL: @cmp_le_smax_always_true(
+; CHECK-NEXT: ret <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> undef, i1 true, i32 0), <vscale x 2 x i1> undef, <vscale x 2 x i32> zeroinitializer)
+ %cmp = icmp sle <vscale x 2 x i64> %x, shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> undef, i64 9223372036854775807, i32 0), <vscale x 2 x i64> undef, <vscale x 2 x i32> zeroinitializer)
+ ret <vscale x 2 x i1> %cmp
+}
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Memory Access and Addressing Operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
EXPECT_FALSE(CP00U->isElementWiseEqual(CP00U0));
}
+TEST(ConstantsTest, GetSplatValueRoundTrip) {
+ LLVMContext Context;
+
+ Type *FloatTy = Type::getFloatTy(Context);
+ Type *Int32Ty = Type::getInt32Ty(Context);
+ Type *Int8Ty = Type::getInt8Ty(Context);
+
+ for (unsigned Min : {1, 2, 8}) {
+ ElementCount SEC = {Min, true};
+ ElementCount FEC = {Min, false};
+
+ for (auto EC : {SEC, FEC}) {
+ for (auto *Ty : {FloatTy, Int32Ty, Int8Ty}) {
+ Constant *Zero = Constant::getNullValue(Ty);
+ Constant *One = Constant::getAllOnesValue(Ty);
+
+ for (auto *C : {Zero, One}) {
+ Constant *Splat = ConstantVector::getSplat(EC, C);
+ ASSERT_NE(nullptr, Splat);
+
+ Constant *SplatVal = Splat->getSplatValue();
+ EXPECT_NE(nullptr, SplatVal);
+ EXPECT_EQ(SplatVal, C);
+ }
+ }
+ }
+ }
+}
+
} // end anonymous namespace
} // end namespace llvm