FastISel.cpp 79 KB

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