From dd497625a5e5fcc9380a1adafea9fe41a9eda7d6 Mon Sep 17 00:00:00 2001 From: Eladash Date: Wed, 29 Jul 2020 23:13:41 +0300 Subject: [PATCH] PPU LLVM: Fix constant folding of BitCast --- rpcs3/Emu/Cell/PPUTranslator.cpp | 48 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/rpcs3/Emu/Cell/PPUTranslator.cpp b/rpcs3/Emu/Cell/PPUTranslator.cpp index 6814344885..591819f8d4 100644 --- a/rpcs3/Emu/Cell/PPUTranslator.cpp +++ b/rpcs3/Emu/Cell/PPUTranslator.cpp @@ -129,9 +129,8 @@ PPUTranslator::PPUTranslator(LLVMContext& context, Module* _module, const ppu_mo m_reloc = &m_info.segs[0]; } - const auto nan_u32 = ConstantInt::get(get_type(), 0x7FC00000u); - const auto nan_f32 = ConstantExpr::getBitCast(nan_u32, get_type()); - nan_vec4 = ConstantVector::getSplat(4, nan_f32); + const auto nan_v = v128::from32p(0x7FC00000u); + nan_vec4 = make_const_vector(nan_v, get_type()); } PPUTranslator::~PPUTranslator() @@ -236,13 +235,13 @@ Value* PPUTranslator::VecHandleNan(Value* val) Value* PPUTranslator::VecHandleDenormal(Value* val) { const auto type = val->getType(); - const auto value = type == GetType() ? val : m_ir->CreateBitCast(val, GetType()); + const auto value = bitcast(val, GetType()); const auto mask = SExt(m_ir->CreateICmpEQ(m_ir->CreateAnd(value, Broadcast(RegLoad(m_jm_mask), 4)), ConstantVector::getSplat(4, m_ir->getInt32(0))), GetType()); const auto nz = m_ir->CreateLShr(mask, 1); const auto result = m_ir->CreateAnd(m_ir->CreateNot(nz), value); - return type == GetType() ? result : m_ir->CreateBitCast(result, type); + return bitcast(result, type); } Value* PPUTranslator::VecHandleResult(Value* val) @@ -398,7 +397,7 @@ void PPUTranslator::FlushRegisters() m_ir->SetInsertPoint(block); } - m_ir->CreateStore(local, m_ir->CreateBitCast(m_globals[index], local->getType()->getPointerTo())); + m_ir->CreateStore(local, bitcast(m_globals[index], local->getType()->getPointerTo())); m_globals[index] = nullptr; } } @@ -414,20 +413,20 @@ Value* PPUTranslator::Solid(Value* value) if (value->getType() == GetType()) { - return m_ir->CreateBitCast(SExt(value, GetType()), m_ir->getIntNTy(128)); + return bitcast(SExt(value, GetType()), m_ir->getIntNTy(128)); } if (value->getType() == GetType()) { - return m_ir->CreateBitCast(SExt(value, GetType()), m_ir->getIntNTy(128)); + return bitcast(SExt(value, GetType()), m_ir->getIntNTy(128)); } if (value->getType() == GetType()) { - return m_ir->CreateBitCast(SExt(value, GetType()), m_ir->getIntNTy(128)); + return bitcast(SExt(value, GetType()), m_ir->getIntNTy(128)); } - return m_ir->CreateBitCast(value, m_ir->getIntNTy(size)); + return bitcast(value, m_ir->getIntNTy(size)); } Value* PPUTranslator::IsZero(Value* value) @@ -580,7 +579,7 @@ void PPUTranslator::UseCondition(MDNode* hint, Value* cond) llvm::Value* PPUTranslator::GetMemory(llvm::Value* addr, llvm::Type* type) { - return m_ir->CreateBitCast(m_ir->CreateGEP(m_base_loaded, {m_ir->getInt64(0), addr}), type->getPointerTo()); + return bitcast(m_ir->CreateGEP(m_base_loaded, {m_ir->getInt64(0), addr}), type->getPointerTo()); } Value* PPUTranslator::ReadMemory(Value* addr, Type* type, bool is_be, u32 align) @@ -592,7 +591,7 @@ Value* PPUTranslator::ReadMemory(Value* addr, Type* type, bool is_be, u32 align) // Read, byteswap, bitcast const auto int_type = m_ir->getIntNTy(size); const auto value = m_ir->CreateAlignedLoad(GetMemory(addr, int_type), llvm::MaybeAlign{align}, true); - return m_ir->CreateBitCast(Call(int_type, fmt::format("llvm.bswap.i%u", size), value), type); + return bitcast(Call(int_type, fmt::format("llvm.bswap.i%u", size), value), type); } // Read normally @@ -608,7 +607,7 @@ void PPUTranslator::WriteMemory(Value* addr, Value* value, bool is_be, u32 align { // Bitcast, byteswap const auto int_type = m_ir->getIntNTy(size); - value = Call(int_type, fmt::format("llvm.bswap.i%u", size), m_ir->CreateBitCast(value, int_type)); + value = Call(int_type, fmt::format("llvm.bswap.i%u", size), bitcast(value, int_type)); } // Write @@ -2771,7 +2770,7 @@ void PPUTranslator::MTOCRF(ppu_opcode_t op) const auto index = m_ir->CreateAnd(m_ir->CreateLShr(value, 28 - i * 4), 15); const auto src = m_ir->CreateGEP(m_mtocr_table, {m_ir->getInt32(0), m_ir->CreateShl(index, 2)}); - const auto dst = m_ir->CreateBitCast(m_ir->CreateStructGEP(nullptr, m_thread, static_cast(m_cr - m_locals) + i * 4), GetType()); + const auto dst = bitcast(m_ir->CreateStructGEP(nullptr, m_thread, static_cast(m_cr - m_locals) + i * 4), GetType()); Call(GetType(), "llvm.memcpy.p0i8.p0i8.i32", dst, src, m_ir->getInt32(4), m_ir->getFalse()); } } @@ -4553,15 +4552,15 @@ Value* PPUTranslator::GetFpr(u32 r, u32 bits, bool as_int) } else { - return Trunc(m_ir->CreateBitCast(value, GetType()), m_ir->getIntNTy(bits)); + return Trunc(bitcast(value, GetType()), m_ir->getIntNTy(bits)); } } void PPUTranslator::SetFpr(u32 r, Value* val) { const auto f64_val = - val->getType() == GetType() ? m_ir->CreateBitCast(SExt(val), GetType()) : - val->getType() == GetType() ? m_ir->CreateBitCast(val, GetType()) : + val->getType() == GetType() ? bitcast(SExt(val), GetType()) : + val->getType() == GetType() ? bitcast(val, GetType()) : val->getType() == GetType() ? m_ir->CreateFPExt(val, GetType()) : val; RegStore(f64_val, m_fpr[r]); @@ -4571,16 +4570,19 @@ Value* PPUTranslator::GetVr(u32 vr, VrType type) { const auto value = RegLoad(m_vr[vr]); + llvm::Type* _type{}; + switch (type) { - case VrType::vi32: return m_ir->CreateBitCast(value, GetType()); - case VrType::vi8: return m_ir->CreateBitCast(value, GetType()); - case VrType::vi16: return m_ir->CreateBitCast(value, GetType()); - case VrType::vf: return m_ir->CreateBitCast(value, GetType()); - case VrType::i128: return m_ir->CreateBitCast(value, GetType()); + case VrType::vi32: _type = GetType(); break; + case VrType::vi8 : _type = GetType(); break; + case VrType::vi16: _type = GetType(); break; + case VrType::vf : _type = GetType(); break; + case VrType::i128: _type = GetType(); break; + default: report_fatal_error("GetVr(): invalid type"); } - report_fatal_error("GetVr(): invalid type"); + return bitcast(value, _type); } void PPUTranslator::SetVr(u32 vr, Value* value)