blob: dab757ef8a245dcef633e2cd972044d05048260e [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 Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
34 case ISD::AND:
35 case ISD::OR:
36 case ISD::XOR: return true;
37 default: return false; // FIXME: Need commutative info for user ops!
38 }
39}
40
41static bool isAssociativeBinOp(unsigned Opcode) {
42 switch (Opcode) {
43 case ISD::ADD:
44 case ISD::MUL:
45 case ISD::AND:
46 case ISD::OR:
47 case ISD::XOR: return true;
48 default: return false; // FIXME: Need associative info for user ops!
49 }
50}
51
Chris Lattner5cdcc582005-01-09 20:52:51 +000052// isInvertibleForFree - Return true if there is no cost to emitting the logical
53// inverse of this node.
54static bool isInvertibleForFree(SDOperand N) {
55 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000056 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000057 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000058 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000059}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
69bool ConstantFPSDNode::isExactlyValue(double V) const {
70 return DoubleToBits(V) == DoubleToBits(Value);
71}
72
73//===----------------------------------------------------------------------===//
74// ISD Class
75//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000076
Chris Lattnerc3aae252005-01-07 07:46:32 +000077/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
78/// when given the operation for (X op Y).
79ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
80 // To perform this operation, we just need to swap the L and G bits of the
81 // operation.
82 unsigned OldL = (Operation >> 2) & 1;
83 unsigned OldG = (Operation >> 1) & 1;
84 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
85 (OldL << 1) | // New G bit
86 (OldG << 2)); // New L bit.
87}
88
89/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
90/// 'op' is a valid SetCC operation.
91ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
92 unsigned Operation = Op;
93 if (isInteger)
94 Operation ^= 7; // Flip L, G, E bits, but not U.
95 else
96 Operation ^= 15; // Flip all of the condition bits.
97 if (Operation > ISD::SETTRUE2)
98 Operation &= ~8; // Don't let N and U bits get set.
99 return ISD::CondCode(Operation);
100}
101
102
103/// isSignedOp - For an integer comparison, return 1 if the comparison is a
104/// signed operation and 2 if the result is an unsigned comparison. Return zero
105/// if the operation does not depend on the sign of the input (setne and seteq).
106static int isSignedOp(ISD::CondCode Opcode) {
107 switch (Opcode) {
108 default: assert(0 && "Illegal integer setcc operation!");
109 case ISD::SETEQ:
110 case ISD::SETNE: return 0;
111 case ISD::SETLT:
112 case ISD::SETLE:
113 case ISD::SETGT:
114 case ISD::SETGE: return 1;
115 case ISD::SETULT:
116 case ISD::SETULE:
117 case ISD::SETUGT:
118 case ISD::SETUGE: return 2;
119 }
120}
121
122/// getSetCCOrOperation - Return the result of a logical OR between different
123/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
124/// returns SETCC_INVALID if it is not possible to represent the resultant
125/// comparison.
126ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
127 bool isInteger) {
128 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
129 // Cannot fold a signed integer setcc with an unsigned integer setcc.
130 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131
Chris Lattnerc3aae252005-01-07 07:46:32 +0000132 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 // If the N and U bits get set then the resultant comparison DOES suddenly
135 // care about orderedness, and is true when ordered.
136 if (Op > ISD::SETTRUE2)
137 Op &= ~16; // Clear the N bit.
138 return ISD::CondCode(Op);
139}
140
141/// getSetCCAndOperation - Return the result of a logical AND between different
142/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
143/// function returns zero if it is not possible to represent the resultant
144/// comparison.
145ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
146 bool isInteger) {
147 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
148 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000149 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000150
151 // Combine all of the condition bits.
152 return ISD::CondCode(Op1 & Op2);
153}
154
Chris Lattnerb48da392005-01-23 04:39:44 +0000155const TargetMachine &SelectionDAG::getTarget() const {
156 return TLI.getTargetMachine();
157}
158
Jim Laskey58b968b2005-08-17 20:08:02 +0000159//===----------------------------------------------------------------------===//
160// SelectionDAG Class
161//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000162
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000163/// RemoveDeadNodes - This method deletes all unreachable nodes in the
164/// SelectionDAG, including nodes (like loads) that have uses of their token
165/// chain but no other uses and no side effect. If a node is passed in as an
166/// argument, it is used as the seed for node deletion.
167void SelectionDAG::RemoveDeadNodes(SDNode *N) {
168 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
169
170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
172 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
173
Chris Lattner8b8749f2005-08-17 19:00:20 +0000174 // If we have a hint to start from, use it.
175 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
177 Restart:
178 unsigned NumNodes = AllNodeSet.size();
179 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
180 I != E; ++I) {
181 // Try to delete this node.
182 DeleteNodeIfDead(*I, &AllNodeSet);
183
184 // If we actually deleted any nodes, do not use invalid iterators in
185 // AllNodeSet.
186 if (AllNodeSet.size() != NumNodes)
187 goto Restart;
188 }
189
190 // Restore AllNodes.
191 if (AllNodes.size() != NumNodes)
192 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
193
194 // If the root changed (e.g. it was a dead load, update the root).
195 setRoot(DummyNode->getOperand(0));
196
197 // Now that we are done with the dummy node, delete it.
198 DummyNode->getOperand(0).Val->removeUser(DummyNode);
199 delete DummyNode;
200}
201
Chris Lattner149c58c2005-08-16 18:17:10 +0000202
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
204 if (!N->use_empty())
205 return;
206
207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000209 RemoveNodeFromCSEMaps(N);
210
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
213 while (!N->Operands.empty()) {
214 SDNode *O = N->Operands.back().Val;
215 N->Operands.pop_back();
216 O->removeUser(N);
217
218 // Now that we removed this operand, see if there are no uses of it left.
219 DeleteNodeIfDead(O, NodeSet);
220 }
221
222 // Remove the node from the nodes set and delete it.
223 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
224 AllNodeSet.erase(N);
225
226 // Now that the node is gone, check to see if any of the operands of this node
227 // are dead now.
228 delete N;
229}
230
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000231void SelectionDAG::DeleteNode(SDNode *N) {
232 assert(N->use_empty() && "Cannot delete a node that is not dead!");
233
234 // First take this out of the appropriate CSE map.
235 RemoveNodeFromCSEMaps(N);
236
237 // Remove it from the AllNodes list.
238 for (std::vector<SDNode*>::iterator I = AllNodes.begin(); ; ++I) {
239 assert(I != AllNodes.end() && "Node not in AllNodes list??");
240 if (*I == N) {
241 // Erase from the vector, which is not ordered.
242 std::swap(*I, AllNodes.back());
243 AllNodes.pop_back();
244 break;
245 }
246 }
247
248 // Drop all of the operands and decrement used nodes use counts.
249 while (!N->Operands.empty()) {
250 SDNode *O = N->Operands.back().Val;
251 N->Operands.pop_back();
252 O->removeUser(N);
253 }
254
255 delete N;
256}
257
Chris Lattner149c58c2005-08-16 18:17:10 +0000258/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
259/// correspond to it. This is useful when we're about to delete or repurpose
260/// the node. We don't want future request for structurally identical nodes
261/// to return N anymore.
262void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000263 switch (N->getOpcode()) {
264 case ISD::Constant:
265 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
266 N->getValueType(0)));
267 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000268 case ISD::TargetConstant:
269 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
270 N->getValueType(0)));
271 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000272 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000273 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
274 ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000275 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000276 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000277 case ISD::CONDCODE:
278 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
279 "Cond code doesn't exist!");
280 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
281 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000282 case ISD::GlobalAddress:
283 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
284 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000285 case ISD::TargetGlobalAddress:
286 TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
287 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 case ISD::FrameIndex:
289 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
290 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000291 case ISD::TargetFrameIndex:
292 TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
293 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000294 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000295 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000296 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000297 case ISD::TargetConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000298 TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get());
Chris Lattner4025a9c2005-08-25 05:03:06 +0000299 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000300 case ISD::BasicBlock:
301 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
302 break;
303 case ISD::ExternalSymbol:
304 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
305 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000306 case ISD::VALUETYPE:
307 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
308 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000309 case ISD::Register:
310 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
311 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000312 case ISD::SRCVALUE: {
313 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
314 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
315 break;
316 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000317 case ISD::LOAD:
318 Loads.erase(std::make_pair(N->getOperand(1),
319 std::make_pair(N->getOperand(0),
320 N->getValueType(0))));
321 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000322 default:
323 if (N->getNumOperands() == 1)
324 UnaryOps.erase(std::make_pair(N->getOpcode(),
325 std::make_pair(N->getOperand(0),
326 N->getValueType(0))));
327 else if (N->getNumOperands() == 2)
328 BinaryOps.erase(std::make_pair(N->getOpcode(),
329 std::make_pair(N->getOperand(0),
330 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000331 else if (N->getNumValues() == 1) {
332 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
333 OneResultNodes.erase(std::make_pair(N->getOpcode(),
334 std::make_pair(N->getValueType(0),
335 Ops)));
336 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000337 // Remove the node from the ArbitraryNodes map.
338 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
339 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
340 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
341 std::make_pair(RV, Ops)));
342 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000343 break;
344 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000345}
346
Chris Lattner8b8749f2005-08-17 19:00:20 +0000347/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
348/// has been taken out and modified in some way. If the specified node already
349/// exists in the CSE maps, do not modify the maps, but return the existing node
350/// instead. If it doesn't exist, add it and return null.
351///
352SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
353 assert(N->getNumOperands() && "This is a leaf node!");
354 if (N->getOpcode() == ISD::LOAD) {
355 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
356 std::make_pair(N->getOperand(0),
357 N->getValueType(0)))];
358 if (L) return L;
359 L = N;
360 } else if (N->getNumOperands() == 1) {
361 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
362 std::make_pair(N->getOperand(0),
363 N->getValueType(0)))];
364 if (U) return U;
365 U = N;
366 } else if (N->getNumOperands() == 2) {
367 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
368 std::make_pair(N->getOperand(0),
369 N->getOperand(1)))];
370 if (B) return B;
371 B = N;
372 } else if (N->getNumValues() == 1) {
373 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
374 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
375 std::make_pair(N->getValueType(0), Ops))];
376 if (ORN) return ORN;
377 ORN = N;
378 } else {
379 // Remove the node from the ArbitraryNodes map.
380 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
381 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
382 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
383 std::make_pair(RV, Ops))];
384 if (AN) return AN;
385 AN = N;
386 }
387 return 0;
388
389}
390
391
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000392
Chris Lattner78ec3112003-08-11 14:57:33 +0000393SelectionDAG::~SelectionDAG() {
394 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
395 delete AllNodes[i];
396}
397
Chris Lattner0f2287b2005-04-13 02:38:18 +0000398SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000399 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000400 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000401 return getNode(ISD::AND, Op.getValueType(), Op,
402 getConstant(Imm, Op.getValueType()));
403}
404
Chris Lattnerc3aae252005-01-07 07:46:32 +0000405SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
406 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
407 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000408 if (VT != MVT::i64)
409 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000410
Chris Lattnerc3aae252005-01-07 07:46:32 +0000411 SDNode *&N = Constants[std::make_pair(Val, VT)];
412 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000413 N = new ConstantSDNode(false, Val, VT);
414 AllNodes.push_back(N);
415 return SDOperand(N, 0);
416}
417
418SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
419 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
420 // Mask out any bits that are not valid for this constant.
421 if (VT != MVT::i64)
422 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
423
424 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
425 if (N) return SDOperand(N, 0);
426 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000427 AllNodes.push_back(N);
428 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000429}
430
Chris Lattnerc3aae252005-01-07 07:46:32 +0000431SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
432 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
433 if (VT == MVT::f32)
434 Val = (float)Val; // Mask out extra precision.
435
Chris Lattnerd8658612005-02-17 20:17:32 +0000436 // Do the map lookup using the actual bit pattern for the floating point
437 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
438 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000439 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000440 if (N) return SDOperand(N, 0);
441 N = new ConstantFPSDNode(Val, VT);
442 AllNodes.push_back(N);
443 return SDOperand(N, 0);
444}
445
446
447
448SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
449 MVT::ValueType VT) {
450 SDNode *&N = GlobalValues[GV];
451 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000452 N = new GlobalAddressSDNode(false, GV, VT);
453 AllNodes.push_back(N);
454 return SDOperand(N, 0);
455}
456
457SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
458 MVT::ValueType VT) {
459 SDNode *&N = TargetGlobalValues[GV];
460 if (N) return SDOperand(N, 0);
461 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000462 AllNodes.push_back(N);
463 return SDOperand(N, 0);
464}
465
466SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
467 SDNode *&N = FrameIndices[FI];
468 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000469 N = new FrameIndexSDNode(FI, VT, false);
470 AllNodes.push_back(N);
471 return SDOperand(N, 0);
472}
473
474SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
475 SDNode *&N = TargetFrameIndices[FI];
476 if (N) return SDOperand(N, 0);
477 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000478 AllNodes.push_back(N);
479 return SDOperand(N, 0);
480}
481
Chris Lattner5839bf22005-08-26 17:15:30 +0000482SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) {
483 SDNode *&N = ConstantPoolIndices[C];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000484 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000485 N = new ConstantPoolSDNode(C, VT, false);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000486 AllNodes.push_back(N);
487 return SDOperand(N, 0);
488}
489
Chris Lattner5839bf22005-08-26 17:15:30 +0000490SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) {
491 SDNode *&N = TargetConstantPoolIndices[C];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000492 if (N) return SDOperand(N, 0);
Chris Lattner5839bf22005-08-26 17:15:30 +0000493 N = new ConstantPoolSDNode(C, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000494 AllNodes.push_back(N);
495 return SDOperand(N, 0);
496}
497
498SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
499 SDNode *&N = BBNodes[MBB];
500 if (N) return SDOperand(N, 0);
501 N = new BasicBlockSDNode(MBB);
502 AllNodes.push_back(N);
503 return SDOperand(N, 0);
504}
505
Chris Lattner15e4b012005-07-10 00:07:11 +0000506SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
507 if ((unsigned)VT >= ValueTypeNodes.size())
508 ValueTypeNodes.resize(VT+1);
509 if (ValueTypeNodes[VT] == 0) {
510 ValueTypeNodes[VT] = new VTSDNode(VT);
511 AllNodes.push_back(ValueTypeNodes[VT]);
512 }
513
514 return SDOperand(ValueTypeNodes[VT], 0);
515}
516
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
518 SDNode *&N = ExternalSymbols[Sym];
519 if (N) return SDOperand(N, 0);
520 N = new ExternalSymbolSDNode(Sym, VT);
521 AllNodes.push_back(N);
522 return SDOperand(N, 0);
523}
524
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000525SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
526 if ((unsigned)Cond >= CondCodeNodes.size())
527 CondCodeNodes.resize(Cond+1);
528
Chris Lattner079a27a2005-08-09 20:40:02 +0000529 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000530 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000531 AllNodes.push_back(CondCodeNodes[Cond]);
532 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000533 return SDOperand(CondCodeNodes[Cond], 0);
534}
535
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000536SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
537 if (Reg >= RegNodes.size())
538 RegNodes.resize(Reg+1);
539 RegisterSDNode *&Result = RegNodes[Reg];
540 if (Result) {
541 assert(Result->getValueType(0) == VT &&
542 "Inconsistent value types for machine registers");
543 } else {
544 Result = new RegisterSDNode(Reg, VT);
545 AllNodes.push_back(Result);
546 }
547 return SDOperand(Result, 0);
548}
549
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000550SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
551 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000552 // These setcc operations always fold.
553 switch (Cond) {
554 default: break;
555 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000556 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000557 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000558 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000559 }
560
Chris Lattner67255a12005-04-07 18:14:58 +0000561 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
562 uint64_t C2 = N2C->getValue();
563 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
564 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000565
Chris Lattnerc3aae252005-01-07 07:46:32 +0000566 // Sign extend the operands if required
567 if (ISD::isSignedIntSetCC(Cond)) {
568 C1 = N1C->getSignExtended();
569 C2 = N2C->getSignExtended();
570 }
571
572 switch (Cond) {
573 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000574 case ISD::SETEQ: return getConstant(C1 == C2, VT);
575 case ISD::SETNE: return getConstant(C1 != C2, VT);
576 case ISD::SETULT: return getConstant(C1 < C2, VT);
577 case ISD::SETUGT: return getConstant(C1 > C2, VT);
578 case ISD::SETULE: return getConstant(C1 <= C2, VT);
579 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
580 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
581 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
582 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
583 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000584 }
Chris Lattner24673922005-04-07 18:58:54 +0000585 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000586 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000587 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
588 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
589
590 // If the comparison constant has bits in the upper part, the
591 // zero-extended value could never match.
592 if (C2 & (~0ULL << InSize)) {
593 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
594 switch (Cond) {
595 case ISD::SETUGT:
596 case ISD::SETUGE:
597 case ISD::SETEQ: return getConstant(0, VT);
598 case ISD::SETULT:
599 case ISD::SETULE:
600 case ISD::SETNE: return getConstant(1, VT);
601 case ISD::SETGT:
602 case ISD::SETGE:
603 // True if the sign bit of C2 is set.
604 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
605 case ISD::SETLT:
606 case ISD::SETLE:
607 // True if the sign bit of C2 isn't set.
608 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
609 default:
610 break;
611 }
612 }
613
614 // Otherwise, we can perform the comparison with the low bits.
615 switch (Cond) {
616 case ISD::SETEQ:
617 case ISD::SETNE:
618 case ISD::SETUGT:
619 case ISD::SETUGE:
620 case ISD::SETULT:
621 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000622 return getSetCC(VT, N1.getOperand(0),
623 getConstant(C2, N1.getOperand(0).getValueType()),
624 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000625 default:
626 break; // todo, be more careful with signed comparisons
627 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000628 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
629 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
630 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
631 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
632 MVT::ValueType ExtDstTy = N1.getValueType();
633 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
634
635 // If the extended part has any inconsistent bits, it cannot ever
636 // compare equal. In other words, they have to be all ones or all
637 // zeros.
638 uint64_t ExtBits =
639 (~0ULL >> 64-ExtSrcTyBits) & (~0ULL << (ExtDstTyBits-1));
640 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
641 return getConstant(Cond == ISD::SETNE, VT);
642
643 // Otherwise, make this a use of a zext.
644 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
645 getConstant(C2 & (~0ULL >> 64-ExtSrcTyBits), ExtDstTy),
646 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000647 }
648
Chris Lattner67255a12005-04-07 18:14:58 +0000649 uint64_t MinVal, MaxVal;
650 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
651 if (ISD::isSignedIntSetCC(Cond)) {
652 MinVal = 1ULL << (OperandBitSize-1);
653 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
654 MaxVal = ~0ULL >> (65-OperandBitSize);
655 else
656 MaxVal = 0;
657 } else {
658 MinVal = 0;
659 MaxVal = ~0ULL >> (64-OperandBitSize);
660 }
661
662 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
663 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
664 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
665 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000666 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
667 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000668 }
669
670 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
671 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
672 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000673 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
674 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000675 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000676
Nate Begeman72ea2812005-04-14 08:56:52 +0000677 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
678 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000679
Nate Begeman72ea2812005-04-14 08:56:52 +0000680 // Canonicalize setgt X, Min --> setne X, Min
681 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000682 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000683
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000684 // If we have setult X, 1, turn it into seteq X, 0
685 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000686 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
687 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000688 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000689 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000690 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
691 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000692
693 // If we have "setcc X, C1", check to see if we can shrink the immediate
694 // by changing cc.
695
696 // SETUGT X, SINTMAX -> SETLT X, 0
697 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
698 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000699 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000700
701 // FIXME: Implement the rest of these.
702
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000703
704 // Fold bit comparisons when we can.
705 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
706 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
707 if (ConstantSDNode *AndRHS =
708 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
709 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
710 // Perform the xform if the AND RHS is a single bit.
711 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
712 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000713 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000714 TLI.getShiftAmountTy()));
715 }
716 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
717 // (X & 8) == 8 --> (X & 8) >> 3
718 // Perform the xform if C2 is a single bit.
719 if ((C2 & (C2-1)) == 0) {
720 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000721 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000722 }
723 }
724 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000725 }
Chris Lattner67255a12005-04-07 18:14:58 +0000726 } else if (isa<ConstantSDNode>(N1.Val)) {
727 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000728 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000729 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000730
731 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
732 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
733 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000734
Chris Lattnerc3aae252005-01-07 07:46:32 +0000735 switch (Cond) {
736 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000737 case ISD::SETEQ: return getConstant(C1 == C2, VT);
738 case ISD::SETNE: return getConstant(C1 != C2, VT);
739 case ISD::SETLT: return getConstant(C1 < C2, VT);
740 case ISD::SETGT: return getConstant(C1 > C2, VT);
741 case ISD::SETLE: return getConstant(C1 <= C2, VT);
742 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743 }
744 } else {
745 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000746 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000747 }
748
749 if (N1 == N2) {
750 // We can always fold X == Y for integer setcc's.
751 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000752 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000753 unsigned UOF = ISD::getUnorderedFlavor(Cond);
754 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000755 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000756 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000757 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000758 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
759 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000760 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
761 if (NewCond != Cond)
762 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763 }
764
Chris Lattner5cdcc582005-01-09 20:52:51 +0000765 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000766 MVT::isInteger(N1.getValueType())) {
767 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
768 N1.getOpcode() == ISD::XOR) {
769 // Simplify (X+Y) == (X+Z) --> Y == Z
770 if (N1.getOpcode() == N2.getOpcode()) {
771 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000772 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000773 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000774 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000775 if (isCommutativeBinOp(N1.getOpcode())) {
776 // If X op Y == Y op X, try other combinations.
777 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000778 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000779 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000780 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000781 }
782 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000783
784 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000785
Chris Lattner68dc3102005-01-10 02:03:02 +0000786 // Simplify (X+Z) == X --> Z == 0
787 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000788 return getSetCC(VT, N1.getOperand(1),
789 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000790 if (N1.getOperand(1) == N2) {
791 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000792 return getSetCC(VT, N1.getOperand(0),
793 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000794 else {
795 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
796 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000797 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000798 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000799 N2, getConstant(1, TLI.getShiftAmountTy())),
800 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000801 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000802 }
803 }
804
Chris Lattner68dc3102005-01-10 02:03:02 +0000805 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
806 N2.getOpcode() == ISD::XOR) {
807 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000808 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000809 return getSetCC(VT, N2.getOperand(1),
810 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000811 } else if (N2.getOperand(1) == N1) {
812 if (isCommutativeBinOp(N2.getOpcode())) {
813 return getSetCC(VT, N2.getOperand(0),
814 getConstant(0, N2.getValueType()), Cond);
815 } else {
816 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
817 // X == (Z-X) --> X<<1 == Z
818 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
819 getConstant(1, TLI.getShiftAmountTy())),
820 N2.getOperand(0), Cond);
821 }
822 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000823 }
824 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000825
Chris Lattnerfda2b552005-04-18 04:48:12 +0000826 // Fold away ALL boolean setcc's.
827 if (N1.getValueType() == MVT::i1) {
828 switch (Cond) {
829 default: assert(0 && "Unknown integer setcc!");
830 case ISD::SETEQ: // X == Y -> (X^Y)^1
831 N1 = getNode(ISD::XOR, MVT::i1,
832 getNode(ISD::XOR, MVT::i1, N1, N2),
833 getConstant(1, MVT::i1));
834 break;
835 case ISD::SETNE: // X != Y --> (X^Y)
836 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
837 break;
838 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
839 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
840 N1 = getNode(ISD::AND, MVT::i1, N2,
841 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
842 break;
843 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
844 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
845 N1 = getNode(ISD::AND, MVT::i1, N1,
846 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
847 break;
848 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
849 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
850 N1 = getNode(ISD::OR, MVT::i1, N2,
851 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
852 break;
853 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
854 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
855 N1 = getNode(ISD::OR, MVT::i1, N1,
856 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
857 break;
858 }
859 if (VT != MVT::i1)
860 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
861 return N1;
862 }
863
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000864 // Could not fold it.
865 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000866}
867
Nate Begemanff663682005-08-13 06:14:17 +0000868SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
869 SDOperand N3, SDOperand N4,
870 ISD::CondCode CC) {
871 MVT::ValueType VT = N3.getValueType();
Nate Begeman1999b4b2005-08-25 20:04:38 +0000872 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begeman32c392a2005-08-13 06:00:21 +0000873 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
874 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
875 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
876
877 // Check to see if we can simplify the select into an fabs node
878 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
879 // Allow either -0.0 or 0.0
880 if (CFP->getValue() == 0.0) {
881 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
882 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
883 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
884 N1 == N4.getOperand(0))
885 return getNode(ISD::FABS, VT, N1);
886
887 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
888 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
889 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
890 N3.getOperand(0) == N4)
891 return getNode(ISD::FABS, VT, N4);
892 }
893 }
894
Nate Begeman1999b4b2005-08-25 20:04:38 +0000895 // check to see if we're select_cc'ing a select_cc.
896 // this allows us to turn:
897 // select_cc set[eq,ne] (select_cc cc, lhs, rhs, 1, 0), 0, true, false ->
898 // select_cc cc, lhs, rhs, true, false
899 if ((N1C && N1C->isNullValue() && N2.getOpcode() == ISD::SELECT_CC) ||
900 (N2C && N2C->isNullValue() && N1.getOpcode() == ISD::SELECT_CC) &&
901 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
902 SDOperand SCC = N1C ? N2 : N1;
903 ConstantSDNode *SCCT = dyn_cast<ConstantSDNode>(SCC.getOperand(2));
904 ConstantSDNode *SCCF = dyn_cast<ConstantSDNode>(SCC.getOperand(3));
905 if (SCCT && SCCF && SCCF->isNullValue() && SCCT->getValue() == 1ULL) {
906 if (CC == ISD::SETEQ) std::swap(N3, N4);
907 return getNode(ISD::SELECT_CC, N3.getValueType(), SCC.getOperand(0),
908 SCC.getOperand(1), N3, N4, SCC.getOperand(4));
909 }
910 }
911
Nate Begeman32c392a2005-08-13 06:00:21 +0000912 // Check to see if we can perform the "gzip trick", transforming
913 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
914 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
915 MVT::isInteger(N1.getValueType()) &&
916 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
917 MVT::ValueType XType = N1.getValueType();
918 MVT::ValueType AType = N3.getValueType();
919 if (XType >= AType) {
920 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
921 // single-bit constant. FIXME: remove once the dag combiner
922 // exists.
923 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
924 unsigned ShCtV = Log2_64(N3C->getValue());
925 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
926 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
927 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
928 if (XType > AType)
929 Shift = getNode(ISD::TRUNCATE, AType, Shift);
930 return getNode(ISD::AND, AType, Shift, N3);
931 }
932 SDOperand Shift = getNode(ISD::SRA, XType, N1,
933 getConstant(MVT::getSizeInBits(XType)-1,
934 TLI.getShiftAmountTy()));
935 if (XType > AType)
936 Shift = getNode(ISD::TRUNCATE, AType, Shift);
937 return getNode(ISD::AND, AType, Shift, N3);
938 }
939 }
940
Nate Begeman1999b4b2005-08-25 20:04:38 +0000941 // Check to see if this is the equivalent of setcc
Nate Begemancebd4332005-08-24 04:57:57 +0000942 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000943 MVT::ValueType XType = N1.getValueType();
Chris Lattner9d338cf2005-08-25 17:54:58 +0000944 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy()))
Nate Begemancebd4332005-08-24 04:57:57 +0000945 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Chris Lattner9d338cf2005-08-25 17:54:58 +0000946
Nate Begemancebd4332005-08-24 04:57:57 +0000947 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
948 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
Chris Lattner9d338cf2005-08-25 17:54:58 +0000949 TLI.isOperationLegal(ISD::CTLZ, XType)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000950 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
951 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000952 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000953 TLI.getShiftAmountTy()));
954 }
Nate Begemancebd4332005-08-24 04:57:57 +0000955 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
956 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
957 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
958 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
959 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
960 getConstant(MVT::getSizeInBits(XType)-1,
961 TLI.getShiftAmountTy()));
962 }
963 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
964 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
965 SDOperand Sign = getNode(ISD::SRL, XType, N1,
966 getConstant(MVT::getSizeInBits(XType)-1,
967 TLI.getShiftAmountTy()));
968 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
969 }
Nate Begeman7042f152005-08-23 05:41:12 +0000970 }
971
Nate Begeman32c392a2005-08-13 06:00:21 +0000972 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
973 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
974 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
975 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
976 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
977 MVT::ValueType XType = N1.getValueType();
978 if (SubC->isNullValue() && MVT::isInteger(XType)) {
979 SDOperand Shift = getNode(ISD::SRA, XType, N1,
980 getConstant(MVT::getSizeInBits(XType)-1,
981 TLI.getShiftAmountTy()));
982 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
983 Shift);
984 }
985 }
986 }
987
988 // Could not fold it.
989 return SDOperand();
990}
991
Chris Lattnerc3aae252005-01-07 07:46:32 +0000992/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000993///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000994SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
995 SDNode *N = new SDNode(Opcode, VT);
996 AllNodes.push_back(N);
997 return SDOperand(N, 0);
998}
999
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1001 SDOperand Operand) {
1002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1003 uint64_t Val = C->getValue();
1004 switch (Opcode) {
1005 default: break;
1006 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
1007 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1008 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001009 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1010 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 }
1012 }
1013
1014 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1015 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001016 case ISD::FNEG:
1017 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 case ISD::FP_ROUND:
1019 case ISD::FP_EXTEND:
1020 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001021 case ISD::FP_TO_SINT:
1022 return getConstant((int64_t)C->getValue(), VT);
1023 case ISD::FP_TO_UINT:
1024 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001025 }
1026
1027 unsigned OpOpcode = Operand.Val->getOpcode();
1028 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001029 case ISD::TokenFactor:
1030 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 case ISD::SIGN_EXTEND:
1032 if (Operand.getValueType() == VT) return Operand; // noop extension
1033 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1034 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1035 break;
1036 case ISD::ZERO_EXTEND:
1037 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +00001038 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001039 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 break;
1041 case ISD::TRUNCATE:
1042 if (Operand.getValueType() == VT) return Operand; // noop truncate
1043 if (OpOpcode == ISD::TRUNCATE)
1044 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001045 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
1046 // If the source is smaller than the dest, we still need an extend.
1047 if (Operand.Val->getOperand(0).getValueType() < VT)
1048 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1049 else if (Operand.Val->getOperand(0).getValueType() > VT)
1050 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1051 else
1052 return Operand.Val->getOperand(0);
1053 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001055 case ISD::FNEG:
1056 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
1057 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
1058 Operand.Val->getOperand(0));
1059 if (OpOpcode == ISD::FNEG) // --X -> X
1060 return Operand.Val->getOperand(0);
1061 break;
1062 case ISD::FABS:
1063 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1064 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1065 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 }
1067
Chris Lattner43247a12005-08-25 19:12:10 +00001068 SDNode *N;
1069 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1070 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001071 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001072 E = N = new SDNode(Opcode, Operand);
1073 } else {
1074 N = new SDNode(Opcode, Operand);
1075 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 N->setValueTypes(VT);
1077 AllNodes.push_back(N);
1078 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001079}
1080
Chris Lattner36019aa2005-04-18 03:48:41 +00001081/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
1082/// this predicate to simplify operations downstream. V and Mask are known to
1083/// be the same type.
1084static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
1085 const TargetLowering &TLI) {
1086 unsigned SrcBits;
1087 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001088
Chris Lattner36019aa2005-04-18 03:48:41 +00001089 // If we know the result of a setcc has the top bits zero, use this info.
1090 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001091 case ISD::Constant:
1092 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1093
1094 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001095 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001096 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1097
1098 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001099 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001100 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1101 case ISD::ZERO_EXTEND:
1102 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1103 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
1104
1105 case ISD::AND:
1106 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001107 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001108 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1109
1110 // FALL THROUGH
1111 case ISD::OR:
1112 case ISD::XOR:
1113 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1114 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1115 case ISD::SELECT:
1116 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1117 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner1d6373c2005-08-24 16:46:55 +00001118 case ISD::SELECT_CC:
1119 return MaskedValueIsZero(Op.getOperand(2), Mask, TLI) &&
1120 MaskedValueIsZero(Op.getOperand(3), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001121 case ISD::SRL:
1122 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1123 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1124 uint64_t NewVal = Mask << ShAmt->getValue();
1125 SrcBits = MVT::getSizeInBits(Op.getValueType());
1126 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1127 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1128 }
1129 return false;
1130 case ISD::SHL:
1131 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1132 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1133 uint64_t NewVal = Mask >> ShAmt->getValue();
1134 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1135 }
1136 return false;
Chris Lattner1d6373c2005-08-24 16:46:55 +00001137 case ISD::CTTZ:
1138 case ISD::CTLZ:
1139 case ISD::CTPOP:
1140 // Bit counting instructions can not set the high bits of the result
1141 // register. The max number of bits sets depends on the input.
1142 return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
1143
Chris Lattner6ea92792005-04-25 21:03:25 +00001144 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001145 default: break;
1146 }
1147
1148 return false;
1149}
1150
1151
1152
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1154 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001155#ifndef NDEBUG
1156 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001157 case ISD::TokenFactor:
1158 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1159 N2.getValueType() == MVT::Other && "Invalid token factor!");
1160 break;
Chris Lattner76365122005-01-16 02:23:22 +00001161 case ISD::AND:
1162 case ISD::OR:
1163 case ISD::XOR:
1164 case ISD::UDIV:
1165 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001166 case ISD::MULHU:
1167 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001168 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1169 // fall through
1170 case ISD::ADD:
1171 case ISD::SUB:
1172 case ISD::MUL:
1173 case ISD::SDIV:
1174 case ISD::SREM:
1175 assert(N1.getValueType() == N2.getValueType() &&
1176 N1.getValueType() == VT && "Binary operator types must match!");
1177 break;
1178
1179 case ISD::SHL:
1180 case ISD::SRA:
1181 case ISD::SRL:
1182 assert(VT == N1.getValueType() &&
1183 "Shift operators return type must be the same as their first arg");
1184 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001185 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001186 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001187 case ISD::FP_ROUND_INREG: {
1188 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1189 assert(VT == N1.getValueType() && "Not an inreg round!");
1190 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1191 "Cannot FP_ROUND_INREG integer types");
1192 assert(EVT <= VT && "Not rounding down!");
1193 break;
1194 }
1195 case ISD::SIGN_EXTEND_INREG: {
1196 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1197 assert(VT == N1.getValueType() && "Not an inreg extend!");
1198 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1199 "Cannot *_EXTEND_INREG FP types");
1200 assert(EVT <= VT && "Not extending!");
1201 }
1202
Chris Lattner76365122005-01-16 02:23:22 +00001203 default: break;
1204 }
1205#endif
1206
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1208 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1209 if (N1C) {
1210 if (N2C) {
1211 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1212 switch (Opcode) {
1213 case ISD::ADD: return getConstant(C1 + C2, VT);
1214 case ISD::SUB: return getConstant(C1 - C2, VT);
1215 case ISD::MUL: return getConstant(C1 * C2, VT);
1216 case ISD::UDIV:
1217 if (C2) return getConstant(C1 / C2, VT);
1218 break;
1219 case ISD::UREM :
1220 if (C2) return getConstant(C1 % C2, VT);
1221 break;
1222 case ISD::SDIV :
1223 if (C2) return getConstant(N1C->getSignExtended() /
1224 N2C->getSignExtended(), VT);
1225 break;
1226 case ISD::SREM :
1227 if (C2) return getConstant(N1C->getSignExtended() %
1228 N2C->getSignExtended(), VT);
1229 break;
1230 case ISD::AND : return getConstant(C1 & C2, VT);
1231 case ISD::OR : return getConstant(C1 | C2, VT);
1232 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001233 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1234 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1235 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001236 default: break;
1237 }
1238
1239 } else { // Cannonicalize constant to RHS if commutative
1240 if (isCommutativeBinOp(Opcode)) {
1241 std::swap(N1C, N2C);
1242 std::swap(N1, N2);
1243 }
1244 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001245
1246 switch (Opcode) {
1247 default: break;
1248 case ISD::SHL: // shl 0, X -> 0
1249 if (N1C->isNullValue()) return N1;
1250 break;
1251 case ISD::SRL: // srl 0, X -> 0
1252 if (N1C->isNullValue()) return N1;
1253 break;
1254 case ISD::SRA: // sra -1, X -> -1
1255 if (N1C->isAllOnesValue()) return N1;
1256 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001257 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1258 // Extending a constant? Just return the extended constant.
1259 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1260 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001261 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001262 }
1263
1264 if (N2C) {
1265 uint64_t C2 = N2C->getValue();
1266
1267 switch (Opcode) {
1268 case ISD::ADD:
1269 if (!C2) return N1; // add X, 0 -> X
1270 break;
1271 case ISD::SUB:
1272 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001273 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001274 case ISD::MUL:
1275 if (!C2) return N2; // mul X, 0 -> 0
1276 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1277 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1278
Chris Lattnerb48da392005-01-23 04:39:44 +00001279 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001280 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001281 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001282 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001283 }
1284 break;
1285
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001286 case ISD::MULHU:
1287 case ISD::MULHS:
1288 if (!C2) return N2; // mul X, 0 -> 0
1289
1290 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1291 return getConstant(0, VT);
1292
1293 // Many others could be handled here, including -1, powers of 2, etc.
1294 break;
1295
Chris Lattnerc3aae252005-01-07 07:46:32 +00001296 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001297 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001298 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001299 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001300 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 }
1302 break;
1303
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001304 case ISD::SHL:
1305 case ISD::SRL:
1306 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001307 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001308 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001309 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1310 return getNode(ISD::UNDEF, N1.getValueType());
1311 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001312 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001313
1314 if (Opcode == ISD::SRA) {
1315 // If the sign bit is known to be zero, switch this to a SRL.
1316 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001317 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001318 TLI))
1319 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1320 } else {
1321 // If the part left over is known to be zero, the whole thing is zero.
1322 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1323 if (Opcode == ISD::SRL) {
1324 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1325 return getConstant(0, N1.getValueType());
1326 } else if (Opcode == ISD::SHL) {
1327 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1328 return getConstant(0, N1.getValueType());
1329 }
1330 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001331
1332 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1333 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1334 unsigned OpSAC = OpSA->getValue();
1335 if (N1.getOpcode() == ISD::SHL) {
1336 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1337 return getConstant(0, N1.getValueType());
1338 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1339 getConstant(C2+OpSAC, N2.getValueType()));
1340 } else if (N1.getOpcode() == ISD::SRL) {
1341 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1342 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1343 getConstant(~0ULL << OpSAC, VT));
1344 if (C2 > OpSAC) {
1345 return getNode(ISD::SHL, VT, Mask,
1346 getConstant(C2-OpSAC, N2.getValueType()));
1347 } else {
1348 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1349 return getNode(ISD::SRL, VT, Mask,
1350 getConstant(OpSAC-C2, N2.getValueType()));
1351 }
1352 } else if (N1.getOpcode() == ISD::SRA) {
1353 // if C1 == C2, just mask out low bits.
1354 if (C2 == OpSAC)
1355 return getNode(ISD::AND, VT, N1.getOperand(0),
1356 getConstant(~0ULL << C2, VT));
1357 }
1358 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001359 break;
1360
Chris Lattnerc3aae252005-01-07 07:46:32 +00001361 case ISD::AND:
1362 if (!C2) return N2; // X and 0 -> 0
1363 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001364 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001365
Chris Lattner36019aa2005-04-18 03:48:41 +00001366 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1367 return getConstant(0, VT);
1368
Chris Lattner588bbbf2005-04-21 06:28:15 +00001369 {
1370 uint64_t NotC2 = ~C2;
1371 if (VT != MVT::i64)
1372 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1373
1374 if (MaskedValueIsZero(N1, NotC2, TLI))
1375 return N1; // if (X & ~C2) -> 0, the and is redundant
1376 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001377
Chris Lattnerdea29e22005-04-10 01:13:15 +00001378 // FIXME: Should add a corresponding version of this for
1379 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1380 // we don't have yet.
1381
Chris Lattner0f2287b2005-04-13 02:38:18 +00001382 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1383 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001384 // If we are masking out the part of our input that was extended, just
1385 // mask the input to the extension directly.
1386 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001387 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001388 if ((C2 & (~0ULL << ExtendBits)) == 0)
1389 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001390 } else if (N1.getOpcode() == ISD::OR) {
1391 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1392 if ((ORI->getValue() & C2) == C2) {
1393 // If the 'or' is setting all of the bits that we are masking for,
1394 // we know the result of the AND will be the AND mask itself.
1395 return N2;
1396 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001397 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001398 break;
1399 case ISD::OR:
1400 if (!C2)return N1; // X or 0 -> X
1401 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001402 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001403 break;
1404 case ISD::XOR:
1405 if (!C2) return N1; // X xor 0 -> X
1406 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001407 if (N1.Val->getOpcode() == ISD::SETCC){
1408 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001409 // !(X op Y) -> (X !op Y)
1410 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001411 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1412 return getSetCC(SetCC->getValueType(0),
1413 SetCC->getOperand(0), SetCC->getOperand(1),
1414 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1416 SDNode *Op = N1.Val;
1417 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1418 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1419 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1420 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1421 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1422 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1423 if (Op->getOpcode() == ISD::AND)
1424 return getNode(ISD::OR, VT, LHS, RHS);
1425 return getNode(ISD::AND, VT, LHS, RHS);
1426 }
1427 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001428 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001429 }
1430 break;
1431 }
1432
1433 // Reassociate ((X op C1) op C2) if possible.
1434 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1435 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001436 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001437 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1438 }
1439
1440 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1441 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001442 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001443 if (N2CFP) {
1444 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1445 switch (Opcode) {
1446 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1447 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1448 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1449 case ISD::SDIV:
1450 if (C2) return getConstantFP(C1 / C2, VT);
1451 break;
1452 case ISD::SREM :
1453 if (C2) return getConstantFP(fmod(C1, C2), VT);
1454 break;
1455 default: break;
1456 }
1457
1458 } else { // Cannonicalize constant to RHS if commutative
1459 if (isCommutativeBinOp(Opcode)) {
1460 std::swap(N1CFP, N2CFP);
1461 std::swap(N1, N2);
1462 }
1463 }
1464
Chris Lattner15e4b012005-07-10 00:07:11 +00001465 if (Opcode == ISD::FP_ROUND_INREG)
1466 return getNode(ISD::FP_EXTEND, VT,
1467 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1468 }
1469
Chris Lattnerc3aae252005-01-07 07:46:32 +00001470 // Finally, fold operations that do not require constants.
1471 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001472 case ISD::TokenFactor:
1473 if (N1.getOpcode() == ISD::EntryToken)
1474 return N2;
1475 if (N2.getOpcode() == ISD::EntryToken)
1476 return N1;
1477 break;
1478
Chris Lattnerc3aae252005-01-07 07:46:32 +00001479 case ISD::AND:
1480 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001481 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1482 SDNode *LHS = N1.Val, *RHS = N2.Val;
1483 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1484 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1485 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1486 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001488 if (LR == RR && isa<ConstantSDNode>(LR) &&
1489 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1490 // (X != 0) | (Y != 0) -> (X|Y != 0)
1491 // (X == 0) & (Y == 0) -> (X|Y == 0)
1492 // (X < 0) | (Y < 0) -> (X|Y < 0)
1493 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1494 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1495 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1496 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1497 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1498 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001499
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001500 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1501 // (X == -1) & (Y == -1) -> (X&Y == -1)
1502 // (X != -1) | (Y != -1) -> (X&Y != -1)
1503 // (X > -1) | (Y > -1) -> (X&Y > -1)
1504 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1505 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1506 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1507 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1508 LR, Op2);
1509 // (X > -1) & (Y > -1) -> (X|Y > -1)
1510 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1511 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1512 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001513 }
1514 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001515
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001516 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1517 if (LL == RR && LR == RL) {
1518 Op2 = ISD::getSetCCSwappedOperands(Op2);
1519 goto MatchedBackwards;
1520 }
1521
1522 if (LL == RL && LR == RR) {
1523 MatchedBackwards:
1524 ISD::CondCode Result;
1525 bool isInteger = MVT::isInteger(LL.getValueType());
1526 if (Opcode == ISD::OR)
1527 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1528 else
1529 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1530
1531 if (Result != ISD::SETCC_INVALID)
1532 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1533 }
1534 }
1535
Chris Lattner7467c9b2005-04-18 04:11:19 +00001536 // and/or zext(a), zext(b) -> zext(and/or a, b)
1537 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1538 N2.getOpcode() == ISD::ZERO_EXTEND &&
1539 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1540 return getNode(ISD::ZERO_EXTEND, VT,
1541 getNode(Opcode, N1.getOperand(0).getValueType(),
1542 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001543 break;
1544 case ISD::XOR:
1545 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1546 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001547 case ISD::ADD:
1548 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1549 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1550 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1551 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001552 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1553 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1554 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1555 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1556 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1557 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001558 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1559 !MVT::isFloatingPoint(N2.getValueType()))
1560 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001561 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001562 case ISD::SUB:
1563 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001564 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001565 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001566 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001567 if (N1.Val->getOperand(1) == N2 &&
1568 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001569 return N1.Val->getOperand(0); // (A+B)-B == A
1570 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001571 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1572 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001573 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001574 case ISD::FP_ROUND_INREG:
1575 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1576 break;
1577 case ISD::SIGN_EXTEND_INREG: {
1578 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1579 if (EVT == VT) return N1; // Not actually extending
1580
1581 // If we are sign extending an extension, use the original source.
1582 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1583 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1584 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001585
Chris Lattner15e4b012005-07-10 00:07:11 +00001586 // If we are sign extending a sextload, return just the load.
1587 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001588 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001589 return N1;
1590
1591 // If we are extending the result of a setcc, and we already know the
1592 // contents of the top bits, eliminate the extension.
1593 if (N1.getOpcode() == ISD::SETCC &&
1594 TLI.getSetCCResultContents() ==
1595 TargetLowering::ZeroOrNegativeOneSetCCResult)
1596 return N1;
1597
1598 // If we are sign extending the result of an (and X, C) operation, and we
1599 // know the extended bits are zeros already, don't do the extend.
1600 if (N1.getOpcode() == ISD::AND)
1601 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1602 uint64_t Mask = N1C->getValue();
1603 unsigned NumBits = MVT::getSizeInBits(EVT);
1604 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1605 return N1;
1606 }
1607 break;
1608 }
1609
Nate Begemaneea805e2005-04-13 21:23:31 +00001610 // FIXME: figure out how to safely handle things like
1611 // int foo(int x) { return 1 << (x & 255); }
1612 // int bar() { return foo(256); }
1613#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001614 case ISD::SHL:
1615 case ISD::SRL:
1616 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001617 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001618 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001619 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001620 else if (N2.getOpcode() == ISD::AND)
1621 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1622 // If the and is only masking out bits that cannot effect the shift,
1623 // eliminate the and.
1624 unsigned NumBits = MVT::getSizeInBits(VT);
1625 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1626 return getNode(Opcode, VT, N1, N2.getOperand(0));
1627 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001628 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001629#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001630 }
1631
Chris Lattner27e9b412005-05-11 18:57:39 +00001632 // Memoize this node if possible.
1633 SDNode *N;
Chris Lattner43247a12005-08-25 19:12:10 +00001634 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END &&
1635 VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001636 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1637 if (BON) return SDOperand(BON, 0);
1638
1639 BON = N = new SDNode(Opcode, N1, N2);
1640 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001641 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001642 }
1643
Chris Lattner3e011362005-05-14 07:45:46 +00001644 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001645 AllNodes.push_back(N);
1646 return SDOperand(N, 0);
1647}
1648
Chris Lattner88de6e72005-05-12 00:17:04 +00001649// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001650// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001651void SDNode::setAdjCallChain(SDOperand N) {
1652 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001653 assert((getOpcode() == ISD::CALLSEQ_START ||
1654 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001655
1656 Operands[0].Val->removeUser(this);
1657 Operands[0] = N;
1658 N.Val->Uses.push_back(this);
1659}
1660
1661
1662
Chris Lattnerc3aae252005-01-07 07:46:32 +00001663SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001664 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001665 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001666 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1667 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001668 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001669
1670 // Loads have a token chain.
1671 N->setValueTypes(VT, MVT::Other);
1672 AllNodes.push_back(N);
1673 return SDOperand(N, 0);
1674}
1675
Chris Lattner5f056bf2005-07-10 01:55:33 +00001676
1677SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1678 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1679 MVT::ValueType EVT) {
1680 std::vector<SDOperand> Ops;
1681 Ops.reserve(4);
1682 Ops.push_back(Chain);
1683 Ops.push_back(Ptr);
1684 Ops.push_back(SV);
1685 Ops.push_back(getValueType(EVT));
1686 std::vector<MVT::ValueType> VTs;
1687 VTs.reserve(2);
1688 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1689 return getNode(Opcode, VTs, Ops);
1690}
1691
Chris Lattnerc3aae252005-01-07 07:46:32 +00001692SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1693 SDOperand N1, SDOperand N2, SDOperand N3) {
1694 // Perform various simplifications.
1695 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1696 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1697 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1698 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001699 case ISD::SETCC: {
1700 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001701 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001702 if (Simp.Val) return Simp;
1703 break;
1704 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001705 case ISD::SELECT:
1706 if (N1C)
1707 if (N1C->getValue())
1708 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001709 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001710 return N3; // select false, X, Y -> Y
1711
1712 if (N2 == N3) return N2; // select C, X, X -> X
1713
1714 if (VT == MVT::i1) { // Boolean SELECT
1715 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001716 if (N2C->getValue()) // select C, 1, X -> C | X
1717 return getNode(ISD::OR, VT, N1, N3);
1718 else // select C, 0, X -> ~C & X
1719 return getNode(ISD::AND, VT,
1720 getNode(ISD::XOR, N1.getValueType(), N1,
1721 getConstant(1, N1.getValueType())), N3);
1722 } else if (N3C) {
1723 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1724 return getNode(ISD::OR, VT,
1725 getNode(ISD::XOR, N1.getValueType(), N1,
1726 getConstant(1, N1.getValueType())), N2);
1727 else // select C, X, 0 -> C & X
1728 return getNode(ISD::AND, VT, N1, N2);
1729 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001730
1731 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1732 return getNode(ISD::OR, VT, N1, N3);
1733 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1734 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001735 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001736 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001737 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1738 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001739 if (Simp.Val) return Simp;
1740 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001741 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001742 case ISD::BRCOND:
1743 if (N2C)
1744 if (N2C->getValue()) // Unconditional branch
1745 return getNode(ISD::BR, MVT::Other, N1, N3);
1746 else
1747 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001748 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001749 }
1750
Chris Lattner385328c2005-05-14 07:42:29 +00001751 std::vector<SDOperand> Ops;
1752 Ops.reserve(3);
1753 Ops.push_back(N1);
1754 Ops.push_back(N2);
1755 Ops.push_back(N3);
1756
Chris Lattner43247a12005-08-25 19:12:10 +00001757 // Memoize node if it doesn't produce a flag.
1758 SDNode *N;
1759 if (VT != MVT::Flag) {
1760 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1761 if (E) return SDOperand(E, 0);
1762 E = N = new SDNode(Opcode, N1, N2, N3);
1763 } else {
1764 N = new SDNode(Opcode, N1, N2, N3);
1765 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001766 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001767 AllNodes.push_back(N);
1768 return SDOperand(N, 0);
1769}
1770
1771SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001772 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001773 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001774 std::vector<SDOperand> Ops;
1775 Ops.reserve(4);
1776 Ops.push_back(N1);
1777 Ops.push_back(N2);
1778 Ops.push_back(N3);
1779 Ops.push_back(N4);
1780 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001781}
1782
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001783SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1784 SDOperand N1, SDOperand N2, SDOperand N3,
1785 SDOperand N4, SDOperand N5) {
1786 std::vector<SDOperand> Ops;
1787 Ops.reserve(5);
1788 Ops.push_back(N1);
1789 Ops.push_back(N2);
1790 Ops.push_back(N3);
1791 Ops.push_back(N4);
1792 Ops.push_back(N5);
1793 return getNode(Opcode, VT, Ops);
1794}
1795
1796
Chris Lattner0437cdd2005-05-09 04:14:13 +00001797SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001798 assert((!V || isa<PointerType>(V->getType())) &&
1799 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001800 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1801 if (N) return SDOperand(N, 0);
1802
1803 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001804 AllNodes.push_back(N);
1805 return SDOperand(N, 0);
1806}
1807
1808SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001809 std::vector<SDOperand> &Ops) {
1810 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001811 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001812 case 1: return getNode(Opcode, VT, Ops[0]);
1813 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1814 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001815 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001816 }
Chris Lattneref847df2005-04-09 03:27:28 +00001817
Chris Lattner89c34632005-05-14 06:20:26 +00001818 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001819 switch (Opcode) {
1820 default: break;
1821 case ISD::BRCONDTWOWAY:
1822 if (N1C)
1823 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001824 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001825 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001826 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001827 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001828 case ISD::BRTWOWAY_CC:
1829 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1830 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1831 "LHS and RHS of comparison must have same type!");
1832 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001833 case ISD::TRUNCSTORE: {
1834 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1835 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1836#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1837 // If this is a truncating store of a constant, convert to the desired type
1838 // and store it instead.
1839 if (isa<Constant>(Ops[0])) {
1840 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1841 if (isa<Constant>(Op))
1842 N1 = Op;
1843 }
1844 // Also for ConstantFP?
1845#endif
1846 if (Ops[0].getValueType() == EVT) // Normal store?
1847 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1848 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1849 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1850 "Can't do FP-INT conversion!");
1851 break;
1852 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001853 case ISD::SELECT_CC: {
1854 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1855 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1856 "LHS and RHS of condition must have same type!");
1857 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1858 "True and False arms of SelectCC must have same type!");
1859 assert(Ops[2].getValueType() == VT &&
1860 "select_cc node must be of same type as true and false value!");
1861 SDOperand Simp = SimplifySelectCC(Ops[0], Ops[1], Ops[2], Ops[3],
1862 cast<CondCodeSDNode>(Ops[4])->get());
1863 if (Simp.Val) return Simp;
1864 break;
1865 }
1866 case ISD::BR_CC: {
1867 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1868 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1869 "LHS/RHS of comparison should match types!");
1870 // Use SimplifySetCC to simplify SETCC's.
1871 SDOperand Simp = SimplifySetCC(MVT::i1, Ops[2], Ops[3],
1872 cast<CondCodeSDNode>(Ops[1])->get());
1873 if (Simp.Val) {
1874 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Simp)) {
1875 if (C->getValue() & 1) // Unconditional branch
1876 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[4]);
1877 else
1878 return Ops[0]; // Unconditional Fall through
1879 } else if (Simp.Val->getOpcode() == ISD::SETCC) {
1880 Ops[2] = Simp.getOperand(0);
1881 Ops[3] = Simp.getOperand(1);
1882 Ops[1] = Simp.getOperand(2);
1883 }
1884 }
1885 break;
1886 }
Chris Lattneref847df2005-04-09 03:27:28 +00001887 }
1888
Chris Lattner385328c2005-05-14 07:42:29 +00001889 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001890 SDNode *N;
1891 if (VT != MVT::Flag) {
1892 SDNode *&E =
1893 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1894 if (E) return SDOperand(E, 0);
1895 E = N = new SDNode(Opcode, Ops);
1896 } else {
1897 N = new SDNode(Opcode, Ops);
1898 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001899 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001900 AllNodes.push_back(N);
1901 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001902}
1903
Chris Lattner89c34632005-05-14 06:20:26 +00001904SDOperand SelectionDAG::getNode(unsigned Opcode,
1905 std::vector<MVT::ValueType> &ResultTys,
1906 std::vector<SDOperand> &Ops) {
1907 if (ResultTys.size() == 1)
1908 return getNode(Opcode, ResultTys[0], Ops);
1909
Chris Lattner5f056bf2005-07-10 01:55:33 +00001910 switch (Opcode) {
1911 case ISD::EXTLOAD:
1912 case ISD::SEXTLOAD:
1913 case ISD::ZEXTLOAD: {
1914 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1915 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1916 // If they are asking for an extending load from/to the same thing, return a
1917 // normal load.
1918 if (ResultTys[0] == EVT)
1919 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1920 assert(EVT < ResultTys[0] &&
1921 "Should only be an extending load, not truncating!");
1922 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1923 "Cannot sign/zero extend a FP load!");
1924 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1925 "Cannot convert from FP to Int or Int -> FP!");
1926 break;
1927 }
1928
Chris Lattnere89083a2005-05-14 07:25:05 +00001929 // FIXME: figure out how to safely handle things like
1930 // int foo(int x) { return 1 << (x & 255); }
1931 // int bar() { return foo(256); }
1932#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001933 case ISD::SRA_PARTS:
1934 case ISD::SRL_PARTS:
1935 case ISD::SHL_PARTS:
1936 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001937 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001938 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1939 else if (N3.getOpcode() == ISD::AND)
1940 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1941 // If the and is only masking out bits that cannot effect the shift,
1942 // eliminate the and.
1943 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1944 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1945 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1946 }
1947 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001948#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001949 }
Chris Lattner89c34632005-05-14 06:20:26 +00001950
Chris Lattner43247a12005-08-25 19:12:10 +00001951 // Memoize the node unless it returns a flag.
1952 SDNode *N;
1953 if (ResultTys.back() != MVT::Flag) {
1954 SDNode *&E =
1955 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1956 if (E) return SDOperand(E, 0);
1957 E = N = new SDNode(Opcode, Ops);
1958 } else {
1959 N = new SDNode(Opcode, Ops);
1960 }
Chris Lattner89c34632005-05-14 06:20:26 +00001961 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001962 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001963 return SDOperand(N, 0);
1964}
1965
Chris Lattner149c58c2005-08-16 18:17:10 +00001966
1967/// SelectNodeTo - These are used for target selectors to *mutate* the
1968/// specified node to have the specified return type, Target opcode, and
1969/// operands. Note that target opcodes are stored as
1970/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001971void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1972 MVT::ValueType VT) {
Chris Lattner7651fa42005-08-24 23:00:29 +00001973 RemoveNodeFromCSEMaps(N);
1974 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1975 N->setValueTypes(VT);
1976}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001977void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1978 MVT::ValueType VT, SDOperand Op1) {
Chris Lattner149c58c2005-08-16 18:17:10 +00001979 RemoveNodeFromCSEMaps(N);
1980 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1981 N->setValueTypes(VT);
1982 N->setOperands(Op1);
1983}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001984void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1985 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00001986 SDOperand Op2) {
1987 RemoveNodeFromCSEMaps(N);
1988 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1989 N->setValueTypes(VT);
1990 N->setOperands(Op1, Op2);
1991}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001992void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
Chris Lattner99badda2005-08-21 19:48:59 +00001993 MVT::ValueType VT1, MVT::ValueType VT2,
Chris Lattner2bb06cd2005-08-26 16:36:26 +00001994 SDOperand Op1, SDOperand Op2) {
Chris Lattner99badda2005-08-21 19:48:59 +00001995 RemoveNodeFromCSEMaps(N);
1996 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1997 N->setValueTypes(VT1, VT2);
1998 N->setOperands(Op1, Op2);
1999}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002000void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2001 MVT::ValueType VT, SDOperand Op1,
Chris Lattner149c58c2005-08-16 18:17:10 +00002002 SDOperand Op2, SDOperand Op3) {
2003 RemoveNodeFromCSEMaps(N);
2004 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2005 N->setValueTypes(VT);
2006 N->setOperands(Op1, Op2, Op3);
2007}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002008void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2009 MVT::ValueType VT1, MVT::ValueType VT2,
2010 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002011 RemoveNodeFromCSEMaps(N);
2012 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2013 N->setValueTypes(VT1, VT2);
2014 N->setOperands(Op1, Op2, Op3);
2015}
2016
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002017void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2018 MVT::ValueType VT, SDOperand Op1,
Nate Begeman294a0a12005-08-18 07:30:15 +00002019 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
2020 RemoveNodeFromCSEMaps(N);
2021 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2022 N->setValueTypes(VT);
2023 N->setOperands(Op1, Op2, Op3, Op4);
2024}
Chris Lattner2bb06cd2005-08-26 16:36:26 +00002025void SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2026 MVT::ValueType VT, SDOperand Op1,
Chris Lattner6b09a292005-08-21 18:49:33 +00002027 SDOperand Op2, SDOperand Op3, SDOperand Op4,
2028 SDOperand Op5) {
2029 RemoveNodeFromCSEMaps(N);
2030 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2031 N->setValueTypes(VT);
2032 N->setOperands(Op1, Op2, Op3, Op4, Op5);
2033}
Chris Lattner149c58c2005-08-16 18:17:10 +00002034
Chris Lattner8b8749f2005-08-17 19:00:20 +00002035/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2036/// This can cause recursive merging of nodes in the DAG.
2037///
Chris Lattner8b52f212005-08-26 18:36:28 +00002038/// This version assumes From/To have a single result value.
2039///
2040void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN) {
2041 SDNode *From = FromN.Val, *To = ToN.Val;
2042 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2043 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002044 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002045
Chris Lattner8b8749f2005-08-17 19:00:20 +00002046 while (!From->use_empty()) {
2047 // Process users until they are all gone.
2048 SDNode *U = *From->use_begin();
2049
2050 // This node is about to morph, remove its old self from the CSE maps.
2051 RemoveNodeFromCSEMaps(U);
2052
2053 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2054 if (U->getOperand(i).Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002055 From->removeUser(U);
2056 U->Operands[i].Val = To;
2057 To->addUser(U);
2058 }
2059
2060 // Now that we have modified U, add it back to the CSE maps. If it already
2061 // exists there, recursively merge the results together.
2062 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2063 ReplaceAllUsesWith(U, Existing);
2064 // U is now dead.
2065 }
2066}
2067
2068/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2069/// This can cause recursive merging of nodes in the DAG.
2070///
2071/// This version assumes From/To have matching types and numbers of result
2072/// values.
2073///
2074void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
2075 assert(From != To && "Cannot replace uses of with self");
2076 assert(From->getNumValues() == To->getNumValues() &&
2077 "Cannot use this version of ReplaceAllUsesWith!");
2078 if (From->getNumValues() == 1) { // If possible, use the faster version.
2079 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0));
2080 return;
2081 }
2082
2083 while (!From->use_empty()) {
2084 // Process users until they are all gone.
2085 SDNode *U = *From->use_begin();
2086
2087 // This node is about to morph, remove its old self from the CSE maps.
2088 RemoveNodeFromCSEMaps(U);
2089
2090 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2091 if (U->getOperand(i).Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002092 From->removeUser(U);
2093 U->Operands[i].Val = To;
2094 To->addUser(U);
2095 }
2096
2097 // Now that we have modified U, add it back to the CSE maps. If it already
2098 // exists there, recursively merge the results together.
2099 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2100 ReplaceAllUsesWith(U, Existing);
Chris Lattner8b52f212005-08-26 18:36:28 +00002101 // U is now dead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002102 }
2103}
2104
Chris Lattner8b52f212005-08-26 18:36:28 +00002105/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2106/// This can cause recursive merging of nodes in the DAG.
2107///
2108/// This version can replace From with any result values. To must match the
2109/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002110void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
2111 const std::vector<SDOperand> &To) {
2112 assert(From->getNumValues() == To.size() &&
2113 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002114 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002115 // Degenerate case handled above.
Chris Lattner8b52f212005-08-26 18:36:28 +00002116 ReplaceAllUsesWith(SDOperand(From, 0), To[0]);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002117 return;
2118 }
2119
2120 while (!From->use_empty()) {
2121 // Process users until they are all gone.
2122 SDNode *U = *From->use_begin();
2123
2124 // This node is about to morph, remove its old self from the CSE maps.
2125 RemoveNodeFromCSEMaps(U);
2126
2127 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
2128 if (U->getOperand(i).Val == From) {
2129 const SDOperand &ToOp = To[U->getOperand(i).ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002130 From->removeUser(U);
2131 U->Operands[i] = ToOp;
2132 ToOp.Val->addUser(U);
2133 }
2134
2135 // Now that we have modified U, add it back to the CSE maps. If it already
2136 // exists there, recursively merge the results together.
2137 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
2138 ReplaceAllUsesWith(U, Existing);
2139 // U is now dead.
2140 }
2141}
2142
2143
Jim Laskey58b968b2005-08-17 20:08:02 +00002144//===----------------------------------------------------------------------===//
2145// SDNode Class
2146//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002147
Chris Lattner5c884562005-01-12 18:37:47 +00002148/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2149/// indicated value. This method ignores uses of other values defined by this
2150/// operation.
2151bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
2152 assert(Value < getNumValues() && "Bad value!");
2153
2154 // If there is only one value, this is easy.
2155 if (getNumValues() == 1)
2156 return use_size() == NUses;
2157 if (Uses.size() < NUses) return false;
2158
2159 SDOperand TheValue(this, Value);
2160
2161 std::set<SDNode*> UsersHandled;
2162
2163 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
2164 UI != E; ++UI) {
2165 SDNode *User = *UI;
2166 if (User->getNumOperands() == 1 ||
2167 UsersHandled.insert(User).second) // First time we've seen this?
2168 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2169 if (User->getOperand(i) == TheValue) {
2170 if (NUses == 0)
2171 return false; // too many uses
2172 --NUses;
2173 }
2174 }
2175
2176 // Found exactly the right number of uses?
2177 return NUses == 0;
2178}
2179
2180
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002181const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002182 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002183 default:
2184 if (getOpcode() < ISD::BUILTIN_OP_END)
2185 return "<<Unknown DAG Node>>";
2186 else {
2187 if (G)
2188 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
2189 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
2190 return "<<Unknown Target Node>>";
2191 }
2192
Andrew Lenharth95762122005-03-31 21:24:06 +00002193 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002194 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00002195 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002196 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002197 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002198 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00002199 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002200 case ISD::ConstantFP: return "ConstantFP";
2201 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00002202 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002203 case ISD::FrameIndex: return "FrameIndex";
Chris Lattnerafb2dd42005-08-25 00:43:01 +00002204 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002205 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002206 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002207 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner5839bf22005-08-26 17:15:30 +00002208 case ISD::ConstantPool: return "ConstantPool";
2209 case ISD::TargetConstantPool: return "TargetConstantPool";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002210 case ISD::CopyToReg: return "CopyToReg";
2211 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00002212 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002213 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00002214
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002215 // Unary operators
2216 case ISD::FABS: return "fabs";
2217 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002218 case ISD::FSQRT: return "fsqrt";
2219 case ISD::FSIN: return "fsin";
2220 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002221
2222 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002223 case ISD::ADD: return "add";
2224 case ISD::SUB: return "sub";
2225 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002226 case ISD::MULHU: return "mulhu";
2227 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002228 case ISD::SDIV: return "sdiv";
2229 case ISD::UDIV: return "udiv";
2230 case ISD::SREM: return "srem";
2231 case ISD::UREM: return "urem";
2232 case ISD::AND: return "and";
2233 case ISD::OR: return "or";
2234 case ISD::XOR: return "xor";
2235 case ISD::SHL: return "shl";
2236 case ISD::SRA: return "sra";
2237 case ISD::SRL: return "srl";
2238
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002239 case ISD::SETCC: return "setcc";
2240 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002241 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002242 case ISD::ADD_PARTS: return "add_parts";
2243 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002244 case ISD::SHL_PARTS: return "shl_parts";
2245 case ISD::SRA_PARTS: return "sra_parts";
2246 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002247
Chris Lattner7f644642005-04-28 21:44:03 +00002248 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002249 case ISD::SIGN_EXTEND: return "sign_extend";
2250 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002251 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002252 case ISD::TRUNCATE: return "truncate";
2253 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002254 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002255 case ISD::FP_EXTEND: return "fp_extend";
2256
2257 case ISD::SINT_TO_FP: return "sint_to_fp";
2258 case ISD::UINT_TO_FP: return "uint_to_fp";
2259 case ISD::FP_TO_SINT: return "fp_to_sint";
2260 case ISD::FP_TO_UINT: return "fp_to_uint";
2261
2262 // Control flow instructions
2263 case ISD::BR: return "br";
2264 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002265 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002266 case ISD::BR_CC: return "br_cc";
2267 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002268 case ISD::RET: return "ret";
2269 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002270 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002271 case ISD::CALLSEQ_START: return "callseq_start";
2272 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002273
2274 // Other operators
2275 case ISD::LOAD: return "load";
2276 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002277 case ISD::EXTLOAD: return "extload";
2278 case ISD::SEXTLOAD: return "sextload";
2279 case ISD::ZEXTLOAD: return "zextload";
2280 case ISD::TRUNCSTORE: return "truncstore";
2281
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002282 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2283 case ISD::EXTRACT_ELEMENT: return "extract_element";
2284 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002285 case ISD::MEMSET: return "memset";
2286 case ISD::MEMCPY: return "memcpy";
2287 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002288
Chris Lattner276260b2005-05-11 04:50:30 +00002289 // Bit counting
2290 case ISD::CTPOP: return "ctpop";
2291 case ISD::CTTZ: return "cttz";
2292 case ISD::CTLZ: return "ctlz";
2293
2294 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002295 case ISD::READPORT: return "readport";
2296 case ISD::WRITEPORT: return "writeport";
2297 case ISD::READIO: return "readio";
2298 case ISD::WRITEIO: return "writeio";
2299
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002300 case ISD::CONDCODE:
2301 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002302 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002303 case ISD::SETOEQ: return "setoeq";
2304 case ISD::SETOGT: return "setogt";
2305 case ISD::SETOGE: return "setoge";
2306 case ISD::SETOLT: return "setolt";
2307 case ISD::SETOLE: return "setole";
2308 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002309
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002310 case ISD::SETO: return "seto";
2311 case ISD::SETUO: return "setuo";
2312 case ISD::SETUEQ: return "setue";
2313 case ISD::SETUGT: return "setugt";
2314 case ISD::SETUGE: return "setuge";
2315 case ISD::SETULT: return "setult";
2316 case ISD::SETULE: return "setule";
2317 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002318
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002319 case ISD::SETEQ: return "seteq";
2320 case ISD::SETGT: return "setgt";
2321 case ISD::SETGE: return "setge";
2322 case ISD::SETLT: return "setlt";
2323 case ISD::SETLE: return "setle";
2324 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002325 }
2326 }
2327}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002328
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002329void SDNode::dump() const { dump(0); }
2330void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002331 std::cerr << (void*)this << ": ";
2332
2333 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2334 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002335 if (getValueType(i) == MVT::Other)
2336 std::cerr << "ch";
2337 else
2338 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002339 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002340 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002341
2342 std::cerr << " ";
2343 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2344 if (i) std::cerr << ", ";
2345 std::cerr << (void*)getOperand(i).Val;
2346 if (unsigned RN = getOperand(i).ResNo)
2347 std::cerr << ":" << RN;
2348 }
2349
2350 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2351 std::cerr << "<" << CSDN->getValue() << ">";
2352 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2353 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002354 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002355 dyn_cast<GlobalAddressSDNode>(this)) {
2356 std::cerr << "<";
2357 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002358 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002359 std::cerr << "<" << FIDN->getIndex() << ">";
2360 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Chris Lattner5839bf22005-08-26 17:15:30 +00002361 std::cerr << "<" << *CP->get() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002362 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002363 std::cerr << "<";
2364 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2365 if (LBB)
2366 std::cerr << LBB->getName() << " ";
2367 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002368 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002369 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2370 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2371 } else {
2372 std::cerr << " #" << R->getReg();
2373 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002374 } else if (const ExternalSymbolSDNode *ES =
2375 dyn_cast<ExternalSymbolSDNode>(this)) {
2376 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002377 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2378 if (M->getValue())
2379 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2380 else
2381 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002382 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2383 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002384 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002385}
2386
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002387static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002388 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2389 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002390 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002391 else
2392 std::cerr << "\n" << std::string(indent+2, ' ')
2393 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002394
Chris Lattnerea946cd2005-01-09 20:38:33 +00002395
2396 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002397 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002398}
2399
Chris Lattnerc3aae252005-01-07 07:46:32 +00002400void SelectionDAG::dump() const {
2401 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002402 std::vector<SDNode*> Nodes(AllNodes);
2403 std::sort(Nodes.begin(), Nodes.end());
2404
2405 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002406 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002407 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002408 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002409
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002410 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002411
Chris Lattnerc3aae252005-01-07 07:46:32 +00002412 std::cerr << "\n\n";
2413}
2414