1 //===- llvm/unittest/CodeGen/AArch64SelectionDAGTest.cpp -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/CodeGen/SelectionDAG.h"
11 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/CodeGen/MachineModuleInfo.h"
14 #include "llvm/CodeGen/TargetLowering.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/TargetRegistry.h"
17 #include "llvm/Support/TargetSelect.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "gtest/gtest.h"
25 class AArch64SelectionDAGTest
: public testing::Test
{
27 static void SetUpTestCase() {
28 InitializeAllTargets();
29 InitializeAllTargetMCs();
32 void SetUp() override
{
33 StringRef Assembly
= "define void @f() { ret void }";
35 Triple
TargetTriple("aarch64--");
37 const Target
*T
= TargetRegistry::lookupTarget("", TargetTriple
, Error
);
38 // FIXME: These tests do not depend on AArch64 specifically, but we have to
39 // initialize a target. A skeleton Target for unittests would allow us to
40 // always run these tests.
44 TargetOptions Options
;
45 TM
= std::unique_ptr
<TargetMachine
>(T
->createTargetMachine(
46 "AArch64", "", "", Options
, None
, None
, CodeGenOpt::Aggressive
));
51 M
= parseAssemblyString(Assembly
, SMError
, Context
);
53 report_fatal_error(SMError
.getMessage());
54 M
->setDataLayout(TM
->createDataLayout());
56 F
= M
->getFunction("f");
58 report_fatal_error("F?");
60 MachineModuleInfo
MMI(TM
.get());
62 MF
= make_unique
<MachineFunction
>(*F
, *TM
, *TM
->getSubtargetImpl(*F
), 0,
65 DAG
= make_unique
<SelectionDAG
>(*TM
, CodeGenOpt::None
);
67 report_fatal_error("DAG?");
68 OptimizationRemarkEmitter
ORE(F
);
69 DAG
->init(*MF
, ORE
, nullptr, nullptr, nullptr);
73 std::unique_ptr
<TargetMachine
> TM
= nullptr;
74 std::unique_ptr
<Module
> M
;
76 std::unique_ptr
<MachineFunction
> MF
;
77 std::unique_ptr
<SelectionDAG
> DAG
;
80 TEST_F(AArch64SelectionDAGTest
, computeKnownBits_ZERO_EXTEND_VECTOR_INREG
) {
84 auto Int8VT
= EVT::getIntegerVT(Context
, 8);
85 auto Int16VT
= EVT::getIntegerVT(Context
, 16);
86 auto InVecVT
= EVT::getVectorVT(Context
, Int8VT
, 4);
87 auto OutVecVT
= EVT::getVectorVT(Context
, Int16VT
, 2);
88 auto InVec
= DAG
->getConstant(0, Loc
, InVecVT
);
89 auto Op
= DAG
->getZeroExtendVectorInReg(InVec
, Loc
, OutVecVT
);
90 auto DemandedElts
= APInt(4, 15);
92 DAG
->computeKnownBits(Op
, Known
, DemandedElts
);
93 EXPECT_TRUE(Known
.isZero());
96 TEST_F(AArch64SelectionDAGTest
, computeKnownBits_EXTRACT_SUBVECTOR
) {
100 auto IntVT
= EVT::getIntegerVT(Context
, 8);
101 auto VecVT
= EVT::getVectorVT(Context
, IntVT
, 3);
102 auto IdxVT
= EVT::getIntegerVT(Context
, 64);
103 auto Vec
= DAG
->getConstant(0, Loc
, VecVT
);
104 auto ZeroIdx
= DAG
->getConstant(0, Loc
, IdxVT
);
105 auto Op
= DAG
->getNode(ISD::EXTRACT_SUBVECTOR
, Loc
, VecVT
, Vec
, ZeroIdx
);
106 auto DemandedElts
= APInt(3, 7);
108 DAG
->computeKnownBits(Op
, Known
, DemandedElts
);
109 EXPECT_TRUE(Known
.isZero());
112 TEST_F(AArch64SelectionDAGTest
, ComputeNumSignBits_SIGN_EXTEND_VECTOR_INREG
) {
116 auto Int8VT
= EVT::getIntegerVT(Context
, 8);
117 auto Int16VT
= EVT::getIntegerVT(Context
, 16);
118 auto InVecVT
= EVT::getVectorVT(Context
, Int8VT
, 4);
119 auto OutVecVT
= EVT::getVectorVT(Context
, Int16VT
, 2);
120 auto InVec
= DAG
->getConstant(1, Loc
, InVecVT
);
121 auto Op
= DAG
->getSignExtendVectorInReg(InVec
, Loc
, OutVecVT
);
122 auto DemandedElts
= APInt(4, 15);
123 EXPECT_EQ(DAG
->ComputeNumSignBits(Op
, DemandedElts
), 15u);
126 TEST_F(AArch64SelectionDAGTest
, ComputeNumSignBits_EXTRACT_SUBVECTOR
) {
130 auto IntVT
= EVT::getIntegerVT(Context
, 8);
131 auto VecVT
= EVT::getVectorVT(Context
, IntVT
, 3);
132 auto IdxVT
= EVT::getIntegerVT(Context
, 64);
133 auto Vec
= DAG
->getConstant(1, Loc
, VecVT
);
134 auto ZeroIdx
= DAG
->getConstant(0, Loc
, IdxVT
);
135 auto Op
= DAG
->getNode(ISD::EXTRACT_SUBVECTOR
, Loc
, VecVT
, Vec
, ZeroIdx
);
136 auto DemandedElts
= APInt(3, 7);
137 EXPECT_EQ(DAG
->ComputeNumSignBits(Op
, DemandedElts
), 7u);
140 TEST_F(AArch64SelectionDAGTest
, SimplifyDemandedVectorElts_EXTRACT_SUBVECTOR
) {
144 TargetLowering
TL(*TM
);
147 auto IntVT
= EVT::getIntegerVT(Context
, 8);
148 auto VecVT
= EVT::getVectorVT(Context
, IntVT
, 3);
149 auto IdxVT
= EVT::getIntegerVT(Context
, 64);
150 auto Vec
= DAG
->getConstant(1, Loc
, VecVT
);
151 auto ZeroIdx
= DAG
->getConstant(0, Loc
, IdxVT
);
152 auto Op
= DAG
->getNode(ISD::EXTRACT_SUBVECTOR
, Loc
, VecVT
, Vec
, ZeroIdx
);
153 auto DemandedElts
= APInt(3, 7);
154 auto KnownUndef
= APInt(3, 0);
155 auto KnownZero
= APInt(3, 0);
156 TargetLowering::TargetLoweringOpt
TLO(*DAG
, false, false);
157 EXPECT_EQ(TL
.SimplifyDemandedVectorElts(Op
, DemandedElts
, KnownUndef
,
162 } // end anonymous namespace