Skip to content

Commit

Permalink
Add the helper classes for abstract interpretation.
Browse files Browse the repository at this point in the history
Including:
- AbsValue.cpp. The abstract representation of a 'value'.
- AbsOpStack.cpp. The abstract representation of an operand stack.
- AbsOpArray. The abstract representation of an operand array.

Issue: eclipse-omr#5488

Signed-off-by: cijie xia <[email protected]>
  • Loading branch information
xiacijie committed Nov 16, 2020
1 parent daaa2fc commit 87b9607
Show file tree
Hide file tree
Showing 7 changed files with 553 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ compiler_library(optimizer
${CMAKE_CURRENT_LIST_DIR}/ValuePropagationCommon.cpp
${CMAKE_CURRENT_LIST_DIR}/TrivialDeadBlockRemover.cpp
${CMAKE_CURRENT_LIST_DIR}/FEInliner.cpp
${CMAKE_CURRENT_LIST_DIR}/abstractinterpreter/AbsValue.cpp
${CMAKE_CURRENT_LIST_DIR}/abstractinterpreter/AbsOpStack.cpp
${CMAKE_CURRENT_LIST_DIR}/abstractinterpreter/AbsOpArray.cpp
)
89 changes: 89 additions & 0 deletions compiler/optimizer/abstractinterpreter/AbsOpArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* Copyright (c) 2020, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

#include "optimizer/abstractinterpreter/AbsOpArray.hpp"

TR::AbsOpArray* TR::AbsOpArray::clone(TR::Region& region) const
{
TR::AbsOpArray* copy = new (region) TR::AbsOpArray(_container.size(), region);
for (size_t i = 0; i < _container.size(); i ++)
{
copy->_container[i] = _container[i] ? _container[i]->clone(region) : NULL;
}
return copy;
}

void TR::AbsOpArray::merge(const TR::AbsOpArray* other, TR::Region& region)
{
TR_ASSERT_FATAL(other->size() == size(), "Op Array Size not equal! other:%d vs self:%d\n", other->size(), size());

for (size_t i = 0; i < size(); i++)
{
TR::AbsValue *selfValue = at(i);
TR::AbsValue *otherValue = other->at(i);

if (!selfValue && !otherValue)
{
continue;
}
else if (selfValue && otherValue)
{
TR::AbsValue* mergedVal = selfValue->merge(otherValue);
set(i, mergedVal);
}
else if (selfValue)
{
set(i, selfValue);
}
else
{
set(i, otherValue->clone(region));
}
}
}

void TR::AbsOpArray::set(uint32_t index, TR::AbsValue *value)
{
TR_ASSERT_FATAL(index < size(), "Index out of range! Max array size: %d, Index: %d\n", size(), index);
_container[index] = value;
}

TR::AbsValue* TR::AbsOpArray::at(uint32_t index) const
{
TR_ASSERT_FATAL(index < size(), "Index out of range! Max array size: %d, Index: %d\n", size(), index);
return _container[index];
}

void TR::AbsOpArray::print(TR::Compilation* comp) const
{
traceMsg(comp, "Contents of Abstract Local Variable Array:\n");
for (size_t i = 0; i < size(); i++)
{
traceMsg(comp, "A[%d] = ", i);
if (!at(i))
traceMsg(comp, "Uninitialized");
else
at(i)->print(comp);

traceMsg(comp, "\n");
}
traceMsg(comp, "\n");
}
81 changes: 81 additions & 0 deletions compiler/optimizer/abstractinterpreter/AbsOpArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2020, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

#ifndef ABS_OP_ARRAY
#define ABS_OP_ARRAY

#include "env/Region.hpp"
#include "optimizer/abstractinterpreter/AbsValue.hpp"
#include "infra/vector.hpp"

