Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2275 lines
97 KiB

  1. //===-- llvm/Target/TargetLowering.h - Target Lowering Info -----*- C++ -*-===//
  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 describes how to lower LLVM code to machine code. This has two
  11. // main components:
  12. //
  13. // 1. Which ValueTypes are natively supported by the target.
  14. // 2. Which operations are supported for supported ValueTypes.
  15. // 3. Cost thresholds for alternative implementations of certain operations.
  16. //
  17. // In addition it has a few other components, like information about FP
  18. // immediates.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_TARGET_TARGETLOWERING_H
  22. #define LLVM_TARGET_TARGETLOWERING_H
  23. #include "llvm/ADT/DenseMap.h"
  24. #include "llvm/CodeGen/DAGCombine.h"
  25. #include "llvm/CodeGen/RuntimeLibcalls.h"
  26. #include "llvm/CodeGen/SelectionDAGNodes.h"
  27. #include "llvm/IR/Attributes.h"
  28. #include "llvm/IR/CallingConv.h"
  29. #include "llvm/IR/InlineAsm.h"
  30. #include "llvm/Support/CallSite.h"
  31. #include "llvm/Support/DebugLoc.h"
  32. #include "llvm/Target/TargetCallingConv.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. #include <climits>
  35. #include <map>
  36. #include <vector>
  37. namespace llvm {
  38. class CallInst;
  39. class CCState;
  40. class FastISel;
  41. class FunctionLoweringInfo;
  42. class ImmutableCallSite;
  43. class IntrinsicInst;
  44. class MachineBasicBlock;
  45. class MachineFunction;
  46. class MachineInstr;
  47. class MachineJumpTableInfo;
  48. class MCContext;
  49. class MCExpr;
  50. template<typename T> class SmallVectorImpl;
  51. class DataLayout;
  52. class TargetRegisterClass;
  53. class TargetLibraryInfo;
  54. class TargetLoweringObjectFile;
  55. class Value;
  56. namespace Sched {
  57. enum Preference {
  58. None, // No preference
  59. Source, // Follow source order.
  60. RegPressure, // Scheduling for lowest register pressure.
  61. Hybrid, // Scheduling for both latency and register pressure.
  62. ILP, // Scheduling for ILP in low register pressure mode.
  63. VLIW // Scheduling for VLIW targets.
  64. };
  65. }
  66. /// TargetLoweringBase - This base class for TargetLowering contains the
  67. /// SelectionDAG-independent parts that can be used from the rest of CodeGen.
  68. class TargetLoweringBase {
  69. TargetLoweringBase(const TargetLoweringBase&) LLVM_DELETED_FUNCTION;
  70. void operator=(const TargetLoweringBase&) LLVM_DELETED_FUNCTION;
  71. public:
  72. /// LegalizeAction - This enum indicates whether operations are valid for a
  73. /// target, and if not, what action should be used to make them valid.
  74. enum LegalizeAction {
  75. Legal, // The target natively supports this operation.
  76. Promote, // This operation should be executed in a larger type.
  77. Expand, // Try to expand this to other ops, otherwise use a libcall.
  78. Custom // Use the LowerOperation hook to implement custom lowering.
  79. };
  80. /// LegalizeTypeAction - This enum indicates whether a types are legal for a
  81. /// target, and if not, what action should be used to make them valid.
  82. enum LegalizeTypeAction {
  83. TypeLegal, // The target natively supports this type.
  84. TypePromoteInteger, // Replace this integer with a larger one.
  85. TypeExpandInteger, // Split this integer into two of half the size.
  86. TypeSoftenFloat, // Convert this float to a same size integer type.
  87. TypeExpandFloat, // Split this float into two of half the size.
  88. TypeScalarizeVector, // Replace this one-element vector with its element.
  89. TypeSplitVector, // Split this vector into two of half the size.
  90. TypeWidenVector // This vector should be widened into a larger vector.
  91. };
  92. /// LegalizeKind holds the legalization kind that needs to happen to EVT
  93. /// in order to type-legalize it.
  94. typedef std::pair<LegalizeTypeAction, EVT> LegalizeKind;
  95. enum BooleanContent { // How the target represents true/false values.
  96. UndefinedBooleanContent, // Only bit 0 counts, the rest can hold garbage.
  97. ZeroOrOneBooleanContent, // All bits zero except for bit 0.
  98. ZeroOrNegativeOneBooleanContent // All bits equal to bit 0.
  99. };
  100. enum SelectSupportKind {
  101. ScalarValSelect, // The target supports scalar selects (ex: cmov).
  102. ScalarCondVectorVal, // The target supports selects with a scalar condition
  103. // and vector values (ex: cmov).
  104. VectorMaskSelect // The target supports vector selects with a vector
  105. // mask (ex: x86 blends).
  106. };
  107. static ISD::NodeType getExtendForContent(BooleanContent Content) {
  108. switch (Content) {
  109. case UndefinedBooleanContent:
  110. // Extend by adding rubbish bits.
  111. return ISD::ANY_EXTEND;
  112. case ZeroOrOneBooleanContent:
  113. // Extend by adding zero bits.
  114. return ISD::ZERO_EXTEND;
  115. case ZeroOrNegativeOneBooleanContent:
  116. // Extend by copying the sign bit.
  117. return ISD::SIGN_EXTEND;
  118. }
  119. llvm_unreachable("Invalid content kind");
  120. }
  121. /// NOTE: The constructor takes ownership of TLOF.
  122. explicit TargetLoweringBase(const TargetMachine &TM,
  123. const TargetLoweringObjectFile *TLOF);
  124. virtual ~TargetLoweringBase();
  125. protected:
  126. /// \brief Initialize all of the actions to default values.
  127. void initActions();
  128. public:
  129. const TargetMachine &getTargetMachine() const { return TM; }
  130. const DataLayout *getDataLayout() const { return TD; }
  131. const TargetLoweringObjectFile &getObjFileLowering() const { return TLOF; }
  132. bool isBigEndian() const { return !IsLittleEndian; }
  133. bool isLittleEndian() const { return IsLittleEndian; }
  134. // Return the pointer type for the given address space, defaults to
  135. // the pointer type from the data layout.
  136. // FIXME: The default needs to be removed once all the code is updated.
  137. virtual MVT getPointerTy(uint32_t AS = 0) const { return PointerTy; }
  138. virtual MVT getScalarShiftAmountTy(EVT LHSTy) const;
  139. EVT getShiftAmountTy(EVT LHSTy) const;
  140. /// isSelectExpensive - Return true if the select operation is expensive for
  141. /// this target.
  142. bool isSelectExpensive() const { return SelectIsExpensive; }
  143. virtual bool isSelectSupported(SelectSupportKind kind) const { return true; }
  144. /// shouldSplitVectorElementType - Return true if a vector of the given type
  145. /// should be split (TypeSplitVector) instead of promoted
  146. /// (TypePromoteInteger) during type legalization.
  147. virtual bool shouldSplitVectorElementType(EVT VT) const { return false; }
  148. /// isIntDivCheap() - Return true if integer divide is usually cheaper than
  149. /// a sequence of several shifts, adds, and multiplies for this target.
  150. bool isIntDivCheap() const { return IntDivIsCheap; }
  151. /// isSlowDivBypassed - Returns true if target has indicated at least one
  152. /// type should be bypassed.
  153. bool isSlowDivBypassed() const { return !BypassSlowDivWidths.empty(); }
  154. /// getBypassSlowDivTypes - Returns map of slow types for division or
  155. /// remainder with corresponding fast types
  156. const DenseMap<unsigned int, unsigned int> &getBypassSlowDivWidths() const {
  157. return BypassSlowDivWidths;
  158. }
  159. /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of
  160. /// srl/add/sra.
  161. bool isPow2DivCheap() const { return Pow2DivIsCheap; }
  162. /// isJumpExpensive() - Return true if Flow Control is an expensive operation
  163. /// that should be avoided.
  164. bool isJumpExpensive() const { return JumpIsExpensive; }
  165. /// isPredictableSelectExpensive - Return true if selects are only cheaper
  166. /// than branches if the branch is unlikely to be predicted right.
  167. bool isPredictableSelectExpensive() const {
  168. return PredictableSelectIsExpensive;
  169. }
  170. /// getSetCCResultType - Return the ValueType of the result of SETCC
  171. /// operations. Also used to obtain the target's preferred type for
  172. /// the condition operand of SELECT and BRCOND nodes. In the case of
  173. /// BRCOND the argument passed is MVT::Other since there are no other
  174. /// operands to get a type hint from.
  175. virtual EVT getSetCCResultType(EVT VT) const;
  176. /// getCmpLibcallReturnType - Return the ValueType for comparison
  177. /// libcalls. Comparions libcalls include floating point comparion calls,
  178. /// and Ordered/Unordered check calls on floating point numbers.
  179. virtual
  180. MVT::SimpleValueType getCmpLibcallReturnType() const;
  181. /// getBooleanContents - For targets without i1 registers, this gives the
  182. /// nature of the high-bits of boolean values held in types wider than i1.
  183. /// "Boolean values" are special true/false values produced by nodes like
  184. /// SETCC and consumed (as the condition) by nodes like SELECT and BRCOND.
  185. /// Not to be confused with general values promoted from i1.
  186. /// Some cpus distinguish between vectors of boolean and scalars; the isVec
  187. /// parameter selects between the two kinds. For example on X86 a scalar
  188. /// boolean should be zero extended from i1, while the elements of a vector
  189. /// of booleans should be sign extended from i1.
  190. BooleanContent getBooleanContents(bool isVec) const {
  191. return isVec ? BooleanVectorContents : BooleanContents;
  192. }
  193. /// getSchedulingPreference - Return target scheduling preference.
  194. Sched::Preference getSchedulingPreference() const {
  195. return SchedPreferenceInfo;
  196. }
  197. /// getSchedulingPreference - Some scheduler, e.g. hybrid, can switch to
  198. /// different scheduling heuristics for different nodes. This function returns
  199. /// the preference (or none) for the given node.
  200. virtual Sched::Preference getSchedulingPreference(SDNode *) const {
  201. return Sched::None;
  202. }
  203. /// getRegClassFor - Return the register class that should be used for the
  204. /// specified value type.
  205. virtual const TargetRegisterClass *getRegClassFor(MVT VT) const {
  206. const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
  207. assert(RC && "This value type is not natively supported!");
  208. return RC;
  209. }
  210. /// getRepRegClassFor - Return the 'representative' register class for the
  211. /// specified value type. The 'representative' register class is the largest
  212. /// legal super-reg register class for the register class of the value type.
  213. /// For example, on i386 the rep register class for i8, i16, and i32 are GR32;
  214. /// while the rep register class is GR64 on x86_64.
  215. virtual const TargetRegisterClass *getRepRegClassFor(MVT VT) const {
  216. const TargetRegisterClass *RC = RepRegClassForVT[VT.SimpleTy];
  217. return RC;
  218. }
  219. /// getRepRegClassCostFor - Return the cost of the 'representative' register
  220. /// class for the specified value type.
  221. virtual uint8_t getRepRegClassCostFor(MVT VT) const {
  222. return RepRegClassCostForVT[VT.SimpleTy];
  223. }
  224. /// isTypeLegal - Return true if the target has native support for the
  225. /// specified value type. This means that it has a register that directly
  226. /// holds it without promotions or expansions.
  227. bool isTypeLegal(EVT VT) const {
  228. assert(!VT.isSimple() ||
  229. (unsigned)VT.getSimpleVT().SimpleTy < array_lengthof(RegClassForVT));
  230. return VT.isSimple() && RegClassForVT[VT.getSimpleVT().SimpleTy] != 0;
  231. }
  232. class ValueTypeActionImpl {
  233. /// ValueTypeActions - For each value type, keep a LegalizeTypeAction enum
  234. /// that indicates how instruction selection should deal with the type.
  235. uint8_t ValueTypeActions[MVT::LAST_VALUETYPE];
  236. public:
  237. ValueTypeActionImpl() {
  238. std::fill(ValueTypeActions, array_endof(ValueTypeActions), 0);
  239. }
  240. LegalizeTypeAction getTypeAction(MVT VT) const {
  241. return (LegalizeTypeAction)ValueTypeActions[VT.SimpleTy];
  242. }
  243. void setTypeAction(MVT VT, LegalizeTypeAction Action) {
  244. unsigned I = VT.SimpleTy;
  245. ValueTypeActions[I] = Action;
  246. }
  247. };
  248. const ValueTypeActionImpl &getValueTypeActions() const {
  249. return ValueTypeActions;
  250. }
  251. /// getTypeAction - Return how we should legalize values of this type, either
  252. /// it is already legal (return 'Legal') or we need to promote it to a larger
  253. /// type (return 'Promote'), or we need to expand it into multiple registers
  254. /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
  255. LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const {
  256. return getTypeConversion(Context, VT).first;
  257. }
  258. LegalizeTypeAction getTypeAction(MVT VT) const {
  259. return ValueTypeActions.getTypeAction(VT);
  260. }
  261. /// getTypeToTransformTo - For types supported by the target, this is an
  262. /// identity function. For types that must be promoted to larger types, this
  263. /// returns the larger type to promote to. For integer types that are larger
  264. /// than the largest integer register, this contains one step in the expansion
  265. /// to get to the smaller register. For illegal floating point types, this
  266. /// returns the integer type to transform to.
  267. EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const {
  268. return getTypeConversion(Context, VT).second;
  269. }
  270. /// getTypeToExpandTo - For types supported by the target, this is an
  271. /// identity function. For types that must be expanded (i.e. integer types
  272. /// that are larger than the largest integer register or illegal floating
  273. /// point types), this returns the largest legal type it will be expanded to.
  274. EVT getTypeToExpandTo(LLVMContext &Context, EVT VT) const {
  275. assert(!VT.isVector());
  276. while (true) {
  277. switch (getTypeAction(Context, VT)) {
  278. case TypeLegal:
  279. return VT;
  280. case TypeExpandInteger:
  281. VT = getTypeToTransformTo(Context, VT);
  282. break;
  283. default:
  284. llvm_unreachable("Type is not legal nor is it to be expanded!");
  285. }
  286. }
  287. }
  288. /// getVectorTypeBreakdown - Vector types are broken down into some number of
  289. /// legal first class types. For example, EVT::v8f32 maps to 2 EVT::v4f32
  290. /// with Altivec or SSE1, or 8 promoted EVT::f64 values with the X86 FP stack.
  291. /// Similarly, EVT::v2i64 turns into 4 EVT::i32 values with both PPC and X86.
  292. ///
  293. /// This method returns the number of registers needed, and the VT for each
  294. /// register. It also returns the VT and quantity of the intermediate values
  295. /// before they are promoted/expanded.
  296. ///
  297. unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT,
  298. EVT &IntermediateVT,
  299. unsigned &NumIntermediates,
  300. MVT &RegisterVT) const;
  301. /// getTgtMemIntrinsic: Given an intrinsic, checks if on the target the
  302. /// intrinsic will need to map to a MemIntrinsicNode (touches memory). If
  303. /// this is the case, it returns true and store the intrinsic
  304. /// information into the IntrinsicInfo that was passed to the function.
  305. struct IntrinsicInfo {
  306. unsigned opc; // target opcode
  307. EVT memVT; // memory VT
  308. const Value* ptrVal; // value representing memory location
  309. int offset; // offset off of ptrVal
  310. unsigned align; // alignment
  311. bool vol; // is volatile?
  312. bool readMem; // reads memory?
  313. bool writeMem; // writes memory?
  314. };
  315. virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &,
  316. unsigned /*Intrinsic*/) const {
  317. return false;
  318. }
  319. /// isFPImmLegal - Returns true if the target can instruction select the
  320. /// specified FP immediate natively. If false, the legalizer will materialize
  321. /// the FP immediate as a load from a constant pool.
  322. virtual bool isFPImmLegal(const APFloat &/*Imm*/, EVT /*VT*/) const {
  323. return false;
  324. }
  325. /// isShuffleMaskLegal - Targets can use this to indicate that they only
  326. /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
  327. /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
  328. /// are assumed to be legal.
  329. virtual bool isShuffleMaskLegal(const SmallVectorImpl<int> &/*Mask*/,
  330. EVT /*VT*/) const {
  331. return true;
  332. }
  333. /// canOpTrap - Returns true if the operation can trap for the value type.
  334. /// VT must be a legal type. By default, we optimistically assume most
  335. /// operations don't trap except for divide and remainder.
  336. virtual bool canOpTrap(unsigned Op, EVT VT) const;
  337. /// isVectorClearMaskLegal - Similar to isShuffleMaskLegal. This is
  338. /// used by Targets can use this to indicate if there is a suitable
  339. /// VECTOR_SHUFFLE that can be used to replace a VAND with a constant
  340. /// pool entry.
  341. virtual bool isVectorClearMaskLegal(const SmallVectorImpl<int> &/*Mask*/,
  342. EVT /*VT*/) const {
  343. return false;
  344. }
  345. /// getOperationAction - Return how this operation should be treated: either
  346. /// it is legal, needs to be promoted to a larger size, needs to be
  347. /// expanded to some other code sequence, or the target has a custom expander
  348. /// for it.
  349. LegalizeAction getOperationAction(unsigned Op, EVT VT) const {
  350. if (VT.isExtended()) return Expand;
  351. // If a target-specific SDNode requires legalization, require the target
  352. // to provide custom legalization for it.
  353. if (Op > array_lengthof(OpActions[0])) return Custom;
  354. unsigned I = (unsigned) VT.getSimpleVT().SimpleTy;
  355. return (LegalizeAction)OpActions[I][Op];
  356. }
  357. /// isOperationLegalOrCustom - Return true if the specified operation is
  358. /// legal on this target or can be made legal with custom lowering. This
  359. /// is used to help guide high-level lowering decisions.
  360. bool isOperationLegalOrCustom(unsigned Op, EVT VT) const {
  361. return (VT == MVT::Other || isTypeLegal(VT)) &&
  362. (getOperationAction(Op, VT) == Legal ||
  363. getOperationAction(Op, VT) == Custom);
  364. }
  365. /// isOperationLegalOrPromote - Return true if the specified operation is
  366. /// legal on this target or can be made legal using promotion. This
  367. /// is used to help guide high-level lowering decisions.
  368. bool isOperationLegalOrPromote(unsigned Op, EVT VT) const {
  369. return (VT == MVT::Other || isTypeLegal(VT)) &&
  370. (getOperationAction(Op, VT) == Legal ||
  371. getOperationAction(Op, VT) == Promote);
  372. }
  373. /// isOperationExpand - Return true if the specified operation is illegal on
  374. /// this target or unlikely to be made legal with custom lowering. This is
  375. /// used to help guide high-level lowering decisions.
  376. bool isOperationExpand(unsigned Op, EVT VT) const {
  377. return (!isTypeLegal(VT) || getOperationAction(Op, VT) == Expand);
  378. }
  379. /// isOperationLegal - Return true if the specified operation is legal on this
  380. /// target.
  381. bool isOperationLegal(unsigned Op, EVT VT) const {
  382. return (VT == MVT::Other || isTypeLegal(VT)) &&
  383. getOperationAction(Op, VT) == Legal;
  384. }
  385. /// getLoadExtAction - Return how this load with extension should be treated:
  386. /// either it is legal, needs to be promoted to a larger size, needs to be
  387. /// expanded to some other code sequence, or the target has a custom expander
  388. /// for it.
  389. LegalizeAction getLoadExtAction(unsigned ExtType, MVT VT) const {
  390. assert(ExtType < ISD::LAST_LOADEXT_TYPE && VT < MVT::LAST_VALUETYPE &&
  391. "Table isn't big enough!");
  392. return (LegalizeAction)LoadExtActions[VT.SimpleTy][ExtType];
  393. }
  394. /// isLoadExtLegal - Return true if the specified load with extension is legal
  395. /// on this target.
  396. bool isLoadExtLegal(unsigned ExtType, EVT VT) const {
  397. return VT.isSimple() &&
  398. getLoadExtAction(ExtType, VT.getSimpleVT()) == Legal;
  399. }
  400. /// getTruncStoreAction - Return how this store with truncation should be
  401. /// treated: either it is legal, needs to be promoted to a larger size, needs
  402. /// to be expanded to some other code sequence, or the target has a custom
  403. /// expander for it.
  404. LegalizeAction getTruncStoreAction(MVT ValVT, MVT MemVT) const {
  405. assert(ValVT < MVT::LAST_VALUETYPE && MemVT < MVT::LAST_VALUETYPE &&
  406. "Table isn't big enough!");
  407. return (LegalizeAction)TruncStoreActions[ValVT.SimpleTy]
  408. [MemVT.SimpleTy];
  409. }
  410. /// isTruncStoreLegal - Return true if the specified store with truncation is
  411. /// legal on this target.
  412. bool isTruncStoreLegal(EVT ValVT, EVT MemVT) const {
  413. return isTypeLegal(ValVT) && MemVT.isSimple() &&
  414. getTruncStoreAction(ValVT.getSimpleVT(), MemVT.getSimpleVT()) == Legal;
  415. }
  416. /// getIndexedLoadAction - Return how the indexed load should be treated:
  417. /// either it is legal, needs to be promoted to a larger size, needs to be
  418. /// expanded to some other code sequence, or the target has a custom expander
  419. /// for it.
  420. LegalizeAction
  421. getIndexedLoadAction(unsigned IdxMode, MVT VT) const {
  422. assert(IdxMode < ISD::LAST_INDEXED_MODE && VT < MVT::LAST_VALUETYPE &&
  423. "Table isn't big enough!");
  424. unsigned Ty = (unsigned)VT.SimpleTy;
  425. return (LegalizeAction)((IndexedModeActions[Ty][IdxMode] & 0xf0) >> 4);
  426. }
  427. /// isIndexedLoadLegal - Return true if the specified indexed load is legal
  428. /// on this target.
  429. bool isIndexedLoadLegal(unsigned IdxMode, EVT VT) const {
  430. return VT.isSimple() &&
  431. (getIndexedLoadAction(IdxMode, VT.getSimpleVT()) == Legal ||
  432. getIndexedLoadAction(IdxMode, VT.getSimpleVT()) == Custom);
  433. }
  434. /// getIndexedStoreAction - Return how the indexed store should be treated:
  435. /// either it is legal, needs to be promoted to a larger size, needs to be
  436. /// expanded to some other code sequence, or the target has a custom expander
  437. /// for it.
  438. LegalizeAction
  439. getIndexedStoreAction(unsigned IdxMode, MVT VT) const {
  440. assert(IdxMode < ISD::LAST_INDEXED_MODE && VT < MVT::LAST_VALUETYPE &&
  441. "Table isn't big enough!");
  442. unsigned Ty = (unsigned)VT.SimpleTy;
  443. return (LegalizeAction)(IndexedModeActions[Ty][IdxMode] & 0x0f);
  444. }
  445. /// isIndexedStoreLegal - Return true if the specified indexed load is legal
  446. /// on this target.
  447. bool isIndexedStoreLegal(unsigned IdxMode, EVT VT) const {
  448. return VT.isSimple() &&
  449. (getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Legal ||
  450. getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Custom);
  451. }
  452. /// getCondCodeAction - Return how the condition code should be treated:
  453. /// either it is legal, needs to be expanded to some other code sequence,
  454. /// or the target has a custom expander for it.
  455. LegalizeAction
  456. getCondCodeAction(ISD::CondCode CC, MVT VT) const {
  457. assert((unsigned)CC < array_lengthof(CondCodeActions) &&
  458. (unsigned)VT.SimpleTy < sizeof(CondCodeActions[0])*4 &&
  459. "Table isn't big enough!");
  460. /// The lower 5 bits of the SimpleTy index into Nth 2bit set from the 64bit
  461. /// value and the upper 27 bits index into the second dimension of the
  462. /// array to select what 64bit value to use.
  463. LegalizeAction Action = (LegalizeAction)
  464. ((CondCodeActions[CC][VT.SimpleTy >> 5] >> (2*(VT.SimpleTy & 0x1F))) & 3);
  465. assert(Action != Promote && "Can't promote condition code!");
  466. return Action;
  467. }
  468. /// isCondCodeLegal - Return true if the specified condition code is legal
  469. /// on this target.
  470. bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const {
  471. return
  472. getCondCodeAction(CC, VT) == Legal ||
  473. getCondCodeAction(CC, VT) == Custom;
  474. }
  475. /// getTypeToPromoteTo - If the action for this operation is to promote, this
  476. /// method returns the ValueType to promote to.
  477. MVT getTypeToPromoteTo(unsigned Op, MVT VT) const {
  478. assert(getOperationAction(Op, VT) == Promote &&
  479. "This operation isn't promoted!");
  480. // See if this has an explicit type specified.
  481. std::map<std::pair<unsigned, MVT::SimpleValueType>,
  482. MVT::SimpleValueType>::const_iterator PTTI =
  483. PromoteToType.find(std::make_pair(Op, VT.SimpleTy));
  484. if (PTTI != PromoteToType.end()) return PTTI->second;
  485. assert((VT.isInteger() || VT.isFloatingPoint()) &&
  486. "Cannot autopromote this type, add it with AddPromotedToType.");
  487. MVT NVT = VT;
  488. do {
  489. NVT = (MVT::SimpleValueType)(NVT.SimpleTy+1);
  490. assert(NVT.isInteger() == VT.isInteger() && NVT != MVT::isVoid &&
  491. "Didn't find type to promote to!");
  492. } while (!isTypeLegal(NVT) ||
  493. getOperationAction(Op, NVT) == Promote);
  494. return NVT;
  495. }
  496. /// getValueType - Return the EVT corresponding to this LLVM type.
  497. /// This is fixed by the LLVM operations except for the pointer size. If
  498. /// AllowUnknown is true, this will return MVT::Other for types with no EVT
  499. /// counterpart (e.g. structs), otherwise it will assert.
  500. EVT getValueType(Type *Ty, bool AllowUnknown = false) const {
  501. // Lower scalar pointers to native pointer types.
  502. if (Ty->isPointerTy()) return PointerTy;
  503. if (Ty->isVectorTy()) {
  504. VectorType *VTy = cast<VectorType>(Ty);
  505. Type *Elm = VTy->getElementType();
  506. // Lower vectors of pointers to native pointer types.
  507. if (Elm->isPointerTy())
  508. Elm = EVT(PointerTy).getTypeForEVT(Ty->getContext());
  509. return EVT::getVectorVT(Ty->getContext(), EVT::getEVT(Elm, false),
  510. VTy->getNumElements());
  511. }
  512. return EVT::getEVT(Ty, AllowUnknown);
  513. }
  514. /// Return the MVT corresponding to this LLVM type. See getValueType.
  515. MVT getSimpleValueType(Type *Ty, bool AllowUnknown = false) const {
  516. return getValueType(Ty, AllowUnknown).getSimpleVT();
  517. }
  518. /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
  519. /// function arguments in the caller parameter area. This is the actual
  520. /// alignment, not its logarithm.
  521. virtual unsigned getByValTypeAlignment(Type *Ty) const;
  522. /// getRegisterType - Return the type of registers that this ValueType will
  523. /// eventually require.
  524. MVT getRegisterType(MVT VT) const {
  525. assert((unsigned)VT.SimpleTy < array_lengthof(RegisterTypeForVT));
  526. return RegisterTypeForVT[VT.SimpleTy];
  527. }
  528. /// getRegisterType - Return the type of registers that this ValueType will
  529. /// eventually require.
  530. MVT getRegisterType(LLVMContext &Context, EVT VT) const {
  531. if (VT.isSimple()) {
  532. assert((unsigned)VT.getSimpleVT().SimpleTy <
  533. array_lengthof(RegisterTypeForVT));
  534. return RegisterTypeForVT[VT.getSimpleVT().SimpleTy];
  535. }
  536. if (VT.isVector()) {
  537. EVT VT1;
  538. MVT RegisterVT;
  539. unsigned NumIntermediates;
  540. (void)getVectorTypeBreakdown(Context, VT, VT1,
  541. NumIntermediates, RegisterVT);
  542. return RegisterVT;
  543. }
  544. if (VT.isInteger()) {
  545. return getRegisterType(Context, getTypeToTransformTo(Context, VT));
  546. }
  547. llvm_unreachable("Unsupported extended type!");
  548. }
  549. /// getNumRegisters - Return the number of registers that this ValueType will
  550. /// eventually require. This is one for any types promoted to live in larger
  551. /// registers, but may be more than one for types (like i64) that are split
  552. /// into pieces. For types like i140, which are first promoted then expanded,
  553. /// it is the number of registers needed to hold all the bits of the original
  554. /// type. For an i140 on a 32 bit machine this means 5 registers.
  555. unsigned getNumRegisters(LLVMContext &Context, EVT VT) const {
  556. if (VT.isSimple()) {
  557. assert((unsigned)VT.getSimpleVT().SimpleTy <
  558. array_lengthof(NumRegistersForVT));
  559. return NumRegistersForVT[VT.getSimpleVT().SimpleTy];
  560. }
  561. if (VT.isVector()) {
  562. EVT VT1;
  563. MVT VT2;
  564. unsigned NumIntermediates;
  565. return getVectorTypeBreakdown(Context, VT, VT1, NumIntermediates, VT2);
  566. }
  567. if (VT.isInteger()) {
  568. unsigned BitWidth = VT.getSizeInBits();
  569. unsigned RegWidth = getRegisterType(Context, VT).getSizeInBits();
  570. return (BitWidth + RegWidth - 1) / RegWidth;
  571. }
  572. llvm_unreachable("Unsupported extended type!");
  573. }
  574. /// ShouldShrinkFPConstant - If true, then instruction selection should
  575. /// seek to shrink the FP constant of the specified type to a smaller type
  576. /// in order to save space and / or reduce runtime.
  577. virtual bool ShouldShrinkFPConstant(EVT) const { return true; }
  578. /// hasTargetDAGCombine - If true, the target has custom DAG combine
  579. /// transformations that it can perform for the specified node.
  580. bool hasTargetDAGCombine(ISD::NodeType NT) const {
  581. assert(unsigned(NT >> 3) < array_lengthof(TargetDAGCombineArray));
  582. return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7));
  583. }
  584. /// This function returns the maximum number of store operations permitted
  585. /// to replace a call to llvm.memset. The value is set by the target at the
  586. /// performance threshold for such a replacement. If OptSize is true,
  587. /// return the limit for functions that have OptSize attribute.
  588. /// @brief Get maximum # of store operations permitted for llvm.memset
  589. unsigned getMaxStoresPerMemset(bool OptSize) const {
  590. return OptSize ? MaxStoresPerMemsetOptSize : MaxStoresPerMemset;
  591. }
  592. /// This function returns the maximum number of store operations permitted
  593. /// to replace a call to llvm.memcpy. The value is set by the target at the
  594. /// performance threshold for such a replacement. If OptSize is true,
  595. /// return the limit for functions that have OptSize attribute.
  596. /// @brief Get maximum # of store operations permitted for llvm.memcpy
  597. unsigned getMaxStoresPerMemcpy(bool OptSize) const {
  598. return OptSize ? MaxStoresPerMemcpyOptSize : MaxStoresPerMemcpy;
  599. }
  600. /// This function returns the maximum number of store operations permitted
  601. /// to replace a call to llvm.memmove. The value is set by the target at the
  602. /// performance threshold for such a replacement. If OptSize is true,
  603. /// return the limit for functions that have OptSize attribute.
  604. /// @brief Get maximum # of store operations permitted for llvm.memmove
  605. unsigned getMaxStoresPerMemmove(bool OptSize) const {
  606. return OptSize ? MaxStoresPerMemmoveOptSize : MaxStoresPerMemmove;
  607. }
  608. /// This function returns true if the target allows unaligned memory accesses.
  609. /// of the specified type. If true, it also returns whether the unaligned
  610. /// memory access is "fast" in the second argument by reference. This is used,
  611. /// for example, in situations where an array copy/move/set is converted to a
  612. /// sequence of store operations. It's use helps to ensure that such
  613. /// replacements don't generate code that causes an alignment error (trap) on
  614. /// the target machine.
  615. /// @brief Determine if the target supports unaligned memory accesses.
  616. virtual bool allowsUnalignedMemoryAccesses(EVT, bool *Fast = 0) const {
  617. return false;
  618. }
  619. /// getOptimalMemOpType - Returns the target specific optimal type for load
  620. /// and store operations as a result of memset, memcpy, and memmove
  621. /// lowering. If DstAlign is zero that means it's safe to destination
  622. /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
  623. /// means there isn't a need to check it against alignment requirement,
  624. /// probably because the source does not need to be loaded. If 'IsMemset' is
  625. /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
  626. /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
  627. /// source is constant so it does not need to be loaded.
  628. /// It returns EVT::Other if the type should be determined using generic
  629. /// target-independent logic.
  630. virtual EVT getOptimalMemOpType(uint64_t /*Size*/,
  631. unsigned /*DstAlign*/, unsigned /*SrcAlign*/,
  632. bool /*IsMemset*/,
  633. bool /*ZeroMemset*/,
  634. bool /*MemcpyStrSrc*/,
  635. MachineFunction &/*MF*/) const {
  636. return MVT::Other;
  637. }
  638. /// isSafeMemOpType - Returns true if it's safe to use load / store of the
  639. /// specified type to expand memcpy / memset inline. This is mostly true
  640. /// for all types except for some special cases. For example, on X86
  641. /// targets without SSE2 f64 load / store are done with fldl / fstpl which
  642. /// also does type conversion. Note the specified type doesn't have to be
  643. /// legal as the hook is used before type legalization.
  644. virtual bool isSafeMemOpType(MVT VT) const {
  645. return true;
  646. }
  647. /// usesUnderscoreSetJmp - Determine if we should use _setjmp or setjmp
  648. /// to implement llvm.setjmp.
  649. bool usesUnderscoreSetJmp() const {
  650. return UseUnderscoreSetJmp;
  651. }
  652. /// usesUnderscoreLongJmp - Determine if we should use _longjmp or longjmp
  653. /// to implement llvm.longjmp.
  654. bool usesUnderscoreLongJmp() const {
  655. return UseUnderscoreLongJmp;
  656. }
  657. /// supportJumpTables - return whether the target can generate code for
  658. /// jump tables.
  659. bool supportJumpTables() const {
  660. return SupportJumpTables;
  661. }
  662. /// getMinimumJumpTableEntries - return integer threshold on number of
  663. /// blocks to use jump tables rather than if sequence.
  664. int getMinimumJumpTableEntries() const {
  665. return MinimumJumpTableEntries;
  666. }
  667. /// getStackPointerRegisterToSaveRestore - If a physical register, this
  668. /// specifies the register that llvm.savestack/llvm.restorestack should save
  669. /// and restore.
  670. unsigned getStackPointerRegisterToSaveRestore() const {
  671. return StackPointerRegisterToSaveRestore;
  672. }
  673. /// getExceptionPointerRegister - If a physical register, this returns
  674. /// the register that receives the exception address on entry to a landing
  675. /// pad.
  676. unsigned getExceptionPointerRegister() const {
  677. return ExceptionPointerRegister;
  678. }
  679. /// getExceptionSelectorRegister - If a physical register, this returns
  680. /// the register that receives the exception typeid on entry to a landing
  681. /// pad.
  682. unsigned getExceptionSelectorRegister() const {
  683. return ExceptionSelectorRegister;
  684. }
  685. /// getJumpBufSize - returns the target's jmp_buf size in bytes (if never
  686. /// set, the default is 200)
  687. unsigned getJumpBufSize() const {
  688. return JumpBufSize;
  689. }
  690. /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
  691. /// (if never set, the default is 0)
  692. unsigned getJumpBufAlignment() const {
  693. return JumpBufAlignment;
  694. }
  695. /// getMinStackArgumentAlignment - return the minimum stack alignment of an
  696. /// argument.
  697. unsigned getMinStackArgumentAlignment() const {
  698. return MinStackArgumentAlignment;
  699. }
  700. /// getMinFunctionAlignment - return the minimum function alignment.
  701. ///
  702. unsigned getMinFunctionAlignment() const {
  703. return MinFunctionAlignment;
  704. }
  705. /// getPrefFunctionAlignment - return the preferred function alignment.
  706. ///
  707. unsigned getPrefFunctionAlignment() const {
  708. return PrefFunctionAlignment;
  709. }
  710. /// getPrefLoopAlignment - return the preferred loop alignment.
  711. ///
  712. unsigned getPrefLoopAlignment() const {
  713. return PrefLoopAlignment;
  714. }
  715. /// getInsertFencesFor - return whether the DAG builder should automatically
  716. /// insert fences and reduce ordering for atomics.
  717. ///
  718. bool getInsertFencesForAtomic() const {
  719. return InsertFencesForAtomic;
  720. }
  721. /// getStackCookieLocation - Return true if the target stores stack
  722. /// protector cookies at a fixed offset in some non-standard address
  723. /// space, and populates the address space and offset as
  724. /// appropriate.
  725. virtual bool getStackCookieLocation(unsigned &/*AddressSpace*/,
  726. unsigned &/*Offset*/) const {
  727. return false;
  728. }
  729. /// getMaximalGlobalOffset - Returns the maximal possible offset which can be
  730. /// used for loads / stores from the global.
  731. virtual unsigned getMaximalGlobalOffset() const {
  732. return 0;
  733. }
  734. //===--------------------------------------------------------------------===//
  735. /// \name Helpers for TargetTransformInfo implementations
  736. /// @{
  737. /// Get the ISD node that corresponds to the Instruction class opcode.
  738. int InstructionOpcodeToISD(unsigned Opcode) const;
  739. /// Estimate the cost of type-legalization and the legalized type.
  740. std::pair<unsigned, MVT> getTypeLegalizationCost(Type *Ty) const;
  741. /// @}
  742. //===--------------------------------------------------------------------===//
  743. // TargetLowering Configuration Methods - These methods should be invoked by
  744. // the derived class constructor to configure this object for the target.
  745. //
  746. /// \brief Reset the operation actions based on target options.
  747. virtual void resetOperationActions() {}
  748. protected:
  749. /// setBooleanContents - Specify how the target extends the result of a
  750. /// boolean value from i1 to a wider type. See getBooleanContents.
  751. void setBooleanContents(BooleanContent Ty) { BooleanContents = Ty; }
  752. /// setBooleanVectorContents - Specify how the target extends the result
  753. /// of a vector boolean value from a vector of i1 to a wider type. See
  754. /// getBooleanContents.
  755. void setBooleanVectorContents(BooleanContent Ty) {
  756. BooleanVectorContents = Ty;
  757. }
  758. /// setSchedulingPreference - Specify the target scheduling preference.
  759. void setSchedulingPreference(Sched::Preference Pref) {
  760. SchedPreferenceInfo = Pref;
  761. }
  762. /// setUseUnderscoreSetJmp - Indicate whether this target prefers to
  763. /// use _setjmp to implement llvm.setjmp or the non _ version.
  764. /// Defaults to false.
  765. void setUseUnderscoreSetJmp(bool Val) {
  766. UseUnderscoreSetJmp = Val;
  767. }
  768. /// setUseUnderscoreLongJmp - Indicate whether this target prefers to
  769. /// use _longjmp to implement llvm.longjmp or the non _ version.
  770. /// Defaults to false.
  771. void setUseUnderscoreLongJmp(bool Val) {
  772. UseUnderscoreLongJmp = Val;
  773. }
  774. /// setSupportJumpTables - Indicate whether the target can generate code for
  775. /// jump tables.
  776. void setSupportJumpTables(bool Val) {
  777. SupportJumpTables = Val;
  778. }
  779. /// setMinimumJumpTableEntries - Indicate the number of blocks to generate
  780. /// jump tables rather than if sequence.
  781. void setMinimumJumpTableEntries(int Val) {
  782. MinimumJumpTableEntries = Val;
  783. }
  784. /// setStackPointerRegisterToSaveRestore - If set to a physical register, this
  785. /// specifies the register that llvm.savestack/llvm.restorestack should save
  786. /// and restore.
  787. void setStackPointerRegisterToSaveRestore(unsigned R) {
  788. StackPointerRegisterToSaveRestore = R;
  789. }
  790. /// setExceptionPointerRegister - If set to a physical register, this sets
  791. /// the register that receives the exception address on entry to a landing
  792. /// pad.
  793. void setExceptionPointerRegister(unsigned R) {
  794. ExceptionPointerRegister = R;
  795. }
  796. /// setExceptionSelectorRegister - If set to a physical register, this sets
  797. /// the register that receives the exception typeid on entry to a landing
  798. /// pad.
  799. void setExceptionSelectorRegister(unsigned R) {
  800. ExceptionSelectorRegister = R;
  801. }
  802. /// SelectIsExpensive - Tells the code generator not to expand operations
  803. /// into sequences that use the select operations if possible.
  804. void setSelectIsExpensive(bool isExpensive = true) {
  805. SelectIsExpensive = isExpensive;
  806. }
  807. /// JumpIsExpensive - Tells the code generator not to expand sequence of
  808. /// operations into a separate sequences that increases the amount of
  809. /// flow control.
  810. void setJumpIsExpensive(bool isExpensive = true) {
  811. JumpIsExpensive = isExpensive;
  812. }
  813. /// setIntDivIsCheap - Tells the code generator that integer divide is
  814. /// expensive, and if possible, should be replaced by an alternate sequence
  815. /// of instructions not containing an integer divide.
  816. void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; }
  817. /// addBypassSlowDiv - Tells the code generator which bitwidths to bypass.
  818. void addBypassSlowDiv(unsigned int SlowBitWidth, unsigned int FastBitWidth) {
  819. BypassSlowDivWidths[SlowBitWidth] = FastBitWidth;
  820. }
  821. /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate
  822. /// srl/add/sra for a signed divide by power of two, and let the target handle
  823. /// it.
  824. void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; }
  825. /// addRegisterClass - Add the specified register class as an available
  826. /// regclass for the specified value type. This indicates the selector can
  827. /// handle values of that class natively.
  828. void addRegisterClass(MVT VT, const TargetRegisterClass *RC) {
  829. assert((unsigned)VT.SimpleTy < array_lengthof(RegClassForVT));
  830. AvailableRegClasses.push_back(std::make_pair(VT, RC));
  831. RegClassForVT[VT.SimpleTy] = RC;
  832. }
  833. /// clearRegisterClasses - Remove all register classes.
  834. void clearRegisterClasses() {
  835. memset(RegClassForVT, 0,MVT::LAST_VALUETYPE * sizeof(TargetRegisterClass*));
  836. AvailableRegClasses.clear();
  837. }
  838. /// \brief Remove all operation actions.
  839. void clearOperationActions() {
  840. }
  841. /// findRepresentativeClass - Return the largest legal super-reg register class
  842. /// of the register class for the specified type and its associated "cost".
  843. virtual std::pair<const TargetRegisterClass*, uint8_t>
  844. findRepresentativeClass(MVT VT) const;
  845. /// computeRegisterProperties - Once all of the register classes are added,
  846. /// this allows us to compute derived properties we expose.
  847. void computeRegisterProperties();
  848. /// setOperationAction - Indicate that the specified operation does not work
  849. /// with the specified type and indicate what to do about it.
  850. void setOperationAction(unsigned Op, MVT VT,
  851. LegalizeAction Action) {
  852. assert(Op < array_lengthof(OpActions[0]) && "Table isn't big enough!");
  853. OpActions[(unsigned)VT.SimpleTy][Op] = (uint8_t)Action;
  854. }
  855. /// setLoadExtAction - Indicate that the specified load with extension does
  856. /// not work with the specified type and indicate what to do about it.
  857. void setLoadExtAction(unsigned ExtType, MVT VT,
  858. LegalizeAction Action) {
  859. assert(ExtType < ISD::LAST_LOADEXT_TYPE && VT < MVT::LAST_VALUETYPE &&
  860. "Table isn't big enough!");
  861. LoadExtActions[VT.SimpleTy][ExtType] = (uint8_t)Action;
  862. }
  863. /// setTruncStoreAction - Indicate that the specified truncating store does
  864. /// not work with the specified type and indicate what to do about it.
  865. void setTruncStoreAction(MVT ValVT, MVT MemVT,
  866. LegalizeAction Action) {
  867. assert(ValVT < MVT::LAST_VALUETYPE && MemVT < MVT::LAST_VALUETYPE &&
  868. "Table isn't big enough!");
  869. TruncStoreActions[ValVT.SimpleTy][MemVT.SimpleTy] = (uint8_t)Action;
  870. }
  871. /// setIndexedLoadAction - Indicate that the specified indexed load does or
  872. /// does not work with the specified type and indicate what to do abort
  873. /// it. NOTE: All indexed mode loads are initialized to Expand in
  874. /// TargetLowering.cpp
  875. void setIndexedLoadAction(unsigned IdxMode, MVT VT,
  876. LegalizeAction Action) {
  877. assert(VT < MVT::LAST_VALUETYPE && IdxMode < ISD::LAST_INDEXED_MODE &&
  878. (unsigned)Action < 0xf && "Table isn't big enough!");
  879. // Load action are kept in the upper half.
  880. IndexedModeActions[(unsigned)VT.SimpleTy][IdxMode] &= ~0xf0;
  881. IndexedModeActions[(unsigned)VT.SimpleTy][IdxMode] |= ((uint8_t)Action) <<4;
  882. }
  883. /// setIndexedStoreAction - Indicate that the specified indexed store does or
  884. /// does not work with the specified type and indicate what to do about
  885. /// it. NOTE: All indexed mode stores are initialized to Expand in
  886. /// TargetLowering.cpp
  887. void setIndexedStoreAction(unsigned IdxMode, MVT VT,
  888. LegalizeAction Action) {
  889. assert(VT < MVT::LAST_VALUETYPE && IdxMode < ISD::LAST_INDEXED_MODE &&
  890. (unsigned)Action < 0xf && "Table isn't big enough!");
  891. // Store action are kept in the lower half.
  892. IndexedModeActions[(unsigned)VT.SimpleTy][IdxMode] &= ~0x0f;
  893. IndexedModeActions[(unsigned)VT.SimpleTy][IdxMode] |= ((uint8_t)Action);
  894. }
  895. /// setCondCodeAction - Indicate that the specified condition code is or isn't
  896. /// supported on the target and indicate what to do about it.
  897. void setCondCodeAction(ISD::CondCode CC, MVT VT,
  898. LegalizeAction Action) {
  899. assert(VT < MVT::LAST_VALUETYPE &&
  900. (unsigned)CC < array_lengthof(CondCodeActions) &&
  901. "Table isn't big enough!");
  902. /// The lower 5 bits of the SimpleTy index into Nth 2bit set from the 64bit
  903. /// value and the upper 27 bits index into the second dimension of the
  904. /// array to select what 64bit value to use.
  905. CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5]
  906. &= ~(uint64_t(3UL) << (VT.SimpleTy & 0x1F)*2);
  907. CondCodeActions[(unsigned)CC][VT.SimpleTy >> 5]
  908. |= (uint64_t)Action << (VT.SimpleTy & 0x1F)*2;
  909. }
  910. /// AddPromotedToType - If Opc/OrigVT is specified as being promoted, the
  911. /// promotion code defaults to trying a larger integer/fp until it can find
  912. /// one that works. If that default is insufficient, this method can be used
  913. /// by the target to override the default.
  914. void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT) {
  915. PromoteToType[std::make_pair(Opc, OrigVT.SimpleTy)] = DestVT.SimpleTy;
  916. }
  917. /// setTargetDAGCombine - Targets should invoke this method for each target
  918. /// independent node that they want to provide a custom DAG combiner for by
  919. /// implementing the PerformDAGCombine virtual method.
  920. void setTargetDAGCombine(ISD::NodeType NT) {
  921. assert(unsigned(NT >> 3) < array_lengthof(TargetDAGCombineArray));
  922. TargetDAGCombineArray[NT >> 3] |= 1 << (NT&7);
  923. }
  924. /// setJumpBufSize - Set the target's required jmp_buf buffer size (in
  925. /// bytes); default is 200
  926. void setJumpBufSize(unsigned Size) {
  927. JumpBufSize = Size;
  928. }
  929. /// setJumpBufAlignment - Set the target's required jmp_buf buffer
  930. /// alignment (in bytes); default is 0
  931. void setJumpBufAlignment(unsigned Align) {
  932. JumpBufAlignment = Align;
  933. }
  934. /// setMinFunctionAlignment - Set the target's minimum function alignment (in
  935. /// log2(bytes))
  936. void setMinFunctionAlignment(unsigned Align) {
  937. MinFunctionAlignment = Align;
  938. }
  939. /// setPrefFunctionAlignment - Set the target's preferred function alignment.
  940. /// This should be set if there is a performance benefit to
  941. /// higher-than-minimum alignment (in log2(bytes))
  942. void setPrefFunctionAlignment(unsigned Align) {
  943. PrefFunctionAlignment = Align;
  944. }
  945. /// setPrefLoopAlignment - Set the target's preferred loop alignment. Default
  946. /// alignment is zero, it means the target does not care about loop alignment.
  947. /// The alignment is specified in log2(bytes).
  948. void setPrefLoopAlignment(unsigned Align) {
  949. PrefLoopAlignment = Align;
  950. }
  951. /// setMinStackArgumentAlignment - Set the minimum stack alignment of an
  952. /// argument (in log2(bytes)).
  953. void setMinStackArgumentAlignment(unsigned Align) {
  954. MinStackArgumentAlignment = Align;
  955. }
  956. /// setInsertFencesForAtomic - Set if the DAG builder should
  957. /// automatically insert fences and reduce the order of atomic memory
  958. /// operations to Monotonic.
  959. void setInsertFencesForAtomic(bool fence) {
  960. InsertFencesForAtomic = fence;
  961. }
  962. public:
  963. //===--------------------------------------------------------------------===//
  964. // Addressing mode description hooks (used by LSR etc).
  965. //
  966. /// GetAddrModeArguments - CodeGenPrepare sinks address calculations into the
  967. /// same BB as Load/Store instructions reading the address. This allows as
  968. /// much computation as possible to be done in the address mode for that
  969. /// operand. This hook lets targets also pass back when this should be done
  970. /// on intrinsics which load/store.
  971. virtual bool GetAddrModeArguments(IntrinsicInst *I,
  972. SmallVectorImpl<Value*> &Ops,
  973. Type *&AccessTy) const {
  974. return false;
  975. }
  976. /// AddrMode - This represents an addressing mode of:
  977. /// BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
  978. /// If BaseGV is null, there is no BaseGV.
  979. /// If BaseOffs is zero, there is no base offset.
  980. /// If HasBaseReg is false, there is no base register.
  981. /// If Scale is zero, there is no ScaleReg. Scale of 1 indicates a reg with
  982. /// no scale.
  983. ///
  984. struct AddrMode {
  985. GlobalValue *BaseGV;
  986. int64_t BaseOffs;
  987. bool HasBaseReg;
  988. int64_t Scale;
  989. AddrMode() : BaseGV(0), BaseOffs(0), HasBaseReg(false), Scale(0) {}
  990. };
  991. /// isLegalAddressingMode - Return true if the addressing mode represented by
  992. /// AM is legal for this target, for a load/store of the specified type.
  993. /// The type may be VoidTy, in which case only return true if the addressing
  994. /// mode is legal for a load/store of any legal type.
  995. /// TODO: Handle pre/postinc as well.
  996. virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const;
  997. /// isLegalICmpImmediate - Return true if the specified immediate is legal
  998. /// icmp immediate, that is the target has icmp instructions which can compare
  999. /// a register against the immediate without having to materialize the
  1000. /// immediate into a register.
  1001. virtual bool isLegalICmpImmediate(int64_t) const {
  1002. return true;
  1003. }
  1004. /// isLegalAddImmediate - Return true if the specified immediate is legal
  1005. /// add immediate, that is the target has add instructions which can add
  1006. /// a register with the immediate without having to materialize the
  1007. /// immediate into a register.
  1008. virtual bool isLegalAddImmediate(int64_t) const {
  1009. return true;
  1010. }
  1011. /// isTruncateFree - Return true if it's free to truncate a value of
  1012. /// type Ty1 to type Ty2. e.g. On x86 it's free to truncate a i32 value in
  1013. /// register EAX to i16 by referencing its sub-register AX.
  1014. virtual bool isTruncateFree(Type * /*Ty1*/, Type * /*Ty2*/) const {
  1015. return false;
  1016. }
  1017. virtual bool isTruncateFree(EVT /*VT1*/, EVT /*VT2*/) const {
  1018. return false;
  1019. }
  1020. /// isZExtFree - Return true if any actual instruction that defines a
  1021. /// value of type Ty1 implicitly zero-extends the value to Ty2 in the result
  1022. /// register. This does not necessarily include registers defined in
  1023. /// unknown ways, such as incoming arguments, or copies from unknown
  1024. /// virtual registers. Also, if isTruncateFree(Ty2, Ty1) is true, this
  1025. /// does not necessarily apply to truncate instructions. e.g. on x86-64,
  1026. /// all instructions that define 32-bit values implicit zero-extend the
  1027. /// result out to 64 bits.
  1028. virtual bool isZExtFree(Type * /*Ty1*/, Type * /*Ty2*/) const {
  1029. return false;
  1030. }
  1031. virtual bool isZExtFree(EVT /*VT1*/, EVT /*VT2*/) const {
  1032. return false;
  1033. }
  1034. /// isZExtFree - Return true if zero-extending the specific node Val to type
  1035. /// VT2 is free (either because it's implicitly zero-extended such as ARM
  1036. /// ldrb / ldrh or because it's folded such as X86 zero-extending loads).
  1037. virtual bool isZExtFree(SDValue Val, EVT VT2) const {
  1038. return isZExtFree(Val.getValueType(), VT2);
  1039. }
  1040. /// isFNegFree - Return true if an fneg operation is free to the point where
  1041. /// it is never worthwhile to replace it with a bitwise operation.
  1042. virtual bool isFNegFree(EVT) const {
  1043. return false;
  1044. }
  1045. /// isFAbsFree - Return true if an fneg operation is free to the point where
  1046. /// it is never worthwhile to replace it with a bitwise operation.
  1047. virtual bool isFAbsFree(EVT) const {
  1048. return false;
  1049. }
  1050. /// isFMAFasterThanMulAndAdd - Return true if an FMA operation is faster than
  1051. /// a pair of mul and add instructions. fmuladd intrinsics will be expanded to
  1052. /// FMAs when this method returns true (and FMAs are legal), otherwise fmuladd
  1053. /// is expanded to mul + add.
  1054. virtual bool isFMAFasterThanMulAndAdd(EVT) const {
  1055. return false;
  1056. }
  1057. /// isNarrowingProfitable - Return true if it's profitable to narrow
  1058. /// operations of type VT1 to VT2. e.g. on x86, it's profitable to narrow
  1059. /// from i32 to i8 but not from i32 to i16.
  1060. virtual bool isNarrowingProfitable(EVT /*VT1*/, EVT /*VT2*/) const {
  1061. return false;
  1062. }
  1063. //===--------------------------------------------------------------------===//
  1064. // Runtime Library hooks
  1065. //
  1066. /// setLibcallName - Rename the default libcall routine name for the specified
  1067. /// libcall.
  1068. void setLibcallName(RTLIB::Libcall Call, const char *Name) {
  1069. LibcallRoutineNames[Call] = Name;
  1070. }
  1071. /// getLibcallName - Get the libcall routine name for the specified libcall.
  1072. ///
  1073. const char *getLibcallName(RTLIB::Libcall Call) const {
  1074. return LibcallRoutineNames[Call];
  1075. }
  1076. /// setCmpLibcallCC - Override the default CondCode to be used to test the
  1077. /// result of the comparison libcall against zero.
  1078. void setCmpLibcallCC(RTLIB::Libcall Call, ISD::CondCode CC) {
  1079. CmpLibcallCCs[Call] = CC;
  1080. }
  1081. /// getCmpLibcallCC - Get the CondCode that's to be used to test the result of
  1082. /// the comparison libcall against zero.
  1083. ISD::CondCode getCmpLibcallCC(RTLIB::Libcall Call) const {
  1084. return CmpLibcallCCs[Call];
  1085. }
  1086. /// setLibcallCallingConv - Set the CallingConv that should be used for the
  1087. /// specified libcall.
  1088. void setLibcallCallingConv(RTLIB::Libcall Call, CallingConv::ID CC) {
  1089. LibcallCallingConvs[Call] = CC;
  1090. }
  1091. /// getLibcallCallingConv - Get the CallingConv that should be used for the
  1092. /// specified libcall.
  1093. CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const {
  1094. return LibcallCallingConvs[Call];
  1095. }
  1096. private:
  1097. const TargetMachine &TM;
  1098. const DataLayout *TD;
  1099. const TargetLoweringObjectFile &TLOF;
  1100. /// PointerTy - The type to use for pointers for the default address space,
  1101. /// usually i32 or i64.
  1102. ///
  1103. MVT PointerTy;
  1104. /// IsLittleEndian - True if this is a little endian target.
  1105. ///
  1106. bool IsLittleEndian;
  1107. /// SelectIsExpensive - Tells the code generator not to expand operations
  1108. /// into sequences that use the select operations if possible.
  1109. bool SelectIsExpensive;
  1110. /// IntDivIsCheap - Tells the code generator not to expand integer divides by
  1111. /// constants into a sequence of muls, adds, and shifts. This is a hack until
  1112. /// a real cost model is in place. If we ever optimize for size, this will be
  1113. /// set to true unconditionally.
  1114. bool IntDivIsCheap;
  1115. /// BypassSlowDivMap - Tells the code generator to bypass slow divide or
  1116. /// remainder instructions. For example, BypassSlowDivWidths[32,8] tells the
  1117. /// code generator to bypass 32-bit integer div/rem with an 8-bit unsigned
  1118. /// integer div/rem when the operands are positive and less than 256.
  1119. DenseMap <unsigned int, unsigned int> BypassSlowDivWidths;
  1120. /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate
  1121. /// srl/add/sra for a signed divide by power of two, and let the target handle
  1122. /// it.
  1123. bool Pow2DivIsCheap;
  1124. /// JumpIsExpensive - Tells the code generator that it shouldn't generate
  1125. /// extra flow control instructions and should attempt to combine flow
  1126. /// control instructions via predication.
  1127. bool JumpIsExpensive;
  1128. /// UseUnderscoreSetJmp - This target prefers to use _setjmp to implement
  1129. /// llvm.setjmp. Defaults to false.
  1130. bool UseUnderscoreSetJmp;
  1131. /// UseUnderscoreLongJmp - This target prefers to use _longjmp to implement
  1132. /// llvm.longjmp. Defaults to false.
  1133. bool UseUnderscoreLongJmp;
  1134. /// SupportJumpTables - Whether the target can generate code for jumptables.
  1135. /// If it's not true, then each jumptable must be lowered into if-then-else's.
  1136. bool SupportJumpTables;
  1137. /// MinimumJumpTableEntries - Number of blocks threshold to use jump tables.
  1138. int MinimumJumpTableEntries;
  1139. /// BooleanContents - Information about the contents of the high-bits in
  1140. /// boolean values held in a type wider than i1. See getBooleanContents.
  1141. BooleanContent BooleanContents;
  1142. /// BooleanVectorContents - Information about the contents of the high-bits
  1143. /// in boolean vector values when the element type is wider than i1. See
  1144. /// getBooleanContents.
  1145. BooleanContent BooleanVectorContents;
  1146. /// SchedPreferenceInfo - The target scheduling preference: shortest possible
  1147. /// total cycles or lowest register usage.
  1148. Sched::Preference SchedPreferenceInfo;
  1149. /// JumpBufSize - The size, in bytes, of the target's jmp_buf buffers
  1150. unsigned JumpBufSize;
  1151. /// JumpBufAlignment - The alignment, in bytes, of the target's jmp_buf
  1152. /// buffers
  1153. unsigned JumpBufAlignment;
  1154. /// MinStackArgumentAlignment - The minimum alignment that any argument
  1155. /// on the stack needs to have.
  1156. ///
  1157. unsigned MinStackArgumentAlignment;
  1158. /// MinFunctionAlignment - The minimum function alignment (used when
  1159. /// optimizing for size, and to prevent explicitly provided alignment
  1160. /// from leading to incorrect code).
  1161. ///
  1162. unsigned MinFunctionAlignment;
  1163. /// PrefFunctionAlignment - The preferred function alignment (used when
  1164. /// alignment unspecified and optimizing for speed).
  1165. ///
  1166. unsigned PrefFunctionAlignment;
  1167. /// PrefLoopAlignment - The preferred loop alignment.
  1168. ///
  1169. unsigned PrefLoopAlignment;
  1170. /// InsertFencesForAtomic - Whether the DAG builder should automatically
  1171. /// insert fences and reduce ordering for atomics. (This will be set for
  1172. /// for most architectures with weak memory ordering.)
  1173. bool InsertFencesForAtomic;
  1174. /// StackPointerRegisterToSaveRestore - If set to a physical register, this
  1175. /// specifies the register that llvm.savestack/llvm.restorestack should save
  1176. /// and restore.
  1177. unsigned StackPointerRegisterToSaveRestore;
  1178. /// ExceptionPointerRegister - If set to a physical register, this specifies
  1179. /// the register that receives the exception address on entry to a landing
  1180. /// pad.
  1181. unsigned ExceptionPointerRegister;
  1182. /// ExceptionSelectorRegister - If set to a physical register, this specifies
  1183. /// the register that receives the exception typeid on entry to a landing
  1184. /// pad.
  1185. unsigned ExceptionSelectorRegister;
  1186. /// RegClassForVT - This indicates the default register class to use for
  1187. /// each ValueType the target supports natively.
  1188. const TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
  1189. unsigned char NumRegistersForVT[MVT::LAST_VALUETYPE];
  1190. MVT RegisterTypeForVT[MVT::LAST_VALUETYPE];
  1191. /// RepRegClassForVT - This indicates the "representative" register class to
  1192. /// use for each ValueType the target supports natively. This information is
  1193. /// used by the scheduler to track register pressure. By default, the
  1194. /// representative register class is the largest legal super-reg register
  1195. /// class of the register class of the specified type. e.g. On x86, i8, i16,
  1196. /// and i32's representative class would be GR32.
  1197. const TargetRegisterClass *RepRegClassForVT[MVT::LAST_VALUETYPE];
  1198. /// RepRegClassCostForVT - This indicates the "cost" of the "representative"
  1199. /// register class for each ValueType. The cost is used by the scheduler to
  1200. /// approximate register pressure.
  1201. uint8_t RepRegClassCostForVT[MVT::LAST_VALUETYPE];
  1202. /// TransformToType - For any value types we are promoting or expanding, this
  1203. /// contains the value type that we are changing to. For Expanded types, this
  1204. /// contains one step of the expand (e.g. i64 -> i32), even if there are
  1205. /// multiple steps required (e.g. i64 -> i16). For types natively supported
  1206. /// by the system, this holds the same type (e.g. i32 -> i32).
  1207. MVT TransformToType[MVT::LAST_VALUETYPE];
  1208. /// OpActions - For each operation and each value type, keep a LegalizeAction
  1209. /// that indicates how instruction selection should deal with the operation.
  1210. /// Most operations are Legal (aka, supported natively by the target), but
  1211. /// operations that are not should be described. Note that operations on
  1212. /// non-legal value types are not described here.
  1213. uint8_t OpActions[MVT::LAST_VALUETYPE][ISD::BUILTIN_OP_END];
  1214. /// LoadExtActions - For each load extension type and each value type,
  1215. /// keep a LegalizeAction that indicates how instruction selection should deal
  1216. /// with a load of a specific value type and extension type.
  1217. uint8_t LoadExtActions[MVT::LAST_VALUETYPE][ISD::LAST_LOADEXT_TYPE];
  1218. /// TruncStoreActions - For each value type pair keep a LegalizeAction that
  1219. /// indicates whether a truncating store of a specific value type and
  1220. /// truncating type is legal.
  1221. uint8_t TruncStoreActions[MVT::LAST_VALUETYPE][MVT::LAST_VALUETYPE];
  1222. /// IndexedModeActions - For each indexed mode and each value type,
  1223. /// keep a pair of LegalizeAction that indicates how instruction
  1224. /// selection should deal with the load / store. The first dimension is the
  1225. /// value_type for the reference. The second dimension represents the various
  1226. /// modes for load store.
  1227. uint8_t IndexedModeActions[MVT::LAST_VALUETYPE][ISD::LAST_INDEXED_MODE];
  1228. /// CondCodeActions - For each condition code (ISD::CondCode) keep a
  1229. /// LegalizeAction that indicates how instruction selection should
  1230. /// deal with the condition code.
  1231. /// Because each CC action takes up 2 bits, we need to have the array size
  1232. /// be large enough to fit all of the value types. This can be done by
  1233. /// dividing the MVT::LAST_VALUETYPE by 32 and adding one.
  1234. uint64_t CondCodeActions[ISD::SETCC_INVALID][(MVT::LAST_VALUETYPE / 32) + 1];
  1235. ValueTypeActionImpl ValueTypeActions;
  1236. public:
  1237. LegalizeKind
  1238. getTypeConversion(LLVMContext &Context, EVT VT) const {
  1239. // If this is a simple type, use the ComputeRegisterProp mechanism.
  1240. if (VT.isSimple()) {
  1241. MVT SVT = VT.getSimpleVT();
  1242. assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
  1243. MVT NVT = TransformToType[SVT.SimpleTy];
  1244. LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
  1245. assert(
  1246. (LA == TypeLegal ||
  1247. ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)
  1248. && "Promote may not follow Expand or Promote");
  1249. if (LA == TypeSplitVector)
  1250. return LegalizeKind(LA, EVT::getVectorVT(Context,
  1251. SVT.getVectorElementType(),
  1252. SVT.getVectorNumElements()/2));
  1253. if (LA == TypeScalarizeVector)
  1254. return LegalizeKind(LA, SVT.getVectorElementType());
  1255. return LegalizeKind(LA, NVT);
  1256. }
  1257. // Handle Extended Scalar Types.
  1258. if (!VT.isVector()) {
  1259. assert(VT.isInteger() && "Float types must be simple");
  1260. unsigned BitSize = VT.getSizeInBits();
  1261. // First promote to a power-of-two size, then expand if necessary.
  1262. if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
  1263. EVT NVT = VT.getRoundIntegerType(Context);
  1264. assert(NVT != VT && "Unable to round integer VT");
  1265. LegalizeKind NextStep = getTypeConversion(Context, NVT);
  1266. // Avoid multi-step promotion.
  1267. if (NextStep.first == TypePromoteInteger) return NextStep;
  1268. // Return rounded integer type.
  1269. return LegalizeKind(TypePromoteInteger, NVT);
  1270. }
  1271. return LegalizeKind(TypeExpandInteger,
  1272. EVT::getIntegerVT(Context, VT.getSizeInBits()/2));
  1273. }
  1274. // Handle vector types.
  1275. unsigned NumElts = VT.getVectorNumElements();
  1276. EVT EltVT = VT.getVectorElementType();
  1277. // Vectors with only one element are always scalarized.
  1278. if (NumElts == 1)
  1279. return LegalizeKind(TypeScalarizeVector, EltVT);
  1280. // Try to widen vector elements until a legal type is found.
  1281. if (EltVT.isInteger()) {
  1282. // Vectors with a number of elements that is not a power of two are always
  1283. // widened, for example <3 x float> -> <4 x float>.
  1284. if (!VT.isPow2VectorType()) {
  1285. NumElts = (unsigned)NextPowerOf2(NumElts);
  1286. EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  1287. return LegalizeKind(TypeWidenVector, NVT);
  1288. }
  1289. // Examine the element type.
  1290. LegalizeKind LK = getTypeConversion(Context, EltVT);
  1291. // If type is to be expanded, split the vector.
  1292. // <4 x i140> -> <2 x i140>
  1293. if (LK.first == TypeExpandInteger)
  1294. return LegalizeKind(TypeSplitVector,
  1295. EVT::getVectorVT(Context, EltVT, NumElts / 2));
  1296. // Promote the integer element types until a legal vector type is found
  1297. // or until the element integer type is too big. If a legal type was not
  1298. // found, fallback to the usual mechanism of widening/splitting the
  1299. // vector.
  1300. EVT OldEltVT = EltVT;
  1301. while (1) {
  1302. // Increase the bitwidth of the element to the next pow-of-two
  1303. // (which is greater than 8 bits).
  1304. EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits()
  1305. ).getRoundIntegerType(Context);
  1306. // Stop trying when getting a non-simple element type.
  1307. // Note that vector elements may be greater than legal vector element
  1308. // types. Example: X86 XMM registers hold 64bit element on 32bit systems.
  1309. if (!EltVT.isSimple()) break;
  1310. // Build a new vector type and check if it is legal.
  1311. MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  1312. // Found a legal promoted vector type.
  1313. if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
  1314. return LegalizeKind(TypePromoteInteger,
  1315. EVT::getVectorVT(Context, EltVT, NumElts));
  1316. }
  1317. // Reset the type to the unexpanded type if we did not find a legal vector
  1318. // type with a promoted vector element type.
  1319. EltVT = OldEltVT;
  1320. }
  1321. // Try to widen the vector until a legal type is found.
  1322. // If there is no wider legal type, split the vector.
  1323. while (1) {
  1324. // Round up to the next power of 2.
  1325. NumElts = (unsigned)NextPowerOf2(NumElts);
  1326. // If there is no simple vector type with this many elements then there
  1327. // cannot be a larger legal vector type. Note that this assumes that
  1328. // there are no skipped intermediate vector types in the simple types.
  1329. if (!EltVT.isSimple()) break;
  1330. MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
  1331. if (LargerVector == MVT()) break;
  1332. // If this type is legal then widen the vector.
  1333. if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
  1334. return LegalizeKind(TypeWidenVector, LargerVector);
  1335. }
  1336. // Widen odd vectors to next power of two.
  1337. if (!VT.isPow2VectorType()) {
  1338. EVT NVT = VT.getPow2VectorType(Context);
  1339. return LegalizeKind(TypeWidenVector, NVT);
  1340. }
  1341. // Vectors with illegal element types are expanded.
  1342. EVT NVT = EVT::getVectorVT(Context, EltVT, VT.getVectorNumElements() / 2);
  1343. return LegalizeKind(TypeSplitVector, NVT);
  1344. }
  1345. private:
  1346. std::vector<std::pair<MVT, const TargetRegisterClass*> > AvailableRegClasses;
  1347. /// TargetDAGCombineArray - Targets can specify ISD nodes that they would
  1348. /// like PerformDAGCombine callbacks for by calling setTargetDAGCombine(),
  1349. /// which sets a bit in this array.
  1350. unsigned char
  1351. TargetDAGCombineArray[(ISD::BUILTIN_OP_END+CHAR_BIT-1)/CHAR_BIT];
  1352. /// PromoteToType - For operations that must be promoted to a specific type,
  1353. /// this holds the destination type. This map should be sparse, so don't hold
  1354. /// it as an array.
  1355. ///
  1356. /// Targets add entries to this map with AddPromotedToType(..), clients access
  1357. /// this with getTypeToPromoteTo(..).
  1358. std::map<std::pair<unsigned, MVT::SimpleValueType>, MVT::SimpleValueType>
  1359. PromoteToType;
  1360. /// LibcallRoutineNames - Stores the name each libcall.
  1361. ///
  1362. const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL];
  1363. /// CmpLibcallCCs - The ISD::CondCode that should be used to test the result
  1364. /// of each of the comparison libcall against zero.
  1365. ISD::CondCode CmpLibcallCCs[RTLIB::UNKNOWN_LIBCALL];
  1366. /// LibcallCallingConvs - Stores the CallingConv that should be used for each
  1367. /// libcall.
  1368. CallingConv::ID LibcallCallingConvs[RTLIB::UNKNOWN_LIBCALL];
  1369. protected:
  1370. /// When lowering \@llvm.memset this field specifies the maximum number of
  1371. /// store operations that may be substituted for the call to memset. Targets
  1372. /// must set this value based on the cost threshold for that target. Targets
  1373. /// should assume that the memset will be done using as many of the largest
  1374. /// store operations first, followed by smaller ones, if necessary, per
  1375. /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
  1376. /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
  1377. /// store. This only applies to setting a constant array of a constant size.
  1378. /// @brief Specify maximum number of store instructions per memset call.
  1379. unsigned MaxStoresPerMemset;
  1380. /// Maximum number of stores operations that may be substituted for the call
  1381. /// to memset, used for functions with OptSize attribute.
  1382. unsigned MaxStoresPerMemsetOptSize;
  1383. /// When lowering \@llvm.memcpy this field specifies the maximum number of
  1384. /// store operations that may be substituted for a call to memcpy. Targets
  1385. /// must set this value based on the cost threshold for that target. Targets
  1386. /// should assume that the memcpy will be done using as many of the largest
  1387. /// store operations first, followed by smaller ones, if necessary, per
  1388. /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
  1389. /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
  1390. /// and one 1-byte store. This only applies to copying a constant array of
  1391. /// constant size.
  1392. /// @brief Specify maximum bytes of store instructions per memcpy call.
  1393. unsigned MaxStoresPerMemcpy;
  1394. /// Maximum number of store operations that may be substituted for a call
  1395. /// to memcpy, used for functions with OptSize attribute.
  1396. unsigned MaxStoresPerMemcpyOptSize;
  1397. /// When lowering \@llvm.memmove this field specifies the maximum number of
  1398. /// store instructions that may be substituted for a call to memmove. Targets
  1399. /// must set this value based on the cost threshold for that target. Targets
  1400. /// should assume that the memmove will be done using as many of the largest
  1401. /// store operations first, followed by smaller ones, if necessary, per
  1402. /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
  1403. /// with 8-bit alignment would result in nine 1-byte stores. This only
  1404. /// applies to copying a constant array of constant size.
  1405. /// @brief Specify maximum bytes of store instructions per memmove call.
  1406. unsigned MaxStoresPerMemmove;
  1407. /// Maximum number of store instructions that may be substituted for a call
  1408. /// to memmove, used for functions with OpSize attribute.
  1409. unsigned MaxStoresPerMemmoveOptSize;
  1410. /// PredictableSelectIsExpensive - Tells the code generator that select is
  1411. /// more expensive than a branch if the branch is usually predicted right.
  1412. bool PredictableSelectIsExpensive;
  1413. protected:
  1414. /// isLegalRC - Return true if the value types that can be represented by the
  1415. /// specified register class are all legal.
  1416. bool isLegalRC(const TargetRegisterClass *RC) const;
  1417. };
  1418. //===----------------------------------------------------------------------===//
  1419. /// TargetLowering - This class defines information used to lower LLVM code to
  1420. /// legal SelectionDAG operators that the target instruction selector can accept
  1421. /// natively.
  1422. ///
  1423. /// This class also defines callbacks that targets must implement to lower
  1424. /// target-specific constructs to SelectionDAG operators.
  1425. ///
  1426. class TargetLowering : public TargetLoweringBase {
  1427. TargetLowering(const TargetLowering&) LLVM_DELETED_FUNCTION;
  1428. void operator=(const TargetLowering&) LLVM_DELETED_FUNCTION;
  1429. public:
  1430. /// NOTE: The constructor takes ownership of TLOF.
  1431. explicit TargetLowering(const TargetMachine &TM,
  1432. const TargetLoweringObjectFile *TLOF);
  1433. /// getPreIndexedAddressParts - returns true by value, base pointer and
  1434. /// offset pointer and addressing mode by reference if the node's address
  1435. /// can be legally represented as pre-indexed load / store address.
  1436. virtual bool getPreIndexedAddressParts(SDNode * /*N*/, SDValue &/*Base*/,
  1437. SDValue &/*Offset*/,
  1438. ISD::MemIndexedMode &/*AM*/,
  1439. SelectionDAG &/*DAG*/) const {
  1440. return false;
  1441. }
  1442. /// getPostIndexedAddressParts - returns true by value, base pointer and
  1443. /// offset pointer and addressing mode by reference if this node can be
  1444. /// combined with a load / store to form a post-indexed load / store.
  1445. virtual bool getPostIndexedAddressParts(SDNode * /*N*/, SDNode * /*Op*/,
  1446. SDValue &/*Base*/, SDValue &/*Offset*/,
  1447. ISD::MemIndexedMode &/*AM*/,
  1448. SelectionDAG &/*DAG*/) const {
  1449. return false;
  1450. }
  1451. /// getJumpTableEncoding - Return the entry encoding for a jump table in the
  1452. /// current function. The returned value is a member of the
  1453. /// MachineJumpTableInfo::JTEntryKind enum.
  1454. virtual unsigned getJumpTableEncoding() const;
  1455. virtual const MCExpr *
  1456. LowerCustomJumpTableEntry(const MachineJumpTableInfo * /*MJTI*/,
  1457. const MachineBasicBlock * /*MBB*/, unsigned /*uid*/,
  1458. MCContext &/*Ctx*/) const {
  1459. llvm_unreachable("Need to implement this hook if target has custom JTIs");
  1460. }
  1461. /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
  1462. /// jumptable.
  1463. virtual SDValue getPICJumpTableRelocBase(SDValue Table,
  1464. SelectionDAG &DAG) const;
  1465. /// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
  1466. /// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
  1467. /// MCExpr.
  1468. virtual const MCExpr *
  1469. getPICJumpTableRelocBaseExpr(const MachineFunction *MF,
  1470. unsigned JTI, MCContext &Ctx) const;
  1471. /// isOffsetFoldingLegal - Return true if folding a constant offset
  1472. /// with the given GlobalAddress is legal. It is frequently not legal in
  1473. /// PIC relocation models.
  1474. virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const;
  1475. bool isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
  1476. SDValue &Chain) const;
  1477. void softenSetCCOperands(SelectionDAG &DAG, EVT VT,
  1478. SDValue &NewLHS, SDValue &NewRHS,
  1479. ISD::CondCode &CCCode, DebugLoc DL) const;
  1480. SDValue makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, EVT RetVT,
  1481. const SDValue *Ops, unsigned NumOps,
  1482. bool isSigned, DebugLoc dl) const;
  1483. //===--------------------------------------------------------------------===//
  1484. // TargetLowering Optimization Methods
  1485. //
  1486. /// TargetLoweringOpt - A convenience struct that encapsulates a DAG, and two
  1487. /// SDValues for returning information from TargetLowering to its clients
  1488. /// that want to combine
  1489. struct TargetLoweringOpt {
  1490. SelectionDAG &DAG;
  1491. bool LegalTys;
  1492. bool LegalOps;
  1493. SDValue Old;
  1494. SDValue New;
  1495. explicit TargetLoweringOpt(SelectionDAG &InDAG,
  1496. bool LT, bool LO) :
  1497. DAG(InDAG), LegalTys(LT), LegalOps(LO) {}
  1498. bool LegalTypes() const { return LegalTys; }
  1499. bool LegalOperations() const { return LegalOps; }
  1500. bool CombineTo(SDValue O, SDValue N) {
  1501. Old = O;
  1502. New = N;
  1503. return true;
  1504. }
  1505. /// ShrinkDemandedConstant - Check to see if the specified operand of the
  1506. /// specified instruction is a constant integer. If so, check to see if
  1507. /// there are any bits set in the constant that are not demanded. If so,
  1508. /// shrink the constant and return true.
  1509. bool ShrinkDemandedConstant(SDValue Op, const APInt &Demanded);
  1510. /// ShrinkDemandedOp - Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the
  1511. /// casts are free. This uses isZExtFree and ZERO_EXTEND for the widening
  1512. /// cast, but it could be generalized for targets with other types of
  1513. /// implicit widening casts.
  1514. bool ShrinkDemandedOp(SDValue Op, unsigned BitWidth, const APInt &Demanded,
  1515. DebugLoc dl);
  1516. };
  1517. /// SimplifyDemandedBits - Look at Op. At this point, we know that only the
  1518. /// DemandedMask bits of the result of Op are ever used downstream. If we can
  1519. /// use this information to simplify Op, create a new simplified DAG node and
  1520. /// return true, returning the original and new nodes in Old and New.
  1521. /// Otherwise, analyze the expression and return a mask of KnownOne and
  1522. /// KnownZero bits for the expression (used to simplify the caller).
  1523. /// The KnownZero/One bits may only be accurate for those bits in the
  1524. /// DemandedMask.
  1525. bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedMask,
  1526. APInt &KnownZero, APInt &KnownOne,
  1527. TargetLoweringOpt &TLO, unsigned Depth = 0) const;
  1528. /// computeMaskedBitsForTargetNode - Determine which of the bits specified in
  1529. /// Mask are known to be either zero or one and return them in the
  1530. /// KnownZero/KnownOne bitsets.
  1531. virtual void computeMaskedBitsForTargetNode(const SDValue Op,
  1532. APInt &KnownZero,
  1533. APInt &KnownOne,
  1534. const SelectionDAG &DAG,
  1535. unsigned Depth = 0) const;
  1536. /// ComputeNumSignBitsForTargetNode - This method can be implemented by
  1537. /// targets that want to expose additional information about sign bits to the
  1538. /// DAG Combiner.
  1539. virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op,
  1540. unsigned Depth = 0) const;
  1541. struct DAGCombinerInfo {
  1542. void *DC; // The DAG Combiner object.
  1543. CombineLevel Level;
  1544. bool CalledByLegalizer;
  1545. public:
  1546. SelectionDAG &DAG;
  1547. DAGCombinerInfo(SelectionDAG &dag, CombineLevel level, bool cl, void *dc)
  1548. : DC(dc), Level(level), CalledByLegalizer(cl), DAG(dag) {}
  1549. bool isBeforeLegalize() const { return Level == BeforeLegalizeTypes; }
  1550. bool isBeforeLegalizeOps() const { return Level < AfterLegalizeVectorOps; }
  1551. bool isAfterLegalizeVectorOps() const {
  1552. return Level == AfterLegalizeDAG;
  1553. }
  1554. CombineLevel getDAGCombineLevel() { return Level; }
  1555. bool isCalledByLegalizer() const { return CalledByLegalizer; }
  1556. void AddToWorklist(SDNode *N);
  1557. void RemoveFromWorklist(SDNode *N);
  1558. SDValue CombineTo(SDNode *N, const std::vector<SDValue> &To,
  1559. bool AddTo = true);
  1560. SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true);
  1561. SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo = true);
  1562. void CommitTargetLoweringOpt(const TargetLoweringOpt &TLO);
  1563. };
  1564. /// SimplifySetCC - Try to simplify a setcc built with the specified operands
  1565. /// and cc. If it is unable to simplify it, return a null SDValue.
  1566. SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
  1567. ISD::CondCode Cond, bool foldBooleans,
  1568. DAGCombinerInfo &DCI, DebugLoc dl) const;
  1569. /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
  1570. /// node is a GlobalAddress + offset.
  1571. virtual bool
  1572. isGAPlusOffset(SDNode *N, const GlobalValue* &GA, int64_t &Offset) const;
  1573. /// PerformDAGCombine - This method will be invoked for all target nodes and
  1574. /// for any target-independent nodes that the target has registered with
  1575. /// invoke it for.
  1576. ///
  1577. /// The semantics are as follows:
  1578. /// Return Value:
  1579. /// SDValue.Val == 0 - No change was made
  1580. /// SDValue.Val == N - N was replaced, is dead, and is already handled.
  1581. /// otherwise - N should be replaced by the returned Operand.
  1582. ///
  1583. /// In addition, methods provided by DAGCombinerInfo may be used to perform
  1584. /// more complex transformations.
  1585. ///
  1586. virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
  1587. /// isTypeDesirableForOp - Return true if the target has native support for
  1588. /// the specified value type and it is 'desirable' to use the type for the
  1589. /// given node type. e.g. On x86 i16 is legal, but undesirable since i16
  1590. /// instruction encodings are longer and some i16 instructions are slow.
  1591. virtual bool isTypeDesirableForOp(unsigned /*Opc*/, EVT VT) const {
  1592. // By default, assume all legal types are desirable.
  1593. return isTypeLegal(VT);
  1594. }
  1595. /// isDesirableToPromoteOp - Return true if it is profitable for dag combiner
  1596. /// to transform a floating point op of specified opcode to a equivalent op of
  1597. /// an integer type. e.g. f32 load -> i32 load can be profitable on ARM.
  1598. virtual bool isDesirableToTransformToIntegerOp(unsigned /*Opc*/,
  1599. EVT /*VT*/) const {
  1600. return false;
  1601. }
  1602. /// IsDesirableToPromoteOp - This method query the target whether it is
  1603. /// beneficial for dag combiner to promote the specified node. If true, it
  1604. /// should return the desired promotion type by reference.
  1605. virtual bool IsDesirableToPromoteOp(SDValue /*Op*/, EVT &/*PVT*/) const {
  1606. return false;
  1607. }
  1608. //===--------------------------------------------------------------------===//
  1609. // Lowering methods - These methods must be implemented by targets so that
  1610. // the SelectionDAGBuilder code knows how to lower these.
  1611. //
  1612. /// LowerFormalArguments - This hook must be implemented to lower the
  1613. /// incoming (formal) arguments, described by the Ins array, into the
  1614. /// specified DAG. The implementation should fill in the InVals array
  1615. /// with legal-type argument values, and return the resulting token
  1616. /// chain value.
  1617. ///
  1618. virtual SDValue
  1619. LowerFormalArguments(SDValue /*Chain*/, CallingConv::ID /*CallConv*/,
  1620. bool /*isVarArg*/,
  1621. const SmallVectorImpl<ISD::InputArg> &/*Ins*/,
  1622. DebugLoc /*dl*/, SelectionDAG &/*DAG*/,
  1623. SmallVectorImpl<SDValue> &/*InVals*/) const {
  1624. llvm_unreachable("Not Implemented");
  1625. }
  1626. struct ArgListEntry {
  1627. SDValue Node;
  1628. Type* Ty;
  1629. bool isSExt : 1;
  1630. bool isZExt : 1;
  1631. bool isInReg : 1;
  1632. bool isSRet : 1;
  1633. bool isNest : 1;
  1634. bool isByVal : 1;
  1635. bool isReturned : 1;
  1636. uint16_t Alignment;
  1637. ArgListEntry() : isSExt(false), isZExt(false), isInReg(false),
  1638. isSRet(false), isNest(false), isByVal(false), isReturned(false),
  1639. Alignment(0) { }
  1640. };
  1641. typedef std::vector<ArgListEntry> ArgListTy;
  1642. /// CallLoweringInfo - This structure contains all information that is
  1643. /// necessary for lowering calls. It is passed to TLI::LowerCallTo when the
  1644. /// SelectionDAG builder needs to lower a call, and targets will see this
  1645. /// struct in their LowerCall implementation.
  1646. struct CallLoweringInfo {
  1647. SDValue Chain;
  1648. Type *RetTy;
  1649. bool RetSExt : 1;
  1650. bool RetZExt : 1;
  1651. bool IsVarArg : 1;
  1652. bool IsInReg : 1;
  1653. bool DoesNotReturn : 1;
  1654. bool IsReturnValueUsed : 1;
  1655. // IsTailCall should be modified by implementations of
  1656. // TargetLowering::LowerCall that perform tail call conversions.
  1657. bool IsTailCall;
  1658. unsigned NumFixedArgs;
  1659. CallingConv::ID CallConv;
  1660. SDValue Callee;
  1661. ArgListTy &Args;
  1662. SelectionDAG &DAG;
  1663. DebugLoc DL;
  1664. ImmutableCallSite *CS;
  1665. SmallVector<ISD::OutputArg, 32> Outs;
  1666. SmallVector<SDValue, 32> OutVals;
  1667. SmallVector<ISD::InputArg, 32> Ins;
  1668. /// CallLoweringInfo - Constructs a call lowering context based on the
  1669. /// ImmutableCallSite \p cs.
  1670. CallLoweringInfo(SDValue chain, Type *retTy,
  1671. FunctionType *FTy, bool isTailCall, SDValue callee,
  1672. ArgListTy &args, SelectionDAG &dag, DebugLoc dl,
  1673. ImmutableCallSite &cs)
  1674. : Chain(chain), RetTy(retTy), RetSExt(cs.paramHasAttr(0, Attribute::SExt)),
  1675. RetZExt(cs.paramHasAttr(0, Attribute::ZExt)), IsVarArg(FTy->isVarArg()),
  1676. IsInReg(cs.paramHasAttr(0, Attribute::InReg)),
  1677. DoesNotReturn(cs.doesNotReturn()),
  1678. IsReturnValueUsed(!cs.getInstruction()->use_empty()),
  1679. IsTailCall(isTailCall), NumFixedArgs(FTy->getNumParams()),
  1680. CallConv(cs.getCallingConv()), Callee(callee), Args(args), DAG(dag),
  1681. DL(dl), CS(&cs) {}
  1682. /// CallLoweringInfo - Constructs a call lowering context based on the
  1683. /// provided call information.
  1684. CallLoweringInfo(SDValue chain, Type *retTy, bool retSExt, bool retZExt,
  1685. bool isVarArg, bool isInReg, unsigned numFixedArgs,
  1686. CallingConv::ID callConv, bool isTailCall,
  1687. bool doesNotReturn, bool isReturnValueUsed, SDValue callee,
  1688. ArgListTy &args, SelectionDAG &dag, DebugLoc dl)
  1689. : Chain(chain), RetTy(retTy), RetSExt(retSExt), RetZExt(retZExt),
  1690. IsVarArg(isVarArg), IsInReg(isInReg), DoesNotReturn(doesNotReturn),
  1691. IsReturnValueUsed(isReturnValueUsed), IsTailCall(isTailCall),
  1692. NumFixedArgs(numFixedArgs), CallConv(callConv), Callee(callee),
  1693. Args(args), DAG(dag), DL(dl), CS(NULL) {}
  1694. };
  1695. /// LowerCallTo - This function lowers an abstract call to a function into an
  1696. /// actual call. This returns a pair of operands. The first element is the
  1697. /// return value for the function (if RetTy is not VoidTy). The second
  1698. /// element is the outgoing token chain. It calls LowerCall to do the actual
  1699. /// lowering.
  1700. std::pair<SDValue, SDValue> LowerCallTo(CallLoweringInfo &CLI) const;
  1701. /// LowerCall - This hook must be implemented to lower calls into the
  1702. /// the specified DAG. The outgoing arguments to the call are described
  1703. /// by the Outs array, and the values to be returned by the call are
  1704. /// described by the Ins array. The implementation should fill in the
  1705. /// InVals array with legal-type return values from the call, and return
  1706. /// the resulting token chain value.
  1707. virtual SDValue
  1708. LowerCall(CallLoweringInfo &/*CLI*/,
  1709. SmallVectorImpl<SDValue> &/*InVals*/) const {
  1710. llvm_unreachable("Not Implemented");
  1711. }
  1712. /// HandleByVal - Target-specific cleanup for formal ByVal parameters.
  1713. virtual void HandleByVal(CCState *, unsigned &, unsigned) const {}
  1714. /// CanLowerReturn - This hook should be implemented to check whether the
  1715. /// return values described by the Outs array can fit into the return
  1716. /// registers. If false is returned, an sret-demotion is performed.
  1717. ///
  1718. virtual bool CanLowerReturn(CallingConv::ID /*CallConv*/,
  1719. MachineFunction &/*MF*/, bool /*isVarArg*/,
  1720. const SmallVectorImpl<ISD::OutputArg> &/*Outs*/,
  1721. LLVMContext &/*Context*/) const
  1722. {
  1723. // Return true by default to get preexisting behavior.
  1724. return true;
  1725. }
  1726. /// LowerReturn - This hook must be implemented to lower outgoing
  1727. /// return values, described by the Outs array, into the specified
  1728. /// DAG. The implementation should return the resulting token chain
  1729. /// value.
  1730. ///
  1731. virtual SDValue
  1732. LowerReturn(SDValue /*Chain*/, CallingConv::ID /*CallConv*/,
  1733. bool /*isVarArg*/,
  1734. const SmallVectorImpl<ISD::OutputArg> &/*Outs*/,
  1735. const SmallVectorImpl<SDValue> &/*OutVals*/,
  1736. DebugLoc /*dl*/, SelectionDAG &/*DAG*/) const {
  1737. llvm_unreachable("Not Implemented");
  1738. }
  1739. /// isUsedByReturnOnly - Return true if result of the specified node is used
  1740. /// by a return node only. It also compute and return the input chain for the
  1741. /// tail call.
  1742. /// This is used to determine whether it is possible
  1743. /// to codegen a libcall as tail call at legalization time.
  1744. virtual bool isUsedByReturnOnly(SDNode *, SDValue &Chain) const {
  1745. return false;
  1746. }
  1747. /// mayBeEmittedAsTailCall - Return true if the target may be able emit the
  1748. /// call instruction as a tail call. This is used by optimization passes to
  1749. /// determine if it's profitable to duplicate return instructions to enable
  1750. /// tailcall optimization.
  1751. virtual bool mayBeEmittedAsTailCall(CallInst *) const {
  1752. return false;
  1753. }
  1754. /// getTypeForExtArgOrReturn - Return the type that should be used to zero or
  1755. /// sign extend a zeroext/signext integer argument or return value.
  1756. /// FIXME: Most C calling convention requires the return type to be promoted,
  1757. /// but this is not true all the time, e.g. i1 on x86-64. It is also not
  1758. /// necessary for non-C calling conventions. The frontend should handle this
  1759. /// and include all of the necessary information.
  1760. virtual MVT getTypeForExtArgOrReturn(MVT VT,
  1761. ISD::NodeType /*ExtendKind*/) const {
  1762. MVT MinVT = getRegisterType(MVT::i32);
  1763. return VT.bitsLT(MinVT) ? MinVT : VT;
  1764. }
  1765. /// LowerOperationWrapper - This callback is invoked by the type legalizer
  1766. /// to legalize nodes with an illegal operand type but legal result types.
  1767. /// It replaces the LowerOperation callback in the type Legalizer.
  1768. /// The reason we can not do away with LowerOperation entirely is that
  1769. /// LegalizeDAG isn't yet ready to use this callback.
  1770. /// TODO: Consider merging with ReplaceNodeResults.
  1771. /// The target places new result values for the node in Results (their number
  1772. /// and types must exactly match those of the original return values of
  1773. /// the node), or leaves Results empty, which indicates that the node is not
  1774. /// to be custom lowered after all.
  1775. /// The default implementation calls LowerOperation.
  1776. virtual void LowerOperationWrapper(SDNode *N,
  1777. SmallVectorImpl<SDValue> &Results,
  1778. SelectionDAG &DAG) const;
  1779. /// LowerOperation - This callback is invoked for operations that are
  1780. /// unsupported by the target, which are registered to use 'custom' lowering,
  1781. /// and whose defined values are all legal.
  1782. /// If the target has no operations that require custom lowering, it need not
  1783. /// implement this. The default implementation of this aborts.
  1784. virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
  1785. /// ReplaceNodeResults - This callback is invoked when a node result type is
  1786. /// illegal for the target, and the operation was registered to use 'custom'
  1787. /// lowering for that result type. The target places new result values for
  1788. /// the node in Results (their number and types must exactly match those of
  1789. /// the original return values of the node), or leaves Results empty, which
  1790. /// indicates that the node is not to be custom lowered after all.
  1791. ///
  1792. /// If the target has no operations that require custom lowering, it need not
  1793. /// implement this. The default implementation aborts.
  1794. virtual void ReplaceNodeResults(SDNode * /*N*/,
  1795. SmallVectorImpl<SDValue> &/*Results*/,
  1796. SelectionDAG &/*DAG*/) const {
  1797. llvm_unreachable("ReplaceNodeResults not implemented for this target!");
  1798. }
  1799. /// getTargetNodeName() - This method returns the name of a target specific
  1800. /// DAG node.
  1801. virtual const char *getTargetNodeName(unsigned Opcode) const;
  1802. /// createFastISel - This method returns a target specific FastISel object,
  1803. /// or null if the target does not support "fast" ISel.
  1804. virtual FastISel *createFastISel(FunctionLoweringInfo &,
  1805. const TargetLibraryInfo *) const {
  1806. return 0;
  1807. }
  1808. //===--------------------------------------------------------------------===//
  1809. // Inline Asm Support hooks
  1810. //
  1811. /// ExpandInlineAsm - This hook allows the target to expand an inline asm
  1812. /// call to be explicit llvm code if it wants to. This is useful for
  1813. /// turning simple inline asms into LLVM intrinsics, which gives the
  1814. /// compiler more information about the behavior of the code.
  1815. virtual bool ExpandInlineAsm(CallInst *) const {
  1816. return false;
  1817. }
  1818. enum ConstraintType {
  1819. C_Register, // Constraint represents specific register(s).
  1820. C_RegisterClass, // Constraint represents any of register(s) in class.
  1821. C_Memory, // Memory constraint.
  1822. C_Other, // Something else.
  1823. C_Unknown // Unsupported constraint.
  1824. };
  1825. enum ConstraintWeight {
  1826. // Generic weights.
  1827. CW_Invalid = -1, // No match.
  1828. CW_Okay = 0, // Acceptable.
  1829. CW_Good = 1, // Good weight.
  1830. CW_Better = 2, // Better weight.
  1831. CW_Best = 3, // Best weight.
  1832. // Well-known weights.
  1833. CW_SpecificReg = CW_Okay, // Specific register operands.
  1834. CW_Register = CW_Good, // Register operands.
  1835. CW_Memory = CW_Better, // Memory operands.
  1836. CW_Constant = CW_Best, // Constant operand.
  1837. CW_Default = CW_Okay // Default or don't know type.
  1838. };
  1839. /// AsmOperandInfo - This contains information for each constraint that we are
  1840. /// lowering.
  1841. struct AsmOperandInfo : public InlineAsm::ConstraintInfo {
  1842. /// ConstraintCode - This contains the actual string for the code, like "m".
  1843. /// TargetLowering picks the 'best' code from ConstraintInfo::Codes that
  1844. /// most closely matches the operand.
  1845. std::string ConstraintCode;
  1846. /// ConstraintType - Information about the constraint code, e.g. Register,
  1847. /// RegisterClass, Memory, Other, Unknown.
  1848. TargetLowering::ConstraintType ConstraintType;
  1849. /// CallOperandval - If this is the result output operand or a
  1850. /// clobber, this is null, otherwise it is the incoming operand to the
  1851. /// CallInst. This gets modified as the asm is processed.
  1852. Value *CallOperandVal;
  1853. /// ConstraintVT - The ValueType for the operand value.
  1854. MVT ConstraintVT;
  1855. /// isMatchingInputConstraint - Return true of this is an input operand that
  1856. /// is a matching constraint like "4".
  1857. bool isMatchingInputConstraint() const;
  1858. /// getMatchedOperand - If this is an input matching constraint, this method
  1859. /// returns the output operand it matches.
  1860. unsigned getMatchedOperand() const;
  1861. /// Copy constructor for copying from an AsmOperandInfo.
  1862. AsmOperandInfo(const AsmOperandInfo &info)
  1863. : InlineAsm::ConstraintInfo(info),
  1864. ConstraintCode(info.ConstraintCode),
  1865. ConstraintType(info.ConstraintType),
  1866. CallOperandVal(info.CallOperandVal),
  1867. ConstraintVT(info.ConstraintVT) {
  1868. }
  1869. /// Copy constructor for copying from a ConstraintInfo.
  1870. AsmOperandInfo(const InlineAsm::ConstraintInfo &info)
  1871. : InlineAsm::ConstraintInfo(info),
  1872. ConstraintType(TargetLowering::C_Unknown),
  1873. CallOperandVal(0), ConstraintVT(MVT::Other) {
  1874. }
  1875. };
  1876. typedef std::vector<AsmOperandInfo> AsmOperandInfoVector;
  1877. /// ParseConstraints - Split up the constraint string from the inline
  1878. /// assembly value into the specific constraints and their prefixes,
  1879. /// and also tie in the associated operand values.
  1880. /// If this returns an empty vector, and if the constraint string itself
  1881. /// isn't empty, there was an error parsing.
  1882. virtual AsmOperandInfoVector ParseConstraints(ImmutableCallSite CS) const;
  1883. /// Examine constraint type and operand type and determine a weight value.
  1884. /// The operand object must already have been set up with the operand type.
  1885. virtual ConstraintWeight getMultipleConstraintMatchWeight(
  1886. AsmOperandInfo &info, int maIndex) const;
  1887. /// Examine constraint string and operand type and determine a weight value.
  1888. /// The operand object must already have been set up with the operand type.
  1889. virtual ConstraintWeight getSingleConstraintMatchWeight(
  1890. AsmOperandInfo &info, const char *constraint) const;
  1891. /// ComputeConstraintToUse - Determines the constraint code and constraint
  1892. /// type to use for the specific AsmOperandInfo, setting
  1893. /// OpInfo.ConstraintCode and OpInfo.ConstraintType. If the actual operand
  1894. /// being passed in is available, it can be passed in as Op, otherwise an
  1895. /// empty SDValue can be passed.
  1896. virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo,
  1897. SDValue Op,
  1898. SelectionDAG *DAG = 0) const;
  1899. /// getConstraintType - Given a constraint, return the type of constraint it
  1900. /// is for this target.
  1901. virtual ConstraintType getConstraintType(const std::string &Constraint) const;
  1902. /// getRegForInlineAsmConstraint - Given a physical register constraint (e.g.
  1903. /// {edx}), return the register number and the register class for the
  1904. /// register.
  1905. ///
  1906. /// Given a register class constraint, like 'r', if this corresponds directly
  1907. /// to an LLVM register class, return a register of 0 and the register class
  1908. /// pointer.
  1909. ///
  1910. /// This should only be used for C_Register constraints. On error,
  1911. /// this returns a register number of 0 and a null register class pointer..
  1912. virtual std::pair<unsigned, const TargetRegisterClass*>
  1913. getRegForInlineAsmConstraint(const std::string &Constraint,
  1914. EVT VT) const;
  1915. /// LowerXConstraint - try to replace an X constraint, which matches anything,
  1916. /// with another that has more specific requirements based on the type of the
  1917. /// corresponding operand. This returns null if there is no replacement to
  1918. /// make.
  1919. virtual const char *LowerXConstraint(EVT ConstraintVT) const;
  1920. /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
  1921. /// vector. If it is invalid, don't add anything to Ops.
  1922. virtual void LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
  1923. std::vector<SDValue> &Ops,
  1924. SelectionDAG &DAG) const;
  1925. //===--------------------------------------------------------------------===//
  1926. // Div utility functions
  1927. //
  1928. SDValue BuildExactSDIV(SDValue Op1, SDValue Op2, DebugLoc dl,
  1929. SelectionDAG &DAG) const;
  1930. SDValue BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
  1931. std::vector<SDNode*> *Created) const;
  1932. SDValue BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
  1933. std::vector<SDNode*> *Created) const;
  1934. //===--------------------------------------------------------------------===//
  1935. // Instruction Emitting Hooks
  1936. //
  1937. // EmitInstrWithCustomInserter - This method should be implemented by targets
  1938. // that mark instructions with the 'usesCustomInserter' flag. These
  1939. // instructions are special in various ways, which require special support to
  1940. // insert. The specified MachineInstr is created but not inserted into any
  1941. // basic blocks, and this method is called to expand it into a sequence of
  1942. // instructions, potentially also creating new basic blocks and control flow.
  1943. virtual MachineBasicBlock *
  1944. EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const;
  1945. /// AdjustInstrPostInstrSelection - This method should be implemented by
  1946. /// targets that mark instructions with the 'hasPostISelHook' flag. These
  1947. /// instructions must be adjusted after instruction selection by target hooks.
  1948. /// e.g. To fill in optional defs for ARM 's' setting instructions.
  1949. virtual void
  1950. AdjustInstrPostInstrSelection(MachineInstr *MI, SDNode *Node) const;
  1951. };
  1952. /// GetReturnInfo - Given an LLVM IR type and return type attributes,
  1953. /// compute the return value EVTs and flags, and optionally also
  1954. /// the offsets, if the return value is being lowered to memory.
  1955. void GetReturnInfo(Type* ReturnType, AttributeSet attr,
  1956. SmallVectorImpl<ISD::OutputArg> &Outs,
  1957. const TargetLowering &TLI);
  1958. } // end llvm namespace
  1959. #endif