XCoreISelLowering.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  1. //===-- XCoreISelLowering.cpp - XCore DAG Lowering Implementation ---------===//
  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 file implements the XCoreTargetLowering class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "xcore-lower"
  14. #include "XCoreISelLowering.h"
  15. #include "XCore.h"
  16. #include "XCoreMachineFunctionInfo.h"
  17. #include "XCoreSubtarget.h"
  18. #include "XCoreTargetMachine.h"
  19. #include "XCoreTargetObjectFile.h"
  20. #include "llvm/CodeGen/CallingConvLower.h"
  21. #include "llvm/CodeGen/MachineFrameInfo.h"
  22. #include "llvm/CodeGen/MachineFunction.h"
  23. #include "llvm/CodeGen/MachineInstrBuilder.h"
  24. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  25. #include "llvm/CodeGen/MachineRegisterInfo.h"
  26. #include "llvm/CodeGen/SelectionDAGISel.h"
  27. #include "llvm/CodeGen/ValueTypes.h"
  28. #include "llvm/IR/CallingConv.h"
  29. #include "llvm/IR/DerivedTypes.h"
  30. #include "llvm/IR/Function.h"
  31. #include "llvm/IR/GlobalAlias.h"
  32. #include "llvm/IR/GlobalVariable.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/Support/Debug.h"
  35. #include "llvm/Support/ErrorHandling.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <algorithm>
  38. using namespace llvm;
  39. const char *XCoreTargetLowering::
  40. getTargetNodeName(unsigned Opcode) const
  41. {
  42. switch (Opcode)
  43. {
  44. case XCoreISD::BL : return "XCoreISD::BL";
  45. case XCoreISD::PCRelativeWrapper : return "XCoreISD::PCRelativeWrapper";
  46. case XCoreISD::DPRelativeWrapper : return "XCoreISD::DPRelativeWrapper";
  47. case XCoreISD::CPRelativeWrapper : return "XCoreISD::CPRelativeWrapper";
  48. case XCoreISD::STWSP : return "XCoreISD::STWSP";
  49. case XCoreISD::RETSP : return "XCoreISD::RETSP";
  50. case XCoreISD::LADD : return "XCoreISD::LADD";
  51. case XCoreISD::LSUB : return "XCoreISD::LSUB";
  52. case XCoreISD::LMUL : return "XCoreISD::LMUL";
  53. case XCoreISD::MACCU : return "XCoreISD::MACCU";
  54. case XCoreISD::MACCS : return "XCoreISD::MACCS";
  55. case XCoreISD::CRC8 : return "XCoreISD::CRC8";
  56. case XCoreISD::BR_JT : return "XCoreISD::BR_JT";
  57. case XCoreISD::BR_JT32 : return "XCoreISD::BR_JT32";
  58. default : return NULL;
  59. }
  60. }
  61. XCoreTargetLowering::XCoreTargetLowering(XCoreTargetMachine &XTM)
  62. : TargetLowering(XTM, new XCoreTargetObjectFile()),
  63. TM(XTM),
  64. Subtarget(*XTM.getSubtargetImpl()) {
  65. // Set up the register classes.
  66. addRegisterClass(MVT::i32, &XCore::GRRegsRegClass);
  67. // Compute derived properties from the register classes
  68. computeRegisterProperties();
  69. // Division is expensive
  70. setIntDivIsCheap(false);
  71. setStackPointerRegisterToSaveRestore(XCore::SP);
  72. setSchedulingPreference(Sched::RegPressure);
  73. // Use i32 for setcc operations results (slt, sgt, ...).
  74. setBooleanContents(ZeroOrOneBooleanContent);
  75. setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
  76. // XCore does not have the NodeTypes below.
  77. setOperationAction(ISD::BR_CC, MVT::i32, Expand);
  78. setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
  79. setOperationAction(ISD::ADDC, MVT::i32, Expand);
  80. setOperationAction(ISD::ADDE, MVT::i32, Expand);
  81. setOperationAction(ISD::SUBC, MVT::i32, Expand);
  82. setOperationAction(ISD::SUBE, MVT::i32, Expand);
  83. // Stop the combiner recombining select and set_cc
  84. setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
  85. // 64bit
  86. setOperationAction(ISD::ADD, MVT::i64, Custom);
  87. setOperationAction(ISD::SUB, MVT::i64, Custom);
  88. setOperationAction(ISD::SMUL_LOHI, MVT::i32, Custom);
  89. setOperationAction(ISD::UMUL_LOHI, MVT::i32, Custom);
  90. setOperationAction(ISD::MULHS, MVT::i32, Expand);
  91. setOperationAction(ISD::MULHU, MVT::i32, Expand);
  92. setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
  93. setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
  94. setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
  95. // Bit Manipulation
  96. setOperationAction(ISD::CTPOP, MVT::i32, Expand);
  97. setOperationAction(ISD::ROTL , MVT::i32, Expand);
  98. setOperationAction(ISD::ROTR , MVT::i32, Expand);
  99. setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
  100. setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
  101. setOperationAction(ISD::TRAP, MVT::Other, Legal);
  102. // Jump tables.
  103. setOperationAction(ISD::BR_JT, MVT::Other, Custom);
  104. setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
  105. setOperationAction(ISD::BlockAddress, MVT::i32 , Custom);
  106. // Conversion of i64 -> double produces constantpool nodes
  107. setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
  108. // Loads
  109. setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
  110. setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
  111. setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
  112. setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
  113. setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
  114. // Custom expand misaligned loads / stores.
  115. setOperationAction(ISD::LOAD, MVT::i32, Custom);
  116. setOperationAction(ISD::STORE, MVT::i32, Custom);
  117. // Varargs
  118. setOperationAction(ISD::VAEND, MVT::Other, Expand);
  119. setOperationAction(ISD::VACOPY, MVT::Other, Expand);
  120. setOperationAction(ISD::VAARG, MVT::Other, Custom);
  121. setOperationAction(ISD::VASTART, MVT::Other, Custom);
  122. // Dynamic stack
  123. setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
  124. setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
  125. setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
  126. // TRAMPOLINE is custom lowered.
  127. setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
  128. setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
  129. // We want to custom lower some of our intrinsics.
  130. setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
  131. MaxStoresPerMemset = MaxStoresPerMemsetOptSize = 4;
  132. MaxStoresPerMemmove = MaxStoresPerMemmoveOptSize
  133. = MaxStoresPerMemcpy = MaxStoresPerMemcpyOptSize = 2;
  134. // We have target-specific dag combine patterns for the following nodes:
  135. setTargetDAGCombine(ISD::STORE);
  136. setTargetDAGCombine(ISD::ADD);
  137. setMinFunctionAlignment(1);
  138. }
  139. SDValue XCoreTargetLowering::
  140. LowerOperation(SDValue Op, SelectionDAG &DAG) const {
  141. switch (Op.getOpcode())
  142. {
  143. case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
  144. case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
  145. case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
  146. case ISD::BR_JT: return LowerBR_JT(Op, DAG);
  147. case ISD::LOAD: return LowerLOAD(Op, DAG);
  148. case ISD::STORE: return LowerSTORE(Op, DAG);
  149. case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
  150. case ISD::VAARG: return LowerVAARG(Op, DAG);
  151. case ISD::VASTART: return LowerVASTART(Op, DAG);
  152. case ISD::SMUL_LOHI: return LowerSMUL_LOHI(Op, DAG);
  153. case ISD::UMUL_LOHI: return LowerUMUL_LOHI(Op, DAG);
  154. // FIXME: Remove these when LegalizeDAGTypes lands.
  155. case ISD::ADD:
  156. case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG);
  157. case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
  158. case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
  159. case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
  160. case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
  161. default:
  162. llvm_unreachable("unimplemented operand");
  163. }
  164. }
  165. /// ReplaceNodeResults - Replace the results of node with an illegal result
  166. /// type with new values built out of custom code.
  167. void XCoreTargetLowering::ReplaceNodeResults(SDNode *N,
  168. SmallVectorImpl<SDValue>&Results,
  169. SelectionDAG &DAG) const {
  170. switch (N->getOpcode()) {
  171. default:
  172. llvm_unreachable("Don't know how to custom expand this!");
  173. case ISD::ADD:
  174. case ISD::SUB:
  175. Results.push_back(ExpandADDSUB(N, DAG));
  176. return;
  177. }
  178. }
  179. //===----------------------------------------------------------------------===//
  180. // Misc Lower Operation implementation
  181. //===----------------------------------------------------------------------===//
  182. SDValue XCoreTargetLowering::
  183. LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
  184. {
  185. SDLoc dl(Op);
  186. SDValue Cond = DAG.getNode(ISD::SETCC, dl, MVT::i32, Op.getOperand(2),
  187. Op.getOperand(3), Op.getOperand(4));
  188. return DAG.getNode(ISD::SELECT, dl, MVT::i32, Cond, Op.getOperand(0),
  189. Op.getOperand(1));
  190. }
  191. SDValue XCoreTargetLowering::
  192. getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV,
  193. SelectionDAG &DAG) const
  194. {
  195. // FIXME there is no actual debug info here
  196. SDLoc dl(GA);
  197. const GlobalValue *UnderlyingGV = GV;
  198. // If GV is an alias then use the aliasee to determine the wrapper type
  199. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
  200. UnderlyingGV = GA->resolveAliasedGlobal();
  201. if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(UnderlyingGV)) {
  202. if (GVar->isConstant())
  203. return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, GA);
  204. return DAG.getNode(XCoreISD::DPRelativeWrapper, dl, MVT::i32, GA);
  205. }
  206. return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA);
  207. }
  208. SDValue XCoreTargetLowering::
  209. LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const
  210. {
  211. SDLoc DL(Op);
  212. const GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Op);
  213. const GlobalValue *GV = GN->getGlobal();
  214. int64_t Offset = GN->getOffset();
  215. // We can only fold positive offsets that are a multiple of the word size.
  216. int64_t FoldedOffset = std::max(Offset & ~3, (int64_t)0);
  217. SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, FoldedOffset);
  218. GA = getGlobalAddressWrapper(GA, GV, DAG);
  219. // Handle the rest of the offset.
  220. if (Offset != FoldedOffset) {
  221. SDValue Remaining = DAG.getConstant(Offset - FoldedOffset, MVT::i32);
  222. GA = DAG.getNode(ISD::ADD, DL, MVT::i32, GA, Remaining);
  223. }
  224. return GA;
  225. }
  226. static inline SDValue BuildGetId(SelectionDAG &DAG, SDLoc dl) {
  227. return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
  228. DAG.getConstant(Intrinsic::xcore_getid, MVT::i32));
  229. }
  230. SDValue XCoreTargetLowering::
  231. LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const
  232. {
  233. SDLoc DL(Op);
  234. const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
  235. SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy());
  236. return DAG.getNode(XCoreISD::PCRelativeWrapper, DL, getPointerTy(), Result);
  237. }
  238. SDValue XCoreTargetLowering::
  239. LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
  240. {
  241. ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
  242. // FIXME there isn't really debug info here
  243. SDLoc dl(CP);
  244. EVT PtrVT = Op.getValueType();
  245. SDValue Res;
  246. if (CP->isMachineConstantPoolEntry()) {
  247. Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
  248. CP->getAlignment());
  249. } else {
  250. Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
  251. CP->getAlignment());
  252. }
  253. return DAG.getNode(XCoreISD::CPRelativeWrapper, dl, MVT::i32, Res);
  254. }
  255. unsigned XCoreTargetLowering::getJumpTableEncoding() const {
  256. return MachineJumpTableInfo::EK_Inline;
  257. }
  258. SDValue XCoreTargetLowering::
  259. LowerBR_JT(SDValue Op, SelectionDAG &DAG) const
  260. {
  261. SDValue Chain = Op.getOperand(0);
  262. SDValue Table = Op.getOperand(1);
  263. SDValue Index = Op.getOperand(2);
  264. SDLoc dl(Op);
  265. JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
  266. unsigned JTI = JT->getIndex();
  267. MachineFunction &MF = DAG.getMachineFunction();
  268. const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
  269. SDValue TargetJT = DAG.getTargetJumpTable(JT->getIndex(), MVT::i32);
  270. unsigned NumEntries = MJTI->getJumpTables()[JTI].MBBs.size();
  271. if (NumEntries <= 32) {
  272. return DAG.getNode(XCoreISD::BR_JT, dl, MVT::Other, Chain, TargetJT, Index);
  273. }
  274. assert((NumEntries >> 31) == 0);
  275. SDValue ScaledIndex = DAG.getNode(ISD::SHL, dl, MVT::i32, Index,
  276. DAG.getConstant(1, MVT::i32));
  277. return DAG.getNode(XCoreISD::BR_JT32, dl, MVT::Other, Chain, TargetJT,
  278. ScaledIndex);
  279. }
  280. SDValue XCoreTargetLowering::
  281. lowerLoadWordFromAlignedBasePlusOffset(SDLoc DL, SDValue Chain, SDValue Base,
  282. int64_t Offset, SelectionDAG &DAG) const
  283. {
  284. if ((Offset & 0x3) == 0) {
  285. return DAG.getLoad(getPointerTy(), DL, Chain, Base, MachinePointerInfo(),
  286. false, false, false, 0);
  287. }
  288. // Lower to pair of consecutive word aligned loads plus some bit shifting.
  289. int32_t HighOffset = RoundUpToAlignment(Offset, 4);
  290. int32_t LowOffset = HighOffset - 4;
  291. SDValue LowAddr, HighAddr;
  292. if (GlobalAddressSDNode *GASD =
  293. dyn_cast<GlobalAddressSDNode>(Base.getNode())) {
  294. LowAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
  295. LowOffset);
  296. HighAddr = DAG.getGlobalAddress(GASD->getGlobal(), DL, Base.getValueType(),
  297. HighOffset);
  298. } else {
  299. LowAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
  300. DAG.getConstant(LowOffset, MVT::i32));
  301. HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, Base,
  302. DAG.getConstant(HighOffset, MVT::i32));
  303. }
  304. SDValue LowShift = DAG.getConstant((Offset - LowOffset) * 8, MVT::i32);
  305. SDValue HighShift = DAG.getConstant((HighOffset - Offset) * 8, MVT::i32);
  306. SDValue Low = DAG.getLoad(getPointerTy(), DL, Chain,
  307. LowAddr, MachinePointerInfo(),
  308. false, false, false, 0);
  309. SDValue High = DAG.getLoad(getPointerTy(), DL, Chain,
  310. HighAddr, MachinePointerInfo(),
  311. false, false, false, 0);
  312. SDValue LowShifted = DAG.getNode(ISD::SRL, DL, MVT::i32, Low, LowShift);
  313. SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High, HighShift);
  314. SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, LowShifted, HighShifted);
  315. Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
  316. High.getValue(1));
  317. SDValue Ops[] = { Result, Chain };
  318. return DAG.getMergeValues(Ops, 2, DL);
  319. }
  320. static bool isWordAligned(SDValue Value, SelectionDAG &DAG)
  321. {
  322. APInt KnownZero, KnownOne;
  323. DAG.ComputeMaskedBits(Value, KnownZero, KnownOne);
  324. return KnownZero.countTrailingOnes() >= 2;
  325. }
  326. SDValue XCoreTargetLowering::
  327. LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
  328. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  329. LoadSDNode *LD = cast<LoadSDNode>(Op);
  330. assert(LD->getExtensionType() == ISD::NON_EXTLOAD &&
  331. "Unexpected extension type");
  332. assert(LD->getMemoryVT() == MVT::i32 && "Unexpected load EVT");
  333. if (allowsUnalignedMemoryAccesses(LD->getMemoryVT()))
  334. return SDValue();
  335. unsigned ABIAlignment = getDataLayout()->
  336. getABITypeAlignment(LD->getMemoryVT().getTypeForEVT(*DAG.getContext()));
  337. // Leave aligned load alone.
  338. if (LD->getAlignment() >= ABIAlignment)
  339. return SDValue();
  340. SDValue Chain = LD->getChain();
  341. SDValue BasePtr = LD->getBasePtr();
  342. SDLoc DL(Op);
  343. if (!LD->isVolatile()) {
  344. const GlobalValue *GV;
  345. int64_t Offset = 0;
  346. if (DAG.isBaseWithConstantOffset(BasePtr) &&
  347. isWordAligned(BasePtr->getOperand(0), DAG)) {
  348. SDValue NewBasePtr = BasePtr->getOperand(0);
  349. Offset = cast<ConstantSDNode>(BasePtr->getOperand(1))->getSExtValue();
  350. return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
  351. Offset, DAG);
  352. }
  353. if (TLI.isGAPlusOffset(BasePtr.getNode(), GV, Offset) &&
  354. MinAlign(GV->getAlignment(), 4) == 4) {
  355. SDValue NewBasePtr = DAG.getGlobalAddress(GV, DL,
  356. BasePtr->getValueType(0));
  357. return lowerLoadWordFromAlignedBasePlusOffset(DL, Chain, NewBasePtr,
  358. Offset, DAG);
  359. }
  360. }
  361. if (LD->getAlignment() == 2) {
  362. SDValue Low = DAG.getExtLoad(ISD::ZEXTLOAD, DL, MVT::i32, Chain,
  363. BasePtr, LD->getPointerInfo(), MVT::i16,
  364. LD->isVolatile(), LD->isNonTemporal(), 2);
  365. SDValue HighAddr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
  366. DAG.getConstant(2, MVT::i32));
  367. SDValue High = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
  368. HighAddr,
  369. LD->getPointerInfo().getWithOffset(2),
  370. MVT::i16, LD->isVolatile(),
  371. LD->isNonTemporal(), 2);
  372. SDValue HighShifted = DAG.getNode(ISD::SHL, DL, MVT::i32, High,
  373. DAG.getConstant(16, MVT::i32));
  374. SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i32, Low, HighShifted);
  375. Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Low.getValue(1),
  376. High.getValue(1));
  377. SDValue Ops[] = { Result, Chain };
  378. return DAG.getMergeValues(Ops, 2, DL);
  379. }
  380. // Lower to a call to __misaligned_load(BasePtr).
  381. Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
  382. TargetLowering::ArgListTy Args;
  383. TargetLowering::ArgListEntry Entry;
  384. Entry.Ty = IntPtrTy;
  385. Entry.Node = BasePtr;
  386. Args.push_back(Entry);
  387. TargetLowering::CallLoweringInfo CLI(Chain, IntPtrTy, false, false,
  388. false, false, 0, CallingConv::C, /*isTailCall=*/false,
  389. /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
  390. DAG.getExternalSymbol("__misaligned_load", getPointerTy()),
  391. Args, DAG, DL);
  392. std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
  393. SDValue Ops[] =
  394. { CallResult.first, CallResult.second };
  395. return DAG.getMergeValues(Ops, 2, DL);
  396. }
  397. SDValue XCoreTargetLowering::
  398. LowerSTORE(SDValue Op, SelectionDAG &DAG) const
  399. {
  400. StoreSDNode *ST = cast<StoreSDNode>(Op);
  401. assert(!ST->isTruncatingStore() && "Unexpected store type");
  402. assert(ST->getMemoryVT() == MVT::i32 && "Unexpected store EVT");
  403. if (allowsUnalignedMemoryAccesses(ST->getMemoryVT())) {
  404. return SDValue();
  405. }
  406. unsigned ABIAlignment = getDataLayout()->
  407. getABITypeAlignment(ST->getMemoryVT().getTypeForEVT(*DAG.getContext()));
  408. // Leave aligned store alone.
  409. if (ST->getAlignment() >= ABIAlignment) {
  410. return SDValue();
  411. }
  412. SDValue Chain = ST->getChain();
  413. SDValue BasePtr = ST->getBasePtr();
  414. SDValue Value = ST->getValue();
  415. SDLoc dl(Op);
  416. if (ST->getAlignment() == 2) {
  417. SDValue Low = Value;
  418. SDValue High = DAG.getNode(ISD::SRL, dl, MVT::i32, Value,
  419. DAG.getConstant(16, MVT::i32));
  420. SDValue StoreLow = DAG.getTruncStore(Chain, dl, Low, BasePtr,
  421. ST->getPointerInfo(), MVT::i16,
  422. ST->isVolatile(), ST->isNonTemporal(),
  423. 2);
  424. SDValue HighAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, BasePtr,
  425. DAG.getConstant(2, MVT::i32));
  426. SDValue StoreHigh = DAG.getTruncStore(Chain, dl, High, HighAddr,
  427. ST->getPointerInfo().getWithOffset(2),
  428. MVT::i16, ST->isVolatile(),
  429. ST->isNonTemporal(), 2);
  430. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, StoreLow, StoreHigh);
  431. }
  432. // Lower to a call to __misaligned_store(BasePtr, Value).
  433. Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
  434. TargetLowering::ArgListTy Args;
  435. TargetLowering::ArgListEntry Entry;
  436. Entry.Ty = IntPtrTy;
  437. Entry.Node = BasePtr;
  438. Args.push_back(Entry);
  439. Entry.Node = Value;
  440. Args.push_back(Entry);
  441. TargetLowering::CallLoweringInfo CLI(Chain,
  442. Type::getVoidTy(*DAG.getContext()), false, false,
  443. false, false, 0, CallingConv::C, /*isTailCall=*/false,
  444. /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
  445. DAG.getExternalSymbol("__misaligned_store", getPointerTy()),
  446. Args, DAG, dl);
  447. std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
  448. return CallResult.second;
  449. }
  450. SDValue XCoreTargetLowering::
  451. LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
  452. {
  453. assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::SMUL_LOHI &&
  454. "Unexpected operand to lower!");
  455. SDLoc dl(Op);
  456. SDValue LHS = Op.getOperand(0);
  457. SDValue RHS = Op.getOperand(1);
  458. SDValue Zero = DAG.getConstant(0, MVT::i32);
  459. SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
  460. DAG.getVTList(MVT::i32, MVT::i32), Zero, Zero,
  461. LHS, RHS);
  462. SDValue Lo(Hi.getNode(), 1);
  463. SDValue Ops[] = { Lo, Hi };
  464. return DAG.getMergeValues(Ops, 2, dl);
  465. }
  466. SDValue XCoreTargetLowering::
  467. LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const
  468. {
  469. assert(Op.getValueType() == MVT::i32 && Op.getOpcode() == ISD::UMUL_LOHI &&
  470. "Unexpected operand to lower!");
  471. SDLoc dl(Op);
  472. SDValue LHS = Op.getOperand(0);
  473. SDValue RHS = Op.getOperand(1);
  474. SDValue Zero = DAG.getConstant(0, MVT::i32);
  475. SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
  476. DAG.getVTList(MVT::i32, MVT::i32), LHS, RHS,
  477. Zero, Zero);
  478. SDValue Lo(Hi.getNode(), 1);
  479. SDValue Ops[] = { Lo, Hi };
  480. return DAG.getMergeValues(Ops, 2, dl);
  481. }
  482. /// isADDADDMUL - Return whether Op is in a form that is equivalent to
  483. /// add(add(mul(x,y),a),b). If requireIntermediatesHaveOneUse is true then
  484. /// each intermediate result in the calculation must also have a single use.
  485. /// If the Op is in the correct form the constituent parts are written to Mul0,
  486. /// Mul1, Addend0 and Addend1.
  487. static bool
  488. isADDADDMUL(SDValue Op, SDValue &Mul0, SDValue &Mul1, SDValue &Addend0,
  489. SDValue &Addend1, bool requireIntermediatesHaveOneUse)
  490. {
  491. if (Op.getOpcode() != ISD::ADD)
  492. return false;
  493. SDValue N0 = Op.getOperand(0);
  494. SDValue N1 = Op.getOperand(1);
  495. SDValue AddOp;
  496. SDValue OtherOp;
  497. if (N0.getOpcode() == ISD::ADD) {
  498. AddOp = N0;
  499. OtherOp = N1;
  500. } else if (N1.getOpcode() == ISD::ADD) {
  501. AddOp = N1;
  502. OtherOp = N0;
  503. } else {
  504. return false;
  505. }
  506. if (requireIntermediatesHaveOneUse && !AddOp.hasOneUse())
  507. return false;
  508. if (OtherOp.getOpcode() == ISD::MUL) {
  509. // add(add(a,b),mul(x,y))
  510. if (requireIntermediatesHaveOneUse && !OtherOp.hasOneUse())
  511. return false;
  512. Mul0 = OtherOp.getOperand(0);
  513. Mul1 = OtherOp.getOperand(1);
  514. Addend0 = AddOp.getOperand(0);
  515. Addend1 = AddOp.getOperand(1);
  516. return true;
  517. }
  518. if (AddOp.getOperand(0).getOpcode() == ISD::MUL) {
  519. // add(add(mul(x,y),a),b)
  520. if (requireIntermediatesHaveOneUse && !AddOp.getOperand(0).hasOneUse())
  521. return false;
  522. Mul0 = AddOp.getOperand(0).getOperand(0);
  523. Mul1 = AddOp.getOperand(0).getOperand(1);
  524. Addend0 = AddOp.getOperand(1);
  525. Addend1 = OtherOp;
  526. return true;
  527. }
  528. if (AddOp.getOperand(1).getOpcode() == ISD::MUL) {
  529. // add(add(a,mul(x,y)),b)
  530. if (requireIntermediatesHaveOneUse && !AddOp.getOperand(1).hasOneUse())
  531. return false;
  532. Mul0 = AddOp.getOperand(1).getOperand(0);
  533. Mul1 = AddOp.getOperand(1).getOperand(1);
  534. Addend0 = AddOp.getOperand(0);
  535. Addend1 = OtherOp;
  536. return true;
  537. }
  538. return false;
  539. }
  540. SDValue XCoreTargetLowering::
  541. TryExpandADDWithMul(SDNode *N, SelectionDAG &DAG) const
  542. {
  543. SDValue Mul;
  544. SDValue Other;
  545. if (N->getOperand(0).getOpcode() == ISD::MUL) {
  546. Mul = N->getOperand(0);
  547. Other = N->getOperand(1);
  548. } else if (N->getOperand(1).getOpcode() == ISD::MUL) {
  549. Mul = N->getOperand(1);
  550. Other = N->getOperand(0);
  551. } else {
  552. return SDValue();
  553. }
  554. SDLoc dl(N);
  555. SDValue LL, RL, AddendL, AddendH;
  556. LL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  557. Mul.getOperand(0), DAG.getConstant(0, MVT::i32));
  558. RL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  559. Mul.getOperand(1), DAG.getConstant(0, MVT::i32));
  560. AddendL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  561. Other, DAG.getConstant(0, MVT::i32));
  562. AddendH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  563. Other, DAG.getConstant(1, MVT::i32));
  564. APInt HighMask = APInt::getHighBitsSet(64, 32);
  565. unsigned LHSSB = DAG.ComputeNumSignBits(Mul.getOperand(0));
  566. unsigned RHSSB = DAG.ComputeNumSignBits(Mul.getOperand(1));
  567. if (DAG.MaskedValueIsZero(Mul.getOperand(0), HighMask) &&
  568. DAG.MaskedValueIsZero(Mul.getOperand(1), HighMask)) {
  569. // The inputs are both zero-extended.
  570. SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
  571. DAG.getVTList(MVT::i32, MVT::i32), AddendH,
  572. AddendL, LL, RL);
  573. SDValue Lo(Hi.getNode(), 1);
  574. return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
  575. }
  576. if (LHSSB > 32 && RHSSB > 32) {
  577. // The inputs are both sign-extended.
  578. SDValue Hi = DAG.getNode(XCoreISD::MACCS, dl,
  579. DAG.getVTList(MVT::i32, MVT::i32), AddendH,
  580. AddendL, LL, RL);
  581. SDValue Lo(Hi.getNode(), 1);
  582. return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
  583. }
  584. SDValue LH, RH;
  585. LH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  586. Mul.getOperand(0), DAG.getConstant(1, MVT::i32));
  587. RH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  588. Mul.getOperand(1), DAG.getConstant(1, MVT::i32));
  589. SDValue Hi = DAG.getNode(XCoreISD::MACCU, dl,
  590. DAG.getVTList(MVT::i32, MVT::i32), AddendH,
  591. AddendL, LL, RL);
  592. SDValue Lo(Hi.getNode(), 1);
  593. RH = DAG.getNode(ISD::MUL, dl, MVT::i32, LL, RH);
  594. LH = DAG.getNode(ISD::MUL, dl, MVT::i32, LH, RL);
  595. Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, RH);
  596. Hi = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, LH);
  597. return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
  598. }
  599. SDValue XCoreTargetLowering::
  600. ExpandADDSUB(SDNode *N, SelectionDAG &DAG) const
  601. {
  602. assert(N->getValueType(0) == MVT::i64 &&
  603. (N->getOpcode() == ISD::ADD || N->getOpcode() == ISD::SUB) &&
  604. "Unknown operand to lower!");
  605. if (N->getOpcode() == ISD::ADD) {
  606. SDValue Result = TryExpandADDWithMul(N, DAG);
  607. if (Result.getNode() != 0)
  608. return Result;
  609. }
  610. SDLoc dl(N);
  611. // Extract components
  612. SDValue LHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  613. N->getOperand(0), DAG.getConstant(0, MVT::i32));
  614. SDValue LHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  615. N->getOperand(0), DAG.getConstant(1, MVT::i32));
  616. SDValue RHSL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  617. N->getOperand(1), DAG.getConstant(0, MVT::i32));
  618. SDValue RHSH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  619. N->getOperand(1), DAG.getConstant(1, MVT::i32));
  620. // Expand
  621. unsigned Opcode = (N->getOpcode() == ISD::ADD) ? XCoreISD::LADD :
  622. XCoreISD::LSUB;
  623. SDValue Zero = DAG.getConstant(0, MVT::i32);
  624. SDValue Lo = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
  625. LHSL, RHSL, Zero);
  626. SDValue Carry(Lo.getNode(), 1);
  627. SDValue Hi = DAG.getNode(Opcode, dl, DAG.getVTList(MVT::i32, MVT::i32),
  628. LHSH, RHSH, Carry);
  629. SDValue Ignored(Hi.getNode(), 1);
  630. // Merge the pieces
  631. return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
  632. }
  633. SDValue XCoreTargetLowering::
  634. LowerVAARG(SDValue Op, SelectionDAG &DAG) const
  635. {
  636. llvm_unreachable("unimplemented");
  637. // FIXME Arguments passed by reference need a extra dereference.
  638. SDNode *Node = Op.getNode();
  639. SDLoc dl(Node);
  640. const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
  641. EVT VT = Node->getValueType(0);
  642. SDValue VAList = DAG.getLoad(getPointerTy(), dl, Node->getOperand(0),
  643. Node->getOperand(1), MachinePointerInfo(V),
  644. false, false, false, 0);
  645. // Increment the pointer, VAList, to the next vararg
  646. SDValue Tmp3 = DAG.getNode(ISD::ADD, dl, getPointerTy(), VAList,
  647. DAG.getConstant(VT.getSizeInBits(),
  648. getPointerTy()));
  649. // Store the incremented VAList to the legalized pointer
  650. Tmp3 = DAG.getStore(VAList.getValue(1), dl, Tmp3, Node->getOperand(1),
  651. MachinePointerInfo(V), false, false, 0);
  652. // Load the actual argument out of the pointer VAList
  653. return DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(),
  654. false, false, false, 0);
  655. }
  656. SDValue XCoreTargetLowering::
  657. LowerVASTART(SDValue Op, SelectionDAG &DAG) const
  658. {
  659. SDLoc dl(Op);
  660. // vastart stores the address of the VarArgsFrameIndex slot into the
  661. // memory location argument
  662. MachineFunction &MF = DAG.getMachineFunction();
  663. XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
  664. SDValue Addr = DAG.getFrameIndex(XFI->getVarArgsFrameIndex(), MVT::i32);
  665. return DAG.getStore(Op.getOperand(0), dl, Addr, Op.getOperand(1),
  666. MachinePointerInfo(), false, false, 0);
  667. }
  668. SDValue XCoreTargetLowering::LowerFRAMEADDR(SDValue Op,
  669. SelectionDAG &DAG) const {
  670. SDLoc dl(Op);
  671. // Depths > 0 not supported yet!
  672. if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
  673. return SDValue();
  674. MachineFunction &MF = DAG.getMachineFunction();
  675. const TargetRegisterInfo *RegInfo = getTargetMachine().getRegisterInfo();
  676. return DAG.getCopyFromReg(DAG.getEntryNode(), dl,
  677. RegInfo->getFrameRegister(MF), MVT::i32);
  678. }
  679. SDValue XCoreTargetLowering::
  680. LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
  681. return Op.getOperand(0);
  682. }
  683. SDValue XCoreTargetLowering::
  684. LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
  685. SDValue Chain = Op.getOperand(0);
  686. SDValue Trmp = Op.getOperand(1); // trampoline
  687. SDValue FPtr = Op.getOperand(2); // nested function
  688. SDValue Nest = Op.getOperand(3); // 'nest' parameter value
  689. const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
  690. // .align 4
  691. // LDAPF_u10 r11, nest
  692. // LDW_2rus r11, r11[0]
  693. // STWSP_ru6 r11, sp[0]
  694. // LDAPF_u10 r11, fptr
  695. // LDW_2rus r11, r11[0]
  696. // BAU_1r r11
  697. // nest:
  698. // .word nest
  699. // fptr:
  700. // .word fptr
  701. SDValue OutChains[5];
  702. SDValue Addr = Trmp;
  703. SDLoc dl(Op);
  704. OutChains[0] = DAG.getStore(Chain, dl, DAG.getConstant(0x0a3cd805, MVT::i32),
  705. Addr, MachinePointerInfo(TrmpAddr), false, false,
  706. 0);
  707. Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
  708. DAG.getConstant(4, MVT::i32));
  709. OutChains[1] = DAG.getStore(Chain, dl, DAG.getConstant(0xd80456c0, MVT::i32),
  710. Addr, MachinePointerInfo(TrmpAddr, 4), false,
  711. false, 0);
  712. Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
  713. DAG.getConstant(8, MVT::i32));
  714. OutChains[2] = DAG.getStore(Chain, dl, DAG.getConstant(0x27fb0a3c, MVT::i32),
  715. Addr, MachinePointerInfo(TrmpAddr, 8), false,
  716. false, 0);
  717. Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
  718. DAG.getConstant(12, MVT::i32));
  719. OutChains[3] = DAG.getStore(Chain, dl, Nest, Addr,
  720. MachinePointerInfo(TrmpAddr, 12), false, false,
  721. 0);
  722. Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
  723. DAG.getConstant(16, MVT::i32));
  724. OutChains[4] = DAG.getStore(Chain, dl, FPtr, Addr,
  725. MachinePointerInfo(TrmpAddr, 16), false, false,
  726. 0);
  727. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 5);
  728. }
  729. SDValue XCoreTargetLowering::
  730. LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
  731. SDLoc DL(Op);
  732. unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
  733. switch (IntNo) {
  734. case Intrinsic::xcore_crc8:
  735. EVT VT = Op.getValueType();
  736. SDValue Data =
  737. DAG.getNode(XCoreISD::CRC8, DL, DAG.getVTList(VT, VT),
  738. Op.getOperand(1), Op.getOperand(2) , Op.getOperand(3));
  739. SDValue Crc(Data.getNode(), 1);
  740. SDValue Results[] = { Crc, Data };
  741. return DAG.getMergeValues(Results, 2, DL);
  742. }
  743. return SDValue();
  744. }
  745. //===----------------------------------------------------------------------===//
  746. // Calling Convention Implementation
  747. //===----------------------------------------------------------------------===//
  748. #include "XCoreGenCallingConv.inc"
  749. //===----------------------------------------------------------------------===//
  750. // Call Calling Convention Implementation
  751. //===----------------------------------------------------------------------===//
  752. /// XCore call implementation
  753. SDValue
  754. XCoreTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
  755. SmallVectorImpl<SDValue> &InVals) const {
  756. SelectionDAG &DAG = CLI.DAG;
  757. SDLoc &dl = CLI.DL;
  758. SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
  759. SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
  760. SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
  761. SDValue Chain = CLI.Chain;
  762. SDValue Callee = CLI.Callee;
  763. bool &isTailCall = CLI.IsTailCall;
  764. CallingConv::ID CallConv = CLI.CallConv;
  765. bool isVarArg = CLI.IsVarArg;
  766. // XCore target does not yet support tail call optimization.
  767. isTailCall = false;
  768. // For now, only CallingConv::C implemented
  769. switch (CallConv)
  770. {
  771. default:
  772. llvm_unreachable("Unsupported calling convention");
  773. case CallingConv::Fast:
  774. case CallingConv::C:
  775. return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
  776. Outs, OutVals, Ins, dl, DAG, InVals);
  777. }
  778. }
  779. /// LowerCCCCallTo - functions arguments are copied from virtual
  780. /// regs to (physical regs)/(stack frame), CALLSEQ_START and
  781. /// CALLSEQ_END are emitted.
  782. /// TODO: isTailCall, sret.
  783. SDValue
  784. XCoreTargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
  785. CallingConv::ID CallConv, bool isVarArg,
  786. bool isTailCall,
  787. const SmallVectorImpl<ISD::OutputArg> &Outs,
  788. const SmallVectorImpl<SDValue> &OutVals,
  789. const SmallVectorImpl<ISD::InputArg> &Ins,
  790. SDLoc dl, SelectionDAG &DAG,
  791. SmallVectorImpl<SDValue> &InVals) const {
  792. // Analyze operands of the call, assigning locations to each operand.
  793. SmallVector<CCValAssign, 16> ArgLocs;
  794. CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
  795. getTargetMachine(), ArgLocs, *DAG.getContext());
  796. // The ABI dictates there should be one stack slot available to the callee
  797. // on function entry (for saving lr).
  798. CCInfo.AllocateStack(4, 4);
  799. CCInfo.AnalyzeCallOperands(Outs, CC_XCore);
  800. // Get a count of how many bytes are to be pushed on the stack.
  801. unsigned NumBytes = CCInfo.getNextStackOffset();
  802. Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes,
  803. getPointerTy(), true));
  804. SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
  805. SmallVector<SDValue, 12> MemOpChains;
  806. // Walk the register/memloc assignments, inserting copies/loads.
  807. for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
  808. CCValAssign &VA = ArgLocs[i];
  809. SDValue Arg = OutVals[i];
  810. // Promote the value if needed.
  811. switch (VA.getLocInfo()) {
  812. default: llvm_unreachable("Unknown loc info!");
  813. case CCValAssign::Full: break;
  814. case CCValAssign::SExt:
  815. Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
  816. break;
  817. case CCValAssign::ZExt:
  818. Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
  819. break;
  820. case CCValAssign::AExt:
  821. Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
  822. break;
  823. }
  824. // Arguments that can be passed on register must be kept at
  825. // RegsToPass vector
  826. if (VA.isRegLoc()) {
  827. RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
  828. } else {
  829. assert(VA.isMemLoc());
  830. int Offset = VA.getLocMemOffset();
  831. MemOpChains.push_back(DAG.getNode(XCoreISD::STWSP, dl, MVT::Other,
  832. Chain, Arg,
  833. DAG.getConstant(Offset/4, MVT::i32)));
  834. }
  835. }
  836. // Transform all store nodes into one single node because
  837. // all store nodes are independent of each other.
  838. if (!MemOpChains.empty())
  839. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  840. &MemOpChains[0], MemOpChains.size());
  841. // Build a sequence of copy-to-reg nodes chained together with token
  842. // chain and flag operands which copy the outgoing args into registers.
  843. // The InFlag in necessary since all emitted instructions must be
  844. // stuck together.
  845. SDValue InFlag;
  846. for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
  847. Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
  848. RegsToPass[i].second, InFlag);
  849. InFlag = Chain.getValue(1);
  850. }
  851. // If the callee is a GlobalAddress node (quite common, every direct call is)
  852. // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
  853. // Likewise ExternalSymbol -> TargetExternalSymbol.
  854. if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
  855. Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
  856. else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
  857. Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
  858. // XCoreBranchLink = #chain, #target_address, #opt_in_flags...
  859. // = Chain, Callee, Reg#1, Reg#2, ...
  860. //
  861. // Returns a chain & a flag for retval copy to use.
  862. SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
  863. SmallVector<SDValue, 8> Ops;
  864. Ops.push_back(Chain);
  865. Ops.push_back(Callee);
  866. // Add argument registers to the end of the list so that they are
  867. // known live into the call.
  868. for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
  869. Ops.push_back(DAG.getRegister(RegsToPass[i].first,
  870. RegsToPass[i].second.getValueType()));
  871. if (InFlag.getNode())
  872. Ops.push_back(InFlag);
  873. Chain = DAG.getNode(XCoreISD::BL, dl, NodeTys, &Ops[0], Ops.size());
  874. InFlag = Chain.getValue(1);
  875. // Create the CALLSEQ_END node.
  876. Chain = DAG.getCALLSEQ_END(Chain,
  877. DAG.getConstant(NumBytes, getPointerTy(), true),
  878. DAG.getConstant(0, getPointerTy(), true),
  879. InFlag);
  880. InFlag = Chain.getValue(1);
  881. // Handle result values, copying them out of physregs into vregs that we
  882. // return.
  883. return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
  884. Ins, dl, DAG, InVals);
  885. }
  886. /// LowerCallResult - Lower the result values of a call into the
  887. /// appropriate copies out of appropriate physical registers.
  888. SDValue
  889. XCoreTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
  890. CallingConv::ID CallConv, bool isVarArg,
  891. const SmallVectorImpl<ISD::InputArg> &Ins,
  892. SDLoc dl, SelectionDAG &DAG,
  893. SmallVectorImpl<SDValue> &InVals) const {
  894. // Assign locations to each value returned by this call.
  895. SmallVector<CCValAssign, 16> RVLocs;
  896. CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
  897. getTargetMachine(), RVLocs, *DAG.getContext());
  898. CCInfo.AnalyzeCallResult(Ins, RetCC_XCore);
  899. // Copy all of the result registers out of their specified physreg.
  900. for (unsigned i = 0; i != RVLocs.size(); ++i) {
  901. Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
  902. RVLocs[i].getValVT(), InFlag).getValue(1);
  903. InFlag = Chain.getValue(2);
  904. InVals.push_back(Chain.getValue(0));
  905. }
  906. return Chain;
  907. }
  908. //===----------------------------------------------------------------------===//
  909. // Formal Arguments Calling Convention Implementation
  910. //===----------------------------------------------------------------------===//
  911. /// XCore formal arguments implementation
  912. SDValue
  913. XCoreTargetLowering::LowerFormalArguments(SDValue Chain,
  914. CallingConv::ID CallConv,
  915. bool isVarArg,
  916. const SmallVectorImpl<ISD::InputArg> &Ins,
  917. SDLoc dl,
  918. SelectionDAG &DAG,
  919. SmallVectorImpl<SDValue> &InVals)
  920. const {
  921. switch (CallConv)
  922. {
  923. default:
  924. llvm_unreachable("Unsupported calling convention");
  925. case CallingConv::C:
  926. case CallingConv::Fast:
  927. return LowerCCCArguments(Chain, CallConv, isVarArg,
  928. Ins, dl, DAG, InVals);
  929. }
  930. }
  931. /// LowerCCCArguments - transform physical registers into
  932. /// virtual registers and generate load operations for
  933. /// arguments places on the stack.
  934. /// TODO: sret
  935. SDValue
  936. XCoreTargetLowering::LowerCCCArguments(SDValue Chain,
  937. CallingConv::ID CallConv,
  938. bool isVarArg,
  939. const SmallVectorImpl<ISD::InputArg>
  940. &Ins,
  941. SDLoc dl,
  942. SelectionDAG &DAG,
  943. SmallVectorImpl<SDValue> &InVals) const {
  944. MachineFunction &MF = DAG.getMachineFunction();
  945. MachineFrameInfo *MFI = MF.getFrameInfo();
  946. MachineRegisterInfo &RegInfo = MF.getRegInfo();
  947. // Assign locations to all of the incoming arguments.
  948. SmallVector<CCValAssign, 16> ArgLocs;
  949. CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
  950. getTargetMachine(), ArgLocs, *DAG.getContext());
  951. CCInfo.AnalyzeFormalArguments(Ins, CC_XCore);
  952. unsigned StackSlotSize = XCoreFrameLowering::stackSlotSize();
  953. unsigned LRSaveSize = StackSlotSize;
  954. for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
  955. CCValAssign &VA = ArgLocs[i];
  956. if (VA.isRegLoc()) {
  957. // Arguments passed in registers
  958. EVT RegVT = VA.getLocVT();
  959. switch (RegVT.getSimpleVT().SimpleTy) {
  960. default:
  961. {
  962. #ifndef NDEBUG
  963. errs() << "LowerFormalArguments Unhandled argument type: "
  964. << RegVT.getSimpleVT().SimpleTy << "\n";
  965. #endif
  966. llvm_unreachable(0);
  967. }
  968. case MVT::i32:
  969. unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
  970. RegInfo.addLiveIn(VA.getLocReg(), VReg);
  971. InVals.push_back(DAG.getCopyFromReg(Chain, dl, VReg, RegVT));
  972. }
  973. } else {
  974. // sanity check
  975. assert(VA.isMemLoc());
  976. // Load the argument to a virtual register
  977. unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
  978. if (ObjSize > StackSlotSize) {
  979. errs() << "LowerFormalArguments Unhandled argument type: "
  980. << EVT(VA.getLocVT()).getEVTString()
  981. << "\n";
  982. }
  983. // Create the frame index object for this incoming parameter...
  984. int FI = MFI->CreateFixedObject(ObjSize,
  985. LRSaveSize + VA.getLocMemOffset(),
  986. true);
  987. // Create the SelectionDAG nodes corresponding to a load
  988. //from this parameter
  989. SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
  990. InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
  991. MachinePointerInfo::getFixedStack(FI),
  992. false, false, false, 0));
  993. }
  994. }
  995. if (isVarArg) {
  996. /* Argument registers */
  997. static const uint16_t ArgRegs[] = {
  998. XCore::R0, XCore::R1, XCore::R2, XCore::R3
  999. };
  1000. XCoreFunctionInfo *XFI = MF.getInfo<XCoreFunctionInfo>();
  1001. unsigned FirstVAReg = CCInfo.getFirstUnallocated(ArgRegs,
  1002. array_lengthof(ArgRegs));
  1003. if (FirstVAReg < array_lengthof(ArgRegs)) {
  1004. SmallVector<SDValue, 4> MemOps;
  1005. int offset = 0;
  1006. // Save remaining registers, storing higher register numbers at a higher
  1007. // address
  1008. for (int i = array_lengthof(ArgRegs) - 1; i >= (int)FirstVAReg; --i) {
  1009. // Create a stack slot
  1010. int FI = MFI->CreateFixedObject(4, offset, true);
  1011. if (i == (int)FirstVAReg) {
  1012. XFI->setVarArgsFrameIndex(FI);
  1013. }
  1014. offset -= StackSlotSize;
  1015. SDValue FIN = DAG.getFrameIndex(FI, MVT::i32);
  1016. // Move argument from phys reg -> virt reg
  1017. unsigned VReg = RegInfo.createVirtualRegister(&XCore::GRRegsRegClass);
  1018. RegInfo.addLiveIn(ArgRegs[i], VReg);
  1019. SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
  1020. // Move argument from virt reg -> stack
  1021. SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
  1022. MachinePointerInfo(), false, false, 0);
  1023. MemOps.push_back(Store);
  1024. }
  1025. if (!MemOps.empty())
  1026. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  1027. &MemOps[0], MemOps.size());
  1028. } else {
  1029. // This will point to the next argument passed via stack.
  1030. XFI->setVarArgsFrameIndex(
  1031. MFI->CreateFixedObject(4, LRSaveSize + CCInfo.getNextStackOffset(),
  1032. true));
  1033. }
  1034. }
  1035. return Chain;
  1036. }
  1037. //===----------------------------------------------------------------------===//
  1038. // Return Value Calling Convention Implementation
  1039. //===----------------------------------------------------------------------===//
  1040. bool XCoreTargetLowering::
  1041. CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
  1042. bool isVarArg,
  1043. const SmallVectorImpl<ISD::OutputArg> &Outs,
  1044. LLVMContext &Context) const {
  1045. SmallVector<CCValAssign, 16> RVLocs;
  1046. CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
  1047. return CCInfo.CheckReturn(Outs, RetCC_XCore);
  1048. }
  1049. SDValue
  1050. XCoreTargetLowering::LowerReturn(SDValue Chain,
  1051. CallingConv::ID CallConv, bool isVarArg,
  1052. const SmallVectorImpl<ISD::OutputArg> &Outs,
  1053. const SmallVectorImpl<SDValue> &OutVals,
  1054. SDLoc dl, SelectionDAG &DAG) const {
  1055. // CCValAssign - represent the assignment of
  1056. // the return value to a location
  1057. SmallVector<CCValAssign, 16> RVLocs;
  1058. // CCState - Info about the registers and stack slot.
  1059. CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
  1060. getTargetMachine(), RVLocs, *DAG.getContext());
  1061. // Analyze return values.
  1062. CCInfo.AnalyzeReturn(Outs, RetCC_XCore);
  1063. SDValue Flag;
  1064. SmallVector<SDValue, 4> RetOps(1, Chain);
  1065. // Return on XCore is always a "retsp 0"
  1066. RetOps.push_back(DAG.getConstant(0, MVT::i32));
  1067. // Copy the result values into the output registers.
  1068. for (unsigned i = 0; i != RVLocs.size(); ++i) {
  1069. CCValAssign &VA = RVLocs[i];
  1070. assert(VA.isRegLoc() && "Can only return in registers!");
  1071. Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
  1072. OutVals[i], Flag);
  1073. // guarantee that all emitted copies are
  1074. // stuck together, avoiding something bad
  1075. Flag = Chain.getValue(1);
  1076. RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
  1077. }
  1078. RetOps[0] = Chain; // Update chain.
  1079. // Add the flag if we have it.
  1080. if (Flag.getNode())
  1081. RetOps.push_back(Flag);
  1082. return DAG.getNode(XCoreISD::RETSP, dl, MVT::Other,
  1083. &RetOps[0], RetOps.size());
  1084. }
  1085. //===----------------------------------------------------------------------===//
  1086. // Other Lowering Code
  1087. //===----------------------------------------------------------------------===//
  1088. MachineBasicBlock *
  1089. XCoreTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
  1090. MachineBasicBlock *BB) const {
  1091. const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
  1092. DebugLoc dl = MI->getDebugLoc();
  1093. assert((MI->getOpcode() == XCore::SELECT_CC) &&
  1094. "Unexpected instr type to insert");
  1095. // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
  1096. // control-flow pattern. The incoming instruction knows the destination vreg
  1097. // to set, the condition code register to branch on, the true/false values to
  1098. // select between, and a branch opcode to use.
  1099. const BasicBlock *LLVM_BB = BB->getBasicBlock();
  1100. MachineFunction::iterator It = BB;
  1101. ++It;
  1102. // thisMBB:
  1103. // ...
  1104. // TrueVal = ...
  1105. // cmpTY ccX, r1, r2
  1106. // bCC copy1MBB
  1107. // fallthrough --> copy0MBB
  1108. MachineBasicBlock *thisMBB = BB;
  1109. MachineFunction *F = BB->getParent();
  1110. MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
  1111. MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
  1112. F->insert(It, copy0MBB);
  1113. F->insert(It, sinkMBB);
  1114. // Transfer the remainder of BB and its successor edges to sinkMBB.
  1115. sinkMBB->splice(sinkMBB->begin(), BB,
  1116. llvm::next(MachineBasicBlock::iterator(MI)),
  1117. BB->end());
  1118. sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
  1119. // Next, add the true and fallthrough blocks as its successors.
  1120. BB->addSuccessor(copy0MBB);
  1121. BB->addSuccessor(sinkMBB);
  1122. BuildMI(BB, dl, TII.get(XCore::BRFT_lru6))
  1123. .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
  1124. // copy0MBB:
  1125. // %FalseValue = ...
  1126. // # fallthrough to sinkMBB
  1127. BB = copy0MBB;
  1128. // Update machine-CFG edges
  1129. BB->addSuccessor(sinkMBB);
  1130. // sinkMBB:
  1131. // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
  1132. // ...
  1133. BB = sinkMBB;
  1134. BuildMI(*BB, BB->begin(), dl,
  1135. TII.get(XCore::PHI), MI->getOperand(0).getReg())
  1136. .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
  1137. .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
  1138. MI->eraseFromParent(); // The pseudo instruction is gone now.
  1139. return BB;
  1140. }
  1141. //===----------------------------------------------------------------------===//
  1142. // Target Optimization Hooks
  1143. //===----------------------------------------------------------------------===//
  1144. SDValue XCoreTargetLowering::PerformDAGCombine(SDNode *N,
  1145. DAGCombinerInfo &DCI) const {
  1146. SelectionDAG &DAG = DCI.DAG;
  1147. SDLoc dl(N);
  1148. switch (N->getOpcode()) {
  1149. default: break;
  1150. case XCoreISD::LADD: {
  1151. SDValue N0 = N->getOperand(0);
  1152. SDValue N1 = N->getOperand(1);
  1153. SDValue N2 = N->getOperand(2);
  1154. ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
  1155. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
  1156. EVT VT = N0.getValueType();
  1157. // canonicalize constant to RHS
  1158. if (N0C && !N1C)
  1159. return DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N1, N0, N2);
  1160. // fold (ladd 0, 0, x) -> 0, x & 1
  1161. if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
  1162. SDValue Carry = DAG.getConstant(0, VT);
  1163. SDValue Result = DAG.getNode(ISD::AND, dl, VT, N2,
  1164. DAG.getConstant(1, VT));
  1165. SDValue Ops[] = { Result, Carry };
  1166. return DAG.getMergeValues(Ops, 2, dl);
  1167. }
  1168. // fold (ladd x, 0, y) -> 0, add x, y iff carry is unused and y has only the
  1169. // low bit set
  1170. if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
  1171. APInt KnownZero, KnownOne;
  1172. APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
  1173. VT.getSizeInBits() - 1);
  1174. DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
  1175. if ((KnownZero & Mask) == Mask) {
  1176. SDValue Carry = DAG.getConstant(0, VT);
  1177. SDValue Result = DAG.getNode(ISD::ADD, dl, VT, N0, N2);
  1178. SDValue Ops[] = { Result, Carry };
  1179. return DAG.getMergeValues(Ops, 2, dl);
  1180. }
  1181. }
  1182. }
  1183. break;
  1184. case XCoreISD::LSUB: {
  1185. SDValue N0 = N->getOperand(0);
  1186. SDValue N1 = N->getOperand(1);
  1187. SDValue N2 = N->getOperand(2);
  1188. ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
  1189. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
  1190. EVT VT = N0.getValueType();
  1191. // fold (lsub 0, 0, x) -> x, -x iff x has only the low bit set
  1192. if (N0C && N0C->isNullValue() && N1C && N1C->isNullValue()) {
  1193. APInt KnownZero, KnownOne;
  1194. APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
  1195. VT.getSizeInBits() - 1);
  1196. DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
  1197. if ((KnownZero & Mask) == Mask) {
  1198. SDValue Borrow = N2;
  1199. SDValue Result = DAG.getNode(ISD::SUB, dl, VT,
  1200. DAG.getConstant(0, VT), N2);
  1201. SDValue Ops[] = { Result, Borrow };
  1202. return DAG.getMergeValues(Ops, 2, dl);
  1203. }
  1204. }
  1205. // fold (lsub x, 0, y) -> 0, sub x, y iff borrow is unused and y has only the
  1206. // low bit set
  1207. if (N1C && N1C->isNullValue() && N->hasNUsesOfValue(0, 1)) {
  1208. APInt KnownZero, KnownOne;
  1209. APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
  1210. VT.getSizeInBits() - 1);
  1211. DAG.ComputeMaskedBits(N2, KnownZero, KnownOne);
  1212. if ((KnownZero & Mask) == Mask) {
  1213. SDValue Borrow = DAG.getConstant(0, VT);
  1214. SDValue Result = DAG.getNode(ISD::SUB, dl, VT, N0, N2);
  1215. SDValue Ops[] = { Result, Borrow };
  1216. return DAG.getMergeValues(Ops, 2, dl);
  1217. }
  1218. }
  1219. }
  1220. break;
  1221. case XCoreISD::LMUL: {
  1222. SDValue N0 = N->getOperand(0);
  1223. SDValue N1 = N->getOperand(1);
  1224. SDValue N2 = N->getOperand(2);
  1225. SDValue N3 = N->getOperand(3);
  1226. ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
  1227. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
  1228. EVT VT = N0.getValueType();
  1229. // Canonicalize multiplicative constant to RHS. If both multiplicative
  1230. // operands are constant canonicalize smallest to RHS.
  1231. if ((N0C && !N1C) ||
  1232. (N0C && N1C && N0C->getZExtValue() < N1C->getZExtValue()))
  1233. return DAG.getNode(XCoreISD::LMUL, dl, DAG.getVTList(VT, VT),
  1234. N1, N0, N2, N3);
  1235. // lmul(x, 0, a, b)
  1236. if (N1C && N1C->isNullValue()) {
  1237. // If the high result is unused fold to add(a, b)
  1238. if (N->hasNUsesOfValue(0, 0)) {
  1239. SDValue Lo = DAG.getNode(ISD::ADD, dl, VT, N2, N3);
  1240. SDValue Ops[] = { Lo, Lo };
  1241. return DAG.getMergeValues(Ops, 2, dl);
  1242. }
  1243. // Otherwise fold to ladd(a, b, 0)
  1244. SDValue Result =
  1245. DAG.getNode(XCoreISD::LADD, dl, DAG.getVTList(VT, VT), N2, N3, N1);
  1246. SDValue Carry(Result.getNode(), 1);
  1247. SDValue Ops[] = { Carry, Result };
  1248. return DAG.getMergeValues(Ops, 2, dl);
  1249. }
  1250. }
  1251. break;
  1252. case ISD::ADD: {
  1253. // Fold 32 bit expressions such as add(add(mul(x,y),a),b) ->
  1254. // lmul(x, y, a, b). The high result of lmul will be ignored.
  1255. // This is only profitable if the intermediate results are unused
  1256. // elsewhere.
  1257. SDValue Mul0, Mul1, Addend0, Addend1;
  1258. if (N->getValueType(0) == MVT::i32 &&
  1259. isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, true)) {
  1260. SDValue Ignored = DAG.getNode(XCoreISD::LMUL, dl,
  1261. DAG.getVTList(MVT::i32, MVT::i32), Mul0,
  1262. Mul1, Addend0, Addend1);
  1263. SDValue Result(Ignored.getNode(), 1);
  1264. return Result;
  1265. }
  1266. APInt HighMask = APInt::getHighBitsSet(64, 32);
  1267. // Fold 64 bit expression such as add(add(mul(x,y),a),b) ->
  1268. // lmul(x, y, a, b) if all operands are zero-extended. We do this
  1269. // before type legalization as it is messy to match the operands after
  1270. // that.
  1271. if (N->getValueType(0) == MVT::i64 &&
  1272. isADDADDMUL(SDValue(N, 0), Mul0, Mul1, Addend0, Addend1, false) &&
  1273. DAG.MaskedValueIsZero(Mul0, HighMask) &&
  1274. DAG.MaskedValueIsZero(Mul1, HighMask) &&
  1275. DAG.MaskedValueIsZero(Addend0, HighMask) &&
  1276. DAG.MaskedValueIsZero(Addend1, HighMask)) {
  1277. SDValue Mul0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  1278. Mul0, DAG.getConstant(0, MVT::i32));
  1279. SDValue Mul1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  1280. Mul1, DAG.getConstant(0, MVT::i32));
  1281. SDValue Addend0L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  1282. Addend0, DAG.getConstant(0, MVT::i32));
  1283. SDValue Addend1L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
  1284. Addend1, DAG.getConstant(0, MVT::i32));
  1285. SDValue Hi = DAG.getNode(XCoreISD::LMUL, dl,
  1286. DAG.getVTList(MVT::i32, MVT::i32), Mul0L, Mul1L,
  1287. Addend0L, Addend1L);
  1288. SDValue Lo(Hi.getNode(), 1);
  1289. return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
  1290. }
  1291. }
  1292. break;
  1293. case ISD::STORE: {
  1294. // Replace unaligned store of unaligned load with memmove.
  1295. StoreSDNode *ST = cast<StoreSDNode>(N);
  1296. if (!DCI.isBeforeLegalize() ||
  1297. allowsUnalignedMemoryAccesses(ST->getMemoryVT()) ||
  1298. ST->isVolatile() || ST->isIndexed()) {
  1299. break;
  1300. }
  1301. SDValue Chain = ST->getChain();
  1302. unsigned StoreBits = ST->getMemoryVT().getStoreSizeInBits();
  1303. if (StoreBits % 8) {
  1304. break;
  1305. }
  1306. unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(
  1307. ST->getMemoryVT().getTypeForEVT(*DCI.DAG.getContext()));
  1308. unsigned Alignment = ST->getAlignment();
  1309. if (Alignment >= ABIAlignment) {
  1310. break;
  1311. }
  1312. if (LoadSDNode *LD = dyn_cast<LoadSDNode>(ST->getValue())) {
  1313. if (LD->hasNUsesOfValue(1, 0) && ST->getMemoryVT() == LD->getMemoryVT() &&
  1314. LD->getAlignment() == Alignment &&
  1315. !LD->isVolatile() && !LD->isIndexed() &&
  1316. Chain.reachesChainWithoutSideEffects(SDValue(LD, 1))) {
  1317. return DAG.getMemmove(Chain, dl, ST->getBasePtr(),
  1318. LD->getBasePtr(),
  1319. DAG.getConstant(StoreBits/8, MVT::i32),
  1320. Alignment, false, ST->getPointerInfo(),
  1321. LD->getPointerInfo());
  1322. }
  1323. }
  1324. break;
  1325. }
  1326. }
  1327. return SDValue();
  1328. }
  1329. void XCoreTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
  1330. APInt &KnownZero,
  1331. APInt &KnownOne,
  1332. const SelectionDAG &DAG,
  1333. unsigned Depth) const {
  1334. KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
  1335. switch (Op.getOpcode()) {
  1336. default: break;
  1337. case XCoreISD::LADD:
  1338. case XCoreISD::LSUB:
  1339. if (Op.getResNo() == 1) {
  1340. // Top bits of carry / borrow are clear.
  1341. KnownZero = APInt::getHighBitsSet(KnownZero.getBitWidth(),
  1342. KnownZero.getBitWidth() - 1);
  1343. }
  1344. break;
  1345. }
  1346. }
  1347. //===----------------------------------------------------------------------===//
  1348. // Addressing mode description hooks
  1349. //===----------------------------------------------------------------------===//
  1350. static inline bool isImmUs(int64_t val)
  1351. {
  1352. return (val >= 0 && val <= 11);
  1353. }
  1354. static inline bool isImmUs2(int64_t val)
  1355. {
  1356. return (val%2 == 0 && isImmUs(val/2));
  1357. }
  1358. static inline bool isImmUs4(int64_t val)
  1359. {
  1360. return (val%4 == 0 && isImmUs(val/4));
  1361. }
  1362. /// isLegalAddressingMode - Return true if the addressing mode represented
  1363. /// by AM is legal for this target, for a load/store of the specified type.
  1364. bool
  1365. XCoreTargetLowering::isLegalAddressingMode(const AddrMode &AM,
  1366. Type *Ty) const {
  1367. if (Ty->getTypeID() == Type::VoidTyID)
  1368. return AM.Scale == 0 && isImmUs(AM.BaseOffs) && isImmUs4(AM.BaseOffs);
  1369. const DataLayout *TD = TM.getDataLayout();
  1370. unsigned Size = TD->getTypeAllocSize(Ty);
  1371. if (AM.BaseGV) {
  1372. return Size >= 4 && !AM.HasBaseReg && AM.Scale == 0 &&
  1373. AM.BaseOffs%4 == 0;
  1374. }
  1375. switch (Size) {
  1376. case 1:
  1377. // reg + imm
  1378. if (AM.Scale == 0) {
  1379. return isImmUs(AM.BaseOffs);
  1380. }
  1381. // reg + reg
  1382. return AM.Scale == 1 && AM.BaseOffs == 0;
  1383. case 2:
  1384. case 3:
  1385. // reg + imm
  1386. if (AM.Scale == 0) {
  1387. return isImmUs2(AM.BaseOffs);
  1388. }
  1389. // reg + reg<<1
  1390. return AM.Scale == 2 && AM.BaseOffs == 0;
  1391. default:
  1392. // reg + imm
  1393. if (AM.Scale == 0) {
  1394. return isImmUs4(AM.BaseOffs);
  1395. }
  1396. // reg + reg<<2
  1397. return AM.Scale == 4 && AM.BaseOffs == 0;
  1398. }
  1399. }
  1400. //===----------------------------------------------------------------------===//
  1401. // XCore Inline Assembly Support
  1402. //===----------------------------------------------------------------------===//
  1403. std::pair<unsigned, const TargetRegisterClass*>
  1404. XCoreTargetLowering::
  1405. getRegForInlineAsmConstraint(const std::string &Constraint,
  1406. EVT VT) const {
  1407. if (Constraint.size() == 1) {
  1408. switch (Constraint[0]) {
  1409. default : break;
  1410. case 'r':
  1411. return std::make_pair(0U, &XCore::GRRegsRegClass);
  1412. }
  1413. }
  1414. // Use the default implementation in TargetLowering to convert the register
  1415. // constraint into a member of a register class.
  1416. return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
  1417. }