<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p><span id="ms-rterangepaste-start"></span></p>
<div>Hi All,<br>
<br>
Thanks for the reply. I managed to identify and fixed a few errors in my implementation.<br>
<br>
However, there are a few errors that i am not sure what is it indicating.<br>
For starters, i think i should explain what i am trying to achieve.<br>
<br>
I am actually working on MIPS backend to generate smaller set of MIPS Instructions compared to its existing supported instructions.<br>
Currently, i am working on shifting instructions.<br>
<br>
Take an example:<br>
A typical mips sllv syntax goes in this manner:<br>
<br>
         sllv $reg1,$reg2,$reg3<br>
<br>
The $reg3 contains the shifting amount. Only the LSB 5 bit will be used.<br>
The $reg2 contains the data to be shifted.<br>
The $reg1 contains the data after shifting is performed.<br>
<br>
What i want to achieve is to expand sllv instruction to the following routine:<br>
<br>
                 andi $reg3,$reg3,0x1f         //To mask the 5 bit LSB shifting amount
<br>
#BB_1:    beq  $reg3,$zero,#BB_2    //Branch out from basic block if shifting amount is zero<br>
                 sub  $reg3,$reg3,1              //To subtract 1 from the shifting amount<br>
                 sll  $reg2,$reg2,1                //Shift by 1 bit<br>
                 j    #BB_1                              //Branch back to the begining of the routine<br>
#BB_2:    addu $reg1,$reg2,$zero    //Transfer the completed shift data to the original destination register<br>
<br>
Since you guys mentioned that the MI are represented in MachineSSA form, i imagined my routine represented by virtual registers would look something like this:<br>
<br>
                andi $vreg3,$vreg3,0x1f         <br>
#BB_1:   beq  $vreg3,$zero,#BB_2        <br>
                sub  $vreg3,$vreg3,1           <br>
                sll  $vreg2,$vreg2,1           <br>
                j    #BB_1               <br>
#BB_2:  addu $vreg1,$vreg2,$zero           <br>
<br>
With the -verify-machineinstrs invoked together with llc, the errors i encountered are as follow:<br>
1. *** Bad machine code: Explicit definition marked as use ***<br>
- function:    main<br>
- basic block: BB#1 entry (0x546e600)<br>
- instruction: SUB<br>
- operand 0:   %vreg3<br>
<br>
<br>
2.*** Bad machine code: Explicit definition marked as use ***<br>
- function:    main<br>
- basic block: BB#1 entry (0x546e600)<br>
- instruction: SLL<br>
- operand 0:   %vreg2 <br>
<br>
<br>
3.*** Bad machine code: Non-terminator instruction after the first terminator ***<br>
- function:    main<br>
- basic block: BB#1 entry (0x4911600)<br>
- instruction: SUB<br>
First terminator was:    BEQ %vreg3, %ZERO, <BB#2>, %AT<imp-def>; GPR32:%vreg3<br>
<br>
<br>
4.*** Bad machine code: Non-terminator instruction after the first terminator ***<br>
- function:    main<br>
- basic block: BB#1 entry (0x4632600)<br>
- instruction: SLL<br>
First terminator was:    BEQ %vreg3, %ZERO, <BB#2>, %AT<imp-def>; GPR32:%vreg3<br>
<br>
<br>
Here are a few questions i would like to ask:<br>
1.Assuming that $vreg1,$vreg2 and $vreg3 are defined before #BB_1, based on my suggested routine, have i broke the integrity of the MachineSSA form?<br>
<br>
2.What is error 1 and 2 trying to tell me? I believe it has something to do with the define and kill of the virtual register states.
<br>
<br>
3.I am actually not sure about data-flow analysis.I did looked through MachineOperand class. Inside there shows the register states such as IsDef,IsDead,IsKill and etc. What are the definition for those register states? I am not sure what to do with the states,
 so i didn't really pass the virtual register states when i invoked BuildMI() to insert my desired MI. Are there any recommended sources or documentions i can look into so that i could learn more about data-flow analysis or understand the definition for each
 of the register states?  <br>
