blob: 4ad651718f561a4652da9f412b171d10f47cac48 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner061a1ea2005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000019#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000020#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000021#include <cmath>
Chris Lattner560b5e42004-06-02 04:28:06 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner061a1ea2005-01-07 07:46:32 +000024/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
25/// when given the operation for (X op Y).
26ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
27 // To perform this operation, we just need to swap the L and G bits of the
28 // operation.
29 unsigned OldL = (Operation >> 2) & 1;
30 unsigned OldG = (Operation >> 1) & 1;
31 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
32 (OldL << 1) | // New G bit
33 (OldG << 2)); // New L bit.
34}
35
36/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
37/// 'op' is a valid SetCC operation.
38ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
39 unsigned Operation = Op;
40 if (isInteger)
41 Operation ^= 7; // Flip L, G, E bits, but not U.
42 else
43 Operation ^= 15; // Flip all of the condition bits.
44 if (Operation > ISD::SETTRUE2)
45 Operation &= ~8; // Don't let N and U bits get set.
46 return ISD::CondCode(Operation);
47}
48
49
50/// isSignedOp - For an integer comparison, return 1 if the comparison is a
51/// signed operation and 2 if the result is an unsigned comparison. Return zero
52/// if the operation does not depend on the sign of the input (setne and seteq).
53static int isSignedOp(ISD::CondCode Opcode) {
54 switch (Opcode) {
55 default: assert(0 && "Illegal integer setcc operation!");
56 case ISD::SETEQ:
57 case ISD::SETNE: return 0;
58 case ISD::SETLT:
59 case ISD::SETLE:
60 case ISD::SETGT:
61 case ISD::SETGE: return 1;
62 case ISD::SETULT:
63 case ISD::SETULE:
64 case ISD::SETUGT:
65 case ISD::SETUGE: return 2;
66 }
67}
68
69/// getSetCCOrOperation - Return the result of a logical OR between different
70/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
71/// returns SETCC_INVALID if it is not possible to represent the resultant
72/// comparison.
73ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
74 bool isInteger) {
75 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
76 // Cannot fold a signed integer setcc with an unsigned integer setcc.
77 return ISD::SETCC_INVALID;
78
79 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
80
81 // If the N and U bits get set then the resultant comparison DOES suddenly
82 // care about orderedness, and is true when ordered.
83 if (Op > ISD::SETTRUE2)
84 Op &= ~16; // Clear the N bit.
85 return ISD::CondCode(Op);
86}
87
88/// getSetCCAndOperation - Return the result of a logical AND between different
89/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
90/// function returns zero if it is not possible to represent the resultant
91/// comparison.
92ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
93 bool isInteger) {
94 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
95 // Cannot fold a signed setcc with an unsigned setcc.
96 return ISD::SETCC_INVALID;
97
98 // Combine all of the condition bits.
99 return ISD::CondCode(Op1 & Op2);
100}
101
Chris Lattner9c667932005-01-07 21:09:16 +0000102/// RemoveDeadNodes - This method deletes all unreachable nodes in the
103/// SelectionDAG, including nodes (like loads) that have uses of their token
104/// chain but no other uses and no side effect. If a node is passed in as an
105/// argument, it is used as the seed for node deletion.
106void SelectionDAG::RemoveDeadNodes(SDNode *N) {
107 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
108
109 // Create a dummy node (which is not added to allnodes), that adds a reference
110 // to the root node, preventing it from being deleted.
111 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
112
113 DeleteNodeIfDead(N, &AllNodeSet);
114
115 Restart:
116 unsigned NumNodes = AllNodeSet.size();
117 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
118 I != E; ++I) {
119 // Try to delete this node.
120 DeleteNodeIfDead(*I, &AllNodeSet);
121
122 // If we actually deleted any nodes, do not use invalid iterators in
123 // AllNodeSet.
124 if (AllNodeSet.size() != NumNodes)
125 goto Restart;
126 }
127
128 // Restore AllNodes.
129 if (AllNodes.size() != NumNodes)
130 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
131
132 // If the root changed (e.g. it was a dead load, update the root).
133 setRoot(DummyNode->getOperand(0));
134
135 // Now that we are done with the dummy node, delete it.
136 DummyNode->getOperand(0).Val->removeUser(DummyNode);
137 delete DummyNode;
138}
139
140void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
141 if (!N->use_empty())
142 return;
143
144 // Okay, we really are going to delete this node. First take this out of the
145 // appropriate CSE map.
146 switch (N->getOpcode()) {
147 case ISD::Constant:
148 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
149 N->getValueType(0)));
150 break;
151 case ISD::ConstantFP:
152 ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
153 N->getValueType(0)));
154 break;
155 case ISD::GlobalAddress:
156 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
157 break;
158 case ISD::FrameIndex:
159 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
160 break;
161 case ISD::ConstantPool:
162 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
163 break;
164 case ISD::BasicBlock:
165 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
166 break;
167 case ISD::ExternalSymbol:
168 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
169 break;
170
171 case ISD::LOAD:
172 Loads.erase(std::make_pair(N->getOperand(1),
173 std::make_pair(N->getOperand(0),
174 N->getValueType(0))));
175 break;
176 case ISD::SETCC:
177 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
178 N->getOperand(1)),
179 cast<SetCCSDNode>(N)->getCondition()));
180 break;
181 default:
182 if (N->getNumOperands() == 1)
183 UnaryOps.erase(std::make_pair(N->getOpcode(),
184 std::make_pair(N->getOperand(0),
185 N->getValueType(0))));
186 else if (N->getNumOperands() == 2)
187 BinaryOps.erase(std::make_pair(N->getOpcode(),
188 std::make_pair(N->getOperand(0),
189 N->getOperand(1))));
190 break;
191 }
192
193 // Next, brutally remove the operand list.
Chris Lattner9c667932005-01-07 21:09:16 +0000194 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000195 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000196 N->Operands.pop_back();
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000197 O->removeUser(N);
198
199 // Now that we removed this operand, see if there are no uses of it left.
200 DeleteNodeIfDead(O, NodeSet);
Chris Lattner9c667932005-01-07 21:09:16 +0000201 }
202
203 // Remove the node from the nodes set and delete it.
204 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
205 AllNodeSet.erase(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000206
207 // Now that the node is gone, check to see if any of the operands of this node
208 // are dead now.
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000209 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000210}
211
212
Chris Lattner600d3082003-08-11 14:57:33 +0000213SelectionDAG::~SelectionDAG() {
214 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
215 delete AllNodes[i];
216}
217
Chris Lattner061a1ea2005-01-07 07:46:32 +0000218SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
219 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
220 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000221 if (VT != MVT::i64)
222 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
223
Chris Lattner061a1ea2005-01-07 07:46:32 +0000224 SDNode *&N = Constants[std::make_pair(Val, VT)];
225 if (N) return SDOperand(N, 0);
226 N = new ConstantSDNode(Val, VT);
227 AllNodes.push_back(N);
228 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000229}
230
Chris Lattner061a1ea2005-01-07 07:46:32 +0000231SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
232 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
233 if (VT == MVT::f32)
234 Val = (float)Val; // Mask out extra precision.
235
236 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
237 if (N) return SDOperand(N, 0);
238 N = new ConstantFPSDNode(Val, VT);
239 AllNodes.push_back(N);
240 return SDOperand(N, 0);
241}
242
243
244
245SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
246 MVT::ValueType VT) {
247 SDNode *&N = GlobalValues[GV];
248 if (N) return SDOperand(N, 0);
249 N = new GlobalAddressSDNode(GV,VT);
250 AllNodes.push_back(N);
251 return SDOperand(N, 0);
252}
253
254SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
255 SDNode *&N = FrameIndices[FI];
256 if (N) return SDOperand(N, 0);
257 N = new FrameIndexSDNode(FI, VT);
258 AllNodes.push_back(N);
259 return SDOperand(N, 0);
260}
261
262SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
263 SDNode *N = ConstantPoolIndices[CPIdx];
264 if (N) return SDOperand(N, 0);
265 N = new ConstantPoolSDNode(CPIdx, VT);
266 AllNodes.push_back(N);
267 return SDOperand(N, 0);
268}
269
270SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
271 SDNode *&N = BBNodes[MBB];
272 if (N) return SDOperand(N, 0);
273 N = new BasicBlockSDNode(MBB);
274 AllNodes.push_back(N);
275 return SDOperand(N, 0);
276}
277
278SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
279 SDNode *&N = ExternalSymbols[Sym];
280 if (N) return SDOperand(N, 0);
281 N = new ExternalSymbolSDNode(Sym, VT);
282 AllNodes.push_back(N);
283 return SDOperand(N, 0);
284}
285
286SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, SDOperand N1,
287 SDOperand N2) {
288 // These setcc operations always fold.
289 switch (Cond) {
290 default: break;
291 case ISD::SETFALSE:
292 case ISD::SETFALSE2: return getConstant(0, MVT::i1);
293 case ISD::SETTRUE:
294 case ISD::SETTRUE2: return getConstant(1, MVT::i1);
295 }
296
297 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
298 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
299 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
300
301 // Sign extend the operands if required
302 if (ISD::isSignedIntSetCC(Cond)) {
303 C1 = N1C->getSignExtended();
304 C2 = N2C->getSignExtended();
305 }
306
307 switch (Cond) {
308 default: assert(0 && "Unknown integer setcc!");
309 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
310 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
311 case ISD::SETULT: return getConstant(C1 < C2, MVT::i1);
312 case ISD::SETUGT: return getConstant(C1 > C2, MVT::i1);
313 case ISD::SETULE: return getConstant(C1 <= C2, MVT::i1);
314 case ISD::SETUGE: return getConstant(C1 >= C2, MVT::i1);
315 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
316 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
317 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
318 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
319 }
320 } else {
321 // Ensure that the constant occurs on the RHS.
322 Cond = ISD::getSetCCSwappedOperands(Cond);
323 std::swap(N1, N2);
324 }
325
326 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
327 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
328 double C1 = N1C->getValue(), C2 = N2C->getValue();
329
330 switch (Cond) {
331 default: break; // FIXME: Implement the rest of these!
332 case ISD::SETEQ: return getConstant(C1 == C2, MVT::i1);
333 case ISD::SETNE: return getConstant(C1 != C2, MVT::i1);
334 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
335 case ISD::SETGT: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
336 case ISD::SETLE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
337 case ISD::SETGE: return getConstant((int64_t)C1 < (int64_t)C2, MVT::i1);
338 }
339 } else {
340 // Ensure that the constant occurs on the RHS.
341 Cond = ISD::getSetCCSwappedOperands(Cond);
342 std::swap(N1, N2);
343 }
344
345 if (N1 == N2) {
346 // We can always fold X == Y for integer setcc's.
347 if (MVT::isInteger(N1.getValueType()))
348 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
349 unsigned UOF = ISD::getUnorderedFlavor(Cond);
350 if (UOF == 2) // FP operators that are undefined on NaNs.
351 return getConstant(ISD::isTrueWhenEqual(Cond), MVT::i1);
352 if (UOF == ISD::isTrueWhenEqual(Cond))
353 return getConstant(UOF, MVT::i1);
354 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
355 // if it is not already.
356 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
357 }
358
359
360 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2), Cond)];
361 if (N) return SDOperand(N, 0);
362 N = new SetCCSDNode(Cond, N1, N2);
363 AllNodes.push_back(N);
364 return SDOperand(N, 0);
365}
366
367
368
369/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000370///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000371SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
372 SDNode *N = new SDNode(Opcode, VT);
373 AllNodes.push_back(N);
374 return SDOperand(N, 0);
375}
376
377static const Type *getTypeFor(MVT::ValueType VT) {
378 switch (VT) {
379 default: assert(0 && "Unknown MVT!");
380 case MVT::i1: return Type::BoolTy;
381 case MVT::i8: return Type::UByteTy;
382 case MVT::i16: return Type::UShortTy;
383 case MVT::i32: return Type::UIntTy;
384 case MVT::i64: return Type::ULongTy;
385 case MVT::f32: return Type::FloatTy;
386 case MVT::f64: return Type::DoubleTy;
Chris Lattner600d3082003-08-11 14:57:33 +0000387 }
388}
389
Chris Lattner061a1ea2005-01-07 07:46:32 +0000390SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
391 SDOperand Operand) {
392 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
393 uint64_t Val = C->getValue();
394 switch (Opcode) {
395 default: break;
396 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
397 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
398 case ISD::TRUNCATE: return getConstant(Val, VT);
399 }
400 }
401
402 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
403 switch (Opcode) {
404 case ISD::FP_ROUND:
405 case ISD::FP_EXTEND:
406 return getConstantFP(C->getValue(), VT);
407 }
408
409 unsigned OpOpcode = Operand.Val->getOpcode();
410 switch (Opcode) {
411 case ISD::SIGN_EXTEND:
412 if (Operand.getValueType() == VT) return Operand; // noop extension
413 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
414 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
415 break;
416 case ISD::ZERO_EXTEND:
417 if (Operand.getValueType() == VT) return Operand; // noop extension
418 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
419 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
420 break;
421 case ISD::TRUNCATE:
422 if (Operand.getValueType() == VT) return Operand; // noop truncate
423 if (OpOpcode == ISD::TRUNCATE)
424 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000425 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
426 // If the source is smaller than the dest, we still need an extend.
427 if (Operand.Val->getOperand(0).getValueType() < VT)
428 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
429 else if (Operand.Val->getOperand(0).getValueType() > VT)
430 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
431 else
432 return Operand.Val->getOperand(0);
433 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000434 break;
435 }
436
437 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
438 if (N) return SDOperand(N, 0);
439 N = new SDNode(Opcode, Operand);
440 N->setValueTypes(VT);
441 AllNodes.push_back(N);
442 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000443}
444
Chris Lattner061a1ea2005-01-07 07:46:32 +0000445static bool isCommutativeBinOp(unsigned Opcode) {
446 switch (Opcode) {
447 case ISD::ADD:
448 case ISD::MUL:
449 case ISD::AND:
450 case ISD::OR:
451 case ISD::XOR: return true;
452 default: return false; // FIXME: Need commutative info for user ops!
Chris Lattner600d3082003-08-11 14:57:33 +0000453 }
Chris Lattner600d3082003-08-11 14:57:33 +0000454}
Chris Lattner061a1ea2005-01-07 07:46:32 +0000455
456static bool isAssociativeBinOp(unsigned Opcode) {
457 switch (Opcode) {
458 case ISD::ADD:
459 case ISD::MUL:
460 case ISD::AND:
461 case ISD::OR:
462 case ISD::XOR: return true;
463 default: return false; // FIXME: Need associative info for user ops!
464 }
465}
466
467static unsigned ExactLog2(uint64_t Val) {
468 unsigned Count = 0;
469 while (Val != 1) {
470 Val >>= 1;
471 ++Count;
472 }
473 return Count;
474}
475
476// isInvertibleForFree - Return true if there is no cost to emitting the logical
477// inverse of this node.
478static bool isInvertibleForFree(SDOperand N) {
479 if (isa<ConstantSDNode>(N.Val)) return true;
480 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
481 return true;
482 return false;
483}
484
485
486SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
487 SDOperand N1, SDOperand N2) {
488 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
489 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
490 if (N1C) {
491 if (N2C) {
492 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
493 switch (Opcode) {
494 case ISD::ADD: return getConstant(C1 + C2, VT);
495 case ISD::SUB: return getConstant(C1 - C2, VT);
496 case ISD::MUL: return getConstant(C1 * C2, VT);
497 case ISD::UDIV:
498 if (C2) return getConstant(C1 / C2, VT);
499 break;
500 case ISD::UREM :
501 if (C2) return getConstant(C1 % C2, VT);
502 break;
503 case ISD::SDIV :
504 if (C2) return getConstant(N1C->getSignExtended() /
505 N2C->getSignExtended(), VT);
506 break;
507 case ISD::SREM :
508 if (C2) return getConstant(N1C->getSignExtended() %
509 N2C->getSignExtended(), VT);
510 break;
511 case ISD::AND : return getConstant(C1 & C2, VT);
512 case ISD::OR : return getConstant(C1 | C2, VT);
513 case ISD::XOR : return getConstant(C1 ^ C2, VT);
514 default: break;
515 }
516
517 } else { // Cannonicalize constant to RHS if commutative
518 if (isCommutativeBinOp(Opcode)) {
519 std::swap(N1C, N2C);
520 std::swap(N1, N2);
521 }
522 }
523 }
524
525 if (N2C) {
526 uint64_t C2 = N2C->getValue();
527
528 switch (Opcode) {
529 case ISD::ADD:
530 if (!C2) return N1; // add X, 0 -> X
531 break;
532 case ISD::SUB:
533 if (!C2) return N1; // sub X, 0 -> X
534 break;
535 case ISD::MUL:
536 if (!C2) return N2; // mul X, 0 -> 0
537 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
538 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
539
540 // FIXME: This should only be done if the target supports shift
541 // operations.
542 if ((C2 & C2-1) == 0) {
543 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
544 return getNode(ISD::SHL, VT, N1, ShAmt);
545 }
546 break;
547
548 case ISD::UDIV:
549 // FIXME: This should only be done if the target supports shift
550 // operations.
551 if ((C2 & C2-1) == 0 && C2) {
552 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
553 return getNode(ISD::SRL, VT, N1, ShAmt);
554 }
555 break;
556
557 case ISD::AND:
558 if (!C2) return N2; // X and 0 -> 0
559 if (N2C->isAllOnesValue())
560 return N1; // X and -1 -> X
561 break;
562 case ISD::OR:
563 if (!C2)return N1; // X or 0 -> X
564 if (N2C->isAllOnesValue())
565 return N2; // X or -1 -> -1
566 break;
567 case ISD::XOR:
568 if (!C2) return N1; // X xor 0 -> X
569 if (N2C->isAllOnesValue()) {
570 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
571 // !(X op Y) -> (X !op Y)
572 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
573 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
574 SetCC->getOperand(0), SetCC->getOperand(1));
575 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
576 SDNode *Op = N1.Val;
577 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
578 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
579 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
580 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
581 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
582 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
583 if (Op->getOpcode() == ISD::AND)
584 return getNode(ISD::OR, VT, LHS, RHS);
585 return getNode(ISD::AND, VT, LHS, RHS);
586 }
587 }
588 // X xor -1 -> not(x) ?
589 }
590 break;
591 }
592
593 // Reassociate ((X op C1) op C2) if possible.
594 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
595 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +0000596 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000597 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
598 }
599
600 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
601 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
602 if (N1CFP)
603 if (N2CFP) {
604 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
605 switch (Opcode) {
606 case ISD::ADD: return getConstantFP(C1 + C2, VT);
607 case ISD::SUB: return getConstantFP(C1 - C2, VT);
608 case ISD::MUL: return getConstantFP(C1 * C2, VT);
609 case ISD::SDIV:
610 if (C2) return getConstantFP(C1 / C2, VT);
611 break;
612 case ISD::SREM :
613 if (C2) return getConstantFP(fmod(C1, C2), VT);
614 break;
615 default: break;
616 }
617
618 } else { // Cannonicalize constant to RHS if commutative
619 if (isCommutativeBinOp(Opcode)) {
620 std::swap(N1CFP, N2CFP);
621 std::swap(N1, N2);
622 }
623 }
624
625 // Finally, fold operations that do not require constants.
626 switch (Opcode) {
627 case ISD::AND:
628 case ISD::OR:
629 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
630 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
631 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
632 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
633 ISD::CondCode Op2 = RHS->getCondition();
634
635 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
636 if (LL == RR && LR == RL) {
637 Op2 = ISD::getSetCCSwappedOperands(Op2);
638 goto MatchedBackwards;
639 }
640
641 if (LL == RL && LR == RR) {
642 MatchedBackwards:
643 ISD::CondCode Result;
644 bool isInteger = MVT::isInteger(LL.getValueType());
645 if (Opcode == ISD::OR)
646 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
647 isInteger);
648 else
649 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
650 isInteger);
651 if (Result != ISD::SETCC_INVALID)
652 return getSetCC(Result, LL, LR);
653 }
654 }
655 break;
656 case ISD::XOR:
657 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
658 break;
659 }
660
661 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
662 if (N) return SDOperand(N, 0);
663 N = new SDNode(Opcode, N1, N2);
664 N->setValueTypes(VT);
665
666 AllNodes.push_back(N);
667 return SDOperand(N, 0);
668}
669
670SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
671 SDOperand Chain, SDOperand Ptr) {
672 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
673 if (N) return SDOperand(N, 0);
674 N = new SDNode(ISD::LOAD, Chain, Ptr);
675
676 // Loads have a token chain.
677 N->setValueTypes(VT, MVT::Other);
678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
680}
681
682
683SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
684 SDOperand N1, SDOperand N2, SDOperand N3) {
685 // Perform various simplifications.
686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
687 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
688 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
689 switch (Opcode) {
690 case ISD::SELECT:
691 if (N1C)
692 if (N1C->getValue())
693 return N2; // select true, X, Y -> X
694 else
695 return N3; // select false, X, Y -> Y
696
697 if (N2 == N3) return N2; // select C, X, X -> X
698
699 if (VT == MVT::i1) { // Boolean SELECT
700 if (N2C) {
701 if (N3C) {
702 if (N2C->getValue()) // select C, 1, 0 -> C
703 return N1;
704 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
705 }
706
707 if (N2C->getValue()) // select C, 1, X -> C | X
708 return getNode(ISD::OR, VT, N1, N3);
709 else // select C, 0, X -> ~C & X
710 return getNode(ISD::AND, VT,
711 getNode(ISD::XOR, N1.getValueType(), N1,
712 getConstant(1, N1.getValueType())), N3);
713 } else if (N3C) {
714 if (N3C->getValue()) // select C, X, 1 -> ~C | X
715 return getNode(ISD::OR, VT,
716 getNode(ISD::XOR, N1.getValueType(), N1,
717 getConstant(1, N1.getValueType())), N2);
718 else // select C, X, 0 -> C & X
719 return getNode(ISD::AND, VT, N1, N2);
720 }
721 }
722
723 break;
Chris Lattner5c66e452005-01-07 22:49:57 +0000724 case ISD::BRCOND:
725 if (N2C)
726 if (N2C->getValue()) // Unconditional branch
727 return getNode(ISD::BR, MVT::Other, N1, N3);
728 else
729 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000730 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000731 }
732
733 SDNode *N = new SDNode(Opcode, N1, N2, N3);
734 switch (Opcode) {
735 default:
736 N->setValueTypes(VT);
737 break;
738 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
739 N->setValueTypes(VT, MVT::Other);
740 break;
741 }
742
743 // FIXME: memoize NODES
744 AllNodes.push_back(N);
745 return SDOperand(N, 0);
746}
747
748SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
749 std::vector<SDOperand> &Children) {
750 switch (Children.size()) {
751 case 0: return getNode(Opcode, VT);
752 case 1: return getNode(Opcode, VT, Children[0]);
753 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
754 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
755 default:
756 // FIXME: MEMOIZE!!
757 SDNode *N = new SDNode(Opcode, Children);
758 N->setValueTypes(VT);
759 AllNodes.push_back(N);
760 return SDOperand(N, 0);
761 }
762}
763
764
765
766void SDNode::dump() const {
767 std::cerr << (void*)this << ": ";
768
769 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
770 if (i) std::cerr << ",";
771 switch (getValueType(i)) {
772 default: assert(0 && "Unknown value type!");
773 case MVT::i1: std::cerr << "i1"; break;
774 case MVT::i8: std::cerr << "i8"; break;
775 case MVT::i16: std::cerr << "i16"; break;
776 case MVT::i32: std::cerr << "i32"; break;
777 case MVT::i64: std::cerr << "i64"; break;
778 case MVT::f32: std::cerr << "f32"; break;
779 case MVT::f64: std::cerr << "f64"; break;
780 case MVT::Other: std::cerr << "ch"; break;
781 }
782 }
783 std::cerr << " = ";
784
785 switch (getOpcode()) {
786 default: std::cerr << "<<Unknown>>"; break;
787 case ISD::EntryToken: std::cerr << "EntryToken"; break;
788 case ISD::Constant: std::cerr << "Constant"; break;
789 case ISD::ConstantFP: std::cerr << "ConstantFP"; break;
790 case ISD::GlobalAddress: std::cerr << "GlobalAddress"; break;
791 case ISD::FrameIndex: std::cerr << "FrameIndex"; break;
792 case ISD::BasicBlock: std::cerr << "BasicBlock"; break;
793 case ISD::ExternalSymbol: std::cerr << "ExternalSymbol"; break;
794 case ISD::ConstantPool: std::cerr << "ConstantPoolIndex"; break;
795 case ISD::CopyToReg: std::cerr << "CopyToReg"; break;
796 case ISD::CopyFromReg: std::cerr << "CopyFromReg"; break;
797
798 case ISD::ADD: std::cerr << "add"; break;
799 case ISD::SUB: std::cerr << "sub"; break;
800 case ISD::MUL: std::cerr << "mul"; break;
801 case ISD::SDIV: std::cerr << "sdiv"; break;
802 case ISD::UDIV: std::cerr << "udiv"; break;
803 case ISD::SREM: std::cerr << "srem"; break;
804 case ISD::UREM: std::cerr << "urem"; break;
805 case ISD::AND: std::cerr << "and"; break;
806 case ISD::OR: std::cerr << "or"; break;
807 case ISD::XOR: std::cerr << "xor"; break;
808 case ISD::SHL: std::cerr << "shl"; break;
809 case ISD::SRA: std::cerr << "sra"; break;
810 case ISD::SRL: std::cerr << "srl"; break;
811
812 case ISD::SETCC: std::cerr << "setcc"; break;
813 case ISD::SELECT: std::cerr << "select"; break;
814 case ISD::ADDC: std::cerr << "addc"; break;
815 case ISD::SUBB: std::cerr << "subb"; break;
816
817 // Conversion operators.
818 case ISD::SIGN_EXTEND: std::cerr << "sign_extend"; break;
819 case ISD::ZERO_EXTEND: std::cerr << "zero_extend"; break;
820 case ISD::TRUNCATE: std::cerr << "truncate"; break;
821 case ISD::FP_ROUND: std::cerr << "fp_round"; break;
822 case ISD::FP_EXTEND: std::cerr << "fp_extend"; break;
823
824 // Control flow instructions
825 case ISD::BR: std::cerr << "br"; break;
826 case ISD::BRCOND: std::cerr << "brcond"; break;
827 case ISD::RET: std::cerr << "ret"; break;
828 case ISD::CALL: std::cerr << "call"; break;
829 case ISD::ADJCALLSTACKDOWN: std::cerr << "adjcallstackdown"; break;
830 case ISD::ADJCALLSTACKUP: std::cerr << "adjcallstackup"; break;
831
832 // Other operators
833 case ISD::LOAD: std::cerr << "load"; break;
834 case ISD::STORE: std::cerr << "store"; break;
835 case ISD::DYNAMIC_STACKALLOC: std::cerr << "dynamic_stackalloc"; break;
836 case ISD::EXTRACT_ELEMENT: std::cerr << "extract_element"; break;
837 case ISD::BUILD_PAIR: std::cerr << "build_pair"; break;
838 }
839
840 std::cerr << " ";
841 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
842 if (i) std::cerr << ", ";
843 std::cerr << (void*)getOperand(i).Val;
844 if (unsigned RN = getOperand(i).ResNo)
845 std::cerr << ":" << RN;
846 }
847
848 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
849 std::cerr << "<" << CSDN->getValue() << ">";
850 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
851 std::cerr << "<" << CSDN->getValue() << ">";
852 } else if (const GlobalAddressSDNode *GADN =
853 dyn_cast<GlobalAddressSDNode>(this)) {
854 std::cerr << "<";
855 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
856 } else if (const FrameIndexSDNode *FIDN =
857 dyn_cast<FrameIndexSDNode>(this)) {
858 std::cerr << "<" << FIDN->getIndex() << ">";
859 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
860 std::cerr << "<" << CP->getIndex() << ">";
861 } else if (const BasicBlockSDNode *BBDN =
862 dyn_cast<BasicBlockSDNode>(this)) {
863 std::cerr << "<";
864 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
865 if (LBB)
866 std::cerr << LBB->getName() << " ";
867 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
868 } else if (const CopyRegSDNode *C2V = dyn_cast<CopyRegSDNode>(this)) {
869 std::cerr << "<reg #" << C2V->getReg() << ">";
870 } else if (const ExternalSymbolSDNode *ES =
871 dyn_cast<ExternalSymbolSDNode>(this)) {
872 std::cerr << "'" << ES->getSymbol() << "'";
873 } else if (const SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(this)) {
874 std::cerr << " - condition = ";
875 switch (SetCC->getCondition()) {
876 default: assert(0 && "Unknown setcc condition!");
877 case ISD::SETOEQ: std::cerr << "setoeq"; break;
878 case ISD::SETOGT: std::cerr << "setogt"; break;
879 case ISD::SETOGE: std::cerr << "setoge"; break;
880 case ISD::SETOLT: std::cerr << "setolt"; break;
881 case ISD::SETOLE: std::cerr << "setole"; break;
882 case ISD::SETONE: std::cerr << "setone"; break;
883
884 case ISD::SETO: std::cerr << "seto"; break;
885 case ISD::SETUO: std::cerr << "setuo"; break;
886 case ISD::SETUEQ: std::cerr << "setue"; break;
887 case ISD::SETUGT: std::cerr << "setugt"; break;
888 case ISD::SETUGE: std::cerr << "setuge"; break;
889 case ISD::SETULT: std::cerr << "setult"; break;
890 case ISD::SETULE: std::cerr << "setule"; break;
891 case ISD::SETUNE: std::cerr << "setune"; break;
892
893 case ISD::SETEQ: std::cerr << "seteq"; break;
894 case ISD::SETGT: std::cerr << "setgt"; break;
895 case ISD::SETGE: std::cerr << "setge"; break;
896 case ISD::SETLT: std::cerr << "setlt"; break;
897 case ISD::SETLE: std::cerr << "setle"; break;
898 case ISD::SETNE: std::cerr << "setne"; break;
899 }
900 }
901
902}
903
904void SelectionDAG::dump() const {
905 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
906 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) {
907 std::cerr << "\n ";
908 AllNodes[i]->dump();
909 }
910 std::cerr << "\n\n";
911}
912