Frage

I want to get the Value of an array at an variable index. The Index is computed by the program and not known at parse time. So it is stored in an Value and converted to an Int like this:

Value *IndexV = Index->Codegen();
Value *IntV = Builder.CreateFPToUI( IndexV, Type::getInt32Ty( getGlobalContext() ) );

If I know the index, I can use:

Value *VV = Builder.CreateExtractValue( Builder.CreateLoad( V ), 0 );

This gives me the first element of the array. And works correctly. But how can I use IntV as the index? CreateExtractValue only takes an ArrayRef and there is no way of casting the IntV to an ArrayRef, or am I wrong? How would one do such a thing?

Thanks!

War es hilfreich?

Lösung

First of all, whenever an ArrayRef is expected, you can always pass just a single item, as there's an implicit conversion between any T and ArrayRef<T>.

Specifically here, though, extractvalue requires constant indices, and cannot accept general values, which is why it wants unsigned values. If you want to access an element in an unknown index in an array, use a getelementptr instruction instead: invoke it on the address of the array with indices 0 and IntV, and you should get a pointer to the array at location IntV.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top