blob: 79ba3d204207764b472b3654459c92a09f1dc8a0 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner1e111c72005-09-07 05:37:01 +000030// Temporary boolean for testing the dag combiner
31namespace llvm {
32 extern bool CombinerEnabled;
33}
34
Chris Lattner5cdcc582005-01-09 20:52:51 +000035static bool isCommutativeBinOp(unsigned Opcode) {
36 switch (Opcode) {
37 case ISD::ADD:
38 case ISD::MUL:
39 case ISD::AND:
40 case ISD::OR:
41 case ISD::XOR: return true;
42 default: return false; // FIXME: Need commutative info for user ops!
43 }
44}
45
46static bool isAssociativeBinOp(unsigned Opcode) {
47 switch (Opcode) {
48 case ISD::ADD:
49 case ISD::MUL:
50 case ISD::AND:
51 case ISD::OR:
52 case ISD::XOR: return true;
53 default: return false; // FIXME: Need associative info for user ops!
54 }
55}
56
Chris Lattner5cdcc582005-01-09 20:52:51 +000057// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000061 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000062 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000063 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000064}
65
Jim Laskey58b968b2005-08-17 20:08:02 +000066//===----------------------------------------------------------------------===//
67// ConstantFPSDNode Class
68//===----------------------------------------------------------------------===//
69
70/// isExactlyValue - We don't rely on operator== working on double values, as
71/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
72/// As such, this method can be used to do an exact bit-for-bit comparison of
73/// two floating point values.
74bool ConstantFPSDNode::isExactlyValue(double V) const {
75 return DoubleToBits(V) == DoubleToBits(Value);
76}
77
78//===----------------------------------------------------------------------===//
79// ISD Class
80//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000081
Chris Lattnerc3aae252005-01-07 07:46:32 +000082/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
83/// when given the operation for (X op Y).
84ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
85 // To perform this operation, we just need to swap the L and G bits of the
86 // operation.
87 unsigned OldL = (Operation >> 2) & 1;
88 unsigned OldG = (Operation >> 1) & 1;
89 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
90 (OldL << 1) | // New G bit
91 (OldG << 2)); // New L bit.
92}
93
94/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
95/// 'op' is a valid SetCC operation.
96ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
97 unsigned Operation = Op;
98 if (isInteger)
99 Operation ^= 7; // Flip L, G, E bits, but not U.
100 else
101 Operation ^= 15; // Flip all of the condition bits.
102 if (Operation > ISD::SETTRUE2)
103 Operation &= ~8; // Don't let N and U bits get set.
104 return ISD::CondCode(Operation);
105}
106
107
108/// isSignedOp - For an integer comparison, return 1 if the comparison is a
109/// signed operation and 2 if the result is an unsigned comparison. Return zero
110/// if the operation does not depend on the sign of the input (setne and seteq).
111static int isSignedOp(ISD::CondCode Opcode) {
112 switch (Opcode) {
113 default: assert(0 && "Illegal integer setcc operation!");
114 case ISD::SETEQ:
115 case ISD::SETNE: return 0;
116 case ISD::SETLT:
117 case ISD::SETLE:
118 case ISD::SETGT:
119 case ISD::SETGE: return 1;
120 case ISD::SETULT:
121 case ISD::SETULE:
122 case ISD::SETUGT:
123 case ISD::SETUGE: return 2;
124 }
125}
126
127/// getSetCCOrOperation - Return the result of a logical OR between different
128/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
129/// returns SETCC_INVALID if it is not possible to represent the resultant
130/// comparison.
131ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
132 bool isInteger) {
133 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
134 // Cannot fold a signed integer setcc with an unsigned integer setcc.
135 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000136
Chris Lattnerc3aae252005-01-07 07:46:32 +0000137 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000138
Chris Lattnerc3aae252005-01-07 07:46:32 +0000139 // If the N and U bits get set then the resultant comparison DOES suddenly
140 // care about orderedness, and is true when ordered.
141 if (Op > ISD::SETTRUE2)
142 Op &= ~16; // Clear the N bit.
143 return ISD::CondCode(Op);
144}
145
146/// getSetCCAndOperation - Return the result of a logical AND between different
147/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
148/// function returns zero if it is not possible to represent the resultant
149/// comparison.
150ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
151 bool isInteger) {
152 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
153 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000155
156 // Combine all of the condition bits.
157 return ISD::CondCode(Op1 & Op2);
158}
159
Chris Lattnerb48da392005-01-23 04:39:44 +0000160const TargetMachine &SelectionDAG::getTarget() const {
161 return TLI.getTargetMachine();
162}
163
Jim Laskey58b968b2005-08-17 20:08:02 +0000164//===----------------------------------------------------------------------===//
165// SelectionDAG Class
166//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000167
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000168/// RemoveDeadNodes - This method deletes all unreachable nodes in the
169/// SelectionDAG, including nodes (like loads) that have uses of their token
170/// chain but no other uses and no side effect. If a node is passed in as an
171/// argument, it is used as the seed for node deletion.
172void SelectionDAG::RemoveDeadNodes(SDNode *N) {
173 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
174
175 // Create a dummy node (which is not added to allnodes), that adds a reference
176 // to the root node, preventing it from being deleted.
177 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
178
Chris Lattner8b8749f2005-08-17 19:00:20 +0000179 // If we have a hint to start from, use it.
180 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000181
182 Restart:
183 unsigned NumNodes = AllNodeSet.size();
184 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
185 I != E; ++I) {
186 // Try to delete this node.
187 DeleteNodeIfDead(*I, &AllNodeSet);
188
189 // If we actually deleted any nodes, do not use invalid iterators in
190 // AllNodeSet.
191 if (AllNodeSet.size() != NumNodes)
192 goto Restart;
193 }
194
195 // Restore AllNodes.
196 if (AllNodes.size() != NumNodes)
197 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
198
199 // If the root changed (e.g. it was a dead load, update the root).
200 setRoot(DummyNode->getOperand(0));
201
202 // Now that we are done with the dummy node, delete it.
203 DummyNode->getOperand(0).Val->removeUser(DummyNode);
204 delete DummyNode;
205}
206
Chris Lattner149c58c2005-08-16 18:17:10 +0000207
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000208void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
209 if (!N->use_empty())
210 return;
211
212 // Okay, we really are going to delete this node. First take this out of the
213 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000214 RemoveNodeFromCSEMaps(N);
215
216 // Next, brutally remove the operand list. This is safe to do, as there are
217 // no cycles in the graph.
218 while (!N->Operands.empty()) {
219 SDNode *O = N->Operands.back().Val;
220 N->Operands.pop_back();
221 O->removeUser(N);
222
223 // Now that we removed this operand, see if there are no uses of it left.
224 DeleteNodeIfDead(O, NodeSet);
225 }
226
227 // Remove the node from the nodes set and delete it.
228 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
229 AllNodeSet.erase(N);
230
231 // Now that the node is gone, check to see if any of the operands of this node
232 // are dead now.
233 delete N;
234}
235
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000236void SelectionDAG::DeleteNode(SDNode *N) {
237 assert(N->use_empty() && "Cannot delete a node that is not dead!");
238
239 // First take this out of the appropriate CSE map.
240 RemoveNodeFromCSEMaps(N);
241
Chris Lattner1e111c72005-09-07 05:37:01 +0000242 // Finally, remove uses due to operands of this node, remove from the
243 // AllNodes list, and delete the node.
244 DeleteNodeNotInCSEMaps(N);
245}
246
247void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
248
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000249 // Remove it from the AllNodes list.
250 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
251 assert(I != AllNodes.end() && "Node not in AllNodes list??");
252 if (*I == N) {
253 // Erase from the vector, which is not ordered.
254 std::swap(*I, AllNodes.back());
255 AllNodes.pop_back();
256 break;
257 }
258 }
259
260 // Drop all of the operands and decrement used nodes use counts.
261 while (!N->Operands.empty()) {
262 SDNode *O = N->Operands.back().Val;
263 N->Operands.pop_back();
264 O->removeUser(N);
265 }
266
267 delete N;
268}
269
Chris Lattner149c58c2005-08-16 18:17:10 +0000270/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
271/// correspond to it. This is useful when we're about to delete or repurpose
272/// the node. We don't want future request for structurally identical nodes
273/// to return N anymore.
274void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000275 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000276 switch (N->getOpcode()) {
277 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000278 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
279 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000281 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000282 Erased = TargetConstants.erase(std::make_pair(
283 cast<ConstantSDNode>(N)->getValue(),
284 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000285 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000286 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000287 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000288 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000289 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000290 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000291 case ISD::CONDCODE:
292 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
293 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000294 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000295 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
296 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000297 case ISD::GlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000298 Erased = GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000300 case ISD::TargetGlobalAddress:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000301 Erased =TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000302 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000303 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000304 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000305 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000306 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000307 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000308 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309 case ISD::ConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000310 Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000311 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000312 case ISD::TargetConstantPool:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000313 Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000314 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000315 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000316 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000317 break;
318 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000319 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000320 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000321 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000322 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000323 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
324 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000325 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000326 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
327 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000328 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000329 case ISD::SRCVALUE: {
330 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000331 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000332 break;
333 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000334 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000335 Erased = Loads.erase(std::make_pair(N->getOperand(1),
336 std::make_pair(N->getOperand(0),
337 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000338 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000339 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000340 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000341 if (N->getNumOperands() == 0) {
342 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
343 N->getValueType(0)));
344 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000345 Erased =
346 UnaryOps.erase(std::make_pair(N->getOpcode(),
347 std::make_pair(N->getOperand(0),
348 N->getValueType(0))));
349 } else if (N->getNumOperands() == 2) {
350 Erased =
351 BinaryOps.erase(std::make_pair(N->getOpcode(),
352 std::make_pair(N->getOperand(0),
353 N->getOperand(1))));
354 } else {
355 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
356 Erased =
357 OneResultNodes.erase(std::make_pair(N->getOpcode(),
358 std::make_pair(N->getValueType(0),
359 Ops)));
360 }
Chris Lattner385328c2005-05-14 07:42:29 +0000361 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000362 // Remove the node from the ArbitraryNodes map.
363 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
364 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000365 Erased =
366 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
367 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000368 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000369 break;
370 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000371#ifndef NDEBUG
372 // Verify that the node was actually in one of the CSE maps, unless it has a
373 // flag result (which cannot be CSE'd) or is one of the special cases that are
374 // not subject to CSE.
375 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
376 N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START &&
Chris Lattner6a8a21c2005-09-03 01:04:40 +0000377 N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000378
379 N->dump();
380 assert(0 && "Node is not in map!");
381 }
382#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000383}
384
Chris Lattner8b8749f2005-08-17 19:00:20 +0000385/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
386/// has been taken out and modified in some way. If the specified node already
387/// exists in the CSE maps, do not modify the maps, but return the existing node
388/// instead. If it doesn't exist, add it and return null.
389///
390SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
391 assert(N->getNumOperands() && "This is a leaf node!");
392 if (N->getOpcode() == ISD::LOAD) {
393 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
394 std::make_pair(N->getOperand(0),
395 N->getValueType(0)))];
396 if (L) return L;
397 L = N;
398 } else if (N->getNumOperands() == 1) {
399 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
400 std::make_pair(N->getOperand(0),
401 N->getValueType(0)))];
402 if (U) return U;
403 U = N;
404 } else if (N->getNumOperands() == 2) {
405 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
406 std::make_pair(N->getOperand(0),
407 N->getOperand(1)))];
408 if (B) return B;
409 B = N;
410 } else if (N->getNumValues() == 1) {
411 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
412 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
413 std::make_pair(N->getValueType(0), Ops))];
414 if (ORN) return ORN;
415 ORN = N;
416 } else {
417 // Remove the node from the ArbitraryNodes map.
418 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
419 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
420 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
421 std::make_pair(RV, Ops))];
422 if (AN) return AN;
423 AN = N;
424 }
425 return 0;
426
427}
428
429
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000430
Chris Lattner78ec3112003-08-11 14:57:33 +0000431SelectionDAG::~SelectionDAG() {
432 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
433 delete AllNodes[i];
434}
435
Chris Lattner0f2287b2005-04-13 02:38:18 +0000436SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000437 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000438 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000439 return getNode(ISD::AND, Op.getValueType(), Op,
440 getConstant(Imm, Op.getValueType()));
441}
442
Chris Lattnerc3aae252005-01-07 07:46:32 +0000443SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
444 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
445 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000446 if (VT != MVT::i64)
447 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000448
Chris Lattnerc3aae252005-01-07 07:46:32 +0000449 SDNode *&N = Constants[std::make_pair(Val, VT)];
450 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000451 N = new ConstantSDNode(false, Val, VT);
452 AllNodes.push_back(N);
453 return SDOperand(N, 0);
454}
455
456SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
457 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
458 // Mask out any bits that are not valid for this constant.
459 if (VT != MVT::i64)
460 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
461
462 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
463 if (N) return SDOperand(N, 0);
464 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000465 AllNodes.push_back(N);
466 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000467}
468
Chris Lattnerc3aae252005-01-07 07:46:32 +0000469SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
470 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
471 if (VT == MVT::f32)
472 Val = (float)Val; // Mask out extra precision.
473
Chris Lattnerd8658612005-02-17 20:17:32 +0000474 // Do the map lookup using the actual bit pattern for the floating point
475 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
476 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000477 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000478 if (N) return SDOperand(N, 0);
479 N = new ConstantFPSDNode(Val, VT);
480 AllNodes.push_back(N);
481 return SDOperand(N, 0);
482}
483
484
485
486SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
487 MVT::ValueType VT) {
488 SDNode *&N = GlobalValues[GV];
489 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000490 N = new GlobalAddressSDNode(false, GV, VT);
491 AllNodes.push_back(N);
492 return SDOperand(N, 0);
493}
494
495SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
496 MVT::ValueType VT) {
497 SDNode *&N = TargetGlobalValues[GV];
498 if (N) return SDOperand(N, 0);
499 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000500 AllNodes.push_back(N);
501 return SDOperand(N, 0);
502}
503
504SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
505 SDNode *&N = FrameIndices[FI];
506 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000507 N = new FrameIndexSDNode(FI, VT, false);
508 AllNodes.push_back(N);
509 return SDOperand(N, 0);
510}
511
512SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
513 SDNode *&N = TargetFrameIndices[FI];
514 if (N) return SDOperand(N, 0);
515 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000516 AllNodes.push_back(N);
517 return SDOperand(N, 0);
518}
519
Chris Lattner5839bf22005-08-26 17:15:30 +0000520SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
521 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000522 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000523 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000524 AllNodes.push_back(N);
525 return SDOperand(N, 0);
526}
527
Chris Lattner5839bf22005-08-26 17:15:30 +0000528SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
529 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000530 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000531 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000532 AllNodes.push_back(N);
533 return SDOperand(N, 0);
534}
535
536SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
537 SDNode *&N = BBNodes[MBB];
538 if (N) return SDOperand(N, 0);
539 N = new BasicBlockSDNode(MBB);
540 AllNodes.push_back(N);
541 return SDOperand(N, 0);
542}
543
Chris Lattner15e4b012005-07-10 00:07:11 +0000544SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
545 if ((unsigned)VT >= ValueTypeNodes.size())
546 ValueTypeNodes.resize(VT+1);
547 if (ValueTypeNodes[VT] == 0) {
548 ValueTypeNodes[VT] = new VTSDNode(VT);
549 AllNodes.push_back(ValueTypeNodes[VT]);
550 }
551
552 return SDOperand(ValueTypeNodes[VT], 0);
553}
554
Chris Lattnerc3aae252005-01-07 07:46:32 +0000555SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
556 SDNode *&N = ExternalSymbols[Sym];
557 if (N) return SDOperand(N, 0);
558 N = new ExternalSymbolSDNode(Sym, VT);
559 AllNodes.push_back(N);
560 return SDOperand(N, 0);
561}
562
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000563SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
564 if ((unsigned)Cond >= CondCodeNodes.size())
565 CondCodeNodes.resize(Cond+1);
566
Chris Lattner079a27a2005-08-09 20:40:02 +0000567 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000568 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000569 AllNodes.push_back(CondCodeNodes[Cond]);
570 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000571 return SDOperand(CondCodeNodes[Cond], 0);
572}
573
Chris Lattner0fdd7682005-08-30 22:38:38 +0000574SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
575 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
576 if (!Reg) {
577 Reg = new RegisterSDNode(RegNo, VT);
578 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000579 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000580 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000581}
582
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000583SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
584 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000585 // These setcc operations always fold.
586 switch (Cond) {
587 default: break;
588 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000589 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000590 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000591 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 }
593
Chris Lattner67255a12005-04-07 18:14:58 +0000594 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
595 uint64_t C2 = N2C->getValue();
596 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
597 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000598
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599 // Sign extend the operands if required
600 if (ISD::isSignedIntSetCC(Cond)) {
601 C1 = N1C->getSignExtended();
602 C2 = N2C->getSignExtended();
603 }
604
605 switch (Cond) {
606 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000607 case ISD::SETEQ: return getConstant(C1 == C2, VT);
608 case ISD::SETNE: return getConstant(C1 != C2, VT);
609 case ISD::SETULT: return getConstant(C1 < C2, VT);
610 case ISD::SETUGT: return getConstant(C1 > C2, VT);
611 case ISD::SETULE: return getConstant(C1 <= C2, VT);
612 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
613 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
614 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
615 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
616 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000617 }
Chris Lattner24673922005-04-07 18:58:54 +0000618 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000619 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000620 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
621 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
622
623 // If the comparison constant has bits in the upper part, the
624 // zero-extended value could never match.
625 if (C2 & (~0ULL << InSize)) {
626 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
627 switch (Cond) {
628 case ISD::SETUGT:
629 case ISD::SETUGE:
630 case ISD::SETEQ: return getConstant(0, VT);
631 case ISD::SETULT:
632 case ISD::SETULE:
633 case ISD::SETNE: return getConstant(1, VT);
634 case ISD::SETGT:
635 case ISD::SETGE:
636 // True if the sign bit of C2 is set.
637 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
638 case ISD::SETLT:
639 case ISD::SETLE:
640 // True if the sign bit of C2 isn't set.
641 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
642 default:
643 break;
644 }
645 }
646
647 // Otherwise, we can perform the comparison with the low bits.
648 switch (Cond) {
649 case ISD::SETEQ:
650 case ISD::SETNE:
651 case ISD::SETUGT:
652 case ISD::SETUGE:
653 case ISD::SETULT:
654 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000655 return getSetCC(VT, N1.getOperand(0),
656 getConstant(C2, N1.getOperand(0).getValueType()),
657 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000658 default:
659 break; // todo, be more careful with signed comparisons
660 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000661 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
662 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
663 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
664 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
665 MVT::ValueType ExtDstTy = N1.getValueType();
666 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000667
Chris Lattner7b2880c2005-08-24 22:44:39 +0000668 // If the extended part has any inconsistent bits, it cannot ever
669 // compare equal. In other words, they have to be all ones or all
670 // zeros.
671 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000672 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000673 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
674 return getConstant(Cond == ISD::SETNE, VT);
675
676 // Otherwise, make this a use of a zext.
677 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000678 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000679 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000680 }
681
Chris Lattner67255a12005-04-07 18:14:58 +0000682 uint64_t MinVal, MaxVal;
683 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
684 if (ISD::isSignedIntSetCC(Cond)) {
685 MinVal = 1ULL << (OperandBitSize-1);
686 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
687 MaxVal = ~0ULL >> (65-OperandBitSize);
688 else
689 MaxVal = 0;
690 } else {
691 MinVal = 0;
692 MaxVal = ~0ULL >> (64-OperandBitSize);
693 }
694
695 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
696 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
697 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
698 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000699 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
700 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000701 }
702
703 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
704 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
705 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000706 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
707 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000708 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000709
Nate Begeman72ea2812005-04-14 08:56:52 +0000710 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
711 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000712
Nate Begeman72ea2812005-04-14 08:56:52 +0000713 // Canonicalize setgt X, Min --> setne X, Min
714 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000715 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000716
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000717 // If we have setult X, 1, turn it into seteq X, 0
718 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000719 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
720 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000721 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000722 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000723 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
724 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000725
726 // If we have "setcc X, C1", check to see if we can shrink the immediate
727 // by changing cc.
728
729 // SETUGT X, SINTMAX -> SETLT X, 0
730 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
731 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000732 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000733
734 // FIXME: Implement the rest of these.
735
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000736
737 // Fold bit comparisons when we can.
738 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
739 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
740 if (ConstantSDNode *AndRHS =
741 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
742 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
743 // Perform the xform if the AND RHS is a single bit.
744 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
745 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000746 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000747 TLI.getShiftAmountTy()));
748 }
749 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
750 // (X & 8) == 8 --> (X & 8) >> 3
751 // Perform the xform if C2 is a single bit.
752 if ((C2 & (C2-1)) == 0) {
753 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000754 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000755 }
756 }
757 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000758 }
Chris Lattner67255a12005-04-07 18:14:58 +0000759 } else if (isa<ConstantSDNode>(N1.Val)) {
760 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000761 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000762 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763
764 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
765 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
766 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000767
Chris Lattnerc3aae252005-01-07 07:46:32 +0000768 switch (Cond) {
769 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000770 case ISD::SETEQ: return getConstant(C1 == C2, VT);
771 case ISD::SETNE: return getConstant(C1 != C2, VT);
772 case ISD::SETLT: return getConstant(C1 < C2, VT);
773 case ISD::SETGT: return getConstant(C1 > C2, VT);
774 case ISD::SETLE: return getConstant(C1 <= C2, VT);
775 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000776 }
777 } else {
778 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000779 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000780 }
781
782 if (N1 == N2) {
783 // We can always fold X == Y for integer setcc's.
784 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000785 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000786 unsigned UOF = ISD::getUnorderedFlavor(Cond);
787 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000788 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000789 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000790 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
792 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000793 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
794 if (NewCond != Cond)
795 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000796 }
797
Chris Lattner5cdcc582005-01-09 20:52:51 +0000798 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000799 MVT::isInteger(N1.getValueType())) {
800 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
801 N1.getOpcode() == ISD::XOR) {
802 // Simplify (X+Y) == (X+Z) --> Y == Z
803 if (N1.getOpcode() == N2.getOpcode()) {
804 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000805 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000806 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000807 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000808 if (isCommutativeBinOp(N1.getOpcode())) {
809 // If X op Y == Y op X, try other combinations.
810 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000811 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000812 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000813 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000814 }
815 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000816
817 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000818
Chris Lattner68dc3102005-01-10 02:03:02 +0000819 // Simplify (X+Z) == X --> Z == 0
820 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000821 return getSetCC(VT, N1.getOperand(1),
822 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000823 if (N1.getOperand(1) == N2) {
824 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000825 return getSetCC(VT, N1.getOperand(0),
826 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000827 else {
828 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
829 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000830 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000831 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000832 N2, getConstant(1, TLI.getShiftAmountTy())),
833 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000834 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000835 }
836 }
837
Chris Lattner68dc3102005-01-10 02:03:02 +0000838 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
839 N2.getOpcode() == ISD::XOR) {
840 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000841 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000842 return getSetCC(VT, N2.getOperand(1),
843 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000844 } else if (N2.getOperand(1) == N1) {
845 if (isCommutativeBinOp(N2.getOpcode())) {
846 return getSetCC(VT, N2.getOperand(0),
847 getConstant(0, N2.getValueType()), Cond);
848 } else {
849 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
850 // X == (Z-X) --> X<<1 == Z
851 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
852 getConstant(1, TLI.getShiftAmountTy())),
853 N2.getOperand(0), Cond);
854 }
855 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000856 }
857 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000858
Chris Lattnerfda2b552005-04-18 04:48:12 +0000859 // Fold away ALL boolean setcc's.
860 if (N1.getValueType() == MVT::i1) {
861 switch (Cond) {
862 default: assert(0 && "Unknown integer setcc!");
863 case ISD::SETEQ: // X == Y -> (X^Y)^1
864 N1 = getNode(ISD::XOR, MVT::i1,
865 getNode(ISD::XOR, MVT::i1, N1, N2),
866 getConstant(1, MVT::i1));
867 break;
868 case ISD::SETNE: // X != Y --> (X^Y)
869 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
870 break;
871 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
872 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
873 N1 = getNode(ISD::AND, MVT::i1, N2,
874 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
875 break;
876 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
877 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
878 N1 = getNode(ISD::AND, MVT::i1, N1,
879 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
880 break;
881 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
882 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
883 N1 = getNode(ISD::OR, MVT::i1, N2,
884 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
885 break;
886 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
887 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
888 N1 = getNode(ISD::OR, MVT::i1, N1,
889 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
890 break;
891 }
892 if (VT != MVT::i1)
893 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
894 return N1;
895 }
896
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000897 // Could not fold it.
898 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000899}
900
Nate Begemanff663682005-08-13 06:14:17 +0000901SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
902 SDOperand N3, SDOperand N4,
903 ISD::CondCode CC) {
904 MVT::ValueType VT = N3.getValueType();
Nate Begeman1999b4b2005-08-25 20:04:38 +0000905 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman32c392a2005-08-13 06:00:21 +0000906 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
907 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
908 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
909
910 // Check to see if we can simplify the select into an fabs node
911 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
912 // Allow either -0.0 or 0.0
913 if (CFP->getValue() == 0.0) {
914 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
915 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
916 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
917 N1 == N4.getOperand(0))
918 return getNode(ISD::FABS, VT, N1);
919
920 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
921 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
922 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
923 N3.getOperand(0) == N4)
924 return getNode(ISD::FABS, VT, N4);
925 }
926 }
927
Nate Begeman1999b4b2005-08-25 20:04:38 +0000928 // check to see if we're select_cc'ing a select_cc.
929 // this allows us to turn:
930 // select_cc set[eq,ne] (select_cc cc, lhs, rhs, 1, 0), 0, true, false ->
931 // select_cc cc, lhs, rhs, true, false
932 if ((N1C && N1C->isNullValue() && N2.getOpcode() == ISD::SELECT_CC) ||
933 (N2C && N2C->isNullValue() && N1.getOpcode() == ISD::SELECT_CC) &&
934 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
935 SDOperand SCC = N1C ? N2 : N1;
936 ConstantSDNode *SCCT = dyn_cast<ConstantSDNode>(SCC.getOperand(2));
937 ConstantSDNode *SCCF = dyn_cast<ConstantSDNode>(SCC.getOperand(3));
938 if (SCCT && SCCF && SCCF->isNullValue() && SCCT->getValue() == 1ULL) {
939 if (CC == ISD::SETEQ) std::swap(N3, N4);
940 return getNode(ISD::SELECT_CC, N3.getValueType(), SCC.getOperand(0),
941 SCC.getOperand(1), N3, N4, SCC.getOperand(4));
942 }
943 }
944
Nate Begeman32c392a2005-08-13 06:00:21 +0000945 // Check to see if we can perform the "gzip trick", transforming
946 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
947 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
948 MVT::isInteger(N1.getValueType()) &&
949 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
950 MVT::ValueType XType = N1.getValueType();
951 MVT::ValueType AType = N3.getValueType();
952 if (XType >= AType) {
953 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
954 // single-bit constant. FIXME: remove once the dag combiner
955 // exists.
956 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
957 unsigned ShCtV = Log2_64(N3C->getValue());
958 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
959 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
960 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
961 if (XType > AType)
962 Shift = getNode(ISD::TRUNCATE, AType, Shift);
963 return getNode(ISD::AND, AType, Shift, N3);
964 }
965 SDOperand Shift = getNode(ISD::SRA, XType, N1,
966 getConstant(MVT::getSizeInBits(XType)-1,
967 TLI.getShiftAmountTy()));
968 if (XType > AType)
969 Shift = getNode(ISD::TRUNCATE, AType, Shift);
970 return getNode(ISD::AND, AType, Shift, N3);
971 }
972 }
973
Nate Begeman1999b4b2005-08-25 20:04:38 +0000974 // Check to see if this is the equivalent of setcc
Nate Begemancebd4332005-08-24 04:57:57 +0000975 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000976 MVT::ValueType XType = N1.getValueType();
Chris Lattner3ec5d742005-09-09 23:00:07 +0000977 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
978 SDOperand Res = getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
979 if (Res.getValueType() != VT)
980 Res = getNode(ISD::ZERO_EXTEND, VT, Res);
981 return Res;
982 }
Chris Lattner9d338cf2005-08-25 17:54:58 +0000983
Nate Begemancebd4332005-08-24 04:57:57 +0000984 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
985 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
Chris Lattner9d338cf2005-08-25 17:54:58 +0000986 TLI.isOperationLegal(ISD::CTLZ, XType)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000987 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
988 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000989 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000990 TLI.getShiftAmountTy()));
991 }
Nate Begemancebd4332005-08-24 04:57:57 +0000992 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
993 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
994 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
995 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
996 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
997 getConstant(MVT::getSizeInBits(XType)-1,
998 TLI.getShiftAmountTy()));
999 }
1000 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
1001 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
1002 SDOperand Sign = getNode(ISD::SRL, XType, N1,
1003 getConstant(MVT::getSizeInBits(XType)-1,
1004 TLI.getShiftAmountTy()));
1005 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
1006 }
Nate Begeman7042f152005-08-23 05:41:12 +00001007 }
1008
Nate Begeman32c392a2005-08-13 06:00:21 +00001009 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
1010 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
1011 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
1012 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
1013 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
1014 MVT::ValueType XType = N1.getValueType();
1015 if (SubC->isNullValue() && MVT::isInteger(XType)) {
1016 SDOperand Shift = getNode(ISD::SRA, XType, N1,
1017 getConstant(MVT::getSizeInBits(XType)-1,
1018 TLI.getShiftAmountTy()));
1019 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
1020 Shift);
1021 }
1022 }
1023 }
1024
1025 // Could not fold it.
1026 return SDOperand();
1027}
1028
Chris Lattnerc3aae252005-01-07 07:46:32 +00001029/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001030///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001032 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1033 if (!N) {
1034 N = new SDNode(Opcode, VT);
1035 AllNodes.push_back(N);
1036 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001037 return SDOperand(N, 0);
1038}
1039
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1041 SDOperand Operand) {
1042 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1043 uint64_t Val = C->getValue();
1044 switch (Opcode) {
1045 default: break;
1046 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001047 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1049 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001050 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1051 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001052 }
1053 }
1054
1055 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1056 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001057 case ISD::FNEG:
1058 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 case ISD::FP_ROUND:
1060 case ISD::FP_EXTEND:
1061 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001062 case ISD::FP_TO_SINT:
1063 return getConstant((int64_t)C->getValue(), VT);
1064 case ISD::FP_TO_UINT:
1065 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 }
1067
1068 unsigned OpOpcode = Operand.Val->getOpcode();
1069 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001070 case ISD::TokenFactor:
1071 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001072 case ISD::SIGN_EXTEND:
1073 if (Operand.getValueType() == VT) return Operand; // noop extension
1074 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1075 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1076 break;
1077 case ISD::ZERO_EXTEND:
1078 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001079 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001080 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001082 case ISD::ANY_EXTEND:
1083 if (Operand.getValueType() == VT) return Operand; // noop extension
1084 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1085 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1086 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1087 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001088 case ISD::TRUNCATE:
1089 if (Operand.getValueType() == VT) return Operand; // noop truncate
1090 if (OpOpcode == ISD::TRUNCATE)
1091 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001092 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1093 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001094 // If the source is smaller than the dest, we still need an extend.
1095 if (Operand.Val->getOperand(0).getValueType() < VT)
1096 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1097 else if (Operand.Val->getOperand(0).getValueType() > VT)
1098 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1099 else
1100 return Operand.Val->getOperand(0);
1101 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001103 case ISD::FNEG:
1104 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1105 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1106 Operand.Val->getOperand(0));
1107 if (OpOpcode == ISD::FNEG) // --X -> X
1108 return Operand.Val->getOperand(0);
1109 break;
1110 case ISD::FABS:
1111 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1112 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1113 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001114 }
1115
Chris Lattner43247a12005-08-25 19:12:10 +00001116 SDNode *N;
1117 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1118 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001119 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001120 E = N = new SDNode(Opcode, Operand);
1121 } else {
1122 N = new SDNode(Opcode, Operand);
1123 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001124 N->setValueTypes(VT);
1125 AllNodes.push_back(N);
1126 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001127}
1128
Chris Lattner36019aa2005-04-18 03:48:41 +00001129/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1130/// this predicate to simplify operations downstream. V and Mask are known to
1131/// be the same type.
1132static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1133 const TargetLowering &TLI) {
1134 unsigned SrcBits;
1135 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001136
Chris Lattner36019aa2005-04-18 03:48:41 +00001137 // If we know the result of a setcc has the top bits zero, use this info.
1138 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001139 case ISD::Constant:
1140 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1141
1142 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001143 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001144 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1145
1146 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001147 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001148 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1149 case ISD::ZERO_EXTEND:
1150 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1151 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
Nate Begeman9f52f282005-08-31 00:43:08 +00001152 case ISD::AssertZext:
1153 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
Nate Begemanfe75a282005-08-31 00:43:49 +00001154 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
Chris Lattner36019aa2005-04-18 03:48:41 +00001155 case ISD::AND:
1156 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001157 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001158 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1159
1160 // FALL THROUGH
1161 case ISD::OR:
1162 case ISD::XOR:
1163 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1164 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1165 case ISD::SELECT:
1166 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1167 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001168 case ISD::SELECT_CC:
1169 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1170 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001171 case ISD::SRL:
1172 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1173 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1174 uint64_t NewVal = Mask << ShAmt->getValue();
1175 SrcBits = MVT::getSizeInBits(Op.getValueType());
1176 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1177 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1178 }
1179 return false;
1180 case ISD::SHL:
1181 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1182 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1183 uint64_t NewVal = Mask >> ShAmt->getValue();
1184 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1185 }
1186 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001187 case ISD::CTTZ:
1188 case ISD::CTLZ:
1189 case ISD::CTPOP:
1190 // Bit counting instructions can not set the high bits of the result
1191 // register. The max number of bits sets depends on the input.
1192 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1193
Chris Lattner6ea92792005-04-25 21:03:25 +00001194 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001195 default: break;
1196 }
1197
1198 return false;
1199}
1200
1201
1202
Chris Lattnerc3aae252005-01-07 07:46:32 +00001203SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1204 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001205#ifndef NDEBUG
1206 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001207 case ISD::TokenFactor:
1208 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1209 N2.getValueType() == MVT::Other && "Invalid token factor!");
1210 break;
Chris Lattner76365122005-01-16 02:23:22 +00001211 case ISD::AND:
1212 case ISD::OR:
1213 case ISD::XOR:
1214 case ISD::UDIV:
1215 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001216 case ISD::MULHU:
1217 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001218 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1219 // fall through
1220 case ISD::ADD:
1221 case ISD::SUB:
1222 case ISD::MUL:
1223 case ISD::SDIV:
1224 case ISD::SREM:
1225 assert(N1.getValueType() == N2.getValueType() &&
1226 N1.getValueType() == VT && "Binary operator types must match!");
1227 break;
1228
1229 case ISD::SHL:
1230 case ISD::SRA:
1231 case ISD::SRL:
1232 assert(VT == N1.getValueType() &&
1233 "Shift operators return type must be the same as their first arg");
1234 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001235 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001236 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001237 case ISD::FP_ROUND_INREG: {
1238 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1239 assert(VT == N1.getValueType() && "Not an inreg round!");
1240 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1241 "Cannot FP_ROUND_INREG integer types");
1242 assert(EVT <= VT && "Not rounding down!");
1243 break;
1244 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001245 case ISD::AssertSext:
1246 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001247 case ISD::SIGN_EXTEND_INREG: {
1248 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1249 assert(VT == N1.getValueType() && "Not an inreg extend!");
1250 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1251 "Cannot *_EXTEND_INREG FP types");
1252 assert(EVT <= VT && "Not extending!");
1253 }
1254
Chris Lattner76365122005-01-16 02:23:22 +00001255 default: break;
1256 }
1257#endif
1258
Chris Lattnerc3aae252005-01-07 07:46:32 +00001259 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1260 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1261 if (N1C) {
1262 if (N2C) {
1263 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1264 switch (Opcode) {
1265 case ISD::ADD: return getConstant(C1 + C2, VT);
1266 case ISD::SUB: return getConstant(C1 - C2, VT);
1267 case ISD::MUL: return getConstant(C1 * C2, VT);
1268 case ISD::UDIV:
1269 if (C2) return getConstant(C1 / C2, VT);
1270 break;
1271 case ISD::UREM :
1272 if (C2) return getConstant(C1 % C2, VT);
1273 break;
1274 case ISD::SDIV :
1275 if (C2) return getConstant(N1C->getSignExtended() /
1276 N2C->getSignExtended(), VT);
1277 break;
1278 case ISD::SREM :
1279 if (C2) return getConstant(N1C->getSignExtended() %
1280 N2C->getSignExtended(), VT);
1281 break;
1282 case ISD::AND : return getConstant(C1 & C2, VT);
1283 case ISD::OR : return getConstant(C1 | C2, VT);
1284 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001285 case ISD::SHL : return getConstant(C1 << C2, VT);
1286 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001287 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001288 default: break;
1289 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001290 } else { // Cannonicalize constant to RHS if commutative
1291 if (isCommutativeBinOp(Opcode)) {
1292 std::swap(N1C, N2C);
1293 std::swap(N1, N2);
1294 }
1295 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001296
Chris Lattner1e111c72005-09-07 05:37:01 +00001297 if (!CombinerEnabled) {
Chris Lattner88218ef2005-01-19 17:29:49 +00001298 switch (Opcode) {
1299 default: break;
1300 case ISD::SHL: // shl 0, X -> 0
1301 if (N1C->isNullValue()) return N1;
1302 break;
1303 case ISD::SRL: // srl 0, X -> 0
1304 if (N1C->isNullValue()) return N1;
1305 break;
1306 case ISD::SRA: // sra -1, X -> -1
1307 if (N1C->isAllOnesValue()) return N1;
1308 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001309 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1310 // Extending a constant? Just return the extended constant.
1311 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1312 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001313 }
Chris Lattner1e111c72005-09-07 05:37:01 +00001314 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315 }
1316
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001317 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001318 if (N2C) {
1319 uint64_t C2 = N2C->getValue();
1320
1321 switch (Opcode) {
1322 case ISD::ADD:
1323 if (!C2) return N1; // add X, 0 -> X
1324 break;
1325 case ISD::SUB:
1326 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001327 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 case ISD::MUL:
1329 if (!C2) return N2; // mul X, 0 -> 0
1330 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1331 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1332
Chris Lattnerb48da392005-01-23 04:39:44 +00001333 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001334 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001335 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001336 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001337 }
1338 break;
1339
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001340 case ISD::MULHU:
1341 case ISD::MULHS:
1342 if (!C2) return N2; // mul X, 0 -> 0
1343
1344 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1345 return getConstant(0, VT);
1346
1347 // Many others could be handled here, including -1, powers of 2, etc.
1348 break;
1349
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001351 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001352 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001353 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001354 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355 }
1356 break;
1357
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001358 case ISD::SHL:
1359 case ISD::SRL:
1360 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001361 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001362 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001363 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1364 return getNode(ISD::UNDEF, N1.getValueType());
1365 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001366 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001367
1368 if (Opcode == ISD::SRA) {
1369 // If the sign bit is known to be zero, switch this to a SRL.
1370 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001371 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001372 TLI))
1373 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1374 } else {
1375 // If the part left over is known to be zero, the whole thing is zero.
1376 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1377 if (Opcode == ISD::SRL) {
1378 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1379 return getConstant(0, N1.getValueType());
1380 } else if (Opcode == ISD::SHL) {
1381 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1382 return getConstant(0, N1.getValueType());
1383 }
1384 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001385
1386 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1387 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1388 unsigned OpSAC = OpSA->getValue();
1389 if (N1.getOpcode() == ISD::SHL) {
1390 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1391 return getConstant(0, N1.getValueType());
1392 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1393 getConstant(C2+OpSAC, N2.getValueType()));
1394 } else if (N1.getOpcode() == ISD::SRL) {
1395 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1396 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1397 getConstant(~0ULL << OpSAC, VT));
1398 if (C2 > OpSAC) {
1399 return getNode(ISD::SHL, VT, Mask,
1400 getConstant(C2-OpSAC, N2.getValueType()));
1401 } else {
1402 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1403 return getNode(ISD::SRL, VT, Mask,
1404 getConstant(OpSAC-C2, N2.getValueType()));
1405 }
1406 } else if (N1.getOpcode() == ISD::SRA) {
1407 // if C1 == C2, just mask out low bits.
1408 if (C2 == OpSAC)
1409 return getNode(ISD::AND, VT, N1.getOperand(0),
1410 getConstant(~0ULL << C2, VT));
1411 }
1412 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001413 break;
1414
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 case ISD::AND:
1416 if (!C2) return N2; // X and 0 -> 0
1417 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001418 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001419
Chris Lattner36019aa2005-04-18 03:48:41 +00001420 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1421 return getConstant(0, VT);
1422
Chris Lattner588bbbf2005-04-21 06:28:15 +00001423 {
1424 uint64_t NotC2 = ~C2;
1425 if (VT != MVT::i64)
1426 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1427
1428 if (MaskedValueIsZero(N1, NotC2, TLI))
1429 return N1; // if (X & ~C2) -> 0, the and is redundant
1430 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001431
Chris Lattnerdea29e22005-04-10 01:13:15 +00001432 // FIXME: Should add a corresponding version of this for
1433 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1434 // we don't have yet.
Chris Lattner4ed11b42005-09-02 00:17:32 +00001435 // FIXME: NOW WE DO, add this.
Chris Lattnerdea29e22005-04-10 01:13:15 +00001436
Chris Lattner0f2287b2005-04-13 02:38:18 +00001437 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1438 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001439 // If we are masking out the part of our input that was extended, just
1440 // mask the input to the extension directly.
1441 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001442 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001443 if ((C2 & (~0ULL << ExtendBits)) == 0)
1444 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001445 } else if (N1.getOpcode() == ISD::OR) {
1446 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1447 if ((ORI->getValue() & C2) == C2) {
1448 // If the 'or' is setting all of the bits that we are masking for,
1449 // we know the result of the AND will be the AND mask itself.
1450 return N2;
1451 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001452 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001453 break;
1454 case ISD::OR:
1455 if (!C2)return N1; // X or 0 -> X
1456 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001457 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001458 break;
1459 case ISD::XOR:
1460 if (!C2) return N1; // X xor 0 -> X
Nate Begeman39f60a22005-09-01 23:25:49 +00001461 if (N2C->getValue() == 1 && N1.Val->getOpcode() == ISD::SETCC) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001462 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001463 // !(X op Y) -> (X !op Y)
1464 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001465 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1466 return getSetCC(SetCC->getValueType(0),
1467 SetCC->getOperand(0), SetCC->getOperand(1),
1468 ISD::getSetCCInverse(CC, isInteger));
Nate Begeman39f60a22005-09-01 23:25:49 +00001469 } else if (N2C->isAllOnesValue()) {
1470 if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001471 SDNode *Op = N1.Val;
1472 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1473 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1474 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1475 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1476 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1477 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1478 if (Op->getOpcode() == ISD::AND)
1479 return getNode(ISD::OR, VT, LHS, RHS);
1480 return getNode(ISD::AND, VT, LHS, RHS);
1481 }
1482 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001483 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001484 }
1485 break;
1486 }
1487
1488 // Reassociate ((X op C1) op C2) if possible.
1489 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1490 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001491 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001492 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001493 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001494 }
1495
1496 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1497 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001498 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001499 if (N2CFP) {
1500 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1501 switch (Opcode) {
1502 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1503 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1504 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1505 case ISD::SDIV:
1506 if (C2) return getConstantFP(C1 / C2, VT);
1507 break;
1508 case ISD::SREM :
1509 if (C2) return getConstantFP(fmod(C1, C2), VT);
1510 break;
1511 default: break;
1512 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001513 } else { // Cannonicalize constant to RHS if commutative
1514 if (isCommutativeBinOp(Opcode)) {
1515 std::swap(N1CFP, N2CFP);
1516 std::swap(N1, N2);
1517 }
1518 }
1519
Nate Begeman99801192005-09-07 23:25:52 +00001520 if (!CombinerEnabled) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001521 if (Opcode == ISD::FP_ROUND_INREG)
1522 return getNode(ISD::FP_EXTEND, VT,
1523 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
Nate Begeman99801192005-09-07 23:25:52 +00001524 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001525 }
1526
Chris Lattnerc3aae252005-01-07 07:46:32 +00001527 // Finally, fold operations that do not require constants.
1528 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001529 case ISD::TokenFactor:
Chris Lattner1e111c72005-09-07 05:37:01 +00001530 if (!CombinerEnabled) {
Chris Lattner39908e02005-01-19 18:01:40 +00001531 if (N1.getOpcode() == ISD::EntryToken)
1532 return N2;
1533 if (N2.getOpcode() == ISD::EntryToken)
1534 return N1;
Chris Lattner1e111c72005-09-07 05:37:01 +00001535 }
Chris Lattner39908e02005-01-19 18:01:40 +00001536 break;
1537
Chris Lattnerc3aae252005-01-07 07:46:32 +00001538 case ISD::AND:
1539 case ISD::OR:
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001540 if (!CombinerEnabled) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001541 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1542 SDNode *LHS = N1.Val, *RHS = N2.Val;
1543 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1544 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1545 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1546 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001547
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001548 if (LR == RR && isa<ConstantSDNode>(LR) &&
1549 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1550 // (X != 0) | (Y != 0) -> (X|Y != 0)
1551 // (X == 0) & (Y == 0) -> (X|Y == 0)
1552 // (X < 0) | (Y < 0) -> (X|Y < 0)
1553 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1554 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1555 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1556 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1557 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1558 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001559
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001560 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1561 // (X == -1) & (Y == -1) -> (X&Y == -1)
1562 // (X != -1) | (Y != -1) -> (X&Y != -1)
1563 // (X > -1) | (Y > -1) -> (X&Y > -1)
1564 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1565 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1566 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1567 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1568 LR, Op2);
1569 // (X > -1) & (Y > -1) -> (X|Y > -1)
1570 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1571 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1572 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001573 }
1574 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001575
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001576 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1577 if (LL == RR && LR == RL) {
1578 Op2 = ISD::getSetCCSwappedOperands(Op2);
1579 goto MatchedBackwards;
1580 }
1581
1582 if (LL == RL && LR == RR) {
1583 MatchedBackwards:
1584 ISD::CondCode Result;
1585 bool isInteger = MVT::isInteger(LL.getValueType());
1586 if (Opcode == ISD::OR)
1587 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1588 else
1589 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1590
1591 if (Result != ISD::SETCC_INVALID)
1592 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1593 }
1594 }
1595
Chris Lattner7467c9b2005-04-18 04:11:19 +00001596 // and/or zext(a), zext(b) -> zext(and/or a, b)
1597 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1598 N2.getOpcode() == ISD::ZERO_EXTEND &&
1599 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1600 return getNode(ISD::ZERO_EXTEND, VT,
1601 getNode(Opcode, N1.getOperand(0).getValueType(),
1602 N1.getOperand(0), N2.getOperand(0)));
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001603 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001604 break;
1605 case ISD::XOR:
Nate Begeman223df222005-09-08 20:18:10 +00001606 if (!CombinerEnabled) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001607 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
Nate Begeman223df222005-09-08 20:18:10 +00001608 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001609 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001610 case ISD::ADD:
Nate Begeman223df222005-09-08 20:18:10 +00001611 if (!CombinerEnabled) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001612 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1613 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1614 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1615 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001616 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1617 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1618 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1619 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1620 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1621 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001622 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1623 !MVT::isFloatingPoint(N2.getValueType()))
1624 return N2.Val->getOperand(0); // A+(B-A) -> B
Nate Begeman223df222005-09-08 20:18:10 +00001625 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001626 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001627 case ISD::SUB:
Nate Begeman223df222005-09-08 20:18:10 +00001628 if (!CombinerEnabled) {
Chris Lattnerabd21822005-01-09 20:09:57 +00001629 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001630 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001631 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001632 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001633 if (N1.Val->getOperand(1) == N2 &&
1634 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001635 return N1.Val->getOperand(0); // (A+B)-B == A
1636 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001637 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1638 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Nate Begeman223df222005-09-08 20:18:10 +00001639 }
Chris Lattnerabd21822005-01-09 20:09:57 +00001640 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001641 case ISD::FP_ROUND_INREG:
1642 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1643 break;
1644 case ISD::SIGN_EXTEND_INREG: {
1645 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1646 if (EVT == VT) return N1; // Not actually extending
Nate Begeman223df222005-09-08 20:18:10 +00001647 if (!CombinerEnabled) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001648 // If we are sign extending an extension, use the original source.
Nate Begeman56eb8682005-08-30 02:44:00 +00001649 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG ||
1650 N1.getOpcode() == ISD::AssertSext)
Chris Lattner15e4b012005-07-10 00:07:11 +00001651 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1652 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001653
Chris Lattner15e4b012005-07-10 00:07:11 +00001654 // If we are sign extending a sextload, return just the load.
1655 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001656 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Nate Begeman56eb8682005-08-30 02:44:00 +00001657 return N1;
Chris Lattner15e4b012005-07-10 00:07:11 +00001658
1659 // If we are extending the result of a setcc, and we already know the
1660 // contents of the top bits, eliminate the extension.
1661 if (N1.getOpcode() == ISD::SETCC &&
1662 TLI.getSetCCResultContents() ==
1663 TargetLowering::ZeroOrNegativeOneSetCCResult)
1664 return N1;
Nate Begeman56eb8682005-08-30 02:44:00 +00001665
Chris Lattner15e4b012005-07-10 00:07:11 +00001666 // If we are sign extending the result of an (and X, C) operation, and we
1667 // know the extended bits are zeros already, don't do the extend.
1668 if (N1.getOpcode() == ISD::AND)
1669 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1670 uint64_t Mask = N1C->getValue();
1671 unsigned NumBits = MVT::getSizeInBits(EVT);
1672 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1673 return N1;
1674 }
Nate Begeman223df222005-09-08 20:18:10 +00001675 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001676 break;
1677 }
1678
Nate Begemaneea805e2005-04-13 21:23:31 +00001679 // FIXME: figure out how to safely handle things like
1680 // int foo(int x) { return 1 << (x & 255); }
1681 // int bar() { return foo(256); }
1682#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001683 case ISD::SHL:
1684 case ISD::SRL:
1685 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001686 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001687 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001688 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001689 else if (N2.getOpcode() == ISD::AND)
1690 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1691 // If the and is only masking out bits that cannot effect the shift,
1692 // eliminate the and.
1693 unsigned NumBits = MVT::getSizeInBits(VT);
1694 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1695 return getNode(Opcode, VT, N1, N2.getOperand(0));
1696 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001697 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001698#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001699 }
1700
Chris Lattner27e9b412005-05-11 18:57:39 +00001701 // Memoize this node if possible.
1702 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001703 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1704 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001705 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1706 if (BON) return SDOperand(BON, 0);
1707
1708 BON = N = new SDNode(Opcode, N1, N2);
1709 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001710 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001711 }
1712
Chris Lattner3e011362005-05-14 07:45:46 +00001713 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001714 AllNodes.push_back(N);
1715 return SDOperand(N, 0);
1716}
1717
Chris Lattner88de6e72005-05-12 00:17:04 +00001718// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001719// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001720void SDNode::setAdjCallChain(SDOperand N) {
1721 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001722 assert((getOpcode() == ISD::CALLSEQ_START ||
1723 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001724
1725 Operands[0].Val->removeUser(this);
1726 Operands[0] = N;
1727 N.Val->Uses.push_back(this);
1728}
1729
1730
1731
Chris Lattnerc3aae252005-01-07 07:46:32 +00001732SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001733 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001734 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001735 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1736 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001737 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001738
1739 // Loads have a token chain.
1740 N->setValueTypes(VT, MVT::Other);
1741 AllNodes.push_back(N);
1742 return SDOperand(N, 0);
1743}
1744
Chris Lattner5f056bf2005-07-10 01:55:33 +00001745
1746SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1747 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1748 MVT::ValueType EVT) {
1749 std::vector<SDOperand> Ops;
1750 Ops.reserve(4);
1751 Ops.push_back(Chain);
1752 Ops.push_back(Ptr);
1753 Ops.push_back(SV);
1754 Ops.push_back(getValueType(EVT));
1755 std::vector<MVT::ValueType> VTs;
1756 VTs.reserve(2);
1757 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1758 return getNode(Opcode, VTs, Ops);
1759}
1760
Chris Lattnerc3aae252005-01-07 07:46:32 +00001761SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1762 SDOperand N1, SDOperand N2, SDOperand N3) {
1763 // Perform various simplifications.
1764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1765 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1766 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1767 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001768 case ISD::SETCC: {
1769 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001770 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001771 if (Simp.Val) return Simp;
1772 break;
1773 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001774 case ISD::SELECT:
1775 if (N1C)
1776 if (N1C->getValue())
1777 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001778 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001779 return N3; // select false, X, Y -> Y
1780
1781 if (N2 == N3) return N2; // select C, X, X -> X
1782
1783 if (VT == MVT::i1) { // Boolean SELECT
1784 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001785 if (N2C->getValue()) // select C, 1, X -> C | X
1786 return getNode(ISD::OR, VT, N1, N3);
1787 else // select C, 0, X -> ~C & X
1788 return getNode(ISD::AND, VT,
1789 getNode(ISD::XOR, N1.getValueType(), N1,
1790 getConstant(1, N1.getValueType())), N3);
1791 } else if (N3C) {
1792 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1793 return getNode(ISD::OR, VT,
1794 getNode(ISD::XOR, N1.getValueType(), N1,
1795 getConstant(1, N1.getValueType())), N2);
1796 else // select C, X, 0 -> C & X
1797 return getNode(ISD::AND, VT, N1, N2);
1798 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001799
1800 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1801 return getNode(ISD::OR, VT, N1, N3);
1802 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1803 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001804 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001805 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001806 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1807 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001808 if (Simp.Val) return Simp;
1809 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001810 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001811 case ISD::BRCOND:
1812 if (N2C)
1813 if (N2C->getValue()) // Unconditional branch
1814 return getNode(ISD::BR, MVT::Other, N1, N3);
1815 else
1816 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001817 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818 }
1819
Chris Lattner385328c2005-05-14 07:42:29 +00001820 std::vector<SDOperand> Ops;
1821 Ops.reserve(3);
1822 Ops.push_back(N1);
1823 Ops.push_back(N2);
1824 Ops.push_back(N3);
1825
Chris Lattner43247a12005-08-25 19:12:10 +00001826 // Memoize node if it doesn't produce a flag.
1827 SDNode *N;
1828 if (VT != MVT::Flag) {
1829 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1830 if (E) return SDOperand(E, 0);
1831 E = N = new SDNode(Opcode, N1, N2, N3);
1832 } else {
1833 N = new SDNode(Opcode, N1, N2, N3);
1834 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001835 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001836 AllNodes.push_back(N);
1837 return SDOperand(N, 0);
1838}
1839
1840SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001841 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001842 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001843 std::vector<SDOperand> Ops;
1844 Ops.reserve(4);
1845 Ops.push_back(N1);
1846 Ops.push_back(N2);
1847 Ops.push_back(N3);
1848 Ops.push_back(N4);
1849 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001850}
1851
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001852SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1853 SDOperand N1, SDOperand N2, SDOperand N3,
1854 SDOperand N4, SDOperand N5) {
1855 std::vector<SDOperand> Ops;
1856 Ops.reserve(5);
1857 Ops.push_back(N1);
1858 Ops.push_back(N2);
1859 Ops.push_back(N3);
1860 Ops.push_back(N4);
1861 Ops.push_back(N5);
1862 return getNode(Opcode, VT, Ops);
1863}
1864
1865
Chris Lattner0437cdd2005-05-09 04:14:13 +00001866SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001867 assert((!V || isa<PointerType>(V->getType())) &&
1868 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001869 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1870 if (N) return SDOperand(N, 0);
1871
1872 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001873 AllNodes.push_back(N);
1874 return SDOperand(N, 0);
1875}
1876
1877SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001878 std::vector<SDOperand> &Ops) {
1879 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001880 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001881 case 1: return getNode(Opcode, VT, Ops[0]);
1882 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1883 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001884 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001885 }
Chris Lattneref847df2005-04-09 03:27:28 +00001886
Chris Lattner89c34632005-05-14 06:20:26 +00001887 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001888 switch (Opcode) {
1889 default: break;
1890 case ISD::BRCONDTWOWAY:
1891 if (N1C)
1892 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001893 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001894 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001895 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001896 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001897 case ISD::BRTWOWAY_CC:
1898 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1899 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1900 "LHS and RHS of comparison must have same type!");
1901 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001902 case ISD::TRUNCSTORE: {
1903 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1904 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1905#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1906 // If this is a truncating store of a constant, convert to the desired type
1907 // and store it instead.
1908 if (isa<Constant>(Ops[0])) {
1909 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1910 if (isa<Constant>(Op))
1911 N1 = Op;
1912 }
1913 // Also for ConstantFP?
1914#endif
1915 if (Ops[0].getValueType() == EVT) // Normal store?
1916 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1917 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1918 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1919 "Can't do FP-INT conversion!");
1920 break;
1921 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001922 case ISD::SELECT_CC: {
1923 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1924 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1925 "LHS and RHS of condition must have same type!");
1926 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1927 "True and False arms of SelectCC must have same type!");
1928 assert(Ops[2].getValueType() == VT &&
1929 "select_cc node must be of same type as true and false value!");
1930 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1931 cast<CondCodeSDNode>(Ops[4])->get());
1932 if (Simp.Val) return Simp;
1933 break;
1934 }
1935 case ISD::BR_CC: {
1936 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1937 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1938 "LHS/RHS of comparison should match types!");
1939 // Use SimplifySetCC to simplify SETCC's.
1940 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1941 cast<CondCodeSDNode>(Ops[1])->get());
1942 if (Simp.Val) {
1943 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1944 if (C->getValue() & 1) // Unconditional branch
1945 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1946 else
1947 return Ops[0]; // Unconditional Fall through
1948 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1949 Ops[2] = Simp.getOperand(0);
1950 Ops[3] = Simp.getOperand(1);
1951 Ops[1] = Simp.getOperand(2);
1952 }
1953 }
1954 break;
1955 }
Chris Lattneref847df2005-04-09 03:27:28 +00001956 }
1957
Chris Lattner385328c2005-05-14 07:42:29 +00001958 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001959 SDNode *N;
1960 if (VT != MVT::Flag) {
1961 SDNode *&E =
1962 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1963 if (E) return SDOperand(E, 0);
1964 E = N = new SDNode(Opcode, Ops);
1965 } else {
1966 N = new SDNode(Opcode, Ops);
1967 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001968 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001969 AllNodes.push_back(N);
1970 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001971}
1972
Chris Lattner89c34632005-05-14 06:20:26 +00001973SDOperand SelectionDAG::getNode(unsigned Opcode,
1974 std::vector<MVT::ValueType> &ResultTys,
1975 std::vector<SDOperand> &Ops) {
1976 if (ResultTys.size() == 1)
1977 return getNode(Opcode, ResultTys[0], Ops);
1978
Chris Lattner5f056bf2005-07-10 01:55:33 +00001979 switch (Opcode) {
1980 case ISD::EXTLOAD:
1981 case ISD::SEXTLOAD:
1982 case ISD::ZEXTLOAD: {
1983 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1984 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1985 // If they are asking for an extending load from/to the same thing, return a
1986 // normal load.
1987 if (ResultTys[0] == EVT)
1988 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1989 assert(EVT < ResultTys[0] &&
1990 "Should only be an extending load, not truncating!");
1991 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1992 "Cannot sign/zero extend a FP load!");
1993 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1994 "Cannot convert from FP to Int or Int -> FP!");
1995 break;
1996 }
1997
Chris Lattnere89083a2005-05-14 07:25:05 +00001998 // FIXME: figure out how to safely handle things like
1999 // int foo(int x) { return 1 << (x & 255); }
2000 // int bar() { return foo(256); }
2001#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00002002 case ISD::SRA_PARTS:
2003 case ISD::SRL_PARTS:
2004 case ISD::SHL_PARTS:
2005 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00002006 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00002007 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2008 else if (N3.getOpcode() == ISD::AND)
2009 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
2010 // If the and is only masking out bits that cannot effect the shift,
2011 // eliminate the and.
2012 unsigned NumBits = MVT::getSizeInBits(VT)*2;
2013 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
2014 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
2015 }
2016 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00002017#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00002018 }
Chris Lattner89c34632005-05-14 06:20:26 +00002019
Chris Lattner43247a12005-08-25 19:12:10 +00002020 // Memoize the node unless it returns a flag.
2021 SDNode *N;
2022 if (ResultTys.back() != MVT::Flag) {
2023 SDNode *&E =
2024 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
2025 if (E) return SDOperand(E, 0);
2026 E = N = new SDNode(Opcode, Ops);
2027 } else {
2028 N = new SDNode(Opcode, Ops);
2029 }
Chris Lattner89c34632005-05-14 06:20:26 +00002030 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00002031 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00002032 return SDOperand(N, 0);
2033}
2034
Chris Lattner149c58c2005-08-16 18:17:10 +00002035
2036/// SelectNodeTo - These are used for target selectors to *mutate* the
2037/// specified node to have the specified return type, Target opcode, and
2038/// operands. Note that target opcodes are stored as
2039/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002040void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2041 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00002042 RemoveNodeFromCSEMaps(N);
2043 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2044 N->setValueTypes(VT);
2045}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002046void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2047 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00002048 RemoveNodeFromCSEMaps(N);
2049 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2050 N->setValueTypes(VT);
2051 N->setOperands(Op1);
2052}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002053void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2054 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002055 SDOperand Op2) {
2056 RemoveNodeFromCSEMaps(N);
2057 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2058 N->setValueTypes(VT);
2059 N->setOperands(Op1, Op2);
2060}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002061void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00002062 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002063 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00002064 RemoveNodeFromCSEMaps(N);
2065 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2066 N->setValueTypes(VT1, VT2);
2067 N->setOperands(Op1, Op2);
2068}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002069void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2070 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002071 SDOperand Op2, SDOperand Op3) {
2072 RemoveNodeFromCSEMaps(N);
2073 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2074 N->setValueTypes(VT);
2075 N->setOperands(Op1, Op2, Op3);
2076}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002077void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2078 MVT::ValueType VT1, MVT::ValueType VT2,
2079 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002080 RemoveNodeFromCSEMaps(N);
2081 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2082 N->setValueTypes(VT1, VT2);
2083 N->setOperands(Op1, Op2, Op3);
2084}
2085
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002086void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2087 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00002088 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
2089 RemoveNodeFromCSEMaps(N);
2090 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2091 N->setValueTypes(VT);
2092 N->setOperands(Op1, Op2, Op3, Op4);
2093}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002094void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2095 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00002096 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2097 SDOperand Op5) {
2098 RemoveNodeFromCSEMaps(N);
2099 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2100 N->setValueTypes(VT);
2101 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2102}
Chris Lattner149c58c2005-08-16 18:17:10 +00002103
Chris Lattner8b8749f2005-08-17 19:00:20 +00002104/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2105/// This can cause recursive merging of nodes in the DAG.
2106///
Chris Lattner8b52f212005-08-26 18:36:28 +00002107/// This version assumes From/To have a single result value.
2108///
Chris Lattner1e111c72005-09-07 05:37:01 +00002109void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2110 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002111 SDNode *From = FromN.Val, *To = ToN.Val;
2112 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2113 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002114 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002115
Chris Lattner8b8749f2005-08-17 19:00:20 +00002116 while (!From->use_empty()) {
2117 // Process users until they are all gone.
2118 SDNode *U = *From->use_begin();
2119
2120 // This node is about to morph, remove its old self from the CSE maps.
2121 RemoveNodeFromCSEMaps(U);
2122
2123 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2124 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002125 From->removeUser(U);
2126 U->Operands[i].Val = To;
2127 To->addUser(U);
2128 }
2129
2130 // Now that we have modified U, add it back to the CSE maps. If it already
2131 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002132 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2133 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002134 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002135 if (Deleted) Deleted->push_back(U);
2136 DeleteNodeNotInCSEMaps(U);
2137 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002138 }
2139}
2140
2141/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2142/// This can cause recursive merging of nodes in the DAG.
2143///
2144/// This version assumes From/To have matching types and numbers of result
2145/// values.
2146///
Chris Lattner1e111c72005-09-07 05:37:01 +00002147void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2148 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002149 assert(From != To && "Cannot replace uses of with self");
2150 assert(From->getNumValues() == To->getNumValues() &&
2151 "Cannot use this version of ReplaceAllUsesWith!");
2152 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002153 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002154 return;
2155 }
2156
2157 while (!From->use_empty()) {
2158 // Process users until they are all gone.
2159 SDNode *U = *From->use_begin();
2160
2161 // This node is about to morph, remove its old self from the CSE maps.
2162 RemoveNodeFromCSEMaps(U);
2163
2164 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2165 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002166 From->removeUser(U);
2167 U->Operands[i].Val = To;
2168 To->addUser(U);
2169 }
2170
2171 // Now that we have modified U, add it back to the CSE maps. If it already
2172 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002173 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2174 ReplaceAllUsesWith(U, Existing, Deleted);
2175 // U is now dead.
2176 if (Deleted) Deleted->push_back(U);
2177 DeleteNodeNotInCSEMaps(U);
2178 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002179 }
2180}
2181
Chris Lattner8b52f212005-08-26 18:36:28 +00002182/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2183/// This can cause recursive merging of nodes in the DAG.
2184///
2185/// This version can replace From with any result values. To must match the
2186/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002187void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002188 const std::vector<SDOperand> &To,
2189 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002190 assert(From->getNumValues() == To.size() &&
2191 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002192 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002193 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002194 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002195 return;
2196 }
2197
2198 while (!From->use_empty()) {
2199 // Process users until they are all gone.
2200 SDNode *U = *From->use_begin();
2201
2202 // This node is about to morph, remove its old self from the CSE maps.
2203 RemoveNodeFromCSEMaps(U);
2204
2205 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2206 if (U->getOperand(i).Val == From) {
2207 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002208 From->removeUser(U);
2209 U->Operands[i] = ToOp;
2210 ToOp.Val->addUser(U);
2211 }
2212
2213 // Now that we have modified U, add it back to the CSE maps. If it already
2214 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002215 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2216 ReplaceAllUsesWith(U, Existing, Deleted);
2217 // U is now dead.
2218 if (Deleted) Deleted->push_back(U);
2219 DeleteNodeNotInCSEMaps(U);
2220 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002221 }
2222}
2223
2224
Jim Laskey58b968b2005-08-17 20:08:02 +00002225//===----------------------------------------------------------------------===//
2226// SDNode Class
2227//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002228
Chris Lattner5c884562005-01-12 18:37:47 +00002229/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2230/// indicated value. This method ignores uses of other values defined by this
2231/// operation.
2232bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2233 assert(Value < getNumValues() && "Bad value!");
2234
2235 // If there is only one value, this is easy.
2236 if (getNumValues() == 1)
2237 return use_size() == NUses;
2238 if (Uses.size() < NUses) return false;
2239
2240 SDOperand TheValue(this, Value);
2241
2242 std::set<SDNode*> UsersHandled;
2243
2244 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2245 UI != E; ++UI) {
2246 SDNode *User = *UI;
2247 if (User->getNumOperands() == 1 ||
2248 UsersHandled.insert(User).second) // First time we've seen this?
2249 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2250 if (User->getOperand(i) == TheValue) {
2251 if (NUses == 0)
2252 return false; // too many uses
2253 --NUses;
2254 }
2255 }
2256
2257 // Found exactly the right number of uses?
2258 return NUses == 0;
2259}
2260
2261
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002262const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002263 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002264 default:
2265 if (getOpcode() < ISD::BUILTIN_OP_END)
2266 return "<<Unknown DAG Node>>";
2267 else {
2268 if (G)
2269 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002270 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2271 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002272 return "<<Unknown Target Node>>";
2273 }
2274
Andrew Lenharth95762122005-03-31 21:24:06 +00002275 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002276 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002277 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002278 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002279 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002280 case ISD::AssertSext: return "AssertSext";
2281 case ISD::AssertZext: return "AssertZext";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002282 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002283 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002284 case ISD::ConstantFP: return "ConstantFP";
2285 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002286 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002287 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002288 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002289 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002290 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002291 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002292 case ISD::ConstantPool: return "ConstantPool";
2293 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002294 case ISD::CopyToReg: return "CopyToReg";
2295 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002296 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002297 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002298
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002299 // Unary operators
2300 case ISD::FABS: return "fabs";
2301 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002302 case ISD::FSQRT: return "fsqrt";
2303 case ISD::FSIN: return "fsin";
2304 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002305
2306 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002307 case ISD::ADD: return "add";
2308 case ISD::SUB: return "sub";
2309 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002310 case ISD::MULHU: return "mulhu";
2311 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002312 case ISD::SDIV: return "sdiv";
2313 case ISD::UDIV: return "udiv";
2314 case ISD::SREM: return "srem";
2315 case ISD::UREM: return "urem";
2316 case ISD::AND: return "and";
2317 case ISD::OR: return "or";
2318 case ISD::XOR: return "xor";
2319 case ISD::SHL: return "shl";
2320 case ISD::SRA: return "sra";
2321 case ISD::SRL: return "srl";
2322
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002323 case ISD::SETCC: return "setcc";
2324 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002325 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002326 case ISD::ADD_PARTS: return "add_parts";
2327 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002328 case ISD::SHL_PARTS: return "shl_parts";
2329 case ISD::SRA_PARTS: return "sra_parts";
2330 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002331
Chris Lattner7f644642005-04-28 21:44:03 +00002332 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002333 case ISD::SIGN_EXTEND: return "sign_extend";
2334 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002335 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002336 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002337 case ISD::TRUNCATE: return "truncate";
2338 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002339 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002340 case ISD::FP_EXTEND: return "fp_extend";
2341
2342 case ISD::SINT_TO_FP: return "sint_to_fp";
2343 case ISD::UINT_TO_FP: return "uint_to_fp";
2344 case ISD::FP_TO_SINT: return "fp_to_sint";
2345 case ISD::FP_TO_UINT: return "fp_to_uint";
2346
2347 // Control flow instructions
2348 case ISD::BR: return "br";
2349 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002350 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002351 case ISD::BR_CC: return "br_cc";
2352 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002353 case ISD::RET: return "ret";
2354 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002355 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002356 case ISD::CALLSEQ_START: return "callseq_start";
2357 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002358
2359 // Other operators
2360 case ISD::LOAD: return "load";
2361 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002362 case ISD::EXTLOAD: return "extload";
2363 case ISD::SEXTLOAD: return "sextload";
2364 case ISD::ZEXTLOAD: return "zextload";
2365 case ISD::TRUNCSTORE: return "truncstore";
2366
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002367 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2368 case ISD::EXTRACT_ELEMENT: return "extract_element";
2369 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002370 case ISD::MEMSET: return "memset";
2371 case ISD::MEMCPY: return "memcpy";
2372 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002373
Chris Lattner276260b2005-05-11 04:50:30 +00002374 // Bit counting
2375 case ISD::CTPOP: return "ctpop";
2376 case ISD::CTTZ: return "cttz";
2377 case ISD::CTLZ: return "ctlz";
2378
2379 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002380 case ISD::READPORT: return "readport";
2381 case ISD::WRITEPORT: return "writeport";
2382 case ISD::READIO: return "readio";
2383 case ISD::WRITEIO: return "writeio";
2384
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002385 case ISD::CONDCODE:
2386 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002387 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002388 case ISD::SETOEQ: return "setoeq";
2389 case ISD::SETOGT: return "setogt";
2390 case ISD::SETOGE: return "setoge";
2391 case ISD::SETOLT: return "setolt";
2392 case ISD::SETOLE: return "setole";
2393 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002394
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002395 case ISD::SETO: return "seto";
2396 case ISD::SETUO: return "setuo";
2397 case ISD::SETUEQ: return "setue";
2398 case ISD::SETUGT: return "setugt";
2399 case ISD::SETUGE: return "setuge";
2400 case ISD::SETULT: return "setult";
2401 case ISD::SETULE: return "setule";
2402 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002403
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002404 case ISD::SETEQ: return "seteq";
2405 case ISD::SETGT: return "setgt";
2406 case ISD::SETGE: return "setge";
2407 case ISD::SETLT: return "setlt";
2408 case ISD::SETLE: return "setle";
2409 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002410 }
2411 }
2412}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002413
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002414void SDNode::dump() const { dump(0); }
2415void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002416 std::cerr << (void*)this << ": ";
2417
2418 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2419 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002420 if (getValueType(i) == MVT::Other)
2421 std::cerr << "ch";
2422 else
2423 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002424 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002425 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002426
2427 std::cerr << " ";
2428 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2429 if (i) std::cerr << ", ";
2430 std::cerr << (void*)getOperand(i).Val;
2431 if (unsigned RN = getOperand(i).ResNo)
2432 std::cerr << ":" << RN;
2433 }
2434
2435 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2436 std::cerr << "<" << CSDN->getValue() << ">";
2437 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2438 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002439 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002440 dyn_cast<GlobalAddressSDNode>(this)) {
2441 std::cerr << "<";
2442 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002443 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002444 std::cerr << "<" << FIDN->getIndex() << ">";
2445 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002446 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002447 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002448 std::cerr << "<";
2449 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2450 if (LBB)
2451 std::cerr << LBB->getName() << " ";
2452 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002453 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002454 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2455 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2456 } else {
2457 std::cerr << " #" << R->getReg();
2458 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002459 } else if (const ExternalSymbolSDNode *ES =
2460 dyn_cast<ExternalSymbolSDNode>(this)) {
2461 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002462 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2463 if (M->getValue())
2464 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2465 else
2466 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002467 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2468 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002469 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002470}
2471
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002472static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2474 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002475 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002476 else
2477 std::cerr << "\n" << std::string(indent+2, ' ')
2478 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002479
Chris Lattnerea946cd2005-01-09 20:38:33 +00002480
2481 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002482 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002483}
2484
Chris Lattnerc3aae252005-01-07 07:46:32 +00002485void SelectionDAG::dump() const {
2486 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002487 std::vector<SDNode*> Nodes(AllNodes);
2488 std::sort(Nodes.begin(), Nodes.end());
2489
2490 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002491 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002492 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002493 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002494
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002495 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002496
Chris Lattnerc3aae252005-01-07 07:46:32 +00002497 std::cerr << "\n\n";
2498}
2499