llvm.org GIT mirror llvm / afa3b6d
Add some convenience methods for querying attributes, and use them. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44403 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 11 years ago
15 changed file(s) with 83 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
2020 #include "llvm/GlobalValue.h"
2121 #include "llvm/BasicBlock.h"
2222 #include "llvm/Argument.h"
23 #include "llvm/ParameterAttributes.h"
2324 #include "llvm/Support/Annotation.h"
2425
2526 namespace llvm {
151152 /// @brief Set the parameter attributes.
152153 void setParamAttrs(const ParamAttrsList *attrs);
153154
155 /// @brief Determine whether the function has the given attribute.
156 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const {
157 return ParamAttrs && ParamAttrs->paramHasAttr(i, attr);
158 }
159
154160 /// @brief Determine if the function returns a structure.
155 bool isStructReturn() const;
161 bool isStructReturn() const {
162 return paramHasAttr(1, ParamAttr::StructRet);
163 }
156164
157165 /// deleteBody - This method deletes the body of the function, and converts
158166 /// the linkage to external.
1919
2020 #include "llvm/InstrTypes.h"
2121 #include "llvm/DerivedTypes.h"
22 #include "llvm/ParameterAttributes.h"
2223
2324 namespace llvm {
2425
922923 /// @brief Set the parameter attributes.
923924 void setParamAttrs(const ParamAttrsList *attrs);
924925
926 /// @brief Determine whether the call or the callee has the given attribute.
927 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
928
925929 /// @brief Determine if the call returns a structure.
926 bool isStructReturn() const;
930 bool isStructReturn() const {
931 // Be friendly and also check the callee.
932 return paramHasAttr(1, ParamAttr::StructRet);
933 }
927934
928935 /// getCalledFunction - Return the function being called by this instruction
929936 /// if it is a direct call. If it is a call through a function pointer,
17001707 /// @brief Set the parameter attributes.
17011708 void setParamAttrs(const ParamAttrsList *attrs);
17021709
1710 /// @brief Determine whether the call or the callee has the given attribute.
1711 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
1712
17031713 /// @brief Determine if the call returns a structure.
1704 bool isStructReturn() const;
1714 bool isStructReturn() const {
1715 // Be friendly and also check the callee.
1716 return paramHasAttr(1, ParamAttr::StructRet);
1717 }
17051718
17061719 /// getCalledFunction - Return the function called, or null if this is an
17071720 /// indirect function invocation.
2121
2222 #include "llvm/Instruction.h"
2323 #include "llvm/BasicBlock.h"
24 #include "llvm/ParameterAttributes.h"
2425
2526 namespace llvm {
2627
6162 /// the call.
6263 const ParamAttrsList *getParamAttrs() const;
6364 void setParamAttrs(const ParamAttrsList *PAL);
65
66 /// paramHasAttr - whether the call or the callee has the given attribute.
67 bool paramHasAttr(uint16_t i, ParameterAttributes attr) const;
6468
6569 /// getType - Return the type of the instruction that generated this call site
6670 ///
838838 return UnknownModRefBehavior;
839839 }
840840
841 const ParamAttrsList *Attrs = F->getParamAttrs();
842 if (Attrs && Attrs->paramHasAttr(0, ParamAttr::ReadNone))
841 if (F->paramHasAttr(0, ParamAttr::ReadNone))
843842 return DoesNotAccessMemory;
844 if (Attrs && Attrs->paramHasAttr(0, ParamAttr::ReadOnly))
843 if (F->paramHasAttr(0, ParamAttr::ReadOnly))
845844 return OnlyReadsMemory;
846845
847846 return UnknownModRefBehavior;
51605160 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
51615161 if (isVarArg) ParamTypeList.pop_back();
51625162
5163 ParamAttrsList *PAL = 0;
5163 const ParamAttrsList *PAL = 0;
51645164 if (!Attrs.empty())
51655165 PAL = ParamAttrsList::get(Attrs);
51665166
56635663 GEN_ERROR("Invalid number of parameters detected");
56645664 }
56655665
5666 ParamAttrsList *PAL = 0;
5666 const ParamAttrsList *PAL = 0;
56675667 if (!Attrs.empty())
56685668 PAL = ParamAttrsList::get(Attrs);
56695669
60936093 }
60946094
60956095 // Finish off the ParamAttrs and check them
6096 ParamAttrsList *PAL = 0;
6096 const ParamAttrsList *PAL = 0;
60976097 if (!Attrs.empty())
60986098 PAL = ParamAttrsList::get(Attrs);
60996099
22422242 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
22432243 if (isVarArg) ParamTypeList.pop_back();
22442244
2245 ParamAttrsList *PAL = 0;
2245 const ParamAttrsList *PAL = 0;
22462246 if (!Attrs.empty())
22472247 PAL = ParamAttrsList::get(Attrs);
22482248
26442644 GEN_ERROR("Invalid number of parameters detected");
26452645 }
26462646
2647 ParamAttrsList *PAL = 0;
2647 const ParamAttrsList *PAL = 0;
26482648 if (!Attrs.empty())
26492649 PAL = ParamAttrsList::get(Attrs);
26502650
29762976 }
29772977
29782978 // Finish off the ParamAttrs and check them
2979 ParamAttrsList *PAL = 0;
2979 const ParamAttrsList *PAL = 0;
29802980 if (!Attrs.empty())
29812981 PAL = ParamAttrsList::get(Attrs);
29822982
933933 TmpVT = TLI.getTypeToTransformTo(MVT::i32);
934934 else
935935 TmpVT = MVT::i32;
936 const ParamAttrsList *PAL = I.getParent()->getParent()->getParamAttrs();
936 const Function *F = I.getParent()->getParent();
937937 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
938 if (PAL && PAL->paramHasAttr(0, ParamAttr::SExt))
938 if (F->paramHasAttr(0, ParamAttr::SExt))
939939 ExtendKind = ISD::SIGN_EXTEND;
940 if (PAL && PAL->paramHasAttr(0, ParamAttr::ZExt))
940 if (F->paramHasAttr(0, ParamAttr::ZExt))
941941 ExtendKind = ISD::ZERO_EXTEND;
942942 RetOp = DAG.getNode(ExtendKind, TmpVT, RetOp);
943943 NewValues.push_back(RetOp);
38913891 /// integrated into SDISel.
38923892 std::vector
38933893 TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
3894 const ParamAttrsList *Attrs = F.getParamAttrs();
38953894 // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
38963895 std::vector Ops;
38973896 Ops.push_back(DAG.getRoot());
39103909
39113910 // FIXME: Distinguish between a formal with no [sz]ext attribute from one
39123911 // that is zero extended!
3913 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::ZExt))
3912 if (F.paramHasAttr(j, ParamAttr::ZExt))
39143913 Flags &= ~(ISD::ParamFlags::SExt);
3915 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::SExt))
3914 if (F.paramHasAttr(j, ParamAttr::SExt))
39163915 Flags |= ISD::ParamFlags::SExt;
3917 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::InReg))
3916 if (F.paramHasAttr(j, ParamAttr::InReg))
39183917 Flags |= ISD::ParamFlags::InReg;
3919 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::StructRet))
3918 if (F.paramHasAttr(j, ParamAttr::StructRet))
39203919 Flags |= ISD::ParamFlags::StructReturn;
3921 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::ByVal)) {
3920 if (F.paramHasAttr(j, ParamAttr::ByVal)) {
39223921 Flags |= ISD::ParamFlags::ByVal;
39233922 const PointerType *Ty = cast(I->getType());
39243923 const StructType *STy = cast(Ty->getElementType());
39283927 Flags |= (StructAlign << ISD::ParamFlags::ByValAlignOffs);
39293928 Flags |= (StructSize << ISD::ParamFlags::ByValSizeOffs);
39303929 }
3931 if (Attrs && Attrs->paramHasAttr(j, ParamAttr::Nest))
3930 if (F.paramHasAttr(j, ParamAttr::Nest))
39323931 Flags |= ISD::ParamFlags::Nest;
39333932 Flags |= (OriginalAlignment << ISD::ParamFlags::OrigAlignmentOffs);
39343933
39853984 case Promote: {
39863985 SDOperand Op(Result, i++);
39873986 if (MVT::isInteger(VT)) {
3988 if (Attrs && Attrs->paramHasAttr(Idx, ParamAttr::SExt))
3987 if (F.paramHasAttr(Idx, ParamAttr::SExt))
39893988 Op = DAG.getNode(ISD::AssertSext, Op.getValueType(), Op,
39903989 DAG.getValueType(VT));
3991 else if (Attrs && Attrs->paramHasAttr(Idx, ParamAttr::ZExt))
3990 else if (F.paramHasAttr(Idx, ParamAttr::ZExt))
39923991 Op = DAG.getNode(ISD::AssertZext, Op.getValueType(), Op,
39933992 DAG.getValueType(VT));
39943993 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
895895 e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
896896 Value *V = *i;
897897 ArgVals.push_back(getOperandValue(V, SF));
898 if (F) {
899 // Promote all integral types whose size is < sizeof(i32) into i32.
900 // We do this by zero or sign extending the value as appropriate
901 // according to the parameter attributes
902 const Type *Ty = V->getType();
903 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32))
904 if (const ParamAttrsList *PA = F->getParamAttrs())
905 if (PA->paramHasAttr(pNum, ParamAttr::ZExt))
906 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
907 else if (PA->paramHasAttr(pNum, ParamAttr::SExt))
908 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
909 }
898 // Promote all integral types whose size is < sizeof(i32) into i32.
899 // We do this by zero or sign extending the value as appropriate
900 // according to the parameter attributes
901 const Type *Ty = V->getType();
902 if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32))
903 if (CS.paramHasAttr(pNum, ParamAttr::ZExt))
904 ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
905 else if (CS.paramHasAttr(pNum, ParamAttr::SExt))
906 ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
910907 }
911908
912909 // To handle indirect calls, we must get the pointer value from the argument
13861386
13871387
13881388 void MSILWriter::printFunction(const Function& F) {
1389 const ParamAttrsList *Attrs = F.getParamAttrs();
1390 bool isSigned = Attrs && Attrs->paramHasAttr(0, ParamAttr::SExt);
1389 bool isSigned = F.paramHasAttr(0, ParamAttr::SExt);
13911390 Out << "\n.method static ";
13921391 Out << (F.hasInternalLinkage() ? "private " : "public ");
13931392 if (F.isVarArg()) Out << "vararg ";
13981397 unsigned ArgIdx = 1;
13991398 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I!=E;
14001399 ++I, ++ArgIdx) {
1401 isSigned = Attrs && Attrs->paramHasAttr(ArgIdx, ParamAttr::SExt);
1400 isSigned = F.paramHasAttr(ArgIdx, ParamAttr::SExt);
14021401 if (I!=F.arg_begin()) Out << ", ";
14031402 Out << getTypeName(I->getType(),isSigned) << getValueName(I);
14041403 }
8989 BB->getInstList().erase(II);
9090 }
9191
92 /// IsNoReturn - Return true if the specified call is to a no-return function.
93 static bool IsNoReturn(const CallInst *CI) {
94 if (const ParamAttrsList *Attrs = CI->getParamAttrs())
95 if (Attrs->paramHasAttr(0, ParamAttr::NoReturn))
96 return true;
97
98 if (const Function *Callee = CI->getCalledFunction())
99 if (const ParamAttrsList *Attrs = Callee->getParamAttrs())
100 if (Attrs->paramHasAttr(0, ParamAttr::NoReturn))
101 return true;
102
103 return false;
104 }
105
106 /// IsNoUnwind - Return true if the specified invoke is to a no-unwind function.
107 static bool IsNoUnwind(const InvokeInst *II) {
108 if (const ParamAttrsList *Attrs = II->getParamAttrs())
109 if (Attrs->paramHasAttr(0, ParamAttr::NoUnwind))
110 return true;
111
112 if (const Function *Callee = II->getCalledFunction())
113 if (const ParamAttrsList *Attrs = Callee->getParamAttrs())
114 if (Attrs->paramHasAttr(0, ParamAttr::NoUnwind))
115 return true;
116
117 return false;
118 }
119
120
12192 static bool MarkAliveBlocks(BasicBlock *BB,
12293 SmallPtrSet &Reachable) {
12394
136107 // canonicalizes unreachable insts into stores to null or undef.
137108 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E;++BBI){
138109 if (CallInst *CI = dyn_cast(BBI)) {
139 if (IsNoReturn(CI)) {
110 if (CI->paramHasAttr(0, ParamAttr::NoReturn)) {
140111 // If we found a call to a no-return function, insert an unreachable
141112 // instruction after it. Make sure there isn't *already* one there
142113 // though.
160131
161132 // Turn invokes that call 'nounwind' functions into ordinary calls.
162133 if (InvokeInst *II = dyn_cast(BB->getTerminator()))
163 if (IsNoUnwind(II)) {
134 if (II->paramHasAttr(0, ParamAttr::NoUnwind)) {
164135 ChangeToCall(II);
165136 Changed = true;
166137 }
1212
1313 #include "llvm/Module.h"
1414 #include "llvm/DerivedTypes.h"
15 #include "llvm/ParameterAttributes.h"
1615 #include "llvm/IntrinsicInst.h"
1716 #include "llvm/CodeGen/ValueTypes.h"
1817 #include "llvm/Support/LeakDetector.h"
286285 ParamAttrs = attrs;
287286 }
288287
289 bool Function::isStructReturn() const {
290 if (ParamAttrs)
291 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
292 return false;
293 }
294
295288 const FunctionType *Function::getFunctionType() const {
296289 return cast(getType()->getElementType());
297290 }
4545 CI->setParamAttrs(PAL);
4646 else
4747 cast(I)->setParamAttrs(PAL);
48 }
49 bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
50 if (CallInst *CI = dyn_cast(I))
51 return CI->paramHasAttr(i, attr);
52 else
53 return cast(I)->paramHasAttr(i, attr);
4854 }
4955
5056
375381 ParamAttrs = newAttrs;
376382 }
377383
378 bool CallInst::isStructReturn() const {
379 if (ParamAttrs)
380 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
381 return false;
382 }
384 bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
385 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
386 return true;
387 const Function *F = getCalledFunction();
388 return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr);
389 }
390
383391
384392 //===----------------------------------------------------------------------===//
385393 // InvokeInst Implementation
450458 ParamAttrs = newAttrs;
451459 }
452460
453 bool InvokeInst::isStructReturn() const {
454 if (ParamAttrs)
455 return ParamAttrs->paramHasAttr(1, ParamAttr::StructRet);
456 return false;
457 }
461 bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
462 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
463 return true;
464 const Function *F = getCalledFunction();
465 return F && F->getParamAttrs() && F->getParamAttrs()->paramHasAttr(i, attr);
466 }
467
458468
459469 //===----------------------------------------------------------------------===//
460470 // ReturnInst Implementation
22 declare i32 @func(i8*) nounwind
33
44 define i32 @test() {
5 invoke i32 @func( i8* null ) nounwind
5 invoke i32 @func( i8* null )
66 to label %Cont unwind label %Other ; :1 [#uses=0]
77
88 Cont: ; preds = %0
45094509 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
45104510 if (isVarArg) Params.pop_back();
45114511
4512 ParamAttrsList *PAL = 0;
4512 const ParamAttrsList *PAL = 0;
45134513 if (lastCallingConv == OldCallingConv::CSRet) {
45144514 ParamAttrsVector Attrs;
45154515 ParamAttrsWithIndex PAWI;
20542054 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
20552055 if (isVarArg) Params.pop_back();
20562056
2057 ParamAttrsList *PAL = 0;
2057 const ParamAttrsList *PAL = 0;
20582058 if (lastCallingConv == OldCallingConv::CSRet) {
20592059 ParamAttrsVector Attrs;
20602060 ParamAttrsWithIndex PAWI;