Skip to content

Commit 40545eb

Browse files
committed
Implement m_fpext and m_ext Cast Matchers
1 parent 11e2432 commit 40545eb

File tree

4 files changed

+55
-28
lines changed

4 files changed

+55
-28
lines changed

docs/matchers/cast.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
## Cast Instructions Matchers
22

3-
| Function | Parameters | Return | Description |
4-
| :---------------: | :-------------------: | :---------: | :--------------------------------------------------------: |
5-
| m_trunc | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers trunc instruction |
6-
| m_fp_to_ui | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to ui instruction |
7-
| m_fp_to_si | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to si instruction |
8-
| m_fp_trunc | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers trunc instruction |
9-
| m_int_to_ptr | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers IntToPtr instruction |
10-
| m_ptr_to_int | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers PtrToInt instruction |
11-
| m_zext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers zext instruction |
12-
| m_sext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers sext instruction |
13-
| m_bit_cast | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers Bit cast instruction |
14-
| m_addr_space_cast | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers AddrSpaceCast instruction |
3+
| Function | Parameters | Return | Description |
4+
| :---------------: | :-------------------: | :---------: | :--------------------------------------------------------------: |
5+
| m_trunc | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers trunc instruction |
6+
| m_fp_to_ui | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to ui instruction |
7+
| m_fp_to_si | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fp to si instruction |
8+
| m_fp_trunc | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers trunc instruction |
9+
| m_int_to_ptr | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers IntToPtr instruction |
10+
| m_ptr_to_int | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers PtrToInt instruction |
11+
| m_ext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers zext, sext or fpext instruction |
12+
| m_fpext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers fpext instruction |
13+
| m_zext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers zext instruction |
14+
| m_sext | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers sext instruction |
15+
| m_bit_cast | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers Bit cast instruction |
16+
| m_addr_space_cast | (value: InstMatcher?) | InstMatcher | Build Inst Matcher that matchers AddrSpaceCast instruction |

