Propagate changes from my local tree. This patch includes:

1. New parameter attribute called 'inreg'. It has meaning "place this
parameter in registers, if possible". This is some generalization of
gcc's regparm(n) attribute. It's currently used only in X86-32 backend.
2. Completely rewritten CC handling/lowering code inside X86 backend.
Merged stdcall + c CCs and fastcall + fast CC.
3. Dropped CSRET CC. We cannot add struct return variant for each
target-specific CC (e.g. stdcall + csretcc and so on).
4. Instead of CSRET CC introduced 'sret' parameter attribute. Setting in
on first attribute has meaning 'This is hidden pointer to structure
return. Handle it gently'.
5. Fixed small bug in llvm-extract + add new feature to
FunctionExtraction pass, which relinks all internal-linkaged callees
from deleted function to external linkage. This will allow further
linking everything together.

NOTEs: 1. Documentation will be updated soon.
       2. llvm-upgrade should be improved to translate csret => sret.
          Before this, there will be some unexpected test fails.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33597 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Anton Korobeynikov 2007-01-28 13:31:35 +00:00
parent f0876c7cb7
commit b10308e440
24 changed files with 504 additions and 840 deletions

View File

@ -30,14 +30,6 @@ namespace CallingConv {
/// certain amounts of prototype mismatch.
C = 0,
/// CSRet - C Struct Return calling convention. This convention requires
/// that the function return void and take a pointer as the first argument
/// of the struct. This is used by targets which need to distinguish
/// between C functions returning a structure, and C functions taking a
/// structure pointer as the first argument to the function.
CSRet = 1,
// Generic LLVM calling conventions. None of these calling conventions
// support varargs calls, and all assume that the caller and callee
// prototype exactly match.

View File

@ -133,20 +133,25 @@ namespace ISD {
// UNDEF - An undefined node
UNDEF,
/// FORMAL_ARGUMENTS(CHAIN, CC#, ISVARARG) - This node represents the formal
/// arguments for a function. CC# is a Constant value indicating the
/// calling convention of the function, and ISVARARG is a flag that
/// indicates whether the function is varargs or not. This node has one
/// result value for each incoming argument, plus one for the output chain.
/// It must be custom legalized.
/// FORMAL_ARGUMENTS(CHAIN, CC#, ISVARARG, FLAG0, ..., FLAGn) - This node
/// represents the formal arguments for a function. CC# is a Constant value
/// indicating the calling convention of the function, and ISVARARG is a
/// flag that indicates whether the function is varargs or not. This node
/// has one result value for each incoming argument, plus one for the output
/// chain. It must be custom legalized. See description of CALL node for
/// FLAG argument contents explanation.
///
FORMAL_ARGUMENTS,
/// RV1, RV2...RVn, CHAIN = CALL(CHAIN, CC#, ISVARARG, ISTAILCALL, CALLEE,
/// ARG0, SIGN0, ARG1, SIGN1, ... ARGn, SIGNn)
/// ARG0, FLAG0, ARG1, FLAG1, ... ARGn, FLAGn)
/// This node represents a fully general function call, before the legalizer
/// runs. This has one result value for each argument / signness pair, plus
/// a chain result. It must be custom legalized.
/// runs. This has one result value for each argument / flag pair, plus
/// a chain result. It must be custom legalized. Flag argument indicates
/// misc. argument attributes. Currently:
/// Bit 0 - signness
/// Bit 1 - 'inreg' attribute
/// Bit 2 - 'sret' attribute
CALL,
// EXTRACT_ELEMENT - This is used to get the first or second (determined by

View File

@ -134,7 +134,9 @@ public:
NoAttributeSet = 0, ///< No attribute value has been set
ZExtAttribute = 1, ///< zero extended before/after call
SExtAttribute = 1 << 1, ///< sign extended before/after call
NoReturnAttribute = 1 << 2 ///< mark the function as not returning
NoReturnAttribute = 1 << 2, ///< mark the function as not returning
InRegAttribute = 1 << 3, ///< force argument to be passed in register
StructRetAttribute= 1 << 4 ///< hidden pointer to structure to return
};
typedef std::vector<ParameterAttributes> ParamAttrsList;
private:
@ -176,6 +178,10 @@ public:
///
unsigned getNumParams() const { return unsigned(ContainedTys.size()-1); }
bool isStructReturn() const {
return (getNumParams() && paramHasAttr(1, StructRetAttribute));
}
/// The parameter attributes for the \p ith parameter are returned. The 0th
/// parameter refers to the return type of the function.
/// @returns The ParameterAttributes for the \p ith parameter.

View File

@ -730,6 +730,8 @@ public:
SDOperand Node;
const Type* Ty;
bool isSigned;
bool isInReg;
bool isSRet;
};
typedef std::vector<ArgListEntry> ArgListTy;
virtual std::pair<SDOperand, SDOperand>

