<div dir="ltr"><div><font face="courier new, monospace">Hello. </font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">I'm new to LLVM and I've got some problems with LLVM JIT.</font></div>
<div><font face="courier new, monospace">I have set ExecutionEngine as CodeGenOpt::Aggressive and PassManagerBuilder.OptLevel as 3. (mem2reg and GVN are included)</font></div><div><font face="courier new, monospace">However, the machine code generated by JIT is not as good as that generated by clang or llc.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">Here is an example:</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">--------------------------------------------------------------------</font></div>
<div><font face="courier new, monospace"> source fragment ==> clang or llc</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">struct {</font></div>
<div><font face="courier new, monospace"> uint64_t a[10];</font></div><div><font face="courier new, monospace">} *p;</font></div><div><font face="courier new, monospace"> mov 0x8(%rax),%rdx</font></div>
<div><font face="courier new, monospace">p->a[2] = p->a[1]; mov %rdx,0x10(%rax)</font></div><div><font face="courier new, monospace">p->a[3] = p->a[1]; ==> mov %rdx,0x18(%rax)</font></div>
<div><font face="courier new, monospace">p->a[4] = p->a[2]; mov %rdx,0x20(%rax)</font></div><div><font face="courier new, monospace">p->a[5] = p->a[4]; mov %rdx,0x28(%rax)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">--------------------------------------------------------------------</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace"> JIT (map p to GlobalVariable) ==> JIT (map p to constant GlobalVariable)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> 1* movabsq $0x18c6b88, %rax 1* movabsq $0x18c6b88, %rax</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> 2* movq (%rax), %rcx // p 2* movq (%rax), %rax</font></div><div><font face="courier new, monospace"> 3* movq 0x8(%rcx), %rdx // a[1] 3* movq 0x8(%rax), %rcx</font></div>
<div><font face="courier new, monospace"> 4* movq %rdx, 0x10(%rcx) // a[2] 4* movq %rcx, 0x10(%rax)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> 5 movq (%rax), %rcx 5</font></div>
<div><font face="courier new, monospace"> 6 movq 0x8(%rcx), %rdx 6 movq 0x8(%rax), %rcx</font></div><div><font face="courier new, monospace"> 7* movq %rdx, 0x18(%rcx) ==> 7* movq %rcx, 0x18(%rax)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"> 8 movq (%rax), %rcx 8</font></div><div><font face="courier new, monospace"> 9 movq 0x10(%rcx), %rdx 9 movq 0x10(%rax), %rcx</font></div>
<div><font face="courier new, monospace">10* movq %rdx, 0x20(%rcx) 10* movq %rcx, 0x20(%rax)</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">11 movq (%rax), %rax 11</font></div>
<div><font face="courier new, monospace">12 movq 0x20(%rax), %rcx 12</font></div><div><font face="courier new, monospace">13* movq %rcx, 0x28(%rax) 13* movq %rcx, 0x28(%rax)</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">----------------------------------------------------------------------</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">A GlobalValue was declared and mapped to the variable p.</font></div><div><font face="courier new, monospace">Some LLVM IR instructions were created according to those generated by LLVM from source.</font></div>
<div><font face="courier new, monospace">I.e., load p, load a[1] based on p, load p again, store a[2] based on p, etc.</font></div><div><font face="courier new, monospace">The machine code turned out to be slightly optmized, as shown on the left.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">Things were getting better after the GlobalVariable of p was set as a constant.</font></div><div><font face="courier new, monospace">Redundant Loads of p (line 5, 8 and 11) were removed, and so was line 12 because of line 10.</font></div>
<div><font face="courier new, monospace">However, I could not make it better any more, although optimal machine code just need those marked with '*'.</font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace">It seems that store instructions block the optimizations across them.</font></div><div><font face="courier new, monospace">I.e., line 3&6 or 4&9 are similar to line 10&12, but they are not optimized.</font></div>
<div><font face="courier new, monospace">The store (line 4 or 7) between them obviously has no alias problem.</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">My question is: how to make LLVM JIT optimize this code? </font></div>
<div><font face="courier new, monospace">Did I miss anything, or need I write some kind of optimization pass?</font></div><div><font face="courier new, monospace">I will be grateful for any help you can provide.</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div>
<div><br></div></div>