From 9aecd10a6658e075277dab224ca71def29da39f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 27 Apr 2015 12:56:01 +0200 Subject: [PATCH] Revert not needed LLVM workarounds --- evmjit/libevmjit/Arith256.cpp | 104 ++++++++++++++++------------------ evmjit/libevmjit/Compiler.cpp | 13 ++--- 2 files changed, 55 insertions(+), 62 deletions(-) diff --git a/evmjit/libevmjit/Arith256.cpp b/evmjit/libevmjit/Arith256.cpp index 029eb9370..c7608c5b8 100644 --- a/evmjit/libevmjit/Arith256.cpp +++ b/evmjit/libevmjit/Arith256.cpp @@ -360,18 +360,17 @@ llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2) std::pair Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2) { - // FIXME: Disabled because of llvm::APInt::urem bug -// if (auto c1 = llvm::dyn_cast(_arg1)) -// { -// if (auto c2 = llvm::dyn_cast(_arg2)) -// { -// if (!c2->getValue()) -// return std::make_pair(Constant::get(0), Constant::get(0)); -// auto div = Constant::get(c1->getValue().udiv(c2->getValue())); -// auto mod = Constant::get(c1->getValue().urem(c2->getValue())); -// return std::make_pair(div, mod); -// } -// } + if (auto c1 = llvm::dyn_cast(_arg1)) + { + if (auto c2 = llvm::dyn_cast(_arg2)) + { + if (!c2->getValue()) + return std::make_pair(Constant::get(0), Constant::get(0)); + auto div = Constant::get(c1->getValue().udiv(c2->getValue())); + auto mod = Constant::get(c1->getValue().urem(c2->getValue())); + return std::make_pair(div, mod); + } + } auto r = createCall(getDivFunc(Type::Word), {_arg1, _arg2}); auto div = m_builder.CreateExtractValue(r, 0, "div"); @@ -381,18 +380,17 @@ std::pair Arith256::div(llvm::Value* _arg1, llvm::Va std::pair Arith256::sdiv(llvm::Value* _x, llvm::Value* _y) { - // FIXME: Disabled because of llvm::APInt::urem bug -// if (auto c1 = llvm::dyn_cast(_x)) -// { -// if (auto c2 = llvm::dyn_cast(_y)) -// { -// if (!c2->getValue()) -// return std::make_pair(Constant::get(0), Constant::get(0)); -// auto div = Constant::get(c1->getValue().sdiv(c2->getValue())); -// auto mod = Constant::get(c1->getValue().srem(c2->getValue())); -// return std::make_pair(div, mod); -// } -// } + if (auto c1 = llvm::dyn_cast(_x)) + { + if (auto c2 = llvm::dyn_cast(_y)) + { + if (!c2->getValue()) + return std::make_pair(Constant::get(0), Constant::get(0)); + auto div = Constant::get(c1->getValue().sdiv(c2->getValue())); + auto mod = Constant::get(c1->getValue().srem(c2->getValue())); + return std::make_pair(div, mod); + } + } auto xIsNeg = m_builder.CreateICmpSLT(_x, Constant::get(0)); auto xNeg = m_builder.CreateSub(Constant::get(0), _x); @@ -447,42 +445,40 @@ llvm::Value* Arith256::exp(llvm::Value* _arg1, llvm::Value* _arg2) llvm::Value* Arith256::addmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3) { - // FIXME: Disabled because of llvm::APInt::urem bug -// if (auto c1 = llvm::dyn_cast(_arg1)) -// { -// if (auto c2 = llvm::dyn_cast(_arg2)) -// { -// if (auto c3 = llvm::dyn_cast(_arg3)) -// { -// if (!c3->getValue()) -// return Constant::get(0); -// auto s = c1->getValue().zext(256+64) + c2->getValue().zext(256+64); -// auto r = s.urem(c3->getValue().zext(256+64)).trunc(256); -// return Constant::get(r); -// } -// } -// } + if (auto c1 = llvm::dyn_cast(_arg1)) + { + if (auto c2 = llvm::dyn_cast(_arg2)) + { + if (auto c3 = llvm::dyn_cast(_arg3)) + { + if (!c3->getValue()) + return Constant::get(0); + auto s = c1->getValue().zext(256+64) + c2->getValue().zext(256+64); + auto r = s.urem(c3->getValue().zext(256+64)).trunc(256); + return Constant::get(r); + } + } + } return createCall(getAddModFunc(), {_arg1, _arg2, _arg3}); } llvm::Value* Arith256::mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Value* _arg3) { - // FIXME: Disabled because of llvm::APInt::urem bug -// if (auto c1 = llvm::dyn_cast(_arg1)) -// { -// if (auto c2 = llvm::dyn_cast(_arg2)) -// { -// if (auto c3 = llvm::dyn_cast(_arg3)) -// { -// if (!c3->getValue()) -// return Constant::get(0); -// auto p = c1->getValue().zext(512) * c2->getValue().zext(512); -// auto r = p.urem(c3->getValue().zext(512)).trunc(256); -// return Constant::get(r); -// } -// } -// } + if (auto c1 = llvm::dyn_cast(_arg1)) + { + if (auto c2 = llvm::dyn_cast(_arg2)) + { + if (auto c3 = llvm::dyn_cast(_arg3)) + { + if (!c3->getValue()) + return Constant::get(0); + auto p = c1->getValue().zext(512) * c2->getValue().zext(512); + auto r = p.urem(c3->getValue().zext(512)).trunc(256); + return Constant::get(r); + } + } + } return createCall(getMulModFunc(), {_arg1, _arg2, _arg3}); } diff --git a/evmjit/libevmjit/Compiler.cpp b/evmjit/libevmjit/Compiler.cpp index d5cb493ab..25f5932df 100644 --- a/evmjit/libevmjit/Compiler.cpp +++ b/evmjit/libevmjit/Compiler.cpp @@ -417,17 +417,14 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, RuntimeManager& _runti case Instruction::BYTE: { - const auto byteNum = stack.pop(); - auto value = stack.pop(); + const auto idx = stack.pop(); + auto value = Endianness::toBE(m_builder, stack.pop()); - value = Endianness::toBE(m_builder, value); + auto idxValid = m_builder.CreateICmpULT(idx, Constant::get(32), "idxValid"); auto bytes = m_builder.CreateBitCast(value, llvm::VectorType::get(Type::Byte, 32), "bytes"); - auto safeByteNum = m_builder.CreateZExt(m_builder.CreateTrunc(byteNum, m_builder.getIntNTy(5)), Type::lowPrecision); // Trim index, large values can crash - auto byte = m_builder.CreateExtractElement(bytes, safeByteNum, "byte"); + auto byte = m_builder.CreateExtractElement(bytes, idx, "byte"); value = m_builder.CreateZExt(byte, Type::Word); - - auto byteNumValid = m_builder.CreateICmpULT(byteNum, Constant::get(32)); - value = m_builder.CreateSelect(byteNumValid, value, Constant::get(0)); + value = m_builder.CreateSelect(idxValid, value, Constant::get(0)); stack.push(value); break; }