Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1 | //===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris 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 | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetLowering.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 20 | #include <iostream> |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 21 | #include <set> |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 22 | #include <cmath> |
Jeff Cohen | fd161e9 | 2005-01-09 20:41:56 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | e25738c | 2004-06-02 04:28:06 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 26 | static bool isCommutativeBinOp(unsigned Opcode) { |
| 27 | switch (Opcode) { |
| 28 | case ISD::ADD: |
| 29 | case ISD::MUL: |
| 30 | case ISD::AND: |
| 31 | case ISD::OR: |
| 32 | case ISD::XOR: return true; |
| 33 | default: return false; // FIXME: Need commutative info for user ops! |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | static bool isAssociativeBinOp(unsigned Opcode) { |
| 38 | switch (Opcode) { |
| 39 | case ISD::ADD: |
| 40 | case ISD::MUL: |
| 41 | case ISD::AND: |
| 42 | case ISD::OR: |
| 43 | case ISD::XOR: return true; |
| 44 | default: return false; // FIXME: Need associative info for user ops! |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static unsigned ExactLog2(uint64_t Val) { |
| 49 | unsigned Count = 0; |
| 50 | while (Val != 1) { |
| 51 | Val >>= 1; |
| 52 | ++Count; |
| 53 | } |
| 54 | return Count; |
| 55 | } |
| 56 | |
| 57 | // isInvertibleForFree - Return true if there is no cost to emitting the logical |
| 58 | // inverse of this node. |
| 59 | static bool isInvertibleForFree(SDOperand N) { |
| 60 | if (isa<ConstantSDNode>(N.Val)) return true; |
| 61 | if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse()) |
| 62 | return true; |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 67 | /// getSetCCSwappedOperands - Return the operation corresponding to (Y op X) |
| 68 | /// when given the operation for (X op Y). |
| 69 | ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) { |
| 70 | // To perform this operation, we just need to swap the L and G bits of the |
| 71 | // operation. |
| 72 | unsigned OldL = (Operation >> 2) & 1; |
| 73 | unsigned OldG = (Operation >> 1) & 1; |
| 74 | return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits |
| 75 | (OldL << 1) | // New G bit |
| 76 | (OldG << 2)); // New L bit. |
| 77 | } |
| 78 | |
| 79 | /// getSetCCInverse - Return the operation corresponding to !(X op Y), where |
| 80 | /// 'op' is a valid SetCC operation. |
| 81 | ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) { |
| 82 | unsigned Operation = Op; |
| 83 | if (isInteger) |
| 84 | Operation ^= 7; // Flip L, G, E bits, but not U. |
| 85 | else |
| 86 | Operation ^= 15; // Flip all of the condition bits. |
| 87 | if (Operation > ISD::SETTRUE2) |
| 88 | Operation &= ~8; // Don't let N and U bits get set. |
| 89 | return ISD::CondCode(Operation); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /// isSignedOp - For an integer comparison, return 1 if the comparison is a |
| 94 | /// signed operation and 2 if the result is an unsigned comparison. Return zero |
| 95 | /// if the operation does not depend on the sign of the input (setne and seteq). |
| 96 | static int isSignedOp(ISD::CondCode Opcode) { |
| 97 | switch (Opcode) { |
| 98 | default: assert(0 && "Illegal integer setcc operation!"); |
| 99 | case ISD::SETEQ: |
| 100 | case ISD::SETNE: return 0; |
| 101 | case ISD::SETLT: |
| 102 | case ISD::SETLE: |
| 103 | case ISD::SETGT: |
| 104 | case ISD::SETGE: return 1; |
| 105 | case ISD::SETULT: |
| 106 | case ISD::SETULE: |
| 107 | case ISD::SETUGT: |
| 108 | case ISD::SETUGE: return 2; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// getSetCCOrOperation - Return the result of a logical OR between different |
| 113 | /// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function |
| 114 | /// returns SETCC_INVALID if it is not possible to represent the resultant |
| 115 | /// comparison. |
| 116 | ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 117 | bool isInteger) { |
| 118 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 119 | // Cannot fold a signed integer setcc with an unsigned integer setcc. |
| 120 | return ISD::SETCC_INVALID; |
| 121 | |
| 122 | unsigned Op = Op1 | Op2; // Combine all of the condition bits. |
| 123 | |
| 124 | // If the N and U bits get set then the resultant comparison DOES suddenly |
| 125 | // care about orderedness, and is true when ordered. |
| 126 | if (Op > ISD::SETTRUE2) |
| 127 | Op &= ~16; // Clear the N bit. |
| 128 | return ISD::CondCode(Op); |
| 129 | } |
| 130 | |
| 131 | /// getSetCCAndOperation - Return the result of a logical AND between different |
| 132 | /// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This |
| 133 | /// function returns zero if it is not possible to represent the resultant |
| 134 | /// comparison. |
| 135 | ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2, |
| 136 | bool isInteger) { |
| 137 | if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3) |
| 138 | // Cannot fold a signed setcc with an unsigned setcc. |
| 139 | return ISD::SETCC_INVALID; |
| 140 | |
| 141 | // Combine all of the condition bits. |
| 142 | return ISD::CondCode(Op1 & Op2); |
| 143 | } |
| 144 | |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 145 | const TargetMachine &SelectionDAG::getTarget() const { |
| 146 | return TLI.getTargetMachine(); |
| 147 | } |
| 148 | |
| 149 | |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 150 | /// RemoveDeadNodes - This method deletes all unreachable nodes in the |
| 151 | /// SelectionDAG, including nodes (like loads) that have uses of their token |
| 152 | /// chain but no other uses and no side effect. If a node is passed in as an |
| 153 | /// argument, it is used as the seed for node deletion. |
| 154 | void SelectionDAG::RemoveDeadNodes(SDNode *N) { |
| 155 | std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end()); |
| 156 | |
| 157 | // Create a dummy node (which is not added to allnodes), that adds a reference |
| 158 | // to the root node, preventing it from being deleted. |
| 159 | SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot()); |
| 160 | |
| 161 | DeleteNodeIfDead(N, &AllNodeSet); |
| 162 | |
| 163 | Restart: |
| 164 | unsigned NumNodes = AllNodeSet.size(); |
| 165 | for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end(); |
| 166 | I != E; ++I) { |
| 167 | // Try to delete this node. |
| 168 | DeleteNodeIfDead(*I, &AllNodeSet); |
| 169 | |
| 170 | // If we actually deleted any nodes, do not use invalid iterators in |
| 171 | // AllNodeSet. |
| 172 | if (AllNodeSet.size() != NumNodes) |
| 173 | goto Restart; |
| 174 | } |
| 175 | |
| 176 | // Restore AllNodes. |
| 177 | if (AllNodes.size() != NumNodes) |
| 178 | AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end()); |
| 179 | |
| 180 | // If the root changed (e.g. it was a dead load, update the root). |
| 181 | setRoot(DummyNode->getOperand(0)); |
| 182 | |
| 183 | // Now that we are done with the dummy node, delete it. |
| 184 | DummyNode->getOperand(0).Val->removeUser(DummyNode); |
| 185 | delete DummyNode; |
| 186 | } |
| 187 | |
| 188 | void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) { |
| 189 | if (!N->use_empty()) |
| 190 | return; |
| 191 | |
| 192 | // Okay, we really are going to delete this node. First take this out of the |
| 193 | // appropriate CSE map. |
| 194 | switch (N->getOpcode()) { |
| 195 | case ISD::Constant: |
| 196 | Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(), |
| 197 | N->getValueType(0))); |
| 198 | break; |
| 199 | case ISD::ConstantFP: |
| 200 | ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(), |
| 201 | N->getValueType(0))); |
| 202 | break; |
| 203 | case ISD::GlobalAddress: |
| 204 | GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal()); |
| 205 | break; |
| 206 | case ISD::FrameIndex: |
| 207 | FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex()); |
| 208 | break; |
| 209 | case ISD::ConstantPool: |
| 210 | ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex()); |
| 211 | break; |
| 212 | case ISD::BasicBlock: |
| 213 | BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock()); |
| 214 | break; |
| 215 | case ISD::ExternalSymbol: |
| 216 | ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol()); |
| 217 | break; |
| 218 | |
| 219 | case ISD::LOAD: |
| 220 | Loads.erase(std::make_pair(N->getOperand(1), |
| 221 | std::make_pair(N->getOperand(0), |
| 222 | N->getValueType(0)))); |
| 223 | break; |
| 224 | case ISD::SETCC: |
| 225 | SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0), |
| 226 | N->getOperand(1)), |
Chris Lattner | 4a9b4f1 | 2005-01-18 19:26:36 +0000 | [diff] [blame] | 227 | std::make_pair( |
| 228 | cast<SetCCSDNode>(N)->getCondition(), |
| 229 | N->getValueType(0)))); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 230 | break; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 231 | case ISD::TRUNCSTORE: |
| 232 | case ISD::SIGN_EXTEND_INREG: |
| 233 | case ISD::ZERO_EXTEND_INREG: |
| 234 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 235 | case ISD::EXTLOAD: |
| 236 | case ISD::SEXTLOAD: |
| 237 | case ISD::ZEXTLOAD: { |
| 238 | EVTStruct NN; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 239 | NN.Opcode = ISD::TRUNCSTORE; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 240 | NN.VT = N->getValueType(0); |
| 241 | NN.EVT = cast<MVTSDNode>(N)->getExtraValueType(); |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 242 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 243 | NN.Ops.push_back(N->getOperand(i)); |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 244 | MVTSDNodes.erase(NN); |
| 245 | break; |
| 246 | } |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 247 | default: |
| 248 | if (N->getNumOperands() == 1) |
| 249 | UnaryOps.erase(std::make_pair(N->getOpcode(), |
| 250 | std::make_pair(N->getOperand(0), |
| 251 | N->getValueType(0)))); |
| 252 | else if (N->getNumOperands() == 2) |
| 253 | BinaryOps.erase(std::make_pair(N->getOpcode(), |
| 254 | std::make_pair(N->getOperand(0), |
| 255 | N->getOperand(1)))); |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // Next, brutally remove the operand list. |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 260 | while (!N->Operands.empty()) { |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 261 | SDNode *O = N->Operands.back().Val; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 262 | N->Operands.pop_back(); |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 263 | O->removeUser(N); |
| 264 | |
| 265 | // Now that we removed this operand, see if there are no uses of it left. |
| 266 | DeleteNodeIfDead(O, NodeSet); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Remove the node from the nodes set and delete it. |
| 270 | std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet; |
| 271 | AllNodeSet.erase(N); |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 272 | |
| 273 | // Now that the node is gone, check to see if any of the operands of this node |
| 274 | // are dead now. |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 275 | delete N; |
Chris Lattner | 0e12e6e | 2005-01-07 21:09:16 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 279 | SelectionDAG::~SelectionDAG() { |
| 280 | for (unsigned i = 0, e = AllNodes.size(); i != e; ++i) |
| 281 | delete AllNodes[i]; |
| 282 | } |
| 283 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 284 | SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) { |
| 285 | assert(MVT::isInteger(VT) && "Cannot create FP integer constant!"); |
| 286 | // Mask out any bits that are not valid for this constant. |
Chris Lattner | 623f70d | 2005-01-08 06:24:30 +0000 | [diff] [blame] | 287 | if (VT != MVT::i64) |
| 288 | Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1; |
| 289 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 290 | SDNode *&N = Constants[std::make_pair(Val, VT)]; |
| 291 | if (N) return SDOperand(N, 0); |
| 292 | N = new ConstantSDNode(Val, VT); |
| 293 | AllNodes.push_back(N); |
| 294 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 297 | SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) { |
| 298 | assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!"); |
| 299 | if (VT == MVT::f32) |
| 300 | Val = (float)Val; // Mask out extra precision. |
| 301 | |
| 302 | SDNode *&N = ConstantFPs[std::make_pair(Val, VT)]; |
| 303 | if (N) return SDOperand(N, 0); |
| 304 | N = new ConstantFPSDNode(Val, VT); |
| 305 | AllNodes.push_back(N); |
| 306 | return SDOperand(N, 0); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV, |
| 312 | MVT::ValueType VT) { |
| 313 | SDNode *&N = GlobalValues[GV]; |
| 314 | if (N) return SDOperand(N, 0); |
| 315 | N = new GlobalAddressSDNode(GV,VT); |
| 316 | AllNodes.push_back(N); |
| 317 | return SDOperand(N, 0); |
| 318 | } |
| 319 | |
| 320 | SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) { |
| 321 | SDNode *&N = FrameIndices[FI]; |
| 322 | if (N) return SDOperand(N, 0); |
| 323 | N = new FrameIndexSDNode(FI, VT); |
| 324 | AllNodes.push_back(N); |
| 325 | return SDOperand(N, 0); |
| 326 | } |
| 327 | |
| 328 | SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) { |
| 329 | SDNode *N = ConstantPoolIndices[CPIdx]; |
| 330 | if (N) return SDOperand(N, 0); |
| 331 | N = new ConstantPoolSDNode(CPIdx, VT); |
| 332 | AllNodes.push_back(N); |
| 333 | return SDOperand(N, 0); |
| 334 | } |
| 335 | |
| 336 | SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) { |
| 337 | SDNode *&N = BBNodes[MBB]; |
| 338 | if (N) return SDOperand(N, 0); |
| 339 | N = new BasicBlockSDNode(MBB); |
| 340 | AllNodes.push_back(N); |
| 341 | return SDOperand(N, 0); |
| 342 | } |
| 343 | |
| 344 | SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) { |
| 345 | SDNode *&N = ExternalSymbols[Sym]; |
| 346 | if (N) return SDOperand(N, 0); |
| 347 | N = new ExternalSymbolSDNode(Sym, VT); |
| 348 | AllNodes.push_back(N); |
| 349 | return SDOperand(N, 0); |
| 350 | } |
| 351 | |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 352 | SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT, |
| 353 | SDOperand N1, SDOperand N2) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 354 | // These setcc operations always fold. |
| 355 | switch (Cond) { |
| 356 | default: break; |
| 357 | case ISD::SETFALSE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 358 | case ISD::SETFALSE2: return getConstant(0, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 359 | case ISD::SETTRUE: |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 360 | case ISD::SETTRUE2: return getConstant(1, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) |
| 364 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) { |
| 365 | uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); |
| 366 | |
| 367 | // Sign extend the operands if required |
| 368 | if (ISD::isSignedIntSetCC(Cond)) { |
| 369 | C1 = N1C->getSignExtended(); |
| 370 | C2 = N2C->getSignExtended(); |
| 371 | } |
| 372 | |
| 373 | switch (Cond) { |
| 374 | default: assert(0 && "Unknown integer setcc!"); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 375 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 376 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 377 | case ISD::SETULT: return getConstant(C1 < C2, VT); |
| 378 | case ISD::SETUGT: return getConstant(C1 > C2, VT); |
| 379 | case ISD::SETULE: return getConstant(C1 <= C2, VT); |
| 380 | case ISD::SETUGE: return getConstant(C1 >= C2, VT); |
| 381 | case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT); |
| 382 | case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT); |
| 383 | case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT); |
| 384 | case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 385 | } |
| 386 | } else { |
| 387 | // Ensure that the constant occurs on the RHS. |
| 388 | Cond = ISD::getSetCCSwappedOperands(Cond); |
| 389 | std::swap(N1, N2); |
| 390 | } |
| 391 | |
| 392 | if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val)) |
| 393 | if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) { |
| 394 | double C1 = N1C->getValue(), C2 = N2C->getValue(); |
| 395 | |
| 396 | switch (Cond) { |
| 397 | default: break; // FIXME: Implement the rest of these! |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 398 | case ISD::SETEQ: return getConstant(C1 == C2, VT); |
| 399 | case ISD::SETNE: return getConstant(C1 != C2, VT); |
| 400 | case ISD::SETLT: return getConstant(C1 < C2, VT); |
| 401 | case ISD::SETGT: return getConstant(C1 > C2, VT); |
| 402 | case ISD::SETLE: return getConstant(C1 <= C2, VT); |
| 403 | case ISD::SETGE: return getConstant(C1 >= C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 404 | } |
| 405 | } else { |
| 406 | // Ensure that the constant occurs on the RHS. |
| 407 | Cond = ISD::getSetCCSwappedOperands(Cond); |
| 408 | std::swap(N1, N2); |
| 409 | } |
| 410 | |
| 411 | if (N1 == N2) { |
| 412 | // We can always fold X == Y for integer setcc's. |
| 413 | if (MVT::isInteger(N1.getValueType())) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 414 | return getConstant(ISD::isTrueWhenEqual(Cond), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 415 | unsigned UOF = ISD::getUnorderedFlavor(Cond); |
| 416 | if (UOF == 2) // FP operators that are undefined on NaNs. |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 417 | return getConstant(ISD::isTrueWhenEqual(Cond), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 418 | if (UOF == ISD::isTrueWhenEqual(Cond)) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 419 | return getConstant(UOF, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 420 | // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO |
| 421 | // if it is not already. |
| 422 | Cond = UOF == 0 ? ISD::SETUO : ISD::SETO; |
| 423 | } |
| 424 | |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 425 | if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) && |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 426 | MVT::isInteger(N1.getValueType())) { |
| 427 | if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB || |
| 428 | N1.getOpcode() == ISD::XOR) { |
| 429 | // Simplify (X+Y) == (X+Z) --> Y == Z |
| 430 | if (N1.getOpcode() == N2.getOpcode()) { |
| 431 | if (N1.getOperand(0) == N2.getOperand(0)) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 432 | return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1)); |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 433 | if (N1.getOperand(1) == N2.getOperand(1)) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 434 | return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0)); |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 435 | if (isCommutativeBinOp(N1.getOpcode())) { |
| 436 | // If X op Y == Y op X, try other combinations. |
| 437 | if (N1.getOperand(0) == N2.getOperand(1)) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 438 | return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0)); |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 439 | if (N1.getOperand(1) == N2.getOperand(0)) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 440 | return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1)); |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 443 | |
| 444 | // FIXME: move this stuff to the DAG Combiner when it exists! |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 445 | |
| 446 | // Simplify (X+Z) == X --> Z == 0 |
| 447 | if (N1.getOperand(0) == N2) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 448 | return getSetCC(Cond, VT, N1.getOperand(1), |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 449 | getConstant(0, N1.getValueType())); |
| 450 | if (N1.getOperand(1) == N2) { |
| 451 | if (isCommutativeBinOp(N1.getOpcode())) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 452 | return getSetCC(Cond, VT, N1.getOperand(0), |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 453 | getConstant(0, N1.getValueType())); |
| 454 | else { |
| 455 | assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!"); |
| 456 | // (Z-X) == X --> Z == X<<1 |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 457 | return getSetCC(Cond, VT, N1.getOperand(0), |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 458 | getNode(ISD::SHL, N2.getValueType(), |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 459 | N2, getConstant(1, TLI.getShiftAmountTy()))); |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 460 | } |
Chris Lattner | 5cdcc58 | 2005-01-09 20:52:51 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 464 | if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB || |
| 465 | N2.getOpcode() == ISD::XOR) { |
| 466 | // Simplify X == (X+Z) --> Z == 0 |
| 467 | if (N2.getOperand(0) == N1) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 468 | return getSetCC(Cond, VT, N2.getOperand(1), |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 469 | getConstant(0, N2.getValueType())); |
| 470 | else if (N2.getOperand(1) == N1) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 471 | return getSetCC(Cond, VT, N2.getOperand(0), |
Chris Lattner | 68dc310 | 2005-01-10 02:03:02 +0000 | [diff] [blame] | 472 | getConstant(0, N2.getValueType())); |
| 473 | } |
| 474 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 4a9b4f1 | 2005-01-18 19:26:36 +0000 | [diff] [blame] | 476 | SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2), |
| 477 | std::make_pair(Cond, VT))]; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 478 | if (N) return SDOperand(N, 0); |
| 479 | N = new SetCCSDNode(Cond, N1, N2); |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 480 | N->setValueTypes(VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 481 | AllNodes.push_back(N); |
| 482 | return SDOperand(N, 0); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | |
| 487 | /// getNode - Gets or creates the specified node. |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 488 | /// |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 489 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) { |
| 490 | SDNode *N = new SDNode(Opcode, VT); |
| 491 | AllNodes.push_back(N); |
| 492 | return SDOperand(N, 0); |
| 493 | } |
| 494 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 495 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 496 | SDOperand Operand) { |
| 497 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) { |
| 498 | uint64_t Val = C->getValue(); |
| 499 | switch (Opcode) { |
| 500 | default: break; |
| 501 | case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT); |
| 502 | case ISD::ZERO_EXTEND: return getConstant(Val, VT); |
| 503 | case ISD::TRUNCATE: return getConstant(Val, VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 504 | case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT); |
| 505 | case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val)) |
| 510 | switch (Opcode) { |
| 511 | case ISD::FP_ROUND: |
| 512 | case ISD::FP_EXTEND: |
| 513 | return getConstantFP(C->getValue(), VT); |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 514 | case ISD::FP_TO_SINT: |
| 515 | return getConstant((int64_t)C->getValue(), VT); |
| 516 | case ISD::FP_TO_UINT: |
| 517 | return getConstant((uint64_t)C->getValue(), VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | unsigned OpOpcode = Operand.Val->getOpcode(); |
| 521 | switch (Opcode) { |
Chris Lattner | a93ec3e | 2005-01-21 18:01:22 +0000 | [diff] [blame] | 522 | case ISD::TokenFactor: |
| 523 | return Operand; // Factor of one node? No factor. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 524 | case ISD::SIGN_EXTEND: |
| 525 | if (Operand.getValueType() == VT) return Operand; // noop extension |
| 526 | if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) |
| 527 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 528 | break; |
| 529 | case ISD::ZERO_EXTEND: |
| 530 | if (Operand.getValueType() == VT) return Operand; // noop extension |
Chris Lattner | 2f0ca79 | 2005-01-12 18:51:15 +0000 | [diff] [blame] | 531 | if (OpOpcode == ISD::ZERO_EXTEND) |
| 532 | return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 533 | break; |
| 534 | case ISD::TRUNCATE: |
| 535 | if (Operand.getValueType() == VT) return Operand; // noop truncate |
| 536 | if (OpOpcode == ISD::TRUNCATE) |
| 537 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
Chris Lattner | fd8c39b | 2005-01-07 21:56:24 +0000 | [diff] [blame] | 538 | else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) { |
| 539 | // If the source is smaller than the dest, we still need an extend. |
| 540 | if (Operand.Val->getOperand(0).getValueType() < VT) |
| 541 | return getNode(OpOpcode, VT, Operand.Val->getOperand(0)); |
| 542 | else if (Operand.Val->getOperand(0).getValueType() > VT) |
| 543 | return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0)); |
| 544 | else |
| 545 | return Operand.Val->getOperand(0); |
| 546 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
| 550 | SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))]; |
| 551 | if (N) return SDOperand(N, 0); |
| 552 | N = new SDNode(Opcode, Operand); |
| 553 | N->setValueTypes(VT); |
| 554 | AllNodes.push_back(N); |
| 555 | return SDOperand(N, 0); |
Chris Lattner | 78ec311 | 2003-08-11 14:57:33 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 558 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 559 | SDOperand N1, SDOperand N2) { |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 560 | #ifndef NDEBUG |
| 561 | switch (Opcode) { |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 562 | case ISD::TokenFactor: |
| 563 | assert(VT == MVT::Other && N1.getValueType() == MVT::Other && |
| 564 | N2.getValueType() == MVT::Other && "Invalid token factor!"); |
| 565 | break; |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 566 | case ISD::AND: |
| 567 | case ISD::OR: |
| 568 | case ISD::XOR: |
| 569 | case ISD::UDIV: |
| 570 | case ISD::UREM: |
| 571 | assert(MVT::isInteger(VT) && "This operator does not apply to FP types!"); |
| 572 | // fall through |
| 573 | case ISD::ADD: |
| 574 | case ISD::SUB: |
| 575 | case ISD::MUL: |
| 576 | case ISD::SDIV: |
| 577 | case ISD::SREM: |
| 578 | assert(N1.getValueType() == N2.getValueType() && |
| 579 | N1.getValueType() == VT && "Binary operator types must match!"); |
| 580 | break; |
| 581 | |
| 582 | case ISD::SHL: |
| 583 | case ISD::SRA: |
| 584 | case ISD::SRL: |
| 585 | assert(VT == N1.getValueType() && |
| 586 | "Shift operators return type must be the same as their first arg"); |
| 587 | assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) && |
Chris Lattner | 068a81e | 2005-01-17 17:15:02 +0000 | [diff] [blame] | 588 | VT != MVT::i1 && "Shifts only work on integers"); |
Chris Lattner | 7636512 | 2005-01-16 02:23:22 +0000 | [diff] [blame] | 589 | break; |
| 590 | default: break; |
| 591 | } |
| 592 | #endif |
| 593 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 594 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 595 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 596 | if (N1C) { |
| 597 | if (N2C) { |
| 598 | uint64_t C1 = N1C->getValue(), C2 = N2C->getValue(); |
| 599 | switch (Opcode) { |
| 600 | case ISD::ADD: return getConstant(C1 + C2, VT); |
| 601 | case ISD::SUB: return getConstant(C1 - C2, VT); |
| 602 | case ISD::MUL: return getConstant(C1 * C2, VT); |
| 603 | case ISD::UDIV: |
| 604 | if (C2) return getConstant(C1 / C2, VT); |
| 605 | break; |
| 606 | case ISD::UREM : |
| 607 | if (C2) return getConstant(C1 % C2, VT); |
| 608 | break; |
| 609 | case ISD::SDIV : |
| 610 | if (C2) return getConstant(N1C->getSignExtended() / |
| 611 | N2C->getSignExtended(), VT); |
| 612 | break; |
| 613 | case ISD::SREM : |
| 614 | if (C2) return getConstant(N1C->getSignExtended() % |
| 615 | N2C->getSignExtended(), VT); |
| 616 | break; |
| 617 | case ISD::AND : return getConstant(C1 & C2, VT); |
| 618 | case ISD::OR : return getConstant(C1 | C2, VT); |
| 619 | case ISD::XOR : return getConstant(C1 ^ C2, VT); |
Chris Lattner | 8136d1f | 2005-01-10 00:07:15 +0000 | [diff] [blame] | 620 | case ISD::SHL : return getConstant(C1 << (int)C2, VT); |
| 621 | case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT); |
| 622 | case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 623 | default: break; |
| 624 | } |
| 625 | |
| 626 | } else { // Cannonicalize constant to RHS if commutative |
| 627 | if (isCommutativeBinOp(Opcode)) { |
| 628 | std::swap(N1C, N2C); |
| 629 | std::swap(N1, N2); |
| 630 | } |
| 631 | } |
Chris Lattner | 88218ef | 2005-01-19 17:29:49 +0000 | [diff] [blame] | 632 | |
| 633 | switch (Opcode) { |
| 634 | default: break; |
| 635 | case ISD::SHL: // shl 0, X -> 0 |
| 636 | if (N1C->isNullValue()) return N1; |
| 637 | break; |
| 638 | case ISD::SRL: // srl 0, X -> 0 |
| 639 | if (N1C->isNullValue()) return N1; |
| 640 | break; |
| 641 | case ISD::SRA: // sra -1, X -> -1 |
| 642 | if (N1C->isAllOnesValue()) return N1; |
| 643 | break; |
| 644 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (N2C) { |
| 648 | uint64_t C2 = N2C->getValue(); |
| 649 | |
| 650 | switch (Opcode) { |
| 651 | case ISD::ADD: |
| 652 | if (!C2) return N1; // add X, 0 -> X |
| 653 | break; |
| 654 | case ISD::SUB: |
| 655 | if (!C2) return N1; // sub X, 0 -> X |
| 656 | break; |
| 657 | case ISD::MUL: |
| 658 | if (!C2) return N2; // mul X, 0 -> 0 |
| 659 | if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X |
| 660 | return getNode(ISD::SUB, VT, getConstant(0, VT), N1); |
| 661 | |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 662 | // FIXME: Move this to the DAG combiner when it exists. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 663 | if ((C2 & C2-1) == 0) { |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 664 | SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy()); |
| 665 | return getNode(ISD::SHL, VT, N1, ShAmt); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 666 | } |
| 667 | break; |
| 668 | |
| 669 | case ISD::UDIV: |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 670 | // FIXME: Move this to the DAG combiner when it exists. |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 671 | if ((C2 & C2-1) == 0 && C2) { |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 672 | SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy()); |
| 673 | return getNode(ISD::SRL, VT, N1, ShAmt); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 674 | } |
| 675 | break; |
| 676 | |
Chris Lattner | a8d9cc8 | 2005-01-11 04:25:13 +0000 | [diff] [blame] | 677 | case ISD::SHL: |
| 678 | case ISD::SRL: |
Chris Lattner | b48da39 | 2005-01-23 04:39:44 +0000 | [diff] [blame] | 679 | // If the shift amount is bigger than the size of the data, simplify. |
| 680 | if (C2 >= MVT::getSizeInBits(N1.getValueType())) { |
| 681 | if (TLI.getShiftAmountFlavor() == TargetLowering::Mask) { |
| 682 | unsigned NewAmt = |
| 683 | C2 & ((1 << MVT::getSizeInBits(N1.getValueType()))-1); |
| 684 | return getNode(Opcode, VT, N1, getConstant(NewAmt,N2.getValueType())); |
| 685 | } else if (TLI.getShiftAmountFlavor() == TargetLowering::Extend) { |
| 686 | // Shifting all of the bits out? |
| 687 | return getConstant(0, N1.getValueType()); |
| 688 | } |
| 689 | } |
| 690 | // FALL THROUGH. |
Chris Lattner | a8d9cc8 | 2005-01-11 04:25:13 +0000 | [diff] [blame] | 691 | case ISD::SRA: |
| 692 | if (C2 == 0) return N1; |
| 693 | break; |
| 694 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 695 | case ISD::AND: |
| 696 | if (!C2) return N2; // X and 0 -> 0 |
| 697 | if (N2C->isAllOnesValue()) |
| 698 | return N1; // X and -1 -> X |
| 699 | break; |
| 700 | case ISD::OR: |
| 701 | if (!C2)return N1; // X or 0 -> X |
| 702 | if (N2C->isAllOnesValue()) |
| 703 | return N2; // X or -1 -> -1 |
| 704 | break; |
| 705 | case ISD::XOR: |
| 706 | if (!C2) return N1; // X xor 0 -> X |
| 707 | if (N2C->isAllOnesValue()) { |
| 708 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){ |
| 709 | // !(X op Y) -> (X !op Y) |
| 710 | bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); |
| 711 | return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger), |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 712 | SetCC->getValueType(0), |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 713 | SetCC->getOperand(0), SetCC->getOperand(1)); |
| 714 | } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) { |
| 715 | SDNode *Op = N1.Val; |
| 716 | // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible |
| 717 | // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible |
| 718 | SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1); |
| 719 | if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) { |
| 720 | LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS |
| 721 | RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS |
| 722 | if (Op->getOpcode() == ISD::AND) |
| 723 | return getNode(ISD::OR, VT, LHS, RHS); |
| 724 | return getNode(ISD::AND, VT, LHS, RHS); |
| 725 | } |
| 726 | } |
| 727 | // X xor -1 -> not(x) ? |
| 728 | } |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | // Reassociate ((X op C1) op C2) if possible. |
| 733 | if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode)) |
| 734 | if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1))) |
Chris Lattner | 4287d5e | 2005-01-07 22:44:09 +0000 | [diff] [blame] | 735 | return getNode(Opcode, VT, N1.Val->getOperand(0), |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 736 | getNode(Opcode, VT, N2, N1.Val->getOperand(1))); |
| 737 | } |
| 738 | |
| 739 | ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val); |
| 740 | ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val); |
| 741 | if (N1CFP) |
| 742 | if (N2CFP) { |
| 743 | double C1 = N1CFP->getValue(), C2 = N2CFP->getValue(); |
| 744 | switch (Opcode) { |
| 745 | case ISD::ADD: return getConstantFP(C1 + C2, VT); |
| 746 | case ISD::SUB: return getConstantFP(C1 - C2, VT); |
| 747 | case ISD::MUL: return getConstantFP(C1 * C2, VT); |
| 748 | case ISD::SDIV: |
| 749 | if (C2) return getConstantFP(C1 / C2, VT); |
| 750 | break; |
| 751 | case ISD::SREM : |
| 752 | if (C2) return getConstantFP(fmod(C1, C2), VT); |
| 753 | break; |
| 754 | default: break; |
| 755 | } |
| 756 | |
| 757 | } else { // Cannonicalize constant to RHS if commutative |
| 758 | if (isCommutativeBinOp(Opcode)) { |
| 759 | std::swap(N1CFP, N2CFP); |
| 760 | std::swap(N1, N2); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | // Finally, fold operations that do not require constants. |
| 765 | switch (Opcode) { |
Chris Lattner | 39908e0 | 2005-01-19 18:01:40 +0000 | [diff] [blame] | 766 | case ISD::TokenFactor: |
| 767 | if (N1.getOpcode() == ISD::EntryToken) |
| 768 | return N2; |
| 769 | if (N2.getOpcode() == ISD::EntryToken) |
| 770 | return N1; |
| 771 | break; |
| 772 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 773 | case ISD::AND: |
| 774 | case ISD::OR: |
| 775 | if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val)) |
| 776 | if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) { |
| 777 | SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0); |
| 778 | SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1); |
| 779 | ISD::CondCode Op2 = RHS->getCondition(); |
| 780 | |
| 781 | // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y) |
| 782 | if (LL == RR && LR == RL) { |
| 783 | Op2 = ISD::getSetCCSwappedOperands(Op2); |
| 784 | goto MatchedBackwards; |
| 785 | } |
| 786 | |
| 787 | if (LL == RL && LR == RR) { |
| 788 | MatchedBackwards: |
| 789 | ISD::CondCode Result; |
| 790 | bool isInteger = MVT::isInteger(LL.getValueType()); |
| 791 | if (Opcode == ISD::OR) |
| 792 | Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2, |
| 793 | isInteger); |
| 794 | else |
| 795 | Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2, |
| 796 | isInteger); |
| 797 | if (Result != ISD::SETCC_INVALID) |
Chris Lattner | f30b73b | 2005-01-18 02:52:03 +0000 | [diff] [blame] | 798 | return getSetCC(Result, LHS->getValueType(0), LL, LR); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | break; |
| 802 | case ISD::XOR: |
| 803 | if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0 |
| 804 | break; |
Chris Lattner | abd2182 | 2005-01-09 20:09:57 +0000 | [diff] [blame] | 805 | case ISD::SUB: |
| 806 | if (N1.getOpcode() == ISD::ADD) { |
| 807 | if (N1.Val->getOperand(0) == N2) |
| 808 | return N1.Val->getOperand(1); // (A+B)-A == B |
| 809 | if (N1.Val->getOperand(1) == N2) |
| 810 | return N1.Val->getOperand(0); // (A+B)-B == A |
| 811 | } |
| 812 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))]; |
| 816 | if (N) return SDOperand(N, 0); |
| 817 | N = new SDNode(Opcode, N1, N2); |
| 818 | N->setValueTypes(VT); |
| 819 | |
| 820 | AllNodes.push_back(N); |
| 821 | return SDOperand(N, 0); |
| 822 | } |
| 823 | |
| 824 | SDOperand SelectionDAG::getLoad(MVT::ValueType VT, |
| 825 | SDOperand Chain, SDOperand Ptr) { |
| 826 | SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))]; |
| 827 | if (N) return SDOperand(N, 0); |
| 828 | N = new SDNode(ISD::LOAD, Chain, Ptr); |
| 829 | |
| 830 | // Loads have a token chain. |
| 831 | N->setValueTypes(VT, MVT::Other); |
| 832 | AllNodes.push_back(N); |
| 833 | return SDOperand(N, 0); |
| 834 | } |
| 835 | |
| 836 | |
| 837 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 838 | SDOperand N1, SDOperand N2, SDOperand N3) { |
| 839 | // Perform various simplifications. |
| 840 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val); |
| 841 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val); |
| 842 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val); |
| 843 | switch (Opcode) { |
| 844 | case ISD::SELECT: |
| 845 | if (N1C) |
| 846 | if (N1C->getValue()) |
| 847 | return N2; // select true, X, Y -> X |
| 848 | else |
| 849 | return N3; // select false, X, Y -> Y |
| 850 | |
| 851 | if (N2 == N3) return N2; // select C, X, X -> X |
| 852 | |
| 853 | if (VT == MVT::i1) { // Boolean SELECT |
| 854 | if (N2C) { |
| 855 | if (N3C) { |
| 856 | if (N2C->getValue()) // select C, 1, 0 -> C |
| 857 | return N1; |
| 858 | return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C |
| 859 | } |
| 860 | |
| 861 | if (N2C->getValue()) // select C, 1, X -> C | X |
| 862 | return getNode(ISD::OR, VT, N1, N3); |
| 863 | else // select C, 0, X -> ~C & X |
| 864 | return getNode(ISD::AND, VT, |
| 865 | getNode(ISD::XOR, N1.getValueType(), N1, |
| 866 | getConstant(1, N1.getValueType())), N3); |
| 867 | } else if (N3C) { |
| 868 | if (N3C->getValue()) // select C, X, 1 -> ~C | X |
| 869 | return getNode(ISD::OR, VT, |
| 870 | getNode(ISD::XOR, N1.getValueType(), N1, |
| 871 | getConstant(1, N1.getValueType())), N2); |
| 872 | else // select C, X, 0 -> C & X |
| 873 | return getNode(ISD::AND, VT, N1, N2); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | break; |
Chris Lattner | 5351e9b | 2005-01-07 22:49:57 +0000 | [diff] [blame] | 878 | case ISD::BRCOND: |
| 879 | if (N2C) |
| 880 | if (N2C->getValue()) // Unconditional branch |
| 881 | return getNode(ISD::BR, MVT::Other, N1, N3); |
| 882 | else |
| 883 | return N1; // Never-taken branch |
Chris Lattner | 7c68ec6 | 2005-01-07 23:32:00 +0000 | [diff] [blame] | 884 | break; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | SDNode *N = new SDNode(Opcode, N1, N2, N3); |
| 888 | switch (Opcode) { |
| 889 | default: |
| 890 | N->setValueTypes(VT); |
| 891 | break; |
| 892 | case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain |
| 893 | N->setValueTypes(VT, MVT::Other); |
| 894 | break; |
| 895 | } |
| 896 | |
| 897 | // FIXME: memoize NODES |
| 898 | AllNodes.push_back(N); |
| 899 | return SDOperand(N, 0); |
| 900 | } |
| 901 | |
| 902 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, |
| 903 | std::vector<SDOperand> &Children) { |
| 904 | switch (Children.size()) { |
| 905 | case 0: return getNode(Opcode, VT); |
| 906 | case 1: return getNode(Opcode, VT, Children[0]); |
| 907 | case 2: return getNode(Opcode, VT, Children[0], Children[1]); |
| 908 | case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]); |
| 909 | default: |
| 910 | // FIXME: MEMOIZE!! |
| 911 | SDNode *N = new SDNode(Opcode, Children); |
Chris Lattner | 17eee18 | 2005-01-20 18:50:55 +0000 | [diff] [blame] | 912 | if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) { |
| 913 | N->setValueTypes(VT); |
| 914 | } else { |
| 915 | std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT); |
| 916 | N->setValueTypes(V); |
| 917 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 918 | AllNodes.push_back(N); |
| 919 | return SDOperand(N, 0); |
| 920 | } |
| 921 | } |
| 922 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 923 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1, |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 924 | MVT::ValueType EVT) { |
| 925 | |
| 926 | switch (Opcode) { |
| 927 | default: assert(0 && "Bad opcode for this accessor!"); |
| 928 | case ISD::FP_ROUND_INREG: |
| 929 | assert(VT == N1.getValueType() && "Not an inreg round!"); |
| 930 | assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) && |
| 931 | "Cannot FP_ROUND_INREG integer types"); |
| 932 | if (EVT == VT) return N1; // Not actually rounding |
| 933 | assert(EVT < VT && "Not rounding down!"); |
| 934 | break; |
| 935 | case ISD::ZERO_EXTEND_INREG: |
| 936 | case ISD::SIGN_EXTEND_INREG: |
| 937 | assert(VT == N1.getValueType() && "Not an inreg extend!"); |
| 938 | assert(MVT::isInteger(VT) && MVT::isInteger(EVT) && |
| 939 | "Cannot *_EXTEND_INREG FP types"); |
| 940 | if (EVT == VT) return N1; // Not actually extending |
| 941 | assert(EVT < VT && "Not extending!"); |
Chris Lattner | 950aa3c | 2005-01-16 00:17:20 +0000 | [diff] [blame] | 942 | |
| 943 | // If we are sign extending an extension, use the original source. |
| 944 | if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG || |
| 945 | N1.getOpcode() == ISD::SIGN_EXTEND_INREG) { |
| 946 | if (N1.getOpcode() == Opcode && |
| 947 | cast<MVTSDNode>(N1)->getExtraValueType() <= EVT) |
| 948 | return N1; |
| 949 | } |
| 950 | |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 951 | break; |
| 952 | } |
| 953 | |
| 954 | EVTStruct NN; |
| 955 | NN.Opcode = Opcode; |
| 956 | NN.VT = VT; |
| 957 | NN.EVT = EVT; |
| 958 | NN.Ops.push_back(N1); |
| 959 | |
| 960 | SDNode *&N = MVTSDNodes[NN]; |
| 961 | if (N) return SDOperand(N, 0); |
| 962 | N = new MVTSDNode(Opcode, VT, N1, EVT); |
| 963 | AllNodes.push_back(N); |
| 964 | return SDOperand(N, 0); |
| 965 | } |
| 966 | |
| 967 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1, |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 968 | SDOperand N2, MVT::ValueType EVT) { |
| 969 | switch (Opcode) { |
| 970 | default: assert(0 && "Bad opcode for this accessor!"); |
| 971 | case ISD::EXTLOAD: |
| 972 | case ISD::SEXTLOAD: |
| 973 | case ISD::ZEXTLOAD: |
| 974 | // If they are asking for an extending loat from/to the same thing, return a |
| 975 | // normal load. |
| 976 | if (VT == EVT) |
| 977 | return getNode(ISD::LOAD, VT, N1, N2); |
| 978 | assert(EVT < VT && "Should only be an extending load, not truncating!"); |
| 979 | assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) && |
| 980 | "Cannot sign/zero extend a FP load!"); |
| 981 | assert(MVT::isInteger(VT) == MVT::isInteger(EVT) && |
| 982 | "Cannot convert from FP to Int or Int -> FP!"); |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | EVTStruct NN; |
| 987 | NN.Opcode = Opcode; |
| 988 | NN.VT = VT; |
| 989 | NN.EVT = EVT; |
| 990 | NN.Ops.push_back(N1); |
| 991 | NN.Ops.push_back(N2); |
| 992 | |
| 993 | SDNode *&N = MVTSDNodes[NN]; |
| 994 | if (N) return SDOperand(N, 0); |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 995 | N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT); |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 996 | AllNodes.push_back(N); |
| 997 | return SDOperand(N, 0); |
| 998 | } |
| 999 | |
| 1000 | SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1, |
| 1001 | SDOperand N2, SDOperand N3, MVT::ValueType EVT) { |
| 1002 | switch (Opcode) { |
| 1003 | default: assert(0 && "Bad opcode for this accessor!"); |
| 1004 | case ISD::TRUNCSTORE: |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1005 | #if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store |
| 1006 | // If this is a truncating store of a constant, convert to the desired type |
| 1007 | // and store it instead. |
| 1008 | if (isa<Constant>(N1)) { |
| 1009 | SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1); |
| 1010 | if (isa<Constant>(Op)) |
| 1011 | N1 = Op; |
| 1012 | } |
| 1013 | // Also for ConstantFP? |
| 1014 | #endif |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1015 | if (N1.getValueType() == EVT) // Normal store? |
| 1016 | return getNode(ISD::STORE, VT, N1, N2, N3); |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1017 | assert(N2.getValueType() > EVT && "Not a truncation?"); |
| 1018 | assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) && |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1019 | "Can't do FP-INT conversion!"); |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | EVTStruct NN; |
| 1024 | NN.Opcode = Opcode; |
| 1025 | NN.VT = VT; |
| 1026 | NN.EVT = EVT; |
| 1027 | NN.Ops.push_back(N1); |
| 1028 | NN.Ops.push_back(N2); |
| 1029 | NN.Ops.push_back(N3); |
| 1030 | |
| 1031 | SDNode *&N = MVTSDNodes[NN]; |
| 1032 | if (N) return SDOperand(N, 0); |
| 1033 | N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT); |
| 1034 | AllNodes.push_back(N); |
| 1035 | return SDOperand(N, 0); |
| 1036 | } |
| 1037 | |
| 1038 | |
Chris Lattner | 5c88456 | 2005-01-12 18:37:47 +0000 | [diff] [blame] | 1039 | /// hasNUsesOfValue - Return true if there are exactly NUSES uses of the |
| 1040 | /// indicated value. This method ignores uses of other values defined by this |
| 1041 | /// operation. |
| 1042 | bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) { |
| 1043 | assert(Value < getNumValues() && "Bad value!"); |
| 1044 | |
| 1045 | // If there is only one value, this is easy. |
| 1046 | if (getNumValues() == 1) |
| 1047 | return use_size() == NUses; |
| 1048 | if (Uses.size() < NUses) return false; |
| 1049 | |
| 1050 | SDOperand TheValue(this, Value); |
| 1051 | |
| 1052 | std::set<SDNode*> UsersHandled; |
| 1053 | |
| 1054 | for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end(); |
| 1055 | UI != E; ++UI) { |
| 1056 | SDNode *User = *UI; |
| 1057 | if (User->getNumOperands() == 1 || |
| 1058 | UsersHandled.insert(User).second) // First time we've seen this? |
| 1059 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 1060 | if (User->getOperand(i) == TheValue) { |
| 1061 | if (NUses == 0) |
| 1062 | return false; // too many uses |
| 1063 | --NUses; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // Found exactly the right number of uses? |
| 1068 | return NUses == 0; |
| 1069 | } |
| 1070 | |
| 1071 | |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1072 | const char *SDNode::getOperationName() const { |
| 1073 | switch (getOpcode()) { |
| 1074 | default: return "<<Unknown>>"; |
| 1075 | case ISD::EntryToken: return "EntryToken"; |
Chris Lattner | 282c5ca | 2005-01-13 17:59:10 +0000 | [diff] [blame] | 1076 | case ISD::TokenFactor: return "TokenFactor"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1077 | case ISD::Constant: return "Constant"; |
| 1078 | case ISD::ConstantFP: return "ConstantFP"; |
| 1079 | case ISD::GlobalAddress: return "GlobalAddress"; |
| 1080 | case ISD::FrameIndex: return "FrameIndex"; |
| 1081 | case ISD::BasicBlock: return "BasicBlock"; |
| 1082 | case ISD::ExternalSymbol: return "ExternalSymbol"; |
| 1083 | case ISD::ConstantPool: return "ConstantPoolIndex"; |
| 1084 | case ISD::CopyToReg: return "CopyToReg"; |
| 1085 | case ISD::CopyFromReg: return "CopyFromReg"; |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1086 | case ISD::ImplicitDef: return "ImplicitDef"; |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1088 | case ISD::ADD: return "add"; |
| 1089 | case ISD::SUB: return "sub"; |
| 1090 | case ISD::MUL: return "mul"; |
| 1091 | case ISD::SDIV: return "sdiv"; |
| 1092 | case ISD::UDIV: return "udiv"; |
| 1093 | case ISD::SREM: return "srem"; |
| 1094 | case ISD::UREM: return "urem"; |
| 1095 | case ISD::AND: return "and"; |
| 1096 | case ISD::OR: return "or"; |
| 1097 | case ISD::XOR: return "xor"; |
| 1098 | case ISD::SHL: return "shl"; |
| 1099 | case ISD::SRA: return "sra"; |
| 1100 | case ISD::SRL: return "srl"; |
| 1101 | |
| 1102 | case ISD::SELECT: return "select"; |
Chris Lattner | 17eee18 | 2005-01-20 18:50:55 +0000 | [diff] [blame] | 1103 | case ISD::ADD_PARTS: return "add_parts"; |
| 1104 | case ISD::SUB_PARTS: return "sub_parts"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1105 | |
| 1106 | // Conversion operators. |
| 1107 | case ISD::SIGN_EXTEND: return "sign_extend"; |
| 1108 | case ISD::ZERO_EXTEND: return "zero_extend"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1109 | case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg"; |
| 1110 | case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1111 | case ISD::TRUNCATE: return "truncate"; |
| 1112 | case ISD::FP_ROUND: return "fp_round"; |
Chris Lattner | 859157d | 2005-01-15 06:17:04 +0000 | [diff] [blame] | 1113 | case ISD::FP_ROUND_INREG: return "fp_round_inreg"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1114 | case ISD::FP_EXTEND: return "fp_extend"; |
| 1115 | |
| 1116 | case ISD::SINT_TO_FP: return "sint_to_fp"; |
| 1117 | case ISD::UINT_TO_FP: return "uint_to_fp"; |
| 1118 | case ISD::FP_TO_SINT: return "fp_to_sint"; |
| 1119 | case ISD::FP_TO_UINT: return "fp_to_uint"; |
| 1120 | |
| 1121 | // Control flow instructions |
| 1122 | case ISD::BR: return "br"; |
| 1123 | case ISD::BRCOND: return "brcond"; |
| 1124 | case ISD::RET: return "ret"; |
| 1125 | case ISD::CALL: return "call"; |
| 1126 | case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown"; |
| 1127 | case ISD::ADJCALLSTACKUP: return "adjcallstackup"; |
| 1128 | |
| 1129 | // Other operators |
| 1130 | case ISD::LOAD: return "load"; |
| 1131 | case ISD::STORE: return "store"; |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1132 | case ISD::EXTLOAD: return "extload"; |
| 1133 | case ISD::SEXTLOAD: return "sextload"; |
| 1134 | case ISD::ZEXTLOAD: return "zextload"; |
| 1135 | case ISD::TRUNCSTORE: return "truncstore"; |
| 1136 | |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1137 | case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc"; |
| 1138 | case ISD::EXTRACT_ELEMENT: return "extract_element"; |
| 1139 | case ISD::BUILD_PAIR: return "build_pair"; |
Chris Lattner | 4c633e8 | 2005-01-11 05:57:01 +0000 | [diff] [blame] | 1140 | case ISD::MEMSET: return "memset"; |
| 1141 | case ISD::MEMCPY: return "memcpy"; |
| 1142 | case ISD::MEMMOVE: return "memmove"; |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1143 | |
| 1144 | case ISD::SETCC: |
| 1145 | const SetCCSDNode *SetCC = cast<SetCCSDNode>(this); |
| 1146 | switch (SetCC->getCondition()) { |
| 1147 | default: assert(0 && "Unknown setcc condition!"); |
| 1148 | case ISD::SETOEQ: return "setcc:setoeq"; |
| 1149 | case ISD::SETOGT: return "setcc:setogt"; |
| 1150 | case ISD::SETOGE: return "setcc:setoge"; |
| 1151 | case ISD::SETOLT: return "setcc:setolt"; |
| 1152 | case ISD::SETOLE: return "setcc:setole"; |
| 1153 | case ISD::SETONE: return "setcc:setone"; |
| 1154 | |
| 1155 | case ISD::SETO: return "setcc:seto"; |
| 1156 | case ISD::SETUO: return "setcc:setuo"; |
| 1157 | case ISD::SETUEQ: return "setcc:setue"; |
| 1158 | case ISD::SETUGT: return "setcc:setugt"; |
| 1159 | case ISD::SETUGE: return "setcc:setuge"; |
| 1160 | case ISD::SETULT: return "setcc:setult"; |
| 1161 | case ISD::SETULE: return "setcc:setule"; |
| 1162 | case ISD::SETUNE: return "setcc:setune"; |
| 1163 | |
| 1164 | case ISD::SETEQ: return "setcc:seteq"; |
| 1165 | case ISD::SETGT: return "setcc:setgt"; |
| 1166 | case ISD::SETGE: return "setcc:setge"; |
| 1167 | case ISD::SETLT: return "setcc:setlt"; |
| 1168 | case ISD::SETLE: return "setcc:setle"; |
| 1169 | case ISD::SETNE: return "setcc:setne"; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1173 | |
| 1174 | void SDNode::dump() const { |
| 1175 | std::cerr << (void*)this << ": "; |
| 1176 | |
| 1177 | for (unsigned i = 0, e = getNumValues(); i != e; ++i) { |
| 1178 | if (i) std::cerr << ","; |
Chris Lattner | 4ea6924 | 2005-01-15 07:14:32 +0000 | [diff] [blame] | 1179 | if (getValueType(i) == MVT::Other) |
| 1180 | std::cerr << "ch"; |
| 1181 | else |
| 1182 | std::cerr << MVT::getValueTypeString(getValueType(i)); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1183 | } |
Chris Lattner | d75f19f | 2005-01-10 23:25:25 +0000 | [diff] [blame] | 1184 | std::cerr << " = " << getOperationName(); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1185 | |
| 1186 | std::cerr << " "; |
| 1187 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { |
| 1188 | if (i) std::cerr << ", "; |
| 1189 | std::cerr << (void*)getOperand(i).Val; |
| 1190 | if (unsigned RN = getOperand(i).ResNo) |
| 1191 | std::cerr << ":" << RN; |
| 1192 | } |
| 1193 | |
| 1194 | if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) { |
| 1195 | std::cerr << "<" << CSDN->getValue() << ">"; |
| 1196 | } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) { |
| 1197 | std::cerr << "<" << CSDN->getValue() << ">"; |
| 1198 | } else if (const GlobalAddressSDNode *GADN = |
| 1199 | dyn_cast<GlobalAddressSDNode>(this)) { |
| 1200 | std::cerr << "<"; |
| 1201 | WriteAsOperand(std::cerr, GADN->getGlobal()) << ">"; |
| 1202 | } else if (const FrameIndexSDNode *FIDN = |
| 1203 | dyn_cast<FrameIndexSDNode>(this)) { |
| 1204 | std::cerr << "<" << FIDN->getIndex() << ">"; |
| 1205 | } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){ |
| 1206 | std::cerr << "<" << CP->getIndex() << ">"; |
| 1207 | } else if (const BasicBlockSDNode *BBDN = |
| 1208 | dyn_cast<BasicBlockSDNode>(this)) { |
| 1209 | std::cerr << "<"; |
| 1210 | const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock(); |
| 1211 | if (LBB) |
| 1212 | std::cerr << LBB->getName() << " "; |
| 1213 | std::cerr << (const void*)BBDN->getBasicBlock() << ">"; |
Chris Lattner | 18c2f13 | 2005-01-13 20:50:02 +0000 | [diff] [blame] | 1214 | } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) { |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1215 | std::cerr << "<reg #" << C2V->getReg() << ">"; |
| 1216 | } else if (const ExternalSymbolSDNode *ES = |
| 1217 | dyn_cast<ExternalSymbolSDNode>(this)) { |
| 1218 | std::cerr << "'" << ES->getSymbol() << "'"; |
Chris Lattner | 8a389bb | 2005-01-15 21:11:37 +0000 | [diff] [blame] | 1219 | } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) { |
| 1220 | std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType()); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1221 | } |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 1224 | static void DumpNodes(SDNode *N, unsigned indent) { |
| 1225 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 1226 | if (N->getOperand(i).Val->hasOneUse()) |
| 1227 | DumpNodes(N->getOperand(i).Val, indent+2); |
| 1228 | else |
| 1229 | std::cerr << "\n" << std::string(indent+2, ' ') |
| 1230 | << (void*)N->getOperand(i).Val << ": <multiple use>"; |
| 1231 | |
| 1232 | |
| 1233 | std::cerr << "\n" << std::string(indent, ' '); |
| 1234 | N->dump(); |
| 1235 | } |
| 1236 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1237 | void SelectionDAG::dump() const { |
| 1238 | std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:"; |
Chris Lattner | 49d2471 | 2005-01-09 20:26:36 +0000 | [diff] [blame] | 1239 | std::vector<SDNode*> Nodes(AllNodes); |
| 1240 | std::sort(Nodes.begin(), Nodes.end()); |
| 1241 | |
| 1242 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 1243 | if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val) |
| 1244 | DumpNodes(Nodes[i], 2); |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1245 | } |
Chris Lattner | ea946cd | 2005-01-09 20:38:33 +0000 | [diff] [blame] | 1246 | |
| 1247 | DumpNodes(getRoot().Val, 2); |
| 1248 | |
Chris Lattner | c3aae25 | 2005-01-07 07:46:32 +0000 | [diff] [blame] | 1249 | std::cerr << "\n\n"; |
| 1250 | } |
| 1251 | |