8000 select lea32/lea64 for add(x, shl(y, 1..3)) by can-lehmann · Pull Request #144 · can-lehmann/metajit.cpp · GitHub
[go: up one dir, main page]

Skip to content

select lea32/lea64 for add(x, shl(y, 1..3)) - #144

Merged
can-lehmann merged 6 commits into
mainfrom
can-scaled-index-lea
Oct 26, 2026
Merged

can-lehmann merged 6 commits into
mainfrom
can-scaled-index-lea

Conversation

@can-lehmann
Copy link
Copy Markdown
Owner

No description provided.

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 72.93% 🟡 79.45% 🟡 72.11%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟢 90.42% 🟡 87.01% 🟡 84.79%
x86insts.inc.hpp 🟡 84.85% - -
TOTAL 🟡 75.84% 🟡 81.64% 🟡 76.82%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (100%)
  • x86insts.inc.hpp (100%)

Summary

  • Total: 30 lines
  • Missing: 0 lines
  • Coverage: 100%

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 72.93% 🟡 79.45% 🟡 72.08%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟡 89.57% 🟡 86.71% 🟡 84.25%
x86insts.inc.hpp 🟡 84.24% - -
TOTAL 🟡 75.70% 🟡 81.58% 🟡 76.61%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (34.5%): Missing lines 424-426,784-797,1134-1135
  • x86insts.inc.hpp (0.0%): Missing lines 81

Summary

  • Total: 30 lines
  • Missing: 20 lines
  • Coverage: 33%

x86gen.hpp

Lines 420-430

  420     X86Inst* mov64_imm64(Reg dst, X86Inst::Imm imm) {
  421       return &build(X86Inst::Kind::Mov64Imm64).set_rm(dst).set_imm(imm);
  422     }
  423     
! 424     X86Inst* lea32(Reg dst, X86Inst::Mem src) {
! 425       return &build(X86Inst::Kind::Lea32).set_reg(dst).set_rm(src);
! 426     }
  427 
  428     X86Inst* lea64(Reg dst, X86Inst::Mem src) {
  429       return &build(X86Inst::Kind::Lea64).set_reg(dst).set_rm(src);
  430     }

Lines 780-801

  780           !base->is_named() || base->type() != type || index->type() != type) {
  781         return false;
  782       }
  783       if (dynmatch(ShlInst, shift, index)) {
! 784         if (shift->arg(0)->type() == type) {
! 785           if (dynmatch(Const, amount, shift->arg(1))) {
! 786             if (amount->value() >= 1 && amount->value() <= 3) {
! 787               X86Inst::Mem mem(vreg(base), size_t(1) << amount->value(), vreg(shift->arg(0)), 0);
! 788               if (type == Type::Int32) {
! 789                 _builder.lea32(dst, mem);
! 790               } else {
! 791                 _builder.lea64(dst, mem);
! 792               }
! 793               return true;
! 794             }
! 795           }
! 796         }
! 797       }
  798       return false;
  799     }
  800 
  801     void build_add(Reg dst, Value* a, Value* b) {

Lines 1130-1139

  1130         build_add(vreg(inst), add_ptr->ptr(), add_ptr->offset());
  1131       } else if (dynmatch(AddInst, add, inst)) {
  1132         if (build_scaled_add(vreg(inst), add->type(), add->arg(0), add->arg(1)) ||
  1133             build_scaled_add(vreg(inst), add->type(), add->arg(1), add->arg(0))) {
! 1134           return;
! 1135         }
  1136         build_add(vreg(inst), add->arg(0), add->ar
8000
g(1));
  1137       } else if (dynmatch(SubInst, sub, inst)) {
  1138         _builder.mov64(vreg(inst), vreg(sub->arg(0)));

x86insts.inc.hpp

Lines 77-85

  77 
  78 binop_x86_inst(MovZX8To64, movzx8to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb6); modrm(); })
  79 binop_x86_inst(MovZX16To64, movzx16to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb7); modrm(); })
  80 
! 81 x86_inst(Lea32, lea32, { use(rm); def(reg); }, false, { rex_opt(); byte(0x8d); modrm(); })
  82 x86_inst(Lea64, lea64, { use(rm); def(reg); }, true, { rex_w(); byte(0x8d); modrm(); })
  83 
  84 binop_x86_inst(Add64, add64, binop_usedef, true, { rex_w(); byte(0x03); modrm(); })
  85 binop_x86_inst(Sub64, sub64, binop_usedef, true, { rex_w(); byte(0x2b); modrm(); })

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 72.93% 🟡 79.45% 🟡 72.06%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟡 89.56% 🟡 86.67% 🟡 84.29%
x86insts.inc.hpp 🟡 84.24% - -
TOTAL 🟡 75.70% 🟡 81.57% 🟡 76.61%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (32.1%): Missing lines 424-426,786-801
  • x86insts.inc.hpp (0.0%): Missing lines 81

