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