-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPatternMatch-extras.hh
More file actions
81 lines (66 loc) · 2.15 KB
/
PatternMatch-extras.hh
File metadata and controls
81 lines (66 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
////////////////////////////////////////////////////////////////////////
//
// Extra components for use with llvm::PatternMatch that could
// (should?) have been included with LLVM in the first place.
//
#ifndef INCLUDE_PatternMatch_extras_hh
#define INCLUDE_PatternMatch_extras_hh
#include <llvm/Config/llvm-config.h>
#if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
# include <llvm/IR/PatternMatch.h>
#else // LLVM 3.4 or earlier
# include <llvm/Support/PatternMatch.h>
#endif // LLVM 3.4 or earlier
namespace llvm {
namespace PatternMatch {
// match a function formal argument
inline bind_ty<Argument> m_FormalArgument(Argument *&argument) {
return bind_ty<Argument>(argument);
}
template <typename Pointer>
struct load_match {
Pointer pointer;
template <typename Mystery>
bool match(Mystery *);
};
template <typename Pointer>
template <typename Mystery>
bool load_match<Pointer>::match(Mystery *mystery) {
if (const auto load = dyn_cast<LoadInst>(mystery))
return pointer.match(load->getPointerOperand());
return false;
}
// match a load instruction
template <typename Pointer>
inline load_match<Pointer> m_Load(const Pointer &pointer) {
return { pointer };
}
template <typename Pointer, typename Index>
struct get_element_pointer_match {
Pointer pointer;
Index index;
template <typename Mystery>
bool match(Mystery *);
};
template <typename Pointer, typename Index>
template <typename Mystery>
bool get_element_pointer_match<Pointer, Index>::match(Mystery *mystery) {
if (const auto gep = dyn_cast<GetElementPtrInst>(mystery)) {
if (!pointer.match(gep->getPointerOperand()))
return false;
if (gep->getNumIndices() != 1)
return false;
if (!index.match(gep->idx_begin()))
return false;
return true;
}
return false;
}
// match a getelementptr instruction with exactly one index
template <typename Pointer, typename Index>
inline get_element_pointer_match<Pointer, Index> m_GetElementPointer(const Pointer &pointer, const Index &index) {
return { pointer, index };
}
}
}
#endif // !INCLUDE_PatternMatch_extras_hh