Summary

  • Total: 29 lines
  • Missing: 20 lines
  • Coverage: 31%

x86gen.hpp

Lines 420-430

  420     X86Inst* mov64_imm64(Reg dst, X86Inst::Imm imm) {
  421       return &build(X86Inst::Kind::Mov64Imm64).set_rm(dst).set_imm(imm);
  422     }
  423     
! 424     X86Inst* lea32(Reg dst, X86Inst::Mem src) {
! 425       return &build(X86Inst::Kind::Lea32).set_reg(dst).set_rm(src);
! 426     }
  427 
  428     X86Inst* lea64(Reg dst, X86Inst::Mem src) {
  429       return &build(X86Inst::Kind::Lea64).set_reg(dst).set_rm(src);
  430     }

Lines 782-805

  782           if (!base->is_named() || base->type() != type || index->type() != type) {
  783             continue;
  784           }
  785           if (dynmatch(ShlInst, shift, index)) {
! 786             if (shift->arg(0)->type() != type) {
! 787               continue;
! 788             }
! 789             if (dynmatch(Const, amount, shift->arg(1))) {
! 790               if (amount->value() < 1 || amount->value() > 3) {
! 791                 continue;
! 792               }
! 793               X86Inst::Mem mem(vreg(base), size_t(1) << amount->value(), vreg(shift->arg(0)), 0);
! 794               if (type == Type::Int32) {
! 795                 _builder.lea32(dst, mem);
! 796               } else {
! 797                 _builder.lea64(dst, mem);
! 798               }
! 799               return;
! 800             }
! 801           }
  802         }
  803       }
  804       X86Inst::Mem mem;
  805       if (dynmatch(Const, constant_b, b)) {

x86insts.inc.hpp

Lines 77-85

  77 
  78 binop_x86_inst(MovZX8To64, movzx8to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb6); modrm(); })
  79 binop_x86_inst(MovZX16To64, movzx16to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb7); modrm(); })
  80 
! 81 x86_inst(Lea32, lea32, { use(rm); def(reg); }, false, { rex_opt(); byte(0x8d); modrm(); })
  82 x86_inst(Lea64, lea64, { use(rm); def(reg); }, true, { rex_w(); byte(0x8d); modrm(); })
  83 
  84 binop_x86_inst(Add64, add64, binop_usedef, true, { rex_w(); byte(0x03); modrm(); })
  85 binop_x86_inst(Sub64, sub64, binop_usedef, true, { rex_w(); byte(0x2b); modrm(); })

Uh oh!

There was an error while loading. Please reload this page.

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 73.02% 🟡 79.45% 🟡 72.19%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟡 89.79% 🟡 86.67% 🟡 84.19%
x86insts.inc.hpp 🟡 84.24% - -
TOTAL 🟡 75.78% 🟡 81.57% 🟡 76.64%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (43.5%): Missing lines 424-426,780-781,808-814,827
  • x86insts.inc.hpp (0.0%): Missing lines 81

Summary

  • Total: 24 lines
  • Missing: 14 lines
  • Coverage: 41%

x86gen.hpp

Lines 420-430

  420     X86Inst* mov64_imm64(Reg dst, X86Inst::Imm imm) {
  421       return &build(X86Inst::Kind::Mov64Imm64).set_rm(dst).set_imm(imm);
  422     }
  423     
! 424     X86Inst* lea32(Reg dst, X86Inst::Mem src) {
! 425       return &build(X86Inst::Kind::Lea32).set_reg(dst).set_rm(src);
! 426     }
  427 
  428     X86Inst* lea64(Reg dst, X86Inst::Mem src) {
  429       return &build(X86Inst::Kind::Lea64).set_reg(dst).set_rm(src);
  430     }

Lines 776-785

  776     }
  777 
  778     void build_add(Reg dst, Value* a, Value* b) {
  779       if (dynamic_cast<ShlInst*>(a) || dynamic_cast<MulInst*>(a)) {
! 780         std::swap(a, b);
! 781       }
  782       bool use_lea32 = false;
  783       X86Inst::Mem mem;
  784       if (dynmatch(Const, constant_b, b)) {
  785         if (is_sext_imm32(constant_b)) {

Lines 804-818

  804       } else if (dynmatch(ShlInst, shift, b)) {
  805         Type type = a->type();
  806         if ((type == Type::Int32 || type == Type::Int64) &&
  807             a->is_named() && shift->type() == type && shift->arg(0)->type() == type) {
! 808           if (dynmatch(Const, amount, shift->arg(1))) {
! 809             if (amount->value() >= 1 && amount->value() <= 3) {
! 810               mem = X86Inst::Mem(vreg(a), size_t(1) << amount->value(), vreg(shift->arg(0)), 0);
! 811               use_lea32 = type == Type::Int32;
! 812             }
! 813           }
! 814         }
  815       }
  816 
  817       if (mem.is_invalid()) {
  818         mem = X86Inst::Mem(

Lines 823-831

  823         );
  824       }
  825 
  826       if (use_lea32) {
! 827         _builder.lea32(dst, mem);
  828       } else {
  829         _builder.lea64(dst, mem);
  830       }
  831     }

x86insts.inc.hpp

Lines 77-85

  77 
  78 binop_x86_inst(MovZX8To64, movzx8to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb6); modrm(); })
  79 binop_x86_inst(MovZX16To64, movzx16to64, mov_usedef, false, { rex_w(); byte(0x0f); byte(0xb7); modrm(); })
  80 