View File

@ -82,7 +82,8 @@ ModulePass *createGlobalDCEPass();
/// the specified function. Otherwise, it deletes as much of the module as
/// possible, except for the function specified.
///
ModulePass *createFunctionExtractionPass(Function *F, bool deleteFn = false);
ModulePass *createFunctionExtractionPass(Function *F, bool deleteFn = false,
bool relinkCallees = false);
//===----------------------------------------------------------------------===//

View File

@ -227,7 +227,6 @@ sideeffect { return SIDEEFFECT; }
cc { return CC_TOK; }
ccc { return CCC_TOK; }
csretcc { return CSRETCC_TOK; }
fastcc { return FASTCC_TOK; }
coldcc { return COLDCC_TOK; }
x86_stdcallcc { return X86_STDCALLCC_TOK; }
@ -287,6 +286,8 @@ call { RET_TOK(OtherOpVal, Call, CALL); }
trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
sext { RET_TOK(CastOpVal, SExt, SEXT); }
inreg { return INREG; }
sret { return SRET; }
fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }

View File

@ -227,7 +227,6 @@ sideeffect { return SIDEEFFECT; }
cc { return CC_TOK; }
ccc { return CCC_TOK; }
csretcc { return CSRETCC_TOK; }
fastcc { return FASTCC_TOK; }
coldcc { return COLDCC_TOK; }
x86_stdcallcc { return X86_STDCALLCC_TOK; }
@ -287,6 +286,8 @@ call { RET_TOK(OtherOpVal, Call, CALL); }
trunc { RET_TOK(CastOpVal, Trunc, TRUNC); }
zext { RET_TOK(CastOpVal, ZExt, ZEXT); }
sext { RET_TOK(CastOpVal, SExt, SEXT); }
inreg { return INREG; }
sret { return SRET; }
fptrunc { RET_TOK(CastOpVal, FPTrunc, FPTRUNC); }
fpext { RET_TOK(CastOpVal, FPExt, FPEXT); }
uitofp { RET_TOK(CastOpVal, UIToFP, UITOFP); }

View File

@ -984,8 +984,7 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
%token DLLIMPORT DLLEXPORT EXTERN_WEAK
%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
%token DATALAYOUT
%type <UIntVal> OptCallingConv
%type <ParamAttrs> OptParamAttrs ParamAttr
@ -1017,7 +1016,7 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
// Function Attributes
%token NORETURN
%token NORETURN INREG SRET
// Visibility Styles
%token DEFAULT HIDDEN
@ -1119,7 +1118,6 @@ FunctionDefineLinkage
OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
CCC_TOK { $$ = CallingConv::C; } |
CSRETCC_TOK { $$ = CallingConv::CSRet; } |
FASTCC_TOK { $$ = CallingConv::Fast; } |
COLDCC_TOK { $$ = CallingConv::Cold; } |
X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
@ -1131,8 +1129,10 @@ OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
CHECK_FOR_ERROR
};
ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
| SEXT { $$ = FunctionType::SExtAttribute; }
ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
| SEXT { $$ = FunctionType::SExtAttribute; }
| INREG { $$ = FunctionType::InRegAttribute; }
| SRET { $$ = FunctionType::StructRetAttribute; }
;
OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }

View File

