[LLVMdev] Indexing backwards through a structure

David Given dg at cowlark.com
Tue Aug 24 05:30:24 PDT 2010


On 2010-08-24 12:35, Jay Foad wrote:
[...]
> As a general suggestion, try writing the code in C and seeing what
> llvm-gcc or clang turns it into.

I hadn't thought of that; a bit of experimentation with the demo website 
shows that clang is doing raw pointer arithmetic, but with getelementptr 
to avoid too many casts:

%struct.Object = type { i32, double, i32 }

define %struct.Object* @getobj(i32* %ptr) nounwind readnone {
entry:
   %0 = getelementptr inbounds i32* %ptr, i64 -4 ; <i32*>
   %1 = bitcast i32* %0 to %struct.Object*       ; <%struct.Object*>
   ret %struct.Object* %1
}

The offset to getelementptr does, therefore, have structure layout 
information baked into it.

Using getelementptr for raw pointer arithmetic is a neat trick and 
avoids the evil of having to cast pointers to ints, so I think I'll just 
do that.

>> life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }

   "There are three things a man must do,
    Before his life is done.
    Write two lines of APL
    And make the buggers run."
       -- Anonymous

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ life←{ ↑1 ⍵∨.^3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵ }
│ --- Conway's Game Of Life, in one line of APL




More information about the llvm-dev mailing list