On Thu, Nov 12, 2009 at 5:58 PM, Chris Lattner <span dir="ltr"><<a href="mailto:clattner@apple.com">clattner@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div style="word-wrap:break-word"><div><div class="im"><div>On Nov 12, 2009, at 11:29 AM, Talin wrote:</div><blockquote type="cite"><font color="#000000"><br></font>Well, as far as intp goes (or iptr if you prefer - the naming convention in LLVM is i<size>)</blockquote>

<div><br></div></div><div>How about "intptr".</div><div class="im"><br><blockquote type="cite">here's what I would expect:<div><ul><li>General rule #1: If an instruction accepts both i32 and i64, then it should accept iptr as well. If it only accepts i32, then it can continue to only accept i32.</li>

 <li>General rule #2: It should support operations that are commonly used with size_t and ptrdiff_t.</li></ul></div></blockquote></div><div>Ok.  Just realize that obscure optimizations like "constant folding" won't be possible without TargetData around. :)</div>

</div></div></blockquote><div><br></div><div>There is also the issue of how constants should be represented.</div><div><br></div><div>For all current processors that I know of, an intptr will be either 32 or 64 bits. However, there may be some future processor which supports 128-bit pointers (although a system containing that much RAM or even virtual address space is hard to imagine.)</div>

<div><br></div><div>If we assume that i64 is the upper limit, then intptr constants can be converted to i64 until needed.</div><div><br></div><div>Some constant folding can occur if the folding wouldn't change the final result. Specifically, for any function f(x, y) where (i32)f(x, y) is the same as (i32)f((i32)x, (i32)y), it's safe to apply that function before the final size of the integer is chosen. Thus, adding two numbers (ignoring overflow), or shifting to the left should be safe to fold.</div>

<div><br></div><div>That being said, I am perfectly fine with simply disabling folding, and leaving the partial folding as a future optimization.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div style="word-wrap:break-word"><div><div class="im"><blockquote type="cite"><div><ul><li>Operations that should work with iptr:</li><ul><li>Basic math: add, subtract, multiply, divide, mod.</li><li>Bitwise binary operators: shl, ashr, lshr, and, or, xor, etc.</li>

 <li>Comparison operations.</li><li>alloca - currently doesn't work with i64, should it?</li></ul></ul></div></blockquote></div><div>Yes, alloca should work with i64.  Recently malloc was detangled from alloca, but alloca should definitely support an arbitrary integer size.  I don't know anyone planning to do this.  In any case, for the first implementation stage of intptr, just converting to an i32 to do the alloca should be fine (no worse than what we have today).  When alloca gets generalized, if intptr is around it will be handed as well.</div>

<div class="im"><blockquote type="cite"><div><ul><ul><li>GEP - rules are the same as for using i64 indices.</li><li>memcpy intrinsics</li><li>bit manipulation intrinsics</li><li> overflow arithmetic intrinsics - would be nice</li>

<li>atomic intrinsics - would be very nice (I assume that atomic iptr works on all platforms that support atomics: That is, on 32-bit platforms where iptr == i32 I expect atomic i32 to work; on 64-bit platforms where iptr == i64 I expect atomic i64 to work).</li>

</ul></ul></div></blockquote></div><div>This all sounds reasonable.</div><div class="im"><blockquote type="cite"><div><ul><ul> </ul><li>Operations that don't need to work with iptr - i.e. I don't mind having to convert to some other int type first:</li>

<ul><li>switch</li><li>extractelement / insertelement / shufflevector</li><li>extractvalue / insertvalue - not sure about these.</li> <li>code generator intrinsics (frameaddress, etc.)</li></ul></ul></div></blockquote></div>

<div>insert/extractvalue need to work, as does load/store/phi for it to be a useful first class value.  switch should "just work".  I don't have an opinion about whether intptr should work with vectors, but it seems sensible either way.  I agree about frameaddress.</div>

</div></div></blockquote><div><br></div><div>There is also the question of whether intptrs should be allowed as *members* of vectors. I have no opinion on this, except to say that it probably only makes sense in situations where you can also have vectors of pointers.</div>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word"><div><div class="im"><blockquote type="cite"><div><ul><li>Converting to pointer types: inttoptr and ptrtoint should be no-ops, effectively.</li>

<li>Converting to other integer types: The issue here is that with other integer conversions in LLVM, you are required to know whether or not you are converting to a larger or smaller size - whether to use an ext or a trunc instruction. When converting to pointers, however, the choice of trunc or ext is automatic. Ideally, conversion to iptr would work the same way as conversion to a pointer type. There's also the issue of signed vs. unsigned extension.</li>

 <ul><li>Note that some constant-folding operations would need to be deferred until the target size is established.</li></ul></ul></div></blockquote></div><div>Almost *all* constant folding would have to be deferred, which means you'd get big chains of constant exprs.  This isn't a problem per-say, but something to be aware of.</div>

<div><br></div><div>I don't like reusing existing sext/trunc/zext/inttoptr/ptrtoint casts for intptr.  I think we should introduce new operations (hopefully with better names):</div><div><br></div><div>ptr to intptr</div>

<div>intptr to int</div>intptr to signed int</div><div>signed int to intptr</div><div><div>intptr to unsigned int</div><div>unsigned int to intptr</div><div><br></div><div>Does that seem reasonable?</div></div></div></blockquote>

<div><br></div><div>Sure. Another option is to do away with sext/trunc/etc. and just have two cast operations for ints: sicast and uicast. sicast converts any int size to any other (with sign extension if the destination type is bigger), and uicast is the same but with zero extension. That leaves just ptrtoint and inttoptr - which incidentally have the same semantics as sicast and uicast.</div>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word"><div><div class="im"><blockquote type="cite"><div><ul><li>Converting to FP types: Either don't support (i.e. require casting to known-width integer first), or map to i32->FP or i64->FP after the size is known.</li>

 </ul></div></blockquote></div></div>I think we should force conversion to a fixed integer type before converting to/from FP (for example pointers can't currently be converted to FP, they have to go through an intermediate integer type).  If it is important, we can always add this as a second (or third) extension once the basics work.<div>

<br></div><div>I'm going to be away on vacation for two weeks so I won't be able to keep up to date with this thread, if you're interested in pursuing this work, please write up something in the form of an 'llvmnote' (e.g. <a href="http://nondot.org/sabre/LLVMNotes/IndirectGoto.txt)" target="_blank">http://nondot.org/sabre/LLVMNotes/IndirectGoto.txt)</a> which explains in prose the problem it is trying to solve, the tradeoffs, and a proposed implementation approach (like you have above).  Whether or not you get time to start implementing it, it is a good idea to document the design tradeoffs considered and the effects of various decisions (such as neutering constant folding when TD isn't around).  This is also a good way to get others to help out,</div>

<div><br></div><font color="#888888"><div>-Chris</div></font></div></blockquote></div><br><br clear="all"><br>-- <br>-- Talin<br>