src/functions/matchers/cast.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ pub fn register_cast_matchers_function(map: &mut HashMap<&'static str, StandardF
1818
map.insert("m_fp_trunc", match_fp_trunc);
1919
map.insert("m_int_to_ptr", match_int_to_ptr);
2020
map.insert("m_ptr_to_int", match_ptr_to_int);
21+
map.insert("m_ext", match_ext);
22+
map.insert("m_fpext", match_fp_ext);
2123
map.insert("m_zext", match_zext);
2224
map.insert("m_sext", match_sext);
2325
map.insert("m_bit_cast", match_bit_cast);
@@ -32,6 +34,8 @@ pub fn register_cast_matchers_function_signatures(map: &mut HashMap<&'static str
3234
map.insert("m_fp_trunc", cast_function_signature());
3335
map.insert("m_int_to_ptr", cast_function_signature());
3436
map.insert("m_ptr_to_int", cast_function_signature());
37+
map.insert("m_ext", cast_function_signature());
38+
map.insert("m_fpext", cast_function_signature());
3539
map.insert("m_zext", cast_function_signature());
3640
map.insert("m_sext", cast_function_signature());
3741
map.insert("m_bit_cast", cast_function_signature());
@@ -80,6 +84,18 @@ fn match_ptr_to_int(values: &[Box<dyn Value>]) -> Box<dyn Value> {
8084
Box::new(InstMatcherValue { matcher })
8185
}
8286

87+
fn match_ext(values: &[Box<dyn Value>]) -> Box<dyn Value> {
88+
let value_matcher = single_optional_matcher_value(values);
89+
let matcher = Box::new(CastInstMatcher::create_ext(value_matcher));
90+
Box::new(InstMatcherValue { matcher })
91+
}
92+
93+
fn match_fp_ext(values: &[Box<dyn Value>]) -> Box<dyn Value> {
94+
let value_matcher = single_optional_matcher_value(values);
95+
let matcher = Box::new(CastInstMatcher::create_fpext(value_matcher));
96+
Box::new(InstMatcherValue { matcher })
97+
}
98+
8399
fn match_zext(values: &[Box<dyn Value>]) -> Box<dyn Value> {
84100
let value_matcher = single_optional_matcher_value(values);
85101
let matcher = Box::new(CastInstMatcher::create_zext(value_matcher));

src/matchers/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@ impl Matcher<LLVMValueRef> for BinaryInstMatcher {
589589
#[allow(clippy::not_unsafe_ptr_arg_deref)]
590590
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
591591
unsafe {
592-
let operator = LLVMGetInstructionOpcode(*instruction);
593-
if self.operator.match_llvm_opcode(operator) {
592+
let opcode = LLVMGetInstructionOpcode(*instruction);
593+
if self.operator.match_llvm_opcode(opcode) {
594594
if self.operator == BinaryOperator::OrDisjoint {
595595
let instruction_string = LLVMPrintValueToString(*instruction);
596596
let instruction_cstr = CStr::from_ptr(instruction_string).to_owned();

src/matchers/cast.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum CastMatcherKind {
1212
FPToUI,
1313
FPToSI,
1414
FPTrunc,
15+
Ext,
1516
FPExt,
1617
ZExt,
1718
SExt,
@@ -20,19 +21,22 @@ enum CastMatcherKind {
2021
}
2122

2223
impl CastMatcherKind {
23-
pub fn llvm_opcode(&self) -> LLVMOpcode {
24-
match self {
25-
CastMatcherKind::Trunc => LLVMOpcode::LLVMTrunc,
26-
CastMatcherKind::FPToUI => LLVMOpcode::LLVMFPToUI,
27-
CastMatcherKind::FPToSI => LLVMOpcode::LLVMFPToSI,
28-
CastMatcherKind::FPTrunc => LLVMOpcode::LLVMFPTrunc,
29-
CastMatcherKind::FPExt => LLVMOpcode::LLVMFPExt,
30-
CastMatcherKind::IntToPtr => LLVMOpcode::LLVMIntToPtr,
31-
CastMatcherKind::PtrToInt => LLVMOpcode::LLVMPtrToInt,
32-
CastMatcherKind::ZExt => LLVMOpcode::LLVMZExt,
33-
CastMatcherKind::SExt => LLVMOpcode::LLVMSExt,
34-
CastMatcherKind::BitCast => LLVMOpcode::LLVMBitCast,
35-
CastMatcherKind::AddrSpaceCast => LLVMOpcode::LLVMAddrSpaceCast,
24+
pub fn match_llvm_opcode(&self, llvm_op: LLVMOpcode) -> bool {
25+
match llvm_op {
26+
LLVMOpcode::LLVMTrunc => matches!(self, CastMatcherKind::Trunc),
27+
LLVMOpcode::LLVMFPToUI => matches!(self, CastMatcherKind::FPToUI),
28+
LLVMOpcode::LLVMFPToSI => matches!(self, CastMatcherKind::FPToSI),
29+
LLVMOpcode::LLVMFPTrunc => matches!(self, CastMatcherKind::FPTrunc),
30+
31+
LLVMOpcode::LLVMFPExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::FPExt),
32+
LLVMOpcode::LLVMZExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::ZExt),
33+
LLVMOpcode::LLVMSExt => matches!(self, CastMatcherKind::Ext | CastMatcherKind::SExt),
34+
35+
LLVMOpcode::LLVMIntToPtr => matches!(self, CastMatcherKind::IntToPtr),
36+
LLVMOpcode::LLVMPtrToInt => matches!(self, CastMatcherKind::PtrToInt),
37+
LLVMOpcode::LLVMBitCast => matches!(self, CastMatcherKind::BitCast),
38+
LLVMOpcode::LLVMAddrSpaceCast => matches!(self, CastMatcherKind::AddrSpaceCast),
39+
_ => false,
3640
}
3741
}
3842
}
@@ -69,6 +73,10 @@ impl CastInstMatcher {
6973
CastInstMatcher::new(value_matcher, CastMatcherKind::FPTrunc)
7074
}
7175

76+
pub fn create_ext(value_matcher: Box<dyn Matcher<LLVMValueRef>>) -> CastInstMatcher {
77+
CastInstMatcher::new(value_matcher, CastMatcherKind::Ext)
78+
}
79+
7280
pub fn create_fpext(value_matcher: Box<dyn Matcher<LLVMValueRef>>) -> CastInstMatcher {
7381
CastInstMatcher::new(value_matcher, CastMatcherKind::FPExt)
7482
}
@@ -104,7 +112,8 @@ impl Matcher<LLVMValueRef> for CastInstMatcher {
104112
#[allow(clippy::not_unsafe_ptr_arg_deref)]
105113
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
106114
unsafe {
107-
if self.kind.llvm_opcode() != LLVMGetInstructionOpcode(*instruction) {
115+
let opcode = LLVMGetInstructionOpcode(*instruction);
116+
if !self.kind.match_llvm_opcode(opcode) {
108117
return false;
109118
}
110119

0 commit comments

Comments
 (0)