blob: e083609cae634961720f120cd62a57c2e2b96aba [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 Lattnerb48da392005-01-23 04:39:44 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000023#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000024#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000025#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000026#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner5cdcc582005-01-09 20:52:51 +000029static bool isCommutativeBinOp(unsigned Opcode) {
30 switch (Opcode) {
31 case ISD::ADD:
32 case ISD::MUL:
33 case ISD::AND:
34 case ISD::OR:
35 case ISD::XOR: return true;
36 default: return false; // FIXME: Need commutative info for user ops!
37 }
38}
39
40static bool isAssociativeBinOp(unsigned Opcode) {
41 switch (Opcode) {
42 case ISD::ADD:
43 case ISD::MUL:
44 case ISD::AND:
45 case ISD::OR:
46 case ISD::XOR: return true;
47 default: return false; // FIXME: Need associative info for user ops!
48 }
49}
50
Chris Lattner5cdcc582005-01-09 20:52:51 +000051// isInvertibleForFree - Return true if there is no cost to emitting the logical
52// inverse of this node.
53static bool isInvertibleForFree(SDOperand N) {
54 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000055 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000056 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000057 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000058}
59
Jim Laskey58b968b2005-08-17 20:08:02 +000060//===----------------------------------------------------------------------===//
61// ConstantFPSDNode Class
62//===----------------------------------------------------------------------===//
63
64/// isExactlyValue - We don't rely on operator== working on double values, as
65/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
66/// As such, this method can be used to do an exact bit-for-bit comparison of
67/// two floating point values.
68bool ConstantFPSDNode::isExactlyValue(double V) const {
69 return DoubleToBits(V) == DoubleToBits(Value);
70}
71
72//===----------------------------------------------------------------------===//
73// ISD Class
74//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000075
Chris Lattnerc3aae252005-01-07 07:46:32 +000076/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
77/// when given the operation for (X op Y).
78ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
79 // To perform this operation, we just need to swap the L and G bits of the
80 // operation.
81 unsigned OldL = (Operation >> 2) & 1;
82 unsigned OldG = (Operation >> 1) & 1;
83 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
84 (OldL << 1) | // New G bit
85 (OldG << 2)); // New L bit.
86}
87
88/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
89/// 'op' is a valid SetCC operation.
90ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
91 unsigned Operation = Op;
92 if (isInteger)
93 Operation ^= 7; // Flip L, G, E bits, but not U.
94 else
95 Operation ^= 15; // Flip all of the condition bits.
96 if (Operation > ISD::SETTRUE2)
97 Operation &= ~8; // Don't let N and U bits get set.
98 return ISD::CondCode(Operation);
99}
100
101
102/// isSignedOp - For an integer comparison, return 1 if the comparison is a
103/// signed operation and 2 if the result is an unsigned comparison. Return zero
104/// if the operation does not depend on the sign of the input (setne and seteq).
105static int isSignedOp(ISD::CondCode Opcode) {
106 switch (Opcode) {
107 default: assert(0 && "Illegal integer setcc operation!");
108 case ISD::SETEQ:
109 case ISD::SETNE: return 0;
110 case ISD::SETLT:
111 case ISD::SETLE:
112 case ISD::SETGT:
113 case ISD::SETGE: return 1;
114 case ISD::SETULT:
115 case ISD::SETULE:
116 case ISD::SETUGT:
117 case ISD::SETUGE: return 2;
118 }
119}
120
121/// getSetCCOrOperation - Return the result of a logical OR between different
122/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
123/// returns SETCC_INVALID if it is not possible to represent the resultant
124/// comparison.
125ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
126 bool isInteger) {
127 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
128 // Cannot fold a signed integer setcc with an unsigned integer setcc.
129 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000130
Chris Lattnerc3aae252005-01-07 07:46:32 +0000131 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000132
Chris Lattnerc3aae252005-01-07 07:46:32 +0000133 // If the N and U bits get set then the resultant comparison DOES suddenly
134 // care about orderedness, and is true when ordered.
135 if (Op > ISD::SETTRUE2)
136 Op &= ~16; // Clear the N bit.
137 return ISD::CondCode(Op);
138}
139
140/// getSetCCAndOperation - Return the result of a logical AND between different
141/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
142/// function returns zero if it is not possible to represent the resultant
143/// comparison.
144ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
145 bool isInteger) {
146 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
147 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000148 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000149
150 // Combine all of the condition bits.
151 return ISD::CondCode(Op1 & Op2);
152}
153
Chris Lattnerb48da392005-01-23 04:39:44 +0000154const TargetMachine &SelectionDAG::getTarget() const {
155 return TLI.getTargetMachine();
156}
157
Jim Laskey58b968b2005-08-17 20:08:02 +0000158//===----------------------------------------------------------------------===//
159// SelectionDAG Class
160//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000161
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000162/// RemoveDeadNodes - This method deletes all unreachable nodes in the
163/// SelectionDAG, including nodes (like loads) that have uses of their token
164/// chain but no other uses and no side effect. If a node is passed in as an
165/// argument, it is used as the seed for node deletion.
166void SelectionDAG::RemoveDeadNodes(SDNode *N) {
167 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
168
169 // Create a dummy node (which is not added to allnodes), that adds a reference
170 // to the root node, preventing it from being deleted.
171 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
172
Chris Lattner8b8749f2005-08-17 19:00:20 +0000173 // If we have a hint to start from, use it.
174 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000175
176 Restart:
177 unsigned NumNodes = AllNodeSet.size();
178 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
179 I != E; ++I) {
180 // Try to delete this node.
181 DeleteNodeIfDead(*I, &AllNodeSet);
182
183 // If we actually deleted any nodes, do not use invalid iterators in
184 // AllNodeSet.
185 if (AllNodeSet.size() != NumNodes)
186 goto Restart;
187 }
188
189 // Restore AllNodes.
190 if (AllNodes.size() != NumNodes)
191 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
192
193 // If the root changed (e.g. it was a dead load, update the root).
194 setRoot(DummyNode->getOperand(0));
195
196 // Now that we are done with the dummy node, delete it.
197 DummyNode->getOperand(0).Val->removeUser(DummyNode);
198 delete DummyNode;
199}
200
Chris Lattner149c58c2005-08-16 18:17:10 +0000201
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000202void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
203 if (!N->use_empty())
204 return;
205
206 // Okay, we really are going to delete this node. First take this out of the
207 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000208 RemoveNodeFromCSEMaps(N);
209
210 // Next, brutally remove the operand list. This is safe to do, as there are
211 // no cycles in the graph.
212 while (!N->Operands.empty()) {
213 SDNode *O = N->Operands.back().Val;
214 N->Operands.pop_back();
215 O->removeUser(N);
216
217 // Now that we removed this operand, see if there are no uses of it left.
218 DeleteNodeIfDead(O, NodeSet);
219 }
220
221 // Remove the node from the nodes set and delete it.
222 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
223 AllNodeSet.erase(N);
224
225 // Now that the node is gone, check to see if any of the operands of this node
226 // are dead now.
227 delete N;
228}
229
230/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
231/// correspond to it. This is useful when we're about to delete or repurpose
232/// the node. We don't want future request for structurally identical nodes
233/// to return N anymore.
234void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000235 switch (N->getOpcode()) {
236 case ISD::Constant:
237 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
238 N->getValueType(0)));
239 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000240 case ISD::TargetConstant:
241 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
242 N->getValueType(0)));
243 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000244 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000245 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
246 ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000247 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000248 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000249 case ISD::CONDCODE:
250 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
251 "Cond code doesn't exist!");
252 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
253 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254 case ISD::GlobalAddress:
255 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
256 break;
257 case ISD::FrameIndex:
258 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
259 break;
260 case ISD::ConstantPool:
261 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
262 break;
263 case ISD::BasicBlock:
264 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
265 break;
266 case ISD::ExternalSymbol:
267 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
268 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000269 case ISD::VALUETYPE:
270 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
271 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000272 case ISD::Register:
273 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
274 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000275 case ISD::SRCVALUE: {
276 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
277 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
278 break;
279 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280 case ISD::LOAD:
281 Loads.erase(std::make_pair(N->getOperand(1),
282 std::make_pair(N->getOperand(0),
283 N->getValueType(0))));
284 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000285 default:
286 if (N->getNumOperands() == 1)
287 UnaryOps.erase(std::make_pair(N->getOpcode(),
288 std::make_pair(N->getOperand(0),
289 N->getValueType(0))));
290 else if (N->getNumOperands() == 2)
291 BinaryOps.erase(std::make_pair(N->getOpcode(),
292 std::make_pair(N->getOperand(0),
293 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000294 else if (N->getNumValues() == 1) {
295 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
296 OneResultNodes.erase(std::make_pair(N->getOpcode(),
297 std::make_pair(N->getValueType(0),
298 Ops)));
299 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000300 // Remove the node from the ArbitraryNodes map.
301 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
302 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
303 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
304 std::make_pair(RV, Ops)));
305 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000306 break;
307 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308}
309
Chris Lattner8b8749f2005-08-17 19:00:20 +0000310/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
311/// has been taken out and modified in some way. If the specified node already
312/// exists in the CSE maps, do not modify the maps, but return the existing node
313/// instead. If it doesn't exist, add it and return null.
314///
315SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
316 assert(N->getNumOperands() && "This is a leaf node!");
317 if (N->getOpcode() == ISD::LOAD) {
318 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
319 std::make_pair(N->getOperand(0),
320 N->getValueType(0)))];
321 if (L) return L;
322 L = N;
323 } else if (N->getNumOperands() == 1) {
324 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
325 std::make_pair(N->getOperand(0),
326 N->getValueType(0)))];
327 if (U) return U;
328 U = N;
329 } else if (N->getNumOperands() == 2) {
330 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
331 std::make_pair(N->getOperand(0),
332 N->getOperand(1)))];
333 if (B) return B;
334 B = N;
335 } else if (N->getNumValues() == 1) {
336 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
337 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
338 std::make_pair(N->getValueType(0), Ops))];
339 if (ORN) return ORN;
340 ORN = N;
341 } else {
342 // Remove the node from the ArbitraryNodes map.
343 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
344 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
345 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
346 std::make_pair(RV, Ops))];
347 if (AN) return AN;
348 AN = N;
349 }
350 return 0;
351
352}
353
354
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000355
Chris Lattner78ec3112003-08-11 14:57:33 +0000356SelectionDAG::~SelectionDAG() {
357 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
358 delete AllNodes[i];
359}
360
Chris Lattner0f2287b2005-04-13 02:38:18 +0000361SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000362 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000363 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000364 return getNode(ISD::AND, Op.getValueType(), Op,
365 getConstant(Imm, Op.getValueType()));
366}
367
Chris Lattnerc3aae252005-01-07 07:46:32 +0000368SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
369 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
370 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000371 if (VT != MVT::i64)
372 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000373
Chris Lattnerc3aae252005-01-07 07:46:32 +0000374 SDNode *&N = Constants[std::make_pair(Val, VT)];
375 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000376 N = new ConstantSDNode(false, Val, VT);
377 AllNodes.push_back(N);
378 return SDOperand(N, 0);
379}
380
381SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
382 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
383 // Mask out any bits that are not valid for this constant.
384 if (VT != MVT::i64)
385 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
386
387 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
388 if (N) return SDOperand(N, 0);
389 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000390 AllNodes.push_back(N);
391 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000392}
393
Chris Lattnerc3aae252005-01-07 07:46:32 +0000394SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
395 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
396 if (VT == MVT::f32)
397 Val = (float)Val; // Mask out extra precision.
398
Chris Lattnerd8658612005-02-17 20:17:32 +0000399 // Do the map lookup using the actual bit pattern for the floating point
400 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
401 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000402 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000403 if (N) return SDOperand(N, 0);
404 N = new ConstantFPSDNode(Val, VT);
405 AllNodes.push_back(N);
406 return SDOperand(N, 0);
407}
408
409
410
411SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
412 MVT::ValueType VT) {
413 SDNode *&N = GlobalValues[GV];
414 if (N) return SDOperand(N, 0);
415 N = new GlobalAddressSDNode(GV,VT);
416 AllNodes.push_back(N);
417 return SDOperand(N, 0);
418}
419
420SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
421 SDNode *&N = FrameIndices[FI];
422 if (N) return SDOperand(N, 0);
423 N = new FrameIndexSDNode(FI, VT);
424 AllNodes.push_back(N);
425 return SDOperand(N, 0);
426}
427
428SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
429 SDNode *N = ConstantPoolIndices[CPIdx];
430 if (N) return SDOperand(N, 0);
431 N = new ConstantPoolSDNode(CPIdx, VT);
432 AllNodes.push_back(N);
433 return SDOperand(N, 0);
434}
435
436SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
437 SDNode *&N = BBNodes[MBB];
438 if (N) return SDOperand(N, 0);
439 N = new BasicBlockSDNode(MBB);
440 AllNodes.push_back(N);
441 return SDOperand(N, 0);
442}
443
Chris Lattner15e4b012005-07-10 00:07:11 +0000444SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
445 if ((unsigned)VT >= ValueTypeNodes.size())
446 ValueTypeNodes.resize(VT+1);
447 if (ValueTypeNodes[VT] == 0) {
448 ValueTypeNodes[VT] = new VTSDNode(VT);
449 AllNodes.push_back(ValueTypeNodes[VT]);
450 }
451
452 return SDOperand(ValueTypeNodes[VT], 0);
453}
454
Chris Lattnerc3aae252005-01-07 07:46:32 +0000455SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
456 SDNode *&N = ExternalSymbols[Sym];
457 if (N) return SDOperand(N, 0);
458 N = new ExternalSymbolSDNode(Sym, VT);
459 AllNodes.push_back(N);
460 return SDOperand(N, 0);
461}
462
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000463SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
464 if ((unsigned)Cond >= CondCodeNodes.size())
465 CondCodeNodes.resize(Cond+1);
466
Chris Lattner079a27a2005-08-09 20:40:02 +0000467 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000468 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000469 AllNodes.push_back(CondCodeNodes[Cond]);
470 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000471 return SDOperand(CondCodeNodes[Cond], 0);
472}
473
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000474SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
475 if (Reg >= RegNodes.size())
476 RegNodes.resize(Reg+1);
477 RegisterSDNode *&Result = RegNodes[Reg];
478 if (Result) {
479 assert(Result->getValueType(0) == VT &&
480 "Inconsistent value types for machine registers");
481 } else {
482 Result = new RegisterSDNode(Reg, VT);
483 AllNodes.push_back(Result);
484 }
485 return SDOperand(Result, 0);
486}
487
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000488SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
489 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000490 // These setcc operations always fold.
491 switch (Cond) {
492 default: break;
493 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000494 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000495 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000496 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000497 }
498
Chris Lattner67255a12005-04-07 18:14:58 +0000499 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
500 uint64_t C2 = N2C->getValue();
501 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
502 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000503
Chris Lattnerc3aae252005-01-07 07:46:32 +0000504 // Sign extend the operands if required
505 if (ISD::isSignedIntSetCC(Cond)) {
506 C1 = N1C->getSignExtended();
507 C2 = N2C->getSignExtended();
508 }
509
510 switch (Cond) {
511 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000512 case ISD::SETEQ: return getConstant(C1 == C2, VT);
513 case ISD::SETNE: return getConstant(C1 != C2, VT);
514 case ISD::SETULT: return getConstant(C1 < C2, VT);
515 case ISD::SETUGT: return getConstant(C1 > C2, VT);
516 case ISD::SETULE: return getConstant(C1 <= C2, VT);
517 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
518 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
519 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
520 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
521 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000522 }
Chris Lattner24673922005-04-07 18:58:54 +0000523 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000524 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
525 // the comparison on the input.
526 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
527 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
528
529 // If the comparison constant has bits in the upper part, the
530 // zero-extended value could never match.
531 if (C2 & (~0ULL << InSize)) {
532 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
533 switch (Cond) {
534 case ISD::SETUGT:
535 case ISD::SETUGE:
536 case ISD::SETEQ: return getConstant(0, VT);
537 case ISD::SETULT:
538 case ISD::SETULE:
539 case ISD::SETNE: return getConstant(1, VT);
540 case ISD::SETGT:
541 case ISD::SETGE:
542 // True if the sign bit of C2 is set.
543 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
544 case ISD::SETLT:
545 case ISD::SETLE:
546 // True if the sign bit of C2 isn't set.
547 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
548 default:
549 break;
550 }
551 }
552
553 // Otherwise, we can perform the comparison with the low bits.
554 switch (Cond) {
555 case ISD::SETEQ:
556 case ISD::SETNE:
557 case ISD::SETUGT:
558 case ISD::SETUGE:
559 case ISD::SETULT:
560 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000561 return getSetCC(VT, N1.getOperand(0),
562 getConstant(C2, N1.getOperand(0).getValueType()),
563 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000564 default:
565 break; // todo, be more careful with signed comparisons
566 }
567 }
568
Chris Lattner67255a12005-04-07 18:14:58 +0000569 uint64_t MinVal, MaxVal;
570 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
571 if (ISD::isSignedIntSetCC(Cond)) {
572 MinVal = 1ULL << (OperandBitSize-1);
573 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
574 MaxVal = ~0ULL >> (65-OperandBitSize);
575 else
576 MaxVal = 0;
577 } else {
578 MinVal = 0;
579 MaxVal = ~0ULL >> (64-OperandBitSize);
580 }
581
582 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
583 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
584 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
585 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000586 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
587 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000588 }
589
590 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
591 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
592 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000593 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
594 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000595 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000596
Nate Begeman72ea2812005-04-14 08:56:52 +0000597 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
598 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000599
Nate Begeman72ea2812005-04-14 08:56:52 +0000600 // Canonicalize setgt X, Min --> setne X, Min
601 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000602 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000603
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000604 // If we have setult X, 1, turn it into seteq X, 0
605 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000606 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
607 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000608 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000609 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000610 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
611 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000612
613 // If we have "setcc X, C1", check to see if we can shrink the immediate
614 // by changing cc.
615
616 // SETUGT X, SINTMAX -> SETLT X, 0
617 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
618 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000619 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000620
621 // FIXME: Implement the rest of these.
622
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000623
624 // Fold bit comparisons when we can.
625 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
626 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
627 if (ConstantSDNode *AndRHS =
628 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
629 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
630 // Perform the xform if the AND RHS is a single bit.
631 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
632 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000633 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000634 TLI.getShiftAmountTy()));
635 }
636 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
637 // (X & 8) == 8 --> (X & 8) >> 3
638 // Perform the xform if C2 is a single bit.
639 if ((C2 & (C2-1)) == 0) {
640 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000641 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000642 }
643 }
644 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000645 }
Chris Lattner67255a12005-04-07 18:14:58 +0000646 } else if (isa<ConstantSDNode>(N1.Val)) {
647 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000648 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000649 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000650
651 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
652 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
653 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000654
Chris Lattnerc3aae252005-01-07 07:46:32 +0000655 switch (Cond) {
656 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000657 case ISD::SETEQ: return getConstant(C1 == C2, VT);
658 case ISD::SETNE: return getConstant(C1 != C2, VT);
659 case ISD::SETLT: return getConstant(C1 < C2, VT);
660 case ISD::SETGT: return getConstant(C1 > C2, VT);
661 case ISD::SETLE: return getConstant(C1 <= C2, VT);
662 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000663 }
664 } else {
665 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000666 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000667 }
668
669 if (N1 == N2) {
670 // We can always fold X == Y for integer setcc's.
671 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000672 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000673 unsigned UOF = ISD::getUnorderedFlavor(Cond);
674 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000675 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000676 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000677 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000678 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
679 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000680 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
681 if (NewCond != Cond)
682 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683 }
684
Chris Lattner5cdcc582005-01-09 20:52:51 +0000685 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000686 MVT::isInteger(N1.getValueType())) {
687 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
688 N1.getOpcode() == ISD::XOR) {
689 // Simplify (X+Y) == (X+Z) --> Y == Z
690 if (N1.getOpcode() == N2.getOpcode()) {
691 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000692 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000693 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000694 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000695 if (isCommutativeBinOp(N1.getOpcode())) {
696 // If X op Y == Y op X, try other combinations.
697 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000698 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000699 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000700 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000701 }
702 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000703
704 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000705
Chris Lattner68dc3102005-01-10 02:03:02 +0000706 // Simplify (X+Z) == X --> Z == 0
707 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000708 return getSetCC(VT, N1.getOperand(1),
709 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000710 if (N1.getOperand(1) == N2) {
711 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000712 return getSetCC(VT, N1.getOperand(0),
713 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000714 else {
715 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
716 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000717 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000718 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000719 N2, getConstant(1, TLI.getShiftAmountTy())),
720 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000721 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000722 }
723 }
724
Chris Lattner68dc3102005-01-10 02:03:02 +0000725 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
726 N2.getOpcode() == ISD::XOR) {
727 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000728 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000729 return getSetCC(VT, N2.getOperand(1),
730 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000731 } else if (N2.getOperand(1) == N1) {
732 if (isCommutativeBinOp(N2.getOpcode())) {
733 return getSetCC(VT, N2.getOperand(0),
734 getConstant(0, N2.getValueType()), Cond);
735 } else {
736 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
737 // X == (Z-X) --> X<<1 == Z
738 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
739 getConstant(1, TLI.getShiftAmountTy())),
740 N2.getOperand(0), Cond);
741 }
742 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000743 }
744 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000745
Chris Lattnerfda2b552005-04-18 04:48:12 +0000746 // Fold away ALL boolean setcc's.
747 if (N1.getValueType() == MVT::i1) {
748 switch (Cond) {
749 default: assert(0 && "Unknown integer setcc!");
750 case ISD::SETEQ: // X == Y -> (X^Y)^1
751 N1 = getNode(ISD::XOR, MVT::i1,
752 getNode(ISD::XOR, MVT::i1, N1, N2),
753 getConstant(1, MVT::i1));
754 break;
755 case ISD::SETNE: // X != Y --> (X^Y)
756 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
757 break;
758 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
759 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
760 N1 = getNode(ISD::AND, MVT::i1, N2,
761 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
762 break;
763 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
764 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
765 N1 = getNode(ISD::AND, MVT::i1, N1,
766 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
767 break;
768 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
769 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
770 N1 = getNode(ISD::OR, MVT::i1, N2,
771 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
772 break;
773 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
774 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
775 N1 = getNode(ISD::OR, MVT::i1, N1,
776 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
777 break;
778 }
779 if (VT != MVT::i1)
780 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
781 return N1;
782 }
783
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000784 // Could not fold it.
785 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000786}
787
Nate Begemanff663682005-08-13 06:14:17 +0000788SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
789 SDOperand N3, SDOperand N4,
790 ISD::CondCode CC) {
791 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000792 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
793 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
794 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
795
796 // Check to see if we can simplify the select into an fabs node
797 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
798 // Allow either -0.0 or 0.0
799 if (CFP->getValue() == 0.0) {
800 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
801 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
802 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
803 N1 == N4.getOperand(0))
804 return getNode(ISD::FABS, VT, N1);
805
806 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
807 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
808 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
809 N3.getOperand(0) == N4)
810 return getNode(ISD::FABS, VT, N4);
811 }
812 }
813
814 // Check to see if we can perform the "gzip trick", transforming
815 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
816 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
817 MVT::isInteger(N1.getValueType()) &&
818 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
819 MVT::ValueType XType = N1.getValueType();
820 MVT::ValueType AType = N3.getValueType();
821 if (XType >= AType) {
822 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
823 // single-bit constant. FIXME: remove once the dag combiner
824 // exists.
825 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
826 unsigned ShCtV = Log2_64(N3C->getValue());
827 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
828 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
829 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
830 if (XType > AType)
831 Shift = getNode(ISD::TRUNCATE, AType, Shift);
832 return getNode(ISD::AND, AType, Shift, N3);
833 }
834 SDOperand Shift = getNode(ISD::SRA, XType, N1,
835 getConstant(MVT::getSizeInBits(XType)-1,
836 TLI.getShiftAmountTy()));
837 if (XType > AType)
838 Shift = getNode(ISD::TRUNCATE, AType, Shift);
839 return getNode(ISD::AND, AType, Shift, N3);
840 }
841 }
842
843 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
844 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
845 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
846 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
847 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
848 MVT::ValueType XType = N1.getValueType();
849 if (SubC->isNullValue() && MVT::isInteger(XType)) {
850 SDOperand Shift = getNode(ISD::SRA, XType, N1,
851 getConstant(MVT::getSizeInBits(XType)-1,
852 TLI.getShiftAmountTy()));
853 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
854 Shift);
855 }
856 }
857 }
858
859 // Could not fold it.
860 return SDOperand();
861}
862
Chris Lattnerc3aae252005-01-07 07:46:32 +0000863/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000864///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000865SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
866 SDNode *N = new SDNode(Opcode, VT);
867 AllNodes.push_back(N);
868 return SDOperand(N, 0);
869}
870
Chris Lattnerc3aae252005-01-07 07:46:32 +0000871SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
872 SDOperand Operand) {
873 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
874 uint64_t Val = C->getValue();
875 switch (Opcode) {
876 default: break;
877 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
878 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
879 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000880 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
881 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 }
883 }
884
885 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
886 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000887 case ISD::FNEG:
888 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000889 case ISD::FP_ROUND:
890 case ISD::FP_EXTEND:
891 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000892 case ISD::FP_TO_SINT:
893 return getConstant((int64_t)C->getValue(), VT);
894 case ISD::FP_TO_UINT:
895 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000896 }
897
898 unsigned OpOpcode = Operand.Val->getOpcode();
899 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000900 case ISD::TokenFactor:
901 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000902 case ISD::SIGN_EXTEND:
903 if (Operand.getValueType() == VT) return Operand; // noop extension
904 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
905 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
906 break;
907 case ISD::ZERO_EXTEND:
908 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000909 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000910 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 break;
912 case ISD::TRUNCATE:
913 if (Operand.getValueType() == VT) return Operand; // noop truncate
914 if (OpOpcode == ISD::TRUNCATE)
915 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000916 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
917 // If the source is smaller than the dest, we still need an extend.
918 if (Operand.Val->getOperand(0).getValueType() < VT)
919 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
920 else if (Operand.Val->getOperand(0).getValueType() > VT)
921 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
922 else
923 return Operand.Val->getOperand(0);
924 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000925 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000926 case ISD::FNEG:
927 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
928 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
929 Operand.Val->getOperand(0));
930 if (OpOpcode == ISD::FNEG) // --X -> X
931 return Operand.Val->getOperand(0);
932 break;
933 case ISD::FABS:
934 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
935 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
936 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 }
938
939 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
940 if (N) return SDOperand(N, 0);
941 N = new SDNode(Opcode, Operand);
942 N->setValueTypes(VT);
943 AllNodes.push_back(N);
944 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000945}
946
Chris Lattner36019aa2005-04-18 03:48:41 +0000947/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
948/// this predicate to simplify operations downstream. V and Mask are known to
949/// be the same type.
950static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
951 const TargetLowering &TLI) {
952 unsigned SrcBits;
953 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000954
Chris Lattner36019aa2005-04-18 03:48:41 +0000955 // If we know the result of a setcc has the top bits zero, use this info.
956 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +0000957 case ISD::Constant:
958 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
959
960 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000961 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000962 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
963
964 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000965 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000966 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
967 case ISD::ZERO_EXTEND:
968 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
969 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
970
971 case ISD::AND:
972 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000973 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000974 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
975
976 // FALL THROUGH
977 case ISD::OR:
978 case ISD::XOR:
979 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
980 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
981 case ISD::SELECT:
982 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
983 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000984
985 case ISD::SRL:
986 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
987 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
988 uint64_t NewVal = Mask << ShAmt->getValue();
989 SrcBits = MVT::getSizeInBits(Op.getValueType());
990 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
991 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
992 }
993 return false;
994 case ISD::SHL:
995 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
996 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
997 uint64_t NewVal = Mask >> ShAmt->getValue();
998 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
999 }
1000 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +00001001 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001002 default: break;
1003 }
1004
1005 return false;
1006}
1007
1008
1009
Chris Lattnerc3aae252005-01-07 07:46:32 +00001010SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1011 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001012#ifndef NDEBUG
1013 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001014 case ISD::TokenFactor:
1015 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1016 N2.getValueType() == MVT::Other && "Invalid token factor!");
1017 break;
Chris Lattner76365122005-01-16 02:23:22 +00001018 case ISD::AND:
1019 case ISD::OR:
1020 case ISD::XOR:
1021 case ISD::UDIV:
1022 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001023 case ISD::MULHU:
1024 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001025 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1026 // fall through
1027 case ISD::ADD:
1028 case ISD::SUB:
1029 case ISD::MUL:
1030 case ISD::SDIV:
1031 case ISD::SREM:
1032 assert(N1.getValueType() == N2.getValueType() &&
1033 N1.getValueType() == VT && "Binary operator types must match!");
1034 break;
1035
1036 case ISD::SHL:
1037 case ISD::SRA:
1038 case ISD::SRL:
1039 assert(VT == N1.getValueType() &&
1040 "Shift operators return type must be the same as their first arg");
1041 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001042 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001043 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001044 case ISD::FP_ROUND_INREG: {
1045 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1046 assert(VT == N1.getValueType() && "Not an inreg round!");
1047 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1048 "Cannot FP_ROUND_INREG integer types");
1049 assert(EVT <= VT && "Not rounding down!");
1050 break;
1051 }
1052 case ISD::SIGN_EXTEND_INREG: {
1053 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1054 assert(VT == N1.getValueType() && "Not an inreg extend!");
1055 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1056 "Cannot *_EXTEND_INREG FP types");
1057 assert(EVT <= VT && "Not extending!");
1058 }
1059
Chris Lattner76365122005-01-16 02:23:22 +00001060 default: break;
1061 }
1062#endif
1063
Chris Lattnerc3aae252005-01-07 07:46:32 +00001064 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1065 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1066 if (N1C) {
1067 if (N2C) {
1068 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1069 switch (Opcode) {
1070 case ISD::ADD: return getConstant(C1 + C2, VT);
1071 case ISD::SUB: return getConstant(C1 - C2, VT);
1072 case ISD::MUL: return getConstant(C1 * C2, VT);
1073 case ISD::UDIV:
1074 if (C2) return getConstant(C1 / C2, VT);
1075 break;
1076 case ISD::UREM :
1077 if (C2) return getConstant(C1 % C2, VT);
1078 break;
1079 case ISD::SDIV :
1080 if (C2) return getConstant(N1C->getSignExtended() /
1081 N2C->getSignExtended(), VT);
1082 break;
1083 case ISD::SREM :
1084 if (C2) return getConstant(N1C->getSignExtended() %
1085 N2C->getSignExtended(), VT);
1086 break;
1087 case ISD::AND : return getConstant(C1 & C2, VT);
1088 case ISD::OR : return getConstant(C1 | C2, VT);
1089 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001090 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1091 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1092 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001093 default: break;
1094 }
1095
1096 } else { // Cannonicalize constant to RHS if commutative
1097 if (isCommutativeBinOp(Opcode)) {
1098 std::swap(N1C, N2C);
1099 std::swap(N1, N2);
1100 }
1101 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001102
1103 switch (Opcode) {
1104 default: break;
1105 case ISD::SHL: // shl 0, X -> 0
1106 if (N1C->isNullValue()) return N1;
1107 break;
1108 case ISD::SRL: // srl 0, X -> 0
1109 if (N1C->isNullValue()) return N1;
1110 break;
1111 case ISD::SRA: // sra -1, X -> -1
1112 if (N1C->isAllOnesValue()) return N1;
1113 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001114 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1115 // Extending a constant? Just return the extended constant.
1116 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1117 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001118 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119 }
1120
1121 if (N2C) {
1122 uint64_t C2 = N2C->getValue();
1123
1124 switch (Opcode) {
1125 case ISD::ADD:
1126 if (!C2) return N1; // add X, 0 -> X
1127 break;
1128 case ISD::SUB:
1129 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001130 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 case ISD::MUL:
1132 if (!C2) return N2; // mul X, 0 -> 0
1133 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1134 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1135
Chris Lattnerb48da392005-01-23 04:39:44 +00001136 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001138 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001139 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 }
1141 break;
1142
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001143 case ISD::MULHU:
1144 case ISD::MULHS:
1145 if (!C2) return N2; // mul X, 0 -> 0
1146
1147 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1148 return getConstant(0, VT);
1149
1150 // Many others could be handled here, including -1, powers of 2, etc.
1151 break;
1152
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001154 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001156 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001157 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001158 }
1159 break;
1160
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001161 case ISD::SHL:
1162 case ISD::SRL:
1163 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001164 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001165 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001166 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1167 return getNode(ISD::UNDEF, N1.getValueType());
1168 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001169 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001170
1171 if (Opcode == ISD::SRA) {
1172 // If the sign bit is known to be zero, switch this to a SRL.
1173 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001174 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001175 TLI))
1176 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1177 } else {
1178 // If the part left over is known to be zero, the whole thing is zero.
1179 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1180 if (Opcode == ISD::SRL) {
1181 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1182 return getConstant(0, N1.getValueType());
1183 } else if (Opcode == ISD::SHL) {
1184 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1185 return getConstant(0, N1.getValueType());
1186 }
1187 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001188
1189 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1190 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1191 unsigned OpSAC = OpSA->getValue();
1192 if (N1.getOpcode() == ISD::SHL) {
1193 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1194 return getConstant(0, N1.getValueType());
1195 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1196 getConstant(C2+OpSAC, N2.getValueType()));
1197 } else if (N1.getOpcode() == ISD::SRL) {
1198 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1199 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1200 getConstant(~0ULL << OpSAC, VT));
1201 if (C2 > OpSAC) {
1202 return getNode(ISD::SHL, VT, Mask,
1203 getConstant(C2-OpSAC, N2.getValueType()));
1204 } else {
1205 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1206 return getNode(ISD::SRL, VT, Mask,
1207 getConstant(OpSAC-C2, N2.getValueType()));
1208 }
1209 } else if (N1.getOpcode() == ISD::SRA) {
1210 // if C1 == C2, just mask out low bits.
1211 if (C2 == OpSAC)
1212 return getNode(ISD::AND, VT, N1.getOperand(0),
1213 getConstant(~0ULL << C2, VT));
1214 }
1215 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001216 break;
1217
Chris Lattnerc3aae252005-01-07 07:46:32 +00001218 case ISD::AND:
1219 if (!C2) return N2; // X and 0 -> 0
1220 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001221 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001222
Chris Lattner36019aa2005-04-18 03:48:41 +00001223 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1224 return getConstant(0, VT);
1225
Chris Lattner588bbbf2005-04-21 06:28:15 +00001226 {
1227 uint64_t NotC2 = ~C2;
1228 if (VT != MVT::i64)
1229 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1230
1231 if (MaskedValueIsZero(N1, NotC2, TLI))
1232 return N1; // if (X & ~C2) -> 0, the and is redundant
1233 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001234
Chris Lattnerdea29e22005-04-10 01:13:15 +00001235 // FIXME: Should add a corresponding version of this for
1236 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1237 // we don't have yet.
1238
Chris Lattner0f2287b2005-04-13 02:38:18 +00001239 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1240 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001241 // If we are masking out the part of our input that was extended, just
1242 // mask the input to the extension directly.
1243 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001244 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001245 if ((C2 & (~0ULL << ExtendBits)) == 0)
1246 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001247 } else if (N1.getOpcode() == ISD::OR) {
1248 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1249 if ((ORI->getValue() & C2) == C2) {
1250 // If the 'or' is setting all of the bits that we are masking for,
1251 // we know the result of the AND will be the AND mask itself.
1252 return N2;
1253 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001254 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001255 break;
1256 case ISD::OR:
1257 if (!C2)return N1; // X or 0 -> X
1258 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001259 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001260 break;
1261 case ISD::XOR:
1262 if (!C2) return N1; // X xor 0 -> X
1263 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001264 if (N1.Val->getOpcode() == ISD::SETCC){
1265 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001266 // !(X op Y) -> (X !op Y)
1267 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001268 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1269 return getSetCC(SetCC->getValueType(0),
1270 SetCC->getOperand(0), SetCC->getOperand(1),
1271 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001272 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1273 SDNode *Op = N1.Val;
1274 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1275 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1276 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1277 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1278 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1279 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1280 if (Op->getOpcode() == ISD::AND)
1281 return getNode(ISD::OR, VT, LHS, RHS);
1282 return getNode(ISD::AND, VT, LHS, RHS);
1283 }
1284 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001285 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001286 }
1287 break;
1288 }
1289
1290 // Reassociate ((X op C1) op C2) if possible.
1291 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1292 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001293 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001294 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1295 }
1296
1297 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1298 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001299 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001300 if (N2CFP) {
1301 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1302 switch (Opcode) {
1303 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1304 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1305 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1306 case ISD::SDIV:
1307 if (C2) return getConstantFP(C1 / C2, VT);
1308 break;
1309 case ISD::SREM :
1310 if (C2) return getConstantFP(fmod(C1, C2), VT);
1311 break;
1312 default: break;
1313 }
1314
1315 } else { // Cannonicalize constant to RHS if commutative
1316 if (isCommutativeBinOp(Opcode)) {
1317 std::swap(N1CFP, N2CFP);
1318 std::swap(N1, N2);
1319 }
1320 }
1321
Chris Lattner15e4b012005-07-10 00:07:11 +00001322 if (Opcode == ISD::FP_ROUND_INREG)
1323 return getNode(ISD::FP_EXTEND, VT,
1324 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1325 }
1326
Chris Lattnerc3aae252005-01-07 07:46:32 +00001327 // Finally, fold operations that do not require constants.
1328 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001329 case ISD::TokenFactor:
1330 if (N1.getOpcode() == ISD::EntryToken)
1331 return N2;
1332 if (N2.getOpcode() == ISD::EntryToken)
1333 return N1;
1334 break;
1335
Chris Lattnerc3aae252005-01-07 07:46:32 +00001336 case ISD::AND:
1337 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001338 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1339 SDNode *LHS = N1.Val, *RHS = N2.Val;
1340 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1341 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1342 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1343 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001344
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001345 if (LR == RR && isa<ConstantSDNode>(LR) &&
1346 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1347 // (X != 0) | (Y != 0) -> (X|Y != 0)
1348 // (X == 0) & (Y == 0) -> (X|Y == 0)
1349 // (X < 0) | (Y < 0) -> (X|Y < 0)
1350 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1351 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1352 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1353 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1354 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1355 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001356
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001357 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1358 // (X == -1) & (Y == -1) -> (X&Y == -1)
1359 // (X != -1) | (Y != -1) -> (X&Y != -1)
1360 // (X > -1) | (Y > -1) -> (X&Y > -1)
1361 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1362 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1363 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1364 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1365 LR, Op2);
1366 // (X > -1) & (Y > -1) -> (X|Y > -1)
1367 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1368 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1369 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001370 }
1371 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001372
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001373 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1374 if (LL == RR && LR == RL) {
1375 Op2 = ISD::getSetCCSwappedOperands(Op2);
1376 goto MatchedBackwards;
1377 }
1378
1379 if (LL == RL && LR == RR) {
1380 MatchedBackwards:
1381 ISD::CondCode Result;
1382 bool isInteger = MVT::isInteger(LL.getValueType());
1383 if (Opcode == ISD::OR)
1384 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1385 else
1386 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1387
1388 if (Result != ISD::SETCC_INVALID)
1389 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1390 }
1391 }
1392
Chris Lattner7467c9b2005-04-18 04:11:19 +00001393 // and/or zext(a), zext(b) -> zext(and/or a, b)
1394 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1395 N2.getOpcode() == ISD::ZERO_EXTEND &&
1396 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1397 return getNode(ISD::ZERO_EXTEND, VT,
1398 getNode(Opcode, N1.getOperand(0).getValueType(),
1399 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001400 break;
1401 case ISD::XOR:
1402 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1403 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001404 case ISD::ADD:
1405 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1406 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1407 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1408 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001409 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1410 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1411 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1412 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1413 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1414 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001415 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1416 !MVT::isFloatingPoint(N2.getValueType()))
1417 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001418 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001419 case ISD::SUB:
1420 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001421 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001422 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001423 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001424 if (N1.Val->getOperand(1) == N2 &&
1425 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001426 return N1.Val->getOperand(0); // (A+B)-B == A
1427 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001428 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1429 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001430 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001431 case ISD::FP_ROUND_INREG:
1432 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1433 break;
1434 case ISD::SIGN_EXTEND_INREG: {
1435 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1436 if (EVT == VT) return N1; // Not actually extending
1437
1438 // If we are sign extending an extension, use the original source.
1439 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1440 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1441 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001442
Chris Lattner15e4b012005-07-10 00:07:11 +00001443 // If we are sign extending a sextload, return just the load.
1444 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001445 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001446 return N1;
1447
1448 // If we are extending the result of a setcc, and we already know the
1449 // contents of the top bits, eliminate the extension.
1450 if (N1.getOpcode() == ISD::SETCC &&
1451 TLI.getSetCCResultContents() ==
1452 TargetLowering::ZeroOrNegativeOneSetCCResult)
1453 return N1;
1454
1455 // If we are sign extending the result of an (and X, C) operation, and we
1456 // know the extended bits are zeros already, don't do the extend.
1457 if (N1.getOpcode() == ISD::AND)
1458 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1459 uint64_t Mask = N1C->getValue();
1460 unsigned NumBits = MVT::getSizeInBits(EVT);
1461 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1462 return N1;
1463 }
1464 break;
1465 }
1466
Nate Begemaneea805e2005-04-13 21:23:31 +00001467 // FIXME: figure out how to safely handle things like
1468 // int foo(int x) { return 1 << (x & 255); }
1469 // int bar() { return foo(256); }
1470#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001471 case ISD::SHL:
1472 case ISD::SRL:
1473 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001474 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001475 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001476 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001477 else if (N2.getOpcode() == ISD::AND)
1478 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1479 // If the and is only masking out bits that cannot effect the shift,
1480 // eliminate the and.
1481 unsigned NumBits = MVT::getSizeInBits(VT);
1482 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1483 return getNode(Opcode, VT, N1, N2.getOperand(0));
1484 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001485 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001486#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 }
1488
Chris Lattner27e9b412005-05-11 18:57:39 +00001489 // Memoize this node if possible.
1490 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001491 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001492 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1493 if (BON) return SDOperand(BON, 0);
1494
1495 BON = N = new SDNode(Opcode, N1, N2);
1496 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001497 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001498 }
1499
Chris Lattner3e011362005-05-14 07:45:46 +00001500 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001501 AllNodes.push_back(N);
1502 return SDOperand(N, 0);
1503}
1504
Chris Lattner88de6e72005-05-12 00:17:04 +00001505// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001506// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001507void SDNode::setAdjCallChain(SDOperand N) {
1508 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001509 assert((getOpcode() == ISD::CALLSEQ_START ||
1510 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001511
1512 Operands[0].Val->removeUser(this);
1513 Operands[0] = N;
1514 N.Val->Uses.push_back(this);
1515}
1516
1517
1518
Chris Lattnerc3aae252005-01-07 07:46:32 +00001519SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001520 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001521 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001522 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1523 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001524 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001525
1526 // Loads have a token chain.
1527 N->setValueTypes(VT, MVT::Other);
1528 AllNodes.push_back(N);
1529 return SDOperand(N, 0);
1530}
1531
Chris Lattner5f056bf2005-07-10 01:55:33 +00001532
1533SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1534 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1535 MVT::ValueType EVT) {
1536 std::vector<SDOperand> Ops;
1537 Ops.reserve(4);
1538 Ops.push_back(Chain);
1539 Ops.push_back(Ptr);
1540 Ops.push_back(SV);
1541 Ops.push_back(getValueType(EVT));
1542 std::vector<MVT::ValueType> VTs;
1543 VTs.reserve(2);
1544 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1545 return getNode(Opcode, VTs, Ops);
1546}
1547
Chris Lattnerc3aae252005-01-07 07:46:32 +00001548SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1549 SDOperand N1, SDOperand N2, SDOperand N3) {
1550 // Perform various simplifications.
1551 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1552 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1553 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1554 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001555 case ISD::SETCC: {
1556 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001557 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001558 if (Simp.Val) return Simp;
1559 break;
1560 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001561 case ISD::SELECT:
1562 if (N1C)
1563 if (N1C->getValue())
1564 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001565 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001566 return N3; // select false, X, Y -> Y
1567
1568 if (N2 == N3) return N2; // select C, X, X -> X
1569
1570 if (VT == MVT::i1) { // Boolean SELECT
1571 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001572 if (N2C->getValue()) // select C, 1, X -> C | X
1573 return getNode(ISD::OR, VT, N1, N3);
1574 else // select C, 0, X -> ~C & X
1575 return getNode(ISD::AND, VT,
1576 getNode(ISD::XOR, N1.getValueType(), N1,
1577 getConstant(1, N1.getValueType())), N3);
1578 } else if (N3C) {
1579 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1580 return getNode(ISD::OR, VT,
1581 getNode(ISD::XOR, N1.getValueType(), N1,
1582 getConstant(1, N1.getValueType())), N2);
1583 else // select C, X, 0 -> C & X
1584 return getNode(ISD::AND, VT, N1, N2);
1585 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001586
1587 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1588 return getNode(ISD::OR, VT, N1, N3);
1589 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1590 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001591 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001592 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001593 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1594 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001595 if (Simp.Val) return Simp;
1596 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001597 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001598 case ISD::BRCOND:
1599 if (N2C)
1600 if (N2C->getValue()) // Unconditional branch
1601 return getNode(ISD::BR, MVT::Other, N1, N3);
1602 else
1603 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001604 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001605 }
1606
Chris Lattner385328c2005-05-14 07:42:29 +00001607 std::vector<SDOperand> Ops;
1608 Ops.reserve(3);
1609 Ops.push_back(N1);
1610 Ops.push_back(N2);
1611 Ops.push_back(N3);
1612
1613 // Memoize nodes.
1614 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1615 if (N) return SDOperand(N, 0);
1616
1617 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001618 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001619 AllNodes.push_back(N);
1620 return SDOperand(N, 0);
1621}
1622
1623SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001624 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001625 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001626 std::vector<SDOperand> Ops;
1627 Ops.reserve(4);
1628 Ops.push_back(N1);
1629 Ops.push_back(N2);
1630 Ops.push_back(N3);
1631 Ops.push_back(N4);
1632 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001633}
1634
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001635SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1636 SDOperand N1, SDOperand N2, SDOperand N3,
1637 SDOperand N4, SDOperand N5) {
Nate Begemane5d63822005-08-11 01:12:20 +00001638 if (ISD::SELECT_CC == Opcode) {
1639 assert(N1.getValueType() == N2.getValueType() &&
1640 "LHS and RHS of condition must have same type!");
1641 assert(N3.getValueType() == N4.getValueType() &&
1642 "True and False arms of SelectCC must have same type!");
Nate Begemanff663682005-08-13 06:14:17 +00001643 assert(N3.getValueType() == VT &&
1644 "select_cc node must be of same type as true and false value!");
1645 SDOperand Simp = SimplifySelectCC(N1, N2, N3, N4,
1646 cast<CondCodeSDNode>(N5)->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001647 if (Simp.Val) return Simp;
Nate Begemane5d63822005-08-11 01:12:20 +00001648 }
1649
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001650 std::vector<SDOperand> Ops;
1651 Ops.reserve(5);
1652 Ops.push_back(N1);
1653 Ops.push_back(N2);
1654 Ops.push_back(N3);
1655 Ops.push_back(N4);
1656 Ops.push_back(N5);
1657 return getNode(Opcode, VT, Ops);
1658}
1659
1660
Chris Lattner0437cdd2005-05-09 04:14:13 +00001661SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001662 assert((!V || isa<PointerType>(V->getType())) &&
1663 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001664 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1665 if (N) return SDOperand(N, 0);
1666
1667 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001668 AllNodes.push_back(N);
1669 return SDOperand(N, 0);
1670}
1671
1672SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001673 std::vector<SDOperand> &Ops) {
1674 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001675 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001676 case 1: return getNode(Opcode, VT, Ops[0]);
1677 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1678 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001679 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001680 }
Chris Lattneref847df2005-04-09 03:27:28 +00001681
Chris Lattner89c34632005-05-14 06:20:26 +00001682 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001683 switch (Opcode) {
1684 default: break;
1685 case ISD::BRCONDTWOWAY:
1686 if (N1C)
1687 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001688 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001689 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001690 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001691 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001692 case ISD::BRTWOWAY_CC:
1693 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1694 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1695 "LHS and RHS of comparison must have same type!");
1696 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001697 case ISD::TRUNCSTORE: {
1698 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1699 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1700#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1701 // If this is a truncating store of a constant, convert to the desired type
1702 // and store it instead.
1703 if (isa<Constant>(Ops[0])) {
1704 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1705 if (isa<Constant>(Op))
1706 N1 = Op;
1707 }
1708 // Also for ConstantFP?
1709#endif
1710 if (Ops[0].getValueType() == EVT) // Normal store?
1711 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1712 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1713 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1714 "Can't do FP-INT conversion!");
1715 break;
1716 }
Chris Lattneref847df2005-04-09 03:27:28 +00001717 }
1718
Chris Lattner385328c2005-05-14 07:42:29 +00001719 // Memoize nodes.
1720 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1721 if (N) return SDOperand(N, 0);
1722 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001723 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001724 AllNodes.push_back(N);
1725 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001726}
1727
Chris Lattner89c34632005-05-14 06:20:26 +00001728SDOperand SelectionDAG::getNode(unsigned Opcode,
1729 std::vector<MVT::ValueType> &ResultTys,
1730 std::vector<SDOperand> &Ops) {
1731 if (ResultTys.size() == 1)
1732 return getNode(Opcode, ResultTys[0], Ops);
1733
Chris Lattner5f056bf2005-07-10 01:55:33 +00001734 switch (Opcode) {
1735 case ISD::EXTLOAD:
1736 case ISD::SEXTLOAD:
1737 case ISD::ZEXTLOAD: {
1738 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1739 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1740 // If they are asking for an extending load from/to the same thing, return a
1741 // normal load.
1742 if (ResultTys[0] == EVT)
1743 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1744 assert(EVT < ResultTys[0] &&
1745 "Should only be an extending load, not truncating!");
1746 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1747 "Cannot sign/zero extend a FP load!");
1748 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1749 "Cannot convert from FP to Int or Int -> FP!");
1750 break;
1751 }
1752
Chris Lattnere89083a2005-05-14 07:25:05 +00001753 // FIXME: figure out how to safely handle things like
1754 // int foo(int x) { return 1 << (x & 255); }
1755 // int bar() { return foo(256); }
1756#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001757 case ISD::SRA_PARTS:
1758 case ISD::SRL_PARTS:
1759 case ISD::SHL_PARTS:
1760 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001761 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001762 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1763 else if (N3.getOpcode() == ISD::AND)
1764 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1765 // If the and is only masking out bits that cannot effect the shift,
1766 // eliminate the and.
1767 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1768 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1769 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1770 }
1771 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001772#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001773 }
Chris Lattner89c34632005-05-14 06:20:26 +00001774
1775 // Memoize the node.
1776 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1777 Ops))];
1778 if (N) return SDOperand(N, 0);
1779 N = new SDNode(Opcode, Ops);
1780 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001781 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001782 return SDOperand(N, 0);
1783}
1784
Chris Lattner149c58c2005-08-16 18:17:10 +00001785
1786/// SelectNodeTo - These are used for target selectors to *mutate* the
1787/// specified node to have the specified return type, Target opcode, and
1788/// operands. Note that target opcodes are stored as
1789/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1790void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1791 unsigned TargetOpc, SDOperand Op1) {
1792 RemoveNodeFromCSEMaps(N);
1793 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1794 N->setValueTypes(VT);
1795 N->setOperands(Op1);
1796}
1797void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1798 unsigned TargetOpc, SDOperand Op1,
1799 SDOperand Op2) {
1800 RemoveNodeFromCSEMaps(N);
1801 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1802 N->setValueTypes(VT);
1803 N->setOperands(Op1, Op2);
1804}
1805void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1806 unsigned TargetOpc, SDOperand Op1,
1807 SDOperand Op2, SDOperand Op3) {
1808 RemoveNodeFromCSEMaps(N);
1809 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1810 N->setValueTypes(VT);
1811 N->setOperands(Op1, Op2, Op3);
1812}
Nate Begeman294a0a12005-08-18 07:30:15 +00001813void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1814 unsigned TargetOpc, SDOperand Op1,
1815 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1816 RemoveNodeFromCSEMaps(N);
1817 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1818 N->setValueTypes(VT);
1819 N->setOperands(Op1, Op2, Op3, Op4);
1820}
Chris Lattner149c58c2005-08-16 18:17:10 +00001821
Chris Lattner8b8749f2005-08-17 19:00:20 +00001822/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1823/// This can cause recursive merging of nodes in the DAG.
1824///
1825void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1826 assert(From != To && "Cannot replace uses of with self");
1827 while (!From->use_empty()) {
1828 // Process users until they are all gone.
1829 SDNode *U = *From->use_begin();
1830
1831 // This node is about to morph, remove its old self from the CSE maps.
1832 RemoveNodeFromCSEMaps(U);
1833
1834 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1835 if (U->getOperand(i).Val == From) {
1836 assert(From->getValueType(U->getOperand(i).ResNo) ==
1837 To->getValueType(U->getOperand(i).ResNo));
1838 From->removeUser(U);
1839 U->Operands[i].Val = To;
1840 To->addUser(U);
1841 }
1842
1843 // Now that we have modified U, add it back to the CSE maps. If it already
1844 // exists there, recursively merge the results together.
1845 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1846 ReplaceAllUsesWith(U, Existing);
1847 // U is now dead.
1848 }
1849}
1850
Jim Laskey58b968b2005-08-17 20:08:02 +00001851//===----------------------------------------------------------------------===//
1852// SDNode Class
1853//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001854
Chris Lattner5c884562005-01-12 18:37:47 +00001855/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1856/// indicated value. This method ignores uses of other values defined by this
1857/// operation.
1858bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1859 assert(Value < getNumValues() && "Bad value!");
1860
1861 // If there is only one value, this is easy.
1862 if (getNumValues() == 1)
1863 return use_size() == NUses;
1864 if (Uses.size() < NUses) return false;
1865
1866 SDOperand TheValue(this, Value);
1867
1868 std::set<SDNode*> UsersHandled;
1869
1870 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1871 UI != E; ++UI) {
1872 SDNode *User = *UI;
1873 if (User->getNumOperands() == 1 ||
1874 UsersHandled.insert(User).second) // First time we've seen this?
1875 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1876 if (User->getOperand(i) == TheValue) {
1877 if (NUses == 0)
1878 return false; // too many uses
1879 --NUses;
1880 }
1881 }
1882
1883 // Found exactly the right number of uses?
1884 return NUses == 0;
1885}
1886
1887
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001888const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001889 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001890 default:
1891 if (getOpcode() < ISD::BUILTIN_OP_END)
1892 return "<<Unknown DAG Node>>";
1893 else {
1894 if (G)
1895 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1896 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1897 return "<<Unknown Target Node>>";
1898 }
1899
Andrew Lenharth95762122005-03-31 21:24:06 +00001900 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001901 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001902 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001903 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001904 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001905 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001906 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001907 case ISD::ConstantFP: return "ConstantFP";
1908 case ISD::GlobalAddress: return "GlobalAddress";
1909 case ISD::FrameIndex: return "FrameIndex";
1910 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001911 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001912 case ISD::ExternalSymbol: return "ExternalSymbol";
1913 case ISD::ConstantPool: return "ConstantPoolIndex";
1914 case ISD::CopyToReg: return "CopyToReg";
1915 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001916 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001917 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001918
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001919 // Unary operators
1920 case ISD::FABS: return "fabs";
1921 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001922 case ISD::FSQRT: return "fsqrt";
1923 case ISD::FSIN: return "fsin";
1924 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001925
1926 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001927 case ISD::ADD: return "add";
1928 case ISD::SUB: return "sub";
1929 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001930 case ISD::MULHU: return "mulhu";
1931 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001932 case ISD::SDIV: return "sdiv";
1933 case ISD::UDIV: return "udiv";
1934 case ISD::SREM: return "srem";
1935 case ISD::UREM: return "urem";
1936 case ISD::AND: return "and";
1937 case ISD::OR: return "or";
1938 case ISD::XOR: return "xor";
1939 case ISD::SHL: return "shl";
1940 case ISD::SRA: return "sra";
1941 case ISD::SRL: return "srl";
1942
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001943 case ISD::SETCC: return "setcc";
1944 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001945 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001946 case ISD::ADD_PARTS: return "add_parts";
1947 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001948 case ISD::SHL_PARTS: return "shl_parts";
1949 case ISD::SRA_PARTS: return "sra_parts";
1950 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001951
Chris Lattner7f644642005-04-28 21:44:03 +00001952 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001953 case ISD::SIGN_EXTEND: return "sign_extend";
1954 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001955 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001956 case ISD::TRUNCATE: return "truncate";
1957 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001958 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001959 case ISD::FP_EXTEND: return "fp_extend";
1960
1961 case ISD::SINT_TO_FP: return "sint_to_fp";
1962 case ISD::UINT_TO_FP: return "uint_to_fp";
1963 case ISD::FP_TO_SINT: return "fp_to_sint";
1964 case ISD::FP_TO_UINT: return "fp_to_uint";
1965
1966 // Control flow instructions
1967 case ISD::BR: return "br";
1968 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001969 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001970 case ISD::BR_CC: return "br_cc";
1971 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001972 case ISD::RET: return "ret";
1973 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001974 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001975 case ISD::CALLSEQ_START: return "callseq_start";
1976 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001977
1978 // Other operators
1979 case ISD::LOAD: return "load";
1980 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001981 case ISD::EXTLOAD: return "extload";
1982 case ISD::SEXTLOAD: return "sextload";
1983 case ISD::ZEXTLOAD: return "zextload";
1984 case ISD::TRUNCSTORE: return "truncstore";
1985
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001986 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1987 case ISD::EXTRACT_ELEMENT: return "extract_element";
1988 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001989 case ISD::MEMSET: return "memset";
1990 case ISD::MEMCPY: return "memcpy";
1991 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001992
Chris Lattner276260b2005-05-11 04:50:30 +00001993 // Bit counting
1994 case ISD::CTPOP: return "ctpop";
1995 case ISD::CTTZ: return "cttz";
1996 case ISD::CTLZ: return "ctlz";
1997
1998 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001999 case ISD::READPORT: return "readport";
2000 case ISD::WRITEPORT: return "writeport";
2001 case ISD::READIO: return "readio";
2002 case ISD::WRITEIO: return "writeio";
2003
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002004 case ISD::CONDCODE:
2005 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002006 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002007 case ISD::SETOEQ: return "setoeq";
2008 case ISD::SETOGT: return "setogt";
2009 case ISD::SETOGE: return "setoge";
2010 case ISD::SETOLT: return "setolt";
2011 case ISD::SETOLE: return "setole";
2012 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002013
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002014 case ISD::SETO: return "seto";
2015 case ISD::SETUO: return "setuo";
2016 case ISD::SETUEQ: return "setue";
2017 case ISD::SETUGT: return "setugt";
2018 case ISD::SETUGE: return "setuge";
2019 case ISD::SETULT: return "setult";
2020 case ISD::SETULE: return "setule";
2021 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002022
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002023 case ISD::SETEQ: return "seteq";
2024 case ISD::SETGT: return "setgt";
2025 case ISD::SETGE: return "setge";
2026 case ISD::SETLT: return "setlt";
2027 case ISD::SETLE: return "setle";
2028 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002029 }
2030 }
2031}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002032
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002033void SDNode::dump() const { dump(0); }
2034void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002035 std::cerr << (void*)this << ": ";
2036
2037 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2038 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002039 if (getValueType(i) == MVT::Other)
2040 std::cerr << "ch";
2041 else
2042 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002043 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002044 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002045
2046 std::cerr << " ";
2047 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2048 if (i) std::cerr << ", ";
2049 std::cerr << (void*)getOperand(i).Val;
2050 if (unsigned RN = getOperand(i).ResNo)
2051 std::cerr << ":" << RN;
2052 }
2053
2054 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2055 std::cerr << "<" << CSDN->getValue() << ">";
2056 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2057 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002058 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002059 dyn_cast<GlobalAddressSDNode>(this)) {
2060 std::cerr << "<";
2061 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002062 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002063 std::cerr << "<" << FIDN->getIndex() << ">";
2064 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2065 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002066 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002067 std::cerr << "<";
2068 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2069 if (LBB)
2070 std::cerr << LBB->getName() << " ";
2071 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002072 } else if (const RegisterSDNode *C2V = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002073 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2074 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2075 } else {
2076 std::cerr << " #" << R->getReg();
2077 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002078 } else if (const ExternalSymbolSDNode *ES =
2079 dyn_cast<ExternalSymbolSDNode>(this)) {
2080 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002081 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2082 if (M->getValue())
2083 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2084 else
2085 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002086 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2087 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002088 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002089}
2090
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002091static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002092 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2093 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002094 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002095 else
2096 std::cerr << "\n" << std::string(indent+2, ' ')
2097 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002098
Chris Lattnerea946cd2005-01-09 20:38:33 +00002099
2100 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002101 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002102}
2103
Chris Lattnerc3aae252005-01-07 07:46:32 +00002104void SelectionDAG::dump() const {
2105 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002106 std::vector<SDNode*> Nodes(AllNodes);
2107 std::sort(Nodes.begin(), Nodes.end());
2108
2109 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002110 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002111 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002112 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002113
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002114 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002115
Chris Lattnerc3aae252005-01-07 07:46:32 +00002116 std::cerr << "\n\n";
2117}
2118