1 //===- PatternMatchTest.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 "GISelMITest.h"
10 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
11 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
12 #include "llvm/CodeGen/GlobalISel/Utils.h"
13 #include "llvm/CodeGen/MIRParser/MIRParser.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineModuleInfo.h"
16 #include "llvm/CodeGen/TargetFrameLowering.h"
17 #include "llvm/CodeGen/TargetInstrInfo.h"
18 #include "llvm/CodeGen/TargetLowering.h"
19 #include "llvm/CodeGen/TargetSubtargetInfo.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Support/TargetSelect.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "gtest/gtest.h"
26 
27 using namespace llvm;
28 using namespace MIPatternMatch;
29 
30 namespace {
31 
32 TEST_F(AArch64GISelMITest, MatchIntConstant) {
33   setUp();
34   if (!TM)
35     return;
36   auto MIBCst = B.buildConstant(LLT::scalar(64), 42);
37   int64_t Cst;
38   bool match = mi_match(MIBCst.getReg(0), *MRI, m_ICst(Cst));
39   EXPECT_TRUE(match);
40   EXPECT_EQ(Cst, 42);
41 }
42 
43 TEST_F(AArch64GISelMITest, MatchIntConstantRegister) {
44   setUp();
45   if (!TM)
46     return;
47   auto MIBCst = B.buildConstant(LLT::scalar(64), 42);
48   Register Src0;
49   bool match = mi_match(MIBCst.getReg(0), *MRI, m_ICst(Src0));
50   EXPECT_TRUE(match);
51   EXPECT_EQ(Src0, MIBCst.getReg(0));
52 }
53 
54 TEST_F(AArch64GISelMITest, MatchBinaryOp) {
55   setUp();
56   if (!TM)
57     return;
58   LLT s32 = LLT::scalar(32);
59   LLT s64 = LLT::scalar(64);
60   LLT p0 = LLT::pointer(0, 64);
61   auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
62   // Test case for no bind.
63   bool match =
64       mi_match(MIBAdd.getReg(0), *MRI, m_GAdd(m_Reg(), m_Reg()));
65   EXPECT_TRUE(match);
66   Register Src0, Src1, Src2;
67   match = mi_match(MIBAdd.getReg(0), *MRI,
68                    m_GAdd(m_Reg(Src0), m_Reg(Src1)));
69   EXPECT_TRUE(match);
70   EXPECT_EQ(Src0, Copies[0]);
71   EXPECT_EQ(Src1, Copies[1]);
72 
73   // Build MUL(ADD %0, %1), %2
74   auto MIBMul = B.buildMul(s64, MIBAdd, Copies[2]);
75 
76   // Try to match MUL.
77   match = mi_match(MIBMul.getReg(0), *MRI,
78                    m_GMul(m_Reg(Src0), m_Reg(Src1)));
79   EXPECT_TRUE(match);
80   EXPECT_EQ(Src0, MIBAdd.getReg(0));
81   EXPECT_EQ(Src1, Copies[2]);
82 
83   // Try to match MUL(ADD)
84   match = mi_match(MIBMul.getReg(0), *MRI,
85                    m_GMul(m_GAdd(m_Reg(Src0), m_Reg(Src1)), m_Reg(Src2)));
86   EXPECT_TRUE(match);
87   EXPECT_EQ(Src0, Copies[0]);
88   EXPECT_EQ(Src1, Copies[1]);
89   EXPECT_EQ(Src2, Copies[2]);
90 
91   // Test Commutativity.
92   auto MIBMul2 = B.buildMul(s64, Copies[0], B.buildConstant(s64, 42));
93   // Try to match MUL(Cst, Reg) on src of MUL(Reg, Cst) to validate
94   // commutativity.
95   int64_t Cst;
96   match = mi_match(MIBMul2.getReg(0), *MRI,
97                    m_GMul(m_ICst(Cst), m_Reg(Src0)));
98   EXPECT_TRUE(match);
99   EXPECT_EQ(Cst, 42);
100   EXPECT_EQ(Src0, Copies[0]);
101 
102   // Make sure commutative doesn't work with something like SUB.
103   auto MIBSub = B.buildSub(s64, Copies[0], B.buildConstant(s64, 42));
104   match = mi_match(MIBSub.getReg(0), *MRI,
105                    m_GSub(m_ICst(Cst), m_Reg(Src0)));
106   EXPECT_FALSE(match);
107 
108   auto MIBFMul = B.buildInstr(TargetOpcode::G_FMUL, {s64},
109                               {Copies[0], B.buildConstant(s64, 42)});
110   // Match and test commutativity for FMUL.
111   match = mi_match(MIBFMul.getReg(0), *MRI,
112                    m_GFMul(m_ICst(Cst), m_Reg(Src0)));
113   EXPECT_TRUE(match);
114   EXPECT_EQ(Cst, 42);
115   EXPECT_EQ(Src0, Copies[0]);
116 
117   // FSUB
118   auto MIBFSub = B.buildInstr(TargetOpcode::G_FSUB, {s64},
119                               {Copies[0], B.buildConstant(s64, 42)});
120   match = mi_match(MIBFSub.getReg(0), *MRI,
121                    m_GFSub(m_Reg(Src0), m_Reg()));
122   EXPECT_TRUE(match);
123   EXPECT_EQ(Src0, Copies[0]);
124 
125   // Build AND %0, %1
126   auto MIBAnd = B.buildAnd(s64, Copies[0], Copies[1]);
127   // Try to match AND.
128   match = mi_match(MIBAnd.getReg(0), *MRI,
129                    m_GAnd(m_Reg(Src0), m_Reg(Src1)));
130   EXPECT_TRUE(match);
131   EXPECT_EQ(Src0, Copies[0]);
132   EXPECT_EQ(Src1, Copies[1]);
133 
134   // Build OR %0, %1
135   auto MIBOr = B.buildOr(s64, Copies[0], Copies[1]);
136   // Try to match OR.
137   match = mi_match(MIBOr.getReg(0), *MRI,
138                    m_GOr(m_Reg(Src0), m_Reg(Src1)));
139   EXPECT_TRUE(match);
140   EXPECT_EQ(Src0, Copies[0]);
141   EXPECT_EQ(Src1, Copies[1]);
142 
143   // Match lshr, and make sure a different shift amount type works.
144   auto TruncCopy1 = B.buildTrunc(s32, Copies[1]);
145   auto LShr = B.buildLShr(s64, Copies[0], TruncCopy1);
146   match = mi_match(LShr.getReg(0), *MRI,
147                    m_GLShr(m_Reg(Src0), m_Reg(Src1)));
148   EXPECT_TRUE(match);
149   EXPECT_EQ(Src0, Copies[0]);
150   EXPECT_EQ(Src1, TruncCopy1.getReg(0));
151 
152   // Match shl, and make sure a different shift amount type works.
153   auto Shl = B.buildShl(s64, Copies[0], TruncCopy1);
154   match = mi_match(Shl.getReg(0), *MRI,
155                    m_GShl(m_Reg(Src0), m_Reg(Src1)));
156   EXPECT_TRUE(match);
157   EXPECT_EQ(Src0, Copies[0]);
158   EXPECT_EQ(Src1, TruncCopy1.getReg(0));
159 
160   // Build a G_PTR_ADD and check that we can match it.
161   auto PtrAdd = B.buildPtrAdd(p0, {B.buildUndef(p0)}, Copies[0]);
162   match = mi_match(PtrAdd.getReg(0), *MRI, m_GPtrAdd(m_Reg(Src0), m_Reg(Src1)));
163   EXPECT_TRUE(match);
164   EXPECT_EQ(Src0, PtrAdd->getOperand(1).getReg());
165   EXPECT_EQ(Src1, Copies[0]);
166 }
167 
168 TEST_F(AArch64GISelMITest, MatchICmp) {
169   setUp();
170   if (!TM)
171     return;
172 
173   const LLT s1 = LLT::scalar(1);
174   auto CmpEq = B.buildICmp(CmpInst::ICMP_EQ, s1, Copies[0], Copies[1]);
175 
176   // Check match any predicate.
177   bool match =
178       mi_match(CmpEq.getReg(0), *MRI, m_GICmp(m_Pred(), m_Reg(), m_Reg()));
179   EXPECT_TRUE(match);
180 
181   // Check we get the predicate and registers.
182   CmpInst::Predicate Pred;
183   Register Reg0;
184   Register Reg1;
185   match = mi_match(CmpEq.getReg(0), *MRI,
186                    m_GICmp(m_Pred(Pred), m_Reg(Reg0), m_Reg(Reg1)));
187   EXPECT_TRUE(match);
188   EXPECT_EQ(CmpInst::ICMP_EQ, Pred);
189   EXPECT_EQ(Copies[0], Reg0);
190   EXPECT_EQ(Copies[1], Reg1);
191 }
192 
193 TEST_F(AArch64GISelMITest, MatchFCmp) {
194   setUp();
195   if (!TM)
196     return;
197 
198   const LLT s1 = LLT::scalar(1);
199   auto CmpEq = B.buildFCmp(CmpInst::FCMP_OEQ, s1, Copies[0], Copies[1]);
200 
201   // Check match any predicate.
202   bool match =
203       mi_match(CmpEq.getReg(0), *MRI, m_GFCmp(m_Pred(), m_Reg(), m_Reg()));
204   EXPECT_TRUE(match);
205 
206   // Check we get the predicate and registers.
207   CmpInst::Predicate Pred;
208   Register Reg0;
209   Register Reg1;
210   match = mi_match(CmpEq.getReg(0), *MRI,
211                    m_GFCmp(m_Pred(Pred), m_Reg(Reg0), m_Reg(Reg1)));
212   EXPECT_TRUE(match);
213   EXPECT_EQ(CmpInst::FCMP_OEQ, Pred);
214   EXPECT_EQ(Copies[0], Reg0);
215   EXPECT_EQ(Copies[1], Reg1);
216 }
217 
218 TEST_F(AArch64GISelMITest, MatchFPUnaryOp) {
219   setUp();
220   if (!TM)
221     return;
222 
223   // Truncate s64 to s32.
224   LLT s32 = LLT::scalar(32);
225   auto Copy0s32 = B.buildFPTrunc(s32, Copies[0]);
226 
227   // Match G_FABS.
228   auto MIBFabs = B.buildInstr(TargetOpcode::G_FABS, {s32}, {Copy0s32});
229   bool match =
230       mi_match(MIBFabs.getReg(0), *MRI, m_GFabs(m_Reg()));
231   EXPECT_TRUE(match);
232 
233   Register Src;
234   auto MIBFNeg = B.buildInstr(TargetOpcode::G_FNEG, {s32}, {Copy0s32});
235   match = mi_match(MIBFNeg.getReg(0), *MRI, m_GFNeg(m_Reg(Src)));
236   EXPECT_TRUE(match);
237   EXPECT_EQ(Src, Copy0s32.getReg(0));
238 
239   match = mi_match(MIBFabs.getReg(0), *MRI, m_GFabs(m_Reg(Src)));
240   EXPECT_TRUE(match);
241   EXPECT_EQ(Src, Copy0s32.getReg(0));
242 
243   // Build and match FConstant.
244   auto MIBFCst = B.buildFConstant(s32, .5);
245   const ConstantFP *TmpFP{};
246   match = mi_match(MIBFCst.getReg(0), *MRI, m_GFCst(TmpFP));
247   EXPECT_TRUE(match);
248   EXPECT_TRUE(TmpFP);
249   APFloat APF((float).5);
250   auto *CFP = ConstantFP::get(Context, APF);
251   EXPECT_EQ(CFP, TmpFP);
252 
253   // Build double float.
254   LLT s64 = LLT::scalar(64);
255   auto MIBFCst64 = B.buildFConstant(s64, .5);
256   const ConstantFP *TmpFP64{};
257   match = mi_match(MIBFCst64.getReg(0), *MRI, m_GFCst(TmpFP64));
258   EXPECT_TRUE(match);
259   EXPECT_TRUE(TmpFP64);
260   APFloat APF64(.5);
261   auto CFP64 = ConstantFP::get(Context, APF64);
262   EXPECT_EQ(CFP64, TmpFP64);
263   EXPECT_NE(TmpFP64, TmpFP);
264 
265   // Build half float.
266   LLT s16 = LLT::scalar(16);
267   auto MIBFCst16 = B.buildFConstant(s16, .5);
268   const ConstantFP *TmpFP16{};
269   match = mi_match(MIBFCst16.getReg(0), *MRI, m_GFCst(TmpFP16));
270   EXPECT_TRUE(match);
271   EXPECT_TRUE(TmpFP16);
272   bool Ignored;
273   APFloat APF16(.5);
274   APF16.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
275   auto CFP16 = ConstantFP::get(Context, APF16);
276   EXPECT_EQ(TmpFP16, CFP16);
277   EXPECT_NE(TmpFP16, TmpFP);
278 }
279 
280 TEST_F(AArch64GISelMITest, MatchExtendsTrunc) {
281   setUp();
282   if (!TM)
283     return;
284 
285   LLT s64 = LLT::scalar(64);
286   LLT s32 = LLT::scalar(32);
287 
288   auto MIBTrunc = B.buildTrunc(s32, Copies[0]);
289   auto MIBAExt = B.buildAnyExt(s64, MIBTrunc);
290   auto MIBZExt = B.buildZExt(s64, MIBTrunc);
291   auto MIBSExt = B.buildSExt(s64, MIBTrunc);
292   Register Src0;
293   bool match =
294       mi_match(MIBTrunc.getReg(0), *MRI, m_GTrunc(m_Reg(Src0)));
295   EXPECT_TRUE(match);
296   EXPECT_EQ(Src0, Copies[0]);
297   match =
298       mi_match(MIBAExt.getReg(0), *MRI, m_GAnyExt(m_Reg(Src0)));
299   EXPECT_TRUE(match);
300   EXPECT_EQ(Src0, MIBTrunc.getReg(0));
301 
302   match = mi_match(MIBSExt.getReg(0), *MRI, m_GSExt(m_Reg(Src0)));
303   EXPECT_TRUE(match);
304   EXPECT_EQ(Src0, MIBTrunc.getReg(0));
305 
306   match = mi_match(MIBZExt.getReg(0), *MRI, m_GZExt(m_Reg(Src0)));
307   EXPECT_TRUE(match);
308   EXPECT_EQ(Src0, MIBTrunc.getReg(0));
309 
310   // Match ext(trunc src)
311   match = mi_match(MIBAExt.getReg(0), *MRI,
312                    m_GAnyExt(m_GTrunc(m_Reg(Src0))));
313   EXPECT_TRUE(match);
314   EXPECT_EQ(Src0, Copies[0]);
315 
316   match = mi_match(MIBSExt.getReg(0), *MRI,
317                    m_GSExt(m_GTrunc(m_Reg(Src0))));
318   EXPECT_TRUE(match);
319   EXPECT_EQ(Src0, Copies[0]);
320 
321   match = mi_match(MIBZExt.getReg(0), *MRI,
322                    m_GZExt(m_GTrunc(m_Reg(Src0))));
323   EXPECT_TRUE(match);
324   EXPECT_EQ(Src0, Copies[0]);
325 }
326 
327 TEST_F(AArch64GISelMITest, MatchSpecificType) {
328   setUp();
329   if (!TM)
330     return;
331 
332   // Try to match a 64bit add.
333   LLT s64 = LLT::scalar(64);
334   LLT s32 = LLT::scalar(32);
335   auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
336   EXPECT_FALSE(mi_match(MIBAdd.getReg(0), *MRI,
337                         m_GAdd(m_SpecificType(s32), m_Reg())));
338   EXPECT_TRUE(mi_match(MIBAdd.getReg(0), *MRI,
339                        m_GAdd(m_SpecificType(s64), m_Reg())));
340 
341   // Try to match the destination type of a bitcast.
342   LLT v2s32 = LLT::vector(2, 32);
343   auto MIBCast = B.buildCast(v2s32, Copies[0]);
344   EXPECT_TRUE(
345       mi_match(MIBCast.getReg(0), *MRI, m_GBitcast(m_Reg())));
346   EXPECT_TRUE(
347       mi_match(MIBCast.getReg(0), *MRI, m_SpecificType(v2s32)));
348   EXPECT_TRUE(
349       mi_match(MIBCast.getReg(1), *MRI, m_SpecificType(s64)));
350 
351   // Build a PTRToInt and INTTOPTR and match and test them.
352   LLT PtrTy = LLT::pointer(0, 64);
353   auto MIBIntToPtr = B.buildCast(PtrTy, Copies[0]);
354   auto MIBPtrToInt = B.buildCast(s64, MIBIntToPtr);
355   Register Src0;
356 
357   // match the ptrtoint(inttoptr reg)
358   bool match = mi_match(MIBPtrToInt.getReg(0), *MRI,
359                         m_GPtrToInt(m_GIntToPtr(m_Reg(Src0))));
360   EXPECT_TRUE(match);
361   EXPECT_EQ(Src0, Copies[0]);
362 }
363 
364 TEST_F(AArch64GISelMITest, MatchCombinators) {
365   setUp();
366   if (!TM)
367     return;
368 
369   LLT s64 = LLT::scalar(64);
370   LLT s32 = LLT::scalar(32);
371   auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
372   Register Src0, Src1;
373   bool match =
374       mi_match(MIBAdd.getReg(0), *MRI,
375                m_all_of(m_SpecificType(s64), m_GAdd(m_Reg(Src0), m_Reg(Src1))));
376   EXPECT_TRUE(match);
377   EXPECT_EQ(Src0, Copies[0]);
378   EXPECT_EQ(Src1, Copies[1]);
379   // Check for s32 (which should fail).
380   match =
381       mi_match(MIBAdd.getReg(0), *MRI,
382                m_all_of(m_SpecificType(s32), m_GAdd(m_Reg(Src0), m_Reg(Src1))));
383   EXPECT_FALSE(match);
384   match =
385       mi_match(MIBAdd.getReg(0), *MRI,
386                m_any_of(m_SpecificType(s32), m_GAdd(m_Reg(Src0), m_Reg(Src1))));
387   EXPECT_TRUE(match);
388   EXPECT_EQ(Src0, Copies[0]);
389   EXPECT_EQ(Src1, Copies[1]);
390 
391   // Match a case where none of the predicates hold true.
392   match = mi_match(
393       MIBAdd.getReg(0), *MRI,
394       m_any_of(m_SpecificType(LLT::scalar(16)), m_GSub(m_Reg(), m_Reg())));
395   EXPECT_FALSE(match);
396 }
397 
398 TEST_F(AArch64GISelMITest, MatchMiscellaneous) {
399   setUp();
400   if (!TM)
401     return;
402 
403   LLT s64 = LLT::scalar(64);
404   auto MIBAdd = B.buildAdd(s64, Copies[0], Copies[1]);
405   Register Reg = MIBAdd.getReg(0);
406 
407   // Only one use of Reg.
408   B.buildCast(LLT::pointer(0, 32), MIBAdd);
409   EXPECT_TRUE(mi_match(Reg, *MRI, m_OneUse(m_GAdd(m_Reg(), m_Reg()))));
410   EXPECT_TRUE(mi_match(Reg, *MRI, m_OneNonDBGUse(m_GAdd(m_Reg(), m_Reg()))));
411 
412   // Add multiple debug uses of Reg.
413   B.buildInstr(TargetOpcode::DBG_VALUE, {}, {Reg})->getOperand(0).setIsDebug();
414   B.buildInstr(TargetOpcode::DBG_VALUE, {}, {Reg})->getOperand(0).setIsDebug();
415 
416   EXPECT_FALSE(mi_match(Reg, *MRI, m_OneUse(m_GAdd(m_Reg(), m_Reg()))));
417   EXPECT_TRUE(mi_match(Reg, *MRI, m_OneNonDBGUse(m_GAdd(m_Reg(), m_Reg()))));
418 
419   // Multiple non-debug uses of Reg.
420   B.buildCast(LLT::pointer(1, 32), MIBAdd);
421   EXPECT_FALSE(mi_match(Reg, *MRI, m_OneUse(m_GAdd(m_Reg(), m_Reg()))));
422   EXPECT_FALSE(mi_match(Reg, *MRI, m_OneNonDBGUse(m_GAdd(m_Reg(), m_Reg()))));
423 }
424 
425 TEST_F(AArch64GISelMITest, MatchSpecificConstant) {
426   setUp();
427   if (!TM)
428     return;
429 
430   // Basic case: Can we match a G_CONSTANT with a specific value?
431   auto FortyTwo = B.buildConstant(LLT::scalar(64), 42);
432   EXPECT_TRUE(mi_match(FortyTwo.getReg(0), *MRI, m_SpecificICst(42)));
433   EXPECT_FALSE(mi_match(FortyTwo.getReg(0), *MRI, m_SpecificICst(123)));
434 
435   // Test that this works inside of a more complex pattern.
436   LLT s64 = LLT::scalar(64);
437   auto MIBAdd = B.buildAdd(s64, Copies[0], FortyTwo);
438   EXPECT_TRUE(mi_match(MIBAdd.getReg(2), *MRI, m_SpecificICst(42)));
439 
440   // Wrong constant.
441   EXPECT_FALSE(mi_match(MIBAdd.getReg(2), *MRI, m_SpecificICst(123)));
442 
443   // No constant on the LHS.
444   EXPECT_FALSE(mi_match(MIBAdd.getReg(1), *MRI, m_SpecificICst(42)));
445 }
446 
447 TEST_F(AArch64GISelMITest, MatchZeroInt) {
448   setUp();
449   if (!TM)
450     return;
451   auto Zero = B.buildConstant(LLT::scalar(64), 0);
452   EXPECT_TRUE(mi_match(Zero.getReg(0), *MRI, m_ZeroInt()));
453 
454   auto FortyTwo = B.buildConstant(LLT::scalar(64), 42);
455   EXPECT_FALSE(mi_match(FortyTwo.getReg(0), *MRI, m_ZeroInt()));
456 }
457 
458 TEST_F(AArch64GISelMITest, MatchAllOnesInt) {
459   setUp();
460   if (!TM)
461     return;
462   auto AllOnes = B.buildConstant(LLT::scalar(64), -1);
463   EXPECT_TRUE(mi_match(AllOnes.getReg(0), *MRI, m_AllOnesInt()));
464 
465   auto FortyTwo = B.buildConstant(LLT::scalar(64), 42);
466   EXPECT_FALSE(mi_match(FortyTwo.getReg(0), *MRI, m_AllOnesInt()));
467 }
468 
469 TEST_F(AArch64GISelMITest, MatchNeg) {
470   setUp();
471   if (!TM)
472     return;
473 
474   LLT s64 = LLT::scalar(64);
475   auto Zero = B.buildConstant(LLT::scalar(64), 0);
476   auto NegInst = B.buildSub(s64, Zero, Copies[0]);
477   Register NegatedReg;
478 
479   // Match: G_SUB = 0, %Reg
480   EXPECT_TRUE(mi_match(NegInst.getReg(0), *MRI, m_Neg(m_Reg(NegatedReg))));
481   EXPECT_EQ(NegatedReg, Copies[0]);
482 
483   // Don't match: G_SUB = %Reg, 0
484   auto NotNegInst1 = B.buildSub(s64, Copies[0], Zero);
485   EXPECT_FALSE(mi_match(NotNegInst1.getReg(0), *MRI, m_Neg(m_Reg(NegatedReg))));
486 
487   // Don't match: G_SUB = 42, %Reg
488   auto FortyTwo = B.buildConstant(LLT::scalar(64), 42);
489   auto NotNegInst2 = B.buildSub(s64, FortyTwo, Copies[0]);
490   EXPECT_FALSE(mi_match(NotNegInst2.getReg(0), *MRI, m_Neg(m_Reg(NegatedReg))));
491 
492   // Complex testcase.
493   // %sub = G_SUB = 0, %negated_reg
494   // %add = G_ADD = %x, %sub
495   auto AddInst = B.buildAdd(s64, Copies[1], NegInst);
496   NegatedReg = Register();
497   EXPECT_TRUE(mi_match(AddInst.getReg(2), *MRI, m_Neg(m_Reg(NegatedReg))));
498   EXPECT_EQ(NegatedReg, Copies[0]);
499 }
500 
501 TEST_F(AArch64GISelMITest, MatchNot) {
502   setUp();
503   if (!TM)
504     return;
505 
506   LLT s64 = LLT::scalar(64);
507   auto AllOnes = B.buildConstant(LLT::scalar(64), -1);
508   auto NotInst1 = B.buildXor(s64, Copies[0], AllOnes);
509   Register NotReg;
510 
511   // Match: G_XOR %NotReg, -1
512   EXPECT_TRUE(mi_match(NotInst1.getReg(0), *MRI, m_Not(m_Reg(NotReg))));
513   EXPECT_EQ(NotReg, Copies[0]);
514 
515   // Match: G_XOR -1, %NotReg
516   auto NotInst2 = B.buildXor(s64, AllOnes, Copies[1]);
517   EXPECT_TRUE(mi_match(NotInst2.getReg(0), *MRI, m_Not(m_Reg(NotReg))));
518   EXPECT_EQ(NotReg, Copies[1]);
519 
520   // Don't match: G_XOR %NotReg, 42
521   auto FortyTwo = B.buildConstant(LLT::scalar(64), 42);
522   auto WrongCst = B.buildXor(s64, Copies[0], FortyTwo);
523   EXPECT_FALSE(mi_match(WrongCst.getReg(0), *MRI, m_Not(m_Reg(NotReg))));
524 
525   // Complex testcase.
526   // %xor = G_XOR %NotReg, -1
527   // %add = G_ADD %x, %xor
528   auto AddInst = B.buildAdd(s64, Copies[1], NotInst1);
529   NotReg = Register();
530   EXPECT_TRUE(mi_match(AddInst.getReg(2), *MRI, m_Not(m_Reg(NotReg))));
531   EXPECT_EQ(NotReg, Copies[0]);
532 }
533 } // namespace
534 
535 int main(int argc, char **argv) {
536   ::testing::InitGoogleTest(&argc, argv);
537   initLLVM();
538   return RUN_ALL_TESTS();
539 }
540