TargetInfo.cpp 134 KB

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