Skip to content

Commit e7efaed

Browse files
committed
Implement m_const_num Matcher function
1 parent 48fbb10 commit e7efaed

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

docs/matchers/constants.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Function | Parameters | Return | Description |
44
| :------------: | :---------------------: | :---------: | :--------------------------------------------------------------: |
5+
| m_const_num | | InstMatcher | Build Inst Matcher that match constants number value |
56
| m_const_int | | InstMatcher | Build Inst Matcher that match constants int value |
67
| m_zero | | InstMatcher | Build Inst Matcher that match constants int with value 0 |
78
| m_one | | InstMatcher | Build Inst Matcher that match constants int with value 1 |

src/functions/matchers/constants.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ use crate::ir::types::InstMatcherType;
1010
use crate::ir::values::InstMatcherValue;
1111
use crate::matchers::constants::ConstFloatMatcher;
1212
use crate::matchers::constants::ConstIntMatcher;
13+
use crate::matchers::constants::ConstNumberMatcher;
1314
use crate::matchers::constants::ConstPointerNullMatcher;
1415

1516
#[inline(always)]
1617
pub fn register_constants_matchers_functions(map: &mut HashMap<&'static str, StandardFunction>) {
18+
map.insert("m_const_num", match_const_num_inst);
1719
map.insert("m_const_int", match_const_int_inst);
1820
map.insert("m_zero", match_const_zero_inst);
1921
map.insert("m_one", match_const_one_inst);
@@ -27,6 +29,7 @@ pub fn register_constants_matchers_functions(map: &mut HashMap<&'static str, Sta
2729

2830
#[inline(always)]
2931
pub fn register_constants_matchers_function_signatures(map: &mut HashMap<&'static str, Signature>) {
32+
map.insert("m_const_num", matcher_signature_without_parameters());
3033
map.insert("m_const_int", matcher_signature_without_parameters());
3134
map.insert("m_zero", matcher_signature_without_parameters());
3235
map.insert("m_one", matcher_signature_without_parameters());
@@ -51,6 +54,11 @@ pub fn register_constants_matchers_function_signatures(map: &mut HashMap<&'stati
5154
map.insert("m_const_null", matcher_signature_without_parameters());
5255
}
5356

57+
fn match_const_num_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
58+
let matcher = Box::new(ConstNumberMatcher);
59+
Box::new(InstMatcherValue { matcher })
60+
}
61+
5462
fn match_const_int_inst(_values: &[Box<dyn Value>]) -> Box<dyn Value> {
5563
let matcher = Box::new(ConstIntMatcher::create_const_int());
5664
Box::new(InstMatcherValue { matcher })

src/matchers/constants.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use inkwell::llvm_sys::core::{LLVMConstIntGetSExtValue, LLVMGetValueKind, LLVMIsAConstantInt};
1+
use inkwell::llvm_sys::core::LLVMConstIntGetSExtValue;
2+
use inkwell::llvm_sys::core::LLVMGetValueKind;
3+
use inkwell::llvm_sys::core::LLVMIsAConstantFP;
4+
use inkwell::llvm_sys::core::LLVMIsAConstantInt;
25
use inkwell::llvm_sys::prelude::LLVMValueRef;
36
use inkwell::llvm_sys::LLVMValueKind;
47

@@ -94,11 +97,22 @@ impl Matcher<LLVMValueRef> for ConstFloatMatcher {
9497
pub struct ConstPointerNullMatcher;
9598

9699
impl Matcher<LLVMValueRef> for ConstPointerNullMatcher {
100+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
101+
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
102+
unsafe { !LLVMIsAConstantFP(*instruction).is_null() }
103+
}
104+
}
105+
106+
/// Return instruction matcher to check if current value is a constants number
107+
#[derive(Clone)]
108+
pub struct ConstNumberMatcher;
109+
110+
impl Matcher<LLVMValueRef> for ConstNumberMatcher {
97111
#[allow(clippy::not_unsafe_ptr_arg_deref)]
98112
fn is_match(&self, instruction: &LLVMValueRef) -> bool {
99113
unsafe {
100-
let value_kind = LLVMGetValueKind(*instruction);
101-
value_kind == LLVMValueKind::LLVMConstantPointerNullValueKind
114+
!LLVMIsAConstantInt(*instruction).is_null()
115+
|| !LLVMIsAConstantFP(*instruction).is_null()
102116
}
103117
}
104118
}

0 commit comments

Comments
 (0)