! 81 x86_inst(Lea32, lea32, { use(rm); def(reg); }, false, { rex_opt(); byte(0x8d); modrm(); })
  82 x86_inst(Lea64, lea64, { use(rm); def(reg); }, true, { rex_w(); byte(0x8d); modrm(); })
  83 
  84 binop_x86_inst(Add64, add64, binop_usedef, true, { rex_w(); byte(0x03); modrm(); })
  85 binop_x86_inst(Sub64, sub64, binop_usedef, true, { rex_w(); byte(0x2b); modrm(); })

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 73.04% 🟡 79.45% 🟡 72.11%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟢 90.15% 🟡 86.97% 🟡 84.61%
x86insts.inc.hpp 🟡 84.85% - -
TOTAL 🟡 75.86% 🟡 81.63% 🟡 76.74%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (81.0%): Missing lines 780-781,807-808
  • x86insts.inc.hpp (100%)

Summary

  • Total: 22 lines
  • Missing: 4 lines
  • Coverage: 81%

x86gen.hpp

Lines 776-785

  776     }
  777 
  778     void build_add(Reg dst, Type type, Value* a, Value* b) {
  779       if (dynamic_cast<ShlInst*>(a) || dynamic_cast<MulInst*>(a)) {
! 780         std::swap(a, b);
! 781       }
  782       X86Inst::Mem mem;
  783       if (dynmatch(Const, constant_b, b)) {
  784         if (is_sext_imm32(constant_b)) {
  785           mem = X86Inst::Mem(

Lines 803-812

  803       } else if (dynmatch(ShlInst, shift, b)) {
  804         if (dynmatch(Const, amount, shift->arg(1))) {
  805           if (a->is_named() && type_size(type) >= 4 && shift->type() == type &&
  806               amount->value() >= 1 && amount->value() <= 3) {
! 807             mem = X86Inst::Mem(vreg(a), size_t(1) << amount->value(), vreg(shift->arg(0)), 0);
! 808           }
  809         }
  810       }
  811 
  812       if (mem.is_invalid()) {

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
genext.hpp 🟡 71.23% 🟡 86.89% 🟡 72.56%
jitir.hpp 🟡 72.93% 🟡 79.45% 🟡 72.08%
jitir_llvmapi.hpp 🔴 54.74% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.80% 🟢 90.00% 🟡 82.02%
lowerllvm.hpp 🔴 69.57% 🟡 87.50% 🔴 50.49%
x86gen.hpp 🟡 90.00% 🟡 86.97% 🟡 84.47%
x86insts.inc.hpp 🟡 84.85% - -
TOTAL 🟡 75.77% 🟡 81.63% 🟡 76.69%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • x86gen.hpp (61.9%): Missing lines 781-782,807-812
  • x86insts.inc.hpp (100%)

Summary

  • Total: 22 lines
  • Missing: 8 lines
  • Coverage: 63%

x86gen.hpp

Lines 777-786

  777 
  778     void build_add(Reg dst, Value* a, Value* b) {
  779       Type type = a->type();
  780       if (dynamic_cast<ShlInst*>(a) || dynamic_cast<MulInst*>(a)) {
! 781         std::swap(a, b);
! 782       }
  783       X86Inst::Mem mem;
  784       if (dynmatch(Const, constant_b, b)) {
  785         if (is_sext_imm32(constant_b)) {
  786           mem = X86Inst::Mem(

Lines 803-816

  803         }
  804       } else if (dynmatch(ShlInst, shift, b)) {
  805         if ((type == Type::Int32 || type == Type::Int64) &&
  806             a->is_named() && shift->type() == type && shift->arg(0)->type() == type) {
! 807           if (dynmatch(Const, amount, shift->arg(1))) {
! 808             if (amount->value() >= 1 && amount->value() <= 3) {
! 809               mem = X86Inst::Mem(vreg(a), size_t(1) << amount->value(), vreg(shift->arg(0)), 0);
! 810             }
! 811           }
! 812         }
  813       }
  814 
  815       if (mem.is_invalid()) {
  816         mem = X86Inst::Mem(

@can-lehmann can-lehmann changed the title add base-plus-scaled-index LEA selection select lea32/lea64 for add(x, shl(y, 1..3)) Oct 26, 2026
@can-lehmann
can-lehmann merged commit 9f2e8ce into main Oct 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0