Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 1 | //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGCall.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompileOptions.h" |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 26 | #include "llvm/Attributes.h" |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 30 | |
| 31 | #include "ABIInfo.h" |
| 32 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | using namespace CodeGen; |
| 35 | |
| 36 | /***/ |
| 37 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 38 | // FIXME: Use iterator and sidestep silly type array creation. |
| 39 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 40 | const |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 41 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 42 | return getFunctionInfo(FTNP->getResultType(), |
| 43 | llvm::SmallVector<QualType, 16>()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 46 | const |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 47 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) { |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 48 | llvm::SmallVector<QualType, 16> ArgTys; |
| 49 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 50 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 51 | ArgTys.push_back(FTP->getArgType(i)); |
| 52 | return getFunctionInfo(FTP->getResultType(), ArgTys); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 55 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { |
| 56 | llvm::SmallVector<QualType, 16> ArgTys; |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 57 | // Add the 'this' pointer unless this is a static method. |
| 58 | if (MD->isInstance()) |
| 59 | ArgTys.push_back(MD->getThisType(Context)); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 60 | |
| 61 | const FunctionProtoType *FTP = MD->getType()->getAsFunctionProtoType(); |
| 62 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 63 | ArgTys.push_back(FTP->getArgType(i)); |
| 64 | return getFunctionInfo(FTP->getResultType(), ArgTys); |
| 65 | } |
| 66 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 67 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Chris Lattner | 3eb67ca | 2009-05-12 20:27:19 +0000 | [diff] [blame] | 68 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 69 | if (MD->isInstance()) |
| 70 | return getFunctionInfo(MD); |
Anders Carlsson | f6f8ae5 | 2009-04-03 22:48:58 +0000 | [diff] [blame] | 71 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 72 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 73 | if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy)) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 74 | return getFunctionInfo(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 75 | return getFunctionInfo(cast<FunctionNoProtoType>(FTy)); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 78 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
| 79 | llvm::SmallVector<QualType, 16> ArgTys; |
| 80 | ArgTys.push_back(MD->getSelfDecl()->getType()); |
| 81 | ArgTys.push_back(Context.getObjCSelType()); |
| 82 | // FIXME: Kill copy? |
Chris Lattner | 2073216 | 2009-02-20 06:23:21 +0000 | [diff] [blame] | 83 | for (ObjCMethodDecl::param_iterator i = MD->param_begin(), |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 84 | e = MD->param_end(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 85 | ArgTys.push_back((*i)->getType()); |
| 86 | return getFunctionInfo(MD->getResultType(), ArgTys); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 89 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 90 | const CallArgList &Args) { |
| 91 | // FIXME: Kill copy. |
| 92 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 93 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 94 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 95 | ArgTys.push_back(i->second); |
| 96 | return getFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 99 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 100 | const FunctionArgList &Args) { |
| 101 | // FIXME: Kill copy. |
| 102 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 103 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 104 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 105 | ArgTys.push_back(i->second); |
| 106 | return getFunctionInfo(ResTy, ArgTys); |
| 107 | } |
| 108 | |
| 109 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 110 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 111 | // Lookup or create unique function info. |
| 112 | llvm::FoldingSetNodeID ID; |
| 113 | CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); |
| 114 | |
| 115 | void *InsertPos = 0; |
| 116 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 117 | if (FI) |
| 118 | return *FI; |
| 119 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 120 | // Construct the function info. |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 121 | FI = new CGFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 122 | FunctionInfos.InsertNode(FI, InsertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 123 | |
| 124 | // Compute ABI information. |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 125 | getABIInfo().computeInfo(*FI, getContext()); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 127 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /***/ |
| 131 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 132 | ABIInfo::~ABIInfo() {} |
| 133 | |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 134 | void ABIArgInfo::dump() const { |
| 135 | fprintf(stderr, "(ABIArgInfo Kind="); |
| 136 | switch (TheKind) { |
| 137 | case Direct: |
| 138 | fprintf(stderr, "Direct"); |
| 139 | break; |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 140 | case Ignore: |
| 141 | fprintf(stderr, "Ignore"); |
| 142 | break; |
| 143 | case Coerce: |
| 144 | fprintf(stderr, "Coerce Type="); |
| 145 | getCoerceToType()->print(llvm::errs()); |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 146 | break; |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 147 | case Indirect: |
| 148 | fprintf(stderr, "Indirect Align=%d", getIndirectAlign()); |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 149 | break; |
| 150 | case Expand: |
| 151 | fprintf(stderr, "Expand"); |
| 152 | break; |
| 153 | } |
| 154 | fprintf(stderr, ")\n"); |
| 155 | } |
| 156 | |
| 157 | /***/ |
| 158 | |
Daniel Dunbar | 573b907 | 2009-05-11 18:58:49 +0000 | [diff] [blame] | 159 | static bool isEmptyRecord(ASTContext &Context, QualType T); |
| 160 | |
| 161 | /// isEmptyField - Return true iff a the field is "empty", that is it |
| 162 | /// is an unnamed bit-field or an (array of) empty record(s). |
| 163 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD) { |
| 164 | if (FD->isUnnamedBitfield()) |
| 165 | return true; |
| 166 | |
| 167 | QualType FT = FD->getType(); |
Daniel Dunbar | cc401dc | 2009-05-11 23:01:34 +0000 | [diff] [blame] | 168 | // Constant arrays of empty records count as empty, strip them off. |
| 169 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) |
| 170 | FT = AT->getElementType(); |
Daniel Dunbar | 573b907 | 2009-05-11 18:58:49 +0000 | [diff] [blame] | 171 | |
| 172 | return isEmptyRecord(Context, FT); |
| 173 | } |
| 174 | |
| 175 | /// isEmptyRecord - Return true iff a structure contains only empty |
| 176 | /// fields. Note that a structure with a flexible array member is not |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 177 | /// considered empty. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 178 | static bool isEmptyRecord(ASTContext &Context, QualType T) { |
Daniel Dunbar | 5bde6f4 | 2009-03-31 19:01:39 +0000 | [diff] [blame] | 179 | const RecordType *RT = T->getAsRecordType(); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 180 | if (!RT) |
| 181 | return 0; |
| 182 | const RecordDecl *RD = RT->getDecl(); |
| 183 | if (RD->hasFlexibleArrayMember()) |
| 184 | return false; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 185 | for (RecordDecl::field_iterator i = RD->field_begin(Context), |
Daniel Dunbar | 573b907 | 2009-05-11 18:58:49 +0000 | [diff] [blame] | 186 | e = RD->field_end(Context); i != e; ++i) |
| 187 | if (!isEmptyField(Context, *i)) |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 188 | return false; |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
| 192 | /// isSingleElementStruct - Determine if a structure is a "single |
| 193 | /// element struct", i.e. it has exactly one non-empty field or |
| 194 | /// exactly one field which is itself a single element |
| 195 | /// struct. Structures with flexible array members are never |
| 196 | /// considered single element structs. |
| 197 | /// |
| 198 | /// \return The field declaration for the single non-empty field, if |
| 199 | /// it exists. |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 200 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 201 | const RecordType *RT = T->getAsStructureType(); |
| 202 | if (!RT) |
| 203 | return 0; |
| 204 | |
| 205 | const RecordDecl *RD = RT->getDecl(); |
| 206 | if (RD->hasFlexibleArrayMember()) |
| 207 | return 0; |
| 208 | |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 209 | const Type *Found = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 210 | for (RecordDecl::field_iterator i = RD->field_begin(Context), |
| 211 | e = RD->field_end(Context); i != e; ++i) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 212 | const FieldDecl *FD = *i; |
| 213 | QualType FT = FD->getType(); |
| 214 | |
Daniel Dunbar | 573b907 | 2009-05-11 18:58:49 +0000 | [diff] [blame] | 215 | // Ignore empty fields. |
| 216 | if (isEmptyField(Context, FD)) |
| 217 | continue; |
| 218 | |
Daniel Dunbar | cc401dc | 2009-05-11 23:01:34 +0000 | [diff] [blame] | 219 | // If we already found an element then this isn't a single-element |
| 220 | // struct. |
Daniel Dunbar | fcab2ca | 2009-05-08 21:04:47 +0000 | [diff] [blame] | 221 | if (Found) |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 222 | return 0; |
Daniel Dunbar | fcab2ca | 2009-05-08 21:04:47 +0000 | [diff] [blame] | 223 | |
Daniel Dunbar | cc401dc | 2009-05-11 23:01:34 +0000 | [diff] [blame] | 224 | // Treat single element arrays as the element. |
| 225 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 226 | if (AT->getSize().getZExtValue() != 1) |
| 227 | break; |
| 228 | FT = AT->getElementType(); |
| 229 | } |
| 230 | |
Daniel Dunbar | fcab2ca | 2009-05-08 21:04:47 +0000 | [diff] [blame] | 231 | if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 232 | Found = FT.getTypePtr(); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 233 | } else { |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 234 | Found = isSingleElementStruct(FT, Context); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 235 | if (!Found) |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return Found; |
| 241 | } |
| 242 | |
| 243 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 244 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 245 | return false; |
| 246 | |
| 247 | uint64_t Size = Context.getTypeSize(Ty); |
| 248 | return Size == 32 || Size == 64; |
| 249 | } |
| 250 | |
| 251 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 252 | ASTContext &Context) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 253 | for (RecordDecl::field_iterator i = RD->field_begin(Context), |
| 254 | e = RD->field_end(Context); i != e; ++i) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 255 | const FieldDecl *FD = *i; |
| 256 | |
| 257 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 258 | return false; |
| 259 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 260 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 261 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 262 | // counts as "basic" is more complicated than what we were doing previously. |
Daniel Dunbar | e06a75f | 2009-03-11 22:05:26 +0000 | [diff] [blame] | 263 | if (FD->isBitField()) |
| 264 | return false; |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 265 | } |
Daniel Dunbar | e06a75f | 2009-03-11 22:05:26 +0000 | [diff] [blame] | 266 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 270 | namespace { |
| 271 | /// DefaultABIInfo - The default implementation for ABI specific |
| 272 | /// details. This implementation provides information which results in |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 273 | /// self-consistent and sensible LLVM IR generation, but does not |
| 274 | /// conform to any particular ABI. |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 275 | class DefaultABIInfo : public ABIInfo { |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 276 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 277 | ASTContext &Context) const; |
| 278 | |
| 279 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 280 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 281 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 282 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 283 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 284 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 285 | it != ie; ++it) |
| 286 | it->info = classifyArgumentType(it->type, Context); |
| 287 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 288 | |
| 289 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 290 | CodeGenFunction &CGF) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 294 | class X86_32ABIInfo : public ABIInfo { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 295 | ASTContext &Context; |
Eli Friedman | 9fd58e8 | 2009-03-23 23:26:24 +0000 | [diff] [blame] | 296 | bool IsDarwin; |
| 297 | |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 298 | static bool isRegisterSize(unsigned Size) { |
| 299 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 300 | } |
| 301 | |
Daniel Dunbar | cf6bde3 | 2009-04-01 07:45:00 +0000 | [diff] [blame] | 302 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context); |
| 303 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 304 | public: |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 305 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 306 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 307 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 308 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 309 | ASTContext &Context) const; |
| 310 | |
| 311 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 312 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 313 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 314 | it != ie; ++it) |
| 315 | it->info = classifyArgumentType(it->type, Context); |
| 316 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 317 | |
| 318 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 319 | CodeGenFunction &CGF) const; |
Eli Friedman | 9fd58e8 | 2009-03-23 23:26:24 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 321 | X86_32ABIInfo(ASTContext &Context, bool d) |
| 322 | : ABIInfo(), Context(Context), IsDarwin(d) {} |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 323 | }; |
| 324 | } |
| 325 | |
Daniel Dunbar | cf6bde3 | 2009-04-01 07:45:00 +0000 | [diff] [blame] | 326 | |
| 327 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 328 | /// passed in a register (for the Darwin ABI). |
| 329 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 330 | ASTContext &Context) { |
| 331 | uint64_t Size = Context.getTypeSize(Ty); |
| 332 | |
| 333 | // Type must be register sized. |
| 334 | if (!isRegisterSize(Size)) |
| 335 | return false; |
| 336 | |
| 337 | if (Ty->isVectorType()) { |
| 338 | // 64- and 128- bit vectors inside structures are not returned in |
| 339 | // registers. |
| 340 | if (Size == 64 || Size == 128) |
| 341 | return false; |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | // If this is a builtin, pointer, or complex type, it is ok. |
| 347 | if (Ty->getAsBuiltinType() || Ty->isPointerType() || Ty->isAnyComplexType()) |
| 348 | return true; |
| 349 | |
| 350 | // Arrays are treated like records. |
| 351 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
| 352 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
| 353 | |
| 354 | // Otherwise, it must be a record type. |
| 355 | const RecordType *RT = Ty->getAsRecordType(); |
| 356 | if (!RT) return false; |
| 357 | |
| 358 | // Structure types are passed in register if all fields would be |
| 359 | // passed in a register. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 360 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(Context), |
| 361 | e = RT->getDecl()->field_end(Context); i != e; ++i) { |
Daniel Dunbar | cf6bde3 | 2009-04-01 07:45:00 +0000 | [diff] [blame] | 362 | const FieldDecl *FD = *i; |
| 363 | |
Daniel Dunbar | 573b907 | 2009-05-11 18:58:49 +0000 | [diff] [blame] | 364 | // Empty fields are ignored. |
| 365 | if (isEmptyField(Context, FD)) |
Daniel Dunbar | cf6bde3 | 2009-04-01 07:45:00 +0000 | [diff] [blame] | 366 | continue; |
| 367 | |
| 368 | // Check fields recursively. |
| 369 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 376 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 377 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 378 | if (RetTy->isVoidType()) { |
| 379 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 3604316 | 2009-04-01 06:13:08 +0000 | [diff] [blame] | 380 | } else if (const VectorType *VT = RetTy->getAsVectorType()) { |
| 381 | // On Darwin, some vectors are returned in registers. |
| 382 | if (IsDarwin) { |
| 383 | uint64_t Size = Context.getTypeSize(RetTy); |
| 384 | |
| 385 | // 128-bit vectors are a special case; they are returned in |
| 386 | // registers and we need to make sure to pick a type the LLVM |
| 387 | // backend will like. |
| 388 | if (Size == 128) |
| 389 | return ABIArgInfo::getCoerce(llvm::VectorType::get(llvm::Type::Int64Ty, |
| 390 | 2)); |
| 391 | |
| 392 | // Always return in register if it fits in a general purpose |
| 393 | // register, or if it is 64 bits and has a single element. |
| 394 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 395 | (Size == 64 && VT->getNumElements() == 1)) |
| 396 | return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size)); |
| 397 | |
| 398 | return ABIArgInfo::getIndirect(0); |
| 399 | } |
| 400 | |
| 401 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 402 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 8e03444 | 2009-04-27 18:31:32 +0000 | [diff] [blame] | 403 | // Structures with flexible arrays are always indirect. |
| 404 | if (const RecordType *RT = RetTy->getAsStructureType()) |
| 405 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 406 | return ABIArgInfo::getIndirect(0); |
| 407 | |
Eli Friedman | 9fd58e8 | 2009-03-23 23:26:24 +0000 | [diff] [blame] | 408 | // Outside of Darwin, structs and unions are always indirect. |
| 409 | if (!IsDarwin && !RetTy->isAnyComplexType()) |
| 410 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 8e03444 | 2009-04-27 18:31:32 +0000 | [diff] [blame] | 411 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 412 | // Classify "single element" structs as their element type. |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 413 | if (const Type *SeltTy = isSingleElementStruct(RetTy, Context)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 414 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 415 | if (BT->isIntegerType()) { |
Daniel Dunbar | 2e00116 | 2009-05-08 21:30:11 +0000 | [diff] [blame] | 416 | // We need to use the size of the structure, padding |
| 417 | // bit-fields can adjust that to be larger than the single |
| 418 | // element type. |
| 419 | uint64_t Size = Context.getTypeSize(RetTy); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 420 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 421 | } else if (BT->getKind() == BuiltinType::Float) { |
Daniel Dunbar | 2e00116 | 2009-05-08 21:30:11 +0000 | [diff] [blame] | 422 | assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) && |
| 423 | "Unexpect single element structure size!"); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 424 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 425 | } else if (BT->getKind() == BuiltinType::Double) { |
Daniel Dunbar | 2e00116 | 2009-05-08 21:30:11 +0000 | [diff] [blame] | 426 | assert(Context.getTypeSize(RetTy) == Context.getTypeSize(SeltTy) && |
| 427 | "Unexpect single element structure size!"); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 428 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 429 | } |
| 430 | } else if (SeltTy->isPointerType()) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 431 | // FIXME: It would be really nice if this could come out as the proper |
| 432 | // pointer type. |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 433 | llvm::Type *PtrTy = |
| 434 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 435 | return ABIArgInfo::getCoerce(PtrTy); |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 436 | } else if (SeltTy->isVectorType()) { |
| 437 | // 64- and 128-bit vectors are never returned in a |
| 438 | // register when inside a structure. |
| 439 | uint64_t Size = Context.getTypeSize(RetTy); |
| 440 | if (Size == 64 || Size == 128) |
| 441 | return ABIArgInfo::getIndirect(0); |
| 442 | |
| 443 | return classifyReturnType(QualType(SeltTy, 0), Context); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Daniel Dunbar | 836a064 | 2009-05-12 17:00:20 +0000 | [diff] [blame] | 447 | // Small structures which are register sized are generally returned |
| 448 | // in a register. |
| 449 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, Context)) { |
| 450 | uint64_t Size = Context.getTypeSize(RetTy); |
| 451 | return ABIArgInfo::getCoerce(llvm::IntegerType::get(Size)); |
Daniel Dunbar | cf6bde3 | 2009-04-01 07:45:00 +0000 | [diff] [blame] | 452 | } |
Daniel Dunbar | dfc6b80 | 2009-04-01 07:08:38 +0000 | [diff] [blame] | 453 | |
| 454 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 455 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 456 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 460 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 461 | ASTContext &Context) const { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 462 | // FIXME: Set alignment on indirect arguments. |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 463 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 464 | // Structures with flexible arrays are always indirect. |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 465 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 466 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 467 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 468 | |
Daniel Dunbar | 3170c93 | 2009-02-05 01:50:07 +0000 | [diff] [blame] | 469 | // Ignore empty structs. |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 470 | uint64_t Size = Context.getTypeSize(Ty); |
| 471 | if (Ty->isStructureType() && Size == 0) |
Daniel Dunbar | 3170c93 | 2009-02-05 01:50:07 +0000 | [diff] [blame] | 472 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 473 | |
| 474 | // Expand structs with size <= 128-bits which consist only of |
| 475 | // basic types (int, long long, float, double, xxx*). This is |
| 476 | // non-recursive and does not ignore empty fields. |
| 477 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 478 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 479 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 480 | return ABIArgInfo::getExpand(); |
| 481 | } |
| 482 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 483 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 484 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 485 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 489 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 490 | CodeGenFunction &CGF) const { |
| 491 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 492 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 493 | |
| 494 | CGBuilderTy &Builder = CGF.Builder; |
| 495 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 496 | "ap"); |
| 497 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 498 | llvm::Type *PTy = |
| 499 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 500 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 501 | |
Daniel Dunbar | 570f0cf | 2009-02-18 22:28:45 +0000 | [diff] [blame] | 502 | uint64_t Offset = |
| 503 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 504 | llvm::Value *NextAddr = |
| 505 | Builder.CreateGEP(Addr, |
Daniel Dunbar | 570f0cf | 2009-02-18 22:28:45 +0000 | [diff] [blame] | 506 | llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset), |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 507 | "ap.next"); |
| 508 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 509 | |
| 510 | return AddrTyped; |
| 511 | } |
| 512 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 513 | namespace { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 514 | /// X86_64ABIInfo - The X86_64 ABI information. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 515 | class X86_64ABIInfo : public ABIInfo { |
| 516 | enum Class { |
| 517 | Integer = 0, |
| 518 | SSE, |
| 519 | SSEUp, |
| 520 | X87, |
| 521 | X87Up, |
| 522 | ComplexX87, |
| 523 | NoClass, |
| 524 | Memory |
| 525 | }; |
| 526 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 527 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 528 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 529 | /// Merge an accumulating classification \arg Accum with a field |
| 530 | /// classification \arg Field. |
| 531 | /// |
| 532 | /// \param Accum - The accumulating classification. This should |
| 533 | /// always be either NoClass or the result of a previous merge |
| 534 | /// call. In addition, this should never be Memory (the caller |
| 535 | /// should just return Memory for the aggregate). |
| 536 | Class merge(Class Accum, Class Field) const; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 538 | /// classify - Determine the x86_64 register classes in which the |
| 539 | /// given type T should be passed. |
| 540 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 541 | /// \param Lo - The classification for the parts of the type |
| 542 | /// residing in the low word of the containing object. |
| 543 | /// |
| 544 | /// \param Hi - The classification for the parts of the type |
| 545 | /// residing in the high word of the containing object. |
| 546 | /// |
| 547 | /// \param OffsetBase - The bit offset of this type in the |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 548 | /// containing object. Some parameters are classified different |
| 549 | /// depending on whether they straddle an eightbyte boundary. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 550 | /// |
| 551 | /// If a word is unused its result will be NoClass; if a type should |
| 552 | /// be passed in Memory then at least the classification of \arg Lo |
| 553 | /// will be Memory. |
| 554 | /// |
| 555 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 556 | /// |
| 557 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 558 | /// also be ComplexX87. |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 559 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 560 | Class &Lo, Class &Hi) const; |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 561 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 562 | /// getCoerceResult - Given a source type \arg Ty and an LLVM type |
| 563 | /// to coerce to, chose the best way to pass Ty in the same place |
| 564 | /// that \arg CoerceTo would be passed, but while keeping the |
| 565 | /// emitted code as simple as possible. |
| 566 | /// |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 567 | /// FIXME: Note, this should be cleaned up to just take an enumeration of all |
| 568 | /// the ways we might want to pass things, instead of constructing an LLVM |
| 569 | /// type. This makes this code more explicit, and it makes it clearer that we |
| 570 | /// are also doing this for correctness in the case of passing scalar types. |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 571 | ABIArgInfo getCoerceResult(QualType Ty, |
| 572 | const llvm::Type *CoerceTo, |
| 573 | ASTContext &Context) const; |
| 574 | |
Daniel Dunbar | 86e13ee | 2009-05-26 16:37:37 +0000 | [diff] [blame] | 575 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 576 | /// such that the argument will be passed in memory. |
| 577 | ABIArgInfo getIndirectResult(QualType Ty, |
| 578 | ASTContext &Context) const; |
| 579 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 580 | ABIArgInfo classifyReturnType(QualType RetTy, |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 581 | ASTContext &Context) const; |
| 582 | |
| 583 | ABIArgInfo classifyArgumentType(QualType Ty, |
| 584 | ASTContext &Context, |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 585 | unsigned &neededInt, |
| 586 | unsigned &neededSSE) const; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 587 | |
| 588 | public: |
| 589 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const; |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 590 | |
| 591 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 592 | CodeGenFunction &CGF) const; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 593 | }; |
| 594 | } |
| 595 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 596 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 597 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 598 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 599 | // classified recursively so that always two fields are |
| 600 | // considered. The resulting class is calculated according to |
| 601 | // the classes of the fields in the eightbyte: |
| 602 | // |
| 603 | // (a) If both classes are equal, this is the resulting class. |
| 604 | // |
| 605 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 606 | // the other class. |
| 607 | // |
| 608 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 609 | // class. |
| 610 | // |
| 611 | // (d) If one of the classes is INTEGER, the result is the |
| 612 | // INTEGER. |
| 613 | // |
| 614 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 615 | // MEMORY is used as class. |
| 616 | // |
| 617 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | 100f402 | 2009-03-06 17:50:25 +0000 | [diff] [blame] | 618 | |
| 619 | // Accum should never be memory (we should have returned) or |
| 620 | // ComplexX87 (because this cannot be passed in a structure). |
| 621 | assert((Accum != Memory && Accum != ComplexX87) && |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 622 | "Invalid accumulated classification during merge."); |
| 623 | if (Accum == Field || Field == NoClass) |
| 624 | return Accum; |
| 625 | else if (Field == Memory) |
| 626 | return Memory; |
| 627 | else if (Accum == NoClass) |
| 628 | return Field; |
| 629 | else if (Accum == Integer || Field == Integer) |
| 630 | return Integer; |
Daniel Dunbar | 20e95c5 | 2009-05-12 15:22:40 +0000 | [diff] [blame] | 631 | else if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 632 | Accum == X87 || Accum == X87Up) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 633 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 634 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 635 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 638 | void X86_64ABIInfo::classify(QualType Ty, |
| 639 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 640 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 641 | Class &Lo, Class &Hi) const { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 642 | // FIXME: This code can be simplified by introducing a simple value class for |
| 643 | // Class pairs with appropriate constructor methods for the various |
| 644 | // situations. |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 645 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 646 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 647 | // shouldn't be passed in registers for example, so there is no chance they |
| 648 | // can straddle an eightbyte. Verify & simplify. |
Daniel Dunbar | e28099b | 2009-02-22 04:48:22 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 650 | Lo = Hi = NoClass; |
| 651 | |
| 652 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 653 | Current = Memory; |
| 654 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 655 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 656 | BuiltinType::Kind k = BT->getKind(); |
| 657 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 658 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 659 | Current = NoClass; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 660 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
Chris Lattner | ae69e00 | 2009-04-30 06:22:07 +0000 | [diff] [blame] | 661 | Lo = Integer; |
| 662 | Hi = Integer; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 663 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 664 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 665 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 666 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 667 | } else if (k == BuiltinType::LongDouble) { |
| 668 | Lo = X87; |
| 669 | Hi = X87Up; |
| 670 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 671 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 672 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Anders Carlsson | 708762b | 2009-02-26 17:31:15 +0000 | [diff] [blame] | 673 | } else if (const EnumType *ET = Ty->getAsEnumType()) { |
| 674 | // Classify the underlying integer type. |
| 675 | classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi); |
Daniel Dunbar | 8958891 | 2009-02-26 20:52:22 +0000 | [diff] [blame] | 676 | } else if (Ty->hasPointerRepresentation()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 677 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 678 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 679 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | e28099b | 2009-02-22 04:48:22 +0000 | [diff] [blame] | 680 | if (Size == 32) { |
| 681 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 682 | // float> as integer. |
| 683 | Current = Integer; |
| 684 | |
| 685 | // If this type crosses an eightbyte boundary, it should be |
| 686 | // split. |
| 687 | uint64_t EB_Real = (OffsetBase) / 64; |
| 688 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 689 | if (EB_Real != EB_Imag) |
| 690 | Hi = Lo; |
| 691 | } else if (Size == 64) { |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 692 | // gcc passes <1 x double> in memory. :( |
| 693 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 694 | return; |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 695 | |
| 696 | // gcc passes <1 x long long> as INTEGER. |
| 697 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 698 | Current = Integer; |
| 699 | else |
| 700 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 701 | |
| 702 | // If this type crosses an eightbyte boundary, it should be |
| 703 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 704 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 705 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 706 | } else if (Size == 128) { |
| 707 | Lo = SSE; |
| 708 | Hi = SSEUp; |
| 709 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 710 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 711 | QualType ET = Context.getCanonicalType(CT->getElementType()); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 713 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 714 | if (ET->isIntegralType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 715 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 716 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 717 | else if (Size <= 128) |
| 718 | Lo = Hi = Integer; |
| 719 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 720 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 721 | else if (ET == Context.DoubleTy) |
| 722 | Lo = Hi = SSE; |
| 723 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 724 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 725 | |
| 726 | // If this complex type crosses an eightbyte boundary then it |
| 727 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 728 | uint64_t EB_Real = (OffsetBase) / 64; |
| 729 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 730 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 731 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 732 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 733 | // Arrays are treated like structures. |
| 734 | |
| 735 | uint64_t Size = Context.getTypeSize(Ty); |
| 736 | |
| 737 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 738 | // than two eightbytes, ..., it has class MEMORY. |
| 739 | if (Size > 128) |
| 740 | return; |
| 741 | |
| 742 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 743 | // fields, it has class MEMORY. |
| 744 | // |
| 745 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 746 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 747 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 748 | |
| 749 | // Otherwise implement simplified merge. We could be smarter about |
| 750 | // this, but it isn't worth it and would be harder to verify. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 751 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 752 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 753 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 754 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 755 | Class FieldLo, FieldHi; |
| 756 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 757 | Lo = merge(Lo, FieldLo); |
| 758 | Hi = merge(Hi, FieldHi); |
| 759 | if (Lo == Memory || Hi == Memory) |
| 760 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 761 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 762 | |
| 763 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 764 | if (Hi == Memory) |
| 765 | Lo = Memory; |
| 766 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 767 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 768 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 769 | |
| 770 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 771 | // than two eightbytes, ..., it has class MEMORY. |
| 772 | if (Size > 128) |
| 773 | return; |
| 774 | |
| 775 | const RecordDecl *RD = RT->getDecl(); |
| 776 | |
| 777 | // Assume variable sized types are passed in memory. |
| 778 | if (RD->hasFlexibleArrayMember()) |
| 779 | return; |
| 780 | |
| 781 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 782 | |
| 783 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 784 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 785 | unsigned idx = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 786 | for (RecordDecl::field_iterator i = RD->field_begin(Context), |
| 787 | e = RD->field_end(Context); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 788 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 789 | bool BitField = i->isBitField(); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 790 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 791 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 792 | // fields, it has class MEMORY. |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 793 | // |
Daniel Dunbar | 8e03444 | 2009-04-27 18:31:32 +0000 | [diff] [blame] | 794 | // Note, skip this test for bit-fields, see below. |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 795 | if (!BitField && Offset % Context.getTypeAlign(i->getType())) { |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 796 | Lo = Memory; |
| 797 | return; |
| 798 | } |
| 799 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 800 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 801 | // |
| 802 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 803 | // exceeds a single eightbyte, each is classified |
| 804 | // separately. Each eightbyte gets initialized to class |
| 805 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 806 | Class FieldLo, FieldHi; |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 807 | |
Daniel Dunbar | 8e03444 | 2009-04-27 18:31:32 +0000 | [diff] [blame] | 808 | // Bit-fields require special handling, they do not force the |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 809 | // structure to be passed in memory even if unaligned, and |
| 810 | // therefore they can straddle an eightbyte. |
| 811 | if (BitField) { |
Daniel Dunbar | 8236bf1 | 2009-05-08 22:26:44 +0000 | [diff] [blame] | 812 | // Ignore padding bit-fields. |
| 813 | if (i->isUnnamedBitfield()) |
| 814 | continue; |
| 815 | |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 816 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 817 | uint64_t Size = i->getBitWidth()->EvaluateAsInt(Context).getZExtValue(); |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 818 | |
| 819 | uint64_t EB_Lo = Offset / 64; |
| 820 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 821 | FieldLo = FieldHi = NoClass; |
| 822 | if (EB_Lo) { |
| 823 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 824 | FieldLo = NoClass; |
| 825 | FieldHi = Integer; |
| 826 | } else { |
| 827 | FieldLo = Integer; |
| 828 | FieldHi = EB_Hi ? Integer : NoClass; |
| 829 | } |
| 830 | } else |
| 831 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 832 | Lo = merge(Lo, FieldLo); |
| 833 | Hi = merge(Hi, FieldHi); |
| 834 | if (Lo == Memory || Hi == Memory) |
| 835 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 839 | // |
| 840 | // (a) If one of the classes is MEMORY, the whole argument is |
| 841 | // passed in memory. |
| 842 | // |
| 843 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 844 | |
| 845 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 846 | // the merge (just bail). |
| 847 | // |
| 848 | // The second condition occurs in the case of unions; for example |
| 849 | // union { _Complex double; unsigned; }. |
| 850 | if (Hi == Memory) |
| 851 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 852 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 853 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 857 | ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty, |
| 858 | const llvm::Type *CoerceTo, |
| 859 | ASTContext &Context) const { |
| 860 | if (CoerceTo == llvm::Type::Int64Ty) { |
| 861 | // Integer and pointer types will end up in a general purpose |
| 862 | // register. |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 863 | if (Ty->isIntegralType() || Ty->isPointerType()) |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 864 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 866 | } else if (CoerceTo == llvm::Type::DoubleTy) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 867 | // FIXME: It would probably be better to make CGFunctionInfo only map using |
| 868 | // canonical types than to canonize here. |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 869 | QualType CTy = Context.getCanonicalType(Ty); |
| 870 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 871 | // Float and double end up in a single SSE reg. |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 872 | if (CTy == Context.FloatTy || CTy == Context.DoubleTy) |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 873 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 874 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | return ABIArgInfo::getCoerce(CoerceTo); |
| 878 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 879 | |
Daniel Dunbar | 86e13ee | 2009-05-26 16:37:37 +0000 | [diff] [blame] | 880 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 881 | ASTContext &Context) const { |
| 882 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 883 | // place naturally. |
| 884 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 885 | return ABIArgInfo::getDirect(); |
| 886 | |
| 887 | // FIXME: Set alignment correctly. |
| 888 | return ABIArgInfo::getIndirect(0); |
| 889 | } |
| 890 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 891 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 892 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 893 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 894 | // classification algorithm. |
| 895 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 896 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 897 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 898 | // Check some invariants. |
| 899 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 900 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 901 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 902 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 903 | const llvm::Type *ResType = 0; |
| 904 | switch (Lo) { |
| 905 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 906 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 907 | |
| 908 | case SSEUp: |
| 909 | case X87Up: |
| 910 | assert(0 && "Invalid classification for lo word."); |
| 911 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 912 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 913 | // hidden argument. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 914 | case Memory: |
Daniel Dunbar | 86e13ee | 2009-05-26 16:37:37 +0000 | [diff] [blame] | 915 | return getIndirectResult(RetTy, Context); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 916 | |
| 917 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 918 | // available register of the sequence %rax, %rdx is used. |
| 919 | case Integer: |
| 920 | ResType = llvm::Type::Int64Ty; break; |
| 921 | |
| 922 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 923 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 924 | case SSE: |
| 925 | ResType = llvm::Type::DoubleTy; break; |
| 926 | |
| 927 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 928 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 929 | case X87: |
| 930 | ResType = llvm::Type::X86_FP80Ty; break; |
| 931 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 932 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 933 | // part of the value is returned in %st0 and the imaginary part in |
| 934 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 935 | case ComplexX87: |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 936 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 937 | ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty, |
| 938 | llvm::Type::X86_FP80Ty, |
| 939 | NULL); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | |
| 943 | switch (Hi) { |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 944 | // Memory was handled previously and X87 should |
| 945 | // never occur as a hi class. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 946 | case Memory: |
| 947 | case X87: |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 948 | assert(0 && "Invalid classification for hi word."); |
| 949 | |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 950 | case ComplexX87: // Previously handled. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 951 | case NoClass: break; |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 952 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 953 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 954 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 955 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 956 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 957 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 958 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 959 | |
| 960 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 961 | // is passed in the upper half of the last used SSE register. |
| 962 | // |
| 963 | // SSEUP should always be preceeded by SSE, just widen. |
| 964 | case SSEUp: |
| 965 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 966 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 967 | break; |
| 968 | |
| 969 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 970 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 971 | case X87Up: |
Daniel Dunbar | 100f402 | 2009-03-06 17:50:25 +0000 | [diff] [blame] | 972 | // If X87Up is preceeded by X87, we don't need to do |
| 973 | // anything. However, in some cases with unions it may not be |
| 974 | // preceeded by X87. In such situations we follow gcc and pass the |
| 975 | // extra bits in an SSE reg. |
| 976 | if (Lo != X87) |
| 977 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 978 | break; |
| 979 | } |
| 980 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 981 | return getCoerceResult(RetTy, ResType, Context); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 984 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 985 | unsigned &neededInt, |
| 986 | unsigned &neededSSE) const { |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 987 | X86_64ABIInfo::Class Lo, Hi; |
| 988 | classify(Ty, Context, 0, Lo, Hi); |
| 989 | |
| 990 | // Check some invariants. |
| 991 | // FIXME: Enforce these by construction. |
| 992 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 993 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 994 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 995 | |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 996 | neededInt = 0; |
| 997 | neededSSE = 0; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 998 | const llvm::Type *ResType = 0; |
| 999 | switch (Lo) { |
| 1000 | case NoClass: |
| 1001 | return ABIArgInfo::getIgnore(); |
| 1002 | |
| 1003 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 1004 | // on the stack. |
| 1005 | case Memory: |
| 1006 | |
| 1007 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 1008 | // COMPLEX_X87, it is passed in memory. |
| 1009 | case X87: |
| 1010 | case ComplexX87: |
Daniel Dunbar | 86e13ee | 2009-05-26 16:37:37 +0000 | [diff] [blame] | 1011 | return getIndirectResult(Ty, Context); |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1012 | |
| 1013 | case SSEUp: |
| 1014 | case X87Up: |
| 1015 | assert(0 && "Invalid classification for lo word."); |
| 1016 | |
| 1017 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 1018 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 1019 | // and %r9 is used. |
| 1020 | case Integer: |
| 1021 | ++neededInt; |
| 1022 | ResType = llvm::Type::Int64Ty; |
| 1023 | break; |
| 1024 | |
| 1025 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 1026 | // available SSE register is used, the registers are taken in the |
| 1027 | // order from %xmm0 to %xmm7. |
| 1028 | case SSE: |
| 1029 | ++neededSSE; |
| 1030 | ResType = llvm::Type::DoubleTy; |
| 1031 | break; |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1032 | } |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1033 | |
| 1034 | switch (Hi) { |
| 1035 | // Memory was handled previously, ComplexX87 and X87 should |
| 1036 | // never occur as hi classes, and X87Up must be preceed by X87, |
| 1037 | // which is passed in memory. |
| 1038 | case Memory: |
| 1039 | case X87: |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1040 | case ComplexX87: |
| 1041 | assert(0 && "Invalid classification for hi word."); |
Daniel Dunbar | 100f402 | 2009-03-06 17:50:25 +0000 | [diff] [blame] | 1042 | break; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1043 | |
| 1044 | case NoClass: break; |
| 1045 | case Integer: |
| 1046 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 1047 | ++neededInt; |
| 1048 | break; |
Daniel Dunbar | 100f402 | 2009-03-06 17:50:25 +0000 | [diff] [blame] | 1049 | |
| 1050 | // X87Up generally doesn't occur here (long double is passed in |
| 1051 | // memory), except in situations involving unions. |
| 1052 | case X87Up: |
| 1053 | case SSE: |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1054 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 1055 | ++neededSSE; |
| 1056 | break; |
| 1057 | |
| 1058 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 1059 | // eightbyte is passed in the upper half of the last used SSE |
| 1060 | // register. |
| 1061 | case SSEUp: |
| 1062 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 1063 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 1064 | break; |
| 1065 | } |
| 1066 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 1067 | return getCoerceResult(Ty, ResType, Context); |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 1071 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 1072 | |
| 1073 | // Keep track of the number of assigned registers. |
| 1074 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Daniel Dunbar | 3a5f5c5 | 2009-05-22 17:33:44 +0000 | [diff] [blame] | 1075 | |
| 1076 | // If the return value is indirect, then the hidden argument is consuming one |
| 1077 | // integer register. |
| 1078 | if (FI.getReturnInfo().isIndirect()) |
| 1079 | --freeIntRegs; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1080 | |
| 1081 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 1082 | // get assigned (in left-to-right order) for passing as follows... |
| 1083 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 1084 | it != ie; ++it) { |
| 1085 | unsigned neededInt, neededSSE; |
| 1086 | it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE); |
| 1087 | |
| 1088 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 1089 | // eightbyte of an argument, the whole argument is passed on the |
| 1090 | // stack. If registers have already been assigned for some |
| 1091 | // eightbytes of such an argument, the assignments get reverted. |
| 1092 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
| 1093 | freeIntRegs -= neededInt; |
| 1094 | freeSSERegs -= neededSSE; |
| 1095 | } else { |
Daniel Dunbar | 86e13ee | 2009-05-26 16:37:37 +0000 | [diff] [blame] | 1096 | it->info = getIndirectResult(it->type, Context); |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 1097 | } |
| 1098 | } |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1101 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 1102 | QualType Ty, |
| 1103 | CodeGenFunction &CGF) { |
| 1104 | llvm::Value *overflow_arg_area_p = |
| 1105 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 1106 | llvm::Value *overflow_arg_area = |
| 1107 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 1108 | |
| 1109 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 1110 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 1111 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1112 | if (Align > 8) { |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 1113 | // Note that we follow the ABI & gcc here, even though the type |
| 1114 | // could in theory have an alignment greater than 16. This case |
| 1115 | // shouldn't ever matter in practice. |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1116 | |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 1117 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; |
| 1118 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15); |
| 1119 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 1120 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
| 1121 | llvm::Type::Int64Ty); |
| 1122 | llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL); |
| 1123 | overflow_arg_area = |
| 1124 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1125 | overflow_arg_area->getType(), |
| 1126 | "overflow_arg_area.align"); |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
| 1130 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 1131 | llvm::Value *Res = |
| 1132 | CGF.Builder.CreateBitCast(overflow_arg_area, |
| 1133 | llvm::PointerType::getUnqual(LTy)); |
| 1134 | |
| 1135 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 1136 | // l->overflow_arg_area + sizeof(type). |
| 1137 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 1138 | // an 8 byte boundary. |
| 1139 | |
| 1140 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
| 1141 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1142 | (SizeInBytes + 7) & ~7); |
| 1143 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 1144 | "overflow_arg_area.next"); |
| 1145 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 1146 | |
| 1147 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 1148 | return Res; |
| 1149 | } |
| 1150 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1151 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1152 | CodeGenFunction &CGF) const { |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1153 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 1154 | // struct { |
| 1155 | // i32 gp_offset; |
| 1156 | // i32 fp_offset; |
| 1157 | // i8* overflow_arg_area; |
| 1158 | // i8* reg_save_area; |
| 1159 | // }; |
| 1160 | unsigned neededInt, neededSSE; |
| 1161 | ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), |
| 1162 | neededInt, neededSSE); |
| 1163 | |
| 1164 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 1165 | // in the registers. If not go to step 7. |
| 1166 | if (!neededInt && !neededSSE) |
| 1167 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 1168 | |
| 1169 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 1170 | // general purpose registers needed to pass type and num_fp to hold |
| 1171 | // the number of floating point registers needed. |
| 1172 | |
| 1173 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 1174 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 1175 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 1176 | // |
| 1177 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 1178 | // register save space). |
| 1179 | |
| 1180 | llvm::Value *InRegs = 0; |
| 1181 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 1182 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 1183 | if (neededInt) { |
| 1184 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 1185 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
| 1186 | InRegs = |
| 1187 | CGF.Builder.CreateICmpULE(gp_offset, |
| 1188 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1189 | 48 - neededInt * 8), |
| 1190 | "fits_in_gp"); |
| 1191 | } |
| 1192 | |
| 1193 | if (neededSSE) { |
| 1194 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 1195 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 1196 | llvm::Value *FitsInFP = |
| 1197 | CGF.Builder.CreateICmpULE(fp_offset, |
| 1198 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Daniel Dunbar | 90dafa1 | 2009-02-18 22:19:44 +0000 | [diff] [blame] | 1199 | 176 - neededSSE * 16), |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1200 | "fits_in_fp"); |
Daniel Dunbar | f231346 | 2009-02-18 22:05:01 +0000 | [diff] [blame] | 1201 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 1205 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 1206 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 1207 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 1208 | |
| 1209 | // Emit code to load the value if it was passed in registers. |
| 1210 | |
| 1211 | CGF.EmitBlock(InRegBlock); |
| 1212 | |
| 1213 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 1214 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 1215 | // copying to a temporary location in case the parameter is passed |
| 1216 | // in different register classes or requires an alignment greater |
| 1217 | // than 8 for general purpose registers and 16 for XMM registers. |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 1218 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1219 | // FIXME: This really results in shameful code when we end up needing to |
| 1220 | // collect arguments from different places; often what should result in a |
| 1221 | // simple assembling of a structure from scattered addresses has many more |
| 1222 | // loads than necessary. Can we clean this up? |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1223 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 1224 | llvm::Value *RegAddr = |
| 1225 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 1226 | "reg_save_area"); |
| 1227 | if (neededInt && neededSSE) { |
Daniel Dunbar | 55e5d89 | 2009-02-13 17:46:31 +0000 | [diff] [blame] | 1228 | // FIXME: Cleanup. |
| 1229 | assert(AI.isCoerce() && "Unexpected ABI info for mixed regs"); |
| 1230 | const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
| 1231 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 1232 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
| 1233 | const llvm::Type *TyLo = ST->getElementType(0); |
| 1234 | const llvm::Type *TyHi = ST->getElementType(1); |
| 1235 | assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) && |
| 1236 | "Unexpected ABI info for mixed regs"); |
| 1237 | const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 1238 | const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
| 1239 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1240 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1241 | llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr; |
| 1242 | llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr; |
| 1243 | llvm::Value *V = |
| 1244 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 1245 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1246 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 1247 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1248 | |
| 1249 | RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1250 | } else if (neededInt) { |
| 1251 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1252 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 1253 | llvm::PointerType::getUnqual(LTy)); |
| 1254 | } else { |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 1255 | if (neededSSE == 1) { |
| 1256 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1257 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 1258 | llvm::PointerType::getUnqual(LTy)); |
| 1259 | } else { |
| 1260 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 1261 | // SSE registers are spaced 16 bytes apart in the register save |
| 1262 | // area, we need to collect the two eightbytes together. |
| 1263 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1264 | llvm::Value *RegAddrHi = |
| 1265 | CGF.Builder.CreateGEP(RegAddrLo, |
| 1266 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 16)); |
| 1267 | const llvm::Type *DblPtrTy = |
| 1268 | llvm::PointerType::getUnqual(llvm::Type::DoubleTy); |
| 1269 | const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy, |
| 1270 | llvm::Type::DoubleTy, |
| 1271 | NULL); |
| 1272 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 1273 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 1274 | DblPtrTy)); |
| 1275 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1276 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 1277 | DblPtrTy)); |
| 1278 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1279 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 1280 | llvm::PointerType::getUnqual(LTy)); |
| 1281 | } |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 1285 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 1286 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 1287 | if (neededInt) { |
| 1288 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1289 | neededInt * 8); |
| 1290 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 1291 | gp_offset_p); |
| 1292 | } |
| 1293 | if (neededSSE) { |
| 1294 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1295 | neededSSE * 16); |
| 1296 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 1297 | fp_offset_p); |
| 1298 | } |
| 1299 | CGF.EmitBranch(ContBlock); |
| 1300 | |
| 1301 | // Emit code to load the value if it was passed in memory. |
| 1302 | |
| 1303 | CGF.EmitBlock(InMemBlock); |
| 1304 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 1305 | |
| 1306 | // Return the appropriate result. |
| 1307 | |
| 1308 | CGF.EmitBlock(ContBlock); |
| 1309 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), |
| 1310 | "vaarg.addr"); |
| 1311 | ResAddr->reserveOperandSpace(2); |
| 1312 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 1313 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 1314 | |
| 1315 | return ResAddr; |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Sanjiv Gupta | 70aa5f9 | 2009-04-21 06:01:16 +0000 | [diff] [blame] | 1318 | // ABI Info for PIC16 |
| 1319 | class PIC16ABIInfo : public ABIInfo { |
| 1320 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 1321 | ASTContext &Context) const; |
| 1322 | |
| 1323 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 1324 | ASTContext &Context) const; |
| 1325 | |
| 1326 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 1327 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 1328 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1329 | it != ie; ++it) |
| 1330 | it->info = classifyArgumentType(it->type, Context); |
| 1331 | } |
| 1332 | |
| 1333 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1334 | CodeGenFunction &CGF) const; |
| 1335 | |
| 1336 | }; |
| 1337 | |
| 1338 | ABIArgInfo PIC16ABIInfo::classifyReturnType(QualType RetTy, |
| 1339 | ASTContext &Context) const { |
| 1340 | if (RetTy->isVoidType()) { |
| 1341 | return ABIArgInfo::getIgnore(); |
| 1342 | } else { |
| 1343 | return ABIArgInfo::getDirect(); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | ABIArgInfo PIC16ABIInfo::classifyArgumentType(QualType Ty, |
| 1348 | ASTContext &Context) const { |
| 1349 | return ABIArgInfo::getDirect(); |
| 1350 | } |
| 1351 | |
| 1352 | llvm::Value *PIC16ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1353 | CodeGenFunction &CGF) const { |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
Eli Friedman | a027ea9 | 2009-03-29 00:15:25 +0000 | [diff] [blame] | 1357 | class ARMABIInfo : public ABIInfo { |
| 1358 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 1359 | ASTContext &Context) const; |
| 1360 | |
| 1361 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 1362 | ASTContext &Context) const; |
| 1363 | |
| 1364 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const; |
| 1365 | |
| 1366 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1367 | CodeGenFunction &CGF) const; |
| 1368 | }; |
| 1369 | |
| 1370 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 1371 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 1372 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1373 | it != ie; ++it) { |
| 1374 | it->info = classifyArgumentType(it->type, Context); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 1379 | ASTContext &Context) const { |
| 1380 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { |
| 1381 | return ABIArgInfo::getDirect(); |
| 1382 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1383 | // FIXME: This is kind of nasty... but there isn't much choice because the ARM |
| 1384 | // backend doesn't support byval. |
Eli Friedman | a027ea9 | 2009-03-29 00:15:25 +0000 | [diff] [blame] | 1385 | // FIXME: This doesn't handle alignment > 64 bits. |
| 1386 | const llvm::Type* ElemTy; |
| 1387 | unsigned SizeRegs; |
| 1388 | if (Context.getTypeAlign(Ty) > 32) { |
| 1389 | ElemTy = llvm::Type::Int64Ty; |
| 1390 | SizeRegs = (Context.getTypeSize(Ty) + 63) / 64; |
| 1391 | } else { |
| 1392 | ElemTy = llvm::Type::Int32Ty; |
| 1393 | SizeRegs = (Context.getTypeSize(Ty) + 31) / 32; |
| 1394 | } |
| 1395 | std::vector<const llvm::Type*> LLVMFields; |
| 1396 | LLVMFields.push_back(llvm::ArrayType::get(ElemTy, SizeRegs)); |
| 1397 | const llvm::Type* STy = llvm::StructType::get(LLVMFields, true); |
| 1398 | return ABIArgInfo::getCoerce(STy); |
| 1399 | } |
| 1400 | |
| 1401 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 1402 | ASTContext &Context) const { |
| 1403 | if (RetTy->isVoidType()) { |
| 1404 | return ABIArgInfo::getIgnore(); |
| 1405 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1406 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 1407 | // are returned indirectly. |
| 1408 | uint64_t Size = Context.getTypeSize(RetTy); |
| 1409 | if (Size <= 32) |
| 1410 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 1411 | return ABIArgInfo::getIndirect(0); |
| 1412 | } else { |
| 1413 | return ABIArgInfo::getDirect(); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1418 | CodeGenFunction &CGF) const { |
| 1419 | // FIXME: Need to handle alignment |
| 1420 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 1421 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 1422 | |
| 1423 | CGBuilderTy &Builder = CGF.Builder; |
| 1424 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1425 | "ap"); |
| 1426 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 1427 | llvm::Type *PTy = |
| 1428 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 1429 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1430 | |
| 1431 | uint64_t Offset = |
| 1432 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 1433 | llvm::Value *NextAddr = |
| 1434 | Builder.CreateGEP(Addr, |
| 1435 | llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset), |
| 1436 | "ap.next"); |
| 1437 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1438 | |
| 1439 | return AddrTyped; |
| 1440 | } |
| 1441 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1442 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1443 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1444 | if (RetTy->isVoidType()) { |
| 1445 | return ABIArgInfo::getIgnore(); |
| 1446 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1447 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1448 | } else { |
| 1449 | return ABIArgInfo::getDirect(); |
| 1450 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1454 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1455 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1456 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1457 | } else { |
| 1458 | return ABIArgInfo::getDirect(); |
| 1459 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1462 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1463 | CodeGenFunction &CGF) const { |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1467 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 1468 | if (TheABIInfo) |
| 1469 | return *TheABIInfo; |
| 1470 | |
| 1471 | // For now we just cache this in the CodeGenTypes and don't bother |
| 1472 | // to free it. |
| 1473 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 1474 | if (strcmp(TargetPrefix, "x86") == 0) { |
Eli Friedman | 9fd58e8 | 2009-03-23 23:26:24 +0000 | [diff] [blame] | 1475 | bool IsDarwin = strstr(getContext().Target.getTargetTriple(), "darwin"); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1476 | switch (getContext().Target.getPointerWidth(0)) { |
| 1477 | case 32: |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1478 | return *(TheABIInfo = new X86_32ABIInfo(Context, IsDarwin)); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1479 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 1480 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1481 | } |
Eli Friedman | a027ea9 | 2009-03-29 00:15:25 +0000 | [diff] [blame] | 1482 | } else if (strcmp(TargetPrefix, "arm") == 0) { |
| 1483 | // FIXME: Support for OABI? |
| 1484 | return *(TheABIInfo = new ARMABIInfo()); |
Sanjiv Gupta | 70aa5f9 | 2009-04-21 06:01:16 +0000 | [diff] [blame] | 1485 | } else if (strcmp(TargetPrefix, "pic16") == 0) { |
| 1486 | return *(TheABIInfo = new PIC16ABIInfo()); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | return *(TheABIInfo = new DefaultABIInfo); |
| 1490 | } |
| 1491 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1492 | /***/ |
| 1493 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1494 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, |
| 1495 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
| 1496 | NumArgs = ArgTys.size(); |
| 1497 | Args = new ArgInfo[1 + NumArgs]; |
| 1498 | Args[0].type = ResTy; |
| 1499 | for (unsigned i = 0; i < NumArgs; ++i) |
| 1500 | Args[1 + i].type = ArgTys[i]; |
| 1501 | } |
| 1502 | |
| 1503 | /***/ |
| 1504 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1505 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 1506 | std::vector<const llvm::Type*> &ArgTys) { |
| 1507 | const RecordType *RT = Ty->getAsStructureType(); |
| 1508 | assert(RT && "Can only expand structure types."); |
| 1509 | const RecordDecl *RD = RT->getDecl(); |
| 1510 | assert(!RD->hasFlexibleArrayMember() && |
| 1511 | "Cannot expand structure with flexible array."); |
| 1512 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1513 | for (RecordDecl::field_iterator i = RD->field_begin(Context), |
| 1514 | e = RD->field_end(Context); i != e; ++i) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1515 | const FieldDecl *FD = *i; |
| 1516 | assert(!FD->isBitField() && |
| 1517 | "Cannot expand structure with bit-field members."); |
| 1518 | |
| 1519 | QualType FT = FD->getType(); |
| 1520 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1521 | GetExpandedTypes(FT, ArgTys); |
| 1522 | } else { |
| 1523 | ArgTys.push_back(ConvertType(FT)); |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | llvm::Function::arg_iterator |
| 1529 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 1530 | llvm::Function::arg_iterator AI) { |
| 1531 | const RecordType *RT = Ty->getAsStructureType(); |
| 1532 | assert(RT && "Can only expand structure types."); |
| 1533 | |
| 1534 | RecordDecl *RD = RT->getDecl(); |
| 1535 | assert(LV.isSimple() && |
| 1536 | "Unexpected non-simple lvalue during struct expansion."); |
| 1537 | llvm::Value *Addr = LV.getAddress(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1538 | for (RecordDecl::field_iterator i = RD->field_begin(getContext()), |
| 1539 | e = RD->field_end(getContext()); i != e; ++i) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1540 | FieldDecl *FD = *i; |
| 1541 | QualType FT = FD->getType(); |
| 1542 | |
| 1543 | // FIXME: What are the right qualifiers here? |
| 1544 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 1545 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1546 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 1547 | } else { |
| 1548 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 1549 | ++AI; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | return AI; |
| 1554 | } |
| 1555 | |
| 1556 | void |
| 1557 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 1558 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 1559 | const RecordType *RT = Ty->getAsStructureType(); |
| 1560 | assert(RT && "Can only expand structure types."); |
| 1561 | |
| 1562 | RecordDecl *RD = RT->getDecl(); |
| 1563 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 1564 | llvm::Value *Addr = RV.getAggregateAddr(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1565 | for (RecordDecl::field_iterator i = RD->field_begin(getContext()), |
| 1566 | e = RD->field_end(getContext()); i != e; ++i) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1567 | FieldDecl *FD = *i; |
| 1568 | QualType FT = FD->getType(); |
| 1569 | |
| 1570 | // FIXME: What are the right qualifiers here? |
| 1571 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 1572 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1573 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 1574 | } else { |
| 1575 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 1576 | assert(RV.isScalar() && |
| 1577 | "Unexpected non-scalar rvalue during struct expansion."); |
| 1578 | Args.push_back(RV.getScalarVal()); |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1583 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 1584 | /// a pointer to an object of type \arg Ty. |
| 1585 | /// |
| 1586 | /// This safely handles the case when the src type is smaller than the |
| 1587 | /// destination type; in this situation the values of bits which not |
| 1588 | /// present in the src are undefined. |
| 1589 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 1590 | const llvm::Type *Ty, |
| 1591 | CodeGenFunction &CGF) { |
| 1592 | const llvm::Type *SrcTy = |
| 1593 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1594 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
| 1595 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1596 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1597 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 1598 | if (SrcSize >= DstSize) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1599 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 1600 | // losing bits. However, this can happen in cases where the structure has |
| 1601 | // additional padding, for example due to a user specified alignment. |
Daniel Dunbar | 7ef455b | 2009-05-13 18:54:26 +0000 | [diff] [blame] | 1602 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1603 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 1604 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1605 | llvm::Value *Casted = |
| 1606 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1607 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 1608 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 1609 | Load->setAlignment(1); |
| 1610 | return Load; |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1611 | } else { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1612 | // Otherwise do coercion through memory. This is stupid, but |
| 1613 | // simple. |
| 1614 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 1615 | llvm::Value *Casted = |
| 1616 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1617 | llvm::StoreInst *Store = |
| 1618 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 1619 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 1620 | Store->setAlignment(1); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1621 | return CGF.Builder.CreateLoad(Tmp); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 1626 | /// where the source and destination may have different types. |
| 1627 | /// |
| 1628 | /// This safely handles the case when the src type is larger than the |
| 1629 | /// destination type; the upper bits of the src will be lost. |
| 1630 | static void CreateCoercedStore(llvm::Value *Src, |
| 1631 | llvm::Value *DstPtr, |
| 1632 | CodeGenFunction &CGF) { |
| 1633 | const llvm::Type *SrcTy = Src->getType(); |
| 1634 | const llvm::Type *DstTy = |
| 1635 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 1636 | |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1637 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); |
| 1638 | uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1639 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1640 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 1641 | if (SrcSize <= DstSize) { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1642 | llvm::Value *Casted = |
| 1643 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1644 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 1645 | CGF.Builder.CreateStore(Src, Casted)->setAlignment(1); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1646 | } else { |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1647 | // Otherwise do coercion through memory. This is stupid, but |
| 1648 | // simple. |
Daniel Dunbar | fdf4986 | 2009-06-05 07:58:54 +0000 | [diff] [blame] | 1649 | |
| 1650 | // Generally SrcSize is never greater than DstSize, since this means we are |
| 1651 | // losing bits. However, this can happen in cases where the structure has |
| 1652 | // additional padding, for example due to a user specified alignment. |
| 1653 | // |
| 1654 | // FIXME: Assert that we aren't truncating non-padding bits when have access |
| 1655 | // to that information. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1656 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 1657 | CGF.Builder.CreateStore(Src, Tmp); |
| 1658 | llvm::Value *Casted = |
| 1659 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1660 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 1661 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 1662 | Load->setAlignment(1); |
| 1663 | CGF.Builder.CreateStore(Load, DstPtr); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1667 | /***/ |
| 1668 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1669 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1670 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1673 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1674 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1675 | std::vector<const llvm::Type*> ArgTys; |
| 1676 | |
| 1677 | const llvm::Type *ResultType = 0; |
| 1678 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1679 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1680 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1681 | switch (RetAI.getKind()) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1682 | case ABIArgInfo::Expand: |
| 1683 | assert(0 && "Invalid ABI kind for return argument"); |
| 1684 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1685 | case ABIArgInfo::Direct: |
| 1686 | ResultType = ConvertType(RetTy); |
| 1687 | break; |
| 1688 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1689 | case ABIArgInfo::Indirect: { |
| 1690 | assert(!RetAI.getIndirectAlign() && "Align unused on indirect return."); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1691 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1692 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1693 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 1694 | break; |
| 1695 | } |
| 1696 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1697 | case ABIArgInfo::Ignore: |
| 1698 | ResultType = llvm::Type::VoidTy; |
| 1699 | break; |
| 1700 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1701 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1702 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1703 | break; |
| 1704 | } |
| 1705 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1706 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1707 | ie = FI.arg_end(); it != ie; ++it) { |
| 1708 | const ABIArgInfo &AI = it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1709 | |
| 1710 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1711 | case ABIArgInfo::Ignore: |
| 1712 | break; |
| 1713 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1714 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1715 | ArgTys.push_back(AI.getCoerceToType()); |
| 1716 | break; |
| 1717 | |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1718 | case ABIArgInfo::Indirect: { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1719 | // indirect arguments are always on the stack, which is addr space #0. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1720 | const llvm::Type *LTy = ConvertTypeForMem(it->type); |
| 1721 | ArgTys.push_back(llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1722 | break; |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1723 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1724 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1725 | case ABIArgInfo::Direct: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1726 | ArgTys.push_back(ConvertType(it->type)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1727 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1728 | |
| 1729 | case ABIArgInfo::Expand: |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1730 | GetExpandedTypes(it->type, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1731 | break; |
| 1732 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1735 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1738 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1739 | const Decl *TargetDecl, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1740 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1741 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1742 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1743 | |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1744 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1745 | if (TargetDecl) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1746 | if (TargetDecl->hasAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1747 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1748 | if (TargetDecl->hasAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1749 | FuncAttrs |= llvm::Attribute::NoReturn; |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1750 | if (TargetDecl->hasAttr<ConstAttr>()) |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1751 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 1752 | else if (TargetDecl->hasAttr<PureAttr>()) |
Daniel Dunbar | 64c2e07 | 2009-04-10 22:14:52 +0000 | [diff] [blame] | 1753 | FuncAttrs |= llvm::Attribute::ReadOnly; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1756 | if (CompileOpts.DisableRedZone) |
| 1757 | FuncAttrs |= llvm::Attribute::NoRedZone; |
Devang Patel | acebb39 | 2009-06-05 22:05:48 +0000 | [diff] [blame^] | 1758 | if (CompileOpts.NoImplicitFloat) |
| 1759 | FuncAttrs |= llvm::Attribute::NoImplicitFloat; |
Devang Patel | 24095da | 2009-06-04 23:32:02 +0000 | [diff] [blame] | 1760 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1761 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1762 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1763 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1764 | switch (RetAI.getKind()) { |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1765 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1766 | if (RetTy->isPromotableIntegerType()) { |
| 1767 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1768 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1769 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1770 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | break; |
| 1774 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1775 | case ABIArgInfo::Indirect: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1776 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1777 | llvm::Attribute::StructRet | |
| 1778 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1779 | ++Index; |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 1780 | // sret disables readnone and readonly |
| 1781 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 1782 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1783 | break; |
| 1784 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1785 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1786 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1787 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1788 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1789 | case ABIArgInfo::Expand: |
| 1790 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1791 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1792 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1793 | if (RetAttrs) |
| 1794 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1795 | |
| 1796 | // FIXME: we need to honour command line settings also... |
| 1797 | // FIXME: RegParm should be reduced in case of nested functions and/or global |
| 1798 | // register variable. |
| 1799 | signed RegParm = 0; |
| 1800 | if (TargetDecl) |
| 1801 | if (const RegparmAttr *RegParmAttr = TargetDecl->getAttr<RegparmAttr>()) |
| 1802 | RegParm = RegParmAttr->getNumParams(); |
| 1803 | |
| 1804 | unsigned PointerWidth = getContext().Target.getPointerWidth(0); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1805 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1806 | ie = FI.arg_end(); it != ie; ++it) { |
| 1807 | QualType ParamType = it->type; |
| 1808 | const ABIArgInfo &AI = it->info; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1809 | unsigned Attributes = 0; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1810 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1811 | switch (AI.getKind()) { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1812 | case ABIArgInfo::Coerce: |
| 1813 | break; |
| 1814 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1815 | case ABIArgInfo::Indirect: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1816 | Attributes |= llvm::Attribute::ByVal; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1817 | Attributes |= |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1818 | llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); |
Daniel Dunbar | 0ac86f0 | 2009-03-18 19:51:01 +0000 | [diff] [blame] | 1819 | // byval disables readnone and readonly. |
| 1820 | FuncAttrs &= ~(llvm::Attribute::ReadOnly | |
| 1821 | llvm::Attribute::ReadNone); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1824 | case ABIArgInfo::Direct: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1825 | if (ParamType->isPromotableIntegerType()) { |
| 1826 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1827 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1828 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1829 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1830 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1831 | } |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1832 | if (RegParm > 0 && |
| 1833 | (ParamType->isIntegerType() || ParamType->isPointerType())) { |
| 1834 | RegParm -= |
| 1835 | (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; |
| 1836 | if (RegParm >= 0) |
| 1837 | Attributes |= llvm::Attribute::InReg; |
| 1838 | } |
| 1839 | // FIXME: handle sseregparm someday... |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1840 | break; |
Anton Korobeynikov | 1102f42 | 2009-04-04 00:49:24 +0000 | [diff] [blame] | 1841 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1842 | case ABIArgInfo::Ignore: |
| 1843 | // Skip increment, no matching LLVM parameter. |
| 1844 | continue; |
| 1845 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1846 | case ABIArgInfo::Expand: { |
| 1847 | std::vector<const llvm::Type*> Tys; |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1848 | // FIXME: This is rather inefficient. Do we ever actually need to do |
| 1849 | // anything here? The result should be just reconstructed on the other |
| 1850 | // side, so extension should be a non-issue. |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1851 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1852 | Index += Tys.size(); |
| 1853 | continue; |
| 1854 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1855 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1856 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1857 | if (Attributes) |
| 1858 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1859 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1860 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1861 | if (FuncAttrs) |
| 1862 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1865 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1866 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1867 | const FunctionArgList &Args) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1868 | // FIXME: We no longer need the types from FunctionArgList; lift up and |
| 1869 | // simplify. |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1870 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1871 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1872 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1873 | |
| 1874 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1875 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1876 | AI->setName("agg.result"); |
| 1877 | ++AI; |
| 1878 | } |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1879 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1880 | assert(FI.arg_size() == Args.size() && |
| 1881 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1882 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1883 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1884 | i != e; ++i, ++info_it) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1885 | const VarDecl *Arg = i->first; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1886 | QualType Ty = info_it->type; |
| 1887 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1888 | |
| 1889 | switch (ArgI.getKind()) { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1890 | case ABIArgInfo::Indirect: { |
| 1891 | llvm::Value* V = AI; |
| 1892 | if (hasAggregateLLVMType(Ty)) { |
| 1893 | // Do nothing, aggregates and complex variables are accessed by |
| 1894 | // reference. |
| 1895 | } else { |
| 1896 | // Load scalar value from indirect argument. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1897 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1898 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1899 | // This must be a promotion, for something like |
| 1900 | // "void a(x) short x; {..." |
| 1901 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1902 | } |
| 1903 | } |
| 1904 | EmitParmDecl(*Arg, V); |
| 1905 | break; |
| 1906 | } |
| 1907 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1908 | case ABIArgInfo::Direct: { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1909 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1910 | llvm::Value* V = AI; |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1911 | if (hasAggregateLLVMType(Ty)) { |
| 1912 | // Create a temporary alloca to hold the argument; the rest of |
| 1913 | // codegen expects to access aggregates & complex values by |
| 1914 | // reference. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1915 | V = CreateTempAlloca(ConvertTypeForMem(Ty)); |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1916 | Builder.CreateStore(AI, V); |
| 1917 | } else { |
| 1918 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1919 | // This must be a promotion, for something like |
| 1920 | // "void a(x) short x; {..." |
| 1921 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1922 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1923 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1924 | EmitParmDecl(*Arg, V); |
| 1925 | break; |
| 1926 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1927 | |
| 1928 | case ABIArgInfo::Expand: { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1929 | // If this structure was expanded into multiple arguments then |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1930 | // we need to create a temporary and reconstruct it from the |
| 1931 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1932 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1933 | llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1934 | (Name + ".addr").c_str()); |
| 1935 | // FIXME: What are the right qualifiers here? |
| 1936 | llvm::Function::arg_iterator End = |
| 1937 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1938 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1939 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1940 | // Name the arguments used in expansion and increment AI. |
| 1941 | unsigned Index = 0; |
| 1942 | for (; AI != End; ++AI, ++Index) |
| 1943 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1944 | continue; |
| 1945 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1946 | |
| 1947 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1948 | // Initialize the local variable appropriately. |
| 1949 | if (hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1950 | EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty))); |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1951 | } else { |
| 1952 | EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType()))); |
| 1953 | } |
| 1954 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1955 | // Skip increment, no matching LLVM parameter. |
| 1956 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1957 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1958 | case ABIArgInfo::Coerce: { |
| 1959 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1960 | // FIXME: This is very wasteful; EmitParmDecl is just going to drop the |
| 1961 | // result in a new alloca anyway, so we could just store into that |
| 1962 | // directly if we broke the abstraction down more. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1963 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1964 | CreateCoercedStore(AI, V, *this); |
| 1965 | // Match to what EmitParmDecl is expecting for this type. |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1966 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1967 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1968 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1969 | // This must be a promotion, for something like |
| 1970 | // "void a(x) short x; {..." |
| 1971 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1972 | } |
| 1973 | } |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1974 | EmitParmDecl(*Arg, V); |
| 1975 | break; |
| 1976 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1977 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1978 | |
| 1979 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1980 | } |
| 1981 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1982 | } |
| 1983 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1984 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1985 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1986 | llvm::Value *RV = 0; |
| 1987 | |
| 1988 | // Functions with no result always return void. |
| 1989 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1990 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1991 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1992 | |
| 1993 | switch (RetAI.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1994 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1995 | if (RetTy->isAnyComplexType()) { |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1996 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1997 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1998 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1999 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 2000 | } else { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2001 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 2002 | false, RetTy); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 2003 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2004 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2005 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 2006 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 2007 | // The internal return value temp always will have |
| 2008 | // pointer-to-return-type type. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2009 | RV = Builder.CreateLoad(ReturnValue); |
| 2010 | break; |
| 2011 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2012 | case ABIArgInfo::Ignore: |
| 2013 | break; |
| 2014 | |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2015 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 2016 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2017 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2018 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2019 | case ABIArgInfo::Expand: |
| 2020 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2021 | } |
| 2022 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2023 | |
| 2024 | if (RV) { |
| 2025 | Builder.CreateRet(RV); |
| 2026 | } else { |
| 2027 | Builder.CreateRetVoid(); |
| 2028 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2031 | RValue CodeGenFunction::EmitCallArg(const Expr *E, QualType ArgType) { |
Anders Carlsson | 4029ca7 | 2009-05-20 00:24:07 +0000 | [diff] [blame] | 2032 | if (ArgType->isReferenceType()) |
| 2033 | return EmitReferenceBindingToExpr(E, ArgType); |
| 2034 | |
Anders Carlsson | 0139bb9 | 2009-04-08 20:47:54 +0000 | [diff] [blame] | 2035 | return EmitAnyExprToTemp(E); |
| 2036 | } |
| 2037 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 2038 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 2039 | llvm::Value *Callee, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2040 | const CallArgList &CallArgs, |
| 2041 | const Decl *TargetDecl) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2042 | // FIXME: We no longer need the types from CallArgs; lift up and simplify. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2043 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2044 | |
| 2045 | // Handle struct-return functions by passing a pointer to the |
| 2046 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 2047 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2048 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Daniel Dunbar | 2969a02 | 2009-02-05 09:24:53 +0000 | [diff] [blame] | 2049 | if (CGM.ReturnTypeUsesSret(CallInfo)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2050 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2051 | Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy))); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 2054 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 2055 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2056 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2057 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 2058 | I != E; ++I, ++info_it) { |
| 2059 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2060 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2061 | |
| 2062 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 2063 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2064 | if (RV.isScalar() || RV.isComplex()) { |
| 2065 | // Make a temporary alloca to pass the argument. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2066 | Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second))); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2067 | if (RV.isScalar()) |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 2068 | EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, I->second); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 2069 | else |
| 2070 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 2071 | } else { |
| 2072 | Args.push_back(RV.getAggregateAddr()); |
| 2073 | } |
| 2074 | break; |
| 2075 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 2076 | case ABIArgInfo::Direct: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2077 | if (RV.isScalar()) { |
| 2078 | Args.push_back(RV.getScalarVal()); |
| 2079 | } else if (RV.isComplex()) { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 2080 | llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second)); |
| 2081 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0); |
| 2082 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1); |
| 2083 | Args.push_back(Tmp); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2084 | } else { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 2085 | Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2086 | } |
| 2087 | break; |
| 2088 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2089 | case ABIArgInfo::Ignore: |
| 2090 | break; |
| 2091 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2092 | case ABIArgInfo::Coerce: { |
| 2093 | // FIXME: Avoid the conversion through memory if possible. |
| 2094 | llvm::Value *SrcPtr; |
| 2095 | if (RV.isScalar()) { |
Daniel Dunbar | 5a1be6e | 2009-02-03 23:04:57 +0000 | [diff] [blame] | 2096 | SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); |
Anders Carlsson | b4aa466 | 2009-05-19 18:50:41 +0000 | [diff] [blame] | 2097 | EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, I->second); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2098 | } else if (RV.isComplex()) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2099 | SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2100 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
| 2101 | } else |
| 2102 | SrcPtr = RV.getAggregateAddr(); |
| 2103 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 2104 | *this)); |
| 2105 | break; |
| 2106 | } |
| 2107 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2108 | case ABIArgInfo::Expand: |
| 2109 | ExpandTypeToArgs(I->second, RV, Args); |
| 2110 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2113 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2114 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 2115 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 2116 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2117 | llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), |
| 2118 | AttributeList.end()); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 2119 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2120 | llvm::CallSite CS; |
| 2121 | if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) { |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2122 | CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2125 | CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2126 | Args.data(), Args.data()+Args.size()); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 2127 | EmitBlock(Cont); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2130 | CS.setAttributes(Attrs); |
Torok Edwin | 6857d9d | 2009-05-22 07:25:06 +0000 | [diff] [blame] | 2131 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee->stripPointerCasts())) |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2132 | CS.setCallingConv(F->getCallingConv()); |
| 2133 | |
| 2134 | // If the call doesn't return, finish the basic block and clear the |
| 2135 | // insertion point; this allows the rest of IRgen to discard |
| 2136 | // unreachable code. |
| 2137 | if (CS.doesNotReturn()) { |
| 2138 | Builder.CreateUnreachable(); |
| 2139 | Builder.ClearInsertionPoint(); |
| 2140 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2141 | // FIXME: For now, emit a dummy basic block because expr emitters in |
| 2142 | // generally are not ready to handle emitting expressions at unreachable |
| 2143 | // points. |
Daniel Dunbar | d14151d | 2009-03-02 04:32:35 +0000 | [diff] [blame] | 2144 | EnsureInsertPoint(); |
| 2145 | |
| 2146 | // Return a reasonable RValue. |
| 2147 | return GetUndefRValue(RetTy); |
| 2148 | } |
| 2149 | |
| 2150 | llvm::Instruction *CI = CS.getInstruction(); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2151 | if (Builder.isNamePreserving() && CI->getType() != llvm::Type::VoidTy) |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2152 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2153 | |
| 2154 | switch (RetAI.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 2155 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2156 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2157 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2158 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 2159 | return RValue::getAggregate(Args[0]); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2160 | return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2161 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 2162 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 2163 | if (RetTy->isAnyComplexType()) { |
| 2164 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 2165 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 2166 | return RValue::getComplex(std::make_pair(Real, Imag)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2167 | } |
| 2168 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2169 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp"); |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 2170 | Builder.CreateStore(CI, V); |
| 2171 | return RValue::getAggregate(V); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2172 | } |
| 2173 | return RValue::get(CI); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2174 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2175 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 2176 | // If we are ignoring an argument that had a result, make sure to |
| 2177 | // construct the appropriate return value for our caller. |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 2178 | return GetUndefRValue(RetTy); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 2179 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 2180 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 2181 | // FIXME: Avoid the conversion through memory if possible. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 2182 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce"); |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 2183 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 2184 | if (RetTy->isAnyComplexType()) |
| 2185 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2186 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 2187 | return RValue::getAggregate(V); |
Chris Lattner | 3403084 | 2009-03-22 00:32:22 +0000 | [diff] [blame] | 2188 | return RValue::get(EmitLoadOfScalar(V, false, RetTy)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 2189 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2190 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 2191 | case ABIArgInfo::Expand: |
| 2192 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2193 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 2194 | |
| 2195 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 2196 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 2197 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 2198 | |
| 2199 | /* VarArg handling */ |
| 2200 | |
| 2201 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 2202 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 2203 | } |