1 //===- CombinerUtils.h ----------------------------------------------------===//
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 /// \file Utility functions used by both Combiner backends.
10 /// TODO: Can remove when MatchDAG-based backend is removed.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_UTILS_TABLEGEN_COMBINERUTILS_H
15 #define LLVM_UTILS_TABLEGEN_COMBINERUTILS_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/TableGen/Record.h"
19 
20 namespace llvm {
21 
22 /// A convenience function to check that an Init refers to a specific def. This
23 /// is primarily useful for testing for defs and similar in DagInit's since
24 /// DagInit's support any type inside them.
25 inline bool isSpecificDef(const Init &N, StringRef Def) {
26   if (const DefInit *OpI = dyn_cast<DefInit>(&N))
27     if (OpI->getDef()->getName() == Def)
28       return true;
29   return false;
30 }
31 
32 /// A convenience function to check that an Init refers to a def that is a
33 /// subclass of the given class and coerce it to a def if it is. This is
34 /// primarily useful for testing for subclasses of GIMatchKind and similar in
35 /// DagInit's since DagInit's support any type inside them.
36 inline Record *getDefOfSubClass(const Init &N, StringRef Cls) {
37   if (const DefInit *OpI = dyn_cast<DefInit>(&N))
38     if (OpI->getDef()->isSubClassOf(Cls))
39       return OpI->getDef();
40   return nullptr;
41 }
42 
43 /// A convenience function to check that an Init refers to a dag whose operator
44 /// is a specific def and coerce it to a dag if it is. This is primarily useful
45 /// for testing for subclasses of GIMatchKind and similar in DagInit's since
46 /// DagInit's support any type inside them.
47 inline const DagInit *getDagWithSpecificOperator(const Init &N,
48                                                  StringRef Name) {
49   if (const DagInit *I = dyn_cast<DagInit>(&N))
50     if (I->getNumArgs() > 0)
51       if (const DefInit *OpI = dyn_cast<DefInit>(I->getOperator()))
52         if (OpI->getDef()->getName() == Name)
53           return I;
54   return nullptr;
55 }
56 
57 /// A convenience function to check that an Init refers to a dag whose operator
58 /// is a def that is a subclass of the given class and coerce it to a dag if it
59 /// is. This is primarily useful for testing for subclasses of GIMatchKind and
60 /// similar in DagInit's since DagInit's support any type inside them.
61 inline const DagInit *getDagWithOperatorOfSubClass(const Init &N,
62                                                    StringRef Cls) {
63   if (const DagInit *I = dyn_cast<DagInit>(&N))
64     if (I->getNumArgs() > 0)
65       if (const DefInit *OpI = dyn_cast<DefInit>(I->getOperator()))
66         if (OpI->getDef()->isSubClassOf(Cls))
67           return I;
68   return nullptr;
69 }
70 } // namespace llvm
71 
72 #endif
73