Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1 | //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 10 | // This implements the SelectionDAG class. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/GlobalValue.h" |
Chris Lattner | 70a248d | 2006-03-27 06:45:25 +0000 | [diff] [blame] | 17 | #include "llvm/Intrinsics.h" |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/Writer.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | fa164b6 | 2005-08-19 21:34:13 +0000 | [diff] [blame] | 21 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetInstrInfo.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SetVector.h" |
Evan Cheng | 115c036 | 2005-12-19 23:11:49 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 27 | #include <iostream> |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 28 | #include <set> |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 29 | #include <cmath> |
Jeff Cohen | fd161e9 | 2005-01-09 20:41:56 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Chris Lattner | e25738c | 2004-06-02 04:28:06 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 33 | static bool isCommutativeBinOp(unsigned Opcode) { |
| 34 | switch (Opcode) { |
| 35 | case ISD::ADD: |
| 36 | case ISD::MUL: |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 37 | case ISD::MULHU: |
| 38 | case ISD::MULHS: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 39 | case ISD::FADD: |
| 40 | case ISD::FMUL: |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 41 | case ISD::AND: |
| 42 | case ISD::OR: |
| 43 | case ISD::XOR: return true; |
| 44 | default: return false; // FIXME: Need commutative info for user ops! |
| 45 | } |
| 46 | } |
| 47 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 48 | // isInvertibleForFree - Return true if there is no cost to emitting the logical |
| 49 | // inverse of this node. |
| 50 | static bool isInvertibleForFree(SDOperand N) { |
| 51 | if (isa<ConstantSDNode>(N.Val)) return true; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 52 | if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 53 | return true; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 54 | return false; |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | // ConstantFPSDNode Class |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | /// isExactlyValue - We don't rely on operator== working on double values, as |
| 62 | /// it returns true for things that are clearly not equal, like -0.0 and 0.0. |
| 63 | /// As such, this method can be used to do an exact bit-for-bit comparison of |
| 64 | /// two floating point values. |
| 65 | bool ConstantFPSDNode::isExactlyValue(double V) const { |
| 66 | return DoubleToBits(V) == DoubleToBits(Value); |
| 67 | } |
| 68 | |
| 69 | //===----------------------------------------------------------------------===// |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 70 | // ISD Namespace |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 71 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 72 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 73 | /// isBuildVectorAllOnes - Return true if the specified node is a |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 74 | /// BUILD_VECTOR where all of the elements are ~0 or undef. |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 75 | bool ISD::isBuildVectorAllOnes(const SDNode *N) { |
Chris Lattner | 547a16f | 2006-04-15 23:38:00 +0000 | [diff] [blame] | 76 | // Look through a bit convert. |
| 77 | if (N->getOpcode() == ISD::BIT_CONVERT) |
| 78 | N = N->getOperand(0).Val; |
| 79 | |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 80 | if (N->getOpcode() != ISD::BUILD_VECTOR) return false; |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 81 | |
| 82 | unsigned i = 0, e = N->getNumOperands(); |
| 83 | |
| 84 | // Skip over all of the undef values. |
| 85 | while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) |
| 86 | ++i; |
| 87 | |
| 88 | // Do not accept an all-undef vector. |
| 89 | if (i == e) return false; |
| 90 | |
| 91 | // Do not accept build_vectors that aren't all constants or which have non-~0 |
| 92 | // elements. |
Chris Lattner | 452e835 | 2006-03-25 22:59:28 +0000 | [diff] [blame] | 93 | SDOperand NotZero = N->getOperand(i); |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 94 | if (isa<ConstantSDNode>(NotZero)) { |
| 95 | if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue()) |
| 96 | return false; |
| 97 | } else if (isa<ConstantFPSDNode>(NotZero)) { |
Evan Cheng | 23cc870 | 2006-03-27 08:10:26 +0000 | [diff] [blame] | 98 | MVT::ValueType VT = NotZero.getValueType(); |
| 99 | if (VT== MVT::f64) { |
| 100 | if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) != |
| 101 | (uint64_t)-1) |
| 102 | return false; |
| 103 | } else { |
| 104 | if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) != |
| 105 | (uint32_t)-1) |
| 106 | return false; |
| 107 | } |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 108 | } else |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 109 | return false; |
| 110 | |
| 111 | // Okay, we have at least one ~0 value, check to see if the rest match or are |
| 112 | // undefs. |
Chris Lattner | 61d4399 | 2006-03-25 22:57:01 +0000 | [diff] [blame] | 113 | for (++i; i != e; ++i) |
| 114 | if (N->getOperand(i) != NotZero && |
| 115 | N->getOperand(i).getOpcode() != ISD::UNDEF) |
| 116 | return false; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 121 | /// isBuildVectorAllZeros - Return true if the specified node is a |
| 122 | /// BUILD_VECTOR where all of the elements are 0 or undef. |
| 123 | bool ISD::isBuildVectorAllZeros(const SDNode *N) { |
Chris Lattner | 547a16f | 2006-04-15 23:38:00 +0000 | [diff] [blame] | 124 | // Look through a bit convert. |
| 125 | if (N->getOpcode() == ISD::BIT_CONVERT) |
| 126 | N = N->getOperand(0).Val; |
| 127 | |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 128 | if (N->getOpcode() != ISD::BUILD_VECTOR) return false; |
Evan Cheng | a8df166 | 2006-03-27 06:58:47 +0000 | [diff] [blame] | 129 | |
| 130 | unsigned i = 0, e = N->getNumOperands(); |
| 131 | |
| 132 | // Skip over all of the undef values. |
| 133 | while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF) |
| 134 | ++i; |
| 135 | |
| 136 | // Do not accept an all-undef vector. |
| 137 | if (i == e) return false; |
| 138 | |
| 139 | // Do not accept build_vectors that aren't all constants or which have non-~0 |
| 140 | // elements. |
| 141 | SDOperand Zero = N->getOperand(i); |
| 142 | if (isa<ConstantSDNode>(Zero)) { |
| 143 | if (!cast<ConstantSDNode>(Zero)->isNullValue()) |
| 144 | return false; |
| 145 | } else if (isa<ConstantFPSDNode>(Zero)) { |
| 146 | if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0)) |
| 147 | return false; |
| 148 | } else |
| 149 | return false; |
| 150 | |
| 151 | // Okay, we have at least one ~0 value, check to see if the rest match or are |
| 152 | // undefs. |
| 153 | for (++i; i != e; ++i) |
| 154 | if (N->getOperand(i) != Zero && |
| 155 | N->getOperand(i).getOpcode() != ISD::UNDEF) |
| 156 | return false; |
| 157 | return true; |
Evan Cheng | 4a14784 | 2006-03-26 09:50:58 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 160 | /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) |
| 161 | /// when given the operation for (X op Y). |
| 162 | ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { |
| 163 | // To perform this operation, we just need to swap the L and G bits of the |
| 164 | // operation. |
| 165 | unsigned OldL = (Operation >> 2) & 1; |
| 166 | unsigned OldG = (Operation >> 1) & 1; |
| 167 | return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits |
| 168 | (OldL << 1) | // New G bit |
| 169 | (OldG << 2)); // New L bit. |
| 170 | } |
| 171 | |
| 172 | /// getSetCCInverse - Return the operation corresponding to !(X op Y), where |
| 173 | /// 'op' is a valid SetCC operation. |
| 174 | ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { |
| 175 | unsigned Operation = Op; |
| 176 | if (isInteger) |
| 177 | Operation ^= 7; // Flip L, G, E bits, but not U. |
| 178 | else |
| 179 | Operation ^= 15; // Flip all of the condition bits. |
| 180 | if (Operation > ISD::SETTRUE2) |
| 181 | Operation &= ~8; // Don't let N and U bits get set. |
| 182 | return ISD::CondCode(Operation); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /// isSignedOp - For an integer comparison, return 1 if the comparison is a |
| 187 | /// signed operation and 2 if the result is an unsigned comparison. Return zero |
| 188 | /// if the operation does not depend on the sign of the input (setne and seteq). |
| 189 | static int isSignedOp(ISD::CondCode Opcode) { |
| 190 | switch (Opcode) { |
| 191 | default: assert(0 && "Illegal integer setcc operation!"); |
| 192 | case ISD::SETEQ: |
| 193 | case ISD::SETNE: return 0; |
| 194 | case ISD::SETLT: |
| 195 | case ISD::SETLE: |
| 196 | case ISD::SETGT: |
| 197 | case ISD::SETGE: return 1; |
| 198 | case ISD::SETULT: |
| 199 | case ISD::SETULE: |
| 200 | case ISD::SETUGT: |
| 201 | case ISD::SETUGE: return 2; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /// getSetCCOrOperation - Return the result of a logical OR between different |
| 206 | /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function |
| 207 | /// returns SETCC_INVALID if it is not possible to represent the resultant |
| 208 | /// comparison. |
| 209 | ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 210 | bool isInteger) { |
| 211 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 212 | // Cannot fold a signed integer setcc with an unsigned integer setcc. |
| 213 | return ISD::SETCC_INVALID; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 214 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 215 | unsigned Op = Op1 | Op2; // Combine all of the condition bits. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 216 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 217 | // If the N and U bits get set then the resultant comparison DOES suddenly |
| 218 | // care about orderedness, and is true when ordered. |
| 219 | if (Op > ISD::SETTRUE2) |
Chris Lattner | e41102b | 2006-05-12 17:03:46 +0000 | [diff] [blame^] | 220 | Op &= ~16; // Clear the U bit if the N bit is set. |
| 221 | |
| 222 | // Canonicalize illegal integer setcc's. |
| 223 | if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT |
| 224 | Op = ISD::SETNE; |
| 225 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 226 | return ISD::CondCode(Op); |
| 227 | } |
| 228 | |
| 229 | /// getSetCCAndOperation - Return the result of a logical AND between different |
| 230 | /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This |
| 231 | /// function returns zero if it is not possible to represent the resultant |
| 232 | /// comparison. |
| 233 | ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 234 | bool isInteger) { |
| 235 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 236 | // Cannot fold a signed setcc with an unsigned setcc. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 237 | return ISD::SETCC_INVALID; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 238 | |
| 239 | // Combine all of the condition bits. |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 240 | ISD::CondCode Result = ISD::CondCode(Op1 & Op2); |
| 241 | |
| 242 | // Canonicalize illegal integer setcc's. |
| 243 | if (isInteger) { |
| 244 | switch (Result) { |
| 245 | default: break; |
| 246 | case ISD::SETUO: // e.g. SETUGT & SETULT |
| 247 | Result = ISD::SETFALSE; |
| 248 | break; |
| 249 | case ISD::SETUEQ: // e.g. SETUGE & SETULE |
| 250 | Result = ISD::SETEQ; |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return Result; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 258 | const TargetMachine &SelectionDAG::getTarget() const { |
| 259 | return TLI.getTargetMachine(); |
| 260 | } |
| 261 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 262 | //===----------------------------------------------------------------------===// |
| 263 | // SelectionDAG Class |
| 264 | //===----------------------------------------------------------------------===// |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 266 | /// RemoveDeadNodes - This method deletes all unreachable nodes in the |
| 267 | /// SelectionDAG, including nodes (like loads) that have uses of their token |
| 268 | /// chain but no other uses and no side effect. If a node is passed in as an |
| 269 | /// argument, it is used as the seed for node deletion. |
| 270 | void SelectionDAG::RemoveDeadNodes(SDNode *N) { |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 271 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 272 | // to the root node, preventing it from being deleted. |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 273 | HandleSDNode Dummy(getRoot()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 274 | |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 275 | bool MadeChange = false; |
| 276 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 277 | // If we have a hint to start from, use it. |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 278 | if (N && N->use_empty()) { |
| 279 | DestroyDeadNode(N); |
| 280 | MadeChange = true; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 283 | for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) |
| 284 | if (I->use_empty() && I->getOpcode() != 65535) { |
| 285 | // Node is dead, recursively delete newly dead uses. |
| 286 | DestroyDeadNode(I); |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 287 | MadeChange = true; |
| 288 | } |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 289 | |
| 290 | // Walk the nodes list, removing the nodes we've marked as dead. |
| 291 | if (MadeChange) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 292 | for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) { |
| 293 | SDNode *N = I++; |
| 294 | if (N->use_empty()) |
| 295 | AllNodes.erase(N); |
| 296 | } |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 299 | // If the root changed (e.g. it was a dead load, update the root). |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 300 | setRoot(Dummy.getValue()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 303 | /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the |
| 304 | /// graph. If it is the last user of any of its operands, recursively process |
| 305 | /// them the same way. |
| 306 | /// |
| 307 | void SelectionDAG::DestroyDeadNode(SDNode *N) { |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 308 | // Okay, we really are going to delete this node. First take this out of the |
| 309 | // appropriate CSE map. |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 310 | RemoveNodeFromCSEMaps(N); |
| 311 | |
| 312 | // Next, brutally remove the operand list. This is safe to do, as there are |
| 313 | // no cycles in the graph. |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 314 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { |
| 315 | SDNode *O = I->Val; |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 316 | O->removeUser(N); |
| 317 | |
| 318 | // Now that we removed this operand, see if there are no uses of it left. |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 319 | if (O->use_empty()) |
| 320 | DestroyDeadNode(O); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 322 | delete[] N->OperandList; |
| 323 | N->OperandList = 0; |
| 324 | N->NumOperands = 0; |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 325 | |
| 326 | // Mark the node as dead. |
| 327 | N->MorphNodeTo(65535); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 330 | void SelectionDAG::DeleteNode(SDNode *N) { |
| 331 | assert(N->use_empty() && "Cannot delete a node that is not dead!"); |
| 332 | |
| 333 | // First take this out of the appropriate CSE map. |
| 334 | RemoveNodeFromCSEMaps(N); |
| 335 | |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 336 | // Finally, remove uses due to operands of this node, remove from the |
| 337 | // AllNodes list, and delete the node. |
| 338 | DeleteNodeNotInCSEMaps(N); |
| 339 | } |
| 340 | |
| 341 | void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { |
| 342 | |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 343 | // Remove it from the AllNodes list. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 344 | AllNodes.remove(N); |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 345 | |
| 346 | // Drop all of the operands and decrement used nodes use counts. |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 347 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) |
| 348 | I->Val->removeUser(N); |
| 349 | delete[] N->OperandList; |
| 350 | N->OperandList = 0; |
| 351 | N->NumOperands = 0; |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 352 | |
| 353 | delete N; |
| 354 | } |
| 355 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 356 | /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that |
| 357 | /// correspond to it. This is useful when we're about to delete or repurpose |
| 358 | /// the node. We don't want future request for structurally identical nodes |
| 359 | /// to return N anymore. |
| 360 | void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 361 | bool Erased = false; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 362 | switch (N->getOpcode()) { |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 363 | case ISD::HANDLENODE: return; // noop. |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 364 | case ISD::Constant: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 365 | Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), |
| 366 | N->getValueType(0))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 367 | break; |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 368 | case ISD::TargetConstant: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 369 | Erased = TargetConstants.erase(std::make_pair( |
| 370 | cast<ConstantSDNode>(N)->getValue(), |
| 371 | N->getValueType(0))); |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 372 | break; |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 373 | case ISD::ConstantFP: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 374 | uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 375 | Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 376 | break; |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 378 | case ISD::TargetConstantFP: { |
| 379 | uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); |
| 380 | Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0))); |
| 381 | break; |
| 382 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 383 | case ISD::STRING: |
| 384 | Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); |
| 385 | break; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 386 | case ISD::CONDCODE: |
| 387 | assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && |
| 388 | "Cond code doesn't exist!"); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 389 | Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 390 | CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; |
| 391 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 392 | case ISD::GlobalAddress: { |
| 393 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); |
| 394 | Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(), |
| 395 | GN->getOffset())); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 396 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 397 | } |
| 398 | case ISD::TargetGlobalAddress: { |
| 399 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); |
| 400 | Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(), |
| 401 | GN->getOffset())); |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 402 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 403 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 404 | case ISD::FrameIndex: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 405 | Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 406 | break; |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 407 | case ISD::TargetFrameIndex: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 408 | Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 409 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 410 | case ISD::JumpTable: |
| 411 | Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex()); |
| 412 | break; |
| 413 | case ISD::TargetJumpTable: |
| 414 | Erased = |
| 415 | TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex()); |
| 416 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 417 | case ISD::ConstantPool: |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 418 | Erased = ConstantPoolIndices. |
| 419 | erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(), |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 420 | std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(), |
| 421 | cast<ConstantPoolSDNode>(N)->getAlignment()))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 422 | break; |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 423 | case ISD::TargetConstantPool: |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 424 | Erased = TargetConstantPoolIndices. |
| 425 | erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(), |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 426 | std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(), |
| 427 | cast<ConstantPoolSDNode>(N)->getAlignment()))); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 428 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 429 | case ISD::BasicBlock: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 430 | Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 431 | break; |
| 432 | case ISD::ExternalSymbol: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 433 | Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 434 | break; |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 435 | case ISD::TargetExternalSymbol: |
Chris Lattner | 809ec11 | 2006-01-28 10:09:25 +0000 | [diff] [blame] | 436 | Erased = |
| 437 | TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 438 | break; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 439 | case ISD::VALUETYPE: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 440 | Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 441 | ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; |
| 442 | break; |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 443 | case ISD::Register: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 444 | Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(), |
| 445 | N->getValueType(0))); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 446 | break; |
Chris Lattner | c534395 | 2005-08-05 16:55:31 +0000 | [diff] [blame] | 447 | case ISD::SRCVALUE: { |
| 448 | SrcValueSDNode *SVN = cast<SrcValueSDNode>(N); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 449 | Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset())); |
Chris Lattner | c534395 | 2005-08-05 16:55:31 +0000 | [diff] [blame] | 450 | break; |
| 451 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 452 | case ISD::LOAD: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 453 | Erased = Loads.erase(std::make_pair(N->getOperand(1), |
| 454 | std::make_pair(N->getOperand(0), |
| 455 | N->getValueType(0)))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 456 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 457 | default: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 458 | if (N->getNumValues() == 1) { |
Chris Lattner | 70b9b10 | 2005-09-02 19:36:17 +0000 | [diff] [blame] | 459 | if (N->getNumOperands() == 0) { |
| 460 | Erased = NullaryOps.erase(std::make_pair(N->getOpcode(), |
| 461 | N->getValueType(0))); |
| 462 | } else if (N->getNumOperands() == 1) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 463 | Erased = |
| 464 | UnaryOps.erase(std::make_pair(N->getOpcode(), |
| 465 | std::make_pair(N->getOperand(0), |
| 466 | N->getValueType(0)))); |
| 467 | } else if (N->getNumOperands() == 2) { |
| 468 | Erased = |
| 469 | BinaryOps.erase(std::make_pair(N->getOpcode(), |
| 470 | std::make_pair(N->getOperand(0), |
| 471 | N->getOperand(1)))); |
| 472 | } else { |
| 473 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 474 | Erased = |
| 475 | OneResultNodes.erase(std::make_pair(N->getOpcode(), |
| 476 | std::make_pair(N->getValueType(0), |
| 477 | Ops))); |
| 478 | } |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 479 | } else { |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 480 | // Remove the node from the ArbitraryNodes map. |
| 481 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 482 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 483 | Erased = |
| 484 | ArbitraryNodes.erase(std::make_pair(N->getOpcode(), |
| 485 | std::make_pair(RV, Ops))); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 486 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 487 | break; |
| 488 | } |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 489 | #ifndef NDEBUG |
| 490 | // Verify that the node was actually in one of the CSE maps, unless it has a |
| 491 | // flag result (which cannot be CSE'd) or is one of the special cases that are |
| 492 | // not subject to CSE. |
| 493 | if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 494 | !N->isTargetOpcode()) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 495 | N->dump(); |
| 496 | assert(0 && "Node is not in map!"); |
| 497 | } |
| 498 | #endif |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 501 | /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It |
| 502 | /// has been taken out and modified in some way. If the specified node already |
| 503 | /// exists in the CSE maps, do not modify the maps, but return the existing node |
| 504 | /// instead. If it doesn't exist, add it and return null. |
| 505 | /// |
| 506 | SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { |
| 507 | assert(N->getNumOperands() && "This is a leaf node!"); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 508 | if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 509 | return 0; // Never add these nodes. |
Chris Lattner | c85a9f3 | 2005-11-30 18:20:52 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 9f8cc69 | 2005-12-19 22:21:21 +0000 | [diff] [blame] | 511 | // Check that remaining values produced are not flags. |
| 512 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
| 513 | if (N->getValueType(i) == MVT::Flag) |
| 514 | return 0; // Never CSE anything that produces a flag. |
| 515 | |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 516 | if (N->getNumValues() == 1) { |
| 517 | if (N->getNumOperands() == 1) { |
| 518 | SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(), |
| 519 | std::make_pair(N->getOperand(0), |
| 520 | N->getValueType(0)))]; |
| 521 | if (U) return U; |
| 522 | U = N; |
| 523 | } else if (N->getNumOperands() == 2) { |
| 524 | SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(), |
| 525 | std::make_pair(N->getOperand(0), |
| 526 | N->getOperand(1)))]; |
| 527 | if (B) return B; |
| 528 | B = N; |
| 529 | } else { |
| 530 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 531 | SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(), |
Chris Lattner | 809ec11 | 2006-01-28 10:09:25 +0000 | [diff] [blame] | 532 | std::make_pair(N->getValueType(0), Ops))]; |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 533 | if (ORN) return ORN; |
| 534 | ORN = N; |
| 535 | } |
| 536 | } else { |
| 537 | if (N->getOpcode() == ISD::LOAD) { |
| 538 | SDNode *&L = Loads[std::make_pair(N->getOperand(1), |
| 539 | std::make_pair(N->getOperand(0), |
| 540 | N->getValueType(0)))]; |
| 541 | if (L) return L; |
| 542 | L = N; |
| 543 | } else { |
| 544 | // Remove the node from the ArbitraryNodes map. |
| 545 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 546 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 547 | SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(), |
| 548 | std::make_pair(RV, Ops))]; |
| 549 | if (AN) return AN; |
| 550 | AN = N; |
| 551 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 552 | } |
| 553 | return 0; |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 556 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
| 557 | /// were replaced with those specified. If this node is never memoized, |
| 558 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 559 | /// node already exists with these operands, the slot will be non-null. |
| 560 | SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) { |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 561 | if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 562 | return 0; // Never add these nodes. |
| 563 | |
| 564 | // Check that remaining values produced are not flags. |
| 565 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
| 566 | if (N->getValueType(i) == MVT::Flag) |
| 567 | return 0; // Never CSE anything that produces a flag. |
| 568 | |
| 569 | if (N->getNumValues() == 1) { |
| 570 | return &UnaryOps[std::make_pair(N->getOpcode(), |
| 571 | std::make_pair(Op, N->getValueType(0)))]; |
| 572 | } else { |
| 573 | // Remove the node from the ArbitraryNodes map. |
| 574 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 575 | std::vector<SDOperand> Ops; |
| 576 | Ops.push_back(Op); |
| 577 | return &ArbitraryNodes[std::make_pair(N->getOpcode(), |
| 578 | std::make_pair(RV, Ops))]; |
| 579 | } |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
| 584 | /// were replaced with those specified. If this node is never memoized, |
| 585 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 586 | /// node already exists with these operands, the slot will be non-null. |
| 587 | SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, |
| 588 | SDOperand Op1, SDOperand Op2) { |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 589 | if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 590 | return 0; // Never add these nodes. |
| 591 | |
| 592 | // Check that remaining values produced are not flags. |
| 593 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
| 594 | if (N->getValueType(i) == MVT::Flag) |
| 595 | return 0; // Never CSE anything that produces a flag. |
| 596 | |
| 597 | if (N->getNumValues() == 1) { |
| 598 | return &BinaryOps[std::make_pair(N->getOpcode(), |
| 599 | std::make_pair(Op1, Op2))]; |
| 600 | } else { |
| 601 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 602 | std::vector<SDOperand> Ops; |
| 603 | Ops.push_back(Op1); |
| 604 | Ops.push_back(Op2); |
| 605 | return &ArbitraryNodes[std::make_pair(N->getOpcode(), |
| 606 | std::make_pair(RV, Ops))]; |
| 607 | } |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /// FindModifiedNodeSlot - Find a slot for the specified node if its operands |
| 613 | /// were replaced with those specified. If this node is never memoized, |
| 614 | /// return null, otherwise return a pointer to the slot it would take. If a |
| 615 | /// node already exists with these operands, the slot will be non-null. |
| 616 | SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, |
| 617 | const std::vector<SDOperand> &Ops) { |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 618 | if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 619 | return 0; // Never add these nodes. |
| 620 | |
| 621 | // Check that remaining values produced are not flags. |
| 622 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
| 623 | if (N->getValueType(i) == MVT::Flag) |
| 624 | return 0; // Never CSE anything that produces a flag. |
| 625 | |
| 626 | if (N->getNumValues() == 1) { |
| 627 | if (N->getNumOperands() == 1) { |
| 628 | return &UnaryOps[std::make_pair(N->getOpcode(), |
| 629 | std::make_pair(Ops[0], |
| 630 | N->getValueType(0)))]; |
| 631 | } else if (N->getNumOperands() == 2) { |
| 632 | return &BinaryOps[std::make_pair(N->getOpcode(), |
| 633 | std::make_pair(Ops[0], Ops[1]))]; |
| 634 | } else { |
| 635 | return &OneResultNodes[std::make_pair(N->getOpcode(), |
| 636 | std::make_pair(N->getValueType(0), |
| 637 | Ops))]; |
| 638 | } |
| 639 | } else { |
| 640 | if (N->getOpcode() == ISD::LOAD) { |
| 641 | return &Loads[std::make_pair(Ops[1], |
| 642 | std::make_pair(Ops[0], N->getValueType(0)))]; |
| 643 | } else { |
| 644 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 645 | return &ArbitraryNodes[std::make_pair(N->getOpcode(), |
| 646 | std::make_pair(RV, Ops))]; |
| 647 | } |
| 648 | } |
| 649 | return 0; |
| 650 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 653 | SelectionDAG::~SelectionDAG() { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 654 | while (!AllNodes.empty()) { |
| 655 | SDNode *N = AllNodes.begin(); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 656 | delete [] N->OperandList; |
| 657 | N->OperandList = 0; |
| 658 | N->NumOperands = 0; |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 659 | AllNodes.pop_front(); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 660 | } |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Chris Lattner | 0f2287b | 2005-04-13 02:38:18 +0000 | [diff] [blame] | 663 | SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { |
Chris Lattner | 51679c4 | 2005-04-13 19:41:05 +0000 | [diff] [blame] | 664 | if (Op.getValueType() == VT) return Op; |
Jeff Cohen | 19bb228 | 2005-05-10 02:22:38 +0000 | [diff] [blame] | 665 | int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); |
Chris Lattner | 0f2287b | 2005-04-13 02:38:18 +0000 | [diff] [blame] | 666 | return getNode(ISD::AND, Op.getValueType(), Op, |
| 667 | getConstant(Imm, Op.getValueType())); |
| 668 | } |
| 669 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 670 | SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { |
| 671 | assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); |
Chris Lattner | f35b297 | 2006-03-28 19:04:49 +0000 | [diff] [blame] | 672 | assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!"); |
| 673 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 674 | // Mask out any bits that are not valid for this constant. |
Chris Lattner | 623f70d | 2005-01-08 06:24:30 +0000 | [diff] [blame] | 675 | if (VT != MVT::i64) |
| 676 | Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 677 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 678 | SDNode *&N = Constants[std::make_pair(Val, VT)]; |
| 679 | if (N) return SDOperand(N, 0); |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 680 | N = new ConstantSDNode(false, Val, VT); |
| 681 | AllNodes.push_back(N); |
| 682 | return SDOperand(N, 0); |
| 683 | } |
| 684 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 685 | SDOperand SelectionDAG::getString(const std::string &Val) { |
| 686 | StringSDNode *&N = StringNodes[Val]; |
| 687 | if (!N) { |
| 688 | N = new StringSDNode(Val); |
| 689 | AllNodes.push_back(N); |
| 690 | } |
| 691 | return SDOperand(N, 0); |
| 692 | } |
| 693 | |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 694 | SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) { |
| 695 | assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); |
| 696 | // Mask out any bits that are not valid for this constant. |
| 697 | if (VT != MVT::i64) |
| 698 | Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; |
| 699 | |
| 700 | SDNode *&N = TargetConstants[std::make_pair(Val, VT)]; |
| 701 | if (N) return SDOperand(N, 0); |
| 702 | N = new ConstantSDNode(true, Val, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 703 | AllNodes.push_back(N); |
| 704 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 707 | SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { |
| 708 | assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); |
| 709 | if (VT == MVT::f32) |
| 710 | Val = (float)Val; // Mask out extra precision. |
| 711 | |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 712 | // Do the map lookup using the actual bit pattern for the floating point |
| 713 | // value, so that we don't have problems with 0.0 comparing equal to -0.0, and |
| 714 | // we don't have issues with SNANs. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 715 | SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 716 | if (N) return SDOperand(N, 0); |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 717 | N = new ConstantFPSDNode(false, Val, VT); |
| 718 | AllNodes.push_back(N); |
| 719 | return SDOperand(N, 0); |
| 720 | } |
| 721 | |
| 722 | SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) { |
| 723 | assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); |
| 724 | if (VT == MVT::f32) |
| 725 | Val = (float)Val; // Mask out extra precision. |
| 726 | |
| 727 | // Do the map lookup using the actual bit pattern for the floating point |
| 728 | // value, so that we don't have problems with 0.0 comparing equal to -0.0, and |
| 729 | // we don't have issues with SNANs. |
| 730 | SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; |
| 731 | if (N) return SDOperand(N, 0); |
| 732 | N = new ConstantFPSDNode(true, Val, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 733 | AllNodes.push_back(N); |
| 734 | return SDOperand(N, 0); |
| 735 | } |
| 736 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 737 | SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 738 | MVT::ValueType VT, int offset) { |
| 739 | SDNode *&N = GlobalValues[std::make_pair(GV, offset)]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 740 | if (N) return SDOperand(N, 0); |
Nate Begeman | 512beb9 | 2005-12-30 00:10:38 +0000 | [diff] [blame] | 741 | N = new GlobalAddressSDNode(false, GV, VT, offset); |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 742 | AllNodes.push_back(N); |
| 743 | return SDOperand(N, 0); |
| 744 | } |
| 745 | |
| 746 | SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV, |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 747 | MVT::ValueType VT, int offset) { |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 748 | SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)]; |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 749 | if (N) return SDOperand(N, 0); |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 750 | N = new GlobalAddressSDNode(true, GV, VT, offset); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 751 | AllNodes.push_back(N); |
| 752 | return SDOperand(N, 0); |
| 753 | } |
| 754 | |
| 755 | SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { |
| 756 | SDNode *&N = FrameIndices[FI]; |
| 757 | if (N) return SDOperand(N, 0); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 758 | N = new FrameIndexSDNode(FI, VT, false); |
| 759 | AllNodes.push_back(N); |
| 760 | return SDOperand(N, 0); |
| 761 | } |
| 762 | |
| 763 | SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) { |
| 764 | SDNode *&N = TargetFrameIndices[FI]; |
| 765 | if (N) return SDOperand(N, 0); |
| 766 | N = new FrameIndexSDNode(FI, VT, true); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 767 | AllNodes.push_back(N); |
| 768 | return SDOperand(N, 0); |
| 769 | } |
| 770 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 771 | SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) { |
| 772 | SDNode *&N = JumpTableIndices[JTI]; |
| 773 | if (N) return SDOperand(N, 0); |
| 774 | N = new JumpTableSDNode(JTI, VT, false); |
| 775 | AllNodes.push_back(N); |
| 776 | return SDOperand(N, 0); |
| 777 | } |
| 778 | |
| 779 | SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) { |
| 780 | SDNode *&N = TargetJumpTableIndices[JTI]; |
| 781 | if (N) return SDOperand(N, 0); |
| 782 | N = new JumpTableSDNode(JTI, VT, true); |
| 783 | AllNodes.push_back(N); |
| 784 | return SDOperand(N, 0); |
| 785 | } |
| 786 | |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 787 | SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT, |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 788 | unsigned Alignment, int Offset) { |
| 789 | SDNode *&N = ConstantPoolIndices[std::make_pair(C, |
| 790 | std::make_pair(Offset, Alignment))]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 791 | if (N) return SDOperand(N, 0); |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 792 | N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 793 | AllNodes.push_back(N); |
| 794 | return SDOperand(N, 0); |
| 795 | } |
| 796 | |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 797 | SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT, |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 798 | unsigned Alignment, int Offset) { |
| 799 | SDNode *&N = TargetConstantPoolIndices[std::make_pair(C, |
| 800 | std::make_pair(Offset, Alignment))]; |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 801 | if (N) return SDOperand(N, 0); |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 802 | N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 803 | AllNodes.push_back(N); |
| 804 | return SDOperand(N, 0); |
| 805 | } |
| 806 | |
| 807 | SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { |
| 808 | SDNode *&N = BBNodes[MBB]; |
| 809 | if (N) return SDOperand(N, 0); |
| 810 | N = new BasicBlockSDNode(MBB); |
| 811 | AllNodes.push_back(N); |
| 812 | return SDOperand(N, 0); |
| 813 | } |
| 814 | |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 815 | SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { |
| 816 | if ((unsigned)VT >= ValueTypeNodes.size()) |
| 817 | ValueTypeNodes.resize(VT+1); |
| 818 | if (ValueTypeNodes[VT] == 0) { |
| 819 | ValueTypeNodes[VT] = new VTSDNode(VT); |
| 820 | AllNodes.push_back(ValueTypeNodes[VT]); |
| 821 | } |
| 822 | |
| 823 | return SDOperand(ValueTypeNodes[VT], 0); |
| 824 | } |
| 825 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 826 | SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { |
| 827 | SDNode *&N = ExternalSymbols[Sym]; |
| 828 | if (N) return SDOperand(N, 0); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 829 | N = new ExternalSymbolSDNode(false, Sym, VT); |
| 830 | AllNodes.push_back(N); |
| 831 | return SDOperand(N, 0); |
| 832 | } |
| 833 | |
Chris Lattner | 809ec11 | 2006-01-28 10:09:25 +0000 | [diff] [blame] | 834 | SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, |
| 835 | MVT::ValueType VT) { |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 836 | SDNode *&N = TargetExternalSymbols[Sym]; |
| 837 | if (N) return SDOperand(N, 0); |
| 838 | N = new ExternalSymbolSDNode(true, Sym, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 839 | AllNodes.push_back(N); |
| 840 | return SDOperand(N, 0); |
| 841 | } |
| 842 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 843 | SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { |
| 844 | if ((unsigned)Cond >= CondCodeNodes.size()) |
| 845 | CondCodeNodes.resize(Cond+1); |
| 846 | |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 847 | if (CondCodeNodes[Cond] == 0) { |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 848 | CondCodeNodes[Cond] = new CondCodeSDNode(Cond); |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 849 | AllNodes.push_back(CondCodeNodes[Cond]); |
| 850 | } |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 851 | return SDOperand(CondCodeNodes[Cond], 0); |
| 852 | } |
| 853 | |
Chris Lattner | 0fdd768 | 2005-08-30 22:38:38 +0000 | [diff] [blame] | 854 | SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { |
| 855 | RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)]; |
| 856 | if (!Reg) { |
| 857 | Reg = new RegisterSDNode(RegNo, VT); |
| 858 | AllNodes.push_back(Reg); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 859 | } |
Chris Lattner | 0fdd768 | 2005-08-30 22:38:38 +0000 | [diff] [blame] | 860 | return SDOperand(Reg, 0); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 863 | SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, |
| 864 | SDOperand N2, ISD::CondCode Cond) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 865 | // These setcc operations always fold. |
| 866 | switch (Cond) { |
| 867 | default: break; |
| 868 | case ISD::SETFALSE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 869 | case ISD::SETFALSE2: return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 870 | case ISD::SETTRUE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 871 | case ISD::SETTRUE2: return getConstant(1, VT); |
Chris Lattner | a83385f | 2006-04-27 05:01:07 +0000 | [diff] [blame] | 872 | |
| 873 | case ISD::SETOEQ: |
| 874 | case ISD::SETOGT: |
| 875 | case ISD::SETOGE: |
| 876 | case ISD::SETOLT: |
| 877 | case ISD::SETOLE: |
| 878 | case ISD::SETONE: |
| 879 | case ISD::SETO: |
| 880 | case ISD::SETUO: |
| 881 | case ISD::SETUEQ: |
| 882 | case ISD::SETUNE: |
| 883 | assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!"); |
| 884 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 887 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { |
| 888 | uint64_t C2 = N2C->getValue(); |
| 889 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 890 | uint64_t C1 = N1C->getValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 891 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 892 | // Sign extend the operands if required |
| 893 | if (ISD::isSignedIntSetCC(Cond)) { |
| 894 | C1 = N1C->getSignExtended(); |
| 895 | C2 = N2C->getSignExtended(); |
| 896 | } |
| 897 | |
| 898 | switch (Cond) { |
| 899 | default: assert(0 && "Unknown integer setcc!"); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 900 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 901 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 902 | case ISD::SETULT: return getConstant(C1 < C2, VT); |
| 903 | case ISD::SETUGT: return getConstant(C1 > C2, VT); |
| 904 | case ISD::SETULE: return getConstant(C1 <= C2, VT); |
| 905 | case ISD::SETUGE: return getConstant(C1 >= C2, VT); |
| 906 | case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); |
| 907 | case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); |
| 908 | case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); |
| 909 | case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 910 | } |
Chris Lattner | 2467392 | 2005-04-07 18:58:54 +0000 | [diff] [blame] | 911 | } else { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 912 | // If the LHS is a ZERO_EXTEND, perform the comparison on the input. |
Chris Lattner | 4a44c8d | 2005-04-18 04:30:45 +0000 | [diff] [blame] | 913 | if (N1.getOpcode() == ISD::ZERO_EXTEND) { |
| 914 | unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); |
| 915 | |
| 916 | // If the comparison constant has bits in the upper part, the |
| 917 | // zero-extended value could never match. |
| 918 | if (C2 & (~0ULL << InSize)) { |
| 919 | unsigned VSize = MVT::getSizeInBits(N1.getValueType()); |
| 920 | switch (Cond) { |
| 921 | case ISD::SETUGT: |
| 922 | case ISD::SETUGE: |
| 923 | case ISD::SETEQ: return getConstant(0, VT); |
| 924 | case ISD::SETULT: |
| 925 | case ISD::SETULE: |
| 926 | case ISD::SETNE: return getConstant(1, VT); |
| 927 | case ISD::SETGT: |
| 928 | case ISD::SETGE: |
| 929 | // True if the sign bit of C2 is set. |
| 930 | return getConstant((C2 & (1ULL << VSize)) != 0, VT); |
| 931 | case ISD::SETLT: |
| 932 | case ISD::SETLE: |
| 933 | // True if the sign bit of C2 isn't set. |
| 934 | return getConstant((C2 & (1ULL << VSize)) == 0, VT); |
| 935 | default: |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | // Otherwise, we can perform the comparison with the low bits. |
| 941 | switch (Cond) { |
| 942 | case ISD::SETEQ: |
| 943 | case ISD::SETNE: |
| 944 | case ISD::SETUGT: |
| 945 | case ISD::SETUGE: |
| 946 | case ISD::SETULT: |
| 947 | case ISD::SETULE: |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 948 | return getSetCC(VT, N1.getOperand(0), |
| 949 | getConstant(C2, N1.getOperand(0).getValueType()), |
| 950 | Cond); |
Chris Lattner | 4a44c8d | 2005-04-18 04:30:45 +0000 | [diff] [blame] | 951 | default: |
| 952 | break; // todo, be more careful with signed comparisons |
| 953 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 954 | } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 955 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 956 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT(); |
| 957 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 958 | MVT::ValueType ExtDstTy = N1.getValueType(); |
| 959 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 960 | |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 961 | // If the extended part has any inconsistent bits, it cannot ever |
| 962 | // compare equal. In other words, they have to be all ones or all |
| 963 | // zeros. |
| 964 | uint64_t ExtBits = |
Jeff Cohen | 7383ce4 | 2005-08-31 02:47:06 +0000 | [diff] [blame] | 965 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 966 | if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits) |
| 967 | return getConstant(Cond == ISD::SETNE, VT); |
| 968 | |
| 969 | // Otherwise, make this a use of a zext. |
| 970 | return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy), |
Jeff Cohen | 7383ce4 | 2005-08-31 02:47:06 +0000 | [diff] [blame] | 971 | getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy), |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 972 | Cond); |
Chris Lattner | 4a44c8d | 2005-04-18 04:30:45 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 975 | uint64_t MinVal, MaxVal; |
| 976 | unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); |
| 977 | if (ISD::isSignedIntSetCC(Cond)) { |
| 978 | MinVal = 1ULL << (OperandBitSize-1); |
| 979 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 980 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 981 | else |
| 982 | MaxVal = 0; |
| 983 | } else { |
| 984 | MinVal = 0; |
| 985 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 986 | } |
| 987 | |
| 988 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 989 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 990 | if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true |
| 991 | --C2; // X >= C1 --> X > (C1-1) |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 992 | return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), |
| 993 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 997 | if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true |
| 998 | ++C2; // X <= C1 --> X < (C1+1) |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 999 | return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), |
| 1000 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1001 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1002 | |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 1003 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) |
| 1004 | return getConstant(0, VT); // X < MIN --> false |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1005 | |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 1006 | // Canonicalize setgt X, Min --> setne X, Min |
| 1007 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1008 | return getSetCC(VT, N1, N2, ISD::SETNE); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 3b2c1d9 | 2005-04-12 00:28:49 +0000 | [diff] [blame] | 1010 | // If we have setult X, 1, turn it into seteq X, 0 |
| 1011 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1012 | return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), |
| 1013 | ISD::SETEQ); |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 1014 | // If we have setugt X, Max-1, turn it into seteq X, Max |
Chris Lattner | 3b2c1d9 | 2005-04-12 00:28:49 +0000 | [diff] [blame] | 1015 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1016 | return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), |
| 1017 | ISD::SETEQ); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1018 | |
| 1019 | // If we have "setcc X, C1", check to see if we can shrink the immediate |
| 1020 | // by changing cc. |
| 1021 | |
| 1022 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 1023 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 1024 | C2 == (~0ULL >> (65-OperandBitSize))) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1025 | return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1026 | |
| 1027 | // FIXME: Implement the rest of these. |
| 1028 | |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Fold bit comparisons when we can. |
| 1031 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 1032 | VT == N1.getValueType() && N1.getOpcode() == ISD::AND) |
| 1033 | if (ConstantSDNode *AndRHS = |
| 1034 | dyn_cast<ConstantSDNode>(N1.getOperand(1))) { |
| 1035 | if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 1036 | // Perform the xform if the AND RHS is a single bit. |
| 1037 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 1038 | return getNode(ISD::SRL, VT, N1, |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 1039 | getConstant(Log2_64(AndRHS->getValue()), |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 1040 | TLI.getShiftAmountTy())); |
| 1041 | } |
| 1042 | } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { |
| 1043 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 1044 | // Perform the xform if C2 is a single bit. |
| 1045 | if ((C2 & (C2-1)) == 0) { |
| 1046 | return getNode(ISD::SRL, VT, N1, |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 1047 | getConstant(Log2_64(C2),TLI.getShiftAmountTy())); |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1051 | } |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1052 | } else if (isa<ConstantSDNode>(N1.Val)) { |
| 1053 | // Ensure that the constant occurs on the RHS. |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1054 | return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1056 | |
| 1057 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) |
| 1058 | if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { |
| 1059 | double C1 = N1C->getValue(), C2 = N2C->getValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1061 | switch (Cond) { |
| 1062 | default: break; // FIXME: Implement the rest of these! |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 1063 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 1064 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 1065 | case ISD::SETLT: return getConstant(C1 < C2, VT); |
| 1066 | case ISD::SETGT: return getConstant(C1 > C2, VT); |
| 1067 | case ISD::SETLE: return getConstant(C1 <= C2, VT); |
| 1068 | case ISD::SETGE: return getConstant(C1 >= C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } else { |
| 1071 | // Ensure that the constant occurs on the RHS. |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 1072 | return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1075 | // Could not fold it. |
| 1076 | return SDOperand(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1079 | /// getNode - Gets or creates the specified node. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 1080 | /// |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1081 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { |
Chris Lattner | 70b9b10 | 2005-09-02 19:36:17 +0000 | [diff] [blame] | 1082 | SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)]; |
| 1083 | if (!N) { |
| 1084 | N = new SDNode(Opcode, VT); |
| 1085 | AllNodes.push_back(N); |
| 1086 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1087 | return SDOperand(N, 0); |
| 1088 | } |
| 1089 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1090 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1091 | SDOperand Operand) { |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 1092 | unsigned Tmp1; |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1093 | // Constant fold unary operations with an integer constant operand. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1094 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { |
| 1095 | uint64_t Val = C->getValue(); |
| 1096 | switch (Opcode) { |
| 1097 | default: break; |
| 1098 | case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 1099 | case ISD::ANY_EXTEND: |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1100 | case ISD::ZERO_EXTEND: return getConstant(Val, VT); |
| 1101 | case ISD::TRUNCATE: return getConstant(Val, VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 1102 | case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); |
| 1103 | case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1104 | case ISD::BIT_CONVERT: |
Chris Lattner | e8a30fd | 2006-03-24 02:20:47 +0000 | [diff] [blame] | 1105 | if (VT == MVT::f32 && C->getValueType(0) == MVT::i32) |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1106 | return getConstantFP(BitsToFloat(Val), VT); |
Chris Lattner | e8a30fd | 2006-03-24 02:20:47 +0000 | [diff] [blame] | 1107 | else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64) |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1108 | return getConstantFP(BitsToDouble(Val), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1109 | break; |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 1110 | case ISD::BSWAP: |
| 1111 | switch(VT) { |
| 1112 | default: assert(0 && "Invalid bswap!"); break; |
| 1113 | case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT); |
| 1114 | case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT); |
| 1115 | case MVT::i64: return getConstant(ByteSwap_64(Val), VT); |
| 1116 | } |
| 1117 | break; |
| 1118 | case ISD::CTPOP: |
| 1119 | switch(VT) { |
| 1120 | default: assert(0 && "Invalid ctpop!"); break; |
| 1121 | case MVT::i1: return getConstant(Val != 0, VT); |
| 1122 | case MVT::i8: |
| 1123 | Tmp1 = (unsigned)Val & 0xFF; |
| 1124 | return getConstant(CountPopulation_32(Tmp1), VT); |
| 1125 | case MVT::i16: |
| 1126 | Tmp1 = (unsigned)Val & 0xFFFF; |
| 1127 | return getConstant(CountPopulation_32(Tmp1), VT); |
| 1128 | case MVT::i32: |
| 1129 | return getConstant(CountPopulation_32((unsigned)Val), VT); |
| 1130 | case MVT::i64: |
| 1131 | return getConstant(CountPopulation_64(Val), VT); |
| 1132 | } |
| 1133 | case ISD::CTLZ: |
| 1134 | switch(VT) { |
| 1135 | default: assert(0 && "Invalid ctlz!"); break; |
| 1136 | case MVT::i1: return getConstant(Val == 0, VT); |
| 1137 | case MVT::i8: |
| 1138 | Tmp1 = (unsigned)Val & 0xFF; |
| 1139 | return getConstant(CountLeadingZeros_32(Tmp1)-24, VT); |
| 1140 | case MVT::i16: |
| 1141 | Tmp1 = (unsigned)Val & 0xFFFF; |
| 1142 | return getConstant(CountLeadingZeros_32(Tmp1)-16, VT); |
| 1143 | case MVT::i32: |
| 1144 | return getConstant(CountLeadingZeros_32((unsigned)Val), VT); |
| 1145 | case MVT::i64: |
| 1146 | return getConstant(CountLeadingZeros_64(Val), VT); |
| 1147 | } |
| 1148 | case ISD::CTTZ: |
| 1149 | switch(VT) { |
| 1150 | default: assert(0 && "Invalid cttz!"); break; |
| 1151 | case MVT::i1: return getConstant(Val == 0, VT); |
| 1152 | case MVT::i8: |
| 1153 | Tmp1 = (unsigned)Val | 0x100; |
| 1154 | return getConstant(CountTrailingZeros_32(Tmp1), VT); |
| 1155 | case MVT::i16: |
| 1156 | Tmp1 = (unsigned)Val | 0x10000; |
| 1157 | return getConstant(CountTrailingZeros_32(Tmp1), VT); |
| 1158 | case MVT::i32: |
| 1159 | return getConstant(CountTrailingZeros_32((unsigned)Val), VT); |
| 1160 | case MVT::i64: |
| 1161 | return getConstant(CountTrailingZeros_64(Val), VT); |
| 1162 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1166 | // Constant fold unary operations with an floating point constant operand. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1167 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) |
| 1168 | switch (Opcode) { |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 1169 | case ISD::FNEG: |
| 1170 | return getConstantFP(-C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1171 | case ISD::FABS: |
| 1172 | return getConstantFP(fabs(C->getValue()), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1173 | case ISD::FP_ROUND: |
| 1174 | case ISD::FP_EXTEND: |
| 1175 | return getConstantFP(C->getValue(), VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 1176 | case ISD::FP_TO_SINT: |
| 1177 | return getConstant((int64_t)C->getValue(), VT); |
| 1178 | case ISD::FP_TO_UINT: |
| 1179 | return getConstant((uint64_t)C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1180 | case ISD::BIT_CONVERT: |
Chris Lattner | e8a30fd | 2006-03-24 02:20:47 +0000 | [diff] [blame] | 1181 | if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1182 | return getConstant(FloatToBits(C->getValue()), VT); |
Chris Lattner | e8a30fd | 2006-03-24 02:20:47 +0000 | [diff] [blame] | 1183 | else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1184 | return getConstant(DoubleToBits(C->getValue()), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 1185 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | unsigned OpOpcode = Operand.Val->getOpcode(); |
| 1189 | switch (Opcode) { |
Chris Lattner | a93ec3e | 2005-01-21 18:01:22 +0000 | [diff] [blame] | 1190 | case ISD::TokenFactor: |
| 1191 | return Operand; // Factor of one node? No factor. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1192 | case ISD::SIGN_EXTEND: |
| 1193 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 1194 | assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1195 | if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) |
| 1196 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 1197 | break; |
| 1198 | case ISD::ZERO_EXTEND: |
| 1199 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 1200 | assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!"); |
Chris Lattner | 5a6bace | 2005-04-07 19:43:53 +0000 | [diff] [blame] | 1201 | if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) |
Chris Lattner | 2f0ca79 | 2005-01-12 18:51:15 +0000 | [diff] [blame] | 1202 | return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1203 | break; |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 1204 | case ISD::ANY_EXTEND: |
| 1205 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 1206 | assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!"); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 1207 | if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) |
| 1208 | // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) |
| 1209 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 1210 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1211 | case ISD::TRUNCATE: |
| 1212 | if (Operand.getValueType() == VT) return Operand; // noop truncate |
Nate Begeman | b0d04a7 | 2006-02-18 02:40:58 +0000 | [diff] [blame] | 1213 | assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!"); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1214 | if (OpOpcode == ISD::TRUNCATE) |
| 1215 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 1216 | else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || |
| 1217 | OpOpcode == ISD::ANY_EXTEND) { |
Chris Lattner | fd8c39b | 2005-01-07 21:56:24 +0000 | [diff] [blame] | 1218 | // If the source is smaller than the dest, we still need an extend. |
| 1219 | if (Operand.Val->getOperand(0).getValueType() < VT) |
| 1220 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 1221 | else if (Operand.Val->getOperand(0).getValueType() > VT) |
| 1222 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
| 1223 | else |
| 1224 | return Operand.Val->getOperand(0); |
| 1225 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1226 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 1227 | case ISD::BIT_CONVERT: |
| 1228 | // Basic sanity checking. |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 1229 | assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()) |
Chris Lattner | 28b5b1c | 2006-03-15 22:19:46 +0000 | [diff] [blame] | 1230 | && "Cannot BIT_CONVERT between two different types!"); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 1231 | if (VT == Operand.getValueType()) return Operand; // noop conversion. |
Chris Lattner | c8547d8 | 2005-12-23 05:37:50 +0000 | [diff] [blame] | 1232 | if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) |
| 1233 | return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); |
Chris Lattner | 08da55e | 2006-04-04 01:02:22 +0000 | [diff] [blame] | 1234 | if (OpOpcode == ISD::UNDEF) |
| 1235 | return getNode(ISD::UNDEF, VT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 1236 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1237 | case ISD::SCALAR_TO_VECTOR: |
| 1238 | assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) && |
| 1239 | MVT::getVectorBaseType(VT) == Operand.getValueType() && |
| 1240 | "Illegal SCALAR_TO_VECTOR node!"); |
| 1241 | break; |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 1242 | case ISD::FNEG: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1243 | if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) |
| 1244 | return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 1245 | Operand.Val->getOperand(0)); |
| 1246 | if (OpOpcode == ISD::FNEG) // --X -> X |
| 1247 | return Operand.Val->getOperand(0); |
| 1248 | break; |
| 1249 | case ISD::FABS: |
| 1250 | if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) |
| 1251 | return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); |
| 1252 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1255 | SDNode *N; |
| 1256 | if (VT != MVT::Flag) { // Don't CSE flag producing nodes |
| 1257 | SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; |
Chris Lattner | f07d023 | 2005-08-26 00:13:12 +0000 | [diff] [blame] | 1258 | if (E) return SDOperand(E, 0); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1259 | E = N = new SDNode(Opcode, Operand); |
| 1260 | } else { |
| 1261 | N = new SDNode(Opcode, Operand); |
| 1262 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1263 | N->setValueTypes(VT); |
| 1264 | AllNodes.push_back(N); |
| 1265 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Chris Lattner | 36019aa | 2005-04-18 03:48:41 +0000 | [diff] [blame] | 1268 | |
| 1269 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1270 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1271 | SDOperand N1, SDOperand N2) { |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1272 | #ifndef NDEBUG |
| 1273 | switch (Opcode) { |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 1274 | case ISD::TokenFactor: |
| 1275 | assert(VT == MVT::Other && N1.getValueType() == MVT::Other && |
| 1276 | N2.getValueType() == MVT::Other && "Invalid token factor!"); |
| 1277 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1278 | case ISD::AND: |
| 1279 | case ISD::OR: |
| 1280 | case ISD::XOR: |
| 1281 | case ISD::UDIV: |
| 1282 | case ISD::UREM: |
Chris Lattner | e5eb6f8 | 2005-05-15 05:39:08 +0000 | [diff] [blame] | 1283 | case ISD::MULHU: |
| 1284 | case ISD::MULHS: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1285 | assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); |
| 1286 | // fall through |
| 1287 | case ISD::ADD: |
| 1288 | case ISD::SUB: |
| 1289 | case ISD::MUL: |
| 1290 | case ISD::SDIV: |
| 1291 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1292 | assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); |
| 1293 | // fall through. |
| 1294 | case ISD::FADD: |
| 1295 | case ISD::FSUB: |
| 1296 | case ISD::FMUL: |
| 1297 | case ISD::FDIV: |
| 1298 | case ISD::FREM: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1299 | assert(N1.getValueType() == N2.getValueType() && |
| 1300 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 1301 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 1302 | case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match. |
| 1303 | assert(N1.getValueType() == VT && |
| 1304 | MVT::isFloatingPoint(N1.getValueType()) && |
| 1305 | MVT::isFloatingPoint(N2.getValueType()) && |
| 1306 | "Invalid FCOPYSIGN!"); |
| 1307 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1308 | case ISD::SHL: |
| 1309 | case ISD::SRA: |
| 1310 | case ISD::SRL: |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1311 | case ISD::ROTL: |
| 1312 | case ISD::ROTR: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1313 | assert(VT == N1.getValueType() && |
| 1314 | "Shift operators return type must be the same as their first arg"); |
| 1315 | assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 1316 | VT != MVT::i1 && "Shifts only work on integers"); |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1317 | break; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1318 | case ISD::FP_ROUND_INREG: { |
| 1319 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 1320 | assert(VT == N1.getValueType() && "Not an inreg round!"); |
| 1321 | assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && |
| 1322 | "Cannot FP_ROUND_INREG integer types"); |
| 1323 | assert(EVT <= VT && "Not rounding down!"); |
| 1324 | break; |
| 1325 | } |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 1326 | case ISD::AssertSext: |
| 1327 | case ISD::AssertZext: |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1328 | case ISD::SIGN_EXTEND_INREG: { |
| 1329 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 1330 | assert(VT == N1.getValueType() && "Not an inreg extend!"); |
| 1331 | assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && |
| 1332 | "Cannot *_EXTEND_INREG FP types"); |
| 1333 | assert(EVT <= VT && "Not extending!"); |
| 1334 | } |
| 1335 | |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1336 | default: break; |
| 1337 | } |
| 1338 | #endif |
| 1339 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1340 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 1341 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 1342 | if (N1C) { |
Chris Lattner | b9ebacd | 2006-05-06 23:05:41 +0000 | [diff] [blame] | 1343 | if (Opcode == ISD::SIGN_EXTEND_INREG) { |
| 1344 | int64_t Val = N1C->getValue(); |
| 1345 | unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT()); |
| 1346 | Val <<= 64-FromBits; |
| 1347 | Val >>= 64-FromBits; |
| 1348 | return getConstant(Val, VT); |
| 1349 | } |
| 1350 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1351 | if (N2C) { |
| 1352 | uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); |
| 1353 | switch (Opcode) { |
| 1354 | case ISD::ADD: return getConstant(C1 + C2, VT); |
| 1355 | case ISD::SUB: return getConstant(C1 - C2, VT); |
| 1356 | case ISD::MUL: return getConstant(C1 * C2, VT); |
| 1357 | case ISD::UDIV: |
| 1358 | if (C2) return getConstant(C1 / C2, VT); |
| 1359 | break; |
| 1360 | case ISD::UREM : |
| 1361 | if (C2) return getConstant(C1 % C2, VT); |
| 1362 | break; |
| 1363 | case ISD::SDIV : |
| 1364 | if (C2) return getConstant(N1C->getSignExtended() / |
| 1365 | N2C->getSignExtended(), VT); |
| 1366 | break; |
| 1367 | case ISD::SREM : |
| 1368 | if (C2) return getConstant(N1C->getSignExtended() % |
| 1369 | N2C->getSignExtended(), VT); |
| 1370 | break; |
| 1371 | case ISD::AND : return getConstant(C1 & C2, VT); |
| 1372 | case ISD::OR : return getConstant(C1 | C2, VT); |
| 1373 | case ISD::XOR : return getConstant(C1 ^ C2, VT); |
Nate Begeman | b85dfab | 2005-08-31 00:27:53 +0000 | [diff] [blame] | 1374 | case ISD::SHL : return getConstant(C1 << C2, VT); |
| 1375 | case ISD::SRL : return getConstant(C1 >> C2, VT); |
Chris Lattner | 8136d1f | 2005-01-10 00:07:15 +0000 | [diff] [blame] | 1376 | case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1377 | case ISD::ROTL : |
| 1378 | return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), |
| 1379 | VT); |
| 1380 | case ISD::ROTR : |
| 1381 | return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), |
| 1382 | VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1383 | default: break; |
| 1384 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1385 | } else { // Cannonicalize constant to RHS if commutative |
| 1386 | if (isCommutativeBinOp(Opcode)) { |
| 1387 | std::swap(N1C, N2C); |
| 1388 | std::swap(N1, N2); |
| 1389 | } |
| 1390 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); |
| 1394 | ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1395 | if (N1CFP) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1396 | if (N2CFP) { |
| 1397 | double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); |
| 1398 | switch (Opcode) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1399 | case ISD::FADD: return getConstantFP(C1 + C2, VT); |
| 1400 | case ISD::FSUB: return getConstantFP(C1 - C2, VT); |
| 1401 | case ISD::FMUL: return getConstantFP(C1 * C2, VT); |
| 1402 | case ISD::FDIV: |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1403 | if (C2) return getConstantFP(C1 / C2, VT); |
| 1404 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1405 | case ISD::FREM : |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1406 | if (C2) return getConstantFP(fmod(C1, C2), VT); |
| 1407 | break; |
Chris Lattner | 3c232c8 | 2006-03-05 23:57:58 +0000 | [diff] [blame] | 1408 | case ISD::FCOPYSIGN: { |
| 1409 | union { |
| 1410 | double F; |
| 1411 | uint64_t I; |
| 1412 | } u1; |
| 1413 | union { |
| 1414 | double F; |
| 1415 | int64_t I; |
| 1416 | } u2; |
| 1417 | u1.F = C1; |
| 1418 | u2.F = C2; |
| 1419 | if (u2.I < 0) // Sign bit of RHS set? |
| 1420 | u1.I |= 1ULL << 63; // Set the sign bit of the LHS. |
| 1421 | else |
| 1422 | u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS. |
| 1423 | return getConstantFP(u1.F, VT); |
| 1424 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1425 | default: break; |
| 1426 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1427 | } else { // Cannonicalize constant to RHS if commutative |
| 1428 | if (isCommutativeBinOp(Opcode)) { |
| 1429 | std::swap(N1CFP, N2CFP); |
| 1430 | std::swap(N1, N2); |
| 1431 | } |
| 1432 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1433 | } |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1434 | |
| 1435 | // Canonicalize an UNDEF to the RHS, even over a constant. |
| 1436 | if (N1.getOpcode() == ISD::UNDEF) { |
| 1437 | if (isCommutativeBinOp(Opcode)) { |
| 1438 | std::swap(N1, N2); |
| 1439 | } else { |
| 1440 | switch (Opcode) { |
| 1441 | case ISD::FP_ROUND_INREG: |
| 1442 | case ISD::SIGN_EXTEND_INREG: |
| 1443 | case ISD::SUB: |
| 1444 | case ISD::FSUB: |
| 1445 | case ISD::FDIV: |
| 1446 | case ISD::FREM: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 1447 | case ISD::SRA: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1448 | return N1; // fold op(undef, arg2) -> undef |
| 1449 | case ISD::UDIV: |
| 1450 | case ISD::SDIV: |
| 1451 | case ISD::UREM: |
| 1452 | case ISD::SREM: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 1453 | case ISD::SRL: |
| 1454 | case ISD::SHL: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1455 | return getConstant(0, VT); // fold op(undef, arg2) -> 0 |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
Chris Lattner | b9ebacd | 2006-05-06 23:05:41 +0000 | [diff] [blame] | 1460 | // Fold a bunch of operators when the RHS is undef. |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1461 | if (N2.getOpcode() == ISD::UNDEF) { |
| 1462 | switch (Opcode) { |
| 1463 | case ISD::ADD: |
| 1464 | case ISD::SUB: |
| 1465 | case ISD::FADD: |
| 1466 | case ISD::FSUB: |
| 1467 | case ISD::FMUL: |
| 1468 | case ISD::FDIV: |
| 1469 | case ISD::FREM: |
| 1470 | case ISD::UDIV: |
| 1471 | case ISD::SDIV: |
| 1472 | case ISD::UREM: |
| 1473 | case ISD::SREM: |
| 1474 | case ISD::XOR: |
| 1475 | return N2; // fold op(arg1, undef) -> undef |
| 1476 | case ISD::MUL: |
| 1477 | case ISD::AND: |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 1478 | case ISD::SRL: |
| 1479 | case ISD::SHL: |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1480 | return getConstant(0, VT); // fold op(arg1, undef) -> 0 |
| 1481 | case ISD::OR: |
| 1482 | return getConstant(MVT::getIntVTBitMask(VT), VT); |
Chris Lattner | 2cfd674 | 2006-05-08 17:29:49 +0000 | [diff] [blame] | 1483 | case ISD::SRA: |
| 1484 | return N1; |
Chris Lattner | 62b5772 | 2006-04-20 05:39:12 +0000 | [diff] [blame] | 1485 | } |
| 1486 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1488 | // Finally, fold operations that do not require constants. |
| 1489 | switch (Opcode) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1490 | case ISD::FP_ROUND_INREG: |
| 1491 | if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. |
| 1492 | break; |
| 1493 | case ISD::SIGN_EXTEND_INREG: { |
| 1494 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 1495 | if (EVT == VT) return N1; // Not actually extending |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | |
Nate Begeman | eea805e | 2005-04-13 21:23:31 +0000 | [diff] [blame] | 1499 | // FIXME: figure out how to safely handle things like |
| 1500 | // int foo(int x) { return 1 << (x & 255); } |
| 1501 | // int bar() { return foo(256); } |
| 1502 | #if 0 |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1503 | case ISD::SHL: |
| 1504 | case ISD::SRL: |
| 1505 | case ISD::SRA: |
Chris Lattner | e666fcf | 2005-04-13 02:58:13 +0000 | [diff] [blame] | 1506 | if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1507 | cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1508 | return getNode(Opcode, VT, N1, N2.getOperand(0)); |
Chris Lattner | e666fcf | 2005-04-13 02:58:13 +0000 | [diff] [blame] | 1509 | else if (N2.getOpcode() == ISD::AND) |
| 1510 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { |
| 1511 | // If the and is only masking out bits that cannot effect the shift, |
| 1512 | // eliminate the and. |
| 1513 | unsigned NumBits = MVT::getSizeInBits(VT); |
| 1514 | if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) |
| 1515 | return getNode(Opcode, VT, N1, N2.getOperand(0)); |
| 1516 | } |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1517 | break; |
Nate Begeman | eea805e | 2005-04-13 21:23:31 +0000 | [diff] [blame] | 1518 | #endif |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1521 | // Memoize this node if possible. |
| 1522 | SDNode *N; |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1523 | if (VT != MVT::Flag) { |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1524 | SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; |
| 1525 | if (BON) return SDOperand(BON, 0); |
| 1526 | |
| 1527 | BON = N = new SDNode(Opcode, N1, N2); |
| 1528 | } else { |
Chris Lattner | 88de6e7 | 2005-05-12 00:17:04 +0000 | [diff] [blame] | 1529 | N = new SDNode(Opcode, N1, N2); |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Chris Lattner | 3e01136 | 2005-05-14 07:45:46 +0000 | [diff] [blame] | 1532 | N->setValueTypes(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1533 | AllNodes.push_back(N); |
| 1534 | return SDOperand(N, 0); |
| 1535 | } |
| 1536 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1537 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1538 | SDOperand N1, SDOperand N2, SDOperand N3) { |
| 1539 | // Perform various simplifications. |
| 1540 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 1541 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 1542 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 1543 | switch (Opcode) { |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1544 | case ISD::SETCC: { |
| 1545 | // Use SimplifySetCC to simplify SETCC's. |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 1546 | SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1547 | if (Simp.Val) return Simp; |
| 1548 | break; |
| 1549 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1550 | case ISD::SELECT: |
| 1551 | if (N1C) |
| 1552 | if (N1C->getValue()) |
| 1553 | return N2; // select true, X, Y -> X |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1554 | else |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1555 | return N3; // select false, X, Y -> Y |
| 1556 | |
| 1557 | if (N2 == N3) return N2; // select C, X, X -> X |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1558 | break; |
Chris Lattner | 5351e9b | 2005-01-07 22:49:57 +0000 | [diff] [blame] | 1559 | case ISD::BRCOND: |
| 1560 | if (N2C) |
| 1561 | if (N2C->getValue()) // Unconditional branch |
| 1562 | return getNode(ISD::BR, MVT::Other, N1, N3); |
| 1563 | else |
| 1564 | return N1; // Never-taken branch |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 1565 | break; |
Chris Lattner | fb194b9 | 2006-03-19 23:56:04 +0000 | [diff] [blame] | 1566 | case ISD::VECTOR_SHUFFLE: |
| 1567 | assert(VT == N1.getValueType() && VT == N2.getValueType() && |
| 1568 | MVT::isVector(VT) && MVT::isVector(N3.getValueType()) && |
| 1569 | N3.getOpcode() == ISD::BUILD_VECTOR && |
| 1570 | MVT::getVectorNumElements(VT) == N3.getNumOperands() && |
| 1571 | "Illegal VECTOR_SHUFFLE node!"); |
| 1572 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 1575 | std::vector<SDOperand> Ops; |
| 1576 | Ops.reserve(3); |
| 1577 | Ops.push_back(N1); |
| 1578 | Ops.push_back(N2); |
| 1579 | Ops.push_back(N3); |
| 1580 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1581 | // Memoize node if it doesn't produce a flag. |
| 1582 | SDNode *N; |
| 1583 | if (VT != MVT::Flag) { |
| 1584 | SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))]; |
| 1585 | if (E) return SDOperand(E, 0); |
| 1586 | E = N = new SDNode(Opcode, N1, N2, N3); |
| 1587 | } else { |
| 1588 | N = new SDNode(Opcode, N1, N2, N3); |
| 1589 | } |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 1590 | N->setValueTypes(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1591 | AllNodes.push_back(N); |
| 1592 | return SDOperand(N, 0); |
| 1593 | } |
| 1594 | |
| 1595 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1596 | SDOperand N1, SDOperand N2, SDOperand N3, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1597 | SDOperand N4) { |
Chris Lattner | b7f7d51 | 2005-05-14 07:32:14 +0000 | [diff] [blame] | 1598 | std::vector<SDOperand> Ops; |
| 1599 | Ops.reserve(4); |
| 1600 | Ops.push_back(N1); |
| 1601 | Ops.push_back(N2); |
| 1602 | Ops.push_back(N3); |
| 1603 | Ops.push_back(N4); |
| 1604 | return getNode(Opcode, VT, Ops); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1607 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1608 | SDOperand N1, SDOperand N2, SDOperand N3, |
| 1609 | SDOperand N4, SDOperand N5) { |
| 1610 | std::vector<SDOperand> Ops; |
| 1611 | Ops.reserve(5); |
| 1612 | Ops.push_back(N1); |
| 1613 | Ops.push_back(N2); |
| 1614 | Ops.push_back(N3); |
| 1615 | Ops.push_back(N4); |
| 1616 | Ops.push_back(N5); |
| 1617 | return getNode(Opcode, VT, Ops); |
| 1618 | } |
| 1619 | |
Evan Cheng | 7038daf | 2005-12-10 00:37:58 +0000 | [diff] [blame] | 1620 | SDOperand SelectionDAG::getLoad(MVT::ValueType VT, |
| 1621 | SDOperand Chain, SDOperand Ptr, |
| 1622 | SDOperand SV) { |
| 1623 | SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; |
| 1624 | if (N) return SDOperand(N, 0); |
| 1625 | N = new SDNode(ISD::LOAD, Chain, Ptr, SV); |
| 1626 | |
| 1627 | // Loads have a token chain. |
| 1628 | setNodeValueTypes(N, VT, MVT::Other); |
| 1629 | AllNodes.push_back(N); |
| 1630 | return SDOperand(N, 0); |
| 1631 | } |
| 1632 | |
| 1633 | SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT, |
| 1634 | SDOperand Chain, SDOperand Ptr, |
| 1635 | SDOperand SV) { |
Evan Cheng | 7038daf | 2005-12-10 00:37:58 +0000 | [diff] [blame] | 1636 | std::vector<SDOperand> Ops; |
| 1637 | Ops.reserve(5); |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 1638 | Ops.push_back(Chain); |
| 1639 | Ops.push_back(Ptr); |
Evan Cheng | 7038daf | 2005-12-10 00:37:58 +0000 | [diff] [blame] | 1640 | Ops.push_back(SV); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1641 | Ops.push_back(getConstant(Count, MVT::i32)); |
| 1642 | Ops.push_back(getValueType(EVT)); |
Evan Cheng | 7038daf | 2005-12-10 00:37:58 +0000 | [diff] [blame] | 1643 | std::vector<MVT::ValueType> VTs; |
| 1644 | VTs.reserve(2); |
| 1645 | VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain. |
| 1646 | return getNode(ISD::VLOAD, VTs, Ops); |
| 1647 | } |
| 1648 | |
| 1649 | SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT, |
| 1650 | SDOperand Chain, SDOperand Ptr, SDOperand SV, |
| 1651 | MVT::ValueType EVT) { |
| 1652 | std::vector<SDOperand> Ops; |
| 1653 | Ops.reserve(4); |
| 1654 | Ops.push_back(Chain); |
| 1655 | Ops.push_back(Ptr); |
| 1656 | Ops.push_back(SV); |
| 1657 | Ops.push_back(getValueType(EVT)); |
| 1658 | std::vector<MVT::ValueType> VTs; |
| 1659 | VTs.reserve(2); |
| 1660 | VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. |
| 1661 | return getNode(Opcode, VTs, Ops); |
| 1662 | } |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 0437cdd | 2005-05-09 04:14:13 +0000 | [diff] [blame] | 1664 | SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 1665 | assert((!V || isa<PointerType>(V->getType())) && |
| 1666 | "SrcValue is not a pointer?"); |
Chris Lattner | 0437cdd | 2005-05-09 04:14:13 +0000 | [diff] [blame] | 1667 | SDNode *&N = ValueNodes[std::make_pair(V, Offset)]; |
| 1668 | if (N) return SDOperand(N, 0); |
| 1669 | |
| 1670 | N = new SrcValueSDNode(V, Offset); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1671 | AllNodes.push_back(N); |
| 1672 | return SDOperand(N, 0); |
| 1673 | } |
| 1674 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1675 | SDOperand SelectionDAG::getVAArg(MVT::ValueType VT, |
| 1676 | SDOperand Chain, SDOperand Ptr, |
| 1677 | SDOperand SV) { |
| 1678 | std::vector<SDOperand> Ops; |
| 1679 | Ops.reserve(3); |
| 1680 | Ops.push_back(Chain); |
| 1681 | Ops.push_back(Ptr); |
| 1682 | Ops.push_back(SV); |
| 1683 | std::vector<MVT::ValueType> VTs; |
| 1684 | VTs.reserve(2); |
| 1685 | VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. |
| 1686 | return getNode(ISD::VAARG, VTs, Ops); |
| 1687 | } |
| 1688 | |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1689 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1690 | std::vector<SDOperand> &Ops) { |
| 1691 | switch (Ops.size()) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1692 | case 0: return getNode(Opcode, VT); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1693 | case 1: return getNode(Opcode, VT, Ops[0]); |
| 1694 | case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); |
| 1695 | case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1696 | default: break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1697 | } |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 1698 | |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1699 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1700 | switch (Opcode) { |
| 1701 | default: break; |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1702 | case ISD::TRUNCSTORE: { |
| 1703 | assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!"); |
| 1704 | MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); |
| 1705 | #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store |
| 1706 | // If this is a truncating store of a constant, convert to the desired type |
| 1707 | // and store it instead. |
| 1708 | if (isa<Constant>(Ops[0])) { |
| 1709 | SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); |
| 1710 | if (isa<Constant>(Op)) |
| 1711 | N1 = Op; |
| 1712 | } |
| 1713 | // Also for ConstantFP? |
| 1714 | #endif |
| 1715 | if (Ops[0].getValueType() == EVT) // Normal store? |
| 1716 | return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]); |
| 1717 | assert(Ops[1].getValueType() > EVT && "Not a truncation?"); |
| 1718 | assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && |
| 1719 | "Can't do FP-INT conversion!"); |
| 1720 | break; |
| 1721 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1722 | case ISD::SELECT_CC: { |
Chris Lattner | ad13715 | 2005-10-05 06:37:22 +0000 | [diff] [blame] | 1723 | assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1724 | assert(Ops[0].getValueType() == Ops[1].getValueType() && |
| 1725 | "LHS and RHS of condition must have same type!"); |
| 1726 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 1727 | "True and False arms of SelectCC must have same type!"); |
| 1728 | assert(Ops[2].getValueType() == VT && |
| 1729 | "select_cc node must be of same type as true and false value!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1730 | break; |
| 1731 | } |
| 1732 | case ISD::BR_CC: { |
Chris Lattner | ad13715 | 2005-10-05 06:37:22 +0000 | [diff] [blame] | 1733 | assert(Ops.size() == 5 && "BR_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1734 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 1735 | "LHS/RHS of comparison should match types!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | } |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 1740 | // Memoize nodes. |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1741 | SDNode *N; |
| 1742 | if (VT != MVT::Flag) { |
| 1743 | SDNode *&E = |
| 1744 | OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; |
| 1745 | if (E) return SDOperand(E, 0); |
| 1746 | E = N = new SDNode(Opcode, Ops); |
| 1747 | } else { |
| 1748 | N = new SDNode(Opcode, Ops); |
| 1749 | } |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1750 | N->setValueTypes(VT); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1751 | AllNodes.push_back(N); |
| 1752 | return SDOperand(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1755 | SDOperand SelectionDAG::getNode(unsigned Opcode, |
| 1756 | std::vector<MVT::ValueType> &ResultTys, |
| 1757 | std::vector<SDOperand> &Ops) { |
| 1758 | if (ResultTys.size() == 1) |
| 1759 | return getNode(Opcode, ResultTys[0], Ops); |
| 1760 | |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1761 | switch (Opcode) { |
| 1762 | case ISD::EXTLOAD: |
| 1763 | case ISD::SEXTLOAD: |
| 1764 | case ISD::ZEXTLOAD: { |
| 1765 | MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT(); |
| 1766 | assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!"); |
| 1767 | // If they are asking for an extending load from/to the same thing, return a |
| 1768 | // normal load. |
| 1769 | if (ResultTys[0] == EVT) |
| 1770 | return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1771 | if (MVT::isVector(ResultTys[0])) { |
| 1772 | assert(EVT == MVT::getVectorBaseType(ResultTys[0]) && |
| 1773 | "Invalid vector extload!"); |
| 1774 | } else { |
| 1775 | assert(EVT < ResultTys[0] && |
| 1776 | "Should only be an extending load, not truncating!"); |
| 1777 | } |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1778 | assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) && |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1779 | "Cannot sign/zero extend a FP/Vector load!"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1780 | assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) && |
| 1781 | "Cannot convert from FP to Int or Int -> FP!"); |
| 1782 | break; |
| 1783 | } |
| 1784 | |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1785 | // FIXME: figure out how to safely handle things like |
| 1786 | // int foo(int x) { return 1 << (x & 255); } |
| 1787 | // int bar() { return foo(256); } |
| 1788 | #if 0 |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1789 | case ISD::SRA_PARTS: |
| 1790 | case ISD::SRL_PARTS: |
| 1791 | case ISD::SHL_PARTS: |
| 1792 | if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1793 | cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1794 | return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); |
| 1795 | else if (N3.getOpcode() == ISD::AND) |
| 1796 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { |
| 1797 | // If the and is only masking out bits that cannot effect the shift, |
| 1798 | // eliminate the and. |
| 1799 | unsigned NumBits = MVT::getSizeInBits(VT)*2; |
| 1800 | if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) |
| 1801 | return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); |
| 1802 | } |
| 1803 | break; |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1804 | #endif |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1805 | } |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1806 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1807 | // Memoize the node unless it returns a flag. |
| 1808 | SDNode *N; |
| 1809 | if (ResultTys.back() != MVT::Flag) { |
| 1810 | SDNode *&E = |
| 1811 | ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))]; |
| 1812 | if (E) return SDOperand(E, 0); |
| 1813 | E = N = new SDNode(Opcode, Ops); |
| 1814 | } else { |
| 1815 | N = new SDNode(Opcode, Ops); |
| 1816 | } |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 1817 | setNodeValueTypes(N, ResultTys); |
Chris Lattner | 5fa4fa4 | 2005-05-14 06:42:57 +0000 | [diff] [blame] | 1818 | AllNodes.push_back(N); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1819 | return SDOperand(N, 0); |
| 1820 | } |
| 1821 | |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 1822 | void SelectionDAG::setNodeValueTypes(SDNode *N, |
| 1823 | std::vector<MVT::ValueType> &RetVals) { |
| 1824 | switch (RetVals.size()) { |
| 1825 | case 0: return; |
| 1826 | case 1: N->setValueTypes(RetVals[0]); return; |
| 1827 | case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return; |
| 1828 | default: break; |
| 1829 | } |
| 1830 | |
| 1831 | std::list<std::vector<MVT::ValueType> >::iterator I = |
| 1832 | std::find(VTList.begin(), VTList.end(), RetVals); |
| 1833 | if (I == VTList.end()) { |
| 1834 | VTList.push_front(RetVals); |
| 1835 | I = VTList.begin(); |
| 1836 | } |
| 1837 | |
| 1838 | N->setValueTypes(&(*I)[0], I->size()); |
| 1839 | } |
| 1840 | |
| 1841 | void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1, |
| 1842 | MVT::ValueType VT2) { |
| 1843 | for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), |
| 1844 | E = VTList.end(); I != E; ++I) { |
| 1845 | if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) { |
| 1846 | N->setValueTypes(&(*I)[0], 2); |
| 1847 | return; |
| 1848 | } |
| 1849 | } |
| 1850 | std::vector<MVT::ValueType> V; |
| 1851 | V.push_back(VT1); |
| 1852 | V.push_back(VT2); |
| 1853 | VTList.push_front(V); |
| 1854 | N->setValueTypes(&(*VTList.begin())[0], 2); |
| 1855 | } |
| 1856 | |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 1857 | /// UpdateNodeOperands - *Mutate* the specified node in-place to have the |
| 1858 | /// specified operands. If the resultant node already exists in the DAG, |
| 1859 | /// this does not modify the specified node, instead it returns the node that |
| 1860 | /// already exists. If the resultant node does not exist in the DAG, the |
| 1861 | /// input node is returned. As a degenerate case, if you specify the same |
| 1862 | /// input operands as the node already has, the input node is returned. |
| 1863 | SDOperand SelectionDAG:: |
| 1864 | UpdateNodeOperands(SDOperand InN, SDOperand Op) { |
| 1865 | SDNode *N = InN.Val; |
| 1866 | assert(N->getNumOperands() == 1 && "Update with wrong number of operands"); |
| 1867 | |
| 1868 | // Check to see if there is no change. |
| 1869 | if (Op == N->getOperand(0)) return InN; |
| 1870 | |
| 1871 | // See if the modified node already exists. |
| 1872 | SDNode **NewSlot = FindModifiedNodeSlot(N, Op); |
| 1873 | if (NewSlot && *NewSlot) |
| 1874 | return SDOperand(*NewSlot, InN.ResNo); |
| 1875 | |
| 1876 | // Nope it doesn't. Remove the node from it's current place in the maps. |
| 1877 | if (NewSlot) |
| 1878 | RemoveNodeFromCSEMaps(N); |
| 1879 | |
| 1880 | // Now we update the operands. |
| 1881 | N->OperandList[0].Val->removeUser(N); |
| 1882 | Op.Val->addUser(N); |
| 1883 | N->OperandList[0] = Op; |
| 1884 | |
| 1885 | // If this gets put into a CSE map, add it. |
| 1886 | if (NewSlot) *NewSlot = N; |
| 1887 | return InN; |
| 1888 | } |
| 1889 | |
| 1890 | SDOperand SelectionDAG:: |
| 1891 | UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) { |
| 1892 | SDNode *N = InN.Val; |
| 1893 | assert(N->getNumOperands() == 2 && "Update with wrong number of operands"); |
| 1894 | |
| 1895 | // Check to see if there is no change. |
| 1896 | bool AnyChange = false; |
| 1897 | if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1)) |
| 1898 | return InN; // No operands changed, just return the input node. |
| 1899 | |
| 1900 | // See if the modified node already exists. |
| 1901 | SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2); |
| 1902 | if (NewSlot && *NewSlot) |
| 1903 | return SDOperand(*NewSlot, InN.ResNo); |
| 1904 | |
| 1905 | // Nope it doesn't. Remove the node from it's current place in the maps. |
| 1906 | if (NewSlot) |
| 1907 | RemoveNodeFromCSEMaps(N); |
| 1908 | |
| 1909 | // Now we update the operands. |
| 1910 | if (N->OperandList[0] != Op1) { |
| 1911 | N->OperandList[0].Val->removeUser(N); |
| 1912 | Op1.Val->addUser(N); |
| 1913 | N->OperandList[0] = Op1; |
| 1914 | } |
| 1915 | if (N->OperandList[1] != Op2) { |
| 1916 | N->OperandList[1].Val->removeUser(N); |
| 1917 | Op2.Val->addUser(N); |
| 1918 | N->OperandList[1] = Op2; |
| 1919 | } |
| 1920 | |
| 1921 | // If this gets put into a CSE map, add it. |
| 1922 | if (NewSlot) *NewSlot = N; |
| 1923 | return InN; |
| 1924 | } |
| 1925 | |
| 1926 | SDOperand SelectionDAG:: |
| 1927 | UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) { |
| 1928 | std::vector<SDOperand> Ops; |
| 1929 | Ops.push_back(Op1); |
| 1930 | Ops.push_back(Op2); |
| 1931 | Ops.push_back(Op3); |
| 1932 | return UpdateNodeOperands(N, Ops); |
| 1933 | } |
| 1934 | |
| 1935 | SDOperand SelectionDAG:: |
| 1936 | UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, |
| 1937 | SDOperand Op3, SDOperand Op4) { |
| 1938 | std::vector<SDOperand> Ops; |
| 1939 | Ops.push_back(Op1); |
| 1940 | Ops.push_back(Op2); |
| 1941 | Ops.push_back(Op3); |
| 1942 | Ops.push_back(Op4); |
| 1943 | return UpdateNodeOperands(N, Ops); |
| 1944 | } |
| 1945 | |
| 1946 | SDOperand SelectionDAG:: |
Chris Lattner | 809ec11 | 2006-01-28 10:09:25 +0000 | [diff] [blame] | 1947 | UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, |
| 1948 | SDOperand Op3, SDOperand Op4, SDOperand Op5) { |
| 1949 | std::vector<SDOperand> Ops; |
| 1950 | Ops.push_back(Op1); |
| 1951 | Ops.push_back(Op2); |
| 1952 | Ops.push_back(Op3); |
| 1953 | Ops.push_back(Op4); |
| 1954 | Ops.push_back(Op5); |
| 1955 | return UpdateNodeOperands(N, Ops); |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | SDOperand SelectionDAG:: |
Chris Lattner | df6eb30 | 2006-01-28 09:32:45 +0000 | [diff] [blame] | 1960 | UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) { |
| 1961 | SDNode *N = InN.Val; |
| 1962 | assert(N->getNumOperands() == Ops.size() && |
| 1963 | "Update with wrong number of operands"); |
| 1964 | |
| 1965 | // Check to see if there is no change. |
| 1966 | unsigned NumOps = Ops.size(); |
| 1967 | bool AnyChange = false; |
| 1968 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1969 | if (Ops[i] != N->getOperand(i)) { |
| 1970 | AnyChange = true; |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | // No operands changed, just return the input node. |
| 1976 | if (!AnyChange) return InN; |
| 1977 | |
| 1978 | // See if the modified node already exists. |
| 1979 | SDNode **NewSlot = FindModifiedNodeSlot(N, Ops); |
| 1980 | if (NewSlot && *NewSlot) |
| 1981 | return SDOperand(*NewSlot, InN.ResNo); |
| 1982 | |
| 1983 | // Nope it doesn't. Remove the node from it's current place in the maps. |
| 1984 | if (NewSlot) |
| 1985 | RemoveNodeFromCSEMaps(N); |
| 1986 | |
| 1987 | // Now we update the operands. |
| 1988 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1989 | if (N->OperandList[i] != Ops[i]) { |
| 1990 | N->OperandList[i].Val->removeUser(N); |
| 1991 | Ops[i].Val->addUser(N); |
| 1992 | N->OperandList[i] = Ops[i]; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | // If this gets put into a CSE map, add it. |
| 1997 | if (NewSlot) *NewSlot = N; |
| 1998 | return InN; |
| 1999 | } |
| 2000 | |
| 2001 | |
| 2002 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2003 | |
| 2004 | /// SelectNodeTo - These are used for target selectors to *mutate* the |
| 2005 | /// specified node to have the specified return type, Target opcode, and |
| 2006 | /// operands. Note that target opcodes are stored as |
| 2007 | /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2008 | /// |
| 2009 | /// Note that SelectNodeTo returns the resultant node. If there is already a |
| 2010 | /// node of the specified opcode and operands, it returns that node instead of |
| 2011 | /// the current one. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2012 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2013 | MVT::ValueType VT) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2014 | // If an identical node already exists, use it. |
| 2015 | SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)]; |
| 2016 | if (ON) return SDOperand(ON, 0); |
| 2017 | |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 2018 | RemoveNodeFromCSEMaps(N); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 2020 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2021 | N->setValueTypes(VT); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2022 | |
| 2023 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2024 | return SDOperand(N, 0); |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 2025 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2027 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2028 | MVT::ValueType VT, SDOperand Op1) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2029 | // If an identical node already exists, use it. |
| 2030 | SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2031 | std::make_pair(Op1, VT))]; |
| 2032 | if (ON) return SDOperand(ON, 0); |
| 2033 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2034 | RemoveNodeFromCSEMaps(N); |
| 2035 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2036 | N->setValueTypes(VT); |
| 2037 | N->setOperands(Op1); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2038 | |
| 2039 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2040 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2041 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2043 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2044 | MVT::ValueType VT, SDOperand Op1, |
| 2045 | SDOperand Op2) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2046 | // If an identical node already exists, use it. |
| 2047 | SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2048 | std::make_pair(Op1, Op2))]; |
| 2049 | if (ON) return SDOperand(ON, 0); |
| 2050 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2051 | RemoveNodeFromCSEMaps(N); |
| 2052 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2053 | N->setValueTypes(VT); |
| 2054 | N->setOperands(Op1, Op2); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2055 | |
| 2056 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2057 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2058 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2060 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2061 | MVT::ValueType VT, SDOperand Op1, |
| 2062 | SDOperand Op2, SDOperand Op3) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2063 | // If an identical node already exists, use it. |
| 2064 | std::vector<SDOperand> OpList; |
| 2065 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2066 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2067 | std::make_pair(VT, OpList))]; |
| 2068 | if (ON) return SDOperand(ON, 0); |
| 2069 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2070 | RemoveNodeFromCSEMaps(N); |
| 2071 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2072 | N->setValueTypes(VT); |
| 2073 | N->setOperands(Op1, Op2, Op3); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2074 | |
| 2075 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2076 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2077 | } |
Chris Lattner | c975e1d | 2005-08-21 22:30:30 +0000 | [diff] [blame] | 2078 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2079 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2080 | MVT::ValueType VT, SDOperand Op1, |
| 2081 | SDOperand Op2, SDOperand Op3, |
| 2082 | SDOperand Op4) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2083 | // If an identical node already exists, use it. |
| 2084 | std::vector<SDOperand> OpList; |
| 2085 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2086 | OpList.push_back(Op4); |
| 2087 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2088 | std::make_pair(VT, OpList))]; |
| 2089 | if (ON) return SDOperand(ON, 0); |
| 2090 | |
Nate Begeman | 294a0a1 | 2005-08-18 07:30:15 +0000 | [diff] [blame] | 2091 | RemoveNodeFromCSEMaps(N); |
| 2092 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2093 | N->setValueTypes(VT); |
| 2094 | N->setOperands(Op1, Op2, Op3, Op4); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2095 | |
| 2096 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2097 | return SDOperand(N, 0); |
Nate Begeman | 294a0a1 | 2005-08-18 07:30:15 +0000 | [diff] [blame] | 2098 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2099 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2100 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2101 | MVT::ValueType VT, SDOperand Op1, |
| 2102 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 2103 | SDOperand Op5) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2104 | // If an identical node already exists, use it. |
| 2105 | std::vector<SDOperand> OpList; |
| 2106 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2107 | OpList.push_back(Op4); OpList.push_back(Op5); |
| 2108 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2109 | std::make_pair(VT, OpList))]; |
| 2110 | if (ON) return SDOperand(ON, 0); |
| 2111 | |
Chris Lattner | 6b09a29 | 2005-08-21 18:49:33 +0000 | [diff] [blame] | 2112 | RemoveNodeFromCSEMaps(N); |
| 2113 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2114 | N->setValueTypes(VT); |
| 2115 | N->setOperands(Op1, Op2, Op3, Op4, Op5); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2116 | |
| 2117 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2118 | return SDOperand(N, 0); |
Chris Lattner | 6b09a29 | 2005-08-21 18:49:33 +0000 | [diff] [blame] | 2119 | } |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2120 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2121 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2122 | MVT::ValueType VT, SDOperand Op1, |
| 2123 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 2124 | SDOperand Op5, SDOperand Op6) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2125 | // If an identical node already exists, use it. |
| 2126 | std::vector<SDOperand> OpList; |
| 2127 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2128 | OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); |
| 2129 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2130 | std::make_pair(VT, OpList))]; |
| 2131 | if (ON) return SDOperand(ON, 0); |
| 2132 | |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 2133 | RemoveNodeFromCSEMaps(N); |
| 2134 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2135 | N->setValueTypes(VT); |
| 2136 | N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2137 | |
| 2138 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2139 | return SDOperand(N, 0); |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Andrew Lenharth | 8c6f1ee | 2006-01-23 20:59:12 +0000 | [diff] [blame] | 2142 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2143 | MVT::ValueType VT, SDOperand Op1, |
| 2144 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 2145 | SDOperand Op5, SDOperand Op6, |
| 2146 | SDOperand Op7) { |
| 2147 | // If an identical node already exists, use it. |
| 2148 | std::vector<SDOperand> OpList; |
| 2149 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2150 | OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); |
| 2151 | OpList.push_back(Op7); |
| 2152 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2153 | std::make_pair(VT, OpList))]; |
| 2154 | if (ON) return SDOperand(ON, 0); |
| 2155 | |
| 2156 | RemoveNodeFromCSEMaps(N); |
| 2157 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2158 | N->setValueTypes(VT); |
| 2159 | N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7); |
| 2160 | |
| 2161 | ON = N; // Memoize the new node. |
| 2162 | return SDOperand(N, 0); |
| 2163 | } |
Andrew Lenharth | 7cf11b4 | 2006-01-23 21:51:14 +0000 | [diff] [blame] | 2164 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2165 | MVT::ValueType VT, SDOperand Op1, |
| 2166 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 2167 | SDOperand Op5, SDOperand Op6, |
| 2168 | SDOperand Op7, SDOperand Op8) { |
| 2169 | // If an identical node already exists, use it. |
| 2170 | std::vector<SDOperand> OpList; |
| 2171 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2172 | OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); |
| 2173 | OpList.push_back(Op7); OpList.push_back(Op8); |
| 2174 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2175 | std::make_pair(VT, OpList))]; |
| 2176 | if (ON) return SDOperand(ON, 0); |
| 2177 | |
| 2178 | RemoveNodeFromCSEMaps(N); |
| 2179 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2180 | N->setValueTypes(VT); |
| 2181 | N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8); |
| 2182 | |
| 2183 | ON = N; // Memoize the new node. |
| 2184 | return SDOperand(N, 0); |
| 2185 | } |
Andrew Lenharth | 8c6f1ee | 2006-01-23 20:59:12 +0000 | [diff] [blame] | 2186 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2187 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2188 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 2189 | SDOperand Op1, SDOperand Op2) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2190 | // If an identical node already exists, use it. |
| 2191 | std::vector<SDOperand> OpList; |
| 2192 | OpList.push_back(Op1); OpList.push_back(Op2); |
| 2193 | std::vector<MVT::ValueType> VTList; |
| 2194 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 2195 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2196 | std::make_pair(VTList, OpList))]; |
| 2197 | if (ON) return SDOperand(ON, 0); |
| 2198 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2199 | RemoveNodeFromCSEMaps(N); |
| 2200 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2201 | setNodeValueTypes(N, VT1, VT2); |
| 2202 | N->setOperands(Op1, Op2); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2203 | |
| 2204 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2205 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2208 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2209 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 2210 | SDOperand Op1, SDOperand Op2, |
| 2211 | SDOperand Op3) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2212 | // If an identical node already exists, use it. |
| 2213 | std::vector<SDOperand> OpList; |
| 2214 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2215 | std::vector<MVT::ValueType> VTList; |
| 2216 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 2217 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2218 | std::make_pair(VTList, OpList))]; |
| 2219 | if (ON) return SDOperand(ON, 0); |
| 2220 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2221 | RemoveNodeFromCSEMaps(N); |
| 2222 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2223 | setNodeValueTypes(N, VT1, VT2); |
| 2224 | N->setOperands(Op1, Op2, Op3); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2225 | |
| 2226 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2227 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2230 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2231 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 2232 | SDOperand Op1, SDOperand Op2, |
| 2233 | SDOperand Op3, SDOperand Op4) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2234 | // If an identical node already exists, use it. |
| 2235 | std::vector<SDOperand> OpList; |
| 2236 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2237 | OpList.push_back(Op4); |
| 2238 | std::vector<MVT::ValueType> VTList; |
| 2239 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 2240 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2241 | std::make_pair(VTList, OpList))]; |
| 2242 | if (ON) return SDOperand(ON, 0); |
| 2243 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2244 | RemoveNodeFromCSEMaps(N); |
| 2245 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2246 | setNodeValueTypes(N, VT1, VT2); |
| 2247 | N->setOperands(Op1, Op2, Op3, Op4); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2248 | |
| 2249 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2250 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2253 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 2254 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 2255 | SDOperand Op1, SDOperand Op2, |
| 2256 | SDOperand Op3, SDOperand Op4, |
| 2257 | SDOperand Op5) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2258 | // If an identical node already exists, use it. |
| 2259 | std::vector<SDOperand> OpList; |
| 2260 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 2261 | OpList.push_back(Op4); OpList.push_back(Op5); |
| 2262 | std::vector<MVT::ValueType> VTList; |
| 2263 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 2264 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 2265 | std::make_pair(VTList, OpList))]; |
| 2266 | if (ON) return SDOperand(ON, 0); |
| 2267 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2268 | RemoveNodeFromCSEMaps(N); |
| 2269 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 2270 | setNodeValueTypes(N, VT1, VT2); |
| 2271 | N->setOperands(Op1, Op2, Op3, Op4, Op5); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 2272 | |
| 2273 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 2274 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Evan Cheng | 6ae46c4 | 2006-02-09 07:15:23 +0000 | [diff] [blame] | 2277 | /// getTargetNode - These are used for target selectors to create a new node |
| 2278 | /// with specified return type(s), target opcode, and operands. |
| 2279 | /// |
| 2280 | /// Note that getTargetNode returns the resultant node. If there is already a |
| 2281 | /// node of the specified opcode and operands, it returns that node instead of |
| 2282 | /// the current one. |
| 2283 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) { |
| 2284 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val; |
| 2285 | } |
| 2286 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2287 | SDOperand Op1) { |
| 2288 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val; |
| 2289 | } |
| 2290 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2291 | SDOperand Op1, SDOperand Op2) { |
| 2292 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val; |
| 2293 | } |
| 2294 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2295 | SDOperand Op1, SDOperand Op2, SDOperand Op3) { |
| 2296 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val; |
| 2297 | } |
| 2298 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2299 | SDOperand Op1, SDOperand Op2, SDOperand Op3, |
| 2300 | SDOperand Op4) { |
| 2301 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val; |
| 2302 | } |
| 2303 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2304 | SDOperand Op1, SDOperand Op2, SDOperand Op3, |
| 2305 | SDOperand Op4, SDOperand Op5) { |
| 2306 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val; |
| 2307 | } |
| 2308 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2309 | SDOperand Op1, SDOperand Op2, SDOperand Op3, |
| 2310 | SDOperand Op4, SDOperand Op5, SDOperand Op6) { |
| 2311 | std::vector<SDOperand> Ops; |
| 2312 | Ops.reserve(6); |
| 2313 | Ops.push_back(Op1); |
| 2314 | Ops.push_back(Op2); |
| 2315 | Ops.push_back(Op3); |
| 2316 | Ops.push_back(Op4); |
| 2317 | Ops.push_back(Op5); |
| 2318 | Ops.push_back(Op6); |
| 2319 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val; |
| 2320 | } |
| 2321 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2322 | SDOperand Op1, SDOperand Op2, SDOperand Op3, |
| 2323 | SDOperand Op4, SDOperand Op5, SDOperand Op6, |
| 2324 | SDOperand Op7) { |
| 2325 | std::vector<SDOperand> Ops; |
| 2326 | Ops.reserve(7); |
| 2327 | Ops.push_back(Op1); |
| 2328 | Ops.push_back(Op2); |
| 2329 | Ops.push_back(Op3); |
| 2330 | Ops.push_back(Op4); |
| 2331 | Ops.push_back(Op5); |
| 2332 | Ops.push_back(Op6); |
| 2333 | Ops.push_back(Op7); |
| 2334 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val; |
| 2335 | } |
| 2336 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2337 | SDOperand Op1, SDOperand Op2, SDOperand Op3, |
| 2338 | SDOperand Op4, SDOperand Op5, SDOperand Op6, |
| 2339 | SDOperand Op7, SDOperand Op8) { |
| 2340 | std::vector<SDOperand> Ops; |
| 2341 | Ops.reserve(8); |
| 2342 | Ops.push_back(Op1); |
| 2343 | Ops.push_back(Op2); |
| 2344 | Ops.push_back(Op3); |
| 2345 | Ops.push_back(Op4); |
| 2346 | Ops.push_back(Op5); |
| 2347 | Ops.push_back(Op6); |
| 2348 | Ops.push_back(Op7); |
| 2349 | Ops.push_back(Op8); |
| 2350 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val; |
| 2351 | } |
| 2352 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT, |
| 2353 | std::vector<SDOperand> &Ops) { |
| 2354 | return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val; |
| 2355 | } |
| 2356 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2357 | MVT::ValueType VT2, SDOperand Op1) { |
| 2358 | std::vector<MVT::ValueType> ResultTys; |
| 2359 | ResultTys.push_back(VT1); |
| 2360 | ResultTys.push_back(VT2); |
| 2361 | std::vector<SDOperand> Ops; |
| 2362 | Ops.push_back(Op1); |
| 2363 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2364 | } |
| 2365 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2366 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) { |
| 2367 | std::vector<MVT::ValueType> ResultTys; |
| 2368 | ResultTys.push_back(VT1); |
| 2369 | ResultTys.push_back(VT2); |
| 2370 | std::vector<SDOperand> Ops; |
| 2371 | Ops.push_back(Op1); |
| 2372 | Ops.push_back(Op2); |
| 2373 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2374 | } |
| 2375 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2376 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, |
| 2377 | SDOperand Op3) { |
| 2378 | std::vector<MVT::ValueType> ResultTys; |
| 2379 | ResultTys.push_back(VT1); |
| 2380 | ResultTys.push_back(VT2); |
| 2381 | std::vector<SDOperand> Ops; |
| 2382 | Ops.push_back(Op1); |
| 2383 | Ops.push_back(Op2); |
| 2384 | Ops.push_back(Op3); |
| 2385 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2386 | } |
| 2387 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2388 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, |
| 2389 | SDOperand Op3, SDOperand Op4) { |
| 2390 | std::vector<MVT::ValueType> ResultTys; |
| 2391 | ResultTys.push_back(VT1); |
| 2392 | ResultTys.push_back(VT2); |
| 2393 | std::vector<SDOperand> Ops; |
| 2394 | Ops.push_back(Op1); |
| 2395 | Ops.push_back(Op2); |
| 2396 | Ops.push_back(Op3); |
| 2397 | Ops.push_back(Op4); |
| 2398 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2399 | } |
| 2400 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2401 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, |
| 2402 | SDOperand Op3, SDOperand Op4, SDOperand Op5) { |
| 2403 | std::vector<MVT::ValueType> ResultTys; |
| 2404 | ResultTys.push_back(VT1); |
| 2405 | ResultTys.push_back(VT2); |
| 2406 | std::vector<SDOperand> Ops; |
| 2407 | Ops.push_back(Op1); |
| 2408 | Ops.push_back(Op2); |
| 2409 | Ops.push_back(Op3); |
| 2410 | Ops.push_back(Op4); |
| 2411 | Ops.push_back(Op5); |
| 2412 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2413 | } |
| 2414 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2415 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, |
| 2416 | SDOperand Op3, SDOperand Op4, SDOperand Op5, |
| 2417 | SDOperand Op6) { |
| 2418 | std::vector<MVT::ValueType> ResultTys; |
| 2419 | ResultTys.push_back(VT1); |
| 2420 | ResultTys.push_back(VT2); |
| 2421 | std::vector<SDOperand> Ops; |
| 2422 | Ops.push_back(Op1); |
| 2423 | Ops.push_back(Op2); |
| 2424 | Ops.push_back(Op3); |
| 2425 | Ops.push_back(Op4); |
| 2426 | Ops.push_back(Op5); |
| 2427 | Ops.push_back(Op6); |
| 2428 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2429 | } |
| 2430 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2431 | MVT::ValueType VT2, SDOperand Op1, SDOperand Op2, |
| 2432 | SDOperand Op3, SDOperand Op4, SDOperand Op5, |
| 2433 | SDOperand Op6, SDOperand Op7) { |
| 2434 | std::vector<MVT::ValueType> ResultTys; |
| 2435 | ResultTys.push_back(VT1); |
| 2436 | ResultTys.push_back(VT2); |
| 2437 | std::vector<SDOperand> Ops; |
| 2438 | Ops.push_back(Op1); |
| 2439 | Ops.push_back(Op2); |
| 2440 | Ops.push_back(Op3); |
| 2441 | Ops.push_back(Op4); |
| 2442 | Ops.push_back(Op5); |
| 2443 | Ops.push_back(Op6); |
| 2444 | Ops.push_back(Op7); |
| 2445 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2446 | } |
| 2447 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2448 | MVT::ValueType VT2, MVT::ValueType VT3, |
| 2449 | SDOperand Op1, SDOperand Op2) { |
| 2450 | std::vector<MVT::ValueType> ResultTys; |
| 2451 | ResultTys.push_back(VT1); |
| 2452 | ResultTys.push_back(VT2); |
| 2453 | ResultTys.push_back(VT3); |
| 2454 | std::vector<SDOperand> Ops; |
| 2455 | Ops.push_back(Op1); |
| 2456 | Ops.push_back(Op2); |
| 2457 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2458 | } |
| 2459 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2460 | MVT::ValueType VT2, MVT::ValueType VT3, |
| 2461 | SDOperand Op1, SDOperand Op2, |
| 2462 | SDOperand Op3, SDOperand Op4, SDOperand Op5) { |
| 2463 | std::vector<MVT::ValueType> ResultTys; |
| 2464 | ResultTys.push_back(VT1); |
| 2465 | ResultTys.push_back(VT2); |
| 2466 | ResultTys.push_back(VT3); |
| 2467 | std::vector<SDOperand> Ops; |
| 2468 | Ops.push_back(Op1); |
| 2469 | Ops.push_back(Op2); |
| 2470 | Ops.push_back(Op3); |
| 2471 | Ops.push_back(Op4); |
| 2472 | Ops.push_back(Op5); |
| 2473 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2474 | } |
| 2475 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2476 | MVT::ValueType VT2, MVT::ValueType VT3, |
| 2477 | SDOperand Op1, SDOperand Op2, |
| 2478 | SDOperand Op3, SDOperand Op4, SDOperand Op5, |
| 2479 | SDOperand Op6) { |
| 2480 | std::vector<MVT::ValueType> ResultTys; |
| 2481 | ResultTys.push_back(VT1); |
| 2482 | ResultTys.push_back(VT2); |
| 2483 | ResultTys.push_back(VT3); |
| 2484 | std::vector<SDOperand> Ops; |
| 2485 | Ops.push_back(Op1); |
| 2486 | Ops.push_back(Op2); |
| 2487 | Ops.push_back(Op3); |
| 2488 | Ops.push_back(Op4); |
| 2489 | Ops.push_back(Op5); |
| 2490 | Ops.push_back(Op6); |
| 2491 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2492 | } |
| 2493 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2494 | MVT::ValueType VT2, MVT::ValueType VT3, |
| 2495 | SDOperand Op1, SDOperand Op2, |
| 2496 | SDOperand Op3, SDOperand Op4, SDOperand Op5, |
| 2497 | SDOperand Op6, SDOperand Op7) { |
| 2498 | std::vector<MVT::ValueType> ResultTys; |
| 2499 | ResultTys.push_back(VT1); |
| 2500 | ResultTys.push_back(VT2); |
| 2501 | ResultTys.push_back(VT3); |
| 2502 | std::vector<SDOperand> Ops; |
| 2503 | Ops.push_back(Op1); |
| 2504 | Ops.push_back(Op2); |
| 2505 | Ops.push_back(Op3); |
| 2506 | Ops.push_back(Op4); |
| 2507 | Ops.push_back(Op5); |
| 2508 | Ops.push_back(Op6); |
| 2509 | Ops.push_back(Op7); |
| 2510 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2511 | } |
| 2512 | SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1, |
| 2513 | MVT::ValueType VT2, std::vector<SDOperand> &Ops) { |
| 2514 | std::vector<MVT::ValueType> ResultTys; |
| 2515 | ResultTys.push_back(VT1); |
| 2516 | ResultTys.push_back(VT2); |
| 2517 | return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val; |
| 2518 | } |
| 2519 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 2520 | // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2521 | /// This can cause recursive merging of nodes in the DAG. |
| 2522 | /// |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2523 | /// This version assumes From/To have a single result value. |
| 2524 | /// |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2525 | void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, |
| 2526 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2527 | SDNode *From = FromN.Val, *To = ToN.Val; |
| 2528 | assert(From->getNumValues() == 1 && To->getNumValues() == 1 && |
| 2529 | "Cannot replace with this method!"); |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2530 | assert(From != To && "Cannot replace uses of with self"); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2532 | while (!From->use_empty()) { |
| 2533 | // Process users until they are all gone. |
| 2534 | SDNode *U = *From->use_begin(); |
| 2535 | |
| 2536 | // This node is about to morph, remove its old self from the CSE maps. |
| 2537 | RemoveNodeFromCSEMaps(U); |
| 2538 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2539 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 2540 | I != E; ++I) |
| 2541 | if (I->Val == From) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2542 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2543 | I->Val = To; |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2544 | To->addUser(U); |
| 2545 | } |
| 2546 | |
| 2547 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 2548 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2549 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 2550 | ReplaceAllUsesWith(U, Existing, Deleted); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2551 | // U is now dead. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2552 | if (Deleted) Deleted->push_back(U); |
| 2553 | DeleteNodeNotInCSEMaps(U); |
| 2554 | } |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 2559 | /// This can cause recursive merging of nodes in the DAG. |
| 2560 | /// |
| 2561 | /// This version assumes From/To have matching types and numbers of result |
| 2562 | /// values. |
| 2563 | /// |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2564 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, |
| 2565 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2566 | assert(From != To && "Cannot replace uses of with self"); |
| 2567 | assert(From->getNumValues() == To->getNumValues() && |
| 2568 | "Cannot use this version of ReplaceAllUsesWith!"); |
| 2569 | if (From->getNumValues() == 1) { // If possible, use the faster version. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2570 | ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | while (!From->use_empty()) { |
| 2575 | // Process users until they are all gone. |
| 2576 | SDNode *U = *From->use_begin(); |
| 2577 | |
| 2578 | // This node is about to morph, remove its old self from the CSE maps. |
| 2579 | RemoveNodeFromCSEMaps(U); |
| 2580 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2581 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 2582 | I != E; ++I) |
| 2583 | if (I->Val == From) { |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2584 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2585 | I->Val = To; |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2586 | To->addUser(U); |
| 2587 | } |
| 2588 | |
| 2589 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 2590 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2591 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 2592 | ReplaceAllUsesWith(U, Existing, Deleted); |
| 2593 | // U is now dead. |
| 2594 | if (Deleted) Deleted->push_back(U); |
| 2595 | DeleteNodeNotInCSEMaps(U); |
| 2596 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 2600 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 2601 | /// This can cause recursive merging of nodes in the DAG. |
| 2602 | /// |
| 2603 | /// This version can replace From with any result values. To must match the |
| 2604 | /// number and types of values returned by From. |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2605 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2606 | const std::vector<SDOperand> &To, |
| 2607 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2608 | assert(From->getNumValues() == To.size() && |
| 2609 | "Incorrect number of values to replace with!"); |
Chris Lattner | ff01698 | 2005-08-28 23:59:36 +0000 | [diff] [blame] | 2610 | if (To.size() == 1 && To[0].Val->getNumValues() == 1) { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2611 | // Degenerate case handled above. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2612 | ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | |
| 2616 | while (!From->use_empty()) { |
| 2617 | // Process users until they are all gone. |
| 2618 | SDNode *U = *From->use_begin(); |
| 2619 | |
| 2620 | // This node is about to morph, remove its old self from the CSE maps. |
| 2621 | RemoveNodeFromCSEMaps(U); |
| 2622 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2623 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 2624 | I != E; ++I) |
| 2625 | if (I->Val == From) { |
| 2626 | const SDOperand &ToOp = To[I->ResNo]; |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2627 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 2628 | *I = ToOp; |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2629 | ToOp.Val->addUser(U); |
| 2630 | } |
| 2631 | |
| 2632 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 2633 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 2634 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 2635 | ReplaceAllUsesWith(U, Existing, Deleted); |
| 2636 | // U is now dead. |
| 2637 | if (Deleted) Deleted->push_back(U); |
| 2638 | DeleteNodeNotInCSEMaps(U); |
| 2639 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2640 | } |
| 2641 | } |
| 2642 | |
Chris Lattner | 012f241 | 2006-02-17 21:58:01 +0000 | [diff] [blame] | 2643 | /// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving |
| 2644 | /// uses of other values produced by From.Val alone. The Deleted vector is |
| 2645 | /// handled the same was as for ReplaceAllUsesWith. |
| 2646 | void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To, |
| 2647 | std::vector<SDNode*> &Deleted) { |
| 2648 | assert(From != To && "Cannot replace a value with itself"); |
| 2649 | // Handle the simple, trivial, case efficiently. |
| 2650 | if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) { |
| 2651 | ReplaceAllUsesWith(From, To, &Deleted); |
| 2652 | return; |
| 2653 | } |
| 2654 | |
| 2655 | // Get all of the users in a nice, deterministically ordered, uniqued set. |
| 2656 | SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end()); |
| 2657 | |
| 2658 | while (!Users.empty()) { |
| 2659 | // We know that this user uses some value of From. If it is the right |
| 2660 | // value, update it. |
| 2661 | SDNode *User = Users.back(); |
| 2662 | Users.pop_back(); |
| 2663 | |
| 2664 | for (SDOperand *Op = User->OperandList, |
| 2665 | *E = User->OperandList+User->NumOperands; Op != E; ++Op) { |
| 2666 | if (*Op == From) { |
| 2667 | // Okay, we know this user needs to be updated. Remove its old self |
| 2668 | // from the CSE maps. |
| 2669 | RemoveNodeFromCSEMaps(User); |
| 2670 | |
| 2671 | // Update all operands that match "From". |
| 2672 | for (; Op != E; ++Op) { |
| 2673 | if (*Op == From) { |
| 2674 | From.Val->removeUser(User); |
| 2675 | *Op = To; |
| 2676 | To.Val->addUser(User); |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | // Now that we have modified User, add it back to the CSE maps. If it |
| 2681 | // already exists there, recursively merge the results together. |
| 2682 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) { |
| 2683 | unsigned NumDeleted = Deleted.size(); |
| 2684 | ReplaceAllUsesWith(User, Existing, &Deleted); |
| 2685 | |
| 2686 | // User is now dead. |
| 2687 | Deleted.push_back(User); |
| 2688 | DeleteNodeNotInCSEMaps(User); |
| 2689 | |
| 2690 | // We have to be careful here, because ReplaceAllUsesWith could have |
| 2691 | // deleted a user of From, which means there may be dangling pointers |
| 2692 | // in the "Users" setvector. Scan over the deleted node pointers and |
| 2693 | // remove them from the setvector. |
| 2694 | for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i) |
| 2695 | Users.remove(Deleted[i]); |
| 2696 | } |
| 2697 | break; // Exit the operand scanning loop. |
| 2698 | } |
| 2699 | } |
| 2700 | } |
| 2701 | } |
| 2702 | |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 2703 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 2704 | //===----------------------------------------------------------------------===// |
| 2705 | // SDNode Class |
| 2706 | //===----------------------------------------------------------------------===// |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 2708 | |
| 2709 | /// getValueTypeList - Return a pointer to the specified value type. |
| 2710 | /// |
| 2711 | MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { |
| 2712 | static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; |
| 2713 | VTs[VT] = VT; |
| 2714 | return &VTs[VT]; |
| 2715 | } |
| 2716 | |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 2717 | /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the |
| 2718 | /// indicated value. This method ignores uses of other values defined by this |
| 2719 | /// operation. |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 2720 | bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const { |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 2721 | assert(Value < getNumValues() && "Bad value!"); |
| 2722 | |
| 2723 | // If there is only one value, this is easy. |
| 2724 | if (getNumValues() == 1) |
| 2725 | return use_size() == NUses; |
| 2726 | if (Uses.size() < NUses) return false; |
| 2727 | |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 2728 | SDOperand TheValue(const_cast<SDNode *>(this), Value); |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 2729 | |
| 2730 | std::set<SDNode*> UsersHandled; |
| 2731 | |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 2732 | for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end(); |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 2733 | UI != E; ++UI) { |
| 2734 | SDNode *User = *UI; |
| 2735 | if (User->getNumOperands() == 1 || |
| 2736 | UsersHandled.insert(User).second) // First time we've seen this? |
| 2737 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 2738 | if (User->getOperand(i) == TheValue) { |
| 2739 | if (NUses == 0) |
| 2740 | return false; // too many uses |
| 2741 | --NUses; |
| 2742 | } |
| 2743 | } |
| 2744 | |
| 2745 | // Found exactly the right number of uses? |
| 2746 | return NUses == 0; |
| 2747 | } |
| 2748 | |
| 2749 | |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 2750 | // isOnlyUse - Return true if this node is the only use of N. |
| 2751 | bool SDNode::isOnlyUse(SDNode *N) const { |
| 2752 | bool Seen = false; |
| 2753 | for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) { |
| 2754 | SDNode *User = *I; |
| 2755 | if (User == this) |
| 2756 | Seen = true; |
| 2757 | else |
| 2758 | return false; |
| 2759 | } |
| 2760 | |
| 2761 | return Seen; |
| 2762 | } |
| 2763 | |
Evan Cheng | 80d8eaa | 2006-03-03 06:24:54 +0000 | [diff] [blame] | 2764 | // isOperand - Return true if this node is an operand of N. |
Evan Cheng | bfa284f | 2006-03-03 06:42:32 +0000 | [diff] [blame] | 2765 | bool SDOperand::isOperand(SDNode *N) const { |
| 2766 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 2767 | if (*this == N->getOperand(i)) |
| 2768 | return true; |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
Evan Cheng | 80d8eaa | 2006-03-03 06:24:54 +0000 | [diff] [blame] | 2772 | bool SDNode::isOperand(SDNode *N) const { |
| 2773 | for (unsigned i = 0, e = N->NumOperands; i != e; ++i) |
| 2774 | if (this == N->OperandList[i].Val) |
| 2775 | return true; |
| 2776 | return false; |
| 2777 | } |
Evan Cheng | 4ee6211 | 2006-02-05 06:29:23 +0000 | [diff] [blame] | 2778 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2779 | const char *SDNode::getOperationName(const SelectionDAG *G) const { |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2780 | switch (getOpcode()) { |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2781 | default: |
| 2782 | if (getOpcode() < ISD::BUILTIN_OP_END) |
| 2783 | return "<<Unknown DAG Node>>"; |
| 2784 | else { |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2785 | if (G) { |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2786 | if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) |
Chris Lattner | 08addbd | 2005-09-09 22:35:03 +0000 | [diff] [blame] | 2787 | if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) |
| 2788 | return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); |
Evan Cheng | 115c036 | 2005-12-19 23:11:49 +0000 | [diff] [blame] | 2789 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 2790 | TargetLowering &TLI = G->getTargetLoweringInfo(); |
| 2791 | const char *Name = |
| 2792 | TLI.getTargetNodeName(getOpcode()); |
| 2793 | if (Name) return Name; |
| 2794 | } |
| 2795 | |
| 2796 | return "<<Unknown Target Node>>"; |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2799 | case ISD::PCMARKER: return "PCMarker"; |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2800 | case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 2801 | case ISD::SRCVALUE: return "SrcValue"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2802 | case ISD::EntryToken: return "EntryToken"; |
Chris Lattner | 282c5ca | 2005-01-13 17:59:10 +0000 | [diff] [blame] | 2803 | case ISD::TokenFactor: return "TokenFactor"; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 2804 | case ISD::AssertSext: return "AssertSext"; |
| 2805 | case ISD::AssertZext: return "AssertZext"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2806 | |
| 2807 | case ISD::STRING: return "String"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2808 | case ISD::BasicBlock: return "BasicBlock"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2809 | case ISD::VALUETYPE: return "ValueType"; |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2810 | case ISD::Register: return "Register"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2811 | |
| 2812 | case ISD::Constant: return "Constant"; |
| 2813 | case ISD::ConstantFP: return "ConstantFP"; |
| 2814 | case ISD::GlobalAddress: return "GlobalAddress"; |
| 2815 | case ISD::FrameIndex: return "FrameIndex"; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 2816 | case ISD::JumpTable: return "JumpTable"; |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 2817 | case ISD::ConstantPool: return "ConstantPool"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2818 | case ISD::ExternalSymbol: return "ExternalSymbol"; |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 2819 | case ISD::INTRINSIC_WO_CHAIN: { |
| 2820 | unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue(); |
| 2821 | return Intrinsic::getName((Intrinsic::ID)IID); |
| 2822 | } |
| 2823 | case ISD::INTRINSIC_VOID: |
| 2824 | case ISD::INTRINSIC_W_CHAIN: { |
| 2825 | unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue(); |
Chris Lattner | 70a248d | 2006-03-27 06:45:25 +0000 | [diff] [blame] | 2826 | return Intrinsic::getName((Intrinsic::ID)IID); |
Chris Lattner | 401ec7f | 2006-03-27 16:10:59 +0000 | [diff] [blame] | 2827 | } |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2828 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 2829 | case ISD::BUILD_VECTOR: return "BUILD_VECTOR"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2830 | case ISD::TargetConstant: return "TargetConstant"; |
| 2831 | case ISD::TargetConstantFP:return "TargetConstantFP"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2832 | case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; |
| 2833 | case ISD::TargetFrameIndex: return "TargetFrameIndex"; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 2834 | case ISD::TargetJumpTable: return "TargetJumpTable"; |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 2835 | case ISD::TargetConstantPool: return "TargetConstantPool"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2836 | case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; |
Evan Cheng | 1ab7d85 | 2006-03-01 00:51:13 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2838 | case ISD::CopyToReg: return "CopyToReg"; |
| 2839 | case ISD::CopyFromReg: return "CopyFromReg"; |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 2840 | case ISD::UNDEF: return "undef"; |
Chris Lattner | cc0aad2 | 2006-01-15 08:39:35 +0000 | [diff] [blame] | 2841 | case ISD::MERGE_VALUES: return "mergevalues"; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2842 | case ISD::INLINEASM: return "inlineasm"; |
Evan Cheng | 9fda2f9 | 2006-02-03 01:33:01 +0000 | [diff] [blame] | 2843 | case ISD::HANDLENODE: return "handlenode"; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2844 | case ISD::FORMAL_ARGUMENTS: return "formal_arguments"; |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 2845 | |
Chris Lattner | ff9fd0a | 2005-04-02 04:58:41 +0000 | [diff] [blame] | 2846 | // Unary operators |
| 2847 | case ISD::FABS: return "fabs"; |
| 2848 | case ISD::FNEG: return "fneg"; |
Chris Lattner | 7f64464 | 2005-04-28 21:44:03 +0000 | [diff] [blame] | 2849 | case ISD::FSQRT: return "fsqrt"; |
| 2850 | case ISD::FSIN: return "fsin"; |
| 2851 | case ISD::FCOS: return "fcos"; |
Chris Lattner | ff9fd0a | 2005-04-02 04:58:41 +0000 | [diff] [blame] | 2852 | |
| 2853 | // Binary operators |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2854 | case ISD::ADD: return "add"; |
| 2855 | case ISD::SUB: return "sub"; |
| 2856 | case ISD::MUL: return "mul"; |
Nate Begeman | 1867054 | 2005-04-05 22:36:56 +0000 | [diff] [blame] | 2857 | case ISD::MULHU: return "mulhu"; |
| 2858 | case ISD::MULHS: return "mulhs"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2859 | case ISD::SDIV: return "sdiv"; |
| 2860 | case ISD::UDIV: return "udiv"; |
| 2861 | case ISD::SREM: return "srem"; |
| 2862 | case ISD::UREM: return "urem"; |
| 2863 | case ISD::AND: return "and"; |
| 2864 | case ISD::OR: return "or"; |
| 2865 | case ISD::XOR: return "xor"; |
| 2866 | case ISD::SHL: return "shl"; |
| 2867 | case ISD::SRA: return "sra"; |
| 2868 | case ISD::SRL: return "srl"; |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2869 | case ISD::ROTL: return "rotl"; |
| 2870 | case ISD::ROTR: return "rotr"; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2871 | case ISD::FADD: return "fadd"; |
| 2872 | case ISD::FSUB: return "fsub"; |
| 2873 | case ISD::FMUL: return "fmul"; |
| 2874 | case ISD::FDIV: return "fdiv"; |
| 2875 | case ISD::FREM: return "frem"; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2876 | case ISD::FCOPYSIGN: return "fcopysign"; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 2877 | case ISD::VADD: return "vadd"; |
| 2878 | case ISD::VSUB: return "vsub"; |
| 2879 | case ISD::VMUL: return "vmul"; |
Chris Lattner | 97d2333 | 2006-04-02 02:41:18 +0000 | [diff] [blame] | 2880 | case ISD::VSDIV: return "vsdiv"; |
| 2881 | case ISD::VUDIV: return "vudiv"; |
| 2882 | case ISD::VAND: return "vand"; |
| 2883 | case ISD::VOR: return "vor"; |
| 2884 | case ISD::VXOR: return "vxor"; |
Chris Lattner | 0c486bd | 2006-03-17 19:53:59 +0000 | [diff] [blame] | 2885 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2886 | case ISD::SETCC: return "setcc"; |
| 2887 | case ISD::SELECT: return "select"; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2888 | case ISD::SELECT_CC: return "select_cc"; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 2889 | case ISD::VSELECT: return "vselect"; |
| 2890 | case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt"; |
| 2891 | case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt"; |
| 2892 | case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt"; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 2893 | case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt"; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 2894 | case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector"; |
| 2895 | case ISD::VBUILD_VECTOR: return "vbuild_vector"; |
| 2896 | case ISD::VECTOR_SHUFFLE: return "vector_shuffle"; |
| 2897 | case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle"; |
| 2898 | case ISD::VBIT_CONVERT: return "vbit_convert"; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2899 | case ISD::ADDC: return "addc"; |
| 2900 | case ISD::ADDE: return "adde"; |
| 2901 | case ISD::SUBC: return "subc"; |
| 2902 | case ISD::SUBE: return "sube"; |
Chris Lattner | 41be951 | 2005-04-02 03:30:42 +0000 | [diff] [blame] | 2903 | case ISD::SHL_PARTS: return "shl_parts"; |
| 2904 | case ISD::SRA_PARTS: return "sra_parts"; |
| 2905 | case ISD::SRL_PARTS: return "srl_parts"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | 7f64464 | 2005-04-28 21:44:03 +0000 | [diff] [blame] | 2907 | // Conversion operators. |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2908 | case ISD::SIGN_EXTEND: return "sign_extend"; |
| 2909 | case ISD::ZERO_EXTEND: return "zero_extend"; |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 2910 | case ISD::ANY_EXTEND: return "any_extend"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 2911 | case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2912 | case ISD::TRUNCATE: return "truncate"; |
| 2913 | case ISD::FP_ROUND: return "fp_round"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 2914 | case ISD::FP_ROUND_INREG: return "fp_round_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2915 | case ISD::FP_EXTEND: return "fp_extend"; |
| 2916 | |
| 2917 | case ISD::SINT_TO_FP: return "sint_to_fp"; |
| 2918 | case ISD::UINT_TO_FP: return "uint_to_fp"; |
| 2919 | case ISD::FP_TO_SINT: return "fp_to_sint"; |
| 2920 | case ISD::FP_TO_UINT: return "fp_to_uint"; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2921 | case ISD::BIT_CONVERT: return "bit_convert"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Control flow instructions |
| 2924 | case ISD::BR: return "br"; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 2925 | case ISD::BRIND: return "brind"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2926 | case ISD::BRCOND: return "brcond"; |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 2927 | case ISD::BR_CC: return "br_cc"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2928 | case ISD::RET: return "ret"; |
Chris Lattner | a364fa1 | 2005-05-12 23:51:40 +0000 | [diff] [blame] | 2929 | case ISD::CALLSEQ_START: return "callseq_start"; |
| 2930 | case ISD::CALLSEQ_END: return "callseq_end"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2931 | |
| 2932 | // Other operators |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2933 | case ISD::LOAD: return "load"; |
| 2934 | case ISD::STORE: return "store"; |
| 2935 | case ISD::VLOAD: return "vload"; |
| 2936 | case ISD::EXTLOAD: return "extload"; |
| 2937 | case ISD::SEXTLOAD: return "sextload"; |
| 2938 | case ISD::ZEXTLOAD: return "zextload"; |
| 2939 | case ISD::TRUNCSTORE: return "truncstore"; |
| 2940 | case ISD::VAARG: return "vaarg"; |
| 2941 | case ISD::VACOPY: return "vacopy"; |
| 2942 | case ISD::VAEND: return "vaend"; |
| 2943 | case ISD::VASTART: return "vastart"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2944 | case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; |
Chris Lattner | 5a67afc | 2006-01-13 02:39:42 +0000 | [diff] [blame] | 2945 | case ISD::EXTRACT_ELEMENT: return "extract_element"; |
| 2946 | case ISD::BUILD_PAIR: return "build_pair"; |
| 2947 | case ISD::STACKSAVE: return "stacksave"; |
| 2948 | case ISD::STACKRESTORE: return "stackrestore"; |
| 2949 | |
| 2950 | // Block memory operations. |
Chris Lattner | 4c633e8 | 2005-01-11 05:57:01 +0000 | [diff] [blame] | 2951 | case ISD::MEMSET: return "memset"; |
| 2952 | case ISD::MEMCPY: return "memcpy"; |
| 2953 | case ISD::MEMMOVE: return "memmove"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2954 | |
Nate Begeman | 1b5db7a | 2006-01-16 08:07:10 +0000 | [diff] [blame] | 2955 | // Bit manipulation |
| 2956 | case ISD::BSWAP: return "bswap"; |
Chris Lattner | 276260b | 2005-05-11 04:50:30 +0000 | [diff] [blame] | 2957 | case ISD::CTPOP: return "ctpop"; |
| 2958 | case ISD::CTTZ: return "cttz"; |
| 2959 | case ISD::CTLZ: return "ctlz"; |
| 2960 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2961 | // Debug info |
| 2962 | case ISD::LOCATION: return "location"; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 2963 | case ISD::DEBUG_LOC: return "debug_loc"; |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 2964 | case ISD::DEBUG_LABEL: return "debug_label"; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2966 | case ISD::CONDCODE: |
| 2967 | switch (cast<CondCodeSDNode>(this)->get()) { |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2968 | default: assert(0 && "Unknown setcc condition!"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2969 | case ISD::SETOEQ: return "setoeq"; |
| 2970 | case ISD::SETOGT: return "setogt"; |
| 2971 | case ISD::SETOGE: return "setoge"; |
| 2972 | case ISD::SETOLT: return "setolt"; |
| 2973 | case ISD::SETOLE: return "setole"; |
| 2974 | case ISD::SETONE: return "setone"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2975 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2976 | case ISD::SETO: return "seto"; |
| 2977 | case ISD::SETUO: return "setuo"; |
| 2978 | case ISD::SETUEQ: return "setue"; |
| 2979 | case ISD::SETUGT: return "setugt"; |
| 2980 | case ISD::SETUGE: return "setuge"; |
| 2981 | case ISD::SETULT: return "setult"; |
| 2982 | case ISD::SETULE: return "setule"; |
| 2983 | case ISD::SETUNE: return "setune"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2984 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2985 | case ISD::SETEQ: return "seteq"; |
| 2986 | case ISD::SETGT: return "setgt"; |
| 2987 | case ISD::SETGE: return "setge"; |
| 2988 | case ISD::SETLT: return "setlt"; |
| 2989 | case ISD::SETLE: return "setle"; |
| 2990 | case ISD::SETNE: return "setne"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2995 | void SDNode::dump() const { dump(0); } |
| 2996 | void SDNode::dump(const SelectionDAG *G) const { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2997 | std::cerr << (void*)this << ": "; |
| 2998 | |
| 2999 | for (unsigned i = 0, e = getNumValues(); i != e; ++i) { |
| 3000 | if (i) std::cerr << ","; |
Chris Lattner | 4ea6924 | 2005-01-15 07:14:32 +0000 | [diff] [blame] | 3001 | if (getValueType(i) == MVT::Other) |
| 3002 | std::cerr << "ch"; |
| 3003 | else |
| 3004 | std::cerr << MVT::getValueTypeString(getValueType(i)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3005 | } |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 3006 | std::cerr << " = " << getOperationName(G); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3007 | |
| 3008 | std::cerr << " "; |
| 3009 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 3010 | if (i) std::cerr << ", "; |
| 3011 | std::cerr << (void*)getOperand(i).Val; |
| 3012 | if (unsigned RN = getOperand(i).ResNo) |
| 3013 | std::cerr << ":" << RN; |
| 3014 | } |
| 3015 | |
| 3016 | if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { |
| 3017 | std::cerr << "<" << CSDN->getValue() << ">"; |
| 3018 | } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { |
| 3019 | std::cerr << "<" << CSDN->getValue() << ">"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3020 | } else if (const GlobalAddressSDNode *GADN = |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3021 | dyn_cast<GlobalAddressSDNode>(this)) { |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 3022 | int offset = GADN->getOffset(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3023 | std::cerr << "<"; |
| 3024 | WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 3025 | if (offset > 0) |
| 3026 | std::cerr << " + " << offset; |
| 3027 | else |
| 3028 | std::cerr << " " << offset; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 3029 | } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3030 | std::cerr << "<" << FIDN->getIndex() << ">"; |
| 3031 | } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ |
Evan Cheng | 38b7327 | 2006-02-26 08:36:57 +0000 | [diff] [blame] | 3032 | int offset = CP->getOffset(); |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 3033 | std::cerr << "<" << *CP->get() << ">"; |
Evan Cheng | 38b7327 | 2006-02-26 08:36:57 +0000 | [diff] [blame] | 3034 | if (offset > 0) |
| 3035 | std::cerr << " + " << offset; |
| 3036 | else |
| 3037 | std::cerr << " " << offset; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 3038 | } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3039 | std::cerr << "<"; |
| 3040 | const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); |
| 3041 | if (LBB) |
| 3042 | std::cerr << LBB->getName() << " "; |
| 3043 | std::cerr << (const void*)BBDN->getBasicBlock() << ">"; |
Chris Lattner | fa164b6 | 2005-08-19 21:34:13 +0000 | [diff] [blame] | 3044 | } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { |
Evan Cheng | 140e99b | 2006-01-11 22:13:48 +0000 | [diff] [blame] | 3045 | if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { |
Chris Lattner | 7228aa7 | 2005-08-19 21:21:16 +0000 | [diff] [blame] | 3046 | std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); |
| 3047 | } else { |
| 3048 | std::cerr << " #" << R->getReg(); |
| 3049 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3050 | } else if (const ExternalSymbolSDNode *ES = |
| 3051 | dyn_cast<ExternalSymbolSDNode>(this)) { |
| 3052 | std::cerr << "'" << ES->getSymbol() << "'"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 3053 | } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { |
| 3054 | if (M->getValue()) |
| 3055 | std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; |
| 3056 | else |
| 3057 | std::cerr << "<null:" << M->getOffset() << ">"; |
Chris Lattner | a23e815 | 2005-08-18 03:31:02 +0000 | [diff] [blame] | 3058 | } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { |
| 3059 | std::cerr << ":" << getValueTypeString(N->getVT()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3060 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 3063 | static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3064 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 3065 | if (N->getOperand(i).Val->hasOneUse()) |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 3066 | DumpNodes(N->getOperand(i).Val, indent+2, G); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3067 | else |
| 3068 | std::cerr << "\n" << std::string(indent+2, ' ') |
| 3069 | << (void*)N->getOperand(i).Val << ": <multiple use>"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3070 | |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3071 | |
| 3072 | std::cerr << "\n" << std::string(indent, ' '); |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 3073 | N->dump(G); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3076 | void SelectionDAG::dump() const { |
| 3077 | std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 3078 | std::vector<const SDNode*> Nodes; |
| 3079 | for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); |
| 3080 | I != E; ++I) |
| 3081 | Nodes.push_back(I); |
| 3082 | |
Chris Lattner | 49d2471 | 2005-01-09 20:26:36 +0000 | [diff] [blame] | 3083 | std::sort(Nodes.begin(), Nodes.end()); |
| 3084 | |
| 3085 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3086 | if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 3087 | DumpNodes(Nodes[i], 2, this); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3088 | } |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 3090 | DumpNodes(getRoot().Val, 2, this); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 3092 | std::cerr << "\n\n"; |
| 3093 | } |
| 3094 | |
Evan Cheng | fae9f1c | 2006-02-09 22:11:03 +0000 | [diff] [blame] | 3095 | /// InsertISelMapEntry - A helper function to insert a key / element pair |
| 3096 | /// into a SDOperand to SDOperand map. This is added to avoid the map |
| 3097 | /// insertion operator from being inlined. |
| 3098 | void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map, |
| 3099 | SDNode *Key, unsigned KeyResNo, |
| 3100 | SDNode *Element, unsigned ElementResNo) { |
| 3101 | Map.insert(std::make_pair(SDOperand(Key, KeyResNo), |
| 3102 | SDOperand(Element, ElementResNo))); |
| 3103 | } |