Skip to content

Commit 2e93e38

Browse files
committed
rust_llvm: Add way to reflectively ask if a ValueRef is a known constant int.
Add option-returning variants to `const_to_int`/`const_to_uint` that never assert fail. (These will be used for overflow checking from rustc_trans::trans::consts.)
1 parent 7875dae commit 2e93e38

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/librustc_llvm/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,7 @@ extern {
19761976
pub fn LLVMIsAArgument(value_ref: ValueRef) -> ValueRef;
19771977

19781978
pub fn LLVMIsAAllocaInst(value_ref: ValueRef) -> ValueRef;
1979+
pub fn LLVMIsAConstantInt(value_ref: ValueRef) -> ValueRef;
19791980

19801981
pub fn LLVMInitializeX86TargetInfo();
19811982
pub fn LLVMInitializeX86Target();

src/librustc_trans/trans/common.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,32 @@ pub fn const_to_uint(v: ValueRef) -> u64 {
963963
}
964964
}
965965

966+
fn is_const_integral(v: ValueRef) -> bool {
967+
unsafe {
968+
!llvm::LLVMIsAConstantInt(v).is_null()
969+
}
970+
}
971+
972+
pub fn const_to_opt_int(v: ValueRef) -> Option<i64> {
973+
unsafe {
974+
if is_const_integral(v) {
975+
Some(llvm::LLVMConstIntGetSExtValue(v))
976+
} else {
977+
None
978+
}
979+
}
980+
}
981+
982+
pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
983+
unsafe {
984+
if is_const_integral(v) {
985+
Some(llvm::LLVMConstIntGetZExtValue(v))
986+
} else {
987+
None
988+
}
989+
}
990+
}
991+
966992
pub fn is_undef(val: ValueRef) -> bool {
967993
unsafe {
968994
llvm::LLVMIsUndef(val) != False

0 commit comments

Comments
 (0)