1 //===- LegalizerTest.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
10 #include "GISelMITest.h"
11 #include "llvm/CodeGen/GlobalISel/LostDebugLocObserver.h"
12
13 #define DEBUG_TYPE "legalizer-test"
14
15 using namespace LegalizeActions;
16 using namespace LegalizeMutations;
17 using namespace LegalityPredicates;
18
19 namespace {
20
isNullMIPtr(const MachineInstr * MI)21 ::testing::AssertionResult isNullMIPtr(const MachineInstr *MI) {
22 if (MI == nullptr)
23 return ::testing::AssertionSuccess();
24 std::string MIBuffer;
25 raw_string_ostream MISStream(MIBuffer);
26 MI->print(MISStream, /*IsStandalone=*/true, /*SkipOpers=*/false,
27 /*SkipDebugLoc=*/false, /*AddNewLine=*/false);
28 return ::testing::AssertionFailure()
29 << "unable to legalize instruction: " << MISStream.str();
30 }
31
32 DefineLegalizerInfo(ALegalizer, {
33 auto p0 = LLT::pointer(0, 64);
34 auto s8 = LLT::scalar(8);
35 auto v2s8 = LLT::fixed_vector(2, 8);
36 auto v2s16 = LLT::fixed_vector(2, 16);
37 getActionDefinitionsBuilder(G_LOAD)
38 .legalForTypesWithMemDesc({{s16, p0, s8, 8}})
39 .scalarize(0)
40 .clampScalar(0, s16, s16);
41 getActionDefinitionsBuilder(G_PTR_ADD).legalFor({{p0, s64}});
42 getActionDefinitionsBuilder(G_CONSTANT).legalFor({s32, s64});
43 getActionDefinitionsBuilder(G_BUILD_VECTOR)
44 .legalFor({{v2s16, s16}})
45 .clampScalar(1, s16, s16);
46 getActionDefinitionsBuilder(G_BUILD_VECTOR_TRUNC).legalFor({{v2s8, s16}});
47 getActionDefinitionsBuilder(G_ANYEXT).legalFor({{s32, s16}});
48 getActionDefinitionsBuilder(G_ZEXT).legalFor({{s32, s16}});
49 getActionDefinitionsBuilder(G_SEXT).legalFor({{s32, s16}});
50 getActionDefinitionsBuilder(G_AND).legalFor({s32});
51 getActionDefinitionsBuilder(G_SEXT_INREG).lower();
52 getActionDefinitionsBuilder(G_ASHR).legalFor({{s32, s32}});
53 getActionDefinitionsBuilder(G_SHL).legalFor({{s32, s32}});
54 })
55
TEST_F(AArch64GISelMITest,BasicLegalizerTest)56 TEST_F(AArch64GISelMITest, BasicLegalizerTest) {
57 StringRef MIRString = R"(
58 %vptr:_(p0) = COPY $x4
59 %v:_(<2 x s8>) = G_LOAD %vptr:_(p0) :: (load (<2 x s8>), align 1)
60 $h4 = COPY %v:_(<2 x s8>)
61 )";
62 setUp(MIRString.rtrim(' '));
63 if (!TM)
64 return;
65
66 ALegalizerInfo LI(MF->getSubtarget());
67 LostDebugLocObserver LocObserver(DEBUG_TYPE);
68
69 Legalizer::MFResult Result = Legalizer::legalizeMachineFunction(
70 *MF, LI, {&LocObserver}, LocObserver, B);
71
72 EXPECT_TRUE(isNullMIPtr(Result.FailedOn));
73 EXPECT_TRUE(Result.Changed);
74
75 StringRef CheckString = R"(
76 CHECK: %vptr:_(p0) = COPY $x4
77 CHECK-NEXT: [[LOAD_0:%[0-9]+]]:_(s16) = G_LOAD %vptr:_(p0) :: (load (s8))
78 CHECK-NEXT: [[OFFSET_1:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
79 CHECK-NEXT: [[VPTR_1:%[0-9]+]]:_(p0) = G_PTR_ADD %vptr:_, [[OFFSET_1]]:_(s64)
80 CHECK-NEXT: [[LOAD_1:%[0-9]+]]:_(s16) = G_LOAD [[VPTR_1]]:_(p0) :: (load (s8) from unknown-address + 1)
81 CHECK-NEXT: %v:_(<2 x s8>) = G_BUILD_VECTOR_TRUNC [[LOAD_0]]:_(s16), [[LOAD_1]]:_(s16)
82 CHECK-NEXT: $h4 = COPY %v:_(<2 x s8>)
83 )";
84
85 EXPECT_TRUE(CheckMachineFunction(*MF, CheckString)) << *MF;
86 }
87
88 // Making sure the legalization finishes successfully w/o failure to combine
89 // away all the legalization artifacts regardless of the order of their
90 // creation.
TEST_F(AArch64GISelMITest,UnorderedArtifactCombiningTest)91 TEST_F(AArch64GISelMITest, UnorderedArtifactCombiningTest) {
92 StringRef MIRString = R"(
93 %vptr:_(p0) = COPY $x4
94 %v:_(<2 x s8>) = G_LOAD %vptr:_(p0) :: (load (<2 x s8>), align 1)
95 %v0:_(s8), %v1:_(s8) = G_UNMERGE_VALUES %v:_(<2 x s8>)
96 %v0_ext:_(s16) = G_ANYEXT %v0:_(s8)
97 $h4 = COPY %v0_ext:_(s16)
98 )";
99 setUp(MIRString.rtrim(' '));
100 if (!TM)
101 return;
102
103 ALegalizerInfo LI(MF->getSubtarget());
104 LostDebugLocObserver LocObserver(DEBUG_TYPE);
105
106 // The events here unfold as follows:
107 // 1. First, the function is scanned pre-forming the worklist of artifacts:
108 //
109 // UNMERGE (1): pushed into the worklist first, will be processed last.
110 // |
111 // ANYEXT (2)
112 //
113 // 2. Second, the load is scalarized, and then its destination is widened,
114 // forming the following chain of legalization artifacts:
115 //
116 // TRUNC (4): created last, will be processed first.
117 // |
118 // BUILD_VECTOR (3)
119 // |
120 // UNMERGE (1): pushed into the worklist first, will be processed last.
121 // |
122 // ANYEXT (2)
123 //
124 // 3. Third, the artifacts are attempted to be combined in pairs, looking
125 // through the def-use chain from the roots towards the leafs, visiting the
126 // roots in order they happen to be in the worklist:
127 // (4) - (trunc): can not be combined;
128 // (3) - (build_vector (trunc)): can not be combined;
129 // (2) - (anyext (unmerge)): can not be combined;
130 // (1) - (unmerge (build_vector)): combined and eliminated;
131 //
132 // leaving the function in the following state:
133 //
134 // TRUNC (1): moved to non-artifact instructions worklist first.
135 // |
136 // ANYEXT (2): also moved to non-artifact instructions worklist.
137 //
138 // Every other instruction is successfully legalized in full.
139 // If combining (unmerge (build_vector)) does not re-insert every artifact
140 // that had its def-use chain modified (shortened) into the artifact
141 // worklist (here it's just ANYEXT), the process moves on onto the next
142 // outer loop iteration of the top-level legalization algorithm here, w/o
143 // performing all the artifact combines possible. Let's consider this
144 // scenario first:
145 // 4.A. Neither TRUNC, nor ANYEXT can be legalized in isolation, both of them
146 // get moved to the retry worklist, but no additional artifacts were
147 // created in the process, thus algorithm concludes no progress could be
148 // made, and fails.
149 // 4.B. If, however, combining (unmerge (build_vector)) had re-inserted
150 // ANYEXT into the worklist (as ANYEXT's source changes, not by value,
151 // but by implementation), (anyext (trunc)) combine happens next, which
152 // fully eliminates all the artifacts and legalization succeeds.
153 //
154 // We're looking into making sure that (4.B) happens here, not (4.A). Note
155 // that in that case the first scan through the artifacts worklist, while not
156 // being done in any guaranteed order, only needs to find the innermost
157 // pair(s) of artifacts that could be immediately combined out. After that
158 // the process follows def-use chains, making them shorter at each step, thus
159 // combining everything that can be combined in O(n) time.
160 Legalizer::MFResult Result = Legalizer::legalizeMachineFunction(
161 *MF, LI, {&LocObserver}, LocObserver, B);
162
163 EXPECT_TRUE(isNullMIPtr(Result.FailedOn));
164 EXPECT_TRUE(Result.Changed);
165
166 StringRef CheckString = R"(
167 CHECK: %vptr:_(p0) = COPY $x4
168 CHECK-NEXT: [[LOAD_0:%[0-9]+]]:_(s16) = G_LOAD %vptr:_(p0) :: (load (s8))
169 CHECK: $h4 = COPY [[LOAD_0]]:_(s16)
170 )";
171
172 EXPECT_TRUE(CheckMachineFunction(*MF, CheckString)) << *MF;
173 }
174
TEST_F(AArch64GISelMITest,UnorderedArtifactCombiningManyCopiesTest)175 TEST_F(AArch64GISelMITest, UnorderedArtifactCombiningManyCopiesTest) {
176 StringRef MIRString = R"(
177 %vptr:_(p0) = COPY $x4
178 %v:_(<2 x s8>) = G_LOAD %vptr:_(p0) :: (load (<2 x s8>), align 1)
179 %vc0:_(<2 x s8>) = COPY %v:_(<2 x s8>)
180 %vc1:_(<2 x s8>) = COPY %v:_(<2 x s8>)
181 %vc00:_(s8), %vc01:_(s8) = G_UNMERGE_VALUES %vc0:_(<2 x s8>)
182 %vc10:_(s8), %vc11:_(s8) = G_UNMERGE_VALUES %vc1:_(<2 x s8>)
183 %v0t:_(s8) = COPY %vc00:_(s8)
184 %v0:_(s8) = COPY %v0t:_(s8)
185 %v1t:_(s8) = COPY %vc11:_(s8)
186 %v1:_(s8) = COPY %v1t:_(s8)
187 %v0_zext:_(s32) = G_ZEXT %v0:_(s8)
188 %v1_sext:_(s32) = G_SEXT %v1:_(s8)
189 $w4 = COPY %v0_zext:_(s32)
190 $w5 = COPY %v1_sext:_(s32)
191 )";
192 setUp(MIRString.rtrim(' '));
193 if (!TM)
194 return;
195
196 ALegalizerInfo LI(MF->getSubtarget());
197 LostDebugLocObserver LocObserver(DEBUG_TYPE);
198
199 Legalizer::MFResult Result = Legalizer::legalizeMachineFunction(
200 *MF, LI, {&LocObserver}, LocObserver, B);
201
202 EXPECT_TRUE(isNullMIPtr(Result.FailedOn));
203 EXPECT_TRUE(Result.Changed);
204
205 StringRef CheckString = R"(
206 CHECK: %vptr:_(p0) = COPY $x4
207 CHECK-NEXT: [[LOAD_0:%[0-9]+]]:_(s16) = G_LOAD %vptr:_(p0) :: (load (s8))
208 CHECK-NEXT: [[OFFSET_1:%[0-9]+]]:_(s64) = G_CONSTANT i64 1
209 CHECK-NEXT: [[VPTR_1:%[0-9]+]]:_(p0) = G_PTR_ADD %vptr:_, [[OFFSET_1]]:_(s64)
210 CHECK-NEXT: [[LOAD_1:%[0-9]+]]:_(s16) = G_LOAD [[VPTR_1]]:_(p0) :: (load (s8) from unknown-address + 1)
211 CHECK-NEXT: [[FF_MASK:%[0-9]+]]:_(s32) = G_CONSTANT i32 255
212 CHECK-NEXT: [[V0_EXT:%[0-9]+]]:_(s32) = G_ANYEXT [[LOAD_0]]:_(s16)
213 CHECK-NEXT: %v0_zext:_(s32) = G_AND [[V0_EXT]]:_, [[FF_MASK]]:_
214 CHECK-NEXT: [[V1_EXT:%[0-9]+]]:_(s32) = G_ANYEXT [[LOAD_1]]:_(s16)
215 CHECK-NEXT: [[SHAMNT:%[0-9]+]]:_(s32) = G_CONSTANT i32 24
216 CHECK-NEXT: [[V1_SHL:%[0-9]+]]:_(s32) = G_SHL [[V1_EXT]]:_, [[SHAMNT]]:_(s32)
217 CHECK-NEXT: %v1_sext:_(s32) = G_ASHR [[V1_SHL]]:_, [[SHAMNT]]:_(s32)
218 CHECK-NEXT: $w4 = COPY %v0_zext:_(s32)
219 CHECK-NEXT: $w5 = COPY %v1_sext:_(s32)
220 )";
221
222 EXPECT_TRUE(CheckMachineFunction(*MF, CheckString)) << *MF;
223 }
224
225 } // namespace
226