2013-05-31 20:14:49 +08:00
|
|
|
//===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a copy of the generic LLVM PrologEpilogInserter pass, modified
|
|
|
|
// to remove unneeded functionality and to handle virtual registers. Most code
|
|
|
|
// here is a copy of PrologEpilogInserter.cpp.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NVPTX.h"
|
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Pass.h"
|
2013-05-31 20:14:49 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "nvptx-prolog-epilog"
|
|
|
|
|
2013-05-31 20:14:49 +08:00
|
|
|
namespace {
|
|
|
|
class NVPTXPrologEpilogPass : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
|
|
|
|
|
2014-04-29 15:57:44 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void calculateFrameObjectOffsets(MachineFunction &Fn);
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
|
|
|
|
return new NVPTXPrologEpilogPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
char NVPTXPrologEpilogPass::ID = 0;
|
|
|
|
|
|
|
|
bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
|
2015-01-30 09:50:09 +08:00
|
|
|
const TargetSubtargetInfo &STI = MF.getSubtarget();
|
|
|
|
const TargetFrameLowering &TFI = *STI.getFrameLowering();
|
|
|
|
const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
|
2013-05-31 20:14:49 +08:00
|
|
|
bool Modified = false;
|
|
|
|
|
|
|
|
calculateFrameObjectOffsets(MF);
|
|
|
|
|
2016-07-09 05:10:58 +08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
|
|
|
for (MachineInstr &MI : MBB) {
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
|
|
|
|
if (!MI.getOperand(i).isFI())
|
2013-05-31 20:14:49 +08:00
|
|
|
continue;
|
2014-04-25 13:30:21 +08:00
|
|
|
TRI.eliminateFrameIndex(MI, 0, i, nullptr);
|
2013-05-31 20:14:49 +08:00
|
|
|
Modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add function prolog/epilog
|
[ShrinkWrap] Add (a simplified version) of shrink-wrapping.
This patch introduces a new pass that computes the safe point to insert the
prologue and epilogue of the function.
The interest is to find safe points that are cheaper than the entry and exits
blocks.
As an example and to avoid regressions to be introduce, this patch also
implements the required bits to enable the shrink-wrapping pass for AArch64.
** Context **
Currently we insert the prologue and epilogue of the method/function in the
entry and exits blocks. Although this is correct, we can do a better job when
those are not immediately required and insert them at less frequently executed
places.
The job of the shrink-wrapping pass is to identify such places.
** Motivating example **
Let us consider the following function that perform a call only in one branch of
a if:
define i32 @f(i32 %a, i32 %b) {
%tmp = alloca i32, align 4
%tmp2 = icmp slt i32 %a, %b
br i1 %tmp2, label %true, label %false
true:
store i32 %a, i32* %tmp, align 4
%tmp4 = call i32 @doSomething(i32 0, i32* %tmp)
br label %false
false:
%tmp.0 = phi i32 [ %tmp4, %true ], [ %a, %0 ]
ret i32 %tmp.0
}
On AArch64 this code generates (removing the cfi directives to ease
readabilities):
_f: ; @f
; BB#0:
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
LBB0_2: ; %false
mov sp, x29
ldp x29, x30, [sp], #16
ret
With shrink-wrapping we could generate:
_f: ; @f
; BB#0:
cmp w0, w1
b.ge LBB0_2
; BB#1: ; %true
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #16 ; =16
stur w0, [x29, #-4]
sub x1, x29, #4 ; =4
mov w0, wzr
bl _doSomething
add sp, x29, #16 ; =16
ldp x29, x30, [sp], #16
LBB0_2: ; %false
ret
Therefore, we would pay the overhead of setting up/destroying the frame only if
we actually do the call.
** Proposed Solution **
This patch introduces a new machine pass that perform the shrink-wrapping
analysis (See the comments at the beginning of ShrinkWrap.cpp for more details).
It then stores the safe save and restore point into the MachineFrameInfo
attached to the MachineFunction.
This information is then used by the PrologEpilogInserter (PEI) to place the
related code at the right place. This pass runs right before the PEI.
Unlike the original paper of Chow from PLDI’88, this implementation of
shrink-wrapping does not use expensive data-flow analysis and does not need hack
to properly avoid frequently executed point. Instead, it relies on dominance and
loop properties.
The pass is off by default and each target can opt-in by setting the
EnableShrinkWrap boolean to true in their derived class of TargetPassConfig.
This setting can also be overwritten on the command line by using
-enable-shrink-wrap.
Before you try out the pass for your target, make sure you properly fix your
emitProlog/emitEpilog/adjustForXXX method to cope with basic blocks that are not
necessarily the entry block.
** Design Decisions **
1. ShrinkWrap is its own pass right now. It could frankly be merged into PEI but
for debugging and clarity I thought it was best to have its own file.
2. Right now, we only support one save point and one restore point. At some
point we can expand this to several save point and restore point, the impacted
component would then be:
- The pass itself: New algorithm needed.
- MachineFrameInfo: Hold a list or set of Save/Restore point instead of one
pointer.
- PEI: Should loop over the save point and restore point.
Anyhow, at least for this first iteration, I do not believe this is interesting
to support the complex cases. We should revisit that when we motivating
examples.
Differential Revision: http://reviews.llvm.org/D9210
<rdar://problem/3201744>
llvm-svn: 236507
2015-05-06 01:38:16 +08:00
|
|
|
TFI.emitPrologue(MF, MF.front());
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
// If last instruction is a return instruction, add an epilogue
|
2015-09-26 05:25:19 +08:00
|
|
|
if (I->isReturnBlock())
|
2013-05-31 20:14:49 +08:00
|
|
|
TFI.emitEpilogue(MF, *I);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
|
|
|
|
static inline void
|
2016-07-29 02:40:00 +08:00
|
|
|
AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
|
2013-05-31 20:14:49 +08:00
|
|
|
bool StackGrowsDown, int64_t &Offset,
|
|
|
|
unsigned &MaxAlign) {
|
|
|
|
// If the stack grows down, add the object size to find the lowest address.
|
|
|
|
if (StackGrowsDown)
|
2016-07-29 02:40:00 +08:00
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2013-05-31 20:14:49 +08:00
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
unsigned Align = MFI.getObjectAlignment(FrameIdx);
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
// If the alignment of this object is greater than that of the stack, then
|
|
|
|
// increase the stack alignment to match.
|
|
|
|
MaxAlign = std::max(MaxAlign, Align);
|
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
|
|
|
Offset = (Offset + Align - 1) / Align * Align;
|
|
|
|
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
|
2013-05-31 20:14:49 +08:00
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setObjectOffset(FrameIdx, Offset);
|
|
|
|
Offset += MFI.getObjectSize(FrameIdx);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
|
2014-08-05 10:39:49 +08:00
|
|
|
const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
|
|
|
|
const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
bool StackGrowsDown =
|
|
|
|
TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
|
|
|
|
|
|
|
|
// Loop over all of the stack objects, assigning sequential addresses...
|
2016-07-29 02:40:00 +08:00
|
|
|
MachineFrameInfo &MFI = Fn.getFrameInfo();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
// Start at the beginning of the local area.
|
|
|
|
// The Offset is the distance from the stack top in the direction
|
|
|
|
// of stack growth -- so it's always nonnegative.
|
|
|
|
int LocalAreaOffset = TFI.getOffsetOfLocalArea();
|
|
|
|
if (StackGrowsDown)
|
|
|
|
LocalAreaOffset = -LocalAreaOffset;
|
|
|
|
assert(LocalAreaOffset >= 0
|
|
|
|
&& "Local area offset should be in direction of stack growth");
|
|
|
|
int64_t Offset = LocalAreaOffset;
|
|
|
|
|
|
|
|
// If there are fixed sized objects that are preallocated in the local area,
|
|
|
|
// non-fixed objects can't be allocated right at the start of local area.
|
|
|
|
// We currently don't support filling in holes in between fixed sized
|
|
|
|
// objects, so we adjust 'Offset' to point to the end of last fixed sized
|
|
|
|
// preallocated object.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (int i = MFI.getObjectIndexBegin(); i != 0; ++i) {
|
2013-05-31 20:14:49 +08:00
|
|
|
int64_t FixedOff;
|
|
|
|
if (StackGrowsDown) {
|
|
|
|
// The maximum distance from the stack pointer is at lower address of
|
|
|
|
// the object -- which is given by offset. For down growing stack
|
|
|
|
// the offset is negative, so we negate the offset to get the distance.
|
2016-07-29 02:40:00 +08:00
|
|
|
FixedOff = -MFI.getObjectOffset(i);
|
2013-05-31 20:14:49 +08:00
|
|
|
} else {
|
|
|
|
// The maximum distance from the start pointer is at the upper
|
|
|
|
// address of the object.
|
2016-07-29 02:40:00 +08:00
|
|
|
FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|
|
|
|
if (FixedOff > Offset) Offset = FixedOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: We do not have a call stack
|
|
|
|
|
2016-07-29 02:40:00 +08:00
|
|
|
unsigned MaxAlign = MFI.getMaxAlignment();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
// No scavenger
|
|
|
|
|
|
|
|
// FIXME: Once this is working, then enable flag will change to a target
|
|
|
|
// check for whether the frame is large enough to want to use virtual
|
|
|
|
// frame index registers. Functions which don't want/need this optimization
|
|
|
|
// will continue to use the existing code path.
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.getUseLocalStackAllocationBlock()) {
|
|
|
|
unsigned Align = MFI.getLocalFrameMaxAlign();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
// Adjust to alignment boundary.
|
|
|
|
Offset = (Offset + Align - 1) / Align * Align;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
|
|
|
|
|
|
|
|
// Resolve offsets for objects in the local block.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
|
|
|
|
std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
|
2013-05-31 20:14:49 +08:00
|
|
|
int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
|
|
|
|
DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
|
|
|
|
FIOffset << "]\n");
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setObjectOffset(Entry.first, FIOffset);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|
|
|
|
// Allocate the local block
|
2016-07-29 02:40:00 +08:00
|
|
|
Offset += MFI.getLocalFrameSize();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
MaxAlign = std::max(Align, MaxAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No stack protector
|
|
|
|
|
|
|
|
// Then assign frame offsets to stack objects that are not used to spill
|
|
|
|
// callee saved registers.
|
2016-07-29 02:40:00 +08:00
|
|
|
for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
|
|
|
|
if (MFI.isObjectPreAllocated(i) &&
|
|
|
|
MFI.getUseLocalStackAllocationBlock())
|
2013-05-31 20:14:49 +08:00
|
|
|
continue;
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.isDeadObjectIndex(i))
|
2013-05-31 20:14:49 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No scavenger
|
|
|
|
|
|
|
|
if (!TFI.targetHandlesStackFrameRounding()) {
|
|
|
|
// If we have reserved argument space for call sites in the function
|
|
|
|
// immediately on entry to the current function, count it as part of the
|
|
|
|
// overall stack size.
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.adjustsStack() && TFI.hasReservedCallFrame(Fn))
|
|
|
|
Offset += MFI.getMaxCallFrameSize();
|
2013-05-31 20:14:49 +08:00
|
|
|
|
|
|
|
// Round up the size to a multiple of the alignment. If the function has
|
|
|
|
// any calls or alloca's, align to the target's StackAlignment value to
|
|
|
|
// ensure that the callee's frame or the alloca data is suitably aligned;
|
|
|
|
// otherwise, for leaf functions, align to the TransientStackAlignment
|
|
|
|
// value.
|
|
|
|
unsigned StackAlign;
|
2016-07-29 02:40:00 +08:00
|
|
|
if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
|
|
|
|
(RegInfo->needsStackRealignment(Fn) && MFI.getObjectIndexEnd() != 0))
|
2013-05-31 20:14:49 +08:00
|
|
|
StackAlign = TFI.getStackAlignment();
|
|
|
|
else
|
|
|
|
StackAlign = TFI.getTransientStackAlignment();
|
|
|
|
|
|
|
|
// If the frame pointer is eliminated, all frame offsets will be relative to
|
|
|
|
// SP not FP. Align to MaxAlign so this works.
|
|
|
|
StackAlign = std::max(StackAlign, MaxAlign);
|
|
|
|
unsigned AlignMask = StackAlign - 1;
|
|
|
|
Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update frame info to pretend that this is part of the stack...
|
|
|
|
int64_t StackSize = Offset - LocalAreaOffset;
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI.setStackSize(StackSize);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|