<div dir="ltr"><div><div><div>Hi,<br><br></div>AFAIK LLVM does not have direct support for tagged unions. However I'd like to pass tagged unions to/from functions in registers. I.e. I'd like to represent to following data type in registers.<br></div>With Haskell syntax it looks like:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><i>data AllCon = Con1Data i32 | Con2Data i32</i><br></blockquote><div><div><div><br></div><div>A possible LLVM in register representation could be the following including a <i>create</i> and <i>consume</i> function as an example.<br></div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><i>%struct.Con1Data = type { i32 }</i><br><i>%struct.Con2Data = type { i32 }</i><br><i>%struct.AllCon = type { i1, %struct.Con1Data, %struct.Con2Data }</i><br><br><i>define private fastcc %struct.AllCon @create(i1 %s) {</i><br><i>entry:</i><br><i>  %X = select i1 %s,</i><br><i>    %struct.AllCon {i1 true, %struct.Con1Data { i32 4 }, %struct.Con2Data undef},</i><br><i>    %struct.AllCon {i1 false, %struct.Con1Data undef, %struct.Con2Data { i32 5 }}</i><br><i>  ret %struct.AllCon %X</i><br><i>}</i><br><br><i>define private fastcc i32 @consume(i1 %s) {</i><br><i>grinMain.entry:</i><br><i>  %val = call fastcc %struct.AllCon @create(i1 %s)</i><br><i>  %tag = extractvalue %struct.AllCon %val, 0</i><br><br><i>  %con1_int = extractvalue %struct.AllCon %val, 1, 0</i><br><i>  %con2_int = extractvalue %struct.AllCon %val, 2, 0</i><br><i>  %res = select i1 %s, i32 %con1_int, i32 %con2_int</i><br><br><i>  ret i32 %res</i><br><i>}</i><br></blockquote></div><div><br></div><div>I'd like to achieve an efficient mapping to x64 machine code. In the ideal case the <i>create</i> function result would be stored only in 2 registers. One i1 for the tag and one i32 for the argument value either come from Con1Data or Con2Data. For storing the i32 values one register would be always enough hence the constructors are mutually exclusive.</div><div><br></div>As you can see in the generated code below by default LLVM allocates separate registers for Con1Data and Con2Data arguments (ecx and edx). But ideally it would use only a single register for both. Of course to achieve that the frontend have to prove somehow that these values are on mutually exclusive data flow.<br><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><i>  .text</i><br><i>  .file "node_test_reg3.ll.1"</i><br><i>  .p2align  4, 0x90         # -- Begin function create</i><br><i>  .type .Lcreate,@function</i><br><i>.Lcreate:                               # @create</i><br><i>  .cfi_startproc</i><br><i># BB#0:                                 # %entry</i><br><i>  movl  %edi, %eax</i><br><i>  andb  $1, %al</i><br><i>  movl  $5, %ecx</i><br><i>  cmovnel %eax, %ecx</i><br><i>  testb %al, %al</i><br><i>  movl  $4, %eax</i><br><i>  cmovnel %eax, %edx</i><br><i>  movl  %edi, %eax</i><br><i>  retq</i><br><i>.Lfunc_end0:</i><br><i>  .size .Lcreate, .Lfunc_end0-.Lcreate</i><br><i>  .cfi_endproc</i><br><i>                                        # -- End function</i><br><i>  .p2align  4, 0x90         # -- Begin function consume</i><br><i>  .type .Lconsume,@function</i><br><i>.Lconsume:                              # @consume</i><br><i>  .cfi_startproc</i><br><i># BB#0:                                 # %grinMain.entry</i><br><i>  pushq %rbx</i><br><i>.Lcfi0:</i><br><i>  .cfi_def_cfa_offset 16</i><br><i>.Lcfi1:</i><br><i>  .cfi_offset %rbx, -16</i><br><i>  movl  %edi, %ebx</i><br><i>  callq .Lcreate</i><br><i>  testb $1, %bl</i><br><i>  cmovnel %edx, %ecx</i><br><i>  movl  %ecx, %eax</i><br><i>  popq  %rbx</i><br><i>  retq</i><br><i>.Lfunc_end1:</i><br><i>  .size .Lconsume, .Lfunc_end1-.Lconsume</i><br><i>  .cfi_endproc</i><br><i>                                        # -- End function</i><br><br><i>  .section  ".note.GNU-stack","",@progbits</i><br></blockquote></div><div><br></div><div><div>Does anyone have an idea how can I achieve this in LLVM? I guess I 
have to extend the expressive power somehow (i.e. new metadata). What is
 the simplest change to support this?</div><div><br></div></div><div>I'd appreciate if you'd help to figure out the complexity of this extension.</div><div><br></div><div>Best regards,</div><div>Csaba Hruska<br></div></div></div></div>