2002-04-09 05:21:26 +00:00
|
|
|
//===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
|
|
|
|
//
|
|
|
|
// This file contains the code for the Sparc Target that does not fit in any of
|
|
|
|
// the other files in this directory.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-22 13:44:23 +00:00
|
|
|
|
2001-09-14 03:47:57 +00:00
|
|
|
#include "SparcInternals.h"
|
2002-10-29 20:47:26 +00:00
|
|
|
#include "llvm/Target/TargetMachineImpls.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2002-10-28 01:03:43 +00:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2002-10-28 00:28:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-12-28 20:17:43 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionInfo.h"
|
2002-10-28 01:03:43 +00:00
|
|
|
#include "llvm/CodeGen/PreSelection.h"
|
|
|
|
#include "llvm/CodeGen/StackSlots.h"
|
|
|
|
#include "llvm/CodeGen/PeepholeOpts.h"
|
|
|
|
#include "llvm/CodeGen/InstrSelection.h"
|
|
|
|
#include "llvm/CodeGen/InstrScheduling.h"
|
|
|
|
#include "llvm/CodeGen/RegisterAllocation.h"
|
|
|
|
#include "llvm/CodeGen/MachineCodeForInstruction.h"
|
|
|
|
#include "llvm/Reoptimizer/Mapping/MappingInfo.h"
|
|
|
|
#include "llvm/Reoptimizer/Mapping/FInfo.h"
|
|
|
|
#include "Support/CommandLine.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::cerr;
|
2001-09-15 00:30:44 +00:00
|
|
|
|
2002-12-03 05:41:54 +00:00
|
|
|
static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
|
2001-09-19 15:56:23 +00:00
|
|
|
// Build the MachineInstruction Description Array...
|
2003-01-14 22:00:31 +00:00
|
|
|
const TargetInstrDescriptor SparcMachineInstrDesc[] = {
|
2001-09-19 15:56:23 +00:00
|
|
|
#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
|
|
|
|
NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
|
|
|
|
{ OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
|
2002-12-03 05:41:54 +00:00
|
|
|
NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
|
|
|
|
ImplicitRegUseList, ImplicitRegUseList },
|
2001-09-19 15:56:23 +00:00
|
|
|
#include "SparcInstr.def"
|
|
|
|
};
|
2001-09-18 13:01:29 +00:00
|
|
|
|
2002-10-28 01:03:43 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Command line options to control choice of code generation passes.
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static cl::opt<bool> DisablePreSelect("nopreselect",
|
|
|
|
cl::desc("Disable preselection pass"));
|
|
|
|
|
|
|
|
static cl::opt<bool> DisableSched("nosched",
|
|
|
|
cl::desc("Disable local scheduling pass"));
|
|
|
|
|
|
|
|
static cl::opt<bool> DisablePeephole("nopeephole",
|
|
|
|
cl::desc("Disable peephole optimization pass"));
|
|
|
|
|
2001-09-18 13:01:29 +00:00
|
|
|
//----------------------------------------------------------------------------
|
2001-09-14 16:56:32 +00:00
|
|
|
// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
|
|
|
|
// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
|
2001-09-18 13:01:29 +00:00
|
|
|
//----------------------------------------------------------------------------
|
2001-10-15 16:25:28 +00:00
|
|
|
|
2003-05-27 22:24:48 +00:00
|
|
|
TargetMachine *allocateSparcTargetMachine(unsigned Configuration) {
|
|
|
|
return new UltraSparc();
|
|
|
|
}
|
2001-10-22 13:44:23 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// class UltraSparcFrameInfo
|
|
|
|
//
|
|
|
|
// Interface to stack frame layout info for the UltraSPARC.
|
2001-11-12 23:26:35 +00:00
|
|
|
// Starting offsets for each area of the stack frame are aligned at
|
|
|
|
// a multiple of getStackFrameSizeAlignment().
|
2001-10-22 13:44:23 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
|
2001-11-08 04:55:13 +00:00
|
|
|
bool& pos) const
|
2001-10-22 13:44:23 +00:00
|
|
|
{
|
2001-11-08 04:55:13 +00:00
|
|
|
pos = false; // static stack area grows downwards
|
|
|
|
return StaticAreaOffsetFromFP;
|
2001-10-22 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
|
2001-11-08 04:55:13 +00:00
|
|
|
bool& pos) const
|
2001-10-22 13:44:23 +00:00
|
|
|
{
|
2002-12-28 20:17:43 +00:00
|
|
|
// ensure no more auto vars are added
|
|
|
|
mcInfo.getInfo()->freezeAutomaticVarsArea();
|
2002-04-25 04:43:45 +00:00
|
|
|
|
2001-11-08 04:55:13 +00:00
|
|
|
pos = false; // static stack area grows downwards
|
2002-12-28 20:17:43 +00:00
|
|
|
unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
|
2001-11-12 23:26:35 +00:00
|
|
|
return StaticAreaOffsetFromFP - autoVarsSize;
|
2001-10-22 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
|
2001-11-08 04:55:13 +00:00
|
|
|
bool& pos) const
|
2001-10-22 13:44:23 +00:00
|
|
|
{
|
2002-12-28 20:17:43 +00:00
|
|
|
MachineFunctionInfo *MFI = mcInfo.getInfo();
|
|
|
|
MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
|
|
|
|
MFI->freezeSpillsArea(); // ensure no more spill slots are added
|
2002-04-25 04:43:45 +00:00
|
|
|
|
2001-11-08 04:55:13 +00:00
|
|
|
pos = false; // static stack area grows downwards
|
2002-12-28 20:17:43 +00:00
|
|
|
unsigned autoVarsSize = MFI->getAutomaticVarsSize();
|
|
|
|
unsigned spillAreaSize = MFI->getRegSpillsSize();
|
2001-11-12 23:26:35 +00:00
|
|
|
int offset = autoVarsSize + spillAreaSize;
|
|
|
|
return StaticAreaOffsetFromFP - offset;
|
2001-10-22 13:44:23 +00:00
|
|
|
}
|
|
|
|
|
2001-11-08 04:55:13 +00:00
|
|
|
int
|
2002-10-28 00:28:31 +00:00
|
|
|
UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
|
2001-11-08 04:55:13 +00:00
|
|
|
bool& pos) const
|
|
|
|
{
|
2002-03-18 03:08:07 +00:00
|
|
|
// Dynamic stack area grows downwards starting at top of opt-args area.
|
|
|
|
// The opt-args, required-args, and register-save areas are empty except
|
|
|
|
// during calls and traps, so they are shifted downwards on each
|
|
|
|
// dynamic-size alloca.
|
|
|
|
pos = false;
|
2002-12-28 20:17:43 +00:00
|
|
|
unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
|
2002-09-16 15:39:26 +00:00
|
|
|
if (int extra = optArgsSize % getStackFrameSizeAlignment())
|
|
|
|
optArgsSize += (getStackFrameSizeAlignment() - extra);
|
2001-11-12 23:26:35 +00:00
|
|
|
int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
|
2002-03-18 03:08:07 +00:00
|
|
|
assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
|
2001-11-12 23:26:35 +00:00
|
|
|
return offset;
|
2001-11-08 04:55:13 +00:00
|
|
|
}
|
2001-09-15 00:30:44 +00:00
|
|
|
|
2001-09-14 03:47:57 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// class UltraSparcMachine
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
// Primary interface to machine description for the UltraSPARC.
|
|
|
|
// Primarily just initializes machine-dependent parameters in
|
|
|
|
// class TargetMachine, and creates machine-dependent subclasses
|
2003-01-14 22:00:31 +00:00
|
|
|
// for classes such as TargetInstrInfo.
|
2001-09-14 03:47:57 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2001-09-18 13:01:29 +00:00
|
|
|
UltraSparc::UltraSparc()
|
2003-04-26 20:11:09 +00:00
|
|
|
: TargetMachine("UltraSparc-Native", false),
|
2001-11-08 04:55:13 +00:00
|
|
|
schedInfo(*this),
|
|
|
|
regInfo(*this),
|
2001-11-09 02:16:04 +00:00
|
|
|
frameInfo(*this),
|
2002-09-20 00:52:09 +00:00
|
|
|
cacheInfo(*this),
|
2002-10-29 21:48:17 +00:00
|
|
|
optInfo(*this) {
|
2001-09-14 03:47:57 +00:00
|
|
|
}
|
|
|
|
|
2002-10-28 01:03:43 +00:00
|
|
|
|
|
|
|
// addPassesToEmitAssembly - This method controls the entire code generation
|
|
|
|
// process for the ultra sparc.
|
|
|
|
//
|
2002-10-29 21:12:46 +00:00
|
|
|
bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
|
2002-10-28 01:03:43 +00:00
|
|
|
{
|
2003-04-23 16:24:55 +00:00
|
|
|
// FIXME: implement the switch instruction in the instruction selector.
|
|
|
|
PM.add(createLowerSwitchPass());
|
|
|
|
|
2002-10-28 01:03:43 +00:00
|
|
|
// Construct and initialize the MachineFunction object for this fn.
|
2002-10-28 01:12:41 +00:00
|
|
|
PM.add(createMachineCodeConstructionPass(*this));
|
2002-10-28 01:03:43 +00:00
|
|
|
|
|
|
|
//Insert empty stackslots in the stack frame of each function
|
|
|
|
//so %fp+offset-8 and %fp+offset-16 are empty slots now!
|
|
|
|
PM.add(createStackSlotsPass(*this));
|
|
|
|
|
|
|
|
// Specialize LLVM code for this target machine and then
|
|
|
|
// run basic dataflow optimizations on LLVM code.
|
2003-05-27 22:24:48 +00:00
|
|
|
if (!DisablePreSelect) {
|
|
|
|
PM.add(createPreSelectionPass(*this));
|
|
|
|
PM.add(createReassociatePass());
|
|
|
|
PM.add(createLICMPass());
|
|
|
|
PM.add(createGCSEPass());
|
|
|
|
}
|
2002-10-28 01:03:43 +00:00
|
|
|
|
|
|
|
PM.add(createInstructionSelectionPass(*this));
|
|
|
|
|
|
|
|
if (!DisableSched)
|
|
|
|
PM.add(createInstructionSchedulingWithSSAPass(*this));
|
|
|
|
|
|
|
|
PM.add(getRegisterAllocator(*this));
|
|
|
|
|
|
|
|
PM.add(getPrologEpilogInsertionPass());
|
|
|
|
|
|
|
|
if (!DisablePeephole)
|
|
|
|
PM.add(createPeepholeOptsPass(*this));
|
|
|
|
|
2003-06-04 22:07:12 +00:00
|
|
|
PM.add(getMappingInfoCollector(Out));
|
2002-10-28 01:03:43 +00:00
|
|
|
|
|
|
|
// Output assembly language to the .s file. Assembly emission is split into
|
|
|
|
// two parts: Function output and Global value output. This is because
|
|
|
|
// function output is pipelined with all of the rest of code generation stuff,
|
|
|
|
// allowing machine code representations for functions to be free'd after the
|
|
|
|
// function has been emitted.
|
|
|
|
//
|
|
|
|
PM.add(getFunctionAsmPrinterPass(Out));
|
2002-10-28 01:12:41 +00:00
|
|
|
PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
|
2002-10-28 01:03:43 +00:00
|
|
|
|
|
|
|
// Emit Module level assembly after all of the functions have been processed.
|
|
|
|
PM.add(getModuleAsmPrinterPass(Out));
|
|
|
|
|
|
|
|
// Emit bytecode to the assembly file into its special section next
|
|
|
|
PM.add(getEmitBytecodeToAsmPass(Out));
|
|
|
|
PM.add(getFunctionInfo(Out));
|
2002-10-29 21:12:46 +00:00
|
|
|
return false;
|
2002-10-28 01:03:43 +00:00
|
|
|
}
|
2003-05-27 22:24:48 +00:00
|
|
|
|
|
|
|
// addPassesToJITCompile - This method controls the JIT method of code
|
|
|
|
// generation for the UltraSparc.
|
|
|
|
//
|
|
|
|
bool UltraSparc::addPassesToJITCompile(PassManager &PM) {
|
2003-06-06 07:11:16 +00:00
|
|
|
const TargetData &TD = getTargetData();
|
|
|
|
|
|
|
|
PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
|
|
|
|
TD.getPointerAlignment(), TD.getDoubleAlignment()));
|
|
|
|
|
|
|
|
// Replace malloc and free instructions with library calls.
|
|
|
|
// Do this after tracing until lli implements these lib calls.
|
|
|
|
// For now, it will emulate malloc and free internally.
|
|
|
|
PM.add(createLowerAllocationsPass());
|
|
|
|
|
2003-05-27 22:24:48 +00:00
|
|
|
// FIXME: implement the switch instruction in the instruction selector.
|
|
|
|
PM.add(createLowerSwitchPass());
|
|
|
|
|
|
|
|
// Construct and initialize the MachineFunction object for this fn.
|
|
|
|
PM.add(createMachineCodeConstructionPass(*this));
|
|
|
|
|
|
|
|
PM.add(createInstructionSelectionPass(*this));
|
|
|
|
|
|
|
|
// new pass: convert Value* in MachineOperand to an unsigned register
|
|
|
|
// this brings it in line with what the X86 JIT's RegisterAllocator expects
|
|
|
|
//PM.add(createAddRegNumToValuesPass());
|
|
|
|
|
|
|
|
PM.add(getRegisterAllocator(*this));
|
|
|
|
PM.add(getPrologEpilogInsertionPass());
|
|
|
|
|
2003-05-30 20:00:13 +00:00
|
|
|
if (!DisablePeephole)
|
|
|
|
PM.add(createPeepholeOptsPass(*this));
|
|
|
|
|
2003-05-27 22:24:48 +00:00
|
|
|
return false; // success!
|
|
|
|
}
|