TargetInfo.cpp 137 KB

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