Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions cranelift/codegen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,14 @@ fn write_instruction(
// Write out the result values, if any.
let mut has_results = false;
for r in func.dfg.inst_results(inst) {
let r = func.dfg.resolve_aliases(*r);
if !has_results {
has_results = true;
write!(w, "{}", r)?;
} else {
write!(w, ", {}", r)?;
}
if let Some(f) = &func.dfg.facts[*r] {
if let Some(f) = &func.dfg.facts[r] {
write!(w, " ! {}", f)?;
}
}
Expand Down Expand Up @@ -388,43 +389,53 @@ fn write_instruction(
pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt::Result {
let pool = &dfg.value_lists;
let jump_tables = &dfg.jump_tables;

// Resolve a value to its alias, if any.
let v = |v: Value| dfg.resolve_aliases(v);

// Resolve a list of values to their aliases and format them.
let vs = |vs: &[_]| {
let vs: Vec<_> = vs.iter().copied().map(v).collect();
DisplayValues(&vs).to_string()
};

use crate::ir::instructions::InstructionData::*;
match dfg.insts[inst] {
AtomicRmw { op, args, .. } => write!(w, " {} {}, {}", op, args[0], args[1]),
AtomicCas { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
LoadNoOffset { flags, arg, .. } => write!(w, "{} {}", flags, arg),
StoreNoOffset { flags, args, .. } => write!(w, "{} {}, {}", flags, args[0], args[1]),
Unary { arg, .. } => write!(w, " {}", arg),
AtomicRmw { op, args, .. } => write!(w, " {} {}, {}", op, v(args[0]), v(args[1])),
AtomicCas { args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), v(args[2])),
LoadNoOffset { flags, arg, .. } => write!(w, "{} {}", flags, v(arg)),
StoreNoOffset { flags, args, .. } => write!(w, "{} {}, {}", flags, v(args[0]), v(args[1])),
Unary { arg, .. } => write!(w, " {}", v(arg)),
UnaryImm { imm, .. } => write!(w, " {}", imm),
UnaryIeee32 { imm, .. } => write!(w, " {}", imm),
UnaryIeee64 { imm, .. } => write!(w, " {}", imm),
UnaryGlobalValue { global_value, .. } => write!(w, " {}", global_value),
UnaryConst {
constant_handle, ..
} => write!(w, " {}", constant_handle),
Binary { args, .. } => write!(w, " {}, {}", args[0], args[1]),
BinaryImm8 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
BinaryImm64 { arg, imm, .. } => write!(w, " {}, {}", arg, imm),
Ternary { args, .. } => write!(w, " {}, {}, {}", args[0], args[1], args[2]),
Binary { args, .. } => write!(w, " {}, {}", v(args[0]), v(args[1])),
BinaryImm8 { arg, imm, .. } => write!(w, " {}, {}", v(arg), imm),
BinaryImm64 { arg, imm, .. } => write!(w, " {}, {}", v(arg), imm),
Ternary { args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), v(args[2])),
MultiAry { ref args, .. } => {
if args.is_empty() {
write!(w, "")
} else {
write!(w, " {}", DisplayValues(args.as_slice(pool)))
write!(w, " {}", vs(args.as_slice(pool)))
}
}
NullAry { .. } => write!(w, " "),
TernaryImm8 { imm, args, .. } => write!(w, " {}, {}, {}", args[0], args[1], imm),
TernaryImm8 { imm, args, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), imm),
Shuffle { imm, args, .. } => {
let data = dfg.immediates.get(imm).expect(
"Expected the shuffle mask to already be inserted into the immediates table",
);
write!(w, " {}, {}, {}", args[0], args[1], data)
write!(w, " {}, {}, {}", v(args[0]), v(args[1]), data)
}
IntCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
IntCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, v(args[0]), v(args[1])),
IntCompareImm { cond, arg, imm, .. } => write!(w, " {} {}, {}", cond, arg, imm),
IntAddTrap { args, code, .. } => write!(w, " {}, {}, {}", args[0], args[1], code),
FloatCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, args[0], args[1]),
IntAddTrap { args, code, .. } => write!(w, " {}, {}, {}", v(args[0]), v(args[1]), code),
FloatCompare { cond, args, .. } => write!(w, " {} {}, {}", cond, v(args[0]), v(args[1])),
Jump { destination, .. } => {
write!(w, " {}", destination.display(pool))
}
Expand All @@ -433,26 +444,20 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
blocks: [block_then, block_else],
..
} => {
write!(w, " {}, {}", arg, block_then.display(pool))?;
write!(w, " {}, {}", v(arg), block_then.display(pool))?;
write!(w, ", {}", block_else.display(pool))
}
BranchTable { arg, table, .. } => {
write!(w, " {}, {}", arg, jump_tables[table].display(pool))
write!(w, " {}, {}", v(arg), jump_tables[table].display(pool))
}
Call {
func_ref, ref args, ..
} => write!(w, " {}({})", func_ref, DisplayValues(args.as_slice(pool))),
} => write!(w, " {}({})", func_ref, vs(args.as_slice(pool))),
CallIndirect {
sig_ref, ref args, ..
} => {
let args = args.as_slice(pool);
write!(
w,
" {}, {}({})",
sig_ref,
args[0],
DisplayValues(&args[1..])
)
write!(w, " {}, {}({})", sig_ref, args[0], vs(&args[1..]))
}
FuncAddr { func_ref, .. } => write!(w, " {}", func_ref),
StackLoad {
Expand All @@ -463,7 +468,7 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
stack_slot,
offset,
..
} => write!(w, " {}, {}{}", arg, stack_slot, offset),
} => write!(w, " {}, {}{}", v(arg), stack_slot, offset),
DynamicStackLoad {
dynamic_stack_slot, ..
} => write!(w, " {}", dynamic_stack_slot),
Expand All @@ -474,15 +479,15 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
} => write!(w, " {}, {}", arg, dynamic_stack_slot),
Load {
flags, arg, offset, ..
} => write!(w, "{} {}{}", flags, arg, offset),
} => write!(w, "{} {}{}", flags, v(arg), offset),
Store {
flags,
args,
offset,
..
} => write!(w, "{} {}, {}{}", flags, args[0], args[1], offset),
} => write!(w, "{} {}, {}{}", flags, v(args[0]), v(args[1]), offset),
Trap { code, .. } => write!(w, " {}", code),
CondTrap { arg, code, .. } => write!(w, " {}, {}", arg, code),
CondTrap { arg, code, .. } => write!(w, " {}, {}", v(arg), code),
}?;

