blob: fdefa93028592274e48a41ca71f839be47b64ae8 [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;
258 case ISD::FrameIndex:
259 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
260 break;
261 case ISD::ConstantPool:
262 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
263 break;
264 case ISD::BasicBlock:
265 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
266 break;
267 case ISD::ExternalSymbol:
268 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
269 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000270 case ISD::VALUETYPE:
271 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
272 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000273 case ISD::Register:
274 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
275 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000276 case ISD::SRCVALUE: {
277 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
278 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
279 break;
280 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000281 case ISD::LOAD:
282 Loads.erase(std::make_pair(N->getOperand(1),
283 std::make_pair(N->getOperand(0),
284 N->getValueType(0))));
285 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000286 default:
287 if (N->getNumOperands() == 1)
288 UnaryOps.erase(std::make_pair(N->getOpcode(),
289 std::make_pair(N->getOperand(0),
290 N->getValueType(0))));
291 else if (N->getNumOperands() == 2)
292 BinaryOps.erase(std::make_pair(N->getOpcode(),
293 std::make_pair(N->getOperand(0),
294 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000295 else if (N->getNumValues() == 1) {
296 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
297 OneResultNodes.erase(std::make_pair(N->getOpcode(),
298 std::make_pair(N->getValueType(0),
299 Ops)));
300 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000301 // Remove the node from the ArbitraryNodes map.
302 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
303 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
304 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
305 std::make_pair(RV, Ops)));
306 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000307 break;
308 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000309}
310
Chris Lattner8b8749f2005-08-17 19:00:20 +0000311/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
312/// has been taken out and modified in some way. If the specified node already
313/// exists in the CSE maps, do not modify the maps, but return the existing node
314/// instead. If it doesn't exist, add it and return null.
315///
316SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
317 assert(N->getNumOperands() && "This is a leaf node!");
318 if (N->getOpcode() == ISD::LOAD) {
319 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
320 std::make_pair(N->getOperand(0),
321 N->getValueType(0)))];
322 if (L) return L;
323 L = N;
324 } else if (N->getNumOperands() == 1) {
325 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
326 std::make_pair(N->getOperand(0),
327 N->getValueType(0)))];
328 if (U) return U;
329 U = N;
330 } else if (N->getNumOperands() == 2) {
331 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
332 std::make_pair(N->getOperand(0),
333 N->getOperand(1)))];
334 if (B) return B;
335 B = N;
336 } else if (N->getNumValues() == 1) {
337 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
338 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
339 std::make_pair(N->getValueType(0), Ops))];
340 if (ORN) return ORN;
341 ORN = N;
342 } else {
343 // Remove the node from the ArbitraryNodes map.
344 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
345 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
346 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
347 std::make_pair(RV, Ops))];
348 if (AN) return AN;
349 AN = N;
350 }
351 return 0;
352
353}
354
355
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000356
Chris Lattner78ec3112003-08-11 14:57:33 +0000357SelectionDAG::~SelectionDAG() {
358 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
359 delete AllNodes[i];
360}
361
Chris Lattner0f2287b2005-04-13 02:38:18 +0000362SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000363 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000364 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000365 return getNode(ISD::AND, Op.getValueType(), Op,
366 getConstant(Imm, Op.getValueType()));
367}
368
Chris Lattnerc3aae252005-01-07 07:46:32 +0000369SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
370 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
371 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000372 if (VT != MVT::i64)
373 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000374
Chris Lattnerc3aae252005-01-07 07:46:32 +0000375 SDNode *&N = Constants[std::make_pair(Val, VT)];
376 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000377 N = new ConstantSDNode(false, Val, VT);
378 AllNodes.push_back(N);
379 return SDOperand(N, 0);
380}
381
382SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
383 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
384 // Mask out any bits that are not valid for this constant.
385 if (VT != MVT::i64)
386 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
387
388 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
389 if (N) return SDOperand(N, 0);
390 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000391 AllNodes.push_back(N);
392 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000393}
394
Chris Lattnerc3aae252005-01-07 07:46:32 +0000395SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
396 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
397 if (VT == MVT::f32)
398 Val = (float)Val; // Mask out extra precision.
399
Chris Lattnerd8658612005-02-17 20:17:32 +0000400 // Do the map lookup using the actual bit pattern for the floating point
401 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
402 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000403 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000404 if (N) return SDOperand(N, 0);
405 N = new ConstantFPSDNode(Val, VT);
406 AllNodes.push_back(N);
407 return SDOperand(N, 0);
408}
409
410
411
412SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
413 MVT::ValueType VT) {
414 SDNode *&N = GlobalValues[GV];
415 if (N) return SDOperand(N, 0);
416 N = new GlobalAddressSDNode(GV,VT);
417 AllNodes.push_back(N);
418 return SDOperand(N, 0);
419}
420
421SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
422 SDNode *&N = FrameIndices[FI];
423 if (N) return SDOperand(N, 0);
424 N = new FrameIndexSDNode(FI, VT);
425 AllNodes.push_back(N);
426 return SDOperand(N, 0);
427}
428
429SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
430 SDNode *N = ConstantPoolIndices[CPIdx];
431 if (N) return SDOperand(N, 0);
432 N = new ConstantPoolSDNode(CPIdx, VT);
433 AllNodes.push_back(N);
434 return SDOperand(N, 0);
435}
436
437SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
438 SDNode *&N = BBNodes[MBB];
439 if (N) return SDOperand(N, 0);
440 N = new BasicBlockSDNode(MBB);
441 AllNodes.push_back(N);
442 return SDOperand(N, 0);
443}
444
Chris Lattner15e4b012005-07-10 00:07:11 +0000445SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
446 if ((unsigned)VT >= ValueTypeNodes.size())
447 ValueTypeNodes.resize(VT+1);
448 if (ValueTypeNodes[VT] == 0) {
449 ValueTypeNodes[VT] = new VTSDNode(VT);
450 AllNodes.push_back(ValueTypeNodes[VT]);
451 }
452
453 return SDOperand(ValueTypeNodes[VT], 0);
454}
455
Chris Lattnerc3aae252005-01-07 07:46:32 +0000456SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
457 SDNode *&N = ExternalSymbols[Sym];
458 if (N) return SDOperand(N, 0);
459 N = new ExternalSymbolSDNode(Sym, VT);
460 AllNodes.push_back(N);
461 return SDOperand(N, 0);
462}
463
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000464SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
465 if ((unsigned)Cond >= CondCodeNodes.size())
466 CondCodeNodes.resize(Cond+1);
467
Chris Lattner079a27a2005-08-09 20:40:02 +0000468 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000469 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000470 AllNodes.push_back(CondCodeNodes[Cond]);
471 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000472 return SDOperand(CondCodeNodes[Cond], 0);
473}
474
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000475SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
476 if (Reg >= RegNodes.size())
477 RegNodes.resize(Reg+1);
478 RegisterSDNode *&Result = RegNodes[Reg];
479 if (Result) {
480 assert(Result->getValueType(0) == VT &&
481 "Inconsistent value types for machine registers");
482 } else {
483 Result = new RegisterSDNode(Reg, VT);
484 AllNodes.push_back(Result);
485 }
486 return SDOperand(Result, 0);
487}
488
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000489SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
490 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000491 // These setcc operations always fold.
492 switch (Cond) {
493 default: break;
494 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000495 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000496 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000497 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000498 }
499
Chris Lattner67255a12005-04-07 18:14:58 +0000500 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
501 uint64_t C2 = N2C->getValue();
502 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
503 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504
Chris Lattnerc3aae252005-01-07 07:46:32 +0000505 // Sign extend the operands if required
506 if (ISD::isSignedIntSetCC(Cond)) {
507 C1 = N1C->getSignExtended();
508 C2 = N2C->getSignExtended();
509 }
510
511 switch (Cond) {
512 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000513 case ISD::SETEQ: return getConstant(C1 == C2, VT);
514 case ISD::SETNE: return getConstant(C1 != C2, VT);
515 case ISD::SETULT: return getConstant(C1 < C2, VT);
516 case ISD::SETUGT: return getConstant(C1 > C2, VT);
517 case ISD::SETULE: return getConstant(C1 <= C2, VT);
518 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
519 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
520 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
521 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
522 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000523 }
Chris Lattner24673922005-04-07 18:58:54 +0000524 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000525 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
526 // the comparison on the input.
527 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
528 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
529
530 // If the comparison constant has bits in the upper part, the
531 // zero-extended value could never match.
532 if (C2 & (~0ULL << InSize)) {
533 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
534 switch (Cond) {
535 case ISD::SETUGT:
536 case ISD::SETUGE:
537 case ISD::SETEQ: return getConstant(0, VT);
538 case ISD::SETULT:
539 case ISD::SETULE:
540 case ISD::SETNE: return getConstant(1, VT);
541 case ISD::SETGT:
542 case ISD::SETGE:
543 // True if the sign bit of C2 is set.
544 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
545 case ISD::SETLT:
546 case ISD::SETLE:
547 // True if the sign bit of C2 isn't set.
548 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
549 default:
550 break;
551 }
552 }
553
554 // Otherwise, we can perform the comparison with the low bits.
555 switch (Cond) {
556 case ISD::SETEQ:
557 case ISD::SETNE:
558 case ISD::SETUGT:
559 case ISD::SETUGE:
560 case ISD::SETULT:
561 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000562 return getSetCC(VT, N1.getOperand(0),
563 getConstant(C2, N1.getOperand(0).getValueType()),
564 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000565 default:
566 break; // todo, be more careful with signed comparisons
567 }
568 }
569
Chris Lattner67255a12005-04-07 18:14:58 +0000570 uint64_t MinVal, MaxVal;
571 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
572 if (ISD::isSignedIntSetCC(Cond)) {
573 MinVal = 1ULL << (OperandBitSize-1);
574 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
575 MaxVal = ~0ULL >> (65-OperandBitSize);
576 else
577 MaxVal = 0;
578 } else {
579 MinVal = 0;
580 MaxVal = ~0ULL >> (64-OperandBitSize);
581 }
582
583 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
584 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
585 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
586 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000587 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
588 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000589 }
590
591 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
592 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
593 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000594 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
595 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000596 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000597
Nate Begeman72ea2812005-04-14 08:56:52 +0000598 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
599 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000600
Nate Begeman72ea2812005-04-14 08:56:52 +0000601 // Canonicalize setgt X, Min --> setne X, Min
602 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000603 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000604
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000605 // If we have setult X, 1, turn it into seteq X, 0
606 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000607 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
608 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000609 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000610 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000611 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
612 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000613
614 // If we have "setcc X, C1", check to see if we can shrink the immediate
615 // by changing cc.
616
617 // SETUGT X, SINTMAX -> SETLT X, 0
618 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
619 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000620 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000621
622 // FIXME: Implement the rest of these.
623
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000624
625 // Fold bit comparisons when we can.
626 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
627 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
628 if (ConstantSDNode *AndRHS =
629 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
630 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
631 // Perform the xform if the AND RHS is a single bit.
632 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
633 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000634 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000635 TLI.getShiftAmountTy()));
636 }
637 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
638 // (X & 8) == 8 --> (X & 8) >> 3
639 // Perform the xform if C2 is a single bit.
640 if ((C2 & (C2-1)) == 0) {
641 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000642 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000643 }
644 }
645 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000646 }
Chris Lattner67255a12005-04-07 18:14:58 +0000647 } else if (isa<ConstantSDNode>(N1.Val)) {
648 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000649 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000650 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000651
652 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
653 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
654 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000655
Chris Lattnerc3aae252005-01-07 07:46:32 +0000656 switch (Cond) {
657 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000658 case ISD::SETEQ: return getConstant(C1 == C2, VT);
659 case ISD::SETNE: return getConstant(C1 != C2, VT);
660 case ISD::SETLT: return getConstant(C1 < C2, VT);
661 case ISD::SETGT: return getConstant(C1 > C2, VT);
662 case ISD::SETLE: return getConstant(C1 <= C2, VT);
663 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000664 }
665 } else {
666 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000667 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000668 }
669
670 if (N1 == N2) {
671 // We can always fold X == Y for integer setcc's.
672 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000673 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000674 unsigned UOF = ISD::getUnorderedFlavor(Cond);
675 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000676 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000677 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000678 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000679 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
680 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000681 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
682 if (NewCond != Cond)
683 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000684 }
685
Chris Lattner5cdcc582005-01-09 20:52:51 +0000686 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000687 MVT::isInteger(N1.getValueType())) {
688 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
689 N1.getOpcode() == ISD::XOR) {
690 // Simplify (X+Y) == (X+Z) --> Y == Z
691 if (N1.getOpcode() == N2.getOpcode()) {
692 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000693 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000694 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000695 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000696 if (isCommutativeBinOp(N1.getOpcode())) {
697 // If X op Y == Y op X, try other combinations.
698 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000699 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000700 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000701 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000702 }
703 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000704
705 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000706
Chris Lattner68dc3102005-01-10 02:03:02 +0000707 // Simplify (X+Z) == X --> Z == 0
708 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000709 return getSetCC(VT, N1.getOperand(1),
710 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000711 if (N1.getOperand(1) == N2) {
712 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000713 return getSetCC(VT, N1.getOperand(0),
714 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000715 else {
716 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
717 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000718 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000719 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000720 N2, getConstant(1, TLI.getShiftAmountTy())),
721 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000722 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000723 }
724 }
725
Chris Lattner68dc3102005-01-10 02:03:02 +0000726 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
727 N2.getOpcode() == ISD::XOR) {
728 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000729 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000730 return getSetCC(VT, N2.getOperand(1),
731 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000732 } else if (N2.getOperand(1) == N1) {
733 if (isCommutativeBinOp(N2.getOpcode())) {
734 return getSetCC(VT, N2.getOperand(0),
735 getConstant(0, N2.getValueType()), Cond);
736 } else {
737 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
738 // X == (Z-X) --> X<<1 == Z
739 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
740 getConstant(1, TLI.getShiftAmountTy())),
741 N2.getOperand(0), Cond);
742 }
743 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000744 }
745 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000746
Chris Lattnerfda2b552005-04-18 04:48:12 +0000747 // Fold away ALL boolean setcc's.
748 if (N1.getValueType() == MVT::i1) {
749 switch (Cond) {
750 default: assert(0 && "Unknown integer setcc!");
751 case ISD::SETEQ: // X == Y -> (X^Y)^1
752 N1 = getNode(ISD::XOR, MVT::i1,
753 getNode(ISD::XOR, MVT::i1, N1, N2),
754 getConstant(1, MVT::i1));
755 break;
756 case ISD::SETNE: // X != Y --> (X^Y)
757 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
758 break;
759 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
760 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
761 N1 = getNode(ISD::AND, MVT::i1, N2,
762 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
763 break;
764 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
765 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
766 N1 = getNode(ISD::AND, MVT::i1, N1,
767 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
768 break;
769 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
770 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
771 N1 = getNode(ISD::OR, MVT::i1, N2,
772 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
773 break;
774 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
775 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
776 N1 = getNode(ISD::OR, MVT::i1, N1,
777 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
778 break;
779 }
780 if (VT != MVT::i1)
781 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
782 return N1;
783 }
784
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000785 // Could not fold it.
786 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000787}
788
Nate Begemanff663682005-08-13 06:14:17 +0000789SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
790 SDOperand N3, SDOperand N4,
791 ISD::CondCode CC) {
792 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000793 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
794 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
795 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
796
797 // Check to see if we can simplify the select into an fabs node
798 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
799 // Allow either -0.0 or 0.0
800 if (CFP->getValue() == 0.0) {
801 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
802 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
803 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
804 N1 == N4.getOperand(0))
805 return getNode(ISD::FABS, VT, N1);
806
807 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
808 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
809 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
810 N3.getOperand(0) == N4)
811 return getNode(ISD::FABS, VT, N4);
812 }
813 }
814
815 // Check to see if we can perform the "gzip trick", transforming
816 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
817 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
818 MVT::isInteger(N1.getValueType()) &&
819 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
820 MVT::ValueType XType = N1.getValueType();
821 MVT::ValueType AType = N3.getValueType();
822 if (XType >= AType) {
823 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
824 // single-bit constant. FIXME: remove once the dag combiner
825 // exists.
826 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
827 unsigned ShCtV = Log2_64(N3C->getValue());
828 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
829 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
830 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
831 if (XType > AType)
832 Shift = getNode(ISD::TRUNCATE, AType, Shift);
833 return getNode(ISD::AND, AType, Shift, N3);
834 }
835 SDOperand Shift = getNode(ISD::SRA, XType, N1,
836 getConstant(MVT::getSizeInBits(XType)-1,
837 TLI.getShiftAmountTy()));
838 if (XType > AType)
839 Shift = getNode(ISD::TRUNCATE, AType, Shift);
840 return getNode(ISD::AND, AType, Shift, N3);
841 }
842 }
843
844 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
845 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
846 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
847 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
848 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
849 MVT::ValueType XType = N1.getValueType();
850 if (SubC->isNullValue() && MVT::isInteger(XType)) {
851 SDOperand Shift = getNode(ISD::SRA, XType, N1,
852 getConstant(MVT::getSizeInBits(XType)-1,
853 TLI.getShiftAmountTy()));
854 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
855 Shift);
856 }
857 }
858 }
859
860 // Could not fold it.
861 return SDOperand();
862}
863
Chris Lattnerc3aae252005-01-07 07:46:32 +0000864/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000865///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000866SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
867 SDNode *N = new SDNode(Opcode, VT);
868 AllNodes.push_back(N);
869 return SDOperand(N, 0);
870}
871
Chris Lattnerc3aae252005-01-07 07:46:32 +0000872SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
873 SDOperand Operand) {
874 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
875 uint64_t Val = C->getValue();
876 switch (Opcode) {
877 default: break;
878 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
879 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
880 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000881 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
882 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000883 }
884 }
885
886 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
887 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000888 case ISD::FNEG:
889 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000890 case ISD::FP_ROUND:
891 case ISD::FP_EXTEND:
892 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000893 case ISD::FP_TO_SINT:
894 return getConstant((int64_t)C->getValue(), VT);
895 case ISD::FP_TO_UINT:
896 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000897 }
898
899 unsigned OpOpcode = Operand.Val->getOpcode();
900 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000901 case ISD::TokenFactor:
902 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000903 case ISD::SIGN_EXTEND:
904 if (Operand.getValueType() == VT) return Operand; // noop extension
905 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
906 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
907 break;
908 case ISD::ZERO_EXTEND:
909 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000910 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000911 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912 break;
913 case ISD::TRUNCATE:
914 if (Operand.getValueType() == VT) return Operand; // noop truncate
915 if (OpOpcode == ISD::TRUNCATE)
916 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000917 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
918 // If the source is smaller than the dest, we still need an extend.
919 if (Operand.Val->getOperand(0).getValueType() < VT)
920 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
921 else if (Operand.Val->getOperand(0).getValueType() > VT)
922 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
923 else
924 return Operand.Val->getOperand(0);
925 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000926 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000927 case ISD::FNEG:
928 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
929 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
930 Operand.Val->getOperand(0));
931 if (OpOpcode == ISD::FNEG) // --X -> X
932 return Operand.Val->getOperand(0);
933 break;
934 case ISD::FABS:
935 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
936 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
937 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 }
939
940 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
941 if (N) return SDOperand(N, 0);
942 N = new SDNode(Opcode, Operand);
943 N->setValueTypes(VT);
944 AllNodes.push_back(N);
945 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000946}
947
Chris Lattner36019aa2005-04-18 03:48:41 +0000948/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
949/// this predicate to simplify operations downstream. V and Mask are known to
950/// be the same type.
951static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
952 const TargetLowering &TLI) {
953 unsigned SrcBits;
954 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000955
Chris Lattner36019aa2005-04-18 03:48:41 +0000956 // If we know the result of a setcc has the top bits zero, use this info.
957 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +0000958 case ISD::Constant:
959 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
960
961 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000962 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000963 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
964
965 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000966 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000967 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
968 case ISD::ZERO_EXTEND:
969 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
970 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
971
972 case ISD::AND:
973 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000974 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000975 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
976
977 // FALL THROUGH
978 case ISD::OR:
979 case ISD::XOR:
980 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
981 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
982 case ISD::SELECT:
983 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
984 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000985
986 case ISD::SRL:
987 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
988 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
989 uint64_t NewVal = Mask << ShAmt->getValue();
990 SrcBits = MVT::getSizeInBits(Op.getValueType());
991 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
992 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
993 }
994 return false;
995 case ISD::SHL:
996 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
997 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
998 uint64_t NewVal = Mask >> ShAmt->getValue();
999 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
1000 }
1001 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +00001002 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +00001003 default: break;
1004 }
1005
1006 return false;
1007}
1008
1009
1010
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1012 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001013#ifndef NDEBUG
1014 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001015 case ISD::TokenFactor:
1016 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1017 N2.getValueType() == MVT::Other && "Invalid token factor!");
1018 break;
Chris Lattner76365122005-01-16 02:23:22 +00001019 case ISD::AND:
1020 case ISD::OR:
1021 case ISD::XOR:
1022 case ISD::UDIV:
1023 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001024 case ISD::MULHU:
1025 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001026 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1027 // fall through
1028 case ISD::ADD:
1029 case ISD::SUB:
1030 case ISD::MUL:
1031 case ISD::SDIV:
1032 case ISD::SREM:
1033 assert(N1.getValueType() == N2.getValueType() &&
1034 N1.getValueType() == VT && "Binary operator types must match!");
1035 break;
1036
1037 case ISD::SHL:
1038 case ISD::SRA:
1039 case ISD::SRL:
1040 assert(VT == N1.getValueType() &&
1041 "Shift operators return type must be the same as their first arg");
1042 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001043 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001044 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001045 case ISD::FP_ROUND_INREG: {
1046 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1047 assert(VT == N1.getValueType() && "Not an inreg round!");
1048 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1049 "Cannot FP_ROUND_INREG integer types");
1050 assert(EVT <= VT && "Not rounding down!");
1051 break;
1052 }
1053 case ISD::SIGN_EXTEND_INREG: {
1054 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1055 assert(VT == N1.getValueType() && "Not an inreg extend!");
1056 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1057 "Cannot *_EXTEND_INREG FP types");
1058 assert(EVT <= VT && "Not extending!");
1059 }
1060
Chris Lattner76365122005-01-16 02:23:22 +00001061 default: break;
1062 }
1063#endif
1064
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1066 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1067 if (N1C) {
1068 if (N2C) {
1069 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1070 switch (Opcode) {
1071 case ISD::ADD: return getConstant(C1 + C2, VT);
1072 case ISD::SUB: return getConstant(C1 - C2, VT);
1073 case ISD::MUL: return getConstant(C1 * C2, VT);
1074 case ISD::UDIV:
1075 if (C2) return getConstant(C1 / C2, VT);
1076 break;
1077 case ISD::UREM :
1078 if (C2) return getConstant(C1 % C2, VT);
1079 break;
1080 case ISD::SDIV :
1081 if (C2) return getConstant(N1C->getSignExtended() /
1082 N2C->getSignExtended(), VT);
1083 break;
1084 case ISD::SREM :
1085 if (C2) return getConstant(N1C->getSignExtended() %
1086 N2C->getSignExtended(), VT);
1087 break;
1088 case ISD::AND : return getConstant(C1 & C2, VT);
1089 case ISD::OR : return getConstant(C1 | C2, VT);
1090 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001091 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1092 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1093 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 default: break;
1095 }
1096
1097 } else { // Cannonicalize constant to RHS if commutative
1098 if (isCommutativeBinOp(Opcode)) {
1099 std::swap(N1C, N2C);
1100 std::swap(N1, N2);
1101 }
1102 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001103
1104 switch (Opcode) {
1105 default: break;
1106 case ISD::SHL: // shl 0, X -> 0
1107 if (N1C->isNullValue()) return N1;
1108 break;
1109 case ISD::SRL: // srl 0, X -> 0
1110 if (N1C->isNullValue()) return N1;
1111 break;
1112 case ISD::SRA: // sra -1, X -> -1
1113 if (N1C->isAllOnesValue()) return N1;
1114 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001115 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1116 // Extending a constant? Just return the extended constant.
1117 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1118 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001119 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001120 }
1121
1122 if (N2C) {
1123 uint64_t C2 = N2C->getValue();
1124
1125 switch (Opcode) {
1126 case ISD::ADD:
1127 if (!C2) return N1; // add X, 0 -> X
1128 break;
1129 case ISD::SUB:
1130 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001131 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 case ISD::MUL:
1133 if (!C2) return N2; // mul X, 0 -> 0
1134 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1135 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1136
Chris Lattnerb48da392005-01-23 04:39:44 +00001137 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001139 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001140 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 }
1142 break;
1143
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001144 case ISD::MULHU:
1145 case ISD::MULHS:
1146 if (!C2) return N2; // mul X, 0 -> 0
1147
1148 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1149 return getConstant(0, VT);
1150
1151 // Many others could be handled here, including -1, powers of 2, etc.
1152 break;
1153
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001155 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001156 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001157 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001158 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159 }
1160 break;
1161
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001162 case ISD::SHL:
1163 case ISD::SRL:
1164 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001165 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001166 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001167 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1168 return getNode(ISD::UNDEF, N1.getValueType());
1169 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001170 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001171
1172 if (Opcode == ISD::SRA) {
1173 // If the sign bit is known to be zero, switch this to a SRL.
1174 if (MaskedValueIsZero(N1,
Jeff Cohena92b7c32005-08-19 04:39:48 +00001175 1ULL << (MVT::getSizeInBits(N1.getValueType())-1),
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001176 TLI))
1177 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1178 } else {
1179 // If the part left over is known to be zero, the whole thing is zero.
1180 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1181 if (Opcode == ISD::SRL) {
1182 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1183 return getConstant(0, N1.getValueType());
1184 } else if (Opcode == ISD::SHL) {
1185 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1186 return getConstant(0, N1.getValueType());
1187 }
1188 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001189
1190 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1191 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1192 unsigned OpSAC = OpSA->getValue();
1193 if (N1.getOpcode() == ISD::SHL) {
1194 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1195 return getConstant(0, N1.getValueType());
1196 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1197 getConstant(C2+OpSAC, N2.getValueType()));
1198 } else if (N1.getOpcode() == ISD::SRL) {
1199 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1200 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1201 getConstant(~0ULL << OpSAC, VT));
1202 if (C2 > OpSAC) {
1203 return getNode(ISD::SHL, VT, Mask,
1204 getConstant(C2-OpSAC, N2.getValueType()));
1205 } else {
1206 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1207 return getNode(ISD::SRL, VT, Mask,
1208 getConstant(OpSAC-C2, N2.getValueType()));
1209 }
1210 } else if (N1.getOpcode() == ISD::SRA) {
1211 // if C1 == C2, just mask out low bits.
1212 if (C2 == OpSAC)
1213 return getNode(ISD::AND, VT, N1.getOperand(0),
1214 getConstant(~0ULL << C2, VT));
1215 }
1216 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001217 break;
1218
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 case ISD::AND:
1220 if (!C2) return N2; // X and 0 -> 0
1221 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001222 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001223
Chris Lattner36019aa2005-04-18 03:48:41 +00001224 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1225 return getConstant(0, VT);
1226
Chris Lattner588bbbf2005-04-21 06:28:15 +00001227 {
1228 uint64_t NotC2 = ~C2;
1229 if (VT != MVT::i64)
1230 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1231
1232 if (MaskedValueIsZero(N1, NotC2, TLI))
1233 return N1; // if (X & ~C2) -> 0, the and is redundant
1234 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001235
Chris Lattnerdea29e22005-04-10 01:13:15 +00001236 // FIXME: Should add a corresponding version of this for
1237 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1238 // we don't have yet.
1239
Chris Lattner0f2287b2005-04-13 02:38:18 +00001240 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1241 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001242 // If we are masking out the part of our input that was extended, just
1243 // mask the input to the extension directly.
1244 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001245 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001246 if ((C2 & (~0ULL << ExtendBits)) == 0)
1247 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001248 } else if (N1.getOpcode() == ISD::OR) {
1249 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1250 if ((ORI->getValue() & C2) == C2) {
1251 // If the 'or' is setting all of the bits that we are masking for,
1252 // we know the result of the AND will be the AND mask itself.
1253 return N2;
1254 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001255 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001256 break;
1257 case ISD::OR:
1258 if (!C2)return N1; // X or 0 -> X
1259 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001260 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001261 break;
1262 case ISD::XOR:
1263 if (!C2) return N1; // X xor 0 -> X
1264 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001265 if (N1.Val->getOpcode() == ISD::SETCC){
1266 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001267 // !(X op Y) -> (X !op Y)
1268 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001269 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1270 return getSetCC(SetCC->getValueType(0),
1271 SetCC->getOperand(0), SetCC->getOperand(1),
1272 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001273 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1274 SDNode *Op = N1.Val;
1275 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1276 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1277 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1278 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1279 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1280 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1281 if (Op->getOpcode() == ISD::AND)
1282 return getNode(ISD::OR, VT, LHS, RHS);
1283 return getNode(ISD::AND, VT, LHS, RHS);
1284 }
1285 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001286 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001287 }
1288 break;
1289 }
1290
1291 // Reassociate ((X op C1) op C2) if possible.
1292 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1293 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001294 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001295 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1296 }
1297
1298 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1299 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001300 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 if (N2CFP) {
1302 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1303 switch (Opcode) {
1304 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1305 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1306 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1307 case ISD::SDIV:
1308 if (C2) return getConstantFP(C1 / C2, VT);
1309 break;
1310 case ISD::SREM :
1311 if (C2) return getConstantFP(fmod(C1, C2), VT);
1312 break;
1313 default: break;
1314 }
1315
1316 } else { // Cannonicalize constant to RHS if commutative
1317 if (isCommutativeBinOp(Opcode)) {
1318 std::swap(N1CFP, N2CFP);
1319 std::swap(N1, N2);
1320 }
1321 }
1322
Chris Lattner15e4b012005-07-10 00:07:11 +00001323 if (Opcode == ISD::FP_ROUND_INREG)
1324 return getNode(ISD::FP_EXTEND, VT,
1325 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1326 }
1327
Chris Lattnerc3aae252005-01-07 07:46:32 +00001328 // Finally, fold operations that do not require constants.
1329 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001330 case ISD::TokenFactor:
1331 if (N1.getOpcode() == ISD::EntryToken)
1332 return N2;
1333 if (N2.getOpcode() == ISD::EntryToken)
1334 return N1;
1335 break;
1336
Chris Lattnerc3aae252005-01-07 07:46:32 +00001337 case ISD::AND:
1338 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001339 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1340 SDNode *LHS = N1.Val, *RHS = N2.Val;
1341 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1342 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1343 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1344 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001345
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001346 if (LR == RR && isa<ConstantSDNode>(LR) &&
1347 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1348 // (X != 0) | (Y != 0) -> (X|Y != 0)
1349 // (X == 0) & (Y == 0) -> (X|Y == 0)
1350 // (X < 0) | (Y < 0) -> (X|Y < 0)
1351 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1352 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1353 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1354 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1355 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1356 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001357
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001358 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1359 // (X == -1) & (Y == -1) -> (X&Y == -1)
1360 // (X != -1) | (Y != -1) -> (X&Y != -1)
1361 // (X > -1) | (Y > -1) -> (X&Y > -1)
1362 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1363 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1364 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1365 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1366 LR, Op2);
1367 // (X > -1) & (Y > -1) -> (X|Y > -1)
1368 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1369 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1370 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001371 }
1372 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001373
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001374 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1375 if (LL == RR && LR == RL) {
1376 Op2 = ISD::getSetCCSwappedOperands(Op2);
1377 goto MatchedBackwards;
1378 }
1379
1380 if (LL == RL && LR == RR) {
1381 MatchedBackwards:
1382 ISD::CondCode Result;
1383 bool isInteger = MVT::isInteger(LL.getValueType());
1384 if (Opcode == ISD::OR)
1385 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1386 else
1387 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1388
1389 if (Result != ISD::SETCC_INVALID)
1390 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1391 }
1392 }
1393
Chris Lattner7467c9b2005-04-18 04:11:19 +00001394 // and/or zext(a), zext(b) -> zext(and/or a, b)
1395 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1396 N2.getOpcode() == ISD::ZERO_EXTEND &&
1397 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1398 return getNode(ISD::ZERO_EXTEND, VT,
1399 getNode(Opcode, N1.getOperand(0).getValueType(),
1400 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001401 break;
1402 case ISD::XOR:
1403 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1404 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001405 case ISD::ADD:
1406 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1407 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1408 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1409 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001410 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1411 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1412 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1413 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1414 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1415 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001416 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1417 !MVT::isFloatingPoint(N2.getValueType()))
1418 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001419 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001420 case ISD::SUB:
1421 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001422 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001423 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001424 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001425 if (N1.Val->getOperand(1) == N2 &&
1426 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001427 return N1.Val->getOperand(0); // (A+B)-B == A
1428 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001429 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1430 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001431 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001432 case ISD::FP_ROUND_INREG:
1433 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1434 break;
1435 case ISD::SIGN_EXTEND_INREG: {
1436 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1437 if (EVT == VT) return N1; // Not actually extending
1438
1439 // If we are sign extending an extension, use the original source.
1440 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1441 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1442 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001443
Chris Lattner15e4b012005-07-10 00:07:11 +00001444 // If we are sign extending a sextload, return just the load.
1445 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001446 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001447 return N1;
1448
1449 // If we are extending the result of a setcc, and we already know the
1450 // contents of the top bits, eliminate the extension.
1451 if (N1.getOpcode() == ISD::SETCC &&
1452 TLI.getSetCCResultContents() ==
1453 TargetLowering::ZeroOrNegativeOneSetCCResult)
1454 return N1;
1455
1456 // If we are sign extending the result of an (and X, C) operation, and we
1457 // know the extended bits are zeros already, don't do the extend.
1458 if (N1.getOpcode() == ISD::AND)
1459 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1460 uint64_t Mask = N1C->getValue();
1461 unsigned NumBits = MVT::getSizeInBits(EVT);
1462 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1463 return N1;
1464 }
1465 break;
1466 }
1467
Nate Begemaneea805e2005-04-13 21:23:31 +00001468 // FIXME: figure out how to safely handle things like
1469 // int foo(int x) { return 1 << (x & 255); }
1470 // int bar() { return foo(256); }
1471#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001472 case ISD::SHL:
1473 case ISD::SRL:
1474 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001475 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001476 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001477 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001478 else if (N2.getOpcode() == ISD::AND)
1479 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1480 // If the and is only masking out bits that cannot effect the shift,
1481 // eliminate the and.
1482 unsigned NumBits = MVT::getSizeInBits(VT);
1483 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1484 return getNode(Opcode, VT, N1, N2.getOperand(0));
1485 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001486 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001487#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001488 }
1489
Chris Lattner27e9b412005-05-11 18:57:39 +00001490 // Memoize this node if possible.
1491 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001492 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001493 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1494 if (BON) return SDOperand(BON, 0);
1495
1496 BON = N = new SDNode(Opcode, N1, N2);
1497 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001498 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001499 }
1500
Chris Lattner3e011362005-05-14 07:45:46 +00001501 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001502 AllNodes.push_back(N);
1503 return SDOperand(N, 0);
1504}
1505
Chris Lattner88de6e72005-05-12 00:17:04 +00001506// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001507// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001508void SDNode::setAdjCallChain(SDOperand N) {
1509 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001510 assert((getOpcode() == ISD::CALLSEQ_START ||
1511 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001512
1513 Operands[0].Val->removeUser(this);
1514 Operands[0] = N;
1515 N.Val->Uses.push_back(this);
1516}
1517
1518
1519
Chris Lattnerc3aae252005-01-07 07:46:32 +00001520SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001521 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001522 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001523 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1524 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001525 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001526
1527 // Loads have a token chain.
1528 N->setValueTypes(VT, MVT::Other);
1529 AllNodes.push_back(N);
1530 return SDOperand(N, 0);
1531}
1532
Chris Lattner5f056bf2005-07-10 01:55:33 +00001533
1534SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1535 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1536 MVT::ValueType EVT) {
1537 std::vector<SDOperand> Ops;
1538 Ops.reserve(4);
1539 Ops.push_back(Chain);
1540 Ops.push_back(Ptr);
1541 Ops.push_back(SV);
1542 Ops.push_back(getValueType(EVT));
1543 std::vector<MVT::ValueType> VTs;
1544 VTs.reserve(2);
1545 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1546 return getNode(Opcode, VTs, Ops);
1547}
1548
Chris Lattnerc3aae252005-01-07 07:46:32 +00001549SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1550 SDOperand N1, SDOperand N2, SDOperand N3) {
1551 // Perform various simplifications.
1552 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1553 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1554 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1555 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001556 case ISD::SETCC: {
1557 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001558 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001559 if (Simp.Val) return Simp;
1560 break;
1561 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001562 case ISD::SELECT:
1563 if (N1C)
1564 if (N1C->getValue())
1565 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001566 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001567 return N3; // select false, X, Y -> Y
1568
1569 if (N2 == N3) return N2; // select C, X, X -> X
1570
1571 if (VT == MVT::i1) { // Boolean SELECT
1572 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001573 if (N2C->getValue()) // select C, 1, X -> C | X
1574 return getNode(ISD::OR, VT, N1, N3);
1575 else // select C, 0, X -> ~C & X
1576 return getNode(ISD::AND, VT,
1577 getNode(ISD::XOR, N1.getValueType(), N1,
1578 getConstant(1, N1.getValueType())), N3);
1579 } else if (N3C) {
1580 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1581 return getNode(ISD::OR, VT,
1582 getNode(ISD::XOR, N1.getValueType(), N1,
1583 getConstant(1, N1.getValueType())), N2);
1584 else // select C, X, 0 -> C & X
1585 return getNode(ISD::AND, VT, N1, N2);
1586 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001587
1588 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1589 return getNode(ISD::OR, VT, N1, N3);
1590 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1591 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001592 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001593 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001594 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1595 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001596 if (Simp.Val) return Simp;
1597 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001598 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001599 case ISD::BRCOND:
1600 if (N2C)
1601 if (N2C->getValue()) // Unconditional branch
1602 return getNode(ISD::BR, MVT::Other, N1, N3);
1603 else
1604 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001605 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001606 }
1607
Chris Lattner385328c2005-05-14 07:42:29 +00001608 std::vector<SDOperand> Ops;
1609 Ops.reserve(3);
1610 Ops.push_back(N1);
1611 Ops.push_back(N2);
1612 Ops.push_back(N3);
1613
1614 // Memoize nodes.
1615 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1616 if (N) return SDOperand(N, 0);
1617
1618 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001619 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001620 AllNodes.push_back(N);
1621 return SDOperand(N, 0);
1622}
1623
1624SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001625 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001626 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001627 std::vector<SDOperand> Ops;
1628 Ops.reserve(4);
1629 Ops.push_back(N1);
1630 Ops.push_back(N2);
1631 Ops.push_back(N3);
1632 Ops.push_back(N4);
1633 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001634}
1635
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001636SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1637 SDOperand N1, SDOperand N2, SDOperand N3,
1638 SDOperand N4, SDOperand N5) {
Nate Begemane5d63822005-08-11 01:12:20 +00001639 if (ISD::SELECT_CC == Opcode) {
1640 assert(N1.getValueType() == N2.getValueType() &&
1641 "LHS and RHS of condition must have same type!");
1642 assert(N3.getValueType() == N4.getValueType() &&
1643 "True and False arms of SelectCC must have same type!");
Nate Begemanff663682005-08-13 06:14:17 +00001644 assert(N3.getValueType() == VT &&
1645 "select_cc node must be of same type as true and false value!");
1646 SDOperand Simp = SimplifySelectCC(N1, N2, N3, N4,
1647 cast<CondCodeSDNode>(N5)->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001648 if (Simp.Val) return Simp;
Nate Begemane5d63822005-08-11 01:12:20 +00001649 }
1650
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001651 std::vector<SDOperand> Ops;
1652 Ops.reserve(5);
1653 Ops.push_back(N1);
1654 Ops.push_back(N2);
1655 Ops.push_back(N3);
1656 Ops.push_back(N4);
1657 Ops.push_back(N5);
1658 return getNode(Opcode, VT, Ops);
1659}
1660
1661
Chris Lattner0437cdd2005-05-09 04:14:13 +00001662SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001663 assert((!V || isa<PointerType>(V->getType())) &&
1664 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001665 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1666 if (N) return SDOperand(N, 0);
1667
1668 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001669 AllNodes.push_back(N);
1670 return SDOperand(N, 0);
1671}
1672
1673SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001674 std::vector<SDOperand> &Ops) {
1675 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001676 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001677 case 1: return getNode(Opcode, VT, Ops[0]);
1678 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1679 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001680 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001681 }
Chris Lattneref847df2005-04-09 03:27:28 +00001682
Chris Lattner89c34632005-05-14 06:20:26 +00001683 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001684 switch (Opcode) {
1685 default: break;
1686 case ISD::BRCONDTWOWAY:
1687 if (N1C)
1688 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001689 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001690 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001691 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001692 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001693 case ISD::BRTWOWAY_CC:
1694 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1695 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1696 "LHS and RHS of comparison must have same type!");
1697 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001698 case ISD::TRUNCSTORE: {
1699 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1700 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1701#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1702 // If this is a truncating store of a constant, convert to the desired type
1703 // and store it instead.
1704 if (isa<Constant>(Ops[0])) {
1705 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1706 if (isa<Constant>(Op))
1707 N1 = Op;
1708 }
1709 // Also for ConstantFP?
1710#endif
1711 if (Ops[0].getValueType() == EVT) // Normal store?
1712 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1713 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1714 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1715 "Can't do FP-INT conversion!");
1716 break;
1717 }
Chris Lattneref847df2005-04-09 03:27:28 +00001718 }
1719
Chris Lattner385328c2005-05-14 07:42:29 +00001720 // Memoize nodes.
1721 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1722 if (N) return SDOperand(N, 0);
1723 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001724 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001725 AllNodes.push_back(N);
1726 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001727}
1728
Chris Lattner89c34632005-05-14 06:20:26 +00001729SDOperand SelectionDAG::getNode(unsigned Opcode,
1730 std::vector<MVT::ValueType> &ResultTys,
1731 std::vector<SDOperand> &Ops) {
1732 if (ResultTys.size() == 1)
1733 return getNode(Opcode, ResultTys[0], Ops);
1734
Chris Lattner5f056bf2005-07-10 01:55:33 +00001735 switch (Opcode) {
1736 case ISD::EXTLOAD:
1737 case ISD::SEXTLOAD:
1738 case ISD::ZEXTLOAD: {
1739 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1740 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1741 // If they are asking for an extending load from/to the same thing, return a
1742 // normal load.
1743 if (ResultTys[0] == EVT)
1744 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1745 assert(EVT < ResultTys[0] &&
1746 "Should only be an extending load, not truncating!");
1747 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1748 "Cannot sign/zero extend a FP load!");
1749 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1750 "Cannot convert from FP to Int or Int -> FP!");
1751 break;
1752 }
1753
Chris Lattnere89083a2005-05-14 07:25:05 +00001754 // FIXME: figure out how to safely handle things like
1755 // int foo(int x) { return 1 << (x & 255); }
1756 // int bar() { return foo(256); }
1757#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001758 case ISD::SRA_PARTS:
1759 case ISD::SRL_PARTS:
1760 case ISD::SHL_PARTS:
1761 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001762 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001763 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1764 else if (N3.getOpcode() == ISD::AND)
1765 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1766 // If the and is only masking out bits that cannot effect the shift,
1767 // eliminate the and.
1768 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1769 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1770 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1771 }
1772 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001773#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001774 }
Chris Lattner89c34632005-05-14 06:20:26 +00001775
1776 // Memoize the node.
1777 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1778 Ops))];
1779 if (N) return SDOperand(N, 0);
1780 N = new SDNode(Opcode, Ops);
1781 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001782 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001783 return SDOperand(N, 0);
1784}
1785
Chris Lattner149c58c2005-08-16 18:17:10 +00001786
1787/// SelectNodeTo - These are used for target selectors to *mutate* the
1788/// specified node to have the specified return type, Target opcode, and
1789/// operands. Note that target opcodes are stored as
1790/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1791void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1792 unsigned TargetOpc, SDOperand Op1) {
1793 RemoveNodeFromCSEMaps(N);
1794 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1795 N->setValueTypes(VT);
1796 N->setOperands(Op1);
1797}
1798void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1799 unsigned TargetOpc, SDOperand Op1,
1800 SDOperand Op2) {
1801 RemoveNodeFromCSEMaps(N);
1802 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1803 N->setValueTypes(VT);
1804 N->setOperands(Op1, Op2);
1805}
1806void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1807 unsigned TargetOpc, SDOperand Op1,
1808 SDOperand Op2, SDOperand Op3) {
1809 RemoveNodeFromCSEMaps(N);
1810 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1811 N->setValueTypes(VT);
1812 N->setOperands(Op1, Op2, Op3);
1813}
Nate Begeman294a0a12005-08-18 07:30:15 +00001814void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1815 unsigned TargetOpc, SDOperand Op1,
1816 SDOperand Op2, SDOperand Op3, SDOperand Op4) {
1817 RemoveNodeFromCSEMaps(N);
1818 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1819 N->setValueTypes(VT);
1820 N->setOperands(Op1, Op2, Op3, Op4);
1821}
Chris Lattner149c58c2005-08-16 18:17:10 +00001822
Chris Lattner8b8749f2005-08-17 19:00:20 +00001823/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
1824/// This can cause recursive merging of nodes in the DAG.
1825///
1826void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To) {
1827 assert(From != To && "Cannot replace uses of with self");
1828 while (!From->use_empty()) {
1829 // Process users until they are all gone.
1830 SDNode *U = *From->use_begin();
1831
1832 // This node is about to morph, remove its old self from the CSE maps.
1833 RemoveNodeFromCSEMaps(U);
1834
1835 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i)
1836 if (U->getOperand(i).Val == From) {
1837 assert(From->getValueType(U->getOperand(i).ResNo) ==
1838 To->getValueType(U->getOperand(i).ResNo));
1839 From->removeUser(U);
1840 U->Operands[i].Val = To;
1841 To->addUser(U);
1842 }
1843
1844 // Now that we have modified U, add it back to the CSE maps. If it already
1845 // exists there, recursively merge the results together.
1846 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U))
1847 ReplaceAllUsesWith(U, Existing);
1848 // U is now dead.
1849 }
1850}
1851
Jim Laskey58b968b2005-08-17 20:08:02 +00001852//===----------------------------------------------------------------------===//
1853// SDNode Class
1854//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00001855
Chris Lattner5c884562005-01-12 18:37:47 +00001856/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1857/// indicated value. This method ignores uses of other values defined by this
1858/// operation.
1859bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1860 assert(Value < getNumValues() && "Bad value!");
1861
1862 // If there is only one value, this is easy.
1863 if (getNumValues() == 1)
1864 return use_size() == NUses;
1865 if (Uses.size() < NUses) return false;
1866
1867 SDOperand TheValue(this, Value);
1868
1869 std::set<SDNode*> UsersHandled;
1870
1871 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1872 UI != E; ++UI) {
1873 SDNode *User = *UI;
1874 if (User->getNumOperands() == 1 ||
1875 UsersHandled.insert(User).second) // First time we've seen this?
1876 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1877 if (User->getOperand(i) == TheValue) {
1878 if (NUses == 0)
1879 return false; // too many uses
1880 --NUses;
1881 }
1882 }
1883
1884 // Found exactly the right number of uses?
1885 return NUses == 0;
1886}
1887
1888
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001889const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001890 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001891 default:
1892 if (getOpcode() < ISD::BUILTIN_OP_END)
1893 return "<<Unknown DAG Node>>";
1894 else {
1895 if (G)
1896 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1897 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1898 return "<<Unknown Target Node>>";
1899 }
1900
Andrew Lenharth95762122005-03-31 21:24:06 +00001901 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001902 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnera23e8152005-08-18 03:31:02 +00001903 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001904 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001905 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001906 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001907 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001908 case ISD::ConstantFP: return "ConstantFP";
1909 case ISD::GlobalAddress: return "GlobalAddress";
1910 case ISD::FrameIndex: return "FrameIndex";
1911 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001912 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001913 case ISD::ExternalSymbol: return "ExternalSymbol";
1914 case ISD::ConstantPool: return "ConstantPoolIndex";
1915 case ISD::CopyToReg: return "CopyToReg";
1916 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001917 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001918 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001919
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001920 // Unary operators
1921 case ISD::FABS: return "fabs";
1922 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001923 case ISD::FSQRT: return "fsqrt";
1924 case ISD::FSIN: return "fsin";
1925 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001926
1927 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001928 case ISD::ADD: return "add";
1929 case ISD::SUB: return "sub";
1930 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001931 case ISD::MULHU: return "mulhu";
1932 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001933 case ISD::SDIV: return "sdiv";
1934 case ISD::UDIV: return "udiv";
1935 case ISD::SREM: return "srem";
1936 case ISD::UREM: return "urem";
1937 case ISD::AND: return "and";
1938 case ISD::OR: return "or";
1939 case ISD::XOR: return "xor";
1940 case ISD::SHL: return "shl";
1941 case ISD::SRA: return "sra";
1942 case ISD::SRL: return "srl";
1943
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001944 case ISD::SETCC: return "setcc";
1945 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001946 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001947 case ISD::ADD_PARTS: return "add_parts";
1948 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001949 case ISD::SHL_PARTS: return "shl_parts";
1950 case ISD::SRA_PARTS: return "sra_parts";
1951 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001952
Chris Lattner7f644642005-04-28 21:44:03 +00001953 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001954 case ISD::SIGN_EXTEND: return "sign_extend";
1955 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001956 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001957 case ISD::TRUNCATE: return "truncate";
1958 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001959 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001960 case ISD::FP_EXTEND: return "fp_extend";
1961
1962 case ISD::SINT_TO_FP: return "sint_to_fp";
1963 case ISD::UINT_TO_FP: return "uint_to_fp";
1964 case ISD::FP_TO_SINT: return "fp_to_sint";
1965 case ISD::FP_TO_UINT: return "fp_to_uint";
1966
1967 // Control flow instructions
1968 case ISD::BR: return "br";
1969 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001970 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001971 case ISD::BR_CC: return "br_cc";
1972 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001973 case ISD::RET: return "ret";
1974 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001975 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001976 case ISD::CALLSEQ_START: return "callseq_start";
1977 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001978
1979 // Other operators
1980 case ISD::LOAD: return "load";
1981 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001982 case ISD::EXTLOAD: return "extload";
1983 case ISD::SEXTLOAD: return "sextload";
1984 case ISD::ZEXTLOAD: return "zextload";
1985 case ISD::TRUNCSTORE: return "truncstore";
1986
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001987 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1988 case ISD::EXTRACT_ELEMENT: return "extract_element";
1989 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001990 case ISD::MEMSET: return "memset";
1991 case ISD::MEMCPY: return "memcpy";
1992 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001993
Chris Lattner276260b2005-05-11 04:50:30 +00001994 // Bit counting
1995 case ISD::CTPOP: return "ctpop";
1996 case ISD::CTTZ: return "cttz";
1997 case ISD::CTLZ: return "ctlz";
1998
1999 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00002000 case ISD::READPORT: return "readport";
2001 case ISD::WRITEPORT: return "writeport";
2002 case ISD::READIO: return "readio";
2003 case ISD::WRITEIO: return "writeio";
2004
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002005 case ISD::CONDCODE:
2006 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002007 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002008 case ISD::SETOEQ: return "setoeq";
2009 case ISD::SETOGT: return "setogt";
2010 case ISD::SETOGE: return "setoge";
2011 case ISD::SETOLT: return "setolt";
2012 case ISD::SETOLE: return "setole";
2013 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002014
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002015 case ISD::SETO: return "seto";
2016 case ISD::SETUO: return "setuo";
2017 case ISD::SETUEQ: return "setue";
2018 case ISD::SETUGT: return "setugt";
2019 case ISD::SETUGE: return "setuge";
2020 case ISD::SETULT: return "setult";
2021 case ISD::SETULE: return "setule";
2022 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002023
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002024 case ISD::SETEQ: return "seteq";
2025 case ISD::SETGT: return "setgt";
2026 case ISD::SETGE: return "setge";
2027 case ISD::SETLT: return "setlt";
2028 case ISD::SETLE: return "setle";
2029 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002030 }
2031 }
2032}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002033
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002034void SDNode::dump() const { dump(0); }
2035void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002036 std::cerr << (void*)this << ": ";
2037
2038 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2039 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002040 if (getValueType(i) == MVT::Other)
2041 std::cerr << "ch";
2042 else
2043 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002044 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002045 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002046
2047 std::cerr << " ";
2048 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2049 if (i) std::cerr << ", ";
2050 std::cerr << (void*)getOperand(i).Val;
2051 if (unsigned RN = getOperand(i).ResNo)
2052 std::cerr << ":" << RN;
2053 }
2054
2055 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2056 std::cerr << "<" << CSDN->getValue() << ">";
2057 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2058 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002059 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002060 dyn_cast<GlobalAddressSDNode>(this)) {
2061 std::cerr << "<";
2062 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002063 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002064 std::cerr << "<" << FIDN->getIndex() << ">";
2065 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
2066 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002067 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002068 std::cerr << "<";
2069 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2070 if (LBB)
2071 std::cerr << LBB->getName() << " ";
2072 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002073 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002074 if (G && MRegisterInfo::isPhysicalRegister(R->getReg())) {
2075 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2076 } else {
2077 std::cerr << " #" << R->getReg();
2078 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002079 } else if (const ExternalSymbolSDNode *ES =
2080 dyn_cast<ExternalSymbolSDNode>(this)) {
2081 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002082 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2083 if (M->getValue())
2084 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2085 else
2086 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002087 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2088 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002089 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002090}
2091
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002092static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002093 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2094 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002095 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002096 else
2097 std::cerr << "\n" << std::string(indent+2, ' ')
2098 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002099
Chris Lattnerea946cd2005-01-09 20:38:33 +00002100
2101 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002102 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002103}
2104
Chris Lattnerc3aae252005-01-07 07:46:32 +00002105void SelectionDAG::dump() const {
2106 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002107 std::vector<SDNode*> Nodes(AllNodes);
2108 std::sort(Nodes.begin(), Nodes.end());
2109
2110 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002111 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002112 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002113 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002114
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002115 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002116
Chris Lattnerc3aae252005-01-07 07:46:32 +00002117 std::cerr << "\n\n";
2118}
2119