TargetInfo.cpp 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633
  1. //===---- TargetInfo.cpp - Encapsulate target 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. #include "TargetInfo.h"
  15. #include "ABIInfo.h"
  16. #include "CodeGenFunction.h"
  17. #include "clang/AST/RecordLayout.h"
  18. #include "clang/Frontend/CodeGenOptions.h"
  19. #include "llvm/Type.h"
  20. #include "llvm/Target/TargetData.h"
  21. #include "llvm/ADT/Triple.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace clang;
  24. using namespace CodeGen;
  25. static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
  26. llvm::Value *Array,
  27. llvm::Value *Value,
  28. unsigned FirstIndex,
  29. unsigned LastIndex) {
  30. // Alternatively, we could emit this as a loop in the source.
  31. for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
  32. llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
  33. Builder.CreateStore(Value, Cell);
  34. }
  35. }
  36. static bool isAggregateTypeForABI(QualType T) {
  37. return CodeGenFunction::hasAggregateLLVMType(T) ||
  38. T->isMemberFunctionPointerType();
  39. }
  40. ABIInfo::~ABIInfo() {}
  41. ASTContext &ABIInfo::getContext() const {
  42. return CGT.getContext();
  43. }
  44. llvm::LLVMContext &ABIInfo::getVMContext() const {
  45. return CGT.getLLVMContext();
  46. }
  47. const llvm::TargetData &ABIInfo::getTargetData() const {
  48. return CGT.getTargetData();
  49. }
  50. void ABIArgInfo::dump() const {
  51. raw_ostream &OS = llvm::errs();
  52. OS << "(ABIArgInfo Kind=";
  53. switch (TheKind) {
  54. case Direct:
  55. OS << "Direct Type=";
  56. if (llvm::Type *Ty = getCoerceToType())
  57. Ty->print(OS);
  58. else
  59. OS << "null";
  60. break;
  61. case Extend:
  62. OS << "Extend";
  63. break;
  64. case Ignore:
  65. OS << "Ignore";
  66. break;
  67. case Indirect:
  68. OS << "Indirect Align=" << getIndirectAlign()
  69. << " ByVal=" << getIndirectByVal()
  70. << " Realign=" << getIndirectRealign();
  71. break;
  72. case Expand:
  73. OS << "Expand";
  74. break;
  75. }
  76. OS << ")\n";
  77. }
  78. TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
  79. // If someone can figure out a general rule for this, that would be great.
  80. // It's probably just doomed to be platform-dependent, though.
  81. unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
  82. // Verified for:
  83. // x86-64 FreeBSD, Linux, Darwin
  84. // x86-32 FreeBSD, Linux, Darwin
  85. // PowerPC Linux, Darwin
  86. // ARM Darwin (*not* EABI)
  87. return 32;
  88. }
  89. bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
  90. const FunctionNoProtoType *fnType) const {
  91. // The following conventions are known to require this to be false:
  92. // x86_stdcall
  93. // MIPS
  94. // For everything else, we just prefer false unless we opt out.
  95. return false;
  96. }
  97. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
  98. /// isEmptyField - Return true iff a the field is "empty", that is it
  99. /// is an unnamed bit-field or an (array of) empty record(s).
  100. static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
  101. bool AllowArrays) {
  102. if (FD->isUnnamedBitfield())
  103. return true;
  104. QualType FT = FD->getType();
  105. // Constant arrays of empty records count as empty, strip them off.
  106. // Constant arrays of zero length always count as empty.
  107. if (AllowArrays)
  108. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  109. if (AT->getSize() == 0)
  110. return true;
  111. FT = AT->getElementType();
  112. }
  113. const RecordType *RT = FT->getAs<RecordType>();
  114. if (!RT)
  115. return false;
  116. // C++ record fields are never empty, at least in the Itanium ABI.
  117. //
  118. // FIXME: We should use a predicate for whether this behavior is true in the
  119. // current ABI.
  120. if (isa<CXXRecordDecl>(RT->getDecl()))
  121. return false;
  122. return isEmptyRecord(Context, FT, AllowArrays);
  123. }
  124. /// isEmptyRecord - Return true iff a structure contains only empty
  125. /// fields. Note that a structure with a flexible array member is not
  126. /// considered empty.
  127. static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
  128. const RecordType *RT = T->getAs<RecordType>();
  129. if (!RT)
  130. return 0;
  131. const RecordDecl *RD = RT->getDecl();
  132. if (RD->hasFlexibleArrayMember())
  133. return false;
  134. // If this is a C++ record, check the bases first.
  135. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  136. for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
  137. e = CXXRD->bases_end(); i != e; ++i)
  138. if (!isEmptyRecord(Context, i->getType(), true))
  139. return false;
  140. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  141. i != e; ++i)
  142. if (!isEmptyField(Context, *i, AllowArrays))
  143. return false;
  144. return true;
  145. }
  146. /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
  147. /// a non-trivial destructor or a non-trivial copy constructor.
  148. static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
  149. const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
  150. if (!RD)
  151. return false;
  152. return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
  153. }
  154. /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
  155. /// a record type with either a non-trivial destructor or a non-trivial copy
  156. /// constructor.
  157. static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
  158. const RecordType *RT = T->getAs<RecordType>();
  159. if (!RT)
  160. return false;
  161. return hasNonTrivialDestructorOrCopyConstructor(RT);
  162. }
  163. /// isSingleElementStruct - Determine if a structure is a "single
  164. /// element struct", i.e. it has exactly one non-empty field or
  165. /// exactly one field which is itself a single element
  166. /// struct. Structures with flexible array members are never
  167. /// considered single element structs.
  168. ///
  169. /// \return The field declaration for the single non-empty field, if
  170. /// it exists.
  171. static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
  172. const RecordType *RT = T->getAsStructureType();
  173. if (!RT)
  174. return 0;
  175. const RecordDecl *RD = RT->getDecl();
  176. if (RD->hasFlexibleArrayMember())
  177. return 0;
  178. const Type *Found = 0;
  179. // If this is a C++ record, check the bases first.
  180. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  181. for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
  182. e = CXXRD->bases_end(); i != e; ++i) {
  183. // Ignore empty records.
  184. if (isEmptyRecord(Context, i->getType(), true))
  185. continue;
  186. // If we already found an element then this isn't a single-element struct.
  187. if (Found)
  188. return 0;
  189. // If this is non-empty and not a single element struct, the composite
  190. // cannot be a single element struct.
  191. Found = isSingleElementStruct(i->getType(), Context);
  192. if (!Found)
  193. return 0;
  194. }
  195. }
  196. // Check for single element.
  197. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  198. i != e; ++i) {
  199. const FieldDecl *FD = *i;
  200. QualType FT = FD->getType();
  201. // Ignore empty fields.
  202. if (isEmptyField(Context, FD, true))
  203. continue;
  204. // If we already found an element then this isn't a single-element
  205. // struct.
  206. if (Found)
  207. return 0;
  208. // Treat single element arrays as the element.
  209. while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
  210. if (AT->getSize().getZExtValue() != 1)
  211. break;
  212. FT = AT->getElementType();
  213. }
  214. if (!isAggregateTypeForABI(FT)) {
  215. Found = FT.getTypePtr();
  216. } else {
  217. Found = isSingleElementStruct(FT, Context);
  218. if (!Found)
  219. return 0;
  220. }
  221. }
  222. // We don't consider a struct a single-element struct if it has
  223. // padding beyond the element type.
  224. if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
  225. return 0;
  226. return Found;
  227. }
  228. static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
  229. if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
  230. !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
  231. !Ty->isBlockPointerType())
  232. return false;
  233. uint64_t Size = Context.getTypeSize(Ty);
  234. return Size == 32 || Size == 64;
  235. }
  236. /// canExpandIndirectArgument - Test whether an argument type which is to be
  237. /// passed indirectly (on the stack) would have the equivalent layout if it was
  238. /// expanded into separate arguments. If so, we prefer to do the latter to avoid
  239. /// inhibiting optimizations.
  240. ///
  241. // FIXME: This predicate is missing many cases, currently it just follows
  242. // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
  243. // should probably make this smarter, or better yet make the LLVM backend
  244. // capable of handling it.
  245. static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
  246. // We can only expand structure types.
  247. const RecordType *RT = Ty->getAs<RecordType>();
  248. if (!RT)
  249. return false;
  250. // We can only expand (C) structures.
  251. //
  252. // FIXME: This needs to be generalized to handle classes as well.
  253. const RecordDecl *RD = RT->getDecl();
  254. if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
  255. return false;
  256. uint64_t Size = 0;
  257. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  258. i != e; ++i) {
  259. const FieldDecl *FD = *i;
  260. if (!is32Or64BitBasicType(FD->getType(), Context))
  261. return false;
  262. // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
  263. // how to expand them yet, and the predicate for telling if a bitfield still
  264. // counts as "basic" is more complicated than what we were doing previously.
  265. if (FD->isBitField())
  266. return false;
  267. Size += Context.getTypeSize(FD->getType());
  268. }
  269. // Make sure there are not any holes in the struct.
  270. if (Size != Context.getTypeSize(Ty))
  271. return false;
  272. return true;
  273. }
  274. namespace {
  275. /// DefaultABIInfo - The default implementation for ABI specific
  276. /// details. This implementation provides information which results in
  277. /// self-consistent and sensible LLVM IR generation, but does not
  278. /// conform to any particular ABI.
  279. class DefaultABIInfo : public ABIInfo {
  280. public:
  281. DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  282. ABIArgInfo classifyReturnType(QualType RetTy) const;
  283. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  284. virtual void computeInfo(CGFunctionInfo &FI) const {
  285. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  286. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  287. it != ie; ++it)
  288. it->info = classifyArgumentType(it->type);
  289. }
  290. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  291. CodeGenFunction &CGF) const;
  292. };
  293. class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
  294. public:
  295. DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  296. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  297. };
  298. llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  299. CodeGenFunction &CGF) const {
  300. return 0;
  301. }
  302. ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
  303. if (isAggregateTypeForABI(Ty)) {
  304. // Records with non trivial destructors/constructors should not be passed
  305. // by value.
  306. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
  307. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  308. return ABIArgInfo::getIndirect(0);
  309. }
  310. // Treat an enum type as its underlying type.
  311. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  312. Ty = EnumTy->getDecl()->getIntegerType();
  313. return (Ty->isPromotableIntegerType() ?
  314. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  315. }
  316. ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
  317. if (RetTy->isVoidType())
  318. return ABIArgInfo::getIgnore();
  319. if (isAggregateTypeForABI(RetTy))
  320. return ABIArgInfo::getIndirect(0);
  321. // Treat an enum type as its underlying type.
  322. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  323. RetTy = EnumTy->getDecl()->getIntegerType();
  324. return (RetTy->isPromotableIntegerType() ?
  325. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  326. }
  327. /// UseX86_MMXType - Return true if this is an MMX type that should use the
  328. /// special x86_mmx type.
  329. bool UseX86_MMXType(llvm::Type *IRType) {
  330. // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
  331. // special x86_mmx type.
  332. return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
  333. cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
  334. IRType->getScalarSizeInBits() != 64;
  335. }
  336. static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  337. StringRef Constraint,
  338. llvm::Type* Ty) {
  339. if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
  340. return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
  341. return Ty;
  342. }
  343. //===----------------------------------------------------------------------===//
  344. // X86-32 ABI Implementation
  345. //===----------------------------------------------------------------------===//
  346. /// X86_32ABIInfo - The X86-32 ABI information.
  347. class X86_32ABIInfo : public ABIInfo {
  348. static const unsigned MinABIStackAlignInBytes = 4;
  349. bool IsDarwinVectorABI;
  350. bool IsSmallStructInRegABI;
  351. bool IsMMXDisabled;
  352. bool IsWin32FloatStructABI;
  353. static bool isRegisterSize(unsigned Size) {
  354. return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
  355. }
  356. static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context);
  357. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  358. /// such that the argument will be passed in memory.
  359. ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
  360. /// \brief Return the alignment to use for the given type on the stack.
  361. unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
  362. public:
  363. ABIArgInfo classifyReturnType(QualType RetTy) const;
  364. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  365. virtual void computeInfo(CGFunctionInfo &FI) const {
  366. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  367. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  368. it != ie; ++it)
  369. it->info = classifyArgumentType(it->type);
  370. }
  371. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  372. CodeGenFunction &CGF) const;
  373. X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
  374. : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
  375. IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
  376. };
  377. class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
  378. public:
  379. X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
  380. bool d, bool p, bool m, bool w)
  381. :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
  382. void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  383. CodeGen::CodeGenModule &CGM) const;
  384. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
  385. // Darwin uses different dwarf register numbers for EH.
  386. if (CGM.isTargetDarwin()) return 5;
  387. return 4;
  388. }
  389. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  390. llvm::Value *Address) const;
  391. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  392. StringRef Constraint,
  393. llvm::Type* Ty) const {
  394. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  395. }
  396. };
  397. }
  398. /// shouldReturnTypeInRegister - Determine if the given type should be
  399. /// passed in a register (for the Darwin ABI).
  400. bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
  401. ASTContext &Context) {
  402. uint64_t Size = Context.getTypeSize(Ty);
  403. // Type must be register sized.
  404. if (!isRegisterSize(Size))
  405. return false;
  406. if (Ty->isVectorType()) {
  407. // 64- and 128- bit vectors inside structures are not returned in
  408. // registers.
  409. if (Size == 64 || Size == 128)
  410. return false;
  411. return true;
  412. }
  413. // If this is a builtin, pointer, enum, complex type, member pointer, or
  414. // member function pointer it is ok.
  415. if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
  416. Ty->isAnyComplexType() || Ty->isEnumeralType() ||
  417. Ty->isBlockPointerType() || Ty->isMemberPointerType())
  418. return true;
  419. // Arrays are treated like records.
  420. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
  421. return shouldReturnTypeInRegister(AT->getElementType(), Context);
  422. // Otherwise, it must be a record type.
  423. const RecordType *RT = Ty->getAs<RecordType>();
  424. if (!RT) return false;
  425. // FIXME: Traverse bases here too.
  426. // Structure types are passed in register if all fields would be
  427. // passed in a register.
  428. for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
  429. e = RT->getDecl()->field_end(); i != e; ++i) {
  430. const FieldDecl *FD = *i;
  431. // Empty fields are ignored.
  432. if (isEmptyField(Context, FD, true))
  433. continue;
  434. // Check fields recursively.
  435. if (!shouldReturnTypeInRegister(FD->getType(), Context))
  436. return false;
  437. }
  438. return true;
  439. }
  440. ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const {
  441. if (RetTy->isVoidType())
  442. return ABIArgInfo::getIgnore();
  443. if (const VectorType *VT = RetTy->getAs<VectorType>()) {
  444. // On Darwin, some vectors are returned in registers.
  445. if (IsDarwinVectorABI) {
  446. uint64_t Size = getContext().getTypeSize(RetTy);
  447. // 128-bit vectors are a special case; they are returned in
  448. // registers and we need to make sure to pick a type the LLVM
  449. // backend will like.
  450. if (Size == 128)
  451. return ABIArgInfo::getDirect(llvm::VectorType::get(
  452. llvm::Type::getInt64Ty(getVMContext()), 2));
  453. // Always return in register if it fits in a general purpose
  454. // register, or if it is 64 bits and has a single element.
  455. if ((Size == 8 || Size == 16 || Size == 32) ||
  456. (Size == 64 && VT->getNumElements() == 1))
  457. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  458. Size));
  459. return ABIArgInfo::getIndirect(0);
  460. }
  461. return ABIArgInfo::getDirect();
  462. }
  463. if (isAggregateTypeForABI(RetTy)) {
  464. if (const RecordType *RT = RetTy->getAs<RecordType>()) {
  465. // Structures with either a non-trivial destructor or a non-trivial
  466. // copy constructor are always indirect.
  467. if (hasNonTrivialDestructorOrCopyConstructor(RT))
  468. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  469. // Structures with flexible arrays are always indirect.
  470. if (RT->getDecl()->hasFlexibleArrayMember())
  471. return ABIArgInfo::getIndirect(0);
  472. }
  473. // If specified, structs and unions are always indirect.
  474. if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
  475. return ABIArgInfo::getIndirect(0);
  476. // Small structures which are register sized are generally returned
  477. // in a register.
  478. if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) {
  479. uint64_t Size = getContext().getTypeSize(RetTy);
  480. // As a special-case, if the struct is a "single-element" struct, and
  481. // the field is of type "float" or "double", return it in a
  482. // floating-point register. (MSVC does not apply this special case.)
  483. // We apply a similar transformation for pointer types to improve the
  484. // quality of the generated IR.
  485. if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
  486. if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
  487. || SeltTy->hasPointerRepresentation())
  488. return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
  489. // FIXME: We should be able to narrow this integer in cases with dead
  490. // padding.
  491. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
  492. }
  493. return ABIArgInfo::getIndirect(0);
  494. }
  495. // Treat an enum type as its underlying type.
  496. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  497. RetTy = EnumTy->getDecl()->getIntegerType();
  498. return (RetTy->isPromotableIntegerType() ?
  499. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  500. }
  501. static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
  502. const RecordType *RT = Ty->getAs<RecordType>();
  503. if (!RT)
  504. return 0;
  505. const RecordDecl *RD = RT->getDecl();
  506. // If this is a C++ record, check the bases first.
  507. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
  508. for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
  509. e = CXXRD->bases_end(); i != e; ++i)
  510. if (!isRecordWithSSEVectorType(Context, i->getType()))
  511. return false;
  512. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  513. i != e; ++i) {
  514. QualType FT = i->getType();
  515. if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
  516. return true;
  517. if (isRecordWithSSEVectorType(Context, FT))
  518. return true;
  519. }
  520. return false;
  521. }
  522. unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
  523. unsigned Align) const {
  524. // Otherwise, if the alignment is less than or equal to the minimum ABI
  525. // alignment, just use the default; the backend will handle this.
  526. if (Align <= MinABIStackAlignInBytes)
  527. return 0; // Use default alignment.
  528. // On non-Darwin, the stack type alignment is always 4.
  529. if (!IsDarwinVectorABI) {
  530. // Set explicit alignment, since we may need to realign the top.
  531. return MinABIStackAlignInBytes;
  532. }
  533. // Otherwise, if the type contains an SSE vector type, the alignment is 16.
  534. if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
  535. return 16;
  536. return MinABIStackAlignInBytes;
  537. }
  538. ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
  539. if (!ByVal)
  540. return ABIArgInfo::getIndirect(0, false);
  541. // Compute the byval alignment.
  542. unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
  543. unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
  544. if (StackAlign == 0)
  545. return ABIArgInfo::getIndirect(4);
  546. // If the stack alignment is less than the type alignment, realign the
  547. // argument.
  548. if (StackAlign < TypeAlign)
  549. return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
  550. /*Realign=*/true);
  551. return ABIArgInfo::getIndirect(StackAlign);
  552. }
  553. ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
  554. // FIXME: Set alignment on indirect arguments.
  555. if (isAggregateTypeForABI(Ty)) {
  556. // Structures with flexible arrays are always indirect.
  557. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  558. // Structures with either a non-trivial destructor or a non-trivial
  559. // copy constructor are always indirect.
  560. if (hasNonTrivialDestructorOrCopyConstructor(RT))
  561. return getIndirectResult(Ty, /*ByVal=*/false);
  562. if (RT->getDecl()->hasFlexibleArrayMember())
  563. return getIndirectResult(Ty);
  564. }
  565. // Ignore empty structs/unions.
  566. if (isEmptyRecord(getContext(), Ty, true))
  567. return ABIArgInfo::getIgnore();
  568. // Expand small (<= 128-bit) record types when we know that the stack layout
  569. // of those arguments will match the struct. This is important because the
  570. // LLVM backend isn't smart enough to remove byval, which inhibits many
  571. // optimizations.
  572. if (getContext().getTypeSize(Ty) <= 4*32 &&
  573. canExpandIndirectArgument(Ty, getContext()))
  574. return ABIArgInfo::getExpand();
  575. return getIndirectResult(Ty);
  576. }
  577. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  578. // On Darwin, some vectors are passed in memory, we handle this by passing
  579. // it as an i8/i16/i32/i64.
  580. if (IsDarwinVectorABI) {
  581. uint64_t Size = getContext().getTypeSize(Ty);
  582. if ((Size == 8 || Size == 16 || Size == 32) ||
  583. (Size == 64 && VT->getNumElements() == 1))
  584. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  585. Size));
  586. }
  587. llvm::Type *IRType = CGT.ConvertType(Ty);
  588. if (UseX86_MMXType(IRType)) {
  589. if (IsMMXDisabled)
  590. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  591. 64));
  592. ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
  593. AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
  594. return AAI;
  595. }
  596. return ABIArgInfo::getDirect();
  597. }
  598. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  599. Ty = EnumTy->getDecl()->getIntegerType();
  600. return (Ty->isPromotableIntegerType() ?
  601. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  602. }
  603. llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  604. CodeGenFunction &CGF) const {
  605. llvm::Type *BPP = CGF.Int8PtrPtrTy;
  606. CGBuilderTy &Builder = CGF.Builder;
  607. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
  608. "ap");
  609. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  610. // Compute if the address needs to be aligned
  611. unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
  612. Align = getTypeStackAlignInBytes(Ty, Align);
  613. Align = std::max(Align, 4U);
  614. if (Align > 4) {
  615. // addr = (addr + align - 1) & -align;
  616. llvm::Value *Offset =
  617. llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
  618. Addr = CGF.Builder.CreateGEP(Addr, Offset);
  619. llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
  620. CGF.Int32Ty);
  621. llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
  622. Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
  623. Addr->getType(),
  624. "ap.cur.aligned");
  625. }
  626. llvm::Type *PTy =
  627. llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
  628. llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
  629. uint64_t Offset =
  630. llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
  631. llvm::Value *NextAddr =
  632. Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
  633. "ap.next");
  634. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  635. return AddrTyped;
  636. }
  637. void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
  638. llvm::GlobalValue *GV,
  639. CodeGen::CodeGenModule &CGM) const {
  640. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  641. if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
  642. // Get the LLVM function.
  643. llvm::Function *Fn = cast<llvm::Function>(GV);
  644. // Now add the 'alignstack' attribute with a value of 16.
  645. Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
  646. }
  647. }
  648. }
  649. bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
  650. CodeGen::CodeGenFunction &CGF,
  651. llvm::Value *Address) const {
  652. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  653. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  654. // 0-7 are the eight integer registers; the order is different
  655. // on Darwin (for EH), but the range is the same.
  656. // 8 is %eip.
  657. AssignToArrayRange(Builder, Address, Four8, 0, 8);
  658. if (CGF.CGM.isTargetDarwin()) {
  659. // 12-16 are st(0..4). Not sure why we stop at 4.
  660. // These have size 16, which is sizeof(long double) on
  661. // platforms with 8-byte alignment for that type.
  662. llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
  663. AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
  664. } else {
  665. // 9 is %eflags, which doesn't get a size on Darwin for some
  666. // reason.
  667. Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
  668. // 11-16 are st(0..5). Not sure why we stop at 5.
  669. // These have size 12, which is sizeof(long double) on
  670. // platforms with 4-byte alignment for that type.
  671. llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
  672. AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
  673. }
  674. return false;
  675. }
  676. //===----------------------------------------------------------------------===//
  677. // X86-64 ABI Implementation
  678. //===----------------------------------------------------------------------===//
  679. namespace {
  680. /// X86_64ABIInfo - The X86_64 ABI information.
  681. class X86_64ABIInfo : public ABIInfo {
  682. enum Class {
  683. Integer = 0,
  684. SSE,
  685. SSEUp,
  686. X87,
  687. X87Up,
  688. ComplexX87,
  689. NoClass,
  690. Memory
  691. };
  692. /// merge - Implement the X86_64 ABI merging algorithm.
  693. ///
  694. /// Merge an accumulating classification \arg Accum with a field
  695. /// classification \arg Field.
  696. ///
  697. /// \param Accum - The accumulating classification. This should
  698. /// always be either NoClass or the result of a previous merge
  699. /// call. In addition, this should never be Memory (the caller
  700. /// should just return Memory for the aggregate).
  701. static Class merge(Class Accum, Class Field);
  702. /// postMerge - Implement the X86_64 ABI post merging algorithm.
  703. ///
  704. /// Post merger cleanup, reduces a malformed Hi and Lo pair to
  705. /// final MEMORY or SSE classes when necessary.
  706. ///
  707. /// \param AggregateSize - The size of the current aggregate in
  708. /// the classification process.
  709. ///
  710. /// \param Lo - The classification for the parts of the type
  711. /// residing in the low word of the containing object.
  712. ///
  713. /// \param Hi - The classification for the parts of the type
  714. /// residing in the higher words of the containing object.
  715. ///
  716. void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
  717. /// classify - Determine the x86_64 register classes in which the
  718. /// given type T should be passed.
  719. ///
  720. /// \param Lo - The classification for the parts of the type
  721. /// residing in the low word of the containing object.
  722. ///
  723. /// \param Hi - The classification for the parts of the type
  724. /// residing in the high word of the containing object.
  725. ///
  726. /// \param OffsetBase - The bit offset of this type in the
  727. /// containing object. Some parameters are classified different
  728. /// depending on whether they straddle an eightbyte boundary.
  729. ///
  730. /// If a word is unused its result will be NoClass; if a type should
  731. /// be passed in Memory then at least the classification of \arg Lo
  732. /// will be Memory.
  733. ///
  734. /// The \arg Lo class will be NoClass iff the argument is ignored.
  735. ///
  736. /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
  737. /// also be ComplexX87.
  738. void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
  739. llvm::Type *GetByteVectorType(QualType Ty) const;
  740. llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
  741. unsigned IROffset, QualType SourceTy,
  742. unsigned SourceOffset) const;
  743. llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
  744. unsigned IROffset, QualType SourceTy,
  745. unsigned SourceOffset) const;
  746. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  747. /// such that the argument will be returned in memory.
  748. ABIArgInfo getIndirectReturnResult(QualType Ty) const;
  749. /// getIndirectResult - Give a source type \arg Ty, return a suitable result
  750. /// such that the argument will be passed in memory.
  751. ABIArgInfo getIndirectResult(QualType Ty) const;
  752. ABIArgInfo classifyReturnType(QualType RetTy) const;
  753. ABIArgInfo classifyArgumentType(QualType Ty,
  754. unsigned &neededInt,
  755. unsigned &neededSSE) const;
  756. bool IsIllegalVectorType(QualType Ty) const;
  757. /// The 0.98 ABI revision clarified a lot of ambiguities,
  758. /// unfortunately in ways that were not always consistent with
  759. /// certain previous compilers. In particular, platforms which
  760. /// required strict binary compatibility with older versions of GCC
  761. /// may need to exempt themselves.
  762. bool honorsRevision0_98() const {
  763. return !getContext().getTargetInfo().getTriple().isOSDarwin();
  764. }
  765. bool HasAVX;
  766. public:
  767. X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
  768. ABIInfo(CGT), HasAVX(hasavx) {}
  769. bool isPassedUsingAVXType(QualType type) const {
  770. unsigned neededInt, neededSSE;
  771. ABIArgInfo info = classifyArgumentType(type, neededInt, neededSSE);
  772. if (info.isDirect()) {
  773. llvm::Type *ty = info.getCoerceToType();
  774. if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
  775. return (vectorTy->getBitWidth() > 128);
  776. }
  777. return false;
  778. }
  779. virtual void computeInfo(CGFunctionInfo &FI) const;
  780. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  781. CodeGenFunction &CGF) const;
  782. };
  783. /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
  784. class WinX86_64ABIInfo : public ABIInfo {
  785. ABIArgInfo classify(QualType Ty) const;
  786. public:
  787. WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
  788. virtual void computeInfo(CGFunctionInfo &FI) const;
  789. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  790. CodeGenFunction &CGF) const;
  791. };
  792. class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  793. public:
  794. X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
  795. : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
  796. const X86_64ABIInfo &getABIInfo() const {
  797. return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
  798. }
  799. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
  800. return 7;
  801. }
  802. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  803. llvm::Value *Address) const {
  804. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  805. // 0-15 are the 16 integer registers.
  806. // 16 is %rip.
  807. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  808. return false;
  809. }
  810. llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
  811. StringRef Constraint,
  812. llvm::Type* Ty) const {
  813. return X86AdjustInlineAsmType(CGF, Constraint, Ty);
  814. }
  815. bool isNoProtoCallVariadic(const CallArgList &args,
  816. const FunctionNoProtoType *fnType) const {
  817. // The default CC on x86-64 sets %al to the number of SSA
  818. // registers used, and GCC sets this when calling an unprototyped
  819. // function, so we override the default behavior. However, don't do
  820. // that when AVX types are involved: the ABI explicitly states it is
  821. // undefined, and it doesn't work in practice because of how the ABI
  822. // defines varargs anyway.
  823. if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
  824. bool HasAVXType = false;
  825. for (CallArgList::const_iterator
  826. it = args.begin(), ie = args.end(); it != ie; ++it) {
  827. if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
  828. HasAVXType = true;
  829. break;
  830. }
  831. }
  832. if (!HasAVXType)
  833. return true;
  834. }
  835. return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
  836. }
  837. };
  838. class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
  839. public:
  840. WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
  841. : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
  842. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
  843. return 7;
  844. }
  845. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  846. llvm::Value *Address) const {
  847. llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
  848. // 0-15 are the 16 integer registers.
  849. // 16 is %rip.
  850. AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
  851. return false;
  852. }
  853. };
  854. }
  855. void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
  856. Class &Hi) const {
  857. // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
  858. //
  859. // (a) If one of the classes is Memory, the whole argument is passed in
  860. // memory.
  861. //
  862. // (b) If X87UP is not preceded by X87, the whole argument is passed in
  863. // memory.
  864. //
  865. // (c) If the size of the aggregate exceeds two eightbytes and the first
  866. // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
  867. // argument is passed in memory. NOTE: This is necessary to keep the
  868. // ABI working for processors that don't support the __m256 type.
  869. //
  870. // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
  871. //
  872. // Some of these are enforced by the merging logic. Others can arise
  873. // only with unions; for example:
  874. // union { _Complex double; unsigned; }
  875. //
  876. // Note that clauses (b) and (c) were added in 0.98.
  877. //
  878. if (Hi == Memory)
  879. Lo = Memory;
  880. if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
  881. Lo = Memory;
  882. if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
  883. Lo = Memory;
  884. if (Hi == SSEUp && Lo != SSE)
  885. Hi = SSE;
  886. }
  887. X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
  888. // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
  889. // classified recursively so that always two fields are
  890. // considered. The resulting class is calculated according to
  891. // the classes of the fields in the eightbyte:
  892. //
  893. // (a) If both classes are equal, this is the resulting class.
  894. //
  895. // (b) If one of the classes is NO_CLASS, the resulting class is
  896. // the other class.
  897. //
  898. // (c) If one of the classes is MEMORY, the result is the MEMORY
  899. // class.
  900. //
  901. // (d) If one of the classes is INTEGER, the result is the
  902. // INTEGER.
  903. //
  904. // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
  905. // MEMORY is used as class.
  906. //
  907. // (f) Otherwise class SSE is used.
  908. // Accum should never be memory (we should have returned) or
  909. // ComplexX87 (because this cannot be passed in a structure).
  910. assert((Accum != Memory && Accum != ComplexX87) &&
  911. "Invalid accumulated classification during merge.");
  912. if (Accum == Field || Field == NoClass)
  913. return Accum;
  914. if (Field == Memory)
  915. return Memory;
  916. if (Accum == NoClass)
  917. return Field;
  918. if (Accum == Integer || Field == Integer)
  919. return Integer;
  920. if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
  921. Accum == X87 || Accum == X87Up)
  922. return Memory;
  923. return SSE;
  924. }
  925. void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
  926. Class &Lo, Class &Hi) const {
  927. // FIXME: This code can be simplified by introducing a simple value class for
  928. // Class pairs with appropriate constructor methods for the various
  929. // situations.
  930. // FIXME: Some of the split computations are wrong; unaligned vectors
  931. // shouldn't be passed in registers for example, so there is no chance they
  932. // can straddle an eightbyte. Verify & simplify.
  933. Lo = Hi = NoClass;
  934. Class &Current = OffsetBase < 64 ? Lo : Hi;
  935. Current = Memory;
  936. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  937. BuiltinType::Kind k = BT->getKind();
  938. if (k == BuiltinType::Void) {
  939. Current = NoClass;
  940. } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
  941. Lo = Integer;
  942. Hi = Integer;
  943. } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
  944. Current = Integer;
  945. } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
  946. Current = SSE;
  947. } else if (k == BuiltinType::LongDouble) {
  948. Lo = X87;
  949. Hi = X87Up;
  950. }
  951. // FIXME: _Decimal32 and _Decimal64 are SSE.
  952. // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
  953. return;
  954. }
  955. if (const EnumType *ET = Ty->getAs<EnumType>()) {
  956. // Classify the underlying integer type.
  957. classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
  958. return;
  959. }
  960. if (Ty->hasPointerRepresentation()) {
  961. Current = Integer;
  962. return;
  963. }
  964. if (Ty->isMemberPointerType()) {
  965. if (Ty->isMemberFunctionPointerType())
  966. Lo = Hi = Integer;
  967. else
  968. Current = Integer;
  969. return;
  970. }
  971. if (const VectorType *VT = Ty->getAs<VectorType>()) {
  972. uint64_t Size = getContext().getTypeSize(VT);
  973. if (Size == 32) {
  974. // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
  975. // float> as integer.
  976. Current = Integer;
  977. // If this type crosses an eightbyte boundary, it should be
  978. // split.
  979. uint64_t EB_Real = (OffsetBase) / 64;
  980. uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
  981. if (EB_Real != EB_Imag)
  982. Hi = Lo;
  983. } else if (Size == 64) {
  984. // gcc passes <1 x double> in memory. :(
  985. if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
  986. return;
  987. // gcc passes <1 x long long> as INTEGER.
  988. if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
  989. VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
  990. VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
  991. VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
  992. Current = Integer;
  993. else
  994. Current = SSE;
  995. // If this type crosses an eightbyte boundary, it should be
  996. // split.
  997. if (OffsetBase && OffsetBase != 64)
  998. Hi = Lo;
  999. } else if (Size == 128 || (HasAVX && Size == 256)) {
  1000. // Arguments of 256-bits are split into four eightbyte chunks. The
  1001. // least significant one belongs to class SSE and all the others to class
  1002. // SSEUP. The original Lo and Hi design considers that types can't be
  1003. // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
  1004. // This design isn't correct for 256-bits, but since there're no cases
  1005. // where the upper parts would need to be inspected, avoid adding
  1006. // complexity and just consider Hi to match the 64-256 part.
  1007. Lo = SSE;
  1008. Hi = SSEUp;
  1009. }
  1010. return;
  1011. }
  1012. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  1013. QualType ET = getContext().getCanonicalType(CT->getElementType());
  1014. uint64_t Size = getContext().getTypeSize(Ty);
  1015. if (ET->isIntegralOrEnumerationType()) {
  1016. if (Size <= 64)
  1017. Current = Integer;
  1018. else if (Size <= 128)
  1019. Lo = Hi = Integer;
  1020. } else if (ET == getContext().FloatTy)
  1021. Current = SSE;
  1022. else if (ET == getContext().DoubleTy)
  1023. Lo = Hi = SSE;
  1024. else if (ET == getContext().LongDoubleTy)
  1025. Current = ComplexX87;
  1026. // If this complex type crosses an eightbyte boundary then it
  1027. // should be split.
  1028. uint64_t EB_Real = (OffsetBase) / 64;
  1029. uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
  1030. if (Hi == NoClass && EB_Real != EB_Imag)
  1031. Hi = Lo;
  1032. return;
  1033. }
  1034. if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
  1035. // Arrays are treated like structures.
  1036. uint64_t Size = getContext().getTypeSize(Ty);
  1037. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  1038. // than four eightbytes, ..., it has class MEMORY.
  1039. if (Size > 256)
  1040. return;
  1041. // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
  1042. // fields, it has class MEMORY.
  1043. //
  1044. // Only need to check alignment of array base.
  1045. if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
  1046. return;
  1047. // Otherwise implement simplified merge. We could be smarter about
  1048. // this, but it isn't worth it and would be harder to verify.
  1049. Current = NoClass;
  1050. uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
  1051. uint64_t ArraySize = AT->getSize().getZExtValue();
  1052. // The only case a 256-bit wide vector could be used is when the array
  1053. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  1054. // to work for sizes wider than 128, early check and fallback to memory.
  1055. if (Size > 128 && EltSize != 256)
  1056. return;
  1057. for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
  1058. Class FieldLo, FieldHi;
  1059. classify(AT->getElementType(), Offset, FieldLo, FieldHi);
  1060. Lo = merge(Lo, FieldLo);
  1061. Hi = merge(Hi, FieldHi);
  1062. if (Lo == Memory || Hi == Memory)
  1063. break;
  1064. }
  1065. postMerge(Size, Lo, Hi);
  1066. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
  1067. return;
  1068. }
  1069. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  1070. uint64_t Size = getContext().getTypeSize(Ty);
  1071. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
  1072. // than four eightbytes, ..., it has class MEMORY.
  1073. if (Size > 256)
  1074. return;
  1075. // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
  1076. // copy constructor or a non-trivial destructor, it is passed by invisible
  1077. // reference.
  1078. if (hasNonTrivialDestructorOrCopyConstructor(RT))
  1079. return;
  1080. const RecordDecl *RD = RT->getDecl();
  1081. // Assume variable sized types are passed in memory.
  1082. if (RD->hasFlexibleArrayMember())
  1083. return;
  1084. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  1085. // Reset Lo class, this will be recomputed.
  1086. Current = NoClass;
  1087. // If this is a C++ record, classify the bases first.
  1088. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  1089. for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
  1090. e = CXXRD->bases_end(); i != e; ++i) {
  1091. assert(!i->isVirtual() && !i->getType()->isDependentType() &&
  1092. "Unexpected base class!");
  1093. const CXXRecordDecl *Base =
  1094. cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
  1095. // Classify this field.
  1096. //
  1097. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
  1098. // single eightbyte, each is classified separately. Each eightbyte gets
  1099. // initialized to class NO_CLASS.
  1100. Class FieldLo, FieldHi;
  1101. uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
  1102. classify(i->getType(), Offset, FieldLo, FieldHi);
  1103. Lo = merge(Lo, FieldLo);
  1104. Hi = merge(Hi, FieldHi);
  1105. if (Lo == Memory || Hi == Memory)
  1106. break;
  1107. }
  1108. }
  1109. // Classify the fields one at a time, merging the results.
  1110. unsigned idx = 0;
  1111. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  1112. i != e; ++i, ++idx) {
  1113. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  1114. bool BitField = i->isBitField();
  1115. // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
  1116. // four eightbytes, or it contains unaligned fields, it has class MEMORY.
  1117. //
  1118. // The only case a 256-bit wide vector could be used is when the struct
  1119. // contains a single 256-bit element. Since Lo and Hi logic isn't extended
  1120. // to work for sizes wider than 128, early check and fallback to memory.
  1121. //
  1122. if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
  1123. Lo = Memory;
  1124. return;
  1125. }
  1126. // Note, skip this test for bit-fields, see below.
  1127. if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
  1128. Lo = Memory;
  1129. return;
  1130. }
  1131. // Classify this field.
  1132. //
  1133. // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
  1134. // exceeds a single eightbyte, each is classified
  1135. // separately. Each eightbyte gets initialized to class
  1136. // NO_CLASS.
  1137. Class FieldLo, FieldHi;
  1138. // Bit-fields require special handling, they do not force the
  1139. // structure to be passed in memory even if unaligned, and
  1140. // therefore they can straddle an eightbyte.
  1141. if (BitField) {
  1142. // Ignore padding bit-fields.
  1143. if (i->isUnnamedBitfield())
  1144. continue;
  1145. uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
  1146. uint64_t Size = i->getBitWidthValue(getContext());
  1147. uint64_t EB_Lo = Offset / 64;
  1148. uint64_t EB_Hi = (Offset + Size - 1) / 64;
  1149. FieldLo = FieldHi = NoClass;
  1150. if (EB_Lo) {
  1151. assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
  1152. FieldLo = NoClass;
  1153. FieldHi = Integer;
  1154. } else {
  1155. FieldLo = Integer;
  1156. FieldHi = EB_Hi ? Integer : NoClass;
  1157. }
  1158. } else
  1159. classify(i->getType(), Offset, FieldLo, FieldHi);
  1160. Lo = merge(Lo, FieldLo);
  1161. Hi = merge(Hi, FieldHi);
  1162. if (Lo == Memory || Hi == Memory)
  1163. break;
  1164. }
  1165. postMerge(Size, Lo, Hi);
  1166. }
  1167. }
  1168. ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
  1169. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  1170. // place naturally.
  1171. if (!isAggregateTypeForABI(Ty)) {
  1172. // Treat an enum type as its underlying type.
  1173. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1174. Ty = EnumTy->getDecl()->getIntegerType();
  1175. return (Ty->isPromotableIntegerType() ?
  1176. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  1177. }
  1178. return ABIArgInfo::getIndirect(0);
  1179. }
  1180. bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
  1181. if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
  1182. uint64_t Size = getContext().getTypeSize(VecTy);
  1183. unsigned LargestVector = HasAVX ? 256 : 128;
  1184. if (Size <= 64 || Size > LargestVector)
  1185. return true;
  1186. }
  1187. return false;
  1188. }
  1189. ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const {
  1190. // If this is a scalar LLVM value then assume LLVM will pass it in the right
  1191. // place naturally.
  1192. if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
  1193. // Treat an enum type as its underlying type.
  1194. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1195. Ty = EnumTy->getDecl()->getIntegerType();
  1196. return (Ty->isPromotableIntegerType() ?
  1197. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  1198. }
  1199. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
  1200. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  1201. // Compute the byval alignment. We specify the alignment of the byval in all
  1202. // cases so that the mid-level optimizer knows the alignment of the byval.
  1203. unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
  1204. return ABIArgInfo::getIndirect(Align);
  1205. }
  1206. /// GetByteVectorType - The ABI specifies that a value should be passed in an
  1207. /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a
  1208. /// vector register.
  1209. llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
  1210. llvm::Type *IRType = CGT.ConvertType(Ty);
  1211. // Wrapper structs that just contain vectors are passed just like vectors,
  1212. // strip them off if present.
  1213. llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
  1214. while (STy && STy->getNumElements() == 1) {
  1215. IRType = STy->getElementType(0);
  1216. STy = dyn_cast<llvm::StructType>(IRType);
  1217. }
  1218. // If the preferred type is a 16-byte vector, prefer to pass it.
  1219. if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
  1220. llvm::Type *EltTy = VT->getElementType();
  1221. unsigned BitWidth = VT->getBitWidth();
  1222. if ((BitWidth >= 128 && BitWidth <= 256) &&
  1223. (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
  1224. EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
  1225. EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
  1226. EltTy->isIntegerTy(128)))
  1227. return VT;
  1228. }
  1229. return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
  1230. }
  1231. /// BitsContainNoUserData - Return true if the specified [start,end) bit range
  1232. /// is known to either be off the end of the specified type or being in
  1233. /// alignment padding. The user type specified is known to be at most 128 bits
  1234. /// in size, and have passed through X86_64ABIInfo::classify with a successful
  1235. /// classification that put one of the two halves in the INTEGER class.
  1236. ///
  1237. /// It is conservatively correct to return false.
  1238. static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
  1239. unsigned EndBit, ASTContext &Context) {
  1240. // If the bytes being queried are off the end of the type, there is no user
  1241. // data hiding here. This handles analysis of builtins, vectors and other
  1242. // types that don't contain interesting padding.
  1243. unsigned TySize = (unsigned)Context.getTypeSize(Ty);
  1244. if (TySize <= StartBit)
  1245. return true;
  1246. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  1247. unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
  1248. unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
  1249. // Check each element to see if the element overlaps with the queried range.
  1250. for (unsigned i = 0; i != NumElts; ++i) {
  1251. // If the element is after the span we care about, then we're done..
  1252. unsigned EltOffset = i*EltSize;
  1253. if (EltOffset >= EndBit) break;
  1254. unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
  1255. if (!BitsContainNoUserData(AT->getElementType(), EltStart,
  1256. EndBit-EltOffset, Context))
  1257. return false;
  1258. }
  1259. // If it overlaps no elements, then it is safe to process as padding.
  1260. return true;
  1261. }
  1262. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  1263. const RecordDecl *RD = RT->getDecl();
  1264. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  1265. // If this is a C++ record, check the bases first.
  1266. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  1267. for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
  1268. e = CXXRD->bases_end(); i != e; ++i) {
  1269. assert(!i->isVirtual() && !i->getType()->isDependentType() &&
  1270. "Unexpected base class!");
  1271. const CXXRecordDecl *Base =
  1272. cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
  1273. // If the base is after the span we care about, ignore it.
  1274. unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
  1275. if (BaseOffset >= EndBit) continue;
  1276. unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
  1277. if (!BitsContainNoUserData(i->getType(), BaseStart,
  1278. EndBit-BaseOffset, Context))
  1279. return false;
  1280. }
  1281. }
  1282. // Verify that no field has data that overlaps the region of interest. Yes
  1283. // this could be sped up a lot by being smarter about queried fields,
  1284. // however we're only looking at structs up to 16 bytes, so we don't care
  1285. // much.
  1286. unsigned idx = 0;
  1287. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  1288. i != e; ++i, ++idx) {
  1289. unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
  1290. // If we found a field after the region we care about, then we're done.
  1291. if (FieldOffset >= EndBit) break;
  1292. unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
  1293. if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
  1294. Context))
  1295. return false;
  1296. }
  1297. // If nothing in this record overlapped the area of interest, then we're
  1298. // clean.
  1299. return true;
  1300. }
  1301. return false;
  1302. }
  1303. /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
  1304. /// float member at the specified offset. For example, {int,{float}} has a
  1305. /// float at offset 4. It is conservatively correct for this routine to return
  1306. /// false.
  1307. static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
  1308. const llvm::TargetData &TD) {
  1309. // Base case if we find a float.
  1310. if (IROffset == 0 && IRType->isFloatTy())
  1311. return true;
  1312. // If this is a struct, recurse into the field at the specified offset.
  1313. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  1314. const llvm::StructLayout *SL = TD.getStructLayout(STy);
  1315. unsigned Elt = SL->getElementContainingOffset(IROffset);
  1316. IROffset -= SL->getElementOffset(Elt);
  1317. return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
  1318. }
  1319. // If this is an array, recurse into the field at the specified offset.
  1320. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  1321. llvm::Type *EltTy = ATy->getElementType();
  1322. unsigned EltSize = TD.getTypeAllocSize(EltTy);
  1323. IROffset -= IROffset/EltSize*EltSize;
  1324. return ContainsFloatAtOffset(EltTy, IROffset, TD);
  1325. }
  1326. return false;
  1327. }
  1328. /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
  1329. /// low 8 bytes of an XMM register, corresponding to the SSE class.
  1330. llvm::Type *X86_64ABIInfo::
  1331. GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  1332. QualType SourceTy, unsigned SourceOffset) const {
  1333. // The only three choices we have are either double, <2 x float>, or float. We
  1334. // pass as float if the last 4 bytes is just padding. This happens for
  1335. // structs that contain 3 floats.
  1336. if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
  1337. SourceOffset*8+64, getContext()))
  1338. return llvm::Type::getFloatTy(getVMContext());
  1339. // We want to pass as <2 x float> if the LLVM IR type contains a float at
  1340. // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the
  1341. // case.
  1342. if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
  1343. ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
  1344. return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
  1345. return llvm::Type::getDoubleTy(getVMContext());
  1346. }
  1347. /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
  1348. /// an 8-byte GPR. This means that we either have a scalar or we are talking
  1349. /// about the high or low part of an up-to-16-byte struct. This routine picks
  1350. /// the best LLVM IR type to represent this, which may be i64 or may be anything
  1351. /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
  1352. /// etc).
  1353. ///
  1354. /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
  1355. /// the source type. IROffset is an offset in bytes into the LLVM IR type that
  1356. /// the 8-byte value references. PrefType may be null.
  1357. ///
  1358. /// SourceTy is the source level type for the entire argument. SourceOffset is
  1359. /// an offset into this that we're processing (which is always either 0 or 8).
  1360. ///
  1361. llvm::Type *X86_64ABIInfo::
  1362. GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
  1363. QualType SourceTy, unsigned SourceOffset) const {
  1364. // If we're dealing with an un-offset LLVM IR type, then it means that we're
  1365. // returning an 8-byte unit starting with it. See if we can safely use it.
  1366. if (IROffset == 0) {
  1367. // Pointers and int64's always fill the 8-byte unit.
  1368. if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
  1369. return IRType;
  1370. // If we have a 1/2/4-byte integer, we can use it only if the rest of the
  1371. // goodness in the source type is just tail padding. This is allowed to
  1372. // kick in for struct {double,int} on the int, but not on
  1373. // struct{double,int,int} because we wouldn't return the second int. We
  1374. // have to do this analysis on the source type because we can't depend on
  1375. // unions being lowered a specific way etc.
  1376. if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
  1377. IRType->isIntegerTy(32)) {
  1378. unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
  1379. if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
  1380. SourceOffset*8+64, getContext()))
  1381. return IRType;
  1382. }
  1383. }
  1384. if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
  1385. // If this is a struct, recurse into the field at the specified offset.
  1386. const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
  1387. if (IROffset < SL->getSizeInBytes()) {
  1388. unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
  1389. IROffset -= SL->getElementOffset(FieldIdx);
  1390. return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
  1391. SourceTy, SourceOffset);
  1392. }
  1393. }
  1394. if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
  1395. llvm::Type *EltTy = ATy->getElementType();
  1396. unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
  1397. unsigned EltOffset = IROffset/EltSize*EltSize;
  1398. return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
  1399. SourceOffset);
  1400. }
  1401. // Okay, we don't have any better idea of what to pass, so we pass this in an
  1402. // integer register that isn't too big to fit the rest of the struct.
  1403. unsigned TySizeInBytes =
  1404. (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
  1405. assert(TySizeInBytes != SourceOffset && "Empty field?");
  1406. // It is always safe to classify this as an integer type up to i64 that
  1407. // isn't larger than the structure.
  1408. return llvm::IntegerType::get(getVMContext(),
  1409. std::min(TySizeInBytes-SourceOffset, 8U)*8);
  1410. }
  1411. /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
  1412. /// be used as elements of a two register pair to pass or return, return a
  1413. /// first class aggregate to represent them. For example, if the low part of
  1414. /// a by-value argument should be passed as i32* and the high part as float,
  1415. /// return {i32*, float}.
  1416. static llvm::Type *
  1417. GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
  1418. const llvm::TargetData &TD) {
  1419. // In order to correctly satisfy the ABI, we need to the high part to start
  1420. // at offset 8. If the high and low parts we inferred are both 4-byte types
  1421. // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
  1422. // the second element at offset 8. Check for this:
  1423. unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
  1424. unsigned HiAlign = TD.getABITypeAlignment(Hi);
  1425. unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
  1426. assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
  1427. // To handle this, we have to increase the size of the low part so that the
  1428. // second element will start at an 8 byte offset. We can't increase the size
  1429. // of the second element because it might make us access off the end of the
  1430. // struct.
  1431. if (HiStart != 8) {
  1432. // There are only two sorts of types the ABI generation code can produce for
  1433. // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
  1434. // Promote these to a larger type.
  1435. if (Lo->isFloatTy())
  1436. Lo = llvm::Type::getDoubleTy(Lo->getContext());
  1437. else {
  1438. assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
  1439. Lo = llvm::Type::getInt64Ty(Lo->getContext());
  1440. }
  1441. }
  1442. llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
  1443. // Verify that the second element is at an 8-byte offset.
  1444. assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
  1445. "Invalid x86-64 argument pair!");
  1446. return Result;
  1447. }
  1448. ABIArgInfo X86_64ABIInfo::
  1449. classifyReturnType(QualType RetTy) const {
  1450. // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
  1451. // classification algorithm.
  1452. X86_64ABIInfo::Class Lo, Hi;
  1453. classify(RetTy, 0, Lo, Hi);
  1454. // Check some invariants.
  1455. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  1456. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  1457. llvm::Type *ResType = 0;
  1458. switch (Lo) {
  1459. case NoClass:
  1460. if (Hi == NoClass)
  1461. return ABIArgInfo::getIgnore();
  1462. // If the low part is just padding, it takes no register, leave ResType
  1463. // null.
  1464. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  1465. "Unknown missing lo part");
  1466. break;
  1467. case SSEUp:
  1468. case X87Up:
  1469. llvm_unreachable("Invalid classification for lo word.");
  1470. // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
  1471. // hidden argument.
  1472. case Memory:
  1473. return getIndirectReturnResult(RetTy);
  1474. // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
  1475. // available register of the sequence %rax, %rdx is used.
  1476. case Integer:
  1477. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  1478. // If we have a sign or zero extended integer, make sure to return Extend
  1479. // so that the parameter gets the right LLVM IR attributes.
  1480. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  1481. // Treat an enum type as its underlying type.
  1482. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  1483. RetTy = EnumTy->getDecl()->getIntegerType();
  1484. if (RetTy->isIntegralOrEnumerationType() &&
  1485. RetTy->isPromotableIntegerType())
  1486. return ABIArgInfo::getExtend();
  1487. }
  1488. break;
  1489. // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
  1490. // available SSE register of the sequence %xmm0, %xmm1 is used.
  1491. case SSE:
  1492. ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
  1493. break;
  1494. // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
  1495. // returned on the X87 stack in %st0 as 80-bit x87 number.
  1496. case X87:
  1497. ResType = llvm::Type::getX86_FP80Ty(getVMContext());
  1498. break;
  1499. // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
  1500. // part of the value is returned in %st0 and the imaginary part in
  1501. // %st1.
  1502. case ComplexX87:
  1503. assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
  1504. ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
  1505. llvm::Type::getX86_FP80Ty(getVMContext()),
  1506. NULL);
  1507. break;
  1508. }
  1509. llvm::Type *HighPart = 0;
  1510. switch (Hi) {
  1511. // Memory was handled previously and X87 should
  1512. // never occur as a hi class.
  1513. case Memory:
  1514. case X87:
  1515. llvm_unreachable("Invalid classification for hi word.");
  1516. case ComplexX87: // Previously handled.
  1517. case NoClass:
  1518. break;
  1519. case Integer:
  1520. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  1521. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  1522. return ABIArgInfo::getDirect(HighPart, 8);
  1523. break;
  1524. case SSE:
  1525. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  1526. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  1527. return ABIArgInfo::getDirect(HighPart, 8);
  1528. break;
  1529. // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
  1530. // is passed in the next available eightbyte chunk if the last used
  1531. // vector register.
  1532. //
  1533. // SSEUP should always be preceded by SSE, just widen.
  1534. case SSEUp:
  1535. assert(Lo == SSE && "Unexpected SSEUp classification.");
  1536. ResType = GetByteVectorType(RetTy);
  1537. break;
  1538. // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
  1539. // returned together with the previous X87 value in %st0.
  1540. case X87Up:
  1541. // If X87Up is preceded by X87, we don't need to do
  1542. // anything. However, in some cases with unions it may not be
  1543. // preceded by X87. In such situations we follow gcc and pass the
  1544. // extra bits in an SSE reg.
  1545. if (Lo != X87) {
  1546. HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
  1547. if (Lo == NoClass) // Return HighPart at offset 8 in memory.
  1548. return ABIArgInfo::getDirect(HighPart, 8);
  1549. }
  1550. break;
  1551. }
  1552. // If a high part was specified, merge it together with the low part. It is
  1553. // known to pass in the high eightbyte of the result. We do this by forming a
  1554. // first class struct aggregate with the high and low part: {low, high}
  1555. if (HighPart)
  1556. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
  1557. return ABIArgInfo::getDirect(ResType);
  1558. }
  1559. ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt,
  1560. unsigned &neededSSE) const {
  1561. X86_64ABIInfo::Class Lo, Hi;
  1562. classify(Ty, 0, Lo, Hi);
  1563. // Check some invariants.
  1564. // FIXME: Enforce these by construction.
  1565. assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
  1566. assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
  1567. neededInt = 0;
  1568. neededSSE = 0;
  1569. llvm::Type *ResType = 0;
  1570. switch (Lo) {
  1571. case NoClass:
  1572. if (Hi == NoClass)
  1573. return ABIArgInfo::getIgnore();
  1574. // If the low part is just padding, it takes no register, leave ResType
  1575. // null.
  1576. assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
  1577. "Unknown missing lo part");
  1578. break;
  1579. // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
  1580. // on the stack.
  1581. case Memory:
  1582. // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
  1583. // COMPLEX_X87, it is passed in memory.
  1584. case X87:
  1585. case ComplexX87:
  1586. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
  1587. ++neededInt;
  1588. return getIndirectResult(Ty);
  1589. case SSEUp:
  1590. case X87Up:
  1591. llvm_unreachable("Invalid classification for lo word.");
  1592. // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
  1593. // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
  1594. // and %r9 is used.
  1595. case Integer:
  1596. ++neededInt;
  1597. // Pick an 8-byte type based on the preferred type.
  1598. ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
  1599. // If we have a sign or zero extended integer, make sure to return Extend
  1600. // so that the parameter gets the right LLVM IR attributes.
  1601. if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
  1602. // Treat an enum type as its underlying type.
  1603. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1604. Ty = EnumTy->getDecl()->getIntegerType();
  1605. if (Ty->isIntegralOrEnumerationType() &&
  1606. Ty->isPromotableIntegerType())
  1607. return ABIArgInfo::getExtend();
  1608. }
  1609. break;
  1610. // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
  1611. // available SSE register is used, the registers are taken in the
  1612. // order from %xmm0 to %xmm7.
  1613. case SSE: {
  1614. llvm::Type *IRType = CGT.ConvertType(Ty);
  1615. ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
  1616. ++neededSSE;
  1617. break;
  1618. }
  1619. }
  1620. llvm::Type *HighPart = 0;
  1621. switch (Hi) {
  1622. // Memory was handled previously, ComplexX87 and X87 should
  1623. // never occur as hi classes, and X87Up must be preceded by X87,
  1624. // which is passed in memory.
  1625. case Memory:
  1626. case X87:
  1627. case ComplexX87:
  1628. llvm_unreachable("Invalid classification for hi word.");
  1629. case NoClass: break;
  1630. case Integer:
  1631. ++neededInt;
  1632. // Pick an 8-byte type based on the preferred type.
  1633. HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  1634. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  1635. return ABIArgInfo::getDirect(HighPart, 8);
  1636. break;
  1637. // X87Up generally doesn't occur here (long double is passed in
  1638. // memory), except in situations involving unions.
  1639. case X87Up:
  1640. case SSE:
  1641. HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
  1642. if (Lo == NoClass) // Pass HighPart at offset 8 in memory.
  1643. return ABIArgInfo::getDirect(HighPart, 8);
  1644. ++neededSSE;
  1645. break;
  1646. // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
  1647. // eightbyte is passed in the upper half of the last used SSE
  1648. // register. This only happens when 128-bit vectors are passed.
  1649. case SSEUp:
  1650. assert(Lo == SSE && "Unexpected SSEUp classification");
  1651. ResType = GetByteVectorType(Ty);
  1652. break;
  1653. }
  1654. // If a high part was specified, merge it together with the low part. It is
  1655. // known to pass in the high eightbyte of the result. We do this by forming a
  1656. // first class struct aggregate with the high and low part: {low, high}
  1657. if (HighPart)
  1658. ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
  1659. return ABIArgInfo::getDirect(ResType);
  1660. }
  1661. void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  1662. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  1663. // Keep track of the number of assigned registers.
  1664. unsigned freeIntRegs = 6, freeSSERegs = 8;
  1665. // If the return value is indirect, then the hidden argument is consuming one
  1666. // integer register.
  1667. if (FI.getReturnInfo().isIndirect())
  1668. --freeIntRegs;
  1669. // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
  1670. // get assigned (in left-to-right order) for passing as follows...
  1671. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  1672. it != ie; ++it) {
  1673. unsigned neededInt, neededSSE;
  1674. it->info = classifyArgumentType(it->type, neededInt, neededSSE);
  1675. // AMD64-ABI 3.2.3p3: If there are no registers available for any
  1676. // eightbyte of an argument, the whole argument is passed on the
  1677. // stack. If registers have already been assigned for some
  1678. // eightbytes of such an argument, the assignments get reverted.
  1679. if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
  1680. freeIntRegs -= neededInt;
  1681. freeSSERegs -= neededSSE;
  1682. } else {
  1683. it->info = getIndirectResult(it->type);
  1684. }
  1685. }
  1686. }
  1687. static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
  1688. QualType Ty,
  1689. CodeGenFunction &CGF) {
  1690. llvm::Value *overflow_arg_area_p =
  1691. CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
  1692. llvm::Value *overflow_arg_area =
  1693. CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
  1694. // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
  1695. // byte boundary if alignment needed by type exceeds 8 byte boundary.
  1696. // It isn't stated explicitly in the standard, but in practice we use
  1697. // alignment greater than 16 where necessary.
  1698. uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
  1699. if (Align > 8) {
  1700. // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
  1701. llvm::Value *Offset =
  1702. llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
  1703. overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
  1704. llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
  1705. CGF.Int64Ty);
  1706. llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
  1707. overflow_arg_area =
  1708. CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
  1709. overflow_arg_area->getType(),
  1710. "overflow_arg_area.align");
  1711. }
  1712. // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
  1713. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  1714. llvm::Value *Res =
  1715. CGF.Builder.CreateBitCast(overflow_arg_area,
  1716. llvm::PointerType::getUnqual(LTy));
  1717. // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
  1718. // l->overflow_arg_area + sizeof(type).
  1719. // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
  1720. // an 8 byte boundary.
  1721. uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
  1722. llvm::Value *Offset =
  1723. llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7);
  1724. overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
  1725. "overflow_arg_area.next");
  1726. CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
  1727. // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
  1728. return Res;
  1729. }
  1730. llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  1731. CodeGenFunction &CGF) const {
  1732. // Assume that va_list type is correct; should be pointer to LLVM type:
  1733. // struct {
  1734. // i32 gp_offset;
  1735. // i32 fp_offset;
  1736. // i8* overflow_arg_area;
  1737. // i8* reg_save_area;
  1738. // };
  1739. unsigned neededInt, neededSSE;
  1740. Ty = CGF.getContext().getCanonicalType(Ty);
  1741. ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE);
  1742. // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
  1743. // in the registers. If not go to step 7.
  1744. if (!neededInt && !neededSSE)
  1745. return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
  1746. // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
  1747. // general purpose registers needed to pass type and num_fp to hold
  1748. // the number of floating point registers needed.
  1749. // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
  1750. // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
  1751. // l->fp_offset > 304 - num_fp * 16 go to step 7.
  1752. //
  1753. // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
  1754. // register save space).
  1755. llvm::Value *InRegs = 0;
  1756. llvm::Value *gp_offset_p = 0, *gp_offset = 0;
  1757. llvm::Value *fp_offset_p = 0, *fp_offset = 0;
  1758. if (neededInt) {
  1759. gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
  1760. gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
  1761. InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
  1762. InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
  1763. }
  1764. if (neededSSE) {
  1765. fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
  1766. fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
  1767. llvm::Value *FitsInFP =
  1768. llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
  1769. FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
  1770. InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
  1771. }
  1772. llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
  1773. llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
  1774. llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
  1775. CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
  1776. // Emit code to load the value if it was passed in registers.
  1777. CGF.EmitBlock(InRegBlock);
  1778. // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
  1779. // an offset of l->gp_offset and/or l->fp_offset. This may require
  1780. // copying to a temporary location in case the parameter is passed
  1781. // in different register classes or requires an alignment greater
  1782. // than 8 for general purpose registers and 16 for XMM registers.
  1783. //
  1784. // FIXME: This really results in shameful code when we end up needing to
  1785. // collect arguments from different places; often what should result in a
  1786. // simple assembling of a structure from scattered addresses has many more
  1787. // loads than necessary. Can we clean this up?
  1788. llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
  1789. llvm::Value *RegAddr =
  1790. CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
  1791. "reg_save_area");
  1792. if (neededInt && neededSSE) {
  1793. // FIXME: Cleanup.
  1794. assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
  1795. llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
  1796. llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
  1797. assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
  1798. llvm::Type *TyLo = ST->getElementType(0);
  1799. llvm::Type *TyHi = ST->getElementType(1);
  1800. assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
  1801. "Unexpected ABI info for mixed regs");
  1802. llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
  1803. llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
  1804. llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
  1805. llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
  1806. llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
  1807. llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
  1808. llvm::Value *V =
  1809. CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
  1810. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  1811. V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
  1812. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  1813. RegAddr = CGF.Builder.CreateBitCast(Tmp,
  1814. llvm::PointerType::getUnqual(LTy));
  1815. } else if (neededInt) {
  1816. RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
  1817. RegAddr = CGF.Builder.CreateBitCast(RegAddr,
  1818. llvm::PointerType::getUnqual(LTy));
  1819. } else if (neededSSE == 1) {
  1820. RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
  1821. RegAddr = CGF.Builder.CreateBitCast(RegAddr,
  1822. llvm::PointerType::getUnqual(LTy));
  1823. } else {
  1824. assert(neededSSE == 2 && "Invalid number of needed registers!");
  1825. // SSE registers are spaced 16 bytes apart in the register save
  1826. // area, we need to collect the two eightbytes together.
  1827. llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
  1828. llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
  1829. llvm::Type *DoubleTy = CGF.DoubleTy;
  1830. llvm::Type *DblPtrTy =
  1831. llvm::PointerType::getUnqual(DoubleTy);
  1832. llvm::StructType *ST = llvm::StructType::get(DoubleTy,
  1833. DoubleTy, NULL);
  1834. llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
  1835. V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
  1836. DblPtrTy));
  1837. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
  1838. V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
  1839. DblPtrTy));
  1840. CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
  1841. RegAddr = CGF.Builder.CreateBitCast(Tmp,
  1842. llvm::PointerType::getUnqual(LTy));
  1843. }
  1844. // AMD64-ABI 3.5.7p5: Step 5. Set:
  1845. // l->gp_offset = l->gp_offset + num_gp * 8
  1846. // l->fp_offset = l->fp_offset + num_fp * 16.
  1847. if (neededInt) {
  1848. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
  1849. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
  1850. gp_offset_p);
  1851. }
  1852. if (neededSSE) {
  1853. llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
  1854. CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
  1855. fp_offset_p);
  1856. }
  1857. CGF.EmitBranch(ContBlock);
  1858. // Emit code to load the value if it was passed in memory.
  1859. CGF.EmitBlock(InMemBlock);
  1860. llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
  1861. // Return the appropriate result.
  1862. CGF.EmitBlock(ContBlock);
  1863. llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
  1864. "vaarg.addr");
  1865. ResAddr->addIncoming(RegAddr, InRegBlock);
  1866. ResAddr->addIncoming(MemAddr, InMemBlock);
  1867. return ResAddr;
  1868. }
  1869. ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
  1870. if (Ty->isVoidType())
  1871. return ABIArgInfo::getIgnore();
  1872. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  1873. Ty = EnumTy->getDecl()->getIntegerType();
  1874. uint64_t Size = getContext().getTypeSize(Ty);
  1875. if (const RecordType *RT = Ty->getAs<RecordType>()) {
  1876. if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
  1877. RT->getDecl()->hasFlexibleArrayMember())
  1878. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  1879. // FIXME: mingw-w64-gcc emits 128-bit struct as i128
  1880. if (Size == 128 &&
  1881. getContext().getTargetInfo().getTriple().getOS()
  1882. == llvm::Triple::MinGW32)
  1883. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1884. Size));
  1885. // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
  1886. // not 1, 2, 4, or 8 bytes, must be passed by reference."
  1887. if (Size <= 64 &&
  1888. (Size & (Size - 1)) == 0)
  1889. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  1890. Size));
  1891. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  1892. }
  1893. if (Ty->isPromotableIntegerType())
  1894. return ABIArgInfo::getExtend();
  1895. return ABIArgInfo::getDirect();
  1896. }
  1897. void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
  1898. QualType RetTy = FI.getReturnType();
  1899. FI.getReturnInfo() = classify(RetTy);
  1900. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  1901. it != ie; ++it)
  1902. it->info = classify(it->type);
  1903. }
  1904. llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  1905. CodeGenFunction &CGF) const {
  1906. llvm::Type *BPP = CGF.Int8PtrPtrTy;
  1907. CGBuilderTy &Builder = CGF.Builder;
  1908. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
  1909. "ap");
  1910. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  1911. llvm::Type *PTy =
  1912. llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
  1913. llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
  1914. uint64_t Offset =
  1915. llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
  1916. llvm::Value *NextAddr =
  1917. Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
  1918. "ap.next");
  1919. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  1920. return AddrTyped;
  1921. }
  1922. // PowerPC-32
  1923. namespace {
  1924. class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  1925. public:
  1926. PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
  1927. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
  1928. // This is recovered from gcc output.
  1929. return 1; // r1 is the dedicated stack pointer
  1930. }
  1931. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  1932. llvm::Value *Address) const;
  1933. };
  1934. }
  1935. bool
  1936. PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  1937. llvm::Value *Address) const {
  1938. // This is calculated from the LLVM and GCC tables and verified
  1939. // against gcc output. AFAIK all ABIs use the same encoding.
  1940. CodeGen::CGBuilderTy &Builder = CGF.Builder;
  1941. llvm::IntegerType *i8 = CGF.Int8Ty;
  1942. llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
  1943. llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
  1944. llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
  1945. // 0-31: r0-31, the 4-byte general-purpose registers
  1946. AssignToArrayRange(Builder, Address, Four8, 0, 31);
  1947. // 32-63: fp0-31, the 8-byte floating-point registers
  1948. AssignToArrayRange(Builder, Address, Eight8, 32, 63);
  1949. // 64-76 are various 4-byte special-purpose registers:
  1950. // 64: mq
  1951. // 65: lr
  1952. // 66: ctr
  1953. // 67: ap
  1954. // 68-75 cr0-7
  1955. // 76: xer
  1956. AssignToArrayRange(Builder, Address, Four8, 64, 76);
  1957. // 77-108: v0-31, the 16-byte vector registers
  1958. AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
  1959. // 109: vrsave
  1960. // 110: vscr
  1961. // 111: spe_acc
  1962. // 112: spefscr
  1963. // 113: sfp
  1964. AssignToArrayRange(Builder, Address, Four8, 109, 113);
  1965. return false;
  1966. }
  1967. //===----------------------------------------------------------------------===//
  1968. // ARM ABI Implementation
  1969. //===----------------------------------------------------------------------===//
  1970. namespace {
  1971. class ARMABIInfo : public ABIInfo {
  1972. public:
  1973. enum ABIKind {
  1974. APCS = 0,
  1975. AAPCS = 1,
  1976. AAPCS_VFP
  1977. };
  1978. private:
  1979. ABIKind Kind;
  1980. public:
  1981. ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
  1982. bool isEABI() const {
  1983. StringRef Env =
  1984. getContext().getTargetInfo().getTriple().getEnvironmentName();
  1985. return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
  1986. }
  1987. private:
  1988. ABIKind getABIKind() const { return Kind; }
  1989. ABIArgInfo classifyReturnType(QualType RetTy) const;
  1990. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  1991. virtual void computeInfo(CGFunctionInfo &FI) const;
  1992. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  1993. CodeGenFunction &CGF) const;
  1994. };
  1995. class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
  1996. public:
  1997. ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
  1998. :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
  1999. const ARMABIInfo &getABIInfo() const {
  2000. return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
  2001. }
  2002. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
  2003. return 13;
  2004. }
  2005. StringRef getARCRetainAutoreleasedReturnValueMarker() const {
  2006. return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
  2007. }
  2008. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2009. llvm::Value *Address) const {
  2010. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  2011. // 0-15 are the 16 integer registers.
  2012. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
  2013. return false;
  2014. }
  2015. unsigned getSizeOfUnwindException() const {
  2016. if (getABIInfo().isEABI()) return 88;
  2017. return TargetCodeGenInfo::getSizeOfUnwindException();
  2018. }
  2019. };
  2020. }
  2021. void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
  2022. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  2023. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  2024. it != ie; ++it)
  2025. it->info = classifyArgumentType(it->type);
  2026. // Always honor user-specified calling convention.
  2027. if (FI.getCallingConvention() != llvm::CallingConv::C)
  2028. return;
  2029. // Calling convention as default by an ABI.
  2030. llvm::CallingConv::ID DefaultCC;
  2031. if (isEABI())
  2032. DefaultCC = llvm::CallingConv::ARM_AAPCS;
  2033. else
  2034. DefaultCC = llvm::CallingConv::ARM_APCS;
  2035. // If user did not ask for specific calling convention explicitly (e.g. via
  2036. // pcs attribute), set effective calling convention if it's different than ABI
  2037. // default.
  2038. switch (getABIKind()) {
  2039. case APCS:
  2040. if (DefaultCC != llvm::CallingConv::ARM_APCS)
  2041. FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
  2042. break;
  2043. case AAPCS:
  2044. if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
  2045. FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
  2046. break;
  2047. case AAPCS_VFP:
  2048. if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
  2049. FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
  2050. break;
  2051. }
  2052. }
  2053. /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
  2054. /// aggregate. If HAMembers is non-null, the number of base elements
  2055. /// contained in the type is returned through it; this is used for the
  2056. /// recursive calls that check aggregate component types.
  2057. static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
  2058. ASTContext &Context,
  2059. uint64_t *HAMembers = 0) {
  2060. uint64_t Members;
  2061. if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
  2062. if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
  2063. return false;
  2064. Members *= AT->getSize().getZExtValue();
  2065. } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
  2066. const RecordDecl *RD = RT->getDecl();
  2067. if (RD->isUnion() || RD->hasFlexibleArrayMember())
  2068. return false;
  2069. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  2070. if (!CXXRD->isAggregate())
  2071. return false;
  2072. }
  2073. Members = 0;
  2074. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2075. i != e; ++i) {
  2076. const FieldDecl *FD = *i;
  2077. uint64_t FldMembers;
  2078. if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
  2079. return false;
  2080. Members += FldMembers;
  2081. }
  2082. } else {
  2083. Members = 1;
  2084. if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
  2085. Members = 2;
  2086. Ty = CT->getElementType();
  2087. }
  2088. // Homogeneous aggregates for AAPCS-VFP must have base types of float,
  2089. // double, or 64-bit or 128-bit vectors.
  2090. if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
  2091. if (BT->getKind() != BuiltinType::Float &&
  2092. BT->getKind() != BuiltinType::Double)
  2093. return false;
  2094. } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
  2095. unsigned VecSize = Context.getTypeSize(VT);
  2096. if (VecSize != 64 && VecSize != 128)
  2097. return false;
  2098. } else {
  2099. return false;
  2100. }
  2101. // The base type must be the same for all members. Vector types of the
  2102. // same total size are treated as being equivalent here.
  2103. const Type *TyPtr = Ty.getTypePtr();
  2104. if (!Base)
  2105. Base = TyPtr;
  2106. if (Base != TyPtr &&
  2107. (!Base->isVectorType() || !TyPtr->isVectorType() ||
  2108. Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
  2109. return false;
  2110. }
  2111. // Homogeneous Aggregates can have at most 4 members of the base type.
  2112. if (HAMembers)
  2113. *HAMembers = Members;
  2114. return (Members <= 4);
  2115. }
  2116. ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
  2117. if (!isAggregateTypeForABI(Ty)) {
  2118. // Treat an enum type as its underlying type.
  2119. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2120. Ty = EnumTy->getDecl()->getIntegerType();
  2121. return (Ty->isPromotableIntegerType() ?
  2122. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2123. }
  2124. // Ignore empty records.
  2125. if (isEmptyRecord(getContext(), Ty, true))
  2126. return ABIArgInfo::getIgnore();
  2127. // Structures with either a non-trivial destructor or a non-trivial
  2128. // copy constructor are always indirect.
  2129. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
  2130. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  2131. if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
  2132. // Homogeneous Aggregates need to be expanded.
  2133. const Type *Base = 0;
  2134. if (isHomogeneousAggregate(Ty, Base, getContext()))
  2135. return ABIArgInfo::getExpand();
  2136. }
  2137. // Otherwise, pass by coercing to a structure of the appropriate size.
  2138. //
  2139. // FIXME: This is kind of nasty... but there isn't much choice because the ARM
  2140. // backend doesn't support byval.
  2141. // FIXME: This doesn't handle alignment > 64 bits.
  2142. llvm::Type* ElemTy;
  2143. unsigned SizeRegs;
  2144. if (getContext().getTypeAlign(Ty) > 32) {
  2145. ElemTy = llvm::Type::getInt64Ty(getVMContext());
  2146. SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
  2147. } else {
  2148. ElemTy = llvm::Type::getInt32Ty(getVMContext());
  2149. SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
  2150. }
  2151. llvm::Type *STy =
  2152. llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
  2153. return ABIArgInfo::getDirect(STy);
  2154. }
  2155. static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
  2156. llvm::LLVMContext &VMContext) {
  2157. // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
  2158. // is called integer-like if its size is less than or equal to one word, and
  2159. // the offset of each of its addressable sub-fields is zero.
  2160. uint64_t Size = Context.getTypeSize(Ty);
  2161. // Check that the type fits in a word.
  2162. if (Size > 32)
  2163. return false;
  2164. // FIXME: Handle vector types!
  2165. if (Ty->isVectorType())
  2166. return false;
  2167. // Float types are never treated as "integer like".
  2168. if (Ty->isRealFloatingType())
  2169. return false;
  2170. // If this is a builtin or pointer type then it is ok.
  2171. if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
  2172. return true;
  2173. // Small complex integer types are "integer like".
  2174. if (const ComplexType *CT = Ty->getAs<ComplexType>())
  2175. return isIntegerLikeType(CT->getElementType(), Context, VMContext);
  2176. // Single element and zero sized arrays should be allowed, by the definition
  2177. // above, but they are not.
  2178. // Otherwise, it must be a record type.
  2179. const RecordType *RT = Ty->getAs<RecordType>();
  2180. if (!RT) return false;
  2181. // Ignore records with flexible arrays.
  2182. const RecordDecl *RD = RT->getDecl();
  2183. if (RD->hasFlexibleArrayMember())
  2184. return false;
  2185. // Check that all sub-fields are at offset 0, and are themselves "integer
  2186. // like".
  2187. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  2188. bool HadField = false;
  2189. unsigned idx = 0;
  2190. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2191. i != e; ++i, ++idx) {
  2192. const FieldDecl *FD = *i;
  2193. // Bit-fields are not addressable, we only need to verify they are "integer
  2194. // like". We still have to disallow a subsequent non-bitfield, for example:
  2195. // struct { int : 0; int x }
  2196. // is non-integer like according to gcc.
  2197. if (FD->isBitField()) {
  2198. if (!RD->isUnion())
  2199. HadField = true;
  2200. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  2201. return false;
  2202. continue;
  2203. }
  2204. // Check if this field is at offset 0.
  2205. if (Layout.getFieldOffset(idx) != 0)
  2206. return false;
  2207. if (!isIntegerLikeType(FD->getType(), Context, VMContext))
  2208. return false;
  2209. // Only allow at most one field in a structure. This doesn't match the
  2210. // wording above, but follows gcc in situations with a field following an
  2211. // empty structure.
  2212. if (!RD->isUnion()) {
  2213. if (HadField)
  2214. return false;
  2215. HadField = true;
  2216. }
  2217. }
  2218. return true;
  2219. }
  2220. ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
  2221. if (RetTy->isVoidType())
  2222. return ABIArgInfo::getIgnore();
  2223. // Large vector types should be returned via memory.
  2224. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
  2225. return ABIArgInfo::getIndirect(0);
  2226. if (!isAggregateTypeForABI(RetTy)) {
  2227. // Treat an enum type as its underlying type.
  2228. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  2229. RetTy = EnumTy->getDecl()->getIntegerType();
  2230. return (RetTy->isPromotableIntegerType() ?
  2231. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2232. }
  2233. // Structures with either a non-trivial destructor or a non-trivial
  2234. // copy constructor are always indirect.
  2235. if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
  2236. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  2237. // Are we following APCS?
  2238. if (getABIKind() == APCS) {
  2239. if (isEmptyRecord(getContext(), RetTy, false))
  2240. return ABIArgInfo::getIgnore();
  2241. // Complex types are all returned as packed integers.
  2242. //
  2243. // FIXME: Consider using 2 x vector types if the back end handles them
  2244. // correctly.
  2245. if (RetTy->isAnyComplexType())
  2246. return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
  2247. getContext().getTypeSize(RetTy)));
  2248. // Integer like structures are returned in r0.
  2249. if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
  2250. // Return in the smallest viable integer type.
  2251. uint64_t Size = getContext().getTypeSize(RetTy);
  2252. if (Size <= 8)
  2253. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  2254. if (Size <= 16)
  2255. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  2256. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  2257. }
  2258. // Otherwise return in memory.
  2259. return ABIArgInfo::getIndirect(0);
  2260. }
  2261. // Otherwise this is an AAPCS variant.
  2262. if (isEmptyRecord(getContext(), RetTy, true))
  2263. return ABIArgInfo::getIgnore();
  2264. // Check for homogeneous aggregates with AAPCS-VFP.
  2265. if (getABIKind() == AAPCS_VFP) {
  2266. const Type *Base = 0;
  2267. if (isHomogeneousAggregate(RetTy, Base, getContext()))
  2268. // Homogeneous Aggregates are returned directly.
  2269. return ABIArgInfo::getDirect();
  2270. }
  2271. // Aggregates <= 4 bytes are returned in r0; other aggregates
  2272. // are returned indirectly.
  2273. uint64_t Size = getContext().getTypeSize(RetTy);
  2274. if (Size <= 32) {
  2275. // Return in the smallest viable integer type.
  2276. if (Size <= 8)
  2277. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  2278. if (Size <= 16)
  2279. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  2280. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  2281. }
  2282. return ABIArgInfo::getIndirect(0);
  2283. }
  2284. llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2285. CodeGenFunction &CGF) const {
  2286. llvm::Type *BP = CGF.Int8PtrTy;
  2287. llvm::Type *BPP = CGF.Int8PtrPtrTy;
  2288. CGBuilderTy &Builder = CGF.Builder;
  2289. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
  2290. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  2291. // Handle address alignment for type alignment > 32 bits
  2292. uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
  2293. if (TyAlign > 4) {
  2294. assert((TyAlign & (TyAlign - 1)) == 0 &&
  2295. "Alignment is not power of 2!");
  2296. llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
  2297. AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
  2298. AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
  2299. Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
  2300. }
  2301. llvm::Type *PTy =
  2302. llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
  2303. llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
  2304. uint64_t Offset =
  2305. llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
  2306. llvm::Value *NextAddr =
  2307. Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
  2308. "ap.next");
  2309. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  2310. return AddrTyped;
  2311. }
  2312. //===----------------------------------------------------------------------===//
  2313. // PTX ABI Implementation
  2314. //===----------------------------------------------------------------------===//
  2315. namespace {
  2316. class PTXABIInfo : public ABIInfo {
  2317. public:
  2318. PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  2319. ABIArgInfo classifyReturnType(QualType RetTy) const;
  2320. ABIArgInfo classifyArgumentType(QualType Ty) const;
  2321. virtual void computeInfo(CGFunctionInfo &FI) const;
  2322. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2323. CodeGenFunction &CFG) const;
  2324. };
  2325. class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
  2326. public:
  2327. PTXTargetCodeGenInfo(CodeGenTypes &CGT)
  2328. : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
  2329. virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2330. CodeGen::CodeGenModule &M) const;
  2331. };
  2332. ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
  2333. if (RetTy->isVoidType())
  2334. return ABIArgInfo::getIgnore();
  2335. if (isAggregateTypeForABI(RetTy))
  2336. return ABIArgInfo::getIndirect(0);
  2337. return ABIArgInfo::getDirect();
  2338. }
  2339. ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
  2340. if (isAggregateTypeForABI(Ty))
  2341. return ABIArgInfo::getIndirect(0);
  2342. return ABIArgInfo::getDirect();
  2343. }
  2344. void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
  2345. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  2346. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  2347. it != ie; ++it)
  2348. it->info = classifyArgumentType(it->type);
  2349. // Always honor user-specified calling convention.
  2350. if (FI.getCallingConvention() != llvm::CallingConv::C)
  2351. return;
  2352. // Calling convention as default by an ABI.
  2353. llvm::CallingConv::ID DefaultCC;
  2354. const LangOptions &LangOpts = getContext().getLangOptions();
  2355. if (LangOpts.OpenCL || LangOpts.CUDA) {
  2356. // If we are in OpenCL or CUDA mode, then default to device functions
  2357. DefaultCC = llvm::CallingConv::PTX_Device;
  2358. } else {
  2359. // If we are in standard C/C++ mode, use the triple to decide on the default
  2360. StringRef Env =
  2361. getContext().getTargetInfo().getTriple().getEnvironmentName();
  2362. if (Env == "device")
  2363. DefaultCC = llvm::CallingConv::PTX_Device;
  2364. else
  2365. DefaultCC = llvm::CallingConv::PTX_Kernel;
  2366. }
  2367. FI.setEffectiveCallingConvention(DefaultCC);
  2368. }
  2369. llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2370. CodeGenFunction &CFG) const {
  2371. llvm_unreachable("PTX does not support varargs");
  2372. }
  2373. void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
  2374. llvm::GlobalValue *GV,
  2375. CodeGen::CodeGenModule &M) const{
  2376. const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
  2377. if (!FD) return;
  2378. llvm::Function *F = cast<llvm::Function>(GV);
  2379. // Perform special handling in OpenCL mode
  2380. if (M.getLangOptions().OpenCL) {
  2381. // Use OpenCL function attributes to set proper calling conventions
  2382. // By default, all functions are device functions
  2383. if (FD->hasAttr<OpenCLKernelAttr>()) {
  2384. // OpenCL __kernel functions get a kernel calling convention
  2385. F->setCallingConv(llvm::CallingConv::PTX_Kernel);
  2386. // And kernel functions are not subject to inlining
  2387. F->addFnAttr(llvm::Attribute::NoInline);
  2388. }
  2389. }
  2390. // Perform special handling in CUDA mode.
  2391. if (M.getLangOptions().CUDA) {
  2392. // CUDA __global__ functions get a kernel calling convention. Since
  2393. // __global__ functions cannot be called from the device, we do not
  2394. // need to set the noinline attribute.
  2395. if (FD->getAttr<CUDAGlobalAttr>())
  2396. F->setCallingConv(llvm::CallingConv::PTX_Kernel);
  2397. }
  2398. }
  2399. }
  2400. //===----------------------------------------------------------------------===//
  2401. // MBlaze ABI Implementation
  2402. //===----------------------------------------------------------------------===//
  2403. namespace {
  2404. class MBlazeABIInfo : public ABIInfo {
  2405. public:
  2406. MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  2407. bool isPromotableIntegerType(QualType Ty) const;
  2408. ABIArgInfo classifyReturnType(QualType RetTy) const;
  2409. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  2410. virtual void computeInfo(CGFunctionInfo &FI) const {
  2411. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  2412. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  2413. it != ie; ++it)
  2414. it->info = classifyArgumentType(it->type);
  2415. }
  2416. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2417. CodeGenFunction &CGF) const;
  2418. };
  2419. class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
  2420. public:
  2421. MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
  2422. : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
  2423. void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2424. CodeGen::CodeGenModule &M) const;
  2425. };
  2426. }
  2427. bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
  2428. // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
  2429. if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
  2430. switch (BT->getKind()) {
  2431. case BuiltinType::Bool:
  2432. case BuiltinType::Char_S:
  2433. case BuiltinType::Char_U:
  2434. case BuiltinType::SChar:
  2435. case BuiltinType::UChar:
  2436. case BuiltinType::Short:
  2437. case BuiltinType::UShort:
  2438. return true;
  2439. default:
  2440. return false;
  2441. }
  2442. return false;
  2443. }
  2444. llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2445. CodeGenFunction &CGF) const {
  2446. // FIXME: Implement
  2447. return 0;
  2448. }
  2449. ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
  2450. if (RetTy->isVoidType())
  2451. return ABIArgInfo::getIgnore();
  2452. if (isAggregateTypeForABI(RetTy))
  2453. return ABIArgInfo::getIndirect(0);
  2454. return (isPromotableIntegerType(RetTy) ?
  2455. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2456. }
  2457. ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
  2458. if (isAggregateTypeForABI(Ty))
  2459. return ABIArgInfo::getIndirect(0);
  2460. return (isPromotableIntegerType(Ty) ?
  2461. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2462. }
  2463. void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
  2464. llvm::GlobalValue *GV,
  2465. CodeGen::CodeGenModule &M)
  2466. const {
  2467. const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
  2468. if (!FD) return;
  2469. llvm::CallingConv::ID CC = llvm::CallingConv::C;
  2470. if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
  2471. CC = llvm::CallingConv::MBLAZE_INTR;
  2472. else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
  2473. CC = llvm::CallingConv::MBLAZE_SVOL;
  2474. if (CC != llvm::CallingConv::C) {
  2475. // Handle 'interrupt_handler' attribute:
  2476. llvm::Function *F = cast<llvm::Function>(GV);
  2477. // Step 1: Set ISR calling convention.
  2478. F->setCallingConv(CC);
  2479. // Step 2: Add attributes goodness.
  2480. F->addFnAttr(llvm::Attribute::NoInline);
  2481. }
  2482. // Step 3: Emit _interrupt_handler alias.
  2483. if (CC == llvm::CallingConv::MBLAZE_INTR)
  2484. new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
  2485. "_interrupt_handler", GV, &M.getModule());
  2486. }
  2487. //===----------------------------------------------------------------------===//
  2488. // MSP430 ABI Implementation
  2489. //===----------------------------------------------------------------------===//
  2490. namespace {
  2491. class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
  2492. public:
  2493. MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
  2494. : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
  2495. void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2496. CodeGen::CodeGenModule &M) const;
  2497. };
  2498. }
  2499. void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
  2500. llvm::GlobalValue *GV,
  2501. CodeGen::CodeGenModule &M) const {
  2502. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  2503. if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
  2504. // Handle 'interrupt' attribute:
  2505. llvm::Function *F = cast<llvm::Function>(GV);
  2506. // Step 1: Set ISR calling convention.
  2507. F->setCallingConv(llvm::CallingConv::MSP430_INTR);
  2508. // Step 2: Add attributes goodness.
  2509. F->addFnAttr(llvm::Attribute::NoInline);
  2510. // Step 3: Emit ISR vector alias.
  2511. unsigned Num = attr->getNumber() + 0xffe0;
  2512. new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
  2513. "vector_" + Twine::utohexstr(Num),
  2514. GV, &M.getModule());
  2515. }
  2516. }
  2517. }
  2518. //===----------------------------------------------------------------------===//
  2519. // MIPS ABI Implementation. This works for both little-endian and
  2520. // big-endian variants.
  2521. //===----------------------------------------------------------------------===//
  2522. namespace {
  2523. class MipsABIInfo : public ABIInfo {
  2524. bool IsO32;
  2525. unsigned MinABIStackAlignInBytes;
  2526. llvm::Type* HandleAggregates(QualType Ty) const;
  2527. llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
  2528. llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
  2529. public:
  2530. MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
  2531. ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
  2532. ABIArgInfo classifyReturnType(QualType RetTy) const;
  2533. ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
  2534. virtual void computeInfo(CGFunctionInfo &FI) const;
  2535. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2536. CodeGenFunction &CGF) const;
  2537. };
  2538. class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
  2539. unsigned SizeOfUnwindException;
  2540. public:
  2541. MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
  2542. : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
  2543. SizeOfUnwindException(IsO32 ? 24 : 32) {}
  2544. int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
  2545. return 29;
  2546. }
  2547. bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2548. llvm::Value *Address) const;
  2549. unsigned getSizeOfUnwindException() const {
  2550. return SizeOfUnwindException;
  2551. }
  2552. };
  2553. }
  2554. // In N32/64, an aligned double precision floating point field is passed in
  2555. // a register.
  2556. llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty) const {
  2557. if (IsO32)
  2558. return 0;
  2559. if (Ty->isComplexType())
  2560. return CGT.ConvertType(Ty);
  2561. const RecordType *RT = Ty->getAs<RecordType>();
  2562. // Unions are passed in integer registers.
  2563. if (!RT || !RT->isStructureOrClassType())
  2564. return 0;
  2565. const RecordDecl *RD = RT->getDecl();
  2566. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  2567. uint64_t StructSize = getContext().getTypeSize(Ty);
  2568. assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
  2569. uint64_t LastOffset = 0;
  2570. unsigned idx = 0;
  2571. llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
  2572. SmallVector<llvm::Type*, 8> ArgList;
  2573. // Iterate over fields in the struct/class and check if there are any aligned
  2574. // double fields.
  2575. for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
  2576. i != e; ++i, ++idx) {
  2577. const QualType Ty = (*i)->getType();
  2578. const BuiltinType *BT = Ty->getAs<BuiltinType>();
  2579. if (!BT || BT->getKind() != BuiltinType::Double)
  2580. continue;
  2581. uint64_t Offset = Layout.getFieldOffset(idx);
  2582. if (Offset % 64) // Ignore doubles that are not aligned.
  2583. continue;
  2584. // Add ((Offset - LastOffset) / 64) args of type i64.
  2585. for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
  2586. ArgList.push_back(I64);
  2587. // Add double type.
  2588. ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
  2589. LastOffset = Offset + 64;
  2590. }
  2591. // This struct/class doesn't have an aligned double field.
  2592. if (!LastOffset)
  2593. return 0;
  2594. // Add ((StructSize - LastOffset) / 64) args of type i64.
  2595. for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
  2596. ArgList.push_back(I64);
  2597. // If the size of the remainder is not zero, add one more integer type to
  2598. // ArgList.
  2599. unsigned R = (StructSize - LastOffset) % 64;
  2600. if (R)
  2601. ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
  2602. return llvm::StructType::get(getVMContext(), ArgList);
  2603. }
  2604. llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
  2605. // Padding is inserted only for N32/64.
  2606. if (IsO32)
  2607. return 0;
  2608. assert(Align <= 16 && "Alignment larger than 16 not handled.");
  2609. return (Align == 16 && Offset & 0xf) ?
  2610. llvm::IntegerType::get(getVMContext(), 64) : 0;
  2611. }
  2612. ABIArgInfo
  2613. MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
  2614. uint64_t OrigOffset = Offset;
  2615. uint64_t TySize =
  2616. llvm::RoundUpToAlignment(getContext().getTypeSize(Ty), 64) / 8;
  2617. uint64_t Align = getContext().getTypeAlign(Ty) / 8;
  2618. Offset = llvm::RoundUpToAlignment(Offset, std::max(Align, (uint64_t)8));
  2619. Offset += TySize;
  2620. if (isAggregateTypeForABI(Ty)) {
  2621. // Ignore empty aggregates.
  2622. if (TySize == 0)
  2623. return ABIArgInfo::getIgnore();
  2624. // Records with non trivial destructors/constructors should not be passed
  2625. // by value.
  2626. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
  2627. Offset = OrigOffset + 8;
  2628. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  2629. }
  2630. // If we have reached here, aggregates are passed either indirectly via a
  2631. // byval pointer or directly by coercing to another structure type. In the
  2632. // latter case, padding is inserted if the offset of the aggregate is
  2633. // unaligned.
  2634. llvm::Type *ResType = HandleAggregates(Ty);
  2635. if (!ResType)
  2636. return ABIArgInfo::getIndirect(0);
  2637. return ABIArgInfo::getDirect(ResType, 0, getPaddingType(Align, OrigOffset));
  2638. }
  2639. // Treat an enum type as its underlying type.
  2640. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2641. Ty = EnumTy->getDecl()->getIntegerType();
  2642. if (Ty->isPromotableIntegerType())
  2643. return ABIArgInfo::getExtend();
  2644. return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
  2645. }
  2646. llvm::Type*
  2647. MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
  2648. const RecordType *RT = RetTy->getAs<RecordType>();
  2649. SmallVector<llvm::Type*, 2> RTList;
  2650. if (RT && RT->isStructureOrClassType()) {
  2651. const RecordDecl *RD = RT->getDecl();
  2652. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
  2653. unsigned FieldCnt = Layout.getFieldCount();
  2654. // N32/64 returns struct/classes in floating point registers if the
  2655. // following conditions are met:
  2656. // 1. The size of the struct/class is no larger than 128-bit.
  2657. // 2. The struct/class has one or two fields all of which are floating
  2658. // point types.
  2659. // 3. The offset of the first field is zero (this follows what gcc does).
  2660. //
  2661. // Any other composite results are returned in integer registers.
  2662. //
  2663. if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
  2664. RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
  2665. for (; b != e; ++b) {
  2666. const BuiltinType *BT = (*b)->getType()->getAs<BuiltinType>();
  2667. if (!BT || !BT->isFloatingPoint())
  2668. break;
  2669. RTList.push_back(CGT.ConvertType((*b)->getType()));
  2670. }
  2671. if (b == e)
  2672. return llvm::StructType::get(getVMContext(), RTList,
  2673. RD->hasAttr<PackedAttr>());
  2674. RTList.clear();
  2675. }
  2676. }
  2677. RTList.push_back(llvm::IntegerType::get(getVMContext(),
  2678. std::min(Size, (uint64_t)64)));
  2679. if (Size > 64)
  2680. RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
  2681. return llvm::StructType::get(getVMContext(), RTList);
  2682. }
  2683. ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
  2684. uint64_t Size = getContext().getTypeSize(RetTy);
  2685. if (RetTy->isVoidType() || Size == 0)
  2686. return ABIArgInfo::getIgnore();
  2687. if (isAggregateTypeForABI(RetTy)) {
  2688. if (Size <= 128) {
  2689. if (RetTy->isAnyComplexType())
  2690. return ABIArgInfo::getDirect();
  2691. if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
  2692. return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
  2693. }
  2694. return ABIArgInfo::getIndirect(0);
  2695. }
  2696. // Treat an enum type as its underlying type.
  2697. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  2698. RetTy = EnumTy->getDecl()->getIntegerType();
  2699. return (RetTy->isPromotableIntegerType() ?
  2700. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2701. }
  2702. void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
  2703. ABIArgInfo &RetInfo = FI.getReturnInfo();
  2704. RetInfo = classifyReturnType(FI.getReturnType());
  2705. // Check if a pointer to an aggregate is passed as a hidden argument.
  2706. uint64_t Offset = RetInfo.isIndirect() ? 8 : 0;
  2707. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  2708. it != ie; ++it)
  2709. it->info = classifyArgumentType(it->type, Offset);
  2710. }
  2711. llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2712. CodeGenFunction &CGF) const {
  2713. llvm::Type *BP = CGF.Int8PtrTy;
  2714. llvm::Type *BPP = CGF.Int8PtrPtrTy;
  2715. CGBuilderTy &Builder = CGF.Builder;
  2716. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
  2717. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  2718. int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
  2719. llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
  2720. llvm::Value *AddrTyped;
  2721. unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
  2722. llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
  2723. if (TypeAlign > MinABIStackAlignInBytes) {
  2724. llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
  2725. llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
  2726. llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
  2727. llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
  2728. llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
  2729. AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
  2730. }
  2731. else
  2732. AddrTyped = Builder.CreateBitCast(Addr, PTy);
  2733. llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
  2734. TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
  2735. uint64_t Offset =
  2736. llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
  2737. llvm::Value *NextAddr =
  2738. Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
  2739. "ap.next");
  2740. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  2741. return AddrTyped;
  2742. }
  2743. bool
  2744. MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
  2745. llvm::Value *Address) const {
  2746. // This information comes from gcc's implementation, which seems to
  2747. // as canonical as it gets.
  2748. // Everything on MIPS is 4 bytes. Double-precision FP registers
  2749. // are aliased to pairs of single-precision FP registers.
  2750. llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
  2751. // 0-31 are the general purpose registers, $0 - $31.
  2752. // 32-63 are the floating-point registers, $f0 - $f31.
  2753. // 64 and 65 are the multiply/divide registers, $hi and $lo.
  2754. // 66 is the (notional, I think) register for signal-handler return.
  2755. AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
  2756. // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
  2757. // They are one bit wide and ignored here.
  2758. // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
  2759. // (coprocessor 1 is the FP unit)
  2760. // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
  2761. // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
  2762. // 176-181 are the DSP accumulator registers.
  2763. AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
  2764. return false;
  2765. }
  2766. //===----------------------------------------------------------------------===//
  2767. // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
  2768. // Currently subclassed only to implement custom OpenCL C function attribute
  2769. // handling.
  2770. //===----------------------------------------------------------------------===//
  2771. namespace {
  2772. class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
  2773. public:
  2774. TCETargetCodeGenInfo(CodeGenTypes &CGT)
  2775. : DefaultTargetCodeGenInfo(CGT) {}
  2776. virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
  2777. CodeGen::CodeGenModule &M) const;
  2778. };
  2779. void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
  2780. llvm::GlobalValue *GV,
  2781. CodeGen::CodeGenModule &M) const {
  2782. const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
  2783. if (!FD) return;
  2784. llvm::Function *F = cast<llvm::Function>(GV);
  2785. if (M.getLangOptions().OpenCL) {
  2786. if (FD->hasAttr<OpenCLKernelAttr>()) {
  2787. // OpenCL C Kernel functions are not subject to inlining
  2788. F->addFnAttr(llvm::Attribute::NoInline);
  2789. if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
  2790. // Convert the reqd_work_group_size() attributes to metadata.
  2791. llvm::LLVMContext &Context = F->getContext();
  2792. llvm::NamedMDNode *OpenCLMetadata =
  2793. M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
  2794. SmallVector<llvm::Value*, 5> Operands;
  2795. Operands.push_back(F);
  2796. Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
  2797. llvm::APInt(32,
  2798. FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
  2799. Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
  2800. llvm::APInt(32,
  2801. FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
  2802. Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
  2803. llvm::APInt(32,
  2804. FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
  2805. // Add a boolean constant operand for "required" (true) or "hint" (false)
  2806. // for implementing the work_group_size_hint attr later. Currently
  2807. // always true as the hint is not yet implemented.
  2808. Operands.push_back(llvm::ConstantInt::getTrue(Context));
  2809. OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
  2810. }
  2811. }
  2812. }
  2813. }
  2814. }
  2815. //===----------------------------------------------------------------------===//
  2816. // Hexagon ABI Implementation
  2817. //===----------------------------------------------------------------------===//
  2818. namespace {
  2819. class HexagonABIInfo : public ABIInfo {
  2820. public:
  2821. HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
  2822. private:
  2823. ABIArgInfo classifyReturnType(QualType RetTy) const;
  2824. ABIArgInfo classifyArgumentType(QualType RetTy) const;
  2825. virtual void computeInfo(CGFunctionInfo &FI) const;
  2826. virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2827. CodeGenFunction &CGF) const;
  2828. };
  2829. class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
  2830. public:
  2831. HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
  2832. :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
  2833. int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
  2834. return 29;
  2835. }
  2836. };
  2837. }
  2838. void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
  2839. FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
  2840. for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
  2841. it != ie; ++it)
  2842. it->info = classifyArgumentType(it->type);
  2843. }
  2844. ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
  2845. if (!isAggregateTypeForABI(Ty)) {
  2846. // Treat an enum type as its underlying type.
  2847. if (const EnumType *EnumTy = Ty->getAs<EnumType>())
  2848. Ty = EnumTy->getDecl()->getIntegerType();
  2849. return (Ty->isPromotableIntegerType() ?
  2850. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2851. }
  2852. // Ignore empty records.
  2853. if (isEmptyRecord(getContext(), Ty, true))
  2854. return ABIArgInfo::getIgnore();
  2855. // Structures with either a non-trivial destructor or a non-trivial
  2856. // copy constructor are always indirect.
  2857. if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
  2858. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  2859. uint64_t Size = getContext().getTypeSize(Ty);
  2860. if (Size > 64)
  2861. return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
  2862. // Pass in the smallest viable integer type.
  2863. else if (Size > 32)
  2864. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  2865. else if (Size > 16)
  2866. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  2867. else if (Size > 8)
  2868. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  2869. else
  2870. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  2871. }
  2872. ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
  2873. if (RetTy->isVoidType())
  2874. return ABIArgInfo::getIgnore();
  2875. // Large vector types should be returned via memory.
  2876. if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
  2877. return ABIArgInfo::getIndirect(0);
  2878. if (!isAggregateTypeForABI(RetTy)) {
  2879. // Treat an enum type as its underlying type.
  2880. if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
  2881. RetTy = EnumTy->getDecl()->getIntegerType();
  2882. return (RetTy->isPromotableIntegerType() ?
  2883. ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
  2884. }
  2885. // Structures with either a non-trivial destructor or a non-trivial
  2886. // copy constructor are always indirect.
  2887. if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
  2888. return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
  2889. if (isEmptyRecord(getContext(), RetTy, true))
  2890. return ABIArgInfo::getIgnore();
  2891. // Aggregates <= 8 bytes are returned in r0; other aggregates
  2892. // are returned indirectly.
  2893. uint64_t Size = getContext().getTypeSize(RetTy);
  2894. if (Size <= 64) {
  2895. // Return in the smallest viable integer type.
  2896. if (Size <= 8)
  2897. return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
  2898. if (Size <= 16)
  2899. return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
  2900. if (Size <= 32)
  2901. return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
  2902. return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
  2903. }
  2904. return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
  2905. }
  2906. llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
  2907. CodeGenFunction &CGF) const {
  2908. // FIXME: Need to handle alignment
  2909. llvm::Type *BPP = CGF.Int8PtrPtrTy;
  2910. CGBuilderTy &Builder = CGF.Builder;
  2911. llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
  2912. "ap");
  2913. llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
  2914. llvm::Type *PTy =
  2915. llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
  2916. llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
  2917. uint64_t Offset =
  2918. llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
  2919. llvm::Value *NextAddr =
  2920. Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
  2921. "ap.next");
  2922. Builder.CreateStore(NextAddr, VAListAddrAsBPP);
  2923. return AddrTyped;
  2924. }
  2925. const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
  2926. if (TheTargetCodeGenInfo)
  2927. return *TheTargetCodeGenInfo;
  2928. const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
  2929. switch (Triple.getArch()) {
  2930. default:
  2931. return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
  2932. case llvm::Triple::mips:
  2933. case llvm::Triple::mipsel:
  2934. return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
  2935. case llvm::Triple::mips64:
  2936. case llvm::Triple::mips64el:
  2937. return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
  2938. case llvm::Triple::arm:
  2939. case llvm::Triple::thumb:
  2940. {
  2941. ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
  2942. if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
  2943. Kind = ARMABIInfo::APCS;
  2944. else if (CodeGenOpts.FloatABI == "hard")
  2945. Kind = ARMABIInfo::AAPCS_VFP;
  2946. return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
  2947. }
  2948. case llvm::Triple::ppc:
  2949. return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
  2950. case llvm::Triple::ptx32:
  2951. case llvm::Triple::ptx64:
  2952. return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
  2953. case llvm::Triple::mblaze:
  2954. return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
  2955. case llvm::Triple::msp430:
  2956. return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
  2957. case llvm::Triple::tce:
  2958. return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
  2959. case llvm::Triple::x86: {
  2960. bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
  2961. if (Triple.isOSDarwin())
  2962. return *(TheTargetCodeGenInfo =
  2963. new X86_32TargetCodeGenInfo(
  2964. Types, true, true, DisableMMX, false));
  2965. switch (Triple.getOS()) {
  2966. case llvm::Triple::Cygwin:
  2967. case llvm::Triple::MinGW32:
  2968. case llvm::Triple::AuroraUX:
  2969. case llvm::Triple::DragonFly:
  2970. case llvm::Triple::FreeBSD:
  2971. case llvm::Triple::OpenBSD:
  2972. return *(TheTargetCodeGenInfo =
  2973. new X86_32TargetCodeGenInfo(
  2974. Types, false, true, DisableMMX, false));
  2975. case llvm::Triple::Win32:
  2976. return *(TheTargetCodeGenInfo =
  2977. new X86_32TargetCodeGenInfo(
  2978. Types, false, true, DisableMMX, true));
  2979. default:
  2980. return *(TheTargetCodeGenInfo =
  2981. new X86_32TargetCodeGenInfo(
  2982. Types, false, false, DisableMMX, false));
  2983. }
  2984. }
  2985. case llvm::Triple::x86_64: {
  2986. bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
  2987. switch (Triple.getOS()) {
  2988. case llvm::Triple::Win32:
  2989. case llvm::Triple::MinGW32:
  2990. case llvm::Triple::Cygwin:
  2991. return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
  2992. default:
  2993. return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
  2994. HasAVX));
  2995. }
  2996. }
  2997. case llvm::Triple::hexagon:
  2998. return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
  2999. }
  3000. }