Skip to content

Instantly share code, notes, and snippets.

@rednaxelafx
Created June 19, 2012 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rednaxelafx/3818aa3acbc78e28e7fa to your computer and use it in GitHub Desktop.
Save rednaxelafx/3818aa3acbc78e28e7fa to your computer and use it in GitHub Desktop.
A bad try implementing overflow detection in C2 (2)
diff --git a/src/cpu/x86/vm/x86_64.ad b/src/cpu/x86/vm/x86_64.ad
--- a/src/cpu/x86/vm/x86_64.ad
+++ b/src/cpu/x86/vm/x86_64.ad
@@ -10627,6 +10627,45 @@
ins_pipe(pipe_jcc);
%}
+instruct addI_ovf(rRegI dst, rRegI src, rFlagsReg cr)
+%{
+ effect(DEF cr, USE dst, USE src);
+
+ format %{ "addl $dst, $src\t# int with overflow check" %}
+ opcode(0x03);
+ ins_encode(REX_reg_reg(dst, src), OpcP, reg_reg(dst, src));
+ ins_pipe(ialu_reg_reg);
+%}
+
+instruct jmpOvf(rFlagsReg cr, label labl)
+%{
+ effect(USE labl, USE cr);
+
+ format %{ "jo $labl" %}
+ size(6);
+ ins_encode %{
+ Label* L = $labl$$label;
+ __ jcc(Assembler::overflow, *L, false); // Always long jump
+ %}
+ ins_pipe(pipe_jcc);
+%}
+
+// Jump Direct on Add Overflow
+instruct jmpAddOverflow(cmpOp cop, rRegI dst, rRegI src, rRegI sum, immI0 zero, label labl)
+%{
+ predicate( n->in(1)->as_Bool()->_test._test == BoolTest::lt &&
+ n->in(2)->in(1)->in(1)->in(2) == n->in(2)->in(1)->in(2)->in(2)
+ );
+ match(If cop (CmpI (AndI (XorI dst (AddI dst src)) (XorI src sum)) zero));
+
+ ins_cost(300);
+ expand %{
+ rFlagsReg cr;
+ addI_ovf(dst, src, cr);
+ jmpOvf(cr, labl);
+ %}
+%}
+
// Jump Direct Conditional - Label defines a relative address from Jcc+1
instruct jmpLoopEnd(cmpOp cop, rFlagsReg cr, label labl)
%{
@@ -10828,6 +10867,20 @@
ins_short_branch(1);
%}
+instruct jmpOvf_short(rFlagsReg cr, label labl)
+%{
+ effect(USE labl, USE cr);
+
+ format %{ "jo,s $labl" %}
+ size(2);
+ ins_encode %{
+ Label* L = $labl$$label;
+ __ jccb(Assembler::overflow, *L);
+ %}
+ ins_pipe(pipe_jcc);
+ ins_short_branch(1);
+%}
+
// Jump Direct Conditional - Label defines a relative address from Jcc+1
instruct jmpLoopEnd_short(cmpOp cop, rFlagsReg cr, label labl) %{
match(CountedLoopEnd cop cr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment