about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2015-03-27 01:37:10 +0100
committerFelix S. Klock II <pnkfelix@pnkfx.org>2015-04-01 02:56:07 +0200
commit2e93e386fd228176aeb1100bfdf961bdae2b51b9 (patch)
tree309396a0a5033094a186587409bfe272869aee96
parent7875dae83fac23fdf59765eb548c2237850d6b15 (diff)
downloadrust-2e93e386fd228176aeb1100bfdf961bdae2b51b9.tar.gz
rust-2e93e386fd228176aeb1100bfdf961bdae2b51b9.zip
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.)
-rw-r--r--src/librustc_llvm/lib.rs1
-rw-r--r--src/librustc_trans/trans/common.rs26
2 files changed, 27 insertions, 0 deletions
diff --git a/src/librustc_llvm/lib.rs b/src/librustc_llvm/lib.rs
index c7b5b2e7534..2c4305d3c45 100644
--- a/src/librustc_llvm/lib.rs
+++ b/src/librustc_llvm/lib.rs
@@ -1976,6 +1976,7 @@ extern {
     pub fn LLVMIsAArgument(value_ref: ValueRef) -> ValueRef;
 
     pub fn LLVMIsAAllocaInst(value_ref: ValueRef) -> ValueRef;
+    pub fn LLVMIsAConstantInt(value_ref: ValueRef) -> ValueRef;
 
     pub fn LLVMInitializeX86TargetInfo();
     pub fn LLVMInitializeX86Target();
diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs
index 745098d6e87..995f3caf588 100644
--- a/src/librustc_trans/trans/common.rs
+++ b/src/librustc_trans/trans/common.rs
@@ -963,6 +963,32 @@ pub fn const_to_uint(v: ValueRef) -> u64 {
     }
 }
 
+fn is_const_integral(v: ValueRef) -> bool {
+    unsafe {
+        !llvm::LLVMIsAConstantInt(v).is_null()
+    }
+}
+
+pub fn const_to_opt_int(v: ValueRef) -> Option<i64> {
+    unsafe {
+        if is_const_integral(v) {
+            Some(llvm::LLVMConstIntGetSExtValue(v))
+        } else {
+            None
+        }
+    }
+}
+
+pub fn const_to_opt_uint(v: ValueRef) -> Option<u64> {
+    unsafe {
+        if is_const_integral(v) {
+            Some(llvm::LLVMConstIntGetZExtValue(v))
+        } else {
+            None
+        }
+    }
+}
+
 pub fn is_undef(val: ValueRef) -> bool {
     unsafe {
         llvm::LLVMIsUndef(val) != False