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