FastISel.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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. #define DEBUG_TYPE "isel"
  42. #include "llvm/CodeGen/FastISel.h"
  43. #include "llvm/ADT/Optional.h"
  44. #include "llvm/ADT/Statistic.h"
  45. #include "llvm/Analysis/Loads.h"
  46. #include "llvm/CodeGen/Analysis.h"
  47. #include "llvm/CodeGen/FunctionLoweringInfo.h"
  48. #include "llvm/CodeGen/MachineInstrBuilder.h"
  49. #include "llvm/CodeGen/MachineModuleInfo.h"
  50. #include "llvm/CodeGen/MachineRegisterInfo.h"
  51. #include "llvm/DebugInfo.h"
  52. #include "llvm/IR/DataLayout.h"
  53. #include "llvm/IR/Function.h"
  54. #include "llvm/IR/GlobalVariable.h"
  55. #include "llvm/IR/Instructions.h"
  56. #include "llvm/IR/IntrinsicInst.h"
  57. #include "llvm/IR/Operator.h"
  58. #include "llvm/Support/Debug.h"
  59. #include "llvm/Support/ErrorHandling.h"
  60. #include "llvm/Target/TargetInstrInfo.h"
  61. #include "llvm/Target/TargetLibraryInfo.h"
  62. #include "llvm/Target/TargetLowering.h"
  63. #include "llvm/Target/TargetMachine.h"
  64. using namespace llvm;
  65. STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
  66. "target-independent selector");
  67. STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
  68. "target-specific selector");
  69. STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
  70. /// startNewBlock - Set the current block to which generated machine
  71. /// instructions will be appended, and clear the local CSE map.
  72. ///
  73. void FastISel::startNewBlock() {
  74. LocalValueMap.clear();
  75. // Instructions are appended to FuncInfo.MBB. If the basic block already
  76. // contains labels or copies, use the last instruction as the last local
  77. // value.
  78. EmitStartPt = 0;
  79. if (!FuncInfo.MBB->empty())
  80. EmitStartPt = &FuncInfo.MBB->back();
  81. LastLocalValue = EmitStartPt;
  82. }
  83. bool FastISel::LowerArguments() {
  84. if (!FuncInfo.CanLowerReturn)
  85. // Fallback to SDISel argument lowering code to deal with sret pointer
  86. // parameter.
  87. return false;
  88. if (!FastLowerArguments())
  89. return false;
  90. // Enter arguments into ValueMap for uses in non-entry BBs.
  91. for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
  92. E = FuncInfo.Fn->arg_end(); I != E; ++I) {
  93. DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
  94. assert(VI != LocalValueMap.end() && "Missed an argument?");
  95. FuncInfo.ValueMap[I] = VI->second;
  96. }
  97. return true;
  98. }
  99. void FastISel::flushLocalValueMap() {
  100. LocalValueMap.clear();
  101. LastLocalValue = EmitStartPt;
  102. recomputeInsertPt();
  103. }
  104. bool FastISel::hasTrivialKill(const Value *V) const {
  105. // Don't consider constants or arguments to have trivial kills.
  106. const Instruction *I = dyn_cast<Instruction>(V);
  107. if (!I)
  108. return false;
  109. // No-op casts are trivially coalesced by fast-isel.
  110. if (const CastInst *Cast = dyn_cast<CastInst>(I))
  111. if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
  112. !hasTrivialKill(Cast->getOperand(0)))
  113. return false;
  114. // GEPs with all zero indices are trivially coalesced by fast-isel.
  115. if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
  116. if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
  117. return false;
  118. // Only instructions with a single use in the same basic block are considered
  119. // to have trivial kills.
  120. return I->hasOneUse() &&
  121. !(I->getOpcode() == Instruction::BitCast ||
  122. I->getOpcode() == Instruction::PtrToInt ||
  123. I->getOpcode() == Instruction::IntToPtr) &&
  124. cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
  125. }
  126. unsigned FastISel::getRegForValue(const Value *V) {
  127. EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
  128. // Don't handle non-simple values in FastISel.
  129. if (!RealVT.isSimple())
  130. return 0;
  131. // Ignore illegal types. We must do this before looking up the value
  132. // in ValueMap because Arguments are given virtual registers regardless
  133. // of whether FastISel can handle them.
  134. MVT VT = RealVT.getSimpleVT();
  135. if (!TLI.isTypeLegal(VT)) {
  136. // Handle integer promotions, though, because they're common and easy.
  137. if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
  138. VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
  139. else
  140. return 0;
  141. }
  142. // Look up the value to see if we already have a register for it.
  143. unsigned Reg = lookUpRegForValue(V);
  144. if (Reg != 0)
  145. return Reg;
  146. // In bottom-up mode, just create the virtual register which will be used
  147. // to hold the value. It will be materialized later.
  148. if (isa<Instruction>(V) &&
  149. (!isa<AllocaInst>(V) ||
  150. !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
  151. return FuncInfo.InitializeRegForValue(V);
  152. SavePoint SaveInsertPt = enterLocalValueArea();
  153. // Materialize the value in a register. Emit any instructions in the
  154. // local value area.
  155. Reg = materializeRegForValue(V, VT);
  156. leaveLocalValueArea(SaveInsertPt);
  157. return Reg;
  158. }
  159. /// materializeRegForValue - Helper for getRegForValue. This function is
  160. /// called when the value isn't already available in a register and must
  161. /// be materialized with new instructions.
  162. unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
  163. unsigned Reg = 0;
  164. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  165. if (CI->getValue().getActiveBits() <= 64)
  166. Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
  167. } else if (isa<AllocaInst>(V)) {
  168. Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
  169. } else if (isa<ConstantPointerNull>(V)) {
  170. // Translate this as an integer zero so that it can be
  171. // local-CSE'd with actual integer zeros.
  172. Reg =
  173. getRegForValue(Constant::getNullValue(DL.getIntPtrType(V->getContext())));
  174. } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
  175. if (CF->isNullValue()) {
  176. Reg = TargetMaterializeFloatZero(CF);
  177. } else {
  178. // Try to emit the constant directly.
  179. Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
  180. }
  181. if (!Reg) {
  182. // Try to emit the constant by using an integer constant with a cast.
  183. const APFloat &Flt = CF->getValueAPF();
  184. EVT IntVT = TLI.getPointerTy();
  185. uint64_t x[2];
  186. uint32_t IntBitWidth = IntVT.getSizeInBits();
  187. bool isExact;
  188. (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
  189. APFloat::rmTowardZero, &isExact);
  190. if (isExact) {
  191. APInt IntVal(IntBitWidth, x);
  192. unsigned IntegerReg =
  193. getRegForValue(ConstantInt::get(V->getContext(), IntVal));
  194. if (IntegerReg != 0)
  195. Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
  196. IntegerReg, /*Kill=*/false);
  197. }
  198. }
  199. } else if (const Operator *Op = dyn_cast<Operator>(V)) {
  200. if (!SelectOperator(Op, Op->getOpcode()))
  201. if (!isa<Instruction>(Op) ||
  202. !TargetSelectInstruction(cast<Instruction>(Op)))
  203. return 0;
  204. Reg = lookUpRegForValue(Op);
  205. } else if (isa<UndefValue>(V)) {
  206. Reg = createResultReg(TLI.getRegClassFor(VT));
  207. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  208. TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
  209. }
  210. // If target-independent code couldn't handle the value, give target-specific
  211. // code a try.
  212. if (!Reg && isa<Constant>(V))
  213. Reg = TargetMaterializeConstant(cast<Constant>(V));
  214. // Don't cache constant materializations in the general ValueMap.
  215. // To do so would require tracking what uses they dominate.
  216. if (Reg != 0) {
  217. LocalValueMap[V] = Reg;
  218. LastLocalValue = MRI.getVRegDef(Reg);
  219. }
  220. return Reg;
  221. }
  222. unsigned FastISel::lookUpRegForValue(const Value *V) {
  223. // Look up the value to see if we already have a register for it. We
  224. // cache values defined by Instructions across blocks, and other values
  225. // only locally. This is because Instructions already have the SSA
  226. // def-dominates-use requirement enforced.
  227. DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
  228. if (I != FuncInfo.ValueMap.end())
  229. return I->second;
  230. return LocalValueMap[V];
  231. }
  232. /// UpdateValueMap - Update the value map to include the new mapping for this
  233. /// instruction, or insert an extra copy to get the result in a previous
  234. /// determined register.
  235. /// NOTE: This is only necessary because we might select a block that uses
  236. /// a value before we select the block that defines the value. It might be
  237. /// possible to fix this by selecting blocks in reverse postorder.
  238. void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
  239. if (!isa<Instruction>(I)) {
  240. LocalValueMap[I] = Reg;
  241. return;
  242. }
  243. unsigned &AssignedReg = FuncInfo.ValueMap[I];
  244. if (AssignedReg == 0)
  245. // Use the new register.
  246. AssignedReg = Reg;
  247. else if (Reg != AssignedReg) {
  248. // Arrange for uses of AssignedReg to be replaced by uses of Reg.
  249. for (unsigned i = 0; i < NumRegs; i++)
  250. FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
  251. AssignedReg = Reg;
  252. }
  253. }
  254. std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
  255. unsigned IdxN = getRegForValue(Idx);
  256. if (IdxN == 0)
  257. // Unhandled operand. Halt "fast" selection and bail.
  258. return std::pair<unsigned, bool>(0, false);
  259. bool IdxNIsKill = hasTrivialKill(Idx);
  260. // If the index is smaller or larger than intptr_t, truncate or extend it.
  261. MVT PtrVT = TLI.getPointerTy();
  262. EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
  263. if (IdxVT.bitsLT(PtrVT)) {
  264. IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
  265. IdxN, IdxNIsKill);
  266. IdxNIsKill = true;
  267. }
  268. else if (IdxVT.bitsGT(PtrVT)) {
  269. IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
  270. IdxN, IdxNIsKill);
  271. IdxNIsKill = true;
  272. }
  273. return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
  274. }
  275. void FastISel::recomputeInsertPt() {
  276. if (getLastLocalValue()) {
  277. FuncInfo.InsertPt = getLastLocalValue();
  278. FuncInfo.MBB = FuncInfo.InsertPt->getParent();
  279. ++FuncInfo.InsertPt;
  280. } else
  281. FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
  282. // Now skip past any EH_LABELs, which must remain at the beginning.
  283. while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
  284. FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
  285. ++FuncInfo.InsertPt;
  286. }
  287. void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
  288. MachineBasicBlock::iterator E) {
  289. assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
  290. while (I != E) {
  291. MachineInstr *Dead = &*I;
  292. ++I;
  293. Dead->eraseFromParent();
  294. ++NumFastIselDead;
  295. }
  296. recomputeInsertPt();
  297. }
  298. FastISel::SavePoint FastISel::enterLocalValueArea() {
  299. MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
  300. DebugLoc OldDL = DbgLoc;
  301. recomputeInsertPt();
  302. DbgLoc = DebugLoc();
  303. SavePoint SP = { OldInsertPt, OldDL };
  304. return SP;
  305. }
  306. void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
  307. if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
  308. LastLocalValue = std::prev(FuncInfo.InsertPt);
  309. // Restore the previous insert position.
  310. FuncInfo.InsertPt = OldInsertPt.InsertPt;
  311. DbgLoc = OldInsertPt.DL;
  312. }
  313. /// SelectBinaryOp - Select and emit code for a binary operator instruction,
  314. /// which has an opcode which directly corresponds to the given ISD opcode.
  315. ///
  316. bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
  317. EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
  318. if (VT == MVT::Other || !VT.isSimple())
  319. // Unhandled type. Halt "fast" selection and bail.
  320. return false;
  321. // We only handle legal types. For example, on x86-32 the instruction
  322. // selector contains all of the 64-bit instructions from x86-64,
  323. // under the assumption that i64 won't be used if the target doesn't
  324. // support it.
  325. if (!TLI.isTypeLegal(VT)) {
  326. // MVT::i1 is special. Allow AND, OR, or XOR because they
  327. // don't require additional zeroing, which makes them easy.
  328. if (VT == MVT::i1 &&
  329. (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
  330. ISDOpcode == ISD::XOR))
  331. VT = TLI.getTypeToTransformTo(I->getContext(), VT);
  332. else
  333. return false;
  334. }
  335. // Check if the first operand is a constant, and handle it as "ri". At -O0,
  336. // we don't have anything that canonicalizes operand order.
  337. if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
  338. if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
  339. unsigned Op1 = getRegForValue(I->getOperand(1));
  340. if (Op1 == 0) return false;
  341. bool Op1IsKill = hasTrivialKill(I->getOperand(1));
  342. unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
  343. Op1IsKill, CI->getZExtValue(),
  344. VT.getSimpleVT());
  345. if (ResultReg == 0) return false;
  346. // We successfully emitted code for the given LLVM Instruction.
  347. UpdateValueMap(I, ResultReg);
  348. return true;
  349. }
  350. unsigned Op0 = getRegForValue(I->getOperand(0));
  351. if (Op0 == 0) // Unhandled operand. Halt "fast" selection and bail.
  352. return false;
  353. bool Op0IsKill = hasTrivialKill(I->getOperand(0));
  354. // Check if the second operand is a constant and handle it appropriately.
  355. if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
  356. uint64_t Imm = CI->getZExtValue();
  357. // Transform "sdiv exact X, 8" -> "sra X, 3".
  358. if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
  359. cast<BinaryOperator>(I)->isExact() &&
  360. isPowerOf2_64(Imm)) {
  361. Imm = Log2_64(Imm);
  362. ISDOpcode = ISD::SRA;
  363. }
  364. // Transform "urem x, pow2" -> "and x, pow2-1".
  365. if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
  366. isPowerOf2_64(Imm)) {
  367. --Imm;
  368. ISDOpcode = ISD::AND;
  369. }
  370. unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
  371. Op0IsKill, Imm, VT.getSimpleVT());
  372. if (ResultReg == 0) return false;
  373. // We successfully emitted code for the given LLVM Instruction.
  374. UpdateValueMap(I, ResultReg);
  375. return true;
  376. }
  377. // Check if the second operand is a constant float.
  378. if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
  379. unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
  380. ISDOpcode, Op0, Op0IsKill, CF);
  381. if (ResultReg != 0) {
  382. // We successfully emitted code for the given LLVM Instruction.
  383. UpdateValueMap(I, ResultReg);
  384. return true;
  385. }
  386. }
  387. unsigned Op1 = getRegForValue(I->getOperand(1));
  388. if (Op1 == 0)
  389. // Unhandled operand. Halt "fast" selection and bail.
  390. return false;
  391. bool Op1IsKill = hasTrivialKill(I->getOperand(1));
  392. // Now we have both operands in registers. Emit the instruction.
  393. unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
  394. ISDOpcode,
  395. Op0, Op0IsKill,
  396. Op1, Op1IsKill);
  397. if (ResultReg == 0)
  398. // Target-specific code wasn't able to find a machine opcode for
  399. // the given ISD opcode and type. Halt "fast" selection and bail.
  400. return false;
  401. // We successfully emitted code for the given LLVM Instruction.
  402. UpdateValueMap(I, ResultReg);
  403. return true;
  404. }
  405. bool FastISel::SelectGetElementPtr(const User *I) {
  406. unsigned N = getRegForValue(I->getOperand(0));
  407. if (N == 0)
  408. // Unhandled operand. Halt "fast" selection and bail.
  409. return false;
  410. bool NIsKill = hasTrivialKill(I->getOperand(0));
  411. // Keep a running tab of the total offset to coalesce multiple N = N + Offset
  412. // into a single N = N + TotalOffset.
  413. uint64_t TotalOffs = 0;
  414. // FIXME: What's a good SWAG number for MaxOffs?
  415. uint64_t MaxOffs = 2048;
  416. Type *Ty = I->getOperand(0)->getType();
  417. MVT VT = TLI.getPointerTy();
  418. for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
  419. E = I->op_end(); OI != E; ++OI) {
  420. const Value *Idx = *OI;
  421. if (StructType *StTy = dyn_cast<StructType>(Ty)) {
  422. unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
  423. if (Field) {
  424. // N = N + Offset
  425. TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
  426. if (TotalOffs >= MaxOffs) {
  427. N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  428. if (N == 0)
  429. // Unhandled operand. Halt "fast" selection and bail.
  430. return false;
  431. NIsKill = true;
  432. TotalOffs = 0;
  433. }
  434. }
  435. Ty = StTy->getElementType(Field);
  436. } else {
  437. Ty = cast<SequentialType>(Ty)->getElementType();
  438. // If this is a constant subscript, handle it quickly.
  439. if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
  440. if (CI->isZero()) continue;
  441. // N = N + Offset
  442. TotalOffs +=
  443. DL.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
  444. if (TotalOffs >= MaxOffs) {
  445. N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  446. if (N == 0)
  447. // Unhandled operand. Halt "fast" selection and bail.
  448. return false;
  449. NIsKill = true;
  450. TotalOffs = 0;
  451. }
  452. continue;
  453. }
  454. if (TotalOffs) {
  455. N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  456. if (N == 0)
  457. // Unhandled operand. Halt "fast" selection and bail.
  458. return false;
  459. NIsKill = true;
  460. TotalOffs = 0;
  461. }
  462. // N = N + Idx * ElementSize;
  463. uint64_t ElementSize = DL.getTypeAllocSize(Ty);
  464. std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
  465. unsigned IdxN = Pair.first;
  466. bool IdxNIsKill = Pair.second;
  467. if (IdxN == 0)
  468. // Unhandled operand. Halt "fast" selection and bail.
  469. return false;
  470. if (ElementSize != 1) {
  471. IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
  472. if (IdxN == 0)
  473. // Unhandled operand. Halt "fast" selection and bail.
  474. return false;
  475. IdxNIsKill = true;
  476. }
  477. N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
  478. if (N == 0)
  479. // Unhandled operand. Halt "fast" selection and bail.
  480. return false;
  481. }
  482. }
  483. if (TotalOffs) {
  484. N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
  485. if (N == 0)
  486. // Unhandled operand. Halt "fast" selection and bail.
  487. return false;
  488. }
  489. // We successfully emitted code for the given LLVM Instruction.
  490. UpdateValueMap(I, N);
  491. return true;
  492. }
  493. bool FastISel::SelectCall(const User *I) {
  494. const CallInst *Call = cast<CallInst>(I);
  495. // Handle simple inline asms.
  496. if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
  497. // Don't attempt to handle constraints.
  498. if (!IA->getConstraintString().empty())
  499. return false;
  500. unsigned ExtraInfo = 0;
  501. if (IA->hasSideEffects())
  502. ExtraInfo |= InlineAsm::Extra_HasSideEffects;
  503. if (IA->isAlignStack())
  504. ExtraInfo |= InlineAsm::Extra_IsAlignStack;
  505. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  506. TII.get(TargetOpcode::INLINEASM))
  507. .addExternalSymbol(IA->getAsmString().c_str())
  508. .addImm(ExtraInfo);
  509. return true;
  510. }
  511. MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
  512. ComputeUsesVAFloatArgument(*Call, &MMI);
  513. const Function *F = Call->getCalledFunction();
  514. if (!F) return false;
  515. // Handle selected intrinsic function calls.
  516. switch (F->getIntrinsicID()) {
  517. default: break;
  518. // At -O0 we don't care about the lifetime intrinsics.
  519. case Intrinsic::lifetime_start:
  520. case Intrinsic::lifetime_end:
  521. // The donothing intrinsic does, well, nothing.
  522. case Intrinsic::donothing:
  523. return true;
  524. case Intrinsic::dbg_declare: {
  525. const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
  526. DIVariable DIVar(DI->getVariable());
  527. assert((!DIVar || DIVar.isVariable()) &&
  528. "Variable in DbgDeclareInst should be either null or a DIVariable.");
  529. if (!DIVar ||
  530. !FuncInfo.MF->getMMI().hasDebugInfo()) {
  531. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  532. return true;
  533. }
  534. const Value *Address = DI->getAddress();
  535. if (!Address || isa<UndefValue>(Address)) {
  536. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  537. return true;
  538. }
  539. unsigned Offset = 0;
  540. Optional<MachineOperand> Op;
  541. if (const Argument *Arg = dyn_cast<Argument>(Address))
  542. // Some arguments' frame index is recorded during argument lowering.
  543. Offset = FuncInfo.getArgumentFrameIndex(Arg);
  544. if (Offset)
  545. Op = MachineOperand::CreateFI(Offset);
  546. if (!Op)
  547. if (unsigned Reg = lookUpRegForValue(Address))
  548. Op = MachineOperand::CreateReg(Reg, false);
  549. // If we have a VLA that has a "use" in a metadata node that's then used
  550. // here but it has no other uses, then we have a problem. E.g.,
  551. //
  552. // int foo (const int *x) {
  553. // char a[*x];
  554. // return 0;
  555. // }
  556. //
  557. // If we assign 'a' a vreg and fast isel later on has to use the selection
  558. // DAG isel, it will want to copy the value to the vreg. However, there are
  559. // no uses, which goes counter to what selection DAG isel expects.
  560. if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
  561. (!isa<AllocaInst>(Address) ||
  562. !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
  563. Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
  564. false);
  565. if (Op) {
  566. if (Op->isReg()) {
  567. Op->setIsDebug(true);
  568. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  569. TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
  570. DI->getVariable());
  571. } else
  572. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  573. TII.get(TargetOpcode::DBG_VALUE))
  574. .addOperand(*Op)
  575. .addImm(0)
  576. .addMetadata(DI->getVariable());
  577. } else {
  578. // We can't yet handle anything else here because it would require
  579. // generating code, thus altering codegen because of debug info.
  580. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  581. }
  582. return true;
  583. }
  584. case Intrinsic::dbg_value: {
  585. // This form of DBG_VALUE is target-independent.
  586. const DbgValueInst *DI = cast<DbgValueInst>(Call);
  587. const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
  588. const Value *V = DI->getValue();
  589. if (!V) {
  590. // Currently the optimizer can produce this; insert an undef to
  591. // help debugging. Probably the optimizer should not do this.
  592. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  593. .addReg(0U).addImm(DI->getOffset())
  594. .addMetadata(DI->getVariable());
  595. } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  596. if (CI->getBitWidth() > 64)
  597. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  598. .addCImm(CI).addImm(DI->getOffset())
  599. .addMetadata(DI->getVariable());
  600. else
  601. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  602. .addImm(CI->getZExtValue()).addImm(DI->getOffset())
  603. .addMetadata(DI->getVariable());
  604. } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
  605. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  606. .addFPImm(CF).addImm(DI->getOffset())
  607. .addMetadata(DI->getVariable());
  608. } else if (unsigned Reg = lookUpRegForValue(V)) {
  609. // FIXME: This does not handle register-indirect values at offset 0.
  610. bool IsIndirect = DI->getOffset() != 0;
  611. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect,
  612. Reg, DI->getOffset(), DI->getVariable());
  613. } else {
  614. // We can't yet handle anything else here because it would require
  615. // generating code, thus altering codegen because of debug info.
  616. DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
  617. }
  618. return true;
  619. }
  620. case Intrinsic::objectsize: {
  621. ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
  622. unsigned long long Res = CI->isZero() ? -1ULL : 0;
  623. Constant *ResCI = ConstantInt::get(Call->getType(), Res);
  624. unsigned ResultReg = getRegForValue(ResCI);
  625. if (ResultReg == 0)
  626. return false;
  627. UpdateValueMap(Call, ResultReg);
  628. return true;
  629. }
  630. case Intrinsic::expect: {
  631. unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
  632. if (ResultReg == 0)
  633. return false;
  634. UpdateValueMap(Call, ResultReg);
  635. return true;
  636. }
  637. }
  638. // Usually, it does not make sense to initialize a value,
  639. // make an unrelated function call and use the value, because
  640. // it tends to be spilled on the stack. So, we move the pointer
  641. // to the last local value to the beginning of the block, so that
  642. // all the values which have already been materialized,
  643. // appear after the call. It also makes sense to skip intrinsics
  644. // since they tend to be inlined.
  645. if (!isa<IntrinsicInst>(Call))
  646. flushLocalValueMap();
  647. // An arbitrary call. Bail.
  648. return false;
  649. }
  650. bool FastISel::SelectCast(const User *I, unsigned Opcode) {
  651. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  652. EVT DstVT = TLI.getValueType(I->getType());
  653. if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
  654. DstVT == MVT::Other || !DstVT.isSimple())
  655. // Unhandled type. Halt "fast" selection and bail.
  656. return false;
  657. // Check if the destination type is legal.
  658. if (!TLI.isTypeLegal(DstVT))
  659. return false;
  660. // Check if the source operand is legal.
  661. if (!TLI.isTypeLegal(SrcVT))
  662. return false;
  663. unsigned InputReg = getRegForValue(I->getOperand(0));
  664. if (!InputReg)
  665. // Unhandled operand. Halt "fast" selection and bail.
  666. return false;
  667. bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
  668. unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
  669. DstVT.getSimpleVT(),
  670. Opcode,
  671. InputReg, InputRegIsKill);
  672. if (!ResultReg)
  673. return false;
  674. UpdateValueMap(I, ResultReg);
  675. return true;
  676. }
  677. bool FastISel::SelectBitCast(const User *I) {
  678. // If the bitcast doesn't change the type, just use the operand value.
  679. if (I->getType() == I->getOperand(0)->getType()) {
  680. unsigned Reg = getRegForValue(I->getOperand(0));
  681. if (Reg == 0)
  682. return false;
  683. UpdateValueMap(I, Reg);
  684. return true;
  685. }
  686. // Bitcasts of other values become reg-reg copies or BITCAST operators.
  687. EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
  688. EVT DstEVT = TLI.getValueType(I->getType());
  689. if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
  690. !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
  691. // Unhandled type. Halt "fast" selection and bail.
  692. return false;
  693. MVT SrcVT = SrcEVT.getSimpleVT();
  694. MVT DstVT = DstEVT.getSimpleVT();
  695. unsigned Op0 = getRegForValue(I->getOperand(0));
  696. if (Op0 == 0)
  697. // Unhandled operand. Halt "fast" selection and bail.
  698. return false;
  699. bool Op0IsKill = hasTrivialKill(I->getOperand(0));
  700. // First, try to perform the bitcast by inserting a reg-reg copy.
  701. unsigned ResultReg = 0;
  702. if (SrcVT == DstVT) {
  703. const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
  704. const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
  705. // Don't attempt a cross-class copy. It will likely fail.
  706. if (SrcClass == DstClass) {
  707. ResultReg = createResultReg(DstClass);
  708. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  709. TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
  710. }
  711. }
  712. // If the reg-reg copy failed, select a BITCAST opcode.
  713. if (!ResultReg)
  714. ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
  715. if (!ResultReg)
  716. return false;
  717. UpdateValueMap(I, ResultReg);
  718. return true;
  719. }
  720. bool
  721. FastISel::SelectInstruction(const Instruction *I) {
  722. // Just before the terminator instruction, insert instructions to
  723. // feed PHI nodes in successor blocks.
  724. if (isa<TerminatorInst>(I))
  725. if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
  726. return false;
  727. DbgLoc = I->getDebugLoc();
  728. MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
  729. // As a special case, don't handle calls to builtin library functions that
  730. // may be translated directly to target instructions.
  731. if (const CallInst *Call = dyn_cast<CallInst>(I)) {
  732. const Function *F = Call->getCalledFunction();
  733. LibFunc::Func Func;
  734. if (F && !F->hasLocalLinkage() && F->hasName() &&
  735. LibInfo->getLibFunc(F->getName(), Func) &&
  736. LibInfo->hasOptimizedCodeGen(Func))
  737. return false;
  738. }
  739. // First, try doing target-independent selection.
  740. if (SelectOperator(I, I->getOpcode())) {
  741. ++NumFastIselSuccessIndependent;
  742. DbgLoc = DebugLoc();
  743. return true;
  744. }
  745. // Remove dead code. However, ignore call instructions since we've flushed
  746. // the local value map and recomputed the insert point.
  747. if (!isa<CallInst>(I)) {
  748. recomputeInsertPt();
  749. if (SavedInsertPt != FuncInfo.InsertPt)
  750. removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
  751. }
  752. // Next, try calling the target to attempt to handle the instruction.
  753. SavedInsertPt = FuncInfo.InsertPt;
  754. if (TargetSelectInstruction(I)) {
  755. ++NumFastIselSuccessTarget;
  756. DbgLoc = DebugLoc();
  757. return true;
  758. }
  759. // Check for dead code and remove as necessary.
  760. recomputeInsertPt();
  761. if (SavedInsertPt != FuncInfo.InsertPt)
  762. removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
  763. DbgLoc = DebugLoc();
  764. return false;
  765. }
  766. /// FastEmitBranch - Emit an unconditional branch to the given block,
  767. /// unless it is the immediate (fall-through) successor, and update
  768. /// the CFG.
  769. void
  770. FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
  771. if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
  772. FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
  773. // For more accurate line information if this is the only instruction
  774. // in the block then emit it, otherwise we have the unconditional
  775. // fall-through case, which needs no instructions.
  776. } else {
  777. // The unconditional branch case.
  778. TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
  779. SmallVector<MachineOperand, 0>(), DbgLoc);
  780. }
  781. FuncInfo.MBB->addSuccessor(MSucc);
  782. }
  783. /// SelectFNeg - Emit an FNeg operation.
  784. ///
  785. bool
  786. FastISel::SelectFNeg(const User *I) {
  787. unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
  788. if (OpReg == 0) return false;
  789. bool OpRegIsKill = hasTrivialKill(I);
  790. // If the target has ISD::FNEG, use it.
  791. EVT VT = TLI.getValueType(I->getType());
  792. unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
  793. ISD::FNEG, OpReg, OpRegIsKill);
  794. if (ResultReg != 0) {
  795. UpdateValueMap(I, ResultReg);
  796. return true;
  797. }
  798. // Bitcast the value to integer, twiddle the sign bit with xor,
  799. // and then bitcast it back to floating-point.
  800. if (VT.getSizeInBits() > 64) return false;
  801. EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
  802. if (!TLI.isTypeLegal(IntVT))
  803. return false;
  804. unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
  805. ISD::BITCAST, OpReg, OpRegIsKill);
  806. if (IntReg == 0)
  807. return false;
  808. unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
  809. IntReg, /*Kill=*/true,
  810. UINT64_C(1) << (VT.getSizeInBits()-1),
  811. IntVT.getSimpleVT());
  812. if (IntResultReg == 0)
  813. return false;
  814. ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
  815. ISD::BITCAST, IntResultReg, /*Kill=*/true);
  816. if (ResultReg == 0)
  817. return false;
  818. UpdateValueMap(I, ResultReg);
  819. return true;
  820. }
  821. bool
  822. FastISel::SelectExtractValue(const User *U) {
  823. const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
  824. if (!EVI)
  825. return false;
  826. // Make sure we only try to handle extracts with a legal result. But also
  827. // allow i1 because it's easy.
  828. EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
  829. if (!RealVT.isSimple())
  830. return false;
  831. MVT VT = RealVT.getSimpleVT();
  832. if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
  833. return false;
  834. const Value *Op0 = EVI->getOperand(0);
  835. Type *AggTy = Op0->getType();
  836. // Get the base result register.
  837. unsigned ResultReg;
  838. DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
  839. if (I != FuncInfo.ValueMap.end())
  840. ResultReg = I->second;
  841. else if (isa<Instruction>(Op0))
  842. ResultReg = FuncInfo.InitializeRegForValue(Op0);
  843. else
  844. return false; // fast-isel can't handle aggregate constants at the moment
  845. // Get the actual result register, which is an offset from the base register.
  846. unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
  847. SmallVector<EVT, 4> AggValueVTs;
  848. ComputeValueVTs(TLI, AggTy, AggValueVTs);
  849. for (unsigned i = 0; i < VTIndex; i++)
  850. ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
  851. UpdateValueMap(EVI, ResultReg);
  852. return true;
  853. }
  854. bool
  855. FastISel::SelectOperator(const User *I, unsigned Opcode) {
  856. switch (Opcode) {
  857. case Instruction::Add:
  858. return SelectBinaryOp(I, ISD::ADD);
  859. case Instruction::FAdd:
  860. return SelectBinaryOp(I, ISD::FADD);
  861. case Instruction::Sub:
  862. return SelectBinaryOp(I, ISD::SUB);
  863. case Instruction::FSub:
  864. // FNeg is currently represented in LLVM IR as a special case of FSub.
  865. if (BinaryOperator::isFNeg(I))
  866. return SelectFNeg(I);
  867. return SelectBinaryOp(I, ISD::FSUB);
  868. case Instruction::Mul:
  869. return SelectBinaryOp(I, ISD::MUL);
  870. case Instruction::FMul:
  871. return SelectBinaryOp(I, ISD::FMUL);
  872. case Instruction::SDiv:
  873. return SelectBinaryOp(I, ISD::SDIV);
  874. case Instruction::UDiv:
  875. return SelectBinaryOp(I, ISD::UDIV);
  876. case Instruction::FDiv:
  877. return SelectBinaryOp(I, ISD::FDIV);
  878. case Instruction::SRem:
  879. return SelectBinaryOp(I, ISD::SREM);
  880. case Instruction::URem:
  881. return SelectBinaryOp(I, ISD::UREM);
  882. case Instruction::FRem:
  883. return SelectBinaryOp(I, ISD::FREM);
  884. case Instruction::Shl:
  885. return SelectBinaryOp(I, ISD::SHL);
  886. case Instruction::LShr:
  887. return SelectBinaryOp(I, ISD::SRL);
  888. case Instruction::AShr:
  889. return SelectBinaryOp(I, ISD::SRA);
  890. case Instruction::And:
  891. return SelectBinaryOp(I, ISD::AND);
  892. case Instruction::Or:
  893. return SelectBinaryOp(I, ISD::OR);
  894. case Instruction::Xor:
  895. return SelectBinaryOp(I, ISD::XOR);
  896. case Instruction::GetElementPtr:
  897. return SelectGetElementPtr(I);
  898. case Instruction::Br: {
  899. const BranchInst *BI = cast<BranchInst>(I);
  900. if (BI->isUnconditional()) {
  901. const BasicBlock *LLVMSucc = BI->getSuccessor(0);
  902. MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
  903. FastEmitBranch(MSucc, BI->getDebugLoc());
  904. return true;
  905. }
  906. // Conditional branches are not handed yet.
  907. // Halt "fast" selection and bail.
  908. return false;
  909. }
  910. case Instruction::Unreachable:
  911. // Nothing to emit.
  912. return true;
  913. case Instruction::Alloca:
  914. // FunctionLowering has the static-sized case covered.
  915. if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
  916. return true;
  917. // Dynamic-sized alloca is not handled yet.
  918. return false;
  919. case Instruction::Call:
  920. return SelectCall(I);
  921. case Instruction::BitCast:
  922. return SelectBitCast(I);
  923. case Instruction::FPToSI:
  924. return SelectCast(I, ISD::FP_TO_SINT);
  925. case Instruction::ZExt:
  926. return SelectCast(I, ISD::ZERO_EXTEND);
  927. case Instruction::SExt:
  928. return SelectCast(I, ISD::SIGN_EXTEND);
  929. case Instruction::Trunc:
  930. return SelectCast(I, ISD::TRUNCATE);
  931. case Instruction::SIToFP:
  932. return SelectCast(I, ISD::SINT_TO_FP);
  933. case Instruction::IntToPtr: // Deliberate fall-through.
  934. case Instruction::PtrToInt: {
  935. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  936. EVT DstVT = TLI.getValueType(I->getType());
  937. if (DstVT.bitsGT(SrcVT))
  938. return SelectCast(I, ISD::ZERO_EXTEND);
  939. if (DstVT.bitsLT(SrcVT))
  940. return SelectCast(I, ISD::TRUNCATE);
  941. unsigned Reg = getRegForValue(I->getOperand(0));
  942. if (Reg == 0) return false;
  943. UpdateValueMap(I, Reg);
  944. return true;
  945. }
  946. case Instruction::ExtractValue:
  947. return SelectExtractValue(I);
  948. case Instruction::PHI:
  949. llvm_unreachable("FastISel shouldn't visit PHI nodes!");
  950. default:
  951. // Unhandled instruction. Halt "fast" selection and bail.
  952. return false;
  953. }
  954. }
  955. FastISel::FastISel(FunctionLoweringInfo &funcInfo,
  956. const TargetLibraryInfo *libInfo)
  957. : FuncInfo(funcInfo),
  958. MRI(FuncInfo.MF->getRegInfo()),
  959. MFI(*FuncInfo.MF->getFrameInfo()),
  960. MCP(*FuncInfo.MF->getConstantPool()),
  961. TM(FuncInfo.MF->getTarget()),
  962. DL(*TM.getDataLayout()),
  963. TII(*TM.getInstrInfo()),
  964. TLI(*TM.getTargetLowering()),
  965. TRI(*TM.getRegisterInfo()),
  966. LibInfo(libInfo) {
  967. }
  968. FastISel::~FastISel() {}
  969. bool FastISel::FastLowerArguments() {
  970. return false;
  971. }
  972. unsigned FastISel::FastEmit_(MVT, MVT,
  973. unsigned) {
  974. return 0;
  975. }
  976. unsigned FastISel::FastEmit_r(MVT, MVT,
  977. unsigned,
  978. unsigned /*Op0*/, bool /*Op0IsKill*/) {
  979. return 0;
  980. }
  981. unsigned FastISel::FastEmit_rr(MVT, MVT,
  982. unsigned,
  983. unsigned /*Op0*/, bool /*Op0IsKill*/,
  984. unsigned /*Op1*/, bool /*Op1IsKill*/) {
  985. return 0;
  986. }
  987. unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
  988. return 0;
  989. }
  990. unsigned FastISel::FastEmit_f(MVT, MVT,
  991. unsigned, const ConstantFP * /*FPImm*/) {
  992. return 0;
  993. }
  994. unsigned FastISel::FastEmit_ri(MVT, MVT,
  995. unsigned,
  996. unsigned /*Op0*/, bool /*Op0IsKill*/,
  997. uint64_t /*Imm*/) {
  998. return 0;
  999. }
  1000. unsigned FastISel::FastEmit_rf(MVT, MVT,
  1001. unsigned,
  1002. unsigned /*Op0*/, bool /*Op0IsKill*/,
  1003. const ConstantFP * /*FPImm*/) {
  1004. return 0;
  1005. }
  1006. unsigned FastISel::FastEmit_rri(MVT, MVT,
  1007. unsigned,
  1008. unsigned /*Op0*/, bool /*Op0IsKill*/,
  1009. unsigned /*Op1*/, bool /*Op1IsKill*/,
  1010. uint64_t /*Imm*/) {
  1011. return 0;
  1012. }
  1013. /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
  1014. /// to emit an instruction with an immediate operand using FastEmit_ri.
  1015. /// If that fails, it materializes the immediate into a register and try
  1016. /// FastEmit_rr instead.
  1017. unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
  1018. unsigned Op0, bool Op0IsKill,
  1019. uint64_t Imm, MVT ImmType) {
  1020. // If this is a multiply by a power of two, emit this as a shift left.
  1021. if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
  1022. Opcode = ISD::SHL;
  1023. Imm = Log2_64(Imm);
  1024. } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
  1025. // div x, 8 -> srl x, 3
  1026. Opcode = ISD::SRL;
  1027. Imm = Log2_64(Imm);
  1028. }
  1029. // Horrible hack (to be removed), check to make sure shift amounts are
  1030. // in-range.
  1031. if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
  1032. Imm >= VT.getSizeInBits())
  1033. return 0;
  1034. // First check if immediate type is legal. If not, we can't use the ri form.
  1035. unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
  1036. if (ResultReg != 0)
  1037. return ResultReg;
  1038. unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
  1039. if (MaterialReg == 0) {
  1040. // This is a bit ugly/slow, but failing here means falling out of
  1041. // fast-isel, which would be very slow.
  1042. IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
  1043. VT.getSizeInBits());
  1044. MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
  1045. assert (MaterialReg != 0 && "Unable to materialize imm.");
  1046. if (MaterialReg == 0) return 0;
  1047. }
  1048. return FastEmit_rr(VT, VT, Opcode,
  1049. Op0, Op0IsKill,
  1050. MaterialReg, /*Kill=*/true);
  1051. }
  1052. unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
  1053. return MRI.createVirtualRegister(RC);
  1054. }
  1055. unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
  1056. const TargetRegisterClass* RC) {
  1057. unsigned ResultReg = createResultReg(RC);
  1058. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1059. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
  1060. return ResultReg;
  1061. }
  1062. unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
  1063. const TargetRegisterClass *RC,
  1064. unsigned Op0, bool Op0IsKill) {
  1065. unsigned ResultReg = createResultReg(RC);
  1066. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1067. if (II.getNumDefs() >= 1)
  1068. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1069. .addReg(Op0, Op0IsKill * RegState::Kill);
  1070. else {
  1071. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1072. .addReg(Op0, Op0IsKill * RegState::Kill);
  1073. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1074. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1075. }
  1076. return ResultReg;
  1077. }
  1078. unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
  1079. const TargetRegisterClass *RC,
  1080. unsigned Op0, bool Op0IsKill,
  1081. unsigned Op1, bool Op1IsKill) {
  1082. unsigned ResultReg = createResultReg(RC);
  1083. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1084. if (II.getNumDefs() >= 1)
  1085. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1086. .addReg(Op0, Op0IsKill * RegState::Kill)
  1087. .addReg(Op1, Op1IsKill * RegState::Kill);
  1088. else {
  1089. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1090. .addReg(Op0, Op0IsKill * RegState::Kill)
  1091. .addReg(Op1, Op1IsKill * RegState::Kill);
  1092. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1093. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1094. }
  1095. return ResultReg;
  1096. }
  1097. unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
  1098. const TargetRegisterClass *RC,
  1099. unsigned Op0, bool Op0IsKill,
  1100. unsigned Op1, bool Op1IsKill,
  1101. unsigned Op2, bool Op2IsKill) {
  1102. unsigned ResultReg = createResultReg(RC);
  1103. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1104. if (II.getNumDefs() >= 1)
  1105. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1106. .addReg(Op0, Op0IsKill * RegState::Kill)
  1107. .addReg(Op1, Op1IsKill * RegState::Kill)
  1108. .addReg(Op2, Op2IsKill * RegState::Kill);
  1109. else {
  1110. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1111. .addReg(Op0, Op0IsKill * RegState::Kill)
  1112. .addReg(Op1, Op1IsKill * RegState::Kill)
  1113. .addReg(Op2, Op2IsKill * RegState::Kill);
  1114. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1115. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1116. }
  1117. return ResultReg;
  1118. }
  1119. unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
  1120. const TargetRegisterClass *RC,
  1121. unsigned Op0, bool Op0IsKill,
  1122. uint64_t Imm) {
  1123. unsigned ResultReg = createResultReg(RC);
  1124. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1125. if (II.getNumDefs() >= 1)
  1126. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1127. .addReg(Op0, Op0IsKill * RegState::Kill)
  1128. .addImm(Imm);
  1129. else {
  1130. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1131. .addReg(Op0, Op0IsKill * RegState::Kill)
  1132. .addImm(Imm);
  1133. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1134. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1135. }
  1136. return ResultReg;
  1137. }
  1138. unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
  1139. const TargetRegisterClass *RC,
  1140. unsigned Op0, bool Op0IsKill,
  1141. uint64_t Imm1, uint64_t Imm2) {
  1142. unsigned ResultReg = createResultReg(RC);
  1143. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1144. if (II.getNumDefs() >= 1)
  1145. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1146. .addReg(Op0, Op0IsKill * RegState::Kill)
  1147. .addImm(Imm1)
  1148. .addImm(Imm2);
  1149. else {
  1150. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1151. .addReg(Op0, Op0IsKill * RegState::Kill)
  1152. .addImm(Imm1)
  1153. .addImm(Imm2);
  1154. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1155. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1156. }
  1157. return ResultReg;
  1158. }
  1159. unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
  1160. const TargetRegisterClass *RC,
  1161. unsigned Op0, bool Op0IsKill,
  1162. const ConstantFP *FPImm) {
  1163. unsigned ResultReg = createResultReg(RC);
  1164. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1165. if (II.getNumDefs() >= 1)
  1166. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1167. .addReg(Op0, Op0IsKill * RegState::Kill)
  1168. .addFPImm(FPImm);
  1169. else {
  1170. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1171. .addReg(Op0, Op0IsKill * RegState::Kill)
  1172. .addFPImm(FPImm);
  1173. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1174. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1175. }
  1176. return ResultReg;
  1177. }
  1178. unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
  1179. const TargetRegisterClass *RC,
  1180. unsigned Op0, bool Op0IsKill,
  1181. unsigned Op1, bool Op1IsKill,
  1182. uint64_t Imm) {
  1183. unsigned ResultReg = createResultReg(RC);
  1184. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1185. if (II.getNumDefs() >= 1)
  1186. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1187. .addReg(Op0, Op0IsKill * RegState::Kill)
  1188. .addReg(Op1, Op1IsKill * RegState::Kill)
  1189. .addImm(Imm);
  1190. else {
  1191. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1192. .addReg(Op0, Op0IsKill * RegState::Kill)
  1193. .addReg(Op1, Op1IsKill * RegState::Kill)
  1194. .addImm(Imm);
  1195. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1196. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1197. }
  1198. return ResultReg;
  1199. }
  1200. unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
  1201. const TargetRegisterClass *RC,
  1202. unsigned Op0, bool Op0IsKill,
  1203. unsigned Op1, bool Op1IsKill,
  1204. uint64_t Imm1, uint64_t Imm2) {
  1205. unsigned ResultReg = createResultReg(RC);
  1206. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1207. if (II.getNumDefs() >= 1)
  1208. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1209. .addReg(Op0, Op0IsKill * RegState::Kill)
  1210. .addReg(Op1, Op1IsKill * RegState::Kill)
  1211. .addImm(Imm1).addImm(Imm2);
  1212. else {
  1213. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
  1214. .addReg(Op0, Op0IsKill * RegState::Kill)
  1215. .addReg(Op1, Op1IsKill * RegState::Kill)
  1216. .addImm(Imm1).addImm(Imm2);
  1217. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1218. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1219. }
  1220. return ResultReg;
  1221. }
  1222. unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
  1223. const TargetRegisterClass *RC,
  1224. uint64_t Imm) {
  1225. unsigned ResultReg = createResultReg(RC);
  1226. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1227. if (II.getNumDefs() >= 1)
  1228. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg).addImm(Imm);
  1229. else {
  1230. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
  1231. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1232. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1233. }
  1234. return ResultReg;
  1235. }
  1236. unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
  1237. const TargetRegisterClass *RC,
  1238. uint64_t Imm1, uint64_t Imm2) {
  1239. unsigned ResultReg = createResultReg(RC);
  1240. const MCInstrDesc &II = TII.get(MachineInstOpcode);
  1241. if (II.getNumDefs() >= 1)
  1242. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
  1243. .addImm(Imm1).addImm(Imm2);
  1244. else {
  1245. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1).addImm(Imm2);
  1246. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
  1247. TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
  1248. }
  1249. return ResultReg;
  1250. }
  1251. unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
  1252. unsigned Op0, bool Op0IsKill,
  1253. uint32_t Idx) {
  1254. unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
  1255. assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
  1256. "Cannot yet extract from physregs");
  1257. const TargetRegisterClass *RC = MRI.getRegClass(Op0);
  1258. MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
  1259. BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
  1260. DbgLoc, TII.get(TargetOpcode::COPY), ResultReg)
  1261. .addReg(Op0, getKillRegState(Op0IsKill), Idx);
  1262. return ResultReg;
  1263. }
  1264. /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
  1265. /// with all but the least significant bit set to zero.
  1266. unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
  1267. return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
  1268. }
  1269. /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
  1270. /// Emit code to ensure constants are copied into registers when needed.
  1271. /// Remember the virtual registers that need to be added to the Machine PHI
  1272. /// nodes as input. We cannot just directly add them, because expansion
  1273. /// might result in multiple MBB's for one BB. As such, the start of the
  1274. /// BB might correspond to a different MBB than the end.
  1275. bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
  1276. const TerminatorInst *TI = LLVMBB->getTerminator();
  1277. SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
  1278. unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
  1279. // Check successor nodes' PHI nodes that expect a constant to be available
  1280. // from this block.
  1281. for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
  1282. const BasicBlock *SuccBB = TI->getSuccessor(succ);
  1283. if (!isa<PHINode>(SuccBB->begin())) continue;
  1284. MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
  1285. // If this terminator has multiple identical successors (common for
  1286. // switches), only handle each succ once.
  1287. if (!SuccsHandled.insert(SuccMBB)) continue;
  1288. MachineBasicBlock::iterator MBBI = SuccMBB->begin();
  1289. // At this point we know that there is a 1-1 correspondence between LLVM PHI
  1290. // nodes and Machine PHI nodes, but the incoming operands have not been
  1291. // emitted yet.
  1292. for (BasicBlock::const_iterator I = SuccBB->begin();
  1293. const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  1294. // Ignore dead phi's.
  1295. if (PN->use_empty()) continue;
  1296. // Only handle legal types. Two interesting things to note here. First,
  1297. // by bailing out early, we may leave behind some dead instructions,
  1298. // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
  1299. // own moves. Second, this check is necessary because FastISel doesn't
  1300. // use CreateRegs to create registers, so it always creates
  1301. // exactly one register for each non-void instruction.
  1302. EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
  1303. if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
  1304. // Handle integer promotions, though, because they're common and easy.
  1305. if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
  1306. VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
  1307. else {
  1308. FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
  1309. return false;
  1310. }
  1311. }
  1312. const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
  1313. // Set the DebugLoc for the copy. Prefer the location of the operand
  1314. // if there is one; use the location of the PHI otherwise.
  1315. DbgLoc = PN->getDebugLoc();
  1316. if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
  1317. DbgLoc = Inst->getDebugLoc();
  1318. unsigned Reg = getRegForValue(PHIOp);
  1319. if (Reg == 0) {
  1320. FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
  1321. return false;
  1322. }
  1323. FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
  1324. DbgLoc = DebugLoc();
  1325. }
  1326. }
  1327. return true;
  1328. }
  1329. bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
  1330. assert(LI->hasOneUse() &&
  1331. "tryToFoldLoad expected a LoadInst with a single use");
  1332. // We know that the load has a single use, but don't know what it is. If it
  1333. // isn't one of the folded instructions, then we can't succeed here. Handle
  1334. // this by scanning the single-use users of the load until we get to FoldInst.
  1335. unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
  1336. const Instruction *TheUser = LI->use_back();
  1337. while (TheUser != FoldInst && // Scan up until we find FoldInst.
  1338. // Stay in the right block.
  1339. TheUser->getParent() == FoldInst->getParent() &&
  1340. --MaxUsers) { // Don't scan too far.
  1341. // If there are multiple or no uses of this instruction, then bail out.
  1342. if (!TheUser->hasOneUse())
  1343. return false;
  1344. TheUser = TheUser->use_back();
  1345. }
  1346. // If we didn't find the fold instruction, then we failed to collapse the
  1347. // sequence.
  1348. if (TheUser != FoldInst)
  1349. return false;
  1350. // Don't try to fold volatile loads. Target has to deal with alignment
  1351. // constraints.
  1352. if (LI->isVolatile())
  1353. return false;
  1354. // Figure out which vreg this is going into. If there is no assigned vreg yet
  1355. // then there actually was no reference to it. Perhaps the load is referenced
  1356. // by a dead instruction.
  1357. unsigned LoadReg = getRegForValue(LI);
  1358. if (LoadReg == 0)
  1359. return false;
  1360. // We can't fold if this vreg has no uses or more than one use. Multiple uses
  1361. // may mean that the instruction got lowered to multiple MIs, or the use of
  1362. // the loaded value ended up being multiple operands of the result.
  1363. if (!MRI.hasOneUse(LoadReg))
  1364. return false;
  1365. MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
  1366. MachineInstr *User = &*RI;
  1367. // Set the insertion point properly. Folding the load can cause generation of
  1368. // other random instructions (like sign extends) for addressing modes; make
  1369. // sure they get inserted in a logical place before the new instruction.
  1370. FuncInfo.InsertPt = User;
  1371. FuncInfo.MBB = User->getParent();
  1372. // Ask the target to try folding the load.
  1373. return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
  1374. }
  1375. bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
  1376. // Must be an add.
  1377. if (!isa<AddOperator>(Add))
  1378. return false;
  1379. // Type size needs to match.
  1380. if (DL.getTypeSizeInBits(GEP->getType()) !=
  1381. DL.getTypeSizeInBits(Add->getType()))
  1382. return false;
  1383. // Must be in the same basic block.
  1384. if (isa<Instruction>(Add) &&
  1385. FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
  1386. return false;
  1387. // Must have a constant operand.
  1388. return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
  1389. }