|
|
@ -54,30 +54,32 @@ llvm::Function* Memory::createRequireFunc(llvm::Module* _module, GasMeter& _gasM |
|
|
|
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "resize", func); |
|
|
|
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "return", func); |
|
|
|
|
|
|
|
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|
|
|
|
|
|
|
// BB "check"
|
|
|
|
llvm::IRBuilder<> builder(checkBB); |
|
|
|
m_builder.SetInsertPoint(checkBB); |
|
|
|
llvm::Value* sizeRequired = func->arg_begin(); |
|
|
|
sizeRequired->setName("sizeRequired"); |
|
|
|
auto size = builder.CreateLoad(m_size, "size"); |
|
|
|
auto resizeNeeded = builder.CreateICmpULE(size, sizeRequired, "resizeNeeded"); |
|
|
|
builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|
|
|
auto size = m_builder.CreateLoad(m_size, "size"); |
|
|
|
auto resizeNeeded = m_builder.CreateICmpULE(size, sizeRequired, "resizeNeeded"); |
|
|
|
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|
|
|
|
|
|
|
// BB "resize"
|
|
|
|
builder.SetInsertPoint(resizeBB); |
|
|
|
m_builder.SetInsertPoint(resizeBB); |
|
|
|
// Check gas first
|
|
|
|
auto wordsRequired = builder.CreateUDiv(builder.CreateAdd(sizeRequired, Constant::get(31)), Constant::get(32), "wordsRequired"); |
|
|
|
auto words = builder.CreateUDiv(builder.CreateAdd(size, Constant::get(31)), Constant::get(32), "words"); |
|
|
|
auto newWords = builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|
|
|
_gasMeter.checkMemory(newWords, builder); |
|
|
|
auto wordsRequired = m_builder.CreateUDiv(m_builder.CreateAdd(sizeRequired, Constant::get(31)), Constant::get(32), "wordsRequired"); |
|
|
|
auto words = m_builder.CreateUDiv(m_builder.CreateAdd(size, Constant::get(31)), Constant::get(32), "words"); |
|
|
|
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|
|
|
_gasMeter.checkMemory(newWords, m_builder); |
|
|
|
// Resize
|
|
|
|
builder.CreateStore(sizeRequired, m_size); |
|
|
|
auto newData = builder.CreateCall(m_resize, m_size, "newData"); |
|
|
|
builder.CreateStore(newData, m_data); |
|
|
|
builder.CreateBr(returnBB); |
|
|
|
m_builder.CreateStore(sizeRequired, m_size); |
|
|
|
auto newData = m_builder.CreateCall(m_resize, m_size, "newData"); |
|
|
|
m_builder.CreateStore(newData, m_data); |
|
|
|
m_builder.CreateBr(returnBB); |
|
|
|
|
|
|
|
// BB "return"
|
|
|
|
builder.SetInsertPoint(returnBB); |
|
|
|
builder.CreateRetVoid(); |
|
|
|
m_builder.SetInsertPoint(returnBB); |
|
|
|
m_builder.CreateRetVoid(); |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
@ -90,8 +92,7 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, llvm:: |
|
|
|
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::i256, Type::i256, false); |
|
|
|
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, _module); |
|
|
|
|
|
|
|
auto origBB = m_builder.GetInsertBlock(); |
|
|
|
auto origPt = m_builder.GetInsertPoint(); |
|
|
|
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|
|
|
|
|
|
|
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
|
|
|
llvm::Value* index = func->arg_begin(); |
|
|
@ -116,8 +117,6 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, llvm:: |
|
|
|
m_builder.CreateRet(ret); |
|
|
|
} |
|
|
|
|
|
|
|
m_builder.SetInsertPoint(origBB, origPt); |
|
|
|
|
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|