[LLVMdev] the getelementptr noop problem

lee white baugh leebaugh at students.uiuc.edu
Sat Sep 28 21:15:01 PDT 2002

so i confess i'm still not clear on what the first index into
getelementptr is all about.

it makes perfect sense for an example like
getelementptr %mystruct * %reg100
to just return a %mystruct * equivalent to %reg100.

it does *not* make sense to me that
getelementptr %mystruct * %reg100, uint 0
should act the same.  for look, what this speaks of to me is indexing the
first element in an array.  if we are careful to cast it back to %mystruct
*, then it should *work*, although it is useless.  but, i would not expect

int ** x = <some array>;
int ** y = x[0];

to work unless we cast x[0] as an int **, and not the int * it would
otherwise return.

moreover, if this were something that we could really count on acting as a
noop, then we could have

%reg101 = getelementptr %mystruct * %reg100, uint 0
%reg102 = getelementptr %mystruct * %reg101, uint 0
... and so on, and not make a difference.  which to me seems the same as

%regn   = getelementptr %mystruct * %reg100, uint 0, uint 0, ...

which *ought to be* indexing deep into some array.

so -- what exactly does the first indexing uint 0 do?  is it safe to leave
it off?  do all good getelementptrs have it, so that i should assert that
there is one?

thanks a lot for your help!



       +-+     o+---\ /
    +--+  The        *-
   ++   Hamster  +--/ \
   this living hand, now warm and capable
   of earnest grasping, would, if it were cold
   and in the icy silence of the tomb,
   so haunt thy days and chill thy dreaming nights
   that thou wouldst wish thine own heart dry of blood
   so in my veins red life might stream again,
   and thou be conscience-calmed -- see here it is --
   i hold it towards you.

   : john keats


More information about the llvm-dev mailing list