TargetLoweringBase.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. //===-- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ---===//
  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. // This implements the TargetLoweringBase class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetLowering.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/CodeGen/Analysis.h"
  19. #include "llvm/CodeGen/MachineFrameInfo.h"
  20. #include "llvm/CodeGen/MachineFunction.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  23. #include "llvm/CodeGen/StackMaps.h"
  24. #include "llvm/IR/DataLayout.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/Mangler.h"
  28. #include "llvm/MC/MCAsmInfo.h"
  29. #include "llvm/MC/MCContext.h"
  30. #include "llvm/MC/MCExpr.h"
  31. #include "llvm/Support/BranchProbability.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/MathExtras.h"
  35. #include "llvm/Target/TargetLoweringObjectFile.h"
  36. #include "llvm/Target/TargetMachine.h"
  37. #include "llvm/Target/TargetRegisterInfo.h"
  38. #include "llvm/Target/TargetSubtargetInfo.h"
  39. #include <cctype>
  40. using namespace llvm;
  41. static cl::opt<bool> JumpIsExpensiveOverride(
  42. "jump-is-expensive", cl::init(false),
  43. cl::desc("Do not create extra branches to split comparison logic."),
  44. cl::Hidden);
  45. static cl::opt<unsigned> MinimumJumpTableEntries
  46. ("min-jump-table-entries", cl::init(4), cl::Hidden,
  47. cl::desc("Set minimum number of entries to use a jump table."));
  48. static cl::opt<unsigned> MaximumJumpTableSize
  49. ("max-jump-table-size", cl::init(0), cl::Hidden,
  50. cl::desc("Set maximum size of jump tables; zero for no limit."));
  51. // Although this default value is arbitrary, it is not random. It is assumed
  52. // that a condition that evaluates the same way by a higher percentage than this
  53. // is best represented as control flow. Therefore, the default value N should be
  54. // set such that the win from N% correct executions is greater than the loss
  55. // from (100 - N)% mispredicted executions for the majority of intended targets.
  56. static cl::opt<int> MinPercentageForPredictableBranch(
  57. "min-predictable-branch", cl::init(99),
  58. cl::desc("Minimum percentage (0-100) that a condition must be either true "
  59. "or false to assume that the condition is predictable"),
  60. cl::Hidden);
  61. /// InitLibcallNames - Set default libcall names.
  62. ///
  63. static void InitLibcallNames(const char **Names, const Triple &TT) {
  64. Names[RTLIB::SHL_I16] = "__ashlhi3";
  65. Names[RTLIB::SHL_I32] = "__ashlsi3";
  66. Names[RTLIB::SHL_I64] = "__ashldi3";
  67. Names[RTLIB::SHL_I128] = "__ashlti3";
  68. Names[RTLIB::SRL_I16] = "__lshrhi3";
  69. Names[RTLIB::SRL_I32] = "__lshrsi3";
  70. Names[RTLIB::SRL_I64] = "__lshrdi3";
  71. Names[RTLIB::SRL_I128] = "__lshrti3";
  72. Names[RTLIB::SRA_I16] = "__ashrhi3";
  73. Names[RTLIB::SRA_I32] = "__ashrsi3";
  74. Names[RTLIB::SRA_I64] = "__ashrdi3";
  75. Names[RTLIB::SRA_I128] = "__ashrti3";
  76. Names[RTLIB::MUL_I8] = "__mulqi3";
  77. Names[RTLIB::MUL_I16] = "__mulhi3";
  78. Names[RTLIB::MUL_I32] = "__mulsi3";
  79. Names[RTLIB::MUL_I64] = "__muldi3";
  80. Names[RTLIB::MUL_I128] = "__multi3";
  81. Names[RTLIB::MULO_I32] = "__mulosi4";
  82. Names[RTLIB::MULO_I64] = "__mulodi4";
  83. Names[RTLIB::MULO_I128] = "__muloti4";
  84. Names[RTLIB::SDIV_I8] = "__divqi3";
  85. Names[RTLIB::SDIV_I16] = "__divhi3";
  86. Names[RTLIB::SDIV_I32] = "__divsi3";
  87. Names[RTLIB::SDIV_I64] = "__divdi3";
  88. Names[RTLIB::SDIV_I128] = "__divti3";
  89. Names[RTLIB::UDIV_I8] = "__udivqi3";
  90. Names[RTLIB::UDIV_I16] = "__udivhi3";
  91. Names[RTLIB::UDIV_I32] = "__udivsi3";
  92. Names[RTLIB::UDIV_I64] = "__udivdi3";
  93. Names[RTLIB::UDIV_I128] = "__udivti3";
  94. Names[RTLIB::SREM_I8] = "__modqi3";
  95. Names[RTLIB::SREM_I16] = "__modhi3";
  96. Names[RTLIB::SREM_I32] = "__modsi3";
  97. Names[RTLIB::SREM_I64] = "__moddi3";
  98. Names[RTLIB::SREM_I128] = "__modti3";
  99. Names[RTLIB::UREM_I8] = "__umodqi3";
  100. Names[RTLIB::UREM_I16] = "__umodhi3";
  101. Names[RTLIB::UREM_I32] = "__umodsi3";
  102. Names[RTLIB::UREM_I64] = "__umoddi3";
  103. Names[RTLIB::UREM_I128] = "__umodti3";
  104. Names[RTLIB::NEG_I32] = "__negsi2";
  105. Names[RTLIB::NEG_I64] = "__negdi2";
  106. Names[RTLIB::ADD_F32] = "__addsf3";
  107. Names[RTLIB::ADD_F64] = "__adddf3";
  108. Names[RTLIB::ADD_F80] = "__addxf3";
  109. Names[RTLIB::ADD_F128] = "__addtf3";
  110. Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
  111. Names[RTLIB::SUB_F32] = "__subsf3";
  112. Names[RTLIB::SUB_F64] = "__subdf3";
  113. Names[RTLIB::SUB_F80] = "__subxf3";
  114. Names[RTLIB::SUB_F128] = "__subtf3";
  115. Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
  116. Names[RTLIB::MUL_F32] = "__mulsf3";
  117. Names[RTLIB::MUL_F64] = "__muldf3";
  118. Names[RTLIB::MUL_F80] = "__mulxf3";
  119. Names[RTLIB::MUL_F128] = "__multf3";
  120. Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
  121. Names[RTLIB::DIV_F32] = "__divsf3";
  122. Names[RTLIB::DIV_F64] = "__divdf3";
  123. Names[RTLIB::DIV_F80] = "__divxf3";
  124. Names[RTLIB::DIV_F128] = "__divtf3";
  125. Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
  126. Names[RTLIB::REM_F32] = "fmodf";
  127. Names[RTLIB::REM_F64] = "fmod";
  128. Names[RTLIB::REM_F80] = "fmodl";
  129. Names[RTLIB::REM_F128] = "fmodl";
  130. Names[RTLIB::REM_PPCF128] = "fmodl";
  131. Names[RTLIB::FMA_F32] = "fmaf";
  132. Names[RTLIB::FMA_F64] = "fma";
  133. Names[RTLIB::FMA_F80] = "fmal";
  134. Names[RTLIB::FMA_F128] = "fmal";
  135. Names[RTLIB::FMA_PPCF128] = "fmal";
  136. Names[RTLIB::POWI_F32] = "__powisf2";
  137. Names[RTLIB::POWI_F64] = "__powidf2";
  138. Names[RTLIB::POWI_F80] = "__powixf2";
  139. Names[RTLIB::POWI_F128] = "__powitf2";
  140. Names[RTLIB::POWI_PPCF128] = "__powitf2";
  141. Names[RTLIB::SQRT_F32] = "sqrtf";
  142. Names[RTLIB::SQRT_F64] = "sqrt";
  143. Names[RTLIB::SQRT_F80] = "sqrtl";
  144. Names[RTLIB::SQRT_F128] = "sqrtl";
  145. Names[RTLIB::SQRT_PPCF128] = "sqrtl";
  146. Names[RTLIB::LOG_F32] = "logf";
  147. Names[RTLIB::LOG_F64] = "log";
  148. Names[RTLIB::LOG_F80] = "logl";
  149. Names[RTLIB::LOG_F128] = "logl";
  150. Names[RTLIB::LOG_PPCF128] = "logl";
  151. Names[RTLIB::LOG2_F32] = "log2f";
  152. Names[RTLIB::LOG2_F64] = "log2";
  153. Names[RTLIB::LOG2_F80] = "log2l";
  154. Names[RTLIB::LOG2_F128] = "log2l";
  155. Names[RTLIB::LOG2_PPCF128] = "log2l";
  156. Names[RTLIB::LOG10_F32] = "log10f";
  157. Names[RTLIB::LOG10_F64] = "log10";
  158. Names[RTLIB::LOG10_F80] = "log10l";
  159. Names[RTLIB::LOG10_F128] = "log10l";
  160. Names[RTLIB::LOG10_PPCF128] = "log10l";
  161. Names[RTLIB::EXP_F32] = "expf";
  162. Names[RTLIB::EXP_F64] = "exp";
  163. Names[RTLIB::EXP_F80] = "expl";
  164. Names[RTLIB::EXP_F128] = "expl";
  165. Names[RTLIB::EXP_PPCF128] = "expl";
  166. Names[RTLIB::EXP2_F32] = "exp2f";
  167. Names[RTLIB::EXP2_F64] = "exp2";
  168. Names[RTLIB::EXP2_F80] = "exp2l";
  169. Names[RTLIB::EXP2_F128] = "exp2l";
  170. Names[RTLIB::EXP2_PPCF128] = "exp2l";
  171. Names[RTLIB::SIN_F32] = "sinf";
  172. Names[RTLIB::SIN_F64] = "sin";
  173. Names[RTLIB::SIN_F80] = "sinl";
  174. Names[RTLIB::SIN_F128] = "sinl";
  175. Names[RTLIB::SIN_PPCF128] = "sinl";
  176. Names[RTLIB::COS_F32] = "cosf";
  177. Names[RTLIB::COS_F64] = "cos";
  178. Names[RTLIB::COS_F80] = "cosl";
  179. Names[RTLIB::COS_F128] = "cosl";
  180. Names[RTLIB::COS_PPCF128] = "cosl";
  181. Names[RTLIB::POW_F32] = "powf";
  182. Names[RTLIB::POW_F64] = "pow";
  183. Names[RTLIB::POW_F80] = "powl";
  184. Names[RTLIB::POW_F128] = "powl";
  185. Names[RTLIB::POW_PPCF128] = "powl";
  186. Names[RTLIB::CEIL_F32] = "ceilf";
  187. Names[RTLIB::CEIL_F64] = "ceil";
  188. Names[RTLIB::CEIL_F80] = "ceill";
  189. Names[RTLIB::CEIL_F128] = "ceill";
  190. Names[RTLIB::CEIL_PPCF128] = "ceill";
  191. Names[RTLIB::TRUNC_F32] = "truncf";
  192. Names[RTLIB::TRUNC_F64] = "trunc";
  193. Names[RTLIB::TRUNC_F80] = "truncl";
  194. Names[RTLIB::TRUNC_F128] = "truncl";
  195. Names[RTLIB::TRUNC_PPCF128] = "truncl";
  196. Names[RTLIB::RINT_F32] = "rintf";
  197. Names[RTLIB::RINT_F64] = "rint";
  198. Names[RTLIB::RINT_F80] = "rintl";
  199. Names[RTLIB::RINT_F128] = "rintl";
  200. Names[RTLIB::RINT_PPCF128] = "rintl";
  201. Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
  202. Names[RTLIB::NEARBYINT_F64] = "nearbyint";
  203. Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
  204. Names[RTLIB::NEARBYINT_F128] = "nearbyintl";
  205. Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
  206. Names[RTLIB::ROUND_F32] = "roundf";
  207. Names[RTLIB::ROUND_F64] = "round";
  208. Names[RTLIB::ROUND_F80] = "roundl";
  209. Names[RTLIB::ROUND_F128] = "roundl";
  210. Names[RTLIB::ROUND_PPCF128] = "roundl";
  211. Names[RTLIB::FLOOR_F32] = "floorf";
  212. Names[RTLIB::FLOOR_F64] = "floor";
  213. Names[RTLIB::FLOOR_F80] = "floorl";
  214. Names[RTLIB::FLOOR_F128] = "floorl";
  215. Names[RTLIB::FLOOR_PPCF128] = "floorl";
  216. Names[RTLIB::FMIN_F32] = "fminf";
  217. Names[RTLIB::FMIN_F64] = "fmin";
  218. Names[RTLIB::FMIN_F80] = "fminl";
  219. Names[RTLIB::FMIN_F128] = "fminl";
  220. Names[RTLIB::FMIN_PPCF128] = "fminl";
  221. Names[RTLIB::FMAX_F32] = "fmaxf";
  222. Names[RTLIB::FMAX_F64] = "fmax";
  223. Names[RTLIB::FMAX_F80] = "fmaxl";
  224. Names[RTLIB::FMAX_F128] = "fmaxl";
  225. Names[RTLIB::FMAX_PPCF128] = "fmaxl";
  226. Names[RTLIB::ROUND_F32] = "roundf";
  227. Names[RTLIB::ROUND_F64] = "round";
  228. Names[RTLIB::ROUND_F80] = "roundl";
  229. Names[RTLIB::ROUND_F128] = "roundl";
  230. Names[RTLIB::ROUND_PPCF128] = "roundl";
  231. Names[RTLIB::COPYSIGN_F32] = "copysignf";
  232. Names[RTLIB::COPYSIGN_F64] = "copysign";
  233. Names[RTLIB::COPYSIGN_F80] = "copysignl";
  234. Names[RTLIB::COPYSIGN_F128] = "copysignl";
  235. Names[RTLIB::COPYSIGN_PPCF128] = "copysignl";
  236. Names[RTLIB::FPEXT_F32_PPCF128] = "__gcc_stoq";
  237. Names[RTLIB::FPEXT_F64_PPCF128] = "__gcc_dtoq";
  238. Names[RTLIB::FPEXT_F64_F128] = "__extenddftf2";
  239. Names[RTLIB::FPEXT_F32_F128] = "__extendsftf2";
  240. Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
  241. if (TT.isOSDarwin()) {
  242. // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
  243. // of the gnueabi-style __gnu_*_ieee.
  244. // FIXME: What about other targets?
  245. Names[RTLIB::FPEXT_F16_F32] = "__extendhfsf2";
  246. Names[RTLIB::FPROUND_F32_F16] = "__truncsfhf2";
  247. } else {
  248. Names[RTLIB::FPEXT_F16_F32] = "__gnu_h2f_ieee";
  249. Names[RTLIB::FPROUND_F32_F16] = "__gnu_f2h_ieee";
  250. }
  251. Names[RTLIB::FPROUND_F64_F16] = "__truncdfhf2";
  252. Names[RTLIB::FPROUND_F80_F16] = "__truncxfhf2";
  253. Names[RTLIB::FPROUND_F128_F16] = "__trunctfhf2";
  254. Names[RTLIB::FPROUND_PPCF128_F16] = "__trunctfhf2";
  255. Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
  256. Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
  257. Names[RTLIB::FPROUND_F128_F32] = "__trunctfsf2";
  258. Names[RTLIB::FPROUND_PPCF128_F32] = "__gcc_qtos";
  259. Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
  260. Names[RTLIB::FPROUND_F128_F64] = "__trunctfdf2";
  261. Names[RTLIB::FPROUND_PPCF128_F64] = "__gcc_qtod";
  262. Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
  263. Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
  264. Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
  265. Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
  266. Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
  267. Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
  268. Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
  269. Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
  270. Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
  271. Names[RTLIB::FPTOSINT_F128_I32] = "__fixtfsi";
  272. Names[RTLIB::FPTOSINT_F128_I64] = "__fixtfdi";
  273. Names[RTLIB::FPTOSINT_F128_I128] = "__fixtfti";
  274. Names[RTLIB::FPTOSINT_PPCF128_I32] = "__gcc_qtou";
  275. Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
  276. Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
  277. Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
  278. Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
  279. Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
  280. Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
  281. Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
  282. Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
  283. Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
  284. Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
  285. Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
  286. Names[RTLIB::FPTOUINT_F128_I32] = "__fixunstfsi";
  287. Names[RTLIB::FPTOUINT_F128_I64] = "__fixunstfdi";
  288. Names[RTLIB::FPTOUINT_F128_I128] = "__fixunstfti";
  289. Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
  290. Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
  291. Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
  292. Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
  293. Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
  294. Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
  295. Names[RTLIB::SINTTOFP_I32_F128] = "__floatsitf";
  296. Names[RTLIB::SINTTOFP_I32_PPCF128] = "__gcc_itoq";
  297. Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
  298. Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
  299. Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
  300. Names[RTLIB::SINTTOFP_I64_F128] = "__floatditf";
  301. Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
  302. Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
  303. Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
  304. Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
  305. Names[RTLIB::SINTTOFP_I128_F128] = "__floattitf";
  306. Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
  307. Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
  308. Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
  309. Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
  310. Names[RTLIB::UINTTOFP_I32_F128] = "__floatunsitf";
  311. Names[RTLIB::UINTTOFP_I32_PPCF128] = "__gcc_utoq";
  312. Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
  313. Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
  314. Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
  315. Names[RTLIB::UINTTOFP_I64_F128] = "__floatunditf";
  316. Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
  317. Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
  318. Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
  319. Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
  320. Names[RTLIB::UINTTOFP_I128_F128] = "__floatuntitf";
  321. Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
  322. Names[RTLIB::OEQ_F32] = "__eqsf2";
  323. Names[RTLIB::OEQ_F64] = "__eqdf2";
  324. Names[RTLIB::OEQ_F128] = "__eqtf2";
  325. Names[RTLIB::OEQ_PPCF128] = "__gcc_qeq";
  326. Names[RTLIB::UNE_F32] = "__nesf2";
  327. Names[RTLIB::UNE_F64] = "__nedf2";
  328. Names[RTLIB::UNE_F128] = "__netf2";
  329. Names[RTLIB::UNE_PPCF128] = "__gcc_qne";
  330. Names[RTLIB::OGE_F32] = "__gesf2";
  331. Names[RTLIB::OGE_F64] = "__gedf2";
  332. Names[RTLIB::OGE_F128] = "__getf2";
  333. Names[RTLIB::OGE_PPCF128] = "__gcc_qge";
  334. Names[RTLIB::OLT_F32] = "__ltsf2";
  335. Names[RTLIB::OLT_F64] = "__ltdf2";
  336. Names[RTLIB::OLT_F128] = "__lttf2";
  337. Names[RTLIB::OLT_PPCF128] = "__gcc_qlt";
  338. Names[RTLIB::OLE_F32] = "__lesf2";
  339. Names[RTLIB::OLE_F64] = "__ledf2";
  340. Names[RTLIB::OLE_F128] = "__letf2";
  341. Names[RTLIB::OLE_PPCF128] = "__gcc_qle";
  342. Names[RTLIB::OGT_F32] = "__gtsf2";
  343. Names[RTLIB::OGT_F64] = "__gtdf2";
  344. Names[RTLIB::OGT_F128] = "__gttf2";
  345. Names[RTLIB::OGT_PPCF128] = "__gcc_qgt";
  346. Names[RTLIB::UO_F32] = "__unordsf2";
  347. Names[RTLIB::UO_F64] = "__unorddf2";
  348. Names[RTLIB::UO_F128] = "__unordtf2";
  349. Names[RTLIB::UO_PPCF128] = "__gcc_qunord";
  350. Names[RTLIB::O_F32] = "__unordsf2";
  351. Names[RTLIB::O_F64] = "__unorddf2";
  352. Names[RTLIB::O_F128] = "__unordtf2";
  353. Names[RTLIB::O_PPCF128] = "__gcc_qunord";
  354. Names[RTLIB::MEMCPY] = "memcpy";
  355. Names[RTLIB::MEMMOVE] = "memmove";
  356. Names[RTLIB::MEMSET] = "memset";
  357. Names[RTLIB::MEMCPY_ELEMENT_ATOMIC_1] = "__llvm_memcpy_element_atomic_1";
  358. Names[RTLIB::MEMCPY_ELEMENT_ATOMIC_2] = "__llvm_memcpy_element_atomic_2";
  359. Names[RTLIB::MEMCPY_ELEMENT_ATOMIC_4] = "__llvm_memcpy_element_atomic_4";
  360. Names[RTLIB::MEMCPY_ELEMENT_ATOMIC_8] = "__llvm_memcpy_element_atomic_8";
  361. Names[RTLIB::MEMCPY_ELEMENT_ATOMIC_16] = "__llvm_memcpy_element_atomic_16";
  362. Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
  363. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1";
  364. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2";
  365. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4";
  366. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8";
  367. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16] = "__sync_val_compare_and_swap_16";
  368. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_1] = "__sync_lock_test_and_set_1";
  369. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_2] = "__sync_lock_test_and_set_2";
  370. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_4] = "__sync_lock_test_and_set_4";
  371. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_8] = "__sync_lock_test_and_set_8";
  372. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_16] = "__sync_lock_test_and_set_16";
  373. Names[RTLIB::SYNC_FETCH_AND_ADD_1] = "__sync_fetch_and_add_1";
  374. Names[RTLIB::SYNC_FETCH_AND_ADD_2] = "__sync_fetch_and_add_2";
  375. Names[RTLIB::SYNC_FETCH_AND_ADD_4] = "__sync_fetch_and_add_4";
  376. Names[RTLIB::SYNC_FETCH_AND_ADD_8] = "__sync_fetch_and_add_8";
  377. Names[RTLIB::SYNC_FETCH_AND_ADD_16] = "__sync_fetch_and_add_16";
  378. Names[RTLIB::SYNC_FETCH_AND_SUB_1] = "__sync_fetch_and_sub_1";
  379. Names[RTLIB::SYNC_FETCH_AND_SUB_2] = "__sync_fetch_and_sub_2";
  380. Names[RTLIB::SYNC_FETCH_AND_SUB_4] = "__sync_fetch_and_sub_4";
  381. Names[RTLIB::SYNC_FETCH_AND_SUB_8] = "__sync_fetch_and_sub_8";
  382. Names[RTLIB::SYNC_FETCH_AND_SUB_16] = "__sync_fetch_and_sub_16";
  383. Names[RTLIB::SYNC_FETCH_AND_AND_1] = "__sync_fetch_and_and_1";
  384. Names[RTLIB::SYNC_FETCH_AND_AND_2] = "__sync_fetch_and_and_2";
  385. Names[RTLIB::SYNC_FETCH_AND_AND_4] = "__sync_fetch_and_and_4";
  386. Names[RTLIB::SYNC_FETCH_AND_AND_8] = "__sync_fetch_and_and_8";
  387. Names[RTLIB::SYNC_FETCH_AND_AND_16] = "__sync_fetch_and_and_16";
  388. Names[RTLIB::SYNC_FETCH_AND_OR_1] = "__sync_fetch_and_or_1";
  389. Names[RTLIB::SYNC_FETCH_AND_OR_2] = "__sync_fetch_and_or_2";
  390. Names[RTLIB::SYNC_FETCH_AND_OR_4] = "__sync_fetch_and_or_4";
  391. Names[RTLIB::SYNC_FETCH_AND_OR_8] = "__sync_fetch_and_or_8";
  392. Names[RTLIB::SYNC_FETCH_AND_OR_16] = "__sync_fetch_and_or_16";
  393. Names[RTLIB::SYNC_FETCH_AND_XOR_1] = "__sync_fetch_and_xor_1";
  394. Names[RTLIB::SYNC_FETCH_AND_XOR_2] = "__sync_fetch_and_xor_2";
  395. Names[RTLIB::SYNC_FETCH_AND_XOR_4] = "__sync_fetch_and_xor_4";
  396. Names[RTLIB::SYNC_FETCH_AND_XOR_8] = "__sync_fetch_and_xor_8";
  397. Names[RTLIB::SYNC_FETCH_AND_XOR_16] = "__sync_fetch_and_xor_16";
  398. Names[RTLIB::SYNC_FETCH_AND_NAND_1] = "__sync_fetch_and_nand_1";
  399. Names[RTLIB::SYNC_FETCH_AND_NAND_2] = "__sync_fetch_and_nand_2";
  400. Names[RTLIB::SYNC_FETCH_AND_NAND_4] = "__sync_fetch_and_nand_4";
  401. Names[RTLIB::SYNC_FETCH_AND_NAND_8] = "__sync_fetch_and_nand_8";
  402. Names[RTLIB::SYNC_FETCH_AND_NAND_16] = "__sync_fetch_and_nand_16";
  403. Names[RTLIB::SYNC_FETCH_AND_MAX_1] = "__sync_fetch_and_max_1";
  404. Names[RTLIB::SYNC_FETCH_AND_MAX_2] = "__sync_fetch_and_max_2";
  405. Names[RTLIB::SYNC_FETCH_AND_MAX_4] = "__sync_fetch_and_max_4";
  406. Names[RTLIB::SYNC_FETCH_AND_MAX_8] = "__sync_fetch_and_max_8";
  407. Names[RTLIB::SYNC_FETCH_AND_MAX_16] = "__sync_fetch_and_max_16";
  408. Names[RTLIB::SYNC_FETCH_AND_UMAX_1] = "__sync_fetch_and_umax_1";
  409. Names[RTLIB::SYNC_FETCH_AND_UMAX_2] = "__sync_fetch_and_umax_2";
  410. Names[RTLIB::SYNC_FETCH_AND_UMAX_4] = "__sync_fetch_and_umax_4";
  411. Names[RTLIB::SYNC_FETCH_AND_UMAX_8] = "__sync_fetch_and_umax_8";
  412. Names[RTLIB::SYNC_FETCH_AND_UMAX_16] = "__sync_fetch_and_umax_16";
  413. Names[RTLIB::SYNC_FETCH_AND_MIN_1] = "__sync_fetch_and_min_1";
  414. Names[RTLIB::SYNC_FETCH_AND_MIN_2] = "__sync_fetch_and_min_2";
  415. Names[RTLIB::SYNC_FETCH_AND_MIN_4] = "__sync_fetch_and_min_4";
  416. Names[RTLIB::SYNC_FETCH_AND_MIN_8] = "__sync_fetch_and_min_8";
  417. Names[RTLIB::SYNC_FETCH_AND_MIN_16] = "__sync_fetch_and_min_16";
  418. Names[RTLIB::SYNC_FETCH_AND_UMIN_1] = "__sync_fetch_and_umin_1";
  419. Names[RTLIB::SYNC_FETCH_AND_UMIN_2] = "__sync_fetch_and_umin_2";
  420. Names[RTLIB::SYNC_FETCH_AND_UMIN_4] = "__sync_fetch_and_umin_4";
  421. Names[RTLIB::SYNC_FETCH_AND_UMIN_8] = "__sync_fetch_and_umin_8";
  422. Names[RTLIB::SYNC_FETCH_AND_UMIN_16] = "__sync_fetch_and_umin_16";
  423. Names[RTLIB::ATOMIC_LOAD] = "__atomic_load";
  424. Names[RTLIB::ATOMIC_LOAD_1] = "__atomic_load_1";
  425. Names[RTLIB::ATOMIC_LOAD_2] = "__atomic_load_2";
  426. Names[RTLIB::ATOMIC_LOAD_4] = "__atomic_load_4";
  427. Names[RTLIB::ATOMIC_LOAD_8] = "__atomic_load_8";
  428. Names[RTLIB::ATOMIC_LOAD_16] = "__atomic_load_16";
  429. Names[RTLIB::ATOMIC_STORE] = "__atomic_store";
  430. Names[RTLIB::ATOMIC_STORE_1] = "__atomic_store_1";
  431. Names[RTLIB::ATOMIC_STORE_2] = "__atomic_store_2";
  432. Names[RTLIB::ATOMIC_STORE_4] = "__atomic_store_4";
  433. Names[RTLIB::ATOMIC_STORE_8] = "__atomic_store_8";
  434. Names[RTLIB::ATOMIC_STORE_16] = "__atomic_store_16";
  435. Names[RTLIB::ATOMIC_EXCHANGE] = "__atomic_exchange";
  436. Names[RTLIB::ATOMIC_EXCHANGE_1] = "__atomic_exchange_1";
  437. Names[RTLIB::ATOMIC_EXCHANGE_2] = "__atomic_exchange_2";
  438. Names[RTLIB::ATOMIC_EXCHANGE_4] = "__atomic_exchange_4";
  439. Names[RTLIB::ATOMIC_EXCHANGE_8] = "__atomic_exchange_8";
  440. Names[RTLIB::ATOMIC_EXCHANGE_16] = "__atomic_exchange_16";
  441. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE] = "__atomic_compare_exchange";
  442. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE_1] = "__atomic_compare_exchange_1";
  443. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE_2] = "__atomic_compare_exchange_2";
  444. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE_4] = "__atomic_compare_exchange_4";
  445. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE_8] = "__atomic_compare_exchange_8";
  446. Names[RTLIB::ATOMIC_COMPARE_EXCHANGE_16] = "__atomic_compare_exchange_16";
  447. Names[RTLIB::ATOMIC_FETCH_ADD_1] = "__atomic_fetch_add_1";
  448. Names[RTLIB::ATOMIC_FETCH_ADD_2] = "__atomic_fetch_add_2";
  449. Names[RTLIB::ATOMIC_FETCH_ADD_4] = "__atomic_fetch_add_4";
  450. Names[RTLIB::ATOMIC_FETCH_ADD_8] = "__atomic_fetch_add_8";
  451. Names[RTLIB::ATOMIC_FETCH_ADD_16] = "__atomic_fetch_add_16";
  452. Names[RTLIB::ATOMIC_FETCH_SUB_1] = "__atomic_fetch_sub_1";
  453. Names[RTLIB::ATOMIC_FETCH_SUB_2] = "__atomic_fetch_sub_2";
  454. Names[RTLIB::ATOMIC_FETCH_SUB_4] = "__atomic_fetch_sub_4";
  455. Names[RTLIB::ATOMIC_FETCH_SUB_8] = "__atomic_fetch_sub_8";
  456. Names[RTLIB::ATOMIC_FETCH_SUB_16] = "__atomic_fetch_sub_16";
  457. Names[RTLIB::ATOMIC_FETCH_AND_1] = "__atomic_fetch_and_1";
  458. Names[RTLIB::ATOMIC_FETCH_AND_2] = "__atomic_fetch_and_2";
  459. Names[RTLIB::ATOMIC_FETCH_AND_4] = "__atomic_fetch_and_4";
  460. Names[RTLIB::ATOMIC_FETCH_AND_8] = "__atomic_fetch_and_8";
  461. Names[RTLIB::ATOMIC_FETCH_AND_16] = "__atomic_fetch_and_16";
  462. Names[RTLIB::ATOMIC_FETCH_OR_1] = "__atomic_fetch_or_1";
  463. Names[RTLIB::ATOMIC_FETCH_OR_2] = "__atomic_fetch_or_2";
  464. Names[RTLIB::ATOMIC_FETCH_OR_4] = "__atomic_fetch_or_4";
  465. Names[RTLIB::ATOMIC_FETCH_OR_8] = "__atomic_fetch_or_8";
  466. Names[RTLIB::ATOMIC_FETCH_OR_16] = "__atomic_fetch_or_16";
  467. Names[RTLIB::ATOMIC_FETCH_XOR_1] = "__atomic_fetch_xor_1";
  468. Names[RTLIB::ATOMIC_FETCH_XOR_2] = "__atomic_fetch_xor_2";
  469. Names[RTLIB::ATOMIC_FETCH_XOR_4] = "__atomic_fetch_xor_4";
  470. Names[RTLIB::ATOMIC_FETCH_XOR_8] = "__atomic_fetch_xor_8";
  471. Names[RTLIB::ATOMIC_FETCH_XOR_16] = "__atomic_fetch_xor_16";
  472. Names[RTLIB::ATOMIC_FETCH_NAND_1] = "__atomic_fetch_nand_1";
  473. Names[RTLIB::ATOMIC_FETCH_NAND_2] = "__atomic_fetch_nand_2";
  474. Names[RTLIB::ATOMIC_FETCH_NAND_4] = "__atomic_fetch_nand_4";
  475. Names[RTLIB::ATOMIC_FETCH_NAND_8] = "__atomic_fetch_nand_8";
  476. Names[RTLIB::ATOMIC_FETCH_NAND_16] = "__atomic_fetch_nand_16";
  477. if (TT.isGNUEnvironment()) {
  478. Names[RTLIB::SINCOS_F32] = "sincosf";
  479. Names[RTLIB::SINCOS_F64] = "sincos";
  480. Names[RTLIB::SINCOS_F80] = "sincosl";
  481. Names[RTLIB::SINCOS_F128] = "sincosl";
  482. Names[RTLIB::SINCOS_PPCF128] = "sincosl";
  483. }
  484. if (!TT.isOSOpenBSD()) {
  485. Names[RTLIB::STACKPROTECTOR_CHECK_FAIL] = "__stack_chk_fail";
  486. }
  487. Names[RTLIB::DEOPTIMIZE] = "__llvm_deoptimize";
  488. }
  489. /// Set default libcall CallingConvs.
  490. static void InitLibcallCallingConvs(CallingConv::ID *CCs) {
  491. for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
  492. CCs[LC] = CallingConv::C;
  493. }
  494. /// getFPEXT - Return the FPEXT_*_* value for the given types, or
  495. /// UNKNOWN_LIBCALL if there is none.
  496. RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
  497. if (OpVT == MVT::f16) {
  498. if (RetVT == MVT::f32)
  499. return FPEXT_F16_F32;
  500. } else if (OpVT == MVT::f32) {
  501. if (RetVT == MVT::f64)
  502. return FPEXT_F32_F64;
  503. if (RetVT == MVT::f128)
  504. return FPEXT_F32_F128;
  505. if (RetVT == MVT::ppcf128)
  506. return FPEXT_F32_PPCF128;
  507. } else if (OpVT == MVT::f64) {
  508. if (RetVT == MVT::f128)
  509. return FPEXT_F64_F128;
  510. else if (RetVT == MVT::ppcf128)
  511. return FPEXT_F64_PPCF128;
  512. }
  513. return UNKNOWN_LIBCALL;
  514. }
  515. /// getFPROUND - Return the FPROUND_*_* value for the given types, or
  516. /// UNKNOWN_LIBCALL if there is none.
  517. RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
  518. if (RetVT == MVT::f16) {
  519. if (OpVT == MVT::f32)
  520. return FPROUND_F32_F16;
  521. if (OpVT == MVT::f64)
  522. return FPROUND_F64_F16;
  523. if (OpVT == MVT::f80)
  524. return FPROUND_F80_F16;
  525. if (OpVT == MVT::f128)
  526. return FPROUND_F128_F16;
  527. if (OpVT == MVT::ppcf128)
  528. return FPROUND_PPCF128_F16;
  529. } else if (RetVT == MVT::f32) {
  530. if (OpVT == MVT::f64)
  531. return FPROUND_F64_F32;
  532. if (OpVT == MVT::f80)
  533. return FPROUND_F80_F32;
  534. if (OpVT == MVT::f128)
  535. return FPROUND_F128_F32;
  536. if (OpVT == MVT::ppcf128)
  537. return FPROUND_PPCF128_F32;
  538. } else if (RetVT == MVT::f64) {
  539. if (OpVT == MVT::f80)
  540. return FPROUND_F80_F64;
  541. if (OpVT == MVT::f128)
  542. return FPROUND_F128_F64;
  543. if (OpVT == MVT::ppcf128)
  544. return FPROUND_PPCF128_F64;
  545. }
  546. return UNKNOWN_LIBCALL;
  547. }
  548. /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
  549. /// UNKNOWN_LIBCALL if there is none.
  550. RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
  551. if (OpVT == MVT::f32) {
  552. if (RetVT == MVT::i32)
  553. return FPTOSINT_F32_I32;
  554. if (RetVT == MVT::i64)
  555. return FPTOSINT_F32_I64;
  556. if (RetVT == MVT::i128)
  557. return FPTOSINT_F32_I128;
  558. } else if (OpVT == MVT::f64) {
  559. if (RetVT == MVT::i32)
  560. return FPTOSINT_F64_I32;
  561. if (RetVT == MVT::i64)
  562. return FPTOSINT_F64_I64;
  563. if (RetVT == MVT::i128)
  564. return FPTOSINT_F64_I128;
  565. } else if (OpVT == MVT::f80) {
  566. if (RetVT == MVT::i32)
  567. return FPTOSINT_F80_I32;
  568. if (RetVT == MVT::i64)
  569. return FPTOSINT_F80_I64;
  570. if (RetVT == MVT::i128)
  571. return FPTOSINT_F80_I128;
  572. } else if (OpVT == MVT::f128) {
  573. if (RetVT == MVT::i32)
  574. return FPTOSINT_F128_I32;
  575. if (RetVT == MVT::i64)
  576. return FPTOSINT_F128_I64;
  577. if (RetVT == MVT::i128)
  578. return FPTOSINT_F128_I128;
  579. } else if (OpVT == MVT::ppcf128) {
  580. if (RetVT == MVT::i32)
  581. return FPTOSINT_PPCF128_I32;
  582. if (RetVT == MVT::i64)
  583. return FPTOSINT_PPCF128_I64;
  584. if (RetVT == MVT::i128)
  585. return FPTOSINT_PPCF128_I128;
  586. }
  587. return UNKNOWN_LIBCALL;
  588. }
  589. /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
  590. /// UNKNOWN_LIBCALL if there is none.
  591. RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
  592. if (OpVT == MVT::f32) {
  593. if (RetVT == MVT::i32)
  594. return FPTOUINT_F32_I32;
  595. if (RetVT == MVT::i64)
  596. return FPTOUINT_F32_I64;
  597. if (RetVT == MVT::i128)
  598. return FPTOUINT_F32_I128;
  599. } else if (OpVT == MVT::f64) {
  600. if (RetVT == MVT::i32)
  601. return FPTOUINT_F64_I32;
  602. if (RetVT == MVT::i64)
  603. return FPTOUINT_F64_I64;
  604. if (RetVT == MVT::i128)
  605. return FPTOUINT_F64_I128;
  606. } else if (OpVT == MVT::f80) {
  607. if (RetVT == MVT::i32)
  608. return FPTOUINT_F80_I32;
  609. if (RetVT == MVT::i64)
  610. return FPTOUINT_F80_I64;
  611. if (RetVT == MVT::i128)
  612. return FPTOUINT_F80_I128;
  613. } else if (OpVT == MVT::f128) {
  614. if (RetVT == MVT::i32)
  615. return FPTOUINT_F128_I32;
  616. if (RetVT == MVT::i64)
  617. return FPTOUINT_F128_I64;
  618. if (RetVT == MVT::i128)
  619. return FPTOUINT_F128_I128;
  620. } else if (OpVT == MVT::ppcf128) {
  621. if (RetVT == MVT::i32)
  622. return FPTOUINT_PPCF128_I32;
  623. if (RetVT == MVT::i64)
  624. return FPTOUINT_PPCF128_I64;
  625. if (RetVT == MVT::i128)
  626. return FPTOUINT_PPCF128_I128;
  627. }
  628. return UNKNOWN_LIBCALL;
  629. }
  630. /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
  631. /// UNKNOWN_LIBCALL if there is none.
  632. RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
  633. if (OpVT == MVT::i32) {
  634. if (RetVT == MVT::f32)
  635. return SINTTOFP_I32_F32;
  636. if (RetVT == MVT::f64)
  637. return SINTTOFP_I32_F64;
  638. if (RetVT == MVT::f80)
  639. return SINTTOFP_I32_F80;
  640. if (RetVT == MVT::f128)
  641. return SINTTOFP_I32_F128;
  642. if (RetVT == MVT::ppcf128)
  643. return SINTTOFP_I32_PPCF128;
  644. } else if (OpVT == MVT::i64) {
  645. if (RetVT == MVT::f32)
  646. return SINTTOFP_I64_F32;
  647. if (RetVT == MVT::f64)
  648. return SINTTOFP_I64_F64;
  649. if (RetVT == MVT::f80)
  650. return SINTTOFP_I64_F80;
  651. if (RetVT == MVT::f128)
  652. return SINTTOFP_I64_F128;
  653. if (RetVT == MVT::ppcf128)
  654. return SINTTOFP_I64_PPCF128;
  655. } else if (OpVT == MVT::i128) {
  656. if (RetVT == MVT::f32)
  657. return SINTTOFP_I128_F32;
  658. if (RetVT == MVT::f64)
  659. return SINTTOFP_I128_F64;
  660. if (RetVT == MVT::f80)
  661. return SINTTOFP_I128_F80;
  662. if (RetVT == MVT::f128)
  663. return SINTTOFP_I128_F128;
  664. if (RetVT == MVT::ppcf128)
  665. return SINTTOFP_I128_PPCF128;
  666. }
  667. return UNKNOWN_LIBCALL;
  668. }
  669. /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
  670. /// UNKNOWN_LIBCALL if there is none.
  671. RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
  672. if (OpVT == MVT::i32) {
  673. if (RetVT == MVT::f32)
  674. return UINTTOFP_I32_F32;
  675. if (RetVT == MVT::f64)
  676. return UINTTOFP_I32_F64;
  677. if (RetVT == MVT::f80)
  678. return UINTTOFP_I32_F80;
  679. if (RetVT == MVT::f128)
  680. return UINTTOFP_I32_F128;
  681. if (RetVT == MVT::ppcf128)
  682. return UINTTOFP_I32_PPCF128;
  683. } else if (OpVT == MVT::i64) {
  684. if (RetVT == MVT::f32)
  685. return UINTTOFP_I64_F32;
  686. if (RetVT == MVT::f64)
  687. return UINTTOFP_I64_F64;
  688. if (RetVT == MVT::f80)
  689. return UINTTOFP_I64_F80;
  690. if (RetVT == MVT::f128)
  691. return UINTTOFP_I64_F128;
  692. if (RetVT == MVT::ppcf128)
  693. return UINTTOFP_I64_PPCF128;
  694. } else if (OpVT == MVT::i128) {
  695. if (RetVT == MVT::f32)
  696. return UINTTOFP_I128_F32;
  697. if (RetVT == MVT::f64)
  698. return UINTTOFP_I128_F64;
  699. if (RetVT == MVT::f80)
  700. return UINTTOFP_I128_F80;
  701. if (RetVT == MVT::f128)
  702. return UINTTOFP_I128_F128;
  703. if (RetVT == MVT::ppcf128)
  704. return UINTTOFP_I128_PPCF128;
  705. }
  706. return UNKNOWN_LIBCALL;
  707. }
  708. RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
  709. #define OP_TO_LIBCALL(Name, Enum) \
  710. case Name: \
  711. switch (VT.SimpleTy) { \
  712. default: \
  713. return UNKNOWN_LIBCALL; \
  714. case MVT::i8: \
  715. return Enum##_1; \
  716. case MVT::i16: \
  717. return Enum##_2; \
  718. case MVT::i32: \
  719. return Enum##_4; \
  720. case MVT::i64: \
  721. return Enum##_8; \
  722. case MVT::i128: \
  723. return Enum##_16; \
  724. }
  725. switch (Opc) {
  726. OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
  727. OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
  728. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
  729. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
  730. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
  731. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
  732. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
  733. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
  734. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
  735. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
  736. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
  737. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
  738. }
  739. #undef OP_TO_LIBCALL
  740. return UNKNOWN_LIBCALL;
  741. }
  742. RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_ATOMIC(uint64_t ElementSize) {
  743. switch (ElementSize) {
  744. case 1:
  745. return MEMCPY_ELEMENT_ATOMIC_1;
  746. case 2:
  747. return MEMCPY_ELEMENT_ATOMIC_2;
  748. case 4:
  749. return MEMCPY_ELEMENT_ATOMIC_4;
  750. case 8:
  751. return MEMCPY_ELEMENT_ATOMIC_8;
  752. case 16:
  753. return MEMCPY_ELEMENT_ATOMIC_16;
  754. default:
  755. return UNKNOWN_LIBCALL;
  756. }
  757. }
  758. /// InitCmpLibcallCCs - Set default comparison libcall CC.
  759. ///
  760. static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
  761. memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
  762. CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
  763. CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
  764. CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
  765. CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
  766. CCs[RTLIB::UNE_F32] = ISD::SETNE;
  767. CCs[RTLIB::UNE_F64] = ISD::SETNE;
  768. CCs[RTLIB::UNE_F128] = ISD::SETNE;
  769. CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
  770. CCs[RTLIB::OGE_F32] = ISD::SETGE;
  771. CCs[RTLIB::OGE_F64] = ISD::SETGE;
  772. CCs[RTLIB::OGE_F128] = ISD::SETGE;
  773. CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
  774. CCs[RTLIB::OLT_F32] = ISD::SETLT;
  775. CCs[RTLIB::OLT_F64] = ISD::SETLT;
  776. CCs[RTLIB::OLT_F128] = ISD::SETLT;
  777. CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
  778. CCs[RTLIB::OLE_F32] = ISD::SETLE;
  779. CCs[RTLIB::OLE_F64] = ISD::SETLE;
  780. CCs[RTLIB::OLE_F128] = ISD::SETLE;
  781. CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
  782. CCs[RTLIB::OGT_F32] = ISD::SETGT;
  783. CCs[RTLIB::OGT_F64] = ISD::SETGT;
  784. CCs[RTLIB::OGT_F128] = ISD::SETGT;
  785. CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
  786. CCs[RTLIB::UO_F32] = ISD::SETNE;
  787. CCs[RTLIB::UO_F64] = ISD::SETNE;
  788. CCs[RTLIB::UO_F128] = ISD::SETNE;
  789. CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
  790. CCs[RTLIB::O_F32] = ISD::SETEQ;
  791. CCs[RTLIB::O_F64] = ISD::SETEQ;
  792. CCs[RTLIB::O_F128] = ISD::SETEQ;
  793. CCs[RTLIB::O_PPCF128] = ISD::SETEQ;
  794. }
  795. /// NOTE: The TargetMachine owns TLOF.
  796. TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
  797. initActions();
  798. // Perform these initializations only once.
  799. MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove = 8;
  800. MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize
  801. = MaxStoresPerMemmoveOptSize = 4;
  802. UseUnderscoreSetJmp = false;
  803. UseUnderscoreLongJmp = false;
  804. HasMultipleConditionRegisters = false;
  805. HasExtractBitsInsn = false;
  806. JumpIsExpensive = JumpIsExpensiveOverride;
  807. PredictableSelectIsExpensive = false;
  808. EnableExtLdPromotion = false;
  809. HasFloatingPointExceptions = true;
  810. StackPointerRegisterToSaveRestore = 0;
  811. BooleanContents = UndefinedBooleanContent;
  812. BooleanFloatContents = UndefinedBooleanContent;
  813. BooleanVectorContents = UndefinedBooleanContent;
  814. SchedPreferenceInfo = Sched::ILP;
  815. JumpBufSize = 0;
  816. JumpBufAlignment = 0;
  817. MinFunctionAlignment = 0;
  818. PrefFunctionAlignment = 0;
  819. PrefLoopAlignment = 0;
  820. GatherAllAliasesMaxDepth = 18;
  821. MinStackArgumentAlignment = 1;
  822. // TODO: the default will be switched to 0 in the next commit, along
  823. // with the Target-specific changes necessary.
  824. MaxAtomicSizeInBitsSupported = 1024;
  825. MinCmpXchgSizeInBits = 0;
  826. std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
  827. InitLibcallNames(LibcallRoutineNames, TM.getTargetTriple());
  828. InitCmpLibcallCCs(CmpLibcallCCs);
  829. InitLibcallCallingConvs(LibcallCallingConvs);
  830. }
  831. void TargetLoweringBase::initActions() {
  832. // All operations default to being supported.
  833. memset(OpActions, 0, sizeof(OpActions));
  834. memset(LoadExtActions, 0, sizeof(LoadExtActions));
  835. memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
  836. memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
  837. memset(CondCodeActions, 0, sizeof(CondCodeActions));
  838. std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
  839. std::fill(std::begin(TargetDAGCombineArray),
  840. std::end(TargetDAGCombineArray), 0);
  841. // Set default actions for various operations.
  842. for (MVT VT : MVT::all_valuetypes()) {
  843. // Default all indexed load / store to expand.
  844. for (unsigned IM = (unsigned)ISD::PRE_INC;
  845. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  846. setIndexedLoadAction(IM, VT, Expand);
  847. setIndexedStoreAction(IM, VT, Expand);
  848. }
  849. // Most backends expect to see the node which just returns the value loaded.
  850. setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
  851. // These operations default to expand.
  852. setOperationAction(ISD::FGETSIGN, VT, Expand);
  853. setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
  854. setOperationAction(ISD::FMINNUM, VT, Expand);
  855. setOperationAction(ISD::FMAXNUM, VT, Expand);
  856. setOperationAction(ISD::FMINNAN, VT, Expand);
  857. setOperationAction(ISD::FMAXNAN, VT, Expand);
  858. setOperationAction(ISD::FMAD, VT, Expand);
  859. setOperationAction(ISD::SMIN, VT, Expand);
  860. setOperationAction(ISD::SMAX, VT, Expand);
  861. setOperationAction(ISD::UMIN, VT, Expand);
  862. setOperationAction(ISD::UMAX, VT, Expand);
  863. setOperationAction(ISD::ABS, VT, Expand);
  864. // Overflow operations default to expand
  865. setOperationAction(ISD::SADDO, VT, Expand);
  866. setOperationAction(ISD::SSUBO, VT, Expand);
  867. setOperationAction(ISD::UADDO, VT, Expand);
  868. setOperationAction(ISD::USUBO, VT, Expand);
  869. setOperationAction(ISD::SMULO, VT, Expand);
  870. setOperationAction(ISD::UMULO, VT, Expand);
  871. // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
  872. setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
  873. setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
  874. setOperationAction(ISD::BITREVERSE, VT, Expand);
  875. // These library functions default to expand.
  876. setOperationAction(ISD::FROUND, VT, Expand);
  877. // These operations default to expand for vector types.
  878. if (VT.isVector()) {
  879. setOperationAction(ISD::FCOPYSIGN, VT, Expand);
  880. setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
  881. setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
  882. setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
  883. }
  884. // For most targets @llvm.get.dynamic.area.offset just returns 0.
  885. setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
  886. }
  887. // Most targets ignore the @llvm.prefetch intrinsic.
  888. setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
  889. // Most targets also ignore the @llvm.readcyclecounter intrinsic.
  890. setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
  891. // ConstantFP nodes default to expand. Targets can either change this to
  892. // Legal, in which case all fp constants are legal, or use isFPImmLegal()
  893. // to optimize expansions for certain constants.
  894. setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
  895. setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
  896. setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
  897. setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
  898. setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
  899. // These library functions default to expand.
  900. for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
  901. setOperationAction(ISD::FLOG , VT, Expand);
  902. setOperationAction(ISD::FLOG2, VT, Expand);
  903. setOperationAction(ISD::FLOG10, VT, Expand);
  904. setOperationAction(ISD::FEXP , VT, Expand);
  905. setOperationAction(ISD::FEXP2, VT, Expand);
  906. setOperationAction(ISD::FFLOOR, VT, Expand);
  907. setOperationAction(ISD::FNEARBYINT, VT, Expand);
  908. setOperationAction(ISD::FCEIL, VT, Expand);
  909. setOperationAction(ISD::FRINT, VT, Expand);
  910. setOperationAction(ISD::FTRUNC, VT, Expand);
  911. setOperationAction(ISD::FROUND, VT, Expand);
  912. }
  913. // Default ISD::TRAP to expand (which turns it into abort).
  914. setOperationAction(ISD::TRAP, MVT::Other, Expand);
  915. // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
  916. // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
  917. //
  918. setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
  919. }
  920. MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
  921. EVT) const {
  922. return MVT::getIntegerVT(8 * DL.getPointerSize(0));
  923. }
  924. EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy,
  925. const DataLayout &DL) const {
  926. assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
  927. if (LHSTy.isVector())
  928. return LHSTy;
  929. return getScalarShiftAmountTy(DL, LHSTy);
  930. }
  931. bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
  932. assert(isTypeLegal(VT));
  933. switch (Op) {
  934. default:
  935. return false;
  936. case ISD::SDIV:
  937. case ISD::UDIV:
  938. case ISD::SREM:
  939. case ISD::UREM:
  940. return true;
  941. }
  942. }
  943. void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
  944. // If the command-line option was specified, ignore this request.
  945. if (!JumpIsExpensiveOverride.getNumOccurrences())
  946. JumpIsExpensive = isExpensive;
  947. }
  948. TargetLoweringBase::LegalizeKind
  949. TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
  950. // If this is a simple type, use the ComputeRegisterProp mechanism.
  951. if (VT.isSimple()) {
  952. MVT SVT = VT.getSimpleVT();
  953. assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
  954. MVT NVT = TransformToType[SVT.SimpleTy];
  955. LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
  956. assert((LA == TypeLegal || LA == TypeSoftenFloat ||
  957. ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger) &&
  958. "Promote may not follow Expand or Promote");
  959. if (LA == TypeSplitVector)
  960. return LegalizeKind(LA,
  961. EVT::getVectorVT(Context, SVT.getVectorElementType(),
  962. SVT.getVectorNumElements() / 2));
  963. if (LA == TypeScalarizeVector)
  964. return LegalizeKind(LA, SVT.getVectorElementType());
  965. return LegalizeKind(LA, NVT);
  966. }
  967. // Handle Extended Scalar Types.
  968. if (!VT.isVector()) {
  969. assert(VT.isInteger() && "Float types must be simple");
  970. unsigned BitSize = VT.getSizeInBits();
  971. // First promote to a power-of-two size, then expand if necessary.
  972. if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
  973. EVT NVT = VT.getRoundIntegerType(Context);
  974. assert(NVT != VT && "Unable to round integer VT");
  975. LegalizeKind NextStep = getTypeConversion(Context, NVT);
  976. // Avoid multi-step promotion.
  977. if (NextStep.first == TypePromoteInteger)
  978. return NextStep;
  979. // Return rounded integer type.
  980. return LegalizeKind(TypePromoteInteger, NVT);
  981. }
  982. return LegalizeKind(TypeExpandInteger,
  983. EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
  984. }
  985. // Handle vector types.
  986. unsigned NumElts = VT.getVectorNumElements();
  987. EVT EltVT = VT.getVectorElementType();
  988. // Vectors with only one element are always scalarized.
  989. if (NumElts == 1)
  990. return LegalizeKind(TypeScalarizeVector, EltVT);
  991. // Try to widen vector elements until the element type is a power of two and
  992. // promote it to a legal type later on, for example:
  993. // <3 x i8> -> <4 x i8> -> <4 x i32>
  994. if (EltVT.isInteger()) {
  995. // Vectors with a number of elements that is not a power of two are always
  996. // widened, for example <3 x i8> -> <4 x i8>.
  997. if (!VT.isPow2VectorType()) {
  998. NumElts = (unsigned)NextPowerOf2(NumElts);
  999. EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  1000. return LegalizeKind(TypeWidenVector, NVT);
  1001. }
  1002. // Examine the element type.
  1003. LegalizeKind LK = getTypeConversion(Context, EltVT);
  1004. // If type is to be expanded, split the vector.
  1005. // <4 x i140> -> <2 x i140>
  1006. if (LK.first == TypeExpandInteger)
  1007. return LegalizeKind(TypeSplitVector,
  1008. EVT::getVectorVT(Context, EltVT, NumElts / 2));
  1009. // Promote the integer element types until a legal vector type is found
  1010. // or until the element integer type is too big. If a legal type was not
  1011. // found, fallback to the usual mechanism of widening/splitting the
  1012. // vector.
  1013. EVT OldEltVT = EltVT;
  1014. while (1) {
  1015. // Increase the bitwidth of the element to the next pow-of-two
  1016. // (which is greater than 8 bits).
  1017. EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
  1018. .getRoundIntegerType(Context);
  1019. // Stop trying when getting a non-simple element type.
  1020. // Note that vector elements may be greater than legal vector element
  1021. // types. Example: X86 XMM registers hold 64bit element on 32bit
  1022. // systems.
  1023. if (!EltVT.isSimple())
  1024. break;
  1025. // Build a new vector type and check if it is legal.
  1026. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  1027. // Found a legal promoted vector type.
  1028. if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
  1029. return LegalizeKind(TypePromoteInteger,
  1030. EVT::getVectorVT(Context, EltVT, NumElts));
  1031. }
  1032. // Reset the type to the unexpanded type if we did not find a legal vector
  1033. // type with a promoted vector element type.
  1034. EltVT = OldEltVT;
  1035. }
  1036. // Try to widen the vector until a legal type is found.
  1037. // If there is no wider legal type, split the vector.
  1038. while (1) {
  1039. // Round up to the next power of 2.
  1040. NumElts = (unsigned)NextPowerOf2(NumElts);
  1041. // If there is no simple vector type with this many elements then there
  1042. // cannot be a larger legal vector type. Note that this assumes that
  1043. // there are no skipped intermediate vector types in the simple types.
  1044. if (!EltVT.isSimple())
  1045. break;
  1046. MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  1047. if (LargerVector == MVT())
  1048. break;
  1049. // If this type is legal then widen the vector.
  1050. if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
  1051. return LegalizeKind(TypeWidenVector, LargerVector);
  1052. }
  1053. // Widen odd vectors to next power of two.
  1054. if (!VT.isPow2VectorType()) {
  1055. EVT NVT = VT.getPow2VectorType(Context);
  1056. return LegalizeKind(TypeWidenVector, NVT);
  1057. }
  1058. // Vectors with illegal element types are expanded.
  1059. EVT NVT = EVT::getVectorVT(Context, EltVT, VT.getVectorNumElements() / 2);
  1060. return LegalizeKind(TypeSplitVector, NVT);
  1061. }
  1062. static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
  1063. unsigned &NumIntermediates,
  1064. MVT &RegisterVT,
  1065. TargetLoweringBase *TLI) {
  1066. // Figure out the right, legal destination reg to copy into.
  1067. unsigned NumElts = VT.getVectorNumElements();
  1068. MVT EltTy = VT.getVectorElementType();
  1069. unsigned NumVectorRegs = 1;
  1070. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  1071. // could break down into LHS/RHS like LegalizeDAG does.
  1072. if (!isPowerOf2_32(NumElts)) {
  1073. NumVectorRegs = NumElts;
  1074. NumElts = 1;
  1075. }
  1076. // Divide the input until we get to a supported size. This will always
  1077. // end with a scalar if the target doesn't support vectors.
  1078. while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
  1079. NumElts >>= 1;
  1080. NumVectorRegs <<= 1;
  1081. }
  1082. NumIntermediates = NumVectorRegs;
  1083. MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
  1084. if (!TLI->isTypeLegal(NewVT))
  1085. NewVT = EltTy;
  1086. IntermediateVT = NewVT;
  1087. unsigned NewVTSize = NewVT.getSizeInBits();
  1088. // Convert sizes such as i33 to i64.
  1089. if (!isPowerOf2_32(NewVTSize))
  1090. NewVTSize = NextPowerOf2(NewVTSize);
  1091. MVT DestVT = TLI->getRegisterType(NewVT);
  1092. RegisterVT = DestVT;
  1093. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1094. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1095. // Otherwise, promotion or legal types use the same number of registers as
  1096. // the vector decimated to the appropriate level.
  1097. return NumVectorRegs;
  1098. }
  1099. /// isLegalRC - Return true if the value types that can be represented by the
  1100. /// specified register class are all legal.
  1101. bool TargetLoweringBase::isLegalRC(const TargetRegisterClass *RC) const {
  1102. for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
  1103. I != E; ++I) {
  1104. if (isTypeLegal(*I))
  1105. return true;
  1106. }
  1107. return false;
  1108. }
  1109. /// Replace/modify any TargetFrameIndex operands with a targte-dependent
  1110. /// sequence of memory operands that is recognized by PrologEpilogInserter.
  1111. MachineBasicBlock *
  1112. TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
  1113. MachineBasicBlock *MBB) const {
  1114. MachineInstr *MI = &InitialMI;
  1115. MachineFunction &MF = *MI->getParent()->getParent();
  1116. MachineFrameInfo &MFI = MF.getFrameInfo();
  1117. // We're handling multiple types of operands here:
  1118. // PATCHPOINT MetaArgs - live-in, read only, direct
  1119. // STATEPOINT Deopt Spill - live-through, read only, indirect
  1120. // STATEPOINT Deopt Alloca - live-through, read only, direct
  1121. // (We're currently conservative and mark the deopt slots read/write in
  1122. // practice.)
  1123. // STATEPOINT GC Spill - live-through, read/write, indirect
  1124. // STATEPOINT GC Alloca - live-through, read/write, direct
  1125. // The live-in vs live-through is handled already (the live through ones are
  1126. // all stack slots), but we need to handle the different type of stackmap
  1127. // operands and memory effects here.
  1128. // MI changes inside this loop as we grow operands.
  1129. for(unsigned OperIdx = 0; OperIdx != MI->getNumOperands(); ++OperIdx) {
  1130. MachineOperand &MO = MI->getOperand(OperIdx);
  1131. if (!MO.isFI())
  1132. continue;
  1133. // foldMemoryOperand builds a new MI after replacing a single FI operand
  1134. // with the canonical set of five x86 addressing-mode operands.
  1135. int FI = MO.getIndex();
  1136. MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
  1137. // Copy operands before the frame-index.
  1138. for (unsigned i = 0; i < OperIdx; ++i)
  1139. MIB.add(MI->getOperand(i));
  1140. // Add frame index operands recognized by stackmaps.cpp
  1141. if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
  1142. // indirect-mem-ref tag, size, #FI, offset.
  1143. // Used for spills inserted by StatepointLowering. This codepath is not
  1144. // used for patchpoints/stackmaps at all, for these spilling is done via
  1145. // foldMemoryOperand callback only.
  1146. assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
  1147. MIB.addImm(StackMaps::IndirectMemRefOp);
  1148. MIB.addImm(MFI.getObjectSize(FI));
  1149. MIB.add(MI->getOperand(OperIdx));
  1150. MIB.addImm(0);
  1151. } else {
  1152. // direct-mem-ref tag, #FI, offset.
  1153. // Used by patchpoint, and direct alloca arguments to statepoints
  1154. MIB.addImm(StackMaps::DirectMemRefOp);
  1155. MIB.add(MI->getOperand(OperIdx));
  1156. MIB.addImm(0);
  1157. }
  1158. // Copy the operands after the frame index.
  1159. for (unsigned i = OperIdx + 1; i != MI->getNumOperands(); ++i)
  1160. MIB.add(MI->getOperand(i));
  1161. // Inherit previous memory operands.
  1162. MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
  1163. assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
  1164. // Add a new memory operand for this FI.
  1165. assert(MFI.getObjectOffset(FI) != -1);
  1166. auto Flags = MachineMemOperand::MOLoad;
  1167. if (MI->getOpcode() == TargetOpcode::STATEPOINT) {
  1168. Flags |= MachineMemOperand::MOStore;
  1169. Flags |= MachineMemOperand::MOVolatile;
  1170. }
  1171. MachineMemOperand *MMO = MF.getMachineMemOperand(
  1172. MachinePointerInfo::getFixedStack(MF, FI), Flags,
  1173. MF.getDataLayout().getPointerSize(), MFI.getObjectAlignment(FI));
  1174. MIB->addMemOperand(MF, MMO);
  1175. // Replace the instruction and update the operand index.
  1176. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  1177. OperIdx += (MIB->getNumOperands() - MI->getNumOperands()) - 1;
  1178. MI->eraseFromParent();
  1179. MI = MIB;
  1180. }
  1181. return MBB;
  1182. }
  1183. /// findRepresentativeClass - Return the largest legal super-reg register class
  1184. /// of the register class for the specified type and its associated "cost".
  1185. // This function is in TargetLowering because it uses RegClassForVT which would
  1186. // need to be moved to TargetRegisterInfo and would necessitate moving
  1187. // isTypeLegal over as well - a massive change that would just require
  1188. // TargetLowering having a TargetRegisterInfo class member that it would use.
  1189. std::pair<const TargetRegisterClass *, uint8_t>
  1190. TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
  1191. MVT VT) const {
  1192. const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
  1193. if (!RC)
  1194. return std::make_pair(RC, 0);
  1195. // Compute the set of all super-register classes.
  1196. BitVector SuperRegRC(TRI->getNumRegClasses());
  1197. for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
  1198. SuperRegRC.setBitsInMask(RCI.getMask());
  1199. // Find the first legal register class with the largest spill size.
  1200. const TargetRegisterClass *BestRC = RC;
  1201. for (int i = SuperRegRC.find_first(); i >= 0; i = SuperRegRC.find_next(i)) {
  1202. const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
  1203. // We want the largest possible spill size.
  1204. if (SuperRC->getSize() <= BestRC->getSize())
  1205. continue;
  1206. if (!isLegalRC(SuperRC))
  1207. continue;
  1208. BestRC = SuperRC;
  1209. }
  1210. return std::make_pair(BestRC, 1);
  1211. }
  1212. /// computeRegisterProperties - Once all of the register classes are added,
  1213. /// this allows us to compute derived properties we expose.
  1214. void TargetLoweringBase::computeRegisterProperties(
  1215. const TargetRegisterInfo *TRI) {
  1216. static_assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE,
  1217. "Too many value types for ValueTypeActions to hold!");
  1218. // Everything defaults to needing one register.
  1219. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1220. NumRegistersForVT[i] = 1;
  1221. RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
  1222. }
  1223. // ...except isVoid, which doesn't need any registers.
  1224. NumRegistersForVT[MVT::isVoid] = 0;
  1225. // Find the largest integer register class.
  1226. unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
  1227. for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
  1228. assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
  1229. // Every integer value type larger than this largest register takes twice as
  1230. // many registers to represent as the previous ValueType.
  1231. for (unsigned ExpandedReg = LargestIntReg + 1;
  1232. ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
  1233. NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
  1234. RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
  1235. TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
  1236. ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
  1237. TypeExpandInteger);
  1238. }
  1239. // Inspect all of the ValueType's smaller than the largest integer
  1240. // register to see which ones need promotion.
  1241. unsigned LegalIntReg = LargestIntReg;
  1242. for (unsigned IntReg = LargestIntReg - 1;
  1243. IntReg >= (unsigned)MVT::i1; --IntReg) {
  1244. MVT IVT = (MVT::SimpleValueType)IntReg;
  1245. if (isTypeLegal(IVT)) {
  1246. LegalIntReg = IntReg;
  1247. } else {
  1248. RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
  1249. (const MVT::SimpleValueType)LegalIntReg;
  1250. ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
  1251. }
  1252. }
  1253. // ppcf128 type is really two f64's.
  1254. if (!isTypeLegal(MVT::ppcf128)) {
  1255. if (isTypeLegal(MVT::f64)) {
  1256. NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
  1257. RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
  1258. TransformToType[MVT::ppcf128] = MVT::f64;
  1259. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
  1260. } else {
  1261. NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
  1262. RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
  1263. TransformToType[MVT::ppcf128] = MVT::i128;
  1264. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
  1265. }
  1266. }
  1267. // Decide how to handle f128. If the target does not have native f128 support,
  1268. // expand it to i128 and we will be generating soft float library calls.
  1269. if (!isTypeLegal(MVT::f128)) {
  1270. NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
  1271. RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
  1272. TransformToType[MVT::f128] = MVT::i128;
  1273. ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
  1274. }
  1275. // Decide how to handle f64. If the target does not have native f64 support,
  1276. // expand it to i64 and we will be generating soft float library calls.
  1277. if (!isTypeLegal(MVT::f64)) {
  1278. NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
  1279. RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
  1280. TransformToType[MVT::f64] = MVT::i64;
  1281. ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
  1282. }
  1283. // Decide how to handle f32. If the target does not have native f32 support,
  1284. // expand it to i32 and we will be generating soft float library calls.
  1285. if (!isTypeLegal(MVT::f32)) {
  1286. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
  1287. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
  1288. TransformToType[MVT::f32] = MVT::i32;
  1289. ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
  1290. }
  1291. // Decide how to handle f16. If the target does not have native f16 support,
  1292. // promote it to f32, because there are no f16 library calls (except for
  1293. // conversions).
  1294. if (!isTypeLegal(MVT::f16)) {
  1295. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
  1296. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
  1297. TransformToType[MVT::f16] = MVT::f32;
  1298. ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
  1299. }
  1300. // Loop over all of the vector value types to see which need transformations.
  1301. for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
  1302. i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
  1303. MVT VT = (MVT::SimpleValueType) i;
  1304. if (isTypeLegal(VT))
  1305. continue;
  1306. MVT EltVT = VT.getVectorElementType();
  1307. unsigned NElts = VT.getVectorNumElements();
  1308. bool IsLegalWiderType = false;
  1309. LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
  1310. switch (PreferredAction) {
  1311. case TypePromoteInteger: {
  1312. // Try to promote the elements of integer vectors. If no legal
  1313. // promotion was found, fall through to the widen-vector method.
  1314. for (unsigned nVT = i + 1; nVT <= MVT::LAST_INTEGER_VECTOR_VALUETYPE; ++nVT) {
  1315. MVT SVT = (MVT::SimpleValueType) nVT;
  1316. // Promote vectors of integers to vectors with the same number
  1317. // of elements, with a wider element type.
  1318. if (SVT.getScalarSizeInBits() > EltVT.getSizeInBits() &&
  1319. SVT.getVectorNumElements() == NElts && isTypeLegal(SVT)) {
  1320. TransformToType[i] = SVT;
  1321. RegisterTypeForVT[i] = SVT;
  1322. NumRegistersForVT[i] = 1;
  1323. ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
  1324. IsLegalWiderType = true;
  1325. break;
  1326. }
  1327. }
  1328. if (IsLegalWiderType)
  1329. break;
  1330. }
  1331. case TypeWidenVector: {
  1332. // Try to widen the vector.
  1333. for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  1334. MVT SVT = (MVT::SimpleValueType) nVT;
  1335. if (SVT.getVectorElementType() == EltVT
  1336. && SVT.getVectorNumElements() > NElts && isTypeLegal(SVT)) {
  1337. TransformToType[i] = SVT;
  1338. RegisterTypeForVT[i] = SVT;
  1339. NumRegistersForVT[i] = 1;
  1340. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1341. IsLegalWiderType = true;
  1342. break;
  1343. }
  1344. }
  1345. if (IsLegalWiderType)
  1346. break;
  1347. }
  1348. case TypeSplitVector:
  1349. case TypeScalarizeVector: {
  1350. MVT IntermediateVT;
  1351. MVT RegisterVT;
  1352. unsigned NumIntermediates;
  1353. NumRegistersForVT[i] = getVectorTypeBreakdownMVT(VT, IntermediateVT,
  1354. NumIntermediates, RegisterVT, this);
  1355. RegisterTypeForVT[i] = RegisterVT;
  1356. MVT NVT = VT.getPow2VectorType();
  1357. if (NVT == VT) {
  1358. // Type is already a power of 2. The default action is to split.
  1359. TransformToType[i] = MVT::Other;
  1360. if (PreferredAction == TypeScalarizeVector)
  1361. ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
  1362. else if (PreferredAction == TypeSplitVector)
  1363. ValueTypeActions.setTypeAction(VT, TypeSplitVector);
  1364. else
  1365. // Set type action according to the number of elements.
  1366. ValueTypeActions.setTypeAction(VT, NElts == 1 ? TypeScalarizeVector
  1367. : TypeSplitVector);
  1368. } else {
  1369. TransformToType[i] = NVT;
  1370. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1371. }
  1372. break;
  1373. }
  1374. default:
  1375. llvm_unreachable("Unknown vector legalization action!");
  1376. }
  1377. }
  1378. // Determine the 'representative' register class for each value type.
  1379. // An representative register class is the largest (meaning one which is
  1380. // not a sub-register class / subreg register class) legal register class for
  1381. // a group of value types. For example, on i386, i8, i16, and i32
  1382. // representative would be GR32; while on x86_64 it's GR64.
  1383. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1384. const TargetRegisterClass* RRC;
  1385. uint8_t Cost;
  1386. std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
  1387. RepRegClassForVT[i] = RRC;
  1388. RepRegClassCostForVT[i] = Cost;
  1389. }
  1390. }
  1391. EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
  1392. EVT VT) const {
  1393. assert(!VT.isVector() && "No default SetCC type for vectors!");
  1394. return getPointerTy(DL).SimpleTy;
  1395. }
  1396. MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
  1397. return MVT::i32; // return the default value
  1398. }
  1399. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  1400. /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
  1401. /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
  1402. /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
  1403. ///
  1404. /// This method returns the number of registers needed, and the VT for each
  1405. /// register. It also returns the VT and quantity of the intermediate values
  1406. /// before they are promoted/expanded.
  1407. ///
  1408. unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
  1409. EVT &IntermediateVT,
  1410. unsigned &NumIntermediates,
  1411. MVT &RegisterVT) const {
  1412. unsigned NumElts = VT.getVectorNumElements();
  1413. // If there is a wider vector type with the same element type as this one,
  1414. // or a promoted vector type that has the same number of elements which
  1415. // are wider, then we should convert to that legal vector type.
  1416. // This handles things like <2 x float> -> <4 x float> and
  1417. // <4 x i1> -> <4 x i32>.
  1418. LegalizeTypeAction TA = getTypeAction(Context, VT);
  1419. if (NumElts != 1 && (TA == TypeWidenVector || TA == TypePromoteInteger)) {
  1420. EVT RegisterEVT = getTypeToTransformTo(Context, VT);
  1421. if (isTypeLegal(RegisterEVT)) {
  1422. IntermediateVT = RegisterEVT;
  1423. RegisterVT = RegisterEVT.getSimpleVT();
  1424. NumIntermediates = 1;
  1425. return 1;
  1426. }
  1427. }
  1428. // Figure out the right, legal destination reg to copy into.
  1429. EVT EltTy = VT.getVectorElementType();
  1430. unsigned NumVectorRegs = 1;
  1431. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  1432. // could break down into LHS/RHS like LegalizeDAG does.
  1433. if (!isPowerOf2_32(NumElts)) {
  1434. NumVectorRegs = NumElts;
  1435. NumElts = 1;
  1436. }
  1437. // Divide the input until we get to a supported size. This will always
  1438. // end with a scalar if the target doesn't support vectors.
  1439. while (NumElts > 1 && !isTypeLegal(
  1440. EVT::getVectorVT(Context, EltTy, NumElts))) {
  1441. NumElts >>= 1;
  1442. NumVectorRegs <<= 1;
  1443. }
  1444. NumIntermediates = NumVectorRegs;
  1445. EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
  1446. if (!isTypeLegal(NewVT))
  1447. NewVT = EltTy;
  1448. IntermediateVT = NewVT;
  1449. MVT DestVT = getRegisterType(Context, NewVT);
  1450. RegisterVT = DestVT;
  1451. unsigned NewVTSize = NewVT.getSizeInBits();
  1452. // Convert sizes such as i33 to i64.
  1453. if (!isPowerOf2_32(NewVTSize))
  1454. NewVTSize = NextPowerOf2(NewVTSize);
  1455. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1456. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1457. // Otherwise, promotion or legal types use the same number of registers as
  1458. // the vector decimated to the appropriate level.
  1459. return NumVectorRegs;
  1460. }
  1461. /// Get the EVTs and ArgFlags collections that represent the legalized return
  1462. /// type of the given function. This does not require a DAG or a return value,
  1463. /// and is suitable for use before any DAGs for the function are constructed.
  1464. /// TODO: Move this out of TargetLowering.cpp.
  1465. void llvm::GetReturnInfo(Type *ReturnType, AttributeList attr,
  1466. SmallVectorImpl<ISD::OutputArg> &Outs,
  1467. const TargetLowering &TLI, const DataLayout &DL) {
  1468. SmallVector<EVT, 4> ValueVTs;
  1469. ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
  1470. unsigned NumValues = ValueVTs.size();
  1471. if (NumValues == 0) return;
  1472. for (unsigned j = 0, f = NumValues; j != f; ++j) {
  1473. EVT VT = ValueVTs[j];
  1474. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  1475. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
  1476. ExtendKind = ISD::SIGN_EXTEND;
  1477. else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
  1478. ExtendKind = ISD::ZERO_EXTEND;
  1479. // FIXME: C calling convention requires the return type to be promoted to
  1480. // at least 32-bit. But this is not necessary for non-C calling
  1481. // conventions. The frontend should mark functions whose return values
  1482. // require promoting with signext or zeroext attributes.
  1483. if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
  1484. MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
  1485. if (VT.bitsLT(MinVT))
  1486. VT = MinVT;
  1487. }
  1488. unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
  1489. MVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
  1490. // 'inreg' on function refers to return value
  1491. ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
  1492. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
  1493. Flags.setInReg();
  1494. // Propagate extension type if any
  1495. if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
  1496. Flags.setSExt();
  1497. else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
  1498. Flags.setZExt();
  1499. for (unsigned i = 0; i < NumParts; ++i)
  1500. Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isFixed=*/true, 0, 0));
  1501. }
  1502. }
  1503. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  1504. /// function arguments in the caller parameter area. This is the actual
  1505. /// alignment, not its logarithm.
  1506. unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
  1507. const DataLayout &DL) const {
  1508. return DL.getABITypeAlignment(Ty);
  1509. }
  1510. bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
  1511. const DataLayout &DL, EVT VT,
  1512. unsigned AddrSpace,
  1513. unsigned Alignment,
  1514. bool *Fast) const {
  1515. // Check if the specified alignment is sufficient based on the data layout.
  1516. // TODO: While using the data layout works in practice, a better solution
  1517. // would be to implement this check directly (make this a virtual function).
  1518. // For example, the ABI alignment may change based on software platform while
  1519. // this function should only be affected by hardware implementation.
  1520. Type *Ty = VT.getTypeForEVT(Context);
  1521. if (Alignment >= DL.getABITypeAlignment(Ty)) {
  1522. // Assume that an access that meets the ABI-specified alignment is fast.
  1523. if (Fast != nullptr)
  1524. *Fast = true;
  1525. return true;
  1526. }
  1527. // This is a misaligned access.
  1528. return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Fast);
  1529. }
  1530. BranchProbability TargetLoweringBase::getPredictableBranchThreshold() const {
  1531. return BranchProbability(MinPercentageForPredictableBranch, 100);
  1532. }
  1533. //===----------------------------------------------------------------------===//
  1534. // TargetTransformInfo Helpers
  1535. //===----------------------------------------------------------------------===//
  1536. int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
  1537. enum InstructionOpcodes {
  1538. #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
  1539. #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
  1540. #include "llvm/IR/Instruction.def"
  1541. };
  1542. switch (static_cast<InstructionOpcodes>(Opcode)) {
  1543. case Ret: return 0;
  1544. case Br: return 0;
  1545. case Switch: return 0;
  1546. case IndirectBr: return 0;
  1547. case Invoke: return 0;
  1548. case Resume: return 0;
  1549. case Unreachable: return 0;
  1550. case CleanupRet: return 0;
  1551. case CatchRet: return 0;
  1552. case CatchPad: return 0;
  1553. case CatchSwitch: return 0;
  1554. case CleanupPad: return 0;
  1555. case Add: return ISD::ADD;
  1556. case FAdd: return ISD::FADD;
  1557. case Sub: return ISD::SUB;
  1558. case FSub: return ISD::FSUB;
  1559. case Mul: return ISD::MUL;
  1560. case FMul: return ISD::FMUL;
  1561. case UDiv: return ISD::UDIV;
  1562. case SDiv: return ISD::SDIV;
  1563. case FDiv: return ISD::FDIV;
  1564. case URem: return ISD::UREM;
  1565. case SRem: return ISD::SREM;
  1566. case FRem: return ISD::FREM;
  1567. case Shl: return ISD::SHL;
  1568. case LShr: return ISD::SRL;
  1569. case AShr: return ISD::SRA;
  1570. case And: return ISD::AND;
  1571. case Or: return ISD::OR;
  1572. case Xor: return ISD::XOR;
  1573. case Alloca: return 0;
  1574. case Load: return ISD::LOAD;
  1575. case Store: return ISD::STORE;
  1576. case GetElementPtr: return 0;
  1577. case Fence: return 0;
  1578. case AtomicCmpXchg: return 0;
  1579. case AtomicRMW: return 0;
  1580. case Trunc: return ISD::TRUNCATE;
  1581. case ZExt: return ISD::ZERO_EXTEND;
  1582. case SExt: return ISD::SIGN_EXTEND;
  1583. case FPToUI: return ISD::FP_TO_UINT;
  1584. case FPToSI: return ISD::FP_TO_SINT;
  1585. case UIToFP: return ISD::UINT_TO_FP;
  1586. case SIToFP: return ISD::SINT_TO_FP;
  1587. case FPTrunc: return ISD::FP_ROUND;
  1588. case FPExt: return ISD::FP_EXTEND;
  1589. case PtrToInt: return ISD::BITCAST;
  1590. case IntToPtr: return ISD::BITCAST;
  1591. case BitCast: return ISD::BITCAST;
  1592. case AddrSpaceCast: return ISD::ADDRSPACECAST;
  1593. case ICmp: return ISD::SETCC;
  1594. case FCmp: return ISD::SETCC;
  1595. case PHI: return 0;
  1596. case Call: return 0;
  1597. case Select: return ISD::SELECT;
  1598. case UserOp1: return 0;
  1599. case UserOp2: return 0;
  1600. case VAArg: return 0;
  1601. case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
  1602. case InsertElement: return ISD::INSERT_VECTOR_ELT;
  1603. case ShuffleVector: return ISD::VECTOR_SHUFFLE;
  1604. case ExtractValue: return ISD::MERGE_VALUES;
  1605. case InsertValue: return ISD::MERGE_VALUES;
  1606. case LandingPad: return 0;
  1607. }
  1608. llvm_unreachable("Unknown instruction type encountered!");
  1609. }
  1610. std::pair<int, MVT>
  1611. TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
  1612. Type *Ty) const {
  1613. LLVMContext &C = Ty->getContext();
  1614. EVT MTy = getValueType(DL, Ty);
  1615. int Cost = 1;
  1616. // We keep legalizing the type until we find a legal kind. We assume that
  1617. // the only operation that costs anything is the split. After splitting
  1618. // we need to handle two types.
  1619. while (true) {
  1620. LegalizeKind LK = getTypeConversion(C, MTy);
  1621. if (LK.first == TypeLegal)
  1622. return std::make_pair(Cost, MTy.getSimpleVT());
  1623. if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
  1624. Cost *= 2;
  1625. // Do not loop with f128 type.
  1626. if (MTy == LK.second)
  1627. return std::make_pair(Cost, MTy.getSimpleVT());
  1628. // Keep legalizing the type.
  1629. MTy = LK.second;
  1630. }
  1631. }
  1632. Value *TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilder<> &IRB,
  1633. bool UseTLS) const {
  1634. // compiler-rt provides a variable with a magic name. Targets that do not
  1635. // link with compiler-rt may also provide such a variable.
  1636. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1637. const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
  1638. auto UnsafeStackPtr =
  1639. dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
  1640. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1641. if (!UnsafeStackPtr) {
  1642. auto TLSModel = UseTLS ?
  1643. GlobalValue::InitialExecTLSModel :
  1644. GlobalValue::NotThreadLocal;
  1645. // The global variable is not defined yet, define it ourselves.
  1646. // We use the initial-exec TLS model because we do not support the
  1647. // variable living anywhere other than in the main executable.
  1648. UnsafeStackPtr = new GlobalVariable(
  1649. *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
  1650. UnsafeStackPtrVar, nullptr, TLSModel);
  1651. } else {
  1652. // The variable exists, check its type and attributes.
  1653. if (UnsafeStackPtr->getValueType() != StackPtrTy)
  1654. report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
  1655. if (UseTLS != UnsafeStackPtr->isThreadLocal())
  1656. report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
  1657. (UseTLS ? "" : "not ") + "be thread-local");
  1658. }
  1659. return UnsafeStackPtr;
  1660. }
  1661. Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
  1662. if (!TM.getTargetTriple().isAndroid())
  1663. return getDefaultSafeStackPointerLocation(IRB, true);
  1664. // Android provides a libc function to retrieve the address of the current
  1665. // thread's unsafe stack pointer.
  1666. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1667. Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
  1668. Value *Fn = M->getOrInsertFunction("__safestack_pointer_address",
  1669. StackPtrTy->getPointerTo(0));
  1670. return IRB.CreateCall(Fn);
  1671. }
  1672. //===----------------------------------------------------------------------===//
  1673. // Loop Strength Reduction hooks
  1674. //===----------------------------------------------------------------------===//
  1675. /// isLegalAddressingMode - Return true if the addressing mode represented
  1676. /// by AM is legal for this target, for a load/store of the specified type.
  1677. bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
  1678. const AddrMode &AM, Type *Ty,
  1679. unsigned AS) const {
  1680. // The default implementation of this implements a conservative RISCy, r+r and
  1681. // r+i addr mode.
  1682. // Allows a sign-extended 16-bit immediate field.
  1683. if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
  1684. return false;
  1685. // No global is ever allowed as a base.
  1686. if (AM.BaseGV)
  1687. return false;
  1688. // Only support r+r,
  1689. switch (AM.Scale) {
  1690. case 0: // "r+i" or just "i", depending on HasBaseReg.
  1691. break;
  1692. case 1:
  1693. if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
  1694. return false;
  1695. // Otherwise we have r+r or r+i.
  1696. break;
  1697. case 2:
  1698. if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
  1699. return false;
  1700. // Allow 2*r as r+r.
  1701. break;
  1702. default: // Don't allow n * r
  1703. return false;
  1704. }
  1705. return true;
  1706. }
  1707. //===----------------------------------------------------------------------===//
  1708. // Stack Protector
  1709. //===----------------------------------------------------------------------===//
  1710. // For OpenBSD return its special guard variable. Otherwise return nullptr,
  1711. // so that SelectionDAG handle SSP.
  1712. Value *TargetLoweringBase::getIRStackGuard(IRBuilder<> &IRB) const {
  1713. if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
  1714. Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
  1715. PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
  1716. return M.getOrInsertGlobal("__guard_local", PtrTy);
  1717. }
  1718. return nullptr;
  1719. }
  1720. // Currently only support "standard" __stack_chk_guard.
  1721. // TODO: add LOAD_STACK_GUARD support.
  1722. void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
  1723. M.getOrInsertGlobal("__stack_chk_guard", Type::getInt8PtrTy(M.getContext()));
  1724. }
  1725. // Currently only support "standard" __stack_chk_guard.
  1726. // TODO: add LOAD_STACK_GUARD support.
  1727. Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
  1728. return M.getGlobalVariable("__stack_chk_guard", true);
  1729. }
  1730. Value *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
  1731. return nullptr;
  1732. }
  1733. unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
  1734. return MinimumJumpTableEntries;
  1735. }
  1736. void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
  1737. MinimumJumpTableEntries = Val;
  1738. }
  1739. unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
  1740. return MaximumJumpTableSize;
  1741. }
  1742. void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
  1743. MaximumJumpTableSize = Val;
  1744. }
  1745. //===----------------------------------------------------------------------===//
  1746. // Reciprocal Estimates
  1747. //===----------------------------------------------------------------------===//
  1748. /// Get the reciprocal estimate attribute string for a function that will
  1749. /// override the target defaults.
  1750. static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
  1751. const Function *F = MF.getFunction();
  1752. return F->getFnAttribute("reciprocal-estimates").getValueAsString();
  1753. }
  1754. /// Construct a string for the given reciprocal operation of the given type.
  1755. /// This string should match the corresponding option to the front-end's
  1756. /// "-mrecip" flag assuming those strings have been passed through in an
  1757. /// attribute string. For example, "vec-divf" for a division of a vXf32.
  1758. static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
  1759. std::string Name = VT.isVector() ? "vec-" : "";
  1760. Name += IsSqrt ? "sqrt" : "div";
  1761. // TODO: Handle "half" or other float types?
  1762. if (VT.getScalarType() == MVT::f64) {
  1763. Name += "d";
  1764. } else {
  1765. assert(VT.getScalarType() == MVT::f32 &&
  1766. "Unexpected FP type for reciprocal estimate");
  1767. Name += "f";
  1768. }
  1769. return Name;
  1770. }
  1771. /// Return the character position and value (a single numeric character) of a
  1772. /// customized refinement operation in the input string if it exists. Return
  1773. /// false if there is no customized refinement step count.
  1774. static bool parseRefinementStep(StringRef In, size_t &Position,
  1775. uint8_t &Value) {
  1776. const char RefStepToken = ':';
  1777. Position = In.find(RefStepToken);
  1778. if (Position == StringRef::npos)
  1779. return false;
  1780. StringRef RefStepString = In.substr(Position + 1);
  1781. // Allow exactly one numeric character for the additional refinement
  1782. // step parameter.
  1783. if (RefStepString.size() == 1) {
  1784. char RefStepChar = RefStepString[0];
  1785. if (RefStepChar >= '0' && RefStepChar <= '9') {
  1786. Value = RefStepChar - '0';
  1787. return true;
  1788. }
  1789. }
  1790. report_fatal_error("Invalid refinement step for -recip.");
  1791. }
  1792. /// For the input attribute string, return one of the ReciprocalEstimate enum
  1793. /// status values (enabled, disabled, or not specified) for this operation on
  1794. /// the specified data type.
  1795. static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
  1796. if (Override.empty())
  1797. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1798. SmallVector<StringRef, 4> OverrideVector;
  1799. SplitString(Override, OverrideVector, ",");
  1800. unsigned NumArgs = OverrideVector.size();
  1801. // Check if "all", "none", or "default" was specified.
  1802. if (NumArgs == 1) {
  1803. // Look for an optional setting of the number of refinement steps needed
  1804. // for this type of reciprocal operation.
  1805. size_t RefPos;
  1806. uint8_t RefSteps;
  1807. if (parseRefinementStep(Override, RefPos, RefSteps)) {
  1808. // Split the string for further processing.
  1809. Override = Override.substr(0, RefPos);
  1810. }
  1811. // All reciprocal types are enabled.
  1812. if (Override == "all")
  1813. return TargetLoweringBase::ReciprocalEstimate::Enabled;
  1814. // All reciprocal types are disabled.
  1815. if (Override == "none")
  1816. return TargetLoweringBase::ReciprocalEstimate::Disabled;
  1817. // Target defaults for enablement are used.
  1818. if (Override == "default")
  1819. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1820. }
  1821. // The attribute string may omit the size suffix ('f'/'d').
  1822. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1823. std::string VTNameNoSize = VTName;
  1824. VTNameNoSize.pop_back();
  1825. static const char DisabledPrefix = '!';
  1826. for (StringRef RecipType : OverrideVector) {
  1827. size_t RefPos;
  1828. uint8_t RefSteps;
  1829. if (parseRefinementStep(RecipType, RefPos, RefSteps))
  1830. RecipType = RecipType.substr(0, RefPos);
  1831. // Ignore the disablement token for string matching.
  1832. bool IsDisabled = RecipType[0] == DisabledPrefix;
  1833. if (IsDisabled)
  1834. RecipType = RecipType.substr(1);
  1835. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1836. return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
  1837. : TargetLoweringBase::ReciprocalEstimate::Enabled;
  1838. }
  1839. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1840. }
  1841. /// For the input attribute string, return the customized refinement step count
  1842. /// for this operation on the specified data type. If the step count does not
  1843. /// exist, return the ReciprocalEstimate enum value for unspecified.
  1844. static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
  1845. if (Override.empty())
  1846. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1847. SmallVector<StringRef, 4> OverrideVector;
  1848. SplitString(Override, OverrideVector, ",");
  1849. unsigned NumArgs = OverrideVector.size();
  1850. // Check if "all", "default", or "none" was specified.
  1851. if (NumArgs == 1) {
  1852. // Look for an optional setting of the number of refinement steps needed
  1853. // for this type of reciprocal operation.
  1854. size_t RefPos;
  1855. uint8_t RefSteps;
  1856. if (!parseRefinementStep(Override, RefPos, RefSteps))
  1857. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1858. // Split the string for further processing.
  1859. Override = Override.substr(0, RefPos);
  1860. assert(Override != "none" &&
  1861. "Disabled reciprocals, but specifed refinement steps?");
  1862. // If this is a general override, return the specified number of steps.
  1863. if (Override == "all" || Override == "default")
  1864. return RefSteps;
  1865. }
  1866. // The attribute string may omit the size suffix ('f'/'d').
  1867. std::string VTName = getReciprocalOpName(IsSqrt, VT);
  1868. std::string VTNameNoSize = VTName;
  1869. VTNameNoSize.pop_back();
  1870. for (StringRef RecipType : OverrideVector) {
  1871. size_t RefPos;
  1872. uint8_t RefSteps;
  1873. if (!parseRefinementStep(RecipType, RefPos, RefSteps))
  1874. continue;
  1875. RecipType = RecipType.substr(0, RefPos);
  1876. if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
  1877. return RefSteps;
  1878. }
  1879. return TargetLoweringBase::ReciprocalEstimate::Unspecified;
  1880. }
  1881. int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
  1882. MachineFunction &MF) const {
  1883. return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
  1884. }
  1885. int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
  1886. MachineFunction &MF) const {
  1887. return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
  1888. }
  1889. int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
  1890. MachineFunction &MF) const {
  1891. return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
  1892. }
  1893. int TargetLoweringBase::getDivRefinementSteps(EVT VT,
  1894. MachineFunction &MF) const {
  1895. return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
  1896. }