Skip to content

Commit

Permalink
Mitigate return address misprediction & reduce branches.
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed Mar 25, 2019
1 parent 1f037d7 commit 12a776f
Showing 1 changed file with 44 additions and 49 deletions.
93 changes: 44 additions & 49 deletions lib/dynasm-backend/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ impl X64FunctionCode {
}
}

fn emit_cmp_i32<F: FnOnce(&mut Assembler)>(
fn emit_cmp_i32<F: FnOnce(&mut Assembler, Register)>(
assembler: &mut Assembler,
left: Register,
right: Register,
Expand All @@ -1103,18 +1103,14 @@ impl X64FunctionCode {
assembler
; cmp Rd(left as u8), Rd(right as u8)
);
f(assembler);
f(assembler, left);
dynasm!(
assembler
; xor Rd(left as u8), Rd(left as u8)
; jmp >label_end
; label_true:
; mov Rd(left as u8), 1
; label_end:
; and Rd(left as u8), 1
);
}

fn emit_cmp_i64<F: FnOnce(&mut Assembler)>(
fn emit_cmp_i64<F: FnOnce(&mut Assembler, Register)>(
assembler: &mut Assembler,
left: Register,
right: Register,
Expand All @@ -1124,14 +1120,10 @@ impl X64FunctionCode {
assembler
; cmp Rq(left as u8), Rq(right as u8)
);
f(assembler);
f(assembler, left);
dynasm!(
assembler
; xor Rq(left as u8), Rq(left as u8)
; jmp >label_end
; label_true:
; mov Rq(left as u8), 1
; label_end:
; and Rd(left as u8), 1
);
}

Expand Down Expand Up @@ -1453,7 +1445,8 @@ impl X64FunctionCode {
assembler
; mov rsp, rbp
; pop rbp
; ret
; pop rbx
; jmp rbx
);

Ok(())
Expand Down Expand Up @@ -1749,7 +1742,8 @@ impl X64FunctionCode {
; call rax
; mov rsp, rbp
; pop rbp
; ret
; pop rbx
; jmp rbx
);

label
Expand Down Expand Up @@ -2755,10 +2749,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jl >label_true
; setl Rb(reg as u8)
);
});
},
Expand All @@ -2769,10 +2763,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jle >label_true
; setle Rb(reg as u8)
);
});
},
Expand All @@ -2783,10 +2777,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jg >label_true
; setg Rb(reg as u8)
);
});
},
Expand All @@ -2797,10 +2791,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jge >label_true
; setge Rb(reg as u8)
);
});
},
Expand All @@ -2811,10 +2805,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jb >label_true
; setb Rb(reg as u8)
);
});
},
Expand All @@ -2825,10 +2819,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jbe >label_true
; setbe Rb(reg as u8)
);
});
},
Expand All @@ -2839,10 +2833,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; ja >label_true
; seta Rb(reg as u8)
);
});
},
Expand All @@ -2853,10 +2847,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i32(assembler, left, right, |assembler| {
Self::emit_cmp_i32(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jae >label_true
; setae Rb(reg as u8)
);
});
},
Expand Down Expand Up @@ -3191,10 +3185,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jl >label_true
; setl Rb(reg as u8)
);
});
},
Expand All @@ -3207,10 +3201,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jle >label_true
; setle Rb(reg as u8)
);
});
},
Expand All @@ -3223,10 +3217,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jg >label_true
; setg Rb(reg as u8)
);
});
},
Expand All @@ -3239,10 +3233,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jge >label_true
; setge Rb(reg as u8)
);
});
},
Expand All @@ -3255,10 +3249,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jb >label_true
; setb Rb(reg as u8)
);
});
},
Expand All @@ -3271,10 +3265,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jbe >label_true
; setbe Rb(reg as u8)
);
});
},
Expand All @@ -3287,10 +3281,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; ja >label_true
; seta Rb(reg as u8)
);
});
},
Expand All @@ -3303,10 +3297,10 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler,
&mut self.value_stack,
|assembler, _value_stack, left, right| {
Self::emit_cmp_i64(assembler, left, right, |assembler| {
Self::emit_cmp_i64(assembler, left, right, |assembler, reg| {
dynasm!(
assembler
; jae >label_true
; setae Rb(reg as u8)
);
});
},
Expand Down Expand Up @@ -5250,7 +5244,8 @@ impl FunctionCodeGenerator for X64FunctionCode {
assembler
; mov rsp, rbp
; pop rbp
; ret
; pop rbx
; jmp rbx
);

if self.value_stack.values.len() != 0
Expand Down

0 comments on commit 12a776f

Please sign in to comment.