blob: 21f763a94627f27a5d1c80b2a3fd33a857b9769b [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000020#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000024#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000025#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000026#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000027#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner5cdcc582005-01-09 20:52:51 +000030static bool isCommutativeBinOp(unsigned Opcode) {
31 switch (Opcode) {
32 case ISD::ADD:
33 case ISD::MUL:
34 case ISD::AND:
35 case ISD::OR:
36 case ISD::XOR: return true;
37 default: return false; // FIXME: Need commutative info for user ops!
38 }
39}
40
41static bool isAssociativeBinOp(unsigned Opcode) {
42 switch (Opcode) {
43 case ISD::ADD:
44 case ISD::MUL:
45 case ISD::AND:
46 case ISD::OR:
47 case ISD::XOR: return true;
48 default: return false; // FIXME: Need associative info for user ops!
49 }
50}
51
Chris Lattner5cdcc582005-01-09 20:52:51 +000052// isInvertibleForFree - Return true if there is no cost to emitting the logical
53// inverse of this node.
54static bool isInvertibleForFree(SDOperand N) {
55 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000056 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000057 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000058 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000059}
60
Jim Laskey58b968b2005-08-17 20:08:02 +000061//===----------------------------------------------------------------------===//
62// ConstantFPSDNode Class
63//===----------------------------------------------------------------------===//
64
65/// isExactlyValue - We don't rely on operator== working on double values, as
66/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
67/// As such, this method can be used to do an exact bit-for-bit comparison of
68/// two floating point values.
69bool ConstantFPSDNode::isExactlyValue(double V) const {
70 return DoubleToBits(V) == DoubleToBits(Value);
71}
72
73//===----------------------------------------------------------------------===//
74// ISD Class
75//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000076
Chris Lattnerc3aae252005-01-07 07:46:32 +000077/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
78/// when given the operation for (X op Y).
79ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
80 // To perform this operation, we just need to swap the L and G bits of the
81 // operation.
82 unsigned OldL = (Operation >> 2) & 1;
83 unsigned OldG = (Operation >> 1) & 1;
84 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
85 (OldL << 1) | // New G bit
86 (OldG << 2)); // New L bit.
87}
88
89/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
90/// 'op' is a valid SetCC operation.
91ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
92 unsigned Operation = Op;
93 if (isInteger)
94 Operation ^= 7; // Flip L, G, E bits, but not U.
95 else
96 Operation ^= 15; // Flip all of the condition bits.
97 if (Operation > ISD::SETTRUE2)
98 Operation &= ~8; // Don't let N and U bits get set.
99 return ISD::CondCode(Operation);
100}
101
102
103/// isSignedOp - For an integer comparison, return 1 if the comparison is a
104/// signed operation and 2 if the result is an unsigned comparison. Return zero
105/// if the operation does not depend on the sign of the input (setne and seteq).
106static int isSignedOp(ISD::CondCode Opcode) {
107 switch (Opcode) {
108 default: assert(0 && "Illegal integer setcc operation!");
109 case ISD::SETEQ:
110 case ISD::SETNE: return 0;
111 case ISD::SETLT:
112 case ISD::SETLE:
113 case ISD::SETGT:
114 case ISD::SETGE: return 1;
115 case ISD::SETULT:
116 case ISD::SETULE:
117 case ISD::SETUGT:
118 case ISD::SETUGE: return 2;
119 }
120}
121
122/// getSetCCOrOperation - Return the result of a logical OR between different
123/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
124/// returns SETCC_INVALID if it is not possible to represent the resultant
125/// comparison.
126ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
127 bool isInteger) {
128 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
129 // Cannot fold a signed integer setcc with an unsigned integer setcc.
130 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131
Chris Lattnerc3aae252005-01-07 07:46:32 +0000132 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134 // If the N and U bits get set then the resultant comparison DOES suddenly
135 // care about orderedness, and is true when ordered.
136 if (Op > ISD::SETTRUE2)
137 Op &= ~16; // Clear the N bit.
138 return ISD::CondCode(Op);
139}
140
141/// getSetCCAndOperation - Return the result of a logical AND between different
142/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
143/// function returns zero if it is not possible to represent the resultant
144/// comparison.
145ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
146 bool isInteger) {
147 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
148 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000149 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000150
151 // Combine all of the condition bits.
152 return ISD::CondCode(Op1 & Op2);
153}
154
Chris Lattnerb48da392005-01-23 04:39:44 +0000155const TargetMachine &SelectionDAG::getTarget() const {
156 return TLI.getTargetMachine();
157}
158
Jim Laskey58b968b2005-08-17 20:08:02 +0000159//===----------------------------------------------------------------------===//
160// SelectionDAG Class
161//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000162
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000163/// RemoveDeadNodes - This method deletes all unreachable nodes in the
164/// SelectionDAG, including nodes (like loads) that have uses of their token
165/// chain but no other uses and no side effect. If a node is passed in as an
166/// argument, it is used as the seed for node deletion.
167void SelectionDAG::RemoveDeadNodes(SDNode *N) {
168 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
169
170 // Create a dummy node (which is not added to allnodes), that adds a reference
171 // to the root node, preventing it from being deleted.
172 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
173
Chris Lattner8b8749f2005-08-17 19:00:20 +0000174 // If we have a hint to start from, use it.
175 if (N) DeleteNodeIfDead(N, &AllNodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000176
177 Restart:
178 unsigned NumNodes = AllNodeSet.size();
179 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
180 I != E; ++I) {
181 // Try to delete this node.
182 DeleteNodeIfDead(*I, &AllNodeSet);
183
184 // If we actually deleted any nodes, do not use invalid iterators in
185 // AllNodeSet.
186 if (AllNodeSet.size() != NumNodes)
187 goto Restart;
188 }
189
190 // Restore AllNodes.
191 if (AllNodes.size() != NumNodes)
192 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
193
194 // If the root changed (e.g. it was a dead load, update the root).
195 setRoot(DummyNode->getOperand(0));
196
197 // Now that we are done with the dummy node, delete it.
198 DummyNode->getOperand(0).Val->removeUser(DummyNode);
199 delete DummyNode;
200}
201
Chris Lattner149c58c2005-08-16 18:17:10 +0000202
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000203void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
204 if (!N->use_empty())
205 return;
206
207 // Okay, we really are going to delete this node. First take this out of the
208 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000209 RemoveNodeFromCSEMaps(N);
210
211 // Next, brutally remove the operand list. This is safe to do, as there are
212 // no cycles in the graph.
213 while (!N->Operands.empty()) {
214 SDNode *O = N->Operands.back().Val;
215 N->Operands.pop_back();
216 O->removeUser(N);
217
218 // Now that we removed this operand, see if there are no uses of it left.
219 DeleteNodeIfDead(O, NodeSet);
220 }
221
222 // Remove the node from the nodes set and delete it.
223 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
224 AllNodeSet.erase(N);
225
226 // Now that the node is gone, check to see if any of the operands of this node
227 // are dead now.
228 delete N;
229}
230
231/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
232/// correspond to it. This is useful when we're about to delete or repurpose
233/// the node. We don't want future request for structurally identical nodes
234/// to return N anymore.
235void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000236 switch (N->getOpcode()) {
237 case ISD::Constant:
238 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
239 N->getValueType(0)));
240 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000241 case ISD::TargetConstant:
242 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
243 N->getValueType(0)));
244 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000245 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000246 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
247 ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000248 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000249 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000250 case ISD::CONDCODE:
251 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
252 "Cond code doesn't exist!");
253 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
254 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000255 case ISD::GlobalAddress:
256 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
257 break;
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000258 case ISD::TargetGlobalAddress:
259 TargetGlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
260 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 case ISD::FrameIndex:
262 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
263 break;
264 case ISD::ConstantPool:
265 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
266 break;
267 case ISD::BasicBlock:
268 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
269 break;
270 case ISD::ExternalSymbol:
271 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
272 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000273 case ISD::VALUETYPE:
274 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
275 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000276 case ISD::Register:
277 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
278 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000279 case ISD::SRCVALUE: {
280 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
281 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
282 break;
283 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000284 case ISD::LOAD:
285 Loads.erase(std::make_pair(N->getOperand(1),
286 std::make_pair(N->getOperand(0),
287 N->getValueType(0))));
288 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000289 default:
290 if (N->getNumOperands() == 1)
291 UnaryOps.erase(std::make_pair(N->getOpcode(),
292 std::make_pair(N->getOperand(0),
293 N->getValueType(0))));
294 else if (N->getNumOperands() == 2)
295 BinaryOps.erase(std::make_pair(N->getOpcode(),
296 std::make_pair(N->getOperand(0),
297 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000298 else if (N->getNumValues() == 1) {
299 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
300 OneResultNodes.erase(std::make_pair(N->getOpcode(),
301 std::make_pair(N->getValueType(0),
302 Ops)));
303 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000304 // Remove the node from the ArbitraryNodes map.
305 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
306 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
307 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
308 std::make_pair(RV, Ops)));
309 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000310 break;
311 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000312}
313
Chris Lattner8b8749f2005-08-17 19:00:20 +0000314/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
315/// has been taken out and modified in some way. If the specified node already
316/// exists in the CSE maps, do not modify the maps, but return the existing node
317/// instead. If it doesn't exist, add it and return null.
318///
319SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
320 assert(N->getNumOperands() && "This is a leaf node!");
321 if (N->getOpcode() == ISD::LOAD) {
322 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
323 std::make_pair(N->getOperand(0),
324 N->getValueType(0)))];
325 if (L) return L;
326 L = N;
327 } else if (N->getNumOperands() == 1) {
328 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
329 std::make_pair(N->getOperand(0),
330 N->getValueType(0)))];
331 if (U) return U;
332 U = N;
333 } else if (N->getNumOperands() == 2) {
334 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
335 std::make_pair(N->getOperand(0),
336 N->getOperand(1)))];
337 if (B) return B;
338 B = N;
339 } else if (N->getNumValues() == 1) {
340 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
341 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
342 std::make_pair(N->getValueType(0), Ops))];
343 if (ORN) return ORN;
344 ORN = N;
345 } else {
346 // Remove the node from the ArbitraryNodes map.
347 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
348 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
349 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
350 std::make_pair(RV, Ops))];
351 if (AN) return AN;
352 AN = N;
353 }
354 return 0;
355
356}
357
358
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000359
Chris Lattner78ec3112003-08-11 14:57:33 +0000360SelectionDAG::~SelectionDAG() {
361 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
362 delete AllNodes[i];
363}
364
Chris Lattner0f2287b2005-04-13 02:38:18 +0000365SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000366 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000367 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000368 return getNode(ISD::AND, Op.getValueType(), Op,
369 getConstant(Imm, Op.getValueType()));
370}
371
Chris Lattnerc3aae252005-01-07 07:46:32 +0000372SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
373 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
374 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000375 if (VT != MVT::i64)
376 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000377
Chris Lattnerc3aae252005-01-07 07:46:32 +0000378 SDNode *&N = Constants[std::make_pair(Val, VT)];
379 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000380 N = new ConstantSDNode(false, Val, VT);
381 AllNodes.push_back(N);
382 return SDOperand(N, 0);
383}
384
385SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
386 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
387 // Mask out any bits that are not valid for this constant.
388 if (VT != MVT::i64)
389 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
390
391 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
392 if (N) return SDOperand(N, 0);
393 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000394 AllNodes.push_back(N);
395 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000396}
397
Chris Lattnerc3aae252005-01-07 07:46:32 +0000398SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
399 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
400 if (VT == MVT::f32)
401 Val = (float)Val; // Mask out extra precision.
402
Chris Lattnerd8658612005-02-17 20:17:32 +0000403 // Do the map lookup using the actual bit pattern for the floating point
404 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
405 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000406 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000407 if (N) return SDOperand(N, 0);
408 N = new ConstantFPSDNode(Val, VT);
409 AllNodes.push_back(N);
410 return SDOperand(N, 0);
411}
412
413
414
415SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
416 MVT::ValueType VT) {
417 SDNode *&N = GlobalValues[GV];
418 if (N) return SDOperand(N, 0);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000419 N = new GlobalAddressSDNode(false, GV, VT);
420 AllNodes.push_back(N);
421 return SDOperand(N, 0);
422}
423
424SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
425 MVT::ValueType VT) {
426 SDNode *&N = TargetGlobalValues[GV];
427 if (N) return SDOperand(N, 0);
428 N = new GlobalAddressSDNode(true, GV, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000429 AllNodes.push_back(N);
430 return SDOperand(N, 0);
431}
432
433SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
434 SDNode *&N = FrameIndices[FI];
435 if (N) return SDOperand(N, 0);
436 N = new FrameIndexSDNode(FI, VT);
437 AllNodes.push_back(N);
438 return SDOperand(N, 0);
439}
440
441SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
442 SDNode *N = ConstantPoolIndices[CPIdx];
443 if (N) return SDOperand(N, 0);
444 N = new ConstantPoolSDNode(CPIdx, VT);
445 AllNodes.push_back(N);
446 return SDOperand(N, 0);
447}
448
449SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
450 SDNode *&N = BBNodes[MBB];
451 if (N) return SDOperand(N, 0);
452 N = new BasicBlockSDNode(MBB);
453 AllNodes.push_back(N);
454 return SDOperand(N, 0);
455}
456
Chris Lattner15e4b012005-07-10 00:07:11 +0000457SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
458 if ((unsigned)VT >= ValueTypeNodes.size())
459 ValueTypeNodes.resize(VT+1);
460 if (ValueTypeNodes[VT] == 0) {
461 ValueTypeNodes[VT] = new VTSDNode(VT);
462 AllNodes.push_back(ValueTypeNodes[VT]);
463 }
464
465 return SDOperand(ValueTypeNodes[VT], 0);
466}
467
Chris Lattnerc3aae252005-01-07 07:46:32 +0000468SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
469 SDNode *&N = ExternalSymbols[Sym];
470 if (N) return SDOperand(N, 0);
471 N = new ExternalSymbolSDNode(Sym, VT);
472 AllNodes.push_back(N);
473 return SDOperand(N, 0);
474}
475
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000476SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
477 if ((unsigned)Cond >= CondCodeNodes.size())
478 CondCodeNodes.resize(Cond+1);
479
Chris Lattner079a27a2005-08-09 20:40:02 +0000480 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000481 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000482 AllNodes.push_back(CondCodeNodes[Cond]);
483 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000484 return SDOperand(CondCodeNodes[Cond], 0);
485}
486
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000487SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
488 if (Reg >= RegNodes.size())
489 RegNodes.resize(Reg+1);
490 RegisterSDNode *&Result = RegNodes[Reg];
491 if (Result) {
492 assert(Result->getValueType(0) == VT &&
493 "Inconsistent value types for machine registers");
494 } else {
495 Result = new RegisterSDNode(Reg, VT);
496 AllNodes.push_back(Result);
497 }
498 return SDOperand(Result, 0);
499}
500
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000501SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
502 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000503 // These setcc operations always fold.
504 switch (Cond) {
505 default: break;
506 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000507 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000508 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000509 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510 }
511
Chris Lattner67255a12005-04-07 18:14:58 +0000512 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
513 uint64_t C2 = N2C->getValue();
514 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
515 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000516
Chris Lattnerc3aae252005-01-07 07:46:32 +0000517 // Sign extend the operands if required
518 if (ISD::isSignedIntSetCC(Cond)) {
519 C1 = N1C->getSignExtended();
520 C2 = N2C->getSignExtended();
521 }
522
523 switch (Cond) {
524 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000525 case ISD::SETEQ: return getConstant(C1 == C2, VT);
526 case ISD::SETNE: return getConstant(C1 != C2, VT);
527 case ISD::SETULT: return getConstant(C1 < C2, VT);
528 case ISD::SETUGT: return getConstant(C1 > C2, VT);
529 case ISD::SETULE: return getConstant(C1 <= C2, VT);
530 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
531 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
532 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
533 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
534 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000535 }
Chris Lattner24673922005-04-07 18:58:54 +0000536 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000537 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
538 // the comparison on the input.
539 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
540 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
541
542 // If the comparison constant has bits in the upper part, the
543 // zero-extended value could never match.
544 if (C2 & (~0ULL << InSize)) {
545 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
546 switch (Cond) {
547 case ISD::SETUGT:
548 case ISD::SETUGE:
549 case ISD::SETEQ: return getConstant(0, VT);
550 case ISD::SETULT:
551 case ISD::SETULE:
552 case ISD::SETNE: return getConstant(1, VT);
553 case ISD::SETGT:
554 case ISD::SETGE:
555 // True if the sign bit of C2 is set.
556 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
557 case ISD::SETLT:
558 case ISD::SETLE:
559 // True if the sign bit of C2 isn't set.
560 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
561 default:
562 break;
563 }
564 }
565
566 // Otherwise, we can perform the comparison with the low bits.
567 switch (Cond) {
568 case ISD::SETEQ:
569 case ISD::SETNE:
570 case ISD::SETUGT:
571 case ISD::SETUGE:
572 case ISD::SETULT:
573 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000574 return getSetCC(VT, N1.getOperand(0),
575 getConstant(C2, N1.getOperand(0).getValueType()),
576 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000577 default:
578 break; // todo, be more careful with signed comparisons
579 }
580 }
581
Chris Lattner67255a12005-04-07 18:14:58 +0000582 uint64_t MinVal, MaxVal;
583 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
584 if (ISD::isSignedIntSetCC(Cond)) {
585 MinVal = 1ULL << (OperandBitSize-1);
586 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
587 MaxVal = ~0ULL >> (65-OperandBitSize);
588 else
589 MaxVal = 0;
590 } else {
591 MinVal = 0;
592 MaxVal = ~0ULL >> (64-OperandBitSize);
593 }
594
595 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
596 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
597 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
598 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000599 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
600 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000601 }
602
603 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
604 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
605 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000606 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
607 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000608 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000609
Nate Begeman72ea2812005-04-14 08:56:52 +0000610 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
611 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000612
Nate Begeman72ea2812005-04-14 08:56:52 +0000613 // Canonicalize setgt X, Min --> setne X, Min
614 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000615 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000616
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000617 // If we have setult X, 1, turn it into seteq X, 0
618 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000619 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
620 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000621 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000622 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000623 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
624 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000625
626 // If we have "setcc X, C1", check to see if we can shrink the immediate
627 // by changing cc.
628
629 // SETUGT X, SINTMAX -> SETLT X, 0
630 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
631 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000632 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000633
634 // FIXME: Implement the rest of these.
635
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000636
637 // Fold bit comparisons when we can.
638 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
639 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
640 if (ConstantSDNode *AndRHS =
641 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
642 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
643 // Perform the xform if the AND RHS is a single bit.
644 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
645 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000646 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000647 TLI.getShiftAmountTy()));
648 }
649 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
650 // (X & 8) == 8 --> (X & 8) >> 3
651 // Perform the xform if C2 is a single bit.
652 if ((C2 & (C2-1)) == 0) {
653 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000654 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000655 }
656 }
657 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658 }
Chris Lattner67255a12005-04-07 18:14:58 +0000659 } else if (isa<ConstantSDNode>(N1.Val)) {
660 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000661 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000662 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000663
664 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
665 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
666 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000667
Chris Lattnerc3aae252005-01-07 07:46:32 +0000668 switch (Cond) {
669 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000670 case ISD::SETEQ: return getConstant(C1 == C2, VT);
671 case ISD::SETNE: return getConstant(C1 != C2, VT);
672 case ISD::SETLT: return getConstant(C1 < C2, VT);
673 case ISD::SETGT: return getConstant(C1 > C2, VT);
674 case ISD::SETLE: return getConstant(C1 <= C2, VT);
675 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 }
677 } else {
678 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000679 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000680 }
681
682 if (N1 == N2) {
683 // We can always fold X == Y for integer setcc's.
684 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000685 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000686 unsigned UOF = ISD::getUnorderedFlavor(Cond);
687 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000688 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000689 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000690 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000691 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
692 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000693 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
694 if (NewCond != Cond)
695 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 }
697
Chris Lattner5cdcc582005-01-09 20:52:51 +0000698 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000699 MVT::isInteger(N1.getValueType())) {
700 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
701 N1.getOpcode() == ISD::XOR) {
702 // Simplify (X+Y) == (X+Z) --> Y == Z
703 if (N1.getOpcode() == N2.getOpcode()) {
704 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000705 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000706 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000707 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000708 if (isCommutativeBinOp(N1.getOpcode())) {
709 // If X op Y == Y op X, try other combinations.
710 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000711 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000712 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000713 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000714 }
715 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000716
717 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000718
Chris Lattner68dc3102005-01-10 02:03:02 +0000719 // Simplify (X+Z) == X --> Z == 0
720 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000721 return getSetCC(VT, N1.getOperand(1),
722 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000723 if (N1.getOperand(1) == N2) {
724 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000725 return getSetCC(VT, N1.getOperand(0),
726 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000727 else {
728 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
729 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000730 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000731 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000732 N2, getConstant(1, TLI.getShiftAmountTy())),
733 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000734 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000735 }
736 }
737
Chris Lattner68dc3102005-01-10 02:03:02 +0000738 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
739 N2.getOpcode() == ISD::XOR) {
740 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000741 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000742 return getSetCC(VT, N2.getOperand(1),
743 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000744 } else if (N2.getOperand(1) == N1) {
745 if (isCommutativeBinOp(N2.getOpcode())) {
746 return getSetCC(VT, N2.getOperand(0),
747 getConstant(0, N2.getValueType()), Cond);
748 } else {
749 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
750 // X == (Z-X) --> X<<1 == Z
751 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
752 getConstant(1, TLI.getShiftAmountTy())),
753 N2.getOperand(0), Cond);
754 }
755 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000756 }
757 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000758
Chris Lattnerfda2b552005-04-18 04:48:12 +0000759 // Fold away ALL boolean setcc's.
760 if (N1.getValueType() == MVT::i1) {
761 switch (Cond) {
762 default: assert(0 && "Unknown integer setcc!");
763 case ISD::SETEQ: // X == Y -> (X^Y)^1
764 N1 = getNode(ISD::XOR, MVT::i1,
765 getNode(ISD::XOR, MVT::i1, N1, N2),
766 getConstant(1, MVT::i1));
767 break;
768 case ISD::SETNE: // X != Y --> (X^Y)
769 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
770 break;
771 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
772 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
773 N1 = getNode(ISD::AND, MVT::i1, N2,
774 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
775 break;
776 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
777 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
778 N1 = getNode(ISD::AND, MVT::i1, N1,
779 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
780 break;
781 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
782 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
783 N1 = getNode(ISD::OR, MVT::i1, N2,
784 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
785 break;
786 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
787 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
788 N1 = getNode(ISD::OR, MVT::i1, N1,
789 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
790 break;
791 }
792 if (VT != MVT::i1)
793 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
794 return N1;
795 }
796
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000797 // Could not fold it.
798 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000799}
800
Nate Begemanff663682005-08-13 06:14:17 +0000801SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
802 SDOperand N3, SDOperand N4,
803 ISD::CondCode CC) {
804 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000805 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
806 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
807 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
808
809 // Check to see if we can simplify the select into an fabs node
810 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
811 // Allow either -0.0 or 0.0
812 if (CFP->getValue() == 0.0) {
813 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
814 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
815 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
816 N1 == N4.getOperand(0))
817 return getNode(ISD::FABS, VT, N1);
818
819 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
820 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
821 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
822 N3.getOperand(0) == N4)
823 return getNode(ISD::FABS, VT, N4);
824 }
825 }
826
827 // Check to see if we can perform the "gzip trick", transforming
828 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
829 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
830 MVT::isInteger(N1.getValueType()) &&
831 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
832 MVT::ValueType XType = N1.getValueType();
833 MVT::ValueType AType = N3.getValueType();
834 if (XType >= AType) {
835 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
836 // single-bit constant. FIXME: remove once the dag combiner
837 // exists.
838 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
839 unsigned ShCtV = Log2_64(N3C->getValue());
840 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
841 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
842 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
843 if (XType > AType)
844 Shift = getNode(ISD::TRUNCATE, AType, Shift);
845 return getNode(ISD::AND, AType, Shift, N3);
846 }
847 SDOperand Shift = getNode(ISD::SRA, XType, N1,
848 getConstant(MVT::getSizeInBits(XType)-1,
849 TLI.getShiftAmountTy()));
850 if (XType > AType)
851 Shift = getNode(ISD::TRUNCATE, AType, Shift);
852 return getNode(ISD::AND, AType, Shift, N3);
853 }
854 }
855
Nate Begemancebd4332005-08-24 04:57:57 +0000856 // Check to see if this is the equivalent of setcc X, 0
857 if (N4C && N4C->isNullValue() && N3C && (N3C->getValue() == 1ULL)) {
Nate Begeman7042f152005-08-23 05:41:12 +0000858 MVT::ValueType XType = N1.getValueType();
859 if (TLI.getOperationAction(ISD::SETCC, TLI.getSetCCResultTy()) ==
860 TargetLowering::Legal) {
Nate Begemancebd4332005-08-24 04:57:57 +0000861 return getSetCC(TLI.getSetCCResultTy(), N1, N2, CC);
Nate Begeman7042f152005-08-23 05:41:12 +0000862 }
Nate Begemancebd4332005-08-24 04:57:57 +0000863 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
864 if (N2C && N2C->isNullValue() && CC == ISD::SETEQ &&
865 TLI.getOperationAction(ISD::CTLZ, XType) == TargetLowering::Legal) {
Nate Begeman7042f152005-08-23 05:41:12 +0000866 SDOperand Ctlz = getNode(ISD::CTLZ, XType, N1);
867 return getNode(ISD::SRL, XType, Ctlz,
Nate Begeman0750a402005-08-24 00:21:28 +0000868 getConstant(Log2_32(MVT::getSizeInBits(XType)),
Nate Begeman7042f152005-08-23 05:41:12 +0000869 TLI.getShiftAmountTy()));
870 }
Nate Begemancebd4332005-08-24 04:57:57 +0000871 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
872 if (N2C && N2C->isNullValue() && CC == ISD::SETGT) {
873 SDOperand NegN1 = getNode(ISD::SUB, XType, getConstant(0, XType), N1);
874 SDOperand NotN1 = getNode(ISD::XOR, XType, N1, getConstant(~0ULL, XType));
875 return getNode(ISD::SRL, XType, getNode(ISD::AND, XType, NegN1, NotN1),
876 getConstant(MVT::getSizeInBits(XType)-1,
877 TLI.getShiftAmountTy()));
878 }
879 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
880 if (N2C && N2C->isAllOnesValue() && CC == ISD::SETGT) {
881 SDOperand Sign = getNode(ISD::SRL, XType, N1,
882 getConstant(MVT::getSizeInBits(XType)-1,
883 TLI.getShiftAmountTy()));
884 return getNode(ISD::XOR, XType, Sign, getConstant(1, XType));
885 }
Nate Begeman7042f152005-08-23 05:41:12 +0000886 }
887
Nate Begeman32c392a2005-08-13 06:00:21 +0000888 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
889 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
890 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
891 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
892 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
893 MVT::ValueType XType = N1.getValueType();
894 if (SubC->isNullValue() && MVT::isInteger(XType)) {
895 SDOperand Shift = getNode(ISD::SRA, XType, N1,
896 getConstant(MVT::getSizeInBits(XType)-1,
897 TLI.getShiftAmountTy()));
898 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
899 Shift);
900 }
901 }
902 }
903
904 // Could not fold it.
905 return SDOperand();
906}
907
Chris Lattnerc3aae252005-01-07 07:46:32 +0000908/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000909///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
911 SDNode *N = new SDNode(Opcode, VT);
912 AllNodes.push_back(N);
913 return SDOperand(N, 0);
914}
915
Chris Lattnerc3aae252005-01-07 07:46:32 +0000916SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
917 SDOperand Operand) {
918 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
919 uint64_t Val = C->getValue();
920 switch (Opcode) {
921 default: break;
922 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
923 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
924 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000925 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
926 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000927 }
928 }
929
930 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
931 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000932 case ISD::FNEG:
933 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000934 case ISD::FP_ROUND:
935 case ISD::FP_EXTEND:
936 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000937 case ISD::FP_TO_SINT:
938 return getConstant((int64_t)C->getValue(), VT);
939 case ISD::FP_TO_UINT:
940 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000941 }
942
943 unsigned OpOpcode = Operand.Val->getOpcode();
944 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000945 case ISD::TokenFactor:
946 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 case ISD::SIGN_EXTEND:
948 if (Operand.getValueType() == VT) return Operand; // noop extension
949 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
950 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
951 break;
952 case ISD::ZERO_EXTEND:
953 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000954 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000955 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 break;
957 case ISD::TRUNCATE:
958 if (Operand.getValueType() == VT) return Operand; // noop truncate
959 if (OpOpcode == ISD::TRUNCATE)
960 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000961 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
962 // If the source is smaller than the dest, we still need an extend.
963 if (Operand.Val->getOperand(0).getValueType() < VT)
964 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
965 else if (Operand.Val->getOperand(0).getValueType() > VT)
966 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
967 else
968 return Operand.Val->getOperand(0);
969 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000970 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000971 case ISD::FNEG:
972 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
973 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
974 Operand.Val->getOperand(0));
975 if (OpOpcode == ISD::FNEG) // --X -> X
976 return Operand.Val->getOperand(0);
977 break;
978 case ISD::FABS:
979 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
980 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
981 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000982 }
983
984 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
985 if (N) return SDOperand(N, 0);
986 N = new SDNode(Opcode, Operand);
987 N->setValueTypes(VT);
988 AllNodes.push_back(N);
989 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000990}
991
Chris Lattner36019aa2005-04-18 03:48:41 +0000992/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
993/// this predicate to simplify operations downstream. V and Mask are known to
994/// be the same type.
995static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
996 const TargetLowering &TLI) {
997 unsigned SrcBits;
998 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000999
Chris Lattner36019aa2005-04-18 03:48:41 +00001000 // If we know the result of a setcc has the top bits zero, use this info.
1001 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +00001002 case ISD::Constant:
1003 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
1004
1005 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +00001006 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +00001007 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
1008
1009 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +00001010 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +00001011 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
1012 case ISD::ZERO_EXTEND:
1013 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
1014 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
1015
1016 case ISD::AND:
1017 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +00001018 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +00001019 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
1020
1021 // FALL THROUGH
1022 case ISD::OR:
1023 case ISD::XOR:
1024 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
1025 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
1026 case ISD::SELECT:
1027 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
1028 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +00001029
1030 case ISD::SRL:
1031 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
1032 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1033 uint64_t NewVal = Mask << ShAmt->getValue();
1034 SrcBits = MVT::getSizeInBits(Op.getValueType());
1035 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
1036 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1037 }
1038 return false;
1039 case ISD::SHL:
1040 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
1041 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1042 uint64_t NewVal = Mask >> ShAmt->getValue();
1043 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1044 }
1045 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +00001046 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001047 default: break;
1048 }
1049
1050 return false;
1051}
1052
1053
1054
Chris Lattnerc3aae252005-01-07 07:46:32 +00001055SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1056 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001057#ifndef NDEBUG
1058 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001059 case ISD::TokenFactor:
1060 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1061 N2.getValueType() == MVT::Other && "Invalid token factor!");
1062 break;
Chris Lattner76365122005-01-16 02:23:22 +00001063 case ISD::AND:
1064 case ISD::OR:
1065 case ISD::XOR:
1066 case ISD::UDIV:
1067 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001068 case ISD::MULHU:
1069 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001070 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1071 // fall through
1072 case ISD::ADD:
1073 case ISD::SUB:
1074 case ISD::MUL:
1075 case ISD::SDIV:
1076 case ISD::SREM:
1077 assert(N1.getValueType() == N2.getValueType() &&
1078 N1.getValueType() == VT && "Binary operator types must match!");
1079 break;
1080
1081 case ISD::SHL:
1082 case ISD::SRA:
1083 case ISD::SRL:
1084 assert(VT == N1.getValueType() &&
1085 "Shift operators return type must be the same as their first arg");
1086 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001087 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001088 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001089 case ISD::FP_ROUND_INREG: {
1090 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1091 assert(VT == N1.getValueType() && "Not an inreg round!");
1092 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1093 "Cannot FP_ROUND_INREG integer types");
1094 assert(EVT <= VT && "Not rounding down!");
1095 break;
1096 }
1097 case ISD::SIGN_EXTEND_INREG: {
1098 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1099 assert(VT == N1.getValueType() && "Not an inreg extend!");
1100 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1101 "Cannot *_EXTEND_INREG FP types");
1102 assert(EVT <= VT && "Not extending!");
1103 }
1104
Chris Lattner76365122005-01-16 02:23:22 +00001105 default: break;
1106 }
1107#endif
1108
Chris Lattnerc3aae252005-01-07 07:46:32 +00001109 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1110 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1111 if (N1C) {
1112 if (N2C) {
1113 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1114 switch (Opcode) {
1115 case ISD::ADD: return getConstant(C1 + C2, VT);
1116 case ISD::SUB: return getConstant(C1 - C2, VT);
1117 case ISD::MUL: return getConstant(C1 * C2, VT);
1118 case ISD::UDIV:
1119 if (C2) return getConstant(C1 / C2, VT);
1120 break;
1121 case ISD::UREM :
1122 if (C2) return getConstant(C1 % C2, VT);
1123 break;
1124 case ISD::SDIV :
1125 if (C2) return getConstant(N1C->getSignExtended() /
1126 N2C->getSignExtended(), VT);
1127 break;
1128 case ISD::SREM :
1129 if (C2) return getConstant(N1C->getSignExtended() %
1130 N2C->getSignExtended(), VT);
1131 break;
1132 case ISD::AND : return getConstant(C1 & C2, VT);
1133 case ISD::OR : return getConstant(C1 | C2, VT);
1134 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001135 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1136 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1137 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 default: break;
1139 }
1140
1141 } else { // Cannonicalize constant to RHS if commutative
1142 if (isCommutativeBinOp(Opcode)) {
1143 std::swap(N1C, N2C);
1144 std::swap(N1, N2);
1145 }
1146 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001147
1148 switch (Opcode) {
1149 default: break;
1150 case ISD::SHL: // shl 0, X -> 0
1151 if (N1C->isNullValue()) return N1;
1152 break;
1153 case ISD::SRL: // srl 0, X -> 0
1154 if (N1C->isNullValue()) return N1;
1155 break;
1156 case ISD::SRA: // sra -1, X -> -1
1157 if (N1C->isAllOnesValue()) return N1;
1158 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001159 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1160 // Extending a constant? Just return the extended constant.
1161 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1162 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001163 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001164 }
1165
1166 if (N2C) {
1167 uint64_t C2 = N2C->getValue();
1168
1169 switch (Opcode) {
1170 case ISD::ADD:
1171 if (!C2) return N1; // add X, 0 -> X
1172 break;
1173 case ISD::SUB:
1174 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001175 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001176 case ISD::MUL:
1177 if (!C2) return N2; // mul X, 0 -> 0
1178 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1179 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1180
Chris Lattnerb48da392005-01-23 04:39:44 +00001181 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001182 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001183 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001184 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001185 }
1186 break;
1187
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001188 case ISD::MULHU:
1189 case ISD::MULHS:
1190 if (!C2) return N2; // mul X, 0 -> 0
1191
1192 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1193 return getConstant(0, VT);
1194
1195 // Many others could be handled here, including -1, powers of 2, etc.
1196 break;
1197
Chris Lattnerc3aae252005-01-07 07:46:32 +00001198 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001199 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001200 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001201 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001202 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001203 }
1204 break;
1205
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001206 case ISD::SHL:
1207 case ISD::SRL:
1208 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001209 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001210 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001211 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1212 return getNode(ISD::UNDEF, N1.getValueType());
1213 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001214 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001215
1216 if (Opcode == ISD::SRA) {
1217 // If the sign bit is known to be zero, switch this to a SRL.
1218 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001219 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001220 TLI))
1221 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1222 } else {
1223 // If the part left over is known to be zero, the whole thing is zero.
1224 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1225 if (Opcode == ISD::SRL) {
1226 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1227 return getConstant(0, N1.getValueType());
1228 } else if (Opcode == ISD::SHL) {
1229 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1230 return getConstant(0, N1.getValueType());
1231 }
1232 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001233
1234 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1235 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1236 unsigned OpSAC = OpSA->getValue();
1237 if (N1.getOpcode() == ISD::SHL) {
1238 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1239 return getConstant(0, N1.getValueType());
1240 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1241 getConstant(C2+OpSAC, N2.getValueType()));
1242 } else if (N1.getOpcode() == ISD::SRL) {
1243 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1244 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1245 getConstant(~0ULL << OpSAC, VT));
1246 if (C2 > OpSAC) {
1247 return getNode(ISD::SHL, VT, Mask,
1248 getConstant(C2-OpSAC, N2.getValueType()));
1249 } else {
1250 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1251 return getNode(ISD::SRL, VT, Mask,
1252 getConstant(OpSAC-C2, N2.getValueType()));
1253 }
1254 } else if (N1.getOpcode() == ISD::SRA) {
1255 // if C1 == C2, just mask out low bits.
1256 if (C2 == OpSAC)
1257 return getNode(ISD::AND, VT, N1.getOperand(0),
1258 getConstant(~0ULL << C2, VT));
1259 }
1260 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001261 break;
1262
Chris Lattnerc3aae252005-01-07 07:46:32 +00001263 case ISD::AND:
1264 if (!C2) return N2; // X and 0 -> 0
1265 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001266 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001267
Chris Lattner36019aa2005-04-18 03:48:41 +00001268 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1269 return getConstant(0, VT);
1270
Chris Lattner588bbbf2005-04-21 06:28:15 +00001271 {
1272 uint64_t NotC2 = ~C2;
1273 if (VT != MVT::i64)
1274 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1275
1276 if (MaskedValueIsZero(N1, NotC2, TLI))
1277 return N1; // if (X & ~C2) -> 0, the and is redundant
1278 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001279
Chris Lattnerdea29e22005-04-10 01:13:15 +00001280 // FIXME: Should add a corresponding version of this for
1281 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1282 // we don't have yet.
1283
Chris Lattner0f2287b2005-04-13 02:38:18 +00001284 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1285 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001286 // If we are masking out the part of our input that was extended, just
1287 // mask the input to the extension directly.
1288 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001289 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001290 if ((C2 & (~0ULL << ExtendBits)) == 0)
1291 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001292 } else if (N1.getOpcode() == ISD::OR) {
1293 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1294 if ((ORI->getValue() & C2) == C2) {
1295 // If the 'or' is setting all of the bits that we are masking for,
1296 // we know the result of the AND will be the AND mask itself.
1297 return N2;
1298 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001299 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001300 break;
1301 case ISD::OR:
1302 if (!C2)return N1; // X or 0 -> X
1303 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001304 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001305 break;
1306 case ISD::XOR:
1307 if (!C2) return N1; // X xor 0 -> X
1308 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001309 if (N1.Val->getOpcode() == ISD::SETCC){
1310 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 // !(X op Y) -> (X !op Y)
1312 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001313 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1314 return getSetCC(SetCC->getValueType(0),
1315 SetCC->getOperand(0), SetCC->getOperand(1),
1316 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1318 SDNode *Op = N1.Val;
1319 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1320 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1321 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1322 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1323 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1324 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1325 if (Op->getOpcode() == ISD::AND)
1326 return getNode(ISD::OR, VT, LHS, RHS);
1327 return getNode(ISD::AND, VT, LHS, RHS);
1328 }
1329 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001330 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001331 }
1332 break;
1333 }
1334
1335 // Reassociate ((X op C1) op C2) if possible.
1336 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1337 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001338 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1340 }
1341
1342 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1343 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001344 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001345 if (N2CFP) {
1346 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1347 switch (Opcode) {
1348 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1349 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1350 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1351 case ISD::SDIV:
1352 if (C2) return getConstantFP(C1 / C2, VT);
1353 break;
1354 case ISD::SREM :
1355 if (C2) return getConstantFP(fmod(C1, C2), VT);
1356 break;
1357 default: break;
1358 }
1359
1360 } else { // Cannonicalize constant to RHS if commutative
1361 if (isCommutativeBinOp(Opcode)) {
1362 std::swap(N1CFP, N2CFP);
1363 std::swap(N1, N2);
1364 }
1365 }
1366
Chris Lattner15e4b012005-07-10 00:07:11 +00001367 if (Opcode == ISD::FP_ROUND_INREG)
1368 return getNode(ISD::FP_EXTEND, VT,
1369 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1370 }
1371
Chris Lattnerc3aae252005-01-07 07:46:32 +00001372 // Finally, fold operations that do not require constants.
1373 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001374 case ISD::TokenFactor:
1375 if (N1.getOpcode() == ISD::EntryToken)
1376 return N2;
1377 if (N2.getOpcode() == ISD::EntryToken)
1378 return N1;
1379 break;
1380
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 case ISD::AND:
1382 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001383 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1384 SDNode *LHS = N1.Val, *RHS = N2.Val;
1385 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1386 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1387 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1388 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001389
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001390 if (LR == RR && isa<ConstantSDNode>(LR) &&
1391 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1392 // (X != 0) | (Y != 0) -> (X|Y != 0)
1393 // (X == 0) & (Y == 0) -> (X|Y == 0)
1394 // (X < 0) | (Y < 0) -> (X|Y < 0)
1395 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1396 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1397 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1398 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1399 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1400 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001401
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001402 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1403 // (X == -1) & (Y == -1) -> (X&Y == -1)
1404 // (X != -1) | (Y != -1) -> (X&Y != -1)
1405 // (X > -1) | (Y > -1) -> (X&Y > -1)
1406 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1407 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1408 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1409 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1410 LR, Op2);
1411 // (X > -1) & (Y > -1) -> (X|Y > -1)
1412 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1413 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1414 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 }
1416 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001417
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001418 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1419 if (LL == RR && LR == RL) {
1420 Op2 = ISD::getSetCCSwappedOperands(Op2);
1421 goto MatchedBackwards;
1422 }
1423
1424 if (LL == RL && LR == RR) {
1425 MatchedBackwards:
1426 ISD::CondCode Result;
1427 bool isInteger = MVT::isInteger(LL.getValueType());
1428 if (Opcode == ISD::OR)
1429 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1430 else
1431 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1432
1433 if (Result != ISD::SETCC_INVALID)
1434 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1435 }
1436 }
1437
Chris Lattner7467c9b2005-04-18 04:11:19 +00001438 // and/or zext(a), zext(b) -> zext(and/or a, b)
1439 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1440 N2.getOpcode() == ISD::ZERO_EXTEND &&
1441 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1442 return getNode(ISD::ZERO_EXTEND, VT,
1443 getNode(Opcode, N1.getOperand(0).getValueType(),
1444 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001445 break;
1446 case ISD::XOR:
1447 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1448 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001449 case ISD::ADD:
1450 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1451 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1452 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1453 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001454 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1455 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1456 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1457 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1458 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1459 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001460 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1461 !MVT::isFloatingPoint(N2.getValueType()))
1462 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001463 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001464 case ISD::SUB:
1465 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001466 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001467 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001468 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001469 if (N1.Val->getOperand(1) == N2 &&
1470 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001471 return N1.Val->getOperand(0); // (A+B)-B == A
1472 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001473 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1474 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001475 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001476 case ISD::FP_ROUND_INREG:
1477 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1478 break;
1479 case ISD::SIGN_EXTEND_INREG: {
1480 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1481 if (EVT == VT) return N1; // Not actually extending
1482
1483 // If we are sign extending an extension, use the original source.
1484 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1485 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1486 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001487
Chris Lattner15e4b012005-07-10 00:07:11 +00001488 // If we are sign extending a sextload, return just the load.
1489 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001490 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001491 return N1;
1492
1493 // If we are extending the result of a setcc, and we already know the
1494 // contents of the top bits, eliminate the extension.
1495 if (N1.getOpcode() == ISD::SETCC &&
1496 TLI.getSetCCResultContents() ==
1497 TargetLowering::ZeroOrNegativeOneSetCCResult)
1498 return N1;
1499
1500 // If we are sign extending the result of an (and X, C) operation, and we
1501 // know the extended bits are zeros already, don't do the extend.
1502 if (N1.getOpcode() == ISD::AND)
1503 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1504 uint64_t Mask = N1C->getValue();
1505 unsigned NumBits = MVT::getSizeInBits(EVT);
1506 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1507 return N1;
1508 }
1509 break;
1510 }
1511
Nate Begemaneea805e2005-04-13 21:23:31 +00001512 // FIXME: figure out how to safely handle things like
1513 // int foo(int x) { return 1 << (x & 255); }
1514 // int bar() { return foo(256); }
1515#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001516 case ISD::SHL:
1517 case ISD::SRL:
1518 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001519 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001520 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001521 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001522 else if (N2.getOpcode() == ISD::AND)
1523 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1524 // If the and is only masking out bits that cannot effect the shift,
1525 // eliminate the and.
1526 unsigned NumBits = MVT::getSizeInBits(VT);
1527 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1528 return getNode(Opcode, VT, N1, N2.getOperand(0));
1529 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001530 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001531#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001532 }
1533
Chris Lattner27e9b412005-05-11 18:57:39 +00001534 // Memoize this node if possible.
1535 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001536 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001537 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1538 if (BON) return SDOperand(BON, 0);
1539
1540 BON = N = new SDNode(Opcode, N1, N2);
1541 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001542 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001543 }
1544
Chris Lattner3e011362005-05-14 07:45:46 +00001545 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001546 AllNodes.push_back(N);
1547 return SDOperand(N, 0);
1548}
1549
Chris Lattner88de6e72005-05-12 00:17:04 +00001550// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001551// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001552void SDNode::setAdjCallChain(SDOperand N) {
1553 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001554 assert((getOpcode() == ISD::CALLSEQ_START ||
1555 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001556
1557 Operands[0].Val->removeUser(this);
1558 Operands[0] = N;
1559 N.Val->Uses.push_back(this);
1560}
1561
1562
1563
Chris Lattnerc3aae252005-01-07 07:46:32 +00001564SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001565 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001566 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1568 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001569 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001570
1571 // Loads have a token chain.
1572 N->setValueTypes(VT, MVT::Other);
1573 AllNodes.push_back(N);
1574 return SDOperand(N, 0);
1575}
1576
Chris Lattner5f056bf2005-07-10 01:55:33 +00001577
1578SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1579 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1580 MVT::ValueType EVT) {
1581 std::vector<SDOperand> Ops;
1582 Ops.reserve(4);
1583 Ops.push_back(Chain);
1584 Ops.push_back(Ptr);
1585 Ops.push_back(SV);
1586 Ops.push_back(getValueType(EVT));
1587 std::vector<MVT::ValueType> VTs;
1588 VTs.reserve(2);
1589 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1590 return getNode(Opcode, VTs, Ops);
1591}
1592
Chris Lattnerc3aae252005-01-07 07:46:32 +00001593SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1594 SDOperand N1, SDOperand N2, SDOperand N3) {
1595 // Perform various simplifications.
1596 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1597 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1598 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1599 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001600 case ISD::SETCC: {
1601 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001602 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001603 if (Simp.Val) return Simp;
1604 break;
1605 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001606 case ISD::SELECT:
1607 if (N1C)
1608 if (N1C->getValue())
1609 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001610 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001611 return N3; // select false, X, Y -> Y
1612
1613 if (N2 == N3) return N2; // select C, X, X -> X
1614
1615 if (VT == MVT::i1) { // Boolean SELECT
1616 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001617 if (N2C->getValue()) // select C, 1, X -> C | X
1618 return getNode(ISD::OR, VT, N1, N3);
1619 else // select C, 0, X -> ~C & X
1620 return getNode(ISD::AND, VT,
1621 getNode(ISD::XOR, N1.getValueType(), N1,
1622 getConstant(1, N1.getValueType())), N3);
1623 } else if (N3C) {
1624 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1625 return getNode(ISD::OR, VT,
1626 getNode(ISD::XOR, N1.getValueType(), N1,
1627 getConstant(1, N1.getValueType())), N2);
1628 else // select C, X, 0 -> C & X
1629 return getNode(ISD::AND, VT, N1, N2);
1630 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001631
1632 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1633 return getNode(ISD::OR, VT, N1, N3);
1634 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1635 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001636 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001637 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001638 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1639 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001640 if (Simp.Val) return Simp;
1641 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001642 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001643 case ISD::BRCOND:
1644 if (N2C)
1645 if (N2C->getValue()) // Unconditional branch
1646 return getNode(ISD::BR, MVT::Other, N1, N3);
1647 else
1648 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001649 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001650 }
1651
Chris Lattner385328c2005-05-14 07:42:29 +00001652 std::vector<SDOperand> Ops;
1653 Ops.reserve(3);
1654 Ops.push_back(N1);
1655 Ops.push_back(N2);
1656 Ops.push_back(N3);
1657
1658 // Memoize nodes.
1659 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1660 if (N) return SDOperand(N, 0);
1661
1662 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001663 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001664 AllNodes.push_back(N);
1665 return SDOperand(N, 0);
1666}
1667
1668SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001669 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001670 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001671 std::vector<SDOperand> Ops;
1672 Ops.reserve(4);
1673 Ops.push_back(N1);
1674 Ops.push_back(N2);
1675 Ops.push_back(N3);
1676 Ops.push_back(N4);
1677 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001678}
1679
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001680SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1681 SDOperand N1, SDOperand N2, SDOperand N3,
1682 SDOperand N4, SDOperand N5) {
Nate Begemane5d63822005-08-11 01:12:20 +00001683 if (ISD::SELECT_CC == Opcode) {
1684 assert(N1.getValueType() == N2.getValueType() &&
1685 "LHS and RHS of condition must have same type!");
1686 assert(N3.getValueType() == N4.getValueType() &&
1687 "True and False arms of SelectCC must have same type!");
Nate Begemanff663682005-08-13 06:14:17 +00001688 assert(N3.getValueType() == VT &&
1689 "select_cc node must be of same type as true and false value!");
1690 SDOperand Simp = SimplifySelectCC(N1, N2, N3, N4,
1691 cast<CondCodeSDNode>(N5)->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001692 if (Simp.Val) return Simp;
Nate Begemane5d63822005-08-11 01:12:20 +00001693 }
1694
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001695 std::vector<SDOperand> Ops;
1696 Ops.reserve(5);
1697 Ops.push_back(N1);
1698 Ops.push_back(N2);
1699 Ops.push_back(N3);
1700 Ops.push_back(N4);
1701 Ops.push_back(N5);
1702 return getNode(Opcode, VT, Ops);
1703}
1704
1705
Chris Lattner0437cdd2005-05-09 04:14:13 +00001706SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001707 assert((!V || isa<PointerType>(V->getType())) &&
1708 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001709 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1710 if (N) return SDOperand(N, 0);
1711
1712 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001713 AllNodes.push_back(N);
1714 return SDOperand(N, 0);
1715}
1716
1717SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001718 std::vector<SDOperand> &Ops) {
1719 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001720 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001721 case 1: return getNode(Opcode, VT, Ops[0]);
1722 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1723 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001724 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001725 }
Chris Lattneref847df2005-04-09 03:27:28 +00001726
Chris Lattner89c34632005-05-14 06:20:26 +00001727 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001728 switch (Opcode) {
1729 default: break;
1730 case ISD::BRCONDTWOWAY:
1731 if (N1C)
1732 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001733 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001734 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001735 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001736 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001737 case ISD::BRTWOWAY_CC:
1738 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1739 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1740 "LHS and RHS of comparison must have same type!");
1741 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001742 case ISD::TRUNCSTORE: {
1743 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1744 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1745#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1746 // If this is a truncating store of a constant, convert to the desired type
1747 // and store it instead.
1748 if (isa<Constant>(Ops[0])) {
1749 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1750 if (isa<Constant>(Op))
1751 N1 = Op;
1752 }
1753 // Also for ConstantFP?
1754#endif
1755 if (Ops[0].getValueType() == EVT) // Normal store?
1756 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1757 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1758 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1759 "Can't do FP-INT conversion!");
1760 break;
1761 }
Chris Lattneref847df2005-04-09 03:27:28 +00001762 }
1763
Chris Lattner385328c2005-05-14 07:42:29 +00001764 // Memoize nodes.
1765 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1766 if (N) return SDOperand(N, 0);
1767 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001768 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001769 AllNodes.push_back(N);
1770 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001771}
1772
Chris Lattner89c34632005-05-14 06:20:26 +00001773SDOperand SelectionDAG::getNode(unsigned Opcode,
1774 std::vector<MVT::ValueType> &ResultTys,
1775 std::vector<SDOperand> &Ops) {
1776 if (ResultTys.size() == 1)
1777 return getNode(Opcode, ResultTys[0], Ops);
1778
Chris Lattner5f056bf2005-07-10 01:55:33 +00001779 switch (Opcode) {
1780 case ISD::EXTLOAD:
1781 case ISD::SEXTLOAD:
1782 case ISD::ZEXTLOAD: {
1783 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1784 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1785 // If they are asking for an extending load from/to the same thing, return a
1786 // normal load.
1787 if (ResultTys[0] == EVT)
1788 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1789 assert(EVT < ResultTys[0] &&
1790 "Should only be an extending load, not truncating!");
1791 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1792 "Cannot sign/zero extend a FP load!");
1793 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1794 "Cannot convert from FP to Int or Int -> FP!");
1795 break;
1796 }
1797
Chris Lattnere89083a2005-05-14 07:25:05 +00001798 // FIXME: figure out how to safely handle things like
1799 // int foo(int x) { return 1 << (x & 255); }
1800 // int bar() { return foo(256); }
1801#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001802 case ISD::SRA_PARTS:
1803 case ISD::SRL_PARTS:
1804 case ISD::SHL_PARTS:
1805 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001806 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001807 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1808 else if (N3.getOpcode() == ISD::AND)
1809 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1810 // If the and is only masking out bits that cannot effect the shift,
1811 // eliminate the and.
1812 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1813 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1814 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1815 }
1816 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001817#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001818 }
Chris Lattner89c34632005-05-14 06:20:26 +00001819
1820 // Memoize the node.
1821 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1822 Ops))];
1823 if (N) return SDOperand(N, 0);
1824 N = new SDNode(Opcode, Ops);
1825 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001826 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001827 return SDOperand(N, 0);
1828}
1829
Chris Lattner149c58c2005-08-16 18:17:10 +00001830
1831/// SelectNodeTo - These are used for target selectors to *mutate* the
1832/// specified node to have the specified return type, Target opcode, and
1833/// operands. Note that target opcodes are stored as
1834/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1835void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1836 unsigned TargetOpc, SDOperand Op1) {
1837 RemoveNodeFromCSEMaps(N);
1838 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1839 N->setValueTypes(VT);
1840 N->setOperands(Op1);
1841}
1842void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1843 unsigned TargetOpc, SDOperand Op1,
1844 SDOperand Op2) {
1845 RemoveNodeFromCSEMaps(N);
1846 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1847 N->setValueTypes(VT);
1848 N->setOperands(Op1, Op2);
1849}
Chris Lattner99badda2005-08-21 19:48:59 +00001850void SelectionDAG::SelectNodeTo(SDNode *N,
1851 MVT::ValueType VT1, MVT::ValueType VT2,
1852 unsigned TargetOpc, SDOperand Op1,
1853 SDOperand Op2) {
1854 RemoveNodeFromCSEMaps(N);
1855 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1856 N->setValueTypes(VT1, VT2);
1857 N->setOperands(Op1, Op2);
1858}
Chris Lattner149c58c2005-08-16 18:17:10 +00001859void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1860 unsigned TargetOpc, SDOperand Op1,
1861 SDOperand Op2, SDOperand Op3) {
1862 RemoveNodeFromCSEMaps(N);
1863 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1864 N->setValueTypes(VT);
1865 N->setOperands(Op1, Op2, Op3);
1866}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001867void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT1,
1868 MVT::ValueType VT2,
1869 unsigned TargetOpc, SDOperand Op1,
1870 SDOperand Op2, SDOperand Op3) {
1871 RemoveNodeFromCSEMaps(N);
1872 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1873 N->setValueTypes(VT1, VT2);
1874 N->setOperands(Op1, Op2, Op3);
1875}
1876
Nate Begeman294a0a12005-08-18 07:30:15 +00001877void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1878 unsigned TargetOpc, SDOperand Op1,
1879 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1880 RemoveNodeFromCSEMaps(N);
1881 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1882 N->setValueTypes(VT);
1883 N->setOperands(Op1, Op2, Op3, Op4);
1884}
Chris Lattner6b09a292005-08-21 18:49:33 +00001885void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1886 unsigned TargetOpc, SDOperand Op1,
1887 SDOperand Op2, SDOperand Op3, SDOperand Op4,
1888 SDOperand Op5) {
1889 RemoveNodeFromCSEMaps(N);
1890 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1891 N->setValueTypes(VT);
1892 N->setOperands(Op1, Op2, Op3, Op4, Op5);
1893}
Chris Lattner149c58c2005-08-16 18:17:10 +00001894
Chris Lattner8b8749f2005-08-17 19:00:20 +00001895/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1896/// This can cause recursive merging of nodes in the DAG.
1897///
1898void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1899 assert(From != To && "Cannot replace uses of with self");
1900 while (!From->use_empty()) {
1901 // Process users until they are all gone.
1902 SDNode *U = *From->use_begin();
1903
1904 // This node is about to morph, remove its old self from the CSE maps.
1905 RemoveNodeFromCSEMaps(U);
1906
1907 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1908 if (U->getOperand(i).Val == From) {
1909 assert(From->getValueType(U->getOperand(i).ResNo) ==
1910 To->getValueType(U->getOperand(i).ResNo));
1911 From->removeUser(U);
1912 U->Operands[i].Val = To;
1913 To->addUser(U);
1914 }
1915
1916 // Now that we have modified U, add it back to the CSE maps. If it already
1917 // exists there, recursively merge the results together.
1918 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1919 ReplaceAllUsesWith(U, Existing);
1920 // U is now dead.
1921 }
1922}
1923
Jim Laskey58b968b2005-08-17 20:08:02 +00001924//===----------------------------------------------------------------------===//
1925// SDNode Class
1926//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001927
Chris Lattner5c884562005-01-12 18:37:47 +00001928/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1929/// indicated value. This method ignores uses of other values defined by this
1930/// operation.
1931bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1932 assert(Value < getNumValues() && "Bad value!");
1933
1934 // If there is only one value, this is easy.
1935 if (getNumValues() == 1)
1936 return use_size() == NUses;
1937 if (Uses.size() < NUses) return false;
1938
1939 SDOperand TheValue(this, Value);
1940
1941 std::set<SDNode*> UsersHandled;
1942
1943 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1944 UI != E; ++UI) {
1945 SDNode *User = *UI;
1946 if (User->getNumOperands() == 1 ||
1947 UsersHandled.insert(User).second) // First time we've seen this?
1948 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1949 if (User->getOperand(i) == TheValue) {
1950 if (NUses == 0)
1951 return false; // too many uses
1952 --NUses;
1953 }
1954 }
1955
1956 // Found exactly the right number of uses?
1957 return NUses == 0;
1958}
1959
1960
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001961const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001962 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001963 default:
1964 if (getOpcode() < ISD::BUILTIN_OP_END)
1965 return "<<Unknown DAG Node>>";
1966 else {
1967 if (G)
1968 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1969 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1970 return "<<Unknown Target Node>>";
1971 }
1972
Andrew Lenharth95762122005-03-31 21:24:06 +00001973 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001974 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001975 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001976 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001977 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001978 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001979 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001980 case ISD::ConstantFP: return "ConstantFP";
1981 case ISD::GlobalAddress: return "GlobalAddress";
Chris Lattneraaaa0b62005-08-19 22:31:04 +00001982 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001983 case ISD::FrameIndex: return "FrameIndex";
1984 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001985 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001986 case ISD::ExternalSymbol: return "ExternalSymbol";
1987 case ISD::ConstantPool: return "ConstantPoolIndex";
1988 case ISD::CopyToReg: return "CopyToReg";
1989 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001990 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001991 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001992
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001993 // Unary operators
1994 case ISD::FABS: return "fabs";
1995 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001996 case ISD::FSQRT: return "fsqrt";
1997 case ISD::FSIN: return "fsin";
1998 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001999
2000 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002001 case ISD::ADD: return "add";
2002 case ISD::SUB: return "sub";
2003 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002004 case ISD::MULHU: return "mulhu";
2005 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002006 case ISD::SDIV: return "sdiv";
2007 case ISD::UDIV: return "udiv";
2008 case ISD::SREM: return "srem";
2009 case ISD::UREM: return "urem";
2010 case ISD::AND: return "and";
2011 case ISD::OR: return "or";
2012 case ISD::XOR: return "xor";
2013 case ISD::SHL: return "shl";
2014 case ISD::SRA: return "sra";
2015 case ISD::SRL: return "srl";
2016
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002017 case ISD::SETCC: return "setcc";
2018 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002019 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00002020 case ISD::ADD_PARTS: return "add_parts";
2021 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00002022 case ISD::SHL_PARTS: return "shl_parts";
2023 case ISD::SRA_PARTS: return "sra_parts";
2024 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002025
Chris Lattner7f644642005-04-28 21:44:03 +00002026 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002027 case ISD::SIGN_EXTEND: return "sign_extend";
2028 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002029 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002030 case ISD::TRUNCATE: return "truncate";
2031 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002032 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002033 case ISD::FP_EXTEND: return "fp_extend";
2034
2035 case ISD::SINT_TO_FP: return "sint_to_fp";
2036 case ISD::UINT_TO_FP: return "uint_to_fp";
2037 case ISD::FP_TO_SINT: return "fp_to_sint";
2038 case ISD::FP_TO_UINT: return "fp_to_uint";
2039
2040 // Control flow instructions
2041 case ISD::BR: return "br";
2042 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00002043 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00002044 case ISD::BR_CC: return "br_cc";
2045 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002046 case ISD::RET: return "ret";
2047 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00002048 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00002049 case ISD::CALLSEQ_START: return "callseq_start";
2050 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002051
2052 // Other operators
2053 case ISD::LOAD: return "load";
2054 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00002055 case ISD::EXTLOAD: return "extload";
2056 case ISD::SEXTLOAD: return "sextload";
2057 case ISD::ZEXTLOAD: return "zextload";
2058 case ISD::TRUNCSTORE: return "truncstore";
2059
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002060 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
2061 case ISD::EXTRACT_ELEMENT: return "extract_element";
2062 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00002063 case ISD::MEMSET: return "memset";
2064 case ISD::MEMCPY: return "memcpy";
2065 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002066
Chris Lattner276260b2005-05-11 04:50:30 +00002067 // Bit counting
2068 case ISD::CTPOP: return "ctpop";
2069 case ISD::CTTZ: return "cttz";
2070 case ISD::CTLZ: return "ctlz";
2071
2072 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002073 case ISD::READPORT: return "readport";
2074 case ISD::WRITEPORT: return "writeport";
2075 case ISD::READIO: return "readio";
2076 case ISD::WRITEIO: return "writeio";
2077
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002078 case ISD::CONDCODE:
2079 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002080 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002081 case ISD::SETOEQ: return "setoeq";
2082 case ISD::SETOGT: return "setogt";
2083 case ISD::SETOGE: return "setoge";
2084 case ISD::SETOLT: return "setolt";
2085 case ISD::SETOLE: return "setole";
2086 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002087
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002088 case ISD::SETO: return "seto";
2089 case ISD::SETUO: return "setuo";
2090 case ISD::SETUEQ: return "setue";
2091 case ISD::SETUGT: return "setugt";
2092 case ISD::SETUGE: return "setuge";
2093 case ISD::SETULT: return "setult";
2094 case ISD::SETULE: return "setule";
2095 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002096
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002097 case ISD::SETEQ: return "seteq";
2098 case ISD::SETGT: return "setgt";
2099 case ISD::SETGE: return "setge";
2100 case ISD::SETLT: return "setlt";
2101 case ISD::SETLE: return "setle";
2102 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002103 }
2104 }
2105}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002106
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002107void SDNode::dump() const { dump(0); }
2108void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002109 std::cerr << (void*)this << ": ";
2110
2111 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2112 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002113 if (getValueType(i) == MVT::Other)
2114 std::cerr << "ch";
2115 else
2116 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002117 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002118 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002119
2120 std::cerr << " ";
2121 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2122 if (i) std::cerr << ", ";
2123 std::cerr << (void*)getOperand(i).Val;
2124 if (unsigned RN = getOperand(i).ResNo)
2125 std::cerr << ":" << RN;
2126 }
2127
2128 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2129 std::cerr << "<" << CSDN->getValue() << ">";
2130 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2131 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002132 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002133 dyn_cast<GlobalAddressSDNode>(this)) {
2134 std::cerr << "<";
2135 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002136 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002137 std::cerr << "<" << FIDN->getIndex() << ">";
2138 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2139 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002140 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002141 std::cerr << "<";
2142 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2143 if (LBB)
2144 std::cerr << LBB->getName() << " ";
2145 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002146 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002147 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2148 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2149 } else {
2150 std::cerr << " #" << R->getReg();
2151 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002152 } else if (const ExternalSymbolSDNode *ES =
2153 dyn_cast<ExternalSymbolSDNode>(this)) {
2154 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002155 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2156 if (M->getValue())
2157 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2158 else
2159 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002160 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2161 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002163}
2164
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002165static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002166 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2167 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002168 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002169 else
2170 std::cerr << "\n" << std::string(indent+2, ' ')
2171 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002172
Chris Lattnerea946cd2005-01-09 20:38:33 +00002173
2174 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002175 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002176}
2177
Chris Lattnerc3aae252005-01-07 07:46:32 +00002178void SelectionDAG::dump() const {
2179 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002180 std::vector<SDNode*> Nodes(AllNodes);
2181 std::sort(Nodes.begin(), Nodes.end());
2182
2183 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002184 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002185 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002186 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002187
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002188 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002189
Chris Lattnerc3aae252005-01-07 07:46:32 +00002190 std::cerr << "\n\n";
2191}
2192