[LLVMdev] GEP::getIndexValid() with other iterators

Matthijs Kooijman matthijs at stdin.nl
Wed Jul 16 09:17:36 PDT 2008


Hi all,

currently, GetElementPtrInst has a method getIndexedType, which has a
templated variant. You pass in a begin and an end iterator, and it will find
the indexed type for those. However, both iterators must iterate over Value*.

For some argpromotion code, I would like to pass in iterators that iterate
over unsigneds instead of Value*. I currently solve this by transforming my
vector<unsigned> into a vector<Value*>, but this is hardly an efficient
solution.

Currently, however, there is already a templated version of getIndexedType,
probably so it can iterate over any pointer. In there, the begin pointer is
cast as follows:
 (const Value*)&*IdxBegin

Though this works, this has the nasty side effect of accepting a lot of
iterator types that are not really supported. In particular, when I passed in
an iterator over unsigned, the compiler would happily cast the resulting
unsigned* to a const Value*, resulting in all kinds of badness at runtime (and
not even a warning at compile time).

However, simply removing this cast seems to work for me, so I suppose there is
some compiler out there that will warn about this? Or is the cast really not
needed (anymore)?

Anyway, when one removes this cast and also makes the other getIndexedType
implementation (which is called by this one) a template, passing in an
iterator over unsigned should also work. The actual types over which an
iterator is allowed should now be limited by the parameters to
CompositeType::indexValid() and CompositeType::getTypeAtIndex() (which
currently are Value* or unsigned).

I've attached a patch which does exactly this. Since my template-fu is not so
strong, please review :-) I've also attached another patch to argpromotion,
which passes an unsigned iterator to getIndexedType() 
(look for the comment
	// This uses the new templated getIndexdType
in the code)

Gr.

Matthijs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: argpromotion.diff
Type: text/x-diff
Size: 24647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080716/b315b259/attachment.diff>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: getindexedtype.diff
Type: text/x-diff
Size: 3738 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080716/b315b259/attachment-0001.diff>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20080716/b315b259/attachment.sig>


More information about the llvm-dev mailing list