From 20f67ad40a96b90e17fe44e98a026ac6cc9dac7c Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Tue, 27 Sep 2016 22:53:20 +0200 Subject: [PATCH] Add SSAShort, shift, and, or, and fix unaligned store --- src/CMakeLists.txt | 1 + src/r_compiler/ssa/ssa_float_ptr.cpp | 11 +- src/r_compiler/ssa/ssa_int.cpp | 30 ++++++ src/r_compiler/ssa/ssa_int.h | 7 ++ src/r_compiler/ssa/ssa_int_ptr.cpp | 4 +- src/r_compiler/ssa/ssa_short.cpp | 148 +++++++++++++++++++++++++++ src/r_compiler/ssa/ssa_short.h | 49 +++++++++ src/r_compiler/ssa/ssa_ubyte_ptr.cpp | 10 +- src/r_compiler/ssa/ssa_vec4f_ptr.cpp | 10 +- src/r_compiler/ssa/ssa_vec4i_ptr.cpp | 10 +- 10 files changed, 245 insertions(+), 35 deletions(-) create mode 100644 src/r_compiler/ssa/ssa_short.cpp create mode 100644 src/r_compiler/ssa/ssa_short.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 09238ff57d..4b81a24f44 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1434,6 +1434,7 @@ set (PCH_SOURCES r_compiler/ssa/ssa_if_block.cpp r_compiler/ssa/ssa_int.cpp r_compiler/ssa/ssa_int_ptr.cpp + r_compiler/ssa/ssa_short.cpp r_compiler/ssa/ssa_scope.cpp r_compiler/ssa/ssa_struct_type.cpp r_compiler/ssa/ssa_ubyte.cpp diff --git a/src/r_compiler/ssa/ssa_float_ptr.cpp b/src/r_compiler/ssa/ssa_float_ptr.cpp index 4413c6e923..6a1409271b 100644 --- a/src/r_compiler/ssa/ssa_float_ptr.cpp +++ b/src/r_compiler/ssa/ssa_float_ptr.cpp @@ -38,7 +38,6 @@ SSAVec4f SSAFloatPtr::load_unaligned_vec4f() const { llvm::PointerType *m4xfloattypeptr = llvm::VectorType::get(llvm::Type::getFloatTy(SSAScope::context()), 4)->getPointerTo(); return SSAVec4f::from_llvm(SSAScope::builder().Insert(new llvm::LoadInst(SSAScope::builder().CreateBitCast(v, m4xfloattypeptr, SSAScope::hint()), SSAScope::hint(), false, 4), SSAScope::hint())); - // return SSAVec4f::from_llvm(SSAScope::builder().CreateCall(get_intrinsic(llvm::Intrinsic::x86_sse2_loadu_dq), SSAScope::builder().CreateBitCast(v, llvm::PointerType::getUnqual(llvm::IntegerType::get(SSAScope::context(), 8))))); } void SSAFloatPtr::store(const SSAFloat &new_value) @@ -49,17 +48,11 @@ void SSAFloatPtr::store(const SSAFloat &new_value) void SSAFloatPtr::store_vec4f(const SSAVec4f &new_value) { llvm::PointerType *m4xfloattypeptr = llvm::VectorType::get(llvm::Type::getFloatTy(SSAScope::context()), 4)->getPointerTo(); - SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xfloattypeptr, SSAScope::hint()), 16); + SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xfloattypeptr, SSAScope::hint())); } void SSAFloatPtr::store_unaligned_vec4f(const SSAVec4f &new_value) { - /*llvm::Value *values[2] = - { - SSAScope::builder().CreateBitCast(v, llvm::Type::getFloatPtrTy(SSAScope::context())), - new_value.v - }; - SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse_storeu_ps), values);*/ llvm::PointerType *m4xfloattypeptr = llvm::VectorType::get(llvm::Type::getFloatTy(SSAScope::context()), 4)->getPointerTo(); - SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xfloattypeptr, SSAScope::hint())); + SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xfloattypeptr, SSAScope::hint()), 4); } diff --git a/src/r_compiler/ssa/ssa_int.cpp b/src/r_compiler/ssa/ssa_int.cpp index 9f3c54f50c..674f44350f 100644 --- a/src/r_compiler/ssa/ssa_int.cpp +++ b/src/r_compiler/ssa/ssa_int.cpp @@ -115,3 +115,33 @@ SSAInt operator>>(const SSAInt &a, int bits) { return SSAInt::from_llvm(SSAScope::builder().CreateLShr(a.v, bits, SSAScope::hint())); } + +SSAInt operator<<(const SSAInt &a, const SSAInt &bits) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateShl(a.v, bits.v, SSAScope::hint())); +} + +SSAInt operator>>(const SSAInt &a, const SSAInt &bits) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateLShr(a.v, bits.v, SSAScope::hint())); +} + +SSAInt operator&(const SSAInt &a, int b) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateAnd(a.v, b, SSAScope::hint())); +} + +SSAInt operator&(const SSAInt &a, const SSAInt &b) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateAnd(a.v, b.v, SSAScope::hint())); +} + +SSAInt operator|(const SSAInt &a, int b) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateOr(a.v, b, SSAScope::hint())); +} + +SSAInt operator|(const SSAInt &a, const SSAInt &b) +{ + return SSAInt::from_llvm(SSAScope::builder().CreateOr(a.v, b.v, SSAScope::hint())); +} diff --git a/src/r_compiler/ssa/ssa_int.h b/src/r_compiler/ssa/ssa_int.h index 0be37ee7eb..5e373c62e3 100644 --- a/src/r_compiler/ssa/ssa_int.h +++ b/src/r_compiler/ssa/ssa_int.h @@ -39,3 +39,10 @@ SSAInt operator%(const SSAInt &a, int b); SSAInt operator<<(const SSAInt &a, int bits); SSAInt operator>>(const SSAInt &a, int bits); +SSAInt operator<<(const SSAInt &a, const SSAInt &bits); +SSAInt operator>>(const SSAInt &a, const SSAInt &bits); + +SSAInt operator&(const SSAInt &a, int b); +SSAInt operator&(const SSAInt &a, const SSAInt &b); +SSAInt operator|(const SSAInt &a, int b); +SSAInt operator|(const SSAInt &a, const SSAInt &b); diff --git a/src/r_compiler/ssa/ssa_int_ptr.cpp b/src/r_compiler/ssa/ssa_int_ptr.cpp index dd0ca17f6f..3c26370736 100644 --- a/src/r_compiler/ssa/ssa_int_ptr.cpp +++ b/src/r_compiler/ssa/ssa_int_ptr.cpp @@ -48,11 +48,11 @@ void SSAIntPtr::store(const SSAInt &new_value) void SSAIntPtr::store_vec4i(const SSAVec4i &new_value) { llvm::PointerType *m4xint32typeptr = llvm::VectorType::get(llvm::Type::getInt32Ty(SSAScope::context()), 4)->getPointerTo(); - SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xint32typeptr, SSAScope::hint()), 16); + SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xint32typeptr, SSAScope::hint())); } void SSAIntPtr::store_unaligned_vec4i(const SSAVec4i &new_value) { llvm::PointerType *m4xint32typeptr = llvm::VectorType::get(llvm::Type::getInt32Ty(SSAScope::context()), 4)->getPointerTo(); - SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xint32typeptr, SSAScope::hint())); + SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m4xint32typeptr, SSAScope::hint()), 4); } diff --git a/src/r_compiler/ssa/ssa_short.cpp b/src/r_compiler/ssa/ssa_short.cpp new file mode 100644 index 0000000000..fc8de9449b --- /dev/null +++ b/src/r_compiler/ssa/ssa_short.cpp @@ -0,0 +1,148 @@ + +#include "ssa_short.h" +#include "ssa_float.h" +#include "ssa_int.h" +#include "ssa_scope.h" +#include "r_compiler/llvm_include.h" + +SSAShort::SSAShort() +: v(0) +{ +} + +SSAShort::SSAShort(int constant) +: v(0) +{ + v = llvm::ConstantInt::get(SSAScope::context(), llvm::APInt(16, constant, true)); +} + +SSAShort::SSAShort(SSAFloat f) +: v(0) +{ + v = SSAScope::builder().CreateFPToSI(f.v, llvm::Type::getInt16Ty(SSAScope::context()), SSAScope::hint()); +} + +SSAShort::SSAShort(llvm::Value *v) +: v(v) +{ +} + +llvm::Type *SSAShort::llvm_type() +{ + return llvm::Type::getInt16Ty(SSAScope::context()); +} + +SSAShort operator+(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateAdd(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator-(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateSub(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator*(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateMul(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator/(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateSDiv(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator%(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateSRem(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator+(int a, const SSAShort &b) +{ + return SSAShort(a) + b; +} + +SSAShort operator-(int a, const SSAShort &b) +{ + return SSAShort(a) - b; +} + +SSAShort operator*(int a, const SSAShort &b) +{ + return SSAShort(a) * b; +} + +SSAShort operator/(int a, const SSAShort &b) +{ + return SSAShort(a) / b; +} + +SSAShort operator%(int a, const SSAShort &b) +{ + return SSAShort(a) % b; +} + +SSAShort operator+(const SSAShort &a, int b) +{ + return a + SSAShort(b); +} + +SSAShort operator-(const SSAShort &a, int b) +{ + return a - SSAShort(b); +} + +SSAShort operator*(const SSAShort &a, int b) +{ + return a * SSAShort(b); +} + +SSAShort operator/(const SSAShort &a, int b) +{ + return a / SSAShort(b); +} + +SSAShort operator%(const SSAShort &a, int b) +{ + return a % SSAShort(b); +} + +SSAShort operator<<(const SSAShort &a, int bits) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateShl(a.v, bits, SSAScope::hint())); +} + +SSAShort operator>>(const SSAShort &a, int bits) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateLShr(a.v, bits, SSAScope::hint())); +} + +SSAShort operator<<(const SSAShort &a, const SSAInt &bits) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateShl(a.v, bits.v, SSAScope::hint())); +} + +SSAShort operator>>(const SSAShort &a, const SSAInt &bits) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateLShr(a.v, bits.v, SSAScope::hint())); +} + +SSAShort operator&(const SSAShort &a, int b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateAnd(a.v, b, SSAScope::hint())); +} + +SSAShort operator&(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateAnd(a.v, b.v, SSAScope::hint())); +} + +SSAShort operator|(const SSAShort &a, int b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateOr(a.v, b, SSAScope::hint())); +} + +SSAShort operator|(const SSAShort &a, const SSAShort &b) +{ + return SSAShort::from_llvm(SSAScope::builder().CreateOr(a.v, b.v, SSAScope::hint())); +} diff --git a/src/r_compiler/ssa/ssa_short.h b/src/r_compiler/ssa/ssa_short.h new file mode 100644 index 0000000000..ae71a13363 --- /dev/null +++ b/src/r_compiler/ssa/ssa_short.h @@ -0,0 +1,49 @@ + +#pragma once + +namespace llvm { class Value; } +namespace llvm { class Type; } + +class SSAFloat; +class SSAInt; + +class SSAShort +{ +public: + SSAShort(); + SSAShort(int constant); + SSAShort(SSAFloat f); + explicit SSAShort(llvm::Value *v); + static SSAShort from_llvm(llvm::Value *v) { return SSAShort(v); } + static llvm::Type *llvm_type(); + + llvm::Value *v; +}; + +SSAShort operator+(const SSAShort &a, const SSAShort &b); +SSAShort operator-(const SSAShort &a, const SSAShort &b); +SSAShort operator*(const SSAShort &a, const SSAShort &b); +SSAShort operator/(const SSAShort &a, const SSAShort &b); +SSAShort operator%(const SSAShort &a, const SSAShort &b); + +SSAShort operator+(int a, const SSAShort &b); +SSAShort operator-(int a, const SSAShort &b); +SSAShort operator*(int a, const SSAShort &b); +SSAShort operator/(int a, const SSAShort &b); +SSAShort operator%(int a, const SSAShort &b); + +SSAShort operator+(const SSAShort &a, int b); +SSAShort operator-(const SSAShort &a, int b); +SSAShort operator*(const SSAShort &a, int b); +SSAShort operator/(const SSAShort &a, int b); +SSAShort operator%(const SSAShort &a, int b); + +SSAShort operator<<(const SSAShort &a, int bits); +SSAShort operator>>(const SSAShort &a, int bits); +SSAShort operator<<(const SSAShort &a, const SSAInt &bits); +SSAShort operator>>(const SSAShort &a, const SSAInt &bits); + +SSAShort operator&(const SSAShort &a, int b); +SSAShort operator&(const SSAShort &a, const SSAShort &b); +SSAShort operator|(const SSAShort &a, int b); +SSAShort operator|(const SSAShort &a, const SSAShort &b); diff --git a/src/r_compiler/ssa/ssa_ubyte_ptr.cpp b/src/r_compiler/ssa/ssa_ubyte_ptr.cpp index 825806148b..b2408066ee 100644 --- a/src/r_compiler/ssa/ssa_ubyte_ptr.cpp +++ b/src/r_compiler/ssa/ssa_ubyte_ptr.cpp @@ -86,7 +86,7 @@ void SSAUBytePtr::store_vec4ub(const SSAVec4i &new_value) void SSAUBytePtr::store_vec16ub(const SSAVec16ub &new_value) { llvm::PointerType *m16xint8typeptr = llvm::VectorType::get(llvm::Type::getInt8Ty(SSAScope::context()), 16)->getPointerTo(); - llvm::StoreInst *inst = SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m16xint8typeptr, SSAScope::hint()), 16); + llvm::StoreInst *inst = SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m16xint8typeptr, SSAScope::hint())); // The following generates _mm_stream_si128, maybe! // llvm::MDNode *node = llvm::MDNode::get(SSAScope::context(), SSAScope::builder().getInt32(1)); @@ -95,12 +95,6 @@ void SSAUBytePtr::store_vec16ub(const SSAVec16ub &new_value) void SSAUBytePtr::store_unaligned_vec16ub(const SSAVec16ub &new_value) { - /*llvm::Value *values[2] = - { - SSAScope::builder().CreateBitCast(v, llvm::Type::getInt8PtrTy(SSAScope::context())), - new_value.v - }; - SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse2_storeu_dq), values);*/ llvm::PointerType *m16xint8typeptr = llvm::VectorType::get(llvm::Type::getInt8Ty(SSAScope::context()), 16)->getPointerTo(); - llvm::StoreInst *inst = SSAScope::builder().CreateStore(new_value.v, SSAScope::builder().CreateBitCast(v, m16xint8typeptr, SSAScope::hint())); + llvm::StoreInst *inst = SSAScope::builder().CreateAlignedStore(new_value.v, SSAScope::builder().CreateBitCast(v, m16xint8typeptr, SSAScope::hint()), 4); } diff --git a/src/r_compiler/ssa/ssa_vec4f_ptr.cpp b/src/r_compiler/ssa/ssa_vec4f_ptr.cpp index e2df64167a..6a197ec90e 100644 --- a/src/r_compiler/ssa/ssa_vec4f_ptr.cpp +++ b/src/r_compiler/ssa/ssa_vec4f_ptr.cpp @@ -35,16 +35,10 @@ SSAVec4f SSAVec4fPtr::load_unaligned() const void SSAVec4fPtr::store(const SSAVec4f &new_value) { - SSAScope::builder().CreateAlignedStore(new_value.v, v, 16, false); + SSAScope::builder().CreateStore(new_value.v, v, false); } void SSAVec4fPtr::store_unaligned(const SSAVec4f &new_value) { - /*llvm::Value *values[2] = - { - SSAScope::builder().CreateBitCast(v, llvm::Type::getFloatPtrTy(SSAScope::context())), - new_value.v - }; - SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse_storeu_ps), values);*/ - SSAScope::builder().CreateStore(new_value.v, v, false); + SSAScope::builder().CreateAlignedStore(new_value.v, v, 4, false); } diff --git a/src/r_compiler/ssa/ssa_vec4i_ptr.cpp b/src/r_compiler/ssa/ssa_vec4i_ptr.cpp index a28befb707..7138c30d2a 100644 --- a/src/r_compiler/ssa/ssa_vec4i_ptr.cpp +++ b/src/r_compiler/ssa/ssa_vec4i_ptr.cpp @@ -35,16 +35,10 @@ SSAVec4i SSAVec4iPtr::load_unaligned() const void SSAVec4iPtr::store(const SSAVec4i &new_value) { - SSAScope::builder().CreateAlignedStore(new_value.v, v, 16, false); + SSAScope::builder().CreateStore(new_value.v, v, false); } void SSAVec4iPtr::store_unaligned(const SSAVec4i &new_value) { - /*llvm::Value *values[2] = - { - v, - new_value.v - }; - SSAScope::builder().CreateCall(SSAScope::intrinsic(llvm::Intrinsic::x86_sse2_storeu_pd), values);*/ - SSAScope::builder().CreateStore(new_value.v, v, false); + SSAScope::builder().CreateAlignedStore(new_value.v, v, 4, false); }