<br>
4.For error 3 and 4, does it mean i should group the other instructions after branch into another basic block?<br>
<br>
5.For #BB_1, i actually notice another error. If i add more than one successor for it, the llvm actually complains unconditional branch does not have exactly one successor. I had to seperate the jump instruction into another basic block. Is it wrong to have
 to 2 branching instructions in one basic block? <br>
<br>
6.My implementation seems to work only for -O1,-O2,-O3 invocation of llc. The -O0 invocation results in a lot of unecessary spilling and reloading code and causes the behavior of my code to be undefined. Matthias did mentioned that it could be due to the register
 allocator of O0. What should i look into so that my approach could work for all register allocator?<br>
<br>
I might asking for too much, but to be honest, i am actually very new to compiler study. I had to admit i need to study more about compiler techniques.<br>
Can you guys recommend me books to study about compiler techniques? <br>
If you guys find troubled to answer my questions, can you guys at least recommend me some sources or documentations so that i can look into myself?
<br>
<br>
Thanks in advance. <br>
Chuan<br>
        <br>
    <br>
</div>
<span id="ms-rterangepaste-end"></span><br>
<p></p>
<br>
<br>
<div style="color: rgb(0, 0, 0);">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" color="#000000" face="Calibri, sans-serif"><b>From:</b> Song, Ruiling <ruiling.song@intel.com><br>
<b>Sent:</b> Tuesday, September 12, 2017 9:17 AM<br>
<b>To:</b> jin chuan see; Matthias Braun<br>
<b>Cc:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> RE: [llvm-dev] Live Register Spilling</font>
<div> </div>
</div>
<div>
<div style="">
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:#1F497D">Running llc with ‘-verify-machineinstrs’ may tell you which instruction break the SSA form.</span></p>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:#1F497D"> </span></p>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:#1F497D">Ruiling</span></p>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<a name="_MailEndCompose"><span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:#1F497D"> </span></a></p>
<div style="border:none; border-left:solid blue 1.5pt; padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none; border-top:solid #E1E1E1 1.0pt; padding:3.0pt 0in 0in 0in">
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif"> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>jin chuan see via llvm-dev<br>
<b>Sent:</b> Monday, September 11, 2017 10:02 AM<br>
<b>To:</b> Matthias Braun <mbraun@apple.com><br>
<b>Cc:</b> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [llvm-dev] Live Register Spilling</span></p>
</div>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
 </p>
<div id="divtagdefaultwrapper">
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Sorry about the previous message
</span></p>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">This message showed up:</span></p>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">llc: /home/jc/Desktop/Project/For_Testing/llvm/lib/CodeGen/MachineRegisterInfo.cpp:366: llvm::MachineInstr* llvm::MachineRegisterInfo::getVRegDef(unsigned int) const: Assertion `(I.atEnd() || std::next(I)
 == def_instr_end()) && "getVRegDef assumes a single definition or no definition"' failed.</span></p>
</div>
<p><span style="font-family:"Calibri",sans-serif; color:black">I am assuming that i messed up the virtual register allocation when i am using BuildMI().</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">Also, i cannot invoke the clang as -O1 and so on as it will optimize my code and the generated .ll file will contain nothing other than the prologue and epilogue code.<br>
Is there any information i can provide you so that we can discuss the issue further?
<br>
<br>
Chuan. </span></p>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<div>
<div style="text-align: center; margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;" align="center">
<span style="font-family:"Calibri",sans-serif; color:black">
<hr width="98%" size="3" align="center">
</span></div>
<div id="divRplyFwdMsg">
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:black">From:</span></b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:black"> jin chuan see <<a href="mailto:jinchuansee@hotmail.com" style="color: blue; text-decoration: underline;">jinchuansee@hotmail.com</a>><br>
<b>Sent:</b> Monday, September 11, 2017 9:57 AM<br>
<b>To:</b> Matthias Braun<br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" style="color: blue; text-decoration: underline;">
llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [llvm-dev] Live Register Spilling</span><span style="font-family:"Calibri",sans-serif; color:black">
</span></p>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
</div>
<div>
<div id="divtagdefaultwrapper">
<p><span style="font-family:"Calibri",sans-serif; color:black">Hi Matthias,</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">Sorry for the late reply. </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">Yes, you are correct, I do have optnone attribute on my function.
</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">I did pass -O0 to the tools. </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">For your information, my invocations are as below:</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">clang  --target=mips-unknown-linux  -mips32  test.c  -emit-llvm  -S</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">llc  -O0  -march=mips  -mcpu=mips32  test.ll  -o  test.s</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">Based on the generated .ll file, there is optnone attribute on the function, i am guessing this is due to the default optimization -O0 by clang if not specified.</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">As for the llc, i tried to invoke it with -O1,-O2,-O3. All of them resulted in failure during the phase "PROCESS IMPLICIT DEFS"</span></p>
<p><span style="font-family:"Calibri",sans-serif; color:black">This message showed up:</span></p>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<div>
<div style="text-align: center; margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;" align="center">
<span style="font-family:"Calibri",sans-serif; color:black">
<hr width="98%" size="3" align="center">
</span></div>
<div id="divRplyFwdMsg">
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:black">From:</span></b><span style="font-size:11.0pt; font-family:"Calibri",sans-serif; color:black">
<a href="mailto:mbraun@apple.com" style="color: blue; text-decoration: underline;">
mbraun@apple.com</a> <<a href="mailto:mbraun@apple.com" style="color: blue; text-decoration: underline;">mbraun@apple.com</a>> on behalf of Matthias Braun <<a href="mailto:mbraun@apple.com" style="color: blue; text-decoration: underline;">mbraun@apple.com</a>><br>
<b>Sent:</b> Saturday, September 9, 2017 2:06 AM<br>
<b>To:</b> jin chuan see<br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" style="color: blue; text-decoration: underline;">
llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> Re: [llvm-dev] Live Register Spilling</span><span style="font-family:"Calibri",sans-serif; color:black">
</span></p>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
</div>
<div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">This would be a lot easier to discuss having a concrete example, llc invocations etc.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">It sounds like you are using RegAllocFast, have you tried using the optimized register allocators (= make sure you don't have the optnone attribute on your function and are not passing -O0 to the tools).</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">- Matthias</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
<div>
<blockquote style="margin-top:5.0pt; margin-bottom:5.0pt">
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">On Sep 8, 2017, at 12:10 AM, jin chuan see via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" style="color: blue; text-decoration: underline;">llvm-dev@lists.llvm.org</a>> wrote:</span></p>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<div>
<div id="divtagdefaultwrapper">
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Hi All,<span style=""> </span></span></p>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">I faced some problems while using the BuildMI().</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Currently, i am trying to replace specific MI with a series of new MI.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">I wrote a routine under the processFunctionAfterISel() to detect the targeted MI and replace it accordingly.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">After using BuildMI() to perform my replacement, i realize there are unnecessary spilling and reloading of registers in the assembly generated.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">By checking the llc debug output, i am suspecting that the virtual register states have been completely messed up.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">This is because the spilling and reloading codes are only inserted at the register allocation phase, especially during the state of "Spilling live registers at the end of block".</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">These spilling and reloading codes are messing up the assembly generated, and the behavior of the code generated is undetermined.<span style=""> </span></span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">I would like to know is there anything i can do to fix the virtual register use-def relationship?</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Or is there any standard procedure i should follow to handle the MachineOperands while using BuildMI()?</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Any opinions or suggestion are welcomed.</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">Regards,</span></p>
</div>
<div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black">JC</span></p>
</div>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-size:9.0pt; font-family:"Helvetica",sans-serif; color:black">_______________________________________________<br>
LLVM Developers mailing list<br>
</span><span style="font-family:"Calibri",sans-serif; color:black"><a href="mailto:llvm-dev@lists.llvm.org" style="color: blue; text-decoration: underline;"><span style="font-size:9.0pt; font-family:"Helvetica",sans-serif">llvm-dev@lists.llvm.org</span></a></span><span style="font-size:9.0pt; font-family:"Helvetica",sans-serif; color:black"><br>
</span><span style="font-family:"Calibri",sans-serif; color:black"><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" style="color: blue; text-decoration: underline;"><span style="font-size:9.0pt; font-family:"Helvetica",sans-serif">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</span></a></span></p>
</div>
</blockquote>
</div>
<p style="margin: 0in 0in 0.0001pt; font-size: 12pt; font-family: "Times New Roman",serif;">
<span style="font-family:"Calibri",sans-serif; color:black"> </span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>