|
2 | 2 |
|
3 | 3 | //! Code that is useful in various codegen modules. |
4 | 4 |
|
5 | | -use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef}; |
| 5 | +use crate::llvm::{self, True, False, Bool, BasicBlock, OperandBundleDef, ConstantInt}; |
6 | 6 | use crate::abi; |
7 | 7 | use crate::consts; |
8 | 8 | use crate::type_::Type; |
@@ -246,30 +246,22 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { |
246 | 246 | } |
247 | 247 |
|
248 | 248 | fn const_to_opt_uint(&self, v: &'ll Value) -> Option<u64> { |
249 | | - if is_const_integral(v) { |
250 | | - unsafe { |
251 | | - Some(llvm::LLVMConstIntGetZExtValue(v)) |
252 | | - } |
253 | | - } else { |
254 | | - None |
255 | | - } |
| 249 | + try_as_const_integral(v).map(|v| unsafe { |
| 250 | + llvm::LLVMConstIntGetZExtValue(v) |
| 251 | + }) |
256 | 252 | } |
257 | 253 |
|
258 | 254 | fn const_to_opt_u128(&self, v: &'ll Value, sign_ext: bool) -> Option<u128> { |
259 | | - unsafe { |
260 | | - if is_const_integral(v) { |
261 | | - let (mut lo, mut hi) = (0u64, 0u64); |
262 | | - let success = llvm::LLVMRustConstInt128Get(v, sign_ext, |
263 | | - &mut hi, &mut lo); |
264 | | - if success { |
265 | | - Some(hi_lo_to_u128(lo, hi)) |
266 | | - } else { |
267 | | - None |
268 | | - } |
| 255 | + try_as_const_integral(v).and_then(|v| unsafe { |
| 256 | + let (mut lo, mut hi) = (0u64, 0u64); |
| 257 | + let success = llvm::LLVMRustConstInt128Get(v, sign_ext, |
| 258 | + &mut hi, &mut lo); |
| 259 | + if success { |
| 260 | + Some(hi_lo_to_u128(lo, hi)) |
269 | 261 | } else { |
270 | 262 | None |
271 | 263 | } |
272 | | - } |
| 264 | + }) |
273 | 265 | } |
274 | 266 |
|
275 | 267 | fn scalar_to_backend( |
@@ -387,8 +379,8 @@ fn hi_lo_to_u128(lo: u64, hi: u64) -> u128 { |
387 | 379 | ((hi as u128) << 64) | (lo as u128) |
388 | 380 | } |
389 | 381 |
|
390 | | -fn is_const_integral(v: &'ll Value) -> bool { |
| 382 | +fn try_as_const_integral(v: &'ll Value) -> Option<&'ll ConstantInt> { |
391 | 383 | unsafe { |
392 | | - llvm::LLVMIsAConstantInt(v).is_some() |
| 384 | + llvm::LLVMIsAConstantInt(v) |
393 | 385 | } |
394 | 386 | } |
0 commit comments