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