2
0

TargetLoweringBase.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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/Triple.h"
  17. #include "llvm/CodeGen/Analysis.h"
  18. #include "llvm/CodeGen/MachineFrameInfo.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  22. #include "llvm/CodeGen/StackMaps.h"
  23. #include "llvm/IR/DataLayout.h"
  24. #include "llvm/IR/DerivedTypes.h"
  25. #include "llvm/IR/GlobalVariable.h"
  26. #include "llvm/IR/Mangler.h"
  27. #include "llvm/MC/MCAsmInfo.h"
  28. #include "llvm/MC/MCContext.h"
  29. #include "llvm/MC/MCExpr.h"
  30. #include "llvm/Support/CommandLine.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Target/TargetLoweringObjectFile.h"
  34. #include "llvm/Target/TargetMachine.h"
  35. #include "llvm/Target/TargetRegisterInfo.h"
  36. #include "llvm/Target/TargetSubtargetInfo.h"
  37. #include <cctype>
  38. using namespace llvm;
  39. static cl::opt<bool> JumpIsExpensiveOverride(
  40. "jump-is-expensive", cl::init(false),
  41. cl::desc("Do not create extra branches to split comparison logic."),
  42. cl::Hidden);
  43. /// InitLibcallNames - Set default libcall names.
  44. ///
  45. static void InitLibcallNames(const char **Names, const Triple &TT) {
  46. Names[RTLIB::SHL_I16] = "__ashlhi3";
  47. Names[RTLIB::SHL_I32] = "__ashlsi3";
  48. Names[RTLIB::SHL_I64] = "__ashldi3";
  49. Names[RTLIB::SHL_I128] = "__ashlti3";
  50. Names[RTLIB::SRL_I16] = "__lshrhi3";
  51. Names[RTLIB::SRL_I32] = "__lshrsi3";
  52. Names[RTLIB::SRL_I64] = "__lshrdi3";
  53. Names[RTLIB::SRL_I128] = "__lshrti3";
  54. Names[RTLIB::SRA_I16] = "__ashrhi3";
  55. Names[RTLIB::SRA_I32] = "__ashrsi3";
  56. Names[RTLIB::SRA_I64] = "__ashrdi3";
  57. Names[RTLIB::SRA_I128] = "__ashrti3";
  58. Names[RTLIB::MUL_I8] = "__mulqi3";
  59. Names[RTLIB::MUL_I16] = "__mulhi3";
  60. Names[RTLIB::MUL_I32] = "__mulsi3";
  61. Names[RTLIB::MUL_I64] = "__muldi3";
  62. Names[RTLIB::MUL_I128] = "__multi3";
  63. Names[RTLIB::MULO_I32] = "__mulosi4";
  64. Names[RTLIB::MULO_I64] = "__mulodi4";
  65. Names[RTLIB::MULO_I128] = "__muloti4";
  66. Names[RTLIB::SDIV_I8] = "__divqi3";
  67. Names[RTLIB::SDIV_I16] = "__divhi3";
  68. Names[RTLIB::SDIV_I32] = "__divsi3";
  69. Names[RTLIB::SDIV_I64] = "__divdi3";
  70. Names[RTLIB::SDIV_I128] = "__divti3";
  71. Names[RTLIB::UDIV_I8] = "__udivqi3";
  72. Names[RTLIB::UDIV_I16] = "__udivhi3";
  73. Names[RTLIB::UDIV_I32] = "__udivsi3";
  74. Names[RTLIB::UDIV_I64] = "__udivdi3";
  75. Names[RTLIB::UDIV_I128] = "__udivti3";
  76. Names[RTLIB::SREM_I8] = "__modqi3";
  77. Names[RTLIB::SREM_I16] = "__modhi3";
  78. Names[RTLIB::SREM_I32] = "__modsi3";
  79. Names[RTLIB::SREM_I64] = "__moddi3";
  80. Names[RTLIB::SREM_I128] = "__modti3";
  81. Names[RTLIB::UREM_I8] = "__umodqi3";
  82. Names[RTLIB::UREM_I16] = "__umodhi3";
  83. Names[RTLIB::UREM_I32] = "__umodsi3";
  84. Names[RTLIB::UREM_I64] = "__umoddi3";
  85. Names[RTLIB::UREM_I128] = "__umodti3";
  86. // These are generally not available.
  87. Names[RTLIB::SDIVREM_I8] = nullptr;
  88. Names[RTLIB::SDIVREM_I16] = nullptr;
  89. Names[RTLIB::SDIVREM_I32] = nullptr;
  90. Names[RTLIB::SDIVREM_I64] = nullptr;
  91. Names[RTLIB::SDIVREM_I128] = nullptr;
  92. Names[RTLIB::UDIVREM_I8] = nullptr;
  93. Names[RTLIB::UDIVREM_I16] = nullptr;
  94. Names[RTLIB::UDIVREM_I32] = nullptr;
  95. Names[RTLIB::UDIVREM_I64] = nullptr;
  96. Names[RTLIB::UDIVREM_I128] = nullptr;
  97. Names[RTLIB::NEG_I32] = "__negsi2";
  98. Names[RTLIB::NEG_I64] = "__negdi2";
  99. Names[RTLIB::ADD_F32] = "__addsf3";
  100. Names[RTLIB::ADD_F64] = "__adddf3";
  101. Names[RTLIB::ADD_F80] = "__addxf3";
  102. Names[RTLIB::ADD_F128] = "__addtf3";
  103. Names[RTLIB::ADD_PPCF128] = "__gcc_qadd";
  104. Names[RTLIB::SUB_F32] = "__subsf3";
  105. Names[RTLIB::SUB_F64] = "__subdf3";
  106. Names[RTLIB::SUB_F80] = "__subxf3";
  107. Names[RTLIB::SUB_F128] = "__subtf3";
  108. Names[RTLIB::SUB_PPCF128] = "__gcc_qsub";
  109. Names[RTLIB::MUL_F32] = "__mulsf3";
  110. Names[RTLIB::MUL_F64] = "__muldf3";
  111. Names[RTLIB::MUL_F80] = "__mulxf3";
  112. Names[RTLIB::MUL_F128] = "__multf3";
  113. Names[RTLIB::MUL_PPCF128] = "__gcc_qmul";
  114. Names[RTLIB::DIV_F32] = "__divsf3";
  115. Names[RTLIB::DIV_F64] = "__divdf3";
  116. Names[RTLIB::DIV_F80] = "__divxf3";
  117. Names[RTLIB::DIV_F128] = "__divtf3";
  118. Names[RTLIB::DIV_PPCF128] = "__gcc_qdiv";
  119. Names[RTLIB::REM_F32] = "fmodf";
  120. Names[RTLIB::REM_F64] = "fmod";
  121. Names[RTLIB::REM_F80] = "fmodl";
  122. Names[RTLIB::REM_F128] = "fmodl";
  123. Names[RTLIB::REM_PPCF128] = "fmodl";
  124. Names[RTLIB::FMA_F32] = "fmaf";
  125. Names[RTLIB::FMA_F64] = "fma";
  126. Names[RTLIB::FMA_F80] = "fmal";
  127. Names[RTLIB::FMA_F128] = "fmal";
  128. Names[RTLIB::FMA_PPCF128] = "fmal";
  129. Names[RTLIB::POWI_F32] = "__powisf2";
  130. Names[RTLIB::POWI_F64] = "__powidf2";
  131. Names[RTLIB::POWI_F80] = "__powixf2";
  132. Names[RTLIB::POWI_F128] = "__powitf2";
  133. Names[RTLIB::POWI_PPCF128] = "__powitf2";
  134. Names[RTLIB::SQRT_F32] = "sqrtf";
  135. Names[RTLIB::SQRT_F64] = "sqrt";
  136. Names[RTLIB::SQRT_F80] = "sqrtl";
  137. Names[RTLIB::SQRT_F128] = "sqrtl";
  138. Names[RTLIB::SQRT_PPCF128] = "sqrtl";
  139. Names[RTLIB::LOG_F32] = "logf";
  140. Names[RTLIB::LOG_F64] = "log";
  141. Names[RTLIB::LOG_F80] = "logl";
  142. Names[RTLIB::LOG_F128] = "logl";
  143. Names[RTLIB::LOG_PPCF128] = "logl";
  144. Names[RTLIB::LOG2_F32] = "log2f";
  145. Names[RTLIB::LOG2_F64] = "log2";
  146. Names[RTLIB::LOG2_F80] = "log2l";
  147. Names[RTLIB::LOG2_F128] = "log2l";
  148. Names[RTLIB::LOG2_PPCF128] = "log2l";
  149. Names[RTLIB::LOG10_F32] = "log10f";
  150. Names[RTLIB::LOG10_F64] = "log10";
  151. Names[RTLIB::LOG10_F80] = "log10l";
  152. Names[RTLIB::LOG10_F128] = "log10l";
  153. Names[RTLIB::LOG10_PPCF128] = "log10l";
  154. Names[RTLIB::EXP_F32] = "expf";
  155. Names[RTLIB::EXP_F64] = "exp";
  156. Names[RTLIB::EXP_F80] = "expl";
  157. Names[RTLIB::EXP_F128] = "expl";
  158. Names[RTLIB::EXP_PPCF128] = "expl";
  159. Names[RTLIB::EXP2_F32] = "exp2f";
  160. Names[RTLIB::EXP2_F64] = "exp2";
  161. Names[RTLIB::EXP2_F80] = "exp2l";
  162. Names[RTLIB::EXP2_F128] = "exp2l";
  163. Names[RTLIB::EXP2_PPCF128] = "exp2l";
  164. Names[RTLIB::SIN_F32] = "sinf";
  165. Names[RTLIB::SIN_F64] = "sin";
  166. Names[RTLIB::SIN_F80] = "sinl";
  167. Names[RTLIB::SIN_F128] = "sinl";
  168. Names[RTLIB::SIN_PPCF128] = "sinl";
  169. Names[RTLIB::COS_F32] = "cosf";
  170. Names[RTLIB::COS_F64] = "cos";
  171. Names[RTLIB::COS_F80] = "cosl";
  172. Names[RTLIB::COS_F128] = "cosl";
  173. Names[RTLIB::COS_PPCF128] = "cosl";
  174. Names[RTLIB::POW_F32] = "powf";
  175. Names[RTLIB::POW_F64] = "pow";
  176. Names[RTLIB::POW_F80] = "powl";
  177. Names[RTLIB::POW_F128] = "powl";
  178. Names[RTLIB::POW_PPCF128] = "powl";
  179. Names[RTLIB::CEIL_F32] = "ceilf";
  180. Names[RTLIB::CEIL_F64] = "ceil";
  181. Names[RTLIB::CEIL_F80] = "ceill";
  182. Names[RTLIB::CEIL_F128] = "ceill";
  183. Names[RTLIB::CEIL_PPCF128] = "ceill";
  184. Names[RTLIB::TRUNC_F32] = "truncf";
  185. Names[RTLIB::TRUNC_F64] = "trunc";
  186. Names[RTLIB::TRUNC_F80] = "truncl";
  187. Names[RTLIB::TRUNC_F128] = "truncl";
  188. Names[RTLIB::TRUNC_PPCF128] = "truncl";
  189. Names[RTLIB::RINT_F32] = "rintf";
  190. Names[RTLIB::RINT_F64] = "rint";
  191. Names[RTLIB::RINT_F80] = "rintl";
  192. Names[RTLIB::RINT_F128] = "rintl";
  193. Names[RTLIB::RINT_PPCF128] = "rintl";
  194. Names[RTLIB::NEARBYINT_F32] = "nearbyintf";
  195. Names[RTLIB::NEARBYINT_F64] = "nearbyint";
  196. Names[RTLIB::NEARBYINT_F80] = "nearbyintl";
  197. Names[RTLIB::NEARBYINT_F128] = "nearbyintl";
  198. Names[RTLIB::NEARBYINT_PPCF128] = "nearbyintl";
  199. Names[RTLIB::ROUND_F32] = "roundf";
  200. Names[RTLIB::ROUND_F64] = "round";
  201. Names[RTLIB::ROUND_F80] = "roundl";
  202. Names[RTLIB::ROUND_F128] = "roundl";
  203. Names[RTLIB::ROUND_PPCF128] = "roundl";
  204. Names[RTLIB::FLOOR_F32] = "floorf";
  205. Names[RTLIB::FLOOR_F64] = "floor";
  206. Names[RTLIB::FLOOR_F80] = "floorl";
  207. Names[RTLIB::FLOOR_F128] = "floorl";
  208. Names[RTLIB::FLOOR_PPCF128] = "floorl";
  209. Names[RTLIB::FMIN_F32] = "fminf";
  210. Names[RTLIB::FMIN_F64] = "fmin";
  211. Names[RTLIB::FMIN_F80] = "fminl";
  212. Names[RTLIB::FMIN_F128] = "fminl";
  213. Names[RTLIB::FMIN_PPCF128] = "fminl";
  214. Names[RTLIB::FMAX_F32] = "fmaxf";
  215. Names[RTLIB::FMAX_F64] = "fmax";
  216. Names[RTLIB::FMAX_F80] = "fmaxl";
  217. Names[RTLIB::FMAX_F128] = "fmaxl";
  218. Names[RTLIB::FMAX_PPCF128] = "fmaxl";
  219. Names[RTLIB::ROUND_F32] = "roundf";
  220. Names[RTLIB::ROUND_F64] = "round";
  221. Names[RTLIB::ROUND_F80] = "roundl";
  222. Names[RTLIB::ROUND_F128] = "roundl";
  223. Names[RTLIB::ROUND_PPCF128] = "roundl";
  224. Names[RTLIB::COPYSIGN_F32] = "copysignf";
  225. Names[RTLIB::COPYSIGN_F64] = "copysign";
  226. Names[RTLIB::COPYSIGN_F80] = "copysignl";
  227. Names[RTLIB::COPYSIGN_F128] = "copysignl";
  228. Names[RTLIB::COPYSIGN_PPCF128] = "copysignl";
  229. Names[RTLIB::FPEXT_F64_F128] = "__extenddftf2";
  230. Names[RTLIB::FPEXT_F32_F128] = "__extendsftf2";
  231. Names[RTLIB::FPEXT_F32_F64] = "__extendsfdf2";
  232. Names[RTLIB::FPEXT_F16_F32] = "__gnu_h2f_ieee";
  233. Names[RTLIB::FPROUND_F32_F16] = "__gnu_f2h_ieee";
  234. Names[RTLIB::FPROUND_F64_F16] = "__truncdfhf2";
  235. Names[RTLIB::FPROUND_F80_F16] = "__truncxfhf2";
  236. Names[RTLIB::FPROUND_F128_F16] = "__trunctfhf2";
  237. Names[RTLIB::FPROUND_PPCF128_F16] = "__trunctfhf2";
  238. Names[RTLIB::FPROUND_F64_F32] = "__truncdfsf2";
  239. Names[RTLIB::FPROUND_F80_F32] = "__truncxfsf2";
  240. Names[RTLIB::FPROUND_F128_F32] = "__trunctfsf2";
  241. Names[RTLIB::FPROUND_PPCF128_F32] = "__trunctfsf2";
  242. Names[RTLIB::FPROUND_F80_F64] = "__truncxfdf2";
  243. Names[RTLIB::FPROUND_F128_F64] = "__trunctfdf2";
  244. Names[RTLIB::FPROUND_PPCF128_F64] = "__trunctfdf2";
  245. Names[RTLIB::FPTOSINT_F32_I8] = "__fixsfqi";
  246. Names[RTLIB::FPTOSINT_F32_I16] = "__fixsfhi";
  247. Names[RTLIB::FPTOSINT_F32_I32] = "__fixsfsi";
  248. Names[RTLIB::FPTOSINT_F32_I64] = "__fixsfdi";
  249. Names[RTLIB::FPTOSINT_F32_I128] = "__fixsfti";
  250. Names[RTLIB::FPTOSINT_F64_I8] = "__fixdfqi";
  251. Names[RTLIB::FPTOSINT_F64_I16] = "__fixdfhi";
  252. Names[RTLIB::FPTOSINT_F64_I32] = "__fixdfsi";
  253. Names[RTLIB::FPTOSINT_F64_I64] = "__fixdfdi";
  254. Names[RTLIB::FPTOSINT_F64_I128] = "__fixdfti";
  255. Names[RTLIB::FPTOSINT_F80_I32] = "__fixxfsi";
  256. Names[RTLIB::FPTOSINT_F80_I64] = "__fixxfdi";
  257. Names[RTLIB::FPTOSINT_F80_I128] = "__fixxfti";
  258. Names[RTLIB::FPTOSINT_F128_I32] = "__fixtfsi";
  259. Names[RTLIB::FPTOSINT_F128_I64] = "__fixtfdi";
  260. Names[RTLIB::FPTOSINT_F128_I128] = "__fixtfti";
  261. Names[RTLIB::FPTOSINT_PPCF128_I32] = "__fixtfsi";
  262. Names[RTLIB::FPTOSINT_PPCF128_I64] = "__fixtfdi";
  263. Names[RTLIB::FPTOSINT_PPCF128_I128] = "__fixtfti";
  264. Names[RTLIB::FPTOUINT_F32_I8] = "__fixunssfqi";
  265. Names[RTLIB::FPTOUINT_F32_I16] = "__fixunssfhi";
  266. Names[RTLIB::FPTOUINT_F32_I32] = "__fixunssfsi";
  267. Names[RTLIB::FPTOUINT_F32_I64] = "__fixunssfdi";
  268. Names[RTLIB::FPTOUINT_F32_I128] = "__fixunssfti";
  269. Names[RTLIB::FPTOUINT_F64_I8] = "__fixunsdfqi";
  270. Names[RTLIB::FPTOUINT_F64_I16] = "__fixunsdfhi";
  271. Names[RTLIB::FPTOUINT_F64_I32] = "__fixunsdfsi";
  272. Names[RTLIB::FPTOUINT_F64_I64] = "__fixunsdfdi";
  273. Names[RTLIB::FPTOUINT_F64_I128] = "__fixunsdfti";
  274. Names[RTLIB::FPTOUINT_F80_I32] = "__fixunsxfsi";
  275. Names[RTLIB::FPTOUINT_F80_I64] = "__fixunsxfdi";
  276. Names[RTLIB::FPTOUINT_F80_I128] = "__fixunsxfti";
  277. Names[RTLIB::FPTOUINT_F128_I32] = "__fixunstfsi";
  278. Names[RTLIB::FPTOUINT_F128_I64] = "__fixunstfdi";
  279. Names[RTLIB::FPTOUINT_F128_I128] = "__fixunstfti";
  280. Names[RTLIB::FPTOUINT_PPCF128_I32] = "__fixunstfsi";
  281. Names[RTLIB::FPTOUINT_PPCF128_I64] = "__fixunstfdi";
  282. Names[RTLIB::FPTOUINT_PPCF128_I128] = "__fixunstfti";
  283. Names[RTLIB::SINTTOFP_I32_F32] = "__floatsisf";
  284. Names[RTLIB::SINTTOFP_I32_F64] = "__floatsidf";
  285. Names[RTLIB::SINTTOFP_I32_F80] = "__floatsixf";
  286. Names[RTLIB::SINTTOFP_I32_F128] = "__floatsitf";
  287. Names[RTLIB::SINTTOFP_I32_PPCF128] = "__floatsitf";
  288. Names[RTLIB::SINTTOFP_I64_F32] = "__floatdisf";
  289. Names[RTLIB::SINTTOFP_I64_F64] = "__floatdidf";
  290. Names[RTLIB::SINTTOFP_I64_F80] = "__floatdixf";
  291. Names[RTLIB::SINTTOFP_I64_F128] = "__floatditf";
  292. Names[RTLIB::SINTTOFP_I64_PPCF128] = "__floatditf";
  293. Names[RTLIB::SINTTOFP_I128_F32] = "__floattisf";
  294. Names[RTLIB::SINTTOFP_I128_F64] = "__floattidf";
  295. Names[RTLIB::SINTTOFP_I128_F80] = "__floattixf";
  296. Names[RTLIB::SINTTOFP_I128_F128] = "__floattitf";
  297. Names[RTLIB::SINTTOFP_I128_PPCF128] = "__floattitf";
  298. Names[RTLIB::UINTTOFP_I32_F32] = "__floatunsisf";
  299. Names[RTLIB::UINTTOFP_I32_F64] = "__floatunsidf";
  300. Names[RTLIB::UINTTOFP_I32_F80] = "__floatunsixf";
  301. Names[RTLIB::UINTTOFP_I32_F128] = "__floatunsitf";
  302. Names[RTLIB::UINTTOFP_I32_PPCF128] = "__floatunsitf";
  303. Names[RTLIB::UINTTOFP_I64_F32] = "__floatundisf";
  304. Names[RTLIB::UINTTOFP_I64_F64] = "__floatundidf";
  305. Names[RTLIB::UINTTOFP_I64_F80] = "__floatundixf";
  306. Names[RTLIB::UINTTOFP_I64_F128] = "__floatunditf";
  307. Names[RTLIB::UINTTOFP_I64_PPCF128] = "__floatunditf";
  308. Names[RTLIB::UINTTOFP_I128_F32] = "__floatuntisf";
  309. Names[RTLIB::UINTTOFP_I128_F64] = "__floatuntidf";
  310. Names[RTLIB::UINTTOFP_I128_F80] = "__floatuntixf";
  311. Names[RTLIB::UINTTOFP_I128_F128] = "__floatuntitf";
  312. Names[RTLIB::UINTTOFP_I128_PPCF128] = "__floatuntitf";
  313. Names[RTLIB::OEQ_F32] = "__eqsf2";
  314. Names[RTLIB::OEQ_F64] = "__eqdf2";
  315. Names[RTLIB::OEQ_F128] = "__eqtf2";
  316. Names[RTLIB::UNE_F32] = "__nesf2";
  317. Names[RTLIB::UNE_F64] = "__nedf2";
  318. Names[RTLIB::UNE_F128] = "__netf2";
  319. Names[RTLIB::OGE_F32] = "__gesf2";
  320. Names[RTLIB::OGE_F64] = "__gedf2";
  321. Names[RTLIB::OGE_F128] = "__getf2";
  322. Names[RTLIB::OLT_F32] = "__ltsf2";
  323. Names[RTLIB::OLT_F64] = "__ltdf2";
  324. Names[RTLIB::OLT_F128] = "__lttf2";
  325. Names[RTLIB::OLE_F32] = "__lesf2";
  326. Names[RTLIB::OLE_F64] = "__ledf2";
  327. Names[RTLIB::OLE_F128] = "__letf2";
  328. Names[RTLIB::OGT_F32] = "__gtsf2";
  329. Names[RTLIB::OGT_F64] = "__gtdf2";
  330. Names[RTLIB::OGT_F128] = "__gttf2";
  331. Names[RTLIB::UO_F32] = "__unordsf2";
  332. Names[RTLIB::UO_F64] = "__unorddf2";
  333. Names[RTLIB::UO_F128] = "__unordtf2";
  334. Names[RTLIB::O_F32] = "__unordsf2";
  335. Names[RTLIB::O_F64] = "__unorddf2";
  336. Names[RTLIB::O_F128] = "__unordtf2";
  337. Names[RTLIB::MEMCPY] = "memcpy";
  338. Names[RTLIB::MEMMOVE] = "memmove";
  339. Names[RTLIB::MEMSET] = "memset";
  340. Names[RTLIB::UNWIND_RESUME] = "_Unwind_Resume";
  341. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1";
  342. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2";
  343. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4";
  344. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8";
  345. Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16] = "__sync_val_compare_and_swap_16";
  346. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_1] = "__sync_lock_test_and_set_1";
  347. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_2] = "__sync_lock_test_and_set_2";
  348. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_4] = "__sync_lock_test_and_set_4";
  349. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_8] = "__sync_lock_test_and_set_8";
  350. Names[RTLIB::SYNC_LOCK_TEST_AND_SET_16] = "__sync_lock_test_and_set_16";
  351. Names[RTLIB::SYNC_FETCH_AND_ADD_1] = "__sync_fetch_and_add_1";
  352. Names[RTLIB::SYNC_FETCH_AND_ADD_2] = "__sync_fetch_and_add_2";
  353. Names[RTLIB::SYNC_FETCH_AND_ADD_4] = "__sync_fetch_and_add_4";
  354. Names[RTLIB::SYNC_FETCH_AND_ADD_8] = "__sync_fetch_and_add_8";
  355. Names[RTLIB::SYNC_FETCH_AND_ADD_16] = "__sync_fetch_and_add_16";
  356. Names[RTLIB::SYNC_FETCH_AND_SUB_1] = "__sync_fetch_and_sub_1";
  357. Names[RTLIB::SYNC_FETCH_AND_SUB_2] = "__sync_fetch_and_sub_2";
  358. Names[RTLIB::SYNC_FETCH_AND_SUB_4] = "__sync_fetch_and_sub_4";
  359. Names[RTLIB::SYNC_FETCH_AND_SUB_8] = "__sync_fetch_and_sub_8";
  360. Names[RTLIB::SYNC_FETCH_AND_SUB_16] = "__sync_fetch_and_sub_16";
  361. Names[RTLIB::SYNC_FETCH_AND_AND_1] = "__sync_fetch_and_and_1";
  362. Names[RTLIB::SYNC_FETCH_AND_AND_2] = "__sync_fetch_and_and_2";
  363. Names[RTLIB::SYNC_FETCH_AND_AND_4] = "__sync_fetch_and_and_4";
  364. Names[RTLIB::SYNC_FETCH_AND_AND_8] = "__sync_fetch_and_and_8";
  365. Names[RTLIB::SYNC_FETCH_AND_AND_16] = "__sync_fetch_and_and_16";
  366. Names[RTLIB::SYNC_FETCH_AND_OR_1] = "__sync_fetch_and_or_1";
  367. Names[RTLIB::SYNC_FETCH_AND_OR_2] = "__sync_fetch_and_or_2";
  368. Names[RTLIB::SYNC_FETCH_AND_OR_4] = "__sync_fetch_and_or_4";
  369. Names[RTLIB::SYNC_FETCH_AND_OR_8] = "__sync_fetch_and_or_8";
  370. Names[RTLIB::SYNC_FETCH_AND_OR_16] = "__sync_fetch_and_or_16";
  371. Names[RTLIB::SYNC_FETCH_AND_XOR_1] = "__sync_fetch_and_xor_1";
  372. Names[RTLIB::SYNC_FETCH_AND_XOR_2] = "__sync_fetch_and_xor_2";
  373. Names[RTLIB::SYNC_FETCH_AND_XOR_4] = "__sync_fetch_and_xor_4";
  374. Names[RTLIB::SYNC_FETCH_AND_XOR_8] = "__sync_fetch_and_xor_8";
  375. Names[RTLIB::SYNC_FETCH_AND_XOR_16] = "__sync_fetch_and_xor_16";
  376. Names[RTLIB::SYNC_FETCH_AND_NAND_1] = "__sync_fetch_and_nand_1";
  377. Names[RTLIB::SYNC_FETCH_AND_NAND_2] = "__sync_fetch_and_nand_2";
  378. Names[RTLIB::SYNC_FETCH_AND_NAND_4] = "__sync_fetch_and_nand_4";
  379. Names[RTLIB::SYNC_FETCH_AND_NAND_8] = "__sync_fetch_and_nand_8";
  380. Names[RTLIB::SYNC_FETCH_AND_NAND_16] = "__sync_fetch_and_nand_16";
  381. Names[RTLIB::SYNC_FETCH_AND_MAX_1] = "__sync_fetch_and_max_1";
  382. Names[RTLIB::SYNC_FETCH_AND_MAX_2] = "__sync_fetch_and_max_2";
  383. Names[RTLIB::SYNC_FETCH_AND_MAX_4] = "__sync_fetch_and_max_4";
  384. Names[RTLIB::SYNC_FETCH_AND_MAX_8] = "__sync_fetch_and_max_8";
  385. Names[RTLIB::SYNC_FETCH_AND_MAX_16] = "__sync_fetch_and_max_16";
  386. Names[RTLIB::SYNC_FETCH_AND_UMAX_1] = "__sync_fetch_and_umax_1";
  387. Names[RTLIB::SYNC_FETCH_AND_UMAX_2] = "__sync_fetch_and_umax_2";
  388. Names[RTLIB::SYNC_FETCH_AND_UMAX_4] = "__sync_fetch_and_umax_4";
  389. Names[RTLIB::SYNC_FETCH_AND_UMAX_8] = "__sync_fetch_and_umax_8";
  390. Names[RTLIB::SYNC_FETCH_AND_UMAX_16] = "__sync_fetch_and_umax_16";
  391. Names[RTLIB::SYNC_FETCH_AND_MIN_1] = "__sync_fetch_and_min_1";
  392. Names[RTLIB::SYNC_FETCH_AND_MIN_2] = "__sync_fetch_and_min_2";
  393. Names[RTLIB::SYNC_FETCH_AND_MIN_4] = "__sync_fetch_and_min_4";
  394. Names[RTLIB::SYNC_FETCH_AND_MIN_8] = "__sync_fetch_and_min_8";
  395. Names[RTLIB::SYNC_FETCH_AND_MIN_16] = "__sync_fetch_and_min_16";
  396. Names[RTLIB::SYNC_FETCH_AND_UMIN_1] = "__sync_fetch_and_umin_1";
  397. Names[RTLIB::SYNC_FETCH_AND_UMIN_2] = "__sync_fetch_and_umin_2";
  398. Names[RTLIB::SYNC_FETCH_AND_UMIN_4] = "__sync_fetch_and_umin_4";
  399. Names[RTLIB::SYNC_FETCH_AND_UMIN_8] = "__sync_fetch_and_umin_8";
  400. Names[RTLIB::SYNC_FETCH_AND_UMIN_16] = "__sync_fetch_and_umin_16";
  401. if (TT.getEnvironment() == Triple::GNU) {
  402. Names[RTLIB::SINCOS_F32] = "sincosf";
  403. Names[RTLIB::SINCOS_F64] = "sincos";
  404. Names[RTLIB::SINCOS_F80] = "sincosl";
  405. Names[RTLIB::SINCOS_F128] = "sincosl";
  406. Names[RTLIB::SINCOS_PPCF128] = "sincosl";
  407. } else {
  408. // These are generally not available.
  409. Names[RTLIB::SINCOS_F32] = nullptr;
  410. Names[RTLIB::SINCOS_F64] = nullptr;
  411. Names[RTLIB::SINCOS_F80] = nullptr;
  412. Names[RTLIB::SINCOS_F128] = nullptr;
  413. Names[RTLIB::SINCOS_PPCF128] = nullptr;
  414. }
  415. if (!TT.isOSOpenBSD()) {
  416. Names[RTLIB::STACKPROTECTOR_CHECK_FAIL] = "__stack_chk_fail";
  417. } else {
  418. // These are generally not available.
  419. Names[RTLIB::STACKPROTECTOR_CHECK_FAIL] = nullptr;
  420. }
  421. // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
  422. // of the gnueabi-style __gnu_*_ieee.
  423. // FIXME: What about other targets?
  424. if (TT.isOSDarwin()) {
  425. Names[RTLIB::FPEXT_F16_F32] = "__extendhfsf2";
  426. Names[RTLIB::FPROUND_F32_F16] = "__truncsfhf2";
  427. }
  428. }
  429. /// InitLibcallCallingConvs - Set default libcall CallingConvs.
  430. ///
  431. static void InitLibcallCallingConvs(CallingConv::ID *CCs) {
  432. for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i) {
  433. CCs[i] = CallingConv::C;
  434. }
  435. }
  436. /// getFPEXT - Return the FPEXT_*_* value for the given types, or
  437. /// UNKNOWN_LIBCALL if there is none.
  438. RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
  439. if (OpVT == MVT::f16) {
  440. if (RetVT == MVT::f32)
  441. return FPEXT_F16_F32;
  442. } else if (OpVT == MVT::f32) {
  443. if (RetVT == MVT::f64)
  444. return FPEXT_F32_F64;
  445. if (RetVT == MVT::f128)
  446. return FPEXT_F32_F128;
  447. } else if (OpVT == MVT::f64) {
  448. if (RetVT == MVT::f128)
  449. return FPEXT_F64_F128;
  450. }
  451. return UNKNOWN_LIBCALL;
  452. }
  453. /// getFPROUND - Return the FPROUND_*_* value for the given types, or
  454. /// UNKNOWN_LIBCALL if there is none.
  455. RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
  456. if (RetVT == MVT::f16) {
  457. if (OpVT == MVT::f32)
  458. return FPROUND_F32_F16;
  459. if (OpVT == MVT::f64)
  460. return FPROUND_F64_F16;
  461. if (OpVT == MVT::f80)
  462. return FPROUND_F80_F16;
  463. if (OpVT == MVT::f128)
  464. return FPROUND_F128_F16;
  465. if (OpVT == MVT::ppcf128)
  466. return FPROUND_PPCF128_F16;
  467. } else if (RetVT == MVT::f32) {
  468. if (OpVT == MVT::f64)
  469. return FPROUND_F64_F32;
  470. if (OpVT == MVT::f80)
  471. return FPROUND_F80_F32;
  472. if (OpVT == MVT::f128)
  473. return FPROUND_F128_F32;
  474. if (OpVT == MVT::ppcf128)
  475. return FPROUND_PPCF128_F32;
  476. } else if (RetVT == MVT::f64) {
  477. if (OpVT == MVT::f80)
  478. return FPROUND_F80_F64;
  479. if (OpVT == MVT::f128)
  480. return FPROUND_F128_F64;
  481. if (OpVT == MVT::ppcf128)
  482. return FPROUND_PPCF128_F64;
  483. }
  484. return UNKNOWN_LIBCALL;
  485. }
  486. /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
  487. /// UNKNOWN_LIBCALL if there is none.
  488. RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
  489. if (OpVT == MVT::f32) {
  490. if (RetVT == MVT::i8)
  491. return FPTOSINT_F32_I8;
  492. if (RetVT == MVT::i16)
  493. return FPTOSINT_F32_I16;
  494. if (RetVT == MVT::i32)
  495. return FPTOSINT_F32_I32;
  496. if (RetVT == MVT::i64)
  497. return FPTOSINT_F32_I64;
  498. if (RetVT == MVT::i128)
  499. return FPTOSINT_F32_I128;
  500. } else if (OpVT == MVT::f64) {
  501. if (RetVT == MVT::i8)
  502. return FPTOSINT_F64_I8;
  503. if (RetVT == MVT::i16)
  504. return FPTOSINT_F64_I16;
  505. if (RetVT == MVT::i32)
  506. return FPTOSINT_F64_I32;
  507. if (RetVT == MVT::i64)
  508. return FPTOSINT_F64_I64;
  509. if (RetVT == MVT::i128)
  510. return FPTOSINT_F64_I128;
  511. } else if (OpVT == MVT::f80) {
  512. if (RetVT == MVT::i32)
  513. return FPTOSINT_F80_I32;
  514. if (RetVT == MVT::i64)
  515. return FPTOSINT_F80_I64;
  516. if (RetVT == MVT::i128)
  517. return FPTOSINT_F80_I128;
  518. } else if (OpVT == MVT::f128) {
  519. if (RetVT == MVT::i32)
  520. return FPTOSINT_F128_I32;
  521. if (RetVT == MVT::i64)
  522. return FPTOSINT_F128_I64;
  523. if (RetVT == MVT::i128)
  524. return FPTOSINT_F128_I128;
  525. } else if (OpVT == MVT::ppcf128) {
  526. if (RetVT == MVT::i32)
  527. return FPTOSINT_PPCF128_I32;
  528. if (RetVT == MVT::i64)
  529. return FPTOSINT_PPCF128_I64;
  530. if (RetVT == MVT::i128)
  531. return FPTOSINT_PPCF128_I128;
  532. }
  533. return UNKNOWN_LIBCALL;
  534. }
  535. /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
  536. /// UNKNOWN_LIBCALL if there is none.
  537. RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
  538. if (OpVT == MVT::f32) {
  539. if (RetVT == MVT::i8)
  540. return FPTOUINT_F32_I8;
  541. if (RetVT == MVT::i16)
  542. return FPTOUINT_F32_I16;
  543. if (RetVT == MVT::i32)
  544. return FPTOUINT_F32_I32;
  545. if (RetVT == MVT::i64)
  546. return FPTOUINT_F32_I64;
  547. if (RetVT == MVT::i128)
  548. return FPTOUINT_F32_I128;
  549. } else if (OpVT == MVT::f64) {
  550. if (RetVT == MVT::i8)
  551. return FPTOUINT_F64_I8;
  552. if (RetVT == MVT::i16)
  553. return FPTOUINT_F64_I16;
  554. if (RetVT == MVT::i32)
  555. return FPTOUINT_F64_I32;
  556. if (RetVT == MVT::i64)
  557. return FPTOUINT_F64_I64;
  558. if (RetVT == MVT::i128)
  559. return FPTOUINT_F64_I128;
  560. } else if (OpVT == MVT::f80) {
  561. if (RetVT == MVT::i32)
  562. return FPTOUINT_F80_I32;
  563. if (RetVT == MVT::i64)
  564. return FPTOUINT_F80_I64;
  565. if (RetVT == MVT::i128)
  566. return FPTOUINT_F80_I128;
  567. } else if (OpVT == MVT::f128) {
  568. if (RetVT == MVT::i32)
  569. return FPTOUINT_F128_I32;
  570. if (RetVT == MVT::i64)
  571. return FPTOUINT_F128_I64;
  572. if (RetVT == MVT::i128)
  573. return FPTOUINT_F128_I128;
  574. } else if (OpVT == MVT::ppcf128) {
  575. if (RetVT == MVT::i32)
  576. return FPTOUINT_PPCF128_I32;
  577. if (RetVT == MVT::i64)
  578. return FPTOUINT_PPCF128_I64;
  579. if (RetVT == MVT::i128)
  580. return FPTOUINT_PPCF128_I128;
  581. }
  582. return UNKNOWN_LIBCALL;
  583. }
  584. /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
  585. /// UNKNOWN_LIBCALL if there is none.
  586. RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
  587. if (OpVT == MVT::i32) {
  588. if (RetVT == MVT::f32)
  589. return SINTTOFP_I32_F32;
  590. if (RetVT == MVT::f64)
  591. return SINTTOFP_I32_F64;
  592. if (RetVT == MVT::f80)
  593. return SINTTOFP_I32_F80;
  594. if (RetVT == MVT::f128)
  595. return SINTTOFP_I32_F128;
  596. if (RetVT == MVT::ppcf128)
  597. return SINTTOFP_I32_PPCF128;
  598. } else if (OpVT == MVT::i64) {
  599. if (RetVT == MVT::f32)
  600. return SINTTOFP_I64_F32;
  601. if (RetVT == MVT::f64)
  602. return SINTTOFP_I64_F64;
  603. if (RetVT == MVT::f80)
  604. return SINTTOFP_I64_F80;
  605. if (RetVT == MVT::f128)
  606. return SINTTOFP_I64_F128;
  607. if (RetVT == MVT::ppcf128)
  608. return SINTTOFP_I64_PPCF128;
  609. } else if (OpVT == MVT::i128) {
  610. if (RetVT == MVT::f32)
  611. return SINTTOFP_I128_F32;
  612. if (RetVT == MVT::f64)
  613. return SINTTOFP_I128_F64;
  614. if (RetVT == MVT::f80)
  615. return SINTTOFP_I128_F80;
  616. if (RetVT == MVT::f128)
  617. return SINTTOFP_I128_F128;
  618. if (RetVT == MVT::ppcf128)
  619. return SINTTOFP_I128_PPCF128;
  620. }
  621. return UNKNOWN_LIBCALL;
  622. }
  623. /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
  624. /// UNKNOWN_LIBCALL if there is none.
  625. RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
  626. if (OpVT == MVT::i32) {
  627. if (RetVT == MVT::f32)
  628. return UINTTOFP_I32_F32;
  629. if (RetVT == MVT::f64)
  630. return UINTTOFP_I32_F64;
  631. if (RetVT == MVT::f80)
  632. return UINTTOFP_I32_F80;
  633. if (RetVT == MVT::f128)
  634. return UINTTOFP_I32_F128;
  635. if (RetVT == MVT::ppcf128)
  636. return UINTTOFP_I32_PPCF128;
  637. } else if (OpVT == MVT::i64) {
  638. if (RetVT == MVT::f32)
  639. return UINTTOFP_I64_F32;
  640. if (RetVT == MVT::f64)
  641. return UINTTOFP_I64_F64;
  642. if (RetVT == MVT::f80)
  643. return UINTTOFP_I64_F80;
  644. if (RetVT == MVT::f128)
  645. return UINTTOFP_I64_F128;
  646. if (RetVT == MVT::ppcf128)
  647. return UINTTOFP_I64_PPCF128;
  648. } else if (OpVT == MVT::i128) {
  649. if (RetVT == MVT::f32)
  650. return UINTTOFP_I128_F32;
  651. if (RetVT == MVT::f64)
  652. return UINTTOFP_I128_F64;
  653. if (RetVT == MVT::f80)
  654. return UINTTOFP_I128_F80;
  655. if (RetVT == MVT::f128)
  656. return UINTTOFP_I128_F128;
  657. if (RetVT == MVT::ppcf128)
  658. return UINTTOFP_I128_PPCF128;
  659. }
  660. return UNKNOWN_LIBCALL;
  661. }
  662. RTLIB::Libcall RTLIB::getATOMIC(unsigned Opc, MVT VT) {
  663. #define OP_TO_LIBCALL(Name, Enum) \
  664. case Name: \
  665. switch (VT.SimpleTy) { \
  666. default: \
  667. return UNKNOWN_LIBCALL; \
  668. case MVT::i8: \
  669. return Enum##_1; \
  670. case MVT::i16: \
  671. return Enum##_2; \
  672. case MVT::i32: \
  673. return Enum##_4; \
  674. case MVT::i64: \
  675. return Enum##_8; \
  676. case MVT::i128: \
  677. return Enum##_16; \
  678. }
  679. switch (Opc) {
  680. OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
  681. OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
  682. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
  683. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
  684. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
  685. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
  686. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
  687. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
  688. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
  689. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
  690. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
  691. OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
  692. }
  693. #undef OP_TO_LIBCALL
  694. return UNKNOWN_LIBCALL;
  695. }
  696. /// InitCmpLibcallCCs - Set default comparison libcall CC.
  697. ///
  698. static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
  699. memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
  700. CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
  701. CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
  702. CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
  703. CCs[RTLIB::UNE_F32] = ISD::SETNE;
  704. CCs[RTLIB::UNE_F64] = ISD::SETNE;
  705. CCs[RTLIB::UNE_F128] = ISD::SETNE;
  706. CCs[RTLIB::OGE_F32] = ISD::SETGE;
  707. CCs[RTLIB::OGE_F64] = ISD::SETGE;
  708. CCs[RTLIB::OGE_F128] = ISD::SETGE;
  709. CCs[RTLIB::OLT_F32] = ISD::SETLT;
  710. CCs[RTLIB::OLT_F64] = ISD::SETLT;
  711. CCs[RTLIB::OLT_F128] = ISD::SETLT;
  712. CCs[RTLIB::OLE_F32] = ISD::SETLE;
  713. CCs[RTLIB::OLE_F64] = ISD::SETLE;
  714. CCs[RTLIB::OLE_F128] = ISD::SETLE;
  715. CCs[RTLIB::OGT_F32] = ISD::SETGT;
  716. CCs[RTLIB::OGT_F64] = ISD::SETGT;
  717. CCs[RTLIB::OGT_F128] = ISD::SETGT;
  718. CCs[RTLIB::UO_F32] = ISD::SETNE;
  719. CCs[RTLIB::UO_F64] = ISD::SETNE;
  720. CCs[RTLIB::UO_F128] = ISD::SETNE;
  721. CCs[RTLIB::O_F32] = ISD::SETEQ;
  722. CCs[RTLIB::O_F64] = ISD::SETEQ;
  723. CCs[RTLIB::O_F128] = ISD::SETEQ;
  724. }
  725. /// NOTE: The TargetMachine owns TLOF.
  726. TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
  727. initActions();
  728. // Perform these initializations only once.
  729. MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove = 8;
  730. MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize
  731. = MaxStoresPerMemmoveOptSize = 4;
  732. UseUnderscoreSetJmp = false;
  733. UseUnderscoreLongJmp = false;
  734. SelectIsExpensive = false;
  735. HasMultipleConditionRegisters = false;
  736. HasExtractBitsInsn = false;
  737. IntDivIsCheap = false;
  738. FsqrtIsCheap = false;
  739. Pow2SDivIsCheap = false;
  740. JumpIsExpensive = JumpIsExpensiveOverride;
  741. PredictableSelectIsExpensive = false;
  742. MaskAndBranchFoldingIsLegal = false;
  743. EnableExtLdPromotion = false;
  744. HasFloatingPointExceptions = true;
  745. StackPointerRegisterToSaveRestore = 0;
  746. ExceptionPointerRegister = 0;
  747. ExceptionSelectorRegister = 0;
  748. BooleanContents = UndefinedBooleanContent;
  749. BooleanFloatContents = UndefinedBooleanContent;
  750. BooleanVectorContents = UndefinedBooleanContent;
  751. SchedPreferenceInfo = Sched::ILP;
  752. JumpBufSize = 0;
  753. JumpBufAlignment = 0;
  754. MinFunctionAlignment = 0;
  755. PrefFunctionAlignment = 0;
  756. PrefLoopAlignment = 0;
  757. MinStackArgumentAlignment = 1;
  758. InsertFencesForAtomic = false;
  759. MinimumJumpTableEntries = 4;
  760. InitLibcallNames(LibcallRoutineNames, TM.getTargetTriple());
  761. InitCmpLibcallCCs(CmpLibcallCCs);
  762. InitLibcallCallingConvs(LibcallCallingConvs);
  763. }
  764. void TargetLoweringBase::initActions() {
  765. // All operations default to being supported.
  766. memset(OpActions, 0, sizeof(OpActions));
  767. memset(LoadExtActions, 0, sizeof(LoadExtActions));
  768. memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
  769. memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
  770. memset(CondCodeActions, 0, sizeof(CondCodeActions));
  771. memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
  772. memset(TargetDAGCombineArray, 0, array_lengthof(TargetDAGCombineArray));
  773. // Set default actions for various operations.
  774. for (MVT VT : MVT::all_valuetypes()) {
  775. // Default all indexed load / store to expand.
  776. for (unsigned IM = (unsigned)ISD::PRE_INC;
  777. IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
  778. setIndexedLoadAction(IM, VT, Expand);
  779. setIndexedStoreAction(IM, VT, Expand);
  780. }
  781. // Most backends expect to see the node which just returns the value loaded.
  782. setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
  783. // These operations default to expand.
  784. setOperationAction(ISD::FGETSIGN, VT, Expand);
  785. setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
  786. setOperationAction(ISD::FMINNUM, VT, Expand);
  787. setOperationAction(ISD::FMAXNUM, VT, Expand);
  788. setOperationAction(ISD::FMAD, VT, Expand);
  789. setOperationAction(ISD::SMIN, VT, Expand);
  790. setOperationAction(ISD::SMAX, VT, Expand);
  791. setOperationAction(ISD::UMIN, VT, Expand);
  792. setOperationAction(ISD::UMAX, VT, Expand);
  793. // Overflow operations default to expand
  794. setOperationAction(ISD::SADDO, VT, Expand);
  795. setOperationAction(ISD::SSUBO, VT, Expand);
  796. setOperationAction(ISD::UADDO, VT, Expand);
  797. setOperationAction(ISD::USUBO, VT, Expand);
  798. setOperationAction(ISD::SMULO, VT, Expand);
  799. setOperationAction(ISD::UMULO, VT, Expand);
  800. // These library functions default to expand.
  801. setOperationAction(ISD::FROUND, VT, Expand);
  802. // These operations default to expand for vector types.
  803. if (VT.isVector()) {
  804. setOperationAction(ISD::FCOPYSIGN, VT, Expand);
  805. setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
  806. setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
  807. setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
  808. }
  809. }
  810. // Most targets ignore the @llvm.prefetch intrinsic.
  811. setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
  812. // ConstantFP nodes default to expand. Targets can either change this to
  813. // Legal, in which case all fp constants are legal, or use isFPImmLegal()
  814. // to optimize expansions for certain constants.
  815. setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
  816. setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
  817. setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
  818. setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
  819. setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
  820. // These library functions default to expand.
  821. for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
  822. setOperationAction(ISD::FLOG , VT, Expand);
  823. setOperationAction(ISD::FLOG2, VT, Expand);
  824. setOperationAction(ISD::FLOG10, VT, Expand);
  825. setOperationAction(ISD::FEXP , VT, Expand);
  826. setOperationAction(ISD::FEXP2, VT, Expand);
  827. setOperationAction(ISD::FFLOOR, VT, Expand);
  828. setOperationAction(ISD::FMINNUM, VT, Expand);
  829. setOperationAction(ISD::FMAXNUM, VT, Expand);
  830. setOperationAction(ISD::FNEARBYINT, VT, Expand);
  831. setOperationAction(ISD::FCEIL, VT, Expand);
  832. setOperationAction(ISD::FRINT, VT, Expand);
  833. setOperationAction(ISD::FTRUNC, VT, Expand);
  834. setOperationAction(ISD::FROUND, VT, Expand);
  835. }
  836. // Default ISD::TRAP to expand (which turns it into abort).
  837. setOperationAction(ISD::TRAP, MVT::Other, Expand);
  838. // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
  839. // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
  840. //
  841. setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
  842. }
  843. MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
  844. EVT) const {
  845. return MVT::getIntegerVT(8 * DL.getPointerSize(0));
  846. }
  847. EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy,
  848. const DataLayout &DL) const {
  849. assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
  850. if (LHSTy.isVector())
  851. return LHSTy;
  852. return getScalarShiftAmountTy(DL, LHSTy);
  853. }
  854. /// canOpTrap - Returns true if the operation can trap for the value type.
  855. /// VT must be a legal type.
  856. bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
  857. assert(isTypeLegal(VT));
  858. switch (Op) {
  859. default:
  860. return false;
  861. case ISD::FDIV:
  862. case ISD::FREM:
  863. case ISD::SDIV:
  864. case ISD::UDIV:
  865. case ISD::SREM:
  866. case ISD::UREM:
  867. return true;
  868. }
  869. }
  870. void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
  871. // If the command-line option was specified, ignore this request.
  872. if (!JumpIsExpensiveOverride.getNumOccurrences())
  873. JumpIsExpensive = isExpensive;
  874. }
  875. TargetLoweringBase::LegalizeKind
  876. TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
  877. // If this is a simple type, use the ComputeRegisterProp mechanism.
  878. if (VT.isSimple()) {
  879. MVT SVT = VT.getSimpleVT();
  880. assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
  881. MVT NVT = TransformToType[SVT.SimpleTy];
  882. LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
  883. assert((LA == TypeLegal || LA == TypeSoftenFloat ||
  884. ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger) &&
  885. "Promote may not follow Expand or Promote");
  886. if (LA == TypeSplitVector)
  887. return LegalizeKind(LA,
  888. EVT::getVectorVT(Context, SVT.getVectorElementType(),
  889. SVT.getVectorNumElements() / 2));
  890. if (LA == TypeScalarizeVector)
  891. return LegalizeKind(LA, SVT.getVectorElementType());
  892. return LegalizeKind(LA, NVT);
  893. }
  894. // Handle Extended Scalar Types.
  895. if (!VT.isVector()) {
  896. assert(VT.isInteger() && "Float types must be simple");
  897. unsigned BitSize = VT.getSizeInBits();
  898. // First promote to a power-of-two size, then expand if necessary.
  899. if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
  900. EVT NVT = VT.getRoundIntegerType(Context);
  901. assert(NVT != VT && "Unable to round integer VT");
  902. LegalizeKind NextStep = getTypeConversion(Context, NVT);
  903. // Avoid multi-step promotion.
  904. if (NextStep.first == TypePromoteInteger)
  905. return NextStep;
  906. // Return rounded integer type.
  907. return LegalizeKind(TypePromoteInteger, NVT);
  908. }
  909. return LegalizeKind(TypeExpandInteger,
  910. EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
  911. }
  912. // Handle vector types.
  913. unsigned NumElts = VT.getVectorNumElements();
  914. EVT EltVT = VT.getVectorElementType();
  915. // Vectors with only one element are always scalarized.
  916. if (NumElts == 1)
  917. return LegalizeKind(TypeScalarizeVector, EltVT);
  918. // Try to widen vector elements until the element type is a power of two and
  919. // promote it to a legal type later on, for example:
  920. // <3 x i8> -> <4 x i8> -> <4 x i32>
  921. if (EltVT.isInteger()) {
  922. // Vectors with a number of elements that is not a power of two are always
  923. // widened, for example <3 x i8> -> <4 x i8>.
  924. if (!VT.isPow2VectorType()) {
  925. NumElts = (unsigned)NextPowerOf2(NumElts);
  926. EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  927. return LegalizeKind(TypeWidenVector, NVT);
  928. }
  929. // Examine the element type.
  930. LegalizeKind LK = getTypeConversion(Context, EltVT);
  931. // If type is to be expanded, split the vector.
  932. // <4 x i140> -> <2 x i140>
  933. if (LK.first == TypeExpandInteger)
  934. return LegalizeKind(TypeSplitVector,
  935. EVT::getVectorVT(Context, EltVT, NumElts / 2));
  936. // Promote the integer element types until a legal vector type is found
  937. // or until the element integer type is too big. If a legal type was not
  938. // found, fallback to the usual mechanism of widening/splitting the
  939. // vector.
  940. EVT OldEltVT = EltVT;
  941. while (1) {
  942. // Increase the bitwidth of the element to the next pow-of-two
  943. // (which is greater than 8 bits).
  944. EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
  945. .getRoundIntegerType(Context);
  946. // Stop trying when getting a non-simple element type.
  947. // Note that vector elements may be greater than legal vector element
  948. // types. Example: X86 XMM registers hold 64bit element on 32bit
  949. // systems.
  950. if (!EltVT.isSimple())
  951. break;
  952. // Build a new vector type and check if it is legal.
  953. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  954. // Found a legal promoted vector type.
  955. if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
  956. return LegalizeKind(TypePromoteInteger,
  957. EVT::getVectorVT(Context, EltVT, NumElts));
  958. }
  959. // Reset the type to the unexpanded type if we did not find a legal vector
  960. // type with a promoted vector element type.
  961. EltVT = OldEltVT;
  962. }
  963. // Try to widen the vector until a legal type is found.
  964. // If there is no wider legal type, split the vector.
  965. while (1) {
  966. // Round up to the next power of 2.
  967. NumElts = (unsigned)NextPowerOf2(NumElts);
  968. // If there is no simple vector type with this many elements then there
  969. // cannot be a larger legal vector type. Note that this assumes that
  970. // there are no skipped intermediate vector types in the simple types.
  971. if (!EltVT.isSimple())
  972. break;
  973. MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  974. if (LargerVector == MVT())
  975. break;
  976. // If this type is legal then widen the vector.
  977. if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
  978. return LegalizeKind(TypeWidenVector, LargerVector);
  979. }
  980. // Widen odd vectors to next power of two.
  981. if (!VT.isPow2VectorType()) {
  982. EVT NVT = VT.getPow2VectorType(Context);
  983. return LegalizeKind(TypeWidenVector, NVT);
  984. }
  985. // Vectors with illegal element types are expanded.
  986. EVT NVT = EVT::getVectorVT(Context, EltVT, VT.getVectorNumElements() / 2);
  987. return LegalizeKind(TypeSplitVector, NVT);
  988. }
  989. static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
  990. unsigned &NumIntermediates,
  991. MVT &RegisterVT,
  992. TargetLoweringBase *TLI) {
  993. // Figure out the right, legal destination reg to copy into.
  994. unsigned NumElts = VT.getVectorNumElements();
  995. MVT EltTy = VT.getVectorElementType();
  996. unsigned NumVectorRegs = 1;
  997. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  998. // could break down into LHS/RHS like LegalizeDAG does.
  999. if (!isPowerOf2_32(NumElts)) {
  1000. NumVectorRegs = NumElts;
  1001. NumElts = 1;
  1002. }
  1003. // Divide the input until we get to a supported size. This will always
  1004. // end with a scalar if the target doesn't support vectors.
  1005. while (NumElts > 1 && !TLI->isTypeLegal(MVT::getVectorVT(EltTy, NumElts))) {
  1006. NumElts >>= 1;
  1007. NumVectorRegs <<= 1;
  1008. }
  1009. NumIntermediates = NumVectorRegs;
  1010. MVT NewVT = MVT::getVectorVT(EltTy, NumElts);
  1011. if (!TLI->isTypeLegal(NewVT))
  1012. NewVT = EltTy;
  1013. IntermediateVT = NewVT;
  1014. unsigned NewVTSize = NewVT.getSizeInBits();
  1015. // Convert sizes such as i33 to i64.
  1016. if (!isPowerOf2_32(NewVTSize))
  1017. NewVTSize = NextPowerOf2(NewVTSize);
  1018. MVT DestVT = TLI->getRegisterType(NewVT);
  1019. RegisterVT = DestVT;
  1020. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1021. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1022. // Otherwise, promotion or legal types use the same number of registers as
  1023. // the vector decimated to the appropriate level.
  1024. return NumVectorRegs;
  1025. }
  1026. /// isLegalRC - Return true if the value types that can be represented by the
  1027. /// specified register class are all legal.
  1028. bool TargetLoweringBase::isLegalRC(const TargetRegisterClass *RC) const {
  1029. for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
  1030. I != E; ++I) {
  1031. if (isTypeLegal(*I))
  1032. return true;
  1033. }
  1034. return false;
  1035. }
  1036. /// Replace/modify any TargetFrameIndex operands with a targte-dependent
  1037. /// sequence of memory operands that is recognized by PrologEpilogInserter.
  1038. MachineBasicBlock*
  1039. TargetLoweringBase::emitPatchPoint(MachineInstr *MI,
  1040. MachineBasicBlock *MBB) const {
  1041. MachineFunction &MF = *MI->getParent()->getParent();
  1042. // MI changes inside this loop as we grow operands.
  1043. for(unsigned OperIdx = 0; OperIdx != MI->getNumOperands(); ++OperIdx) {
  1044. MachineOperand &MO = MI->getOperand(OperIdx);
  1045. if (!MO.isFI())
  1046. continue;
  1047. // foldMemoryOperand builds a new MI after replacing a single FI operand
  1048. // with the canonical set of five x86 addressing-mode operands.
  1049. int FI = MO.getIndex();
  1050. MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
  1051. // Copy operands before the frame-index.
  1052. for (unsigned i = 0; i < OperIdx; ++i)
  1053. MIB.addOperand(MI->getOperand(i));
  1054. // Add frame index operands: direct-mem-ref tag, #FI, offset.
  1055. MIB.addImm(StackMaps::DirectMemRefOp);
  1056. MIB.addOperand(MI->getOperand(OperIdx));
  1057. MIB.addImm(0);
  1058. // Copy the operands after the frame index.
  1059. for (unsigned i = OperIdx + 1; i != MI->getNumOperands(); ++i)
  1060. MIB.addOperand(MI->getOperand(i));
  1061. // Inherit previous memory operands.
  1062. MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
  1063. assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
  1064. // Add a new memory operand for this FI.
  1065. const MachineFrameInfo &MFI = *MF.getFrameInfo();
  1066. assert(MFI.getObjectOffset(FI) != -1);
  1067. unsigned Flags = MachineMemOperand::MOLoad;
  1068. if (MI->getOpcode() == TargetOpcode::STATEPOINT) {
  1069. Flags |= MachineMemOperand::MOStore;
  1070. Flags |= MachineMemOperand::MOVolatile;
  1071. }
  1072. MachineMemOperand *MMO = MF.getMachineMemOperand(
  1073. MachinePointerInfo::getFixedStack(FI), Flags,
  1074. TM.getDataLayout()->getPointerSize(), MFI.getObjectAlignment(FI));
  1075. MIB->addMemOperand(MF, MMO);
  1076. // Replace the instruction and update the operand index.
  1077. MBB->insert(MachineBasicBlock::iterator(MI), MIB);
  1078. OperIdx += (MIB->getNumOperands() - MI->getNumOperands()) - 1;
  1079. MI->eraseFromParent();
  1080. MI = MIB;
  1081. }
  1082. return MBB;
  1083. }
  1084. /// findRepresentativeClass - Return the largest legal super-reg register class
  1085. /// of the register class for the specified type and its associated "cost".
  1086. // This function is in TargetLowering because it uses RegClassForVT which would
  1087. // need to be moved to TargetRegisterInfo and would necessitate moving
  1088. // isTypeLegal over as well - a massive change that would just require
  1089. // TargetLowering having a TargetRegisterInfo class member that it would use.
  1090. std::pair<const TargetRegisterClass *, uint8_t>
  1091. TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
  1092. MVT VT) const {
  1093. const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
  1094. if (!RC)
  1095. return std::make_pair(RC, 0);
  1096. // Compute the set of all super-register classes.
  1097. BitVector SuperRegRC(TRI->getNumRegClasses());
  1098. for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
  1099. SuperRegRC.setBitsInMask(RCI.getMask());
  1100. // Find the first legal register class with the largest spill size.
  1101. const TargetRegisterClass *BestRC = RC;
  1102. for (int i = SuperRegRC.find_first(); i >= 0; i = SuperRegRC.find_next(i)) {
  1103. const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
  1104. // We want the largest possible spill size.
  1105. if (SuperRC->getSize() <= BestRC->getSize())
  1106. continue;
  1107. if (!isLegalRC(SuperRC))
  1108. continue;
  1109. BestRC = SuperRC;
  1110. }
  1111. return std::make_pair(BestRC, 1);
  1112. }
  1113. /// computeRegisterProperties - Once all of the register classes are added,
  1114. /// this allows us to compute derived properties we expose.
  1115. void TargetLoweringBase::computeRegisterProperties(
  1116. const TargetRegisterInfo *TRI) {
  1117. static_assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE,
  1118. "Too many value types for ValueTypeActions to hold!");
  1119. // Everything defaults to needing one register.
  1120. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1121. NumRegistersForVT[i] = 1;
  1122. RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
  1123. }
  1124. // ...except isVoid, which doesn't need any registers.
  1125. NumRegistersForVT[MVT::isVoid] = 0;
  1126. // Find the largest integer register class.
  1127. unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
  1128. for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
  1129. assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
  1130. // Every integer value type larger than this largest register takes twice as
  1131. // many registers to represent as the previous ValueType.
  1132. for (unsigned ExpandedReg = LargestIntReg + 1;
  1133. ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
  1134. NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
  1135. RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
  1136. TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
  1137. ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
  1138. TypeExpandInteger);
  1139. }
  1140. // Inspect all of the ValueType's smaller than the largest integer
  1141. // register to see which ones need promotion.
  1142. unsigned LegalIntReg = LargestIntReg;
  1143. for (unsigned IntReg = LargestIntReg - 1;
  1144. IntReg >= (unsigned)MVT::i1; --IntReg) {
  1145. MVT IVT = (MVT::SimpleValueType)IntReg;
  1146. if (isTypeLegal(IVT)) {
  1147. LegalIntReg = IntReg;
  1148. } else {
  1149. RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
  1150. (const MVT::SimpleValueType)LegalIntReg;
  1151. ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
  1152. }
  1153. }
  1154. // ppcf128 type is really two f64's.
  1155. if (!isTypeLegal(MVT::ppcf128)) {
  1156. NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
  1157. RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
  1158. TransformToType[MVT::ppcf128] = MVT::f64;
  1159. ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
  1160. }
  1161. // Decide how to handle f128. If the target does not have native f128 support,
  1162. // expand it to i128 and we will be generating soft float library calls.
  1163. if (!isTypeLegal(MVT::f128)) {
  1164. NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
  1165. RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
  1166. TransformToType[MVT::f128] = MVT::i128;
  1167. ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
  1168. }
  1169. // Decide how to handle f64. If the target does not have native f64 support,
  1170. // expand it to i64 and we will be generating soft float library calls.
  1171. if (!isTypeLegal(MVT::f64)) {
  1172. NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
  1173. RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
  1174. TransformToType[MVT::f64] = MVT::i64;
  1175. ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
  1176. }
  1177. // Decide how to handle f32. If the target does not have native f32 support,
  1178. // expand it to i32 and we will be generating soft float library calls.
  1179. if (!isTypeLegal(MVT::f32)) {
  1180. NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
  1181. RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
  1182. TransformToType[MVT::f32] = MVT::i32;
  1183. ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
  1184. }
  1185. if (!isTypeLegal(MVT::f16)) {
  1186. // If the target has native f32 support, promote f16 operations to f32. If
  1187. // f32 is not supported, generate soft float library calls.
  1188. if (isTypeLegal(MVT::f32)) {
  1189. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
  1190. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
  1191. TransformToType[MVT::f16] = MVT::f32;
  1192. ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
  1193. } else {
  1194. NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
  1195. RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
  1196. TransformToType[MVT::f16] = MVT::i16;
  1197. ValueTypeActions.setTypeAction(MVT::f16, TypeSoftenFloat);
  1198. }
  1199. }
  1200. // Loop over all of the vector value types to see which need transformations.
  1201. for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
  1202. i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
  1203. MVT VT = (MVT::SimpleValueType) i;
  1204. if (isTypeLegal(VT))
  1205. continue;
  1206. MVT EltVT = VT.getVectorElementType();
  1207. unsigned NElts = VT.getVectorNumElements();
  1208. bool IsLegalWiderType = false;
  1209. LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
  1210. switch (PreferredAction) {
  1211. case TypePromoteInteger: {
  1212. // Try to promote the elements of integer vectors. If no legal
  1213. // promotion was found, fall through to the widen-vector method.
  1214. for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  1215. MVT SVT = (MVT::SimpleValueType) nVT;
  1216. // Promote vectors of integers to vectors with the same number
  1217. // of elements, with a wider element type.
  1218. if (SVT.getVectorElementType().getSizeInBits() > EltVT.getSizeInBits()
  1219. && SVT.getVectorNumElements() == NElts && isTypeLegal(SVT)
  1220. && SVT.getScalarType().isInteger()) {
  1221. TransformToType[i] = SVT;
  1222. RegisterTypeForVT[i] = SVT;
  1223. NumRegistersForVT[i] = 1;
  1224. ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
  1225. IsLegalWiderType = true;
  1226. break;
  1227. }
  1228. }
  1229. if (IsLegalWiderType)
  1230. break;
  1231. }
  1232. case TypeWidenVector: {
  1233. // Try to widen the vector.
  1234. for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
  1235. MVT SVT = (MVT::SimpleValueType) nVT;
  1236. if (SVT.getVectorElementType() == EltVT
  1237. && SVT.getVectorNumElements() > NElts && isTypeLegal(SVT)) {
  1238. TransformToType[i] = SVT;
  1239. RegisterTypeForVT[i] = SVT;
  1240. NumRegistersForVT[i] = 1;
  1241. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1242. IsLegalWiderType = true;
  1243. break;
  1244. }
  1245. }
  1246. if (IsLegalWiderType)
  1247. break;
  1248. }
  1249. case TypeSplitVector:
  1250. case TypeScalarizeVector: {
  1251. MVT IntermediateVT;
  1252. MVT RegisterVT;
  1253. unsigned NumIntermediates;
  1254. NumRegistersForVT[i] = getVectorTypeBreakdownMVT(VT, IntermediateVT,
  1255. NumIntermediates, RegisterVT, this);
  1256. RegisterTypeForVT[i] = RegisterVT;
  1257. MVT NVT = VT.getPow2VectorType();
  1258. if (NVT == VT) {
  1259. // Type is already a power of 2. The default action is to split.
  1260. TransformToType[i] = MVT::Other;
  1261. if (PreferredAction == TypeScalarizeVector)
  1262. ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
  1263. else if (PreferredAction == TypeSplitVector)
  1264. ValueTypeActions.setTypeAction(VT, TypeSplitVector);
  1265. else
  1266. // Set type action according to the number of elements.
  1267. ValueTypeActions.setTypeAction(VT, NElts == 1 ? TypeScalarizeVector
  1268. : TypeSplitVector);
  1269. } else {
  1270. TransformToType[i] = NVT;
  1271. ValueTypeActions.setTypeAction(VT, TypeWidenVector);
  1272. }
  1273. break;
  1274. }
  1275. default:
  1276. llvm_unreachable("Unknown vector legalization action!");
  1277. }
  1278. }
  1279. // Determine the 'representative' register class for each value type.
  1280. // An representative register class is the largest (meaning one which is
  1281. // not a sub-register class / subreg register class) legal register class for
  1282. // a group of value types. For example, on i386, i8, i16, and i32
  1283. // representative would be GR32; while on x86_64 it's GR64.
  1284. for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
  1285. const TargetRegisterClass* RRC;
  1286. uint8_t Cost;
  1287. std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
  1288. RepRegClassForVT[i] = RRC;
  1289. RepRegClassCostForVT[i] = Cost;
  1290. }
  1291. }
  1292. EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
  1293. EVT VT) const {
  1294. assert(!VT.isVector() && "No default SetCC type for vectors!");
  1295. return getPointerTy(DL).SimpleTy;
  1296. }
  1297. MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
  1298. return MVT::i32; // return the default value
  1299. }
  1300. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  1301. /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
  1302. /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
  1303. /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
  1304. ///
  1305. /// This method returns the number of registers needed, and the VT for each
  1306. /// register. It also returns the VT and quantity of the intermediate values
  1307. /// before they are promoted/expanded.
  1308. ///
  1309. unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
  1310. EVT &IntermediateVT,
  1311. unsigned &NumIntermediates,
  1312. MVT &RegisterVT) const {
  1313. unsigned NumElts = VT.getVectorNumElements();
  1314. // If there is a wider vector type with the same element type as this one,
  1315. // or a promoted vector type that has the same number of elements which
  1316. // are wider, then we should convert to that legal vector type.
  1317. // This handles things like <2 x float> -> <4 x float> and
  1318. // <4 x i1> -> <4 x i32>.
  1319. LegalizeTypeAction TA = getTypeAction(Context, VT);
  1320. if (NumElts != 1 && (TA == TypeWidenVector || TA == TypePromoteInteger)) {
  1321. EVT RegisterEVT = getTypeToTransformTo(Context, VT);
  1322. if (isTypeLegal(RegisterEVT)) {
  1323. IntermediateVT = RegisterEVT;
  1324. RegisterVT = RegisterEVT.getSimpleVT();
  1325. NumIntermediates = 1;
  1326. return 1;
  1327. }
  1328. }
  1329. // Figure out the right, legal destination reg to copy into.
  1330. EVT EltTy = VT.getVectorElementType();
  1331. unsigned NumVectorRegs = 1;
  1332. // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
  1333. // could break down into LHS/RHS like LegalizeDAG does.
  1334. if (!isPowerOf2_32(NumElts)) {
  1335. NumVectorRegs = NumElts;
  1336. NumElts = 1;
  1337. }
  1338. // Divide the input until we get to a supported size. This will always
  1339. // end with a scalar if the target doesn't support vectors.
  1340. while (NumElts > 1 && !isTypeLegal(
  1341. EVT::getVectorVT(Context, EltTy, NumElts))) {
  1342. NumElts >>= 1;
  1343. NumVectorRegs <<= 1;
  1344. }
  1345. NumIntermediates = NumVectorRegs;
  1346. EVT NewVT = EVT::getVectorVT(Context, EltTy, NumElts);
  1347. if (!isTypeLegal(NewVT))
  1348. NewVT = EltTy;
  1349. IntermediateVT = NewVT;
  1350. MVT DestVT = getRegisterType(Context, NewVT);
  1351. RegisterVT = DestVT;
  1352. unsigned NewVTSize = NewVT.getSizeInBits();
  1353. // Convert sizes such as i33 to i64.
  1354. if (!isPowerOf2_32(NewVTSize))
  1355. NewVTSize = NextPowerOf2(NewVTSize);
  1356. if (EVT(DestVT).bitsLT(NewVT)) // Value is expanded, e.g. i64 -> i16.
  1357. return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
  1358. // Otherwise, promotion or legal types use the same number of registers as
  1359. // the vector decimated to the appropriate level.
  1360. return NumVectorRegs;
  1361. }
  1362. /// Get the EVTs and ArgFlags collections that represent the legalized return
  1363. /// type of the given function. This does not require a DAG or a return value,
  1364. /// and is suitable for use before any DAGs for the function are constructed.
  1365. /// TODO: Move this out of TargetLowering.cpp.
  1366. void llvm::GetReturnInfo(Type *ReturnType, AttributeSet attr,
  1367. SmallVectorImpl<ISD::OutputArg> &Outs,
  1368. const TargetLowering &TLI, const DataLayout &DL) {
  1369. SmallVector<EVT, 4> ValueVTs;
  1370. ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
  1371. unsigned NumValues = ValueVTs.size();
  1372. if (NumValues == 0) return;
  1373. for (unsigned j = 0, f = NumValues; j != f; ++j) {
  1374. EVT VT = ValueVTs[j];
  1375. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  1376. if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
  1377. ExtendKind = ISD::SIGN_EXTEND;
  1378. else if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt))
  1379. ExtendKind = ISD::ZERO_EXTEND;
  1380. // FIXME: C calling convention requires the return type to be promoted to
  1381. // at least 32-bit. But this is not necessary for non-C calling
  1382. // conventions. The frontend should mark functions whose return values
  1383. // require promoting with signext or zeroext attributes.
  1384. if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
  1385. MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
  1386. if (VT.bitsLT(MinVT))
  1387. VT = MinVT;
  1388. }
  1389. unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
  1390. MVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
  1391. // 'inreg' on function refers to return value
  1392. ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
  1393. if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::InReg))
  1394. Flags.setInReg();
  1395. // Propagate extension type if any
  1396. if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
  1397. Flags.setSExt();
  1398. else if (attr.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt))
  1399. Flags.setZExt();
  1400. for (unsigned i = 0; i < NumParts; ++i)
  1401. Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isFixed=*/true, 0, 0));
  1402. }
  1403. }
  1404. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  1405. /// function arguments in the caller parameter area. This is the actual
  1406. /// alignment, not its logarithm.
  1407. unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
  1408. const DataLayout &DL) const {
  1409. return DL.getABITypeAlignment(Ty);
  1410. }
  1411. //===----------------------------------------------------------------------===//
  1412. // TargetTransformInfo Helpers
  1413. //===----------------------------------------------------------------------===//
  1414. int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
  1415. enum InstructionOpcodes {
  1416. #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
  1417. #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
  1418. #include "llvm/IR/Instruction.def"
  1419. };
  1420. switch (static_cast<InstructionOpcodes>(Opcode)) {
  1421. case Ret: return 0;
  1422. case Br: return 0;
  1423. case Switch: return 0;
  1424. case IndirectBr: return 0;
  1425. case Invoke: return 0;
  1426. case Resume: return 0;
  1427. case Unreachable: return 0;
  1428. case Add: return ISD::ADD;
  1429. case FAdd: return ISD::FADD;
  1430. case Sub: return ISD::SUB;
  1431. case FSub: return ISD::FSUB;
  1432. case Mul: return ISD::MUL;
  1433. case FMul: return ISD::FMUL;
  1434. case UDiv: return ISD::UDIV;
  1435. case SDiv: return ISD::SDIV;
  1436. case FDiv: return ISD::FDIV;
  1437. case URem: return ISD::UREM;
  1438. case SRem: return ISD::SREM;
  1439. case FRem: return ISD::FREM;
  1440. case Shl: return ISD::SHL;
  1441. case LShr: return ISD::SRL;
  1442. case AShr: return ISD::SRA;
  1443. case And: return ISD::AND;
  1444. case Or: return ISD::OR;
  1445. case Xor: return ISD::XOR;
  1446. case Alloca: return 0;
  1447. case Load: return ISD::LOAD;
  1448. case Store: return ISD::STORE;
  1449. case GetElementPtr: return 0;
  1450. case Fence: return 0;
  1451. case AtomicCmpXchg: return 0;
  1452. case AtomicRMW: return 0;
  1453. case Trunc: return ISD::TRUNCATE;
  1454. case ZExt: return ISD::ZERO_EXTEND;
  1455. case SExt: return ISD::SIGN_EXTEND;
  1456. case FPToUI: return ISD::FP_TO_UINT;
  1457. case FPToSI: return ISD::FP_TO_SINT;
  1458. case UIToFP: return ISD::UINT_TO_FP;
  1459. case SIToFP: return ISD::SINT_TO_FP;
  1460. case FPTrunc: return ISD::FP_ROUND;
  1461. case FPExt: return ISD::FP_EXTEND;
  1462. case PtrToInt: return ISD::BITCAST;
  1463. case IntToPtr: return ISD::BITCAST;
  1464. case BitCast: return ISD::BITCAST;
  1465. case AddrSpaceCast: return ISD::ADDRSPACECAST;
  1466. case ICmp: return ISD::SETCC;
  1467. case FCmp: return ISD::SETCC;
  1468. case PHI: return 0;
  1469. case Call: return 0;
  1470. case Select: return ISD::SELECT;
  1471. case UserOp1: return 0;
  1472. case UserOp2: return 0;
  1473. case VAArg: return 0;
  1474. case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
  1475. case InsertElement: return ISD::INSERT_VECTOR_ELT;
  1476. case ShuffleVector: return ISD::VECTOR_SHUFFLE;
  1477. case ExtractValue: return ISD::MERGE_VALUES;
  1478. case InsertValue: return ISD::MERGE_VALUES;
  1479. case LandingPad: return 0;
  1480. }
  1481. llvm_unreachable("Unknown instruction type encountered!");
  1482. }
  1483. std::pair<unsigned, MVT>
  1484. TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
  1485. Type *Ty) const {
  1486. LLVMContext &C = Ty->getContext();
  1487. EVT MTy = getValueType(DL, Ty);
  1488. unsigned Cost = 1;
  1489. // We keep legalizing the type until we find a legal kind. We assume that
  1490. // the only operation that costs anything is the split. After splitting
  1491. // we need to handle two types.
  1492. while (true) {
  1493. LegalizeKind LK = getTypeConversion(C, MTy);
  1494. if (LK.first == TypeLegal)
  1495. return std::make_pair(Cost, MTy.getSimpleVT());
  1496. if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
  1497. Cost *= 2;
  1498. // Keep legalizing the type.
  1499. MTy = LK.second;
  1500. }
  1501. }
  1502. //===----------------------------------------------------------------------===//
  1503. // Loop Strength Reduction hooks
  1504. //===----------------------------------------------------------------------===//
  1505. /// isLegalAddressingMode - Return true if the addressing mode represented
  1506. /// by AM is legal for this target, for a load/store of the specified type.
  1507. bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
  1508. const AddrMode &AM, Type *Ty,
  1509. unsigned AS) const {
  1510. // The default implementation of this implements a conservative RISCy, r+r and
  1511. // r+i addr mode.
  1512. // Allows a sign-extended 16-bit immediate field.
  1513. if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
  1514. return false;
  1515. // No global is ever allowed as a base.
  1516. if (AM.BaseGV)
  1517. return false;
  1518. // Only support r+r,
  1519. switch (AM.Scale) {
  1520. case 0: // "r+i" or just "i", depending on HasBaseReg.
  1521. break;
  1522. case 1:
  1523. if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
  1524. return false;
  1525. // Otherwise we have r+r or r+i.
  1526. break;
  1527. case 2:
  1528. if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
  1529. return false;
  1530. // Allow 2*r as r+r.
  1531. break;
  1532. default: // Don't allow n * r
  1533. return false;
  1534. }
  1535. return true;
  1536. }