TargetInfo.cpp 128 KB

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