PageRenderTime 81ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/clamav-0.97.5/libclamav/c++/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

#
C++ | 6481 lines | 4984 code | 722 blank | 775 comment | 1430 complexity | 9213e93c9c5845cf4acc5047dc808db7 MD5 | raw file
  1. //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
  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 implements the SelectionDAG class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/SelectionDAG.h"
  14. #include "SDNodeOrdering.h"
  15. #include "SDNodeDbgValue.h"
  16. #include "llvm/Constants.h"
  17. #include "llvm/Analysis/DebugInfo.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/Function.h"
  20. #include "llvm/GlobalAlias.h"
  21. #include "llvm/GlobalVariable.h"
  22. #include "llvm/Intrinsics.h"
  23. #include "llvm/DerivedTypes.h"
  24. #include "llvm/Assembly/Writer.h"
  25. #include "llvm/CallingConv.h"
  26. #include "llvm/CodeGen/MachineBasicBlock.h"
  27. #include "llvm/CodeGen/MachineConstantPool.h"
  28. #include "llvm/CodeGen/MachineFrameInfo.h"
  29. #include "llvm/CodeGen/MachineModuleInfo.h"
  30. #include "llvm/CodeGen/PseudoSourceValue.h"
  31. #include "llvm/Target/TargetRegisterInfo.h"
  32. #include "llvm/Target/TargetData.h"
  33. #include "llvm/Target/TargetFrameInfo.h"
  34. #include "llvm/Target/TargetLowering.h"
  35. #include "llvm/Target/TargetSelectionDAGInfo.h"
  36. #include "llvm/Target/TargetOptions.h"
  37. #include "llvm/Target/TargetInstrInfo.h"
  38. #include "llvm/Target/TargetIntrinsicInfo.h"
  39. #include "llvm/Target/TargetMachine.h"
  40. #include "llvm/Support/CommandLine.h"
  41. #include "llvm/Support/Debug.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/ManagedStatic.h"
  44. #include "llvm/Support/MathExtras.h"
  45. #include "llvm/Support/raw_ostream.h"
  46. #include "llvm/System/Mutex.h"
  47. #include "llvm/ADT/SetVector.h"
  48. #include "llvm/ADT/SmallPtrSet.h"
  49. #include "llvm/ADT/SmallSet.h"
  50. #include "llvm/ADT/SmallVector.h"
  51. #include "llvm/ADT/StringExtras.h"
  52. #include <algorithm>
  53. #include <cmath>
  54. using namespace llvm;
  55. /// makeVTList - Return an instance of the SDVTList struct initialized with the
  56. /// specified members.
  57. static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
  58. SDVTList Res = {VTs, NumVTs};
  59. return Res;
  60. }
  61. static const fltSemantics *EVTToAPFloatSemantics(EVT VT) {
  62. switch (VT.getSimpleVT().SimpleTy) {
  63. default: llvm_unreachable("Unknown FP format");
  64. case MVT::f32: return &APFloat::IEEEsingle;
  65. case MVT::f64: return &APFloat::IEEEdouble;
  66. case MVT::f80: return &APFloat::x87DoubleExtended;
  67. case MVT::f128: return &APFloat::IEEEquad;
  68. case MVT::ppcf128: return &APFloat::PPCDoubleDouble;
  69. }
  70. }
  71. SelectionDAG::DAGUpdateListener::~DAGUpdateListener() {}
  72. //===----------------------------------------------------------------------===//
  73. // ConstantFPSDNode Class
  74. //===----------------------------------------------------------------------===//
  75. /// isExactlyValue - We don't rely on operator== working on double values, as
  76. /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
  77. /// As such, this method can be used to do an exact bit-for-bit comparison of
  78. /// two floating point values.
  79. bool ConstantFPSDNode::isExactlyValue(const APFloat& V) const {
  80. return getValueAPF().bitwiseIsEqual(V);
  81. }
  82. bool ConstantFPSDNode::isValueValidForType(EVT VT,
  83. const APFloat& Val) {
  84. assert(VT.isFloatingPoint() && "Can only convert between FP types");
  85. // PPC long double cannot be converted to any other type.
  86. if (VT == MVT::ppcf128 ||
  87. &Val.getSemantics() == &APFloat::PPCDoubleDouble)
  88. return false;
  89. // convert modifies in place, so make a copy.
  90. APFloat Val2 = APFloat(Val);
  91. bool losesInfo;
  92. (void) Val2.convert(*EVTToAPFloatSemantics(VT), APFloat::rmNearestTiesToEven,
  93. &losesInfo);
  94. return !losesInfo;
  95. }
  96. //===----------------------------------------------------------------------===//
  97. // ISD Namespace
  98. //===----------------------------------------------------------------------===//
  99. /// isBuildVectorAllOnes - Return true if the specified node is a
  100. /// BUILD_VECTOR where all of the elements are ~0 or undef.
  101. bool ISD::isBuildVectorAllOnes(const SDNode *N) {
  102. // Look through a bit convert.
  103. if (N->getOpcode() == ISD::BIT_CONVERT)
  104. N = N->getOperand(0).getNode();
  105. if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
  106. unsigned i = 0, e = N->getNumOperands();
  107. // Skip over all of the undef values.
  108. while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
  109. ++i;
  110. // Do not accept an all-undef vector.
  111. if (i == e) return false;
  112. // Do not accept build_vectors that aren't all constants or which have non-~0
  113. // elements.
  114. SDValue NotZero = N->getOperand(i);
  115. if (isa<ConstantSDNode>(NotZero)) {
  116. if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
  117. return false;
  118. } else if (isa<ConstantFPSDNode>(NotZero)) {
  119. if (!cast<ConstantFPSDNode>(NotZero)->getValueAPF().
  120. bitcastToAPInt().isAllOnesValue())
  121. return false;
  122. } else
  123. return false;
  124. // Okay, we have at least one ~0 value, check to see if the rest match or are
  125. // undefs.
  126. for (++i; i != e; ++i)
  127. if (N->getOperand(i) != NotZero &&
  128. N->getOperand(i).getOpcode() != ISD::UNDEF)
  129. return false;
  130. return true;
  131. }
  132. /// isBuildVectorAllZeros - Return true if the specified node is a
  133. /// BUILD_VECTOR where all of the elements are 0 or undef.
  134. bool ISD::isBuildVectorAllZeros(const SDNode *N) {
  135. // Look through a bit convert.
  136. if (N->getOpcode() == ISD::BIT_CONVERT)
  137. N = N->getOperand(0).getNode();
  138. if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
  139. unsigned i = 0, e = N->getNumOperands();
  140. // Skip over all of the undef values.
  141. while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
  142. ++i;
  143. // Do not accept an all-undef vector.
  144. if (i == e) return false;
  145. // Do not accept build_vectors that aren't all constants or which have non-0
  146. // elements.
  147. SDValue Zero = N->getOperand(i);
  148. if (isa<ConstantSDNode>(Zero)) {
  149. if (!cast<ConstantSDNode>(Zero)->isNullValue())
  150. return false;
  151. } else if (isa<ConstantFPSDNode>(Zero)) {
  152. if (!cast<ConstantFPSDNode>(Zero)->getValueAPF().isPosZero())
  153. return false;
  154. } else
  155. return false;
  156. // Okay, we have at least one 0 value, check to see if the rest match or are
  157. // undefs.
  158. for (++i; i != e; ++i)
  159. if (N->getOperand(i) != Zero &&
  160. N->getOperand(i).getOpcode() != ISD::UNDEF)
  161. return false;
  162. return true;
  163. }
  164. /// isScalarToVector - Return true if the specified node is a
  165. /// ISD::SCALAR_TO_VECTOR node or a BUILD_VECTOR node where only the low
  166. /// element is not an undef.
  167. bool ISD::isScalarToVector(const SDNode *N) {
  168. if (N->getOpcode() == ISD::SCALAR_TO_VECTOR)
  169. return true;
  170. if (N->getOpcode() != ISD::BUILD_VECTOR)
  171. return false;
  172. if (N->getOperand(0).getOpcode() == ISD::UNDEF)
  173. return false;
  174. unsigned NumElems = N->getNumOperands();
  175. for (unsigned i = 1; i < NumElems; ++i) {
  176. SDValue V = N->getOperand(i);
  177. if (V.getOpcode() != ISD::UNDEF)
  178. return false;
  179. }
  180. return true;
  181. }
  182. /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
  183. /// when given the operation for (X op Y).
  184. ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
  185. // To perform this operation, we just need to swap the L and G bits of the
  186. // operation.
  187. unsigned OldL = (Operation >> 2) & 1;
  188. unsigned OldG = (Operation >> 1) & 1;
  189. return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
  190. (OldL << 1) | // New G bit
  191. (OldG << 2)); // New L bit.
  192. }
  193. /// getSetCCInverse - Return the operation corresponding to !(X op Y), where
  194. /// 'op' is a valid SetCC operation.
  195. ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
  196. unsigned Operation = Op;
  197. if (isInteger)
  198. Operation ^= 7; // Flip L, G, E bits, but not U.
  199. else
  200. Operation ^= 15; // Flip all of the condition bits.
  201. if (Operation > ISD::SETTRUE2)
  202. Operation &= ~8; // Don't let N and U bits get set.
  203. return ISD::CondCode(Operation);
  204. }
  205. /// isSignedOp - For an integer comparison, return 1 if the comparison is a
  206. /// signed operation and 2 if the result is an unsigned comparison. Return zero
  207. /// if the operation does not depend on the sign of the input (setne and seteq).
  208. static int isSignedOp(ISD::CondCode Opcode) {
  209. switch (Opcode) {
  210. default: llvm_unreachable("Illegal integer setcc operation!");
  211. case ISD::SETEQ:
  212. case ISD::SETNE: return 0;
  213. case ISD::SETLT:
  214. case ISD::SETLE:
  215. case ISD::SETGT:
  216. case ISD::SETGE: return 1;
  217. case ISD::SETULT:
  218. case ISD::SETULE:
  219. case ISD::SETUGT:
  220. case ISD::SETUGE: return 2;
  221. }
  222. }
  223. /// getSetCCOrOperation - Return the result of a logical OR between different
  224. /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
  225. /// returns SETCC_INVALID if it is not possible to represent the resultant
  226. /// comparison.
  227. ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  228. bool isInteger) {
  229. if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  230. // Cannot fold a signed integer setcc with an unsigned integer setcc.
  231. return ISD::SETCC_INVALID;
  232. unsigned Op = Op1 | Op2; // Combine all of the condition bits.
  233. // If the N and U bits get set then the resultant comparison DOES suddenly
  234. // care about orderedness, and is true when ordered.
  235. if (Op > ISD::SETTRUE2)
  236. Op &= ~16; // Clear the U bit if the N bit is set.
  237. // Canonicalize illegal integer setcc's.
  238. if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
  239. Op = ISD::SETNE;
  240. return ISD::CondCode(Op);
  241. }
  242. /// getSetCCAndOperation - Return the result of a logical AND between different
  243. /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
  244. /// function returns zero if it is not possible to represent the resultant
  245. /// comparison.
  246. ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
  247. bool isInteger) {
  248. if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
  249. // Cannot fold a signed setcc with an unsigned setcc.
  250. return ISD::SETCC_INVALID;
  251. // Combine all of the condition bits.
  252. ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
  253. // Canonicalize illegal integer setcc's.
  254. if (isInteger) {
  255. switch (Result) {
  256. default: break;
  257. case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
  258. case ISD::SETOEQ: // SETEQ & SETU[LG]E
  259. case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
  260. case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
  261. case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
  262. }
  263. }
  264. return Result;
  265. }
  266. //===----------------------------------------------------------------------===//
  267. // SDNode Profile Support
  268. //===----------------------------------------------------------------------===//
  269. /// AddNodeIDOpcode - Add the node opcode to the NodeID data.
  270. ///
  271. static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
  272. ID.AddInteger(OpC);
  273. }
  274. /// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
  275. /// solely with their pointer.
  276. static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList) {
  277. ID.AddPointer(VTList.VTs);
  278. }
  279. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  280. ///
  281. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  282. const SDValue *Ops, unsigned NumOps) {
  283. for (; NumOps; --NumOps, ++Ops) {
  284. ID.AddPointer(Ops->getNode());
  285. ID.AddInteger(Ops->getResNo());
  286. }
  287. }
  288. /// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
  289. ///
  290. static void AddNodeIDOperands(FoldingSetNodeID &ID,
  291. const SDUse *Ops, unsigned NumOps) {
  292. for (; NumOps; --NumOps, ++Ops) {
  293. ID.AddPointer(Ops->getNode());
  294. ID.AddInteger(Ops->getResNo());
  295. }
  296. }
  297. static void AddNodeIDNode(FoldingSetNodeID &ID,
  298. unsigned short OpC, SDVTList VTList,
  299. const SDValue *OpList, unsigned N) {
  300. AddNodeIDOpcode(ID, OpC);
  301. AddNodeIDValueTypes(ID, VTList);
  302. AddNodeIDOperands(ID, OpList, N);
  303. }
  304. /// AddNodeIDCustom - If this is an SDNode with special info, add this info to
  305. /// the NodeID data.
  306. static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N) {
  307. switch (N->getOpcode()) {
  308. case ISD::TargetExternalSymbol:
  309. case ISD::ExternalSymbol:
  310. llvm_unreachable("Should only be used on nodes with operands");
  311. default: break; // Normal nodes don't need extra info.
  312. case ISD::TargetConstant:
  313. case ISD::Constant:
  314. ID.AddPointer(cast<ConstantSDNode>(N)->getConstantIntValue());
  315. break;
  316. case ISD::TargetConstantFP:
  317. case ISD::ConstantFP: {
  318. ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
  319. break;
  320. }
  321. case ISD::TargetGlobalAddress:
  322. case ISD::GlobalAddress:
  323. case ISD::TargetGlobalTLSAddress:
  324. case ISD::GlobalTLSAddress: {
  325. const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
  326. ID.AddPointer(GA->getGlobal());
  327. ID.AddInteger(GA->getOffset());
  328. ID.AddInteger(GA->getTargetFlags());
  329. break;
  330. }
  331. case ISD::BasicBlock:
  332. ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
  333. break;
  334. case ISD::Register:
  335. ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
  336. break;
  337. case ISD::SRCVALUE:
  338. ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
  339. break;
  340. case ISD::FrameIndex:
  341. case ISD::TargetFrameIndex:
  342. ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
  343. break;
  344. case ISD::JumpTable:
  345. case ISD::TargetJumpTable:
  346. ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
  347. ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
  348. break;
  349. case ISD::ConstantPool:
  350. case ISD::TargetConstantPool: {
  351. const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
  352. ID.AddInteger(CP->getAlignment());
  353. ID.AddInteger(CP->getOffset());
  354. if (CP->isMachineConstantPoolEntry())
  355. CP->getMachineCPVal()->AddSelectionDAGCSEId(ID);
  356. else
  357. ID.AddPointer(CP->getConstVal());
  358. ID.AddInteger(CP->getTargetFlags());
  359. break;
  360. }
  361. case ISD::LOAD: {
  362. const LoadSDNode *LD = cast<LoadSDNode>(N);
  363. ID.AddInteger(LD->getMemoryVT().getRawBits());
  364. ID.AddInteger(LD->getRawSubclassData());
  365. break;
  366. }
  367. case ISD::STORE: {
  368. const StoreSDNode *ST = cast<StoreSDNode>(N);
  369. ID.AddInteger(ST->getMemoryVT().getRawBits());
  370. ID.AddInteger(ST->getRawSubclassData());
  371. break;
  372. }
  373. case ISD::ATOMIC_CMP_SWAP:
  374. case ISD::ATOMIC_SWAP:
  375. case ISD::ATOMIC_LOAD_ADD:
  376. case ISD::ATOMIC_LOAD_SUB:
  377. case ISD::ATOMIC_LOAD_AND:
  378. case ISD::ATOMIC_LOAD_OR:
  379. case ISD::ATOMIC_LOAD_XOR:
  380. case ISD::ATOMIC_LOAD_NAND:
  381. case ISD::ATOMIC_LOAD_MIN:
  382. case ISD::ATOMIC_LOAD_MAX:
  383. case ISD::ATOMIC_LOAD_UMIN:
  384. case ISD::ATOMIC_LOAD_UMAX: {
  385. const AtomicSDNode *AT = cast<AtomicSDNode>(N);
  386. ID.AddInteger(AT->getMemoryVT().getRawBits());
  387. ID.AddInteger(AT->getRawSubclassData());
  388. break;
  389. }
  390. case ISD::VECTOR_SHUFFLE: {
  391. const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
  392. for (unsigned i = 0, e = N->getValueType(0).getVectorNumElements();
  393. i != e; ++i)
  394. ID.AddInteger(SVN->getMaskElt(i));
  395. break;
  396. }
  397. case ISD::TargetBlockAddress:
  398. case ISD::BlockAddress: {
  399. ID.AddPointer(cast<BlockAddressSDNode>(N)->getBlockAddress());
  400. ID.AddInteger(cast<BlockAddressSDNode>(N)->getTargetFlags());
  401. break;
  402. }
  403. } // end switch (N->getOpcode())
  404. }
  405. /// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
  406. /// data.
  407. static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
  408. AddNodeIDOpcode(ID, N->getOpcode());
  409. // Add the return value info.
  410. AddNodeIDValueTypes(ID, N->getVTList());
  411. // Add the operand info.
  412. AddNodeIDOperands(ID, N->op_begin(), N->getNumOperands());
  413. // Handle SDNode leafs with special info.
  414. AddNodeIDCustom(ID, N);
  415. }
  416. /// encodeMemSDNodeFlags - Generic routine for computing a value for use in
  417. /// the CSE map that carries volatility, temporalness, indexing mode, and
  418. /// extension/truncation information.
  419. ///
  420. static inline unsigned
  421. encodeMemSDNodeFlags(int ConvType, ISD::MemIndexedMode AM, bool isVolatile,
  422. bool isNonTemporal) {
  423. assert((ConvType & 3) == ConvType &&
  424. "ConvType may not require more than 2 bits!");
  425. assert((AM & 7) == AM &&
  426. "AM may not require more than 3 bits!");
  427. return ConvType |
  428. (AM << 2) |
  429. (isVolatile << 5) |
  430. (isNonTemporal << 6);
  431. }
  432. //===----------------------------------------------------------------------===//
  433. // SelectionDAG Class
  434. //===----------------------------------------------------------------------===//
  435. /// doNotCSE - Return true if CSE should not be performed for this node.
  436. static bool doNotCSE(SDNode *N) {
  437. if (N->getValueType(0) == MVT::Flag)
  438. return true; // Never CSE anything that produces a flag.
  439. switch (N->getOpcode()) {
  440. default: break;
  441. case ISD::HANDLENODE:
  442. case ISD::EH_LABEL:
  443. return true; // Never CSE these nodes.
  444. }
  445. // Check that remaining values produced are not flags.
  446. for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
  447. if (N->getValueType(i) == MVT::Flag)
  448. return true; // Never CSE anything that produces a flag.
  449. return false;
  450. }
  451. /// RemoveDeadNodes - This method deletes all unreachable nodes in the
  452. /// SelectionDAG.
  453. void SelectionDAG::RemoveDeadNodes() {
  454. // Create a dummy node (which is not added to allnodes), that adds a reference
  455. // to the root node, preventing it from being deleted.
  456. HandleSDNode Dummy(getRoot());
  457. SmallVector<SDNode*, 128> DeadNodes;
  458. // Add all obviously-dead nodes to the DeadNodes worklist.
  459. for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
  460. if (I->use_empty())
  461. DeadNodes.push_back(I);
  462. RemoveDeadNodes(DeadNodes);
  463. // If the root changed (e.g. it was a dead load, update the root).
  464. setRoot(Dummy.getValue());
  465. }
  466. /// RemoveDeadNodes - This method deletes the unreachable nodes in the
  467. /// given list, and any nodes that become unreachable as a result.
  468. void SelectionDAG::RemoveDeadNodes(SmallVectorImpl<SDNode *> &DeadNodes,
  469. DAGUpdateListener *UpdateListener) {
  470. // Process the worklist, deleting the nodes and adding their uses to the
  471. // worklist.
  472. while (!DeadNodes.empty()) {
  473. SDNode *N = DeadNodes.pop_back_val();
  474. if (UpdateListener)
  475. UpdateListener->NodeDeleted(N, 0);
  476. // Take the node out of the appropriate CSE map.
  477. RemoveNodeFromCSEMaps(N);
  478. // Next, brutally remove the operand list. This is safe to do, as there are
  479. // no cycles in the graph.
  480. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  481. SDUse &Use = *I++;
  482. SDNode *Operand = Use.getNode();
  483. Use.set(SDValue());
  484. // Now that we removed this operand, see if there are no uses of it left.
  485. if (Operand->use_empty())
  486. DeadNodes.push_back(Operand);
  487. }
  488. DeallocateNode(N);
  489. }
  490. }
  491. void SelectionDAG::RemoveDeadNode(SDNode *N, DAGUpdateListener *UpdateListener){
  492. SmallVector<SDNode*, 16> DeadNodes(1, N);
  493. RemoveDeadNodes(DeadNodes, UpdateListener);
  494. }
  495. void SelectionDAG::DeleteNode(SDNode *N) {
  496. // First take this out of the appropriate CSE map.
  497. RemoveNodeFromCSEMaps(N);
  498. // Finally, remove uses due to operands of this node, remove from the
  499. // AllNodes list, and delete the node.
  500. DeleteNodeNotInCSEMaps(N);
  501. }
  502. void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
  503. assert(N != AllNodes.begin() && "Cannot delete the entry node!");
  504. assert(N->use_empty() && "Cannot delete a node that is not dead!");
  505. // Drop all of the operands and decrement used node's use counts.
  506. N->DropOperands();
  507. DeallocateNode(N);
  508. }
  509. void SelectionDAG::DeallocateNode(SDNode *N) {
  510. if (N->OperandsNeedDelete)
  511. delete[] N->OperandList;
  512. // Set the opcode to DELETED_NODE to help catch bugs when node
  513. // memory is reallocated.
  514. N->NodeType = ISD::DELETED_NODE;
  515. NodeAllocator.Deallocate(AllNodes.remove(N));
  516. // Remove the ordering of this node.
  517. Ordering->remove(N);
  518. // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
  519. SmallVector<SDDbgValue*, 2> &DbgVals = DbgInfo->getSDDbgValues(N);
  520. for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
  521. DbgVals[i]->setIsInvalidated();
  522. }
  523. /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
  524. /// correspond to it. This is useful when we're about to delete or repurpose
  525. /// the node. We don't want future request for structurally identical nodes
  526. /// to return N anymore.
  527. bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
  528. bool Erased = false;
  529. switch (N->getOpcode()) {
  530. case ISD::EntryToken:
  531. llvm_unreachable("EntryToken should not be in CSEMaps!");
  532. return false;
  533. case ISD::HANDLENODE: return false; // noop.
  534. case ISD::CONDCODE:
  535. assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
  536. "Cond code doesn't exist!");
  537. Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
  538. CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
  539. break;
  540. case ISD::ExternalSymbol:
  541. Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
  542. break;
  543. case ISD::TargetExternalSymbol: {
  544. ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
  545. Erased = TargetExternalSymbols.erase(
  546. std::pair<std::string,unsigned char>(ESN->getSymbol(),
  547. ESN->getTargetFlags()));
  548. break;
  549. }
  550. case ISD::VALUETYPE: {
  551. EVT VT = cast<VTSDNode>(N)->getVT();
  552. if (VT.isExtended()) {
  553. Erased = ExtendedValueTypeNodes.erase(VT);
  554. } else {
  555. Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != 0;
  556. ValueTypeNodes[VT.getSimpleVT().SimpleTy] = 0;
  557. }
  558. break;
  559. }
  560. default:
  561. // Remove it from the CSE Map.
  562. Erased = CSEMap.RemoveNode(N);
  563. break;
  564. }
  565. #ifndef NDEBUG
  566. // Verify that the node was actually in one of the CSE maps, unless it has a
  567. // flag result (which cannot be CSE'd) or is one of the special cases that are
  568. // not subject to CSE.
  569. if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
  570. !N->isMachineOpcode() && !doNotCSE(N)) {
  571. N->dump(this);
  572. dbgs() << "\n";
  573. llvm_unreachable("Node is not in map!");
  574. }
  575. #endif
  576. return Erased;
  577. }
  578. /// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
  579. /// maps and modified in place. Add it back to the CSE maps, unless an identical
  580. /// node already exists, in which case transfer all its users to the existing
  581. /// node. This transfer can potentially trigger recursive merging.
  582. ///
  583. void
  584. SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N,
  585. DAGUpdateListener *UpdateListener) {
  586. // For node types that aren't CSE'd, just act as if no identical node
  587. // already exists.
  588. if (!doNotCSE(N)) {
  589. SDNode *Existing = CSEMap.GetOrInsertNode(N);
  590. if (Existing != N) {
  591. // If there was already an existing matching node, use ReplaceAllUsesWith
  592. // to replace the dead one with the existing one. This can cause
  593. // recursive merging of other unrelated nodes down the line.
  594. ReplaceAllUsesWith(N, Existing, UpdateListener);
  595. // N is now dead. Inform the listener if it exists and delete it.
  596. if (UpdateListener)
  597. UpdateListener->NodeDeleted(N, Existing);
  598. DeleteNodeNotInCSEMaps(N);
  599. return;
  600. }
  601. }
  602. // If the node doesn't already exist, we updated it. Inform a listener if
  603. // it exists.
  604. if (UpdateListener)
  605. UpdateListener->NodeUpdated(N);
  606. }
  607. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  608. /// were replaced with those specified. If this node is never memoized,
  609. /// return null, otherwise return a pointer to the slot it would take. If a
  610. /// node already exists with these operands, the slot will be non-null.
  611. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
  612. void *&InsertPos) {
  613. if (doNotCSE(N))
  614. return 0;
  615. SDValue Ops[] = { Op };
  616. FoldingSetNodeID ID;
  617. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 1);
  618. AddNodeIDCustom(ID, N);
  619. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  620. return Node;
  621. }
  622. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  623. /// were replaced with those specified. If this node is never memoized,
  624. /// return null, otherwise return a pointer to the slot it would take. If a
  625. /// node already exists with these operands, the slot will be non-null.
  626. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
  627. SDValue Op1, SDValue Op2,
  628. void *&InsertPos) {
  629. if (doNotCSE(N))
  630. return 0;
  631. SDValue Ops[] = { Op1, Op2 };
  632. FoldingSetNodeID ID;
  633. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, 2);
  634. AddNodeIDCustom(ID, N);
  635. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  636. return Node;
  637. }
  638. /// FindModifiedNodeSlot - Find a slot for the specified node if its operands
  639. /// were replaced with those specified. If this node is never memoized,
  640. /// return null, otherwise return a pointer to the slot it would take. If a
  641. /// node already exists with these operands, the slot will be non-null.
  642. SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
  643. const SDValue *Ops,unsigned NumOps,
  644. void *&InsertPos) {
  645. if (doNotCSE(N))
  646. return 0;
  647. FoldingSetNodeID ID;
  648. AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops, NumOps);
  649. AddNodeIDCustom(ID, N);
  650. SDNode *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
  651. return Node;
  652. }
  653. /// VerifyNodeCommon - Sanity check the given node. Aborts if it is invalid.
  654. static void VerifyNodeCommon(SDNode *N) {
  655. switch (N->getOpcode()) {
  656. default:
  657. break;
  658. case ISD::BUILD_PAIR: {
  659. EVT VT = N->getValueType(0);
  660. assert(N->getNumValues() == 1 && "Too many results!");
  661. assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
  662. "Wrong return type!");
  663. assert(N->getNumOperands() == 2 && "Wrong number of operands!");
  664. assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
  665. "Mismatched operand types!");
  666. assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
  667. "Wrong operand type!");
  668. assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
  669. "Wrong return type size");
  670. break;
  671. }
  672. case ISD::BUILD_VECTOR: {
  673. assert(N->getNumValues() == 1 && "Too many results!");
  674. assert(N->getValueType(0).isVector() && "Wrong return type!");
  675. assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
  676. "Wrong number of operands!");
  677. EVT EltVT = N->getValueType(0).getVectorElementType();
  678. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
  679. assert((I->getValueType() == EltVT ||
  680. (EltVT.isInteger() && I->getValueType().isInteger() &&
  681. EltVT.bitsLE(I->getValueType()))) &&
  682. "Wrong operand type!");
  683. break;
  684. }
  685. }
  686. }
  687. /// VerifySDNode - Sanity check the given SDNode. Aborts if it is invalid.
  688. static void VerifySDNode(SDNode *N) {
  689. // The SDNode allocators cannot be used to allocate nodes with fields that are
  690. // not present in an SDNode!
  691. assert(!isa<MemSDNode>(N) && "Bad MemSDNode!");
  692. assert(!isa<ShuffleVectorSDNode>(N) && "Bad ShuffleVectorSDNode!");
  693. assert(!isa<ConstantSDNode>(N) && "Bad ConstantSDNode!");
  694. assert(!isa<ConstantFPSDNode>(N) && "Bad ConstantFPSDNode!");
  695. assert(!isa<GlobalAddressSDNode>(N) && "Bad GlobalAddressSDNode!");
  696. assert(!isa<FrameIndexSDNode>(N) && "Bad FrameIndexSDNode!");
  697. assert(!isa<JumpTableSDNode>(N) && "Bad JumpTableSDNode!");
  698. assert(!isa<ConstantPoolSDNode>(N) && "Bad ConstantPoolSDNode!");
  699. assert(!isa<BasicBlockSDNode>(N) && "Bad BasicBlockSDNode!");
  700. assert(!isa<SrcValueSDNode>(N) && "Bad SrcValueSDNode!");
  701. assert(!isa<MDNodeSDNode>(N) && "Bad MDNodeSDNode!");
  702. assert(!isa<RegisterSDNode>(N) && "Bad RegisterSDNode!");
  703. assert(!isa<BlockAddressSDNode>(N) && "Bad BlockAddressSDNode!");
  704. assert(!isa<EHLabelSDNode>(N) && "Bad EHLabelSDNode!");
  705. assert(!isa<ExternalSymbolSDNode>(N) && "Bad ExternalSymbolSDNode!");
  706. assert(!isa<CondCodeSDNode>(N) && "Bad CondCodeSDNode!");
  707. assert(!isa<CvtRndSatSDNode>(N) && "Bad CvtRndSatSDNode!");
  708. assert(!isa<VTSDNode>(N) && "Bad VTSDNode!");
  709. assert(!isa<MachineSDNode>(N) && "Bad MachineSDNode!");
  710. VerifyNodeCommon(N);
  711. }
  712. /// VerifyMachineNode - Sanity check the given MachineNode. Aborts if it is
  713. /// invalid.
  714. static void VerifyMachineNode(SDNode *N) {
  715. // The MachineNode allocators cannot be used to allocate nodes with fields
  716. // that are not present in a MachineNode!
  717. // Currently there are no such nodes.
  718. VerifyNodeCommon(N);
  719. }
  720. /// getEVTAlignment - Compute the default alignment value for the
  721. /// given type.
  722. ///
  723. unsigned SelectionDAG::getEVTAlignment(EVT VT) const {
  724. const Type *Ty = VT == MVT::iPTR ?
  725. PointerType::get(Type::getInt8Ty(*getContext()), 0) :
  726. VT.getTypeForEVT(*getContext());
  727. return TLI.getTargetData()->getABITypeAlignment(Ty);
  728. }
  729. // EntryNode could meaningfully have debug info if we can find it...
  730. SelectionDAG::SelectionDAG(const TargetMachine &tm)
  731. : TM(tm), TLI(*tm.getTargetLowering()), TSI(*tm.getSelectionDAGInfo()),
  732. EntryNode(ISD::EntryToken, DebugLoc(), getVTList(MVT::Other)),
  733. Root(getEntryNode()), Ordering(0) {
  734. AllNodes.push_back(&EntryNode);
  735. Ordering = new SDNodeOrdering();
  736. DbgInfo = new SDDbgInfo();
  737. }
  738. void SelectionDAG::init(MachineFunction &mf) {
  739. MF = &mf;
  740. Context = &mf.getFunction()->getContext();
  741. }
  742. SelectionDAG::~SelectionDAG() {
  743. allnodes_clear();
  744. delete Ordering;
  745. delete DbgInfo;
  746. }
  747. void SelectionDAG::allnodes_clear() {
  748. assert(&*AllNodes.begin() == &EntryNode);
  749. AllNodes.remove(AllNodes.begin());
  750. while (!AllNodes.empty())
  751. DeallocateNode(AllNodes.begin());
  752. }
  753. void SelectionDAG::clear() {
  754. allnodes_clear();
  755. OperandAllocator.Reset();
  756. CSEMap.clear();
  757. ExtendedValueTypeNodes.clear();
  758. ExternalSymbols.clear();
  759. TargetExternalSymbols.clear();
  760. std::fill(CondCodeNodes.begin(), CondCodeNodes.end(),
  761. static_cast<CondCodeSDNode*>(0));
  762. std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(),
  763. static_cast<SDNode*>(0));
  764. EntryNode.UseList = 0;
  765. AllNodes.push_back(&EntryNode);
  766. Root = getEntryNode();
  767. Ordering->clear();
  768. DbgInfo->clear();
  769. }
  770. SDValue SelectionDAG::getSExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
  771. return VT.bitsGT(Op.getValueType()) ?
  772. getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
  773. getNode(ISD::TRUNCATE, DL, VT, Op);
  774. }
  775. SDValue SelectionDAG::getZExtOrTrunc(SDValue Op, DebugLoc DL, EVT VT) {
  776. return VT.bitsGT(Op.getValueType()) ?
  777. getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
  778. getNode(ISD::TRUNCATE, DL, VT, Op);
  779. }
  780. SDValue SelectionDAG::getZeroExtendInReg(SDValue Op, DebugLoc DL, EVT VT) {
  781. assert(!VT.isVector() &&
  782. "getZeroExtendInReg should use the vector element type instead of "
  783. "the vector type!");
  784. if (Op.getValueType() == VT) return Op;
  785. unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
  786. APInt Imm = APInt::getLowBitsSet(BitWidth,
  787. VT.getSizeInBits());
  788. return getNode(ISD::AND, DL, Op.getValueType(), Op,
  789. getConstant(Imm, Op.getValueType()));
  790. }
  791. /// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
  792. ///
  793. SDValue SelectionDAG::getNOT(DebugLoc DL, SDValue Val, EVT VT) {
  794. EVT EltVT = VT.getScalarType();
  795. SDValue NegOne =
  796. getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
  797. return getNode(ISD::XOR, DL, VT, Val, NegOne);
  798. }
  799. SDValue SelectionDAG::getConstant(uint64_t Val, EVT VT, bool isT) {
  800. EVT EltVT = VT.getScalarType();
  801. assert((EltVT.getSizeInBits() >= 64 ||
  802. (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
  803. "getConstant with a uint64_t value that doesn't fit in the type!");
  804. return getConstant(APInt(EltVT.getSizeInBits(), Val), VT, isT);
  805. }
  806. SDValue SelectionDAG::getConstant(const APInt &Val, EVT VT, bool isT) {
  807. return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
  808. }
  809. SDValue SelectionDAG::getConstant(const ConstantInt &Val, EVT VT, bool isT) {
  810. assert(VT.isInteger() && "Cannot create FP integer constant!");
  811. EVT EltVT = VT.getScalarType();
  812. assert(Val.getBitWidth() == EltVT.getSizeInBits() &&
  813. "APInt size does not match type size!");
  814. unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
  815. FoldingSetNodeID ID;
  816. AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
  817. ID.AddPointer(&Val);
  818. void *IP = 0;
  819. SDNode *N = NULL;
  820. if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
  821. if (!VT.isVector())
  822. return SDValue(N, 0);
  823. if (!N) {
  824. N = new (NodeAllocator) ConstantSDNode(isT, &Val, EltVT);
  825. CSEMap.InsertNode(N, IP);
  826. AllNodes.push_back(N);
  827. }
  828. SDValue Result(N, 0);
  829. if (VT.isVector()) {
  830. SmallVector<SDValue, 8> Ops;
  831. Ops.assign(VT.getVectorNumElements(), Result);
  832. Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
  833. }
  834. return Result;
  835. }
  836. SDValue SelectionDAG::getIntPtrConstant(uint64_t Val, bool isTarget) {
  837. return getConstant(Val, TLI.getPointerTy(), isTarget);
  838. }
  839. SDValue SelectionDAG::getConstantFP(const APFloat& V, EVT VT, bool isTarget) {
  840. return getConstantFP(*ConstantFP::get(*getContext(), V), VT, isTarget);
  841. }
  842. SDValue SelectionDAG::getConstantFP(const ConstantFP& V, EVT VT, bool isTarget){
  843. assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
  844. EVT EltVT = VT.getScalarType();
  845. // Do the map lookup using the actual bit pattern for the floating point
  846. // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
  847. // we don't have issues with SNANs.
  848. unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
  849. FoldingSetNodeID ID;
  850. AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
  851. ID.AddPointer(&V);
  852. void *IP = 0;
  853. SDNode *N = NULL;
  854. if ((N = CSEMap.FindNodeOrInsertPos(ID, IP)))
  855. if (!VT.isVector())
  856. return SDValue(N, 0);
  857. if (!N) {
  858. N = new (NodeAllocator) ConstantFPSDNode(isTarget, &V, EltVT);
  859. CSEMap.InsertNode(N, IP);
  860. AllNodes.push_back(N);
  861. }
  862. SDValue Result(N, 0);
  863. if (VT.isVector()) {
  864. SmallVector<SDValue, 8> Ops;
  865. Ops.assign(VT.getVectorNumElements(), Result);
  866. // FIXME DebugLoc info might be appropriate here
  867. Result = getNode(ISD::BUILD_VECTOR, DebugLoc(), VT, &Ops[0], Ops.size());
  868. }
  869. return Result;
  870. }
  871. SDValue SelectionDAG::getConstantFP(double Val, EVT VT, bool isTarget) {
  872. EVT EltVT = VT.getScalarType();
  873. if (EltVT==MVT::f32)
  874. return getConstantFP(APFloat((float)Val), VT, isTarget);
  875. else if (EltVT==MVT::f64)
  876. return getConstantFP(APFloat(Val), VT, isTarget);
  877. else if (EltVT==MVT::f80 || EltVT==MVT::f128) {
  878. bool ignored;
  879. APFloat apf = APFloat(Val);
  880. apf.convert(*EVTToAPFloatSemantics(EltVT), APFloat::rmNearestTiesToEven,
  881. &ignored);
  882. return getConstantFP(apf, VT, isTarget);
  883. } else {
  884. assert(0 && "Unsupported type in getConstantFP");
  885. return SDValue();
  886. }
  887. }
  888. SDValue SelectionDAG::getGlobalAddress(const GlobalValue *GV, DebugLoc DL,
  889. EVT VT, int64_t Offset,
  890. bool isTargetGA,
  891. unsigned char TargetFlags) {
  892. assert((TargetFlags == 0 || isTargetGA) &&
  893. "Cannot set target flags on target-independent globals");
  894. // Truncate (with sign-extension) the offset value to the pointer size.
  895. EVT PTy = TLI.getPointerTy();
  896. unsigned BitWidth = PTy.getSizeInBits();
  897. if (BitWidth < 64)
  898. Offset = (Offset << (64 - BitWidth) >> (64 - BitWidth));
  899. const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
  900. if (!GVar) {
  901. // If GV is an alias then use the aliasee for determining thread-localness.
  902. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
  903. GVar = dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false));
  904. }
  905. unsigned Opc;
  906. if (GVar && GVar->isThreadLocal())
  907. Opc = isTargetGA ? ISD::TargetGlobalTLSAddress : ISD::GlobalTLSAddress;
  908. else
  909. Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
  910. FoldingSetNodeID ID;
  911. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  912. ID.AddPointer(GV);
  913. ID.AddInteger(Offset);
  914. ID.AddInteger(TargetFlags);
  915. void *IP = 0;
  916. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  917. return SDValue(E, 0);
  918. SDNode *N = new (NodeAllocator) GlobalAddressSDNode(Opc, DL, GV, VT,
  919. Offset, TargetFlags);
  920. CSEMap.InsertNode(N, IP);
  921. AllNodes.push_back(N);
  922. return SDValue(N, 0);
  923. }
  924. SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
  925. unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
  926. FoldingSetNodeID ID;
  927. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  928. ID.AddInteger(FI);
  929. void *IP = 0;
  930. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  931. return SDValue(E, 0);
  932. SDNode *N = new (NodeAllocator) FrameIndexSDNode(FI, VT, isTarget);
  933. CSEMap.InsertNode(N, IP);
  934. AllNodes.push_back(N);
  935. return SDValue(N, 0);
  936. }
  937. SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
  938. unsigned char TargetFlags) {
  939. assert((TargetFlags == 0 || isTarget) &&
  940. "Cannot set target flags on target-independent jump tables");
  941. unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
  942. FoldingSetNodeID ID;
  943. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  944. ID.AddInteger(JTI);
  945. ID.AddInteger(TargetFlags);
  946. void *IP = 0;
  947. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  948. return SDValue(E, 0);
  949. SDNode *N = new (NodeAllocator) JumpTableSDNode(JTI, VT, isTarget,
  950. TargetFlags);
  951. CSEMap.InsertNode(N, IP);
  952. AllNodes.push_back(N);
  953. return SDValue(N, 0);
  954. }
  955. SDValue SelectionDAG::getConstantPool(const Constant *C, EVT VT,
  956. unsigned Alignment, int Offset,
  957. bool isTarget,
  958. unsigned char TargetFlags) {
  959. assert((TargetFlags == 0 || isTarget) &&
  960. "Cannot set target flags on target-independent globals");
  961. if (Alignment == 0)
  962. Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
  963. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  964. FoldingSetNodeID ID;
  965. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  966. ID.AddInteger(Alignment);
  967. ID.AddInteger(Offset);
  968. ID.AddPointer(C);
  969. ID.AddInteger(TargetFlags);
  970. void *IP = 0;
  971. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  972. return SDValue(E, 0);
  973. SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
  974. Alignment, TargetFlags);
  975. CSEMap.InsertNode(N, IP);
  976. AllNodes.push_back(N);
  977. return SDValue(N, 0);
  978. }
  979. SDValue SelectionDAG::getConstantPool(MachineConstantPoolValue *C, EVT VT,
  980. unsigned Alignment, int Offset,
  981. bool isTarget,
  982. unsigned char TargetFlags) {
  983. assert((TargetFlags == 0 || isTarget) &&
  984. "Cannot set target flags on target-independent globals");
  985. if (Alignment == 0)
  986. Alignment = TLI.getTargetData()->getPrefTypeAlignment(C->getType());
  987. unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
  988. FoldingSetNodeID ID;
  989. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  990. ID.AddInteger(Alignment);
  991. ID.AddInteger(Offset);
  992. C->AddSelectionDAGCSEId(ID);
  993. ID.AddInteger(TargetFlags);
  994. void *IP = 0;
  995. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  996. return SDValue(E, 0);
  997. SDNode *N = new (NodeAllocator) ConstantPoolSDNode(isTarget, C, VT, Offset,
  998. Alignment, TargetFlags);
  999. CSEMap.InsertNode(N, IP);
  1000. AllNodes.push_back(N);
  1001. return SDValue(N, 0);
  1002. }
  1003. SDValue SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
  1004. FoldingSetNodeID ID;
  1005. AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), 0, 0);
  1006. ID.AddPointer(MBB);
  1007. void *IP = 0;
  1008. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1009. return SDValue(E, 0);
  1010. SDNode *N = new (NodeAllocator) BasicBlockSDNode(MBB);
  1011. CSEMap.InsertNode(N, IP);
  1012. AllNodes.push_back(N);
  1013. return SDValue(N, 0);
  1014. }
  1015. SDValue SelectionDAG::getValueType(EVT VT) {
  1016. if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
  1017. ValueTypeNodes.size())
  1018. ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
  1019. SDNode *&N = VT.isExtended() ?
  1020. ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
  1021. if (N) return SDValue(N, 0);
  1022. N = new (NodeAllocator) VTSDNode(VT);
  1023. AllNodes.push_back(N);
  1024. return SDValue(N, 0);
  1025. }
  1026. SDValue SelectionDAG::getExternalSymbol(const char *Sym, EVT VT) {
  1027. SDNode *&N = ExternalSymbols[Sym];
  1028. if (N) return SDValue(N, 0);
  1029. N = new (NodeAllocator) ExternalSymbolSDNode(false, Sym, 0, VT);
  1030. AllNodes.push_back(N);
  1031. return SDValue(N, 0);
  1032. }
  1033. SDValue SelectionDAG::getTargetExternalSymbol(const char *Sym, EVT VT,
  1034. unsigned char TargetFlags) {
  1035. SDNode *&N =
  1036. TargetExternalSymbols[std::pair<std::string,unsigned char>(Sym,
  1037. TargetFlags)];
  1038. if (N) return SDValue(N, 0);
  1039. N = new (NodeAllocator) ExternalSymbolSDNode(true, Sym, TargetFlags, VT);
  1040. AllNodes.push_back(N);
  1041. return SDValue(N, 0);
  1042. }
  1043. SDValue SelectionDAG::getCondCode(ISD::CondCode Cond) {
  1044. if ((unsigned)Cond >= CondCodeNodes.size())
  1045. CondCodeNodes.resize(Cond+1);
  1046. if (CondCodeNodes[Cond] == 0) {
  1047. CondCodeSDNode *N = new (NodeAllocator) CondCodeSDNode(Cond);
  1048. CondCodeNodes[Cond] = N;
  1049. AllNodes.push_back(N);
  1050. }
  1051. return SDValue(CondCodeNodes[Cond], 0);
  1052. }
  1053. // commuteShuffle - swaps the values of N1 and N2, and swaps all indices in
  1054. // the shuffle mask M that point at N1 to point at N2, and indices that point
  1055. // N2 to point at N1.
  1056. static void commuteShuffle(SDValue &N1, SDValue &N2, SmallVectorImpl<int> &M) {
  1057. std::swap(N1, N2);
  1058. int NElts = M.size();
  1059. for (int i = 0; i != NElts; ++i) {
  1060. if (M[i] >= NElts)
  1061. M[i] -= NElts;
  1062. else if (M[i] >= 0)
  1063. M[i] += NElts;
  1064. }
  1065. }
  1066. SDValue SelectionDAG::getVectorShuffle(EVT VT, DebugLoc dl, SDValue N1,
  1067. SDValue N2, const int *Mask) {
  1068. assert(N1.getValueType() == N2.getValueType() && "Invalid VECTOR_SHUFFLE");
  1069. assert(VT.isVector() && N1.getValueType().isVector() &&
  1070. "Vector Shuffle VTs must be a vectors");
  1071. assert(VT.getVectorElementType() == N1.getValueType().getVectorElementType()
  1072. && "Vector Shuffle VTs must have same element type");
  1073. // Canonicalize shuffle undef, undef -> undef
  1074. if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() == ISD::UNDEF)
  1075. return getUNDEF(VT);
  1076. // Validate that all indices in Mask are within the range of the elements
  1077. // input to the shuffle.
  1078. unsigned NElts = VT.getVectorNumElements();
  1079. SmallVector<int, 8> MaskVec;
  1080. for (unsigned i = 0; i != NElts; ++i) {
  1081. assert(Mask[i] < (int)(NElts * 2) && "Index out of range");
  1082. MaskVec.push_back(Mask[i]);
  1083. }
  1084. // Canonicalize shuffle v, v -> v, undef
  1085. if (N1 == N2) {
  1086. N2 = getUNDEF(VT);
  1087. for (unsigned i = 0; i != NElts; ++i)
  1088. if (MaskVec[i] >= (int)NElts) MaskVec[i] -= NElts;
  1089. }
  1090. // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
  1091. if (N1.getOpcode() == ISD::UNDEF)
  1092. commuteShuffle(N1, N2, MaskVec);
  1093. // Canonicalize all index into lhs, -> shuffle lhs, undef
  1094. // Canonicalize all index into rhs, -> shuffle rhs, undef
  1095. bool AllLHS = true, AllRHS = true;
  1096. bool N2Undef = N2.getOpcode() == ISD::UNDEF;
  1097. for (unsigned i = 0; i != NElts; ++i) {
  1098. if (MaskVec[i] >= (int)NElts) {
  1099. if (N2Undef)
  1100. MaskVec[i] = -1;
  1101. else
  1102. AllLHS = false;
  1103. } else if (MaskVec[i] >= 0) {
  1104. AllRHS = false;
  1105. }
  1106. }
  1107. if (AllLHS && AllRHS)
  1108. return getUNDEF(VT);
  1109. if (AllLHS && !N2Undef)
  1110. N2 = getUNDEF(VT);
  1111. if (AllRHS) {
  1112. N1 = getUNDEF(VT);
  1113. commuteShuffle(N1, N2, MaskVec);
  1114. }
  1115. // If Identity shuffle, or all shuffle in to undef, return that node.
  1116. bool AllUndef = true;
  1117. bool Identity = true;
  1118. for (unsigned i = 0; i != NElts; ++i) {
  1119. if (MaskVec[i] >= 0 && MaskVec[i] != (int)i) Identity = false;
  1120. if (MaskVec[i] >= 0) AllUndef = false;
  1121. }
  1122. if (Identity && NElts == N1.getValueType().getVectorNumElements())
  1123. return N1;
  1124. if (AllUndef)
  1125. return getUNDEF(VT);
  1126. FoldingSetNodeID ID;
  1127. SDValue Ops[2] = { N1, N2 };
  1128. AddNodeIDNode(ID, ISD::VECTOR_SHUFFLE, getVTList(VT), Ops, 2);
  1129. for (unsigned i = 0; i != NElts; ++i)
  1130. ID.AddInteger(MaskVec[i]);
  1131. void* IP = 0;
  1132. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1133. return SDValue(E, 0);
  1134. // Allocate the mask array for the node out of the BumpPtrAllocator, since
  1135. // SDNode doesn't have access to it. This memory will be "leaked" when
  1136. // the node is deallocated, but recovered when the NodeAllocator is released.
  1137. int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
  1138. memcpy(MaskAlloc, &MaskVec[0], NElts * sizeof(int));
  1139. ShuffleVectorSDNode *N =
  1140. new (NodeAllocator) ShuffleVectorSDNode(VT, dl, N1, N2, MaskAlloc);
  1141. CSEMap.InsertNode(N, IP);
  1142. AllNodes.push_back(N);
  1143. return SDValue(N, 0);
  1144. }
  1145. SDValue SelectionDAG::getConvertRndSat(EVT VT, DebugLoc dl,
  1146. SDValue Val, SDValue DTy,
  1147. SDValue STy, SDValue Rnd, SDValue Sat,
  1148. ISD::CvtCode Code) {
  1149. // If the src and dest types are the same and the conversion is between
  1150. // integer types of the same sign or two floats, no conversion is necessary.
  1151. if (DTy == STy &&
  1152. (Code == ISD::CVT_UU || Code == ISD::CVT_SS || Code == ISD::CVT_FF))
  1153. return Val;
  1154. FoldingSetNodeID ID;
  1155. SDValue Ops[] = { Val, DTy, STy, Rnd, Sat };
  1156. AddNodeIDNode(ID, ISD::CONVERT_RNDSAT, getVTList(VT), &Ops[0], 5);
  1157. void* IP = 0;
  1158. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1159. return SDValue(E, 0);
  1160. CvtRndSatSDNode *N = new (NodeAllocator) CvtRndSatSDNode(VT, dl, Ops, 5,
  1161. Code);
  1162. CSEMap.InsertNode(N, IP);
  1163. AllNodes.push_back(N);
  1164. return SDValue(N, 0);
  1165. }
  1166. SDValue SelectionDAG::getRegister(unsigned RegNo, EVT VT) {
  1167. FoldingSetNodeID ID;
  1168. AddNodeIDNode(ID, ISD::Register, getVTList(VT), 0, 0);
  1169. ID.AddInteger(RegNo);
  1170. void *IP = 0;
  1171. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1172. return SDValue(E, 0);
  1173. SDNode *N = new (NodeAllocator) RegisterSDNode(RegNo, VT);
  1174. CSEMap.InsertNode(N, IP);
  1175. AllNodes.push_back(N);
  1176. return SDValue(N, 0);
  1177. }
  1178. SDValue SelectionDAG::getEHLabel(DebugLoc dl, SDValue Root, MCSymbol *Label) {
  1179. FoldingSetNodeID ID;
  1180. SDValue Ops[] = { Root };
  1181. AddNodeIDNode(ID, ISD::EH_LABEL, getVTList(MVT::Other), &Ops[0], 1);
  1182. ID.AddPointer(Label);
  1183. void *IP = 0;
  1184. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1185. return SDValue(E, 0);
  1186. SDNode *N = new (NodeAllocator) EHLabelSDNode(dl, Root, Label);
  1187. CSEMap.InsertNode(N, IP);
  1188. AllNodes.push_back(N);
  1189. return SDValue(N, 0);
  1190. }
  1191. SDValue SelectionDAG::getBlockAddress(const BlockAddress *BA, EVT VT,
  1192. bool isTarget,
  1193. unsigned char TargetFlags) {
  1194. unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
  1195. FoldingSetNodeID ID;
  1196. AddNodeIDNode(ID, Opc, getVTList(VT), 0, 0);
  1197. ID.AddPointer(BA);
  1198. ID.AddInteger(TargetFlags);
  1199. void *IP = 0;
  1200. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1201. return SDValue(E, 0);
  1202. SDNode *N = new (NodeAllocator) BlockAddressSDNode(Opc, VT, BA, TargetFlags);
  1203. CSEMap.InsertNode(N, IP);
  1204. AllNodes.push_back(N);
  1205. return SDValue(N, 0);
  1206. }
  1207. SDValue SelectionDAG::getSrcValue(const Value *V) {
  1208. assert((!V || V->getType()->isPointerTy()) &&
  1209. "SrcValue is not a pointer?");
  1210. FoldingSetNodeID ID;
  1211. AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), 0, 0);
  1212. ID.AddPointer(V);
  1213. void *IP = 0;
  1214. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1215. return SDValue(E, 0);
  1216. SDNode *N = new (NodeAllocator) SrcValueSDNode(V);
  1217. CSEMap.InsertNode(N, IP);
  1218. AllNodes.push_back(N);
  1219. return SDValue(N, 0);
  1220. }
  1221. /// getMDNode - Return an MDNodeSDNode which holds an MDNode.
  1222. SDValue SelectionDAG::getMDNode(const MDNode *MD) {
  1223. FoldingSetNodeID ID;
  1224. AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), 0, 0);
  1225. ID.AddPointer(MD);
  1226. void *IP = 0;
  1227. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  1228. return SDValue(E, 0);
  1229. SDNode *N = new (NodeAllocator) MDNodeSDNode(MD);
  1230. CSEMap.InsertNode(N, IP);
  1231. AllNodes.push_back(N);
  1232. return SDValue(N, 0);
  1233. }
  1234. /// getShiftAmountOperand - Return the specified value casted to
  1235. /// the target's desired shift amount type.
  1236. SDValue SelectionDAG::getShiftAmountOperand(SDValue Op) {
  1237. EVT OpTy = Op.getValueType();
  1238. MVT ShTy = TLI.getShiftAmountTy();
  1239. if (OpTy == ShTy || OpTy.isVector()) return Op;
  1240. ISD::NodeType Opcode = OpTy.bitsGT(ShTy) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
  1241. return getNode(Opcode, Op.getDebugLoc(), ShTy, Op);
  1242. }
  1243. /// CreateStackTemporary - Create a stack temporary, suitable for holding the
  1244. /// specified value type.
  1245. SDValue SelectionDAG::CreateStackTemporary(EVT VT, unsigned minAlign) {
  1246. MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
  1247. unsigned ByteSize = VT.getStoreSize();
  1248. const Type *Ty = VT.getTypeForEVT(*getContext());
  1249. unsigned StackAlign =
  1250. std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), minAlign);
  1251. int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
  1252. return getFrameIndex(FrameIdx, TLI.getPointerTy());
  1253. }
  1254. /// CreateStackTemporary - Create a stack temporary suitable for holding
  1255. /// either of the specified value types.
  1256. SDValue SelectionDAG::CreateStackTemporary(EVT VT1, EVT VT2) {
  1257. unsigned Bytes = std::max(VT1.getStoreSizeInBits(),
  1258. VT2.getStoreSizeInBits())/8;
  1259. const Type *Ty1 = VT1.getTypeForEVT(*getContext());
  1260. const Type *Ty2 = VT2.getTypeForEVT(*getContext());
  1261. const TargetData *TD = TLI.getTargetData();
  1262. unsigned Align = std::max(TD->getPrefTypeAlignment(Ty1),
  1263. TD->getPrefTypeAlignment(Ty2));
  1264. MachineFrameInfo *FrameInfo = getMachineFunction().getFrameInfo();
  1265. int FrameIdx = FrameInfo->CreateStackObject(Bytes, Align, false);
  1266. return getFrameIndex(FrameIdx, TLI.getPointerTy());
  1267. }
  1268. SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1,
  1269. SDValue N2, ISD::CondCode Cond, DebugLoc dl) {
  1270. // These setcc operations always fold.
  1271. switch (Cond) {
  1272. default: break;
  1273. case ISD::SETFALSE:
  1274. case ISD::SETFALSE2: return getConstant(0, VT);
  1275. case ISD::SETTRUE:
  1276. case ISD::SETTRUE2: return getConstant(1, VT);
  1277. case ISD::SETOEQ:
  1278. case ISD::SETOGT:
  1279. case ISD::SETOGE:
  1280. case ISD::SETOLT:
  1281. case ISD::SETOLE:
  1282. case ISD::SETONE:
  1283. case ISD::SETO:
  1284. case ISD::SETUO:
  1285. case ISD::SETUEQ:
  1286. case ISD::SETUNE:
  1287. assert(!N1.getValueType().isInteger() && "Illegal setcc for integer!");
  1288. break;
  1289. }
  1290. if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode())) {
  1291. const APInt &C2 = N2C->getAPIntValue();
  1292. if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode())) {
  1293. const APInt &C1 = N1C->getAPIntValue();
  1294. switch (Cond) {
  1295. default: llvm_unreachable("Unknown integer setcc!");
  1296. case ISD::SETEQ: return getConstant(C1 == C2, VT);
  1297. case ISD::SETNE: return getConstant(C1 != C2, VT);
  1298. case ISD::SETULT: return getConstant(C1.ult(C2), VT);
  1299. case ISD::SETUGT: return getConstant(C1.ugt(C2), VT);
  1300. case ISD::SETULE: return getConstant(C1.ule(C2), VT);
  1301. case ISD::SETUGE: return getConstant(C1.uge(C2), VT);
  1302. case ISD::SETLT: return getConstant(C1.slt(C2), VT);
  1303. case ISD::SETGT: return getConstant(C1.sgt(C2), VT);
  1304. case ISD::SETLE: return getConstant(C1.sle(C2), VT);
  1305. case ISD::SETGE: return getConstant(C1.sge(C2), VT);
  1306. }
  1307. }
  1308. }
  1309. if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.getNode())) {
  1310. if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.getNode())) {
  1311. // No compile time operations on this type yet.
  1312. if (N1C->getValueType(0) == MVT::ppcf128)
  1313. return SDValue();
  1314. APFloat::cmpResult R = N1C->getValueAPF().compare(N2C->getValueAPF());
  1315. switch (Cond) {
  1316. default: break;
  1317. case ISD::SETEQ: if (R==APFloat::cmpUnordered)
  1318. return getUNDEF(VT);
  1319. // fall through
  1320. case ISD::SETOEQ: return getConstant(R==APFloat::cmpEqual, VT);
  1321. case ISD::SETNE: if (R==APFloat::cmpUnordered)
  1322. return getUNDEF(VT);
  1323. // fall through
  1324. case ISD::SETONE: return getConstant(R==APFloat::cmpGreaterThan ||
  1325. R==APFloat::cmpLessThan, VT);
  1326. case ISD::SETLT: if (R==APFloat::cmpUnordered)
  1327. return getUNDEF(VT);
  1328. // fall through
  1329. case ISD::SETOLT: return getConstant(R==APFloat::cmpLessThan, VT);
  1330. case ISD::SETGT: if (R==APFloat::cmpUnordered)
  1331. return getUNDEF(VT);
  1332. // fall through
  1333. case ISD::SETOGT: return getConstant(R==APFloat::cmpGreaterThan, VT);
  1334. case ISD::SETLE: if (R==APFloat::cmpUnordered)
  1335. return getUNDEF(VT);
  1336. // fall through
  1337. case ISD::SETOLE: return getConstant(R==APFloat::cmpLessThan ||
  1338. R==APFloat::cmpEqual, VT);
  1339. case ISD::SETGE: if (R==APFloat::cmpUnordered)
  1340. return getUNDEF(VT);
  1341. // fall through
  1342. case ISD::SETOGE: return getConstant(R==APFloat::cmpGreaterThan ||
  1343. R==APFloat::cmpEqual, VT);
  1344. case ISD::SETO: return getConstant(R!=APFloat::cmpUnordered, VT);
  1345. case ISD::SETUO: return getConstant(R==APFloat::cmpUnordered, VT);
  1346. case ISD::SETUEQ: return getConstant(R==APFloat::cmpUnordered ||
  1347. R==APFloat::cmpEqual, VT);
  1348. case ISD::SETUNE: return getConstant(R!=APFloat::cmpEqual, VT);
  1349. case ISD::SETULT: return getConstant(R==APFloat::cmpUnordered ||
  1350. R==APFloat::cmpLessThan, VT);
  1351. case ISD::SETUGT: return getConstant(R==APFloat::cmpGreaterThan ||
  1352. R==APFloat::cmpUnordered, VT);
  1353. case ISD::SETULE: return getConstant(R!=APFloat::cmpGreaterThan, VT);
  1354. case ISD::SETUGE: return getConstant(R!=APFloat::cmpLessThan, VT);
  1355. }
  1356. } else {
  1357. // Ensure that the constant occurs on the RHS.
  1358. return getSetCC(dl, VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
  1359. }
  1360. }
  1361. // Could not fold it.
  1362. return SDValue();
  1363. }
  1364. /// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
  1365. /// use this predicate to simplify operations downstream.
  1366. bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {
  1367. // This predicate is not safe for vector operations.
  1368. if (Op.getValueType().isVector())
  1369. return false;
  1370. unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
  1371. return MaskedValueIsZero(Op, APInt::getSignBit(BitWidth), Depth);
  1372. }
  1373. /// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
  1374. /// this predicate to simplify operations downstream. Mask is known to be zero
  1375. /// for bits that V cannot have.
  1376. bool SelectionDAG::MaskedValueIsZero(SDValue Op, const APInt &Mask,
  1377. unsigned Depth) const {
  1378. APInt KnownZero, KnownOne;
  1379. ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
  1380. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1381. return (KnownZero & Mask) == Mask;
  1382. }
  1383. /// ComputeMaskedBits - Determine which of the bits specified in Mask are
  1384. /// known to be either zero or one and return them in the KnownZero/KnownOne
  1385. /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
  1386. /// processing.
  1387. void SelectionDAG::ComputeMaskedBits(SDValue Op, const APInt &Mask,
  1388. APInt &KnownZero, APInt &KnownOne,
  1389. unsigned Depth) const {
  1390. unsigned BitWidth = Mask.getBitWidth();
  1391. assert(BitWidth == Op.getValueType().getScalarType().getSizeInBits() &&
  1392. "Mask size mismatches value type size!");
  1393. KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything.
  1394. if (Depth == 6 || Mask == 0)
  1395. return; // Limit search depth.
  1396. APInt KnownZero2, KnownOne2;
  1397. switch (Op.getOpcode()) {
  1398. case ISD::Constant:
  1399. // We know all of the bits for a constant!
  1400. KnownOne = cast<ConstantSDNode>(Op)->getAPIntValue() & Mask;
  1401. KnownZero = ~KnownOne & Mask;
  1402. return;
  1403. case ISD::AND:
  1404. // If either the LHS or the RHS are Zero, the result is zero.
  1405. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1406. ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownZero,
  1407. KnownZero2, KnownOne2, Depth+1);
  1408. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1409. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1410. // Output known-1 bits are only known if set in both the LHS & RHS.
  1411. KnownOne &= KnownOne2;
  1412. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  1413. KnownZero |= KnownZero2;
  1414. return;
  1415. case ISD::OR:
  1416. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1417. ComputeMaskedBits(Op.getOperand(0), Mask & ~KnownOne,
  1418. KnownZero2, KnownOne2, Depth+1);
  1419. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1420. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1421. // Output known-0 bits are only known if clear in both the LHS & RHS.
  1422. KnownZero &= KnownZero2;
  1423. // Output known-1 are known to be set if set in either the LHS | RHS.
  1424. KnownOne |= KnownOne2;
  1425. return;
  1426. case ISD::XOR: {
  1427. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1428. ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2, Depth+1);
  1429. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1430. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1431. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  1432. APInt KnownZeroOut = (KnownZero & KnownZero2) | (KnownOne & KnownOne2);
  1433. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  1434. KnownOne = (KnownZero & KnownOne2) | (KnownOne & KnownZero2);
  1435. KnownZero = KnownZeroOut;
  1436. return;
  1437. }
  1438. case ISD::MUL: {
  1439. APInt Mask2 = APInt::getAllOnesValue(BitWidth);
  1440. ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero, KnownOne, Depth+1);
  1441. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
  1442. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1443. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1444. // If low bits are zero in either operand, output low known-0 bits.
  1445. // Also compute a conserative estimate for high known-0 bits.
  1446. // More trickiness is possible, but this is sufficient for the
  1447. // interesting case of alignment computation.
  1448. KnownOne.clear();
  1449. unsigned TrailZ = KnownZero.countTrailingOnes() +
  1450. KnownZero2.countTrailingOnes();
  1451. unsigned LeadZ = std::max(KnownZero.countLeadingOnes() +
  1452. KnownZero2.countLeadingOnes(),
  1453. BitWidth) - BitWidth;
  1454. TrailZ = std::min(TrailZ, BitWidth);
  1455. LeadZ = std::min(LeadZ, BitWidth);
  1456. KnownZero = APInt::getLowBitsSet(BitWidth, TrailZ) |
  1457. APInt::getHighBitsSet(BitWidth, LeadZ);
  1458. KnownZero &= Mask;
  1459. return;
  1460. }
  1461. case ISD::UDIV: {
  1462. // For the purposes of computing leading zeros we can conservatively
  1463. // treat a udiv as a logical right shift by the power of 2 known to
  1464. // be less than the denominator.
  1465. APInt AllOnes = APInt::getAllOnesValue(BitWidth);
  1466. ComputeMaskedBits(Op.getOperand(0),
  1467. AllOnes, KnownZero2, KnownOne2, Depth+1);
  1468. unsigned LeadZ = KnownZero2.countLeadingOnes();
  1469. KnownOne2.clear();
  1470. KnownZero2.clear();
  1471. ComputeMaskedBits(Op.getOperand(1),
  1472. AllOnes, KnownZero2, KnownOne2, Depth+1);
  1473. unsigned RHSUnknownLeadingOnes = KnownOne2.countLeadingZeros();
  1474. if (RHSUnknownLeadingOnes != BitWidth)
  1475. LeadZ = std::min(BitWidth,
  1476. LeadZ + BitWidth - RHSUnknownLeadingOnes - 1);
  1477. KnownZero = APInt::getHighBitsSet(BitWidth, LeadZ) & Mask;
  1478. return;
  1479. }
  1480. case ISD::SELECT:
  1481. ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero, KnownOne, Depth+1);
  1482. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero2, KnownOne2, Depth+1);
  1483. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1484. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1485. // Only known if known in both the LHS and RHS.
  1486. KnownOne &= KnownOne2;
  1487. KnownZero &= KnownZero2;
  1488. return;
  1489. case ISD::SELECT_CC:
  1490. ComputeMaskedBits(Op.getOperand(3), Mask, KnownZero, KnownOne, Depth+1);
  1491. ComputeMaskedBits(Op.getOperand(2), Mask, KnownZero2, KnownOne2, Depth+1);
  1492. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1493. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1494. // Only known if known in both the LHS and RHS.
  1495. KnownOne &= KnownOne2;
  1496. KnownZero &= KnownZero2;
  1497. return;
  1498. case ISD::SADDO:
  1499. case ISD::UADDO:
  1500. case ISD::SSUBO:
  1501. case ISD::USUBO:
  1502. case ISD::SMULO:
  1503. case ISD::UMULO:
  1504. if (Op.getResNo() != 1)
  1505. return;
  1506. // The boolean result conforms to getBooleanContents. Fall through.
  1507. case ISD::SETCC:
  1508. // If we know the result of a setcc has the top bits zero, use this info.
  1509. if (TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent &&
  1510. BitWidth > 1)
  1511. KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
  1512. return;
  1513. case ISD::SHL:
  1514. // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
  1515. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1516. unsigned ShAmt = SA->getZExtValue();
  1517. // If the shift count is an invalid immediate, don't do anything.
  1518. if (ShAmt >= BitWidth)
  1519. return;
  1520. ComputeMaskedBits(Op.getOperand(0), Mask.lshr(ShAmt),
  1521. KnownZero, KnownOne, Depth+1);
  1522. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1523. KnownZero <<= ShAmt;
  1524. KnownOne <<= ShAmt;
  1525. // low bits known zero.
  1526. KnownZero |= APInt::getLowBitsSet(BitWidth, ShAmt);
  1527. }
  1528. return;
  1529. case ISD::SRL:
  1530. // (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
  1531. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1532. unsigned ShAmt = SA->getZExtValue();
  1533. // If the shift count is an invalid immediate, don't do anything.
  1534. if (ShAmt >= BitWidth)
  1535. return;
  1536. ComputeMaskedBits(Op.getOperand(0), (Mask << ShAmt),
  1537. KnownZero, KnownOne, Depth+1);
  1538. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1539. KnownZero = KnownZero.lshr(ShAmt);
  1540. KnownOne = KnownOne.lshr(ShAmt);
  1541. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
  1542. KnownZero |= HighBits; // High bits known zero.
  1543. }
  1544. return;
  1545. case ISD::SRA:
  1546. if (ConstantSDNode *SA = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1547. unsigned ShAmt = SA->getZExtValue();
  1548. // If the shift count is an invalid immediate, don't do anything.
  1549. if (ShAmt >= BitWidth)
  1550. return;
  1551. APInt InDemandedMask = (Mask << ShAmt);
  1552. // If any of the demanded bits are produced by the sign extension, we also
  1553. // demand the input sign bit.
  1554. APInt HighBits = APInt::getHighBitsSet(BitWidth, ShAmt) & Mask;
  1555. if (HighBits.getBoolValue())
  1556. InDemandedMask |= APInt::getSignBit(BitWidth);
  1557. ComputeMaskedBits(Op.getOperand(0), InDemandedMask, KnownZero, KnownOne,
  1558. Depth+1);
  1559. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1560. KnownZero = KnownZero.lshr(ShAmt);
  1561. KnownOne = KnownOne.lshr(ShAmt);
  1562. // Handle the sign bits.
  1563. APInt SignBit = APInt::getSignBit(BitWidth);
  1564. SignBit = SignBit.lshr(ShAmt); // Adjust to where it is now in the mask.
  1565. if (KnownZero.intersects(SignBit)) {
  1566. KnownZero |= HighBits; // New bits are known zero.
  1567. } else if (KnownOne.intersects(SignBit)) {
  1568. KnownOne |= HighBits; // New bits are known one.
  1569. }
  1570. }
  1571. return;
  1572. case ISD::SIGN_EXTEND_INREG: {
  1573. EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1574. unsigned EBits = EVT.getScalarType().getSizeInBits();
  1575. // Sign extension. Compute the demanded bits in the result that are not
  1576. // present in the input.
  1577. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - EBits) & Mask;
  1578. APInt InSignBit = APInt::getSignBit(EBits);
  1579. APInt InputDemandedBits = Mask & APInt::getLowBitsSet(BitWidth, EBits);
  1580. // If the sign extended bits are demanded, we know that the sign
  1581. // bit is demanded.
  1582. InSignBit.zext(BitWidth);
  1583. if (NewBits.getBoolValue())
  1584. InputDemandedBits |= InSignBit;
  1585. ComputeMaskedBits(Op.getOperand(0), InputDemandedBits,
  1586. KnownZero, KnownOne, Depth+1);
  1587. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1588. // If the sign bit of the input is known set or clear, then we know the
  1589. // top bits of the result.
  1590. if (KnownZero.intersects(InSignBit)) { // Input sign bit known clear
  1591. KnownZero |= NewBits;
  1592. KnownOne &= ~NewBits;
  1593. } else if (KnownOne.intersects(InSignBit)) { // Input sign bit known set
  1594. KnownOne |= NewBits;
  1595. KnownZero &= ~NewBits;
  1596. } else { // Input sign bit unknown
  1597. KnownZero &= ~NewBits;
  1598. KnownOne &= ~NewBits;
  1599. }
  1600. return;
  1601. }
  1602. case ISD::CTTZ:
  1603. case ISD::CTLZ:
  1604. case ISD::CTPOP: {
  1605. unsigned LowBits = Log2_32(BitWidth)+1;
  1606. KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - LowBits);
  1607. KnownOne.clear();
  1608. return;
  1609. }
  1610. case ISD::LOAD: {
  1611. if (ISD::isZEXTLoad(Op.getNode())) {
  1612. LoadSDNode *LD = cast<LoadSDNode>(Op);
  1613. EVT VT = LD->getMemoryVT();
  1614. unsigned MemBits = VT.getScalarType().getSizeInBits();
  1615. KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits) & Mask;
  1616. }
  1617. return;
  1618. }
  1619. case ISD::ZERO_EXTEND: {
  1620. EVT InVT = Op.getOperand(0).getValueType();
  1621. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1622. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
  1623. APInt InMask = Mask;
  1624. InMask.trunc(InBits);
  1625. KnownZero.trunc(InBits);
  1626. KnownOne.trunc(InBits);
  1627. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1628. KnownZero.zext(BitWidth);
  1629. KnownOne.zext(BitWidth);
  1630. KnownZero |= NewBits;
  1631. return;
  1632. }
  1633. case ISD::SIGN_EXTEND: {
  1634. EVT InVT = Op.getOperand(0).getValueType();
  1635. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1636. APInt InSignBit = APInt::getSignBit(InBits);
  1637. APInt NewBits = APInt::getHighBitsSet(BitWidth, BitWidth - InBits) & Mask;
  1638. APInt InMask = Mask;
  1639. InMask.trunc(InBits);
  1640. // If any of the sign extended bits are demanded, we know that the sign
  1641. // bit is demanded. Temporarily set this bit in the mask for our callee.
  1642. if (NewBits.getBoolValue())
  1643. InMask |= InSignBit;
  1644. KnownZero.trunc(InBits);
  1645. KnownOne.trunc(InBits);
  1646. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1647. // Note if the sign bit is known to be zero or one.
  1648. bool SignBitKnownZero = KnownZero.isNegative();
  1649. bool SignBitKnownOne = KnownOne.isNegative();
  1650. assert(!(SignBitKnownZero && SignBitKnownOne) &&
  1651. "Sign bit can't be known to be both zero and one!");
  1652. // If the sign bit wasn't actually demanded by our caller, we don't
  1653. // want it set in the KnownZero and KnownOne result values. Reset the
  1654. // mask and reapply it to the result values.
  1655. InMask = Mask;
  1656. InMask.trunc(InBits);
  1657. KnownZero &= InMask;
  1658. KnownOne &= InMask;
  1659. KnownZero.zext(BitWidth);
  1660. KnownOne.zext(BitWidth);
  1661. // If the sign bit is known zero or one, the top bits match.
  1662. if (SignBitKnownZero)
  1663. KnownZero |= NewBits;
  1664. else if (SignBitKnownOne)
  1665. KnownOne |= NewBits;
  1666. return;
  1667. }
  1668. case ISD::ANY_EXTEND: {
  1669. EVT InVT = Op.getOperand(0).getValueType();
  1670. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1671. APInt InMask = Mask;
  1672. InMask.trunc(InBits);
  1673. KnownZero.trunc(InBits);
  1674. KnownOne.trunc(InBits);
  1675. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1676. KnownZero.zext(BitWidth);
  1677. KnownOne.zext(BitWidth);
  1678. return;
  1679. }
  1680. case ISD::TRUNCATE: {
  1681. EVT InVT = Op.getOperand(0).getValueType();
  1682. unsigned InBits = InVT.getScalarType().getSizeInBits();
  1683. APInt InMask = Mask;
  1684. InMask.zext(InBits);
  1685. KnownZero.zext(InBits);
  1686. KnownOne.zext(InBits);
  1687. ComputeMaskedBits(Op.getOperand(0), InMask, KnownZero, KnownOne, Depth+1);
  1688. assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
  1689. KnownZero.trunc(BitWidth);
  1690. KnownOne.trunc(BitWidth);
  1691. break;
  1692. }
  1693. case ISD::AssertZext: {
  1694. EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
  1695. APInt InMask = APInt::getLowBitsSet(BitWidth, VT.getSizeInBits());
  1696. ComputeMaskedBits(Op.getOperand(0), Mask & InMask, KnownZero,
  1697. KnownOne, Depth+1);
  1698. KnownZero |= (~InMask) & Mask;
  1699. return;
  1700. }
  1701. case ISD::FGETSIGN:
  1702. // All bits are zero except the low bit.
  1703. KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - 1);
  1704. return;
  1705. case ISD::SUB: {
  1706. if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
  1707. // We know that the top bits of C-X are clear if X contains less bits
  1708. // than C (i.e. no wrap-around can happen). For example, 20-X is
  1709. // positive if we can prove that X is >= 0 and < 16.
  1710. if (CLHS->getAPIntValue().isNonNegative()) {
  1711. unsigned NLZ = (CLHS->getAPIntValue()+1).countLeadingZeros();
  1712. // NLZ can't be BitWidth with no sign bit
  1713. APInt MaskV = APInt::getHighBitsSet(BitWidth, NLZ+1);
  1714. ComputeMaskedBits(Op.getOperand(1), MaskV, KnownZero2, KnownOne2,
  1715. Depth+1);
  1716. // If all of the MaskV bits are known to be zero, then we know the
  1717. // output top bits are zero, because we now know that the output is
  1718. // from [0-C].
  1719. if ((KnownZero2 & MaskV) == MaskV) {
  1720. unsigned NLZ2 = CLHS->getAPIntValue().countLeadingZeros();
  1721. // Top bits known zero.
  1722. KnownZero = APInt::getHighBitsSet(BitWidth, NLZ2) & Mask;
  1723. }
  1724. }
  1725. }
  1726. }
  1727. // fall through
  1728. case ISD::ADD: {
  1729. // Output known-0 bits are known if clear or set in both the low clear bits
  1730. // common to both LHS & RHS. For example, 8+(X<<3) is known to have the
  1731. // low 3 bits clear.
  1732. APInt Mask2 = APInt::getLowBitsSet(BitWidth,
  1733. BitWidth - Mask.countLeadingZeros());
  1734. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero2, KnownOne2, Depth+1);
  1735. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1736. unsigned KnownZeroOut = KnownZero2.countTrailingOnes();
  1737. ComputeMaskedBits(Op.getOperand(1), Mask2, KnownZero2, KnownOne2, Depth+1);
  1738. assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
  1739. KnownZeroOut = std::min(KnownZeroOut,
  1740. KnownZero2.countTrailingOnes());
  1741. KnownZero |= APInt::getLowBitsSet(BitWidth, KnownZeroOut);
  1742. return;
  1743. }
  1744. case ISD::SREM:
  1745. if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1746. const APInt &RA = Rem->getAPIntValue().abs();
  1747. if (RA.isPowerOf2()) {
  1748. APInt LowBits = RA - 1;
  1749. APInt Mask2 = LowBits | APInt::getSignBit(BitWidth);
  1750. ComputeMaskedBits(Op.getOperand(0), Mask2,KnownZero2,KnownOne2,Depth+1);
  1751. // The low bits of the first operand are unchanged by the srem.
  1752. KnownZero = KnownZero2 & LowBits;
  1753. KnownOne = KnownOne2 & LowBits;
  1754. // If the first operand is non-negative or has all low bits zero, then
  1755. // the upper bits are all zero.
  1756. if (KnownZero2[BitWidth-1] || ((KnownZero2 & LowBits) == LowBits))
  1757. KnownZero |= ~LowBits;
  1758. // If the first operand is negative and not all low bits are zero, then
  1759. // the upper bits are all one.
  1760. if (KnownOne2[BitWidth-1] && ((KnownOne2 & LowBits) != 0))
  1761. KnownOne |= ~LowBits;
  1762. KnownZero &= Mask;
  1763. KnownOne &= Mask;
  1764. assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
  1765. }
  1766. }
  1767. return;
  1768. case ISD::UREM: {
  1769. if (ConstantSDNode *Rem = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1770. const APInt &RA = Rem->getAPIntValue();
  1771. if (RA.isPowerOf2()) {
  1772. APInt LowBits = (RA - 1);
  1773. APInt Mask2 = LowBits & Mask;
  1774. KnownZero |= ~LowBits & Mask;
  1775. ComputeMaskedBits(Op.getOperand(0), Mask2, KnownZero, KnownOne,Depth+1);
  1776. assert((KnownZero & KnownOne) == 0&&"Bits known to be one AND zero?");
  1777. break;
  1778. }
  1779. }
  1780. // Since the result is less than or equal to either operand, any leading
  1781. // zero bits in either operand must also exist in the result.
  1782. APInt AllOnes = APInt::getAllOnesValue(BitWidth);
  1783. ComputeMaskedBits(Op.getOperand(0), AllOnes, KnownZero, KnownOne,
  1784. Depth+1);
  1785. ComputeMaskedBits(Op.getOperand(1), AllOnes, KnownZero2, KnownOne2,
  1786. Depth+1);
  1787. uint32_t Leaders = std::max(KnownZero.countLeadingOnes(),
  1788. KnownZero2.countLeadingOnes());
  1789. KnownOne.clear();
  1790. KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & Mask;
  1791. return;
  1792. }
  1793. default:
  1794. // Allow the target to implement this method for its nodes.
  1795. if (Op.getOpcode() >= ISD::BUILTIN_OP_END) {
  1796. case ISD::INTRINSIC_WO_CHAIN:
  1797. case ISD::INTRINSIC_W_CHAIN:
  1798. case ISD::INTRINSIC_VOID:
  1799. TLI.computeMaskedBitsForTargetNode(Op, Mask, KnownZero, KnownOne, *this,
  1800. Depth);
  1801. }
  1802. return;
  1803. }
  1804. }
  1805. /// ComputeNumSignBits - Return the number of times the sign bit of the
  1806. /// register is replicated into the other bits. We know that at least 1 bit
  1807. /// is always equal to the sign bit (itself), but other cases can give us
  1808. /// information. For example, immediately after an "SRA X, 2", we know that
  1809. /// the top 3 bits are all equal to each other, so we return 3.
  1810. unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const{
  1811. EVT VT = Op.getValueType();
  1812. assert(VT.isInteger() && "Invalid VT!");
  1813. unsigned VTBits = VT.getScalarType().getSizeInBits();
  1814. unsigned Tmp, Tmp2;
  1815. unsigned FirstAnswer = 1;
  1816. if (Depth == 6)
  1817. return 1; // Limit search depth.
  1818. switch (Op.getOpcode()) {
  1819. default: break;
  1820. case ISD::AssertSext:
  1821. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  1822. return VTBits-Tmp+1;
  1823. case ISD::AssertZext:
  1824. Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
  1825. return VTBits-Tmp;
  1826. case ISD::Constant: {
  1827. const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
  1828. // If negative, return # leading ones.
  1829. if (Val.isNegative())
  1830. return Val.countLeadingOnes();
  1831. // Return # leading zeros.
  1832. return Val.countLeadingZeros();
  1833. }
  1834. case ISD::SIGN_EXTEND:
  1835. Tmp = VTBits-Op.getOperand(0).getValueType().getScalarType().getSizeInBits();
  1836. return ComputeNumSignBits(Op.getOperand(0), Depth+1) + Tmp;
  1837. case ISD::SIGN_EXTEND_INREG:
  1838. // Max of the input and what this extends.
  1839. Tmp =
  1840. cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarType().getSizeInBits();
  1841. Tmp = VTBits-Tmp+1;
  1842. Tmp2 = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1843. return std::max(Tmp, Tmp2);
  1844. case ISD::SRA:
  1845. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1846. // SRA X, C -> adds C sign bits.
  1847. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1848. Tmp += C->getZExtValue();
  1849. if (Tmp > VTBits) Tmp = VTBits;
  1850. }
  1851. return Tmp;
  1852. case ISD::SHL:
  1853. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1854. // shl destroys sign bits.
  1855. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1856. if (C->getZExtValue() >= VTBits || // Bad shift.
  1857. C->getZExtValue() >= Tmp) break; // Shifted all sign bits out.
  1858. return Tmp - C->getZExtValue();
  1859. }
  1860. break;
  1861. case ISD::AND:
  1862. case ISD::OR:
  1863. case ISD::XOR: // NOT is handled here.
  1864. // Logical binary ops preserve the number of sign bits at the worst.
  1865. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1866. if (Tmp != 1) {
  1867. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1868. FirstAnswer = std::min(Tmp, Tmp2);
  1869. // We computed what we know about the sign bits as our first
  1870. // answer. Now proceed to the generic code that uses
  1871. // ComputeMaskedBits, and pick whichever answer is better.
  1872. }
  1873. break;
  1874. case ISD::SELECT:
  1875. Tmp = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1876. if (Tmp == 1) return 1; // Early out.
  1877. Tmp2 = ComputeNumSignBits(Op.getOperand(2), Depth+1);
  1878. return std::min(Tmp, Tmp2);
  1879. case ISD::SADDO:
  1880. case ISD::UADDO:
  1881. case ISD::SSUBO:
  1882. case ISD::USUBO:
  1883. case ISD::SMULO:
  1884. case ISD::UMULO:
  1885. if (Op.getResNo() != 1)
  1886. break;
  1887. // The boolean result conforms to getBooleanContents. Fall through.
  1888. case ISD::SETCC:
  1889. // If setcc returns 0/-1, all bits are sign bits.
  1890. if (TLI.getBooleanContents() ==
  1891. TargetLowering::ZeroOrNegativeOneBooleanContent)
  1892. return VTBits;
  1893. break;
  1894. case ISD::ROTL:
  1895. case ISD::ROTR:
  1896. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
  1897. unsigned RotAmt = C->getZExtValue() & (VTBits-1);
  1898. // Handle rotate right by N like a rotate left by 32-N.
  1899. if (Op.getOpcode() == ISD::ROTR)
  1900. RotAmt = (VTBits-RotAmt) & (VTBits-1);
  1901. // If we aren't rotating out all of the known-in sign bits, return the
  1902. // number that are left. This handles rotl(sext(x), 1) for example.
  1903. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1904. if (Tmp > RotAmt+1) return Tmp-RotAmt;
  1905. }
  1906. break;
  1907. case ISD::ADD:
  1908. // Add can have at most one carry bit. Thus we know that the output
  1909. // is, at worst, one more bit than the inputs.
  1910. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1911. if (Tmp == 1) return 1; // Early out.
  1912. // Special case decrementing a value (ADD X, -1):
  1913. if (ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
  1914. if (CRHS->isAllOnesValue()) {
  1915. APInt KnownZero, KnownOne;
  1916. APInt Mask = APInt::getAllOnesValue(VTBits);
  1917. ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero, KnownOne, Depth+1);
  1918. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  1919. // sign bits set.
  1920. if ((KnownZero | APInt(VTBits, 1)) == Mask)
  1921. return VTBits;
  1922. // If we are subtracting one from a positive number, there is no carry
  1923. // out of the result.
  1924. if (KnownZero.isNegative())
  1925. return Tmp;
  1926. }
  1927. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1928. if (Tmp2 == 1) return 1;
  1929. return std::min(Tmp, Tmp2)-1;
  1930. break;
  1931. case ISD::SUB:
  1932. Tmp2 = ComputeNumSignBits(Op.getOperand(1), Depth+1);
  1933. if (Tmp2 == 1) return 1;
  1934. // Handle NEG.
  1935. if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
  1936. if (CLHS->isNullValue()) {
  1937. APInt KnownZero, KnownOne;
  1938. APInt Mask = APInt::getAllOnesValue(VTBits);
  1939. ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne, Depth+1);
  1940. // If the input is known to be 0 or 1, the output is 0/-1, which is all
  1941. // sign bits set.
  1942. if ((KnownZero | APInt(VTBits, 1)) == Mask)
  1943. return VTBits;
  1944. // If the input is known to be positive (the sign bit is known clear),
  1945. // the output of the NEG has the same number of sign bits as the input.
  1946. if (KnownZero.isNegative())
  1947. return Tmp2;
  1948. // Otherwise, we treat this like a SUB.
  1949. }
  1950. // Sub can have at most one carry bit. Thus we know that the output
  1951. // is, at worst, one more bit than the inputs.
  1952. Tmp = ComputeNumSignBits(Op.getOperand(0), Depth+1);
  1953. if (Tmp == 1) return 1; // Early out.
  1954. return std::min(Tmp, Tmp2)-1;
  1955. break;
  1956. case ISD::TRUNCATE:
  1957. // FIXME: it's tricky to do anything useful for this, but it is an important
  1958. // case for targets like X86.
  1959. break;
  1960. }
  1961. // Handle LOADX separately here. EXTLOAD case will fallthrough.
  1962. if (Op.getOpcode() == ISD::LOAD) {
  1963. LoadSDNode *LD = cast<LoadSDNode>(Op);
  1964. unsigned ExtType = LD->getExtensionType();
  1965. switch (ExtType) {
  1966. default: break;
  1967. case ISD::SEXTLOAD: // '17' bits known
  1968. Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
  1969. return VTBits-Tmp+1;
  1970. case ISD::ZEXTLOAD: // '16' bits known
  1971. Tmp = LD->getMemoryVT().getScalarType().getSizeInBits();
  1972. return VTBits-Tmp;
  1973. }
  1974. }
  1975. // Allow the target to implement this method for its nodes.
  1976. if (Op.getOpcode() >= ISD::BUILTIN_OP_END ||
  1977. Op.getOpcode() == ISD::INTRINSIC_WO_CHAIN ||
  1978. Op.getOpcode() == ISD::INTRINSIC_W_CHAIN ||
  1979. Op.getOpcode() == ISD::INTRINSIC_VOID) {
  1980. unsigned NumBits = TLI.ComputeNumSignBitsForTargetNode(Op, Depth);
  1981. if (NumBits > 1) FirstAnswer = std::max(FirstAnswer, NumBits);
  1982. }
  1983. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  1984. // use this information.
  1985. APInt KnownZero, KnownOne;
  1986. APInt Mask = APInt::getAllOnesValue(VTBits);
  1987. ComputeMaskedBits(Op, Mask, KnownZero, KnownOne, Depth);
  1988. if (KnownZero.isNegative()) { // sign bit is 0
  1989. Mask = KnownZero;
  1990. } else if (KnownOne.isNegative()) { // sign bit is 1;
  1991. Mask = KnownOne;
  1992. } else {
  1993. // Nothing known.
  1994. return FirstAnswer;
  1995. }
  1996. // Okay, we know that the sign bit in Mask is set. Use CLZ to determine
  1997. // the number of identical bits in the top of the input value.
  1998. Mask = ~Mask;
  1999. Mask <<= Mask.getBitWidth()-VTBits;
  2000. // Return # leading zeros. We use 'min' here in case Val was zero before
  2001. // shifting. We don't want to return '64' as for an i32 "0".
  2002. return std::max(FirstAnswer, std::min(VTBits, Mask.countLeadingZeros()));
  2003. }
  2004. bool SelectionDAG::isKnownNeverNaN(SDValue Op) const {
  2005. // If we're told that NaNs won't happen, assume they won't.
  2006. if (NoNaNsFPMath)
  2007. return true;
  2008. // If the value is a constant, we can obviously see if it is a NaN or not.
  2009. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
  2010. return !C->getValueAPF().isNaN();
  2011. // TODO: Recognize more cases here.
  2012. return false;
  2013. }
  2014. bool SelectionDAG::isKnownNeverZero(SDValue Op) const {
  2015. // If the value is a constant, we can obviously see if it is a zero or not.
  2016. if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op))
  2017. return !C->isZero();
  2018. // TODO: Recognize more cases here.
  2019. return false;
  2020. }
  2021. bool SelectionDAG::isEqualTo(SDValue A, SDValue B) const {
  2022. // Check the obvious case.
  2023. if (A == B) return true;
  2024. // For for negative and positive zero.
  2025. if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
  2026. if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
  2027. if (CA->isZero() && CB->isZero()) return true;
  2028. // Otherwise they may not be equal.
  2029. return false;
  2030. }
  2031. bool SelectionDAG::isVerifiedDebugInfoDesc(SDValue Op) const {
  2032. GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
  2033. if (!GA) return false;
  2034. if (GA->getOffset() != 0) return false;
  2035. const GlobalVariable *GV = dyn_cast<GlobalVariable>(GA->getGlobal());
  2036. if (!GV) return false;
  2037. return MF->getMMI().hasDebugInfo();
  2038. }
  2039. /// getNode - Gets or creates the specified node.
  2040. ///
  2041. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT) {
  2042. FoldingSetNodeID ID;
  2043. AddNodeIDNode(ID, Opcode, getVTList(VT), 0, 0);
  2044. void *IP = 0;
  2045. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2046. return SDValue(E, 0);
  2047. SDNode *N = new (NodeAllocator) SDNode(Opcode, DL, getVTList(VT));
  2048. CSEMap.InsertNode(N, IP);
  2049. AllNodes.push_back(N);
  2050. #ifndef NDEBUG
  2051. VerifySDNode(N);
  2052. #endif
  2053. return SDValue(N, 0);
  2054. }
  2055. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  2056. EVT VT, SDValue Operand) {
  2057. // Constant fold unary operations with an integer constant operand.
  2058. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.getNode())) {
  2059. const APInt &Val = C->getAPIntValue();
  2060. switch (Opcode) {
  2061. default: break;
  2062. case ISD::SIGN_EXTEND:
  2063. return getConstant(APInt(Val).sextOrTrunc(VT.getSizeInBits()), VT);
  2064. case ISD::ANY_EXTEND:
  2065. case ISD::ZERO_EXTEND:
  2066. case ISD::TRUNCATE:
  2067. return getConstant(APInt(Val).zextOrTrunc(VT.getSizeInBits()), VT);
  2068. case ISD::UINT_TO_FP:
  2069. case ISD::SINT_TO_FP: {
  2070. const uint64_t zero[] = {0, 0};
  2071. // No compile time operations on ppcf128.
  2072. if (VT == MVT::ppcf128) break;
  2073. APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
  2074. (void)apf.convertFromAPInt(Val,
  2075. Opcode==ISD::SINT_TO_FP,
  2076. APFloat::rmNearestTiesToEven);
  2077. return getConstantFP(apf, VT);
  2078. }
  2079. case ISD::BIT_CONVERT:
  2080. if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
  2081. return getConstantFP(Val.bitsToFloat(), VT);
  2082. else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
  2083. return getConstantFP(Val.bitsToDouble(), VT);
  2084. break;
  2085. case ISD::BSWAP:
  2086. return getConstant(Val.byteSwap(), VT);
  2087. case ISD::CTPOP:
  2088. return getConstant(Val.countPopulation(), VT);
  2089. case ISD::CTLZ:
  2090. return getConstant(Val.countLeadingZeros(), VT);
  2091. case ISD::CTTZ:
  2092. return getConstant(Val.countTrailingZeros(), VT);
  2093. }
  2094. }
  2095. // Constant fold unary operations with a floating point constant operand.
  2096. if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.getNode())) {
  2097. APFloat V = C->getValueAPF(); // make copy
  2098. if (VT != MVT::ppcf128 && Operand.getValueType() != MVT::ppcf128) {
  2099. switch (Opcode) {
  2100. case ISD::FNEG:
  2101. V.changeSign();
  2102. return getConstantFP(V, VT);
  2103. case ISD::FABS:
  2104. V.clearSign();
  2105. return getConstantFP(V, VT);
  2106. case ISD::FP_ROUND:
  2107. case ISD::FP_EXTEND: {
  2108. bool ignored;
  2109. // This can return overflow, underflow, or inexact; we don't care.
  2110. // FIXME need to be more flexible about rounding mode.
  2111. (void)V.convert(*EVTToAPFloatSemantics(VT),
  2112. APFloat::rmNearestTiesToEven, &ignored);
  2113. return getConstantFP(V, VT);
  2114. }
  2115. case ISD::FP_TO_SINT:
  2116. case ISD::FP_TO_UINT: {
  2117. integerPart x[2];
  2118. bool ignored;
  2119. assert(integerPartWidth >= 64);
  2120. // FIXME need to be more flexible about rounding mode.
  2121. APFloat::opStatus s = V.convertToInteger(x, VT.getSizeInBits(),
  2122. Opcode==ISD::FP_TO_SINT,
  2123. APFloat::rmTowardZero, &ignored);
  2124. if (s==APFloat::opInvalidOp) // inexact is OK, in fact usual
  2125. break;
  2126. APInt api(VT.getSizeInBits(), 2, x);
  2127. return getConstant(api, VT);
  2128. }
  2129. case ISD::BIT_CONVERT:
  2130. if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
  2131. return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), VT);
  2132. else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
  2133. return getConstant(V.bitcastToAPInt().getZExtValue(), VT);
  2134. break;
  2135. }
  2136. }
  2137. }
  2138. unsigned OpOpcode = Operand.getNode()->getOpcode();
  2139. switch (Opcode) {
  2140. case ISD::TokenFactor:
  2141. case ISD::MERGE_VALUES:
  2142. case ISD::CONCAT_VECTORS:
  2143. return Operand; // Factor, merge or concat of one node? No need.
  2144. case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
  2145. case ISD::FP_EXTEND:
  2146. assert(VT.isFloatingPoint() &&
  2147. Operand.getValueType().isFloatingPoint() && "Invalid FP cast!");
  2148. if (Operand.getValueType() == VT) return Operand; // noop conversion.
  2149. assert((!VT.isVector() ||
  2150. VT.getVectorNumElements() ==
  2151. Operand.getValueType().getVectorNumElements()) &&
  2152. "Vector element count mismatch!");
  2153. if (Operand.getOpcode() == ISD::UNDEF)
  2154. return getUNDEF(VT);
  2155. break;
  2156. case ISD::SIGN_EXTEND:
  2157. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2158. "Invalid SIGN_EXTEND!");
  2159. if (Operand.getValueType() == VT) return Operand; // noop extension
  2160. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2161. "Invalid sext node, dst < src!");
  2162. assert((!VT.isVector() ||
  2163. VT.getVectorNumElements() ==
  2164. Operand.getValueType().getVectorNumElements()) &&
  2165. "Vector element count mismatch!");
  2166. if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
  2167. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2168. break;
  2169. case ISD::ZERO_EXTEND:
  2170. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2171. "Invalid ZERO_EXTEND!");
  2172. if (Operand.getValueType() == VT) return Operand; // noop extension
  2173. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2174. "Invalid zext node, dst < src!");
  2175. assert((!VT.isVector() ||
  2176. VT.getVectorNumElements() ==
  2177. Operand.getValueType().getVectorNumElements()) &&
  2178. "Vector element count mismatch!");
  2179. if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
  2180. return getNode(ISD::ZERO_EXTEND, DL, VT,
  2181. Operand.getNode()->getOperand(0));
  2182. break;
  2183. case ISD::ANY_EXTEND:
  2184. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2185. "Invalid ANY_EXTEND!");
  2186. if (Operand.getValueType() == VT) return Operand; // noop extension
  2187. assert(Operand.getValueType().getScalarType().bitsLT(VT.getScalarType()) &&
  2188. "Invalid anyext node, dst < src!");
  2189. assert((!VT.isVector() ||
  2190. VT.getVectorNumElements() ==
  2191. Operand.getValueType().getVectorNumElements()) &&
  2192. "Vector element count mismatch!");
  2193. if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  2194. OpOpcode == ISD::ANY_EXTEND)
  2195. // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
  2196. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2197. // (ext (trunx x)) -> x
  2198. if (OpOpcode == ISD::TRUNCATE) {
  2199. SDValue OpOp = Operand.getNode()->getOperand(0);
  2200. if (OpOp.getValueType() == VT)
  2201. return OpOp;
  2202. }
  2203. break;
  2204. case ISD::TRUNCATE:
  2205. assert(VT.isInteger() && Operand.getValueType().isInteger() &&
  2206. "Invalid TRUNCATE!");
  2207. if (Operand.getValueType() == VT) return Operand; // noop truncate
  2208. assert(Operand.getValueType().getScalarType().bitsGT(VT.getScalarType()) &&
  2209. "Invalid truncate node, src < dst!");
  2210. assert((!VT.isVector() ||
  2211. VT.getVectorNumElements() ==
  2212. Operand.getValueType().getVectorNumElements()) &&
  2213. "Vector element count mismatch!");
  2214. if (OpOpcode == ISD::TRUNCATE)
  2215. return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
  2216. else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
  2217. OpOpcode == ISD::ANY_EXTEND) {
  2218. // If the source is smaller than the dest, we still need an extend.
  2219. if (Operand.getNode()->getOperand(0).getValueType().getScalarType()
  2220. .bitsLT(VT.getScalarType()))
  2221. return getNode(OpOpcode, DL, VT, Operand.getNode()->getOperand(0));
  2222. else if (Operand.getNode()->getOperand(0).getValueType().bitsGT(VT))
  2223. return getNode(ISD::TRUNCATE, DL, VT, Operand.getNode()->getOperand(0));
  2224. else
  2225. return Operand.getNode()->getOperand(0);
  2226. }
  2227. break;
  2228. case ISD::BIT_CONVERT:
  2229. // Basic sanity checking.
  2230. assert(VT.getSizeInBits() == Operand.getValueType().getSizeInBits()
  2231. && "Cannot BIT_CONVERT between types of different sizes!");
  2232. if (VT == Operand.getValueType()) return Operand; // noop conversion.
  2233. if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
  2234. return getNode(ISD::BIT_CONVERT, DL, VT, Operand.getOperand(0));
  2235. if (OpOpcode == ISD::UNDEF)
  2236. return getUNDEF(VT);
  2237. break;
  2238. case ISD::SCALAR_TO_VECTOR:
  2239. assert(VT.isVector() && !Operand.getValueType().isVector() &&
  2240. (VT.getVectorElementType() == Operand.getValueType() ||
  2241. (VT.getVectorElementType().isInteger() &&
  2242. Operand.getValueType().isInteger() &&
  2243. VT.getVectorElementType().bitsLE(Operand.getValueType()))) &&
  2244. "Illegal SCALAR_TO_VECTOR node!");
  2245. if (OpOpcode == ISD::UNDEF)
  2246. return getUNDEF(VT);
  2247. // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
  2248. if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
  2249. isa<ConstantSDNode>(Operand.getOperand(1)) &&
  2250. Operand.getConstantOperandVal(1) == 0 &&
  2251. Operand.getOperand(0).getValueType() == VT)
  2252. return Operand.getOperand(0);
  2253. break;
  2254. case ISD::FNEG:
  2255. // -(X-Y) -> (Y-X) is unsafe because when X==Y, -0.0 != +0.0
  2256. if (UnsafeFPMath && OpOpcode == ISD::FSUB)
  2257. return getNode(ISD::FSUB, DL, VT, Operand.getNode()->getOperand(1),
  2258. Operand.getNode()->getOperand(0));
  2259. if (OpOpcode == ISD::FNEG) // --X -> X
  2260. return Operand.getNode()->getOperand(0);
  2261. break;
  2262. case ISD::FABS:
  2263. if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
  2264. return getNode(ISD::FABS, DL, VT, Operand.getNode()->getOperand(0));
  2265. break;
  2266. }
  2267. SDNode *N;
  2268. SDVTList VTs = getVTList(VT);
  2269. if (VT != MVT::Flag) { // Don't CSE flag producing nodes
  2270. FoldingSetNodeID ID;
  2271. SDValue Ops[1] = { Operand };
  2272. AddNodeIDNode(ID, Opcode, VTs, Ops, 1);
  2273. void *IP = 0;
  2274. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2275. return SDValue(E, 0);
  2276. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
  2277. CSEMap.InsertNode(N, IP);
  2278. } else {
  2279. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTs, Operand);
  2280. }
  2281. AllNodes.push_back(N);
  2282. #ifndef NDEBUG
  2283. VerifySDNode(N);
  2284. #endif
  2285. return SDValue(N, 0);
  2286. }
  2287. SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode,
  2288. EVT VT,
  2289. ConstantSDNode *Cst1,
  2290. ConstantSDNode *Cst2) {
  2291. const APInt &C1 = Cst1->getAPIntValue(), &C2 = Cst2->getAPIntValue();
  2292. switch (Opcode) {
  2293. case ISD::ADD: return getConstant(C1 + C2, VT);
  2294. case ISD::SUB: return getConstant(C1 - C2, VT);
  2295. case ISD::MUL: return getConstant(C1 * C2, VT);
  2296. case ISD::UDIV:
  2297. if (C2.getBoolValue()) return getConstant(C1.udiv(C2), VT);
  2298. break;
  2299. case ISD::UREM:
  2300. if (C2.getBoolValue()) return getConstant(C1.urem(C2), VT);
  2301. break;
  2302. case ISD::SDIV:
  2303. if (C2.getBoolValue()) return getConstant(C1.sdiv(C2), VT);
  2304. break;
  2305. case ISD::SREM:
  2306. if (C2.getBoolValue()) return getConstant(C1.srem(C2), VT);
  2307. break;
  2308. case ISD::AND: return getConstant(C1 & C2, VT);
  2309. case ISD::OR: return getConstant(C1 | C2, VT);
  2310. case ISD::XOR: return getConstant(C1 ^ C2, VT);
  2311. case ISD::SHL: return getConstant(C1 << C2, VT);
  2312. case ISD::SRL: return getConstant(C1.lshr(C2), VT);
  2313. case ISD::SRA: return getConstant(C1.ashr(C2), VT);
  2314. case ISD::ROTL: return getConstant(C1.rotl(C2), VT);
  2315. case ISD::ROTR: return getConstant(C1.rotr(C2), VT);
  2316. default: break;
  2317. }
  2318. return SDValue();
  2319. }
  2320. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2321. SDValue N1, SDValue N2) {
  2322. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
  2323. ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
  2324. switch (Opcode) {
  2325. default: break;
  2326. case ISD::TokenFactor:
  2327. assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
  2328. N2.getValueType() == MVT::Other && "Invalid token factor!");
  2329. // Fold trivial token factors.
  2330. if (N1.getOpcode() == ISD::EntryToken) return N2;
  2331. if (N2.getOpcode() == ISD::EntryToken) return N1;
  2332. if (N1 == N2) return N1;
  2333. break;
  2334. case ISD::CONCAT_VECTORS:
  2335. // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
  2336. // one big BUILD_VECTOR.
  2337. if (N1.getOpcode() == ISD::BUILD_VECTOR &&
  2338. N2.getOpcode() == ISD::BUILD_VECTOR) {
  2339. SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
  2340. N1.getNode()->op_end());
  2341. Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
  2342. return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
  2343. }
  2344. break;
  2345. case ISD::AND:
  2346. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2347. assert(N1.getValueType() == N2.getValueType() &&
  2348. N1.getValueType() == VT && "Binary operator types must match!");
  2349. // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
  2350. // worth handling here.
  2351. if (N2C && N2C->isNullValue())
  2352. return N2;
  2353. if (N2C && N2C->isAllOnesValue()) // X & -1 -> X
  2354. return N1;
  2355. break;
  2356. case ISD::OR:
  2357. case ISD::XOR:
  2358. case ISD::ADD:
  2359. case ISD::SUB:
  2360. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2361. assert(N1.getValueType() == N2.getValueType() &&
  2362. N1.getValueType() == VT && "Binary operator types must match!");
  2363. // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
  2364. // it's worth handling here.
  2365. if (N2C && N2C->isNullValue())
  2366. return N1;
  2367. break;
  2368. case ISD::UDIV:
  2369. case ISD::UREM:
  2370. case ISD::MULHU:
  2371. case ISD::MULHS:
  2372. case ISD::MUL:
  2373. case ISD::SDIV:
  2374. case ISD::SREM:
  2375. assert(VT.isInteger() && "This operator does not apply to FP types!");
  2376. assert(N1.getValueType() == N2.getValueType() &&
  2377. N1.getValueType() == VT && "Binary operator types must match!");
  2378. break;
  2379. case ISD::FADD:
  2380. case ISD::FSUB:
  2381. case ISD::FMUL:
  2382. case ISD::FDIV:
  2383. case ISD::FREM:
  2384. if (UnsafeFPMath) {
  2385. if (Opcode == ISD::FADD) {
  2386. // 0+x --> x
  2387. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1))
  2388. if (CFP->getValueAPF().isZero())
  2389. return N2;
  2390. // x+0 --> x
  2391. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
  2392. if (CFP->getValueAPF().isZero())
  2393. return N1;
  2394. } else if (Opcode == ISD::FSUB) {
  2395. // x-0 --> x
  2396. if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2))
  2397. if (CFP->getValueAPF().isZero())
  2398. return N1;
  2399. }
  2400. }
  2401. assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
  2402. assert(N1.getValueType() == N2.getValueType() &&
  2403. N1.getValueType() == VT && "Binary operator types must match!");
  2404. break;
  2405. case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
  2406. assert(N1.getValueType() == VT &&
  2407. N1.getValueType().isFloatingPoint() &&
  2408. N2.getValueType().isFloatingPoint() &&
  2409. "Invalid FCOPYSIGN!");
  2410. break;
  2411. case ISD::SHL:
  2412. case ISD::SRA:
  2413. case ISD::SRL:
  2414. case ISD::ROTL:
  2415. case ISD::ROTR:
  2416. assert(VT == N1.getValueType() &&
  2417. "Shift operators return type must be the same as their first arg");
  2418. assert(VT.isInteger() && N2.getValueType().isInteger() &&
  2419. "Shifts only work on integers");
  2420. // Always fold shifts of i1 values so the code generator doesn't need to
  2421. // handle them. Since we know the size of the shift has to be less than the
  2422. // size of the value, the shift/rotate count is guaranteed to be zero.
  2423. if (VT == MVT::i1)
  2424. return N1;
  2425. if (N2C && N2C->isNullValue())
  2426. return N1;
  2427. break;
  2428. case ISD::FP_ROUND_INREG: {
  2429. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2430. assert(VT == N1.getValueType() && "Not an inreg round!");
  2431. assert(VT.isFloatingPoint() && EVT.isFloatingPoint() &&
  2432. "Cannot FP_ROUND_INREG integer types");
  2433. assert(EVT.isVector() == VT.isVector() &&
  2434. "FP_ROUND_INREG type should be vector iff the operand "
  2435. "type is vector!");
  2436. assert((!EVT.isVector() ||
  2437. EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
  2438. "Vector element counts must match in FP_ROUND_INREG");
  2439. assert(EVT.bitsLE(VT) && "Not rounding down!");
  2440. if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
  2441. break;
  2442. }
  2443. case ISD::FP_ROUND:
  2444. assert(VT.isFloatingPoint() &&
  2445. N1.getValueType().isFloatingPoint() &&
  2446. VT.bitsLE(N1.getValueType()) &&
  2447. isa<ConstantSDNode>(N2) && "Invalid FP_ROUND!");
  2448. if (N1.getValueType() == VT) return N1; // noop conversion.
  2449. break;
  2450. case ISD::AssertSext:
  2451. case ISD::AssertZext: {
  2452. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2453. assert(VT == N1.getValueType() && "Not an inreg extend!");
  2454. assert(VT.isInteger() && EVT.isInteger() &&
  2455. "Cannot *_EXTEND_INREG FP types");
  2456. assert(!EVT.isVector() &&
  2457. "AssertSExt/AssertZExt type should be the vector element type "
  2458. "rather than the vector type!");
  2459. assert(EVT.bitsLE(VT) && "Not extending!");
  2460. if (VT == EVT) return N1; // noop assertion.
  2461. break;
  2462. }
  2463. case ISD::SIGN_EXTEND_INREG: {
  2464. EVT EVT = cast<VTSDNode>(N2)->getVT();
  2465. assert(VT == N1.getValueType() && "Not an inreg extend!");
  2466. assert(VT.isInteger() && EVT.isInteger() &&
  2467. "Cannot *_EXTEND_INREG FP types");
  2468. assert(EVT.isVector() == VT.isVector() &&
  2469. "SIGN_EXTEND_INREG type should be vector iff the operand "
  2470. "type is vector!");
  2471. assert((!EVT.isVector() ||
  2472. EVT.getVectorNumElements() == VT.getVectorNumElements()) &&
  2473. "Vector element counts must match in SIGN_EXTEND_INREG");
  2474. assert(EVT.bitsLE(VT) && "Not extending!");
  2475. if (EVT == VT) return N1; // Not actually extending
  2476. if (N1C) {
  2477. APInt Val = N1C->getAPIntValue();
  2478. unsigned FromBits = EVT.getScalarType().getSizeInBits();
  2479. Val <<= Val.getBitWidth()-FromBits;
  2480. Val = Val.ashr(Val.getBitWidth()-FromBits);
  2481. return getConstant(Val, VT);
  2482. }
  2483. break;
  2484. }
  2485. case ISD::EXTRACT_VECTOR_ELT:
  2486. // EXTRACT_VECTOR_ELT of an UNDEF is an UNDEF.
  2487. if (N1.getOpcode() == ISD::UNDEF)
  2488. return getUNDEF(VT);
  2489. // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
  2490. // expanding copies of large vectors from registers.
  2491. if (N2C &&
  2492. N1.getOpcode() == ISD::CONCAT_VECTORS &&
  2493. N1.getNumOperands() > 0) {
  2494. unsigned Factor =
  2495. N1.getOperand(0).getValueType().getVectorNumElements();
  2496. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
  2497. N1.getOperand(N2C->getZExtValue() / Factor),
  2498. getConstant(N2C->getZExtValue() % Factor,
  2499. N2.getValueType()));
  2500. }
  2501. // EXTRACT_VECTOR_ELT of BUILD_VECTOR is often formed while lowering is
  2502. // expanding large vector constants.
  2503. if (N2C && N1.getOpcode() == ISD::BUILD_VECTOR) {
  2504. SDValue Elt = N1.getOperand(N2C->getZExtValue());
  2505. EVT VEltTy = N1.getValueType().getVectorElementType();
  2506. if (Elt.getValueType() != VEltTy) {
  2507. // If the vector element type is not legal, the BUILD_VECTOR operands
  2508. // are promoted and implicitly truncated. Make that explicit here.
  2509. Elt = getNode(ISD::TRUNCATE, DL, VEltTy, Elt);
  2510. }
  2511. if (VT != VEltTy) {
  2512. // If the vector element type is not legal, the EXTRACT_VECTOR_ELT
  2513. // result is implicitly extended.
  2514. Elt = getNode(ISD::ANY_EXTEND, DL, VT, Elt);
  2515. }
  2516. return Elt;
  2517. }
  2518. // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
  2519. // operations are lowered to scalars.
  2520. if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
  2521. // If the indices are the same, return the inserted element else
  2522. // if the indices are known different, extract the element from
  2523. // the original vector.
  2524. SDValue N1Op2 = N1.getOperand(2);
  2525. ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2.getNode());
  2526. if (N1Op2C && N2C) {
  2527. if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
  2528. if (VT == N1.getOperand(1).getValueType())
  2529. return N1.getOperand(1);
  2530. else
  2531. return getSExtOrTrunc(N1.getOperand(1), DL, VT);
  2532. }
  2533. return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
  2534. }
  2535. }
  2536. break;
  2537. case ISD::EXTRACT_ELEMENT:
  2538. assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
  2539. assert(!N1.getValueType().isVector() && !VT.isVector() &&
  2540. (N1.getValueType().isInteger() == VT.isInteger()) &&
  2541. "Wrong types for EXTRACT_ELEMENT!");
  2542. // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
  2543. // 64-bit integers into 32-bit parts. Instead of building the extract of
  2544. // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
  2545. if (N1.getOpcode() == ISD::BUILD_PAIR)
  2546. return N1.getOperand(N2C->getZExtValue());
  2547. // EXTRACT_ELEMENT of a constant int is also very common.
  2548. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
  2549. unsigned ElementSize = VT.getSizeInBits();
  2550. unsigned Shift = ElementSize * N2C->getZExtValue();
  2551. APInt ShiftedVal = C->getAPIntValue().lshr(Shift);
  2552. return getConstant(ShiftedVal.trunc(ElementSize), VT);
  2553. }
  2554. break;
  2555. case ISD::EXTRACT_SUBVECTOR:
  2556. if (N1.getValueType() == VT) // Trivial extraction.
  2557. return N1;
  2558. break;
  2559. }
  2560. if (N1C) {
  2561. if (N2C) {
  2562. SDValue SV = FoldConstantArithmetic(Opcode, VT, N1C, N2C);
  2563. if (SV.getNode()) return SV;
  2564. } else { // Cannonicalize constant to RHS if commutative
  2565. if (isCommutativeBinOp(Opcode)) {
  2566. std::swap(N1C, N2C);
  2567. std::swap(N1, N2);
  2568. }
  2569. }
  2570. }
  2571. // Constant fold FP operations.
  2572. ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.getNode());
  2573. ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.getNode());
  2574. if (N1CFP) {
  2575. if (!N2CFP && isCommutativeBinOp(Opcode)) {
  2576. // Cannonicalize constant to RHS if commutative
  2577. std::swap(N1CFP, N2CFP);
  2578. std::swap(N1, N2);
  2579. } else if (N2CFP && VT != MVT::ppcf128) {
  2580. APFloat V1 = N1CFP->getValueAPF(), V2 = N2CFP->getValueAPF();
  2581. APFloat::opStatus s;
  2582. switch (Opcode) {
  2583. case ISD::FADD:
  2584. s = V1.add(V2, APFloat::rmNearestTiesToEven);
  2585. if (s != APFloat::opInvalidOp)
  2586. return getConstantFP(V1, VT);
  2587. break;
  2588. case ISD::FSUB:
  2589. s = V1.subtract(V2, APFloat::rmNearestTiesToEven);
  2590. if (s!=APFloat::opInvalidOp)
  2591. return getConstantFP(V1, VT);
  2592. break;
  2593. case ISD::FMUL:
  2594. s = V1.multiply(V2, APFloat::rmNearestTiesToEven);
  2595. if (s!=APFloat::opInvalidOp)
  2596. return getConstantFP(V1, VT);
  2597. break;
  2598. case ISD::FDIV:
  2599. s = V1.divide(V2, APFloat::rmNearestTiesToEven);
  2600. if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
  2601. return getConstantFP(V1, VT);
  2602. break;
  2603. case ISD::FREM :
  2604. s = V1.mod(V2, APFloat::rmNearestTiesToEven);
  2605. if (s!=APFloat::opInvalidOp && s!=APFloat::opDivByZero)
  2606. return getConstantFP(V1, VT);
  2607. break;
  2608. case ISD::FCOPYSIGN:
  2609. V1.copySign(V2);
  2610. return getConstantFP(V1, VT);
  2611. default: break;
  2612. }
  2613. }
  2614. }
  2615. // Canonicalize an UNDEF to the RHS, even over a constant.
  2616. if (N1.getOpcode() == ISD::UNDEF) {
  2617. if (isCommutativeBinOp(Opcode)) {
  2618. std::swap(N1, N2);
  2619. } else {
  2620. switch (Opcode) {
  2621. case ISD::FP_ROUND_INREG:
  2622. case ISD::SIGN_EXTEND_INREG:
  2623. case ISD::SUB:
  2624. case ISD::FSUB:
  2625. case ISD::FDIV:
  2626. case ISD::FREM:
  2627. case ISD::SRA:
  2628. return N1; // fold op(undef, arg2) -> undef
  2629. case ISD::UDIV:
  2630. case ISD::SDIV:
  2631. case ISD::UREM:
  2632. case ISD::SREM:
  2633. case ISD::SRL:
  2634. case ISD::SHL:
  2635. if (!VT.isVector())
  2636. return getConstant(0, VT); // fold op(undef, arg2) -> 0
  2637. // For vectors, we can't easily build an all zero vector, just return
  2638. // the LHS.
  2639. return N2;
  2640. }
  2641. }
  2642. }
  2643. // Fold a bunch of operators when the RHS is undef.
  2644. if (N2.getOpcode() == ISD::UNDEF) {
  2645. switch (Opcode) {
  2646. case ISD::XOR:
  2647. if (N1.getOpcode() == ISD::UNDEF)
  2648. // Handle undef ^ undef -> 0 special case. This is a common
  2649. // idiom (misuse).
  2650. return getConstant(0, VT);
  2651. // fallthrough
  2652. case ISD::ADD:
  2653. case ISD::ADDC:
  2654. case ISD::ADDE:
  2655. case ISD::SUB:
  2656. case ISD::UDIV:
  2657. case ISD::SDIV:
  2658. case ISD::UREM:
  2659. case ISD::SREM:
  2660. return N2; // fold op(arg1, undef) -> undef
  2661. case ISD::FADD:
  2662. case ISD::FSUB:
  2663. case ISD::FMUL:
  2664. case ISD::FDIV:
  2665. case ISD::FREM:
  2666. if (UnsafeFPMath)
  2667. return N2;
  2668. break;
  2669. case ISD::MUL:
  2670. case ISD::AND:
  2671. case ISD::SRL:
  2672. case ISD::SHL:
  2673. if (!VT.isVector())
  2674. return getConstant(0, VT); // fold op(arg1, undef) -> 0
  2675. // For vectors, we can't easily build an all zero vector, just return
  2676. // the LHS.
  2677. return N1;
  2678. case ISD::OR:
  2679. if (!VT.isVector())
  2680. return getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
  2681. // For vectors, we can't easily build an all one vector, just return
  2682. // the LHS.
  2683. return N1;
  2684. case ISD::SRA:
  2685. return N1;
  2686. }
  2687. }
  2688. // Memoize this node if possible.
  2689. SDNode *N;
  2690. SDVTList VTs = getVTList(VT);
  2691. if (VT != MVT::Flag) {
  2692. SDValue Ops[] = { N1, N2 };
  2693. FoldingSetNodeID ID;
  2694. AddNodeIDNode(ID, Opcode, VTs, Ops, 2);
  2695. void *IP = 0;
  2696. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2697. return SDValue(E, 0);
  2698. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
  2699. CSEMap.InsertNode(N, IP);
  2700. } else {
  2701. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTs, N1, N2);
  2702. }
  2703. AllNodes.push_back(N);
  2704. #ifndef NDEBUG
  2705. VerifySDNode(N);
  2706. #endif
  2707. return SDValue(N, 0);
  2708. }
  2709. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2710. SDValue N1, SDValue N2, SDValue N3) {
  2711. // Perform various simplifications.
  2712. ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
  2713. switch (Opcode) {
  2714. case ISD::CONCAT_VECTORS:
  2715. // A CONCAT_VECTOR with all operands BUILD_VECTOR can be simplified to
  2716. // one big BUILD_VECTOR.
  2717. if (N1.getOpcode() == ISD::BUILD_VECTOR &&
  2718. N2.getOpcode() == ISD::BUILD_VECTOR &&
  2719. N3.getOpcode() == ISD::BUILD_VECTOR) {
  2720. SmallVector<SDValue, 16> Elts(N1.getNode()->op_begin(),
  2721. N1.getNode()->op_end());
  2722. Elts.append(N2.getNode()->op_begin(), N2.getNode()->op_end());
  2723. Elts.append(N3.getNode()->op_begin(), N3.getNode()->op_end());
  2724. return getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], Elts.size());
  2725. }
  2726. break;
  2727. case ISD::SETCC: {
  2728. // Use FoldSetCC to simplify SETCC's.
  2729. SDValue Simp = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL);
  2730. if (Simp.getNode()) return Simp;
  2731. break;
  2732. }
  2733. case ISD::SELECT:
  2734. if (N1C) {
  2735. if (N1C->getZExtValue())
  2736. return N2; // select true, X, Y -> X
  2737. else
  2738. return N3; // select false, X, Y -> Y
  2739. }
  2740. if (N2 == N3) return N2; // select C, X, X -> X
  2741. break;
  2742. case ISD::VECTOR_SHUFFLE:
  2743. llvm_unreachable("should use getVectorShuffle constructor!");
  2744. break;
  2745. case ISD::BIT_CONVERT:
  2746. // Fold bit_convert nodes from a type to themselves.
  2747. if (N1.getValueType() == VT)
  2748. return N1;
  2749. break;
  2750. }
  2751. // Memoize node if it doesn't produce a flag.
  2752. SDNode *N;
  2753. SDVTList VTs = getVTList(VT);
  2754. if (VT != MVT::Flag) {
  2755. SDValue Ops[] = { N1, N2, N3 };
  2756. FoldingSetNodeID ID;
  2757. AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
  2758. void *IP = 0;
  2759. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  2760. return SDValue(E, 0);
  2761. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
  2762. CSEMap.InsertNode(N, IP);
  2763. } else {
  2764. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTs, N1, N2, N3);
  2765. }
  2766. AllNodes.push_back(N);
  2767. #ifndef NDEBUG
  2768. VerifySDNode(N);
  2769. #endif
  2770. return SDValue(N, 0);
  2771. }
  2772. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2773. SDValue N1, SDValue N2, SDValue N3,
  2774. SDValue N4) {
  2775. SDValue Ops[] = { N1, N2, N3, N4 };
  2776. return getNode(Opcode, DL, VT, Ops, 4);
  2777. }
  2778. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  2779. SDValue N1, SDValue N2, SDValue N3,
  2780. SDValue N4, SDValue N5) {
  2781. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  2782. return getNode(Opcode, DL, VT, Ops, 5);
  2783. }
  2784. /// getStackArgumentTokenFactor - Compute a TokenFactor to force all
  2785. /// the incoming stack arguments to be loaded from the stack.
  2786. SDValue SelectionDAG::getStackArgumentTokenFactor(SDValue Chain) {
  2787. SmallVector<SDValue, 8> ArgChains;
  2788. // Include the original chain at the beginning of the list. When this is
  2789. // used by target LowerCall hooks, this helps legalize find the
  2790. // CALLSEQ_BEGIN node.
  2791. ArgChains.push_back(Chain);
  2792. // Add a chain value for each stack argument.
  2793. for (SDNode::use_iterator U = getEntryNode().getNode()->use_begin(),
  2794. UE = getEntryNode().getNode()->use_end(); U != UE; ++U)
  2795. if (LoadSDNode *L = dyn_cast<LoadSDNode>(*U))
  2796. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
  2797. if (FI->getIndex() < 0)
  2798. ArgChains.push_back(SDValue(L, 1));
  2799. // Build a tokenfactor for all the chains.
  2800. return getNode(ISD::TokenFactor, Chain.getDebugLoc(), MVT::Other,
  2801. &ArgChains[0], ArgChains.size());
  2802. }
  2803. /// getMemsetValue - Vectorized representation of the memset value
  2804. /// operand.
  2805. static SDValue getMemsetValue(SDValue Value, EVT VT, SelectionDAG &DAG,
  2806. DebugLoc dl) {
  2807. assert(Value.getOpcode() != ISD::UNDEF);
  2808. unsigned NumBits = VT.getScalarType().getSizeInBits();
  2809. if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
  2810. APInt Val = APInt(NumBits, C->getZExtValue() & 255);
  2811. unsigned Shift = 8;
  2812. for (unsigned i = NumBits; i > 8; i >>= 1) {
  2813. Val = (Val << Shift) | Val;
  2814. Shift <<= 1;
  2815. }
  2816. if (VT.isInteger())
  2817. return DAG.getConstant(Val, VT);
  2818. return DAG.getConstantFP(APFloat(Val), VT);
  2819. }
  2820. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  2821. Value = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Value);
  2822. unsigned Shift = 8;
  2823. for (unsigned i = NumBits; i > 8; i >>= 1) {
  2824. Value = DAG.getNode(ISD::OR, dl, VT,
  2825. DAG.getNode(ISD::SHL, dl, VT, Value,
  2826. DAG.getConstant(Shift,
  2827. TLI.getShiftAmountTy())),
  2828. Value);
  2829. Shift <<= 1;
  2830. }
  2831. return Value;
  2832. }
  2833. /// getMemsetStringVal - Similar to getMemsetValue. Except this is only
  2834. /// used when a memcpy is turned into a memset when the source is a constant
  2835. /// string ptr.
  2836. static SDValue getMemsetStringVal(EVT VT, DebugLoc dl, SelectionDAG &DAG,
  2837. const TargetLowering &TLI,
  2838. std::string &Str, unsigned Offset) {
  2839. // Handle vector with all elements zero.
  2840. if (Str.empty()) {
  2841. if (VT.isInteger())
  2842. return DAG.getConstant(0, VT);
  2843. else if (VT.getSimpleVT().SimpleTy == MVT::f32 ||
  2844. VT.getSimpleVT().SimpleTy == MVT::f64)
  2845. return DAG.getConstantFP(0.0, VT);
  2846. else if (VT.isVector()) {
  2847. unsigned NumElts = VT.getVectorNumElements();
  2848. MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
  2849. return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
  2850. DAG.getConstant(0, EVT::getVectorVT(*DAG.getContext(),
  2851. EltVT, NumElts)));
  2852. } else
  2853. llvm_unreachable("Expected type!");
  2854. }
  2855. assert(!VT.isVector() && "Can't handle vector type here!");
  2856. unsigned NumBits = VT.getSizeInBits();
  2857. unsigned MSB = NumBits / 8;
  2858. uint64_t Val = 0;
  2859. if (TLI.isLittleEndian())
  2860. Offset = Offset + MSB - 1;
  2861. for (unsigned i = 0; i != MSB; ++i) {
  2862. Val = (Val << 8) | (unsigned char)Str[Offset];
  2863. Offset += TLI.isLittleEndian() ? -1 : 1;
  2864. }
  2865. return DAG.getConstant(Val, VT);
  2866. }
  2867. /// getMemBasePlusOffset - Returns base and offset node for the
  2868. ///
  2869. static SDValue getMemBasePlusOffset(SDValue Base, unsigned Offset,
  2870. SelectionDAG &DAG) {
  2871. EVT VT = Base.getValueType();
  2872. return DAG.getNode(ISD::ADD, Base.getDebugLoc(),
  2873. VT, Base, DAG.getConstant(Offset, VT));
  2874. }
  2875. /// isMemSrcFromString - Returns true if memcpy source is a string constant.
  2876. ///
  2877. static bool isMemSrcFromString(SDValue Src, std::string &Str) {
  2878. unsigned SrcDelta = 0;
  2879. GlobalAddressSDNode *G = NULL;
  2880. if (Src.getOpcode() == ISD::GlobalAddress)
  2881. G = cast<GlobalAddressSDNode>(Src);
  2882. else if (Src.getOpcode() == ISD::ADD &&
  2883. Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
  2884. Src.getOperand(1).getOpcode() == ISD::Constant) {
  2885. G = cast<GlobalAddressSDNode>(Src.getOperand(0));
  2886. SrcDelta = cast<ConstantSDNode>(Src.getOperand(1))->getZExtValue();
  2887. }
  2888. if (!G)
  2889. return false;
  2890. const GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
  2891. if (GV && GetConstantStringInfo(GV, Str, SrcDelta, false))
  2892. return true;
  2893. return false;
  2894. }
  2895. /// FindOptimalMemOpLowering - Determines the optimial series memory ops
  2896. /// to replace the memset / memcpy. Return true if the number of memory ops
  2897. /// is below the threshold. It returns the types of the sequence of
  2898. /// memory ops to perform memset / memcpy by reference.
  2899. static bool FindOptimalMemOpLowering(std::vector<EVT> &MemOps,
  2900. unsigned Limit, uint64_t Size,
  2901. unsigned DstAlign, unsigned SrcAlign,
  2902. bool NonScalarIntSafe,
  2903. bool MemcpyStrSrc,
  2904. SelectionDAG &DAG,
  2905. const TargetLowering &TLI) {
  2906. assert((SrcAlign == 0 || SrcAlign >= DstAlign) &&
  2907. "Expecting memcpy / memset source to meet alignment requirement!");
  2908. // If 'SrcAlign' is zero, that means the memory operation does not need load
  2909. // the value, i.e. memset or memcpy from constant string. Otherwise, it's
  2910. // the inferred alignment of the source. 'DstAlign', on the other hand, is the
  2911. // specified alignment of the memory operation. If it is zero, that means
  2912. // it's possible to change the alignment of the destination. 'MemcpyStrSrc'
  2913. // indicates whether the memcpy source is constant so it does not need to be
  2914. // loaded.
  2915. EVT VT = TLI.getOptimalMemOpType(Size, DstAlign, SrcAlign,
  2916. NonScalarIntSafe, MemcpyStrSrc,
  2917. DAG.getMachineFunction());
  2918. if (VT == MVT::Other) {
  2919. if (DstAlign >= TLI.getTargetData()->getPointerPrefAlignment() ||
  2920. TLI.allowsUnalignedMemoryAccesses(VT)) {
  2921. VT = TLI.getPointerTy();
  2922. } else {
  2923. switch (DstAlign & 7) {
  2924. case 0: VT = MVT::i64; break;
  2925. case 4: VT = MVT::i32; break;
  2926. case 2: VT = MVT::i16; break;
  2927. default: VT = MVT::i8; break;
  2928. }
  2929. }
  2930. MVT LVT = MVT::i64;
  2931. while (!TLI.isTypeLegal(LVT))
  2932. LVT = (MVT::SimpleValueType)(LVT.SimpleTy - 1);
  2933. assert(LVT.isInteger());
  2934. if (VT.bitsGT(LVT))
  2935. VT = LVT;
  2936. }
  2937. // If we're optimizing for size, and there is a limit, bump the maximum number
  2938. // of operations inserted down to 4. This is a wild guess that approximates
  2939. // the size of a call to memcpy or memset (3 arguments + call).
  2940. if (Limit != ~0U) {
  2941. const Function *F = DAG.getMachineFunction().getFunction();
  2942. if (F->hasFnAttr(Attribute::OptimizeForSize))
  2943. Limit = 4;
  2944. }
  2945. unsigned NumMemOps = 0;
  2946. while (Size != 0) {
  2947. unsigned VTSize = VT.getSizeInBits() / 8;
  2948. while (VTSize > Size) {
  2949. // For now, only use non-vector load / store's for the left-over pieces.
  2950. if (VT.isVector() || VT.isFloatingPoint()) {
  2951. VT = MVT::i64;
  2952. while (!TLI.isTypeLegal(VT))
  2953. VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
  2954. VTSize = VT.getSizeInBits() / 8;
  2955. } else {
  2956. // This can result in a type that is not legal on the target, e.g.
  2957. // 1 or 2 bytes on PPC.
  2958. VT = (MVT::SimpleValueType)(VT.getSimpleVT().SimpleTy - 1);
  2959. VTSize >>= 1;
  2960. }
  2961. }
  2962. if (++NumMemOps > Limit)
  2963. return false;
  2964. MemOps.push_back(VT);
  2965. Size -= VTSize;
  2966. }
  2967. return true;
  2968. }
  2969. static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
  2970. SDValue Chain, SDValue Dst,
  2971. SDValue Src, uint64_t Size,
  2972. unsigned Align, bool isVol,
  2973. bool AlwaysInline,
  2974. const Value *DstSV, uint64_t DstSVOff,
  2975. const Value *SrcSV, uint64_t SrcSVOff) {
  2976. // Turn a memcpy of undef to nop.
  2977. if (Src.getOpcode() == ISD::UNDEF)
  2978. return Chain;
  2979. // Expand memcpy to a series of load and store ops if the size operand falls
  2980. // below a certain threshold.
  2981. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  2982. std::vector<EVT> MemOps;
  2983. bool DstAlignCanChange = false;
  2984. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  2985. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  2986. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  2987. DstAlignCanChange = true;
  2988. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  2989. if (Align > SrcAlign)
  2990. SrcAlign = Align;
  2991. std::string Str;
  2992. bool CopyFromStr = isMemSrcFromString(Src, Str);
  2993. bool isZeroStr = CopyFromStr && Str.empty();
  2994. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy();
  2995. if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
  2996. (DstAlignCanChange ? 0 : Align),
  2997. (isZeroStr ? 0 : SrcAlign),
  2998. true, CopyFromStr, DAG, TLI))
  2999. return SDValue();
  3000. if (DstAlignCanChange) {
  3001. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  3002. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  3003. if (NewAlign > Align) {
  3004. // Give the stack frame object a larger alignment if needed.
  3005. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  3006. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  3007. Align = NewAlign;
  3008. }
  3009. }
  3010. SmallVector<SDValue, 8> OutChains;
  3011. unsigned NumMemOps = MemOps.size();
  3012. uint64_t SrcOff = 0, DstOff = 0;
  3013. for (unsigned i = 0; i != NumMemOps; ++i) {
  3014. EVT VT = MemOps[i];
  3015. unsigned VTSize = VT.getSizeInBits() / 8;
  3016. SDValue Value, Store;
  3017. if (CopyFromStr &&
  3018. (isZeroStr || (VT.isInteger() && !VT.isVector()))) {
  3019. // It's unlikely a store of a vector immediate can be done in a single
  3020. // instruction. It would require a load from a constantpool first.
  3021. // We only handle zero vectors here.
  3022. // FIXME: Handle other cases where store of vector immediate is done in
  3023. // a single instruction.
  3024. Value = getMemsetStringVal(VT, dl, DAG, TLI, Str, SrcOff);
  3025. Store = DAG.getStore(Chain, dl, Value,
  3026. getMemBasePlusOffset(Dst, DstOff, DAG),
  3027. DstSV, DstSVOff + DstOff, isVol, false, Align);
  3028. } else {
  3029. // The type might not be legal for the target. This should only happen
  3030. // if the type is smaller than a legal type, as on PPC, so the right
  3031. // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
  3032. // to Load/Store if NVT==VT.
  3033. // FIXME does the case above also need this?
  3034. EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
  3035. assert(NVT.bitsGE(VT));
  3036. Value = DAG.getExtLoad(ISD::EXTLOAD, NVT, dl, Chain,
  3037. getMemBasePlusOffset(Src, SrcOff, DAG),
  3038. SrcSV, SrcSVOff + SrcOff, VT, isVol, false,
  3039. MinAlign(SrcAlign, SrcOff));
  3040. Store = DAG.getTruncStore(Chain, dl, Value,
  3041. getMemBasePlusOffset(Dst, DstOff, DAG),
  3042. DstSV, DstSVOff + DstOff, VT, isVol, false,
  3043. Align);
  3044. }
  3045. OutChains.push_back(Store);
  3046. SrcOff += VTSize;
  3047. DstOff += VTSize;
  3048. }
  3049. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3050. &OutChains[0], OutChains.size());
  3051. }
  3052. static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, DebugLoc dl,
  3053. SDValue Chain, SDValue Dst,
  3054. SDValue Src, uint64_t Size,
  3055. unsigned Align, bool isVol,
  3056. bool AlwaysInline,
  3057. const Value *DstSV, uint64_t DstSVOff,
  3058. const Value *SrcSV, uint64_t SrcSVOff) {
  3059. // Turn a memmove of undef to nop.
  3060. if (Src.getOpcode() == ISD::UNDEF)
  3061. return Chain;
  3062. // Expand memmove to a series of load and store ops if the size operand falls
  3063. // below a certain threshold.
  3064. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  3065. std::vector<EVT> MemOps;
  3066. bool DstAlignCanChange = false;
  3067. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  3068. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  3069. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  3070. DstAlignCanChange = true;
  3071. unsigned SrcAlign = DAG.InferPtrAlignment(Src);
  3072. if (Align > SrcAlign)
  3073. SrcAlign = Align;
  3074. unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove();
  3075. if (!FindOptimalMemOpLowering(MemOps, Limit, Size,
  3076. (DstAlignCanChange ? 0 : Align),
  3077. SrcAlign, true, false, DAG, TLI))
  3078. return SDValue();
  3079. if (DstAlignCanChange) {
  3080. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  3081. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  3082. if (NewAlign > Align) {
  3083. // Give the stack frame object a larger alignment if needed.
  3084. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  3085. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  3086. Align = NewAlign;
  3087. }
  3088. }
  3089. uint64_t SrcOff = 0, DstOff = 0;
  3090. SmallVector<SDValue, 8> LoadValues;
  3091. SmallVector<SDValue, 8> LoadChains;
  3092. SmallVector<SDValue, 8> OutChains;
  3093. unsigned NumMemOps = MemOps.size();
  3094. for (unsigned i = 0; i < NumMemOps; i++) {
  3095. EVT VT = MemOps[i];
  3096. unsigned VTSize = VT.getSizeInBits() / 8;
  3097. SDValue Value, Store;
  3098. Value = DAG.getLoad(VT, dl, Chain,
  3099. getMemBasePlusOffset(Src, SrcOff, DAG),
  3100. SrcSV, SrcSVOff + SrcOff, isVol, false, SrcAlign);
  3101. LoadValues.push_back(Value);
  3102. LoadChains.push_back(Value.getValue(1));
  3103. SrcOff += VTSize;
  3104. }
  3105. Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3106. &LoadChains[0], LoadChains.size());
  3107. OutChains.clear();
  3108. for (unsigned i = 0; i < NumMemOps; i++) {
  3109. EVT VT = MemOps[i];
  3110. unsigned VTSize = VT.getSizeInBits() / 8;
  3111. SDValue Value, Store;
  3112. Store = DAG.getStore(Chain, dl, LoadValues[i],
  3113. getMemBasePlusOffset(Dst, DstOff, DAG),
  3114. DstSV, DstSVOff + DstOff, isVol, false, Align);
  3115. OutChains.push_back(Store);
  3116. DstOff += VTSize;
  3117. }
  3118. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3119. &OutChains[0], OutChains.size());
  3120. }
  3121. static SDValue getMemsetStores(SelectionDAG &DAG, DebugLoc dl,
  3122. SDValue Chain, SDValue Dst,
  3123. SDValue Src, uint64_t Size,
  3124. unsigned Align, bool isVol,
  3125. const Value *DstSV, uint64_t DstSVOff) {
  3126. // Turn a memset of undef to nop.
  3127. if (Src.getOpcode() == ISD::UNDEF)
  3128. return Chain;
  3129. // Expand memset to a series of load/store ops if the size operand
  3130. // falls below a certain threshold.
  3131. const TargetLowering &TLI = DAG.getTargetLoweringInfo();
  3132. std::vector<EVT> MemOps;
  3133. bool DstAlignCanChange = false;
  3134. MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
  3135. FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
  3136. if (FI && !MFI->isFixedObjectIndex(FI->getIndex()))
  3137. DstAlignCanChange = true;
  3138. bool NonScalarIntSafe =
  3139. isa<ConstantSDNode>(Src) && cast<ConstantSDNode>(Src)->isNullValue();
  3140. if (!FindOptimalMemOpLowering(MemOps, TLI.getMaxStoresPerMemset(),
  3141. Size, (DstAlignCanChange ? 0 : Align), 0,
  3142. NonScalarIntSafe, false, DAG, TLI))
  3143. return SDValue();
  3144. if (DstAlignCanChange) {
  3145. const Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
  3146. unsigned NewAlign = (unsigned) TLI.getTargetData()->getABITypeAlignment(Ty);
  3147. if (NewAlign > Align) {
  3148. // Give the stack frame object a larger alignment if needed.
  3149. if (MFI->getObjectAlignment(FI->getIndex()) < NewAlign)
  3150. MFI->setObjectAlignment(FI->getIndex(), NewAlign);
  3151. Align = NewAlign;
  3152. }
  3153. }
  3154. SmallVector<SDValue, 8> OutChains;
  3155. uint64_t DstOff = 0;
  3156. unsigned NumMemOps = MemOps.size();
  3157. for (unsigned i = 0; i < NumMemOps; i++) {
  3158. EVT VT = MemOps[i];
  3159. unsigned VTSize = VT.getSizeInBits() / 8;
  3160. SDValue Value = getMemsetValue(Src, VT, DAG, dl);
  3161. SDValue Store = DAG.getStore(Chain, dl, Value,
  3162. getMemBasePlusOffset(Dst, DstOff, DAG),
  3163. DstSV, DstSVOff + DstOff, isVol, false, 0);
  3164. OutChains.push_back(Store);
  3165. DstOff += VTSize;
  3166. }
  3167. return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
  3168. &OutChains[0], OutChains.size());
  3169. }
  3170. SDValue SelectionDAG::getMemcpy(SDValue Chain, DebugLoc dl, SDValue Dst,
  3171. SDValue Src, SDValue Size,
  3172. unsigned Align, bool isVol, bool AlwaysInline,
  3173. const Value *DstSV, uint64_t DstSVOff,
  3174. const Value *SrcSV, uint64_t SrcSVOff) {
  3175. // Check to see if we should lower the memcpy to loads and stores first.
  3176. // For cases within the target-specified limits, this is the best choice.
  3177. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3178. if (ConstantSize) {
  3179. // Memcpy with size zero? Just return the original chain.
  3180. if (ConstantSize->isNullValue())
  3181. return Chain;
  3182. SDValue Result = getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  3183. ConstantSize->getZExtValue(),Align,
  3184. isVol, false, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3185. if (Result.getNode())
  3186. return Result;
  3187. }
  3188. // Then check to see if we should lower the memcpy with target-specific
  3189. // code. If the target chooses to do this, this is the next best.
  3190. SDValue Result =
  3191. TSI.EmitTargetCodeForMemcpy(*this, dl, Chain, Dst, Src, Size, Align,
  3192. isVol, AlwaysInline,
  3193. DstSV, DstSVOff, SrcSV, SrcSVOff);
  3194. if (Result.getNode())
  3195. return Result;
  3196. // If we really need inline code and the target declined to provide it,
  3197. // use a (potentially long) sequence of loads and stores.
  3198. if (AlwaysInline) {
  3199. assert(ConstantSize && "AlwaysInline requires a constant size!");
  3200. return getMemcpyLoadsAndStores(*this, dl, Chain, Dst, Src,
  3201. ConstantSize->getZExtValue(), Align, isVol,
  3202. true, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3203. }
  3204. // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
  3205. // memcpy is not guaranteed to be safe. libc memcpys aren't required to
  3206. // respect volatile, so they may do things like read or write memory
  3207. // beyond the given memory regions. But fixing this isn't easy, and most
  3208. // people don't care.
  3209. // Emit a library call.
  3210. TargetLowering::ArgListTy Args;
  3211. TargetLowering::ArgListEntry Entry;
  3212. Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
  3213. Entry.Node = Dst; Args.push_back(Entry);
  3214. Entry.Node = Src; Args.push_back(Entry);
  3215. Entry.Node = Size; Args.push_back(Entry);
  3216. // FIXME: pass in DebugLoc
  3217. std::pair<SDValue,SDValue> CallResult =
  3218. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3219. false, false, false, false, 0,
  3220. TLI.getLibcallCallingConv(RTLIB::MEMCPY), false,
  3221. /*isReturnValueUsed=*/false,
  3222. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMCPY),
  3223. TLI.getPointerTy()),
  3224. Args, *this, dl);
  3225. return CallResult.second;
  3226. }
  3227. SDValue SelectionDAG::getMemmove(SDValue Chain, DebugLoc dl, SDValue Dst,
  3228. SDValue Src, SDValue Size,
  3229. unsigned Align, bool isVol,
  3230. const Value *DstSV, uint64_t DstSVOff,
  3231. const Value *SrcSV, uint64_t SrcSVOff) {
  3232. // Check to see if we should lower the memmove to loads and stores first.
  3233. // For cases within the target-specified limits, this is the best choice.
  3234. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3235. if (ConstantSize) {
  3236. // Memmove with size zero? Just return the original chain.
  3237. if (ConstantSize->isNullValue())
  3238. return Chain;
  3239. SDValue Result =
  3240. getMemmoveLoadsAndStores(*this, dl, Chain, Dst, Src,
  3241. ConstantSize->getZExtValue(), Align, isVol,
  3242. false, DstSV, DstSVOff, SrcSV, SrcSVOff);
  3243. if (Result.getNode())
  3244. return Result;
  3245. }
  3246. // Then check to see if we should lower the memmove with target-specific
  3247. // code. If the target chooses to do this, this is the next best.
  3248. SDValue Result =
  3249. TSI.EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size, Align, isVol,
  3250. DstSV, DstSVOff, SrcSV, SrcSVOff);
  3251. if (Result.getNode())
  3252. return Result;
  3253. // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
  3254. // not be safe. See memcpy above for more details.
  3255. // Emit a library call.
  3256. TargetLowering::ArgListTy Args;
  3257. TargetLowering::ArgListEntry Entry;
  3258. Entry.Ty = TLI.getTargetData()->getIntPtrType(*getContext());
  3259. Entry.Node = Dst; Args.push_back(Entry);
  3260. Entry.Node = Src; Args.push_back(Entry);
  3261. Entry.Node = Size; Args.push_back(Entry);
  3262. // FIXME: pass in DebugLoc
  3263. std::pair<SDValue,SDValue> CallResult =
  3264. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3265. false, false, false, false, 0,
  3266. TLI.getLibcallCallingConv(RTLIB::MEMMOVE), false,
  3267. /*isReturnValueUsed=*/false,
  3268. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMMOVE),
  3269. TLI.getPointerTy()),
  3270. Args, *this, dl);
  3271. return CallResult.second;
  3272. }
  3273. SDValue SelectionDAG::getMemset(SDValue Chain, DebugLoc dl, SDValue Dst,
  3274. SDValue Src, SDValue Size,
  3275. unsigned Align, bool isVol,
  3276. const Value *DstSV, uint64_t DstSVOff) {
  3277. // Check to see if we should lower the memset to stores first.
  3278. // For cases within the target-specified limits, this is the best choice.
  3279. ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
  3280. if (ConstantSize) {
  3281. // Memset with size zero? Just return the original chain.
  3282. if (ConstantSize->isNullValue())
  3283. return Chain;
  3284. SDValue Result =
  3285. getMemsetStores(*this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(),
  3286. Align, isVol, DstSV, DstSVOff);
  3287. if (Result.getNode())
  3288. return Result;
  3289. }
  3290. // Then check to see if we should lower the memset with target-specific
  3291. // code. If the target chooses to do this, this is the next best.
  3292. SDValue Result =
  3293. TSI.EmitTargetCodeForMemset(*this, dl, Chain, Dst, Src, Size, Align, isVol,
  3294. DstSV, DstSVOff);
  3295. if (Result.getNode())
  3296. return Result;
  3297. // Emit a library call.
  3298. const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*getContext());
  3299. TargetLowering::ArgListTy Args;
  3300. TargetLowering::ArgListEntry Entry;
  3301. Entry.Node = Dst; Entry.Ty = IntPtrTy;
  3302. Args.push_back(Entry);
  3303. // Extend or truncate the argument to be an i32 value for the call.
  3304. if (Src.getValueType().bitsGT(MVT::i32))
  3305. Src = getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
  3306. else
  3307. Src = getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
  3308. Entry.Node = Src;
  3309. Entry.Ty = Type::getInt32Ty(*getContext());
  3310. Entry.isSExt = true;
  3311. Args.push_back(Entry);
  3312. Entry.Node = Size;
  3313. Entry.Ty = IntPtrTy;
  3314. Entry.isSExt = false;
  3315. Args.push_back(Entry);
  3316. // FIXME: pass in DebugLoc
  3317. std::pair<SDValue,SDValue> CallResult =
  3318. TLI.LowerCallTo(Chain, Type::getVoidTy(*getContext()),
  3319. false, false, false, false, 0,
  3320. TLI.getLibcallCallingConv(RTLIB::MEMSET), false,
  3321. /*isReturnValueUsed=*/false,
  3322. getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
  3323. TLI.getPointerTy()),
  3324. Args, *this, dl);
  3325. return CallResult.second;
  3326. }
  3327. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3328. SDValue Chain,
  3329. SDValue Ptr, SDValue Cmp,
  3330. SDValue Swp, const Value* PtrVal,
  3331. unsigned Alignment) {
  3332. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3333. Alignment = getEVTAlignment(MemVT);
  3334. // Check if the memory reference references a frame index
  3335. if (!PtrVal)
  3336. if (const FrameIndexSDNode *FI =
  3337. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3338. PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
  3339. MachineFunction &MF = getMachineFunction();
  3340. unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
  3341. // For now, atomics are considered to be volatile always.
  3342. Flags |= MachineMemOperand::MOVolatile;
  3343. MachineMemOperand *MMO =
  3344. MF.getMachineMemOperand(PtrVal, Flags, 0,
  3345. MemVT.getStoreSize(), Alignment);
  3346. return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Cmp, Swp, MMO);
  3347. }
  3348. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3349. SDValue Chain,
  3350. SDValue Ptr, SDValue Cmp,
  3351. SDValue Swp, MachineMemOperand *MMO) {
  3352. assert(Opcode == ISD::ATOMIC_CMP_SWAP && "Invalid Atomic Op");
  3353. assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
  3354. EVT VT = Cmp.getValueType();
  3355. SDVTList VTs = getVTList(VT, MVT::Other);
  3356. FoldingSetNodeID ID;
  3357. ID.AddInteger(MemVT.getRawBits());
  3358. SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
  3359. AddNodeIDNode(ID, Opcode, VTs, Ops, 4);
  3360. void* IP = 0;
  3361. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3362. cast<AtomicSDNode>(E)->refineAlignment(MMO);
  3363. return SDValue(E, 0);
  3364. }
  3365. SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
  3366. Ptr, Cmp, Swp, MMO);
  3367. CSEMap.InsertNode(N, IP);
  3368. AllNodes.push_back(N);
  3369. return SDValue(N, 0);
  3370. }
  3371. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3372. SDValue Chain,
  3373. SDValue Ptr, SDValue Val,
  3374. const Value* PtrVal,
  3375. unsigned Alignment) {
  3376. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3377. Alignment = getEVTAlignment(MemVT);
  3378. // Check if the memory reference references a frame index
  3379. if (!PtrVal)
  3380. if (const FrameIndexSDNode *FI =
  3381. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3382. PtrVal = PseudoSourceValue::getFixedStack(FI->getIndex());
  3383. MachineFunction &MF = getMachineFunction();
  3384. unsigned Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
  3385. // For now, atomics are considered to be volatile always.
  3386. Flags |= MachineMemOperand::MOVolatile;
  3387. MachineMemOperand *MMO =
  3388. MF.getMachineMemOperand(PtrVal, Flags, 0,
  3389. MemVT.getStoreSize(), Alignment);
  3390. return getAtomic(Opcode, dl, MemVT, Chain, Ptr, Val, MMO);
  3391. }
  3392. SDValue SelectionDAG::getAtomic(unsigned Opcode, DebugLoc dl, EVT MemVT,
  3393. SDValue Chain,
  3394. SDValue Ptr, SDValue Val,
  3395. MachineMemOperand *MMO) {
  3396. assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
  3397. Opcode == ISD::ATOMIC_LOAD_SUB ||
  3398. Opcode == ISD::ATOMIC_LOAD_AND ||
  3399. Opcode == ISD::ATOMIC_LOAD_OR ||
  3400. Opcode == ISD::ATOMIC_LOAD_XOR ||
  3401. Opcode == ISD::ATOMIC_LOAD_NAND ||
  3402. Opcode == ISD::ATOMIC_LOAD_MIN ||
  3403. Opcode == ISD::ATOMIC_LOAD_MAX ||
  3404. Opcode == ISD::ATOMIC_LOAD_UMIN ||
  3405. Opcode == ISD::ATOMIC_LOAD_UMAX ||
  3406. Opcode == ISD::ATOMIC_SWAP) &&
  3407. "Invalid Atomic Op");
  3408. EVT VT = Val.getValueType();
  3409. SDVTList VTs = getVTList(VT, MVT::Other);
  3410. FoldingSetNodeID ID;
  3411. ID.AddInteger(MemVT.getRawBits());
  3412. SDValue Ops[] = {Chain, Ptr, Val};
  3413. AddNodeIDNode(ID, Opcode, VTs, Ops, 3);
  3414. void* IP = 0;
  3415. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3416. cast<AtomicSDNode>(E)->refineAlignment(MMO);
  3417. return SDValue(E, 0);
  3418. }
  3419. SDNode *N = new (NodeAllocator) AtomicSDNode(Opcode, dl, VTs, MemVT, Chain,
  3420. Ptr, Val, MMO);
  3421. CSEMap.InsertNode(N, IP);
  3422. AllNodes.push_back(N);
  3423. return SDValue(N, 0);
  3424. }
  3425. /// getMergeValues - Create a MERGE_VALUES node from the given operands.
  3426. /// Allowed to return something different (and simpler) if Simplify is true.
  3427. SDValue SelectionDAG::getMergeValues(const SDValue *Ops, unsigned NumOps,
  3428. DebugLoc dl) {
  3429. if (NumOps == 1)
  3430. return Ops[0];
  3431. SmallVector<EVT, 4> VTs;
  3432. VTs.reserve(NumOps);
  3433. for (unsigned i = 0; i < NumOps; ++i)
  3434. VTs.push_back(Ops[i].getValueType());
  3435. return getNode(ISD::MERGE_VALUES, dl, getVTList(&VTs[0], NumOps),
  3436. Ops, NumOps);
  3437. }
  3438. SDValue
  3439. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl,
  3440. const EVT *VTs, unsigned NumVTs,
  3441. const SDValue *Ops, unsigned NumOps,
  3442. EVT MemVT, const Value *srcValue, int SVOff,
  3443. unsigned Align, bool Vol,
  3444. bool ReadMem, bool WriteMem) {
  3445. return getMemIntrinsicNode(Opcode, dl, makeVTList(VTs, NumVTs), Ops, NumOps,
  3446. MemVT, srcValue, SVOff, Align, Vol,
  3447. ReadMem, WriteMem);
  3448. }
  3449. SDValue
  3450. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
  3451. const SDValue *Ops, unsigned NumOps,
  3452. EVT MemVT, const Value *srcValue, int SVOff,
  3453. unsigned Align, bool Vol,
  3454. bool ReadMem, bool WriteMem) {
  3455. if (Align == 0) // Ensure that codegen never sees alignment 0
  3456. Align = getEVTAlignment(MemVT);
  3457. MachineFunction &MF = getMachineFunction();
  3458. unsigned Flags = 0;
  3459. if (WriteMem)
  3460. Flags |= MachineMemOperand::MOStore;
  3461. if (ReadMem)
  3462. Flags |= MachineMemOperand::MOLoad;
  3463. if (Vol)
  3464. Flags |= MachineMemOperand::MOVolatile;
  3465. MachineMemOperand *MMO =
  3466. MF.getMachineMemOperand(srcValue, Flags, SVOff,
  3467. MemVT.getStoreSize(), Align);
  3468. return getMemIntrinsicNode(Opcode, dl, VTList, Ops, NumOps, MemVT, MMO);
  3469. }
  3470. SDValue
  3471. SelectionDAG::getMemIntrinsicNode(unsigned Opcode, DebugLoc dl, SDVTList VTList,
  3472. const SDValue *Ops, unsigned NumOps,
  3473. EVT MemVT, MachineMemOperand *MMO) {
  3474. assert((Opcode == ISD::INTRINSIC_VOID ||
  3475. Opcode == ISD::INTRINSIC_W_CHAIN ||
  3476. (Opcode <= INT_MAX &&
  3477. (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
  3478. "Opcode is not a memory-accessing opcode!");
  3479. // Memoize the node unless it returns a flag.
  3480. MemIntrinsicSDNode *N;
  3481. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  3482. FoldingSetNodeID ID;
  3483. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  3484. void *IP = 0;
  3485. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3486. cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
  3487. return SDValue(E, 0);
  3488. }
  3489. N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
  3490. MemVT, MMO);
  3491. CSEMap.InsertNode(N, IP);
  3492. } else {
  3493. N = new (NodeAllocator) MemIntrinsicSDNode(Opcode, dl, VTList, Ops, NumOps,
  3494. MemVT, MMO);
  3495. }
  3496. AllNodes.push_back(N);
  3497. return SDValue(N, 0);
  3498. }
  3499. SDValue
  3500. SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  3501. EVT VT, DebugLoc dl, SDValue Chain,
  3502. SDValue Ptr, SDValue Offset,
  3503. const Value *SV, int SVOffset, EVT MemVT,
  3504. bool isVolatile, bool isNonTemporal,
  3505. unsigned Alignment) {
  3506. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3507. Alignment = getEVTAlignment(VT);
  3508. // Check if the memory reference references a frame index
  3509. if (!SV)
  3510. if (const FrameIndexSDNode *FI =
  3511. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3512. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3513. MachineFunction &MF = getMachineFunction();
  3514. unsigned Flags = MachineMemOperand::MOLoad;
  3515. if (isVolatile)
  3516. Flags |= MachineMemOperand::MOVolatile;
  3517. if (isNonTemporal)
  3518. Flags |= MachineMemOperand::MONonTemporal;
  3519. MachineMemOperand *MMO =
  3520. MF.getMachineMemOperand(SV, Flags, SVOffset,
  3521. MemVT.getStoreSize(), Alignment);
  3522. return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
  3523. }
  3524. SDValue
  3525. SelectionDAG::getLoad(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType,
  3526. EVT VT, DebugLoc dl, SDValue Chain,
  3527. SDValue Ptr, SDValue Offset, EVT MemVT,
  3528. MachineMemOperand *MMO) {
  3529. if (VT == MemVT) {
  3530. ExtType = ISD::NON_EXTLOAD;
  3531. } else if (ExtType == ISD::NON_EXTLOAD) {
  3532. assert(VT == MemVT && "Non-extending load from different memory type!");
  3533. } else {
  3534. // Extending load.
  3535. assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
  3536. "Should only be an extending load, not truncating!");
  3537. assert(VT.isInteger() == MemVT.isInteger() &&
  3538. "Cannot convert from FP to Int or Int -> FP!");
  3539. assert(VT.isVector() == MemVT.isVector() &&
  3540. "Cannot use trunc store to convert to or from a vector!");
  3541. assert((!VT.isVector() ||
  3542. VT.getVectorNumElements() == MemVT.getVectorNumElements()) &&
  3543. "Cannot use trunc store to change the number of vector elements!");
  3544. }
  3545. bool Indexed = AM != ISD::UNINDEXED;
  3546. assert((Indexed || Offset.getOpcode() == ISD::UNDEF) &&
  3547. "Unindexed load with an offset!");
  3548. SDVTList VTs = Indexed ?
  3549. getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
  3550. SDValue Ops[] = { Chain, Ptr, Offset };
  3551. FoldingSetNodeID ID;
  3552. AddNodeIDNode(ID, ISD::LOAD, VTs, Ops, 3);
  3553. ID.AddInteger(MemVT.getRawBits());
  3554. ID.AddInteger(encodeMemSDNodeFlags(ExtType, AM, MMO->isVolatile(),
  3555. MMO->isNonTemporal()));
  3556. void *IP = 0;
  3557. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3558. cast<LoadSDNode>(E)->refineAlignment(MMO);
  3559. return SDValue(E, 0);
  3560. }
  3561. SDNode *N = new (NodeAllocator) LoadSDNode(Ops, dl, VTs, AM, ExtType,
  3562. MemVT, MMO);
  3563. CSEMap.InsertNode(N, IP);
  3564. AllNodes.push_back(N);
  3565. return SDValue(N, 0);
  3566. }
  3567. SDValue SelectionDAG::getLoad(EVT VT, DebugLoc dl,
  3568. SDValue Chain, SDValue Ptr,
  3569. const Value *SV, int SVOffset,
  3570. bool isVolatile, bool isNonTemporal,
  3571. unsigned Alignment) {
  3572. SDValue Undef = getUNDEF(Ptr.getValueType());
  3573. return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
  3574. SV, SVOffset, VT, isVolatile, isNonTemporal, Alignment);
  3575. }
  3576. SDValue SelectionDAG::getExtLoad(ISD::LoadExtType ExtType, EVT VT, DebugLoc dl,
  3577. SDValue Chain, SDValue Ptr,
  3578. const Value *SV,
  3579. int SVOffset, EVT MemVT,
  3580. bool isVolatile, bool isNonTemporal,
  3581. unsigned Alignment) {
  3582. SDValue Undef = getUNDEF(Ptr.getValueType());
  3583. return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
  3584. SV, SVOffset, MemVT, isVolatile, isNonTemporal, Alignment);
  3585. }
  3586. SDValue
  3587. SelectionDAG::getIndexedLoad(SDValue OrigLoad, DebugLoc dl, SDValue Base,
  3588. SDValue Offset, ISD::MemIndexedMode AM) {
  3589. LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
  3590. assert(LD->getOffset().getOpcode() == ISD::UNDEF &&
  3591. "Load is already a indexed load!");
  3592. return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
  3593. LD->getChain(), Base, Offset, LD->getSrcValue(),
  3594. LD->getSrcValueOffset(), LD->getMemoryVT(),
  3595. LD->isVolatile(), LD->isNonTemporal(), LD->getAlignment());
  3596. }
  3597. SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3598. SDValue Ptr, const Value *SV, int SVOffset,
  3599. bool isVolatile, bool isNonTemporal,
  3600. unsigned Alignment) {
  3601. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3602. Alignment = getEVTAlignment(Val.getValueType());
  3603. // Check if the memory reference references a frame index
  3604. if (!SV)
  3605. if (const FrameIndexSDNode *FI =
  3606. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3607. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3608. MachineFunction &MF = getMachineFunction();
  3609. unsigned Flags = MachineMemOperand::MOStore;
  3610. if (isVolatile)
  3611. Flags |= MachineMemOperand::MOVolatile;
  3612. if (isNonTemporal)
  3613. Flags |= MachineMemOperand::MONonTemporal;
  3614. MachineMemOperand *MMO =
  3615. MF.getMachineMemOperand(SV, Flags, SVOffset,
  3616. Val.getValueType().getStoreSize(), Alignment);
  3617. return getStore(Chain, dl, Val, Ptr, MMO);
  3618. }
  3619. SDValue SelectionDAG::getStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3620. SDValue Ptr, MachineMemOperand *MMO) {
  3621. EVT VT = Val.getValueType();
  3622. SDVTList VTs = getVTList(MVT::Other);
  3623. SDValue Undef = getUNDEF(Ptr.getValueType());
  3624. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  3625. FoldingSetNodeID ID;
  3626. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3627. ID.AddInteger(VT.getRawBits());
  3628. ID.AddInteger(encodeMemSDNodeFlags(false, ISD::UNINDEXED, MMO->isVolatile(),
  3629. MMO->isNonTemporal()));
  3630. void *IP = 0;
  3631. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3632. cast<StoreSDNode>(E)->refineAlignment(MMO);
  3633. return SDValue(E, 0);
  3634. }
  3635. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
  3636. false, VT, MMO);
  3637. CSEMap.InsertNode(N, IP);
  3638. AllNodes.push_back(N);
  3639. return SDValue(N, 0);
  3640. }
  3641. SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3642. SDValue Ptr, const Value *SV,
  3643. int SVOffset, EVT SVT,
  3644. bool isVolatile, bool isNonTemporal,
  3645. unsigned Alignment) {
  3646. if (Alignment == 0) // Ensure that codegen never sees alignment 0
  3647. Alignment = getEVTAlignment(SVT);
  3648. // Check if the memory reference references a frame index
  3649. if (!SV)
  3650. if (const FrameIndexSDNode *FI =
  3651. dyn_cast<const FrameIndexSDNode>(Ptr.getNode()))
  3652. SV = PseudoSourceValue::getFixedStack(FI->getIndex());
  3653. MachineFunction &MF = getMachineFunction();
  3654. unsigned Flags = MachineMemOperand::MOStore;
  3655. if (isVolatile)
  3656. Flags |= MachineMemOperand::MOVolatile;
  3657. if (isNonTemporal)
  3658. Flags |= MachineMemOperand::MONonTemporal;
  3659. MachineMemOperand *MMO =
  3660. MF.getMachineMemOperand(SV, Flags, SVOffset, SVT.getStoreSize(), Alignment);
  3661. return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
  3662. }
  3663. SDValue SelectionDAG::getTruncStore(SDValue Chain, DebugLoc dl, SDValue Val,
  3664. SDValue Ptr, EVT SVT,
  3665. MachineMemOperand *MMO) {
  3666. EVT VT = Val.getValueType();
  3667. if (VT == SVT)
  3668. return getStore(Chain, dl, Val, Ptr, MMO);
  3669. assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
  3670. "Should only be a truncating store, not extending!");
  3671. assert(VT.isInteger() == SVT.isInteger() &&
  3672. "Can't do FP-INT conversion!");
  3673. assert(VT.isVector() == SVT.isVector() &&
  3674. "Cannot use trunc store to convert to or from a vector!");
  3675. assert((!VT.isVector() ||
  3676. VT.getVectorNumElements() == SVT.getVectorNumElements()) &&
  3677. "Cannot use trunc store to change the number of vector elements!");
  3678. SDVTList VTs = getVTList(MVT::Other);
  3679. SDValue Undef = getUNDEF(Ptr.getValueType());
  3680. SDValue Ops[] = { Chain, Val, Ptr, Undef };
  3681. FoldingSetNodeID ID;
  3682. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3683. ID.AddInteger(SVT.getRawBits());
  3684. ID.AddInteger(encodeMemSDNodeFlags(true, ISD::UNINDEXED, MMO->isVolatile(),
  3685. MMO->isNonTemporal()));
  3686. void *IP = 0;
  3687. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP)) {
  3688. cast<StoreSDNode>(E)->refineAlignment(MMO);
  3689. return SDValue(E, 0);
  3690. }
  3691. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, ISD::UNINDEXED,
  3692. true, SVT, MMO);
  3693. CSEMap.InsertNode(N, IP);
  3694. AllNodes.push_back(N);
  3695. return SDValue(N, 0);
  3696. }
  3697. SDValue
  3698. SelectionDAG::getIndexedStore(SDValue OrigStore, DebugLoc dl, SDValue Base,
  3699. SDValue Offset, ISD::MemIndexedMode AM) {
  3700. StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
  3701. assert(ST->getOffset().getOpcode() == ISD::UNDEF &&
  3702. "Store is already a indexed store!");
  3703. SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
  3704. SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
  3705. FoldingSetNodeID ID;
  3706. AddNodeIDNode(ID, ISD::STORE, VTs, Ops, 4);
  3707. ID.AddInteger(ST->getMemoryVT().getRawBits());
  3708. ID.AddInteger(ST->getRawSubclassData());
  3709. void *IP = 0;
  3710. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3711. return SDValue(E, 0);
  3712. SDNode *N = new (NodeAllocator) StoreSDNode(Ops, dl, VTs, AM,
  3713. ST->isTruncatingStore(),
  3714. ST->getMemoryVT(),
  3715. ST->getMemOperand());
  3716. CSEMap.InsertNode(N, IP);
  3717. AllNodes.push_back(N);
  3718. return SDValue(N, 0);
  3719. }
  3720. SDValue SelectionDAG::getVAArg(EVT VT, DebugLoc dl,
  3721. SDValue Chain, SDValue Ptr,
  3722. SDValue SV,
  3723. unsigned Align) {
  3724. SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, MVT::i32) };
  3725. return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops, 4);
  3726. }
  3727. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  3728. const SDUse *Ops, unsigned NumOps) {
  3729. switch (NumOps) {
  3730. case 0: return getNode(Opcode, DL, VT);
  3731. case 1: return getNode(Opcode, DL, VT, Ops[0]);
  3732. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
  3733. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
  3734. default: break;
  3735. }
  3736. // Copy from an SDUse array into an SDValue array for use with
  3737. // the regular getNode logic.
  3738. SmallVector<SDValue, 8> NewOps(Ops, Ops + NumOps);
  3739. return getNode(Opcode, DL, VT, &NewOps[0], NumOps);
  3740. }
  3741. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, EVT VT,
  3742. const SDValue *Ops, unsigned NumOps) {
  3743. switch (NumOps) {
  3744. case 0: return getNode(Opcode, DL, VT);
  3745. case 1: return getNode(Opcode, DL, VT, Ops[0]);
  3746. case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
  3747. case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
  3748. default: break;
  3749. }
  3750. switch (Opcode) {
  3751. default: break;
  3752. case ISD::SELECT_CC: {
  3753. assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
  3754. assert(Ops[0].getValueType() == Ops[1].getValueType() &&
  3755. "LHS and RHS of condition must have same type!");
  3756. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  3757. "True and False arms of SelectCC must have same type!");
  3758. assert(Ops[2].getValueType() == VT &&
  3759. "select_cc node must be of same type as true and false value!");
  3760. break;
  3761. }
  3762. case ISD::BR_CC: {
  3763. assert(NumOps == 5 && "BR_CC takes 5 operands!");
  3764. assert(Ops[2].getValueType() == Ops[3].getValueType() &&
  3765. "LHS/RHS of comparison should match types!");
  3766. break;
  3767. }
  3768. }
  3769. // Memoize nodes.
  3770. SDNode *N;
  3771. SDVTList VTs = getVTList(VT);
  3772. if (VT != MVT::Flag) {
  3773. FoldingSetNodeID ID;
  3774. AddNodeIDNode(ID, Opcode, VTs, Ops, NumOps);
  3775. void *IP = 0;
  3776. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3777. return SDValue(E, 0);
  3778. N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
  3779. CSEMap.InsertNode(N, IP);
  3780. } else {
  3781. N = new (NodeAllocator) SDNode(Opcode, DL, VTs, Ops, NumOps);
  3782. }
  3783. AllNodes.push_back(N);
  3784. #ifndef NDEBUG
  3785. VerifySDNode(N);
  3786. #endif
  3787. return SDValue(N, 0);
  3788. }
  3789. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  3790. const std::vector<EVT> &ResultTys,
  3791. const SDValue *Ops, unsigned NumOps) {
  3792. return getNode(Opcode, DL, getVTList(&ResultTys[0], ResultTys.size()),
  3793. Ops, NumOps);
  3794. }
  3795. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL,
  3796. const EVT *VTs, unsigned NumVTs,
  3797. const SDValue *Ops, unsigned NumOps) {
  3798. if (NumVTs == 1)
  3799. return getNode(Opcode, DL, VTs[0], Ops, NumOps);
  3800. return getNode(Opcode, DL, makeVTList(VTs, NumVTs), Ops, NumOps);
  3801. }
  3802. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3803. const SDValue *Ops, unsigned NumOps) {
  3804. if (VTList.NumVTs == 1)
  3805. return getNode(Opcode, DL, VTList.VTs[0], Ops, NumOps);
  3806. #if 0
  3807. switch (Opcode) {
  3808. // FIXME: figure out how to safely handle things like
  3809. // int foo(int x) { return 1 << (x & 255); }
  3810. // int bar() { return foo(256); }
  3811. case ISD::SRA_PARTS:
  3812. case ISD::SRL_PARTS:
  3813. case ISD::SHL_PARTS:
  3814. if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
  3815. cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
  3816. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  3817. else if (N3.getOpcode() == ISD::AND)
  3818. if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
  3819. // If the and is only masking out bits that cannot effect the shift,
  3820. // eliminate the and.
  3821. unsigned NumBits = VT.getScalarType().getSizeInBits()*2;
  3822. if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
  3823. return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
  3824. }
  3825. break;
  3826. }
  3827. #endif
  3828. // Memoize the node unless it returns a flag.
  3829. SDNode *N;
  3830. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  3831. FoldingSetNodeID ID;
  3832. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  3833. void *IP = 0;
  3834. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  3835. return SDValue(E, 0);
  3836. if (NumOps == 1) {
  3837. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
  3838. } else if (NumOps == 2) {
  3839. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
  3840. } else if (NumOps == 3) {
  3841. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
  3842. Ops[2]);
  3843. } else {
  3844. N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
  3845. }
  3846. CSEMap.InsertNode(N, IP);
  3847. } else {
  3848. if (NumOps == 1) {
  3849. N = new (NodeAllocator) UnarySDNode(Opcode, DL, VTList, Ops[0]);
  3850. } else if (NumOps == 2) {
  3851. N = new (NodeAllocator) BinarySDNode(Opcode, DL, VTList, Ops[0], Ops[1]);
  3852. } else if (NumOps == 3) {
  3853. N = new (NodeAllocator) TernarySDNode(Opcode, DL, VTList, Ops[0], Ops[1],
  3854. Ops[2]);
  3855. } else {
  3856. N = new (NodeAllocator) SDNode(Opcode, DL, VTList, Ops, NumOps);
  3857. }
  3858. }
  3859. AllNodes.push_back(N);
  3860. #ifndef NDEBUG
  3861. VerifySDNode(N);
  3862. #endif
  3863. return SDValue(N, 0);
  3864. }
  3865. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList) {
  3866. return getNode(Opcode, DL, VTList, 0, 0);
  3867. }
  3868. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3869. SDValue N1) {
  3870. SDValue Ops[] = { N1 };
  3871. return getNode(Opcode, DL, VTList, Ops, 1);
  3872. }
  3873. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3874. SDValue N1, SDValue N2) {
  3875. SDValue Ops[] = { N1, N2 };
  3876. return getNode(Opcode, DL, VTList, Ops, 2);
  3877. }
  3878. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3879. SDValue N1, SDValue N2, SDValue N3) {
  3880. SDValue Ops[] = { N1, N2, N3 };
  3881. return getNode(Opcode, DL, VTList, Ops, 3);
  3882. }
  3883. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3884. SDValue N1, SDValue N2, SDValue N3,
  3885. SDValue N4) {
  3886. SDValue Ops[] = { N1, N2, N3, N4 };
  3887. return getNode(Opcode, DL, VTList, Ops, 4);
  3888. }
  3889. SDValue SelectionDAG::getNode(unsigned Opcode, DebugLoc DL, SDVTList VTList,
  3890. SDValue N1, SDValue N2, SDValue N3,
  3891. SDValue N4, SDValue N5) {
  3892. SDValue Ops[] = { N1, N2, N3, N4, N5 };
  3893. return getNode(Opcode, DL, VTList, Ops, 5);
  3894. }
  3895. SDVTList SelectionDAG::getVTList(EVT VT) {
  3896. return makeVTList(SDNode::getValueTypeList(VT), 1);
  3897. }
  3898. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2) {
  3899. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3900. E = VTList.rend(); I != E; ++I)
  3901. if (I->NumVTs == 2 && I->VTs[0] == VT1 && I->VTs[1] == VT2)
  3902. return *I;
  3903. EVT *Array = Allocator.Allocate<EVT>(2);
  3904. Array[0] = VT1;
  3905. Array[1] = VT2;
  3906. SDVTList Result = makeVTList(Array, 2);
  3907. VTList.push_back(Result);
  3908. return Result;
  3909. }
  3910. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3) {
  3911. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3912. E = VTList.rend(); I != E; ++I)
  3913. if (I->NumVTs == 3 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
  3914. I->VTs[2] == VT3)
  3915. return *I;
  3916. EVT *Array = Allocator.Allocate<EVT>(3);
  3917. Array[0] = VT1;
  3918. Array[1] = VT2;
  3919. Array[2] = VT3;
  3920. SDVTList Result = makeVTList(Array, 3);
  3921. VTList.push_back(Result);
  3922. return Result;
  3923. }
  3924. SDVTList SelectionDAG::getVTList(EVT VT1, EVT VT2, EVT VT3, EVT VT4) {
  3925. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3926. E = VTList.rend(); I != E; ++I)
  3927. if (I->NumVTs == 4 && I->VTs[0] == VT1 && I->VTs[1] == VT2 &&
  3928. I->VTs[2] == VT3 && I->VTs[3] == VT4)
  3929. return *I;
  3930. EVT *Array = Allocator.Allocate<EVT>(4);
  3931. Array[0] = VT1;
  3932. Array[1] = VT2;
  3933. Array[2] = VT3;
  3934. Array[3] = VT4;
  3935. SDVTList Result = makeVTList(Array, 4);
  3936. VTList.push_back(Result);
  3937. return Result;
  3938. }
  3939. SDVTList SelectionDAG::getVTList(const EVT *VTs, unsigned NumVTs) {
  3940. switch (NumVTs) {
  3941. case 0: llvm_unreachable("Cannot have nodes without results!");
  3942. case 1: return getVTList(VTs[0]);
  3943. case 2: return getVTList(VTs[0], VTs[1]);
  3944. case 3: return getVTList(VTs[0], VTs[1], VTs[2]);
  3945. case 4: return getVTList(VTs[0], VTs[1], VTs[2], VTs[3]);
  3946. default: break;
  3947. }
  3948. for (std::vector<SDVTList>::reverse_iterator I = VTList.rbegin(),
  3949. E = VTList.rend(); I != E; ++I) {
  3950. if (I->NumVTs != NumVTs || VTs[0] != I->VTs[0] || VTs[1] != I->VTs[1])
  3951. continue;
  3952. bool NoMatch = false;
  3953. for (unsigned i = 2; i != NumVTs; ++i)
  3954. if (VTs[i] != I->VTs[i]) {
  3955. NoMatch = true;
  3956. break;
  3957. }
  3958. if (!NoMatch)
  3959. return *I;
  3960. }
  3961. EVT *Array = Allocator.Allocate<EVT>(NumVTs);
  3962. std::copy(VTs, VTs+NumVTs, Array);
  3963. SDVTList Result = makeVTList(Array, NumVTs);
  3964. VTList.push_back(Result);
  3965. return Result;
  3966. }
  3967. /// UpdateNodeOperands - *Mutate* the specified node in-place to have the
  3968. /// specified operands. If the resultant node already exists in the DAG,
  3969. /// this does not modify the specified node, instead it returns the node that
  3970. /// already exists. If the resultant node does not exist in the DAG, the
  3971. /// input node is returned. As a degenerate case, if you specify the same
  3972. /// input operands as the node already has, the input node is returned.
  3973. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op) {
  3974. assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
  3975. // Check to see if there is no change.
  3976. if (Op == N->getOperand(0)) return N;
  3977. // See if the modified node already exists.
  3978. void *InsertPos = 0;
  3979. if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
  3980. return Existing;
  3981. // Nope it doesn't. Remove the node from its current place in the maps.
  3982. if (InsertPos)
  3983. if (!RemoveNodeFromCSEMaps(N))
  3984. InsertPos = 0;
  3985. // Now we update the operands.
  3986. N->OperandList[0].set(Op);
  3987. // If this gets put into a CSE map, add it.
  3988. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  3989. return N;
  3990. }
  3991. SDNode *SelectionDAG::UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2) {
  3992. assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
  3993. // Check to see if there is no change.
  3994. if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
  3995. return N; // No operands changed, just return the input node.
  3996. // See if the modified node already exists.
  3997. void *InsertPos = 0;
  3998. if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
  3999. return Existing;
  4000. // Nope it doesn't. Remove the node from its current place in the maps.
  4001. if (InsertPos)
  4002. if (!RemoveNodeFromCSEMaps(N))
  4003. InsertPos = 0;
  4004. // Now we update the operands.
  4005. if (N->OperandList[0] != Op1)
  4006. N->OperandList[0].set(Op1);
  4007. if (N->OperandList[1] != Op2)
  4008. N->OperandList[1].set(Op2);
  4009. // If this gets put into a CSE map, add it.
  4010. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  4011. return N;
  4012. }
  4013. SDNode *SelectionDAG::
  4014. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2, SDValue Op3) {
  4015. SDValue Ops[] = { Op1, Op2, Op3 };
  4016. return UpdateNodeOperands(N, Ops, 3);
  4017. }
  4018. SDNode *SelectionDAG::
  4019. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  4020. SDValue Op3, SDValue Op4) {
  4021. SDValue Ops[] = { Op1, Op2, Op3, Op4 };
  4022. return UpdateNodeOperands(N, Ops, 4);
  4023. }
  4024. SDNode *SelectionDAG::
  4025. UpdateNodeOperands(SDNode *N, SDValue Op1, SDValue Op2,
  4026. SDValue Op3, SDValue Op4, SDValue Op5) {
  4027. SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
  4028. return UpdateNodeOperands(N, Ops, 5);
  4029. }
  4030. SDNode *SelectionDAG::
  4031. UpdateNodeOperands(SDNode *N, const SDValue *Ops, unsigned NumOps) {
  4032. assert(N->getNumOperands() == NumOps &&
  4033. "Update with wrong number of operands");
  4034. // Check to see if there is no change.
  4035. bool AnyChange = false;
  4036. for (unsigned i = 0; i != NumOps; ++i) {
  4037. if (Ops[i] != N->getOperand(i)) {
  4038. AnyChange = true;
  4039. break;
  4040. }
  4041. }
  4042. // No operands changed, just return the input node.
  4043. if (!AnyChange) return N;
  4044. // See if the modified node already exists.
  4045. void *InsertPos = 0;
  4046. if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, NumOps, InsertPos))
  4047. return Existing;
  4048. // Nope it doesn't. Remove the node from its current place in the maps.
  4049. if (InsertPos)
  4050. if (!RemoveNodeFromCSEMaps(N))
  4051. InsertPos = 0;
  4052. // Now we update the operands.
  4053. for (unsigned i = 0; i != NumOps; ++i)
  4054. if (N->OperandList[i] != Ops[i])
  4055. N->OperandList[i].set(Ops[i]);
  4056. // If this gets put into a CSE map, add it.
  4057. if (InsertPos) CSEMap.InsertNode(N, InsertPos);
  4058. return N;
  4059. }
  4060. /// DropOperands - Release the operands and set this node to have
  4061. /// zero operands.
  4062. void SDNode::DropOperands() {
  4063. // Unlike the code in MorphNodeTo that does this, we don't need to
  4064. // watch for dead nodes here.
  4065. for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
  4066. SDUse &Use = *I++;
  4067. Use.set(SDValue());
  4068. }
  4069. }
  4070. /// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
  4071. /// machine opcode.
  4072. ///
  4073. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4074. EVT VT) {
  4075. SDVTList VTs = getVTList(VT);
  4076. return SelectNodeTo(N, MachineOpc, VTs, 0, 0);
  4077. }
  4078. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4079. EVT VT, SDValue Op1) {
  4080. SDVTList VTs = getVTList(VT);
  4081. SDValue Ops[] = { Op1 };
  4082. return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
  4083. }
  4084. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4085. EVT VT, SDValue Op1,
  4086. SDValue Op2) {
  4087. SDVTList VTs = getVTList(VT);
  4088. SDValue Ops[] = { Op1, Op2 };
  4089. return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
  4090. }
  4091. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4092. EVT VT, SDValue Op1,
  4093. SDValue Op2, SDValue Op3) {
  4094. SDVTList VTs = getVTList(VT);
  4095. SDValue Ops[] = { Op1, Op2, Op3 };
  4096. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4097. }
  4098. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4099. EVT VT, const SDValue *Ops,
  4100. unsigned NumOps) {
  4101. SDVTList VTs = getVTList(VT);
  4102. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4103. }
  4104. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4105. EVT VT1, EVT VT2, const SDValue *Ops,
  4106. unsigned NumOps) {
  4107. SDVTList VTs = getVTList(VT1, VT2);
  4108. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4109. }
  4110. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4111. EVT VT1, EVT VT2) {
  4112. SDVTList VTs = getVTList(VT1, VT2);
  4113. return SelectNodeTo(N, MachineOpc, VTs, (SDValue *)0, 0);
  4114. }
  4115. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4116. EVT VT1, EVT VT2, EVT VT3,
  4117. const SDValue *Ops, unsigned NumOps) {
  4118. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4119. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4120. }
  4121. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4122. EVT VT1, EVT VT2, EVT VT3, EVT VT4,
  4123. const SDValue *Ops, unsigned NumOps) {
  4124. SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
  4125. return SelectNodeTo(N, MachineOpc, VTs, Ops, NumOps);
  4126. }
  4127. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4128. EVT VT1, EVT VT2,
  4129. SDValue Op1) {
  4130. SDVTList VTs = getVTList(VT1, VT2);
  4131. SDValue Ops[] = { Op1 };
  4132. return SelectNodeTo(N, MachineOpc, VTs, Ops, 1);
  4133. }
  4134. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4135. EVT VT1, EVT VT2,
  4136. SDValue Op1, SDValue Op2) {
  4137. SDVTList VTs = getVTList(VT1, VT2);
  4138. SDValue Ops[] = { Op1, Op2 };
  4139. return SelectNodeTo(N, MachineOpc, VTs, Ops, 2);
  4140. }
  4141. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4142. EVT VT1, EVT VT2,
  4143. SDValue Op1, SDValue Op2,
  4144. SDValue Op3) {
  4145. SDVTList VTs = getVTList(VT1, VT2);
  4146. SDValue Ops[] = { Op1, Op2, Op3 };
  4147. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4148. }
  4149. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4150. EVT VT1, EVT VT2, EVT VT3,
  4151. SDValue Op1, SDValue Op2,
  4152. SDValue Op3) {
  4153. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4154. SDValue Ops[] = { Op1, Op2, Op3 };
  4155. return SelectNodeTo(N, MachineOpc, VTs, Ops, 3);
  4156. }
  4157. SDNode *SelectionDAG::SelectNodeTo(SDNode *N, unsigned MachineOpc,
  4158. SDVTList VTs, const SDValue *Ops,
  4159. unsigned NumOps) {
  4160. N = MorphNodeTo(N, ~MachineOpc, VTs, Ops, NumOps);
  4161. // Reset the NodeID to -1.
  4162. N->setNodeId(-1);
  4163. return N;
  4164. }
  4165. /// MorphNodeTo - This *mutates* the specified node to have the specified
  4166. /// return type, opcode, and operands.
  4167. ///
  4168. /// Note that MorphNodeTo returns the resultant node. If there is already a
  4169. /// node of the specified opcode and operands, it returns that node instead of
  4170. /// the current one. Note that the DebugLoc need not be the same.
  4171. ///
  4172. /// Using MorphNodeTo is faster than creating a new node and swapping it in
  4173. /// with ReplaceAllUsesWith both because it often avoids allocating a new
  4174. /// node, and because it doesn't require CSE recalculation for any of
  4175. /// the node's users.
  4176. ///
  4177. SDNode *SelectionDAG::MorphNodeTo(SDNode *N, unsigned Opc,
  4178. SDVTList VTs, const SDValue *Ops,
  4179. unsigned NumOps) {
  4180. // If an identical node already exists, use it.
  4181. void *IP = 0;
  4182. if (VTs.VTs[VTs.NumVTs-1] != MVT::Flag) {
  4183. FoldingSetNodeID ID;
  4184. AddNodeIDNode(ID, Opc, VTs, Ops, NumOps);
  4185. if (SDNode *ON = CSEMap.FindNodeOrInsertPos(ID, IP))
  4186. return ON;
  4187. }
  4188. if (!RemoveNodeFromCSEMaps(N))
  4189. IP = 0;
  4190. // Start the morphing.
  4191. N->NodeType = Opc;
  4192. N->ValueList = VTs.VTs;
  4193. N->NumValues = VTs.NumVTs;
  4194. // Clear the operands list, updating used nodes to remove this from their
  4195. // use list. Keep track of any operands that become dead as a result.
  4196. SmallPtrSet<SDNode*, 16> DeadNodeSet;
  4197. for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
  4198. SDUse &Use = *I++;
  4199. SDNode *Used = Use.getNode();
  4200. Use.set(SDValue());
  4201. if (Used->use_empty())
  4202. DeadNodeSet.insert(Used);
  4203. }
  4204. if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N)) {
  4205. // Initialize the memory references information.
  4206. MN->setMemRefs(0, 0);
  4207. // If NumOps is larger than the # of operands we can have in a
  4208. // MachineSDNode, reallocate the operand list.
  4209. if (NumOps > MN->NumOperands || !MN->OperandsNeedDelete) {
  4210. if (MN->OperandsNeedDelete)
  4211. delete[] MN->OperandList;
  4212. if (NumOps > array_lengthof(MN->LocalOperands))
  4213. // We're creating a final node that will live unmorphed for the
  4214. // remainder of the current SelectionDAG iteration, so we can allocate
  4215. // the operands directly out of a pool with no recycling metadata.
  4216. MN->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
  4217. Ops, NumOps);
  4218. else
  4219. MN->InitOperands(MN->LocalOperands, Ops, NumOps);
  4220. MN->OperandsNeedDelete = false;
  4221. } else
  4222. MN->InitOperands(MN->OperandList, Ops, NumOps);
  4223. } else {
  4224. // If NumOps is larger than the # of operands we currently have, reallocate
  4225. // the operand list.
  4226. if (NumOps > N->NumOperands) {
  4227. if (N->OperandsNeedDelete)
  4228. delete[] N->OperandList;
  4229. N->InitOperands(new SDUse[NumOps], Ops, NumOps);
  4230. N->OperandsNeedDelete = true;
  4231. } else
  4232. N->InitOperands(N->OperandList, Ops, NumOps);
  4233. }
  4234. // Delete any nodes that are still dead after adding the uses for the
  4235. // new operands.
  4236. if (!DeadNodeSet.empty()) {
  4237. SmallVector<SDNode *, 16> DeadNodes;
  4238. for (SmallPtrSet<SDNode *, 16>::iterator I = DeadNodeSet.begin(),
  4239. E = DeadNodeSet.end(); I != E; ++I)
  4240. if ((*I)->use_empty())
  4241. DeadNodes.push_back(*I);
  4242. RemoveDeadNodes(DeadNodes);
  4243. }
  4244. if (IP)
  4245. CSEMap.InsertNode(N, IP); // Memoize the new node.
  4246. return N;
  4247. }
  4248. /// getMachineNode - These are used for target selectors to create a new node
  4249. /// with specified return type(s), MachineInstr opcode, and operands.
  4250. ///
  4251. /// Note that getMachineNode returns the resultant node. If there is already a
  4252. /// node of the specified opcode and operands, it returns that node instead of
  4253. /// the current one.
  4254. MachineSDNode *
  4255. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT) {
  4256. SDVTList VTs = getVTList(VT);
  4257. return getMachineNode(Opcode, dl, VTs, 0, 0);
  4258. }
  4259. MachineSDNode *
  4260. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT, SDValue Op1) {
  4261. SDVTList VTs = getVTList(VT);
  4262. SDValue Ops[] = { Op1 };
  4263. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4264. }
  4265. MachineSDNode *
  4266. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4267. SDValue Op1, SDValue Op2) {
  4268. SDVTList VTs = getVTList(VT);
  4269. SDValue Ops[] = { Op1, Op2 };
  4270. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4271. }
  4272. MachineSDNode *
  4273. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4274. SDValue Op1, SDValue Op2, SDValue Op3) {
  4275. SDVTList VTs = getVTList(VT);
  4276. SDValue Ops[] = { Op1, Op2, Op3 };
  4277. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4278. }
  4279. MachineSDNode *
  4280. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT,
  4281. const SDValue *Ops, unsigned NumOps) {
  4282. SDVTList VTs = getVTList(VT);
  4283. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4284. }
  4285. MachineSDNode *
  4286. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1, EVT VT2) {
  4287. SDVTList VTs = getVTList(VT1, VT2);
  4288. return getMachineNode(Opcode, dl, VTs, 0, 0);
  4289. }
  4290. MachineSDNode *
  4291. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4292. EVT VT1, EVT VT2, SDValue Op1) {
  4293. SDVTList VTs = getVTList(VT1, VT2);
  4294. SDValue Ops[] = { Op1 };
  4295. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4296. }
  4297. MachineSDNode *
  4298. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4299. EVT VT1, EVT VT2, SDValue Op1, SDValue Op2) {
  4300. SDVTList VTs = getVTList(VT1, VT2);
  4301. SDValue Ops[] = { Op1, Op2 };
  4302. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4303. }
  4304. MachineSDNode *
  4305. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4306. EVT VT1, EVT VT2, SDValue Op1,
  4307. SDValue Op2, SDValue Op3) {
  4308. SDVTList VTs = getVTList(VT1, VT2);
  4309. SDValue Ops[] = { Op1, Op2, Op3 };
  4310. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4311. }
  4312. MachineSDNode *
  4313. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4314. EVT VT1, EVT VT2,
  4315. const SDValue *Ops, unsigned NumOps) {
  4316. SDVTList VTs = getVTList(VT1, VT2);
  4317. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4318. }
  4319. MachineSDNode *
  4320. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4321. EVT VT1, EVT VT2, EVT VT3,
  4322. SDValue Op1, SDValue Op2) {
  4323. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4324. SDValue Ops[] = { Op1, Op2 };
  4325. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4326. }
  4327. MachineSDNode *
  4328. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4329. EVT VT1, EVT VT2, EVT VT3,
  4330. SDValue Op1, SDValue Op2, SDValue Op3) {
  4331. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4332. SDValue Ops[] = { Op1, Op2, Op3 };
  4333. return getMachineNode(Opcode, dl, VTs, Ops, array_lengthof(Ops));
  4334. }
  4335. MachineSDNode *
  4336. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4337. EVT VT1, EVT VT2, EVT VT3,
  4338. const SDValue *Ops, unsigned NumOps) {
  4339. SDVTList VTs = getVTList(VT1, VT2, VT3);
  4340. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4341. }
  4342. MachineSDNode *
  4343. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl, EVT VT1,
  4344. EVT VT2, EVT VT3, EVT VT4,
  4345. const SDValue *Ops, unsigned NumOps) {
  4346. SDVTList VTs = getVTList(VT1, VT2, VT3, VT4);
  4347. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4348. }
  4349. MachineSDNode *
  4350. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc dl,
  4351. const std::vector<EVT> &ResultTys,
  4352. const SDValue *Ops, unsigned NumOps) {
  4353. SDVTList VTs = getVTList(&ResultTys[0], ResultTys.size());
  4354. return getMachineNode(Opcode, dl, VTs, Ops, NumOps);
  4355. }
  4356. MachineSDNode *
  4357. SelectionDAG::getMachineNode(unsigned Opcode, DebugLoc DL, SDVTList VTs,
  4358. const SDValue *Ops, unsigned NumOps) {
  4359. bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Flag;
  4360. MachineSDNode *N;
  4361. void *IP;
  4362. if (DoCSE) {
  4363. FoldingSetNodeID ID;
  4364. AddNodeIDNode(ID, ~Opcode, VTs, Ops, NumOps);
  4365. IP = 0;
  4366. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  4367. return cast<MachineSDNode>(E);
  4368. }
  4369. // Allocate a new MachineSDNode.
  4370. N = new (NodeAllocator) MachineSDNode(~Opcode, DL, VTs);
  4371. // Initialize the operands list.
  4372. if (NumOps > array_lengthof(N->LocalOperands))
  4373. // We're creating a final node that will live unmorphed for the
  4374. // remainder of the current SelectionDAG iteration, so we can allocate
  4375. // the operands directly out of a pool with no recycling metadata.
  4376. N->InitOperands(OperandAllocator.Allocate<SDUse>(NumOps),
  4377. Ops, NumOps);
  4378. else
  4379. N->InitOperands(N->LocalOperands, Ops, NumOps);
  4380. N->OperandsNeedDelete = false;
  4381. if (DoCSE)
  4382. CSEMap.InsertNode(N, IP);
  4383. AllNodes.push_back(N);
  4384. #ifndef NDEBUG
  4385. VerifyMachineNode(N);
  4386. #endif
  4387. return N;
  4388. }
  4389. /// getTargetExtractSubreg - A convenience function for creating
  4390. /// TargetOpcode::EXTRACT_SUBREG nodes.
  4391. SDValue
  4392. SelectionDAG::getTargetExtractSubreg(int SRIdx, DebugLoc DL, EVT VT,
  4393. SDValue Operand) {
  4394. SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
  4395. SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
  4396. VT, Operand, SRIdxVal);
  4397. return SDValue(Subreg, 0);
  4398. }
  4399. /// getTargetInsertSubreg - A convenience function for creating
  4400. /// TargetOpcode::INSERT_SUBREG nodes.
  4401. SDValue
  4402. SelectionDAG::getTargetInsertSubreg(int SRIdx, DebugLoc DL, EVT VT,
  4403. SDValue Operand, SDValue Subreg) {
  4404. SDValue SRIdxVal = getTargetConstant(SRIdx, MVT::i32);
  4405. SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
  4406. VT, Operand, Subreg, SRIdxVal);
  4407. return SDValue(Result, 0);
  4408. }
  4409. /// getNodeIfExists - Get the specified node if it's already available, or
  4410. /// else return NULL.
  4411. SDNode *SelectionDAG::getNodeIfExists(unsigned Opcode, SDVTList VTList,
  4412. const SDValue *Ops, unsigned NumOps) {
  4413. if (VTList.VTs[VTList.NumVTs-1] != MVT::Flag) {
  4414. FoldingSetNodeID ID;
  4415. AddNodeIDNode(ID, Opcode, VTList, Ops, NumOps);
  4416. void *IP = 0;
  4417. if (SDNode *E = CSEMap.FindNodeOrInsertPos(ID, IP))
  4418. return E;
  4419. }
  4420. return NULL;
  4421. }
  4422. /// getDbgValue - Creates a SDDbgValue node.
  4423. ///
  4424. SDDbgValue *
  4425. SelectionDAG::getDbgValue(MDNode *MDPtr, SDNode *N, unsigned R, uint64_t Off,
  4426. DebugLoc DL, unsigned O) {
  4427. return new (Allocator) SDDbgValue(MDPtr, N, R, Off, DL, O);
  4428. }
  4429. SDDbgValue *
  4430. SelectionDAG::getDbgValue(MDNode *MDPtr, const Value *C, uint64_t Off,
  4431. DebugLoc DL, unsigned O) {
  4432. return new (Allocator) SDDbgValue(MDPtr, C, Off, DL, O);
  4433. }
  4434. SDDbgValue *
  4435. SelectionDAG::getDbgValue(MDNode *MDPtr, unsigned FI, uint64_t Off,
  4436. DebugLoc DL, unsigned O) {
  4437. return new (Allocator) SDDbgValue(MDPtr, FI, Off, DL, O);
  4438. }
  4439. namespace {
  4440. /// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
  4441. /// pointed to by a use iterator is deleted, increment the use iterator
  4442. /// so that it doesn't dangle.
  4443. ///
  4444. /// This class also manages a "downlink" DAGUpdateListener, to forward
  4445. /// messages to ReplaceAllUsesWith's callers.
  4446. ///
  4447. class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
  4448. SelectionDAG::DAGUpdateListener *DownLink;
  4449. SDNode::use_iterator &UI;
  4450. SDNode::use_iterator &UE;
  4451. virtual void NodeDeleted(SDNode *N, SDNode *E) {
  4452. // Increment the iterator as needed.
  4453. while (UI != UE && N == *UI)
  4454. ++UI;
  4455. // Then forward the message.
  4456. if (DownLink) DownLink->NodeDeleted(N, E);
  4457. }
  4458. virtual void NodeUpdated(SDNode *N) {
  4459. // Just forward the message.
  4460. if (DownLink) DownLink->NodeUpdated(N);
  4461. }
  4462. public:
  4463. RAUWUpdateListener(SelectionDAG::DAGUpdateListener *dl,
  4464. SDNode::use_iterator &ui,
  4465. SDNode::use_iterator &ue)
  4466. : DownLink(dl), UI(ui), UE(ue) {}
  4467. };
  4468. }
  4469. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4470. /// This can cause recursive merging of nodes in the DAG.
  4471. ///
  4472. /// This version assumes From has a single result value.
  4473. ///
  4474. void SelectionDAG::ReplaceAllUsesWith(SDValue FromN, SDValue To,
  4475. DAGUpdateListener *UpdateListener) {
  4476. SDNode *From = FromN.getNode();
  4477. assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
  4478. "Cannot replace with this method!");
  4479. assert(From != To.getNode() && "Cannot replace uses of with self");
  4480. // Iterate over all the existing uses of From. New uses will be added
  4481. // to the beginning of the use list, which we avoid visiting.
  4482. // This specifically avoids visiting uses of From that arise while the
  4483. // replacement is happening, because any such uses would be the result
  4484. // of CSE: If an existing node looks like From after one of its operands
  4485. // is replaced by To, we don't want to replace of all its users with To
  4486. // too. See PR3018 for more info.
  4487. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4488. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4489. while (UI != UE) {
  4490. SDNode *User = *UI;
  4491. // This node is about to morph, remove its old self from the CSE maps.
  4492. RemoveNodeFromCSEMaps(User);
  4493. // A user can appear in a use list multiple times, and when this
  4494. // happens the uses are usually next to each other in the list.
  4495. // To help reduce the number of CSE recomputations, process all
  4496. // the uses of this user that we can find this way.
  4497. do {
  4498. SDUse &Use = UI.getUse();
  4499. ++UI;
  4500. Use.set(To);
  4501. } while (UI != UE && *UI == User);
  4502. // Now that we have modified User, add it back to the CSE maps. If it
  4503. // already exists there, recursively merge the results together.
  4504. AddModifiedNodeToCSEMaps(User, &Listener);
  4505. }
  4506. }
  4507. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4508. /// This can cause recursive merging of nodes in the DAG.
  4509. ///
  4510. /// This version assumes that for each value of From, there is a
  4511. /// corresponding value in To in the same position with the same type.
  4512. ///
  4513. void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
  4514. DAGUpdateListener *UpdateListener) {
  4515. #ifndef NDEBUG
  4516. for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
  4517. assert((!From->hasAnyUseOfValue(i) ||
  4518. From->getValueType(i) == To->getValueType(i)) &&
  4519. "Cannot use this version of ReplaceAllUsesWith!");
  4520. #endif
  4521. // Handle the trivial case.
  4522. if (From == To)
  4523. return;
  4524. // Iterate over just the existing users of From. See the comments in
  4525. // the ReplaceAllUsesWith above.
  4526. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4527. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4528. while (UI != UE) {
  4529. SDNode *User = *UI;
  4530. // This node is about to morph, remove its old self from the CSE maps.
  4531. RemoveNodeFromCSEMaps(User);
  4532. // A user can appear in a use list multiple times, and when this
  4533. // happens the uses are usually next to each other in the list.
  4534. // To help reduce the number of CSE recomputations, process all
  4535. // the uses of this user that we can find this way.
  4536. do {
  4537. SDUse &Use = UI.getUse();
  4538. ++UI;
  4539. Use.setNode(To);
  4540. } while (UI != UE && *UI == User);
  4541. // Now that we have modified User, add it back to the CSE maps. If it
  4542. // already exists there, recursively merge the results together.
  4543. AddModifiedNodeToCSEMaps(User, &Listener);
  4544. }
  4545. }
  4546. /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
  4547. /// This can cause recursive merging of nodes in the DAG.
  4548. ///
  4549. /// This version can replace From with any result values. To must match the
  4550. /// number and types of values returned by From.
  4551. void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
  4552. const SDValue *To,
  4553. DAGUpdateListener *UpdateListener) {
  4554. if (From->getNumValues() == 1) // Handle the simple case efficiently.
  4555. return ReplaceAllUsesWith(SDValue(From, 0), To[0], UpdateListener);
  4556. // Iterate over just the existing users of From. See the comments in
  4557. // the ReplaceAllUsesWith above.
  4558. SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
  4559. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4560. while (UI != UE) {
  4561. SDNode *User = *UI;
  4562. // This node is about to morph, remove its old self from the CSE maps.
  4563. RemoveNodeFromCSEMaps(User);
  4564. // A user can appear in a use list multiple times, and when this
  4565. // happens the uses are usually next to each other in the list.
  4566. // To help reduce the number of CSE recomputations, process all
  4567. // the uses of this user that we can find this way.
  4568. do {
  4569. SDUse &Use = UI.getUse();
  4570. const SDValue &ToOp = To[Use.getResNo()];
  4571. ++UI;
  4572. Use.set(ToOp);
  4573. } while (UI != UE && *UI == User);
  4574. // Now that we have modified User, add it back to the CSE maps. If it
  4575. // already exists there, recursively merge the results together.
  4576. AddModifiedNodeToCSEMaps(User, &Listener);
  4577. }
  4578. }
  4579. /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
  4580. /// uses of other values produced by From.getNode() alone. The Deleted
  4581. /// vector is handled the same way as for ReplaceAllUsesWith.
  4582. void SelectionDAG::ReplaceAllUsesOfValueWith(SDValue From, SDValue To,
  4583. DAGUpdateListener *UpdateListener){
  4584. // Handle the really simple, really trivial case efficiently.
  4585. if (From == To) return;
  4586. // Handle the simple, trivial, case efficiently.
  4587. if (From.getNode()->getNumValues() == 1) {
  4588. ReplaceAllUsesWith(From, To, UpdateListener);
  4589. return;
  4590. }
  4591. // Iterate over just the existing users of From. See the comments in
  4592. // the ReplaceAllUsesWith above.
  4593. SDNode::use_iterator UI = From.getNode()->use_begin(),
  4594. UE = From.getNode()->use_end();
  4595. RAUWUpdateListener Listener(UpdateListener, UI, UE);
  4596. while (UI != UE) {
  4597. SDNode *User = *UI;
  4598. bool UserRemovedFromCSEMaps = false;
  4599. // A user can appear in a use list multiple times, and when this
  4600. // happens the uses are usually next to each other in the list.
  4601. // To help reduce the number of CSE recomputations, process all
  4602. // the uses of this user that we can find this way.
  4603. do {
  4604. SDUse &Use = UI.getUse();
  4605. // Skip uses of different values from the same node.
  4606. if (Use.getResNo() != From.getResNo()) {
  4607. ++UI;
  4608. continue;
  4609. }
  4610. // If this node hasn't been modified yet, it's still in the CSE maps,
  4611. // so remove its old self from the CSE maps.
  4612. if (!UserRemovedFromCSEMaps) {
  4613. RemoveNodeFromCSEMaps(User);
  4614. UserRemovedFromCSEMaps = true;
  4615. }
  4616. ++UI;
  4617. Use.set(To);
  4618. } while (UI != UE && *UI == User);
  4619. // We are iterating over all uses of the From node, so if a use
  4620. // doesn't use the specific value, no changes are made.
  4621. if (!UserRemovedFromCSEMaps)
  4622. continue;
  4623. // Now that we have modified User, add it back to the CSE maps. If it
  4624. // already exists there, recursively merge the results together.
  4625. AddModifiedNodeToCSEMaps(User, &Listener);
  4626. }
  4627. }
  4628. namespace {
  4629. /// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
  4630. /// to record information about a use.
  4631. struct UseMemo {
  4632. SDNode *User;
  4633. unsigned Index;
  4634. SDUse *Use;
  4635. };
  4636. /// operator< - Sort Memos by User.
  4637. bool operator<(const UseMemo &L, const UseMemo &R) {
  4638. return (intptr_t)L.User < (intptr_t)R.User;
  4639. }
  4640. }
  4641. /// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
  4642. /// uses of other values produced by From.getNode() alone. The same value
  4643. /// may appear in both the From and To list. The Deleted vector is
  4644. /// handled the same way as for ReplaceAllUsesWith.
  4645. void SelectionDAG::ReplaceAllUsesOfValuesWith(const SDValue *From,
  4646. const SDValue *To,
  4647. unsigned Num,
  4648. DAGUpdateListener *UpdateListener){
  4649. // Handle the simple, trivial case efficiently.
  4650. if (Num == 1)
  4651. return ReplaceAllUsesOfValueWith(*From, *To, UpdateListener);
  4652. // Read up all the uses and make records of them. This helps
  4653. // processing new uses that are introduced during the
  4654. // replacement process.
  4655. SmallVector<UseMemo, 4> Uses;
  4656. for (unsigned i = 0; i != Num; ++i) {
  4657. unsigned FromResNo = From[i].getResNo();
  4658. SDNode *FromNode = From[i].getNode();
  4659. for (SDNode::use_iterator UI = FromNode->use_begin(),
  4660. E = FromNode->use_end(); UI != E; ++UI) {
  4661. SDUse &Use = UI.getUse();
  4662. if (Use.getResNo() == FromResNo) {
  4663. UseMemo Memo = { *UI, i, &Use };
  4664. Uses.push_back(Memo);
  4665. }
  4666. }
  4667. }
  4668. // Sort the uses, so that all the uses from a given User are together.
  4669. std::sort(Uses.begin(), Uses.end());
  4670. for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
  4671. UseIndex != UseIndexEnd; ) {
  4672. // We know that this user uses some value of From. If it is the right
  4673. // value, update it.
  4674. SDNode *User = Uses[UseIndex].User;
  4675. // This node is about to morph, remove its old self from the CSE maps.
  4676. RemoveNodeFromCSEMaps(User);
  4677. // The Uses array is sorted, so all the uses for a given User
  4678. // are next to each other in the list.
  4679. // To help reduce the number of CSE recomputations, process all
  4680. // the uses of this user that we can find this way.
  4681. do {
  4682. unsigned i = Uses[UseIndex].Index;
  4683. SDUse &Use = *Uses[UseIndex].Use;
  4684. ++UseIndex;
  4685. Use.set(To[i]);
  4686. } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
  4687. // Now that we have modified User, add it back to the CSE maps. If it
  4688. // already exists there, recursively merge the results together.
  4689. AddModifiedNodeToCSEMaps(User, UpdateListener);
  4690. }
  4691. }
  4692. /// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
  4693. /// based on their topological order. It returns the maximum id and a vector
  4694. /// of the SDNodes* in assigned order by reference.
  4695. unsigned SelectionDAG::AssignTopologicalOrder() {
  4696. unsigned DAGSize = 0;
  4697. // SortedPos tracks the progress of the algorithm. Nodes before it are
  4698. // sorted, nodes after it are unsorted. When the algorithm completes
  4699. // it is at the end of the list.
  4700. allnodes_iterator SortedPos = allnodes_begin();
  4701. // Visit all the nodes. Move nodes with no operands to the front of
  4702. // the list immediately. Annotate nodes that do have operands with their
  4703. // operand count. Before we do this, the Node Id fields of the nodes
  4704. // may contain arbitrary values. After, the Node Id fields for nodes
  4705. // before SortedPos will contain the topological sort index, and the
  4706. // Node Id fields for nodes At SortedPos and after will contain the
  4707. // count of outstanding operands.
  4708. for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ) {
  4709. SDNode *N = I++;
  4710. checkForCycles(N);
  4711. unsigned Degree = N->getNumOperands();
  4712. if (Degree == 0) {
  4713. // A node with no uses, add it to the result array immediately.
  4714. N->setNodeId(DAGSize++);
  4715. allnodes_iterator Q = N;
  4716. if (Q != SortedPos)
  4717. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
  4718. assert(SortedPos != AllNodes.end() && "Overran node list");
  4719. ++SortedPos;
  4720. } else {
  4721. // Temporarily use the Node Id as scratch space for the degree count.
  4722. N->setNodeId(Degree);
  4723. }
  4724. }
  4725. // Visit all the nodes. As we iterate, moves nodes into sorted order,
  4726. // such that by the time the end is reached all nodes will be sorted.
  4727. for (allnodes_iterator I = allnodes_begin(),E = allnodes_end(); I != E; ++I) {
  4728. SDNode *N = I;
  4729. checkForCycles(N);
  4730. // N is in sorted position, so all its uses have one less operand
  4731. // that needs to be sorted.
  4732. for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
  4733. UI != UE; ++UI) {
  4734. SDNode *P = *UI;
  4735. unsigned Degree = P->getNodeId();
  4736. assert(Degree != 0 && "Invalid node degree");
  4737. --Degree;
  4738. if (Degree == 0) {
  4739. // All of P's operands are sorted, so P may sorted now.
  4740. P->setNodeId(DAGSize++);
  4741. if (P != SortedPos)
  4742. SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
  4743. assert(SortedPos != AllNodes.end() && "Overran node list");
  4744. ++SortedPos;
  4745. } else {
  4746. // Update P's outstanding operand count.
  4747. P->setNodeId(Degree);
  4748. }
  4749. }
  4750. if (I == SortedPos) {
  4751. #ifndef NDEBUG
  4752. SDNode *S = ++I;
  4753. dbgs() << "Overran sorted position:\n";
  4754. S->dumprFull();
  4755. #endif
  4756. llvm_unreachable(0);
  4757. }
  4758. }
  4759. assert(SortedPos == AllNodes.end() &&
  4760. "Topological sort incomplete!");
  4761. assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
  4762. "First node in topological sort is not the entry token!");
  4763. assert(AllNodes.front().getNodeId() == 0 &&
  4764. "First node in topological sort has non-zero id!");
  4765. assert(AllNodes.front().getNumOperands() == 0 &&
  4766. "First node in topological sort has operands!");
  4767. assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
  4768. "Last node in topologic sort has unexpected id!");
  4769. assert(AllNodes.back().use_empty() &&
  4770. "Last node in topologic sort has users!");
  4771. assert(DAGSize == allnodes_size() && "Node count mismatch!");
  4772. return DAGSize;
  4773. }
  4774. /// AssignOrdering - Assign an order to the SDNode.
  4775. void SelectionDAG::AssignOrdering(const SDNode *SD, unsigned Order) {
  4776. assert(SD && "Trying to assign an order to a null node!");
  4777. Ordering->add(SD, Order);
  4778. }
  4779. /// GetOrdering - Get the order for the SDNode.
  4780. unsigned SelectionDAG::GetOrdering(const SDNode *SD) const {
  4781. assert(SD && "Trying to get the order of a null node!");
  4782. return Ordering->getOrder(SD);
  4783. }
  4784. /// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
  4785. /// value is produced by SD.
  4786. void SelectionDAG::AddDbgValue(SDDbgValue *DB, SDNode *SD, bool isParameter) {
  4787. DbgInfo->add(DB, SD, isParameter);
  4788. if (SD)
  4789. SD->setHasDebugValue(true);
  4790. }
  4791. //===----------------------------------------------------------------------===//
  4792. // SDNode Class
  4793. //===----------------------------------------------------------------------===//
  4794. HandleSDNode::~HandleSDNode() {
  4795. DropOperands();
  4796. }
  4797. GlobalAddressSDNode::GlobalAddressSDNode(unsigned Opc, DebugLoc DL,
  4798. const GlobalValue *GA,
  4799. EVT VT, int64_t o, unsigned char TF)
  4800. : SDNode(Opc, DL, getSDVTList(VT)), Offset(o), TargetFlags(TF) {
  4801. TheGlobal = GA;
  4802. }
  4803. MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs, EVT memvt,
  4804. MachineMemOperand *mmo)
  4805. : SDNode(Opc, dl, VTs), MemoryVT(memvt), MMO(mmo) {
  4806. SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
  4807. MMO->isNonTemporal());
  4808. assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
  4809. assert(isNonTemporal() == MMO->isNonTemporal() &&
  4810. "Non-temporal encoding error!");
  4811. assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
  4812. }
  4813. MemSDNode::MemSDNode(unsigned Opc, DebugLoc dl, SDVTList VTs,
  4814. const SDValue *Ops, unsigned NumOps, EVT memvt,
  4815. MachineMemOperand *mmo)
  4816. : SDNode(Opc, dl, VTs, Ops, NumOps),
  4817. MemoryVT(memvt), MMO(mmo) {
  4818. SubclassData = encodeMemSDNodeFlags(0, ISD::UNINDEXED, MMO->isVolatile(),
  4819. MMO->isNonTemporal());
  4820. assert(isVolatile() == MMO->isVolatile() && "Volatile encoding error!");
  4821. assert(memvt.getStoreSize() == MMO->getSize() && "Size mismatch!");
  4822. }
  4823. /// Profile - Gather unique data for the node.
  4824. ///
  4825. void SDNode::Profile(FoldingSetNodeID &ID) const {
  4826. AddNodeIDNode(ID, this);
  4827. }
  4828. namespace {
  4829. struct EVTArray {
  4830. std::vector<EVT> VTs;
  4831. EVTArray() {
  4832. VTs.reserve(MVT::LAST_VALUETYPE);
  4833. for (unsigned i = 0; i < MVT::LAST_VALUETYPE; ++i)
  4834. VTs.push_back(MVT((MVT::SimpleValueType)i));
  4835. }
  4836. };
  4837. }
  4838. static ManagedStatic<std::set<EVT, EVT::compareRawBits> > EVTs;
  4839. static ManagedStatic<EVTArray> SimpleVTArray;
  4840. static ManagedStatic<sys::SmartMutex<true> > VTMutex;
  4841. /// getValueTypeList - Return a pointer to the specified value type.
  4842. ///
  4843. const EVT *SDNode::getValueTypeList(EVT VT) {
  4844. if (VT.isExtended()) {
  4845. sys::SmartScopedLock<true> Lock(*VTMutex);
  4846. return &(*EVTs->insert(VT).first);
  4847. } else {
  4848. assert(VT.getSimpleVT().SimpleTy < MVT::LAST_VALUETYPE &&
  4849. "Value type out of range!");
  4850. return &SimpleVTArray->VTs[VT.getSimpleVT().SimpleTy];
  4851. }
  4852. }
  4853. /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
  4854. /// indicated value. This method ignores uses of other values defined by this
  4855. /// operation.
  4856. bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
  4857. assert(Value < getNumValues() && "Bad value!");
  4858. // TODO: Only iterate over uses of a given value of the node
  4859. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
  4860. if (UI.getUse().getResNo() == Value) {
  4861. if (NUses == 0)
  4862. return false;
  4863. --NUses;
  4864. }
  4865. }
  4866. // Found exactly the right number of uses?
  4867. return NUses == 0;
  4868. }
  4869. /// hasAnyUseOfValue - Return true if there are any use of the indicated
  4870. /// value. This method ignores uses of other values defined by this operation.
  4871. bool SDNode::hasAnyUseOfValue(unsigned Value) const {
  4872. assert(Value < getNumValues() && "Bad value!");
  4873. for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
  4874. if (UI.getUse().getResNo() == Value)
  4875. return true;
  4876. return false;
  4877. }
  4878. /// isOnlyUserOf - Return true if this node is the only use of N.
  4879. ///
  4880. bool SDNode::isOnlyUserOf(SDNode *N) const {
  4881. bool Seen = false;
  4882. for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
  4883. SDNode *User = *I;
  4884. if (User == this)
  4885. Seen = true;
  4886. else
  4887. return false;
  4888. }
  4889. return Seen;
  4890. }
  4891. /// isOperand - Return true if this node is an operand of N.
  4892. ///
  4893. bool SDValue::isOperandOf(SDNode *N) const {
  4894. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  4895. if (*this == N->getOperand(i))
  4896. return true;
  4897. return false;
  4898. }
  4899. bool SDNode::isOperandOf(SDNode *N) const {
  4900. for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
  4901. if (this == N->OperandList[i].getNode())
  4902. return true;
  4903. return false;
  4904. }
  4905. /// reachesChainWithoutSideEffects - Return true if this operand (which must
  4906. /// be a chain) reaches the specified operand without crossing any
  4907. /// side-effecting instructions. In practice, this looks through token
  4908. /// factors and non-volatile loads. In order to remain efficient, this only
  4909. /// looks a couple of nodes in, it does not do an exhaustive search.
  4910. bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
  4911. unsigned Depth) const {
  4912. if (*this == Dest) return true;
  4913. // Don't search too deeply, we just want to be able to see through
  4914. // TokenFactor's etc.
  4915. if (Depth == 0) return false;
  4916. // If this is a token factor, all inputs to the TF happen in parallel. If any
  4917. // of the operands of the TF reach dest, then we can do the xform.
  4918. if (getOpcode() == ISD::TokenFactor) {
  4919. for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
  4920. if (getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
  4921. return true;
  4922. return false;
  4923. }
  4924. // Loads don't have side effects, look through them.
  4925. if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
  4926. if (!Ld->isVolatile())
  4927. return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
  4928. }
  4929. return false;
  4930. }
  4931. /// isPredecessorOf - Return true if this node is a predecessor of N. This node
  4932. /// is either an operand of N or it can be reached by traversing up the operands.
  4933. /// NOTE: this is an expensive method. Use it carefully.
  4934. bool SDNode::isPredecessorOf(SDNode *N) const {
  4935. SmallPtrSet<SDNode *, 32> Visited;
  4936. SmallVector<SDNode *, 16> Worklist;
  4937. Worklist.push_back(N);
  4938. do {
  4939. N = Worklist.pop_back_val();
  4940. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  4941. SDNode *Op = N->getOperand(i).getNode();
  4942. if (Op == this)
  4943. return true;
  4944. if (Visited.insert(Op))
  4945. Worklist.push_back(Op);
  4946. }
  4947. } while (!Worklist.empty());
  4948. return false;
  4949. }
  4950. uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
  4951. assert(Num < NumOperands && "Invalid child # of SDNode!");
  4952. return cast<ConstantSDNode>(OperandList[Num])->getZExtValue();
  4953. }
  4954. std::string SDNode::getOperationName(const SelectionDAG *G) const {
  4955. switch (getOpcode()) {
  4956. default:
  4957. if (getOpcode() < ISD::BUILTIN_OP_END)
  4958. return "<<Unknown DAG Node>>";
  4959. if (isMachineOpcode()) {
  4960. if (G)
  4961. if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
  4962. if (getMachineOpcode() < TII->getNumOpcodes())
  4963. return TII->get(getMachineOpcode()).getName();
  4964. return "<<Unknown Machine Node #" + utostr(getOpcode()) + ">>";
  4965. }
  4966. if (G) {
  4967. const TargetLowering &TLI = G->getTargetLoweringInfo();
  4968. const char *Name = TLI.getTargetNodeName(getOpcode());
  4969. if (Name) return Name;
  4970. return "<<Unknown Target Node #" + utostr(getOpcode()) + ">>";
  4971. }
  4972. return "<<Unknown Node #" + utostr(getOpcode()) + ">>";
  4973. #ifndef NDEBUG
  4974. case ISD::DELETED_NODE:
  4975. return "<<Deleted Node!>>";
  4976. #endif
  4977. case ISD::PREFETCH: return "Prefetch";
  4978. case ISD::MEMBARRIER: return "MemBarrier";
  4979. case ISD::ATOMIC_CMP_SWAP: return "AtomicCmpSwap";
  4980. case ISD::ATOMIC_SWAP: return "AtomicSwap";
  4981. case ISD::ATOMIC_LOAD_ADD: return "AtomicLoadAdd";
  4982. case ISD::ATOMIC_LOAD_SUB: return "AtomicLoadSub";
  4983. case ISD::ATOMIC_LOAD_AND: return "AtomicLoadAnd";
  4984. case ISD::ATOMIC_LOAD_OR: return "AtomicLoadOr";
  4985. case ISD::ATOMIC_LOAD_XOR: return "AtomicLoadXor";
  4986. case ISD::ATOMIC_LOAD_NAND: return "AtomicLoadNand";
  4987. case ISD::ATOMIC_LOAD_MIN: return "AtomicLoadMin";
  4988. case ISD::ATOMIC_LOAD_MAX: return "AtomicLoadMax";
  4989. case ISD::ATOMIC_LOAD_UMIN: return "AtomicLoadUMin";
  4990. case ISD::ATOMIC_LOAD_UMAX: return "AtomicLoadUMax";
  4991. case ISD::PCMARKER: return "PCMarker";
  4992. case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
  4993. case ISD::SRCVALUE: return "SrcValue";
  4994. case ISD::MDNODE_SDNODE: return "MDNode";
  4995. case ISD::EntryToken: return "EntryToken";
  4996. case ISD::TokenFactor: return "TokenFactor";
  4997. case ISD::AssertSext: return "AssertSext";
  4998. case ISD::AssertZext: return "AssertZext";
  4999. case ISD::BasicBlock: return "BasicBlock";
  5000. case ISD::VALUETYPE: return "ValueType";
  5001. case ISD::Register: return "Register";
  5002. case ISD::Constant: return "Constant";
  5003. case ISD::ConstantFP: return "ConstantFP";
  5004. case ISD::GlobalAddress: return "GlobalAddress";
  5005. case ISD::GlobalTLSAddress: return "GlobalTLSAddress";
  5006. case ISD::FrameIndex: return "FrameIndex";
  5007. case ISD::JumpTable: return "JumpTable";
  5008. case ISD::GLOBAL_OFFSET_TABLE: return "GLOBAL_OFFSET_TABLE";
  5009. case ISD::RETURNADDR: return "RETURNADDR";
  5010. case ISD::FRAMEADDR: return "FRAMEADDR";
  5011. case ISD::FRAME_TO_ARGS_OFFSET: return "FRAME_TO_ARGS_OFFSET";
  5012. case ISD::EXCEPTIONADDR: return "EXCEPTIONADDR";
  5013. case ISD::LSDAADDR: return "LSDAADDR";
  5014. case ISD::EHSELECTION: return "EHSELECTION";
  5015. case ISD::EH_RETURN: return "EH_RETURN";
  5016. case ISD::EH_SJLJ_SETJMP: return "EH_SJLJ_SETJMP";
  5017. case ISD::EH_SJLJ_LONGJMP: return "EH_SJLJ_LONGJMP";
  5018. case ISD::ConstantPool: return "ConstantPool";
  5019. case ISD::ExternalSymbol: return "ExternalSymbol";
  5020. case ISD::BlockAddress: return "BlockAddress";
  5021. case ISD::INTRINSIC_WO_CHAIN:
  5022. case ISD::INTRINSIC_VOID:
  5023. case ISD::INTRINSIC_W_CHAIN: {
  5024. unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
  5025. unsigned IID = cast<ConstantSDNode>(getOperand(OpNo))->getZExtValue();
  5026. if (IID < Intrinsic::num_intrinsics)
  5027. return Intrinsic::getName((Intrinsic::ID)IID);
  5028. else if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())
  5029. return TII->getName(IID);
  5030. llvm_unreachable("Invalid intrinsic ID");
  5031. }
  5032. case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
  5033. case ISD::TargetConstant: return "TargetConstant";
  5034. case ISD::TargetConstantFP:return "TargetConstantFP";
  5035. case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
  5036. case ISD::TargetGlobalTLSAddress: return "TargetGlobalTLSAddress";
  5037. case ISD::TargetFrameIndex: return "TargetFrameIndex";
  5038. case ISD::TargetJumpTable: return "TargetJumpTable";
  5039. case ISD::TargetConstantPool: return "TargetConstantPool";
  5040. case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
  5041. case ISD::TargetBlockAddress: return "TargetBlockAddress";
  5042. case ISD::CopyToReg: return "CopyToReg";
  5043. case ISD::CopyFromReg: return "CopyFromReg";
  5044. case ISD::UNDEF: return "undef";
  5045. case ISD::MERGE_VALUES: return "merge_values";
  5046. case ISD::INLINEASM: return "inlineasm";
  5047. case ISD::EH_LABEL: return "eh_label";
  5048. case ISD::HANDLENODE: return "handlenode";
  5049. // Unary operators
  5050. case ISD::FABS: return "fabs";
  5051. case ISD::FNEG: return "fneg";
  5052. case ISD::FSQRT: return "fsqrt";
  5053. case ISD::FSIN: return "fsin";
  5054. case ISD::FCOS: return "fcos";
  5055. case ISD::FTRUNC: return "ftrunc";
  5056. case ISD::FFLOOR: return "ffloor";
  5057. case ISD::FCEIL: return "fceil";
  5058. case ISD::FRINT: return "frint";
  5059. case ISD::FNEARBYINT: return "fnearbyint";
  5060. case ISD::FEXP: return "fexp";
  5061. case ISD::FEXP2: return "fexp2";
  5062. case ISD::FLOG: return "flog";
  5063. case ISD::FLOG2: return "flog2";
  5064. case ISD::FLOG10: return "flog10";
  5065. // Binary operators
  5066. case ISD::ADD: return "add";
  5067. case ISD::SUB: return "sub";
  5068. case ISD::MUL: return "mul";
  5069. case ISD::MULHU: return "mulhu";
  5070. case ISD::MULHS: return "mulhs";
  5071. case ISD::SDIV: return "sdiv";
  5072. case ISD::UDIV: return "udiv";
  5073. case ISD::SREM: return "srem";
  5074. case ISD::UREM: return "urem";
  5075. case ISD::SMUL_LOHI: return "smul_lohi";
  5076. case ISD::UMUL_LOHI: return "umul_lohi";
  5077. case ISD::SDIVREM: return "sdivrem";
  5078. case ISD::UDIVREM: return "udivrem";
  5079. case ISD::AND: return "and";
  5080. case ISD::OR: return "or";
  5081. case ISD::XOR: return "xor";
  5082. case ISD::SHL: return "shl";
  5083. case ISD::SRA: return "sra";
  5084. case ISD::SRL: return "srl";
  5085. case ISD::ROTL: return "rotl";
  5086. case ISD::ROTR: return "rotr";
  5087. case ISD::FADD: return "fadd";
  5088. case ISD::FSUB: return "fsub";
  5089. case ISD::FMUL: return "fmul";
  5090. case ISD::FDIV: return "fdiv";
  5091. case ISD::FREM: return "frem";
  5092. case ISD::FCOPYSIGN: return "fcopysign";
  5093. case ISD::FGETSIGN: return "fgetsign";
  5094. case ISD::FPOW: return "fpow";
  5095. case ISD::FPOWI: return "fpowi";
  5096. case ISD::SETCC: return "setcc";
  5097. case ISD::VSETCC: return "vsetcc";
  5098. case ISD::SELECT: return "select";
  5099. case ISD::SELECT_CC: return "select_cc";
  5100. case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
  5101. case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
  5102. case ISD::CONCAT_VECTORS: return "concat_vectors";
  5103. case ISD::EXTRACT_SUBVECTOR: return "extract_subvector";
  5104. case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
  5105. case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
  5106. case ISD::CARRY_FALSE: return "carry_false";
  5107. case ISD::ADDC: return "addc";
  5108. case ISD::ADDE: return "adde";
  5109. case ISD::SADDO: return "saddo";
  5110. case ISD::UADDO: return "uaddo";
  5111. case ISD::SSUBO: return "ssubo";
  5112. case ISD::USUBO: return "usubo";
  5113. case ISD::SMULO: return "smulo";
  5114. case ISD::UMULO: return "umulo";
  5115. case ISD::SUBC: return "subc";
  5116. case ISD::SUBE: return "sube";
  5117. case ISD::SHL_PARTS: return "shl_parts";
  5118. case ISD::SRA_PARTS: return "sra_parts";
  5119. case ISD::SRL_PARTS: return "srl_parts";
  5120. // Conversion operators.
  5121. case ISD::SIGN_EXTEND: return "sign_extend";
  5122. case ISD::ZERO_EXTEND: return "zero_extend";
  5123. case ISD::ANY_EXTEND: return "any_extend";
  5124. case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
  5125. case ISD::TRUNCATE: return "truncate";
  5126. case ISD::FP_ROUND: return "fp_round";
  5127. case ISD::FLT_ROUNDS_: return "flt_rounds";
  5128. case ISD::FP_ROUND_INREG: return "fp_round_inreg";
  5129. case ISD::FP_EXTEND: return "fp_extend";
  5130. case ISD::SINT_TO_FP: return "sint_to_fp";
  5131. case ISD::UINT_TO_FP: return "uint_to_fp";
  5132. case ISD::FP_TO_SINT: return "fp_to_sint";
  5133. case ISD::FP_TO_UINT: return "fp_to_uint";
  5134. case ISD::BIT_CONVERT: return "bit_convert";
  5135. case ISD::FP16_TO_FP32: return "fp16_to_fp32";
  5136. case ISD::FP32_TO_FP16: return "fp32_to_fp16";
  5137. case ISD::CONVERT_RNDSAT: {
  5138. switch (cast<CvtRndSatSDNode>(this)->getCvtCode()) {
  5139. default: llvm_unreachable("Unknown cvt code!");
  5140. case ISD::CVT_FF: return "cvt_ff";
  5141. case ISD::CVT_FS: return "cvt_fs";
  5142. case ISD::CVT_FU: return "cvt_fu";
  5143. case ISD::CVT_SF: return "cvt_sf";
  5144. case ISD::CVT_UF: return "cvt_uf";
  5145. case ISD::CVT_SS: return "cvt_ss";
  5146. case ISD::CVT_SU: return "cvt_su";
  5147. case ISD::CVT_US: return "cvt_us";
  5148. case ISD::CVT_UU: return "cvt_uu";
  5149. }
  5150. }
  5151. // Control flow instructions
  5152. case ISD::BR: return "br";
  5153. case ISD::BRIND: return "brind";
  5154. case ISD::BR_JT: return "br_jt";
  5155. case ISD::BRCOND: return "brcond";
  5156. case ISD::BR_CC: return "br_cc";
  5157. case ISD::CALLSEQ_START: return "callseq_start";
  5158. case ISD::CALLSEQ_END: return "callseq_end";
  5159. // Other operators
  5160. case ISD::LOAD: return "load";
  5161. case ISD::STORE: return "store";
  5162. case ISD::VAARG: return "vaarg";
  5163. case ISD::VACOPY: return "vacopy";
  5164. case ISD::VAEND: return "vaend";
  5165. case ISD::VASTART: return "vastart";
  5166. case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
  5167. case ISD::EXTRACT_ELEMENT: return "extract_element";
  5168. case ISD::BUILD_PAIR: return "build_pair";
  5169. case ISD::STACKSAVE: return "stacksave";
  5170. case ISD::STACKRESTORE: return "stackrestore";
  5171. case ISD::TRAP: return "trap";
  5172. // Bit manipulation
  5173. case ISD::BSWAP: return "bswap";
  5174. case ISD::CTPOP: return "ctpop";
  5175. case ISD::CTTZ: return "cttz";
  5176. case ISD::CTLZ: return "ctlz";
  5177. // Trampolines
  5178. case ISD::TRAMPOLINE: return "trampoline";
  5179. case ISD::CONDCODE:
  5180. switch (cast<CondCodeSDNode>(this)->get()) {
  5181. default: llvm_unreachable("Unknown setcc condition!");
  5182. case ISD::SETOEQ: return "setoeq";
  5183. case ISD::SETOGT: return "setogt";
  5184. case ISD::SETOGE: return "setoge";
  5185. case ISD::SETOLT: return "setolt";
  5186. case ISD::SETOLE: return "setole";
  5187. case ISD::SETONE: return "setone";
  5188. case ISD::SETO: return "seto";
  5189. case ISD::SETUO: return "setuo";
  5190. case ISD::SETUEQ: return "setue";
  5191. case ISD::SETUGT: return "setugt";
  5192. case ISD::SETUGE: return "setuge";
  5193. case ISD::SETULT: return "setult";
  5194. case ISD::SETULE: return "setule";
  5195. case ISD::SETUNE: return "setune";
  5196. case ISD::SETEQ: return "seteq";
  5197. case ISD::SETGT: return "setgt";
  5198. case ISD::SETGE: return "setge";
  5199. case ISD::SETLT: return "setlt";
  5200. case ISD::SETLE: return "setle";
  5201. case ISD::SETNE: return "setne";
  5202. }
  5203. }
  5204. }
  5205. const char *SDNode::getIndexedModeName(ISD::MemIndexedMode AM) {
  5206. switch (AM) {
  5207. default:
  5208. return "";
  5209. case ISD::PRE_INC:
  5210. return "<pre-inc>";
  5211. case ISD::PRE_DEC:
  5212. return "<pre-dec>";
  5213. case ISD::POST_INC:
  5214. return "<post-inc>";
  5215. case ISD::POST_DEC:
  5216. return "<post-dec>";
  5217. }
  5218. }
  5219. std::string ISD::ArgFlagsTy::getArgFlagsString() {
  5220. std::string S = "< ";
  5221. if (isZExt())
  5222. S += "zext ";
  5223. if (isSExt())
  5224. S += "sext ";
  5225. if (isInReg())
  5226. S += "inreg ";
  5227. if (isSRet())
  5228. S += "sret ";
  5229. if (isByVal())
  5230. S += "byval ";
  5231. if (isNest())
  5232. S += "nest ";
  5233. if (getByValAlign())
  5234. S += "byval-align:" + utostr(getByValAlign()) + " ";
  5235. if (getOrigAlign())
  5236. S += "orig-align:" + utostr(getOrigAlign()) + " ";
  5237. if (getByValSize())
  5238. S += "byval-size:" + utostr(getByValSize()) + " ";
  5239. return S + ">";
  5240. }
  5241. void SDNode::dump() const { dump(0); }
  5242. void SDNode::dump(const SelectionDAG *G) const {
  5243. print(dbgs(), G);
  5244. dbgs() << '\n';
  5245. }
  5246. void SDNode::print_types(raw_ostream &OS, const SelectionDAG *G) const {
  5247. OS << (void*)this << ": ";
  5248. for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
  5249. if (i) OS << ",";
  5250. if (getValueType(i) == MVT::Other)
  5251. OS << "ch";
  5252. else
  5253. OS << getValueType(i).getEVTString();
  5254. }
  5255. OS << " = " << getOperationName(G);
  5256. }
  5257. void SDNode::print_details(raw_ostream &OS, const SelectionDAG *G) const {
  5258. if (const MachineSDNode *MN = dyn_cast<MachineSDNode>(this)) {
  5259. if (!MN->memoperands_empty()) {
  5260. OS << "<";
  5261. OS << "Mem:";
  5262. for (MachineSDNode::mmo_iterator i = MN->memoperands_begin(),
  5263. e = MN->memoperands_end(); i != e; ++i) {
  5264. OS << **i;
  5265. if (llvm::next(i) != e)
  5266. OS << " ";
  5267. }
  5268. OS << ">";
  5269. }
  5270. } else if (const ShuffleVectorSDNode *SVN =
  5271. dyn_cast<ShuffleVectorSDNode>(this)) {
  5272. OS << "<";
  5273. for (unsigned i = 0, e = ValueList[0].getVectorNumElements(); i != e; ++i) {
  5274. int Idx = SVN->getMaskElt(i);
  5275. if (i) OS << ",";
  5276. if (Idx < 0)
  5277. OS << "u";
  5278. else
  5279. OS << Idx;
  5280. }
  5281. OS << ">";
  5282. } else if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
  5283. OS << '<' << CSDN->getAPIntValue() << '>';
  5284. } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
  5285. if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEsingle)
  5286. OS << '<' << CSDN->getValueAPF().convertToFloat() << '>';
  5287. else if (&CSDN->getValueAPF().getSemantics()==&APFloat::IEEEdouble)
  5288. OS << '<' << CSDN->getValueAPF().convertToDouble() << '>';
  5289. else {
  5290. OS << "<APFloat(";
  5291. CSDN->getValueAPF().bitcastToAPInt().dump();
  5292. OS << ")>";
  5293. }
  5294. } else if (const GlobalAddressSDNode *GADN =
  5295. dyn_cast<GlobalAddressSDNode>(this)) {
  5296. int64_t offset = GADN->getOffset();
  5297. OS << '<';
  5298. WriteAsOperand(OS, GADN->getGlobal());
  5299. OS << '>';
  5300. if (offset > 0)
  5301. OS << " + " << offset;
  5302. else
  5303. OS << " " << offset;
  5304. if (unsigned int TF = GADN->getTargetFlags())
  5305. OS << " [TF=" << TF << ']';
  5306. } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
  5307. OS << "<" << FIDN->getIndex() << ">";
  5308. } else if (const JumpTableSDNode *JTDN = dyn_cast<JumpTableSDNode>(this)) {
  5309. OS << "<" << JTDN->getIndex() << ">";
  5310. if (unsigned int TF = JTDN->getTargetFlags())
  5311. OS << " [TF=" << TF << ']';
  5312. } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
  5313. int offset = CP->getOffset();
  5314. if (CP->isMachineConstantPoolEntry())
  5315. OS << "<" << *CP->getMachineCPVal() << ">";
  5316. else
  5317. OS << "<" << *CP->getConstVal() << ">";
  5318. if (offset > 0)
  5319. OS << " + " << offset;
  5320. else
  5321. OS << " " << offset;
  5322. if (unsigned int TF = CP->getTargetFlags())
  5323. OS << " [TF=" << TF << ']';
  5324. } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
  5325. OS << "<";
  5326. const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
  5327. if (LBB)
  5328. OS << LBB->getName() << " ";
  5329. OS << (const void*)BBDN->getBasicBlock() << ">";
  5330. } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
  5331. if (G && R->getReg() &&
  5332. TargetRegisterInfo::isPhysicalRegister(R->getReg())) {
  5333. OS << " %" << G->getTarget().getRegisterInfo()->getName(R->getReg());
  5334. } else {
  5335. OS << " %reg" << R->getReg();
  5336. }
  5337. } else if (const ExternalSymbolSDNode *ES =
  5338. dyn_cast<ExternalSymbolSDNode>(this)) {
  5339. OS << "'" << ES->getSymbol() << "'";
  5340. if (unsigned int TF = ES->getTargetFlags())
  5341. OS << " [TF=" << TF << ']';
  5342. } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
  5343. if (M->getValue())
  5344. OS << "<" << M->getValue() << ">";
  5345. else
  5346. OS << "<null>";
  5347. } else if (const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(this)) {
  5348. if (MD->getMD())
  5349. OS << "<" << MD->getMD() << ">";
  5350. else
  5351. OS << "<null>";
  5352. } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
  5353. OS << ":" << N->getVT().getEVTString();
  5354. }
  5355. else if (const LoadSDNode *LD = dyn_cast<LoadSDNode>(this)) {
  5356. OS << "<" << *LD->getMemOperand();
  5357. bool doExt = true;
  5358. switch (LD->getExtensionType()) {
  5359. default: doExt = false; break;
  5360. case ISD::EXTLOAD: OS << ", anyext"; break;
  5361. case ISD::SEXTLOAD: OS << ", sext"; break;
  5362. case ISD::ZEXTLOAD: OS << ", zext"; break;
  5363. }
  5364. if (doExt)
  5365. OS << " from " << LD->getMemoryVT().getEVTString();
  5366. const char *AM = getIndexedModeName(LD->getAddressingMode());
  5367. if (*AM)
  5368. OS << ", " << AM;
  5369. OS << ">";
  5370. } else if (const StoreSDNode *ST = dyn_cast<StoreSDNode>(this)) {
  5371. OS << "<" << *ST->getMemOperand();
  5372. if (ST->isTruncatingStore())
  5373. OS << ", trunc to " << ST->getMemoryVT().getEVTString();
  5374. const char *AM = getIndexedModeName(ST->getAddressingMode());
  5375. if (*AM)
  5376. OS << ", " << AM;
  5377. OS << ">";
  5378. } else if (const MemSDNode* M = dyn_cast<MemSDNode>(this)) {
  5379. OS << "<" << *M->getMemOperand() << ">";
  5380. } else if (const BlockAddressSDNode *BA =
  5381. dyn_cast<BlockAddressSDNode>(this)) {
  5382. OS << "<";
  5383. WriteAsOperand(OS, BA->getBlockAddress()->getFunction(), false);
  5384. OS << ", ";
  5385. WriteAsOperand(OS, BA->getBlockAddress()->getBasicBlock(), false);
  5386. OS << ">";
  5387. if (unsigned int TF = BA->getTargetFlags())
  5388. OS << " [TF=" << TF << ']';
  5389. }
  5390. if (G)
  5391. if (unsigned Order = G->GetOrdering(this))
  5392. OS << " [ORD=" << Order << ']';
  5393. if (getNodeId() != -1)
  5394. OS << " [ID=" << getNodeId() << ']';
  5395. DebugLoc dl = getDebugLoc();
  5396. if (G && !dl.isUnknown()) {
  5397. DIScope
  5398. Scope(dl.getScope(G->getMachineFunction().getFunction()->getContext()));
  5399. OS << " dbg:";
  5400. // Omit the directory, since it's usually long and uninteresting.
  5401. if (Scope.Verify())
  5402. OS << Scope.getFilename();
  5403. else
  5404. OS << "<unknown>";
  5405. OS << ':' << dl.getLine();
  5406. if (dl.getCol() != 0)
  5407. OS << ':' << dl.getCol();
  5408. }
  5409. }
  5410. void SDNode::print(raw_ostream &OS, const SelectionDAG *G) const {
  5411. print_types(OS, G);
  5412. for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
  5413. if (i) OS << ", "; else OS << " ";
  5414. OS << (void*)getOperand(i).getNode();
  5415. if (unsigned RN = getOperand(i).getResNo())
  5416. OS << ":" << RN;
  5417. }
  5418. print_details(OS, G);
  5419. }
  5420. static void printrWithDepthHelper(raw_ostream &OS, const SDNode *N,
  5421. const SelectionDAG *G, unsigned depth,
  5422. unsigned indent)
  5423. {
  5424. if (depth == 0)
  5425. return;
  5426. OS.indent(indent);
  5427. N->print(OS, G);
  5428. if (depth < 1)
  5429. return;
  5430. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5431. OS << '\n';
  5432. printrWithDepthHelper(OS, N->getOperand(i).getNode(), G, depth-1, indent+2);
  5433. }
  5434. }
  5435. void SDNode::printrWithDepth(raw_ostream &OS, const SelectionDAG *G,
  5436. unsigned depth) const {
  5437. printrWithDepthHelper(OS, this, G, depth, 0);
  5438. }
  5439. void SDNode::printrFull(raw_ostream &OS, const SelectionDAG *G) const {
  5440. // Don't print impossibly deep things.
  5441. printrWithDepth(OS, G, 100);
  5442. }
  5443. void SDNode::dumprWithDepth(const SelectionDAG *G, unsigned depth) const {
  5444. printrWithDepth(dbgs(), G, depth);
  5445. }
  5446. void SDNode::dumprFull(const SelectionDAG *G) const {
  5447. // Don't print impossibly deep things.
  5448. dumprWithDepth(G, 100);
  5449. }
  5450. static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
  5451. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  5452. if (N->getOperand(i).getNode()->hasOneUse())
  5453. DumpNodes(N->getOperand(i).getNode(), indent+2, G);
  5454. else
  5455. dbgs() << "\n" << std::string(indent+2, ' ')
  5456. << (void*)N->getOperand(i).getNode() << ": <multiple use>";
  5457. dbgs() << "\n";
  5458. dbgs().indent(indent);
  5459. N->dump(G);
  5460. }
  5461. SDValue SelectionDAG::UnrollVectorOp(SDNode *N, unsigned ResNE) {
  5462. assert(N->getNumValues() == 1 &&
  5463. "Can't unroll a vector with multiple results!");
  5464. EVT VT = N->getValueType(0);
  5465. unsigned NE = VT.getVectorNumElements();
  5466. EVT EltVT = VT.getVectorElementType();
  5467. DebugLoc dl = N->getDebugLoc();
  5468. SmallVector<SDValue, 8> Scalars;
  5469. SmallVector<SDValue, 4> Operands(N->getNumOperands());
  5470. // If ResNE is 0, fully unroll the vector op.
  5471. if (ResNE == 0)
  5472. ResNE = NE;
  5473. else if (NE > ResNE)
  5474. NE = ResNE;
  5475. unsigned i;
  5476. for (i= 0; i != NE; ++i) {
  5477. for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
  5478. SDValue Operand = N->getOperand(j);
  5479. EVT OperandVT = Operand.getValueType();
  5480. if (OperandVT.isVector()) {
  5481. // A vector operand; extract a single element.
  5482. EVT OperandEltVT = OperandVT.getVectorElementType();
  5483. Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl,
  5484. OperandEltVT,
  5485. Operand,
  5486. getConstant(i, MVT::i32));
  5487. } else {
  5488. // A scalar operand; just use it as is.
  5489. Operands[j] = Operand;
  5490. }
  5491. }
  5492. switch (N->getOpcode()) {
  5493. default:
  5494. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
  5495. &Operands[0], Operands.size()));
  5496. break;
  5497. case ISD::SHL:
  5498. case ISD::SRA:
  5499. case ISD::SRL:
  5500. case ISD::ROTL:
  5501. case ISD::ROTR:
  5502. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
  5503. getShiftAmountOperand(Operands[1])));
  5504. break;
  5505. case ISD::SIGN_EXTEND_INREG:
  5506. case ISD::FP_ROUND_INREG: {
  5507. EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
  5508. Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
  5509. Operands[0],
  5510. getValueType(ExtVT)));
  5511. }
  5512. }
  5513. }
  5514. for (; i < ResNE; ++i)
  5515. Scalars.push_back(getUNDEF(EltVT));
  5516. return getNode(ISD::BUILD_VECTOR, dl,
  5517. EVT::getVectorVT(*getContext(), EltVT, ResNE),
  5518. &Scalars[0], Scalars.size());
  5519. }
  5520. /// isConsecutiveLoad - Return true if LD is loading 'Bytes' bytes from a
  5521. /// location that is 'Dist' units away from the location that the 'Base' load
  5522. /// is loading from.
  5523. bool SelectionDAG::isConsecutiveLoad(LoadSDNode *LD, LoadSDNode *Base,
  5524. unsigned Bytes, int Dist) const {
  5525. if (LD->getChain() != Base->getChain())
  5526. return false;
  5527. EVT VT = LD->getValueType(0);
  5528. if (VT.getSizeInBits() / 8 != Bytes)
  5529. return false;
  5530. SDValue Loc = LD->getOperand(1);
  5531. SDValue BaseLoc = Base->getOperand(1);
  5532. if (Loc.getOpcode() == ISD::FrameIndex) {
  5533. if (BaseLoc.getOpcode() != ISD::FrameIndex)
  5534. return false;
  5535. const MachineFrameInfo *MFI = getMachineFunction().getFrameInfo();
  5536. int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
  5537. int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
  5538. int FS = MFI->getObjectSize(FI);
  5539. int BFS = MFI->getObjectSize(BFI);
  5540. if (FS != BFS || FS != (int)Bytes) return false;
  5541. return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes);
  5542. }
  5543. if (Loc.getOpcode() == ISD::ADD && Loc.getOperand(0) == BaseLoc) {
  5544. ConstantSDNode *V = dyn_cast<ConstantSDNode>(Loc.getOperand(1));
  5545. if (V && (V->getSExtValue() == Dist*Bytes))
  5546. return true;
  5547. }
  5548. const GlobalValue *GV1 = NULL;
  5549. const GlobalValue *GV2 = NULL;
  5550. int64_t Offset1 = 0;
  5551. int64_t Offset2 = 0;
  5552. bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1);
  5553. bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2);
  5554. if (isGA1 && isGA2 && GV1 == GV2)
  5555. return Offset1 == (Offset2 + Dist*Bytes);
  5556. return false;
  5557. }
  5558. /// InferPtrAlignment - Infer alignment of a load / store address. Return 0 if
  5559. /// it cannot be inferred.
  5560. unsigned SelectionDAG::InferPtrAlignment(SDValue Ptr) const {
  5561. // If this is a GlobalAddress + cst, return the alignment.
  5562. const GlobalValue *GV;
  5563. int64_t GVOffset = 0;
  5564. if (TLI.isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
  5565. // If GV has specified alignment, then use it. Otherwise, use the preferred
  5566. // alignment.
  5567. unsigned Align = GV->getAlignment();
  5568. if (!Align) {
  5569. if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
  5570. if (GVar->hasInitializer()) {
  5571. const TargetData *TD = TLI.getTargetData();
  5572. Align = TD->getPreferredAlignment(GVar);
  5573. }
  5574. }
  5575. }
  5576. return MinAlign(Align, GVOffset);
  5577. }
  5578. // If this is a direct reference to a stack slot, use information about the
  5579. // stack slot's alignment.
  5580. int FrameIdx = 1 << 31;
  5581. int64_t FrameOffset = 0;
  5582. if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
  5583. FrameIdx = FI->getIndex();
  5584. } else if (Ptr.getOpcode() == ISD::ADD &&
  5585. isa<ConstantSDNode>(Ptr.getOperand(1)) &&
  5586. isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
  5587. FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
  5588. FrameOffset = Ptr.getConstantOperandVal(1);
  5589. }
  5590. if (FrameIdx != (1 << 31)) {
  5591. // FIXME: Handle FI+CST.
  5592. const MachineFrameInfo &MFI = *getMachineFunction().getFrameInfo();
  5593. unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
  5594. FrameOffset);
  5595. return FIInfoAlign;
  5596. }
  5597. return 0;
  5598. }
  5599. void SelectionDAG::dump() const {
  5600. dbgs() << "SelectionDAG has " << AllNodes.size() << " nodes:";
  5601. for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
  5602. I != E; ++I) {
  5603. const SDNode *N = I;
  5604. if (!N->hasOneUse() && N != getRoot().getNode())
  5605. DumpNodes(N, 2, this);
  5606. }
  5607. if (getRoot().getNode()) DumpNodes(getRoot().getNode(), 2, this);
  5608. dbgs() << "\n\n";
  5609. }
  5610. void SDNode::printr(raw_ostream &OS, const SelectionDAG *G) const {
  5611. print_types(OS, G);
  5612. print_details(OS, G);
  5613. }
  5614. typedef SmallPtrSet<const SDNode *, 128> VisitedSDNodeSet;
  5615. static void DumpNodesr(raw_ostream &OS, const SDNode *N, unsigned indent,
  5616. const SelectionDAG *G, VisitedSDNodeSet &once) {
  5617. if (!once.insert(N)) // If we've been here before, return now.
  5618. return;
  5619. // Dump the current SDNode, but don't end the line yet.
  5620. OS << std::string(indent, ' ');
  5621. N->printr(OS, G);
  5622. // Having printed this SDNode, walk the children:
  5623. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5624. const SDNode *child = N->getOperand(i).getNode();
  5625. if (i) OS << ",";
  5626. OS << " ";
  5627. if (child->getNumOperands() == 0) {
  5628. // This child has no grandchildren; print it inline right here.
  5629. child->printr(OS, G);
  5630. once.insert(child);
  5631. } else { // Just the address. FIXME: also print the child's opcode.
  5632. OS << (void*)child;
  5633. if (unsigned RN = N->getOperand(i).getResNo())
  5634. OS << ":" << RN;
  5635. }
  5636. }
  5637. OS << "\n";
  5638. // Dump children that have grandchildren on their own line(s).
  5639. for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
  5640. const SDNode *child = N->getOperand(i).getNode();
  5641. DumpNodesr(OS, child, indent+2, G, once);
  5642. }
  5643. }
  5644. void SDNode::dumpr() const {
  5645. VisitedSDNodeSet once;
  5646. DumpNodesr(dbgs(), this, 0, 0, once);
  5647. }
  5648. void SDNode::dumpr(const SelectionDAG *G) const {
  5649. VisitedSDNodeSet once;
  5650. DumpNodesr(dbgs(), this, 0, G, once);
  5651. }
  5652. // getAddressSpace - Return the address space this GlobalAddress belongs to.
  5653. unsigned GlobalAddressSDNode::getAddressSpace() const {
  5654. return getGlobal()->getType()->getAddressSpace();
  5655. }
  5656. const Type *ConstantPoolSDNode::getType() const {
  5657. if (isMachineConstantPoolEntry())
  5658. return Val.MachineCPVal->getType();
  5659. return Val.ConstVal->getType();
  5660. }
  5661. bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue,
  5662. APInt &SplatUndef,
  5663. unsigned &SplatBitSize,
  5664. bool &HasAnyUndefs,
  5665. unsigned MinSplatBits,
  5666. bool isBigEndian) {
  5667. EVT VT = getValueType(0);
  5668. assert(VT.isVector() && "Expected a vector type");
  5669. unsigned sz = VT.getSizeInBits();
  5670. if (MinSplatBits > sz)
  5671. return false;
  5672. SplatValue = APInt(sz, 0);
  5673. SplatUndef = APInt(sz, 0);
  5674. // Get the bits. Bits with undefined values (when the corresponding element
  5675. // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
  5676. // in SplatValue. If any of the values are not constant, give up and return
  5677. // false.
  5678. unsigned int nOps = getNumOperands();
  5679. assert(nOps > 0 && "isConstantSplat has 0-size build vector");
  5680. unsigned EltBitSize = VT.getVectorElementType().getSizeInBits();
  5681. for (unsigned j = 0; j < nOps; ++j) {
  5682. unsigned i = isBigEndian ? nOps-1-j : j;
  5683. SDValue OpVal = getOperand(i);
  5684. unsigned BitPos = j * EltBitSize;
  5685. if (OpVal.getOpcode() == ISD::UNDEF)
  5686. SplatUndef |= APInt::getBitsSet(sz, BitPos, BitPos + EltBitSize);
  5687. else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal))
  5688. SplatValue |= APInt(CN->getAPIntValue()).zextOrTrunc(EltBitSize).
  5689. zextOrTrunc(sz) << BitPos;
  5690. else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal))
  5691. SplatValue |= CN->getValueAPF().bitcastToAPInt().zextOrTrunc(sz) <<BitPos;
  5692. else
  5693. return false;
  5694. }
  5695. // The build_vector is all constants or undefs. Find the smallest element
  5696. // size that splats the vector.
  5697. HasAnyUndefs = (SplatUndef != 0);
  5698. while (sz > 8) {
  5699. unsigned HalfSize = sz / 2;
  5700. APInt HighValue = APInt(SplatValue).lshr(HalfSize).trunc(HalfSize);
  5701. APInt LowValue = APInt(SplatValue).trunc(HalfSize);
  5702. APInt HighUndef = APInt(SplatUndef).lshr(HalfSize).trunc(HalfSize);
  5703. APInt LowUndef = APInt(SplatUndef).trunc(HalfSize);
  5704. // If the two halves do not match (ignoring undef bits), stop here.
  5705. if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
  5706. MinSplatBits > HalfSize)
  5707. break;
  5708. SplatValue = HighValue | LowValue;
  5709. SplatUndef = HighUndef & LowUndef;
  5710. sz = HalfSize;
  5711. }
  5712. SplatBitSize = sz;
  5713. return true;
  5714. }
  5715. bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
  5716. // Find the first non-undef value in the shuffle mask.
  5717. unsigned i, e;
  5718. for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
  5719. /* search */;
  5720. assert(i != e && "VECTOR_SHUFFLE node with all undef indices!");
  5721. // Make sure all remaining elements are either undef or the same as the first
  5722. // non-undef value.
  5723. for (int Idx = Mask[i]; i != e; ++i)
  5724. if (Mask[i] >= 0 && Mask[i] != Idx)
  5725. return false;
  5726. return true;
  5727. }
  5728. #ifdef XDEBUG
  5729. static void checkForCyclesHelper(const SDNode *N,
  5730. SmallPtrSet<const SDNode*, 32> &Visited,
  5731. SmallPtrSet<const SDNode*, 32> &Checked) {
  5732. // If this node has already been checked, don't check it again.
  5733. if (Checked.count(N))
  5734. return;
  5735. // If a node has already been visited on this depth-first walk, reject it as
  5736. // a cycle.
  5737. if (!Visited.insert(N)) {
  5738. dbgs() << "Offending node:\n";
  5739. N->dumprFull();
  5740. errs() << "Detected cycle in SelectionDAG\n";
  5741. abort();
  5742. }
  5743. for(unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
  5744. checkForCyclesHelper(N->getOperand(i).getNode(), Visited, Checked);
  5745. Checked.insert(N);
  5746. Visited.erase(N);
  5747. }
  5748. #endif
  5749. void llvm::checkForCycles(const llvm::SDNode *N) {
  5750. #ifdef XDEBUG
  5751. assert(N && "Checking nonexistant SDNode");
  5752. SmallPtrSet<const SDNode*, 32> visited;
  5753. SmallPtrSet<const SDNode*, 32> checked;
  5754. checkForCyclesHelper(N, visited, checked);
  5755. #endif
  5756. }
  5757. void llvm::checkForCycles(const llvm::SelectionDAG *DAG) {
  5758. checkForCycles(DAG->getRoot().getNode());
  5759. }