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" |
| 17 | #include "llvm/Assembly/Writer.h" |
| 18 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | fa164b6 | 2005-08-19 21:34:13 +0000 | [diff] [blame] | 20 | #include "llvm/Target/MRegisterInfo.h" |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 115c036 | 2005-12-19 23:11:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 25 | #include <iostream> |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 26 | #include <set> |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 27 | #include <cmath> |
Jeff Cohen | fd161e9 | 2005-01-09 20:41:56 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Chris Lattner | e25738c | 2004-06-02 04:28:06 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 31 | static bool isCommutativeBinOp(unsigned Opcode) { |
| 32 | switch (Opcode) { |
| 33 | case ISD::ADD: |
| 34 | case ISD::MUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 35 | case ISD::FADD: |
| 36 | case ISD::FMUL: |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 37 | case ISD::AND: |
| 38 | case ISD::OR: |
| 39 | case ISD::XOR: return true; |
| 40 | default: return false; // FIXME: Need commutative info for user ops! |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static bool isAssociativeBinOp(unsigned Opcode) { |
| 45 | switch (Opcode) { |
| 46 | case ISD::ADD: |
| 47 | case ISD::MUL: |
| 48 | case ISD::AND: |
| 49 | case ISD::OR: |
| 50 | case ISD::XOR: return true; |
| 51 | default: return false; // FIXME: Need associative info for user ops! |
| 52 | } |
| 53 | } |
| 54 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 55 | // isInvertibleForFree - Return true if there is no cost to emitting the logical |
| 56 | // inverse of this node. |
| 57 | static bool isInvertibleForFree(SDOperand N) { |
| 58 | if (isa<ConstantSDNode>(N.Val)) return true; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 59 | if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse()) |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 60 | return true; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 61 | return false; |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // ConstantFPSDNode Class |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
| 68 | /// isExactlyValue - We don't rely on operator== working on double values, as |
| 69 | /// it returns true for things that are clearly not equal, like -0.0 and 0.0. |
| 70 | /// As such, this method can be used to do an exact bit-for-bit comparison of |
| 71 | /// two floating point values. |
| 72 | bool ConstantFPSDNode::isExactlyValue(double V) const { |
| 73 | return DoubleToBits(V) == DoubleToBits(Value); |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // ISD Class |
| 78 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 79 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 80 | /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) |
| 81 | /// when given the operation for (X op Y). |
| 82 | ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { |
| 83 | // To perform this operation, we just need to swap the L and G bits of the |
| 84 | // operation. |
| 85 | unsigned OldL = (Operation >> 2) & 1; |
| 86 | unsigned OldG = (Operation >> 1) & 1; |
| 87 | return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits |
| 88 | (OldL << 1) | // New G bit |
| 89 | (OldG << 2)); // New L bit. |
| 90 | } |
| 91 | |
| 92 | /// getSetCCInverse - Return the operation corresponding to !(X op Y), where |
| 93 | /// 'op' is a valid SetCC operation. |
| 94 | ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { |
| 95 | unsigned Operation = Op; |
| 96 | if (isInteger) |
| 97 | Operation ^= 7; // Flip L, G, E bits, but not U. |
| 98 | else |
| 99 | Operation ^= 15; // Flip all of the condition bits. |
| 100 | if (Operation > ISD::SETTRUE2) |
| 101 | Operation &= ~8; // Don't let N and U bits get set. |
| 102 | return ISD::CondCode(Operation); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /// isSignedOp - For an integer comparison, return 1 if the comparison is a |
| 107 | /// signed operation and 2 if the result is an unsigned comparison. Return zero |
| 108 | /// if the operation does not depend on the sign of the input (setne and seteq). |
| 109 | static int isSignedOp(ISD::CondCode Opcode) { |
| 110 | switch (Opcode) { |
| 111 | default: assert(0 && "Illegal integer setcc operation!"); |
| 112 | case ISD::SETEQ: |
| 113 | case ISD::SETNE: return 0; |
| 114 | case ISD::SETLT: |
| 115 | case ISD::SETLE: |
| 116 | case ISD::SETGT: |
| 117 | case ISD::SETGE: return 1; |
| 118 | case ISD::SETULT: |
| 119 | case ISD::SETULE: |
| 120 | case ISD::SETUGT: |
| 121 | case ISD::SETUGE: return 2; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// getSetCCOrOperation - Return the result of a logical OR between different |
| 126 | /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function |
| 127 | /// returns SETCC_INVALID if it is not possible to represent the resultant |
| 128 | /// comparison. |
| 129 | ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 130 | bool isInteger) { |
| 131 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 132 | // Cannot fold a signed integer setcc with an unsigned integer setcc. |
| 133 | return ISD::SETCC_INVALID; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 134 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 135 | unsigned Op = Op1 | Op2; // Combine all of the condition bits. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 136 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 137 | // If the N and U bits get set then the resultant comparison DOES suddenly |
| 138 | // care about orderedness, and is true when ordered. |
| 139 | if (Op > ISD::SETTRUE2) |
| 140 | Op &= ~16; // Clear the N bit. |
| 141 | return ISD::CondCode(Op); |
| 142 | } |
| 143 | |
| 144 | /// getSetCCAndOperation - Return the result of a logical AND between different |
| 145 | /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This |
| 146 | /// function returns zero if it is not possible to represent the resultant |
| 147 | /// comparison. |
| 148 | ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 149 | bool isInteger) { |
| 150 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 151 | // Cannot fold a signed setcc with an unsigned setcc. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 152 | return ISD::SETCC_INVALID; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 153 | |
| 154 | // Combine all of the condition bits. |
| 155 | return ISD::CondCode(Op1 & Op2); |
| 156 | } |
| 157 | |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 158 | const TargetMachine &SelectionDAG::getTarget() const { |
| 159 | return TLI.getTargetMachine(); |
| 160 | } |
| 161 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 162 | //===----------------------------------------------------------------------===// |
| 163 | // SelectionDAG Class |
| 164 | //===----------------------------------------------------------------------===// |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 166 | /// RemoveDeadNodes - This method deletes all unreachable nodes in the |
| 167 | /// SelectionDAG, including nodes (like loads) that have uses of their token |
| 168 | /// chain but no other uses and no side effect. If a node is passed in as an |
| 169 | /// argument, it is used as the seed for node deletion. |
| 170 | void SelectionDAG::RemoveDeadNodes(SDNode *N) { |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 171 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 172 | // to the root node, preventing it from being deleted. |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 173 | HandleSDNode Dummy(getRoot()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 174 | |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 175 | bool MadeChange = false; |
| 176 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 177 | // If we have a hint to start from, use it. |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 178 | if (N && N->use_empty()) { |
| 179 | DestroyDeadNode(N); |
| 180 | MadeChange = true; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 183 | for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I) |
| 184 | if (I->use_empty() && I->getOpcode() != 65535) { |
| 185 | // Node is dead, recursively delete newly dead uses. |
| 186 | DestroyDeadNode(I); |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 187 | MadeChange = true; |
| 188 | } |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 189 | |
| 190 | // Walk the nodes list, removing the nodes we've marked as dead. |
| 191 | if (MadeChange) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 192 | for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) { |
| 193 | SDNode *N = I++; |
| 194 | if (N->use_empty()) |
| 195 | AllNodes.erase(N); |
| 196 | } |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 199 | // 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] | 200 | setRoot(Dummy.getValue()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 203 | /// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the |
| 204 | /// graph. If it is the last user of any of its operands, recursively process |
| 205 | /// them the same way. |
| 206 | /// |
| 207 | void SelectionDAG::DestroyDeadNode(SDNode *N) { |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 208 | // Okay, we really are going to delete this node. First take this out of the |
| 209 | // appropriate CSE map. |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 210 | RemoveNodeFromCSEMaps(N); |
| 211 | |
| 212 | // Next, brutally remove the operand list. This is safe to do, as there are |
| 213 | // no cycles in the graph. |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 214 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) { |
| 215 | SDNode *O = I->Val; |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 216 | O->removeUser(N); |
| 217 | |
| 218 | // 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] | 219 | if (O->use_empty()) |
| 220 | DestroyDeadNode(O); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 221 | } |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 222 | delete[] N->OperandList; |
| 223 | N->OperandList = 0; |
| 224 | N->NumOperands = 0; |
Chris Lattner | f469cb6 | 2005-11-08 18:52:27 +0000 | [diff] [blame] | 225 | |
| 226 | // Mark the node as dead. |
| 227 | N->MorphNodeTo(65535); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 230 | void SelectionDAG::DeleteNode(SDNode *N) { |
| 231 | assert(N->use_empty() && "Cannot delete a node that is not dead!"); |
| 232 | |
| 233 | // First take this out of the appropriate CSE map. |
| 234 | RemoveNodeFromCSEMaps(N); |
| 235 | |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 236 | // Finally, remove uses due to operands of this node, remove from the |
| 237 | // AllNodes list, and delete the node. |
| 238 | DeleteNodeNotInCSEMaps(N); |
| 239 | } |
| 240 | |
| 241 | void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) { |
| 242 | |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 243 | // Remove it from the AllNodes list. |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 244 | AllNodes.remove(N); |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 245 | |
| 246 | // Drop all of the operands and decrement used nodes use counts. |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 247 | for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) |
| 248 | I->Val->removeUser(N); |
| 249 | delete[] N->OperandList; |
| 250 | N->OperandList = 0; |
| 251 | N->NumOperands = 0; |
Chris Lattner | c26aefa | 2005-08-29 21:59:31 +0000 | [diff] [blame] | 252 | |
| 253 | delete N; |
| 254 | } |
| 255 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 256 | /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that |
| 257 | /// correspond to it. This is useful when we're about to delete or repurpose |
| 258 | /// the node. We don't want future request for structurally identical nodes |
| 259 | /// to return N anymore. |
| 260 | void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 261 | bool Erased = false; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 262 | switch (N->getOpcode()) { |
Chris Lattner | 9503859 | 2005-10-05 06:35:28 +0000 | [diff] [blame] | 263 | case ISD::HANDLENODE: return; // noop. |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 264 | case ISD::Constant: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 265 | Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), |
| 266 | N->getValueType(0))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 267 | break; |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 268 | case ISD::TargetConstant: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 269 | Erased = TargetConstants.erase(std::make_pair( |
| 270 | cast<ConstantSDNode>(N)->getValue(), |
| 271 | N->getValueType(0))); |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 272 | break; |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 273 | case ISD::ConstantFP: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 274 | uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 275 | Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 276 | break; |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 278 | case ISD::STRING: |
| 279 | Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue()); |
| 280 | break; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 281 | case ISD::CONDCODE: |
| 282 | assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] && |
| 283 | "Cond code doesn't exist!"); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 284 | Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0; |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 285 | CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0; |
| 286 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 287 | case ISD::GlobalAddress: { |
| 288 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); |
| 289 | Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(), |
| 290 | GN->getOffset())); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 291 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 292 | } |
| 293 | case ISD::TargetGlobalAddress: { |
| 294 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N); |
| 295 | Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(), |
| 296 | GN->getOffset())); |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 297 | break; |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 298 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 299 | case ISD::FrameIndex: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 300 | Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 301 | break; |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 302 | case ISD::TargetFrameIndex: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 303 | Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 304 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 305 | case ISD::ConstantPool: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 306 | Erased = ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 307 | break; |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 308 | case ISD::TargetConstantPool: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 309 | Erased =TargetConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->get()); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 310 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 311 | case ISD::BasicBlock: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 312 | Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 313 | break; |
| 314 | case ISD::ExternalSymbol: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 315 | Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 316 | break; |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 317 | case ISD::TargetExternalSymbol: |
| 318 | Erased = TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
| 319 | break; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 320 | case ISD::VALUETYPE: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 321 | Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 322 | ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0; |
| 323 | break; |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 324 | case ISD::Register: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 325 | Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(), |
| 326 | N->getValueType(0))); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 327 | break; |
Chris Lattner | c534395 | 2005-08-05 16:55:31 +0000 | [diff] [blame] | 328 | case ISD::SRCVALUE: { |
| 329 | SrcValueSDNode *SVN = cast<SrcValueSDNode>(N); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 330 | Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset())); |
Chris Lattner | c534395 | 2005-08-05 16:55:31 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 333 | case ISD::LOAD: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 334 | Erased = Loads.erase(std::make_pair(N->getOperand(1), |
| 335 | std::make_pair(N->getOperand(0), |
| 336 | N->getValueType(0)))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 337 | break; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 338 | default: |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 339 | if (N->getNumValues() == 1) { |
Chris Lattner | 70b9b10 | 2005-09-02 19:36:17 +0000 | [diff] [blame] | 340 | if (N->getNumOperands() == 0) { |
| 341 | Erased = NullaryOps.erase(std::make_pair(N->getOpcode(), |
| 342 | N->getValueType(0))); |
| 343 | } else if (N->getNumOperands() == 1) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 344 | Erased = |
| 345 | UnaryOps.erase(std::make_pair(N->getOpcode(), |
| 346 | std::make_pair(N->getOperand(0), |
| 347 | N->getValueType(0)))); |
| 348 | } else if (N->getNumOperands() == 2) { |
| 349 | Erased = |
| 350 | BinaryOps.erase(std::make_pair(N->getOpcode(), |
| 351 | std::make_pair(N->getOperand(0), |
| 352 | N->getOperand(1)))); |
| 353 | } else { |
| 354 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 355 | Erased = |
| 356 | OneResultNodes.erase(std::make_pair(N->getOpcode(), |
| 357 | std::make_pair(N->getValueType(0), |
| 358 | Ops))); |
| 359 | } |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 360 | } else { |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 361 | // Remove the node from the ArbitraryNodes map. |
| 362 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 363 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 364 | Erased = |
| 365 | ArbitraryNodes.erase(std::make_pair(N->getOpcode(), |
| 366 | std::make_pair(RV, Ops))); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 368 | break; |
| 369 | } |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 370 | #ifndef NDEBUG |
| 371 | // Verify that the node was actually in one of the CSE maps, unless it has a |
| 372 | // flag result (which cannot be CSE'd) or is one of the special cases that are |
| 373 | // not subject to CSE. |
| 374 | if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag && |
| 375 | N->getOpcode() != ISD::CALL && N->getOpcode() != ISD::CALLSEQ_START && |
Chris Lattner | 6a8a21c | 2005-09-03 01:04:40 +0000 | [diff] [blame] | 376 | N->getOpcode() != ISD::CALLSEQ_END && !N->isTargetOpcode()) { |
Chris Lattner | 6621e3b | 2005-09-02 19:15:44 +0000 | [diff] [blame] | 377 | |
| 378 | N->dump(); |
| 379 | assert(0 && "Node is not in map!"); |
| 380 | } |
| 381 | #endif |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 384 | /// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It |
| 385 | /// has been taken out and modified in some way. If the specified node already |
| 386 | /// exists in the CSE maps, do not modify the maps, but return the existing node |
| 387 | /// instead. If it doesn't exist, add it and return null. |
| 388 | /// |
| 389 | SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) { |
| 390 | assert(N->getNumOperands() && "This is a leaf node!"); |
Chris Lattner | c85a9f3 | 2005-11-30 18:20:52 +0000 | [diff] [blame] | 391 | if (N->getOpcode() == ISD::CALLSEQ_START || |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 392 | N->getOpcode() == ISD::CALLSEQ_END || |
Chris Lattner | 9f8cc69 | 2005-12-19 22:21:21 +0000 | [diff] [blame] | 393 | N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag) |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 394 | return 0; // Never add these nodes. |
Chris Lattner | c85a9f3 | 2005-11-30 18:20:52 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 9f8cc69 | 2005-12-19 22:21:21 +0000 | [diff] [blame] | 396 | // Check that remaining values produced are not flags. |
| 397 | for (unsigned i = 1, e = N->getNumValues(); i != e; ++i) |
| 398 | if (N->getValueType(i) == MVT::Flag) |
| 399 | return 0; // Never CSE anything that produces a flag. |
| 400 | |
Chris Lattner | fe14b34 | 2005-12-01 23:14:50 +0000 | [diff] [blame] | 401 | if (N->getNumValues() == 1) { |
| 402 | if (N->getNumOperands() == 1) { |
| 403 | SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(), |
| 404 | std::make_pair(N->getOperand(0), |
| 405 | N->getValueType(0)))]; |
| 406 | if (U) return U; |
| 407 | U = N; |
| 408 | } else if (N->getNumOperands() == 2) { |
| 409 | SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(), |
| 410 | std::make_pair(N->getOperand(0), |
| 411 | N->getOperand(1)))]; |
| 412 | if (B) return B; |
| 413 | B = N; |
| 414 | } else { |
| 415 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 416 | SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(), |
| 417 | std::make_pair(N->getValueType(0), Ops))]; |
| 418 | if (ORN) return ORN; |
| 419 | ORN = N; |
| 420 | } |
| 421 | } else { |
| 422 | if (N->getOpcode() == ISD::LOAD) { |
| 423 | SDNode *&L = Loads[std::make_pair(N->getOperand(1), |
| 424 | std::make_pair(N->getOperand(0), |
| 425 | N->getValueType(0)))]; |
| 426 | if (L) return L; |
| 427 | L = N; |
| 428 | } else { |
| 429 | // Remove the node from the ArbitraryNodes map. |
| 430 | std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end()); |
| 431 | std::vector<SDOperand> Ops(N->op_begin(), N->op_end()); |
| 432 | SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(), |
| 433 | std::make_pair(RV, Ops))]; |
| 434 | if (AN) return AN; |
| 435 | AN = N; |
| 436 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 437 | } |
| 438 | return 0; |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 443 | SelectionDAG::~SelectionDAG() { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 444 | while (!AllNodes.empty()) { |
| 445 | SDNode *N = AllNodes.begin(); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 446 | delete [] N->OperandList; |
| 447 | N->OperandList = 0; |
| 448 | N->NumOperands = 0; |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 449 | AllNodes.pop_front(); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 0f2287b | 2005-04-13 02:38:18 +0000 | [diff] [blame] | 453 | SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) { |
Chris Lattner | 51679c4 | 2005-04-13 19:41:05 +0000 | [diff] [blame] | 454 | if (Op.getValueType() == VT) return Op; |
Jeff Cohen | 19bb228 | 2005-05-10 02:22:38 +0000 | [diff] [blame] | 455 | int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT)); |
Chris Lattner | 0f2287b | 2005-04-13 02:38:18 +0000 | [diff] [blame] | 456 | return getNode(ISD::AND, Op.getValueType(), Op, |
| 457 | getConstant(Imm, Op.getValueType())); |
| 458 | } |
| 459 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 460 | SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { |
| 461 | assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); |
| 462 | // Mask out any bits that are not valid for this constant. |
Chris Lattner | 623f70d | 2005-01-08 06:24:30 +0000 | [diff] [blame] | 463 | if (VT != MVT::i64) |
| 464 | Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 465 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 466 | SDNode *&N = Constants[std::make_pair(Val, VT)]; |
| 467 | if (N) return SDOperand(N, 0); |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 468 | N = new ConstantSDNode(false, Val, VT); |
| 469 | AllNodes.push_back(N); |
| 470 | return SDOperand(N, 0); |
| 471 | } |
| 472 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 473 | SDOperand SelectionDAG::getString(const std::string &Val) { |
| 474 | StringSDNode *&N = StringNodes[Val]; |
| 475 | if (!N) { |
| 476 | N = new StringSDNode(Val); |
| 477 | AllNodes.push_back(N); |
| 478 | } |
| 479 | return SDOperand(N, 0); |
| 480 | } |
| 481 | |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 482 | SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) { |
| 483 | assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); |
| 484 | // Mask out any bits that are not valid for this constant. |
| 485 | if (VT != MVT::i64) |
| 486 | Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; |
| 487 | |
| 488 | SDNode *&N = TargetConstants[std::make_pair(Val, VT)]; |
| 489 | if (N) return SDOperand(N, 0); |
| 490 | N = new ConstantSDNode(true, Val, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 491 | AllNodes.push_back(N); |
| 492 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 495 | SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { |
| 496 | assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); |
| 497 | if (VT == MVT::f32) |
| 498 | Val = (float)Val; // Mask out extra precision. |
| 499 | |
Chris Lattner | d865861 | 2005-02-17 20:17:32 +0000 | [diff] [blame] | 500 | // Do the map lookup using the actual bit pattern for the floating point |
| 501 | // value, so that we don't have problems with 0.0 comparing equal to -0.0, and |
| 502 | // we don't have issues with SNANs. |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 503 | SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 504 | if (N) return SDOperand(N, 0); |
| 505 | N = new ConstantFPSDNode(Val, VT); |
| 506 | AllNodes.push_back(N); |
| 507 | return SDOperand(N, 0); |
| 508 | } |
| 509 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 510 | SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 511 | MVT::ValueType VT, int offset) { |
| 512 | SDNode *&N = GlobalValues[std::make_pair(GV, offset)]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 513 | if (N) return SDOperand(N, 0); |
Nate Begeman | 512beb9 | 2005-12-30 00:10:38 +0000 | [diff] [blame] | 514 | N = new GlobalAddressSDNode(false, GV, VT, offset); |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 515 | AllNodes.push_back(N); |
| 516 | return SDOperand(N, 0); |
| 517 | } |
| 518 | |
| 519 | SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV, |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 520 | MVT::ValueType VT, int offset) { |
Evan Cheng | 14229bb | 2005-11-30 02:49:21 +0000 | [diff] [blame] | 521 | SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)]; |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 522 | if (N) return SDOperand(N, 0); |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 523 | N = new GlobalAddressSDNode(true, GV, VT, offset); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 524 | AllNodes.push_back(N); |
| 525 | return SDOperand(N, 0); |
| 526 | } |
| 527 | |
| 528 | SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { |
| 529 | SDNode *&N = FrameIndices[FI]; |
| 530 | if (N) return SDOperand(N, 0); |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 531 | N = new FrameIndexSDNode(FI, VT, false); |
| 532 | AllNodes.push_back(N); |
| 533 | return SDOperand(N, 0); |
| 534 | } |
| 535 | |
| 536 | SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) { |
| 537 | SDNode *&N = TargetFrameIndices[FI]; |
| 538 | if (N) return SDOperand(N, 0); |
| 539 | N = new FrameIndexSDNode(FI, VT, true); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 540 | AllNodes.push_back(N); |
| 541 | return SDOperand(N, 0); |
| 542 | } |
| 543 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 544 | SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT) { |
| 545 | SDNode *&N = ConstantPoolIndices[C]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 546 | if (N) return SDOperand(N, 0); |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 547 | N = new ConstantPoolSDNode(C, VT, false); |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 548 | AllNodes.push_back(N); |
| 549 | return SDOperand(N, 0); |
| 550 | } |
| 551 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 552 | SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT) { |
| 553 | SDNode *&N = TargetConstantPoolIndices[C]; |
Chris Lattner | 4025a9c | 2005-08-25 05:03:06 +0000 | [diff] [blame] | 554 | if (N) return SDOperand(N, 0); |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 555 | N = new ConstantPoolSDNode(C, VT, true); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 556 | AllNodes.push_back(N); |
| 557 | return SDOperand(N, 0); |
| 558 | } |
| 559 | |
| 560 | SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { |
| 561 | SDNode *&N = BBNodes[MBB]; |
| 562 | if (N) return SDOperand(N, 0); |
| 563 | N = new BasicBlockSDNode(MBB); |
| 564 | AllNodes.push_back(N); |
| 565 | return SDOperand(N, 0); |
| 566 | } |
| 567 | |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 568 | SDOperand SelectionDAG::getValueType(MVT::ValueType VT) { |
| 569 | if ((unsigned)VT >= ValueTypeNodes.size()) |
| 570 | ValueTypeNodes.resize(VT+1); |
| 571 | if (ValueTypeNodes[VT] == 0) { |
| 572 | ValueTypeNodes[VT] = new VTSDNode(VT); |
| 573 | AllNodes.push_back(ValueTypeNodes[VT]); |
| 574 | } |
| 575 | |
| 576 | return SDOperand(ValueTypeNodes[VT], 0); |
| 577 | } |
| 578 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 579 | SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { |
| 580 | SDNode *&N = ExternalSymbols[Sym]; |
| 581 | if (N) return SDOperand(N, 0); |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 582 | N = new ExternalSymbolSDNode(false, Sym, VT); |
| 583 | AllNodes.push_back(N); |
| 584 | return SDOperand(N, 0); |
| 585 | } |
| 586 | |
| 587 | SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym, MVT::ValueType VT) { |
| 588 | SDNode *&N = TargetExternalSymbols[Sym]; |
| 589 | if (N) return SDOperand(N, 0); |
| 590 | N = new ExternalSymbolSDNode(true, Sym, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 591 | AllNodes.push_back(N); |
| 592 | return SDOperand(N, 0); |
| 593 | } |
| 594 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 595 | SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) { |
| 596 | if ((unsigned)Cond >= CondCodeNodes.size()) |
| 597 | CondCodeNodes.resize(Cond+1); |
| 598 | |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 599 | if (CondCodeNodes[Cond] == 0) { |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 600 | CondCodeNodes[Cond] = new CondCodeSDNode(Cond); |
Chris Lattner | 079a27a | 2005-08-09 20:40:02 +0000 | [diff] [blame] | 601 | AllNodes.push_back(CondCodeNodes[Cond]); |
| 602 | } |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 603 | return SDOperand(CondCodeNodes[Cond], 0); |
| 604 | } |
| 605 | |
Chris Lattner | 0fdd768 | 2005-08-30 22:38:38 +0000 | [diff] [blame] | 606 | SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) { |
| 607 | RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)]; |
| 608 | if (!Reg) { |
| 609 | Reg = new RegisterSDNode(RegNo, VT); |
| 610 | AllNodes.push_back(Reg); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 611 | } |
Chris Lattner | 0fdd768 | 2005-08-30 22:38:38 +0000 | [diff] [blame] | 612 | return SDOperand(Reg, 0); |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 615 | SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1, |
| 616 | SDOperand N2, ISD::CondCode Cond) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 617 | // These setcc operations always fold. |
| 618 | switch (Cond) { |
| 619 | default: break; |
| 620 | case ISD::SETFALSE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 621 | case ISD::SETFALSE2: return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 622 | case ISD::SETTRUE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 623 | case ISD::SETTRUE2: return getConstant(1, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 626 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { |
| 627 | uint64_t C2 = N2C->getValue(); |
| 628 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) { |
| 629 | uint64_t C1 = N1C->getValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 630 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 631 | // Sign extend the operands if required |
| 632 | if (ISD::isSignedIntSetCC(Cond)) { |
| 633 | C1 = N1C->getSignExtended(); |
| 634 | C2 = N2C->getSignExtended(); |
| 635 | } |
| 636 | |
| 637 | switch (Cond) { |
| 638 | default: assert(0 && "Unknown integer setcc!"); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 639 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 640 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 641 | case ISD::SETULT: return getConstant(C1 < C2, VT); |
| 642 | case ISD::SETUGT: return getConstant(C1 > C2, VT); |
| 643 | case ISD::SETULE: return getConstant(C1 <= C2, VT); |
| 644 | case ISD::SETUGE: return getConstant(C1 >= C2, VT); |
| 645 | case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); |
| 646 | case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); |
| 647 | case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); |
| 648 | case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 649 | } |
Chris Lattner | 2467392 | 2005-04-07 18:58:54 +0000 | [diff] [blame] | 650 | } else { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 651 | // 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] | 652 | if (N1.getOpcode() == ISD::ZERO_EXTEND) { |
| 653 | unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType()); |
| 654 | |
| 655 | // If the comparison constant has bits in the upper part, the |
| 656 | // zero-extended value could never match. |
| 657 | if (C2 & (~0ULL << InSize)) { |
| 658 | unsigned VSize = MVT::getSizeInBits(N1.getValueType()); |
| 659 | switch (Cond) { |
| 660 | case ISD::SETUGT: |
| 661 | case ISD::SETUGE: |
| 662 | case ISD::SETEQ: return getConstant(0, VT); |
| 663 | case ISD::SETULT: |
| 664 | case ISD::SETULE: |
| 665 | case ISD::SETNE: return getConstant(1, VT); |
| 666 | case ISD::SETGT: |
| 667 | case ISD::SETGE: |
| 668 | // True if the sign bit of C2 is set. |
| 669 | return getConstant((C2 & (1ULL << VSize)) != 0, VT); |
| 670 | case ISD::SETLT: |
| 671 | case ISD::SETLE: |
| 672 | // True if the sign bit of C2 isn't set. |
| 673 | return getConstant((C2 & (1ULL << VSize)) == 0, VT); |
| 674 | default: |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // Otherwise, we can perform the comparison with the low bits. |
| 680 | switch (Cond) { |
| 681 | case ISD::SETEQ: |
| 682 | case ISD::SETNE: |
| 683 | case ISD::SETUGT: |
| 684 | case ISD::SETUGE: |
| 685 | case ISD::SETULT: |
| 686 | case ISD::SETULE: |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 687 | return getSetCC(VT, N1.getOperand(0), |
| 688 | getConstant(C2, N1.getOperand(0).getValueType()), |
| 689 | Cond); |
Chris Lattner | 4a44c8d | 2005-04-18 04:30:45 +0000 | [diff] [blame] | 690 | default: |
| 691 | break; // todo, be more careful with signed comparisons |
| 692 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 693 | } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 694 | (Cond == ISD::SETEQ || Cond == ISD::SETNE)) { |
| 695 | MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT(); |
| 696 | unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy); |
| 697 | MVT::ValueType ExtDstTy = N1.getValueType(); |
| 698 | unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 700 | // If the extended part has any inconsistent bits, it cannot ever |
| 701 | // compare equal. In other words, they have to be all ones or all |
| 702 | // zeros. |
| 703 | uint64_t ExtBits = |
Jeff Cohen | 7383ce4 | 2005-08-31 02:47:06 +0000 | [diff] [blame] | 704 | (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1)); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 705 | if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits) |
| 706 | return getConstant(Cond == ISD::SETNE, VT); |
| 707 | |
| 708 | // Otherwise, make this a use of a zext. |
| 709 | return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy), |
Jeff Cohen | 7383ce4 | 2005-08-31 02:47:06 +0000 | [diff] [blame] | 710 | getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy), |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 711 | Cond); |
Chris Lattner | 4a44c8d | 2005-04-18 04:30:45 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 714 | uint64_t MinVal, MaxVal; |
| 715 | unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0)); |
| 716 | if (ISD::isSignedIntSetCC(Cond)) { |
| 717 | MinVal = 1ULL << (OperandBitSize-1); |
| 718 | if (OperandBitSize != 1) // Avoid X >> 64, which is undefined. |
| 719 | MaxVal = ~0ULL >> (65-OperandBitSize); |
| 720 | else |
| 721 | MaxVal = 0; |
| 722 | } else { |
| 723 | MinVal = 0; |
| 724 | MaxVal = ~0ULL >> (64-OperandBitSize); |
| 725 | } |
| 726 | |
| 727 | // Canonicalize GE/LE comparisons to use GT/LT comparisons. |
| 728 | if (Cond == ISD::SETGE || Cond == ISD::SETUGE) { |
| 729 | if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true |
| 730 | --C2; // X >= C1 --> X > (C1-1) |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 731 | return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), |
| 732 | (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if (Cond == ISD::SETLE || Cond == ISD::SETULE) { |
| 736 | if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true |
| 737 | ++C2; // X <= C1 --> X < (C1+1) |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 738 | return getSetCC(VT, N1, getConstant(C2, N2.getValueType()), |
| 739 | (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 740 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 741 | |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 742 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal) |
| 743 | return getConstant(0, VT); // X < MIN --> false |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 744 | |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 745 | // Canonicalize setgt X, Min --> setne X, Min |
| 746 | if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 747 | return getSetCC(VT, N1, N2, ISD::SETNE); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 3b2c1d9 | 2005-04-12 00:28:49 +0000 | [diff] [blame] | 749 | // If we have setult X, 1, turn it into seteq X, 0 |
| 750 | if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 751 | return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()), |
| 752 | ISD::SETEQ); |
Nate Begeman | 72ea281 | 2005-04-14 08:56:52 +0000 | [diff] [blame] | 753 | // 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] | 754 | else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 755 | return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()), |
| 756 | ISD::SETEQ); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 757 | |
| 758 | // If we have "setcc X, C1", check to see if we can shrink the immediate |
| 759 | // by changing cc. |
| 760 | |
| 761 | // SETUGT X, SINTMAX -> SETLT X, 0 |
| 762 | if (Cond == ISD::SETUGT && OperandBitSize != 1 && |
| 763 | C2 == (~0ULL >> (65-OperandBitSize))) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 764 | return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 765 | |
| 766 | // FIXME: Implement the rest of these. |
| 767 | |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 768 | |
| 769 | // Fold bit comparisons when we can. |
| 770 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
| 771 | VT == N1.getValueType() && N1.getOpcode() == ISD::AND) |
| 772 | if (ConstantSDNode *AndRHS = |
| 773 | dyn_cast<ConstantSDNode>(N1.getOperand(1))) { |
| 774 | if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3 |
| 775 | // Perform the xform if the AND RHS is a single bit. |
| 776 | if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) { |
| 777 | return getNode(ISD::SRL, VT, N1, |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 778 | getConstant(Log2_64(AndRHS->getValue()), |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 779 | TLI.getShiftAmountTy())); |
| 780 | } |
| 781 | } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) { |
| 782 | // (X & 8) == 8 --> (X & 8) >> 3 |
| 783 | // Perform the xform if C2 is a single bit. |
| 784 | if ((C2 & (C2-1)) == 0) { |
| 785 | return getNode(ISD::SRL, VT, N1, |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 786 | getConstant(Log2_64(C2),TLI.getShiftAmountTy())); |
Chris Lattner | 1c2a9b9 | 2005-04-21 06:12:41 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 790 | } |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 791 | } else if (isa<ConstantSDNode>(N1.Val)) { |
| 792 | // Ensure that the constant occurs on the RHS. |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 793 | return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); |
Chris Lattner | 67255a1 | 2005-04-07 18:14:58 +0000 | [diff] [blame] | 794 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 795 | |
| 796 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) |
| 797 | if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { |
| 798 | double C1 = N1C->getValue(), C2 = N2C->getValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 799 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 800 | switch (Cond) { |
| 801 | default: break; // FIXME: Implement the rest of these! |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 802 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 803 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 804 | case ISD::SETLT: return getConstant(C1 < C2, VT); |
| 805 | case ISD::SETGT: return getConstant(C1 > C2, VT); |
| 806 | case ISD::SETLE: return getConstant(C1 <= C2, VT); |
| 807 | case ISD::SETGE: return getConstant(C1 >= C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 808 | } |
| 809 | } else { |
| 810 | // Ensure that the constant occurs on the RHS. |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 811 | return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 814 | // Could not fold it. |
| 815 | return SDOperand(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 818 | /// getNode - Gets or creates the specified node. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 819 | /// |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 820 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { |
Chris Lattner | 70b9b10 | 2005-09-02 19:36:17 +0000 | [diff] [blame] | 821 | SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)]; |
| 822 | if (!N) { |
| 823 | N = new SDNode(Opcode, VT); |
| 824 | AllNodes.push_back(N); |
| 825 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 826 | return SDOperand(N, 0); |
| 827 | } |
| 828 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 829 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 830 | SDOperand Operand) { |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 831 | // Constant fold unary operations with an integer constant operand. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 832 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { |
| 833 | uint64_t Val = C->getValue(); |
| 834 | switch (Opcode) { |
| 835 | default: break; |
| 836 | case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 837 | case ISD::ANY_EXTEND: |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 838 | case ISD::ZERO_EXTEND: return getConstant(Val, VT); |
| 839 | case ISD::TRUNCATE: return getConstant(Val, VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 840 | case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); |
| 841 | case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 842 | case ISD::BIT_CONVERT: |
| 843 | if (VT == MVT::f32) { |
| 844 | assert(C->getValueType(0) == MVT::i32 && "Invalid bit_convert!"); |
| 845 | return getConstantFP(BitsToFloat(Val), VT); |
| 846 | } else if (VT == MVT::f64) { |
| 847 | assert(C->getValueType(0) == MVT::i64 && "Invalid bit_convert!"); |
| 848 | return getConstantFP(BitsToDouble(Val), VT); |
| 849 | } |
| 850 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 854 | // Constant fold unary operations with an floating point constant operand. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 855 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) |
| 856 | switch (Opcode) { |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 857 | case ISD::FNEG: |
| 858 | return getConstantFP(-C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 859 | case ISD::FABS: |
| 860 | return getConstantFP(fabs(C->getValue()), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 861 | case ISD::FP_ROUND: |
| 862 | case ISD::FP_EXTEND: |
| 863 | return getConstantFP(C->getValue(), VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 864 | case ISD::FP_TO_SINT: |
| 865 | return getConstant((int64_t)C->getValue(), VT); |
| 866 | case ISD::FP_TO_UINT: |
| 867 | return getConstant((uint64_t)C->getValue(), VT); |
Chris Lattner | 9468377 | 2005-12-23 05:30:37 +0000 | [diff] [blame] | 868 | case ISD::BIT_CONVERT: |
| 869 | if (VT == MVT::i32) { |
| 870 | assert(C->getValueType(0) == MVT::f32 && "Invalid bit_convert!"); |
| 871 | return getConstant(FloatToBits(C->getValue()), VT); |
| 872 | } else if (VT == MVT::i64) { |
| 873 | assert(C->getValueType(0) == MVT::f64 && "Invalid bit_convert!"); |
| 874 | return getConstant(DoubleToBits(C->getValue()), VT); |
| 875 | } |
| 876 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | unsigned OpOpcode = Operand.Val->getOpcode(); |
| 880 | switch (Opcode) { |
Chris Lattner | a93ec3e | 2005-01-21 18:01:22 +0000 | [diff] [blame] | 881 | case ISD::TokenFactor: |
| 882 | return Operand; // Factor of one node? No factor. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 883 | case ISD::SIGN_EXTEND: |
| 884 | if (Operand.getValueType() == VT) return Operand; // noop extension |
| 885 | if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) |
| 886 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 887 | break; |
| 888 | case ISD::ZERO_EXTEND: |
| 889 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Chris Lattner | 5a6bace | 2005-04-07 19:43:53 +0000 | [diff] [blame] | 890 | if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x) |
Chris Lattner | 2f0ca79 | 2005-01-12 18:51:15 +0000 | [diff] [blame] | 891 | return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 892 | break; |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 893 | case ISD::ANY_EXTEND: |
| 894 | if (Operand.getValueType() == VT) return Operand; // noop extension |
| 895 | if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) |
| 896 | // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x) |
| 897 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 898 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 899 | case ISD::TRUNCATE: |
| 900 | if (Operand.getValueType() == VT) return Operand; // noop truncate |
| 901 | if (OpOpcode == ISD::TRUNCATE) |
| 902 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 903 | else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND || |
| 904 | OpOpcode == ISD::ANY_EXTEND) { |
Chris Lattner | fd8c39b | 2005-01-07 21:56:24 +0000 | [diff] [blame] | 905 | // If the source is smaller than the dest, we still need an extend. |
| 906 | if (Operand.Val->getOperand(0).getValueType() < VT) |
| 907 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 908 | else if (Operand.Val->getOperand(0).getValueType() > VT) |
| 909 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
| 910 | else |
| 911 | return Operand.Val->getOperand(0); |
| 912 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 913 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 914 | case ISD::BIT_CONVERT: |
| 915 | // Basic sanity checking. |
| 916 | assert(MVT::getSizeInBits(VT)==MVT::getSizeInBits(Operand.getValueType()) && |
| 917 | "Cannot BIT_CONVERT between two different types!"); |
| 918 | if (VT == Operand.getValueType()) return Operand; // noop conversion. |
Chris Lattner | c8547d8 | 2005-12-23 05:37:50 +0000 | [diff] [blame] | 919 | if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x) |
| 920 | return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 921 | break; |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 922 | case ISD::FNEG: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 923 | if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X) |
| 924 | return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1), |
Chris Lattner | 485df9b | 2005-04-09 03:02:46 +0000 | [diff] [blame] | 925 | Operand.Val->getOperand(0)); |
| 926 | if (OpOpcode == ISD::FNEG) // --X -> X |
| 927 | return Operand.Val->getOperand(0); |
| 928 | break; |
| 929 | case ISD::FABS: |
| 930 | if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X) |
| 931 | return getNode(ISD::FABS, VT, Operand.Val->getOperand(0)); |
| 932 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 935 | SDNode *N; |
| 936 | if (VT != MVT::Flag) { // Don't CSE flag producing nodes |
| 937 | SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; |
Chris Lattner | f07d023 | 2005-08-26 00:13:12 +0000 | [diff] [blame] | 938 | if (E) return SDOperand(E, 0); |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 939 | E = N = new SDNode(Opcode, Operand); |
| 940 | } else { |
| 941 | N = new SDNode(Opcode, Operand); |
| 942 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 943 | N->setValueTypes(VT); |
| 944 | AllNodes.push_back(N); |
| 945 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Chris Lattner | 36019aa | 2005-04-18 03:48:41 +0000 | [diff] [blame] | 948 | |
| 949 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 950 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 951 | SDOperand N1, SDOperand N2) { |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 952 | #ifndef NDEBUG |
| 953 | switch (Opcode) { |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 954 | case ISD::TokenFactor: |
| 955 | assert(VT == MVT::Other && N1.getValueType() == MVT::Other && |
| 956 | N2.getValueType() == MVT::Other && "Invalid token factor!"); |
| 957 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 958 | case ISD::AND: |
| 959 | case ISD::OR: |
| 960 | case ISD::XOR: |
| 961 | case ISD::UDIV: |
| 962 | case ISD::UREM: |
Chris Lattner | e5eb6f8 | 2005-05-15 05:39:08 +0000 | [diff] [blame] | 963 | case ISD::MULHU: |
| 964 | case ISD::MULHS: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 965 | assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); |
| 966 | // fall through |
| 967 | case ISD::ADD: |
| 968 | case ISD::SUB: |
| 969 | case ISD::MUL: |
| 970 | case ISD::SDIV: |
| 971 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 972 | assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops"); |
| 973 | // fall through. |
| 974 | case ISD::FADD: |
| 975 | case ISD::FSUB: |
| 976 | case ISD::FMUL: |
| 977 | case ISD::FDIV: |
| 978 | case ISD::FREM: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 979 | assert(N1.getValueType() == N2.getValueType() && |
| 980 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 981 | break; |
| 982 | |
| 983 | case ISD::SHL: |
| 984 | case ISD::SRA: |
| 985 | case ISD::SRL: |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 986 | case ISD::ROTL: |
| 987 | case ISD::ROTR: |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 988 | assert(VT == N1.getValueType() && |
| 989 | "Shift operators return type must be the same as their first arg"); |
| 990 | assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 991 | VT != MVT::i1 && "Shifts only work on integers"); |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 992 | break; |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 993 | case ISD::FP_ROUND_INREG: { |
| 994 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 995 | assert(VT == N1.getValueType() && "Not an inreg round!"); |
| 996 | assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && |
| 997 | "Cannot FP_ROUND_INREG integer types"); |
| 998 | assert(EVT <= VT && "Not rounding down!"); |
| 999 | break; |
| 1000 | } |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 1001 | case ISD::AssertSext: |
| 1002 | case ISD::AssertZext: |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1003 | case ISD::SIGN_EXTEND_INREG: { |
| 1004 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 1005 | assert(VT == N1.getValueType() && "Not an inreg extend!"); |
| 1006 | assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && |
| 1007 | "Cannot *_EXTEND_INREG FP types"); |
| 1008 | assert(EVT <= VT && "Not extending!"); |
| 1009 | } |
| 1010 | |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 1011 | default: break; |
| 1012 | } |
| 1013 | #endif |
| 1014 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1015 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 1016 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 1017 | if (N1C) { |
| 1018 | if (N2C) { |
| 1019 | uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); |
| 1020 | switch (Opcode) { |
| 1021 | case ISD::ADD: return getConstant(C1 + C2, VT); |
| 1022 | case ISD::SUB: return getConstant(C1 - C2, VT); |
| 1023 | case ISD::MUL: return getConstant(C1 * C2, VT); |
| 1024 | case ISD::UDIV: |
| 1025 | if (C2) return getConstant(C1 / C2, VT); |
| 1026 | break; |
| 1027 | case ISD::UREM : |
| 1028 | if (C2) return getConstant(C1 % C2, VT); |
| 1029 | break; |
| 1030 | case ISD::SDIV : |
| 1031 | if (C2) return getConstant(N1C->getSignExtended() / |
| 1032 | N2C->getSignExtended(), VT); |
| 1033 | break; |
| 1034 | case ISD::SREM : |
| 1035 | if (C2) return getConstant(N1C->getSignExtended() % |
| 1036 | N2C->getSignExtended(), VT); |
| 1037 | break; |
| 1038 | case ISD::AND : return getConstant(C1 & C2, VT); |
| 1039 | case ISD::OR : return getConstant(C1 | C2, VT); |
| 1040 | case ISD::XOR : return getConstant(C1 ^ C2, VT); |
Nate Begeman | b85dfab | 2005-08-31 00:27:53 +0000 | [diff] [blame] | 1041 | case ISD::SHL : return getConstant(C1 << C2, VT); |
| 1042 | case ISD::SRL : return getConstant(C1 >> C2, VT); |
Chris Lattner | 8136d1f | 2005-01-10 00:07:15 +0000 | [diff] [blame] | 1043 | case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1044 | case ISD::ROTL : |
| 1045 | return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)), |
| 1046 | VT); |
| 1047 | case ISD::ROTR : |
| 1048 | return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)), |
| 1049 | VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1050 | default: break; |
| 1051 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1052 | } else { // Cannonicalize constant to RHS if commutative |
| 1053 | if (isCommutativeBinOp(Opcode)) { |
| 1054 | std::swap(N1C, N2C); |
| 1055 | std::swap(N1, N2); |
| 1056 | } |
| 1057 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); |
| 1061 | ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1062 | if (N1CFP) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1063 | if (N2CFP) { |
| 1064 | double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); |
| 1065 | switch (Opcode) { |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1066 | case ISD::FADD: return getConstantFP(C1 + C2, VT); |
| 1067 | case ISD::FSUB: return getConstantFP(C1 - C2, VT); |
| 1068 | case ISD::FMUL: return getConstantFP(C1 * C2, VT); |
| 1069 | case ISD::FDIV: |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1070 | if (C2) return getConstantFP(C1 / C2, VT); |
| 1071 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1072 | case ISD::FREM : |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1073 | if (C2) return getConstantFP(fmod(C1, C2), VT); |
| 1074 | break; |
| 1075 | default: break; |
| 1076 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1077 | } else { // Cannonicalize constant to RHS if commutative |
| 1078 | if (isCommutativeBinOp(Opcode)) { |
| 1079 | std::swap(N1CFP, N2CFP); |
| 1080 | std::swap(N1, N2); |
| 1081 | } |
| 1082 | } |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1085 | // Finally, fold operations that do not require constants. |
| 1086 | switch (Opcode) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1087 | case ISD::FP_ROUND_INREG: |
| 1088 | if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding. |
| 1089 | break; |
| 1090 | case ISD::SIGN_EXTEND_INREG: { |
| 1091 | MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT(); |
| 1092 | if (EVT == VT) return N1; // Not actually extending |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | } |
| 1095 | |
Nate Begeman | eea805e | 2005-04-13 21:23:31 +0000 | [diff] [blame] | 1096 | // FIXME: figure out how to safely handle things like |
| 1097 | // int foo(int x) { return 1 << (x & 255); } |
| 1098 | // int bar() { return foo(256); } |
| 1099 | #if 0 |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1100 | case ISD::SHL: |
| 1101 | case ISD::SRL: |
| 1102 | case ISD::SRA: |
Chris Lattner | e666fcf | 2005-04-13 02:58:13 +0000 | [diff] [blame] | 1103 | if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1104 | cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1) |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1105 | return getNode(Opcode, VT, N1, N2.getOperand(0)); |
Chris Lattner | e666fcf | 2005-04-13 02:58:13 +0000 | [diff] [blame] | 1106 | else if (N2.getOpcode() == ISD::AND) |
| 1107 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) { |
| 1108 | // If the and is only masking out bits that cannot effect the shift, |
| 1109 | // eliminate the and. |
| 1110 | unsigned NumBits = MVT::getSizeInBits(VT); |
| 1111 | if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) |
| 1112 | return getNode(Opcode, VT, N1, N2.getOperand(0)); |
| 1113 | } |
Nate Begeman | db81eba | 2005-04-12 23:32:28 +0000 | [diff] [blame] | 1114 | break; |
Nate Begeman | eea805e | 2005-04-13 21:23:31 +0000 | [diff] [blame] | 1115 | #endif |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1118 | // Memoize this node if possible. |
| 1119 | SDNode *N; |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1120 | if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END && |
| 1121 | VT != MVT::Flag) { |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1122 | SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; |
| 1123 | if (BON) return SDOperand(BON, 0); |
| 1124 | |
| 1125 | BON = N = new SDNode(Opcode, N1, N2); |
| 1126 | } else { |
Chris Lattner | 88de6e7 | 2005-05-12 00:17:04 +0000 | [diff] [blame] | 1127 | N = new SDNode(Opcode, N1, N2); |
Chris Lattner | 27e9b41 | 2005-05-11 18:57:39 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | 3e01136 | 2005-05-14 07:45:46 +0000 | [diff] [blame] | 1130 | N->setValueTypes(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1131 | AllNodes.push_back(N); |
| 1132 | return SDOperand(N, 0); |
| 1133 | } |
| 1134 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1135 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1136 | SDOperand N1, SDOperand N2, SDOperand N3) { |
| 1137 | // Perform various simplifications. |
| 1138 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 1139 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 1140 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 1141 | switch (Opcode) { |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1142 | case ISD::SETCC: { |
| 1143 | // Use SimplifySetCC to simplify SETCC's. |
Chris Lattner | bd8625b | 2005-08-09 23:09:05 +0000 | [diff] [blame] | 1144 | SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get()); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1145 | if (Simp.Val) return Simp; |
| 1146 | break; |
| 1147 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1148 | case ISD::SELECT: |
| 1149 | if (N1C) |
| 1150 | if (N1C->getValue()) |
| 1151 | return N2; // select true, X, Y -> X |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1152 | else |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1153 | return N3; // select false, X, Y -> Y |
| 1154 | |
| 1155 | if (N2 == N3) return N2; // select C, X, X -> X |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1156 | break; |
Chris Lattner | 5351e9b | 2005-01-07 22:49:57 +0000 | [diff] [blame] | 1157 | case ISD::BRCOND: |
| 1158 | if (N2C) |
| 1159 | if (N2C->getValue()) // Unconditional branch |
| 1160 | return getNode(ISD::BR, MVT::Other, N1, N3); |
| 1161 | else |
| 1162 | return N1; // Never-taken branch |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 1163 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 1166 | std::vector<SDOperand> Ops; |
| 1167 | Ops.reserve(3); |
| 1168 | Ops.push_back(N1); |
| 1169 | Ops.push_back(N2); |
| 1170 | Ops.push_back(N3); |
| 1171 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1172 | // Memoize node if it doesn't produce a flag. |
| 1173 | SDNode *N; |
| 1174 | if (VT != MVT::Flag) { |
| 1175 | SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))]; |
| 1176 | if (E) return SDOperand(E, 0); |
| 1177 | E = N = new SDNode(Opcode, N1, N2, N3); |
| 1178 | } else { |
| 1179 | N = new SDNode(Opcode, N1, N2, N3); |
| 1180 | } |
Chris Lattner | adf6c2a | 2005-05-14 07:29:57 +0000 | [diff] [blame] | 1181 | N->setValueTypes(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1182 | AllNodes.push_back(N); |
| 1183 | return SDOperand(N, 0); |
| 1184 | } |
| 1185 | |
| 1186 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1187 | SDOperand N1, SDOperand N2, SDOperand N3, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1188 | SDOperand N4) { |
Chris Lattner | b7f7d51 | 2005-05-14 07:32:14 +0000 | [diff] [blame] | 1189 | std::vector<SDOperand> Ops; |
| 1190 | Ops.reserve(4); |
| 1191 | Ops.push_back(N1); |
| 1192 | Ops.push_back(N2); |
| 1193 | Ops.push_back(N3); |
| 1194 | Ops.push_back(N4); |
| 1195 | return getNode(Opcode, VT, Ops); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1198 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 1199 | SDOperand N1, SDOperand N2, SDOperand N3, |
| 1200 | SDOperand N4, SDOperand N5) { |
| 1201 | std::vector<SDOperand> Ops; |
| 1202 | Ops.reserve(5); |
| 1203 | Ops.push_back(N1); |
| 1204 | Ops.push_back(N2); |
| 1205 | Ops.push_back(N3); |
| 1206 | Ops.push_back(N4); |
| 1207 | Ops.push_back(N5); |
| 1208 | return getNode(Opcode, VT, Ops); |
| 1209 | } |
| 1210 | |
Evan Cheng | 7038daf | 2005-12-10 00:37:58 +0000 | [diff] [blame] | 1211 | // setAdjCallChain - This method changes the token chain of an |
| 1212 | // CALLSEQ_START/END node to be the specified operand. |
| 1213 | void SDNode::setAdjCallChain(SDOperand N) { |
| 1214 | assert(N.getValueType() == MVT::Other); |
| 1215 | assert((getOpcode() == ISD::CALLSEQ_START || |
| 1216 | getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!"); |
| 1217 | |
| 1218 | OperandList[0].Val->removeUser(this); |
| 1219 | OperandList[0] = N; |
| 1220 | OperandList[0].Val->Uses.push_back(this); |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | SDOperand SelectionDAG::getLoad(MVT::ValueType VT, |
| 1226 | SDOperand Chain, SDOperand Ptr, |
| 1227 | SDOperand SV) { |
| 1228 | SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; |
| 1229 | if (N) return SDOperand(N, 0); |
| 1230 | N = new SDNode(ISD::LOAD, Chain, Ptr, SV); |
| 1231 | |
| 1232 | // Loads have a token chain. |
| 1233 | setNodeValueTypes(N, VT, MVT::Other); |
| 1234 | AllNodes.push_back(N); |
| 1235 | return SDOperand(N, 0); |
| 1236 | } |
| 1237 | |
| 1238 | SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT, |
| 1239 | SDOperand Chain, SDOperand Ptr, |
| 1240 | SDOperand SV) { |
| 1241 | SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))]; |
| 1242 | if (N) return SDOperand(N, 0); |
| 1243 | std::vector<SDOperand> Ops; |
| 1244 | Ops.reserve(5); |
| 1245 | Ops.push_back(Chain); |
| 1246 | Ops.push_back(Ptr); |
| 1247 | Ops.push_back(getConstant(Count, MVT::i32)); |
| 1248 | Ops.push_back(getValueType(EVT)); |
| 1249 | Ops.push_back(SV); |
| 1250 | std::vector<MVT::ValueType> VTs; |
| 1251 | VTs.reserve(2); |
| 1252 | VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain. |
| 1253 | return getNode(ISD::VLOAD, VTs, Ops); |
| 1254 | } |
| 1255 | |
| 1256 | SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT, |
| 1257 | SDOperand Chain, SDOperand Ptr, SDOperand SV, |
| 1258 | MVT::ValueType EVT) { |
| 1259 | std::vector<SDOperand> Ops; |
| 1260 | Ops.reserve(4); |
| 1261 | Ops.push_back(Chain); |
| 1262 | Ops.push_back(Ptr); |
| 1263 | Ops.push_back(SV); |
| 1264 | Ops.push_back(getValueType(EVT)); |
| 1265 | std::vector<MVT::ValueType> VTs; |
| 1266 | VTs.reserve(2); |
| 1267 | VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain. |
| 1268 | return getNode(Opcode, VTs, Ops); |
| 1269 | } |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 0437cdd | 2005-05-09 04:14:13 +0000 | [diff] [blame] | 1271 | SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) { |
Andrew Lenharth | 06ef884 | 2005-06-29 18:54:02 +0000 | [diff] [blame] | 1272 | assert((!V || isa<PointerType>(V->getType())) && |
| 1273 | "SrcValue is not a pointer?"); |
Chris Lattner | 0437cdd | 2005-05-09 04:14:13 +0000 | [diff] [blame] | 1274 | SDNode *&N = ValueNodes[std::make_pair(V, Offset)]; |
| 1275 | if (N) return SDOperand(N, 0); |
| 1276 | |
| 1277 | N = new SrcValueSDNode(V, Offset); |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1278 | AllNodes.push_back(N); |
| 1279 | return SDOperand(N, 0); |
| 1280 | } |
| 1281 | |
| 1282 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1283 | std::vector<SDOperand> &Ops) { |
| 1284 | switch (Ops.size()) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1285 | case 0: return getNode(Opcode, VT); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1286 | case 1: return getNode(Opcode, VT, Ops[0]); |
| 1287 | case 2: return getNode(Opcode, VT, Ops[0], Ops[1]); |
| 1288 | case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1289 | default: break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1290 | } |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1292 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1293 | switch (Opcode) { |
| 1294 | default: break; |
| 1295 | case ISD::BRCONDTWOWAY: |
| 1296 | if (N1C) |
| 1297 | if (N1C->getValue()) // Unconditional branch to true dest. |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1298 | return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1299 | else // Unconditional branch to false dest. |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1300 | return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1301 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1302 | case ISD::BRTWOWAY_CC: |
| 1303 | assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!"); |
| 1304 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 1305 | "LHS and RHS of comparison must have same type!"); |
| 1306 | break; |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1307 | case ISD::TRUNCSTORE: { |
| 1308 | assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!"); |
| 1309 | MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT(); |
| 1310 | #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store |
| 1311 | // If this is a truncating store of a constant, convert to the desired type |
| 1312 | // and store it instead. |
| 1313 | if (isa<Constant>(Ops[0])) { |
| 1314 | SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); |
| 1315 | if (isa<Constant>(Op)) |
| 1316 | N1 = Op; |
| 1317 | } |
| 1318 | // Also for ConstantFP? |
| 1319 | #endif |
| 1320 | if (Ops[0].getValueType() == EVT) // Normal store? |
| 1321 | return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]); |
| 1322 | assert(Ops[1].getValueType() > EVT && "Not a truncation?"); |
| 1323 | assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) && |
| 1324 | "Can't do FP-INT conversion!"); |
| 1325 | break; |
| 1326 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1327 | case ISD::SELECT_CC: { |
Chris Lattner | ad13715 | 2005-10-05 06:37:22 +0000 | [diff] [blame] | 1328 | assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1329 | assert(Ops[0].getValueType() == Ops[1].getValueType() && |
| 1330 | "LHS and RHS of condition must have same type!"); |
| 1331 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 1332 | "True and False arms of SelectCC must have same type!"); |
| 1333 | assert(Ops[2].getValueType() == VT && |
| 1334 | "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] | 1335 | break; |
| 1336 | } |
| 1337 | case ISD::BR_CC: { |
Chris Lattner | ad13715 | 2005-10-05 06:37:22 +0000 | [diff] [blame] | 1338 | assert(Ops.size() == 5 && "BR_CC takes 5 operands!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1339 | assert(Ops[2].getValueType() == Ops[3].getValueType() && |
| 1340 | "LHS/RHS of comparison should match types!"); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | } |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Chris Lattner | 385328c | 2005-05-14 07:42:29 +0000 | [diff] [blame] | 1345 | // Memoize nodes. |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1346 | SDNode *N; |
| 1347 | if (VT != MVT::Flag) { |
| 1348 | SDNode *&E = |
| 1349 | OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))]; |
| 1350 | if (E) return SDOperand(E, 0); |
| 1351 | E = N = new SDNode(Opcode, Ops); |
| 1352 | } else { |
| 1353 | N = new SDNode(Opcode, Ops); |
| 1354 | } |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1355 | N->setValueTypes(VT); |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1356 | AllNodes.push_back(N); |
| 1357 | return SDOperand(N, 0); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1360 | SDOperand SelectionDAG::getNode(unsigned Opcode, |
| 1361 | std::vector<MVT::ValueType> &ResultTys, |
| 1362 | std::vector<SDOperand> &Ops) { |
| 1363 | if (ResultTys.size() == 1) |
| 1364 | return getNode(Opcode, ResultTys[0], Ops); |
| 1365 | |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1366 | switch (Opcode) { |
| 1367 | case ISD::EXTLOAD: |
| 1368 | case ISD::SEXTLOAD: |
| 1369 | case ISD::ZEXTLOAD: { |
| 1370 | MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT(); |
| 1371 | assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!"); |
| 1372 | // If they are asking for an extending load from/to the same thing, return a |
| 1373 | // normal load. |
| 1374 | if (ResultTys[0] == EVT) |
| 1375 | return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]); |
| 1376 | assert(EVT < ResultTys[0] && |
| 1377 | "Should only be an extending load, not truncating!"); |
| 1378 | assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) && |
| 1379 | "Cannot sign/zero extend a FP load!"); |
| 1380 | assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) && |
| 1381 | "Cannot convert from FP to Int or Int -> FP!"); |
| 1382 | break; |
| 1383 | } |
| 1384 | |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1385 | // FIXME: figure out how to safely handle things like |
| 1386 | // int foo(int x) { return 1 << (x & 255); } |
| 1387 | // int bar() { return foo(256); } |
| 1388 | #if 0 |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1389 | case ISD::SRA_PARTS: |
| 1390 | case ISD::SRL_PARTS: |
| 1391 | case ISD::SHL_PARTS: |
| 1392 | if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG && |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1393 | cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1) |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1394 | return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); |
| 1395 | else if (N3.getOpcode() == ISD::AND) |
| 1396 | if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) { |
| 1397 | // If the and is only masking out bits that cannot effect the shift, |
| 1398 | // eliminate the and. |
| 1399 | unsigned NumBits = MVT::getSizeInBits(VT)*2; |
| 1400 | if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1) |
| 1401 | return getNode(Opcode, VT, N1, N2, N3.getOperand(0)); |
| 1402 | } |
| 1403 | break; |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 1404 | #endif |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1405 | } |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | 43247a1 | 2005-08-25 19:12:10 +0000 | [diff] [blame] | 1407 | // Memoize the node unless it returns a flag. |
| 1408 | SDNode *N; |
| 1409 | if (ResultTys.back() != MVT::Flag) { |
| 1410 | SDNode *&E = |
| 1411 | ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))]; |
| 1412 | if (E) return SDOperand(E, 0); |
| 1413 | E = N = new SDNode(Opcode, Ops); |
| 1414 | } else { |
| 1415 | N = new SDNode(Opcode, Ops); |
| 1416 | } |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 1417 | setNodeValueTypes(N, ResultTys); |
Chris Lattner | 5fa4fa4 | 2005-05-14 06:42:57 +0000 | [diff] [blame] | 1418 | AllNodes.push_back(N); |
Chris Lattner | 89c3463 | 2005-05-14 06:20:26 +0000 | [diff] [blame] | 1419 | return SDOperand(N, 0); |
| 1420 | } |
| 1421 | |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 1422 | void SelectionDAG::setNodeValueTypes(SDNode *N, |
| 1423 | std::vector<MVT::ValueType> &RetVals) { |
| 1424 | switch (RetVals.size()) { |
| 1425 | case 0: return; |
| 1426 | case 1: N->setValueTypes(RetVals[0]); return; |
| 1427 | case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return; |
| 1428 | default: break; |
| 1429 | } |
| 1430 | |
| 1431 | std::list<std::vector<MVT::ValueType> >::iterator I = |
| 1432 | std::find(VTList.begin(), VTList.end(), RetVals); |
| 1433 | if (I == VTList.end()) { |
| 1434 | VTList.push_front(RetVals); |
| 1435 | I = VTList.begin(); |
| 1436 | } |
| 1437 | |
| 1438 | N->setValueTypes(&(*I)[0], I->size()); |
| 1439 | } |
| 1440 | |
| 1441 | void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1, |
| 1442 | MVT::ValueType VT2) { |
| 1443 | for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(), |
| 1444 | E = VTList.end(); I != E; ++I) { |
| 1445 | if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) { |
| 1446 | N->setValueTypes(&(*I)[0], 2); |
| 1447 | return; |
| 1448 | } |
| 1449 | } |
| 1450 | std::vector<MVT::ValueType> V; |
| 1451 | V.push_back(VT1); |
| 1452 | V.push_back(VT2); |
| 1453 | VTList.push_front(V); |
| 1454 | N->setValueTypes(&(*VTList.begin())[0], 2); |
| 1455 | } |
| 1456 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1457 | |
| 1458 | /// SelectNodeTo - These are used for target selectors to *mutate* the |
| 1459 | /// specified node to have the specified return type, Target opcode, and |
| 1460 | /// operands. Note that target opcodes are stored as |
| 1461 | /// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field. |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1462 | /// |
| 1463 | /// Note that SelectNodeTo returns the resultant node. If there is already a |
| 1464 | /// node of the specified opcode and operands, it returns that node instead of |
| 1465 | /// the current one. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1466 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1467 | MVT::ValueType VT) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1468 | // If an identical node already exists, use it. |
| 1469 | SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)]; |
| 1470 | if (ON) return SDOperand(ON, 0); |
| 1471 | |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 1472 | RemoveNodeFromCSEMaps(N); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 1474 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1475 | N->setValueTypes(VT); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1476 | |
| 1477 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1478 | return SDOperand(N, 0); |
Chris Lattner | 7651fa4 | 2005-08-24 23:00:29 +0000 | [diff] [blame] | 1479 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1481 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1482 | MVT::ValueType VT, SDOperand Op1) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1483 | // If an identical node already exists, use it. |
| 1484 | SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1485 | std::make_pair(Op1, VT))]; |
| 1486 | if (ON) return SDOperand(ON, 0); |
| 1487 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1488 | RemoveNodeFromCSEMaps(N); |
| 1489 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1490 | N->setValueTypes(VT); |
| 1491 | N->setOperands(Op1); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1492 | |
| 1493 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1494 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1495 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1496 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1497 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1498 | MVT::ValueType VT, SDOperand Op1, |
| 1499 | SDOperand Op2) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1500 | // If an identical node already exists, use it. |
| 1501 | SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1502 | std::make_pair(Op1, Op2))]; |
| 1503 | if (ON) return SDOperand(ON, 0); |
| 1504 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1505 | RemoveNodeFromCSEMaps(N); |
| 1506 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1507 | N->setValueTypes(VT); |
| 1508 | N->setOperands(Op1, Op2); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1509 | |
| 1510 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1511 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1512 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1514 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1515 | MVT::ValueType VT, SDOperand Op1, |
| 1516 | SDOperand Op2, SDOperand Op3) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1517 | // If an identical node already exists, use it. |
| 1518 | std::vector<SDOperand> OpList; |
| 1519 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1520 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1521 | std::make_pair(VT, OpList))]; |
| 1522 | if (ON) return SDOperand(ON, 0); |
| 1523 | |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1524 | RemoveNodeFromCSEMaps(N); |
| 1525 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1526 | N->setValueTypes(VT); |
| 1527 | N->setOperands(Op1, Op2, Op3); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1528 | |
| 1529 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1530 | return SDOperand(N, 0); |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1531 | } |
Chris Lattner | c975e1d | 2005-08-21 22:30:30 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1533 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1534 | MVT::ValueType VT, SDOperand Op1, |
| 1535 | SDOperand Op2, SDOperand Op3, |
| 1536 | SDOperand Op4) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1537 | // If an identical node already exists, use it. |
| 1538 | std::vector<SDOperand> OpList; |
| 1539 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1540 | OpList.push_back(Op4); |
| 1541 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1542 | std::make_pair(VT, OpList))]; |
| 1543 | if (ON) return SDOperand(ON, 0); |
| 1544 | |
Nate Begeman | 294a0a1 | 2005-08-18 07:30:15 +0000 | [diff] [blame] | 1545 | RemoveNodeFromCSEMaps(N); |
| 1546 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1547 | N->setValueTypes(VT); |
| 1548 | N->setOperands(Op1, Op2, Op3, Op4); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1549 | |
| 1550 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1551 | return SDOperand(N, 0); |
Nate Begeman | 294a0a1 | 2005-08-18 07:30:15 +0000 | [diff] [blame] | 1552 | } |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1554 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1555 | MVT::ValueType VT, SDOperand Op1, |
| 1556 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 1557 | SDOperand Op5) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1558 | // If an identical node already exists, use it. |
| 1559 | std::vector<SDOperand> OpList; |
| 1560 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1561 | OpList.push_back(Op4); OpList.push_back(Op5); |
| 1562 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1563 | std::make_pair(VT, OpList))]; |
| 1564 | if (ON) return SDOperand(ON, 0); |
| 1565 | |
Chris Lattner | 6b09a29 | 2005-08-21 18:49:33 +0000 | [diff] [blame] | 1566 | RemoveNodeFromCSEMaps(N); |
| 1567 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1568 | N->setValueTypes(VT); |
| 1569 | N->setOperands(Op1, Op2, Op3, Op4, Op5); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1570 | |
| 1571 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1572 | return SDOperand(N, 0); |
Chris Lattner | 6b09a29 | 2005-08-21 18:49:33 +0000 | [diff] [blame] | 1573 | } |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1575 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1576 | MVT::ValueType VT, SDOperand Op1, |
| 1577 | SDOperand Op2, SDOperand Op3,SDOperand Op4, |
| 1578 | SDOperand Op5, SDOperand Op6) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1579 | // If an identical node already exists, use it. |
| 1580 | std::vector<SDOperand> OpList; |
| 1581 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1582 | OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6); |
| 1583 | SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1584 | std::make_pair(VT, OpList))]; |
| 1585 | if (ON) return SDOperand(ON, 0); |
| 1586 | |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 1587 | RemoveNodeFromCSEMaps(N); |
| 1588 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1589 | N->setValueTypes(VT); |
| 1590 | N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1591 | |
| 1592 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1593 | return SDOperand(N, 0); |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1596 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1597 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 1598 | SDOperand Op1, SDOperand Op2) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1599 | // If an identical node already exists, use it. |
| 1600 | std::vector<SDOperand> OpList; |
| 1601 | OpList.push_back(Op1); OpList.push_back(Op2); |
| 1602 | std::vector<MVT::ValueType> VTList; |
| 1603 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 1604 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1605 | std::make_pair(VTList, OpList))]; |
| 1606 | if (ON) return SDOperand(ON, 0); |
| 1607 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1608 | RemoveNodeFromCSEMaps(N); |
| 1609 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1610 | setNodeValueTypes(N, VT1, VT2); |
| 1611 | N->setOperands(Op1, Op2); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1612 | |
| 1613 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1614 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1617 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1618 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 1619 | SDOperand Op1, SDOperand Op2, |
| 1620 | SDOperand Op3) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1621 | // If an identical node already exists, use it. |
| 1622 | std::vector<SDOperand> OpList; |
| 1623 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1624 | std::vector<MVT::ValueType> VTList; |
| 1625 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 1626 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1627 | std::make_pair(VTList, OpList))]; |
| 1628 | if (ON) return SDOperand(ON, 0); |
| 1629 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1630 | RemoveNodeFromCSEMaps(N); |
| 1631 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1632 | setNodeValueTypes(N, VT1, VT2); |
| 1633 | N->setOperands(Op1, Op2, Op3); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1634 | |
| 1635 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1636 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1639 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1640 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 1641 | SDOperand Op1, SDOperand Op2, |
| 1642 | SDOperand Op3, SDOperand Op4) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1643 | // If an identical node already exists, use it. |
| 1644 | std::vector<SDOperand> OpList; |
| 1645 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1646 | OpList.push_back(Op4); |
| 1647 | std::vector<MVT::ValueType> VTList; |
| 1648 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 1649 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1650 | std::make_pair(VTList, OpList))]; |
| 1651 | if (ON) return SDOperand(ON, 0); |
| 1652 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1653 | RemoveNodeFromCSEMaps(N); |
| 1654 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1655 | setNodeValueTypes(N, VT1, VT2); |
| 1656 | N->setOperands(Op1, Op2, Op3, Op4); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1657 | |
| 1658 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1659 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1662 | SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc, |
| 1663 | MVT::ValueType VT1, MVT::ValueType VT2, |
| 1664 | SDOperand Op1, SDOperand Op2, |
| 1665 | SDOperand Op3, SDOperand Op4, |
| 1666 | SDOperand Op5) { |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1667 | // If an identical node already exists, use it. |
| 1668 | std::vector<SDOperand> OpList; |
| 1669 | OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3); |
| 1670 | OpList.push_back(Op4); OpList.push_back(Op5); |
| 1671 | std::vector<MVT::ValueType> VTList; |
| 1672 | VTList.push_back(VT1); VTList.push_back(VT2); |
| 1673 | SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, |
| 1674 | std::make_pair(VTList, OpList))]; |
| 1675 | if (ON) return SDOperand(ON, 0); |
| 1676 | |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1677 | RemoveNodeFromCSEMaps(N); |
| 1678 | N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc); |
| 1679 | setNodeValueTypes(N, VT1, VT2); |
| 1680 | N->setOperands(Op1, Op2, Op3, Op4, Op5); |
Chris Lattner | c5e6c64 | 2005-12-01 18:00:57 +0000 | [diff] [blame] | 1681 | |
| 1682 | ON = N; // Memoize the new node. |
Chris Lattner | eb19e40 | 2005-11-30 22:45:14 +0000 | [diff] [blame] | 1683 | return SDOperand(N, 0); |
Chris Lattner | 0fb094f | 2005-11-19 01:44:53 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | // ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1687 | /// This can cause recursive merging of nodes in the DAG. |
| 1688 | /// |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1689 | /// This version assumes From/To have a single result value. |
| 1690 | /// |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1691 | void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN, |
| 1692 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1693 | SDNode *From = FromN.Val, *To = ToN.Val; |
| 1694 | assert(From->getNumValues() == 1 && To->getNumValues() == 1 && |
| 1695 | "Cannot replace with this method!"); |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1696 | assert(From != To && "Cannot replace uses of with self"); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1698 | while (!From->use_empty()) { |
| 1699 | // Process users until they are all gone. |
| 1700 | SDNode *U = *From->use_begin(); |
| 1701 | |
| 1702 | // This node is about to morph, remove its old self from the CSE maps. |
| 1703 | RemoveNodeFromCSEMaps(U); |
| 1704 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1705 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 1706 | I != E; ++I) |
| 1707 | if (I->Val == From) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1708 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1709 | I->Val = To; |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1710 | To->addUser(U); |
| 1711 | } |
| 1712 | |
| 1713 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 1714 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1715 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 1716 | ReplaceAllUsesWith(U, Existing, Deleted); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1717 | // U is now dead. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1718 | if (Deleted) Deleted->push_back(U); |
| 1719 | DeleteNodeNotInCSEMaps(U); |
| 1720 | } |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 1725 | /// This can cause recursive merging of nodes in the DAG. |
| 1726 | /// |
| 1727 | /// This version assumes From/To have matching types and numbers of result |
| 1728 | /// values. |
| 1729 | /// |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1730 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To, |
| 1731 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1732 | assert(From != To && "Cannot replace uses of with self"); |
| 1733 | assert(From->getNumValues() == To->getNumValues() && |
| 1734 | "Cannot use this version of ReplaceAllUsesWith!"); |
| 1735 | if (From->getNumValues() == 1) { // If possible, use the faster version. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1736 | ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted); |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | while (!From->use_empty()) { |
| 1741 | // Process users until they are all gone. |
| 1742 | SDNode *U = *From->use_begin(); |
| 1743 | |
| 1744 | // This node is about to morph, remove its old self from the CSE maps. |
| 1745 | RemoveNodeFromCSEMaps(U); |
| 1746 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1747 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 1748 | I != E; ++I) |
| 1749 | if (I->Val == From) { |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1750 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1751 | I->Val = To; |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1752 | To->addUser(U); |
| 1753 | } |
| 1754 | |
| 1755 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 1756 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1757 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 1758 | ReplaceAllUsesWith(U, Existing, Deleted); |
| 1759 | // U is now dead. |
| 1760 | if (Deleted) Deleted->push_back(U); |
| 1761 | DeleteNodeNotInCSEMaps(U); |
| 1762 | } |
Chris Lattner | 8b8749f | 2005-08-17 19:00:20 +0000 | [diff] [blame] | 1763 | } |
| 1764 | } |
| 1765 | |
Chris Lattner | 8b52f21 | 2005-08-26 18:36:28 +0000 | [diff] [blame] | 1766 | /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead. |
| 1767 | /// This can cause recursive merging of nodes in the DAG. |
| 1768 | /// |
| 1769 | /// This version can replace From with any result values. To must match the |
| 1770 | /// number and types of values returned by From. |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1771 | void SelectionDAG::ReplaceAllUsesWith(SDNode *From, |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1772 | const std::vector<SDOperand> &To, |
| 1773 | std::vector<SDNode*> *Deleted) { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1774 | assert(From->getNumValues() == To.size() && |
| 1775 | "Incorrect number of values to replace with!"); |
Chris Lattner | ff01698 | 2005-08-28 23:59:36 +0000 | [diff] [blame] | 1776 | if (To.size() == 1 && To[0].Val->getNumValues() == 1) { |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1777 | // Degenerate case handled above. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1778 | ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted); |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | while (!From->use_empty()) { |
| 1783 | // Process users until they are all gone. |
| 1784 | SDNode *U = *From->use_begin(); |
| 1785 | |
| 1786 | // This node is about to morph, remove its old self from the CSE maps. |
| 1787 | RemoveNodeFromCSEMaps(U); |
| 1788 | |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1789 | for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands; |
| 1790 | I != E; ++I) |
| 1791 | if (I->Val == From) { |
| 1792 | const SDOperand &ToOp = To[I->ResNo]; |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1793 | From->removeUser(U); |
Chris Lattner | 65113b2 | 2005-11-08 22:07:03 +0000 | [diff] [blame] | 1794 | *I = ToOp; |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1795 | ToOp.Val->addUser(U); |
| 1796 | } |
| 1797 | |
| 1798 | // Now that we have modified U, add it back to the CSE maps. If it already |
| 1799 | // exists there, recursively merge the results together. |
Chris Lattner | 1e111c7 | 2005-09-07 05:37:01 +0000 | [diff] [blame] | 1800 | if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) { |
| 1801 | ReplaceAllUsesWith(U, Existing, Deleted); |
| 1802 | // U is now dead. |
| 1803 | if (Deleted) Deleted->push_back(U); |
| 1804 | DeleteNodeNotInCSEMaps(U); |
| 1805 | } |
Chris Lattner | 7b2880c | 2005-08-24 22:44:39 +0000 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | |
Jim Laskey | 58b968b | 2005-08-17 20:08:02 +0000 | [diff] [blame] | 1810 | //===----------------------------------------------------------------------===// |
| 1811 | // SDNode Class |
| 1812 | //===----------------------------------------------------------------------===// |
Chris Lattner | 149c58c | 2005-08-16 18:17:10 +0000 | [diff] [blame] | 1813 | |
Chris Lattner | a325511 | 2005-11-08 23:30:28 +0000 | [diff] [blame] | 1814 | |
| 1815 | /// getValueTypeList - Return a pointer to the specified value type. |
| 1816 | /// |
| 1817 | MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) { |
| 1818 | static MVT::ValueType VTs[MVT::LAST_VALUETYPE]; |
| 1819 | VTs[VT] = VT; |
| 1820 | return &VTs[VT]; |
| 1821 | } |
| 1822 | |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 1823 | /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the |
| 1824 | /// indicated value. This method ignores uses of other values defined by this |
| 1825 | /// operation. |
| 1826 | bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) { |
| 1827 | assert(Value < getNumValues() && "Bad value!"); |
| 1828 | |
| 1829 | // If there is only one value, this is easy. |
| 1830 | if (getNumValues() == 1) |
| 1831 | return use_size() == NUses; |
| 1832 | if (Uses.size() < NUses) return false; |
| 1833 | |
| 1834 | SDOperand TheValue(this, Value); |
| 1835 | |
| 1836 | std::set<SDNode*> UsersHandled; |
| 1837 | |
| 1838 | for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end(); |
| 1839 | UI != E; ++UI) { |
| 1840 | SDNode *User = *UI; |
| 1841 | if (User->getNumOperands() == 1 || |
| 1842 | UsersHandled.insert(User).second) // First time we've seen this? |
| 1843 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 1844 | if (User->getOperand(i) == TheValue) { |
| 1845 | if (NUses == 0) |
| 1846 | return false; // too many uses |
| 1847 | --NUses; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | // Found exactly the right number of uses? |
| 1852 | return NUses == 0; |
| 1853 | } |
| 1854 | |
| 1855 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 1856 | const char *SDNode::getOperationName(const SelectionDAG *G) const { |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1857 | switch (getOpcode()) { |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 1858 | default: |
| 1859 | if (getOpcode() < ISD::BUILTIN_OP_END) |
| 1860 | return "<<Unknown DAG Node>>"; |
| 1861 | else { |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1862 | if (G) { |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 1863 | if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo()) |
Chris Lattner | 08addbd | 2005-09-09 22:35:03 +0000 | [diff] [blame] | 1864 | if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes()) |
| 1865 | return TII->getName(getOpcode()-ISD::BUILTIN_OP_END); |
Evan Cheng | 115c036 | 2005-12-19 23:11:49 +0000 | [diff] [blame] | 1866 | |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1867 | TargetLowering &TLI = G->getTargetLoweringInfo(); |
| 1868 | const char *Name = |
| 1869 | TLI.getTargetNodeName(getOpcode()); |
| 1870 | if (Name) return Name; |
| 1871 | } |
| 1872 | |
| 1873 | return "<<Unknown Target Node>>"; |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1876 | case ISD::PCMARKER: return "PCMarker"; |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1877 | case ISD::READCYCLECOUNTER: return "ReadCycleCounter"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 1878 | case ISD::SRCVALUE: return "SrcValue"; |
Chris Lattner | a23e815 | 2005-08-18 03:31:02 +0000 | [diff] [blame] | 1879 | case ISD::VALUETYPE: return "ValueType"; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1880 | case ISD::STRING: return "String"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1881 | case ISD::EntryToken: return "EntryToken"; |
Chris Lattner | 282c5ca | 2005-01-13 17:59:10 +0000 | [diff] [blame] | 1882 | case ISD::TokenFactor: return "TokenFactor"; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 1883 | case ISD::AssertSext: return "AssertSext"; |
| 1884 | case ISD::AssertZext: return "AssertZext"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1885 | case ISD::Constant: return "Constant"; |
Chris Lattner | 37bfbb4 | 2005-08-17 00:34:06 +0000 | [diff] [blame] | 1886 | case ISD::TargetConstant: return "TargetConstant"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1887 | case ISD::ConstantFP: return "ConstantFP"; |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 1888 | case ISD::ConstantVec: return "ConstantVec"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1889 | case ISD::GlobalAddress: return "GlobalAddress"; |
Chris Lattner | aaaa0b6 | 2005-08-19 22:31:04 +0000 | [diff] [blame] | 1890 | case ISD::TargetGlobalAddress: return "TargetGlobalAddress"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1891 | case ISD::FrameIndex: return "FrameIndex"; |
Chris Lattner | afb2dd4 | 2005-08-25 00:43:01 +0000 | [diff] [blame] | 1892 | case ISD::TargetFrameIndex: return "TargetFrameIndex"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1893 | case ISD::BasicBlock: return "BasicBlock"; |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1894 | case ISD::Register: return "Register"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1895 | case ISD::ExternalSymbol: return "ExternalSymbol"; |
Andrew Lenharth | 2a2de66 | 2005-10-23 03:40:17 +0000 | [diff] [blame] | 1896 | case ISD::TargetExternalSymbol: return "TargetExternalSymbol"; |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 1897 | case ISD::ConstantPool: return "ConstantPool"; |
| 1898 | case ISD::TargetConstantPool: return "TargetConstantPool"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1899 | case ISD::CopyToReg: return "CopyToReg"; |
| 1900 | case ISD::CopyFromReg: return "CopyFromReg"; |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1901 | case ISD::UNDEF: return "undef"; |
Chris Lattner | cc0aad2 | 2006-01-15 08:39:35 +0000 | [diff] [blame^] | 1902 | case ISD::MERGE_VALUES: return "mergevalues"; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | ff9fd0a | 2005-04-02 04:58:41 +0000 | [diff] [blame] | 1904 | // Unary operators |
| 1905 | case ISD::FABS: return "fabs"; |
| 1906 | case ISD::FNEG: return "fneg"; |
Chris Lattner | 7f64464 | 2005-04-28 21:44:03 +0000 | [diff] [blame] | 1907 | case ISD::FSQRT: return "fsqrt"; |
| 1908 | case ISD::FSIN: return "fsin"; |
| 1909 | case ISD::FCOS: return "fcos"; |
Chris Lattner | ff9fd0a | 2005-04-02 04:58:41 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Binary operators |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1912 | case ISD::ADD: return "add"; |
| 1913 | case ISD::SUB: return "sub"; |
| 1914 | case ISD::MUL: return "mul"; |
Nate Begeman | 1867054 | 2005-04-05 22:36:56 +0000 | [diff] [blame] | 1915 | case ISD::MULHU: return "mulhu"; |
| 1916 | case ISD::MULHS: return "mulhs"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1917 | case ISD::SDIV: return "sdiv"; |
| 1918 | case ISD::UDIV: return "udiv"; |
| 1919 | case ISD::SREM: return "srem"; |
| 1920 | case ISD::UREM: return "urem"; |
| 1921 | case ISD::AND: return "and"; |
| 1922 | case ISD::OR: return "or"; |
| 1923 | case ISD::XOR: return "xor"; |
| 1924 | case ISD::SHL: return "shl"; |
| 1925 | case ISD::SRA: return "sra"; |
| 1926 | case ISD::SRL: return "srl"; |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 1927 | case ISD::ROTL: return "rotl"; |
| 1928 | case ISD::ROTR: return "rotr"; |
| 1929 | case ISD::BSWAP: return "bswap"; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1930 | case ISD::FADD: return "fadd"; |
| 1931 | case ISD::FSUB: return "fsub"; |
| 1932 | case ISD::FMUL: return "fmul"; |
| 1933 | case ISD::FDIV: return "fdiv"; |
| 1934 | case ISD::FREM: return "frem"; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1935 | case ISD::VADD: return "vadd"; |
| 1936 | case ISD::VSUB: return "vsub"; |
| 1937 | case ISD::VMUL: return "vmul"; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 1939 | case ISD::SETCC: return "setcc"; |
| 1940 | case ISD::SELECT: return "select"; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1941 | case ISD::SELECT_CC: return "select_cc"; |
Chris Lattner | 17eee18 | 2005-01-20 18:50:55 +0000 | [diff] [blame] | 1942 | case ISD::ADD_PARTS: return "add_parts"; |
| 1943 | case ISD::SUB_PARTS: return "sub_parts"; |
Chris Lattner | 41be951 | 2005-04-02 03:30:42 +0000 | [diff] [blame] | 1944 | case ISD::SHL_PARTS: return "shl_parts"; |
| 1945 | case ISD::SRA_PARTS: return "sra_parts"; |
| 1946 | case ISD::SRL_PARTS: return "srl_parts"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1947 | |
Chris Lattner | 7f64464 | 2005-04-28 21:44:03 +0000 | [diff] [blame] | 1948 | // Conversion operators. |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1949 | case ISD::SIGN_EXTEND: return "sign_extend"; |
| 1950 | case ISD::ZERO_EXTEND: return "zero_extend"; |
Chris Lattner | 4ed11b4 | 2005-09-02 00:17:32 +0000 | [diff] [blame] | 1951 | case ISD::ANY_EXTEND: return "any_extend"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1952 | case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1953 | case ISD::TRUNCATE: return "truncate"; |
| 1954 | case ISD::FP_ROUND: return "fp_round"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1955 | case ISD::FP_ROUND_INREG: return "fp_round_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1956 | case ISD::FP_EXTEND: return "fp_extend"; |
| 1957 | |
| 1958 | case ISD::SINT_TO_FP: return "sint_to_fp"; |
| 1959 | case ISD::UINT_TO_FP: return "uint_to_fp"; |
| 1960 | case ISD::FP_TO_SINT: return "fp_to_sint"; |
| 1961 | case ISD::FP_TO_UINT: return "fp_to_uint"; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 1962 | case ISD::BIT_CONVERT: return "bit_convert"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1963 | |
| 1964 | // Control flow instructions |
| 1965 | case ISD::BR: return "br"; |
| 1966 | case ISD::BRCOND: return "brcond"; |
Chris Lattner | ef847df | 2005-04-09 03:27:28 +0000 | [diff] [blame] | 1967 | case ISD::BRCONDTWOWAY: return "brcondtwoway"; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1968 | case ISD::BR_CC: return "br_cc"; |
| 1969 | case ISD::BRTWOWAY_CC: return "brtwoway_cc"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1970 | case ISD::RET: return "ret"; |
| 1971 | case ISD::CALL: return "call"; |
Chris Lattner | d71c041 | 2005-05-13 18:43:43 +0000 | [diff] [blame] | 1972 | case ISD::TAILCALL:return "tailcall"; |
Chris Lattner | a364fa1 | 2005-05-12 23:51:40 +0000 | [diff] [blame] | 1973 | case ISD::CALLSEQ_START: return "callseq_start"; |
| 1974 | case ISD::CALLSEQ_END: return "callseq_end"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Other operators |
| 1977 | case ISD::LOAD: return "load"; |
| 1978 | case ISD::STORE: return "store"; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 1979 | case ISD::VLOAD: return "vload"; |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1980 | case ISD::EXTLOAD: return "extload"; |
| 1981 | case ISD::SEXTLOAD: return "sextload"; |
| 1982 | case ISD::ZEXTLOAD: return "zextload"; |
| 1983 | case ISD::TRUNCSTORE: return "truncstore"; |
| 1984 | |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1985 | case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; |
Chris Lattner | 5a67afc | 2006-01-13 02:39:42 +0000 | [diff] [blame] | 1986 | case ISD::EXTRACT_ELEMENT: return "extract_element"; |
| 1987 | case ISD::BUILD_PAIR: return "build_pair"; |
| 1988 | case ISD::STACKSAVE: return "stacksave"; |
| 1989 | case ISD::STACKRESTORE: return "stackrestore"; |
| 1990 | |
| 1991 | // Block memory operations. |
Chris Lattner | 4c633e8 | 2005-01-11 05:57:01 +0000 | [diff] [blame] | 1992 | case ISD::MEMSET: return "memset"; |
| 1993 | case ISD::MEMCPY: return "memcpy"; |
| 1994 | case ISD::MEMMOVE: return "memmove"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 276260b | 2005-05-11 04:50:30 +0000 | [diff] [blame] | 1996 | // Bit counting |
| 1997 | case ISD::CTPOP: return "ctpop"; |
| 1998 | case ISD::CTTZ: return "cttz"; |
| 1999 | case ISD::CTLZ: return "ctlz"; |
| 2000 | |
| 2001 | // IO Intrinsics |
Chris Lattner | 3c69101 | 2005-05-09 20:22:17 +0000 | [diff] [blame] | 2002 | case ISD::READPORT: return "readport"; |
| 2003 | case ISD::WRITEPORT: return "writeport"; |
| 2004 | case ISD::READIO: return "readio"; |
| 2005 | case ISD::WRITEIO: return "writeio"; |
| 2006 | |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2007 | // Debug info |
| 2008 | case ISD::LOCATION: return "location"; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 2009 | case ISD::DEBUG_LOC: return "debug_loc"; |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 2010 | case ISD::DEBUG_LABEL: return "debug_label"; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 2011 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2012 | case ISD::CONDCODE: |
| 2013 | switch (cast<CondCodeSDNode>(this)->get()) { |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2014 | default: assert(0 && "Unknown setcc condition!"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2015 | case ISD::SETOEQ: return "setoeq"; |
| 2016 | case ISD::SETOGT: return "setogt"; |
| 2017 | case ISD::SETOGE: return "setoge"; |
| 2018 | case ISD::SETOLT: return "setolt"; |
| 2019 | case ISD::SETOLE: return "setole"; |
| 2020 | case ISD::SETONE: return "setone"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2022 | case ISD::SETO: return "seto"; |
| 2023 | case ISD::SETUO: return "setuo"; |
| 2024 | case ISD::SETUEQ: return "setue"; |
| 2025 | case ISD::SETUGT: return "setugt"; |
| 2026 | case ISD::SETUGE: return "setuge"; |
| 2027 | case ISD::SETULT: return "setult"; |
| 2028 | case ISD::SETULE: return "setule"; |
| 2029 | case ISD::SETUNE: return "setune"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2031 | case ISD::SETEQ: return "seteq"; |
| 2032 | case ISD::SETGT: return "setgt"; |
| 2033 | case ISD::SETGE: return "setge"; |
| 2034 | case ISD::SETLT: return "setlt"; |
| 2035 | case ISD::SETLE: return "setle"; |
| 2036 | case ISD::SETNE: return "setne"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2040 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2041 | void SDNode::dump() const { dump(0); } |
| 2042 | void SDNode::dump(const SelectionDAG *G) const { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2043 | std::cerr << (void*)this << ": "; |
| 2044 | |
| 2045 | for (unsigned i = 0, e = getNumValues(); i != e; ++i) { |
| 2046 | if (i) std::cerr << ","; |
Chris Lattner | 4ea6924 | 2005-01-15 07:14:32 +0000 | [diff] [blame] | 2047 | if (getValueType(i) == MVT::Other) |
| 2048 | std::cerr << "ch"; |
| 2049 | else |
| 2050 | std::cerr << MVT::getValueTypeString(getValueType(i)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2051 | } |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2052 | std::cerr << " = " << getOperationName(G); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2053 | |
| 2054 | std::cerr << " "; |
| 2055 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 2056 | if (i) std::cerr << ", "; |
| 2057 | std::cerr << (void*)getOperand(i).Val; |
| 2058 | if (unsigned RN = getOperand(i).ResNo) |
| 2059 | std::cerr << ":" << RN; |
| 2060 | } |
| 2061 | |
| 2062 | if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { |
| 2063 | std::cerr << "<" << CSDN->getValue() << ">"; |
| 2064 | } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { |
| 2065 | std::cerr << "<" << CSDN->getValue() << ">"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2066 | } else if (const GlobalAddressSDNode *GADN = |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2067 | dyn_cast<GlobalAddressSDNode>(this)) { |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 2068 | int offset = GADN->getOffset(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2069 | std::cerr << "<"; |
| 2070 | WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; |
Evan Cheng | 61ca74b | 2005-11-30 02:04:11 +0000 | [diff] [blame] | 2071 | if (offset > 0) |
| 2072 | std::cerr << " + " << offset; |
| 2073 | else |
| 2074 | std::cerr << " " << offset; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 2075 | } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2076 | std::cerr << "<" << FIDN->getIndex() << ">"; |
| 2077 | } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 2078 | std::cerr << "<" << *CP->get() << ">"; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 2079 | } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2080 | std::cerr << "<"; |
| 2081 | const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); |
| 2082 | if (LBB) |
| 2083 | std::cerr << LBB->getName() << " "; |
| 2084 | std::cerr << (const void*)BBDN->getBasicBlock() << ">"; |
Chris Lattner | fa164b6 | 2005-08-19 21:34:13 +0000 | [diff] [blame] | 2085 | } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) { |
Evan Cheng | 140e99b | 2006-01-11 22:13:48 +0000 | [diff] [blame] | 2086 | if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) { |
Chris Lattner | 7228aa7 | 2005-08-19 21:21:16 +0000 | [diff] [blame] | 2087 | std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg()); |
| 2088 | } else { |
| 2089 | std::cerr << " #" << R->getReg(); |
| 2090 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2091 | } else if (const ExternalSymbolSDNode *ES = |
| 2092 | dyn_cast<ExternalSymbolSDNode>(this)) { |
| 2093 | std::cerr << "'" << ES->getSymbol() << "'"; |
Chris Lattner | 2bf3c26 | 2005-05-09 04:08:27 +0000 | [diff] [blame] | 2094 | } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) { |
| 2095 | if (M->getValue()) |
| 2096 | std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">"; |
| 2097 | else |
| 2098 | std::cerr << "<null:" << M->getOffset() << ">"; |
Chris Lattner | a23e815 | 2005-08-18 03:31:02 +0000 | [diff] [blame] | 2099 | } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) { |
| 2100 | std::cerr << ":" << getValueTypeString(N->getVT()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2101 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 2104 | static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) { |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2105 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 2106 | if (N->getOperand(i).Val->hasOneUse()) |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2107 | DumpNodes(N->getOperand(i).Val, indent+2, G); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2108 | else |
| 2109 | std::cerr << "\n" << std::string(indent+2, ' ') |
| 2110 | << (void*)N->getOperand(i).Val << ": <multiple use>"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2111 | |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2112 | |
| 2113 | std::cerr << "\n" << std::string(indent, ' '); |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2114 | N->dump(G); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2117 | void SelectionDAG::dump() const { |
| 2118 | std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 2119 | std::vector<const SDNode*> Nodes; |
| 2120 | for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end(); |
| 2121 | I != E; ++I) |
| 2122 | Nodes.push_back(I); |
| 2123 | |
Chris Lattner | 49d2471 | 2005-01-09 20:26:36 +0000 | [diff] [blame] | 2124 | std::sort(Nodes.begin(), Nodes.end()); |
| 2125 | |
| 2126 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2127 | if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2128 | DumpNodes(Nodes[i], 2, this); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2129 | } |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | f3e133a | 2005-08-16 18:33:07 +0000 | [diff] [blame] | 2131 | DumpNodes(getRoot().Val, 2, this); |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 2132 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 2133 | std::cerr << "\n\n"; |
| 2134 | } |
| 2135 | |