@ -984,8 +984,7 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
%token DLLIMPORT DLLEXPORT EXTERN_WEAK
%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
%token DATALAYOUT
%type <UIntVal> OptCallingConv
%type <ParamAttrs> OptParamAttrs ParamAttr
@ -1017,7 +1016,7 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
// Function Attributes
%token NORETURN
%token NORETURN INREG SRET
// Visibility Styles
%token DEFAULT HIDDEN
@ -1119,7 +1118,6 @@ FunctionDefineLinkage
OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
CCC_TOK { $$ = CallingConv::C; } |
CSRETCC_TOK { $$ = CallingConv::CSRet; } |
FASTCC_TOK { $$ = CallingConv::Fast; } |
COLDCC_TOK { $$ = CallingConv::Cold; } |
X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
@ -1131,8 +1129,10 @@ OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
CHECK_FOR_ERROR
};
ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
| SEXT { $$ = FunctionType::SExtAttribute; }
ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
| SEXT { $$ = FunctionType::SExtAttribute; }
| INREG { $$ = FunctionType::InRegAttribute; }
| SRET { $$ = FunctionType::StructRetAttribute; }
;
OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }

View File

@ -2168,6 +2168,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
const char *FnName = 0;
if (Node->getOpcode() == ISD::MEMSET) {
Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy;
Entry.isInReg = false;
Args.push_back(Entry);
// Extend the (previously legalized) ubyte argument to be an int value
// for the call.
@ -2176,6 +2177,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
else
Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true;
Entry.isInReg = false;
Args.push_back(Entry);
Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false;
Args.push_back(Entry);
@ -2183,7 +2185,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
FnName = "memset";
} else if (Node->getOpcode() == ISD::MEMCPY ||
Node->getOpcode() == ISD::MEMMOVE) {
Entry.Ty = IntPtrTy; Entry.isSigned = false;
Entry.Ty = IntPtrTy; Entry.isSigned = false; Entry.isInReg = false;
Entry.Node = Tmp2; Args.push_back(Entry);
Entry.Node = Tmp3; Args.push_back(Entry);
Entry.Node = Tmp4; Args.push_back(Entry);
@ -4124,7 +4126,7 @@ SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Entry.isSigned = isSigned;
Entry.isSigned = isSigned; Entry.isInReg = false;
Args.push_back(Entry);
}
SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());

View File

