PPCFastISel.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. //===-- PPCFastISel.cpp - PowerPC FastISel implementation -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the PowerPC-specific support for the FastISel class. Some
  10. // of the target-specific code is generated by tablegen in the file
  11. // PPCGenFastISel.inc, which is #included here.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "MCTargetDesc/PPCPredicates.h"
  15. #include "PPC.h"
  16. #include "PPCCCState.h"
  17. #include "PPCCallingConv.h"
  18. #include "PPCISelLowering.h"
  19. #include "PPCMachineFunctionInfo.h"
  20. #include "PPCSubtarget.h"
  21. #include "PPCTargetMachine.h"
  22. #include "llvm/CodeGen/CallingConvLower.h"
  23. #include "llvm/CodeGen/FastISel.h"
  24. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  25. #include "llvm/CodeGen/MachineConstantPool.h"
  26. #include "llvm/CodeGen/MachineFrameInfo.h"
  27. #include "llvm/CodeGen/MachineInstrBuilder.h"
  28. #include "llvm/CodeGen/MachineRegisterInfo.h"
  29. #include "llvm/CodeGen/TargetLowering.h"
  30. #include "llvm/IR/CallingConv.h"
  31. #include "llvm/IR/GetElementPtrTypeIterator.h"
  32. #include "llvm/IR/GlobalAlias.h"
  33. #include "llvm/IR/GlobalVariable.h"
  34. #include "llvm/IR/IntrinsicInst.h"
  35. #include "llvm/IR/Operator.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Target/TargetMachine.h"
  38. //===----------------------------------------------------------------------===//
  39. //
  40. // TBD:
  41. // fastLowerArguments: Handle simple cases.
  42. // PPCMaterializeGV: Handle TLS.
  43. // SelectCall: Handle function pointers.
  44. // SelectCall: Handle multi-register return values.
  45. // SelectCall: Optimize away nops for local calls.
  46. // processCallArgs: Handle bit-converted arguments.
  47. // finishCall: Handle multi-register return values.
  48. // PPCComputeAddress: Handle parameter references as FrameIndex's.
  49. // PPCEmitCmp: Handle immediate as operand 1.
  50. // SelectCall: Handle small byval arguments.
  51. // SelectIntrinsicCall: Implement.
  52. // SelectSelect: Implement.
  53. // Consider factoring isTypeLegal into the base class.
  54. // Implement switches and jump tables.
  55. //
  56. //===----------------------------------------------------------------------===//
  57. using namespace llvm;
  58. #define DEBUG_TYPE "ppcfastisel"
  59. namespace {
  60. struct Address {
  61. enum {
  62. RegBase,
  63. FrameIndexBase
  64. } BaseType;
  65. union {
  66. unsigned Reg;
  67. int FI;
  68. } Base;
  69. int64_t Offset;
  70. // Innocuous defaults for our address.
  71. Address()
  72. : BaseType(RegBase), Offset(0) {
  73. Base.Reg = 0;
  74. }
  75. };
  76. class PPCFastISel final : public FastISel {
  77. const TargetMachine &TM;
  78. const PPCSubtarget *Subtarget;
  79. PPCFunctionInfo *PPCFuncInfo;
  80. const TargetInstrInfo &TII;
  81. const TargetLowering &TLI;
  82. LLVMContext *Context;
  83. public:
  84. explicit PPCFastISel(FunctionLoweringInfo &FuncInfo,
  85. const TargetLibraryInfo *LibInfo)
  86. : FastISel(FuncInfo, LibInfo), TM(FuncInfo.MF->getTarget()),
  87. Subtarget(&FuncInfo.MF->getSubtarget<PPCSubtarget>()),
  88. PPCFuncInfo(FuncInfo.MF->getInfo<PPCFunctionInfo>()),
  89. TII(*Subtarget->getInstrInfo()), TLI(*Subtarget->getTargetLowering()),
  90. Context(&FuncInfo.Fn->getContext()) {}
  91. // Backend specific FastISel code.
  92. private:
  93. bool fastSelectInstruction(const Instruction *I) override;
  94. unsigned fastMaterializeConstant(const Constant *C) override;
  95. unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
  96. bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
  97. const LoadInst *LI) override;
  98. bool fastLowerArguments() override;
  99. unsigned fastEmit_i(MVT Ty, MVT RetTy, unsigned Opc, uint64_t Imm) override;
  100. unsigned fastEmitInst_ri(unsigned MachineInstOpcode,
  101. const TargetRegisterClass *RC,
  102. unsigned Op0, uint64_t Imm);
  103. unsigned fastEmitInst_r(unsigned MachineInstOpcode,
  104. const TargetRegisterClass *RC, unsigned Op0);
  105. unsigned fastEmitInst_rr(unsigned MachineInstOpcode,
  106. const TargetRegisterClass *RC,
  107. unsigned Op0, unsigned Op1);
  108. bool fastLowerCall(CallLoweringInfo &CLI) override;
  109. // Instruction selection routines.
  110. private:
  111. bool SelectLoad(const Instruction *I);
  112. bool SelectStore(const Instruction *I);
  113. bool SelectBranch(const Instruction *I);
  114. bool SelectIndirectBr(const Instruction *I);
  115. bool SelectFPExt(const Instruction *I);
  116. bool SelectFPTrunc(const Instruction *I);
  117. bool SelectIToFP(const Instruction *I, bool IsSigned);
  118. bool SelectFPToI(const Instruction *I, bool IsSigned);
  119. bool SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode);
  120. bool SelectRet(const Instruction *I);
  121. bool SelectTrunc(const Instruction *I);
  122. bool SelectIntExt(const Instruction *I);
  123. // Utility routines.
  124. private:
  125. bool isTypeLegal(Type *Ty, MVT &VT);
  126. bool isLoadTypeLegal(Type *Ty, MVT &VT);
  127. bool isValueAvailable(const Value *V) const;
  128. bool isVSFRCRegClass(const TargetRegisterClass *RC) const {
  129. return RC->getID() == PPC::VSFRCRegClassID;
  130. }
  131. bool isVSSRCRegClass(const TargetRegisterClass *RC) const {
  132. return RC->getID() == PPC::VSSRCRegClassID;
  133. }
  134. unsigned copyRegToRegClass(const TargetRegisterClass *ToRC,
  135. unsigned SrcReg, unsigned Flag = 0,
  136. unsigned SubReg = 0) {
  137. Register TmpReg = createResultReg(ToRC);
  138. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  139. TII.get(TargetOpcode::COPY), TmpReg).addReg(SrcReg, Flag, SubReg);
  140. return TmpReg;
  141. }
  142. bool PPCEmitCmp(const Value *Src1Value, const Value *Src2Value,
  143. bool isZExt, unsigned DestReg,
  144. const PPC::Predicate Pred);
  145. bool PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
  146. const TargetRegisterClass *RC, bool IsZExt = true,
  147. unsigned FP64LoadOpc = PPC::LFD);
  148. bool PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr);
  149. bool PPCComputeAddress(const Value *Obj, Address &Addr);
  150. void PPCSimplifyAddress(Address &Addr, bool &UseOffset,
  151. unsigned &IndexReg);
  152. bool PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
  153. unsigned DestReg, bool IsZExt);
  154. unsigned PPCMaterializeFP(const ConstantFP *CFP, MVT VT);
  155. unsigned PPCMaterializeGV(const GlobalValue *GV, MVT VT);
  156. unsigned PPCMaterializeInt(const ConstantInt *CI, MVT VT,
  157. bool UseSExt = true);
  158. unsigned PPCMaterialize32BitInt(int64_t Imm,
  159. const TargetRegisterClass *RC);
  160. unsigned PPCMaterialize64BitInt(int64_t Imm,
  161. const TargetRegisterClass *RC);
  162. unsigned PPCMoveToIntReg(const Instruction *I, MVT VT,
  163. unsigned SrcReg, bool IsSigned);
  164. unsigned PPCMoveToFPReg(MVT VT, unsigned SrcReg, bool IsSigned);
  165. // Call handling routines.
  166. private:
  167. bool processCallArgs(SmallVectorImpl<Value*> &Args,
  168. SmallVectorImpl<unsigned> &ArgRegs,
  169. SmallVectorImpl<MVT> &ArgVTs,
  170. SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
  171. SmallVectorImpl<unsigned> &RegArgs,
  172. CallingConv::ID CC,
  173. unsigned &NumBytes,
  174. bool IsVarArg);
  175. bool finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes);
  176. private:
  177. #include "PPCGenFastISel.inc"
  178. };
  179. } // end anonymous namespace
  180. static std::optional<PPC::Predicate> getComparePred(CmpInst::Predicate Pred) {
  181. switch (Pred) {
  182. // These are not representable with any single compare.
  183. case CmpInst::FCMP_FALSE:
  184. case CmpInst::FCMP_TRUE:
  185. // Major concern about the following 6 cases is NaN result. The comparison
  186. // result consists of 4 bits, indicating lt, eq, gt and un (unordered),
  187. // only one of which will be set. The result is generated by fcmpu
  188. // instruction. However, bc instruction only inspects one of the first 3
  189. // bits, so when un is set, bc instruction may jump to an undesired
  190. // place.
  191. //
  192. // More specifically, if we expect an unordered comparison and un is set, we
  193. // expect to always go to true branch; in such case UEQ, UGT and ULT still
  194. // give false, which are undesired; but UNE, UGE, ULE happen to give true,
  195. // since they are tested by inspecting !eq, !lt, !gt, respectively.
  196. //
  197. // Similarly, for ordered comparison, when un is set, we always expect the
  198. // result to be false. In such case OGT, OLT and OEQ is good, since they are
  199. // actually testing GT, LT, and EQ respectively, which are false. OGE, OLE
  200. // and ONE are tested through !lt, !gt and !eq, and these are true.
  201. case CmpInst::FCMP_UEQ:
  202. case CmpInst::FCMP_UGT:
  203. case CmpInst::FCMP_ULT:
  204. case CmpInst::FCMP_OGE:
  205. case CmpInst::FCMP_OLE:
  206. case CmpInst::FCMP_ONE:
  207. default:
  208. return std::nullopt;
  209. case CmpInst::FCMP_OEQ:
  210. case CmpInst::ICMP_EQ:
  211. return PPC::PRED_EQ;
  212. case CmpInst::FCMP_OGT:
  213. case CmpInst::ICMP_UGT:
  214. case CmpInst::ICMP_SGT:
  215. return PPC::PRED_GT;
  216. case CmpInst::FCMP_UGE:
  217. case CmpInst::ICMP_UGE:
  218. case CmpInst::ICMP_SGE:
  219. return PPC::PRED_GE;
  220. case CmpInst::FCMP_OLT:
  221. case CmpInst::ICMP_ULT:
  222. case CmpInst::ICMP_SLT:
  223. return PPC::PRED_LT;
  224. case CmpInst::FCMP_ULE:
  225. case CmpInst::ICMP_ULE:
  226. case CmpInst::ICMP_SLE:
  227. return PPC::PRED_LE;
  228. case CmpInst::FCMP_UNE:
  229. case CmpInst::ICMP_NE:
  230. return PPC::PRED_NE;
  231. case CmpInst::FCMP_ORD:
  232. return PPC::PRED_NU;
  233. case CmpInst::FCMP_UNO:
  234. return PPC::PRED_UN;
  235. }
  236. }
  237. // Determine whether the type Ty is simple enough to be handled by
  238. // fast-isel, and return its equivalent machine type in VT.
  239. // FIXME: Copied directly from ARM -- factor into base class?
  240. bool PPCFastISel::isTypeLegal(Type *Ty, MVT &VT) {
  241. EVT Evt = TLI.getValueType(DL, Ty, true);
  242. // Only handle simple types.
  243. if (Evt == MVT::Other || !Evt.isSimple()) return false;
  244. VT = Evt.getSimpleVT();
  245. // Handle all legal types, i.e. a register that will directly hold this
  246. // value.
  247. return TLI.isTypeLegal(VT);
  248. }
  249. // Determine whether the type Ty is simple enough to be handled by
  250. // fast-isel as a load target, and return its equivalent machine type in VT.
  251. bool PPCFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) {
  252. if (isTypeLegal(Ty, VT)) return true;
  253. // If this is a type than can be sign or zero-extended to a basic operation
  254. // go ahead and accept it now.
  255. if (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. bool PPCFastISel::isValueAvailable(const Value *V) const {
  261. if (!isa<Instruction>(V))
  262. return true;
  263. const auto *I = cast<Instruction>(V);
  264. return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
  265. }
  266. // Given a value Obj, create an Address object Addr that represents its
  267. // address. Return false if we can't handle it.
  268. bool PPCFastISel::PPCComputeAddress(const Value *Obj, Address &Addr) {
  269. const User *U = nullptr;
  270. unsigned Opcode = Instruction::UserOp1;
  271. if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
  272. // Don't walk into other basic blocks unless the object is an alloca from
  273. // another block, otherwise it may not have a virtual register assigned.
  274. if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
  275. FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
  276. Opcode = I->getOpcode();
  277. U = I;
  278. }
  279. } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
  280. Opcode = C->getOpcode();
  281. U = C;
  282. }
  283. switch (Opcode) {
  284. default:
  285. break;
  286. case Instruction::BitCast:
  287. // Look through bitcasts.
  288. return PPCComputeAddress(U->getOperand(0), Addr);
  289. case Instruction::IntToPtr:
  290. // Look past no-op inttoptrs.
  291. if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
  292. TLI.getPointerTy(DL))
  293. return PPCComputeAddress(U->getOperand(0), Addr);
  294. break;
  295. case Instruction::PtrToInt:
  296. // Look past no-op ptrtoints.
  297. if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
  298. return PPCComputeAddress(U->getOperand(0), Addr);
  299. break;
  300. case Instruction::GetElementPtr: {
  301. Address SavedAddr = Addr;
  302. int64_t TmpOffset = Addr.Offset;
  303. // Iterate through the GEP folding the constants into offsets where
  304. // we can.
  305. gep_type_iterator GTI = gep_type_begin(U);
  306. for (User::const_op_iterator II = U->op_begin() + 1, IE = U->op_end();
  307. II != IE; ++II, ++GTI) {
  308. const Value *Op = *II;
  309. if (StructType *STy = GTI.getStructTypeOrNull()) {
  310. const StructLayout *SL = DL.getStructLayout(STy);
  311. unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
  312. TmpOffset += SL->getElementOffset(Idx);
  313. } else {
  314. uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
  315. for (;;) {
  316. if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
  317. // Constant-offset addressing.
  318. TmpOffset += CI->getSExtValue() * S;
  319. break;
  320. }
  321. if (canFoldAddIntoGEP(U, Op)) {
  322. // A compatible add with a constant operand. Fold the constant.
  323. ConstantInt *CI =
  324. cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
  325. TmpOffset += CI->getSExtValue() * S;
  326. // Iterate on the other operand.
  327. Op = cast<AddOperator>(Op)->getOperand(0);
  328. continue;
  329. }
  330. // Unsupported
  331. goto unsupported_gep;
  332. }
  333. }
  334. }
  335. // Try to grab the base operand now.
  336. Addr.Offset = TmpOffset;
  337. if (PPCComputeAddress(U->getOperand(0), Addr)) return true;
  338. // We failed, restore everything and try the other options.
  339. Addr = SavedAddr;
  340. unsupported_gep:
  341. break;
  342. }
  343. case Instruction::Alloca: {
  344. const AllocaInst *AI = cast<AllocaInst>(Obj);
  345. DenseMap<const AllocaInst*, int>::iterator SI =
  346. FuncInfo.StaticAllocaMap.find(AI);
  347. if (SI != FuncInfo.StaticAllocaMap.end()) {
  348. Addr.BaseType = Address::FrameIndexBase;
  349. Addr.Base.FI = SI->second;
  350. return true;
  351. }
  352. break;
  353. }
  354. }
  355. // FIXME: References to parameters fall through to the behavior
  356. // below. They should be able to reference a frame index since
  357. // they are stored to the stack, so we can get "ld rx, offset(r1)"
  358. // instead of "addi ry, r1, offset / ld rx, 0(ry)". Obj will
  359. // just contain the parameter. Try to handle this with a FI.
  360. // Try to get this in a register if nothing else has worked.
  361. if (Addr.Base.Reg == 0)
  362. Addr.Base.Reg = getRegForValue(Obj);
  363. // Prevent assignment of base register to X0, which is inappropriate
  364. // for loads and stores alike.
  365. if (Addr.Base.Reg != 0)
  366. MRI.setRegClass(Addr.Base.Reg, &PPC::G8RC_and_G8RC_NOX0RegClass);
  367. return Addr.Base.Reg != 0;
  368. }
  369. // Fix up some addresses that can't be used directly. For example, if
  370. // an offset won't fit in an instruction field, we may need to move it
  371. // into an index register.
  372. void PPCFastISel::PPCSimplifyAddress(Address &Addr, bool &UseOffset,
  373. unsigned &IndexReg) {
  374. // Check whether the offset fits in the instruction field.
  375. if (!isInt<16>(Addr.Offset))
  376. UseOffset = false;
  377. // If this is a stack pointer and the offset needs to be simplified then
  378. // put the alloca address into a register, set the base type back to
  379. // register and continue. This should almost never happen.
  380. if (!UseOffset && Addr.BaseType == Address::FrameIndexBase) {
  381. Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
  382. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDI8),
  383. ResultReg).addFrameIndex(Addr.Base.FI).addImm(0);
  384. Addr.Base.Reg = ResultReg;
  385. Addr.BaseType = Address::RegBase;
  386. }
  387. if (!UseOffset) {
  388. IntegerType *OffsetTy = Type::getInt64Ty(*Context);
  389. const ConstantInt *Offset = ConstantInt::getSigned(OffsetTy, Addr.Offset);
  390. IndexReg = PPCMaterializeInt(Offset, MVT::i64);
  391. assert(IndexReg && "Unexpected error in PPCMaterializeInt!");
  392. }
  393. }
  394. // Emit a load instruction if possible, returning true if we succeeded,
  395. // otherwise false. See commentary below for how the register class of
  396. // the load is determined.
  397. bool PPCFastISel::PPCEmitLoad(MVT VT, Register &ResultReg, Address &Addr,
  398. const TargetRegisterClass *RC,
  399. bool IsZExt, unsigned FP64LoadOpc) {
  400. unsigned Opc;
  401. bool UseOffset = true;
  402. bool HasSPE = Subtarget->hasSPE();
  403. // If ResultReg is given, it determines the register class of the load.
  404. // Otherwise, RC is the register class to use. If the result of the
  405. // load isn't anticipated in this block, both may be zero, in which
  406. // case we must make a conservative guess. In particular, don't assign
  407. // R0 or X0 to the result register, as the result may be used in a load,
  408. // store, add-immediate, or isel that won't permit this. (Though
  409. // perhaps the spill and reload of live-exit values would handle this?)
  410. const TargetRegisterClass *UseRC =
  411. (ResultReg ? MRI.getRegClass(ResultReg) :
  412. (RC ? RC :
  413. (VT == MVT::f64 ? (HasSPE ? &PPC::SPERCRegClass : &PPC::F8RCRegClass) :
  414. (VT == MVT::f32 ? (HasSPE ? &PPC::GPRCRegClass : &PPC::F4RCRegClass) :
  415. (VT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
  416. &PPC::GPRC_and_GPRC_NOR0RegClass)))));
  417. bool Is32BitInt = UseRC->hasSuperClassEq(&PPC::GPRCRegClass);
  418. switch (VT.SimpleTy) {
  419. default: // e.g., vector types not handled
  420. return false;
  421. case MVT::i8:
  422. Opc = Is32BitInt ? PPC::LBZ : PPC::LBZ8;
  423. break;
  424. case MVT::i16:
  425. Opc = (IsZExt ? (Is32BitInt ? PPC::LHZ : PPC::LHZ8)
  426. : (Is32BitInt ? PPC::LHA : PPC::LHA8));
  427. break;
  428. case MVT::i32:
  429. Opc = (IsZExt ? (Is32BitInt ? PPC::LWZ : PPC::LWZ8)
  430. : (Is32BitInt ? PPC::LWA_32 : PPC::LWA));
  431. if ((Opc == PPC::LWA || Opc == PPC::LWA_32) && ((Addr.Offset & 3) != 0))
  432. UseOffset = false;
  433. break;
  434. case MVT::i64:
  435. Opc = PPC::LD;
  436. assert(UseRC->hasSuperClassEq(&PPC::G8RCRegClass) &&
  437. "64-bit load with 32-bit target??");
  438. UseOffset = ((Addr.Offset & 3) == 0);
  439. break;
  440. case MVT::f32:
  441. Opc = Subtarget->hasSPE() ? PPC::SPELWZ : PPC::LFS;
  442. break;
  443. case MVT::f64:
  444. Opc = FP64LoadOpc;
  445. break;
  446. }
  447. // If necessary, materialize the offset into a register and use
  448. // the indexed form. Also handle stack pointers with special needs.
  449. unsigned IndexReg = 0;
  450. PPCSimplifyAddress(Addr, UseOffset, IndexReg);
  451. // If this is a potential VSX load with an offset of 0, a VSX indexed load can
  452. // be used.
  453. bool IsVSSRC = isVSSRCRegClass(UseRC);
  454. bool IsVSFRC = isVSFRCRegClass(UseRC);
  455. bool Is32VSXLoad = IsVSSRC && Opc == PPC::LFS;
  456. bool Is64VSXLoad = IsVSFRC && Opc == PPC::LFD;
  457. if ((Is32VSXLoad || Is64VSXLoad) &&
  458. (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
  459. (Addr.Offset == 0)) {
  460. UseOffset = false;
  461. }
  462. if (ResultReg == 0)
  463. ResultReg = createResultReg(UseRC);
  464. // Note: If we still have a frame index here, we know the offset is
  465. // in range, as otherwise PPCSimplifyAddress would have converted it
  466. // into a RegBase.
  467. if (Addr.BaseType == Address::FrameIndexBase) {
  468. // VSX only provides an indexed load.
  469. if (Is32VSXLoad || Is64VSXLoad) return false;
  470. MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
  471. MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
  472. Addr.Offset),
  473. MachineMemOperand::MOLoad, MFI.getObjectSize(Addr.Base.FI),
  474. MFI.getObjectAlign(Addr.Base.FI));
  475. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
  476. .addImm(Addr.Offset).addFrameIndex(Addr.Base.FI).addMemOperand(MMO);
  477. // Base reg with offset in range.
  478. } else if (UseOffset) {
  479. // VSX only provides an indexed load.
  480. if (Is32VSXLoad || Is64VSXLoad) return false;
  481. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
  482. .addImm(Addr.Offset).addReg(Addr.Base.Reg);
  483. // Indexed form.
  484. } else {
  485. // Get the RR opcode corresponding to the RI one. FIXME: It would be
  486. // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
  487. // is hard to get at.
  488. switch (Opc) {
  489. default: llvm_unreachable("Unexpected opcode!");
  490. case PPC::LBZ: Opc = PPC::LBZX; break;
  491. case PPC::LBZ8: Opc = PPC::LBZX8; break;
  492. case PPC::LHZ: Opc = PPC::LHZX; break;
  493. case PPC::LHZ8: Opc = PPC::LHZX8; break;
  494. case PPC::LHA: Opc = PPC::LHAX; break;
  495. case PPC::LHA8: Opc = PPC::LHAX8; break;
  496. case PPC::LWZ: Opc = PPC::LWZX; break;
  497. case PPC::LWZ8: Opc = PPC::LWZX8; break;
  498. case PPC::LWA: Opc = PPC::LWAX; break;
  499. case PPC::LWA_32: Opc = PPC::LWAX_32; break;
  500. case PPC::LD: Opc = PPC::LDX; break;
  501. case PPC::LFS: Opc = IsVSSRC ? PPC::LXSSPX : PPC::LFSX; break;
  502. case PPC::LFD: Opc = IsVSFRC ? PPC::LXSDX : PPC::LFDX; break;
  503. case PPC::EVLDD: Opc = PPC::EVLDDX; break;
  504. case PPC::SPELWZ: Opc = PPC::SPELWZX; break;
  505. }
  506. auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),
  507. ResultReg);
  508. // If we have an index register defined we use it in the store inst,
  509. // otherwise we use X0 as base as it makes the vector instructions to
  510. // use zero in the computation of the effective address regardless the
  511. // content of the register.
  512. if (IndexReg)
  513. MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
  514. else
  515. MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
  516. }
  517. return true;
  518. }
  519. // Attempt to fast-select a load instruction.
  520. bool PPCFastISel::SelectLoad(const Instruction *I) {
  521. // FIXME: No atomic loads are supported.
  522. if (cast<LoadInst>(I)->isAtomic())
  523. return false;
  524. // Verify we have a legal type before going any further.
  525. MVT VT;
  526. if (!isLoadTypeLegal(I->getType(), VT))
  527. return false;
  528. // See if we can handle this address.
  529. Address Addr;
  530. if (!PPCComputeAddress(I->getOperand(0), Addr))
  531. return false;
  532. // Look at the currently assigned register for this instruction
  533. // to determine the required register class. This is necessary
  534. // to constrain RA from using R0/X0 when this is not legal.
  535. Register AssignedReg = FuncInfo.ValueMap[I];
  536. const TargetRegisterClass *RC =
  537. AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
  538. Register ResultReg = 0;
  539. if (!PPCEmitLoad(VT, ResultReg, Addr, RC, true,
  540. Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
  541. return false;
  542. updateValueMap(I, ResultReg);
  543. return true;
  544. }
  545. // Emit a store instruction to store SrcReg at Addr.
  546. bool PPCFastISel::PPCEmitStore(MVT VT, unsigned SrcReg, Address &Addr) {
  547. assert(SrcReg && "Nothing to store!");
  548. unsigned Opc;
  549. bool UseOffset = true;
  550. const TargetRegisterClass *RC = MRI.getRegClass(SrcReg);
  551. bool Is32BitInt = RC->hasSuperClassEq(&PPC::GPRCRegClass);
  552. switch (VT.SimpleTy) {
  553. default: // e.g., vector types not handled
  554. return false;
  555. case MVT::i8:
  556. Opc = Is32BitInt ? PPC::STB : PPC::STB8;
  557. break;
  558. case MVT::i16:
  559. Opc = Is32BitInt ? PPC::STH : PPC::STH8;
  560. break;
  561. case MVT::i32:
  562. assert(Is32BitInt && "Not GPRC for i32??");
  563. Opc = PPC::STW;
  564. break;
  565. case MVT::i64:
  566. Opc = PPC::STD;
  567. UseOffset = ((Addr.Offset & 3) == 0);
  568. break;
  569. case MVT::f32:
  570. Opc = Subtarget->hasSPE() ? PPC::SPESTW : PPC::STFS;
  571. break;
  572. case MVT::f64:
  573. Opc = Subtarget->hasSPE() ? PPC::EVSTDD : PPC::STFD;
  574. break;
  575. }
  576. // If necessary, materialize the offset into a register and use
  577. // the indexed form. Also handle stack pointers with special needs.
  578. unsigned IndexReg = 0;
  579. PPCSimplifyAddress(Addr, UseOffset, IndexReg);
  580. // If this is a potential VSX store with an offset of 0, a VSX indexed store
  581. // can be used.
  582. bool IsVSSRC = isVSSRCRegClass(RC);
  583. bool IsVSFRC = isVSFRCRegClass(RC);
  584. bool Is32VSXStore = IsVSSRC && Opc == PPC::STFS;
  585. bool Is64VSXStore = IsVSFRC && Opc == PPC::STFD;
  586. if ((Is32VSXStore || Is64VSXStore) &&
  587. (Addr.BaseType != Address::FrameIndexBase) && UseOffset &&
  588. (Addr.Offset == 0)) {
  589. UseOffset = false;
  590. }
  591. // Note: If we still have a frame index here, we know the offset is
  592. // in range, as otherwise PPCSimplifyAddress would have converted it
  593. // into a RegBase.
  594. if (Addr.BaseType == Address::FrameIndexBase) {
  595. // VSX only provides an indexed store.
  596. if (Is32VSXStore || Is64VSXStore) return false;
  597. MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
  598. MachinePointerInfo::getFixedStack(*FuncInfo.MF, Addr.Base.FI,
  599. Addr.Offset),
  600. MachineMemOperand::MOStore, MFI.getObjectSize(Addr.Base.FI),
  601. MFI.getObjectAlign(Addr.Base.FI));
  602. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))
  603. .addReg(SrcReg)
  604. .addImm(Addr.Offset)
  605. .addFrameIndex(Addr.Base.FI)
  606. .addMemOperand(MMO);
  607. // Base reg with offset in range.
  608. } else if (UseOffset) {
  609. // VSX only provides an indexed store.
  610. if (Is32VSXStore || Is64VSXStore)
  611. return false;
  612. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))
  613. .addReg(SrcReg).addImm(Addr.Offset).addReg(Addr.Base.Reg);
  614. // Indexed form.
  615. } else {
  616. // Get the RR opcode corresponding to the RI one. FIXME: It would be
  617. // preferable to use the ImmToIdxMap from PPCRegisterInfo.cpp, but it
  618. // is hard to get at.
  619. switch (Opc) {
  620. default: llvm_unreachable("Unexpected opcode!");
  621. case PPC::STB: Opc = PPC::STBX; break;
  622. case PPC::STH : Opc = PPC::STHX; break;
  623. case PPC::STW : Opc = PPC::STWX; break;
  624. case PPC::STB8: Opc = PPC::STBX8; break;
  625. case PPC::STH8: Opc = PPC::STHX8; break;
  626. case PPC::STW8: Opc = PPC::STWX8; break;
  627. case PPC::STD: Opc = PPC::STDX; break;
  628. case PPC::STFS: Opc = IsVSSRC ? PPC::STXSSPX : PPC::STFSX; break;
  629. case PPC::STFD: Opc = IsVSFRC ? PPC::STXSDX : PPC::STFDX; break;
  630. case PPC::EVSTDD: Opc = PPC::EVSTDDX; break;
  631. case PPC::SPESTW: Opc = PPC::SPESTWX; break;
  632. }
  633. auto MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc))
  634. .addReg(SrcReg);
  635. // If we have an index register defined we use it in the store inst,
  636. // otherwise we use X0 as base as it makes the vector instructions to
  637. // use zero in the computation of the effective address regardless the
  638. // content of the register.
  639. if (IndexReg)
  640. MIB.addReg(Addr.Base.Reg).addReg(IndexReg);
  641. else
  642. MIB.addReg(PPC::ZERO8).addReg(Addr.Base.Reg);
  643. }
  644. return true;
  645. }
  646. // Attempt to fast-select a store instruction.
  647. bool PPCFastISel::SelectStore(const Instruction *I) {
  648. Value *Op0 = I->getOperand(0);
  649. unsigned SrcReg = 0;
  650. // FIXME: No atomics loads are supported.
  651. if (cast<StoreInst>(I)->isAtomic())
  652. return false;
  653. // Verify we have a legal type before going any further.
  654. MVT VT;
  655. if (!isLoadTypeLegal(Op0->getType(), VT))
  656. return false;
  657. // Get the value to be stored into a register.
  658. SrcReg = getRegForValue(Op0);
  659. if (SrcReg == 0)
  660. return false;
  661. // See if we can handle this address.
  662. Address Addr;
  663. if (!PPCComputeAddress(I->getOperand(1), Addr))
  664. return false;
  665. if (!PPCEmitStore(VT, SrcReg, Addr))
  666. return false;
  667. return true;
  668. }
  669. // Attempt to fast-select a branch instruction.
  670. bool PPCFastISel::SelectBranch(const Instruction *I) {
  671. const BranchInst *BI = cast<BranchInst>(I);
  672. MachineBasicBlock *BrBB = FuncInfo.MBB;
  673. MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
  674. MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
  675. // For now, just try the simplest case where it's fed by a compare.
  676. if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
  677. if (isValueAvailable(CI)) {
  678. std::optional<PPC::Predicate> OptPPCPred =
  679. getComparePred(CI->getPredicate());
  680. if (!OptPPCPred)
  681. return false;
  682. PPC::Predicate PPCPred = *OptPPCPred;
  683. // Take advantage of fall-through opportunities.
  684. if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
  685. std::swap(TBB, FBB);
  686. PPCPred = PPC::InvertPredicate(PPCPred);
  687. }
  688. Register CondReg = createResultReg(&PPC::CRRCRegClass);
  689. if (!PPCEmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned(),
  690. CondReg, PPCPred))
  691. return false;
  692. BuildMI(*BrBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::BCC))
  693. .addImm(Subtarget->hasSPE() ? PPC::PRED_SPE : PPCPred)
  694. .addReg(CondReg)
  695. .addMBB(TBB);
  696. finishCondBranch(BI->getParent(), TBB, FBB);
  697. return true;
  698. }
  699. } else if (const ConstantInt *CI =
  700. dyn_cast<ConstantInt>(BI->getCondition())) {
  701. uint64_t Imm = CI->getZExtValue();
  702. MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
  703. fastEmitBranch(Target, MIMD.getDL());
  704. return true;
  705. }
  706. // FIXME: ARM looks for a case where the block containing the compare
  707. // has been split from the block containing the branch. If this happens,
  708. // there is a vreg available containing the result of the compare. I'm
  709. // not sure we can do much, as we've lost the predicate information with
  710. // the compare instruction -- we have a 4-bit CR but don't know which bit
  711. // to test here.
  712. return false;
  713. }
  714. // Attempt to emit a compare of the two source values. Signed and unsigned
  715. // comparisons are supported. Return false if we can't handle it.
  716. bool PPCFastISel::PPCEmitCmp(const Value *SrcValue1, const Value *SrcValue2,
  717. bool IsZExt, unsigned DestReg,
  718. const PPC::Predicate Pred) {
  719. Type *Ty = SrcValue1->getType();
  720. EVT SrcEVT = TLI.getValueType(DL, Ty, true);
  721. if (!SrcEVT.isSimple())
  722. return false;
  723. MVT SrcVT = SrcEVT.getSimpleVT();
  724. if (SrcVT == MVT::i1 && Subtarget->useCRBits())
  725. return false;
  726. // See if operand 2 is an immediate encodeable in the compare.
  727. // FIXME: Operands are not in canonical order at -O0, so an immediate
  728. // operand in position 1 is a lost opportunity for now. We are
  729. // similar to ARM in this regard.
  730. int64_t Imm = 0;
  731. bool UseImm = false;
  732. const bool HasSPE = Subtarget->hasSPE();
  733. // Only 16-bit integer constants can be represented in compares for
  734. // PowerPC. Others will be materialized into a register.
  735. if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(SrcValue2)) {
  736. if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
  737. SrcVT == MVT::i8 || SrcVT == MVT::i1) {
  738. const APInt &CIVal = ConstInt->getValue();
  739. Imm = (IsZExt) ? (int64_t)CIVal.getZExtValue() :
  740. (int64_t)CIVal.getSExtValue();
  741. if ((IsZExt && isUInt<16>(Imm)) || (!IsZExt && isInt<16>(Imm)))
  742. UseImm = true;
  743. }
  744. }
  745. Register SrcReg1 = getRegForValue(SrcValue1);
  746. if (SrcReg1 == 0)
  747. return false;
  748. unsigned SrcReg2 = 0;
  749. if (!UseImm) {
  750. SrcReg2 = getRegForValue(SrcValue2);
  751. if (SrcReg2 == 0)
  752. return false;
  753. }
  754. unsigned CmpOpc;
  755. bool NeedsExt = false;
  756. auto RC1 = MRI.getRegClass(SrcReg1);
  757. auto RC2 = SrcReg2 != 0 ? MRI.getRegClass(SrcReg2) : nullptr;
  758. switch (SrcVT.SimpleTy) {
  759. default: return false;
  760. case MVT::f32:
  761. if (HasSPE) {
  762. switch (Pred) {
  763. default: return false;
  764. case PPC::PRED_EQ:
  765. CmpOpc = PPC::EFSCMPEQ;
  766. break;
  767. case PPC::PRED_LT:
  768. CmpOpc = PPC::EFSCMPLT;
  769. break;
  770. case PPC::PRED_GT:
  771. CmpOpc = PPC::EFSCMPGT;
  772. break;
  773. }
  774. } else {
  775. CmpOpc = PPC::FCMPUS;
  776. if (isVSSRCRegClass(RC1))
  777. SrcReg1 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg1);
  778. if (RC2 && isVSSRCRegClass(RC2))
  779. SrcReg2 = copyRegToRegClass(&PPC::F4RCRegClass, SrcReg2);
  780. }
  781. break;
  782. case MVT::f64:
  783. if (HasSPE) {
  784. switch (Pred) {
  785. default: return false;
  786. case PPC::PRED_EQ:
  787. CmpOpc = PPC::EFDCMPEQ;
  788. break;
  789. case PPC::PRED_LT:
  790. CmpOpc = PPC::EFDCMPLT;
  791. break;
  792. case PPC::PRED_GT:
  793. CmpOpc = PPC::EFDCMPGT;
  794. break;
  795. }
  796. } else if (isVSFRCRegClass(RC1) || (RC2 && isVSFRCRegClass(RC2))) {
  797. CmpOpc = PPC::XSCMPUDP;
  798. } else {
  799. CmpOpc = PPC::FCMPUD;
  800. }
  801. break;
  802. case MVT::i1:
  803. case MVT::i8:
  804. case MVT::i16:
  805. NeedsExt = true;
  806. [[fallthrough]];
  807. case MVT::i32:
  808. if (!UseImm)
  809. CmpOpc = IsZExt ? PPC::CMPLW : PPC::CMPW;
  810. else
  811. CmpOpc = IsZExt ? PPC::CMPLWI : PPC::CMPWI;
  812. break;
  813. case MVT::i64:
  814. if (!UseImm)
  815. CmpOpc = IsZExt ? PPC::CMPLD : PPC::CMPD;
  816. else
  817. CmpOpc = IsZExt ? PPC::CMPLDI : PPC::CMPDI;
  818. break;
  819. }
  820. if (NeedsExt) {
  821. Register ExtReg = createResultReg(&PPC::GPRCRegClass);
  822. if (!PPCEmitIntExt(SrcVT, SrcReg1, MVT::i32, ExtReg, IsZExt))
  823. return false;
  824. SrcReg1 = ExtReg;
  825. if (!UseImm) {
  826. Register ExtReg = createResultReg(&PPC::GPRCRegClass);
  827. if (!PPCEmitIntExt(SrcVT, SrcReg2, MVT::i32, ExtReg, IsZExt))
  828. return false;
  829. SrcReg2 = ExtReg;
  830. }
  831. }
  832. if (!UseImm)
  833. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(CmpOpc), DestReg)
  834. .addReg(SrcReg1).addReg(SrcReg2);
  835. else
  836. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(CmpOpc), DestReg)
  837. .addReg(SrcReg1).addImm(Imm);
  838. return true;
  839. }
  840. // Attempt to fast-select a floating-point extend instruction.
  841. bool PPCFastISel::SelectFPExt(const Instruction *I) {
  842. Value *Src = I->getOperand(0);
  843. EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
  844. EVT DestVT = TLI.getValueType(DL, I->getType(), true);
  845. if (SrcVT != MVT::f32 || DestVT != MVT::f64)
  846. return false;
  847. Register SrcReg = getRegForValue(Src);
  848. if (!SrcReg)
  849. return false;
  850. // No code is generated for a FP extend.
  851. updateValueMap(I, SrcReg);
  852. return true;
  853. }
  854. // Attempt to fast-select a floating-point truncate instruction.
  855. bool PPCFastISel::SelectFPTrunc(const Instruction *I) {
  856. Value *Src = I->getOperand(0);
  857. EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
  858. EVT DestVT = TLI.getValueType(DL, I->getType(), true);
  859. if (SrcVT != MVT::f64 || DestVT != MVT::f32)
  860. return false;
  861. Register SrcReg = getRegForValue(Src);
  862. if (!SrcReg)
  863. return false;
  864. // Round the result to single precision.
  865. unsigned DestReg;
  866. auto RC = MRI.getRegClass(SrcReg);
  867. if (Subtarget->hasSPE()) {
  868. DestReg = createResultReg(&PPC::GPRCRegClass);
  869. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::EFSCFD),
  870. DestReg)
  871. .addReg(SrcReg);
  872. } else if (Subtarget->hasP8Vector() && isVSFRCRegClass(RC)) {
  873. DestReg = createResultReg(&PPC::VSSRCRegClass);
  874. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::XSRSP),
  875. DestReg)
  876. .addReg(SrcReg);
  877. } else {
  878. SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
  879. DestReg = createResultReg(&PPC::F4RCRegClass);
  880. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  881. TII.get(PPC::FRSP), DestReg)
  882. .addReg(SrcReg);
  883. }
  884. updateValueMap(I, DestReg);
  885. return true;
  886. }
  887. // Move an i32 or i64 value in a GPR to an f64 value in an FPR.
  888. // FIXME: When direct register moves are implemented (see PowerISA 2.07),
  889. // those should be used instead of moving via a stack slot when the
  890. // subtarget permits.
  891. // FIXME: The code here is sloppy for the 4-byte case. Can use a 4-byte
  892. // stack slot and 4-byte store/load sequence. Or just sext the 4-byte
  893. // case to 8 bytes which produces tighter code but wastes stack space.
  894. unsigned PPCFastISel::PPCMoveToFPReg(MVT SrcVT, unsigned SrcReg,
  895. bool IsSigned) {
  896. // If necessary, extend 32-bit int to 64-bit.
  897. if (SrcVT == MVT::i32) {
  898. Register TmpReg = createResultReg(&PPC::G8RCRegClass);
  899. if (!PPCEmitIntExt(MVT::i32, SrcReg, MVT::i64, TmpReg, !IsSigned))
  900. return 0;
  901. SrcReg = TmpReg;
  902. }
  903. // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
  904. Address Addr;
  905. Addr.BaseType = Address::FrameIndexBase;
  906. Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
  907. // Store the value from the GPR.
  908. if (!PPCEmitStore(MVT::i64, SrcReg, Addr))
  909. return 0;
  910. // Load the integer value into an FPR. The kind of load used depends
  911. // on a number of conditions.
  912. unsigned LoadOpc = PPC::LFD;
  913. if (SrcVT == MVT::i32) {
  914. if (!IsSigned) {
  915. LoadOpc = PPC::LFIWZX;
  916. Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
  917. } else if (Subtarget->hasLFIWAX()) {
  918. LoadOpc = PPC::LFIWAX;
  919. Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
  920. }
  921. }
  922. const TargetRegisterClass *RC = &PPC::F8RCRegClass;
  923. Register ResultReg = 0;
  924. if (!PPCEmitLoad(MVT::f64, ResultReg, Addr, RC, !IsSigned, LoadOpc))
  925. return 0;
  926. return ResultReg;
  927. }
  928. // Attempt to fast-select an integer-to-floating-point conversion.
  929. // FIXME: Once fast-isel has better support for VSX, conversions using
  930. // direct moves should be implemented.
  931. bool PPCFastISel::SelectIToFP(const Instruction *I, bool IsSigned) {
  932. MVT DstVT;
  933. Type *DstTy = I->getType();
  934. if (!isTypeLegal(DstTy, DstVT))
  935. return false;
  936. if (DstVT != MVT::f32 && DstVT != MVT::f64)
  937. return false;
  938. Value *Src = I->getOperand(0);
  939. EVT SrcEVT = TLI.getValueType(DL, Src->getType(), true);
  940. if (!SrcEVT.isSimple())
  941. return false;
  942. MVT SrcVT = SrcEVT.getSimpleVT();
  943. if (SrcVT != MVT::i8 && SrcVT != MVT::i16 &&
  944. SrcVT != MVT::i32 && SrcVT != MVT::i64)
  945. return false;
  946. Register SrcReg = getRegForValue(Src);
  947. if (SrcReg == 0)
  948. return false;
  949. // Shortcut for SPE. Doesn't need to store/load, since it's all in the GPRs
  950. if (Subtarget->hasSPE()) {
  951. unsigned Opc;
  952. if (DstVT == MVT::f32)
  953. Opc = IsSigned ? PPC::EFSCFSI : PPC::EFSCFUI;
  954. else
  955. Opc = IsSigned ? PPC::EFDCFSI : PPC::EFDCFUI;
  956. Register DestReg = createResultReg(&PPC::SPERCRegClass);
  957. // Generate the convert.
  958. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  959. .addReg(SrcReg);
  960. updateValueMap(I, DestReg);
  961. return true;
  962. }
  963. // We can only lower an unsigned convert if we have the newer
  964. // floating-point conversion operations.
  965. if (!IsSigned && !Subtarget->hasFPCVT())
  966. return false;
  967. // FIXME: For now we require the newer floating-point conversion operations
  968. // (which are present only on P7 and A2 server models) when converting
  969. // to single-precision float. Otherwise we have to generate a lot of
  970. // fiddly code to avoid double rounding. If necessary, the fiddly code
  971. // can be found in PPCTargetLowering::LowerINT_TO_FP().
  972. if (DstVT == MVT::f32 && !Subtarget->hasFPCVT())
  973. return false;
  974. // Extend the input if necessary.
  975. if (SrcVT == MVT::i8 || SrcVT == MVT::i16) {
  976. Register TmpReg = createResultReg(&PPC::G8RCRegClass);
  977. if (!PPCEmitIntExt(SrcVT, SrcReg, MVT::i64, TmpReg, !IsSigned))
  978. return false;
  979. SrcVT = MVT::i64;
  980. SrcReg = TmpReg;
  981. }
  982. // Move the integer value to an FPR.
  983. unsigned FPReg = PPCMoveToFPReg(SrcVT, SrcReg, IsSigned);
  984. if (FPReg == 0)
  985. return false;
  986. // Determine the opcode for the conversion.
  987. const TargetRegisterClass *RC = &PPC::F8RCRegClass;
  988. Register DestReg = createResultReg(RC);
  989. unsigned Opc;
  990. if (DstVT == MVT::f32)
  991. Opc = IsSigned ? PPC::FCFIDS : PPC::FCFIDUS;
  992. else
  993. Opc = IsSigned ? PPC::FCFID : PPC::FCFIDU;
  994. // Generate the convert.
  995. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  996. .addReg(FPReg);
  997. updateValueMap(I, DestReg);
  998. return true;
  999. }
  1000. // Move the floating-point value in SrcReg into an integer destination
  1001. // register, and return the register (or zero if we can't handle it).
  1002. // FIXME: When direct register moves are implemented (see PowerISA 2.07),
  1003. // those should be used instead of moving via a stack slot when the
  1004. // subtarget permits.
  1005. unsigned PPCFastISel::PPCMoveToIntReg(const Instruction *I, MVT VT,
  1006. unsigned SrcReg, bool IsSigned) {
  1007. // Get a stack slot 8 bytes wide, aligned on an 8-byte boundary.
  1008. // Note that if have STFIWX available, we could use a 4-byte stack
  1009. // slot for i32, but this being fast-isel we'll just go with the
  1010. // easiest code gen possible.
  1011. Address Addr;
  1012. Addr.BaseType = Address::FrameIndexBase;
  1013. Addr.Base.FI = MFI.CreateStackObject(8, Align(8), false);
  1014. // Store the value from the FPR.
  1015. if (!PPCEmitStore(MVT::f64, SrcReg, Addr))
  1016. return 0;
  1017. // Reload it into a GPR. If we want an i32 on big endian, modify the
  1018. // address to have a 4-byte offset so we load from the right place.
  1019. if (VT == MVT::i32)
  1020. Addr.Offset = (Subtarget->isLittleEndian()) ? 0 : 4;
  1021. // Look at the currently assigned register for this instruction
  1022. // to determine the required register class.
  1023. Register AssignedReg = FuncInfo.ValueMap[I];
  1024. const TargetRegisterClass *RC =
  1025. AssignedReg ? MRI.getRegClass(AssignedReg) : nullptr;
  1026. Register ResultReg = 0;
  1027. if (!PPCEmitLoad(VT, ResultReg, Addr, RC, !IsSigned))
  1028. return 0;
  1029. return ResultReg;
  1030. }
  1031. // Attempt to fast-select a floating-point-to-integer conversion.
  1032. // FIXME: Once fast-isel has better support for VSX, conversions using
  1033. // direct moves should be implemented.
  1034. bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) {
  1035. MVT DstVT, SrcVT;
  1036. Type *DstTy = I->getType();
  1037. if (!isTypeLegal(DstTy, DstVT))
  1038. return false;
  1039. if (DstVT != MVT::i32 && DstVT != MVT::i64)
  1040. return false;
  1041. // If we don't have FCTIDUZ, or SPE, and we need it, punt to SelectionDAG.
  1042. if (DstVT == MVT::i64 && !IsSigned && !Subtarget->hasFPCVT() &&
  1043. !Subtarget->hasSPE())
  1044. return false;
  1045. Value *Src = I->getOperand(0);
  1046. Type *SrcTy = Src->getType();
  1047. if (!isTypeLegal(SrcTy, SrcVT))
  1048. return false;
  1049. if (SrcVT != MVT::f32 && SrcVT != MVT::f64)
  1050. return false;
  1051. Register SrcReg = getRegForValue(Src);
  1052. if (SrcReg == 0)
  1053. return false;
  1054. // Convert f32 to f64 or convert VSSRC to VSFRC if necessary. This is just a
  1055. // meaningless copy to get the register class right.
  1056. const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg);
  1057. if (InRC == &PPC::F4RCRegClass)
  1058. SrcReg = copyRegToRegClass(&PPC::F8RCRegClass, SrcReg);
  1059. else if (InRC == &PPC::VSSRCRegClass)
  1060. SrcReg = copyRegToRegClass(&PPC::VSFRCRegClass, SrcReg);
  1061. // Determine the opcode for the conversion, which takes place
  1062. // entirely within FPRs or VSRs.
  1063. unsigned DestReg;
  1064. unsigned Opc;
  1065. auto RC = MRI.getRegClass(SrcReg);
  1066. if (Subtarget->hasSPE()) {
  1067. DestReg = createResultReg(&PPC::GPRCRegClass);
  1068. if (IsSigned)
  1069. Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTSIZ : PPC::EFDCTSIZ;
  1070. else
  1071. Opc = InRC == &PPC::GPRCRegClass ? PPC::EFSCTUIZ : PPC::EFDCTUIZ;
  1072. } else if (isVSFRCRegClass(RC)) {
  1073. DestReg = createResultReg(&PPC::VSFRCRegClass);
  1074. if (DstVT == MVT::i32)
  1075. Opc = IsSigned ? PPC::XSCVDPSXWS : PPC::XSCVDPUXWS;
  1076. else
  1077. Opc = IsSigned ? PPC::XSCVDPSXDS : PPC::XSCVDPUXDS;
  1078. } else {
  1079. DestReg = createResultReg(&PPC::F8RCRegClass);
  1080. if (DstVT == MVT::i32)
  1081. if (IsSigned)
  1082. Opc = PPC::FCTIWZ;
  1083. else
  1084. Opc = Subtarget->hasFPCVT() ? PPC::FCTIWUZ : PPC::FCTIDZ;
  1085. else
  1086. Opc = IsSigned ? PPC::FCTIDZ : PPC::FCTIDUZ;
  1087. }
  1088. // Generate the convert.
  1089. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  1090. .addReg(SrcReg);
  1091. // Now move the integer value from a float register to an integer register.
  1092. unsigned IntReg = Subtarget->hasSPE()
  1093. ? DestReg
  1094. : PPCMoveToIntReg(I, DstVT, DestReg, IsSigned);
  1095. if (IntReg == 0)
  1096. return false;
  1097. updateValueMap(I, IntReg);
  1098. return true;
  1099. }
  1100. // Attempt to fast-select a binary integer operation that isn't already
  1101. // handled automatically.
  1102. bool PPCFastISel::SelectBinaryIntOp(const Instruction *I, unsigned ISDOpcode) {
  1103. EVT DestVT = TLI.getValueType(DL, I->getType(), true);
  1104. // We can get here in the case when we have a binary operation on a non-legal
  1105. // type and the target independent selector doesn't know how to handle it.
  1106. if (DestVT != MVT::i16 && DestVT != MVT::i8)
  1107. return false;
  1108. // Look at the currently assigned register for this instruction
  1109. // to determine the required register class. If there is no register,
  1110. // make a conservative choice (don't assign R0).
  1111. Register AssignedReg = FuncInfo.ValueMap[I];
  1112. const TargetRegisterClass *RC =
  1113. (AssignedReg ? MRI.getRegClass(AssignedReg) :
  1114. &PPC::GPRC_and_GPRC_NOR0RegClass);
  1115. bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
  1116. unsigned Opc;
  1117. switch (ISDOpcode) {
  1118. default: return false;
  1119. case ISD::ADD:
  1120. Opc = IsGPRC ? PPC::ADD4 : PPC::ADD8;
  1121. break;
  1122. case ISD::OR:
  1123. Opc = IsGPRC ? PPC::OR : PPC::OR8;
  1124. break;
  1125. case ISD::SUB:
  1126. Opc = IsGPRC ? PPC::SUBF : PPC::SUBF8;
  1127. break;
  1128. }
  1129. Register ResultReg = createResultReg(RC ? RC : &PPC::G8RCRegClass);
  1130. Register SrcReg1 = getRegForValue(I->getOperand(0));
  1131. if (SrcReg1 == 0) return false;
  1132. // Handle case of small immediate operand.
  1133. if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(I->getOperand(1))) {
  1134. const APInt &CIVal = ConstInt->getValue();
  1135. int Imm = (int)CIVal.getSExtValue();
  1136. bool UseImm = true;
  1137. if (isInt<16>(Imm)) {
  1138. switch (Opc) {
  1139. default:
  1140. llvm_unreachable("Missing case!");
  1141. case PPC::ADD4:
  1142. Opc = PPC::ADDI;
  1143. MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
  1144. break;
  1145. case PPC::ADD8:
  1146. Opc = PPC::ADDI8;
  1147. MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
  1148. break;
  1149. case PPC::OR:
  1150. Opc = PPC::ORI;
  1151. break;
  1152. case PPC::OR8:
  1153. Opc = PPC::ORI8;
  1154. break;
  1155. case PPC::SUBF:
  1156. if (Imm == -32768)
  1157. UseImm = false;
  1158. else {
  1159. Opc = PPC::ADDI;
  1160. MRI.setRegClass(SrcReg1, &PPC::GPRC_and_GPRC_NOR0RegClass);
  1161. Imm = -Imm;
  1162. }
  1163. break;
  1164. case PPC::SUBF8:
  1165. if (Imm == -32768)
  1166. UseImm = false;
  1167. else {
  1168. Opc = PPC::ADDI8;
  1169. MRI.setRegClass(SrcReg1, &PPC::G8RC_and_G8RC_NOX0RegClass);
  1170. Imm = -Imm;
  1171. }
  1172. break;
  1173. }
  1174. if (UseImm) {
  1175. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc),
  1176. ResultReg)
  1177. .addReg(SrcReg1)
  1178. .addImm(Imm);
  1179. updateValueMap(I, ResultReg);
  1180. return true;
  1181. }
  1182. }
  1183. }
  1184. // Reg-reg case.
  1185. Register SrcReg2 = getRegForValue(I->getOperand(1));
  1186. if (SrcReg2 == 0) return false;
  1187. // Reverse operands for subtract-from.
  1188. if (ISDOpcode == ISD::SUB)
  1189. std::swap(SrcReg1, SrcReg2);
  1190. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ResultReg)
  1191. .addReg(SrcReg1).addReg(SrcReg2);
  1192. updateValueMap(I, ResultReg);
  1193. return true;
  1194. }
  1195. // Handle arguments to a call that we're attempting to fast-select.
  1196. // Return false if the arguments are too complex for us at the moment.
  1197. bool PPCFastISel::processCallArgs(SmallVectorImpl<Value*> &Args,
  1198. SmallVectorImpl<unsigned> &ArgRegs,
  1199. SmallVectorImpl<MVT> &ArgVTs,
  1200. SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
  1201. SmallVectorImpl<unsigned> &RegArgs,
  1202. CallingConv::ID CC,
  1203. unsigned &NumBytes,
  1204. bool IsVarArg) {
  1205. SmallVector<CCValAssign, 16> ArgLocs;
  1206. CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, ArgLocs, *Context);
  1207. // Reserve space for the linkage area on the stack.
  1208. unsigned LinkageSize = Subtarget->getFrameLowering()->getLinkageSize();
  1209. CCInfo.AllocateStack(LinkageSize, Align(8));
  1210. CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_PPC64_ELF_FIS);
  1211. // Bail out if we can't handle any of the arguments.
  1212. for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
  1213. CCValAssign &VA = ArgLocs[I];
  1214. MVT ArgVT = ArgVTs[VA.getValNo()];
  1215. // Skip vector arguments for now, as well as long double and
  1216. // uint128_t, and anything that isn't passed in a register.
  1217. if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64 || ArgVT == MVT::i1 ||
  1218. !VA.isRegLoc() || VA.needsCustom())
  1219. return false;
  1220. // Skip bit-converted arguments for now.
  1221. if (VA.getLocInfo() == CCValAssign::BCvt)
  1222. return false;
  1223. }
  1224. // Get a count of how many bytes are to be pushed onto the stack.
  1225. NumBytes = CCInfo.getNextStackOffset();
  1226. // The prolog code of the callee may store up to 8 GPR argument registers to
  1227. // the stack, allowing va_start to index over them in memory if its varargs.
  1228. // Because we cannot tell if this is needed on the caller side, we have to
  1229. // conservatively assume that it is needed. As such, make sure we have at
  1230. // least enough stack space for the caller to store the 8 GPRs.
  1231. // FIXME: On ELFv2, it may be unnecessary to allocate the parameter area.
  1232. NumBytes = std::max(NumBytes, LinkageSize + 64);
  1233. // Issue CALLSEQ_START.
  1234. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1235. TII.get(TII.getCallFrameSetupOpcode()))
  1236. .addImm(NumBytes).addImm(0);
  1237. // Prepare to assign register arguments. Every argument uses up a
  1238. // GPR protocol register even if it's passed in a floating-point
  1239. // register (unless we're using the fast calling convention).
  1240. unsigned NextGPR = PPC::X3;
  1241. unsigned NextFPR = PPC::F1;
  1242. // Process arguments.
  1243. for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
  1244. CCValAssign &VA = ArgLocs[I];
  1245. unsigned Arg = ArgRegs[VA.getValNo()];
  1246. MVT ArgVT = ArgVTs[VA.getValNo()];
  1247. // Handle argument promotion and bitcasts.
  1248. switch (VA.getLocInfo()) {
  1249. default:
  1250. llvm_unreachable("Unknown loc info!");
  1251. case CCValAssign::Full:
  1252. break;
  1253. case CCValAssign::SExt: {
  1254. MVT DestVT = VA.getLocVT();
  1255. const TargetRegisterClass *RC =
  1256. (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
  1257. Register TmpReg = createResultReg(RC);
  1258. if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/false))
  1259. llvm_unreachable("Failed to emit a sext!");
  1260. ArgVT = DestVT;
  1261. Arg = TmpReg;
  1262. break;
  1263. }
  1264. case CCValAssign::AExt:
  1265. case CCValAssign::ZExt: {
  1266. MVT DestVT = VA.getLocVT();
  1267. const TargetRegisterClass *RC =
  1268. (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
  1269. Register TmpReg = createResultReg(RC);
  1270. if (!PPCEmitIntExt(ArgVT, Arg, DestVT, TmpReg, /*IsZExt*/true))
  1271. llvm_unreachable("Failed to emit a zext!");
  1272. ArgVT = DestVT;
  1273. Arg = TmpReg;
  1274. break;
  1275. }
  1276. case CCValAssign::BCvt: {
  1277. // FIXME: Not yet handled.
  1278. llvm_unreachable("Should have bailed before getting here!");
  1279. break;
  1280. }
  1281. }
  1282. // Copy this argument to the appropriate register.
  1283. unsigned ArgReg;
  1284. if (ArgVT == MVT::f32 || ArgVT == MVT::f64) {
  1285. ArgReg = NextFPR++;
  1286. if (CC != CallingConv::Fast)
  1287. ++NextGPR;
  1288. } else
  1289. ArgReg = NextGPR++;
  1290. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1291. TII.get(TargetOpcode::COPY), ArgReg).addReg(Arg);
  1292. RegArgs.push_back(ArgReg);
  1293. }
  1294. return true;
  1295. }
  1296. // For a call that we've determined we can fast-select, finish the
  1297. // call sequence and generate a copy to obtain the return value (if any).
  1298. bool PPCFastISel::finishCall(MVT RetVT, CallLoweringInfo &CLI, unsigned &NumBytes) {
  1299. CallingConv::ID CC = CLI.CallConv;
  1300. // Issue CallSEQ_END.
  1301. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1302. TII.get(TII.getCallFrameDestroyOpcode()))
  1303. .addImm(NumBytes).addImm(0);
  1304. // Next, generate a copy to obtain the return value.
  1305. // FIXME: No multi-register return values yet, though I don't foresee
  1306. // any real difficulties there.
  1307. if (RetVT != MVT::isVoid) {
  1308. SmallVector<CCValAssign, 16> RVLocs;
  1309. CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
  1310. CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
  1311. CCValAssign &VA = RVLocs[0];
  1312. assert(RVLocs.size() == 1 && "No support for multi-reg return values!");
  1313. assert(VA.isRegLoc() && "Can only return in registers!");
  1314. MVT DestVT = VA.getValVT();
  1315. MVT CopyVT = DestVT;
  1316. // Ints smaller than a register still arrive in a full 64-bit
  1317. // register, so make sure we recognize this.
  1318. if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32)
  1319. CopyVT = MVT::i64;
  1320. unsigned SourcePhysReg = VA.getLocReg();
  1321. unsigned ResultReg = 0;
  1322. if (RetVT == CopyVT) {
  1323. const TargetRegisterClass *CpyRC = TLI.getRegClassFor(CopyVT);
  1324. ResultReg = copyRegToRegClass(CpyRC, SourcePhysReg);
  1325. // If necessary, round the floating result to single precision.
  1326. } else if (CopyVT == MVT::f64) {
  1327. ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
  1328. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::FRSP),
  1329. ResultReg).addReg(SourcePhysReg);
  1330. // If only the low half of a general register is needed, generate
  1331. // a GPRC copy instead of a G8RC copy. (EXTRACT_SUBREG can't be
  1332. // used along the fast-isel path (not lowered), and downstream logic
  1333. // also doesn't like a direct subreg copy on a physical reg.)
  1334. } else if (RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32) {
  1335. // Convert physical register from G8RC to GPRC.
  1336. SourcePhysReg -= PPC::X0 - PPC::R0;
  1337. ResultReg = copyRegToRegClass(&PPC::GPRCRegClass, SourcePhysReg);
  1338. }
  1339. assert(ResultReg && "ResultReg unset!");
  1340. CLI.InRegs.push_back(SourcePhysReg);
  1341. CLI.ResultReg = ResultReg;
  1342. CLI.NumResultRegs = 1;
  1343. }
  1344. return true;
  1345. }
  1346. bool PPCFastISel::fastLowerCall(CallLoweringInfo &CLI) {
  1347. CallingConv::ID CC = CLI.CallConv;
  1348. bool IsTailCall = CLI.IsTailCall;
  1349. bool IsVarArg = CLI.IsVarArg;
  1350. const Value *Callee = CLI.Callee;
  1351. const MCSymbol *Symbol = CLI.Symbol;
  1352. if (!Callee && !Symbol)
  1353. return false;
  1354. // Allow SelectionDAG isel to handle tail calls.
  1355. if (IsTailCall)
  1356. return false;
  1357. // Let SDISel handle vararg functions.
  1358. if (IsVarArg)
  1359. return false;
  1360. // If this is a PC-Rel function, let SDISel handle the call.
  1361. if (Subtarget->isUsingPCRelativeCalls())
  1362. return false;
  1363. // Handle simple calls for now, with legal return types and
  1364. // those that can be extended.
  1365. Type *RetTy = CLI.RetTy;
  1366. MVT RetVT;
  1367. if (RetTy->isVoidTy())
  1368. RetVT = MVT::isVoid;
  1369. else if (!isTypeLegal(RetTy, RetVT) && RetVT != MVT::i16 &&
  1370. RetVT != MVT::i8)
  1371. return false;
  1372. else if (RetVT == MVT::i1 && Subtarget->useCRBits())
  1373. // We can't handle boolean returns when CR bits are in use.
  1374. return false;
  1375. // FIXME: No multi-register return values yet.
  1376. if (RetVT != MVT::isVoid && RetVT != MVT::i8 && RetVT != MVT::i16 &&
  1377. RetVT != MVT::i32 && RetVT != MVT::i64 && RetVT != MVT::f32 &&
  1378. RetVT != MVT::f64) {
  1379. SmallVector<CCValAssign, 16> RVLocs;
  1380. CCState CCInfo(CC, IsVarArg, *FuncInfo.MF, RVLocs, *Context);
  1381. CCInfo.AnalyzeCallResult(RetVT, RetCC_PPC64_ELF_FIS);
  1382. if (RVLocs.size() > 1)
  1383. return false;
  1384. }
  1385. // Bail early if more than 8 arguments, as we only currently
  1386. // handle arguments passed in registers.
  1387. unsigned NumArgs = CLI.OutVals.size();
  1388. if (NumArgs > 8)
  1389. return false;
  1390. // Set up the argument vectors.
  1391. SmallVector<Value*, 8> Args;
  1392. SmallVector<unsigned, 8> ArgRegs;
  1393. SmallVector<MVT, 8> ArgVTs;
  1394. SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
  1395. Args.reserve(NumArgs);
  1396. ArgRegs.reserve(NumArgs);
  1397. ArgVTs.reserve(NumArgs);
  1398. ArgFlags.reserve(NumArgs);
  1399. for (unsigned i = 0, ie = NumArgs; i != ie; ++i) {
  1400. // Only handle easy calls for now. It would be reasonably easy
  1401. // to handle <= 8-byte structures passed ByVal in registers, but we
  1402. // have to ensure they are right-justified in the register.
  1403. ISD::ArgFlagsTy Flags = CLI.OutFlags[i];
  1404. if (Flags.isInReg() || Flags.isSRet() || Flags.isNest() || Flags.isByVal())
  1405. return false;
  1406. Value *ArgValue = CLI.OutVals[i];
  1407. Type *ArgTy = ArgValue->getType();
  1408. MVT ArgVT;
  1409. if (!isTypeLegal(ArgTy, ArgVT) && ArgVT != MVT::i16 && ArgVT != MVT::i8)
  1410. return false;
  1411. // FIXME: FastISel cannot handle non-simple types yet, including 128-bit FP
  1412. // types, which is passed through vector register. Skip these types and
  1413. // fallback to default SelectionDAG based selection.
  1414. if (ArgVT.isVector() || ArgVT == MVT::f128)
  1415. return false;
  1416. Register Arg = getRegForValue(ArgValue);
  1417. if (Arg == 0)
  1418. return false;
  1419. Args.push_back(ArgValue);
  1420. ArgRegs.push_back(Arg);
  1421. ArgVTs.push_back(ArgVT);
  1422. ArgFlags.push_back(Flags);
  1423. }
  1424. // Process the arguments.
  1425. SmallVector<unsigned, 8> RegArgs;
  1426. unsigned NumBytes;
  1427. if (!processCallArgs(Args, ArgRegs, ArgVTs, ArgFlags,
  1428. RegArgs, CC, NumBytes, IsVarArg))
  1429. return false;
  1430. MachineInstrBuilder MIB;
  1431. // FIXME: No handling for function pointers yet. This requires
  1432. // implementing the function descriptor (OPD) setup.
  1433. const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
  1434. if (!GV) {
  1435. // patchpoints are a special case; they always dispatch to a pointer value.
  1436. // However, we don't actually want to generate the indirect call sequence
  1437. // here (that will be generated, as necessary, during asm printing), and
  1438. // the call we generate here will be erased by FastISel::selectPatchpoint,
  1439. // so don't try very hard...
  1440. if (CLI.IsPatchPoint)
  1441. MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::NOP));
  1442. else
  1443. return false;
  1444. } else {
  1445. // Build direct call with NOP for TOC restore.
  1446. // FIXME: We can and should optimize away the NOP for local calls.
  1447. MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1448. TII.get(PPC::BL8_NOP));
  1449. // Add callee.
  1450. MIB.addGlobalAddress(GV);
  1451. }
  1452. // Add implicit physical register uses to the call.
  1453. for (unsigned II = 0, IE = RegArgs.size(); II != IE; ++II)
  1454. MIB.addReg(RegArgs[II], RegState::Implicit);
  1455. // Direct calls, in both the ELF V1 and V2 ABIs, need the TOC register live
  1456. // into the call.
  1457. PPCFuncInfo->setUsesTOCBasePtr();
  1458. MIB.addReg(PPC::X2, RegState::Implicit);
  1459. // Add a register mask with the call-preserved registers. Proper
  1460. // defs for return values will be added by setPhysRegsDeadExcept().
  1461. MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
  1462. CLI.Call = MIB;
  1463. // Finish off the call including any return values.
  1464. return finishCall(RetVT, CLI, NumBytes);
  1465. }
  1466. // Attempt to fast-select a return instruction.
  1467. bool PPCFastISel::SelectRet(const Instruction *I) {
  1468. if (!FuncInfo.CanLowerReturn)
  1469. return false;
  1470. const ReturnInst *Ret = cast<ReturnInst>(I);
  1471. const Function &F = *I->getParent()->getParent();
  1472. // Build a list of return value registers.
  1473. SmallVector<unsigned, 4> RetRegs;
  1474. CallingConv::ID CC = F.getCallingConv();
  1475. if (Ret->getNumOperands() > 0) {
  1476. SmallVector<ISD::OutputArg, 4> Outs;
  1477. GetReturnInfo(CC, F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
  1478. // Analyze operands of the call, assigning locations to each operand.
  1479. SmallVector<CCValAssign, 16> ValLocs;
  1480. CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, *Context);
  1481. CCInfo.AnalyzeReturn(Outs, RetCC_PPC64_ELF_FIS);
  1482. const Value *RV = Ret->getOperand(0);
  1483. // FIXME: Only one output register for now.
  1484. if (ValLocs.size() > 1)
  1485. return false;
  1486. // Special case for returning a constant integer of any size - materialize
  1487. // the constant as an i64 and copy it to the return register.
  1488. if (const ConstantInt *CI = dyn_cast<ConstantInt>(RV)) {
  1489. CCValAssign &VA = ValLocs[0];
  1490. Register RetReg = VA.getLocReg();
  1491. // We still need to worry about properly extending the sign. For example,
  1492. // we could have only a single bit or a constant that needs zero
  1493. // extension rather than sign extension. Make sure we pass the return
  1494. // value extension property to integer materialization.
  1495. unsigned SrcReg =
  1496. PPCMaterializeInt(CI, MVT::i64, VA.getLocInfo() != CCValAssign::ZExt);
  1497. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1498. TII.get(TargetOpcode::COPY), RetReg).addReg(SrcReg);
  1499. RetRegs.push_back(RetReg);
  1500. } else {
  1501. Register Reg = getRegForValue(RV);
  1502. if (Reg == 0)
  1503. return false;
  1504. // Copy the result values into the output registers.
  1505. for (unsigned i = 0; i < ValLocs.size(); ++i) {
  1506. CCValAssign &VA = ValLocs[i];
  1507. assert(VA.isRegLoc() && "Can only return in registers!");
  1508. RetRegs.push_back(VA.getLocReg());
  1509. unsigned SrcReg = Reg + VA.getValNo();
  1510. EVT RVEVT = TLI.getValueType(DL, RV->getType());
  1511. if (!RVEVT.isSimple())
  1512. return false;
  1513. MVT RVVT = RVEVT.getSimpleVT();
  1514. MVT DestVT = VA.getLocVT();
  1515. if (RVVT != DestVT && RVVT != MVT::i8 &&
  1516. RVVT != MVT::i16 && RVVT != MVT::i32)
  1517. return false;
  1518. if (RVVT != DestVT) {
  1519. switch (VA.getLocInfo()) {
  1520. default:
  1521. llvm_unreachable("Unknown loc info!");
  1522. case CCValAssign::Full:
  1523. llvm_unreachable("Full value assign but types don't match?");
  1524. case CCValAssign::AExt:
  1525. case CCValAssign::ZExt: {
  1526. const TargetRegisterClass *RC =
  1527. (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
  1528. Register TmpReg = createResultReg(RC);
  1529. if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, true))
  1530. return false;
  1531. SrcReg = TmpReg;
  1532. break;
  1533. }
  1534. case CCValAssign::SExt: {
  1535. const TargetRegisterClass *RC =
  1536. (DestVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass;
  1537. Register TmpReg = createResultReg(RC);
  1538. if (!PPCEmitIntExt(RVVT, SrcReg, DestVT, TmpReg, false))
  1539. return false;
  1540. SrcReg = TmpReg;
  1541. break;
  1542. }
  1543. }
  1544. }
  1545. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1546. TII.get(TargetOpcode::COPY), RetRegs[i])
  1547. .addReg(SrcReg);
  1548. }
  1549. }
  1550. }
  1551. MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1552. TII.get(PPC::BLR8));
  1553. for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
  1554. MIB.addReg(RetRegs[i], RegState::Implicit);
  1555. return true;
  1556. }
  1557. // Attempt to emit an integer extend of SrcReg into DestReg. Both
  1558. // signed and zero extensions are supported. Return false if we
  1559. // can't handle it.
  1560. bool PPCFastISel::PPCEmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
  1561. unsigned DestReg, bool IsZExt) {
  1562. if (DestVT != MVT::i32 && DestVT != MVT::i64)
  1563. return false;
  1564. if (SrcVT != MVT::i8 && SrcVT != MVT::i16 && SrcVT != MVT::i32)
  1565. return false;
  1566. // Signed extensions use EXTSB, EXTSH, EXTSW.
  1567. if (!IsZExt) {
  1568. unsigned Opc;
  1569. if (SrcVT == MVT::i8)
  1570. Opc = (DestVT == MVT::i32) ? PPC::EXTSB : PPC::EXTSB8_32_64;
  1571. else if (SrcVT == MVT::i16)
  1572. Opc = (DestVT == MVT::i32) ? PPC::EXTSH : PPC::EXTSH8_32_64;
  1573. else {
  1574. assert(DestVT == MVT::i64 && "Signed extend from i32 to i32??");
  1575. Opc = PPC::EXTSW_32_64;
  1576. }
  1577. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  1578. .addReg(SrcReg);
  1579. // Unsigned 32-bit extensions use RLWINM.
  1580. } else if (DestVT == MVT::i32) {
  1581. unsigned MB;
  1582. if (SrcVT == MVT::i8)
  1583. MB = 24;
  1584. else {
  1585. assert(SrcVT == MVT::i16 && "Unsigned extend from i32 to i32??");
  1586. MB = 16;
  1587. }
  1588. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::RLWINM),
  1589. DestReg)
  1590. .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB).addImm(/*ME=*/31);
  1591. // Unsigned 64-bit extensions use RLDICL (with a 32-bit source).
  1592. } else {
  1593. unsigned MB;
  1594. if (SrcVT == MVT::i8)
  1595. MB = 56;
  1596. else if (SrcVT == MVT::i16)
  1597. MB = 48;
  1598. else
  1599. MB = 32;
  1600. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1601. TII.get(PPC::RLDICL_32_64), DestReg)
  1602. .addReg(SrcReg).addImm(/*SH=*/0).addImm(MB);
  1603. }
  1604. return true;
  1605. }
  1606. // Attempt to fast-select an indirect branch instruction.
  1607. bool PPCFastISel::SelectIndirectBr(const Instruction *I) {
  1608. Register AddrReg = getRegForValue(I->getOperand(0));
  1609. if (AddrReg == 0)
  1610. return false;
  1611. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::MTCTR8))
  1612. .addReg(AddrReg);
  1613. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::BCTR8));
  1614. const IndirectBrInst *IB = cast<IndirectBrInst>(I);
  1615. for (const BasicBlock *SuccBB : IB->successors())
  1616. FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[SuccBB]);
  1617. return true;
  1618. }
  1619. // Attempt to fast-select an integer truncate instruction.
  1620. bool PPCFastISel::SelectTrunc(const Instruction *I) {
  1621. Value *Src = I->getOperand(0);
  1622. EVT SrcVT = TLI.getValueType(DL, Src->getType(), true);
  1623. EVT DestVT = TLI.getValueType(DL, I->getType(), true);
  1624. if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16)
  1625. return false;
  1626. if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8)
  1627. return false;
  1628. Register SrcReg = getRegForValue(Src);
  1629. if (!SrcReg)
  1630. return false;
  1631. // The only interesting case is when we need to switch register classes.
  1632. if (SrcVT == MVT::i64)
  1633. SrcReg = copyRegToRegClass(&PPC::GPRCRegClass, SrcReg, 0, PPC::sub_32);
  1634. updateValueMap(I, SrcReg);
  1635. return true;
  1636. }
  1637. // Attempt to fast-select an integer extend instruction.
  1638. bool PPCFastISel::SelectIntExt(const Instruction *I) {
  1639. Type *DestTy = I->getType();
  1640. Value *Src = I->getOperand(0);
  1641. Type *SrcTy = Src->getType();
  1642. bool IsZExt = isa<ZExtInst>(I);
  1643. Register SrcReg = getRegForValue(Src);
  1644. if (!SrcReg) return false;
  1645. EVT SrcEVT, DestEVT;
  1646. SrcEVT = TLI.getValueType(DL, SrcTy, true);
  1647. DestEVT = TLI.getValueType(DL, DestTy, true);
  1648. if (!SrcEVT.isSimple())
  1649. return false;
  1650. if (!DestEVT.isSimple())
  1651. return false;
  1652. MVT SrcVT = SrcEVT.getSimpleVT();
  1653. MVT DestVT = DestEVT.getSimpleVT();
  1654. // If we know the register class needed for the result of this
  1655. // instruction, use it. Otherwise pick the register class of the
  1656. // correct size that does not contain X0/R0, since we don't know
  1657. // whether downstream uses permit that assignment.
  1658. Register AssignedReg = FuncInfo.ValueMap[I];
  1659. const TargetRegisterClass *RC =
  1660. (AssignedReg ? MRI.getRegClass(AssignedReg) :
  1661. (DestVT == MVT::i64 ? &PPC::G8RC_and_G8RC_NOX0RegClass :
  1662. &PPC::GPRC_and_GPRC_NOR0RegClass));
  1663. Register ResultReg = createResultReg(RC);
  1664. if (!PPCEmitIntExt(SrcVT, SrcReg, DestVT, ResultReg, IsZExt))
  1665. return false;
  1666. updateValueMap(I, ResultReg);
  1667. return true;
  1668. }
  1669. // Attempt to fast-select an instruction that wasn't handled by
  1670. // the table-generated machinery.
  1671. bool PPCFastISel::fastSelectInstruction(const Instruction *I) {
  1672. switch (I->getOpcode()) {
  1673. case Instruction::Load:
  1674. return SelectLoad(I);
  1675. case Instruction::Store:
  1676. return SelectStore(I);
  1677. case Instruction::Br:
  1678. return SelectBranch(I);
  1679. case Instruction::IndirectBr:
  1680. return SelectIndirectBr(I);
  1681. case Instruction::FPExt:
  1682. return SelectFPExt(I);
  1683. case Instruction::FPTrunc:
  1684. return SelectFPTrunc(I);
  1685. case Instruction::SIToFP:
  1686. return SelectIToFP(I, /*IsSigned*/ true);
  1687. case Instruction::UIToFP:
  1688. return SelectIToFP(I, /*IsSigned*/ false);
  1689. case Instruction::FPToSI:
  1690. return SelectFPToI(I, /*IsSigned*/ true);
  1691. case Instruction::FPToUI:
  1692. return SelectFPToI(I, /*IsSigned*/ false);
  1693. case Instruction::Add:
  1694. return SelectBinaryIntOp(I, ISD::ADD);
  1695. case Instruction::Or:
  1696. return SelectBinaryIntOp(I, ISD::OR);
  1697. case Instruction::Sub:
  1698. return SelectBinaryIntOp(I, ISD::SUB);
  1699. case Instruction::Ret:
  1700. return SelectRet(I);
  1701. case Instruction::Trunc:
  1702. return SelectTrunc(I);
  1703. case Instruction::ZExt:
  1704. case Instruction::SExt:
  1705. return SelectIntExt(I);
  1706. // Here add other flavors of Instruction::XXX that automated
  1707. // cases don't catch. For example, switches are terminators
  1708. // that aren't yet handled.
  1709. default:
  1710. break;
  1711. }
  1712. return false;
  1713. }
  1714. // Materialize a floating-point constant into a register, and return
  1715. // the register number (or zero if we failed to handle it).
  1716. unsigned PPCFastISel::PPCMaterializeFP(const ConstantFP *CFP, MVT VT) {
  1717. // If this is a PC-Rel function, let SDISel handle constant pool.
  1718. if (Subtarget->isUsingPCRelativeCalls())
  1719. return false;
  1720. // No plans to handle long double here.
  1721. if (VT != MVT::f32 && VT != MVT::f64)
  1722. return 0;
  1723. // All FP constants are loaded from the constant pool.
  1724. Align Alignment = DL.getPrefTypeAlign(CFP->getType());
  1725. unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Alignment);
  1726. const bool HasSPE = Subtarget->hasSPE();
  1727. const TargetRegisterClass *RC;
  1728. if (HasSPE)
  1729. RC = ((VT == MVT::f32) ? &PPC::GPRCRegClass : &PPC::SPERCRegClass);
  1730. else
  1731. RC = ((VT == MVT::f32) ? &PPC::F4RCRegClass : &PPC::F8RCRegClass);
  1732. Register DestReg = createResultReg(RC);
  1733. CodeModel::Model CModel = TM.getCodeModel();
  1734. MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
  1735. MachinePointerInfo::getConstantPool(*FuncInfo.MF),
  1736. MachineMemOperand::MOLoad, (VT == MVT::f32) ? 4 : 8, Alignment);
  1737. unsigned Opc;
  1738. if (HasSPE)
  1739. Opc = ((VT == MVT::f32) ? PPC::SPELWZ : PPC::EVLDD);
  1740. else
  1741. Opc = ((VT == MVT::f32) ? PPC::LFS : PPC::LFD);
  1742. Register TmpReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
  1743. PPCFuncInfo->setUsesTOCBasePtr();
  1744. // For small code model, generate a LF[SD](0, LDtocCPT(Idx, X2)).
  1745. if (CModel == CodeModel::Small) {
  1746. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocCPT),
  1747. TmpReg)
  1748. .addConstantPoolIndex(Idx).addReg(PPC::X2);
  1749. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  1750. .addImm(0).addReg(TmpReg).addMemOperand(MMO);
  1751. } else {
  1752. // Otherwise we generate LF[SD](Idx[lo], ADDIStocHA8(X2, Idx)).
  1753. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDIStocHA8),
  1754. TmpReg).addReg(PPC::X2).addConstantPoolIndex(Idx);
  1755. // But for large code model, we must generate a LDtocL followed
  1756. // by the LF[SD].
  1757. if (CModel == CodeModel::Large) {
  1758. Register TmpReg2 = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
  1759. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocL),
  1760. TmpReg2).addConstantPoolIndex(Idx).addReg(TmpReg);
  1761. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  1762. .addImm(0)
  1763. .addReg(TmpReg2);
  1764. } else
  1765. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), DestReg)
  1766. .addConstantPoolIndex(Idx, 0, PPCII::MO_TOC_LO)
  1767. .addReg(TmpReg)
  1768. .addMemOperand(MMO);
  1769. }
  1770. return DestReg;
  1771. }
  1772. // Materialize the address of a global value into a register, and return
  1773. // the register number (or zero if we failed to handle it).
  1774. unsigned PPCFastISel::PPCMaterializeGV(const GlobalValue *GV, MVT VT) {
  1775. // If this is a PC-Rel function, let SDISel handle GV materialization.
  1776. if (Subtarget->isUsingPCRelativeCalls())
  1777. return false;
  1778. assert(VT == MVT::i64 && "Non-address!");
  1779. const TargetRegisterClass *RC = &PPC::G8RC_and_G8RC_NOX0RegClass;
  1780. Register DestReg = createResultReg(RC);
  1781. // Global values may be plain old object addresses, TLS object
  1782. // addresses, constant pool entries, or jump tables. How we generate
  1783. // code for these may depend on small, medium, or large code model.
  1784. CodeModel::Model CModel = TM.getCodeModel();
  1785. // FIXME: Jump tables are not yet required because fast-isel doesn't
  1786. // handle switches; if that changes, we need them as well. For now,
  1787. // what follows assumes everything's a generic (or TLS) global address.
  1788. // FIXME: We don't yet handle the complexity of TLS.
  1789. if (GV->isThreadLocal())
  1790. return 0;
  1791. // If the global has the toc-data attribute then fallback to DAG-ISEL.
  1792. if (TM.getTargetTriple().isOSAIX())
  1793. if (const GlobalVariable *Var = dyn_cast_or_null<GlobalVariable>(GV))
  1794. if (Var->hasAttribute("toc-data"))
  1795. return false;
  1796. PPCFuncInfo->setUsesTOCBasePtr();
  1797. // For small code model, generate a simple TOC load.
  1798. if (CModel == CodeModel::Small)
  1799. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtoc),
  1800. DestReg)
  1801. .addGlobalAddress(GV)
  1802. .addReg(PPC::X2);
  1803. else {
  1804. // If the address is an externally defined symbol, a symbol with common
  1805. // or externally available linkage, a non-local function address, or a
  1806. // jump table address (not yet needed), or if we are generating code
  1807. // for large code model, we generate:
  1808. // LDtocL(GV, ADDIStocHA8(%x2, GV))
  1809. // Otherwise we generate:
  1810. // ADDItocL(ADDIStocHA8(%x2, GV), GV)
  1811. // Either way, start with the ADDIStocHA8:
  1812. Register HighPartReg = createResultReg(RC);
  1813. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDIStocHA8),
  1814. HighPartReg).addReg(PPC::X2).addGlobalAddress(GV);
  1815. if (Subtarget->isGVIndirectSymbol(GV)) {
  1816. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::LDtocL),
  1817. DestReg).addGlobalAddress(GV).addReg(HighPartReg);
  1818. } else {
  1819. // Otherwise generate the ADDItocL.
  1820. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDItocL),
  1821. DestReg).addReg(HighPartReg).addGlobalAddress(GV);
  1822. }
  1823. }
  1824. return DestReg;
  1825. }
  1826. // Materialize a 32-bit integer constant into a register, and return
  1827. // the register number (or zero if we failed to handle it).
  1828. unsigned PPCFastISel::PPCMaterialize32BitInt(int64_t Imm,
  1829. const TargetRegisterClass *RC) {
  1830. unsigned Lo = Imm & 0xFFFF;
  1831. unsigned Hi = (Imm >> 16) & 0xFFFF;
  1832. Register ResultReg = createResultReg(RC);
  1833. bool IsGPRC = RC->hasSuperClassEq(&PPC::GPRCRegClass);
  1834. if (isInt<16>(Imm))
  1835. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1836. TII.get(IsGPRC ? PPC::LI : PPC::LI8), ResultReg)
  1837. .addImm(Imm);
  1838. else if (Lo) {
  1839. // Both Lo and Hi have nonzero bits.
  1840. Register TmpReg = createResultReg(RC);
  1841. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1842. TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), TmpReg)
  1843. .addImm(Hi);
  1844. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1845. TII.get(IsGPRC ? PPC::ORI : PPC::ORI8), ResultReg)
  1846. .addReg(TmpReg).addImm(Lo);
  1847. } else
  1848. // Just Hi bits.
  1849. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1850. TII.get(IsGPRC ? PPC::LIS : PPC::LIS8), ResultReg)
  1851. .addImm(Hi);
  1852. return ResultReg;
  1853. }
  1854. // Materialize a 64-bit integer constant into a register, and return
  1855. // the register number (or zero if we failed to handle it).
  1856. unsigned PPCFastISel::PPCMaterialize64BitInt(int64_t Imm,
  1857. const TargetRegisterClass *RC) {
  1858. unsigned Remainder = 0;
  1859. unsigned Shift = 0;
  1860. // If the value doesn't fit in 32 bits, see if we can shift it
  1861. // so that it fits in 32 bits.
  1862. if (!isInt<32>(Imm)) {
  1863. Shift = countTrailingZeros<uint64_t>(Imm);
  1864. int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
  1865. if (isInt<32>(ImmSh))
  1866. Imm = ImmSh;
  1867. else {
  1868. Remainder = Imm;
  1869. Shift = 32;
  1870. Imm >>= 32;
  1871. }
  1872. }
  1873. // Handle the high-order 32 bits (if shifted) or the whole 32 bits
  1874. // (if not shifted).
  1875. unsigned TmpReg1 = PPCMaterialize32BitInt(Imm, RC);
  1876. if (!Shift)
  1877. return TmpReg1;
  1878. // If upper 32 bits were not zero, we've built them and need to shift
  1879. // them into place.
  1880. unsigned TmpReg2;
  1881. if (Imm) {
  1882. TmpReg2 = createResultReg(RC);
  1883. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::RLDICR),
  1884. TmpReg2).addReg(TmpReg1).addImm(Shift).addImm(63 - Shift);
  1885. } else
  1886. TmpReg2 = TmpReg1;
  1887. unsigned TmpReg3, Hi, Lo;
  1888. if ((Hi = (Remainder >> 16) & 0xFFFF)) {
  1889. TmpReg3 = createResultReg(RC);
  1890. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ORIS8),
  1891. TmpReg3).addReg(TmpReg2).addImm(Hi);
  1892. } else
  1893. TmpReg3 = TmpReg2;
  1894. if ((Lo = Remainder & 0xFFFF)) {
  1895. Register ResultReg = createResultReg(RC);
  1896. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ORI8),
  1897. ResultReg).addReg(TmpReg3).addImm(Lo);
  1898. return ResultReg;
  1899. }
  1900. return TmpReg3;
  1901. }
  1902. // Materialize an integer constant into a register, and return
  1903. // the register number (or zero if we failed to handle it).
  1904. unsigned PPCFastISel::PPCMaterializeInt(const ConstantInt *CI, MVT VT,
  1905. bool UseSExt) {
  1906. // If we're using CR bit registers for i1 values, handle that as a special
  1907. // case first.
  1908. if (VT == MVT::i1 && Subtarget->useCRBits()) {
  1909. Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);
  1910. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  1911. TII.get(CI->isZero() ? PPC::CRUNSET : PPC::CRSET), ImmReg);
  1912. return ImmReg;
  1913. }
  1914. if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
  1915. VT != MVT::i1)
  1916. return 0;
  1917. const TargetRegisterClass *RC =
  1918. ((VT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass);
  1919. int64_t Imm = UseSExt ? CI->getSExtValue() : CI->getZExtValue();
  1920. // If the constant is in range, use a load-immediate.
  1921. // Since LI will sign extend the constant we need to make sure that for
  1922. // our zeroext constants that the sign extended constant fits into 16-bits -
  1923. // a range of 0..0x7fff.
  1924. if (isInt<16>(Imm)) {
  1925. unsigned Opc = (VT == MVT::i64) ? PPC::LI8 : PPC::LI;
  1926. Register ImmReg = createResultReg(RC);
  1927. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(Opc), ImmReg)
  1928. .addImm(Imm);
  1929. return ImmReg;
  1930. }
  1931. // Construct the constant piecewise.
  1932. if (VT == MVT::i64)
  1933. return PPCMaterialize64BitInt(Imm, RC);
  1934. else if (VT == MVT::i32)
  1935. return PPCMaterialize32BitInt(Imm, RC);
  1936. return 0;
  1937. }
  1938. // Materialize a constant into a register, and return the register
  1939. // number (or zero if we failed to handle it).
  1940. unsigned PPCFastISel::fastMaterializeConstant(const Constant *C) {
  1941. EVT CEVT = TLI.getValueType(DL, C->getType(), true);
  1942. // Only handle simple types.
  1943. if (!CEVT.isSimple()) return 0;
  1944. MVT VT = CEVT.getSimpleVT();
  1945. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
  1946. return PPCMaterializeFP(CFP, VT);
  1947. else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
  1948. return PPCMaterializeGV(GV, VT);
  1949. else if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
  1950. // Note that the code in FunctionLoweringInfo::ComputePHILiveOutRegInfo
  1951. // assumes that constant PHI operands will be zero extended, and failure to
  1952. // match that assumption will cause problems if we sign extend here but
  1953. // some user of a PHI is in a block for which we fall back to full SDAG
  1954. // instruction selection.
  1955. return PPCMaterializeInt(CI, VT, false);
  1956. return 0;
  1957. }
  1958. // Materialize the address created by an alloca into a register, and
  1959. // return the register number (or zero if we failed to handle it).
  1960. unsigned PPCFastISel::fastMaterializeAlloca(const AllocaInst *AI) {
  1961. // Don't handle dynamic allocas.
  1962. if (!FuncInfo.StaticAllocaMap.count(AI)) return 0;
  1963. MVT VT;
  1964. if (!isLoadTypeLegal(AI->getType(), VT)) return 0;
  1965. DenseMap<const AllocaInst*, int>::iterator SI =
  1966. FuncInfo.StaticAllocaMap.find(AI);
  1967. if (SI != FuncInfo.StaticAllocaMap.end()) {
  1968. Register ResultReg = createResultReg(&PPC::G8RC_and_G8RC_NOX0RegClass);
  1969. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD, TII.get(PPC::ADDI8),
  1970. ResultReg).addFrameIndex(SI->second).addImm(0);
  1971. return ResultReg;
  1972. }
  1973. return 0;
  1974. }
  1975. // Fold loads into extends when possible.
  1976. // FIXME: We can have multiple redundant extend/trunc instructions
  1977. // following a load. The folding only picks up one. Extend this
  1978. // to check subsequent instructions for the same pattern and remove
  1979. // them. Thus ResultReg should be the def reg for the last redundant
  1980. // instruction in a chain, and all intervening instructions can be
  1981. // removed from parent. Change test/CodeGen/PowerPC/fast-isel-fold.ll
  1982. // to add ELF64-NOT: rldicl to the appropriate tests when this works.
  1983. bool PPCFastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
  1984. const LoadInst *LI) {
  1985. // Verify we have a legal type before going any further.
  1986. MVT VT;
  1987. if (!isLoadTypeLegal(LI->getType(), VT))
  1988. return false;
  1989. // Combine load followed by zero- or sign-extend.
  1990. bool IsZExt = false;
  1991. switch(MI->getOpcode()) {
  1992. default:
  1993. return false;
  1994. case PPC::RLDICL:
  1995. case PPC::RLDICL_32_64: {
  1996. IsZExt = true;
  1997. unsigned MB = MI->getOperand(3).getImm();
  1998. if ((VT == MVT::i8 && MB <= 56) ||
  1999. (VT == MVT::i16 && MB <= 48) ||
  2000. (VT == MVT::i32 && MB <= 32))
  2001. break;
  2002. return false;
  2003. }
  2004. case PPC::RLWINM:
  2005. case PPC::RLWINM8: {
  2006. IsZExt = true;
  2007. unsigned MB = MI->getOperand(3).getImm();
  2008. if ((VT == MVT::i8 && MB <= 24) ||
  2009. (VT == MVT::i16 && MB <= 16))
  2010. break;
  2011. return false;
  2012. }
  2013. case PPC::EXTSB:
  2014. case PPC::EXTSB8:
  2015. case PPC::EXTSB8_32_64:
  2016. /* There is no sign-extending load-byte instruction. */
  2017. return false;
  2018. case PPC::EXTSH:
  2019. case PPC::EXTSH8:
  2020. case PPC::EXTSH8_32_64: {
  2021. if (VT != MVT::i16 && VT != MVT::i8)
  2022. return false;
  2023. break;
  2024. }
  2025. case PPC::EXTSW:
  2026. case PPC::EXTSW_32:
  2027. case PPC::EXTSW_32_64: {
  2028. if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8)
  2029. return false;
  2030. break;
  2031. }
  2032. }
  2033. // See if we can handle this address.
  2034. Address Addr;
  2035. if (!PPCComputeAddress(LI->getOperand(0), Addr))
  2036. return false;
  2037. Register ResultReg = MI->getOperand(0).getReg();
  2038. if (!PPCEmitLoad(VT, ResultReg, Addr, nullptr, IsZExt,
  2039. Subtarget->hasSPE() ? PPC::EVLDD : PPC::LFD))
  2040. return false;
  2041. MachineBasicBlock::iterator I(MI);
  2042. removeDeadCode(I, std::next(I));
  2043. return true;
  2044. }
  2045. // Attempt to lower call arguments in a faster way than done by
  2046. // the selection DAG code.
  2047. bool PPCFastISel::fastLowerArguments() {
  2048. // Defer to normal argument lowering for now. It's reasonably
  2049. // efficient. Consider doing something like ARM to handle the
  2050. // case where all args fit in registers, no varargs, no float
  2051. // or vector args.
  2052. return false;
  2053. }
  2054. // Handle materializing integer constants into a register. This is not
  2055. // automatically generated for PowerPC, so must be explicitly created here.
  2056. unsigned PPCFastISel::fastEmit_i(MVT Ty, MVT VT, unsigned Opc, uint64_t Imm) {
  2057. if (Opc != ISD::Constant)
  2058. return 0;
  2059. // If we're using CR bit registers for i1 values, handle that as a special
  2060. // case first.
  2061. if (VT == MVT::i1 && Subtarget->useCRBits()) {
  2062. Register ImmReg = createResultReg(&PPC::CRBITRCRegClass);
  2063. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
  2064. TII.get(Imm == 0 ? PPC::CRUNSET : PPC::CRSET), ImmReg);
  2065. return ImmReg;
  2066. }
  2067. if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 &&
  2068. VT != MVT::i1)
  2069. return 0;
  2070. const TargetRegisterClass *RC = ((VT == MVT::i64) ? &PPC::G8RCRegClass :
  2071. &PPC::GPRCRegClass);
  2072. if (VT == MVT::i64)
  2073. return PPCMaterialize64BitInt(Imm, RC);
  2074. else
  2075. return PPCMaterialize32BitInt(Imm, RC);
  2076. }
  2077. // Override for ADDI and ADDI8 to set the correct register class
  2078. // on RHS operand 0. The automatic infrastructure naively assumes
  2079. // GPRC for i32 and G8RC for i64; the concept of "no R0" is lost
  2080. // for these cases. At the moment, none of the other automatically
  2081. // generated RI instructions require special treatment. However, once
  2082. // SelectSelect is implemented, "isel" requires similar handling.
  2083. //
  2084. // Also be conservative about the output register class. Avoid
  2085. // assigning R0 or X0 to the output register for GPRC and G8RC
  2086. // register classes, as any such result could be used in ADDI, etc.,
  2087. // where those regs have another meaning.
  2088. unsigned PPCFastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
  2089. const TargetRegisterClass *RC,
  2090. unsigned Op0,
  2091. uint64_t Imm) {
  2092. if (MachineInstOpcode == PPC::ADDI)
  2093. MRI.setRegClass(Op0, &PPC::GPRC_and_GPRC_NOR0RegClass);
  2094. else if (MachineInstOpcode == PPC::ADDI8)
  2095. MRI.setRegClass(Op0, &PPC::G8RC_and_G8RC_NOX0RegClass);
  2096. const TargetRegisterClass *UseRC =
  2097. (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
  2098. (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
  2099. return FastISel::fastEmitInst_ri(MachineInstOpcode, UseRC, Op0, Imm);
  2100. }
  2101. // Override for instructions with one register operand to avoid use of
  2102. // R0/X0. The automatic infrastructure isn't aware of the context so
  2103. // we must be conservative.
  2104. unsigned PPCFastISel::fastEmitInst_r(unsigned MachineInstOpcode,
  2105. const TargetRegisterClass* RC,
  2106. unsigned Op0) {
  2107. const TargetRegisterClass *UseRC =
  2108. (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
  2109. (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
  2110. return FastISel::fastEmitInst_r(MachineInstOpcode, UseRC, Op0);
  2111. }
  2112. // Override for instructions with two register operands to avoid use
  2113. // of R0/X0. The automatic infrastructure isn't aware of the context
  2114. // so we must be conservative.
  2115. unsigned PPCFastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
  2116. const TargetRegisterClass* RC,
  2117. unsigned Op0, unsigned Op1) {
  2118. const TargetRegisterClass *UseRC =
  2119. (RC == &PPC::GPRCRegClass ? &PPC::GPRC_and_GPRC_NOR0RegClass :
  2120. (RC == &PPC::G8RCRegClass ? &PPC::G8RC_and_G8RC_NOX0RegClass : RC));
  2121. return FastISel::fastEmitInst_rr(MachineInstOpcode, UseRC, Op0, Op1);
  2122. }
  2123. namespace llvm {
  2124. // Create the fast instruction selector for PowerPC64 ELF.
  2125. FastISel *PPC::createFastISel(FunctionLoweringInfo &FuncInfo,
  2126. const TargetLibraryInfo *LibInfo) {
  2127. // Only available on 64-bit for now.
  2128. const PPCSubtarget &Subtarget = FuncInfo.MF->getSubtarget<PPCSubtarget>();
  2129. if (Subtarget.isPPC64())
  2130. return new PPCFastISel(FuncInfo, LibInfo);
  2131. return nullptr;
  2132. }
  2133. }