Path: blob/master/runtime/compiler/x/codegen/J9Linkage.cpp
6004 views
/*******************************************************************************1* Copyright (c) 2000, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/2122#include "codegen/Linkage.hpp"23#include "codegen/Linkage_inlines.hpp"2425/** \brief26* Align stackIndex so that rsp+stackIndex is a multiple of localObjectAlignment27*28* \param stackIndex29* Stack offset to be aligned (negative)30*31* \return32* stackIndex is modified in place.33*/34void J9::X86::Linkage::alignOffset(uint32_t &stackIndex, int32_t localObjectAlignment)35{36/* On the entry of a method37* RSP = 16*N + sizeOfReturnAddress38* The address of a local object needs to be aligned to localObjectAlignment39* Thus its offset needs to be aligned to localObjectAlignment*N - sizeOfReturnAddress40* i.e. (sizeOfReturnAddress -stackIndex) % localObjectAlignment = 041*42* Limitation of current implementation:43* Only support alignment of 16-byte or less, if larger alignment is required, rsp has to be44* localObjectAlignment*N + sizeOfReturnAddress (on the entry of a method)45*/46uint32_t sizeOfReturnAddress = self()->getProperties().getRetAddressWidth();47// sizeOfReturnAddress-stackIndex is always positive if stackIndex is read as a signed integer48uint32_t remainder = (sizeOfReturnAddress - stackIndex) % localObjectAlignment;49if (remainder)50{51// Align stackIndex52uint32_t adjust = localObjectAlignment - remainder;53stackIndex -= adjust;54}55}5657/** \brief58* Align stackIndex as part of alignment for local object with collected fields59*60* \param stackIndex61* Offset of the first collected references or local objects with collected fields62*63* \return64* stackIndex is modified in place65*66* \note67* Only to be called after stackIndex is calculated in mapStack/mapCompactedStack,68* and before the real stack mapping for collected references and local objects with collected fields69*/70void J9::X86::Linkage::alignLocalObjectWithCollectedFields(uint32_t & stackIndex)71{72int32_t localObjectAlignment = self()->cg()->fej9()->getLocalObjectAlignmentInBytes();73TR::GCStackAtlas *atlas = self()->cg()->getStackAtlas();74uint8_t pointerSize = self()->getProperties().getPointerSize();75// Note that sizeofReferenceAddress is identical to the size of a stack slot76// Both sizeofReferenceAddress and localObjectAlignment are powers of 2,77// and it's safe to skip the alignment when sizeofReferenceAddress is a multiple78// of localObjectAlignment79if (localObjectAlignment <= TR::Compiler->om.sizeofReferenceAddress())80return;81// Collected local objects have gc indice larger than -182// Offset of a collected local object determined by (stackIndex + pointerSize*(localCursor->getGCMapIndex()-firstLocalGCIndex))83// In createStackAtlas, we align pointerSize*(localCursor->getGCMapIndex()-firstLocalGCIndex) by modifying local objects' gc indice84// Here we align the stackIndex85//86traceMsg(self()->comp(),"\nLOCAL OBJECT ALIGNMENT: stack offset before alignment: %d,", stackIndex);87// When compaction is enabled, stackIndex is calculated using only collected local refs/objects size,88// it doesn't include the size of padding slots added by aligning collected local objects' gc indice89// Add them to reflect the correct space needed for collected locals90if (self()->cg()->getLocalsIG() && self()->cg()->getSupportsCompactedLocals())91{92uint8_t pointerSize = self()->getProperties().getPointerSize();93stackIndex -= pointerSize*atlas->getNumberOfPaddingSlots();94traceMsg(self()->comp()," with padding: %d,", stackIndex);95}9697uint32_t stackIndexBeforeAlignment = stackIndex;98self()->alignOffset(stackIndex, localObjectAlignment);99100traceMsg(self()->comp()," after alignment: %d\n", stackIndex);101102// Update numberOfSlotsMapped in stackAtlas otherwise there will be mis-match103// between the compile-time gc maps and the runtime gc maps104//105uint32_t adjust = stackIndexBeforeAlignment - stackIndex;106int32_t numberOfSlotsMapped = atlas->getNumberOfSlotsMapped();107atlas->setNumberOfSlotsMapped(numberOfSlotsMapped + adjust/pointerSize);108}109110/** \brief111* Align stack offset local object without collected fields112*113* \param stackIndex114* Offset of the local object115*116* \return117* stackIndex is modified in place118*/119void J9::X86::Linkage::alignLocalObjectWithoutCollectedFields(uint32_t & stackIndex)120{121int32_t localObjectAlignment = self()->cg()->fej9()->getLocalObjectAlignmentInBytes();122// Note that sizeofReferenceAddress is identical to the size of a stack slot123// Both sizeofReferenceAddress and localObjectAlignment are powers of 2,124// and it's safe to skip the alignment when sizeofReferenceAddress is a multiple125// of localObjectAlignment126if (localObjectAlignment <= TR::Compiler->om.sizeofReferenceAddress())127return;128// Align uncollected local object129traceMsg(self()->comp(), "\nLOCAL OBJECT ALIGNMENT: stack offset before alignment: %d,", stackIndex);130131self()->alignOffset(stackIndex, localObjectAlignment);132133traceMsg(self()->comp(), " after alignment: %d\n", stackIndex);134}135136137