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