Skip to content

Commit c776ed7

Browse files
committed
Implement m_binop, m_c_binop Matcher
1 parent e37da87 commit c776ed7

4 files changed

Lines changed: 94 additions & 21 deletions

File tree

docs/matchers/binary.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### General Binary Instructions Matchers functions
2+
3+
| Function | Parameters | Return | Description |
4+
| :-------: | :------------------------------------: | :---------: | :-----------------------------------------------------------------: |
5+
| m_binop | (lhs: InstMatcher?, rhs: InstMatcher?) | InstMatcher | Build Inst Matcher that match binary Instruction |
6+
| m_c_binop | (lhs: InstMatcher?, rhs: InstMatcher?) | InstMatcher | Build Inst Matcher that match binary Instruction with commutatively |

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ nav:
8989
- Matchers:
9090
- General: matchers/general.md
9191
- Constants: matchers/constants.md
92+
- Binary: matchers/binary.md
9293
- Arithmetics: matchers/arithmetics.md
9394
- ICmp: matchers/icmp.md
9495
- FCmp: matchers/fcmp.md

src/functions/matchers/binary.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ use crate::matchers::binary::BinaryInstMatcher;
1111

1212
#[inline(always)]
1313
pub fn register_binary_inst_matchers_functions(map: &mut HashMap<&'static str, StandardFunction>) {
14+
map.insert("m_binop", match_binary_inst);
1415
map.insert("m_or", match_or_inst);
1516
map.insert("m_or_disjoint", match_or_disjoint_inst);
1617
map.insert("m_and", match_and_inst);
1718
map.insert("m_xor", match_xor_inst);
1819

20+
map.insert("m_c_binop", match_commutatively_binary_inst);
1921
map.insert("m_c_or", match_commutatively_or_inst);
2022
map.insert("m_c_or_disjoint", match_commutatively_or_disjoint_inst);
2123
map.insert("m_c_and", match_commutatively_and_inst);
@@ -26,11 +28,13 @@ pub fn register_binary_inst_matchers_functions(map: &mut HashMap<&'static str, S
2628
pub fn register_binary_inst_matchers_function_signatures(
2729
map: &mut HashMap<&'static str, Signature>,
2830
) {
31+
map.insert("m_binop", binary_matcher_signature());
2932
map.insert("m_or", binary_matcher_signature());
3033
map.insert("m_or_disjoint", binary_matcher_signature());
3134
map.insert("m_and", binary_matcher_signature());
3235
map.insert("m_xor", binary_matcher_signature());
3336

37+
map.insert("m_c_binop", binary_matcher_signature());
3438
map.insert("m_c_or", binary_matcher_signature());
3539
map.insert("m_c_or_disjoint", binary_matcher_signature());
3640
map.insert("m_c_and", binary_matcher_signature());
@@ -84,3 +88,15 @@ fn match_commutatively_xor_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
8488
let matcher = BinaryInstMatcher::create_commutatively_xor(lhs_matcher, rhs_matcher);
8589
Box::new(InstMatcherValue { matcher })
8690
}
91+
92+
fn match_binary_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
93+
let (lhs_matcher, rhs_matcher) = binary_matchers_sides(values);
94+
let matcher = BinaryInstMatcher::create_any(lhs_matcher, rhs_matcher);
95+
Box::new(InstMatcherValue { matcher })
96+
}
97+
98+
fn match_commutatively_binary_inst(values: &[Box<dyn Value>]) -> Box<dyn Value> {
99+
let (lhs_matcher, rhs_matcher) = binary_matchers_sides(values);
100+
let matcher = BinaryInstMatcher::create_commutatively_any(lhs_matcher, rhs_matcher);
101+
Box::new(InstMatcherValue { matcher })
102+
}

src/matchers/binary.rs

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use super::InstMatcher;
1111

1212
#[derive(PartialEq, Clone)]
1313
pub enum BinaryOperator {
14+
Any,
15+
1416
Add,
1517
Sub,
1618
Mul,
@@ -33,26 +35,50 @@ pub enum BinaryOperator {
3335
}
3436

3537
impl BinaryOperator {
36-
pub fn match_llvm_opcode(&self, op: LLVMOpcode) -> bool {
37-
match op {
38-
LLVMOpcode::LLVMAdd => matches!(self, BinaryOperator::Add),
39-
LLVMOpcode::LLVMSub => matches!(self, BinaryOperator::Sub),
40-
LLVMOpcode::LLVMMul => matches!(self, BinaryOperator::Mul),
41-
LLVMOpcode::LLVMSDiv => matches!(self, BinaryOperator::SDiv),
42-
LLVMOpcode::LLVMSRem => matches!(self, BinaryOperator::SRem),
43-
LLVMOpcode::LLVMFAdd => matches!(self, BinaryOperator::FAdd),
44-
LLVMOpcode::LLVMFSub => matches!(self, BinaryOperator::FSub),
45-
LLVMOpcode::LLVMFMul => matches!(self, BinaryOperator::FMul),
46-
LLVMOpcode::LLVMFDiv => matches!(self, BinaryOperator::FDiv),
47-
LLVMOpcode::LLVMFRem => matches!(self, BinaryOperator::FRem),
48-
49-
LLVMOpcode::LLVMAnd => matches!(self, BinaryOperator::And),
50-
LLVMOpcode::LLVMOr => matches!(self, BinaryOperator::Or | BinaryOperator::OrDisjoint),
51-
LLVMOpcode::LLVMXor => matches!(self, BinaryOperator::Xor),
52-
53-
LLVMOpcode::LLVMShl => matches!(self, BinaryOperator::LogicalShiftLeft),
54-
LLVMOpcode::LLVMLShr => matches!(self, BinaryOperator::LogicalShiftRight),
55-
LLVMOpcode::LLVMAShr => matches!(self, BinaryOperator::ArithmeticShiftRight),
38+
pub fn match_llvm_opcode(&self, llvm_op: LLVMOpcode) -> bool {
39+
match llvm_op {
40+
LLVMOpcode::LLVMAdd => matches!(self, BinaryOperator::Any | BinaryOperator::Add),
41+
42+
LLVMOpcode::LLVMSub => matches!(self, BinaryOperator::Any | BinaryOperator::Sub),
43+
44+
LLVMOpcode::LLVMMul => matches!(self, BinaryOperator::Any | BinaryOperator::Mul),
45+
46+
LLVMOpcode::LLVMSDiv => matches!(self, BinaryOperator::Any | BinaryOperator::SDiv),
47+
48+
LLVMOpcode::LLVMSRem => matches!(self, BinaryOperator::Any | BinaryOperator::SRem),
49+
50+
LLVMOpcode::LLVMFAdd => matches!(self, BinaryOperator::Any | BinaryOperator::FAdd),
51+
52+
LLVMOpcode::LLVMFSub => matches!(self, BinaryOperator::Any | BinaryOperator::FSub),
53+
54+
LLVMOpcode::LLVMFMul => matches!(self, BinaryOperator::Any | BinaryOperator::FMul),
55+
56+
LLVMOpcode::LLVMFDiv => matches!(self, BinaryOperator::Any | BinaryOperator::FDiv),
57+
58+
LLVMOpcode::LLVMFRem => matches!(self, BinaryOperator::Any | BinaryOperator::FRem),
59+
60+
LLVMOpcode::LLVMAnd => matches!(self, BinaryOperator::Any | BinaryOperator::And),
61+
62+
LLVMOpcode::LLVMOr => matches!(
63+
self,
64+
BinaryOperator::Any | BinaryOperator::Or | BinaryOperator::OrDisjoint
65+
),
66+
67+
LLVMOpcode::LLVMXor => matches!(self, BinaryOperator::Any | BinaryOperator::Xor),
68+
69+
LLVMOpcode::LLVMShl => {
70+
matches!(self, BinaryOperator::Any | BinaryOperator::LogicalShiftLeft)
71+
}
72+
73+
LLVMOpcode::LLVMLShr => matches!(
74+
self,
75+
BinaryOperator::Any | BinaryOperator::LogicalShiftRight
76+
),
77+
78+
LLVMOpcode::LLVMAShr => matches!(
79+
self,
80+
BinaryOperator::Any | BinaryOperator::ArithmeticShiftRight
81+
),
5682

5783
_ => false,
5884
}
@@ -69,6 +95,30 @@ pub struct BinaryInstMatcher {
6995
}
7096

7197
impl BinaryInstMatcher {
98+
pub fn create_any(
99+
lhs: Box<dyn InstMatcher>,
100+
rhs: Box<dyn InstMatcher>,
101+
) -> Box<dyn InstMatcher> {
102+
Box::new(BinaryInstMatcher {
103+
lhs_matcher: lhs,
104+
rhs_matcher: rhs,
105+
operator: BinaryOperator::Any,
106+
commutatively: false,
107+
})
108+
}
109+
110+
pub fn create_commutatively_any(
111+
lhs: Box<dyn InstMatcher>,
112+
rhs: Box<dyn InstMatcher>,
113+
) -> Box<dyn InstMatcher> {
114+
Box::new(BinaryInstMatcher {
115+
lhs_matcher: lhs,
116+
rhs_matcher: rhs,
117+
operator: BinaryOperator::Any,
118+
commutatively: true,
119+
})
120+
}
121+
72122
pub fn create_add(
73123
lhs: Box<dyn InstMatcher>,
74124
rhs: Box<dyn InstMatcher>,
@@ -494,8 +544,8 @@ impl InstMatcher for BinaryInstMatcher {
494544
}
495545
}
496546

497-
let rhs = LLVMGetOperand(instruction, 1);
498547
let lhs = LLVMGetOperand(instruction, 0);
548+
let rhs = LLVMGetOperand(instruction, 1);
499549

500550
if self.lhs_matcher.is_match(lhs) && self.rhs_matcher.is_match(rhs) {
501551
return true;

0 commit comments

Comments
 (0)