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