From eec258c2d29d0b36635ad2e398603d6342176842 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 10 Nov 2022 16:04:19 +0100 Subject: [PATCH] Bugfix. --- libyul/optimiser/KnowledgeBase.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/libyul/optimiser/KnowledgeBase.cpp b/libyul/optimiser/KnowledgeBase.cpp index 23627653a..558dc8988 100644 --- a/libyul/optimiser/KnowledgeBase.cpp +++ b/libyul/optimiser/KnowledgeBase.cpp @@ -122,24 +122,33 @@ optional KnowledgeBase::explore(Expression const& else if (Identifier const* identifier = std::get_if(&_value)) return explore(identifier->name); else if (FunctionCall const* f = get_if(&_value)) - if (f->functionName.name == "add"_yulstring || f->functionName.name == "sub"_yulstring) + { + if (f->functionName.name == "add"_yulstring) + { if (optional a = explore(f->arguments[0])) if (optional b = explore(f->arguments[1])) { - u256 offset = - f->functionName.name == "add"_yulstring ? - a->offset + b->offset : - a->offset - b->offset; - if (a->reference == b->reference) - // Offsets relative to the same reference variable - return VariableOffset{a->reference, offset}; - else if (a->reference == YulString{}) + u256 offset = a->offset + b->offset; + if (a->reference.empty()) // a is constant return VariableOffset{b->reference, offset}; - else if (b->reference == YulString{}) + else if (b->reference.empty()) // b is constant return VariableOffset{a->reference, offset}; } + } + else if (f->functionName.name == "sub"_yulstring) + if (optional a = explore(f->arguments[0])) + if (optional b = explore(f->arguments[1])) + { + u256 offset = a->offset - b->offset; + if (a->reference == b->reference) + return VariableOffset{YulString{}, offset}; + else if (b->reference.empty()) + // b is constant + return VariableOffset{a->reference, offset}; + } + } return {}; }