let mut sep = " ; ";
Expand All @@ -497,7 +502,7 @@ pub fn write_operands(w: &mut dyn Write, dfg: &DataFlowGraph, inst: Inst) -> fmt
} => constant_handle.to_string(),
_ => continue,
};
write!(w, "{}{} = {}", sep, arg, imm)?;
write!(w, "{}{} = {}", sep, v(arg), imm)?;
sep = ", ";
}
}
Expand Down Expand Up @@ -603,7 +608,7 @@ mod tests {
}
assert_eq!(
func.to_string(),
"function u0:0() fast {\nblock0(v3: i32):\n v0 -> v3\n v2 -> v0\n v4 = iconst.i32 42\n v5 = iadd v0, v0\n v1 -> v5\n v6 = iconst.i32 23\n v7 = iadd v1, v1\n}\n"
"function u0:0() fast {\nblock0(v3: i32):\n v0 -> v3\n v2 -> v0\n v4 = iconst.i32 42\n v5 = iadd v3, v3\n v1 -> v5\n v6 = iconst.i32 23\n v7 = iadd v5, v5\n}\n"
);
}

Expand Down
40 changes: 20 additions & 20 deletions cranelift/frontend/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,8 @@ block0:
v1 -> v4
v3 = iconst.i64 0
v0 -> v3
v2 = call fn0(v1, v0, v1) ; v1 = 0, v0 = 0, v1 = 0
return v1 ; v1 = 0
v2 = call fn0(v4, v3, v4) ; v4 = 0, v3 = 0, v4 = 0
return v4 ; v4 = 0
}
",
);
Expand Down Expand Up @@ -1347,9 +1347,9 @@ block0:
v1 -> v4
v3 = iconst.i64 0
v0 -> v3
v2 = load.i64 aligned v0 ; v0 = 0
store aligned v2, v1 ; v1 = 0
return v1 ; v1 = 0
v2 = load.i64 aligned v3 ; v3 = 0
store aligned v2, v4 ; v4 = 0
return v4 ; v4 = 0
}
",
);
Expand Down Expand Up @@ -1405,8 +1405,8 @@ block0:
v4 = iconst.i64 0
v0 -> v4
v2 = iconst.i64 8192
v3 = call fn0(v1, v0, v2) ; v1 = 0, v0 = 0, v2 = 8192
return v1 ; v1 = 0
v3 = call fn0(v5, v4, v2) ; v5 = 0, v4 = 0, v2 = 8192
return v5 ; v5 = 0
}
",
);
Expand Down Expand Up @@ -1445,8 +1445,8 @@ block0:
v2 = iconst.i64 0
v0 -> v2
v1 = iconst.i64 0x0101_0101_0101_0101
store aligned v1, v0 ; v1 = 0x0101_0101_0101_0101, v0 = 0
return v0 ; v0 = 0
store aligned v1, v2 ; v1 = 0x0101_0101_0101_0101, v2 = 0
return v2 ; v2 = 0
}
",
);
Expand Down Expand Up @@ -1490,8 +1490,8 @@ block0:
v1 = iconst.i8 1
v2 = iconst.i64 8192
v3 = uextend.i32 v1 ; v1 = 1
v4 = call fn0(v0, v3, v2) ; v0 = 0, v2 = 8192
return v0 ; v0 = 0
v4 = call fn0(v5, v3, v2) ; v5 = 0, v2 = 8192
return v5 ; v5 = 0
}
",
);
Expand Down Expand Up @@ -1555,7 +1555,7 @@ block0:
v1 -> v5
v4 = iconst.i64 0
v0 -> v4
v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 0
v3 = call fn0(v4, v5, v6) ; v4 = 0, v5 = 0, v6 = 0
return v3
}
",
Expand Down Expand Up @@ -1599,8 +1599,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i8 aligned v0 ; v0 = 0
v3 = load.i8 aligned v1 ; v1 = 0
v2 = load.i8 aligned v5 ; v5 = 0
v3 = load.i8 aligned v6 ; v6 = 0
v4 = icmp ugt v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1628,8 +1628,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i32 aligned v0 ; v0 = 0
v3 = load.i32 aligned v1 ; v1 = 0
v2 = load.i32 aligned v5 ; v5 = 0
v3 = load.i32 aligned v6 ; v6 = 0
v4 = icmp eq v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1657,8 +1657,8 @@ block0:
v1 -> v6
v5 = iconst.i64 0
v0 -> v5
v2 = load.i128 v0 ; v0 = 0
v3 = load.i128 v1 ; v1 = 0
v2 = load.i128 v5 ; v5 = 0
v3 = load.i128 v6 ; v6 = 0
v4 = icmp ne v2, v3
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1690,7 +1690,7 @@ block0:
v5 = iconst.i64 0
v0 -> v5
v2 = iconst.i64 3
v3 = call fn0(v0, v1, v2) ; v0 = 0, v1 = 0, v2 = 3
v3 = call fn0(v5, v6, v2) ; v5 = 0, v6 = 0, v2 = 3
v4 = icmp_imm sge v3, 0
return v4",
|builder, target, x, y| {
Expand Down Expand Up @@ -1801,7 +1801,7 @@ block0:
v1 -> v4
v3 = vconst.i8x16 const0
v0 -> v3
return v0, v1, v2 ; v0 = const0, v1 = const0
return v3, v4, v6 ; v3 = const0, v4 = const0
}
",
);
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/dead-code.wat
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
;; @0023 jump block2
;;
;; block2:
;; @0029 brif.i32 v3, block4, block5
;; @0029 brif.i32 v2, block4, block5
;;
;; block5:
;; @002b jump block2
Expand Down
2 changes: 1 addition & 1 deletion tests/disas/if-unreachable-else-params.wat
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
;; @0045 jump block2(v3) ; v3 = 35
;;
;; block2(v4: i32):
;; @0049 brif.i32 v5, block4, block6(v4)
;; @0049 brif.i32 v2, block4, block6(v4)
;;
;; block4:
;; @004b v7 = uextend.i64 v4
Expand Down
20 changes: 10 additions & 10 deletions tests/disas/table-get-fixed-size.wat
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
;; @0054 v4 = iconst.i32 7
;; @0054 v5 = icmp uge v3, v4 ; v3 = 0, v4 = 7
;; @0054 v6 = uextend.i64 v3 ; v3 = 0
;; @0054 v7 = load.i64 notrap aligned v25+72
;; @0054 v7 = load.i64 notrap aligned v0+72
;; v26 = iconst.i64 3
;; @0054 v8 = ishl v6, v26 ; v26 = 3
;; @0054 v9 = iadd v7, v8
Expand All @@ -44,7 +44,7 @@
;; @0054 brif v13, block2, block3
;;
;; block3:
;; @0054 v15 = load.i64 notrap aligned v14+32
;; @0054 v15 = load.i64 notrap aligned v0+32
;; @0054 v16 = load.i64 notrap aligned v15
;; @0054 v17 = load.i64 notrap aligned v15+8
;; @0054 v18 = icmp eq v16, v17
Expand All @@ -62,16 +62,16 @@
;; @0054 jump block2
;;
;; block4:
;; @0054 v20 = load.i64 notrap aligned readonly v19+56
;; @0054 v20 = load.i64 notrap aligned readonly v0+56
;; @0054 v21 = load.i64 notrap aligned readonly v20+208
;; @0054 call_indirect sig0, v21(v19, v12)
;; @0054 call_indirect sig0, v21(v0, v12)
;; @0054 jump block2
;;
;; block2:
;; @0056 jump block1
;;
;; block1:
;; @0056 return v2
;; @0056 return v12
;; }
;;
;; function u0:1(i64 vmctx, i64, i32) -> r64 fast {
Expand All @@ -90,7 +90,7 @@
;; @005b v4 = iconst.i32 7
;; @005b v5 = icmp uge v2, v4 ; v4 = 7
;; @005b v6 = uextend.i64 v2
;; @005b v7 = load.i64 notrap aligned v25+72
;; @005b v7 = load.i64 notrap aligned v0+72
;; v26 = iconst.i64 3
;; @005b v8 = ishl v6, v26 ; v26 = 3
;; @005b v9 = iadd v7, v8
Expand All @@ -102,7 +102,7 @@
;; @005b brif v13, block2, block3
;;
;; block3:
;; @005b v15 = load.i64 notrap aligned v14+32
;; @005b v15 = load.i64 notrap aligned v0+32
;; @005b v16 = load.i64 notrap aligned v15
;; @005b v17 = load.i64 notrap aligned v15+8
;; @005b v18 = icmp eq v16, v17
Expand All @@ -120,14 +120,14 @@
;; @005b jump block2
;;
;; block4:
;; @005b v20 = load.i64 notrap aligned readonly v19+56
;; @005b v20 = load.i64 notrap aligned readonly v0+56
;; @005b v21 = load.i64 notrap aligned readonly v20+208
;; @005b call_indirect sig0, v21(v19, v12)
;; @005b call_indirect sig0, v21(v0, v12)
;; @005b jump block2
;;
;; block2:
;; @005d jump block1
;;
;; block1:
;; @005d return v3
;; @005d return v12
;; }
Loading