2008-05-13 09:02:57 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the interfaces that PIC16 uses to lower LLVM code into a
|
|
|
|
// selection DAG.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "pic16-lower"
|
|
|
|
|
|
|
|
#include "PIC16ISelLowering.h"
|
|
|
|
#include "PIC16TargetMachine.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2008-11-19 11:00:54 +00:00
|
|
|
#include "llvm/GlobalValue.h"
|
2008-05-13 09:02:57 +00:00
|
|
|
#include "llvm/Function.h"
|
2009-01-13 19:18:47 +00:00
|
|
|
#include "llvm/CallingConv.h"
|
2008-05-13 09:02:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-01-13 19:18:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2008-05-13 09:02:57 +00:00
|
|
|
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-05-11 08:52:04 +00:00
|
|
|
static const char *getIntrinsicName(unsigned opcode) {
|
|
|
|
std::string Basename;
|
|
|
|
switch(opcode) {
|
|
|
|
default: assert (0 && "do not know intrinsic name");
|
|
|
|
case PIC16ISD::SRA_I8: Basename = "sra.i8"; break;
|
|
|
|
case RTLIB::SRA_I16: Basename = "sra.i16"; break;
|
|
|
|
case RTLIB::SRA_I32: Basename = "sra.i32"; break;
|
|
|
|
|
|
|
|
case PIC16ISD::SLL_I8: Basename = "sll.i8"; break;
|
|
|
|
case RTLIB::SHL_I16: Basename = "sll.i16"; break;
|
|
|
|
case RTLIB::SHL_I32: Basename = "sll.i32"; break;
|
|
|
|
|
|
|
|
case PIC16ISD::SRL_I8: Basename = "srl.i8"; break;
|
|
|
|
case RTLIB::SRL_I16: Basename = "srl.i16"; break;
|
|
|
|
case RTLIB::SRL_I32: Basename = "srl.i32"; break;
|
|
|
|
|
|
|
|
case PIC16ISD::MUL_I8: Basename = "mul.i8"; break;
|
|
|
|
case RTLIB::MUL_I16: Basename = "mul.i16"; break;
|
|
|
|
case RTLIB::MUL_I32: Basename = "mul.i32"; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string prefix = PAN::getTagName(PAN::PREFIX_SYMBOL);
|
|
|
|
std::string tagname = PAN::getTagName(PAN::LIBCALL);
|
|
|
|
std::string Fullname = prefix + tagname + Basename;
|
|
|
|
|
|
|
|
// The name has to live through program life.
|
|
|
|
char *tmp = new char[Fullname.size() + 1];
|
|
|
|
strcpy (tmp, Fullname.c_str());
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// PIC16TargetLowering Constructor.
|
|
|
|
PIC16TargetLowering::PIC16TargetLowering(PIC16TargetMachine &TM)
|
2009-04-06 10:54:50 +00:00
|
|
|
: TargetLowering(TM), TmpSize(0) {
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
Subtarget = &TM.getSubtarget<PIC16Subtarget>();
|
|
|
|
|
|
|
|
addRegisterClass(MVT::i8, PIC16::GPRRegisterClass);
|
|
|
|
|
|
|
|
setShiftAmountType(MVT::i8);
|
|
|
|
setShiftAmountFlavor(Extend);
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
// SRA library call names
|
2009-05-11 08:52:04 +00:00
|
|
|
setPIC16LibcallName(PIC16ISD::SRA_I8, getIntrinsicName(PIC16ISD::SRA_I8));
|
|
|
|
setLibcallName(RTLIB::SRA_I16, getIntrinsicName(RTLIB::SRA_I16));
|
|
|
|
setLibcallName(RTLIB::SRA_I32, getIntrinsicName(RTLIB::SRA_I32));
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-01-21 05:44:05 +00:00
|
|
|
// SHL library call names
|
2009-05-11 08:52:04 +00:00
|
|
|
setPIC16LibcallName(PIC16ISD::SLL_I8, getIntrinsicName(PIC16ISD::SLL_I8));
|
|
|
|
setLibcallName(RTLIB::SHL_I16, getIntrinsicName(RTLIB::SHL_I16));
|
|
|
|
setLibcallName(RTLIB::SHL_I32, getIntrinsicName(RTLIB::SHL_I32));
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// SRL library call names
|
2009-05-11 08:52:04 +00:00
|
|
|
setPIC16LibcallName(PIC16ISD::SRL_I8, getIntrinsicName(PIC16ISD::SRL_I8));
|
|
|
|
setLibcallName(RTLIB::SRL_I16, getIntrinsicName(RTLIB::SRL_I16));
|
|
|
|
setLibcallName(RTLIB::SRL_I32, getIntrinsicName(RTLIB::SRL_I32));
|
2009-01-21 05:44:05 +00:00
|
|
|
|
|
|
|
// MUL Library call names
|
2009-05-11 08:52:04 +00:00
|
|
|
setPIC16LibcallName(PIC16ISD::MUL_I8, getIntrinsicName(PIC16ISD::MUL_I8));
|
|
|
|
setLibcallName(RTLIB::MUL_I16, getIntrinsicName(RTLIB::MUL_I16));
|
|
|
|
setLibcallName(RTLIB::MUL_I32, getIntrinsicName(RTLIB::MUL_I32));
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
|
2009-01-21 05:44:05 +00:00
|
|
|
setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
setOperationAction(ISD::LOAD, MVT::i8, Legal);
|
|
|
|
setOperationAction(ISD::LOAD, MVT::i16, Custom);
|
|
|
|
setOperationAction(ISD::LOAD, MVT::i32, Custom);
|
|
|
|
|
|
|
|
setOperationAction(ISD::STORE, MVT::i8, Legal);
|
|
|
|
setOperationAction(ISD::STORE, MVT::i16, Custom);
|
|
|
|
setOperationAction(ISD::STORE, MVT::i32, Custom);
|
|
|
|
|
|
|
|
setOperationAction(ISD::ADDE, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::ADDC, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::SUBE, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::SUBC, MVT::i8, Custom);
|
2009-06-04 08:52:28 +00:00
|
|
|
setOperationAction(ISD::SUB, MVT::i8, Custom);
|
2009-01-21 05:44:05 +00:00
|
|
|
setOperationAction(ISD::ADD, MVT::i8, Custom);
|
2008-11-19 11:00:54 +00:00
|
|
|
setOperationAction(ISD::ADD, MVT::i16, Custom);
|
|
|
|
|
2008-11-26 10:53:50 +00:00
|
|
|
setOperationAction(ISD::OR, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::AND, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::XOR, MVT::i8, Custom);
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
setOperationAction(ISD::FrameIndex, MVT::i16, Custom);
|
|
|
|
setOperationAction(ISD::CALL, MVT::i16, Custom);
|
|
|
|
setOperationAction(ISD::RET, MVT::Other, Custom);
|
|
|
|
|
2009-01-21 05:44:05 +00:00
|
|
|
setOperationAction(ISD::MUL, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::MUL, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::MUL, MVT::i32, Expand);
|
|
|
|
|
|
|
|
setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::MULHU, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::MULHU, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::MULHU, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::MULHS, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::MULHS, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::MULHS, MVT::i32, Expand);
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
setOperationAction(ISD::SRA, MVT::i8, Custom);
|
2009-01-21 05:44:05 +00:00
|
|
|
setOperationAction(ISD::SRA, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SRA, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::SHL, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::SHL, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SHL, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::SRL, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::SRL, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SRL, MVT::i32, Expand);
|
|
|
|
|
|
|
|
// PIC16 does not support shift parts
|
|
|
|
setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
|
|
|
|
setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
|
|
|
|
setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
// PIC16 does not have a SETCC, expand it to SELECT_CC.
|
|
|
|
setOperationAction(ISD::SETCC, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::SELECT, MVT::i8, Expand);
|
|
|
|
setOperationAction(ISD::BRCOND, MVT::Other, Expand);
|
|
|
|
setOperationAction(ISD::BRIND, MVT::Other, Expand);
|
|
|
|
|
|
|
|
setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
|
|
|
|
setOperationAction(ISD::BR_CC, MVT::i8, Custom);
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
//setOperationAction(ISD::TRUNCATE, MVT::i16, Custom);
|
|
|
|
setTruncStoreAction(MVT::i16, MVT::i8, Custom);
|
|
|
|
|
|
|
|
// Now deduce the information based on the above mentioned
|
|
|
|
// actions
|
2008-05-13 09:02:57 +00:00
|
|
|
computeRegisterProperties();
|
|
|
|
}
|
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// getOutFlag - Extract the flag result if the Op has it.
|
|
|
|
static SDValue getOutFlag(SDValue &Op) {
|
|
|
|
// Flag is the last value of the node.
|
|
|
|
SDValue Flag = Op.getValue(Op.getNode()->getNumValues() - 1);
|
|
|
|
|
|
|
|
assert (Flag.getValueType() == MVT::Flag
|
|
|
|
&& "Node does not have an out Flag");
|
|
|
|
|
|
|
|
return Flag;
|
|
|
|
}
|
2009-04-06 10:54:50 +00:00
|
|
|
// Get the TmpOffset for FrameIndex
|
2009-04-10 15:10:14 +00:00
|
|
|
unsigned PIC16TargetLowering::GetTmpOffsetForFI(unsigned FI, unsigned size) {
|
2009-04-06 10:54:50 +00:00
|
|
|
std::map<unsigned, unsigned>::iterator
|
|
|
|
MapIt = FiTmpOffsetMap.find(FI);
|
|
|
|
if (MapIt != FiTmpOffsetMap.end())
|
|
|
|
return MapIt->second;
|
|
|
|
|
|
|
|
// This FI (FrameIndex) is not yet mapped, so map it
|
|
|
|
FiTmpOffsetMap[FI] = TmpSize;
|
2009-04-10 15:10:14 +00:00
|
|
|
TmpSize += size;
|
|
|
|
return FiTmpOffsetMap[FI];
|
2009-04-06 10:54:50 +00:00
|
|
|
}
|
2009-03-20 13:42:20 +00:00
|
|
|
|
|
|
|
// To extract chain value from the SDValue Nodes
|
|
|
|
// This function will help to maintain the chain extracting
|
|
|
|
// code at one place. In case of any change in future it will
|
|
|
|
// help maintain the code.
|
|
|
|
static SDValue getChain(SDValue &Op) {
|
|
|
|
SDValue Chain = Op.getValue(Op.getNode()->getNumValues() - 1);
|
|
|
|
|
|
|
|
// If the last value returned in Flag then the chain is
|
|
|
|
// second last value returned.
|
|
|
|
if (Chain.getValueType() == MVT::Flag)
|
|
|
|
Chain = Op.getValue(Op.getNode()->getNumValues() - 2);
|
|
|
|
|
|
|
|
// All nodes may not produce a chain. Therefore following assert
|
|
|
|
// verifies that the node is returning a chain only.
|
|
|
|
assert (Chain.getValueType() == MVT::Other
|
|
|
|
&& "Node does not have a chain");
|
|
|
|
|
|
|
|
return Chain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PopulateResults - Helper function to LowerOperation.
|
|
|
|
/// If a node wants to return multiple results after lowering,
|
|
|
|
/// it stuffs them into an array of SDValue called Results.
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
static void PopulateResults(SDValue N, SmallVectorImpl<SDValue>&Results) {
|
|
|
|
if (N.getOpcode() == ISD::MERGE_VALUES) {
|
|
|
|
int NumResults = N.getNumOperands();
|
|
|
|
for( int i = 0; i < NumResults; i++)
|
|
|
|
Results.push_back(N.getOperand(i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Results.push_back(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
MVT PIC16TargetLowering::getSetCCResultType(MVT ValType) const {
|
|
|
|
return MVT::i8;
|
|
|
|
}
|
|
|
|
|
2009-03-20 14:10:20 +00:00
|
|
|
/// The type legalizer framework of generating legalizer can generate libcalls
|
|
|
|
/// only when the operand/result types are illegal.
|
|
|
|
/// PIC16 needs to generate libcalls even for the legal types (i8) for some ops.
|
|
|
|
/// For example an arithmetic right shift. These functions are used to lower
|
|
|
|
/// such operations that generate libcall for legal types.
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
void
|
2009-01-21 05:44:05 +00:00
|
|
|
PIC16TargetLowering::setPIC16LibcallName(PIC16ISD::PIC16Libcall Call,
|
2009-01-13 19:18:47 +00:00
|
|
|
const char *Name) {
|
2009-03-20 14:10:20 +00:00
|
|
|
PIC16LibcallNames[Call] = Name;
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2009-01-21 05:44:05 +00:00
|
|
|
PIC16TargetLowering::getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) {
|
2009-03-20 14:10:20 +00:00
|
|
|
return PIC16LibcallNames[Call];
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SDValue
|
2009-01-21 05:44:05 +00:00
|
|
|
PIC16TargetLowering::MakePIC16Libcall(PIC16ISD::PIC16Libcall Call,
|
2009-01-13 19:18:47 +00:00
|
|
|
MVT RetVT, const SDValue *Ops,
|
|
|
|
unsigned NumOps, bool isSigned,
|
2009-01-30 23:10:59 +00:00
|
|
|
SelectionDAG &DAG, DebugLoc dl) {
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-03-20 14:10:20 +00:00
|
|
|
TargetLowering::ArgListTy Args;
|
|
|
|
Args.reserve(NumOps);
|
|
|
|
|
|
|
|
TargetLowering::ArgListEntry Entry;
|
|
|
|
for (unsigned i = 0; i != NumOps; ++i) {
|
|
|
|
Entry.Node = Ops[i];
|
|
|
|
Entry.Ty = Entry.Node.getValueType().getTypeForMVT();
|
|
|
|
Entry.isSExt = isSigned;
|
|
|
|
Entry.isZExt = !isSigned;
|
|
|
|
Args.push_back(Entry);
|
|
|
|
}
|
|
|
|
SDValue Callee = DAG.getExternalSymbol(getPIC16LibcallName(Call), MVT::i8);
|
|
|
|
|
|
|
|
const Type *RetTy = RetVT.getTypeForMVT();
|
|
|
|
std::pair<SDValue,SDValue> CallInfo =
|
2009-01-13 19:18:47 +00:00
|
|
|
LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false,
|
2009-03-20 14:10:20 +00:00
|
|
|
false, CallingConv::C, false, Callee, Args, DAG, dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
return CallInfo.first;
|
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
const char *PIC16TargetLowering::getTargetNodeName(unsigned Opcode) const {
|
|
|
|
switch (Opcode) {
|
|
|
|
default: return NULL;
|
|
|
|
case PIC16ISD::Lo: return "PIC16ISD::Lo";
|
|
|
|
case PIC16ISD::Hi: return "PIC16ISD::Hi";
|
|
|
|
case PIC16ISD::MTLO: return "PIC16ISD::MTLO";
|
|
|
|
case PIC16ISD::MTHI: return "PIC16ISD::MTHI";
|
2009-04-08 05:38:48 +00:00
|
|
|
case PIC16ISD::MTPCLATH: return "PIC16ISD::MTPCLATH";
|
|
|
|
case PIC16ISD::PIC16Connect: return "PIC16ISD::PIC16Connect";
|
2008-11-19 11:00:54 +00:00
|
|
|
case PIC16ISD::Banksel: return "PIC16ISD::Banksel";
|
|
|
|
case PIC16ISD::PIC16Load: return "PIC16ISD::PIC16Load";
|
2009-04-02 17:42:00 +00:00
|
|
|
case PIC16ISD::PIC16LdArg: return "PIC16ISD::PIC16LdArg";
|
2009-01-13 19:18:47 +00:00
|
|
|
case PIC16ISD::PIC16LdWF: return "PIC16ISD::PIC16LdWF";
|
2008-11-19 11:00:54 +00:00
|
|
|
case PIC16ISD::PIC16Store: return "PIC16ISD::PIC16Store";
|
2009-01-13 19:18:47 +00:00
|
|
|
case PIC16ISD::PIC16StWF: return "PIC16ISD::PIC16StWF";
|
2008-11-19 11:00:54 +00:00
|
|
|
case PIC16ISD::BCF: return "PIC16ISD::BCF";
|
|
|
|
case PIC16ISD::LSLF: return "PIC16ISD::LSLF";
|
|
|
|
case PIC16ISD::LRLF: return "PIC16ISD::LRLF";
|
|
|
|
case PIC16ISD::RLF: return "PIC16ISD::RLF";
|
|
|
|
case PIC16ISD::RRF: return "PIC16ISD::RRF";
|
2009-01-13 19:18:47 +00:00
|
|
|
case PIC16ISD::CALL: return "PIC16ISD::CALL";
|
2009-04-08 05:38:48 +00:00
|
|
|
case PIC16ISD::CALLW: return "PIC16ISD::CALLW";
|
2009-01-13 19:18:47 +00:00
|
|
|
case PIC16ISD::SUBCC: return "PIC16ISD::SUBCC";
|
|
|
|
case PIC16ISD::SELECT_ICC: return "PIC16ISD::SELECT_ICC";
|
|
|
|
case PIC16ISD::BRCOND: return "PIC16ISD::BRCOND";
|
2008-11-19 11:00:54 +00:00
|
|
|
case PIC16ISD::Dummy: return "PIC16ISD::Dummy";
|
|
|
|
}
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-12-01 11:39:25 +00:00
|
|
|
void PIC16TargetLowering::ReplaceNodeResults(SDNode *N,
|
|
|
|
SmallVectorImpl<SDValue>&Results,
|
|
|
|
SelectionDAG &DAG) {
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
switch (N->getOpcode()) {
|
|
|
|
case ISD::GlobalAddress:
|
2008-12-01 11:39:25 +00:00
|
|
|
Results.push_back(ExpandGlobalAddress(N, DAG));
|
|
|
|
return;
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::ExternalSymbol:
|
|
|
|
Results.push_back(ExpandExternalSymbol(N, DAG));
|
|
|
|
return;
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::STORE:
|
2008-12-01 11:39:25 +00:00
|
|
|
Results.push_back(ExpandStore(N, DAG));
|
|
|
|
return;
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::LOAD:
|
2009-01-13 19:18:47 +00:00
|
|
|
PopulateResults(ExpandLoad(N, DAG), Results);
|
2008-12-01 11:39:25 +00:00
|
|
|
return;
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::ADD:
|
2009-01-13 19:18:47 +00:00
|
|
|
// Results.push_back(ExpandAdd(N, DAG));
|
2008-12-01 11:39:25 +00:00
|
|
|
return;
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::FrameIndex:
|
|
|
|
Results.push_back(ExpandFrameIndex(N, DAG));
|
|
|
|
return;
|
2008-11-19 11:00:54 +00:00
|
|
|
default:
|
|
|
|
assert (0 && "not implemented");
|
2008-12-01 11:39:25 +00:00
|
|
|
return;
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue PIC16TargetLowering::ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) {
|
|
|
|
|
|
|
|
// Currently handling FrameIndex of size MVT::i16 only
|
|
|
|
// One example of this scenario is when return value is written on
|
|
|
|
// FrameIndex#0
|
|
|
|
|
|
|
|
if (N->getValueType(0) != MVT::i16)
|
|
|
|
return SDValue();
|
|
|
|
|
|
|
|
// Expand the FrameIndex into ExternalSymbol and a Constant node
|
|
|
|
// The constant will represent the frame index number
|
|
|
|
// Get the current function frame
|
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
const Function *Func = MF.getFunction();
|
|
|
|
const std::string Name = Func->getName();
|
2009-02-06 21:50:26 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(SDValue(N,0));
|
2009-02-06 21:50:26 +00:00
|
|
|
// FIXME there isn't really debug info here
|
|
|
|
DebugLoc dl = FR->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-04-14 02:49:52 +00:00
|
|
|
// Expand FrameIndex like GlobalAddress and ExternalSymbol
|
|
|
|
// Also use Offset field for lo and hi parts. The default
|
|
|
|
// offset is zero.
|
2009-06-03 15:31:12 +00:00
|
|
|
|
|
|
|
SDValue ES;
|
|
|
|
int FrameOffset;
|
|
|
|
SDValue FI = SDValue(N,0);
|
|
|
|
LegalizeFrameIndex(FI, DAG, ES, FrameOffset);
|
|
|
|
SDValue Offset = DAG.getConstant(FrameOffset, MVT::i8);
|
|
|
|
SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, ES, Offset);
|
|
|
|
SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, ES, Offset);
|
|
|
|
return DAG.getNode(ISD::BUILD_PAIR, dl, N->getValueType(0), Lo, Hi);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::ExpandStore(SDNode *N, SelectionDAG &DAG) {
|
2008-11-19 11:00:54 +00:00
|
|
|
StoreSDNode *St = cast<StoreSDNode>(N);
|
|
|
|
SDValue Chain = St->getChain();
|
|
|
|
SDValue Src = St->getValue();
|
|
|
|
SDValue Ptr = St->getBasePtr();
|
|
|
|
MVT ValueType = Src.getValueType();
|
|
|
|
unsigned StoreOffset = 0;
|
2009-02-06 21:50:26 +00:00
|
|
|
DebugLoc dl = N->getDebugLoc();
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
SDValue PtrLo, PtrHi;
|
2009-02-07 00:55:49 +00:00
|
|
|
LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, StoreOffset, dl);
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
if (ValueType == MVT::i8) {
|
2009-02-06 21:50:26 +00:00
|
|
|
return DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, Src,
|
2009-01-13 19:18:47 +00:00
|
|
|
PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (0 + StoreOffset, MVT::i8));
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
else if (ValueType == MVT::i16) {
|
|
|
|
// Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
|
|
|
|
SDValue SrcLo, SrcHi;
|
|
|
|
GetExpandedParts(Src, DAG, SrcLo, SrcHi);
|
|
|
|
SDValue ChainLo = Chain, ChainHi = Chain;
|
|
|
|
if (Chain.getOpcode() == ISD::TokenFactor) {
|
|
|
|
ChainLo = Chain.getOperand(0);
|
|
|
|
ChainHi = Chain.getOperand(1);
|
|
|
|
}
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
|
2008-11-19 11:00:54 +00:00
|
|
|
ChainLo,
|
|
|
|
SrcLo, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (0 + StoreOffset, MVT::i8));
|
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi,
|
2008-11-19 11:00:54 +00:00
|
|
|
SrcHi, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (1 + StoreOffset, MVT::i8));
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, getChain(Store1),
|
2008-12-01 11:39:25 +00:00
|
|
|
getChain(Store2));
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
else if (ValueType == MVT::i32) {
|
|
|
|
// Get the Lo and Hi parts from MERGE_VALUE or BUILD_PAIR.
|
|
|
|
SDValue SrcLo, SrcHi;
|
|
|
|
GetExpandedParts(Src, DAG, SrcLo, SrcHi);
|
|
|
|
|
|
|
|
// Get the expanded parts of each of SrcLo and SrcHi.
|
|
|
|
SDValue SrcLo1, SrcLo2, SrcHi1, SrcHi2;
|
|
|
|
GetExpandedParts(SrcLo, DAG, SrcLo1, SrcLo2);
|
|
|
|
GetExpandedParts(SrcHi, DAG, SrcHi1, SrcHi2);
|
|
|
|
|
|
|
|
SDValue ChainLo = Chain, ChainHi = Chain;
|
|
|
|
if (Chain.getOpcode() == ISD::TokenFactor) {
|
|
|
|
ChainLo = Chain.getOperand(0);
|
|
|
|
ChainHi = Chain.getOperand(1);
|
|
|
|
}
|
|
|
|
SDValue ChainLo1 = ChainLo, ChainLo2 = ChainLo, ChainHi1 = ChainHi,
|
|
|
|
ChainHi2 = ChainHi;
|
|
|
|
if (ChainLo.getOpcode() == ISD::TokenFactor) {
|
|
|
|
ChainLo1 = ChainLo.getOperand(0);
|
|
|
|
ChainLo2 = ChainLo.getOperand(1);
|
|
|
|
}
|
|
|
|
if (ChainHi.getOpcode() == ISD::TokenFactor) {
|
|
|
|
ChainHi1 = ChainHi.getOperand(0);
|
|
|
|
ChainHi2 = ChainHi.getOperand(1);
|
|
|
|
}
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store1 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other,
|
2008-11-19 11:00:54 +00:00
|
|
|
ChainLo1,
|
|
|
|
SrcLo1, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (0 + StoreOffset, MVT::i8));
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store2 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainLo2,
|
2008-11-19 11:00:54 +00:00
|
|
|
SrcLo2, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (1 + StoreOffset, MVT::i8));
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store3 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi1,
|
2008-11-19 11:00:54 +00:00
|
|
|
SrcHi1, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (2 + StoreOffset, MVT::i8));
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue Store4 = DAG.getNode(PIC16ISD::PIC16Store, dl, MVT::Other, ChainHi2,
|
2008-11-19 11:00:54 +00:00
|
|
|
SrcHi2, PtrLo, PtrHi,
|
|
|
|
DAG.getConstant (3 + StoreOffset, MVT::i8));
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
SDValue RetLo = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
|
|
|
getChain(Store1), getChain(Store2));
|
|
|
|
SDValue RetHi = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
|
|
|
getChain(Store3), getChain(Store4));
|
|
|
|
return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, RetLo, RetHi);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
assert (0 && "value type not supported");
|
2008-12-01 11:39:25 +00:00
|
|
|
return SDValue();
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue PIC16TargetLowering::ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG)
|
|
|
|
{
|
|
|
|
ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(SDValue(N, 0));
|
2009-02-06 21:50:26 +00:00
|
|
|
// FIXME there isn't really debug info here
|
|
|
|
DebugLoc dl = ES->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
SDValue TES = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
|
2009-04-14 02:49:52 +00:00
|
|
|
SDValue Offset = DAG.getConstant(0, MVT::i8);
|
|
|
|
SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TES, Offset);
|
|
|
|
SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TES, Offset);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// ExpandGlobalAddress -
|
2008-12-01 11:39:25 +00:00
|
|
|
SDValue PIC16TargetLowering::ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) {
|
2008-11-19 11:00:54 +00:00
|
|
|
GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(SDValue(N, 0));
|
2009-02-06 21:50:26 +00:00
|
|
|
// FIXME there isn't really debug info here
|
|
|
|
DebugLoc dl = G->getDebugLoc();
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
SDValue TGA = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i8,
|
|
|
|
G->getOffset());
|
|
|
|
|
2009-04-14 02:49:52 +00:00
|
|
|
SDValue Offset = DAG.getConstant(0, MVT::i8);
|
|
|
|
SDValue Lo = DAG.getNode(PIC16ISD::Lo, dl, MVT::i8, TGA, Offset);
|
|
|
|
SDValue Hi = DAG.getNode(PIC16ISD::Hi, dl, MVT::i8, TGA, Offset);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-02-06 21:50:26 +00:00
|
|
|
return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16, Lo, Hi);
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
bool PIC16TargetLowering::isDirectAddress(const SDValue &Op) {
|
|
|
|
assert (Op.getNode() != NULL && "Can't operate on NULL SDNode!!");
|
|
|
|
|
|
|
|
if (Op.getOpcode() == ISD::BUILD_PAIR) {
|
|
|
|
if (Op.getOperand(0).getOpcode() == PIC16ISD::Lo)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// Return true if DirectAddress is in ROM_SPACE
|
|
|
|
bool PIC16TargetLowering::isRomAddress(const SDValue &Op) {
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// RomAddress is a GlobalAddress in ROM_SPACE_
|
|
|
|
// If the Op is not a GlobalAddress return NULL without checking
|
|
|
|
// anything further.
|
|
|
|
if (!isDirectAddress(Op))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Its a GlobalAddress.
|
|
|
|
// It is BUILD_PAIR((PIC16Lo TGA), (PIC16Hi TGA)) and Op is BUILD_PAIR
|
|
|
|
SDValue TGA = Op.getOperand(0).getOperand(0);
|
|
|
|
GlobalAddressSDNode *GSDN = dyn_cast<GlobalAddressSDNode>(TGA);
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-04-29 04:43:24 +00:00
|
|
|
if (GSDN->getAddressSpace() == PIC16ISD::ROM_SPACE)
|
2008-11-19 11:00:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Any other address space return it false
|
|
|
|
return false;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// GetExpandedParts - This function is on the similiar lines as
|
|
|
|
// the GetExpandedInteger in type legalizer is. This returns expanded
|
|
|
|
// parts of Op in Lo and Hi.
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
void PIC16TargetLowering::GetExpandedParts(SDValue Op, SelectionDAG &DAG,
|
|
|
|
SDValue &Lo, SDValue &Hi) {
|
|
|
|
SDNode *N = Op.getNode();
|
2009-02-06 01:31:28 +00:00
|
|
|
DebugLoc dl = N->getDebugLoc();
|
2009-03-20 13:42:20 +00:00
|
|
|
MVT NewVT = getTypeToTransformTo(N->getValueType(0));
|
|
|
|
|
|
|
|
// Extract the lo component.
|
|
|
|
Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
|
|
|
|
DAG.getConstant(0, MVT::i8));
|
2009-05-30 01:09:53 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// extract the hi component
|
2009-03-20 13:42:20 +00:00
|
|
|
Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NewVT, Op,
|
|
|
|
DAG.getConstant(1, MVT::i8));
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Legalize FrameIndex into ExternalSymbol and offset.
|
|
|
|
void
|
|
|
|
PIC16TargetLowering::LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG,
|
|
|
|
SDValue &ES, int &Offset) {
|
|
|
|
|
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
const Function *Func = MF.getFunction();
|
2009-04-02 17:42:00 +00:00
|
|
|
MachineFrameInfo *MFI = MF.getFrameInfo();
|
2009-01-13 19:18:47 +00:00
|
|
|
const std::string Name = Func->getName();
|
|
|
|
|
|
|
|
FrameIndexSDNode *FR = dyn_cast<FrameIndexSDNode>(Op);
|
2009-04-02 17:42:00 +00:00
|
|
|
|
|
|
|
// FrameIndices are not stack offsets. But they represent the request
|
|
|
|
// for space on stack. That space requested may be more than one byte.
|
|
|
|
// Therefore, to calculate the stack offset that a FrameIndex aligns
|
|
|
|
// with, we need to traverse all the FrameIndices available earlier in
|
|
|
|
// the list and add their requested size.
|
|
|
|
unsigned FIndex = FR->getIndex();
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *tmpName;
|
2009-04-21 05:54:51 +00:00
|
|
|
if (FIndex < ReservedFrameCount) {
|
2009-05-10 05:23:47 +00:00
|
|
|
tmpName = createESName(PAN::getFrameLabel(Name));
|
2009-04-21 05:54:51 +00:00
|
|
|
ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
|
|
|
|
Offset = 0;
|
|
|
|
for (unsigned i=0; i<FIndex ; ++i) {
|
|
|
|
Offset += MFI->getObjectSize(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FrameIndex has been made for some temporary storage
|
2009-05-10 05:23:47 +00:00
|
|
|
tmpName = createESName(PAN::getTempdataLabel(Name));
|
2009-04-21 05:54:51 +00:00
|
|
|
ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
|
|
|
|
Offset = GetTmpOffsetForFI(FIndex, MFI->getObjectSize(FIndex));
|
2009-04-02 17:42:00 +00:00
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
return;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// This function legalizes the PIC16 Addresses. If the Pointer is
|
|
|
|
// -- Direct address variable residing
|
|
|
|
// --> then a Banksel for that variable will be created.
|
|
|
|
// -- Rom variable
|
|
|
|
// --> then it will be treated as an indirect address.
|
|
|
|
// -- Indirect address
|
|
|
|
// --> then the address will be loaded into FSR
|
|
|
|
// -- ADD with constant operand
|
|
|
|
// --> then constant operand of ADD will be returned as Offset
|
|
|
|
// and non-constant operand of ADD will be treated as pointer.
|
|
|
|
// Returns the high and lo part of the address, and the offset(in case of ADD).
|
|
|
|
|
2009-04-06 10:54:50 +00:00
|
|
|
void PIC16TargetLowering::LegalizeAddress(SDValue Ptr, SelectionDAG &DAG,
|
|
|
|
SDValue &Lo, SDValue &Hi,
|
|
|
|
unsigned &Offset, DebugLoc dl) {
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
// Offset, by default, should be 0
|
|
|
|
Offset = 0;
|
|
|
|
|
|
|
|
// If the pointer is ADD with constant,
|
|
|
|
// return the constant value as the offset
|
|
|
|
if (Ptr.getOpcode() == ISD::ADD) {
|
|
|
|
SDValue OperLeft = Ptr.getOperand(0);
|
|
|
|
SDValue OperRight = Ptr.getOperand(1);
|
|
|
|
if (OperLeft.getOpcode() == ISD::Constant) {
|
|
|
|
Offset = dyn_cast<ConstantSDNode>(OperLeft)->getZExtValue();
|
|
|
|
Ptr = OperRight;
|
2009-01-13 19:18:47 +00:00
|
|
|
} else if (OperRight.getOpcode() == ISD::Constant) {
|
2008-11-19 11:00:54 +00:00
|
|
|
Offset = dyn_cast<ConstantSDNode>(OperRight)->getZExtValue();
|
2009-01-13 19:18:47 +00:00
|
|
|
Ptr = OperLeft;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
// If the pointer is Type i8 and an external symbol
|
|
|
|
// then treat it as direct address.
|
|
|
|
// One example for such case is storing and loading
|
|
|
|
// from function frame during a call
|
|
|
|
if (Ptr.getValueType() == MVT::i8) {
|
|
|
|
switch (Ptr.getOpcode()) {
|
|
|
|
case ISD::TargetExternalSymbol:
|
|
|
|
Lo = Ptr;
|
|
|
|
Hi = DAG.getConstant(1, MVT::i8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-14 02:49:52 +00:00
|
|
|
// Expansion of FrameIndex has Lo/Hi parts
|
|
|
|
if (isDirectAddress(Ptr)) {
|
|
|
|
SDValue TFI = Ptr.getOperand(0).getOperand(0);
|
2009-06-03 15:31:12 +00:00
|
|
|
int FrameOffset;
|
2009-04-14 02:49:52 +00:00
|
|
|
if (TFI.getOpcode() == ISD::TargetFrameIndex) {
|
|
|
|
LegalizeFrameIndex(TFI, DAG, Lo, FrameOffset);
|
|
|
|
Hi = DAG.getConstant(1, MVT::i8);
|
|
|
|
Offset += FrameOffset;
|
|
|
|
return;
|
2009-06-03 15:31:12 +00:00
|
|
|
} else if (TFI.getOpcode() == ISD::TargetExternalSymbol) {
|
|
|
|
// FrameIndex has already been expanded.
|
|
|
|
// Now just make use of its expansion
|
|
|
|
Lo = TFI;
|
|
|
|
Hi = DAG.getConstant(1, MVT::i8);
|
|
|
|
SDValue FOffset = Ptr.getOperand(0).getOperand(1);
|
|
|
|
assert (FOffset.getOpcode() == ISD::Constant &&
|
|
|
|
"Invalid operand of PIC16ISD::Lo");
|
|
|
|
Offset += dyn_cast<ConstantSDNode>(FOffset)->getZExtValue();
|
|
|
|
return;
|
2009-04-14 02:49:52 +00:00
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
if (isDirectAddress(Ptr) && !isRomAddress(Ptr)) {
|
|
|
|
// Direct addressing case for RAM variables. The Hi part is constant
|
|
|
|
// and the Lo part is the TGA itself.
|
|
|
|
Lo = Ptr.getOperand(0).getOperand(0);
|
|
|
|
|
|
|
|
// For direct addresses Hi is a constant. Value 1 for the constant
|
|
|
|
// signifies that banksel needs to generated for it. Value 0 for
|
|
|
|
// the constant signifies that banksel does not need to be generated
|
|
|
|
// for it. Mark it as 1 now and optimize later.
|
|
|
|
Hi = DAG.getConstant(1, MVT::i8);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// Indirect addresses. Get the hi and lo parts of ptr.
|
|
|
|
GetExpandedParts(Ptr, DAG, Lo, Hi);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// Put the hi and lo parts into FSR.
|
2009-02-07 00:55:49 +00:00
|
|
|
Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Lo);
|
|
|
|
Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Hi);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
return;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 11:39:25 +00:00
|
|
|
SDValue PIC16TargetLowering::ExpandLoad(SDNode *N, SelectionDAG &DAG) {
|
2008-11-19 11:00:54 +00:00
|
|
|
LoadSDNode *LD = dyn_cast<LoadSDNode>(SDValue(N, 0));
|
|
|
|
SDValue Chain = LD->getChain();
|
|
|
|
SDValue Ptr = LD->getBasePtr();
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = LD->getDebugLoc();
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
SDValue Load, Offset;
|
|
|
|
SDVTList Tys;
|
|
|
|
MVT VT, NewVT;
|
|
|
|
SDValue PtrLo, PtrHi;
|
|
|
|
unsigned LoadOffset;
|
|
|
|
|
|
|
|
// Legalize direct/indirect addresses. This will give the lo and hi parts
|
|
|
|
// of the address and the offset.
|
2009-02-07 00:55:49 +00:00
|
|
|
LegalizeAddress(Ptr, DAG, PtrLo, PtrHi, LoadOffset, dl);
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
// Load from the pointer (direct address or FSR)
|
|
|
|
VT = N->getValueType(0);
|
|
|
|
unsigned NumLoads = VT.getSizeInBits() / 8;
|
|
|
|
std::vector<SDValue> PICLoads;
|
|
|
|
unsigned iter;
|
|
|
|
MVT MemVT = LD->getMemoryVT();
|
|
|
|
if(ISD::isNON_EXTLoad(N)) {
|
|
|
|
for (iter=0; iter<NumLoads ; ++iter) {
|
|
|
|
// Add the pointer offset if any
|
|
|
|
Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
|
|
|
|
Tys = DAG.getVTList(MVT::i8, MVT::Other);
|
2009-02-05 01:01:16 +00:00
|
|
|
Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
|
2008-11-19 11:00:54 +00:00
|
|
|
Offset);
|
|
|
|
PICLoads.push_back(Load);
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
} else {
|
|
|
|
// If it is extended load then use PIC16Load for Memory Bytes
|
|
|
|
// and for all extended bytes perform action based on type of
|
|
|
|
// extention - i.e. SignExtendedLoad or ZeroExtendedLoad
|
|
|
|
|
|
|
|
|
|
|
|
// For extended loads this is the memory value type
|
|
|
|
// i.e. without any extension
|
|
|
|
MVT MemVT = LD->getMemoryVT();
|
|
|
|
unsigned MemBytes = MemVT.getSizeInBits() / 8;
|
|
|
|
unsigned ExtdBytes = VT.getSizeInBits() / 8;
|
|
|
|
Offset = DAG.getConstant(LoadOffset, MVT::i8);
|
|
|
|
|
|
|
|
Tys = DAG.getVTList(MVT::i8, MVT::Other);
|
|
|
|
// For MemBytes generate PIC16Load with proper offset
|
|
|
|
for (iter=0; iter<MemBytes; ++iter) {
|
|
|
|
// Add the pointer offset if any
|
|
|
|
Offset = DAG.getConstant(iter + LoadOffset, MVT::i8);
|
2009-02-05 01:01:16 +00:00
|
|
|
Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Chain, PtrLo, PtrHi,
|
2008-11-19 11:00:54 +00:00
|
|
|
Offset);
|
|
|
|
PICLoads.push_back(Load);
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
// For SignExtendedLoad
|
|
|
|
if (ISD::isSEXTLoad(N)) {
|
|
|
|
// For all ExtdBytes use the Right Shifted(Arithmetic) Value of the
|
|
|
|
// highest MemByte
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue SRA = DAG.getNode(ISD::SRA, dl, MVT::i8, Load,
|
2008-11-19 11:00:54 +00:00
|
|
|
DAG.getConstant(7, MVT::i8));
|
|
|
|
for (iter=MemBytes; iter<ExtdBytes; ++iter) {
|
|
|
|
PICLoads.push_back(SRA);
|
|
|
|
}
|
2009-06-03 15:31:12 +00:00
|
|
|
} else if (ISD::isZEXTLoad(N) || ISD::isEXTLoad(N)) {
|
|
|
|
//} else if (ISD::isZEXTLoad(N)) {
|
2008-11-19 11:00:54 +00:00
|
|
|
// ZeroExtendedLoad -- For all ExtdBytes use constant 0
|
|
|
|
SDValue ConstZero = DAG.getConstant(0, MVT::i8);
|
|
|
|
for (iter=MemBytes; iter<ExtdBytes; ++iter) {
|
|
|
|
PICLoads.push_back(ConstZero);
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
SDValue BP;
|
|
|
|
|
|
|
|
if (VT == MVT::i8) {
|
|
|
|
// Operand of Load is illegal -- Load itself is legal
|
2008-12-01 11:39:25 +00:00
|
|
|
return PICLoads[0];
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
else if (VT == MVT::i16) {
|
2009-02-05 01:01:16 +00:00
|
|
|
BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, PICLoads[0], PICLoads[1]);
|
2008-11-19 11:00:54 +00:00
|
|
|
if (MemVT == MVT::i8)
|
|
|
|
Chain = getChain(PICLoads[0]);
|
|
|
|
else
|
2009-02-05 01:01:16 +00:00
|
|
|
Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
|
|
|
getChain(PICLoads[0]), getChain(PICLoads[1]));
|
2008-11-19 11:00:54 +00:00
|
|
|
} else if (VT == MVT::i32) {
|
|
|
|
SDValue BPs[2];
|
2009-02-05 01:01:16 +00:00
|
|
|
BPs[0] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
|
|
|
|
PICLoads[0], PICLoads[1]);
|
|
|
|
BPs[1] = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i16,
|
|
|
|
PICLoads[2], PICLoads[3]);
|
|
|
|
BP = DAG.getNode(ISD::BUILD_PAIR, dl, VT, BPs[0], BPs[1]);
|
2008-11-19 11:00:54 +00:00
|
|
|
if (MemVT == MVT::i8)
|
|
|
|
Chain = getChain(PICLoads[0]);
|
|
|
|
else if (MemVT == MVT::i16)
|
2009-02-05 01:01:16 +00:00
|
|
|
Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
|
|
|
getChain(PICLoads[0]), getChain(PICLoads[1]));
|
2008-05-13 09:02:57 +00:00
|
|
|
else {
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue Chains[2];
|
2009-02-05 01:01:16 +00:00
|
|
|
Chains[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
2008-11-19 11:00:54 +00:00
|
|
|
getChain(PICLoads[0]), getChain(PICLoads[1]));
|
2009-02-05 01:01:16 +00:00
|
|
|
Chains[1] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
2008-11-19 11:00:54 +00:00
|
|
|
getChain(PICLoads[2]), getChain(PICLoads[3]));
|
2009-02-05 01:01:16 +00:00
|
|
|
Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
|
|
|
|
Chains[0], Chains[1]);
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
Tys = DAG.getVTList(VT, MVT::Other);
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, BP, Chain);
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-01-21 05:44:05 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
|
|
|
|
// We should have handled larger operands in type legalizer itself.
|
|
|
|
assert (Op.getValueType() == MVT::i8 && "illegal shift to lower");
|
|
|
|
|
|
|
|
SDNode *N = Op.getNode();
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue Value = N->getOperand(0);
|
|
|
|
SDValue Amt = N->getOperand(1);
|
2009-01-21 05:44:05 +00:00
|
|
|
PIC16ISD::PIC16Libcall CallCode;
|
|
|
|
switch (N->getOpcode()) {
|
|
|
|
case ISD::SRA:
|
|
|
|
CallCode = PIC16ISD::SRA_I8;
|
|
|
|
break;
|
|
|
|
case ISD::SHL:
|
|
|
|
CallCode = PIC16ISD::SLL_I8;
|
|
|
|
break;
|
|
|
|
case ISD::SRL:
|
|
|
|
CallCode = PIC16ISD::SRL_I8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert ( 0 && "This shift is not implemented yet.");
|
2009-01-13 19:18:47 +00:00
|
|
|
return SDValue();
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
SmallVector<SDValue, 2> Ops(2);
|
|
|
|
Ops[0] = Value;
|
|
|
|
Ops[1] = Amt;
|
2009-01-30 23:10:59 +00:00
|
|
|
SDValue Call = MakePIC16Libcall(CallCode, N->getValueType(0), &Ops[0], 2,
|
|
|
|
true, DAG, N->getDebugLoc());
|
2009-01-13 19:18:47 +00:00
|
|
|
return Call;
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
|
2009-01-21 09:00:29 +00:00
|
|
|
void
|
|
|
|
PIC16TargetLowering::LowerOperationWrapper(SDNode *N,
|
|
|
|
SmallVectorImpl<SDValue>&Results,
|
|
|
|
SelectionDAG &DAG) {
|
|
|
|
SDValue Op = SDValue(N, 0);
|
2009-01-21 05:44:05 +00:00
|
|
|
SDValue Res;
|
|
|
|
unsigned i;
|
|
|
|
switch (Op.getOpcode()) {
|
|
|
|
case ISD::FORMAL_ARGUMENTS:
|
|
|
|
Res = LowerFORMAL_ARGUMENTS(Op, DAG); break;
|
|
|
|
case ISD::LOAD:
|
|
|
|
Res = ExpandLoad(Op.getNode(), DAG); break;
|
|
|
|
case ISD::CALL:
|
|
|
|
Res = LowerCALL(Op, DAG); break;
|
|
|
|
default: {
|
|
|
|
// All other operations are handled in LowerOperation.
|
|
|
|
Res = LowerOperation(Op, DAG);
|
|
|
|
if (Res.getNode())
|
|
|
|
Results.push_back(Res);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-01-21 09:00:29 +00:00
|
|
|
|
|
|
|
N = Res.getNode();
|
2009-01-21 05:44:05 +00:00
|
|
|
unsigned NumValues = N->getNumValues();
|
2009-01-21 09:00:29 +00:00
|
|
|
for (i = 0; i < NumValues ; i++) {
|
2009-01-21 05:44:05 +00:00
|
|
|
Results.push_back(SDValue(N, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
|
|
|
|
switch (Op.getOpcode()) {
|
|
|
|
case ISD::FORMAL_ARGUMENTS:
|
|
|
|
return LowerFORMAL_ARGUMENTS(Op, DAG);
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::ADD:
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::ADDC:
|
|
|
|
case ISD::ADDE:
|
2009-01-13 19:18:47 +00:00
|
|
|
return LowerADD(Op, DAG);
|
|
|
|
case ISD::SUB:
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::SUBC:
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::SUBE:
|
|
|
|
return LowerSUB(Op, DAG);
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::LOAD:
|
2008-12-01 11:39:25 +00:00
|
|
|
return ExpandLoad(Op.getNode(), DAG);
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::STORE:
|
2008-12-01 11:39:25 +00:00
|
|
|
return ExpandStore(Op.getNode(), DAG);
|
2008-11-19 11:00:54 +00:00
|
|
|
case ISD::SHL:
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::SRA:
|
|
|
|
case ISD::SRL:
|
2009-01-21 05:44:05 +00:00
|
|
|
return LowerShift(Op, DAG);
|
2008-11-26 10:53:50 +00:00
|
|
|
case ISD::OR:
|
|
|
|
case ISD::AND:
|
|
|
|
case ISD::XOR:
|
|
|
|
return LowerBinOp(Op, DAG);
|
2009-01-13 19:18:47 +00:00
|
|
|
case ISD::CALL:
|
|
|
|
return LowerCALL(Op, DAG);
|
|
|
|
case ISD::RET:
|
|
|
|
return LowerRET(Op, DAG);
|
|
|
|
case ISD::BR_CC:
|
|
|
|
return LowerBR_CC(Op, DAG);
|
|
|
|
case ISD::SELECT_CC:
|
|
|
|
return LowerSELECT_CC(Op, DAG);
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
|
|
|
return SDValue();
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue PIC16TargetLowering::ConvertToMemOperand(SDValue Op,
|
2009-02-05 01:01:16 +00:00
|
|
|
SelectionDAG &DAG,
|
|
|
|
DebugLoc dl) {
|
2008-11-19 11:00:54 +00:00
|
|
|
assert (Op.getValueType() == MVT::i8
|
|
|
|
&& "illegal value type to store on stack.");
|
|
|
|
|
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
const Function *Func = MF.getFunction();
|
|
|
|
const std::string FuncName = Func->getName();
|
|
|
|
|
|
|
|
|
|
|
|
// Put the value on stack.
|
|
|
|
// Get a stack slot index and convert to es.
|
|
|
|
int FI = MF.getFrameInfo()->CreateStackObject(1, 1);
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *tmpName = createESName(PAN::getTempdataLabel(FuncName));
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
|
|
|
|
|
|
|
|
// Store the value to ES.
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue Store = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other,
|
2008-11-19 11:00:54 +00:00
|
|
|
DAG.getEntryNode(),
|
|
|
|
Op, ES,
|
|
|
|
DAG.getConstant (1, MVT::i8), // Banksel.
|
2009-04-10 15:10:14 +00:00
|
|
|
DAG.getConstant (GetTmpOffsetForFI(FI, 1),
|
2009-04-06 10:54:50 +00:00
|
|
|
MVT::i8));
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
// Load the value from ES.
|
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other);
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue Load = DAG.getNode(PIC16ISD::PIC16Load, dl, Tys, Store,
|
2008-11-19 11:00:54 +00:00
|
|
|
ES, DAG.getConstant (1, MVT::i8),
|
2009-04-10 15:10:14 +00:00
|
|
|
DAG.getConstant (GetTmpOffsetForFI(FI, 1),
|
2009-04-06 10:54:50 +00:00
|
|
|
MVT::i8));
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
return Load.getValue(0);
|
|
|
|
}
|
2009-04-08 05:38:48 +00:00
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::
|
|
|
|
LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
|
|
|
|
SDValue DataAddr_Lo, SDValue DataAddr_Hi,
|
|
|
|
SelectionDAG &DAG) {
|
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
|
|
|
unsigned NumOps = TheCall->getNumArgs();
|
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
|
|
|
|
|
|
|
// If call has no arguments then do nothing and return.
|
|
|
|
if (NumOps == 0)
|
|
|
|
return Chain;
|
|
|
|
|
|
|
|
std::vector<SDValue> Ops;
|
|
|
|
SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
|
|
|
|
SDValue Arg, StoreRet;
|
2009-04-09 10:29:32 +00:00
|
|
|
|
|
|
|
// For PIC16 ABI the arguments come after the return value.
|
|
|
|
unsigned RetVals = TheCall->getNumRetVals();
|
|
|
|
for (unsigned i = 0, ArgOffset = RetVals; i < NumOps; i++) {
|
2009-04-08 05:38:48 +00:00
|
|
|
// Get the arguments
|
|
|
|
Arg = TheCall->getArg(i);
|
2009-04-21 05:54:51 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
Ops.clear();
|
|
|
|
Ops.push_back(Chain);
|
|
|
|
Ops.push_back(Arg);
|
|
|
|
Ops.push_back(DataAddr_Lo);
|
|
|
|
Ops.push_back(DataAddr_Hi);
|
2009-04-09 10:29:32 +00:00
|
|
|
Ops.push_back(DAG.getConstant(ArgOffset, MVT::i8));
|
2009-04-08 05:38:48 +00:00
|
|
|
Ops.push_back(InFlag);
|
|
|
|
|
|
|
|
StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
|
|
|
|
|
|
|
|
Chain = getChain(StoreRet);
|
|
|
|
InFlag = getOutFlag(StoreRet);
|
2009-04-09 10:29:32 +00:00
|
|
|
ArgOffset++;
|
2009-04-08 05:38:48 +00:00
|
|
|
}
|
|
|
|
return Chain;
|
|
|
|
}
|
2009-04-14 02:49:52 +00:00
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::
|
|
|
|
LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue ArgLabel,
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue InFlag, SelectionDAG &DAG) {
|
2009-01-13 19:18:47 +00:00
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
|
|
|
unsigned NumOps = TheCall->getNumArgs();
|
2009-02-06 23:05:02 +00:00
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
std::string Name;
|
|
|
|
SDValue Arg, StoreAt;
|
|
|
|
MVT ArgVT;
|
|
|
|
unsigned Size=0;
|
|
|
|
unsigned ArgCount=0;
|
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// If call has no arguments then do nothing and return.
|
|
|
|
if (NumOps == 0)
|
|
|
|
return Chain;
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// FIXME: This portion of code currently assumes only
|
|
|
|
// primitive types being passed as arguments.
|
|
|
|
|
|
|
|
// Legalize the address before use
|
|
|
|
SDValue PtrLo, PtrHi;
|
|
|
|
unsigned AddressOffset;
|
|
|
|
int StoreOffset = 0;
|
2009-04-14 02:49:52 +00:00
|
|
|
LegalizeAddress(ArgLabel, DAG, PtrLo, PtrHi, AddressOffset, dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue StoreRet;
|
|
|
|
|
|
|
|
std::vector<SDValue> Ops;
|
|
|
|
SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
|
|
|
|
for (unsigned i=ArgCount, Offset = 0; i<NumOps; i++) {
|
|
|
|
// Get the argument
|
|
|
|
Arg = TheCall->getArg(i);
|
|
|
|
StoreOffset = (Offset + AddressOffset);
|
|
|
|
|
|
|
|
// Store the argument on frame
|
|
|
|
|
|
|
|
Ops.clear();
|
|
|
|
Ops.push_back(Chain);
|
2009-04-13 09:38:38 +00:00
|
|
|
Ops.push_back(Arg);
|
2009-01-13 19:18:47 +00:00
|
|
|
Ops.push_back(PtrLo);
|
|
|
|
Ops.push_back(PtrHi);
|
|
|
|
Ops.push_back(DAG.getConstant(StoreOffset, MVT::i8));
|
|
|
|
Ops.push_back(InFlag);
|
|
|
|
|
2009-02-06 23:05:02 +00:00
|
|
|
StoreRet = DAG.getNode (PIC16ISD::PIC16StWF, dl, Tys, &Ops[0], Ops.size());
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
Chain = getChain(StoreRet);
|
|
|
|
InFlag = getOutFlag(StoreRet);
|
|
|
|
|
|
|
|
// Update the frame offset to be used for next argument
|
|
|
|
ArgVT = Arg.getValueType();
|
|
|
|
Size = ArgVT.getSizeInBits();
|
|
|
|
Size = Size/8; // Calculate size in bytes
|
|
|
|
Offset += Size; // Increase the frame offset
|
|
|
|
}
|
|
|
|
return Chain;
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue PIC16TargetLowering::
|
|
|
|
LowerIndirectCallReturn (SDValue Op, SDValue Chain, SDValue InFlag,
|
|
|
|
SDValue DataAddr_Lo, SDValue DataAddr_Hi,
|
|
|
|
SelectionDAG &DAG) {
|
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
|
|
|
unsigned RetVals = TheCall->getNumRetVals();
|
|
|
|
|
|
|
|
// If call does not have anything to return
|
|
|
|
// then do nothing and go back.
|
|
|
|
if (RetVals == 0)
|
|
|
|
return Chain;
|
|
|
|
|
|
|
|
// Call has something to return
|
|
|
|
std::vector<SDValue> ResultVals;
|
|
|
|
SDValue LoadRet;
|
|
|
|
|
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
|
|
|
|
for(unsigned i=0;i<RetVals;i++) {
|
|
|
|
LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, DataAddr_Lo,
|
|
|
|
DataAddr_Hi, DAG.getConstant(i, MVT::i8),
|
|
|
|
InFlag);
|
|
|
|
InFlag = getOutFlag(LoadRet);
|
|
|
|
Chain = getChain(LoadRet);
|
|
|
|
ResultVals.push_back(LoadRet);
|
|
|
|
}
|
|
|
|
ResultVals.push_back(Chain);
|
|
|
|
SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::
|
2009-04-14 02:49:52 +00:00
|
|
|
LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue RetLabel,
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue InFlag, SelectionDAG &DAG) {
|
2009-01-13 19:18:47 +00:00
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
2009-02-05 00:20:09 +00:00
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
// Currently handling primitive types only. They will come in
|
|
|
|
// i8 parts
|
|
|
|
unsigned RetVals = TheCall->getNumRetVals();
|
|
|
|
|
|
|
|
std::vector<SDValue> ResultVals;
|
2008-11-26 10:53:50 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
// Return immediately if the return type is void
|
|
|
|
if (RetVals == 0)
|
|
|
|
return Chain;
|
2008-11-26 10:53:50 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
// Call has something to return
|
|
|
|
|
|
|
|
// Legalize the address before use
|
|
|
|
SDValue LdLo, LdHi;
|
|
|
|
unsigned LdOffset;
|
2009-04-14 02:49:52 +00:00
|
|
|
LegalizeAddress(RetLabel, DAG, LdLo, LdHi, LdOffset, dl);
|
2008-11-26 10:53:50 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
|
|
|
|
SDValue LoadRet;
|
|
|
|
|
|
|
|
for(unsigned i=0, Offset=0;i<RetVals;i++) {
|
2008-11-26 10:53:50 +00:00
|
|
|
|
2009-02-05 00:20:09 +00:00
|
|
|
LoadRet = DAG.getNode(PIC16ISD::PIC16LdWF, dl, Tys, Chain, LdLo, LdHi,
|
2009-01-13 19:18:47 +00:00
|
|
|
DAG.getConstant(LdOffset + Offset, MVT::i8),
|
|
|
|
InFlag);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
InFlag = getOutFlag(LoadRet);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
Chain = getChain(LoadRet);
|
|
|
|
Offset++;
|
|
|
|
ResultVals.push_back(LoadRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
// To return use MERGE_VALUES
|
|
|
|
ResultVals.push_back(Chain);
|
2009-02-05 00:20:09 +00:00
|
|
|
SDValue Res = DAG.getMergeValues(&ResultVals[0], ResultVals.size(), dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
return Res;
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
|
2009-04-02 17:42:00 +00:00
|
|
|
SDValue Chain = Op.getOperand(0);
|
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
|
|
|
|
|
|
|
if (Op.getNumOperands() == 1) // return void
|
|
|
|
return Op;
|
|
|
|
|
|
|
|
// return should have odd number of operands
|
|
|
|
if ((Op.getNumOperands() % 2) == 0 ) {
|
|
|
|
assert(0 && "Do not know how to return this many arguments!");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Number of values to return
|
|
|
|
unsigned NumRet = (Op.getNumOperands() / 2);
|
|
|
|
|
|
|
|
// Function returns value always on stack with the offset starting
|
|
|
|
// from 0
|
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
std::string FuncName = F->getName();
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *tmpName = createESName(PAN::getFrameLabel(FuncName));
|
2009-04-02 17:42:00 +00:00
|
|
|
SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
|
|
|
|
SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
|
|
|
|
SDValue BS = DAG.getConstant(1, MVT::i8);
|
|
|
|
SDValue RetVal;
|
|
|
|
for(unsigned i=0;i<NumRet; ++i) {
|
|
|
|
RetVal = Op.getNode()->getOperand(2*i + 1);
|
|
|
|
Chain = DAG.getNode (PIC16ISD::PIC16Store, dl, MVT::Other, Chain, RetVal,
|
|
|
|
ES, BS,
|
|
|
|
DAG.getConstant (i, MVT::i8));
|
|
|
|
|
|
|
|
}
|
|
|
|
return DAG.getNode(ISD::RET, dl, MVT::Other, Chain);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// CALL node may have some operands non-legal to PIC16. Generate new CALL
|
|
|
|
// node with all the operands legal.
|
|
|
|
// Currently only Callee operand of the CALL node is non-legal. This function
|
|
|
|
// legalizes the Callee operand and uses all other operands as are to generate
|
|
|
|
// new CALL node.
|
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::LegalizeCALL(SDValue Op, SelectionDAG &DAG) {
|
2009-01-13 19:18:47 +00:00
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
|
|
|
SDValue Chain = TheCall->getChain();
|
|
|
|
SDValue Callee = TheCall->getCallee();
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
unsigned i =0;
|
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
assert(Callee.getValueType() == MVT::i16 &&
|
|
|
|
"Don't know how to legalize this call node!!!");
|
|
|
|
assert(Callee.getOpcode() == ISD::BUILD_PAIR &&
|
|
|
|
"Don't know how to legalize this call node!!!");
|
|
|
|
|
|
|
|
if (isDirectAddress(Callee)) {
|
|
|
|
// Come here for direct calls
|
|
|
|
Callee = Callee.getOperand(0).getOperand(0);
|
|
|
|
} else {
|
|
|
|
// Come here for indirect calls
|
|
|
|
SDValue Lo, Hi;
|
|
|
|
// Indirect addresses. Get the hi and lo parts of ptr.
|
|
|
|
GetExpandedParts(Callee, DAG, Lo, Hi);
|
|
|
|
// Connect Lo and Hi parts of the callee with the PIC16Connect
|
|
|
|
Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Lo, Hi);
|
|
|
|
}
|
|
|
|
std::vector<SDValue> Ops;
|
|
|
|
Ops.push_back(Chain);
|
|
|
|
Ops.push_back(Callee);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// Add the call arguments and their flags
|
|
|
|
unsigned NumArgs = TheCall->getNumArgs();
|
|
|
|
for(i=0;i<NumArgs;i++) {
|
|
|
|
Ops.push_back(TheCall->getArg(i));
|
|
|
|
Ops.push_back(TheCall->getArgFlagsVal(i));
|
|
|
|
}
|
|
|
|
std::vector<MVT> NodeTys;
|
|
|
|
unsigned NumRets = TheCall->getNumRetVals();
|
|
|
|
for(i=0;i<NumRets;i++)
|
|
|
|
NodeTys.push_back(TheCall->getRetValType(i));
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// Return a Chain as well
|
|
|
|
NodeTys.push_back(MVT::Other);
|
|
|
|
|
|
|
|
SDVTList VTs = DAG.getVTList(&NodeTys[0], NodeTys.size());
|
|
|
|
// Generate new call with all the operands legal
|
|
|
|
return DAG.getCall(TheCall->getCallingConv(), dl,
|
|
|
|
TheCall->isVarArg(), TheCall->isTailCall(),
|
|
|
|
TheCall->isInreg(), VTs, &Ops[0], Ops.size());
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
void PIC16TargetLowering::
|
|
|
|
GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
|
|
|
|
SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
|
|
|
|
SelectionDAG &DAG) {
|
|
|
|
assert (Callee.getOpcode() == PIC16ISD::PIC16Connect
|
|
|
|
&& "Don't know what to do of such callee!!");
|
|
|
|
SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
|
|
|
|
SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
|
|
|
|
Chain = getChain(SeqStart);
|
|
|
|
SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
|
|
|
|
|
|
|
|
// Get the Lo and Hi part of code address
|
|
|
|
SDValue Lo = Callee.getOperand(0);
|
|
|
|
SDValue Hi = Callee.getOperand(1);
|
|
|
|
|
2009-04-22 12:02:36 +00:00
|
|
|
SDValue Data_Lo, Data_Hi;
|
2009-04-08 05:38:48 +00:00
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Other, MVT::Flag);
|
2009-05-09 05:11:19 +00:00
|
|
|
// Subtract 2 from Address to get the Lower part of DataAddress.
|
|
|
|
SDVTList VTList = DAG.getVTList(MVT::i8, MVT::Flag);
|
|
|
|
Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
|
|
|
|
DAG.getConstant(2, MVT::i8));
|
|
|
|
SDValue Ops[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
|
|
|
|
Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, Ops, 3);
|
|
|
|
SDValue PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
|
|
|
|
Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee,
|
|
|
|
OperFlag);
|
|
|
|
Chain = getChain(Call);
|
|
|
|
OperFlag = getOutFlag(Call);
|
|
|
|
SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
|
|
|
|
OperFlag);
|
|
|
|
Chain = getChain(SeqEnd);
|
|
|
|
OperFlag = getOutFlag(SeqEnd);
|
|
|
|
|
|
|
|
// Low part of Data Address
|
|
|
|
DataAddr_Lo = DAG.getNode(PIC16ISD::MTLO, dl, MVT::i8, Call, OperFlag);
|
|
|
|
|
|
|
|
// Make the second call.
|
|
|
|
SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
|
|
|
|
Chain = getChain(SeqStart);
|
|
|
|
OperFlag = getOutFlag(SeqStart); // To manage the data dependency
|
|
|
|
|
2009-05-09 05:11:19 +00:00
|
|
|
// Subtract 1 from Address to get high part of data address.
|
|
|
|
Data_Lo = DAG.getNode(ISD::SUBC, dl, VTList, Lo,
|
|
|
|
DAG.getConstant(1, MVT::i8));
|
|
|
|
SDValue HiOps[3] = { Hi, DAG.getConstant(0, MVT::i8), Data_Lo.getValue(1)};
|
|
|
|
Data_Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
|
|
|
|
PCLATH = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, Data_Hi);
|
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// Use new Lo to make another CALLW
|
2009-05-09 05:11:19 +00:00
|
|
|
Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, Data_Lo, PCLATH);
|
2009-04-08 05:38:48 +00:00
|
|
|
Call = DAG.getNode(PIC16ISD::CALLW, dl, Tys, Chain, Callee, OperFlag);
|
|
|
|
Chain = getChain(Call);
|
|
|
|
OperFlag = getOutFlag(Call);
|
|
|
|
SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
|
|
|
|
OperFlag);
|
|
|
|
Chain = getChain(SeqEnd);
|
|
|
|
OperFlag = getOutFlag(SeqEnd);
|
|
|
|
// Hi part of Data Address
|
|
|
|
DataAddr_Hi = DAG.getNode(PIC16ISD::MTHI, dl, MVT::i8, Call, OperFlag);
|
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
|
|
|
|
CallSDNode *TheCall = dyn_cast<CallSDNode>(Op);
|
|
|
|
SDValue Chain = TheCall->getChain();
|
|
|
|
SDValue Callee = TheCall->getCallee();
|
|
|
|
DebugLoc dl = TheCall->getDebugLoc();
|
|
|
|
if (Callee.getValueType() == MVT::i16 &&
|
|
|
|
Callee.getOpcode() == ISD::BUILD_PAIR) {
|
|
|
|
// Control should come here only from TypeLegalizer for lowering
|
|
|
|
|
|
|
|
// Legalize the non-legal arguments of call and return the
|
|
|
|
// new call with legal arguments.
|
|
|
|
return LegalizeCALL(Op, DAG);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
2009-04-08 05:38:48 +00:00
|
|
|
// Control should come here from Legalize DAG.
|
|
|
|
// Here all the operands of CALL node should be legal.
|
2008-11-19 11:00:54 +00:00
|
|
|
|
2009-04-08 05:38:48 +00:00
|
|
|
// If this is an indirect call then to pass the arguments
|
|
|
|
// and read the return value back, we need the data address
|
|
|
|
// of the function being called.
|
|
|
|
// To get the data address two more calls need to be made.
|
|
|
|
|
|
|
|
// The flag to track if this is a direct or indirect call.
|
|
|
|
bool IsDirectCall = true;
|
|
|
|
unsigned RetVals = TheCall->getNumRetVals();
|
|
|
|
unsigned NumArgs = TheCall->getNumArgs();
|
|
|
|
|
|
|
|
SDValue DataAddr_Lo, DataAddr_Hi;
|
|
|
|
if (Callee.getOpcode() == PIC16ISD::PIC16Connect) {
|
|
|
|
IsDirectCall = false; // This is indirect call
|
|
|
|
// Read DataAddress only if we have to pass arguments or
|
|
|
|
// read return value.
|
|
|
|
if ((RetVals > 0) || (NumArgs > 0))
|
|
|
|
GetDataAddress(dl, Callee, Chain, DataAddr_Lo, DataAddr_Hi, DAG);
|
|
|
|
}
|
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue ZeroOperand = DAG.getConstant(0, MVT::i8);
|
|
|
|
|
|
|
|
// Start the call sequence.
|
|
|
|
// Carring the Constant 0 along the CALLSEQSTART
|
|
|
|
// because there is nothing else to carry.
|
|
|
|
SDValue SeqStart = DAG.getCALLSEQ_START(Chain, ZeroOperand);
|
|
|
|
Chain = getChain(SeqStart);
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue OperFlag = getOutFlag(SeqStart); // To manage the data dependency
|
|
|
|
std::string Name;
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// For any direct call - callee will be GlobalAddressNode or
|
|
|
|
// ExternalSymbol
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue ArgLabel, RetLabel;
|
|
|
|
if (IsDirectCall) {
|
|
|
|
// Considering the GlobalAddressNode case here.
|
|
|
|
if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
|
|
|
|
GlobalValue *GV = G->getGlobal();
|
|
|
|
Callee = DAG.getTargetGlobalAddress(GV, MVT::i8);
|
|
|
|
Name = G->getGlobal()->getName();
|
|
|
|
} else {// Considering the ExternalSymbol case here
|
|
|
|
ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Callee);
|
|
|
|
Callee = DAG.getTargetExternalSymbol(ES->getSymbol(), MVT::i8);
|
|
|
|
Name = ES->getSymbol();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Label for argument passing
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *argFrame = createESName(PAN::getArgsLabel(Name));
|
2009-04-08 05:38:48 +00:00
|
|
|
ArgLabel = DAG.getTargetExternalSymbol(argFrame, MVT::i8);
|
|
|
|
|
|
|
|
// Label for reading return value
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *retName = createESName(PAN::getRetvalLabel(Name));
|
2009-04-08 05:38:48 +00:00
|
|
|
RetLabel = DAG.getTargetExternalSymbol(retName, MVT::i8);
|
|
|
|
} else {
|
|
|
|
// if indirect call
|
|
|
|
SDValue CodeAddr_Lo = Callee.getOperand(0);
|
|
|
|
SDValue CodeAddr_Hi = Callee.getOperand(1);
|
|
|
|
|
2009-04-22 12:02:36 +00:00
|
|
|
/*CodeAddr_Lo = DAG.getNode(ISD::ADD, dl, MVT::i8, CodeAddr_Lo,
|
|
|
|
DAG.getConstant(2, MVT::i8));*/
|
2009-04-08 05:38:48 +00:00
|
|
|
|
|
|
|
// move Hi part in PCLATH
|
|
|
|
CodeAddr_Hi = DAG.getNode(PIC16ISD::MTPCLATH, dl, MVT::i8, CodeAddr_Hi);
|
|
|
|
Callee = DAG.getNode(PIC16ISD::PIC16Connect, dl, MVT::i8, CodeAddr_Lo,
|
|
|
|
CodeAddr_Hi);
|
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// Pass the argument to function before making the call.
|
2009-04-08 05:38:48 +00:00
|
|
|
SDValue CallArgs;
|
|
|
|
if (IsDirectCall) {
|
|
|
|
CallArgs = LowerDirectCallArguments(Op, Chain, ArgLabel, OperFlag, DAG);
|
|
|
|
Chain = getChain(CallArgs);
|
|
|
|
OperFlag = getOutFlag(CallArgs);
|
|
|
|
} else {
|
|
|
|
CallArgs = LowerIndirectCallArguments(Op, Chain, OperFlag, DataAddr_Lo,
|
|
|
|
DataAddr_Hi, DAG);
|
|
|
|
Chain = getChain(CallArgs);
|
|
|
|
OperFlag = getOutFlag(CallArgs);
|
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue PICCall = DAG.getNode(PIC16ISD::CALL, dl, Tys, Chain, Callee,
|
2009-01-13 19:18:47 +00:00
|
|
|
OperFlag);
|
|
|
|
Chain = getChain(PICCall);
|
|
|
|
OperFlag = getOutFlag(PICCall);
|
|
|
|
|
|
|
|
|
|
|
|
// Carrying the Constant 0 along the CALLSEQSTART
|
|
|
|
// because there is nothing else to carry.
|
|
|
|
SDValue SeqEnd = DAG.getCALLSEQ_END(Chain, ZeroOperand, ZeroOperand,
|
|
|
|
OperFlag);
|
|
|
|
Chain = getChain(SeqEnd);
|
|
|
|
OperFlag = getOutFlag(SeqEnd);
|
|
|
|
|
|
|
|
// Lower the return value reading after the call.
|
2009-04-08 05:38:48 +00:00
|
|
|
if (IsDirectCall)
|
|
|
|
return LowerDirectCallReturn(Op, Chain, RetLabel, OperFlag, DAG);
|
|
|
|
else
|
|
|
|
return LowerIndirectCallReturn(Op, Chain, OperFlag, DataAddr_Lo,
|
|
|
|
DataAddr_Hi, DAG);
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
bool PIC16TargetLowering::isDirectLoad(const SDValue Op) {
|
|
|
|
if (Op.getOpcode() == PIC16ISD::PIC16Load)
|
|
|
|
if (Op.getOperand(1).getOpcode() == ISD::TargetGlobalAddress
|
|
|
|
|| Op.getOperand(1).getOpcode() == ISD::TargetExternalSymbol)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// NeedToConvertToMemOp - Returns true if one of the operands of the
|
|
|
|
// operation 'Op' needs to be put into memory. Also returns the
|
|
|
|
// operand no. of the operand to be converted in 'MemOp'. Remember, PIC16 has
|
|
|
|
// no instruction that can operation on two registers. Most insns take
|
|
|
|
// one register and one memory operand (addwf) / Constant (addlw).
|
2009-01-13 19:18:47 +00:00
|
|
|
bool PIC16TargetLowering::NeedToConvertToMemOp(SDValue Op, unsigned &MemOp) {
|
2009-03-20 13:42:20 +00:00
|
|
|
// If one of the operand is a constant, return false.
|
2009-01-13 19:18:47 +00:00
|
|
|
if (Op.getOperand(0).getOpcode() == ISD::Constant ||
|
|
|
|
Op.getOperand(1).getOpcode() == ISD::Constant)
|
|
|
|
return false;
|
2009-03-20 13:42:20 +00:00
|
|
|
|
|
|
|
// Return false if one of the operands is already a direct
|
|
|
|
// load and that operand has only one use.
|
2009-01-13 19:18:47 +00:00
|
|
|
if (isDirectLoad(Op.getOperand(0))) {
|
|
|
|
if (Op.getOperand(0).hasOneUse())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
MemOp = 0;
|
|
|
|
}
|
|
|
|
if (isDirectLoad(Op.getOperand(1))) {
|
|
|
|
if (Op.getOperand(1).hasOneUse())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
MemOp = 1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// LowerBinOp - Lower a commutative binary operation that does not
|
|
|
|
// affect status flag carry.
|
|
|
|
SDValue PIC16TargetLowering::LowerBinOp(SDValue Op, SelectionDAG &DAG) {
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-03-20 13:42:20 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// We should have handled larger operands in type legalizer itself.
|
2009-01-13 19:18:47 +00:00
|
|
|
assert (Op.getValueType() == MVT::i8 && "illegal Op to lower");
|
2009-03-20 13:42:20 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
unsigned MemOp = 1;
|
|
|
|
if (NeedToConvertToMemOp(Op, MemOp)) {
|
|
|
|
// Put one value on stack.
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
|
2009-01-13 19:18:47 +00:00
|
|
|
NewVal);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Op;
|
|
|
|
}
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// LowerADD - Lower all types of ADD operations including the ones
|
|
|
|
// that affects carry.
|
|
|
|
SDValue PIC16TargetLowering::LowerADD(SDValue Op, SelectionDAG &DAG) {
|
2009-01-13 19:18:47 +00:00
|
|
|
// We should have handled larger operands in type legalizer itself.
|
|
|
|
assert (Op.getValueType() == MVT::i8 && "illegal add to lower");
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
unsigned MemOp = 1;
|
|
|
|
if (NeedToConvertToMemOp(Op, MemOp)) {
|
|
|
|
// Put one value on stack.
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue NewVal = ConvertToMemOperand (Op.getOperand(MemOp), DAG, dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-06-01 23:13:42 +00:00
|
|
|
// ADDC and ADDE produce two results.
|
2009-01-13 19:18:47 +00:00
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-06-01 23:13:42 +00:00
|
|
|
// ADDE has three operands, the last one is the carry bit.
|
2009-01-13 19:18:47 +00:00
|
|
|
if (Op.getOpcode() == ISD::ADDE)
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
|
|
|
|
NewVal, Op.getOperand(2));
|
2009-04-09 04:03:43 +00:00
|
|
|
// ADDC has two operands.
|
|
|
|
else if (Op.getOpcode() == ISD::ADDC)
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(Op.getOpcode(), dl, Tys, Op.getOperand(MemOp ^ 1),
|
|
|
|
NewVal);
|
2009-04-09 04:03:43 +00:00
|
|
|
// ADD it is. It produces only one result.
|
|
|
|
else
|
|
|
|
return DAG.getNode(Op.getOpcode(), dl, MVT::i8, Op.getOperand(MemOp ^ 1),
|
|
|
|
NewVal);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
2009-04-09 04:03:43 +00:00
|
|
|
else
|
2009-05-28 06:59:57 +00:00
|
|
|
return Op;
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerSUB(SDValue Op, SelectionDAG &DAG) {
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2008-11-19 11:00:54 +00:00
|
|
|
// We should have handled larger operands in type legalizer itself.
|
2009-01-13 19:18:47 +00:00
|
|
|
assert (Op.getValueType() == MVT::i8 && "illegal sub to lower");
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-01-13 19:18:47 +00:00
|
|
|
// Nothing to do if the first operand is already a direct load and it has
|
|
|
|
// only one use.
|
|
|
|
if (isDirectLoad(Op.getOperand(0)) && Op.getOperand(0).hasOneUse())
|
2009-05-28 06:59:57 +00:00
|
|
|
return Op;
|
2008-06-06 12:08:01 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
// Put first operand on stack.
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue NewVal = ConvertToMemOperand (Op.getOperand(0), DAG, dl);
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2008-11-19 11:00:54 +00:00
|
|
|
SDVTList Tys = DAG.getVTList(MVT::i8, MVT::Flag);
|
2009-01-13 19:18:47 +00:00
|
|
|
if (Op.getOpcode() == ISD::SUBE)
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1),
|
2009-01-13 19:18:47 +00:00
|
|
|
Op.getOperand(2));
|
|
|
|
else
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(Op.getOpcode(), dl, Tys, NewVal, Op.getOperand(1));
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-04-21 05:54:51 +00:00
|
|
|
void PIC16TargetLowering::InitReservedFrameCount(const Function *F) {
|
|
|
|
unsigned NumArgs = F->arg_size();
|
|
|
|
|
|
|
|
bool isVoidFunc = (F->getReturnType()->getTypeID() == Type::VoidTyID);
|
|
|
|
|
|
|
|
if (isVoidFunc)
|
|
|
|
ReservedFrameCount = NumArgs;
|
|
|
|
else
|
|
|
|
ReservedFrameCount = NumArgs + 1;
|
|
|
|
}
|
|
|
|
|
2009-04-06 10:54:50 +00:00
|
|
|
// LowerFORMAL_ARGUMENTS - Argument values are loaded from the
|
|
|
|
// <fname>.args + offset. All arguments are already broken to leaglized
|
|
|
|
// types, so the offset just runs from 0 to NumArgVals - 1.
|
2008-05-14 11:31:39 +00:00
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
SDValue PIC16TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
|
2009-04-06 10:54:50 +00:00
|
|
|
SelectionDAG &DAG) {
|
2008-11-19 11:00:54 +00:00
|
|
|
SmallVector<SDValue, 8> ArgValues;
|
2009-04-06 10:54:50 +00:00
|
|
|
unsigned NumArgVals = Op.getNode()->getNumValues() - 1;
|
2009-02-06 23:05:02 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-04-02 17:42:00 +00:00
|
|
|
SDValue Chain = Op.getOperand(0); // Formal arguments' chain
|
|
|
|
|
2009-04-06 10:54:50 +00:00
|
|
|
|
|
|
|
// Get the callee's name to create the <fname>.args label to pass args.
|
2009-04-02 17:42:00 +00:00
|
|
|
MachineFunction &MF = DAG.getMachineFunction();
|
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
std::string FuncName = F->getName();
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-04-21 05:54:51 +00:00
|
|
|
// Reset the map of FI and TmpOffset
|
|
|
|
ResetTmpOffsetMap();
|
|
|
|
// Initialize the ReserveFrameCount
|
|
|
|
InitReservedFrameCount(F);
|
|
|
|
|
2009-04-06 10:54:50 +00:00
|
|
|
// Create the <fname>.args external symbol.
|
2009-05-10 05:23:47 +00:00
|
|
|
const char *tmpName = createESName(PAN::getArgsLabel(FuncName));
|
2009-04-02 17:42:00 +00:00
|
|
|
SDValue ES = DAG.getTargetExternalSymbol(tmpName, MVT::i8);
|
2009-04-06 10:54:50 +00:00
|
|
|
|
|
|
|
// Load arg values from the label + offset.
|
|
|
|
SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Other);
|
2009-04-02 17:42:00 +00:00
|
|
|
SDValue BS = DAG.getConstant(1, MVT::i8);
|
2009-04-06 10:54:50 +00:00
|
|
|
for (unsigned i = 0; i < NumArgVals ; ++i) {
|
2009-04-02 17:42:00 +00:00
|
|
|
SDValue Offset = DAG.getConstant(i, MVT::i8);
|
|
|
|
SDValue PICLoad = DAG.getNode(PIC16ISD::PIC16LdArg, dl, VTs, Chain, ES, BS,
|
2009-04-06 10:54:50 +00:00
|
|
|
Offset);
|
2009-04-02 17:42:00 +00:00
|
|
|
Chain = getChain(PICLoad);
|
|
|
|
ArgValues.push_back(PICLoad);
|
2008-11-19 11:00:54 +00:00
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-04-06 10:54:50 +00:00
|
|
|
// Return a MERGE_VALUE node.
|
2008-11-19 11:00:54 +00:00
|
|
|
ArgValues.push_back(Op.getOperand(0));
|
2009-02-06 23:05:02 +00:00
|
|
|
return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
|
2009-04-06 10:54:50 +00:00
|
|
|
&ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-20 13:42:20 +00:00
|
|
|
// Perform DAGCombine of PIC16Load.
|
|
|
|
// FIXME - Need a more elaborate comment here.
|
2008-11-19 11:00:54 +00:00
|
|
|
SDValue PIC16TargetLowering::
|
|
|
|
PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const {
|
|
|
|
SelectionDAG &DAG = DCI.DAG;
|
|
|
|
SDValue Chain = N->getOperand(0);
|
|
|
|
if (N->hasNUsesOfValue(0, 0)) {
|
|
|
|
DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), Chain);
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
return SDValue();
|
|
|
|
}
|
2008-05-13 09:02:57 +00:00
|
|
|
|
2009-04-02 17:42:00 +00:00
|
|
|
// For all the functions with arguments some STORE nodes are generated
|
|
|
|
// that store the argument on the frameindex. However in PIC16 the arguments
|
|
|
|
// are passed on stack only. Therefore these STORE nodes are redundant.
|
|
|
|
// To remove these STORE nodes will be removed in PerformStoreCombine
|
|
|
|
//
|
|
|
|
// Currently this function is doint nothing and will be updated for removing
|
|
|
|
// unwanted store operations
|
|
|
|
SDValue PIC16TargetLowering::
|
|
|
|
PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const {
|
|
|
|
return SDValue(N, 0);
|
|
|
|
/*
|
|
|
|
// Storing an undef value is of no use, so remove it
|
|
|
|
if (isStoringUndef(N, Chain, DAG)) {
|
|
|
|
return Chain; // remove the store and return the chain
|
|
|
|
}
|
|
|
|
//else everything is ok.
|
|
|
|
return SDValue(N, 0);
|
|
|
|
*/
|
|
|
|
}
|
2008-11-19 11:00:54 +00:00
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::PerformDAGCombine(SDNode *N,
|
|
|
|
DAGCombinerInfo &DCI) const {
|
|
|
|
switch (N->getOpcode()) {
|
2009-04-02 17:42:00 +00:00
|
|
|
case ISD::STORE:
|
|
|
|
return PerformStoreCombine(N, DCI);
|
|
|
|
case PIC16ISD::PIC16Load:
|
2008-11-19 11:00:54 +00:00
|
|
|
return PerformPIC16LoadCombine(N, DCI);
|
|
|
|
}
|
|
|
|
return SDValue();
|
2008-05-13 09:02:57 +00:00
|
|
|
}
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
static PIC16CC::CondCodes IntCCToPIC16CC(ISD::CondCode CC) {
|
|
|
|
switch (CC) {
|
|
|
|
default: assert(0 && "Unknown condition code!");
|
|
|
|
case ISD::SETNE: return PIC16CC::NE;
|
|
|
|
case ISD::SETEQ: return PIC16CC::EQ;
|
|
|
|
case ISD::SETGT: return PIC16CC::GT;
|
|
|
|
case ISD::SETGE: return PIC16CC::GE;
|
|
|
|
case ISD::SETLT: return PIC16CC::LT;
|
|
|
|
case ISD::SETLE: return PIC16CC::LE;
|
2009-01-21 05:44:05 +00:00
|
|
|
case ISD::SETULT: return PIC16CC::ULT;
|
2009-06-03 13:36:44 +00:00
|
|
|
case ISD::SETULE: return PIC16CC::ULE;
|
|
|
|
case ISD::SETUGE: return PIC16CC::UGE;
|
2009-01-21 05:44:05 +00:00
|
|
|
case ISD::SETUGT: return PIC16CC::UGT;
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
|
|
|
|
// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
|
|
|
|
static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
|
|
|
|
ISD::CondCode CC, unsigned &SPCC) {
|
|
|
|
if (isa<ConstantSDNode>(RHS) &&
|
|
|
|
cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
|
|
|
|
CC == ISD::SETNE &&
|
|
|
|
(LHS.getOpcode() == PIC16ISD::SELECT_ICC &&
|
|
|
|
LHS.getOperand(3).getOpcode() == PIC16ISD::SUBCC) &&
|
|
|
|
isa<ConstantSDNode>(LHS.getOperand(0)) &&
|
|
|
|
isa<ConstantSDNode>(LHS.getOperand(1)) &&
|
|
|
|
cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
|
|
|
|
cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
|
|
|
|
SDValue CMPCC = LHS.getOperand(3);
|
|
|
|
SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
|
|
|
|
LHS = CMPCC.getOperand(0);
|
|
|
|
RHS = CMPCC.getOperand(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns appropriate CMP insn and corresponding condition code in PIC16CC
|
|
|
|
SDValue PIC16TargetLowering::getPIC16Cmp(SDValue LHS, SDValue RHS,
|
|
|
|
unsigned CC, SDValue &PIC16CC,
|
2009-02-05 01:01:16 +00:00
|
|
|
SelectionDAG &DAG, DebugLoc dl) {
|
2009-01-13 19:18:47 +00:00
|
|
|
PIC16CC::CondCodes CondCode = (PIC16CC::CondCodes) CC;
|
|
|
|
|
|
|
|
// PIC16 sub is literal - W. So Swap the operands and condition if needed.
|
|
|
|
// i.e. a < 12 can be rewritten as 12 > a.
|
|
|
|
if (RHS.getOpcode() == ISD::Constant) {
|
|
|
|
|
|
|
|
SDValue Tmp = LHS;
|
|
|
|
LHS = RHS;
|
|
|
|
RHS = Tmp;
|
|
|
|
|
|
|
|
switch (CondCode) {
|
|
|
|
default: break;
|
|
|
|
case PIC16CC::LT:
|
|
|
|
CondCode = PIC16CC::GT;
|
|
|
|
break;
|
|
|
|
case PIC16CC::GT:
|
|
|
|
CondCode = PIC16CC::LT;
|
|
|
|
break;
|
2009-01-21 05:44:05 +00:00
|
|
|
case PIC16CC::ULT:
|
|
|
|
CondCode = PIC16CC::UGT;
|
|
|
|
break;
|
|
|
|
case PIC16CC::UGT:
|
|
|
|
CondCode = PIC16CC::ULT;
|
|
|
|
break;
|
2009-01-13 19:18:47 +00:00
|
|
|
case PIC16CC::GE:
|
|
|
|
CondCode = PIC16CC::LE;
|
|
|
|
break;
|
|
|
|
case PIC16CC::LE:
|
|
|
|
CondCode = PIC16CC::GE;
|
|
|
|
break;
|
2009-01-21 05:44:05 +00:00
|
|
|
case PIC16CC::ULE:
|
|
|
|
CondCode = PIC16CC::UGE;
|
|
|
|
break;
|
|
|
|
case PIC16CC::UGE:
|
|
|
|
CondCode = PIC16CC::ULE;
|
|
|
|
break;
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PIC16CC = DAG.getConstant(CondCode, MVT::i8);
|
|
|
|
|
2009-01-21 05:44:05 +00:00
|
|
|
// These are signed comparisons.
|
|
|
|
SDValue Mask = DAG.getConstant(128, MVT::i8);
|
|
|
|
if (isSignedComparison(CondCode)) {
|
2009-02-05 01:01:16 +00:00
|
|
|
LHS = DAG.getNode (ISD::XOR, dl, MVT::i8, LHS, Mask);
|
|
|
|
RHS = DAG.getNode (ISD::XOR, dl, MVT::i8, RHS, Mask);
|
2009-01-21 05:44:05 +00:00
|
|
|
}
|
2009-01-30 07:55:25 +00:00
|
|
|
|
|
|
|
SDVTList VTs = DAG.getVTList (MVT::i8, MVT::Flag);
|
2009-01-13 19:18:47 +00:00
|
|
|
// We can use a subtract operation to set the condition codes. But
|
|
|
|
// we need to put one operand in memory if required.
|
2009-01-30 07:55:25 +00:00
|
|
|
// Nothing to do if the first operand is already a valid type (direct load
|
|
|
|
// for subwf and literal for sublw) and it is used by this operation only.
|
|
|
|
if ((LHS.getOpcode() == ISD::Constant || isDirectLoad(LHS))
|
|
|
|
&& LHS.hasOneUse())
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
|
2009-01-30 07:55:25 +00:00
|
|
|
|
|
|
|
// else convert the first operand to mem.
|
2009-02-05 01:01:16 +00:00
|
|
|
LHS = ConvertToMemOperand (LHS, DAG, dl);
|
|
|
|
return DAG.getNode(PIC16ISD::SUBCC, dl, VTs, LHS, RHS);
|
2009-01-13 19:18:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
|
|
|
|
SDValue LHS = Op.getOperand(0);
|
|
|
|
SDValue RHS = Op.getOperand(1);
|
|
|
|
ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
|
|
|
|
SDValue TrueVal = Op.getOperand(2);
|
|
|
|
SDValue FalseVal = Op.getOperand(3);
|
|
|
|
unsigned ORIGCC = ~0;
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// If this is a select_cc of a "setcc", and if the setcc got lowered into
|
|
|
|
// an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
|
|
|
|
// i.e.
|
|
|
|
// A setcc: lhs, rhs, cc is expanded by llvm to
|
|
|
|
// select_cc: result of setcc, 0, 1, 0, setne
|
|
|
|
// We can think of it as:
|
|
|
|
// select_cc: lhs, rhs, 1, 0, cc
|
|
|
|
LookThroughSetCC(LHS, RHS, CC, ORIGCC);
|
|
|
|
if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
|
|
|
|
|
|
|
|
SDValue PIC16CC;
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode (PIC16ISD::SELECT_ICC, dl, TrueVal.getValueType(), TrueVal,
|
2009-01-13 19:18:47 +00:00
|
|
|
FalseVal, PIC16CC, Cmp.getValue(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *
|
|
|
|
PIC16TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
|
2009-02-07 16:15:20 +00:00
|
|
|
MachineBasicBlock *BB) const {
|
2009-01-13 19:18:47 +00:00
|
|
|
const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
|
|
|
|
unsigned CC = (PIC16CC::CondCodes)MI->getOperand(3).getImm();
|
2009-02-13 02:29:03 +00:00
|
|
|
DebugLoc dl = MI->getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// To "insert" a SELECT_CC instruction, we actually have to insert the diamond
|
|
|
|
// control-flow pattern. The incoming instruction knows the destination vreg
|
|
|
|
// to set, the condition code register to branch on, the true/false values to
|
|
|
|
// select between, and a branch opcode to use.
|
|
|
|
const BasicBlock *LLVM_BB = BB->getBasicBlock();
|
|
|
|
MachineFunction::iterator It = BB;
|
|
|
|
++It;
|
|
|
|
|
|
|
|
// thisMBB:
|
|
|
|
// ...
|
|
|
|
// TrueVal = ...
|
|
|
|
// [f]bCC copy1MBB
|
|
|
|
// fallthrough --> copy0MBB
|
|
|
|
MachineBasicBlock *thisMBB = BB;
|
|
|
|
MachineFunction *F = BB->getParent();
|
|
|
|
MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
|
|
|
|
MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(BB, dl, TII.get(PIC16::pic16brcond)).addMBB(sinkMBB).addImm(CC);
|
2009-01-13 19:18:47 +00:00
|
|
|
F->insert(It, copy0MBB);
|
|
|
|
F->insert(It, sinkMBB);
|
|
|
|
|
|
|
|
// Update machine-CFG edges by transferring all successors of the current
|
|
|
|
// block to the new block which will contain the Phi node for the select.
|
|
|
|
sinkMBB->transferSuccessors(BB);
|
|
|
|
// Next, add the true and fallthrough blocks as its successors.
|
|
|
|
BB->addSuccessor(copy0MBB);
|
|
|
|
BB->addSuccessor(sinkMBB);
|
|
|
|
|
|
|
|
// copy0MBB:
|
|
|
|
// %FalseValue = ...
|
|
|
|
// # fallthrough to sinkMBB
|
|
|
|
BB = copy0MBB;
|
|
|
|
|
|
|
|
// Update machine-CFG edges
|
|
|
|
BB->addSuccessor(sinkMBB);
|
|
|
|
|
|
|
|
// sinkMBB:
|
|
|
|
// %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
|
|
|
|
// ...
|
|
|
|
BB = sinkMBB;
|
2009-02-13 02:29:03 +00:00
|
|
|
BuildMI(BB, dl, TII.get(PIC16::PHI), MI->getOperand(0).getReg())
|
2009-01-13 19:18:47 +00:00
|
|
|
.addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
|
|
|
|
.addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
|
|
|
|
|
|
|
|
F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
|
|
|
|
return BB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SDValue PIC16TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
|
|
|
|
SDValue Chain = Op.getOperand(0);
|
|
|
|
ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
|
|
|
|
SDValue LHS = Op.getOperand(2); // LHS of the condition.
|
|
|
|
SDValue RHS = Op.getOperand(3); // RHS of the condition.
|
|
|
|
SDValue Dest = Op.getOperand(4); // BB to jump to
|
|
|
|
unsigned ORIGCC = ~0;
|
2009-02-05 01:01:16 +00:00
|
|
|
DebugLoc dl = Op.getDebugLoc();
|
2009-01-13 19:18:47 +00:00
|
|
|
|
|
|
|
// If this is a br_cc of a "setcc", and if the setcc got lowered into
|
|
|
|
// an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
|
|
|
|
LookThroughSetCC(LHS, RHS, CC, ORIGCC);
|
|
|
|
if (ORIGCC == ~0U) ORIGCC = IntCCToPIC16CC (CC);
|
|
|
|
|
|
|
|
// Get the Compare insn and condition code.
|
|
|
|
SDValue PIC16CC;
|
2009-02-05 01:01:16 +00:00
|
|
|
SDValue Cmp = getPIC16Cmp(LHS, RHS, ORIGCC, PIC16CC, DAG, dl);
|
2009-01-13 19:18:47 +00:00
|
|
|
|
2009-02-05 01:01:16 +00:00
|
|
|
return DAG.getNode(PIC16ISD::BRCOND, dl, MVT::Other, Chain, Dest, PIC16CC,
|
2009-01-13 19:18:47 +00:00
|
|
|
Cmp.getValue(1));
|
|
|
|
}
|
|
|
|
|