FastISel.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/Function.h"
  42. #include "llvm/GlobalVariable.h"
  43. #include "llvm/Instructions.h"
  44. #include "llvm/IntrinsicInst.h"
  45. #include "llvm/CodeGen/FastISel.h"
  46. #include "llvm/CodeGen/MachineInstrBuilder.h"
  47. #include "llvm/CodeGen/MachineModuleInfo.h"
  48. #include "llvm/CodeGen/MachineRegisterInfo.h"
  49. #include "llvm/CodeGen/DwarfWriter.h"
  50. #include "llvm/Analysis/DebugInfo.h"
  51. #include "llvm/Target/TargetData.h"
  52. #include "llvm/Target/TargetInstrInfo.h"
  53. #include "llvm/Target/TargetLowering.h"
  54. #include "llvm/Target/TargetMachine.h"
  55. #include "SelectionDAGBuild.h"
  56. using namespace llvm;
  57. unsigned FastISel::getRegForValue(Value *V) {
  58. EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
  59. // Don't handle non-simple values in FastISel.
  60. if (!RealVT.isSimple())
  61. return 0;
  62. // Ignore illegal types. We must do this before looking up the value
  63. // in ValueMap because Arguments are given virtual registers regardless
  64. // of whether FastISel can handle them.
  65. MVT VT = RealVT.getSimpleVT();
  66. if (!TLI.isTypeLegal(VT)) {
  67. // Promote MVT::i1 to a legal type though, because it's common and easy.
  68. if (VT == MVT::i1)
  69. VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
  70. else
  71. return 0;
  72. }
  73. // Look up the value to see if we already have a register for it. We
  74. // cache values defined by Instructions across blocks, and other values
  75. // only locally. This is because Instructions already have the SSA
  76. // def-dominatess-use requirement enforced.
  77. if (ValueMap.count(V))
  78. return ValueMap[V];
  79. unsigned Reg = LocalValueMap[V];
  80. if (Reg != 0)
  81. return Reg;
  82. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  83. if (CI->getValue().getActiveBits() <= 64)
  84. Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
  85. } else if (isa<AllocaInst>(V)) {
  86. Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
  87. } else if (isa<ConstantPointerNull>(V)) {
  88. // Translate this as an integer zero so that it can be
  89. // local-CSE'd with actual integer zeros.
  90. Reg =
  91. getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
  92. } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
  93. Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
  94. if (!Reg) {
  95. const APFloat &Flt = CF->getValueAPF();
  96. EVT IntVT = TLI.getPointerTy();
  97. uint64_t x[2];
  98. uint32_t IntBitWidth = IntVT.getSizeInBits();
  99. bool isExact;
  100. (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
  101. APFloat::rmTowardZero, &isExact);
  102. if (isExact) {
  103. APInt IntVal(IntBitWidth, 2, x);
  104. unsigned IntegerReg =
  105. getRegForValue(ConstantInt::get(V->getContext(), IntVal));
  106. if (IntegerReg != 0)
  107. Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
  108. }
  109. }
  110. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  111. if (!SelectOperator(CE, CE->getOpcode())) return 0;
  112. Reg = LocalValueMap[CE];
  113. } else if (isa<UndefValue>(V)) {
  114. Reg = createResultReg(TLI.getRegClassFor(VT));
  115. BuildMI(MBB, DL, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
  116. }
  117. // If target-independent code couldn't handle the value, give target-specific
  118. // code a try.
  119. if (!Reg && isa<Constant>(V))
  120. Reg = TargetMaterializeConstant(cast<Constant>(V));
  121. // Don't cache constant materializations in the general ValueMap.
  122. // To do so would require tracking what uses they dominate.
  123. if (Reg != 0)
  124. LocalValueMap[V] = Reg;
  125. return Reg;
  126. }
  127. unsigned FastISel::lookUpRegForValue(Value *V) {
  128. // Look up the value to see if we already have a register for it. We
  129. // cache values defined by Instructions across blocks, and other values
  130. // only locally. This is because Instructions already have the SSA
  131. // def-dominatess-use requirement enforced.
  132. if (ValueMap.count(V))
  133. return ValueMap[V];
  134. return LocalValueMap[V];
  135. }
  136. /// UpdateValueMap - Update the value map to include the new mapping for this
  137. /// instruction, or insert an extra copy to get the result in a previous
  138. /// determined register.
  139. /// NOTE: This is only necessary because we might select a block that uses
  140. /// a value before we select the block that defines the value. It might be
  141. /// possible to fix this by selecting blocks in reverse postorder.
  142. unsigned FastISel::UpdateValueMap(Value* I, unsigned Reg) {
  143. if (!isa<Instruction>(I)) {
  144. LocalValueMap[I] = Reg;
  145. return Reg;
  146. }
  147. unsigned &AssignedReg = ValueMap[I];
  148. if (AssignedReg == 0)
  149. AssignedReg = Reg;
  150. else if (Reg != AssignedReg) {
  151. const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
  152. TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
  153. Reg, RegClass, RegClass);
  154. }
  155. return AssignedReg;
  156. }
  157. unsigned FastISel::getRegForGEPIndex(Value *Idx) {
  158. unsigned IdxN = getRegForValue(Idx);
  159. if (IdxN == 0)
  160. // Unhandled operand. Halt "fast" selection and bail.
  161. return 0;
  162. // If the index is smaller or larger than intptr_t, truncate or extend it.
  163. MVT PtrVT = TLI.getPointerTy();
  164. EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
  165. if (IdxVT.bitsLT(PtrVT))
  166. IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN);
  167. else if (IdxVT.bitsGT(PtrVT))
  168. IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN);
  169. return IdxN;
  170. }
  171. /// SelectBinaryOp - Select and emit code for a binary operator instruction,
  172. /// which has an opcode which directly corresponds to the given ISD opcode.
  173. ///
  174. bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
  175. EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
  176. if (VT == MVT::Other || !VT.isSimple())
  177. // Unhandled type. Halt "fast" selection and bail.
  178. return false;
  179. // We only handle legal types. For example, on x86-32 the instruction
  180. // selector contains all of the 64-bit instructions from x86-64,
  181. // under the assumption that i64 won't be used if the target doesn't
  182. // support it.
  183. if (!TLI.isTypeLegal(VT)) {
  184. // MVT::i1 is special. Allow AND, OR, or XOR because they
  185. // don't require additional zeroing, which makes them easy.
  186. if (VT == MVT::i1 &&
  187. (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
  188. ISDOpcode == ISD::XOR))
  189. VT = TLI.getTypeToTransformTo(I->getContext(), VT);
  190. else
  191. return false;
  192. }
  193. unsigned Op0 = getRegForValue(I->getOperand(0));
  194. if (Op0 == 0)
  195. // Unhandled operand. Halt "fast" selection and bail.
  196. return false;
  197. // Check if the second operand is a constant and handle it appropriately.
  198. if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
  199. unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
  200. ISDOpcode, Op0, CI->getZExtValue());
  201. if (ResultReg != 0) {
  202. // We successfully emitted code for the given LLVM Instruction.
  203. UpdateValueMap(I, ResultReg);
  204. return true;
  205. }
  206. }
  207. // Check if the second operand is a constant float.
  208. if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
  209. unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
  210. ISDOpcode, Op0, CF);
  211. if (ResultReg != 0) {
  212. // We successfully emitted code for the given LLVM Instruction.
  213. UpdateValueMap(I, ResultReg);
  214. return true;
  215. }
  216. }
  217. unsigned Op1 = getRegForValue(I->getOperand(1));
  218. if (Op1 == 0)
  219. // Unhandled operand. Halt "fast" selection and bail.
  220. return false;
  221. // Now we have both operands in registers. Emit the instruction.
  222. unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
  223. ISDOpcode, Op0, Op1);
  224. if (ResultReg == 0)
  225. // Target-specific code wasn't able to find a machine opcode for
  226. // the given ISD opcode and type. Halt "fast" selection and bail.
  227. return false;
  228. // We successfully emitted code for the given LLVM Instruction.
  229. UpdateValueMap(I, ResultReg);
  230. return true;
  231. }
  232. bool FastISel::SelectGetElementPtr(User *I) {
  233. unsigned N = getRegForValue(I->getOperand(0));
  234. if (N == 0)
  235. // Unhandled operand. Halt "fast" selection and bail.
  236. return false;
  237. const Type *Ty = I->getOperand(0)->getType();
  238. MVT VT = TLI.getPointerTy();
  239. for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
  240. OI != E; ++OI) {
  241. Value *Idx = *OI;
  242. if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
  243. unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
  244. if (Field) {
  245. // N = N + Offset
  246. uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
  247. // FIXME: This can be optimized by combining the add with a
  248. // subsequent one.
  249. N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
  250. if (N == 0)
  251. // Unhandled operand. Halt "fast" selection and bail.
  252. return false;
  253. }
  254. Ty = StTy->getElementType(Field);
  255. } else {
  256. Ty = cast<SequentialType>(Ty)->getElementType();
  257. // If this is a constant subscript, handle it quickly.
  258. if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
  259. if (CI->getZExtValue() == 0) continue;
  260. uint64_t Offs =
  261. TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
  262. N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
  263. if (N == 0)
  264. // Unhandled operand. Halt "fast" selection and bail.
  265. return false;
  266. continue;
  267. }
  268. // N = N + Idx * ElementSize;
  269. uint64_t ElementSize = TD.getTypeAllocSize(Ty);
  270. unsigned IdxN = getRegForGEPIndex(Idx);
  271. if (IdxN == 0)
  272. // Unhandled operand. Halt "fast" selection and bail.
  273. return false;
  274. if (ElementSize != 1) {
  275. IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
  276. if (IdxN == 0)
  277. // Unhandled operand. Halt "fast" selection and bail.
  278. return false;
  279. }
  280. N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
  281. if (N == 0)
  282. // Unhandled operand. Halt "fast" selection and bail.
  283. return false;
  284. }
  285. }
  286. // We successfully emitted code for the given LLVM Instruction.
  287. UpdateValueMap(I, N);
  288. return true;
  289. }
  290. bool FastISel::SelectCall(User *I) {
  291. Function *F = cast<CallInst>(I)->getCalledFunction();
  292. if (!F) return false;
  293. unsigned IID = F->getIntrinsicID();
  294. switch (IID) {
  295. default: break;
  296. case Intrinsic::dbg_stoppoint: {
  297. DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
  298. if (isValidDebugInfoIntrinsic(*SPI, CodeGenOpt::None))
  299. setCurDebugLoc(ExtractDebugLocation(*SPI, MF.getDebugLocInfo()));
  300. return true;
  301. }
  302. case Intrinsic::dbg_region_start: {
  303. DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
  304. if (isValidDebugInfoIntrinsic(*RSI, CodeGenOpt::None) && DW
  305. && DW->ShouldEmitDwarfDebug()) {
  306. unsigned ID =
  307. DW->RecordRegionStart(cast<GlobalVariable>(RSI->getContext()));
  308. const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
  309. BuildMI(MBB, DL, II).addImm(ID);
  310. }
  311. return true;
  312. }
  313. case Intrinsic::dbg_region_end: {
  314. DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
  315. if (isValidDebugInfoIntrinsic(*REI, CodeGenOpt::None) && DW
  316. && DW->ShouldEmitDwarfDebug()) {
  317. unsigned ID = 0;
  318. DISubprogram Subprogram(cast<GlobalVariable>(REI->getContext()));
  319. if (isInlinedFnEnd(*REI, MF.getFunction())) {
  320. // This is end of an inlined function.
  321. const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
  322. ID = DW->RecordInlinedFnEnd(Subprogram);
  323. if (ID)
  324. // Returned ID is 0 if this is unbalanced "end of inlined
  325. // scope". This could happen if optimizer eats dbg intrinsics
  326. // or "beginning of inlined scope" is not recoginized due to
  327. // missing location info. In such cases, ignore this region.end.
  328. BuildMI(MBB, DL, II).addImm(ID);
  329. } else {
  330. const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
  331. ID = DW->RecordRegionEnd(cast<GlobalVariable>(REI->getContext()));
  332. BuildMI(MBB, DL, II).addImm(ID);
  333. }
  334. }
  335. return true;
  336. }
  337. case Intrinsic::dbg_func_start: {
  338. DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
  339. if (!isValidDebugInfoIntrinsic(*FSI, CodeGenOpt::None) || !DW
  340. || !DW->ShouldEmitDwarfDebug())
  341. return true;
  342. if (isInlinedFnStart(*FSI, MF.getFunction())) {
  343. // This is a beginning of an inlined function.
  344. // If llvm.dbg.func.start is seen in a new block before any
  345. // llvm.dbg.stoppoint intrinsic then the location info is unknown.
  346. // FIXME : Why DebugLoc is reset at the beginning of each block ?
  347. DebugLoc PrevLoc = DL;
  348. if (PrevLoc.isUnknown())
  349. return true;
  350. // Record the source line.
  351. setCurDebugLoc(ExtractDebugLocation(*FSI, MF.getDebugLocInfo()));
  352. DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc);
  353. DISubprogram SP(cast<GlobalVariable>(FSI->getSubprogram()));
  354. unsigned LabelID = DW->RecordInlinedFnStart(SP,
  355. DICompileUnit(PrevLocTpl.CompileUnit),
  356. PrevLocTpl.Line,
  357. PrevLocTpl.Col);
  358. const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
  359. BuildMI(MBB, DL, II).addImm(LabelID);
  360. return true;
  361. }
  362. // This is a beginning of a new function.
  363. MF.setDefaultDebugLoc(ExtractDebugLocation(*FSI, MF.getDebugLocInfo()));
  364. // llvm.dbg.func_start also defines beginning of function scope.
  365. DW->RecordRegionStart(cast<GlobalVariable>(FSI->getSubprogram()));
  366. return true;
  367. }
  368. case Intrinsic::dbg_declare: {
  369. DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
  370. if (!isValidDebugInfoIntrinsic(*DI, CodeGenOpt::None) || !DW
  371. || !DW->ShouldEmitDwarfDebug())
  372. return true;
  373. Value *Variable = DI->getVariable();
  374. Value *Address = DI->getAddress();
  375. if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
  376. Address = BCI->getOperand(0);
  377. AllocaInst *AI = dyn_cast<AllocaInst>(Address);
  378. // Don't handle byval struct arguments or VLAs, for example.
  379. if (!AI) break;
  380. DenseMap<const AllocaInst*, int>::iterator SI =
  381. StaticAllocaMap.find(AI);
  382. if (SI == StaticAllocaMap.end()) break; // VLAs.
  383. int FI = SI->second;
  384. // Determine the debug globalvariable.
  385. GlobalValue *GV = cast<GlobalVariable>(Variable);
  386. // Build the DECLARE instruction.
  387. const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
  388. MachineInstr *DeclareMI
  389. = BuildMI(MBB, DL, II).addFrameIndex(FI).addGlobalAddress(GV);
  390. DIVariable DV(cast<GlobalVariable>(GV));
  391. DW->RecordVariableScope(DV, DeclareMI);
  392. return true;
  393. }
  394. case Intrinsic::eh_exception: {
  395. EVT VT = TLI.getValueType(I->getType());
  396. switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
  397. default: break;
  398. case TargetLowering::Expand: {
  399. assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
  400. unsigned Reg = TLI.getExceptionAddressRegister();
  401. const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
  402. unsigned ResultReg = createResultReg(RC);
  403. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  404. Reg, RC, RC);
  405. assert(InsertedCopy && "Can't copy address registers!");
  406. InsertedCopy = InsertedCopy;
  407. UpdateValueMap(I, ResultReg);
  408. return true;
  409. }
  410. }
  411. break;
  412. }
  413. case Intrinsic::eh_selector_i32:
  414. case Intrinsic::eh_selector_i64: {
  415. EVT VT = TLI.getValueType(I->getType());
  416. switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
  417. default: break;
  418. case TargetLowering::Expand: {
  419. EVT VT = (IID == Intrinsic::eh_selector_i32 ?
  420. MVT::i32 : MVT::i64);
  421. if (MMI) {
  422. if (MBB->isLandingPad())
  423. AddCatchInfo(*cast<CallInst>(I), MMI, MBB);
  424. else {
  425. #ifndef NDEBUG
  426. CatchInfoLost.insert(cast<CallInst>(I));
  427. #endif
  428. // FIXME: Mark exception selector register as live in. Hack for PR1508.
  429. unsigned Reg = TLI.getExceptionSelectorRegister();
  430. if (Reg) MBB->addLiveIn(Reg);
  431. }
  432. unsigned Reg = TLI.getExceptionSelectorRegister();
  433. const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
  434. unsigned ResultReg = createResultReg(RC);
  435. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  436. Reg, RC, RC);
  437. assert(InsertedCopy && "Can't copy address registers!");
  438. InsertedCopy = InsertedCopy;
  439. UpdateValueMap(I, ResultReg);
  440. } else {
  441. unsigned ResultReg =
  442. getRegForValue(Constant::getNullValue(I->getType()));
  443. UpdateValueMap(I, ResultReg);
  444. }
  445. return true;
  446. }
  447. }
  448. break;
  449. }
  450. }
  451. return false;
  452. }
  453. bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
  454. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  455. EVT DstVT = TLI.getValueType(I->getType());
  456. if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
  457. DstVT == MVT::Other || !DstVT.isSimple())
  458. // Unhandled type. Halt "fast" selection and bail.
  459. return false;
  460. // Check if the destination type is legal. Or as a special case,
  461. // it may be i1 if we're doing a truncate because that's
  462. // easy and somewhat common.
  463. if (!TLI.isTypeLegal(DstVT))
  464. if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
  465. // Unhandled type. Halt "fast" selection and bail.
  466. return false;
  467. // Check if the source operand is legal. Or as a special case,
  468. // it may be i1 if we're doing zero-extension because that's
  469. // easy and somewhat common.
  470. if (!TLI.isTypeLegal(SrcVT))
  471. if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
  472. // Unhandled type. Halt "fast" selection and bail.
  473. return false;
  474. unsigned InputReg = getRegForValue(I->getOperand(0));
  475. if (!InputReg)
  476. // Unhandled operand. Halt "fast" selection and bail.
  477. return false;
  478. // If the operand is i1, arrange for the high bits in the register to be zero.
  479. if (SrcVT == MVT::i1) {
  480. SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
  481. InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg);
  482. if (!InputReg)
  483. return false;
  484. }
  485. // If the result is i1, truncate to the target's type for i1 first.
  486. if (DstVT == MVT::i1)
  487. DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
  488. unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
  489. DstVT.getSimpleVT(),
  490. Opcode,
  491. InputReg);
  492. if (!ResultReg)
  493. return false;
  494. UpdateValueMap(I, ResultReg);
  495. return true;
  496. }
  497. bool FastISel::SelectBitCast(User *I) {
  498. // If the bitcast doesn't change the type, just use the operand value.
  499. if (I->getType() == I->getOperand(0)->getType()) {
  500. unsigned Reg = getRegForValue(I->getOperand(0));
  501. if (Reg == 0)
  502. return false;
  503. UpdateValueMap(I, Reg);
  504. return true;
  505. }
  506. // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
  507. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  508. EVT DstVT = TLI.getValueType(I->getType());
  509. if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
  510. DstVT == MVT::Other || !DstVT.isSimple() ||
  511. !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
  512. // Unhandled type. Halt "fast" selection and bail.
  513. return false;
  514. unsigned Op0 = getRegForValue(I->getOperand(0));
  515. if (Op0 == 0)
  516. // Unhandled operand. Halt "fast" selection and bail.
  517. return false;
  518. // First, try to perform the bitcast by inserting a reg-reg copy.
  519. unsigned ResultReg = 0;
  520. if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
  521. TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
  522. TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
  523. ResultReg = createResultReg(DstClass);
  524. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  525. Op0, DstClass, SrcClass);
  526. if (!InsertedCopy)
  527. ResultReg = 0;
  528. }
  529. // If the reg-reg copy failed, select a BIT_CONVERT opcode.
  530. if (!ResultReg)
  531. ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
  532. ISD::BIT_CONVERT, Op0);
  533. if (!ResultReg)
  534. return false;
  535. UpdateValueMap(I, ResultReg);
  536. return true;
  537. }
  538. bool
  539. FastISel::SelectInstruction(Instruction *I) {
  540. return SelectOperator(I, I->getOpcode());
  541. }
  542. /// FastEmitBranch - Emit an unconditional branch to the given block,
  543. /// unless it is the immediate (fall-through) successor, and update
  544. /// the CFG.
  545. void
  546. FastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
  547. MachineFunction::iterator NextMBB =
  548. next(MachineFunction::iterator(MBB));
  549. if (MBB->isLayoutSuccessor(MSucc)) {
  550. // The unconditional fall-through case, which needs no instructions.
  551. } else {
  552. // The unconditional branch case.
  553. TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
  554. }
  555. MBB->addSuccessor(MSucc);
  556. }
  557. bool
  558. FastISel::SelectOperator(User *I, unsigned Opcode) {
  559. switch (Opcode) {
  560. case Instruction::Add:
  561. return SelectBinaryOp(I, ISD::ADD);
  562. case Instruction::FAdd:
  563. return SelectBinaryOp(I, ISD::FADD);
  564. case Instruction::Sub:
  565. return SelectBinaryOp(I, ISD::SUB);
  566. case Instruction::FSub:
  567. return SelectBinaryOp(I, ISD::FSUB);
  568. case Instruction::Mul:
  569. return SelectBinaryOp(I, ISD::MUL);
  570. case Instruction::FMul:
  571. return SelectBinaryOp(I, ISD::FMUL);
  572. case Instruction::SDiv:
  573. return SelectBinaryOp(I, ISD::SDIV);
  574. case Instruction::UDiv:
  575. return SelectBinaryOp(I, ISD::UDIV);
  576. case Instruction::FDiv:
  577. return SelectBinaryOp(I, ISD::FDIV);
  578. case Instruction::SRem:
  579. return SelectBinaryOp(I, ISD::SREM);
  580. case Instruction::URem:
  581. return SelectBinaryOp(I, ISD::UREM);
  582. case Instruction::FRem:
  583. return SelectBinaryOp(I, ISD::FREM);
  584. case Instruction::Shl:
  585. return SelectBinaryOp(I, ISD::SHL);
  586. case Instruction::LShr:
  587. return SelectBinaryOp(I, ISD::SRL);
  588. case Instruction::AShr:
  589. return SelectBinaryOp(I, ISD::SRA);
  590. case Instruction::And:
  591. return SelectBinaryOp(I, ISD::AND);
  592. case Instruction::Or:
  593. return SelectBinaryOp(I, ISD::OR);
  594. case Instruction::Xor:
  595. return SelectBinaryOp(I, ISD::XOR);
  596. case Instruction::GetElementPtr:
  597. return SelectGetElementPtr(I);
  598. case Instruction::Br: {
  599. BranchInst *BI = cast<BranchInst>(I);
  600. if (BI->isUnconditional()) {
  601. BasicBlock *LLVMSucc = BI->getSuccessor(0);
  602. MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
  603. FastEmitBranch(MSucc);
  604. return true;
  605. }
  606. // Conditional branches are not handed yet.
  607. // Halt "fast" selection and bail.
  608. return false;
  609. }
  610. case Instruction::Unreachable:
  611. // Nothing to emit.
  612. return true;
  613. case Instruction::PHI:
  614. // PHI nodes are already emitted.
  615. return true;
  616. case Instruction::Alloca:
  617. // FunctionLowering has the static-sized case covered.
  618. if (StaticAllocaMap.count(cast<AllocaInst>(I)))
  619. return true;
  620. // Dynamic-sized alloca is not handled yet.
  621. return false;
  622. case Instruction::Call:
  623. return SelectCall(I);
  624. case Instruction::BitCast:
  625. return SelectBitCast(I);
  626. case Instruction::FPToSI:
  627. return SelectCast(I, ISD::FP_TO_SINT);
  628. case Instruction::ZExt:
  629. return SelectCast(I, ISD::ZERO_EXTEND);
  630. case Instruction::SExt:
  631. return SelectCast(I, ISD::SIGN_EXTEND);
  632. case Instruction::Trunc:
  633. return SelectCast(I, ISD::TRUNCATE);
  634. case Instruction::SIToFP:
  635. return SelectCast(I, ISD::SINT_TO_FP);
  636. case Instruction::IntToPtr: // Deliberate fall-through.
  637. case Instruction::PtrToInt: {
  638. EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
  639. EVT DstVT = TLI.getValueType(I->getType());
  640. if (DstVT.bitsGT(SrcVT))
  641. return SelectCast(I, ISD::ZERO_EXTEND);
  642. if (DstVT.bitsLT(SrcVT))
  643. return SelectCast(I, ISD::TRUNCATE);
  644. unsigned Reg = getRegForValue(I->getOperand(0));
  645. if (Reg == 0) return false;
  646. UpdateValueMap(I, Reg);
  647. return true;
  648. }
  649. default:
  650. // Unhandled instruction. Halt "fast" selection and bail.
  651. return false;
  652. }
  653. }
  654. FastISel::FastISel(MachineFunction &mf,
  655. MachineModuleInfo *mmi,
  656. DwarfWriter *dw,
  657. DenseMap<const Value *, unsigned> &vm,
  658. DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
  659. DenseMap<const AllocaInst *, int> &am
  660. #ifndef NDEBUG
  661. , SmallSet<Instruction*, 8> &cil
  662. #endif
  663. )
  664. : MBB(0),
  665. ValueMap(vm),
  666. MBBMap(bm),
  667. StaticAllocaMap(am),
  668. #ifndef NDEBUG
  669. CatchInfoLost(cil),
  670. #endif
  671. MF(mf),
  672. MMI(mmi),
  673. DW(dw),
  674. MRI(MF.getRegInfo()),
  675. MFI(*MF.getFrameInfo()),
  676. MCP(*MF.getConstantPool()),
  677. TM(MF.getTarget()),
  678. TD(*TM.getTargetData()),
  679. TII(*TM.getInstrInfo()),
  680. TLI(*TM.getTargetLowering()) {
  681. }
  682. FastISel::~FastISel() {}
  683. unsigned FastISel::FastEmit_(MVT, MVT,
  684. ISD::NodeType) {
  685. return 0;
  686. }
  687. unsigned FastISel::FastEmit_r(MVT, MVT,
  688. ISD::NodeType, unsigned /*Op0*/) {
  689. return 0;
  690. }
  691. unsigned FastISel::FastEmit_rr(MVT, MVT,
  692. ISD::NodeType, unsigned /*Op0*/,
  693. unsigned /*Op0*/) {
  694. return 0;
  695. }
  696. unsigned FastISel::FastEmit_i(MVT, MVT, ISD::NodeType, uint64_t /*Imm*/) {
  697. return 0;
  698. }
  699. unsigned FastISel::FastEmit_f(MVT, MVT,
  700. ISD::NodeType, ConstantFP * /*FPImm*/) {
  701. return 0;
  702. }
  703. unsigned FastISel::FastEmit_ri(MVT, MVT,
  704. ISD::NodeType, unsigned /*Op0*/,
  705. uint64_t /*Imm*/) {
  706. return 0;
  707. }
  708. unsigned FastISel::FastEmit_rf(MVT, MVT,
  709. ISD::NodeType, unsigned /*Op0*/,
  710. ConstantFP * /*FPImm*/) {
  711. return 0;
  712. }
  713. unsigned FastISel::FastEmit_rri(MVT, MVT,
  714. ISD::NodeType,
  715. unsigned /*Op0*/, unsigned /*Op1*/,
  716. uint64_t /*Imm*/) {
  717. return 0;
  718. }
  719. /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
  720. /// to emit an instruction with an immediate operand using FastEmit_ri.
  721. /// If that fails, it materializes the immediate into a register and try
  722. /// FastEmit_rr instead.
  723. unsigned FastISel::FastEmit_ri_(MVT VT, ISD::NodeType Opcode,
  724. unsigned Op0, uint64_t Imm,
  725. MVT ImmType) {
  726. // First check if immediate type is legal. If not, we can't use the ri form.
  727. unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
  728. if (ResultReg != 0)
  729. return ResultReg;
  730. unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
  731. if (MaterialReg == 0)
  732. return 0;
  733. return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
  734. }
  735. /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
  736. /// to emit an instruction with a floating-point immediate operand using
  737. /// FastEmit_rf. If that fails, it materializes the immediate into a register
  738. /// and try FastEmit_rr instead.
  739. unsigned FastISel::FastEmit_rf_(MVT VT, ISD::NodeType Opcode,
  740. unsigned Op0, ConstantFP *FPImm,
  741. MVT ImmType) {
  742. // First check if immediate type is legal. If not, we can't use the rf form.
  743. unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
  744. if (ResultReg != 0)
  745. return ResultReg;
  746. // Materialize the constant in a register.
  747. unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
  748. if (MaterialReg == 0) {
  749. // If the target doesn't have a way to directly enter a floating-point
  750. // value into a register, use an alternate approach.
  751. // TODO: The current approach only supports floating-point constants
  752. // that can be constructed by conversion from integer values. This should
  753. // be replaced by code that creates a load from a constant-pool entry,
  754. // which will require some target-specific work.
  755. const APFloat &Flt = FPImm->getValueAPF();
  756. EVT IntVT = TLI.getPointerTy();
  757. uint64_t x[2];
  758. uint32_t IntBitWidth = IntVT.getSizeInBits();
  759. bool isExact;
  760. (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
  761. APFloat::rmTowardZero, &isExact);
  762. if (!isExact)
  763. return 0;
  764. APInt IntVal(IntBitWidth, 2, x);
  765. unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
  766. ISD::Constant, IntVal.getZExtValue());
  767. if (IntegerReg == 0)
  768. return 0;
  769. MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
  770. ISD::SINT_TO_FP, IntegerReg);
  771. if (MaterialReg == 0)
  772. return 0;
  773. }
  774. return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
  775. }
  776. unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
  777. return MRI.createVirtualRegister(RC);
  778. }
  779. unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
  780. const TargetRegisterClass* RC) {
  781. unsigned ResultReg = createResultReg(RC);
  782. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  783. BuildMI(MBB, DL, II, ResultReg);
  784. return ResultReg;
  785. }
  786. unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
  787. const TargetRegisterClass *RC,
  788. unsigned Op0) {
  789. unsigned ResultReg = createResultReg(RC);
  790. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  791. if (II.getNumDefs() >= 1)
  792. BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
  793. else {
  794. BuildMI(MBB, DL, II).addReg(Op0);
  795. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  796. II.ImplicitDefs[0], RC, RC);
  797. if (!InsertedCopy)
  798. ResultReg = 0;
  799. }
  800. return ResultReg;
  801. }
  802. unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
  803. const TargetRegisterClass *RC,
  804. unsigned Op0, unsigned Op1) {
  805. unsigned ResultReg = createResultReg(RC);
  806. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  807. if (II.getNumDefs() >= 1)
  808. BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
  809. else {
  810. BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
  811. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  812. II.ImplicitDefs[0], RC, RC);
  813. if (!InsertedCopy)
  814. ResultReg = 0;
  815. }
  816. return ResultReg;
  817. }
  818. unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
  819. const TargetRegisterClass *RC,
  820. unsigned Op0, uint64_t Imm) {
  821. unsigned ResultReg = createResultReg(RC);
  822. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  823. if (II.getNumDefs() >= 1)
  824. BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
  825. else {
  826. BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
  827. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  828. II.ImplicitDefs[0], RC, RC);
  829. if (!InsertedCopy)
  830. ResultReg = 0;
  831. }
  832. return ResultReg;
  833. }
  834. unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
  835. const TargetRegisterClass *RC,
  836. unsigned Op0, ConstantFP *FPImm) {
  837. unsigned ResultReg = createResultReg(RC);
  838. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  839. if (II.getNumDefs() >= 1)
  840. BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
  841. else {
  842. BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
  843. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  844. II.ImplicitDefs[0], RC, RC);
  845. if (!InsertedCopy)
  846. ResultReg = 0;
  847. }
  848. return ResultReg;
  849. }
  850. unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
  851. const TargetRegisterClass *RC,
  852. unsigned Op0, unsigned Op1, uint64_t Imm) {
  853. unsigned ResultReg = createResultReg(RC);
  854. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  855. if (II.getNumDefs() >= 1)
  856. BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
  857. else {
  858. BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
  859. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  860. II.ImplicitDefs[0], RC, RC);
  861. if (!InsertedCopy)
  862. ResultReg = 0;
  863. }
  864. return ResultReg;
  865. }
  866. unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
  867. const TargetRegisterClass *RC,
  868. uint64_t Imm) {
  869. unsigned ResultReg = createResultReg(RC);
  870. const TargetInstrDesc &II = TII.get(MachineInstOpcode);
  871. if (II.getNumDefs() >= 1)
  872. BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
  873. else {
  874. BuildMI(MBB, DL, II).addImm(Imm);
  875. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  876. II.ImplicitDefs[0], RC, RC);
  877. if (!InsertedCopy)
  878. ResultReg = 0;
  879. }
  880. return ResultReg;
  881. }
  882. unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
  883. unsigned Op0, uint32_t Idx) {
  884. const TargetRegisterClass* RC = MRI.getRegClass(Op0);
  885. unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
  886. const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
  887. if (II.getNumDefs() >= 1)
  888. BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
  889. else {
  890. BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
  891. bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
  892. II.ImplicitDefs[0], RC, RC);
  893. if (!InsertedCopy)
  894. ResultReg = 0;
  895. }
  896. return ResultReg;
  897. }
  898. /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
  899. /// with all but the least significant bit set to zero.
  900. unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op) {
  901. return FastEmit_ri(VT, VT, ISD::AND, Op, 1);
  902. }