@ -2167,6 +2167,8 @@ void SelectionDAGLowering::visitCall(CallInst &I) {
SDOperand ArgNode = getValue(Arg);
Entry.Node = ArgNode; Entry.Ty = Arg->getType();
Entry.isSigned = FTy->paramHasAttr(i, FunctionType::SExtAttribute);
Entry.isInReg = FTy->paramHasAttr(i, FunctionType::InRegAttribute);
Entry.isSRet = FTy->paramHasAttr(i, FunctionType::StructRetAttribute);
Args.push_back(Entry);
}
@ -2761,6 +2763,8 @@ void SelectionDAGLowering::visitMalloc(MallocInst &I) {
Entry.Node = Src;
Entry.Ty = TLI.getTargetData()->getIntPtrType();
Entry.isSigned = false;
Entry.isInReg = false;
Entry.isSRet = false;
Args.push_back(Entry);
std::pair<SDOperand,SDOperand> Result =
@ -2777,6 +2781,8 @@ void SelectionDAGLowering::visitFree(FreeInst &I) {
Entry.Node = getValue(I.getOperand(0));
Entry.Ty = TLI.getTargetData()->getIntPtrType();
Entry.isSigned = false;
Entry.isInReg = false;
Entry.isSRet = false;
Args.push_back(Entry);
MVT::ValueType IntPtr = TLI.getPointerTy();
std::pair<SDOperand,SDOperand> Result =
@ -2859,6 +2865,7 @@ static SDOperand ExpandScalarFormalArgs(MVT::ValueType VT, SDNode *Arg,
/// integrated into SDISel.
std::vector<SDOperand>
TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
const FunctionType *FTy = F.getFunctionType();
// Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
std::vector<SDOperand> Ops;
Ops.push_back(DAG.getRoot());
@ -2867,16 +2874,22 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
// Add one result value for each formal argument.
std::vector<MVT::ValueType> RetVals;
unsigned j = 0;
for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
MVT::ValueType VT = getValueType(I->getType());
bool isInReg = FTy->paramHasAttr(++j, FunctionType::InRegAttribute);
bool isSRet = FTy->paramHasAttr(j, FunctionType::StructRetAttribute);
unsigned Flags = (isInReg << 1) | (isSRet << 2);
switch (getTypeAction(VT)) {
default: assert(0 && "Unknown type action!");
case Legal:
RetVals.push_back(VT);
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
break;
case Promote:
RetVals.push_back(getTypeToTransformTo(VT));
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
break;
case Expand:
if (VT != MVT::Vector) {
@ -2885,8 +2898,10 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
// integers it turns into.
MVT::ValueType NVT = getTypeToExpandTo(VT);
unsigned NumVals = getNumElements(VT);
for (unsigned i = 0; i != NumVals; ++i)
for (unsigned i = 0; i != NumVals; ++i) {
RetVals.push_back(NVT);
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
}
} else {
// Otherwise, this is a vector type. We only support legal vectors
// right now.
@ -2898,6 +2913,7 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
MVT::ValueType TVT = MVT::getVectorType(getValueType(EltTy), NumElems);
if (TVT != MVT::Other && isTypeLegal(TVT)) {
RetVals.push_back(TVT);
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
} else {
assert(0 && "Don't support illegal by-val vector arguments yet!");
}
@ -2917,7 +2933,6 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
// Set up the return result vector.
Ops.clear();
const FunctionType *FTy = F.getFunctionType();
unsigned i = 0;
unsigned Idx = 1;
for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
@ -2984,13 +2999,13 @@ TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
/// ExpandScalarCallArgs - Recursively expand call argument node by
/// bit_converting it or extract a pair of elements from the larger node.
static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg,
bool isSigned,
unsigned Flags,
SmallVector<SDOperand, 32> &Ops,
SelectionDAG &DAG,
TargetLowering &TLI) {
if (TLI.getTypeAction(VT) != TargetLowering::Expand) {
Ops.push_back(Arg);
Ops.push_back(DAG.getConstant(isSigned, MVT::i32));
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
return;
}
@ -2998,7 +3013,7 @@ static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg,
unsigned NumVals = MVT::getSizeInBits(VT) / MVT::getSizeInBits(EVT);
if (NumVals == 1) {
Arg = DAG.getNode(ISD::BIT_CONVERT, EVT, Arg);
ExpandScalarCallArgs(EVT, Arg, isSigned, Ops, DAG, TLI);
ExpandScalarCallArgs(EVT, Arg, Flags, Ops, DAG, TLI);
} else if (NumVals == 2) {
SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, EVT, Arg,
DAG.getConstant(0, TLI.getPointerTy()));
@ -3006,8 +3021,8 @@ static void ExpandScalarCallArgs(MVT::ValueType VT, SDOperand Arg,
DAG.getConstant(1, TLI.getPointerTy()));
if (!TLI.isLittleEndian())
std::swap(Lo, Hi);
ExpandScalarCallArgs(EVT, Lo, isSigned, Ops, DAG, TLI);
ExpandScalarCallArgs(EVT, Hi, isSigned, Ops, DAG, TLI);
ExpandScalarCallArgs(EVT, Lo, Flags, Ops, DAG, TLI);
ExpandScalarCallArgs(EVT, Hi, Flags, Ops, DAG, TLI);
} else {
// Value scalarized into many values. Unimp for now.
assert(0 && "Cannot expand i64 -> i16 yet!");
@ -3036,11 +3051,14 @@ TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
MVT::ValueType VT = getValueType(Args[i].Ty);
SDOperand Op = Args[i].Node;
bool isSigned = Args[i].isSigned;
bool isInReg = Args[i].isInReg;
bool isSRet = Args[i].isSRet;
unsigned Flags = (isSRet << 2) | (isInReg << 1) | isSigned;
switch (getTypeAction(VT)) {
default: assert(0 && "Unknown type action!");
case Legal:
Ops.push_back(Op);
Ops.push_back(DAG.getConstant(isSigned, MVT::i32));
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
break;
case Promote:
if (MVT::isInteger(VT)) {
@ -3051,14 +3069,14 @@ TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
Op = DAG.getNode(ISD::FP_EXTEND, getTypeToTransformTo(VT), Op);
}
Ops.push_back(Op);
Ops.push_back(DAG.getConstant(isSigned, MVT::i32));
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
break;
case Expand:
if (VT != MVT::Vector) {
// If this is a large integer, it needs to be broken down into small
// integers. Figure out what the source elt type is and how many small
// integers it is.
ExpandScalarCallArgs(VT, Op, isSigned, Ops, DAG, *this);
ExpandScalarCallArgs(VT, Op, Flags, Ops, DAG, *this);
} else {
// Otherwise, this is a vector type. We only support legal vectors
// right now.
@ -3073,7 +3091,7 @@ TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
// Insert a VBIT_CONVERT of the MVT::Vector type to the packed type.
Op = DAG.getNode(ISD::VBIT_CONVERT, TVT, Op);
Ops.push_back(Op);
Ops.push_back(DAG.getConstant(isSigned, MVT::i32));
Ops.push_back(DAG.getConstant(Flags, MVT::i32));
} else {
assert(0 && "Don't support illegal by-val vector call args yet!");
abort();

View File

@ -352,7 +352,6 @@ SDOperand ARMTargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
SDOperand Chain = Op.getOperand(0);
unsigned CallConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
assert((CallConv == CallingConv::C ||
CallConv == CallingConv::CSRet ||
CallConv == CallingConv::Fast) && "unknown calling convention");
SDOperand Callee = Op.getOperand(4);
unsigned NumOps = (Op.getNumOperands() - 5) / 2;

View File

@ -1748,8 +1748,8 @@ void CWriter::printContainedStructs(const Type *Ty,
}
void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
/// isCStructReturn - Should this function actually return a struct by-value?
bool isCStructReturn = F->getCallingConv() == CallingConv::CSRet;
/// isStructReturn - Should this function actually return a struct by-value?
bool isStructReturn = F->getFunctionType()->isStructReturn();
if (F->hasInternalLinkage()) Out << "static ";
if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
@ -1778,7 +1778,7 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
// If this is a struct-return function, don't print the hidden
// struct-return argument.
if (isCStructReturn) {
if (isStructReturn) {
assert(I != E && "Invalid struct return function!");
++I;
}
@ -1804,7 +1804,7 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
// If this is a struct-return function, don't print the hidden
// struct-return argument.
if (isCStructReturn) {
if (isStructReturn) {
assert(I != E && "Invalid struct return function!");
++I;
}
@ -1832,7 +1832,7 @@ void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
// Get the return tpe for the function.
const Type *RetTy;
if (!isCStructReturn)
if (!isStructReturn)
RetTy = F->getReturnType();
else {
// If this is a struct-return function, print the struct-return type.
@ -1855,11 +1855,14 @@ static inline bool isFPIntBitCast(const Instruction &I) {
}
void CWriter::printFunction(Function &F) {
/// isStructReturn - Should this function actually return a struct by-value?
bool isStructReturn = F.getFunctionType()->isStructReturn();
printFunctionSignature(&F, false);
Out << " {\n";
// If this is a struct return function, handle the result with magic.
if (F.getCallingConv() == CallingConv::CSRet) {
if (isStructReturn) {
const Type *StructTy =
cast<PointerType>(F.arg_begin()->getType())->getElementType();
Out << " ";
@ -1977,7 +1980,10 @@ void CWriter::printBasicBlock(BasicBlock *BB) {
//
void CWriter::visitReturnInst(ReturnInst &I) {
// If this is a struct return function, return the temporary struct.
if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) {
bool isStructReturn = I.getParent()->getParent()->
getFunctionType()->isStructReturn();
if (isStructReturn) {
Out << " return StructReturn;\n";
return;
}
@ -2468,9 +2474,12 @@ void CWriter::visitCallInst(CallInst &I) {
Value *Callee = I.getCalledValue();
const PointerType *PTy = cast<PointerType>(Callee->getType());
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
// If this is a call to a struct-return function, assign to the first
// parameter instead of passing it to the call.
bool isStructRet = I.getCallingConv() == CallingConv::CSRet;
bool isStructRet = FTy->isStructReturn();
if (isStructRet) {
Out << "*(";
writeOperand(I.getOperand(1));
@ -2478,9 +2487,6 @@ void CWriter::visitCallInst(CallInst &I) {
}
if (I.isTailCall()) Out << " /*tail*/ ";
const PointerType *PTy = cast<PointerType>(Callee->getType());
const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
if (!WroteCallee) {
// If this is an indirect call to a struct return function, we need to cast

View File

@ -1360,9 +1360,9 @@ static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
// On PPC64, promote integers to 64-bit values.
if (isPPC64 && Arg.getValueType() == MVT::i32) {
unsigned ExtOp = ISD::ZERO_EXTEND;
if (cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue())
ExtOp = ISD::SIGN_EXTEND;
unsigned Flags = cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
unsigned ExtOp = (Flags & 1) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Arg = DAG.getNode(ExtOp, MVT::i64, Arg);
}

View File

@ -71,6 +71,10 @@ void X86SharedAsmPrinter::decorateName(std::string &Name,
unsigned CC = F->getCallingConv();
if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
return;
// Decorate names only when we're targeting Cygwin/Mingw32 targets
if (!Subtarget->isTargetCygMing())
return;
FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);

File diff suppressed because it is too large Load Diff

View File

@ -364,25 +364,21 @@ namespace llvm {
/// X86ScalarSSE - Select between SSE2 or x87 floating point ops.
bool X86ScalarSSE;
// C Calling Convention implementation.
SDOperand LowerCCCArguments(SDOperand Op, SelectionDAG &DAG);
SDOperand LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG);
// C and StdCall Calling Convention implementation.
SDOperand LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
bool isStdCall = false);
SDOperand LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
bool isStdCall = false);
// X86-64 C Calling Convention implementation.
SDOperand LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG);
SDOperand LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG);
// Fast Calling Convention implementation.
SDOperand LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG);
// Fast and FastCall Calling Convention implementation.
SDOperand LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG,
bool isFastCall = false);
SDOperand LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
bool isFastCall);
// StdCall Calling Convention implementation.
SDOperand LowerStdCallCCArguments(SDOperand Op, SelectionDAG &DAG);
SDOperand LowerStdCallCCCallTo(SDOperand Op, SelectionDAG &DAG);
// FastCall Calling Convention implementation.
SDOperand LowerFastCallCCArguments(SDOperand Op, SelectionDAG &DAG);
bool isFastCall = false);
SDOperand LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG);
SDOperand LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG);

View File

@ -35,6 +35,7 @@ bool X86Subtarget::GVRequiresExtraLoad(const GlobalValue* GV,
const TargetMachine& TM,
bool isDirectCall) const
{
// FIXME: PIC
if (TM.getRelocationModel() != Reloc::Static)
if (isTargetDarwin()) {
return (!isDirectCall &&

View File

@ -230,9 +230,10 @@ static inline bool CallPassesValueThoughVararg(Instruction *Call,
// (used in a computation), MaybeLive (only passed as an argument to a call), or
// Dead (not used).
DAE::Liveness DAE::getArgumentLiveness(const Argument &A) {
// If this is the return value of a csret function, it's not really dead.
if (A.getParent()->getCallingConv() == CallingConv::CSRet &&
&*A.getParent()->arg_begin() == &A)
const FunctionType *FTy = A.getParent()->getFunctionType();
// If this is the return value of a struct function, it's not really dead.
if (FTy->isStructReturn() && &*A.getParent()->arg_begin() == &A)
return Live;
if (A.use_empty()) // First check, directly dead?

View File

@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/IPO.h"
@ -20,13 +21,15 @@ namespace {
class FunctionExtractorPass : public ModulePass {
Function *Named;
bool deleteFunc;
bool reLink;
public:
/// FunctionExtractorPass - If deleteFn is true, this pass deletes as the
/// specified function. Otherwise, it deletes as much of the module as
/// possible, except for the function specified.
///
FunctionExtractorPass(Function *F = 0, bool deleteFn = true)
: Named(F), deleteFunc(deleteFn) {}
FunctionExtractorPass(Function *F = 0, bool deleteFn = true,
bool relinkCallees = false)
: Named(F), deleteFunc(deleteFn), reLink(relinkCallees) {}
bool runOnModule(Module &M) {
if (Named == 0) {
@ -41,6 +44,23 @@ namespace {
}
bool deleteFunction() {
// If we're in relinking mode, set linkage of all internal callees to
// external. This will allow us extract function, and then - link
// everything together
if (reLink) {
for (Function::iterator B = Named->begin(), BE = Named->end();
B != BE; ++B) {
for (BasicBlock::iterator I = B->begin(), E = B->end();
I != E; ++I) {
if (CallInst* callInst = dyn_cast<CallInst>(&*I)) {
Function* Callee = callInst->getCalledFunction();
if (Callee && Callee->hasInternalLinkage())
Callee->setLinkage(GlobalValue::ExternalLinkage);
}
}
}
}
Named->setLinkage(GlobalValue::ExternalLinkage);
Named->deleteBody();
assert(Named->isExternal() && "This didn't make the function external!");
@ -113,6 +133,7 @@ namespace {
RegisterPass<FunctionExtractorPass> X("extract", "Function Extractor");
}
ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn) {
return new FunctionExtractorPass(F, deleteFn);
ModulePass *llvm::createFunctionExtractionPass(Function *F, bool deleteFn,
bool relinkCallees) {
return new FunctionExtractorPass(F, deleteFn, relinkCallees);
}

View File

@ -956,7 +956,6 @@ void AssemblyWriter::printFunction(const Function *F) {
// Print the calling convention.
switch (F->getCallingConv()) {
case CallingConv::C: break; // default
case CallingConv::CSRet: Out << "csretcc "; break;
case CallingConv::Fast: Out << "fastcc "; break;
case CallingConv::Cold: Out << "coldcc "; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
@ -1166,7 +1165,6 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
// Print the calling convention being used.
switch (CI->getCallingConv()) {
case CallingConv::C: break; // default
case CallingConv::CSRet: Out << " csretcc"; break;
case CallingConv::Fast: Out << " fastcc"; break;
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
@ -1209,7 +1207,6 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
// Print the calling convention being used.
switch (II->getCallingConv()) {
case CallingConv::C: break; // default
case CallingConv::CSRet: Out << " csretcc"; break;
case CallingConv::Fast: Out << " fastcc"; break;
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;

View File

@ -1089,6 +1089,10 @@ std::string FunctionType::getParamAttrsText(ParameterAttributes Attr) {
Result += "sext ";
if (Attr & NoReturnAttribute)
Result += "noreturn ";
if (Attr & InRegAttribute)
Result += "inreg ";
if (Attr & StructRetAttribute)
Result += "sret ";
return Result;
}

View File

@ -338,17 +338,17 @@ void Verifier::visitFunction(Function &F) {
F.getReturnType() == Type::VoidTy,
"Functions cannot return aggregate values!", &F);
Assert1(!FT->isStructReturn() ||
(FT->getReturnType() == Type::VoidTy &&
FT->getNumParams() > 0 && isa<PointerType>(FT->getParamType(0))),
"Invalid struct-return function!", &F);
// Check that this function meets the restrictions on this calling convention.
switch (F.getCallingConv()) {
default:
break;
case CallingConv::C:
break;
case CallingConv::CSRet:
Assert1(FT->getReturnType() == Type::VoidTy &&
FT->getNumParams() > 0 && isa<PointerType>(FT->getParamType(0)),
"Invalid struct-return function!", &F);
break;
case CallingConv::Fast:
case CallingConv::Cold:
case CallingConv::X86_FastCall:

View File

@ -42,6 +42,10 @@ Force("f", cl::desc("Overwrite output files"));
static cl::opt<bool>
DeleteFn("delete", cl::desc("Delete specified function from Module"));
static cl::opt<bool>
Relink("relink",
cl::desc("Turn external linkage for callees of function to delete"));
// ExtractFunc - The function to extract from the module... defaults to main.
static cl::opt<std::string>
ExtractFunc("func", cl::desc("Specify function to extract"), cl::init("main"),
@ -72,8 +76,9 @@ int main(int argc, char **argv) {
PassManager Passes;
Passes.add(new TargetData(M.get())); // Use correct TargetData
// Either isolate the function or delete it from the Module
Passes.add(createFunctionExtractionPass(F, DeleteFn));
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Passes.add(createFunctionExtractionPass(F, DeleteFn, Relink));
if (!DeleteFn)
Passes.add(createGlobalDCEPass()); // Delete unreachable globals
Passes.add(createFunctionResolvingPass()); // Delete prototypes
Passes.add(createDeadTypeEliminationPass()); // Remove dead types...