blob: 3b106d192d13c45f34b60c9f399f85361b87b5e2 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Reid Spencer954da372004-07-04 12:19:56 +000019#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000020#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000021#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000022#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner5cdcc582005-01-09 20:52:51 +000025static bool isCommutativeBinOp(unsigned Opcode) {
26 switch (Opcode) {
27 case ISD::ADD:
28 case ISD::MUL:
29 case ISD::AND:
30 case ISD::OR:
31 case ISD::XOR: return true;
32 default: return false; // FIXME: Need commutative info for user ops!
33 }
34}
35
36static bool isAssociativeBinOp(unsigned Opcode) {
37 switch (Opcode) {
38 case ISD::ADD:
39 case ISD::MUL:
40 case ISD::AND:
41 case ISD::OR:
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need associative info for user ops!
44 }
45}
46
47static unsigned ExactLog2(uint64_t Val) {
48 unsigned Count = 0;
49 while (Val != 1) {
50 Val >>= 1;
51 ++Count;
52 }
53 return Count;
54}
55
56// isInvertibleForFree - Return true if there is no cost to emitting the logical
57// inverse of this node.
58static bool isInvertibleForFree(SDOperand N) {
59 if (isa<ConstantSDNode>(N.Val)) return true;
60 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
61 return true;
62 return false;
63}
64
65
Chris Lattnerc3aae252005-01-07 07:46:32 +000066/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
67/// when given the operation for (X op Y).
68ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
69 // To perform this operation, we just need to swap the L and G bits of the
70 // operation.
71 unsigned OldL = (Operation >> 2) & 1;
72 unsigned OldG = (Operation >> 1) & 1;
73 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
74 (OldL << 1) | // New G bit
75 (OldG << 2)); // New L bit.
76}
77
78/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
79/// 'op' is a valid SetCC operation.
80ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
81 unsigned Operation = Op;
82 if (isInteger)
83 Operation ^= 7; // Flip L, G, E bits, but not U.
84 else
85 Operation ^= 15; // Flip all of the condition bits.
86 if (Operation > ISD::SETTRUE2)
87 Operation &= ~8; // Don't let N and U bits get set.
88 return ISD::CondCode(Operation);
89}
90
91
92/// isSignedOp - For an integer comparison, return 1 if the comparison is a
93/// signed operation and 2 if the result is an unsigned comparison. Return zero
94/// if the operation does not depend on the sign of the input (setne and seteq).
95static int isSignedOp(ISD::CondCode Opcode) {
96 switch (Opcode) {
97 default: assert(0 && "Illegal integer setcc operation!");
98 case ISD::SETEQ:
99 case ISD::SETNE: return 0;
100 case ISD::SETLT:
101 case ISD::SETLE:
102 case ISD::SETGT:
103 case ISD::SETGE: return 1;
104 case ISD::SETULT:
105 case ISD::SETULE:
106 case ISD::SETUGT:
107 case ISD::SETUGE: return 2;
108 }
109}
110
111/// getSetCCOrOperation - Return the result of a logical OR between different
112/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
113/// returns SETCC_INVALID if it is not possible to represent the resultant
114/// comparison.
115ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
116 bool isInteger) {
117 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
118 // Cannot fold a signed integer setcc with an unsigned integer setcc.
119 return ISD::SETCC_INVALID;
120
121 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
122
123 // If the N and U bits get set then the resultant comparison DOES suddenly
124 // care about orderedness, and is true when ordered.
125 if (Op > ISD::SETTRUE2)
126 Op &= ~16; // Clear the N bit.
127 return ISD::CondCode(Op);
128}
129
130/// getSetCCAndOperation - Return the result of a logical AND between different
131/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
132/// function returns zero if it is not possible to represent the resultant
133/// comparison.
134ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
135 bool isInteger) {
136 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
137 // Cannot fold a signed setcc with an unsigned setcc.
138 return ISD::SETCC_INVALID;
139
140 // Combine all of the condition bits.
141 return ISD::CondCode(Op1 & Op2);
142}
143
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000144/// RemoveDeadNodes - This method deletes all unreachable nodes in the
145/// SelectionDAG, including nodes (like loads) that have uses of their token
146/// chain but no other uses and no side effect. If a node is passed in as an
147/// argument, it is used as the seed for node deletion.
148void SelectionDAG::RemoveDeadNodes(SDNode *N) {
149 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
150
151 // Create a dummy node (which is not added to allnodes), that adds a reference
152 // to the root node, preventing it from being deleted.
153 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
154
155 DeleteNodeIfDead(N, &AllNodeSet);
156
157 Restart:
158 unsigned NumNodes = AllNodeSet.size();
159 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
160 I != E; ++I) {
161 // Try to delete this node.
162 DeleteNodeIfDead(*I, &AllNodeSet);
163
164 // If we actually deleted any nodes, do not use invalid iterators in
165 // AllNodeSet.
166 if (AllNodeSet.size() != NumNodes)
167 goto Restart;
168 }
169
170 // Restore AllNodes.
171 if (AllNodes.size() != NumNodes)
172 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
173
174 // If the root changed (e.g. it was a dead load, update the root).
175 setRoot(DummyNode->getOperand(0));
176
177 // Now that we are done with the dummy node, delete it.
178 DummyNode->getOperand(0).Val->removeUser(DummyNode);
179 delete DummyNode;
180}
181
182void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
183 if (!N->use_empty())
184 return;
185
186 // Okay, we really are going to delete this node. First take this out of the
187 // appropriate CSE map.
188 switch (N->getOpcode()) {
189 case ISD::Constant:
190 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
191 N->getValueType(0)));
192 break;
193 case ISD::ConstantFP:
194 ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
195 N->getValueType(0)));
196 break;
197 case ISD::GlobalAddress:
198 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
199 break;
200 case ISD::FrameIndex:
201 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
202 break;
203 case ISD::ConstantPool:
204 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
205 break;
206 case ISD::BasicBlock:
207 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
208 break;
209 case ISD::ExternalSymbol:
210 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
211 break;
212
213 case ISD::LOAD:
214 Loads.erase(std::make_pair(N->getOperand(1),
215 std::make_pair(N->getOperand(0),
216 N->getValueType(0))));
217 break;
218 case ISD::SETCC:
219 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
220 N->getOperand(1)),
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000221 std::make_pair(
222 cast<SetCCSDNode>(N)->getCondition(),
223 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000224 break;
Chris Lattner859157d2005-01-15 06:17:04 +0000225 case ISD::TRUNCSTORE:
226 case ISD::SIGN_EXTEND_INREG:
227 case ISD::ZERO_EXTEND_INREG:
228 case ISD::FP_ROUND_INREG:
Chris Lattner69a52152005-01-14 22:38:01 +0000229 case ISD::EXTLOAD:
230 case ISD::SEXTLOAD:
231 case ISD::ZEXTLOAD: {
232 EVTStruct NN;
Chris Lattner859157d2005-01-15 06:17:04 +0000233 NN.Opcode = ISD::TRUNCSTORE;
Chris Lattner69a52152005-01-14 22:38:01 +0000234 NN.VT = N->getValueType(0);
235 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner859157d2005-01-15 06:17:04 +0000236 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
237 NN.Ops.push_back(N->getOperand(i));
Chris Lattner69a52152005-01-14 22:38:01 +0000238 MVTSDNodes.erase(NN);
239 break;
240 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000241 default:
242 if (N->getNumOperands() == 1)
243 UnaryOps.erase(std::make_pair(N->getOpcode(),
244 std::make_pair(N->getOperand(0),
245 N->getValueType(0))));
246 else if (N->getNumOperands() == 2)
247 BinaryOps.erase(std::make_pair(N->getOpcode(),
248 std::make_pair(N->getOperand(0),
249 N->getOperand(1))));
250 break;
251 }
252
253 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000255 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000256 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000257 O->removeUser(N);
258
259 // Now that we removed this operand, see if there are no uses of it left.
260 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 }
262
263 // Remove the node from the nodes set and delete it.
264 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
265 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266
267 // Now that the node is gone, check to see if any of the operands of this node
268 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000269 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000270}
271
272
Chris Lattner78ec3112003-08-11 14:57:33 +0000273SelectionDAG::~SelectionDAG() {
274 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
275 delete AllNodes[i];
276}
277
Chris Lattnerc3aae252005-01-07 07:46:32 +0000278SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
279 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
280 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000281 if (VT != MVT::i64)
282 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
283
Chris Lattnerc3aae252005-01-07 07:46:32 +0000284 SDNode *&N = Constants[std::make_pair(Val, VT)];
285 if (N) return SDOperand(N, 0);
286 N = new ConstantSDNode(Val, VT);
287 AllNodes.push_back(N);
288 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000289}
290
Chris Lattnerc3aae252005-01-07 07:46:32 +0000291SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
292 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
293 if (VT == MVT::f32)
294 Val = (float)Val; // Mask out extra precision.
295
296 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
297 if (N) return SDOperand(N, 0);
298 N = new ConstantFPSDNode(Val, VT);
299 AllNodes.push_back(N);
300 return SDOperand(N, 0);
301}
302
303
304
305SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
306 MVT::ValueType VT) {
307 SDNode *&N = GlobalValues[GV];
308 if (N) return SDOperand(N, 0);
309 N = new GlobalAddressSDNode(GV,VT);
310 AllNodes.push_back(N);
311 return SDOperand(N, 0);
312}
313
314SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
315 SDNode *&N = FrameIndices[FI];
316 if (N) return SDOperand(N, 0);
317 N = new FrameIndexSDNode(FI, VT);
318 AllNodes.push_back(N);
319 return SDOperand(N, 0);
320}
321
322SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
323 SDNode *N = ConstantPoolIndices[CPIdx];
324 if (N) return SDOperand(N, 0);
325 N = new ConstantPoolSDNode(CPIdx, VT);
326 AllNodes.push_back(N);
327 return SDOperand(N, 0);
328}
329
330SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
331 SDNode *&N = BBNodes[MBB];
332 if (N) return SDOperand(N, 0);
333 N = new BasicBlockSDNode(MBB);
334 AllNodes.push_back(N);
335 return SDOperand(N, 0);
336}
337
338SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
339 SDNode *&N = ExternalSymbols[Sym];
340 if (N) return SDOperand(N, 0);
341 N = new ExternalSymbolSDNode(Sym, VT);
342 AllNodes.push_back(N);
343 return SDOperand(N, 0);
344}
345
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000346SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
347 SDOperand N1, SDOperand N2) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000348 // These setcc operations always fold.
349 switch (Cond) {
350 default: break;
351 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000352 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000353 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000354 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000355 }
356
357 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
358 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
359 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
360
361 // Sign extend the operands if required
362 if (ISD::isSignedIntSetCC(Cond)) {
363 C1 = N1C->getSignExtended();
364 C2 = N2C->getSignExtended();
365 }
366
367 switch (Cond) {
368 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000369 case ISD::SETEQ: return getConstant(C1 == C2, VT);
370 case ISD::SETNE: return getConstant(C1 != C2, VT);
371 case ISD::SETULT: return getConstant(C1 < C2, VT);
372 case ISD::SETUGT: return getConstant(C1 > C2, VT);
373 case ISD::SETULE: return getConstant(C1 <= C2, VT);
374 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
375 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
376 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
377 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
378 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000379 }
380 } else {
381 // Ensure that the constant occurs on the RHS.
382 Cond = ISD::getSetCCSwappedOperands(Cond);
383 std::swap(N1, N2);
384 }
385
386 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
387 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
388 double C1 = N1C->getValue(), C2 = N2C->getValue();
389
390 switch (Cond) {
391 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000392 case ISD::SETEQ: return getConstant(C1 == C2, VT);
393 case ISD::SETNE: return getConstant(C1 != C2, VT);
394 case ISD::SETLT: return getConstant(C1 < C2, VT);
395 case ISD::SETGT: return getConstant(C1 > C2, VT);
396 case ISD::SETLE: return getConstant(C1 <= C2, VT);
397 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000398 }
399 } else {
400 // Ensure that the constant occurs on the RHS.
401 Cond = ISD::getSetCCSwappedOperands(Cond);
402 std::swap(N1, N2);
403 }
404
405 if (N1 == N2) {
406 // We can always fold X == Y for integer setcc's.
407 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000408 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000409 unsigned UOF = ISD::getUnorderedFlavor(Cond);
410 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000411 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000412 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000413 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000414 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
415 // if it is not already.
416 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
417 }
418
Chris Lattner5cdcc582005-01-09 20:52:51 +0000419 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000420 MVT::isInteger(N1.getValueType())) {
421 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
422 N1.getOpcode() == ISD::XOR) {
423 // Simplify (X+Y) == (X+Z) --> Y == Z
424 if (N1.getOpcode() == N2.getOpcode()) {
425 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000426 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000427 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000428 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000429 if (isCommutativeBinOp(N1.getOpcode())) {
430 // If X op Y == Y op X, try other combinations.
431 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000432 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000433 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000434 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000435 }
436 }
437
438 // Simplify (X+Z) == X --> Z == 0
439 if (N1.getOperand(0) == N2)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000440 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000441 getConstant(0, N1.getValueType()));
442 if (N1.getOperand(1) == N2) {
443 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000444 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000445 getConstant(0, N1.getValueType()));
446 else {
447 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
448 // (Z-X) == X --> Z == X<<1
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000449 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000450 getNode(ISD::SHL, N2.getValueType(),
451 N2, getConstant(1, MVT::i8)));
452 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000453 }
454 }
455
Chris Lattner68dc3102005-01-10 02:03:02 +0000456 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
457 N2.getOpcode() == ISD::XOR) {
458 // Simplify X == (X+Z) --> Z == 0
459 if (N2.getOperand(0) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000460 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000461 getConstant(0, N2.getValueType()));
462 else if (N2.getOperand(1) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000463 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000464 getConstant(0, N2.getValueType()));
465 }
466 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000467
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000468 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
469 std::make_pair(Cond, VT))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000470 if (N) return SDOperand(N, 0);
471 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000472 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000473 AllNodes.push_back(N);
474 return SDOperand(N, 0);
475}
476
477
478
479/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000480///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000481SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
482 SDNode *N = new SDNode(Opcode, VT);
483 AllNodes.push_back(N);
484 return SDOperand(N, 0);
485}
486
Chris Lattnerc3aae252005-01-07 07:46:32 +0000487SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
488 SDOperand Operand) {
489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
490 uint64_t Val = C->getValue();
491 switch (Opcode) {
492 default: break;
493 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
494 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
495 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000496 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
497 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000498 }
499 }
500
501 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
502 switch (Opcode) {
503 case ISD::FP_ROUND:
504 case ISD::FP_EXTEND:
505 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000506 case ISD::FP_TO_SINT:
507 return getConstant((int64_t)C->getValue(), VT);
508 case ISD::FP_TO_UINT:
509 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510 }
511
512 unsigned OpOpcode = Operand.Val->getOpcode();
513 switch (Opcode) {
514 case ISD::SIGN_EXTEND:
515 if (Operand.getValueType() == VT) return Operand; // noop extension
516 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
517 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
518 break;
519 case ISD::ZERO_EXTEND:
520 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner2f0ca792005-01-12 18:51:15 +0000521 if (OpOpcode == ISD::ZERO_EXTEND)
522 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000523 break;
524 case ISD::TRUNCATE:
525 if (Operand.getValueType() == VT) return Operand; // noop truncate
526 if (OpOpcode == ISD::TRUNCATE)
527 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000528 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
529 // If the source is smaller than the dest, we still need an extend.
530 if (Operand.Val->getOperand(0).getValueType() < VT)
531 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
532 else if (Operand.Val->getOperand(0).getValueType() > VT)
533 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
534 else
535 return Operand.Val->getOperand(0);
536 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000537 break;
538 }
539
540 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
541 if (N) return SDOperand(N, 0);
542 N = new SDNode(Opcode, Operand);
543 N->setValueTypes(VT);
544 AllNodes.push_back(N);
545 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000546}
547
Chris Lattnerc3aae252005-01-07 07:46:32 +0000548SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
549 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000550#ifndef NDEBUG
551 switch (Opcode) {
552 case ISD::AND:
553 case ISD::OR:
554 case ISD::XOR:
555 case ISD::UDIV:
556 case ISD::UREM:
557 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
558 // fall through
559 case ISD::ADD:
560 case ISD::SUB:
561 case ISD::MUL:
562 case ISD::SDIV:
563 case ISD::SREM:
564 assert(N1.getValueType() == N2.getValueType() &&
565 N1.getValueType() == VT && "Binary operator types must match!");
566 break;
567
568 case ISD::SHL:
569 case ISD::SRA:
570 case ISD::SRL:
571 assert(VT == N1.getValueType() &&
572 "Shift operators return type must be the same as their first arg");
573 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000574 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000575 break;
576 default: break;
577 }
578#endif
579
Chris Lattnerc3aae252005-01-07 07:46:32 +0000580 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
581 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
582 if (N1C) {
583 if (N2C) {
584 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
585 switch (Opcode) {
586 case ISD::ADD: return getConstant(C1 + C2, VT);
587 case ISD::SUB: return getConstant(C1 - C2, VT);
588 case ISD::MUL: return getConstant(C1 * C2, VT);
589 case ISD::UDIV:
590 if (C2) return getConstant(C1 / C2, VT);
591 break;
592 case ISD::UREM :
593 if (C2) return getConstant(C1 % C2, VT);
594 break;
595 case ISD::SDIV :
596 if (C2) return getConstant(N1C->getSignExtended() /
597 N2C->getSignExtended(), VT);
598 break;
599 case ISD::SREM :
600 if (C2) return getConstant(N1C->getSignExtended() %
601 N2C->getSignExtended(), VT);
602 break;
603 case ISD::AND : return getConstant(C1 & C2, VT);
604 case ISD::OR : return getConstant(C1 | C2, VT);
605 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000606 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
607 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
608 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000609 default: break;
610 }
611
612 } else { // Cannonicalize constant to RHS if commutative
613 if (isCommutativeBinOp(Opcode)) {
614 std::swap(N1C, N2C);
615 std::swap(N1, N2);
616 }
617 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000618
619 switch (Opcode) {
620 default: break;
621 case ISD::SHL: // shl 0, X -> 0
622 if (N1C->isNullValue()) return N1;
623 break;
624 case ISD::SRL: // srl 0, X -> 0
625 if (N1C->isNullValue()) return N1;
626 break;
627 case ISD::SRA: // sra -1, X -> -1
628 if (N1C->isAllOnesValue()) return N1;
629 break;
630 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000631 }
632
633 if (N2C) {
634 uint64_t C2 = N2C->getValue();
635
636 switch (Opcode) {
637 case ISD::ADD:
638 if (!C2) return N1; // add X, 0 -> X
639 break;
640 case ISD::SUB:
641 if (!C2) return N1; // sub X, 0 -> X
642 break;
643 case ISD::MUL:
644 if (!C2) return N2; // mul X, 0 -> 0
645 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
646 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
647
648 // FIXME: This should only be done if the target supports shift
649 // operations.
650 if ((C2 & C2-1) == 0) {
651 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
652 return getNode(ISD::SHL, VT, N1, ShAmt);
653 }
654 break;
655
656 case ISD::UDIV:
657 // FIXME: This should only be done if the target supports shift
658 // operations.
659 if ((C2 & C2-1) == 0 && C2) {
660 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
661 return getNode(ISD::SRL, VT, N1, ShAmt);
662 }
663 break;
664
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000665 case ISD::SHL:
666 case ISD::SRL:
667 case ISD::SRA:
668 if (C2 == 0) return N1;
669 break;
670
Chris Lattnerc3aae252005-01-07 07:46:32 +0000671 case ISD::AND:
672 if (!C2) return N2; // X and 0 -> 0
673 if (N2C->isAllOnesValue())
674 return N1; // X and -1 -> X
675 break;
676 case ISD::OR:
677 if (!C2)return N1; // X or 0 -> X
678 if (N2C->isAllOnesValue())
679 return N2; // X or -1 -> -1
680 break;
681 case ISD::XOR:
682 if (!C2) return N1; // X xor 0 -> X
683 if (N2C->isAllOnesValue()) {
684 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
685 // !(X op Y) -> (X !op Y)
686 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
687 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000688 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689 SetCC->getOperand(0), SetCC->getOperand(1));
690 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
691 SDNode *Op = N1.Val;
692 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
693 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
694 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
695 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
696 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
697 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
698 if (Op->getOpcode() == ISD::AND)
699 return getNode(ISD::OR, VT, LHS, RHS);
700 return getNode(ISD::AND, VT, LHS, RHS);
701 }
702 }
703 // X xor -1 -> not(x) ?
704 }
705 break;
706 }
707
708 // Reassociate ((X op C1) op C2) if possible.
709 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
710 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000711 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000712 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
713 }
714
715 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
716 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
717 if (N1CFP)
718 if (N2CFP) {
719 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
720 switch (Opcode) {
721 case ISD::ADD: return getConstantFP(C1 + C2, VT);
722 case ISD::SUB: return getConstantFP(C1 - C2, VT);
723 case ISD::MUL: return getConstantFP(C1 * C2, VT);
724 case ISD::SDIV:
725 if (C2) return getConstantFP(C1 / C2, VT);
726 break;
727 case ISD::SREM :
728 if (C2) return getConstantFP(fmod(C1, C2), VT);
729 break;
730 default: break;
731 }
732
733 } else { // Cannonicalize constant to RHS if commutative
734 if (isCommutativeBinOp(Opcode)) {
735 std::swap(N1CFP, N2CFP);
736 std::swap(N1, N2);
737 }
738 }
739
740 // Finally, fold operations that do not require constants.
741 switch (Opcode) {
742 case ISD::AND:
743 case ISD::OR:
744 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
745 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
746 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
747 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
748 ISD::CondCode Op2 = RHS->getCondition();
749
750 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
751 if (LL == RR && LR == RL) {
752 Op2 = ISD::getSetCCSwappedOperands(Op2);
753 goto MatchedBackwards;
754 }
755
756 if (LL == RL && LR == RR) {
757 MatchedBackwards:
758 ISD::CondCode Result;
759 bool isInteger = MVT::isInteger(LL.getValueType());
760 if (Opcode == ISD::OR)
761 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
762 isInteger);
763 else
764 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
765 isInteger);
766 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000767 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000768 }
769 }
770 break;
771 case ISD::XOR:
772 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
773 break;
Chris Lattnerabd21822005-01-09 20:09:57 +0000774 case ISD::SUB:
775 if (N1.getOpcode() == ISD::ADD) {
776 if (N1.Val->getOperand(0) == N2)
777 return N1.Val->getOperand(1); // (A+B)-A == B
778 if (N1.Val->getOperand(1) == N2)
779 return N1.Val->getOperand(0); // (A+B)-B == A
780 }
781 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000782 }
783
784 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
785 if (N) return SDOperand(N, 0);
786 N = new SDNode(Opcode, N1, N2);
787 N->setValueTypes(VT);
788
789 AllNodes.push_back(N);
790 return SDOperand(N, 0);
791}
792
793SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
794 SDOperand Chain, SDOperand Ptr) {
795 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
796 if (N) return SDOperand(N, 0);
797 N = new SDNode(ISD::LOAD, Chain, Ptr);
798
799 // Loads have a token chain.
800 N->setValueTypes(VT, MVT::Other);
801 AllNodes.push_back(N);
802 return SDOperand(N, 0);
803}
804
805
806SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
807 SDOperand N1, SDOperand N2, SDOperand N3) {
808 // Perform various simplifications.
809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
810 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
811 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
812 switch (Opcode) {
813 case ISD::SELECT:
814 if (N1C)
815 if (N1C->getValue())
816 return N2; // select true, X, Y -> X
817 else
818 return N3; // select false, X, Y -> Y
819
820 if (N2 == N3) return N2; // select C, X, X -> X
821
822 if (VT == MVT::i1) { // Boolean SELECT
823 if (N2C) {
824 if (N3C) {
825 if (N2C->getValue()) // select C, 1, 0 -> C
826 return N1;
827 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
828 }
829
830 if (N2C->getValue()) // select C, 1, X -> C | X
831 return getNode(ISD::OR, VT, N1, N3);
832 else // select C, 0, X -> ~C & X
833 return getNode(ISD::AND, VT,
834 getNode(ISD::XOR, N1.getValueType(), N1,
835 getConstant(1, N1.getValueType())), N3);
836 } else if (N3C) {
837 if (N3C->getValue()) // select C, X, 1 -> ~C | X
838 return getNode(ISD::OR, VT,
839 getNode(ISD::XOR, N1.getValueType(), N1,
840 getConstant(1, N1.getValueType())), N2);
841 else // select C, X, 0 -> C & X
842 return getNode(ISD::AND, VT, N1, N2);
843 }
844 }
845
846 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +0000847 case ISD::BRCOND:
848 if (N2C)
849 if (N2C->getValue()) // Unconditional branch
850 return getNode(ISD::BR, MVT::Other, N1, N3);
851 else
852 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +0000853 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000854 }
855
856 SDNode *N = new SDNode(Opcode, N1, N2, N3);
857 switch (Opcode) {
858 default:
859 N->setValueTypes(VT);
860 break;
861 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
862 N->setValueTypes(VT, MVT::Other);
863 break;
864 }
865
866 // FIXME: memoize NODES
867 AllNodes.push_back(N);
868 return SDOperand(N, 0);
869}
870
871SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
872 std::vector<SDOperand> &Children) {
873 switch (Children.size()) {
874 case 0: return getNode(Opcode, VT);
875 case 1: return getNode(Opcode, VT, Children[0]);
876 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
877 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
878 default:
879 // FIXME: MEMOIZE!!
880 SDNode *N = new SDNode(Opcode, Children);
881 N->setValueTypes(VT);
882 AllNodes.push_back(N);
883 return SDOperand(N, 0);
884 }
885}
886
Chris Lattner2ee743f2005-01-14 22:08:15 +0000887SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner859157d2005-01-15 06:17:04 +0000888 MVT::ValueType EVT) {
889
890 switch (Opcode) {
891 default: assert(0 && "Bad opcode for this accessor!");
892 case ISD::FP_ROUND_INREG:
893 assert(VT == N1.getValueType() && "Not an inreg round!");
894 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
895 "Cannot FP_ROUND_INREG integer types");
896 if (EVT == VT) return N1; // Not actually rounding
897 assert(EVT < VT && "Not rounding down!");
898 break;
899 case ISD::ZERO_EXTEND_INREG:
900 case ISD::SIGN_EXTEND_INREG:
901 assert(VT == N1.getValueType() && "Not an inreg extend!");
902 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
903 "Cannot *_EXTEND_INREG FP types");
904 if (EVT == VT) return N1; // Not actually extending
905 assert(EVT < VT && "Not extending!");
Chris Lattner950aa3c2005-01-16 00:17:20 +0000906
907 // If we are sign extending an extension, use the original source.
908 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
909 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
910 if (N1.getOpcode() == Opcode &&
911 cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
912 return N1;
913 }
914
Chris Lattner859157d2005-01-15 06:17:04 +0000915 break;
916 }
917
918 EVTStruct NN;
919 NN.Opcode = Opcode;
920 NN.VT = VT;
921 NN.EVT = EVT;
922 NN.Ops.push_back(N1);
923
924 SDNode *&N = MVTSDNodes[NN];
925 if (N) return SDOperand(N, 0);
926 N = new MVTSDNode(Opcode, VT, N1, EVT);
927 AllNodes.push_back(N);
928 return SDOperand(N, 0);
929}
930
931SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner2ee743f2005-01-14 22:08:15 +0000932 SDOperand N2, MVT::ValueType EVT) {
933 switch (Opcode) {
934 default: assert(0 && "Bad opcode for this accessor!");
935 case ISD::EXTLOAD:
936 case ISD::SEXTLOAD:
937 case ISD::ZEXTLOAD:
938 // If they are asking for an extending loat from/to the same thing, return a
939 // normal load.
940 if (VT == EVT)
941 return getNode(ISD::LOAD, VT, N1, N2);
942 assert(EVT < VT && "Should only be an extending load, not truncating!");
943 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
944 "Cannot sign/zero extend a FP load!");
945 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
946 "Cannot convert from FP to Int or Int -> FP!");
947 break;
948 }
949
950 EVTStruct NN;
951 NN.Opcode = Opcode;
952 NN.VT = VT;
953 NN.EVT = EVT;
954 NN.Ops.push_back(N1);
955 NN.Ops.push_back(N2);
956
957 SDNode *&N = MVTSDNodes[NN];
958 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +0000959 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +0000960 AllNodes.push_back(N);
961 return SDOperand(N, 0);
962}
963
964SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
965 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
966 switch (Opcode) {
967 default: assert(0 && "Bad opcode for this accessor!");
968 case ISD::TRUNCSTORE:
Chris Lattner859157d2005-01-15 06:17:04 +0000969#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
970 // If this is a truncating store of a constant, convert to the desired type
971 // and store it instead.
972 if (isa<Constant>(N1)) {
973 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
974 if (isa<Constant>(Op))
975 N1 = Op;
976 }
977 // Also for ConstantFP?
978#endif
Chris Lattner2ee743f2005-01-14 22:08:15 +0000979 if (N1.getValueType() == EVT) // Normal store?
980 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner859157d2005-01-15 06:17:04 +0000981 assert(N2.getValueType() > EVT && "Not a truncation?");
982 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner2ee743f2005-01-14 22:08:15 +0000983 "Can't do FP-INT conversion!");
984 break;
985 }
986
987 EVTStruct NN;
988 NN.Opcode = Opcode;
989 NN.VT = VT;
990 NN.EVT = EVT;
991 NN.Ops.push_back(N1);
992 NN.Ops.push_back(N2);
993 NN.Ops.push_back(N3);
994
995 SDNode *&N = MVTSDNodes[NN];
996 if (N) return SDOperand(N, 0);
997 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
998 AllNodes.push_back(N);
999 return SDOperand(N, 0);
1000}
1001
1002
Chris Lattner5c884562005-01-12 18:37:47 +00001003/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1004/// indicated value. This method ignores uses of other values defined by this
1005/// operation.
1006bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1007 assert(Value < getNumValues() && "Bad value!");
1008
1009 // If there is only one value, this is easy.
1010 if (getNumValues() == 1)
1011 return use_size() == NUses;
1012 if (Uses.size() < NUses) return false;
1013
1014 SDOperand TheValue(this, Value);
1015
1016 std::set<SDNode*> UsersHandled;
1017
1018 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1019 UI != E; ++UI) {
1020 SDNode *User = *UI;
1021 if (User->getNumOperands() == 1 ||
1022 UsersHandled.insert(User).second) // First time we've seen this?
1023 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1024 if (User->getOperand(i) == TheValue) {
1025 if (NUses == 0)
1026 return false; // too many uses
1027 --NUses;
1028 }
1029 }
1030
1031 // Found exactly the right number of uses?
1032 return NUses == 0;
1033}
1034
1035
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001036const char *SDNode::getOperationName() const {
1037 switch (getOpcode()) {
1038 default: return "<<Unknown>>";
1039 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001040 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001041 case ISD::Constant: return "Constant";
1042 case ISD::ConstantFP: return "ConstantFP";
1043 case ISD::GlobalAddress: return "GlobalAddress";
1044 case ISD::FrameIndex: return "FrameIndex";
1045 case ISD::BasicBlock: return "BasicBlock";
1046 case ISD::ExternalSymbol: return "ExternalSymbol";
1047 case ISD::ConstantPool: return "ConstantPoolIndex";
1048 case ISD::CopyToReg: return "CopyToReg";
1049 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001050 case ISD::ImplicitDef: return "ImplicitDef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001052 case ISD::ADD: return "add";
1053 case ISD::SUB: return "sub";
1054 case ISD::MUL: return "mul";
1055 case ISD::SDIV: return "sdiv";
1056 case ISD::UDIV: return "udiv";
1057 case ISD::SREM: return "srem";
1058 case ISD::UREM: return "urem";
1059 case ISD::AND: return "and";
1060 case ISD::OR: return "or";
1061 case ISD::XOR: return "xor";
1062 case ISD::SHL: return "shl";
1063 case ISD::SRA: return "sra";
1064 case ISD::SRL: return "srl";
1065
1066 case ISD::SELECT: return "select";
1067 case ISD::ADDC: return "addc";
1068 case ISD::SUBB: return "subb";
1069
1070 // Conversion operators.
1071 case ISD::SIGN_EXTEND: return "sign_extend";
1072 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001073 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1074 case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001075 case ISD::TRUNCATE: return "truncate";
1076 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001077 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001078 case ISD::FP_EXTEND: return "fp_extend";
1079
1080 case ISD::SINT_TO_FP: return "sint_to_fp";
1081 case ISD::UINT_TO_FP: return "uint_to_fp";
1082 case ISD::FP_TO_SINT: return "fp_to_sint";
1083 case ISD::FP_TO_UINT: return "fp_to_uint";
1084
1085 // Control flow instructions
1086 case ISD::BR: return "br";
1087 case ISD::BRCOND: return "brcond";
1088 case ISD::RET: return "ret";
1089 case ISD::CALL: return "call";
1090 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1091 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1092
1093 // Other operators
1094 case ISD::LOAD: return "load";
1095 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001096 case ISD::EXTLOAD: return "extload";
1097 case ISD::SEXTLOAD: return "sextload";
1098 case ISD::ZEXTLOAD: return "zextload";
1099 case ISD::TRUNCSTORE: return "truncstore";
1100
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001101 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1102 case ISD::EXTRACT_ELEMENT: return "extract_element";
1103 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001104 case ISD::MEMSET: return "memset";
1105 case ISD::MEMCPY: return "memcpy";
1106 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001107
1108 case ISD::SETCC:
1109 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1110 switch (SetCC->getCondition()) {
1111 default: assert(0 && "Unknown setcc condition!");
1112 case ISD::SETOEQ: return "setcc:setoeq";
1113 case ISD::SETOGT: return "setcc:setogt";
1114 case ISD::SETOGE: return "setcc:setoge";
1115 case ISD::SETOLT: return "setcc:setolt";
1116 case ISD::SETOLE: return "setcc:setole";
1117 case ISD::SETONE: return "setcc:setone";
1118
1119 case ISD::SETO: return "setcc:seto";
1120 case ISD::SETUO: return "setcc:setuo";
1121 case ISD::SETUEQ: return "setcc:setue";
1122 case ISD::SETUGT: return "setcc:setugt";
1123 case ISD::SETUGE: return "setcc:setuge";
1124 case ISD::SETULT: return "setcc:setult";
1125 case ISD::SETULE: return "setcc:setule";
1126 case ISD::SETUNE: return "setcc:setune";
1127
1128 case ISD::SETEQ: return "setcc:seteq";
1129 case ISD::SETGT: return "setcc:setgt";
1130 case ISD::SETGE: return "setcc:setge";
1131 case ISD::SETLT: return "setcc:setlt";
1132 case ISD::SETLE: return "setcc:setle";
1133 case ISD::SETNE: return "setcc:setne";
1134 }
1135 }
1136}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137
1138void SDNode::dump() const {
1139 std::cerr << (void*)this << ": ";
1140
1141 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1142 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001143 if (getValueType(i) == MVT::Other)
1144 std::cerr << "ch";
1145 else
1146 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001148 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001149
1150 std::cerr << " ";
1151 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1152 if (i) std::cerr << ", ";
1153 std::cerr << (void*)getOperand(i).Val;
1154 if (unsigned RN = getOperand(i).ResNo)
1155 std::cerr << ":" << RN;
1156 }
1157
1158 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1159 std::cerr << "<" << CSDN->getValue() << ">";
1160 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1161 std::cerr << "<" << CSDN->getValue() << ">";
1162 } else if (const GlobalAddressSDNode *GADN =
1163 dyn_cast<GlobalAddressSDNode>(this)) {
1164 std::cerr << "<";
1165 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1166 } else if (const FrameIndexSDNode *FIDN =
1167 dyn_cast<FrameIndexSDNode>(this)) {
1168 std::cerr << "<" << FIDN->getIndex() << ">";
1169 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1170 std::cerr << "<" << CP->getIndex() << ">";
1171 } else if (const BasicBlockSDNode *BBDN =
1172 dyn_cast<BasicBlockSDNode>(this)) {
1173 std::cerr << "<";
1174 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1175 if (LBB)
1176 std::cerr << LBB->getName() << " ";
1177 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001178 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179 std::cerr << "<reg #" << C2V->getReg() << ">";
1180 } else if (const ExternalSymbolSDNode *ES =
1181 dyn_cast<ExternalSymbolSDNode>(this)) {
1182 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +00001183 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1184 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001186}
1187
Chris Lattnerea946cd2005-01-09 20:38:33 +00001188static void DumpNodes(SDNode *N, unsigned indent) {
1189 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1190 if (N->getOperand(i).Val->hasOneUse())
1191 DumpNodes(N->getOperand(i).Val, indent+2);
1192 else
1193 std::cerr << "\n" << std::string(indent+2, ' ')
1194 << (void*)N->getOperand(i).Val << ": <multiple use>";
1195
1196
1197 std::cerr << "\n" << std::string(indent, ' ');
1198 N->dump();
1199}
1200
Chris Lattnerc3aae252005-01-07 07:46:32 +00001201void SelectionDAG::dump() const {
1202 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001203 std::vector<SDNode*> Nodes(AllNodes);
1204 std::sort(Nodes.begin(), Nodes.end());
1205
1206 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001207 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1208 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001209 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001210
1211 DumpNodes(getRoot().Val, 2);
1212
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 std::cerr << "\n\n";
1214}
1215