Reuse the DominatorTree postorder travesal in BlockLoweringOrder (#5843)

* Rework the blockorder module to reuse the dom tree's cfg postorder

* Update domtree tests

* Treat br_table with an empty jump table as multiple block exits

* Bless tests

* Change branch_idx to succ_idx and fix the comment
This commit is contained in:
Trevor Elliott
2023-02-23 14:05:20 -08:00
committed by GitHub
parent 4314210162
commit 8abfe928d6
175 changed files with 2936 additions and 3186 deletions

View File

@@ -1,5 +1,6 @@
//! ARM 64-bit Instruction Set Architecture.
use crate::dominator_tree::DominatorTree;
use crate::ir::condcodes::IntCC;
use crate::ir::{Function, Type};
use crate::isa::aarch64::settings as aarch64_settings;
@@ -56,11 +57,12 @@ impl AArch64Backend {
fn compile_vcode(
&self,
func: &Function,
domtree: &DominatorTree,
) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
let emit_info = EmitInfo::new(self.flags.clone());
let sigs = SigSet::new::<abi::AArch64MachineDeps>(func, &self.flags)?;
let abi = abi::AArch64Callee::new(func, self, &self.isa_flags, &sigs)?;
compile::compile::<AArch64Backend>(func, self, abi, emit_info, sigs)
compile::compile::<AArch64Backend>(func, domtree, self, abi, emit_info, sigs)
}
}
@@ -68,9 +70,10 @@ impl TargetIsa for AArch64Backend {
fn compile_function(
&self,
func: &Function,
domtree: &DominatorTree,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let (vcode, regalloc_result) = self.compile_vcode(func)?;
let (vcode, regalloc_result) = self.compile_vcode(func, domtree)?;
let emit_result = vcode.emit(
&regalloc_result,
@@ -241,6 +244,8 @@ pub fn isa_builder(triple: Triple) -> IsaBuilder {
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::ir::types::*;
use crate::ir::{AbiParam, Function, InstBuilder, JumpTableData, Signature, UserFuncName};
use crate::isa::CallConv;
@@ -275,7 +280,12 @@ mod test {
shared_flags,
isa_flags,
);
let buffer = backend.compile_function(&mut func, false).unwrap().buffer;
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let buffer = backend
.compile_function(&mut func, &domtree, false)
.unwrap()
.buffer;
let code = buffer.data();
// To update this comment, write the golden bytes to a file, and run the following command
@@ -328,8 +338,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -340,21 +352,22 @@ mod test {
// 0: 52824689 mov w9, #0x1234 // #4660
// 4: 0b09000b add w11, w0, w9
// 8: 2a0b03ea mov w10, w11
// c: b50000aa cbnz x10, 0x20
// 10: 5282468c mov w12, #0x1234 // #4660
// 14: 0b0c016e add w14, w11, w12
// 18: 2a0e03ed mov w13, w14
// 1c: b5ffffad cbnz x13, 0x10
// 20: 2a0b03e0 mov w0, w11
// 24: b5ffff60 cbnz x0, 0x10
// 28: 52824681 mov w1, #0x1234 // #4660
// 2c: 4b010160 sub w0, w11, w1
// 30: d65f03c0 ret
// c: b40000ca cbz x10, 0x24
// 10: 2a0b03ed mov w13, w11
// 14: b500008d cbnz x13, 0x24
// 18: 5282468e mov w14, #0x1234 // #4660
// 1c: 4b0e0160 sub w0, w11, w14
// 20: d65f03c0 ret
// 24: 5282468f mov w15, #0x1234 // #4660
// 28: 0b0f0161 add w1, w11, w15
// 2c: 2a0103e0 mov w0, w1
// 30: b5ffffa0 cbnz x0, 0x24
// 34: 17fffff7 b 0x10
let golden = vec![
137, 70, 130, 82, 11, 0, 9, 11, 234, 3, 11, 42, 170, 0, 0, 181, 140, 70, 130, 82, 110,
1, 12, 11, 237, 3, 14, 42, 173, 255, 255, 181, 224, 3, 11, 42, 96, 255, 255, 181, 129,
70, 130, 82, 96, 1, 1, 75, 192, 3, 95, 214,
137, 70, 130, 82, 11, 0, 9, 11, 234, 3, 11, 42, 202, 0, 0, 180, 237, 3, 11, 42, 141, 0,
0, 181, 142, 70, 130, 82, 96, 1, 14, 75, 192, 3, 95, 214, 143, 70, 130, 82, 97, 1, 15,
11, 224, 3, 1, 42, 160, 255, 255, 181, 247, 255, 255, 23,
];
assert_eq!(code, &golden[..]);
@@ -409,8 +422,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -419,7 +434,7 @@ mod test {
// > aarch64-linux-gnu-objdump -b binary -D <file> -m aarch64
//
// 0: 7100081f cmp w0, #0x2
// 4: 54000122 b.cs 0x28 // b.hs, b.nlast
// 4: 540001a2 b.cs 0x38 // b.hs, b.nlast
// 8: 9a8023e8 csel x8, xzr, x0, cs // cs = hs, nlast
// c: d503229f csdb
// 10: 10000087 adr x7, 0x20
@@ -427,18 +442,18 @@ mod test {
// 18: 8b0800e7 add x7, x7, x8
// 1c: d61f00e0 br x7
// 20: 00000010 udf #16
// 24: 00000018 udf #24
// 28: 52800060 mov w0, #0x3 // #3
// 24: 00000008 udf #8
// 28: 52800040 mov w0, #0x2 // #2
// 2c: d65f03c0 ret
// 30: 52800020 mov w0, #0x1 // #1
// 34: d65f03c0 ret
// 38: 52800040 mov w0, #0x2 // #2
// 38: 52800060 mov w0, #0x3 // #3
// 3c: d65f03c0 ret
let golden = vec![
31, 8, 0, 113, 34, 1, 0, 84, 232, 35, 128, 154, 159, 34, 3, 213, 135, 0, 0, 16, 232,
88, 168, 184, 231, 0, 8, 139, 224, 0, 31, 214, 16, 0, 0, 0, 24, 0, 0, 0, 96, 0, 128,
82, 192, 3, 95, 214, 32, 0, 128, 82, 192, 3, 95, 214, 64, 0, 128, 82, 192, 3, 95, 214,
31, 8, 0, 113, 162, 1, 0, 84, 232, 35, 128, 154, 159, 34, 3, 213, 135, 0, 0, 16, 232,
88, 168, 184, 231, 0, 8, 139, 224, 0, 31, 214, 16, 0, 0, 0, 8, 0, 0, 0, 64, 0, 128, 82,
192, 3, 95, 214, 32, 0, 128, 82, 192, 3, 95, 214, 96, 0, 128, 82, 192, 3, 95, 214,
];
assert_eq!(code, &golden[..]);

View File

@@ -43,6 +43,7 @@
//! The configured target ISA trait object is a `Box<TargetIsa>` which can be used for multiple
//! concurrent function compilations.
use crate::dominator_tree::DominatorTree;
pub use crate::isa::call_conv::CallConv;
use crate::flowgraph;
@@ -252,6 +253,7 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
fn compile_function(
&self,
func: &Function,
domtree: &DominatorTree,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil>;

View File

@@ -1,5 +1,6 @@
//! risc-v 64-bit Instruction Set Architecture.
use crate::dominator_tree::DominatorTree;
use crate::ir;
use crate::ir::condcodes::IntCC;
use crate::ir::Function;
@@ -56,11 +57,12 @@ impl Riscv64Backend {
fn compile_vcode(
&self,
func: &Function,
domtree: &DominatorTree,
) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
let emit_info = EmitInfo::new(self.flags.clone(), self.isa_flags.clone());
let sigs = SigSet::new::<abi::Riscv64MachineDeps>(func, &self.flags)?;
let abi = abi::Riscv64Callee::new(func, self, &self.isa_flags, &sigs)?;
compile::compile::<Riscv64Backend>(func, self, abi, emit_info, sigs)
compile::compile::<Riscv64Backend>(func, domtree, self, abi, emit_info, sigs)
}
}
@@ -68,9 +70,10 @@ impl TargetIsa for Riscv64Backend {
fn compile_function(
&self,
func: &Function,
domtree: &DominatorTree,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let (vcode, regalloc_result) = self.compile_vcode(func)?;
let (vcode, regalloc_result) = self.compile_vcode(func, domtree)?;
let want_disasm = want_disasm || log::log_enabled!(log::Level::Debug);
let emit_result = vcode.emit(
@@ -216,6 +219,8 @@ pub fn isa_builder(triple: Triple) -> IsaBuilder {
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::ir::types::*;
use crate::ir::{AbiParam, Function, InstBuilder, Signature, UserFuncName};
use crate::isa::CallConv;
@@ -250,7 +255,9 @@ mod test {
shared_flags,
isa_flags,
);
let buffer = backend.compile_function(&mut func, true).unwrap();
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let buffer = backend.compile_function(&mut func, &domtree, true).unwrap();
let code = buffer.buffer.data();
// To update this comment, write the golden bytes to a file, and run the following command

View File

@@ -1,5 +1,6 @@
//! IBM Z 64-bit Instruction Set Architecture.
use crate::dominator_tree::DominatorTree;
use crate::ir::condcodes::IntCC;
use crate::ir::{Function, Type};
use crate::isa::s390x::settings as s390x_settings;
@@ -56,11 +57,12 @@ impl S390xBackend {
fn compile_vcode(
&self,
func: &Function,
domtree: &DominatorTree,
) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
let emit_info = EmitInfo::new(self.isa_flags.clone());
let sigs = SigSet::new::<abi::S390xMachineDeps>(func, &self.flags)?;
let abi = abi::S390xCallee::new(func, self, &self.isa_flags, &sigs)?;
compile::compile::<S390xBackend>(func, self, abi, emit_info, sigs)
compile::compile::<S390xBackend>(func, domtree, self, abi, emit_info, sigs)
}
}
@@ -68,10 +70,11 @@ impl TargetIsa for S390xBackend {
fn compile_function(
&self,
func: &Function,
domtree: &DominatorTree,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let flags = self.flags();
let (vcode, regalloc_result) = self.compile_vcode(func)?;
let (vcode, regalloc_result) = self.compile_vcode(func, domtree)?;
let emit_result = vcode.emit(&regalloc_result, want_disasm, flags.machine_code_cfg_info());
let frame_size = emit_result.frame_size;
@@ -213,6 +216,8 @@ pub fn isa_builder(triple: Triple) -> IsaBuilder {
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::ir::types::*;
use crate::ir::UserFuncName;
use crate::ir::{AbiParam, Function, InstBuilder, Signature};
@@ -248,8 +253,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -297,8 +304,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -310,19 +319,20 @@ mod test {
//
// 0: a7 2a 12 34 ahi %r2,4660
// 4: a7 2e 00 00 chi %r2,0
// 8: c0 64 00 00 00 0b jglh 0x1e
// e: ec 32 12 34 00 d8 ahik %r3,%r2,4660
// 14: a7 3e 00 00 chi %r3,0
// 18: c0 64 ff ff ff fb jglh 0xe
// 1e: a7 2e 00 00 chi %r2,0
// 22: c0 64 ff ff ff f6 jglh 0xe
// 28: a7 2a ed cc ahi %r2,-4660
// 2c: 07 fe br %r14
// 8: c0 94 00 00 00 0b jgnlh 0x1e
// e: a7 2e 00 00 chi %r2,0
// 12: c0 64 00 00 00 06 jglh 0x1e
// 18: a7 2a ed cc ahi %r2,-4660
// 1c: 07 fe br %r14
// 1e: ec 32 12 34 00 d8 ahik %r3,%r2,4660
// 24: a7 3e 00 00 chi %r3,0
// 28: c0 64 ff ff ff fb jglh 0x1e
// 2e: c0 f4 ff ff ff f0 jg 0xe
let golden = vec![
167, 42, 18, 52, 167, 46, 0, 0, 192, 100, 0, 0, 0, 11, 236, 50, 18, 52, 0, 216, 167,
62, 0, 0, 192, 100, 255, 255, 255, 251, 167, 46, 0, 0, 192, 100, 255, 255, 255, 246,
167, 42, 237, 204, 7, 254,
167, 42, 18, 52, 167, 46, 0, 0, 192, 148, 0, 0, 0, 11, 167, 46, 0, 0, 192, 100, 0, 0,
0, 6, 167, 42, 237, 204, 7, 254, 236, 50, 18, 52, 0, 216, 167, 62, 0, 0, 192, 100, 255,
255, 255, 251, 192, 244, 255, 255, 255, 240,
];
assert_eq!(code, &golden[..]);

View File

@@ -3,6 +3,7 @@
pub use self::inst::{args, EmitInfo, EmitState, Inst};
use super::{OwnedTargetIsa, TargetIsa};
use crate::dominator_tree::DominatorTree;
use crate::ir::{condcodes::IntCC, Function, Type};
#[cfg(feature = "unwind")]
use crate::isa::unwind::systemv;
@@ -48,13 +49,14 @@ impl X64Backend {
fn compile_vcode(
&self,
func: &Function,
domtree: &DominatorTree,
) -> CodegenResult<(VCode<inst::Inst>, regalloc2::Output)> {
// This performs lowering to VCode, register-allocates the code, computes
// block layout and finalizes branches. The result is ready for binary emission.
let emit_info = EmitInfo::new(self.flags.clone(), self.x64_flags.clone());
let sigs = SigSet::new::<abi::X64ABIMachineSpec>(func, &self.flags)?;
let abi = abi::X64Callee::new(&func, self, &self.x64_flags, &sigs)?;
compile::compile::<Self>(&func, self, abi, emit_info, sigs)
let abi = abi::X64Callee::new(func, self, &self.x64_flags, &sigs)?;
compile::compile::<Self>(func, domtree, self, abi, emit_info, sigs)
}
}
@@ -62,9 +64,10 @@ impl TargetIsa for X64Backend {
fn compile_function(
&self,
func: &Function,
domtree: &DominatorTree,
want_disasm: bool,
) -> CodegenResult<CompiledCodeStencil> {
let (vcode, regalloc_result) = self.compile_vcode(func)?;
let (vcode, regalloc_result) = self.compile_vcode(func, domtree)?;
let emit_result = vcode.emit(
&regalloc_result,
@@ -231,6 +234,8 @@ fn isa_constructor(
mod test {
use super::*;
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::ir::{types::*, RelSourceLoc, SourceLoc, UserFuncName, ValueLabel, ValueLabelStart};
use crate::ir::{AbiParam, Function, InstBuilder, JumpTableData, Signature};
use crate::isa::CallConv;
@@ -341,8 +346,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -355,27 +362,28 @@ mod test {
// 4: 48 89 fe mov rsi,rdi
// 7: 81 c6 34 12 00 00 add esi,0x1234
// d: 85 f6 test esi,esi
// f: 0f 84 1c 00 00 00 je 0x31
// f: 0f 84 21 00 00 00 je 0x36
// 15: 49 89 f0 mov r8,rsi
// 18: 48 89 f0 mov rax,rsi
// 1b: 81 e8 34 12 00 00 sub eax,0x1234
// 21: 44 01 c0 add eax,r8d
// 24: 85 f6 test esi,esi
// 26: 0f 85 05 00 00 00 jne 0x31
// 26: 0f 85 0a 00 00 00 jne 0x36
// 2c: 48 89 ec mov rsp,rbp
// 2f: 5d pop rbp
// 30: c3 ret
// 31: 49 89 f0 mov r8,rsi
// 34: 41 81 c0 34 12 00 00 add r8d,0x1234
// 3b: 45 85 c0 test r8d,r8d
// 3e: 0f 85 ed ff ff ff jne 0x31
// 44: e9 cf ff ff ff jmp 0x18
// 31: e9 e2 ff ff ff jmp 0x18
// 36: 49 89 f0 mov r8,rsi
// 39: 41 81 c0 34 12 00 00 add r8d,0x1234
// 40: 45 85 c0 test r8d,r8d
// 43: 0f 84 cf ff ff ff je 0x18
// 49: e9 e8 ff ff ff jmp 0x36
let golden = vec![
85, 72, 137, 229, 72, 137, 254, 129, 198, 52, 18, 0, 0, 133, 246, 15, 132, 28, 0, 0, 0,
73, 137, 240, 72, 137, 240, 129, 232, 52, 18, 0, 0, 68, 1, 192, 133, 246, 15, 133, 5,
0, 0, 0, 72, 137, 236, 93, 195, 73, 137, 240, 65, 129, 192, 52, 18, 0, 0, 69, 133, 192,
15, 133, 237, 255, 255, 255, 233, 207, 255, 255, 255,
85, 72, 137, 229, 72, 137, 254, 129, 198, 52, 18, 0, 0, 133, 246, 15, 132, 33, 0, 0, 0,
73, 137, 240, 72, 137, 240, 129, 232, 52, 18, 0, 0, 68, 1, 192, 133, 246, 15, 133, 10,
0, 0, 0, 72, 137, 236, 93, 195, 233, 226, 255, 255, 255, 73, 137, 240, 65, 129, 192,
52, 18, 0, 0, 69, 133, 192, 15, 132, 207, 255, 255, 255, 233, 232, 255, 255, 255,
];
assert_eq!(code, &golden[..]);
@@ -450,8 +458,10 @@ mod test {
shared_flags,
isa_flags,
);
let cfg = ControlFlowGraph::with_function(&func);
let domtree = DominatorTree::with_function(&func, &cfg);
let result = backend
.compile_function(&mut func, /* want_disasm = */ false)
.compile_function(&mut func, &domtree, /* want_disasm = */ false)
.unwrap();
let code = result.buffer.data();
@@ -462,7 +472,7 @@ mod test {
// 0: 55 push rbp
// 1: 48 89 e5 mov rbp,rsp
// 4: 83 ff 02 cmp edi,0x2
// 7: 0f 83 27 00 00 00 jae 0x34
// 7: 0f 83 3b 00 00 00 jae 0x48
// d: 44 8b d7 mov r10d,edi
// 10: 41 b9 00 00 00 00 mov r9d,0x0
// 16: 4d 0f 43 d1 cmovae r10,r9
@@ -472,9 +482,9 @@ mod test {
// 29: 41 ff e1 jmp r9
// 2c: 12 00 adc al,BYTE PTR [rax]
// 2e: 00 00 add BYTE PTR [rax],al
// 30: 1c 00 sbb al,0x0
// 30: 08 00 or BYTE PTR [rax],al
// 32: 00 00 add BYTE PTR [rax],al
// 34: b8 03 00 00 00 mov eax,0x3
// 34: b8 02 00 00 00 mov eax,0x2
// 39: 48 89 ec mov rsp,rbp
// 3c: 5d pop rbp
// 3d: c3 ret
@@ -482,16 +492,16 @@ mod test {
// 43: 48 89 ec mov rsp,rbp
// 46: 5d pop rbp
// 47: c3 ret
// 48: b8 02 00 00 00 mov eax,0x2
// 48: b8 03 00 00 00 mov eax,0x3
// 4d: 48 89 ec mov rsp,rbp
// 50: 5d pop rbp
// 51: c3 ret
let golden = vec![
85, 72, 137, 229, 131, 255, 2, 15, 131, 39, 0, 0, 0, 68, 139, 215, 65, 185, 0, 0, 0, 0,
85, 72, 137, 229, 131, 255, 2, 15, 131, 59, 0, 0, 0, 68, 139, 215, 65, 185, 0, 0, 0, 0,
77, 15, 67, 209, 76, 141, 13, 11, 0, 0, 0, 79, 99, 84, 145, 0, 77, 1, 209, 65, 255,
225, 18, 0, 0, 0, 28, 0, 0, 0, 184, 3, 0, 0, 0, 72, 137, 236, 93, 195, 184, 1, 0, 0, 0,
72, 137, 236, 93, 195, 184, 2, 0, 0, 0, 72, 137, 236, 93, 195,
225, 18, 0, 0, 0, 8, 0, 0, 0, 184, 2, 0, 0, 0, 72, 137, 236, 93, 195, 184, 1, 0, 0, 0,
72, 137, 236, 93, 195, 184, 3, 0, 0, 0, 72, 137, 236, 93, 195,
];
assert_eq!(code, &golden[..]);