namespace TR {

/**
.* Abstract representation of the operand array.
*/
class AbsOpArray
{
public:

AbsOpArray(uint32_t maxArraySize, TR::Region& region) :
_container(maxArraySize, NULL, region)
{}

/**
* @brief Clone the operand array
*
* @param region The memory region where the cloned operand array should be allocated.
* @return the cloned operand array
*/
TR::AbsOpArray *clone(TR::Region& region) const;

/**
* @brief Perform an in-place merge with another operand array.
*
* @param other The operand array to be merged with.
*/
void merge(const TR::AbsOpArray* other, TR::Region& region);

/**
* @brief Get the abstract value at index i.
*
* @param i the array index
* @return the abstract value
*/
TR::AbsValue *at(uint32_t i) const;

/**
* @brief Set the abstract value at index i.
*
* @param i the array index
* @param value the abstract value to be set
*/
void set(uint32_t i, TR::AbsValue* value);

size_t size() const { return _container.size(); }
void print(TR::Compilation* comp) const;

private:
TR::vector<TR::AbsValue*, TR::Region&> _container;
};

}
#endif
81 changes: 81 additions & 0 deletions compiler/optimizer/abstractinterpreter/AbsOpStack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright (c) 2020, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

#include "optimizer/abstractinterpreter/AbsOpStack.hpp"

TR::AbsOpStack* TR::AbsOpStack::clone(TR::Region &region) const
{
TR::AbsOpStack* copy = new (region) TR::AbsOpStack(region);
for (size_t i = 0; i < _container.size(); i ++)
{
copy->_container.push_back(_container[i] ? _container[i]->clone(region) : NULL);
}
return copy;
}

TR::AbsValue* TR::AbsOpStack::pop()
{
TR_ASSERT_FATAL(size() > 0, "Pop an empty stack!");
TR::AbsValue *value = _container.back();
_container.pop_back();
return value;
}

void TR::AbsOpStack::merge(const TR::AbsOpStack* other, TR::Region& region)
{
TR_ASSERT_FATAL(other->_container.size() == _container.size(), "Stacks have different sizes! other: %d vs self: %d", other->_container.size(), _container.size());

for (size_t i = 0; i < _container.size(); i ++)
{
if (_container[i] == NULL)
_container[i] = other->_container[i]->clone(region);
else
_container[i]->merge(other->_container[i]);
}
}

void TR::AbsOpStack::print(TR::Compilation* comp) const
{
traceMsg(comp, "Contents of Abstract Operand Stack:\n");

const size_t stackSize = size();

if (stackSize == 0)
{
traceMsg(comp, "<empty>\n\n");
return;
}

traceMsg(comp, "<top>\n");

for (size_t i = 0; i < stackSize; i++)
{
TR::AbsValue *value = _container[stackSize - i -1 ];
traceMsg(comp, "S[%d] = ", stackSize - i - 1);
if (value)
value->print(comp);
else
traceMsg(comp, "Uninitialized");
traceMsg(comp, "\n");
}

traceMsg(comp, "<bottom>\n\n");
}
90 changes: 90 additions & 0 deletions compiler/optimizer/abstractinterpreter/AbsOpStack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* Copyright (c) 2020, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/

#ifndef ABS_OP_STACK_INCL
#define ABS_OP_STACK_INCL

#include "env/Region.hpp"
#include "infra/vector.hpp"
#include "optimizer/abstractinterpreter/AbsValue.hpp"

namespace TR {

/**
* Abstract representation of the operand stack.
*/
class AbsOpStack
{
public:
explicit AbsOpStack(TR::Region& region) :
_container(region)
{}

/**
* @brief Clone an operand stack
*
* @param region The memory region where the cloned operand stack should be allocated.
* @return the cloned operand stack
*/
TR::AbsOpStack* clone(TR::Region& region) const;

/**
* @brief Perform an in-place merge another operand stack.
*
* @param other the operand stack to be merged with
*/
void merge(const TR::AbsOpStack* other, TR::Region& region);

/**
* @brief Push an abstract value onto the operand stack.
*
* @param value the value to be pushed
*/
void push(TR::AbsValue* value) { _container.push_back(value); }

/**
* @brief Peek the top value on the operand stack (stack top; not lattice top).
* @note the stack must not be empty
*
* @return the abstract value
*/
TR::AbsValue* peek() const { TR_ASSERT_FATAL(size() > 0, "Peek an empty stack!"); return _container.back(); }

/**
* @brief Get and pop a value off of the operand stack.
* @note the stack must not be empty.
*
* @return the abstract value
*/
TR::AbsValue* pop();

bool empty() const { return _container.empty(); }
size_t size() const { return _container.size(); }

void print(TR::Compilation* comp) const;

private:
TR::vector<TR::AbsValue*, TR::Region&> _container;
};

}

#endif
Loading

0 comments on commit 87b9607

Please sign in to comment.