FastISel.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. //===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the implementation of the FastISel class.
  11. //
  12. // "Fast" instruction selection is designed to emit very poor code quickly.
  13. // Also, it is not designed to be able to do much lowering, so most illegal
  14. // types (e.g. i64 on 32-bit targets) and operations are not supported. It is
  15. // also not intended to be able to do much optimization, except in a few cases
  16. // where doing optimizations reduces overall compile time. For example, folding
  17. // constants into immediate fields is often done, because it's cheap and it
  18. // reduces the number of instructions later phases have to examine.
  19. //
  20. // "Fast" instruction selection is able to fail gracefully and transfer
  21. // control to the SelectionDAG selector for operations that it doesn't
  22. // support. In many cases, this allows us to avoid duplicating a lot of
  23. // the complicated lowering logic that SelectionDAG currently has.
  24. //
  25. // The intended use for "fast" instruction selection is "-O0" mode
  26. // compilation, where the quality of the generated code is irrelevant when
  27. // weighed against the speed at which the code can be generated. Also,
  28. // at -O0, the LLVM optimizers are not running, and this makes the
  29. // compile time of codegen a much higher portion of the overall compile
  30. // time. Despite its limitations, "fast" instruction selection is able to
  31. // handle enough code on its own to provide noticeable overall speedups
  32. // in -O0 compiles.
  33. //
  34. // Basic operations are supported in a target-independent way, by reading
  35. // the same instruction descriptions that the SelectionDAG selector reads,
  36. // and identifying simple arithmetic operations that can be directly selected
  37. // from simple operators. More complicated operations currently require
  38. // target-specific code.
  39. //
  40. //===----------------------------------------------------------------------===//
  41. #include "llvm/CodeGen/Analysis.h"
  42. #include "llvm/ADT/Optional.h"
  43. #include "llvm/ADT/Statistic.h"
  44. #include "llvm/Analysis/BranchProbabilityInfo.h"
  45. #include "llvm/Analysis/Loads.h"
  46. #include "llvm/Analysis/TargetLibraryInfo.h"
  47. #include "llvm/CodeGen/Analysis.h"
  48. #include "llvm/CodeGen/FastISel.h"
  49. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  50. #include "llvm/CodeGen/MachineFrameInfo.h"
  51. #include "llvm/CodeGen/MachineInstrBuilder.h"
  52. #include "llvm/CodeGen/MachineModuleInfo.h"
  53. #include "llvm/CodeGen/MachineRegisterInfo.h"
  54. #include "llvm/CodeGen/StackMaps.h"
  55. #include "llvm/IR/DataLayout.h"
  56. #include "llvm/IR/DebugInfo.h"
  57. #include "llvm/IR/Function.h"
  58. #include "llvm/IR/GlobalVariable.h"
  59. #include "llvm/IR/Instructions.h"
  60. #include "llvm/IR/IntrinsicInst.h"
  61. #include "llvm/IR/Operator.h"
  62. #include "llvm/Support/Debug.h"
  63. #include "llvm/Support/ErrorHandling.h"
  64. #include "llvm/Support/raw_ostream.h"
  65. #include "llvm/Target/TargetInstrInfo.h"
  66. #include "llvm/Target/TargetLowering.h"
  67. #include "llvm/Target/TargetMachine.h"
  68. #include "llvm/Target/TargetSubtargetInfo.h"
  69. using namespace llvm;
  70. #define DEBUG_TYPE "isel"
  71. STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
  72. "target-independent selector");
  73. STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
  74. "target-specific selector");
  75. STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
  76. void FastISel::ArgListEntry::setAttributes(ImmutableCallSite *CS,
  77. unsigned AttrIdx) {
  78. IsSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt);
  79. IsZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt);
  80. IsInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg);
  81. IsSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet);
  82. IsNest = CS->paramHasAttr(AttrIdx, Attribute::Nest);
  83. IsByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal);
  84. IsInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca);
  85. IsReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
  86. Alignment = CS->getParamAlignment(AttrIdx);
  87. }
  88. /// Set the current block to which generated machine instructions will be
  89. /// appended, and clear the local CSE map.
  90. void FastISel::startNewBlock() {
  91. LocalValueMap.clear();
  92. // Instructions are appended to FuncInfo.MBB. If the basic block already
  93. // contains labels or copies, use the last instruction as the last local
  94. // value.
  95. EmitStartPt = nullptr;
  96. if (!FuncInfo.MBB->empty())
  97. EmitStartPt = &FuncInfo.MBB->back();
  98. LastLocalValue = EmitStartPt;
  99. }
  100. bool FastISel::lowerArguments() {
  101. if (!FuncInfo.CanLowerReturn)
  102. // Fallback to SDISel argument lowering code to deal with sret pointer
  103. // parameter.
  104. return false;
  105. if (!fastLowerArguments())
  106. return false;
  107. // Enter arguments into ValueMap for uses in non-entry BBs.
  108. for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
  109. E = FuncInfo.Fn->arg_end();
  110. I != E; ++I) {
  111. DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
  112. assert(VI != LocalValueMap.end() && "Missed an argument?");
  113. FuncInfo.ValueMap[I] = VI->second;
  114. }
  115. return true;
  116. }
  117. void FastISel::flushLocalValueMap() {
  118. LocalValueMap.clear();
  119. LastLocalValue = EmitStartPt;
  120. recomputeInsertPt();
  121. SavedInsertPt = FuncInfo.InsertPt;
  122. }
  123. bool FastISel::hasTrivialKill(const Value *V) {
  124. // Don't consider constants or arguments to have trivial kills.
  125. const Instruction *I = dyn_cast<Instruction>(V);
  126. if (!I)
  127. return false;
  128. // No-op casts are trivially coalesced by fast-isel.
  129. if (const auto *Cast = dyn_cast<CastInst>(I))
  130. if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
  131. !hasTrivialKill(Cast->getOperand(0)))
  132. return false;
  133. // Even the value might have only one use in the LLVM IR, it is possible that
  134. // FastISel might fold the use into another instruction and now there is more
  135. // than one use at the Machine Instruction level.
  136. unsigned Reg = lookUpRegForValue(V);
  137. if (Reg && !MRI.use_empty(Reg))
  138. return false;
  139. // GEPs with all zero indices are trivially coalesced by fast-isel.
  140. if (const auto *GEP = dyn_cast<GetElementPtrInst>(I))
  141. if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
  142. return false;
  143. // Only instructions with a single use in the same basic block are considered
  144. // to have trivial kills.
  145. return I->hasOneUse() &&
  146. !(I->getOpcode() == Instruction::BitCast ||
  147. I->getOpcode() == Instruction::PtrToInt ||
  148. I->getOpcode() == Instruction::IntToPtr) &&
  149. cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
  150. }
  151. unsigned FastISel::getRegForValue(const Value *V) {
  152. EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
  153. // Don't handle non-simple values in FastISel.
  154. if (!RealVT.isSimple())
  155. return 0;
  156. // Ignore illegal types. We must do this before looking up the value
  157. // in ValueMap because Arguments are given virtual registers regardless
  158. // of whether FastISel can handle them.
  159. MVT VT = RealVT.getSimpleVT();
  160. if (!TLI.isTypeLegal(VT)) {
  161. // Handle integer promotions, though, because they're common and easy.
  162. if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
  163. VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
  164. else
  165. return 0;
  166. }
  167. // Look up the value to see if we already have a register for it.
  168. unsigned Reg = lookUpRegForValue(V);
  169. if (Reg)
  170. return Reg;
  171. // In bottom-up mode, just create the virtual register which will be used
  172. // to hold the value. It will be materialized later.
  173. if (isa<Instruction>(V) &&
  174. (!isa<AllocaInst>(V) ||
  175. !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
  176. return FuncInfo.InitializeRegForValue(V);
  177. SavePoint SaveInsertPt = enterLocalValueArea();
  178. // Materialize the value in a register. Emit any instructions in the
  179. // local value area.
  180. Reg = materializeRegForValue(V, VT);
  181. leaveLocalValueArea(SaveInsertPt);
  182. return Reg;
  183. }
  184. unsigned FastISel::materializeConstant(const Value *V, MVT VT) {
  185. unsigned Reg = 0;
  186. if (const auto *CI = dyn_cast<ConstantInt>(V)) {
  187. if (CI->getValue().getActiveBits() <= 64)
  188. Reg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
  189. } else if (isa<AllocaInst>(V))
  190. Reg = fastMaterializeAlloca(cast<AllocaInst>(V));
  191. else if (isa<ConstantPointerNull>(V))
  192. // Translate this as an integer zero so that it can be
  193. // local-CSE'd with actual integer zeros.
  194. Reg = getRegForValue(
  195. Constant::getNullValue(DL.getIntPtrType(V->getContext())));
  196. else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
  197. if (CF->isNullValue())
  198. Reg = fastMaterializeFloatZero(CF);
  199. else
  200. // Try to emit the constant directly.
  201. Reg = fastEmit_f(VT, VT, ISD::ConstantFP, CF);
  202. if (!Reg) {
  203. // Try to emit the constant by using an integer constant with a cast.
  204. const APFloat &Flt = CF->getValueAPF();
  205. EVT IntVT = TLI.getPointerTy();
  206. uint64_t x[2];
  207. uint32_t IntBitWidth = IntVT.getSizeInBits();
  208. bool isExact;
  209. (void)Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
  210. APFloat::rmTowardZero, &isExact);
  211. if (isExact) {
  212. APInt IntVal(IntBitWidth, x);
  213. unsigned IntegerReg =
  214. getRegForValue(ConstantInt::get(V->getContext(), IntVal));
  215. if (IntegerReg != 0)
  216. Reg = fastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg,
  217. /*Kill=*/false);
  218. }
  219. }
  220. } else if (const auto *Op = dyn_cast<Operator>(V)) {
  221. if (!selectOperator(Op, Op->getOpcode()))
  222. if (!isa<Instruction>(Op) ||
  223. !fastSelectInstruction(cast<Instruction>(Op)))
  224. return 0;
  225. Reg = lookUpRegForValue(Op);
  226. } else if (isa<UndefValue>(V)) {
  227. Reg = createResultReg(TLI.getRegClassFor(VT));
  228. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  229. TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
  230. }
  231. return Reg;
  232. }
  233. /// Helper for getRegForValue. This function is called when the value isn't
  234. /// already available in a register and must be materialized with new
  235. /// instructions.
  236. unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
  237. unsigned Reg = 0;
  238. // Give the target-specific code a try first.
  239. if (isa<Constant>(V))
  240. Reg = fastMaterializeConstant(cast<Constant>(V));
  241. // If target-specific code couldn't or didn't want to handle the value, then
  242. // give target-independent code a try.
  243. if (!Reg)
  244. Reg = materializeConstant(V, VT);
  245. // Don't cache constant materializations in the general ValueMap.
  246. // To do so would require tracking what uses they dominate.
  247. if (Reg) {
  248. LocalValueMap[V] = Reg;
  249. LastLocalValue = MRI.getVRegDef(Reg);
  250. }
  251. return Reg;
  252. }
  253. unsigned FastISel::lookUpRegForValue(const Value *V) {
  254. // Look up the value to see if we already have a register for it. We
  255. // cache values defined by Instructions across blocks, and other values
  256. // only locally. This is because Instructions already have the SSA
  257. // def-dominates-use requirement enforced.
  258. DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
  259. if (I != FuncInfo.ValueMap.end())
  260. return I->second;
  261. return LocalValueMap[V];
  262. }
  263. void FastISel::updateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
  264. if (!isa<Instruction>(I)) {
  265. LocalValueMap[I] = Reg;
  266. return;
  267. }
  268. unsigned &AssignedReg = FuncInfo.ValueMap[I];
  269. if (AssignedReg == 0)
  270. // Use the new register.
  271. AssignedReg = Reg;
  272. else if (Reg != AssignedReg) {
  273. // Arrange for uses of AssignedReg to be replaced by uses of Reg.
  274. for (unsigned i = 0; i < NumRegs; i++)
  275. FuncInfo.RegFixups[AssignedReg + i] = Reg + i;
  276. AssignedReg = Reg;
  277. }
  278. }
  279. std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
  280. unsigned IdxN = getRegForValue(Idx);
  281. if (IdxN == 0)
  282. // Unhandled operand. Halt "fast" selection and bail.
  283. return std::pair<unsigned, bool>(0, false);
  284. bool IdxNIsKill = hasTrivialKill(Idx);
  285. // If the index is smaller or larger than intptr_t, truncate or extend it.
  286. MVT PtrVT = TLI.getPointerTy();
  287. EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
  288. if (IdxVT.bitsLT(PtrVT)) {
  289. IdxN = fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN,
  290. IdxNIsKill);
  291. IdxNIsKill = true;
  292. } else if (IdxVT.bitsGT(PtrVT)) {
  293. IdxN =
  294. fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN, IdxNIsKill);
  295. IdxNIsKill = true;
  296. }
  297. return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
  298. }
  299. void FastISel::recomputeInsertPt() {
  300. if (getLastLocalValue()) {
  301. FuncInfo.InsertPt = getLastLocalValue();
  302. FuncInfo.MBB = FuncInfo.InsertPt->getParent();
  303. ++FuncInfo.InsertPt;
  304. } else
  305. FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
  306. // Now skip past any EH_LABELs, which must remain at the beginning.
  307. while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
  308. FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
  309. ++FuncInfo.InsertPt;
  310. }
  311. void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
  312. MachineBasicBlock::iterator E) {
  313. assert(I && E && std::distance(I, E) > 0 && "Invalid iterator!");
  314. while (I != E) {
  315. MachineInstr *Dead = &*I;
  316. ++I;
  317. Dead->eraseFromParent();
  318. ++NumFastIselDead;
  319. }
  320. recomputeInsertPt();
  321. }
  322. FastISel::SavePoint FastISel::enterLocalValueArea() {
  323. MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
  324. DebugLoc OldDL = DbgLoc;
  325. recomputeInsertPt();
  326. DbgLoc = DebugLoc();
  327. SavePoint SP = {OldInsertPt, OldDL};
  328. return SP;
  329. }
  330. void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
  331. if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
  332. LastLocalValue = std::prev(FuncInfo.InsertPt);
  333. // Restore the previous insert position.
  334. FuncInfo.InsertPt = OldInsertPt.InsertPt;
  335. DbgLoc = OldInsertPt.DL;
  336. }
  337. bool FastISel::selectBinaryOp(const User *I, unsigned ISDOpcode) {
  338. EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
  339. if (VT == MVT::Other || !VT.isSimple())
  340. // Unhandled type. Halt "fast" selection and bail.
  341. return false;
  342. // We only handle legal types. For example, on x86-32 the instruction
  343. // selector contains all of the 64-bit instructions from x86-64,
  344. // under the assumption that i64 won't be used if the target doesn't
  345. // support it.
  346. if (!TLI.isTypeLegal(VT)) {
  347. // MVT::i1 is special. Allow AND, OR, or XOR because they
  348. // don't require additional zeroing, which makes them easy.
  349. if (VT == MVT::i1 && (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
  350. ISDOpcode == ISD::XOR))
  351. VT = TLI.getTypeToTransformTo(I->getContext(), VT);
  352. else
  353. return false;
  354. }
  355. // Check if the first operand is a constant, and handle it as "ri". At -O0,
  356. // we don't have anything that canonicalizes operand order.
  357. if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
  358. if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
  359. unsigned Op1 = getRegForValue(I->getOperand(1));
  360. if (!Op1)
  361. return false;
  362. bool Op1IsKill = hasTrivialKill(I->getOperand(1));
  363. unsigned ResultReg =
  364. fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, Op1IsKill,
  365. CI->getZExtValue(), VT.getSimpleVT());
  366. if (!ResultReg)
  367. return false;
  368. // We successfully emitted code for the given LLVM Instruction.
  369. updateValueMap(I, ResultReg);
  370. return true;
  371. }
  372. unsigned Op0 = getRegForValue(I->getOperand(0));
  373. if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
  374. return false;
  375. bool Op0IsKill = hasTrivialKill(I->getOperand(0));
  376. // Check if the second operand is a constant and handle it appropriately.
  377. if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
  378. uint64_t Imm = CI->getZExtValue();
  379. // Transform "sdiv exact X, 8" -> "sra X, 3".
  380. if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
  381. cast<BinaryOperator>(I)->isExact() && isPowerOf2_64(Imm)) {
  382. Imm = Log2_64(Imm);
  383. ISDOpcode = ISD::SRA;
  384. }
  385. // Transform "urem x, pow2" -> "and x, pow2-1".
  386. if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
  387. isPowerOf2_64(Imm)) {
  388. --Imm;
  389. ISDOpcode = ISD::AND;
  390. }
  391. unsigned ResultReg = fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
  392. Op0IsKill, Imm, VT.getSimpleVT());
  393. if (!ResultReg)
  394. return false;
  395. // We successfully emitted code for the given LLVM Instruction.
  396. updateValueMap(I, ResultReg);
  397. return true;
  398. }
  399. // Check if the second operand is a constant float.
  400. if (const auto *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
  401. unsigned ResultReg = fastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
  402. ISDOpcode, Op0, Op0IsKill, CF);
  403. if (ResultReg) {
  404. // We successfully emitted code for the given LLVM Instruction.
  405. updateValueMap(I, ResultReg);
  406. return true;
  407. }
  408. }
  409. unsigned Op1 = getRegForValue(I->getOperand(1));
  410. if (!Op1) // Unhandled operand. Halt "fast" selection and bail.
  411. return false;
  412. bool Op1IsKill = hasTrivialKill(I->getOperand(1));
  413. // Now we have both operands in registers. Emit the instruction.
  414. unsigned ResultReg = fastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
  415. ISDOpcode, Op0, Op0IsKill, Op1, Op1IsKill);
  416. if (!ResultReg)
  417. // Target-specific code wasn't able to find a machine opcode for
  418. // the given ISD opcode and type. Halt "fast" selection and bail.
  419. return false;
  420. // We successfully emitted code for the given LLVM Instruction.
  421. updateValueMap(I, ResultReg);
  422. return true;
  423. }
  424. bool FastISel::selectGetElementPtr(const User *I) {
  425. unsigned N = getRegForValue(I->getOperand(0));
  426. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  427. return false;
  428. bool NIsKill = hasTrivialKill(I->getOperand(0));
  429. // Keep a running tab of the total offset to coalesce multiple N = N + Offset
  430. // into a single N = N + TotalOffset.
  431. uint64_t TotalOffs = 0;
  432. // FIXME: What's a good SWAG number for MaxOffs?
  433. uint64_t MaxOffs = 2048;
  434. Type *Ty = I->getOperand(0)->getType();
  435. MVT VT = TLI.getPointerTy();
  436. for (GetElementPtrInst::const_op_iterator OI = I->op_begin() + 1,
  437. E = I->op_end();
  438. OI != E; ++OI) {
  439. const Value *Idx = *OI;
  440. if (auto *StTy = dyn_cast<StructType>(Ty)) {
  441. uint64_t Field = cast<ConstantInt>(Idx)->getZExtValue();
  442. if (Field) {
  443. // N = N + Offset
  444. TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
  445. if (TotalOffs >= MaxOffs) {
  446. N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  447. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  448. return false;
  449. NIsKill = true;
  450. TotalOffs = 0;
  451. }
  452. }
  453. Ty = StTy->getElementType(Field);
  454. } else {
  455. Ty = cast<SequentialType>(Ty)->getElementType();
  456. // If this is a constant subscript, handle it quickly.
  457. if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
  458. if (CI->isZero())
  459. continue;
  460. // N = N + Offset
  461. uint64_t IdxN = CI->getValue().sextOrTrunc(64).getSExtValue();
  462. TotalOffs += DL.getTypeAllocSize(Ty) * IdxN;
  463. if (TotalOffs >= MaxOffs) {
  464. N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  465. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  466. return false;
  467. NIsKill = true;
  468. TotalOffs = 0;
  469. }
  470. continue;
  471. }
  472. if (TotalOffs) {
  473. N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  474. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  475. return false;
  476. NIsKill = true;
  477. TotalOffs = 0;
  478. }
  479. // N = N + Idx * ElementSize;
  480. uint64_t ElementSize = DL.getTypeAllocSize(Ty);
  481. std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
  482. unsigned IdxN = Pair.first;
  483. bool IdxNIsKill = Pair.second;
  484. if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
  485. return false;
  486. if (ElementSize != 1) {
  487. IdxN = fastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
  488. if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
  489. return false;
  490. IdxNIsKill = true;
  491. }
  492. N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
  493. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  494. return false;
  495. }
  496. }
  497. if (TotalOffs) {
  498. N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  499. if (!N) // Unhandled operand. Halt "fast" selection and bail.
  500. return false;
  501. }
  502. // We successfully emitted code for the given LLVM Instruction.
  503. updateValueMap(I, N);
  504. return true;
  505. }
  506. bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
  507. const CallInst *CI, unsigned StartIdx) {
  508. for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) {
  509. Value *Val = CI->getArgOperand(i);
  510. // Check for constants and encode them with a StackMaps::ConstantOp prefix.
  511. if (const auto *C = dyn_cast<ConstantInt>(Val)) {
  512. Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
  513. Ops.push_back(MachineOperand::CreateImm(C->getSExtValue()));
  514. } else if (isa<ConstantPointerNull>(Val)) {
  515. Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
  516. Ops.push_back(MachineOperand::CreateImm(0));
  517. } else if (auto *AI = dyn_cast<AllocaInst>(Val)) {
  518. // Values coming from a stack location also require a sepcial encoding,
  519. // but that is added later on by the target specific frame index
  520. // elimination implementation.
  521. auto SI = FuncInfo.StaticAllocaMap.find(AI);
  522. if (SI != FuncInfo.StaticAllocaMap.end())
  523. Ops.push_back(MachineOperand::CreateFI(SI->second));
  524. else
  525. return false;
  526. } else {
  527. unsigned Reg = getRegForValue(Val);
  528. if (!Reg)
  529. return false;
  530. Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
  531. }
  532. }
  533. return true;
  534. }
  535. bool FastISel::selectStackmap(const CallInst *I) {
  536. // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
  537. // [live variables...])
  538. assert(I->getCalledFunction()->getReturnType()->isVoidTy() &&
  539. "Stackmap cannot return a value.");
  540. // The stackmap intrinsic only records the live variables (the arguments
  541. // passed to it) and emits NOPS (if requested). Unlike the patchpoint
  542. // intrinsic, this won't be lowered to a function call. This means we don't
  543. // have to worry about calling conventions and target-specific lowering code.
  544. // Instead we perform the call lowering right here.
  545. //
  546. // CALLSEQ_START(0)
  547. // STACKMAP(id, nbytes, ...)
  548. // CALLSEQ_END(0, 0)
  549. //
  550. SmallVector<MachineOperand, 32> Ops;
  551. // Add the <id> and <numBytes> constants.
  552. assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
  553. "Expected a constant integer.");
  554. const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
  555. Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
  556. assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
  557. "Expected a constant integer.");
  558. const auto *NumBytes =
  559. cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
  560. Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
  561. // Push live variables for the stack map (skipping the first two arguments
  562. // <id> and <numBytes>).
  563. if (!addStackMapLiveVars(Ops, I, 2))
  564. return false;
  565. // We are not adding any register mask info here, because the stackmap doesn't
  566. // clobber anything.
  567. // Add scratch registers as implicit def and early clobber.
  568. CallingConv::ID CC = I->getCallingConv();
  569. const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
  570. for (unsigned i = 0; ScratchRegs[i]; ++i)
  571. Ops.push_back(MachineOperand::CreateReg(
  572. ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
  573. /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
  574. // Issue CALLSEQ_START
  575. unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
  576. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
  577. .addImm(0);
  578. // Issue STACKMAP.
  579. MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  580. TII.get(TargetOpcode::STACKMAP));
  581. for (auto const &MO : Ops)
  582. MIB.addOperand(MO);
  583. // Issue CALLSEQ_END
  584. unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
  585. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
  586. .addImm(0)
  587. .addImm(0);
  588. // Inform the Frame Information that we have a stackmap in this function.
  589. FuncInfo.MF->getFrameInfo()->setHasStackMap();
  590. return true;
  591. }
  592. /// \brief Lower an argument list according to the target calling convention.
  593. ///
  594. /// This is a helper for lowering intrinsics that follow a target calling
  595. /// convention or require stack pointer adjustment. Only a subset of the
  596. /// intrinsic's operands need to participate in the calling convention.
  597. bool FastISel::lowerCallOperands(const CallInst *CI, unsigned ArgIdx,
  598. unsigned NumArgs, const Value *Callee,
  599. bool ForceRetVoidTy, CallLoweringInfo &CLI) {
  600. ArgListTy Args;
  601. Args.reserve(NumArgs);
  602. // Populate the argument list.
  603. // Attributes for args start at offset 1, after the return attribute.
  604. ImmutableCallSite CS(CI);
  605. for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs, AttrI = ArgIdx + 1;
  606. ArgI != ArgE; ++ArgI) {
  607. Value *V = CI->getOperand(ArgI);
  608. assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
  609. ArgListEntry Entry;
  610. Entry.Val = V;
  611. Entry.Ty = V->getType();
  612. Entry.setAttributes(&CS, AttrI);
  613. Args.push_back(Entry);
  614. }
  615. Type *RetTy = ForceRetVoidTy ? Type::getVoidTy(CI->getType()->getContext())
  616. : CI->getType();
  617. CLI.setCallee(CI->getCallingConv(), RetTy, Callee, std::move(Args), NumArgs);
  618. return lowerCallTo(CLI);
  619. }
  620. bool FastISel::selectPatchpoint(const CallInst *I) {
  621. // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>,
  622. // i32 <numBytes>,
  623. // i8* <target>,
  624. // i32 <numArgs>,
  625. // [Args...],
  626. // [live variables...])
  627. CallingConv::ID CC = I->getCallingConv();
  628. bool IsAnyRegCC = CC == CallingConv::AnyReg;
  629. bool HasDef = !I->getType()->isVoidTy();
  630. Value *Callee = I->getOperand(PatchPointOpers::TargetPos);
  631. // Get the real number of arguments participating in the call <numArgs>
  632. assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)) &&
  633. "Expected a constant integer.");
  634. const auto *NumArgsVal =
  635. cast<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos));
  636. unsigned NumArgs = NumArgsVal->getZExtValue();
  637. // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
  638. // This includes all meta-operands up to but not including CC.
  639. unsigned NumMetaOpers = PatchPointOpers::CCPos;
  640. assert(I->getNumArgOperands() >= NumMetaOpers + NumArgs &&
  641. "Not enough arguments provided to the patchpoint intrinsic");
  642. // For AnyRegCC the arguments are lowered later on manually.
  643. unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
  644. CallLoweringInfo CLI;
  645. CLI.setIsPatchPoint();
  646. if (!lowerCallOperands(I, NumMetaOpers, NumCallArgs, Callee, IsAnyRegCC, CLI))
  647. return false;
  648. assert(CLI.Call && "No call instruction specified.");
  649. SmallVector<MachineOperand, 32> Ops;
  650. // Add an explicit result reg if we use the anyreg calling convention.
  651. if (IsAnyRegCC && HasDef) {
  652. assert(CLI.NumResultRegs == 0 && "Unexpected result register.");
  653. CLI.ResultReg = createResultReg(TLI.getRegClassFor(MVT::i64));
  654. CLI.NumResultRegs = 1;
  655. Ops.push_back(MachineOperand::CreateReg(CLI.ResultReg, /*IsDef=*/true));
  656. }
  657. // Add the <id> and <numBytes> constants.
  658. assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
  659. "Expected a constant integer.");
  660. const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
  661. Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
  662. assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
  663. "Expected a constant integer.");
  664. const auto *NumBytes =
  665. cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
  666. Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
  667. // Assume that the callee is a constant address or null pointer.
  668. // FIXME: handle function symbols in the future.
  669. uint64_t CalleeAddr;
  670. if (const auto *C = dyn_cast<IntToPtrInst>(Callee))
  671. CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
  672. else if (const auto *C = dyn_cast<ConstantExpr>(Callee)) {
  673. if (C->getOpcode() == Instruction::IntToPtr)
  674. CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
  675. else
  676. llvm_unreachable("Unsupported ConstantExpr.");
  677. } else if (isa<ConstantPointerNull>(Callee))
  678. CalleeAddr = 0;
  679. else
  680. llvm_unreachable("Unsupported callee address.");
  681. Ops.push_back(MachineOperand::CreateImm(CalleeAddr));
  682. // Adjust <numArgs> to account for any arguments that have been passed on
  683. // the stack instead.
  684. unsigned NumCallRegArgs = IsAnyRegCC ? NumArgs : CLI.OutRegs.size();
  685. Ops.push_back(MachineOperand::CreateImm(NumCallRegArgs));
  686. // Add the calling convention
  687. Ops.push_back(MachineOperand::CreateImm((unsigned)CC));
  688. // Add the arguments we omitted previously. The register allocator should
  689. // place these in any free register.
  690. if (IsAnyRegCC) {
  691. for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) {
  692. unsigned Reg = getRegForValue(I->getArgOperand(i));
  693. if (!Reg)
  694. return false;
  695. Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
  696. }
  697. }
  698. // Push the arguments from the call instruction.
  699. for (auto Reg : CLI.OutRegs)
  700. Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
  701. // Push live variables for the stack map.
  702. if (!addStackMapLiveVars(Ops, I, NumMetaOpers + NumArgs))
  703. return false;
  704. // Push the register mask info.
  705. Ops.push_back(MachineOperand::CreateRegMask(
  706. TRI.getCallPreservedMask(*FuncInfo.MF, CC)));
  707. // Add scratch registers as implicit def and early clobber.
  708. const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
  709. for (unsigned i = 0; ScratchRegs[i]; ++i)
  710. Ops.push_back(MachineOperand::CreateReg(
  711. ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
  712. /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
  713. // Add implicit defs (return values).
  714. for (auto Reg : CLI.InRegs)
  715. Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/true,
  716. /*IsImpl=*/true));
  717. // Insert the patchpoint instruction before the call generated by the target.
  718. MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, CLI.Call, DbgLoc,
  719. TII.get(TargetOpcode::PATCHPOINT));
  720. for (auto &MO : Ops)
  721. MIB.addOperand(MO);
  722. MIB->setPhysRegsDeadExcept(CLI.InRegs, TRI);
  723. // Delete the original call instruction.
  724. CLI.Call->eraseFromParent();
  725. // Inform the Frame Information that we have a patchpoint in this function.
  726. FuncInfo.MF->getFrameInfo()->setHasPatchPoint();
  727. if (CLI.NumResultRegs)
  728. updateValueMap(I, CLI.ResultReg, CLI.NumResultRegs);
  729. return true;
  730. }
  731. /// Returns an AttributeSet representing the attributes applied to the return
  732. /// value of the given call.
  733. static AttributeSet getReturnAttrs(FastISel::CallLoweringInfo &CLI) {
  734. SmallVector<Attribute::AttrKind, 2> Attrs;
  735. if (CLI.RetSExt)
  736. Attrs.push_back(Attribute::SExt);
  737. if (CLI.RetZExt)
  738. Attrs.push_back(Attribute::ZExt);
  739. if (CLI.IsInReg)
  740. Attrs.push_back(Attribute::InReg);
  741. return AttributeSet::get(CLI.RetTy->getContext(), AttributeSet::ReturnIndex,
  742. Attrs);
  743. }
  744. bool FastISel::lowerCallTo(const CallInst *CI, const char *SymName,
  745. unsigned NumArgs) {
  746. ImmutableCallSite CS(CI);
  747. PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
  748. FunctionType *FTy = cast<FunctionType>(PT->getElementType());
  749. Type *RetTy = FTy->getReturnType();
  750. ArgListTy Args;
  751. Args.reserve(NumArgs);
  752. // Populate the argument list.
  753. // Attributes for args start at offset 1, after the return attribute.
  754. for (unsigned ArgI = 0; ArgI != NumArgs; ++ArgI) {
  755. Value *V = CI->getOperand(ArgI);
  756. assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
  757. ArgListEntry Entry;
  758. Entry.Val = V;
  759. Entry.Ty = V->getType();
  760. Entry.setAttributes(&CS, ArgI + 1);
  761. Args.push_back(Entry);
  762. }
  763. CallLoweringInfo CLI;
  764. CLI.setCallee(RetTy, FTy, SymName, std::move(Args), CS, NumArgs);
  765. return lowerCallTo(CLI);
  766. }
  767. bool FastISel::lowerCallTo(CallLoweringInfo &CLI) {
  768. // Handle the incoming return values from the call.
  769. CLI.clearIns();
  770. SmallVector<EVT, 4> RetTys;
  771. ComputeValueVTs(TLI, CLI.RetTy, RetTys);
  772. SmallVector<ISD::OutputArg, 4> Outs;
  773. GetReturnInfo(CLI.RetTy, getReturnAttrs(CLI), Outs, TLI);
  774. bool CanLowerReturn = TLI.CanLowerReturn(
  775. CLI.CallConv, *FuncInfo.MF, CLI.IsVarArg, Outs, CLI.RetTy->getContext());
  776. // FIXME: sret demotion isn't supported yet - bail out.
  777. if (!CanLowerReturn)
  778. return false;
  779. for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
  780. EVT VT = RetTys[I];
  781. MVT RegisterVT = TLI.getRegisterType(CLI.RetTy->getContext(), VT);
  782. unsigned NumRegs = TLI.getNumRegisters(CLI.RetTy->getContext(), VT);
  783. for (unsigned i = 0; i != NumRegs; ++i) {
  784. ISD::InputArg MyFlags;
  785. MyFlags.VT = RegisterVT;
  786. MyFlags.ArgVT = VT;
  787. MyFlags.Used = CLI.IsReturnValueUsed;
  788. if (CLI.RetSExt)
  789. MyFlags.Flags.setSExt();
  790. if (CLI.RetZExt)
  791. MyFlags.Flags.setZExt();
  792. if (CLI.IsInReg)
  793. MyFlags.Flags.setInReg();
  794. CLI.Ins.push_back(MyFlags);
  795. }
  796. }
  797. // Handle all of the outgoing arguments.
  798. CLI.clearOuts();
  799. for (auto &Arg : CLI.getArgs()) {
  800. Type *FinalType = Arg.Ty;
  801. if (Arg.IsByVal)
  802. FinalType = cast<PointerType>(Arg.Ty)->getElementType();
  803. bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
  804. FinalType, CLI.CallConv, CLI.IsVarArg);
  805. ISD::ArgFlagsTy Flags;
  806. if (Arg.IsZExt)
  807. Flags.setZExt();
  808. if (Arg.IsSExt)
  809. Flags.setSExt();
  810. if (Arg.IsInReg)
  811. Flags.setInReg();
  812. if (Arg.IsSRet)
  813. Flags.setSRet();
  814. if (Arg.IsByVal)
  815. Flags.setByVal();
  816. if (Arg.IsInAlloca) {
  817. Flags.setInAlloca();
  818. // Set the byval flag for CCAssignFn callbacks that don't know about
  819. // inalloca. This way we can know how many bytes we should've allocated
  820. // and how many bytes a callee cleanup function will pop. If we port
  821. // inalloca to more targets, we'll have to add custom inalloca handling in
  822. // the various CC lowering callbacks.
  823. Flags.setByVal();
  824. }
  825. if (Arg.IsByVal || Arg.IsInAlloca) {
  826. PointerType *Ty = cast<PointerType>(Arg.Ty);
  827. Type *ElementTy = Ty->getElementType();
  828. unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
  829. // For ByVal, alignment should come from FE. BE will guess if this info is
  830. // not there, but there are cases it cannot get right.
  831. unsigned FrameAlign = Arg.Alignment;
  832. if (!FrameAlign)
  833. FrameAlign = TLI.getByValTypeAlignment(ElementTy);
  834. Flags.setByValSize(FrameSize);
  835. Flags.setByValAlign(FrameAlign);
  836. }
  837. if (Arg.IsNest)
  838. Flags.setNest();
  839. if (NeedsRegBlock)
  840. Flags.setInConsecutiveRegs();
  841. unsigned OriginalAlignment = DL.getABITypeAlignment(Arg.Ty);
  842. Flags.setOrigAlign(OriginalAlignment);
  843. CLI.OutVals.push_back(Arg.Val);
  844. CLI.OutFlags.push_back(Flags);
  845. }
  846. if (!fastLowerCall(CLI))
  847. return false;
  848. // Set all unused physreg defs as dead.
  849. assert(CLI.Call && "No call instruction specified.");
  850. CLI.Call->setPhysRegsDeadExcept(CLI.InRegs, TRI);
  851. if (CLI.NumResultRegs && CLI.CS)
  852. updateValueMap(CLI.CS->getInstruction(), CLI.ResultReg, CLI.NumResultRegs);
  853. return true;
  854. }
  855. bool FastISel::lowerCall(const CallInst *CI) {
  856. ImmutableCallSite CS(CI);
  857. PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
  858. FunctionType *FuncTy = cast<FunctionType>(PT->getElementType());
  859. Type *RetTy = FuncTy->getReturnType();
  860. ArgListTy Args;
  861. ArgListEntry Entry;
  862. Args.reserve(CS.arg_size());
  863. for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
  864. i != e; ++i) {
  865. Value *V = *i;
  866. // Skip empty types
  867. if (V->getType()->isEmptyTy())
  868. continue;
  869. Entry.Val = V;
  870. Entry.Ty = V->getType();
  871. // Skip the first return-type Attribute to get to params.
  872. Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
  873. Args.push_back(Entry);
  874. }
  875. // Check if target-independent constraints permit a tail call here.
  876. // Target-dependent constraints are checked within fastLowerCall.
  877. bool IsTailCall = CI->isTailCall();
  878. if (IsTailCall && !isInTailCallPosition(CS, TM))
  879. IsTailCall = false;
  880. CallLoweringInfo CLI;
  881. CLI.setCallee(RetTy, FuncTy, CI->getCalledValue(), std::move(Args), CS)
  882. .setTailCall(IsTailCall);
  883. return lowerCallTo(CLI);
  884. }
  885. bool FastISel::selectCall(const User *I) {
  886. const CallInst *Call = cast<CallInst>(I);
  887. // Handle simple inline asms.
  888. if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
  889. // If the inline asm has side effects, then make sure that no local value
  890. // lives across by flushing the local value map.
  891. if (IA->hasSideEffects())
  892. flushLocalValueMap();
  893. // Don't attempt to handle constraints.
  894. if (!IA->getConstraintString().empty())
  895. return false;
  896. unsigned ExtraInfo = 0;
  897. if (IA->hasSideEffects())
  898. ExtraInfo |= InlineAsm::Extra_HasSideEffects;
  899. if (IA->isAlignStack())
  900. ExtraInfo |= InlineAsm::Extra_IsAlignStack;
  901. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  902. TII.get(TargetOpcode::INLINEASM))
  903. .addExternalSymbol(IA->getAsmString().c_str())
  904. .addImm(ExtraInfo);
  905. return true;
  906. }
  907. MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
  908. ComputeUsesVAFloatArgument(*Call, &MMI);
  909. // Handle intrinsic function calls.
  910. if (const auto *II = dyn_cast<IntrinsicInst>(Call))
  911. return selectIntrinsicCall(II);
  912. // Usually, it does not make sense to initialize a value,
  913. // make an unrelated function call and use the value, because
  914. // it tends to be spilled on the stack. So, we move the pointer
  915. // to the last local value to the beginning of the block, so that
  916. // all the values which have already been materialized,
  917. // appear after the call. It also makes sense to skip intrinsics
  918. // since they tend to be inlined.
  919. flushLocalValueMap();
  920. return lowerCall(Call);
  921. }
  922. bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
  923. switch (II->getIntrinsicID()) {
  924. default:
  925. break;
  926. // At -O0 we don't care about the lifetime intrinsics.
  927. case Intrinsic::lifetime_start:
  928. case Intrinsic::lifetime_end:
  929. // The donothing intrinsic does, well, nothing.
  930. case Intrinsic::donothing:
  931. return true;
  932. case Intrinsic::eh_actions: {
  933. unsigned ResultReg = getRegForValue(UndefValue::get(II->getType()));
  934. if (!ResultReg)
  935. return false;
  936. updateValueMap(II, ResultReg);
  937. return true;
  938. }
  939. case Intrinsic::dbg_declare: {
  940. const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
  941. DIVariable DIVar(DI->getVariable());
  942. assert((!DIVar || DIVar.isVariable()) &&
  943. "Variable in DbgDeclareInst should be either null or a DIVariable.");
  944. if (!DIVar || !FuncInfo.MF->getMMI().hasDebugInfo()) {
  945. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  946. return true;
  947. }
  948. const Value *Address = DI->getAddress();
  949. if (!Address || isa<UndefValue>(Address)) {
  950. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  951. return true;
  952. }
  953. unsigned Offset = 0;
  954. Optional<MachineOperand> Op;
  955. if (const auto *Arg = dyn_cast<Argument>(Address))
  956. // Some arguments' frame index is recorded during argument lowering.
  957. Offset = FuncInfo.getArgumentFrameIndex(Arg);
  958. if (Offset)
  959. Op = MachineOperand::CreateFI(Offset);
  960. if (!Op)
  961. if (unsigned Reg = lookUpRegForValue(Address))
  962. Op = MachineOperand::CreateReg(Reg, false);
  963. // If we have a VLA that has a "use" in a metadata node that's then used
  964. // here but it has no other uses, then we have a problem. E.g.,
  965. //
  966. // int foo (const int *x) {
  967. // char a[*x];
  968. // return 0;
  969. // }
  970. //
  971. // If we assign 'a' a vreg and fast isel later on has to use the selection
  972. // DAG isel, it will want to copy the value to the vreg. However, there are
  973. // no uses, which goes counter to what selection DAG isel expects.
  974. if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
  975. (!isa<AllocaInst>(Address) ||
  976. !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
  977. Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
  978. false);
  979. if (Op) {
  980. if (Op->isReg()) {
  981. Op->setIsDebug(true);
  982. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  983. TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
  984. DI->getVariable(), DI->getExpression());
  985. } else
  986. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  987. TII.get(TargetOpcode::DBG_VALUE))
  988. .addOperand(*Op)
  989. .addImm(0)
  990. .addMetadata(DI->getVariable())
  991. .addMetadata(DI->getExpression());
  992. } else {
  993. // We can't yet handle anything else here because it would require
  994. // generating code, thus altering codegen because of debug info.
  995. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  996. }
  997. return true;
  998. }
  999. case Intrinsic::dbg_value: {
  1000. // This form of DBG_VALUE is target-independent.
  1001. const DbgValueInst *DI = cast<DbgValueInst>(II);
  1002. const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
  1003. const Value *V = DI->getValue();
  1004. if (!V) {
  1005. // Currently the optimizer can produce this; insert an undef to
  1006. // help debugging. Probably the optimizer should not do this.
  1007. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1008. .addReg(0U)
  1009. .addImm(DI->getOffset())
  1010. .addMetadata(DI->getVariable())
  1011. .addMetadata(DI->getExpression());
  1012. } else if (const auto *CI = dyn_cast<ConstantInt>(V)) {
  1013. if (CI->getBitWidth() > 64)
  1014. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1015. .addCImm(CI)
  1016. .addImm(DI->getOffset())
  1017. .addMetadata(DI->getVariable())
  1018. .addMetadata(DI->getExpression());
  1019. else
  1020. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1021. .addImm(CI->getZExtValue())
  1022. .addImm(DI->getOffset())
  1023. .addMetadata(DI->getVariable())
  1024. .addMetadata(DI->getExpression());
  1025. } else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
  1026. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1027. .addFPImm(CF)
  1028. .addImm(DI->getOffset())
  1029. .addMetadata(DI->getVariable())
  1030. .addMetadata(DI->getExpression());
  1031. } else if (unsigned Reg = lookUpRegForValue(V)) {
  1032. // FIXME: This does not handle register-indirect values at offset 0.
  1033. bool IsIndirect = DI->getOffset() != 0;
  1034. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, Reg,
  1035. DI->getOffset(), DI->getVariable(), DI->getExpression());
  1036. } else {
  1037. // We can't yet handle anything else here because it would require
  1038. // generating code, thus altering codegen because of debug info.
  1039. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  1040. }
  1041. return true;
  1042. }
  1043. case Intrinsic::objectsize: {
  1044. ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
  1045. unsigned long long Res = CI->isZero() ? -1ULL : 0;
  1046. Constant *ResCI = ConstantInt::get(II->getType(), Res);
  1047. unsigned ResultReg = getRegForValue(ResCI);
  1048. if (!ResultReg)
  1049. return false;
  1050. updateValueMap(II, ResultReg);
  1051. return true;
  1052. }
  1053. case Intrinsic::expect: {
  1054. unsigned ResultReg = getRegForValue(II->getArgOperand(0));
  1055. if (!ResultReg)
  1056. return false;
  1057. updateValueMap(II, ResultReg);
  1058. return true;
  1059. }
  1060. case Intrinsic::experimental_stackmap:
  1061. return selectStackmap(II);
  1062. case Intrinsic::experimental_patchpoint_void:
  1063. case Intrinsic::experimental_patchpoint_i64:
  1064. return selectPatchpoint(II);
  1065. }
  1066. return fastLowerIntrinsicCall(II);
  1067. }
  1068. bool FastISel::selectCast(const User *I, unsigned Opcode) {
  1069. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  1070. EVT DstVT = TLI.getValueType(I->getType());
  1071. if (SrcVT == MVT::Other || !SrcVT.isSimple() || DstVT == MVT::Other ||
  1072. !DstVT.isSimple())
  1073. // Unhandled type. Halt "fast" selection and bail.
  1074. return false;
  1075. // Check if the destination type is legal.
  1076. if (!TLI.isTypeLegal(DstVT))
  1077. return false;
  1078. // Check if the source operand is legal.
  1079. if (!TLI.isTypeLegal(SrcVT))
  1080. return false;
  1081. unsigned InputReg = getRegForValue(I->getOperand(0));
  1082. if (!InputReg)
  1083. // Unhandled operand. Halt "fast" selection and bail.
  1084. return false;
  1085. bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
  1086. unsigned ResultReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
  1087. Opcode, InputReg, InputRegIsKill);
  1088. if (!ResultReg)
  1089. return false;
  1090. updateValueMap(I, ResultReg);
  1091. return true;
  1092. }
  1093. bool FastISel::selectBitCast(const User *I) {
  1094. // If the bitcast doesn't change the type, just use the operand value.
  1095. if (I->getType() == I->getOperand(0)->getType()) {
  1096. unsigned Reg = getRegForValue(I->getOperand(0));
  1097. if (!Reg)
  1098. return false;
  1099. updateValueMap(I, Reg);
  1100. return true;
  1101. }
  1102. // Bitcasts of other values become reg-reg copies or BITCAST operators.
  1103. EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
  1104. EVT DstEVT = TLI.getValueType(I->getType());
  1105. if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
  1106. !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
  1107. // Unhandled type. Halt "fast" selection and bail.
  1108. return false;
  1109. MVT SrcVT = SrcEVT.getSimpleVT();
  1110. MVT DstVT = DstEVT.getSimpleVT();
  1111. unsigned Op0 = getRegForValue(I->getOperand(0));
  1112. if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
  1113. return false;
  1114. bool Op0IsKill = hasTrivialKill(I->getOperand(0));
  1115. // First, try to perform the bitcast by inserting a reg-reg copy.
  1116. unsigned ResultReg = 0;
  1117. if (SrcVT == DstVT) {
  1118. const TargetRegisterClass *SrcClass = TLI.getRegClassFor(SrcVT);
  1119. const TargetRegisterClass *DstClass = TLI.getRegClassFor(DstVT);
  1120. // Don't attempt a cross-class copy. It will likely fail.
  1121. if (SrcClass == DstClass) {
  1122. ResultReg = createResultReg(DstClass);
  1123. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1124. TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
  1125. }
  1126. }
  1127. // If the reg-reg copy failed, select a BITCAST opcode.
  1128. if (!ResultReg)
  1129. ResultReg = fastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
  1130. if (!ResultReg)
  1131. return false;
  1132. updateValueMap(I, ResultReg);
  1133. return true;
  1134. }
  1135. bool FastISel::selectInstruction(const Instruction *I) {
  1136. // Just before the terminator instruction, insert instructions to
  1137. // feed PHI nodes in successor blocks.
  1138. if (isa<TerminatorInst>(I))
  1139. if (!handlePHINodesInSuccessorBlocks(I->getParent()))
  1140. return false;
  1141. DbgLoc = I->getDebugLoc();
  1142. SavedInsertPt = FuncInfo.InsertPt;
  1143. if (const auto *Call = dyn_cast<CallInst>(I)) {
  1144. const Function *F = Call->getCalledFunction();
  1145. LibFunc::Func Func;
  1146. // As a special case, don't handle calls to builtin library functions that
  1147. // may be translated directly to target instructions.
  1148. if (F && !F->hasLocalLinkage() && F->hasName() &&
  1149. LibInfo->getLibFunc(F->getName(), Func) &&
  1150. LibInfo->hasOptimizedCodeGen(Func))
  1151. return false;
  1152. // Don't handle Intrinsic::trap if a trap funciton is specified.
  1153. if (F && F->getIntrinsicID() == Intrinsic::trap &&
  1154. !TM.Options.getTrapFunctionName().empty())
  1155. return false;
  1156. }
  1157. // First, try doing target-independent selection.
  1158. if (!SkipTargetIndependentISel) {
  1159. if (selectOperator(I, I->getOpcode())) {
  1160. ++NumFastIselSuccessIndependent;
  1161. DbgLoc = DebugLoc();
  1162. return true;
  1163. }
  1164. // Remove dead code.
  1165. recomputeInsertPt();
  1166. if (SavedInsertPt != FuncInfo.InsertPt)
  1167. removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
  1168. SavedInsertPt = FuncInfo.InsertPt;
  1169. }
  1170. // Next, try calling the target to attempt to handle the instruction.
  1171. if (fastSelectInstruction(I)) {
  1172. ++NumFastIselSuccessTarget;
  1173. DbgLoc = DebugLoc();
  1174. return true;
  1175. }
  1176. // Remove dead code.
  1177. recomputeInsertPt();
  1178. if (SavedInsertPt != FuncInfo.InsertPt)
  1179. removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
  1180. DbgLoc = DebugLoc();
  1181. // Undo phi node updates, because they will be added again by SelectionDAG.
  1182. if (isa<TerminatorInst>(I))
  1183. FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
  1184. return false;
  1185. }
  1186. /// Emit an unconditional branch to the given block, unless it is the immediate
  1187. /// (fall-through) successor, and update the CFG.
  1188. void FastISel::fastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
  1189. if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
  1190. FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
  1191. // For more accurate line information if this is the only instruction
  1192. // in the block then emit it, otherwise we have the unconditional
  1193. // fall-through case, which needs no instructions.
  1194. } else {
  1195. // The unconditional branch case.
  1196. TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
  1197. SmallVector<MachineOperand, 0>(), DbgLoc);
  1198. }
  1199. uint32_t BranchWeight = 0;
  1200. if (FuncInfo.BPI)
  1201. BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
  1202. MSucc->getBasicBlock());
  1203. FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
  1204. }
  1205. /// Emit an FNeg operation.
  1206. bool FastISel::selectFNeg(const User *I) {
  1207. unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
  1208. if (!OpReg)
  1209. return false;
  1210. bool OpRegIsKill = hasTrivialKill(I);
  1211. // If the target has ISD::FNEG, use it.
  1212. EVT VT = TLI.getValueType(I->getType());
  1213. unsigned ResultReg = fastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), ISD::FNEG,
  1214. OpReg, OpRegIsKill);
  1215. if (ResultReg) {
  1216. updateValueMap(I, ResultReg);
  1217. return true;
  1218. }
  1219. // Bitcast the value to integer, twiddle the sign bit with xor,
  1220. // and then bitcast it back to floating-point.
  1221. if (VT.getSizeInBits() > 64)
  1222. return false;
  1223. EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
  1224. if (!TLI.isTypeLegal(IntVT))
  1225. return false;
  1226. unsigned IntReg = fastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
  1227. ISD::BITCAST, OpReg, OpRegIsKill);
  1228. if (!IntReg)
  1229. return false;
  1230. unsigned IntResultReg = fastEmit_ri_(
  1231. IntVT.getSimpleVT(), ISD::XOR, IntReg, /*IsKill=*/true,
  1232. UINT64_C(1) << (VT.getSizeInBits() - 1), IntVT.getSimpleVT());
  1233. if (!IntResultReg)
  1234. return false;
  1235. ResultReg = fastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), ISD::BITCAST,
  1236. IntResultReg, /*IsKill=*/true);
  1237. if (!ResultReg)
  1238. return false;
  1239. updateValueMap(I, ResultReg);
  1240. return true;
  1241. }
  1242. bool FastISel::selectExtractValue(const User *U) {
  1243. const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
  1244. if (!EVI)
  1245. return false;
  1246. // Make sure we only try to handle extracts with a legal result. But also
  1247. // allow i1 because it's easy.
  1248. EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
  1249. if (!RealVT.isSimple())
  1250. return false;
  1251. MVT VT = RealVT.getSimpleVT();
  1252. if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
  1253. return false;
  1254. const Value *Op0 = EVI->getOperand(0);
  1255. Type *AggTy = Op0->getType();
  1256. // Get the base result register.
  1257. unsigned ResultReg;
  1258. DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
  1259. if (I != FuncInfo.ValueMap.end())
  1260. ResultReg = I->second;
  1261. else if (isa<Instruction>(Op0))
  1262. ResultReg = FuncInfo.InitializeRegForValue(Op0);
  1263. else
  1264. return false; // fast-isel can't handle aggregate constants at the moment
  1265. // Get the actual result register, which is an offset from the base register.
  1266. unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
  1267. SmallVector<EVT, 4> AggValueVTs;
  1268. ComputeValueVTs(TLI, AggTy, AggValueVTs);
  1269. for (unsigned i = 0; i < VTIndex; i++)
  1270. ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
  1271. updateValueMap(EVI, ResultReg);
  1272. return true;
  1273. }
  1274. bool FastISel::selectOperator(const User *I, unsigned Opcode) {
  1275. switch (Opcode) {
  1276. case Instruction::Add:
  1277. return selectBinaryOp(I, ISD::ADD);
  1278. case Instruction::FAdd:
  1279. return selectBinaryOp(I, ISD::FADD);
  1280. case Instruction::Sub:
  1281. return selectBinaryOp(I, ISD::SUB);
  1282. case Instruction::FSub:
  1283. // FNeg is currently represented in LLVM IR as a special case of FSub.
  1284. if (BinaryOperator::isFNeg(I))
  1285. return selectFNeg(I);
  1286. return selectBinaryOp(I, ISD::FSUB);
  1287. case Instruction::Mul:
  1288. return selectBinaryOp(I, ISD::MUL);
  1289. case Instruction::FMul:
  1290. return selectBinaryOp(I, ISD::FMUL);
  1291. case Instruction::SDiv:
  1292. return selectBinaryOp(I, ISD::SDIV);
  1293. case Instruction::UDiv:
  1294. return selectBinaryOp(I, ISD::UDIV);
  1295. case Instruction::FDiv:
  1296. return selectBinaryOp(I, ISD::FDIV);
  1297. case Instruction::SRem:
  1298. return selectBinaryOp(I, ISD::SREM);
  1299. case Instruction::URem:
  1300. return selectBinaryOp(I, ISD::UREM);
  1301. case Instruction::FRem:
  1302. return selectBinaryOp(I, ISD::FREM);
  1303. case Instruction::Shl:
  1304. return selectBinaryOp(I, ISD::SHL);
  1305. case Instruction::LShr:
  1306. return selectBinaryOp(I, ISD::SRL);
  1307. case Instruction::AShr:
  1308. return selectBinaryOp(I, ISD::SRA);
  1309. case Instruction::And:
  1310. return selectBinaryOp(I, ISD::AND);
  1311. case Instruction::Or:
  1312. return selectBinaryOp(I, ISD::OR);
  1313. case Instruction::Xor:
  1314. return selectBinaryOp(I, ISD::XOR);
  1315. case Instruction::GetElementPtr:
  1316. return selectGetElementPtr(I);
  1317. case Instruction::Br: {
  1318. const BranchInst *BI = cast<BranchInst>(I);
  1319. if (BI->isUnconditional()) {
  1320. const BasicBlock *LLVMSucc = BI->getSuccessor(0);
  1321. MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
  1322. fastEmitBranch(MSucc, BI->getDebugLoc());
  1323. return true;
  1324. }
  1325. // Conditional branches are not handed yet.
  1326. // Halt "fast" selection and bail.
  1327. return false;
  1328. }
  1329. case Instruction::Unreachable:
  1330. if (TM.Options.TrapUnreachable)
  1331. return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
  1332. else
  1333. return true;
  1334. case Instruction::Alloca:
  1335. // FunctionLowering has the static-sized case covered.
  1336. if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
  1337. return true;
  1338. // Dynamic-sized alloca is not handled yet.
  1339. return false;
  1340. case Instruction::Call:
  1341. return selectCall(I);
  1342. case Instruction::BitCast:
  1343. return selectBitCast(I);
  1344. case Instruction::FPToSI:
  1345. return selectCast(I, ISD::FP_TO_SINT);
  1346. case Instruction::ZExt:
  1347. return selectCast(I, ISD::ZERO_EXTEND);
  1348. case Instruction::SExt:
  1349. return selectCast(I, ISD::SIGN_EXTEND);
  1350. case Instruction::Trunc:
  1351. return selectCast(I, ISD::TRUNCATE);
  1352. case Instruction::SIToFP:
  1353. return selectCast(I, ISD::SINT_TO_FP);
  1354. case Instruction::IntToPtr: // Deliberate fall-through.
  1355. case Instruction::PtrToInt: {
  1356. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  1357. EVT DstVT = TLI.getValueType(I->getType());
  1358. if (DstVT.bitsGT(SrcVT))
  1359. return selectCast(I, ISD::ZERO_EXTEND);
  1360. if (DstVT.bitsLT(SrcVT))
  1361. return selectCast(I, ISD::TRUNCATE);
  1362. unsigned Reg = getRegForValue(I->getOperand(0));
  1363. if (!Reg)
  1364. return false;
  1365. updateValueMap(I, Reg);
  1366. return true;
  1367. }
  1368. case Instruction::ExtractValue:
  1369. return selectExtractValue(I);
  1370. case Instruction::PHI:
  1371. llvm_unreachable("FastISel shouldn't visit PHI nodes!");
  1372. default:
  1373. // Unhandled instruction. Halt "fast" selection and bail.
  1374. return false;
  1375. }
  1376. }
  1377. FastISel::FastISel(FunctionLoweringInfo &FuncInfo,
  1378. const TargetLibraryInfo *LibInfo,
  1379. bool SkipTargetIndependentISel)
  1380. : FuncInfo(FuncInfo), MF(FuncInfo.MF), MRI(FuncInfo.MF->getRegInfo()),
  1381. MFI(*FuncInfo.MF->getFrameInfo()), MCP(*FuncInfo.MF->getConstantPool()),
  1382. TM(FuncInfo.MF->getTarget()), DL(*TM.getDataLayout()),
  1383. TII(*MF->getSubtarget().getInstrInfo()),
  1384. TLI(*MF->getSubtarget().getTargetLowering()),
  1385. TRI(*MF->getSubtarget().getRegisterInfo()), LibInfo(LibInfo),
  1386. SkipTargetIndependentISel(SkipTargetIndependentISel) {}
  1387. FastISel::~FastISel() {}
  1388. bool FastISel::fastLowerArguments() { return false; }
  1389. bool FastISel::fastLowerCall(CallLoweringInfo & /*CLI*/) { return false; }
  1390. bool FastISel::fastLowerIntrinsicCall(const IntrinsicInst * /*II*/) {
  1391. return false;
  1392. }
  1393. unsigned FastISel::fastEmit_(MVT, MVT, unsigned) { return 0; }
  1394. unsigned FastISel::fastEmit_r(MVT, MVT, unsigned, unsigned /*Op0*/,
  1395. bool /*Op0IsKill*/) {
  1396. return 0;
  1397. }
  1398. unsigned FastISel::fastEmit_rr(MVT, MVT, unsigned, unsigned /*Op0*/,
  1399. bool /*Op0IsKill*/, unsigned /*Op1*/,
  1400. bool /*Op1IsKill*/) {
  1401. return 0;
  1402. }
  1403. unsigned FastISel::fastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
  1404. return 0;
  1405. }
  1406. unsigned FastISel::fastEmit_f(MVT, MVT, unsigned,
  1407. const ConstantFP * /*FPImm*/) {
  1408. return 0;
  1409. }
  1410. unsigned FastISel::fastEmit_ri(MVT, MVT, unsigned, unsigned /*Op0*/,
  1411. bool /*Op0IsKill*/, uint64_t /*Imm*/) {
  1412. return 0;
  1413. }
  1414. unsigned FastISel::fastEmit_rf(MVT, MVT, unsigned, unsigned /*Op0*/,
  1415. bool /*Op0IsKill*/,
  1416. const ConstantFP * /*FPImm*/) {
  1417. return 0;
  1418. }
  1419. unsigned FastISel::fastEmit_rri(MVT, MVT, unsigned, unsigned /*Op0*/,
  1420. bool /*Op0IsKill*/, unsigned /*Op1*/,
  1421. bool /*Op1IsKill*/, uint64_t /*Imm*/) {
  1422. return 0;
  1423. }
  1424. /// This method is a wrapper of fastEmit_ri. It first tries to emit an
  1425. /// instruction with an immediate operand using fastEmit_ri.
  1426. /// If that fails, it materializes the immediate into a register and try
  1427. /// fastEmit_rr instead.
  1428. unsigned FastISel::fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0,
  1429. bool Op0IsKill, uint64_t Imm, MVT ImmType) {
  1430. // If this is a multiply by a power of two, emit this as a shift left.
  1431. if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
  1432. Opcode = ISD::SHL;
  1433. Imm = Log2_64(Imm);
  1434. } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
  1435. // div x, 8 -> srl x, 3
  1436. Opcode = ISD::SRL;
  1437. Imm = Log2_64(Imm);
  1438. }
  1439. // Horrible hack (to be removed), check to make sure shift amounts are
  1440. // in-range.
  1441. if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
  1442. Imm >= VT.getSizeInBits())
  1443. return 0;
  1444. // First check if immediate type is legal. If not, we can't use the ri form.
  1445. unsigned ResultReg = fastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
  1446. if (ResultReg)
  1447. return ResultReg;
  1448. unsigned MaterialReg = fastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
  1449. if (!MaterialReg) {
  1450. // This is a bit ugly/slow, but failing here means falling out of
  1451. // fast-isel, which would be very slow.
  1452. IntegerType *ITy =
  1453. IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits());
  1454. MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
  1455. if (!MaterialReg)
  1456. return 0;
  1457. }
  1458. return fastEmit_rr(VT, VT, Opcode, Op0, Op0IsKill, MaterialReg,
  1459. /*IsKill=*/true);
  1460. }
  1461. unsigned FastISel::createResultReg(const TargetRegisterClass *RC) {
  1462. return MRI.createVirtualRegister(RC);
  1463. }
  1464. unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II, unsigned Op,
  1465. unsigned OpNum) {
  1466. if (TargetRegisterInfo::isVirtualRegister(Op)) {
  1467. const TargetRegisterClass *RegClass =
  1468. TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
  1469. if (!MRI.constrainRegClass(Op, RegClass)) {
  1470. // If it's not legal to COPY between the register classes, something
  1471. // has gone very wrong before we got here.
  1472. unsigned NewOp = createResultReg(RegClass);
  1473. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1474. TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
  1475. return NewOp;
  1476. }
  1477. }
  1478. return Op;
  1479. }
  1480. unsigned FastISel::fastEmitInst_(unsigned MachineInstOpcode,
  1481. const TargetRegisterClass *RC) {
  1482. unsigned ResultReg = createResultReg(RC);
  1483. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1484. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
  1485. return ResultReg;
  1486. }
  1487. unsigned FastISel::fastEmitInst_r(unsigned MachineInstOpcode,
  1488. const TargetRegisterClass *RC, unsigned Op0,
  1489. bool Op0IsKill) {
  1490. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1491. unsigned ResultReg = createResultReg(RC);
  1492. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1493. if (II.getNumDefs() >= 1)
  1494. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1495. .addReg(Op0, getKillRegState(Op0IsKill));
  1496. else {
  1497. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1498. .addReg(Op0, getKillRegState(Op0IsKill));
  1499. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1500. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1501. }
  1502. return ResultReg;
  1503. }
  1504. unsigned FastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
  1505. const TargetRegisterClass *RC, unsigned Op0,
  1506. bool Op0IsKill, unsigned Op1,
  1507. bool Op1IsKill) {
  1508. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1509. unsigned ResultReg = createResultReg(RC);
  1510. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1511. Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
  1512. if (II.getNumDefs() >= 1)
  1513. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1514. .addReg(Op0, getKillRegState(Op0IsKill))
  1515. .addReg(Op1, getKillRegState(Op1IsKill));
  1516. else {
  1517. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1518. .addReg(Op0, getKillRegState(Op0IsKill))
  1519. .addReg(Op1, getKillRegState(Op1IsKill));
  1520. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1521. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1522. }
  1523. return ResultReg;
  1524. }
  1525. unsigned FastISel::fastEmitInst_rrr(unsigned MachineInstOpcode,
  1526. const TargetRegisterClass *RC, unsigned Op0,
  1527. bool Op0IsKill, unsigned Op1,
  1528. bool Op1IsKill, unsigned Op2,
  1529. bool Op2IsKill) {
  1530. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1531. unsigned ResultReg = createResultReg(RC);
  1532. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1533. Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
  1534. Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
  1535. if (II.getNumDefs() >= 1)
  1536. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1537. .addReg(Op0, getKillRegState(Op0IsKill))
  1538. .addReg(Op1, getKillRegState(Op1IsKill))
  1539. .addReg(Op2, getKillRegState(Op2IsKill));
  1540. else {
  1541. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1542. .addReg(Op0, getKillRegState(Op0IsKill))
  1543. .addReg(Op1, getKillRegState(Op1IsKill))
  1544. .addReg(Op2, getKillRegState(Op2IsKill));
  1545. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1546. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1547. }
  1548. return ResultReg;
  1549. }
  1550. unsigned FastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
  1551. const TargetRegisterClass *RC, unsigned Op0,
  1552. bool Op0IsKill, uint64_t Imm) {
  1553. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1554. unsigned ResultReg = createResultReg(RC);
  1555. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1556. if (II.getNumDefs() >= 1)
  1557. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1558. .addReg(Op0, getKillRegState(Op0IsKill))
  1559. .addImm(Imm);
  1560. else {
  1561. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1562. .addReg(Op0, getKillRegState(Op0IsKill))
  1563. .addImm(Imm);
  1564. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1565. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1566. }
  1567. return ResultReg;
  1568. }
  1569. unsigned FastISel::fastEmitInst_rii(unsigned MachineInstOpcode,
  1570. const TargetRegisterClass *RC, unsigned Op0,
  1571. bool Op0IsKill, uint64_t Imm1,
  1572. uint64_t Imm2) {
  1573. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1574. unsigned ResultReg = createResultReg(RC);
  1575. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1576. if (II.getNumDefs() >= 1)
  1577. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1578. .addReg(Op0, getKillRegState(Op0IsKill))
  1579. .addImm(Imm1)
  1580. .addImm(Imm2);
  1581. else {
  1582. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1583. .addReg(Op0, getKillRegState(Op0IsKill))
  1584. .addImm(Imm1)
  1585. .addImm(Imm2);
  1586. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1587. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1588. }
  1589. return ResultReg;
  1590. }
  1591. unsigned FastISel::fastEmitInst_rf(unsigned MachineInstOpcode,
  1592. const TargetRegisterClass *RC, unsigned Op0,
  1593. bool Op0IsKill, const ConstantFP *FPImm) {
  1594. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1595. unsigned ResultReg = createResultReg(RC);
  1596. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1597. if (II.getNumDefs() >= 1)
  1598. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1599. .addReg(Op0, getKillRegState(Op0IsKill))
  1600. .addFPImm(FPImm);
  1601. else {
  1602. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1603. .addReg(Op0, getKillRegState(Op0IsKill))
  1604. .addFPImm(FPImm);
  1605. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1606. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1607. }
  1608. return ResultReg;
  1609. }
  1610. unsigned FastISel::fastEmitInst_rri(unsigned MachineInstOpcode,
  1611. const TargetRegisterClass *RC, unsigned Op0,
  1612. bool Op0IsKill, unsigned Op1,
  1613. bool Op1IsKill, uint64_t Imm) {
  1614. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1615. unsigned ResultReg = createResultReg(RC);
  1616. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1617. Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
  1618. if (II.getNumDefs() >= 1)
  1619. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1620. .addReg(Op0, getKillRegState(Op0IsKill))
  1621. .addReg(Op1, getKillRegState(Op1IsKill))
  1622. .addImm(Imm);
  1623. else {
  1624. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1625. .addReg(Op0, getKillRegState(Op0IsKill))
  1626. .addReg(Op1, getKillRegState(Op1IsKill))
  1627. .addImm(Imm);
  1628. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1629. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1630. }
  1631. return ResultReg;
  1632. }
  1633. unsigned FastISel::fastEmitInst_rrii(unsigned MachineInstOpcode,
  1634. const TargetRegisterClass *RC,
  1635. unsigned Op0, bool Op0IsKill, unsigned Op1,
  1636. bool Op1IsKill, uint64_t Imm1,
  1637. uint64_t Imm2) {
  1638. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1639. unsigned ResultReg = createResultReg(RC);
  1640. Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
  1641. Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
  1642. if (II.getNumDefs() >= 1)
  1643. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1644. .addReg(Op0, getKillRegState(Op0IsKill))
  1645. .addReg(Op1, getKillRegState(Op1IsKill))
  1646. .addImm(Imm1)
  1647. .addImm(Imm2);
  1648. else {
  1649. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1650. .addReg(Op0, getKillRegState(Op0IsKill))
  1651. .addReg(Op1, getKillRegState(Op1IsKill))
  1652. .addImm(Imm1)
  1653. .addImm(Imm2);
  1654. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1655. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1656. }
  1657. return ResultReg;
  1658. }
  1659. unsigned FastISel::fastEmitInst_i(unsigned MachineInstOpcode,
  1660. const TargetRegisterClass *RC, uint64_t Imm) {
  1661. unsigned ResultReg = createResultReg(RC);
  1662. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1663. if (II.getNumDefs() >= 1)
  1664. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1665. .addImm(Imm);
  1666. else {
  1667. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
  1668. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1669. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1670. }
  1671. return ResultReg;
  1672. }
  1673. unsigned FastISel::fastEmitInst_ii(unsigned MachineInstOpcode,
  1674. const TargetRegisterClass *RC, uint64_t Imm1,
  1675. uint64_t Imm2) {
  1676. unsigned ResultReg = createResultReg(RC);
  1677. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1678. if (II.getNumDefs() >= 1)
  1679. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1680. .addImm(Imm1)
  1681. .addImm(Imm2);
  1682. else {
  1683. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1)
  1684. .addImm(Imm2);
  1685. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1686. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1687. }
  1688. return ResultReg;
  1689. }
  1690. unsigned FastISel::fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0,
  1691. bool Op0IsKill, uint32_t Idx) {
  1692. unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
  1693. assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
  1694. "Cannot yet extract from physregs");
  1695. const TargetRegisterClass *RC = MRI.getRegClass(Op0);
  1696. MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
  1697. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
  1698. ResultReg).addReg(Op0, getKillRegState(Op0IsKill), Idx);
  1699. return ResultReg;
  1700. }
  1701. /// Emit MachineInstrs to compute the value of Op with all but the least
  1702. /// significant bit set to zero.
  1703. unsigned FastISel::fastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
  1704. return fastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
  1705. }
  1706. /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
  1707. /// Emit code to ensure constants are copied into registers when needed.
  1708. /// Remember the virtual registers that need to be added to the Machine PHI
  1709. /// nodes as input. We cannot just directly add them, because expansion
  1710. /// might result in multiple MBB's for one BB. As such, the start of the
  1711. /// BB might correspond to a different MBB than the end.
  1712. bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
  1713. const TerminatorInst *TI = LLVMBB->getTerminator();
  1714. SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
  1715. FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
  1716. // Check successor nodes' PHI nodes that expect a constant to be available
  1717. // from this block.
  1718. for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
  1719. const BasicBlock *SuccBB = TI->getSuccessor(succ);
  1720. if (!isa<PHINode>(SuccBB->begin()))
  1721. continue;
  1722. MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
  1723. // If this terminator has multiple identical successors (common for
  1724. // switches), only handle each succ once.
  1725. if (!SuccsHandled.insert(SuccMBB).second)
  1726. continue;
  1727. MachineBasicBlock::iterator MBBI = SuccMBB->begin();
  1728. // At this point we know that there is a 1-1 correspondence between LLVM PHI
  1729. // nodes and Machine PHI nodes, but the incoming operands have not been
  1730. // emitted yet.
  1731. for (BasicBlock::const_iterator I = SuccBB->begin();
  1732. const auto *PN = dyn_cast<PHINode>(I); ++I) {
  1733. // Ignore dead phi's.
  1734. if (PN->use_empty())
  1735. continue;
  1736. // Only handle legal types. Two interesting things to note here. First,
  1737. // by bailing out early, we may leave behind some dead instructions,
  1738. // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
  1739. // own moves. Second, this check is necessary because FastISel doesn't
  1740. // use CreateRegs to create registers, so it always creates
  1741. // exactly one register for each non-void instruction.
  1742. EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
  1743. if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
  1744. // Handle integer promotions, though, because they're common and easy.
  1745. if (!(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)) {
  1746. FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
  1747. return false;
  1748. }
  1749. }
  1750. const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
  1751. // Set the DebugLoc for the copy. Prefer the location of the operand
  1752. // if there is one; use the location of the PHI otherwise.
  1753. DbgLoc = PN->getDebugLoc();
  1754. if (const auto *Inst = dyn_cast<Instruction>(PHIOp))
  1755. DbgLoc = Inst->getDebugLoc();
  1756. unsigned Reg = getRegForValue(PHIOp);
  1757. if (!Reg) {
  1758. FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
  1759. return false;
  1760. }
  1761. FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
  1762. DbgLoc = DebugLoc();
  1763. }
  1764. }
  1765. return true;
  1766. }
  1767. bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
  1768. assert(LI->hasOneUse() &&
  1769. "tryToFoldLoad expected a LoadInst with a single use");
  1770. // We know that the load has a single use, but don't know what it is. If it
  1771. // isn't one of the folded instructions, then we can't succeed here. Handle
  1772. // this by scanning the single-use users of the load until we get to FoldInst.
  1773. unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
  1774. const Instruction *TheUser = LI->user_back();
  1775. while (TheUser != FoldInst && // Scan up until we find FoldInst.
  1776. // Stay in the right block.
  1777. TheUser->getParent() == FoldInst->getParent() &&
  1778. --MaxUsers) { // Don't scan too far.
  1779. // If there are multiple or no uses of this instruction, then bail out.
  1780. if (!TheUser->hasOneUse())
  1781. return false;
  1782. TheUser = TheUser->user_back();
  1783. }
  1784. // If we didn't find the fold instruction, then we failed to collapse the
  1785. // sequence.
  1786. if (TheUser != FoldInst)
  1787. return false;
  1788. // Don't try to fold volatile loads. Target has to deal with alignment
  1789. // constraints.
  1790. if (LI->isVolatile())
  1791. return false;
  1792. // Figure out which vreg this is going into. If there is no assigned vreg yet
  1793. // then there actually was no reference to it. Perhaps the load is referenced
  1794. // by a dead instruction.
  1795. unsigned LoadReg = getRegForValue(LI);
  1796. if (!LoadReg)
  1797. return false;
  1798. // We can't fold if this vreg has no uses or more than one use. Multiple uses
  1799. // may mean that the instruction got lowered to multiple MIs, or the use of
  1800. // the loaded value ended up being multiple operands of the result.
  1801. if (!MRI.hasOneUse(LoadReg))
  1802. return false;
  1803. MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
  1804. MachineInstr *User = RI->getParent();
  1805. // Set the insertion point properly. Folding the load can cause generation of
  1806. // other random instructions (like sign extends) for addressing modes; make
  1807. // sure they get inserted in a logical place before the new instruction.
  1808. FuncInfo.InsertPt = User;
  1809. FuncInfo.MBB = User->getParent();
  1810. // Ask the target to try folding the load.
  1811. return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
  1812. }
  1813. bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
  1814. // Must be an add.
  1815. if (!isa<AddOperator>(Add))
  1816. return false;
  1817. // Type size needs to match.
  1818. if (DL.getTypeSizeInBits(GEP->getType()) !=
  1819. DL.getTypeSizeInBits(Add->getType()))
  1820. return false;
  1821. // Must be in the same basic block.
  1822. if (isa<Instruction>(Add) &&
  1823. FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
  1824. return false;
  1825. // Must have a constant operand.
  1826. return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
  1827. }
  1828. MachineMemOperand *
  1829. FastISel::createMachineMemOperandFor(const Instruction *I) const {
  1830. const Value *Ptr;
  1831. Type *ValTy;
  1832. unsigned Alignment;
  1833. unsigned Flags;
  1834. bool IsVolatile;
  1835. if (const auto *LI = dyn_cast<LoadInst>(I)) {
  1836. Alignment = LI->getAlignment();
  1837. IsVolatile = LI->isVolatile();
  1838. Flags = MachineMemOperand::MOLoad;
  1839. Ptr = LI->getPointerOperand();
  1840. ValTy = LI->getType();
  1841. } else if (const auto *SI = dyn_cast<StoreInst>(I)) {
  1842. Alignment = SI->getAlignment();
  1843. IsVolatile = SI->isVolatile();
  1844. Flags = MachineMemOperand::MOStore;
  1845. Ptr = SI->getPointerOperand();
  1846. ValTy = SI->getValueOperand()->getType();
  1847. } else
  1848. return nullptr;
  1849. bool IsNonTemporal = I->getMetadata(LLVMContext::MD_nontemporal) != nullptr;
  1850. bool IsInvariant = I->getMetadata(LLVMContext::MD_invariant_load) != nullptr;
  1851. const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range);
  1852. AAMDNodes AAInfo;
  1853. I->getAAMetadata(AAInfo);
  1854. if (Alignment == 0) // Ensure that codegen never sees alignment 0.
  1855. Alignment = DL.getABITypeAlignment(ValTy);
  1856. unsigned Size = DL.getTypeStoreSize(ValTy);
  1857. if (IsVolatile)
  1858. Flags |= MachineMemOperand::MOVolatile;
  1859. if (IsNonTemporal)
  1860. Flags |= MachineMemOperand::MONonTemporal;
  1861. if (IsInvariant)
  1862. Flags |= MachineMemOperand::MOInvariant;
  1863. return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size,
  1864. Alignment, AAInfo, Ranges);
  1865. }
  1866. CmpInst::Predicate FastISel::optimizeCmpPredicate(const CmpInst *CI) const {
  1867. // If both operands are the same, then try to optimize or fold the cmp.
  1868. CmpInst::Predicate Predicate = CI->getPredicate();
  1869. if (CI->getOperand(0) != CI->getOperand(1))
  1870. return Predicate;
  1871. switch (Predicate) {
  1872. default: llvm_unreachable("Invalid predicate!");
  1873. case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
  1874. case CmpInst::FCMP_OEQ: Predicate = CmpInst::FCMP_ORD; break;
  1875. case CmpInst::FCMP_OGT: Predicate = CmpInst::FCMP_FALSE; break;
  1876. case CmpInst::FCMP_OGE: Predicate = CmpInst::FCMP_ORD; break;
  1877. case CmpInst::FCMP_OLT: Predicate = CmpInst::FCMP_FALSE; break;
  1878. case CmpInst::FCMP_OLE: Predicate = CmpInst::FCMP_ORD; break;
  1879. case CmpInst::FCMP_ONE: Predicate = CmpInst::FCMP_FALSE; break;
  1880. case CmpInst::FCMP_ORD: Predicate = CmpInst::FCMP_ORD; break;
  1881. case CmpInst::FCMP_UNO: Predicate = CmpInst::FCMP_UNO; break;
  1882. case CmpInst::FCMP_UEQ: Predicate = CmpInst::FCMP_TRUE; break;
  1883. case CmpInst::FCMP_UGT: Predicate = CmpInst::FCMP_UNO; break;
  1884. case CmpInst::FCMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
  1885. case CmpInst::FCMP_ULT: Predicate = CmpInst::FCMP_UNO; break;
  1886. case CmpInst::FCMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
  1887. case CmpInst::FCMP_UNE: Predicate = CmpInst::FCMP_UNO; break;
  1888. case CmpInst::FCMP_TRUE: Predicate = CmpInst::FCMP_TRUE; break;
  1889. case CmpInst::ICMP_EQ: Predicate = CmpInst::FCMP_TRUE; break;
  1890. case CmpInst::ICMP_NE: Predicate = CmpInst::FCMP_FALSE; break;
  1891. case CmpInst::ICMP_UGT: Predicate = CmpInst::FCMP_FALSE; break;
  1892. case CmpInst::ICMP_UGE: Predicate = CmpInst::FCMP_TRUE; break;
  1893. case CmpInst::ICMP_ULT: Predicate = CmpInst::FCMP_FALSE; break;
  1894. case CmpInst::ICMP_ULE: Predicate = CmpInst::FCMP_TRUE; break;
  1895. case CmpInst::ICMP_SGT: Predicate = CmpInst::FCMP_FALSE; break;
  1896. case CmpInst::ICMP_SGE: Predicate = CmpInst::FCMP_TRUE; break;
  1897. case CmpInst::ICMP_SLT: Predicate = CmpInst::FCMP_FALSE; break;
  1898. case CmpInst::ICMP_SLE: Predicate = CmpInst::FCMP_TRUE; break;
  1899. }
  1900. return Predicate;
  1901. }