FastISel.cpp 81 KB

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