Commit d294def5 by Nicolas Capens

Support unsigned array indices.

Change-Id: I4136781005cbd0551461adecdd94d8e3cc8688d0 Reviewed-on: https://swiftshader-review.googlesource.com/8570Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent 584088c0
...@@ -451,8 +451,13 @@ namespace sw ...@@ -451,8 +451,13 @@ namespace sw
return value; return value;
} }
Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index) Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
{ {
if(unsignedIndex && sizeof(void*) == 8)
{
index = createZExt(index, Long::getType());
}
assert(ptr->getType()->getContainedType(0) == type); assert(ptr->getType()->getContainedType(0) == type);
return V(::builder->CreateGEP(ptr, index)); return V(::builder->CreateGEP(ptr, index));
} }
...@@ -6282,17 +6287,17 @@ namespace sw ...@@ -6282,17 +6287,17 @@ namespace sw
RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset) RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, int offset)
{ {
return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), V(Nucleus::createConstantInt(offset)))); return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), V(Nucleus::createConstantInt(offset)), false));
} }
RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset) RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
{ {
return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value)); return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
} }
RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset) RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
{ {
return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value)); return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
} }
RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset) RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
......
...@@ -96,7 +96,7 @@ namespace sw ...@@ -96,7 +96,7 @@ namespace sw
// Memory instructions // Memory instructions
static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); static Value *createLoad(Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0);
static Value *createStore(Value *value, Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0); static Value *createStore(Value *value, Value *ptr, Type *type, bool isVolatile = false, unsigned int align = 0);
static Value *createGEP(Value *ptr, Type *type, Value *index); static Value *createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex);
// Atomic instructions // Atomic instructions
static Value *createAtomicAdd(Value *ptr, Value *value); static Value *createAtomicAdd(Value *ptr, Value *value);
......
...@@ -90,7 +90,7 @@ namespace sw ...@@ -90,7 +90,7 @@ namespace sw
Value *loadValue(unsigned int alignment = 0) const; Value *loadValue(unsigned int alignment = 0) const;
Value *storeValue(Value *value, unsigned int alignment = 0) const; Value *storeValue(Value *value, unsigned int alignment = 0) const;
Value *getAddress(Value *index) const; Value *getAddress(Value *index, bool unsignedIndex) const;
}; };
template<class T> template<class T>
...@@ -2120,7 +2120,9 @@ namespace sw ...@@ -2120,7 +2120,9 @@ namespace sw
Reference<T> operator*(); Reference<T> operator*();
Reference<T> operator[](int index); Reference<T> operator[](int index);
Reference<T> operator[](unsigned int index);
Reference<T> operator[](RValue<Int> index); Reference<T> operator[](RValue<Int> index);
Reference<T> operator[](RValue<UInt> index);
static Type *getType(); static Type *getType();
...@@ -2149,7 +2151,9 @@ namespace sw ...@@ -2149,7 +2151,9 @@ namespace sw
Array(int size = S); Array(int size = S);
Reference<T> operator[](int index); Reference<T> operator[](int index);
Reference<T> operator[](unsigned int index);
Reference<T> operator[](RValue<Int> index); Reference<T> operator[](RValue<Int> index);
Reference<T> operator[](RValue<UInt> index);
}; };
// RValue<Array<T>> operator++(Array<T> &val, int); // Post-increment // RValue<Array<T>> operator++(Array<T> &val, int); // Post-increment
...@@ -2245,9 +2249,9 @@ namespace sw ...@@ -2245,9 +2249,9 @@ namespace sw
} }
template<class T> template<class T>
Value *LValue<T>::getAddress(Value *index) const Value *LValue<T>::getAddress(Value *index, bool unsignedIndex) const
{ {
return Nucleus::createGEP(address, T::getType(), index); return Nucleus::createGEP(address, T::getType(), index, unsignedIndex);
} }
template<class T> template<class T>
...@@ -2536,7 +2540,15 @@ namespace sw ...@@ -2536,7 +2540,15 @@ namespace sw
template<class T> template<class T>
Reference<T> Pointer<T>::operator[](int index) Reference<T> Pointer<T>::operator[](int index)
{ {
Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), Nucleus::createConstantInt(index)); Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), Nucleus::createConstantInt(index), false);
return Reference<T>(element, alignment);
}
template<class T>
Reference<T> Pointer<T>::operator[](unsigned int index)
{
Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), Nucleus::createConstantInt(index), true);
return Reference<T>(element, alignment); return Reference<T>(element, alignment);
} }
...@@ -2544,7 +2556,15 @@ namespace sw ...@@ -2544,7 +2556,15 @@ namespace sw
template<class T> template<class T>
Reference<T> Pointer<T>::operator[](RValue<Int> index) Reference<T> Pointer<T>::operator[](RValue<Int> index)
{ {
Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), index.value); Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), index.value, false);
return Reference<T>(element, alignment);
}
template<class T>
Reference<T> Pointer<T>::operator[](RValue<UInt> index)
{
Value *element = Nucleus::createGEP(LValue<Pointer<T>>::loadValue(), T::getType(), index.value, true);
return Reference<T>(element, alignment); return Reference<T>(element, alignment);
} }
...@@ -2563,7 +2583,15 @@ namespace sw ...@@ -2563,7 +2583,15 @@ namespace sw
template<class T, int S> template<class T, int S>
Reference<T> Array<T, S>::operator[](int index) Reference<T> Array<T, S>::operator[](int index)
{ {
Value *element = LValue<T>::getAddress(Nucleus::createConstantInt(index)); Value *element = LValue<T>::getAddress(Nucleus::createConstantInt(index), false);
return Reference<T>(element);
}
template<class T, int S>
Reference<T> Array<T, S>::operator[](unsigned int index)
{
Value *element = LValue<T>::getAddress(Nucleus::createConstantInt(index), true);
return Reference<T>(element); return Reference<T>(element);
} }
...@@ -2571,7 +2599,15 @@ namespace sw ...@@ -2571,7 +2599,15 @@ namespace sw
template<class T, int S> template<class T, int S>
Reference<T> Array<T, S>::operator[](RValue<Int> index) Reference<T> Array<T, S>::operator[](RValue<Int> index)
{ {
Value *element = LValue<T>::getAddress(index.value); Value *element = LValue<T>::getAddress(index.value, false);
return Reference<T>(element);
}
template<class T, int S>
Reference<T> Array<T, S>::operator[](RValue<UInt> index)
{
Value *element = LValue<T>::getAddress(index.value, true);
return Reference<T>(element); return Reference<T>(element);
} }
......
...@@ -804,7 +804,7 @@ namespace sw ...@@ -804,7 +804,7 @@ namespace sw
return value; return value;
} }
Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index) Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex)
{ {
assert(index->getType() == Ice::IceType_i32); assert(index->getType() == Ice::IceType_i32);
...@@ -827,7 +827,14 @@ namespace sw ...@@ -827,7 +827,14 @@ namespace sw
if(sizeof(void*) == 8) if(sizeof(void*) == 8)
{ {
index = createSExt(index, T(Ice::IceType_i64)); if(unsignedIndex)
{
index = createZExt(index, T(Ice::IceType_i64));
}
else
{
index = createSExt(index, T(Ice::IceType_i64));
}
} }
return createAdd(ptr, index); return createAdd(ptr, index);
...@@ -6272,12 +6279,12 @@ namespace sw ...@@ -6272,12 +6279,12 @@ namespace sw
RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset) RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<Int> offset)
{ {
return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value)); return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, false));
} }
RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset) RValue<Pointer<Byte>> operator+(RValue<Pointer<Byte>> lhs, RValue<UInt> offset)
{ {
return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value)); return RValue<Pointer<Byte>>(Nucleus::createGEP(lhs.value, Byte::getType(), offset.value, true));
} }
RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset) RValue<Pointer<Byte>> operator+=(Pointer<Byte> &lhs, int offset)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment