TargetInfo.cpp 140 KB

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