diff --git a/cranelift/filetests/filetests/preopt/constant_fold.clif b/cranelift/filetests/filetests/preopt/constant_fold.clif new file mode 100644 index 0000000000..f0ea9539ba --- /dev/null +++ b/cranelift/filetests/filetests/preopt/constant_fold.clif @@ -0,0 +1,19 @@ +test preopt +target x86_64 + +function %constant_fold(f64) -> f64 { +ebb0(v0: f64): + v1 = f64const 0x1.0000000000000p0 + v2 = f64const 0x1.0000000000000p1 + v3 = fadd v1, v2 + v4 = fadd v3, v0 + return v4 +} +; sameln: function %constant_fold(f64) -> f64 fast { +; nextln: ebb0(v0: f64): +; nextln: v1 = f64const 0x1.0000000000000p0 +; nextln: v2 = f64const 0x1.0000000000000p1 +; nextln: v3 = f64const 0x1.8000000000000p1 +; nextln: v4 = fadd v3, v0 +; nextln: return v4 +; nextln: } diff --git a/cranelift/preopt/src/constant_folding.rs b/cranelift/preopt/src/constant_folding.rs index 2d7c1c2dbb..018ac04a7e 100644 --- a/cranelift/preopt/src/constant_folding.rs +++ b/cranelift/preopt/src/constant_folding.rs @@ -2,7 +2,7 @@ use cranelift_codegen::{ cursor::{Cursor, FuncCursor}, - ir::{self, InstBuilder}, + ir::{self, dfg::ValueDef, InstBuilder}, }; // use rustc_apfloat::{ // ieee::{Double, Single}, @@ -69,7 +69,10 @@ pub fn fold_constants(func: &mut ir::Function) { fn resolve_value_to_imm(dfg: &ir::DataFlowGraph, value: ir::Value) -> Option { let original = dfg.resolve_aliases(value); - let inst = dfg.value_def(original).unwrap_inst(); + let inst = match dfg.value_def(original) { + ValueDef::Result(inst, _) => inst, + ValueDef::Param(_, _) => return None, + }; use self::ir::{InstructionData::*, Opcode::*}; match dfg[inst] {