summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp
diff options
context:
space:
mode:
authorRoman Tereshin <rtereshin@apple.com>2019-12-12 08:35:16 -0800
committerRoman Tereshin <rtereshin@apple.com>2019-12-13 15:45:18 -0800
commit18bf9670aac901f71bc43ac55c5feb603555a1f6 (patch)
tree483445b4e99a972aad95fb0b75b54332493f7ca6 /llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp
parent8207c81597adab5a06b50339e5ee2891f6e453bf (diff)
downloadbcm5719-llvm-18bf9670aac901f71bc43ac55c5feb603555a1f6.tar.gz
bcm5719-llvm-18bf9670aac901f71bc43ac55c5feb603555a1f6.zip
[Legalizer] Refactoring out legalizeMachineFunction
and introducing new unittests/CodeGen/GlobalISel/LegalizerTest.cpp relying on it to unit test the entire legalizer algorithm (including the top-level main loop). See also https://reviews.llvm.org/D71448
Diffstat (limited to 'llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp')
-rw-r--r--llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp b/llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp
new file mode 100644
index 00000000000..7bb348d3046
--- /dev/null
+++ b/llvm/unittests/CodeGen/GlobalISel/LegalizerTest.cpp
@@ -0,0 +1,79 @@
+//===- LegalizerTest.cpp --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "GISelMITest.h"
+#include "llvm/CodeGen/GlobalISel/Legalizer.h"
+
+using namespace LegalizeActions;
+using namespace LegalizeMutations;
+using namespace LegalityPredicates;
+
+namespace {
+
+::testing::AssertionResult isNullMIPtr(const MachineInstr *MI) {
+ if (MI == nullptr)
+ return ::testing::AssertionSuccess();
+ std::string MIBuffer;
+ raw_string_ostream MISStream(MIBuffer);
+ MI->print(MISStream, /*IsStandalone=*/true, /*SkipOpers=*/false,
+ /*SkipDebugLoc=*/false, /*AddNewLine=*/false);
+ return ::testing::AssertionFailure()
+ << "unable to legalize instruction: " << MISStream.str();
+}
+
+TEST_F(GISelMITest, BasicLegalizerTest) {
+ StringRef MIRString = R"(
+ %vptr:_(p0) = COPY $x4
+ %v:_(<2 x s8>) = G_LOAD %vptr:_(p0) :: (load 2, align 1)
+ $h4 = COPY %v:_(<2 x s8>)
+ )";
+ setUp(MIRString.rtrim(' '));
+ if (!TM)
+ return;
+
+ DefineLegalizerInfo(ALegalizer, {
+ auto p0 = LLT::pointer(0, 64);
+ auto v2s8 = LLT::vector(2, 8);
+ auto v2s16 = LLT::vector(2, 16);
+ getActionDefinitionsBuilder(G_LOAD)
+ .legalForTypesWithMemDesc({{s16, p0, 8, 8}})
+ .scalarize(0)
+ .clampScalar(0, s16, s16);
+ getActionDefinitionsBuilder(G_PTR_ADD).legalFor({{p0, s64}});
+ getActionDefinitionsBuilder(G_CONSTANT).legalFor({s64});
+ getActionDefinitionsBuilder(G_BUILD_VECTOR)
+ .legalFor({{v2s16, s16}})
+ .clampScalar(1, s16, s16);
+ getActionDefinitionsBuilder(G_BUILD_VECTOR_TRUNC).legalFor({{v2s8, s16}});
+ getActionDefinitionsBuilder(G_ANYEXT).legalFor({{s32, s16}});
+ });
+
+ ALegalizerInfo LI(MF->getSubtarget());
+
+ Legalizer::MFResult Result =
+ Legalizer::legalizeMachineFunction(*MF, LI, {}, B);
+
+ EXPECT_TRUE(isNullMIPtr(Result.FailedOn));
+ EXPECT_TRUE(Result.Changed);
+
+ StringRef CheckString = R"(
+ CHECK: %vptr:_(p0) = COPY $x4
+ CHECK-NEXT: [[LOAD_0:%[0-9]+]]:_(s16) = G_LOAD %vptr:_(p0) :: (load 1)
+ CHECK-NEXT: [[OFFSET_1:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
+ CHECK-NEXT: [[VPTR_1:%[0-9]+]]:_(p0) = G_PTR_ADD %vptr:_, [[OFFSET_1]]:_(s64)
+ CHECK-NEXT: [[LOAD_1:%[0-9]+]]:_(s16) = G_LOAD [[VPTR_1]]:_(p0) :: (load 1)
+ CHECK-NEXT: [[V0:%[0-9]+]]:_(s16) = COPY [[LOAD_0]]:_(s16)
+ CHECK-NEXT: [[V1:%[0-9]+]]:_(s16) = COPY [[LOAD_1]]:_(s16)
+ CHECK-NEXT: %v:_(<2 x s8>) = G_BUILD_VECTOR_TRUNC [[V0]]:_(s16), [[V1]]:_(s16)
+ CHECK-NEXT: $h4 = COPY %v:_(<2 x s8>)
+ )";
+
+ EXPECT_TRUE(CheckMachineFunction(*MF, CheckString)) << *MF;
+}
+
+} // namespace
OpenPOWER on IntegriCloud