PageRenderTime 68ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 2ms

/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

Large files files are truncated, but you can click here to view the full 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 = getMa

Large files files are truncated, but you can click here to view the full file