Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +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 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 20 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 24 | #include <iostream> |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 25 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 28 | #ifndef NDEBUG |
| 29 | static cl::opt<bool> |
| 30 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 31 | cl::desc("Pop up a window to show dags before legalize")); |
| 32 | #else |
| 33 | static const bool ViewLegalizeDAGs = 0; |
| 34 | #endif |
| 35 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 38 | /// hacks on it until the target machine can handle it. This involves |
| 39 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 40 | /// large sizes or splitting up large values into small values) as well as |
| 41 | /// eliminating operations the machine cannot handle. |
| 42 | /// |
| 43 | /// This code also does a small amount of optimization and recognition of idioms |
| 44 | /// as part of its processing. For example, if a target does not support a |
| 45 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 46 | /// will attempt merge setcc and brc instructions into brcc's. |
| 47 | /// |
| 48 | namespace { |
| 49 | class SelectionDAGLegalize { |
| 50 | TargetLowering &TLI; |
| 51 | SelectionDAG &DAG; |
| 52 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 53 | // Libcall insertion helpers. |
| 54 | |
| 55 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 56 | /// legalized. We use this to ensure that calls are properly serialized |
| 57 | /// against each other, including inserted libcalls. |
| 58 | SDOperand LastCALLSEQ_END; |
| 59 | |
| 60 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 61 | /// helpful assertions that a libcall isn't created while another call is |
| 62 | /// being legalized (which could lead to non-serialized call sequences). |
| 63 | bool IsLegalizingCall; |
| 64 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 65 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 66 | Legal, // The target natively supports this operation. |
| 67 | Promote, // This operation should be executed in a larger type. |
| 68 | Expand, // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 69 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 71 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 72 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 73 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 74 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 76 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 77 | /// than one use, this map indicates what regularized operand to use. This |
| 78 | /// allows us to avoid legalizing the same thing more than once. |
| 79 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 80 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 81 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 82 | /// than one use, this map indicates what promoted value to use. This allows |
| 83 | /// us to avoid promoting the same thing more than once. |
| 84 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 85 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 86 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 87 | /// which which operands are the expanded version of the input. This allows |
| 88 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 89 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 90 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 91 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 92 | /// which which operands are the split version of the input. This allows us |
| 93 | /// to avoid splitting the same node more than once. |
| 94 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 95 | |
| 96 | /// PackedNodes - For nodes that need to be packed from MVT::Vector types to |
| 97 | /// concrete packed types, this contains the mapping of ones we have already |
| 98 | /// processed to the result. |
| 99 | std::map<SDOperand, SDOperand> PackedNodes; |
| 100 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 101 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 102 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 103 | // If someone requests legalization of the new node, return itself. |
| 104 | if (From != To) |
| 105 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 107 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 108 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 109 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 110 | // If someone requests legalization of the new node, return itself. |
| 111 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 114 | public: |
| 115 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 116 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 118 | /// getTypeAction - Return how we should legalize values of this type, either |
| 119 | /// it is already legal or we need to expand it into multiple registers of |
| 120 | /// smaller integer type, or we need to promote it to a larger type. |
| 121 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 122 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /// isTypeLegal - Return true if this type is legal on this target. |
| 126 | /// |
| 127 | bool isTypeLegal(MVT::ValueType VT) const { |
| 128 | return getTypeAction(VT) == Legal; |
| 129 | } |
| 130 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 131 | void LegalizeDAG(); |
| 132 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 133 | private: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 134 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 135 | /// appropriate for its type. |
| 136 | void HandleOp(SDOperand Op); |
| 137 | |
| 138 | /// LegalizeOp - We know that the specified value has a legal type. |
| 139 | /// Recursively ensure that the operands have legal types, then return the |
| 140 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 141 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 142 | |
| 143 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 144 | /// promote it to compute the value into a larger type. The produced value |
| 145 | /// will have the correct bits for the low portion of the register, but no |
| 146 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 147 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 148 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 149 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 150 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 151 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 152 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 153 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 154 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 155 | /// types. |
| 156 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 157 | |
| 158 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 159 | /// two smaller values of MVT::Vector type. |
| 160 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 161 | |
| 162 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 163 | /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When |
| 164 | /// this is called, we know that PackedVT is the right type for the result and |
| 165 | /// we know that this type is legal for the target. |
| 166 | SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT); |
| 167 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 168 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 169 | /// specified mask and type. Targets can specify exactly which masks they |
| 170 | /// support and the code generator is tasked with not creating illegal masks. |
| 171 | /// |
| 172 | /// Note that this will also return true for shuffles that are promoted to a |
| 173 | /// different type. |
| 174 | /// |
| 175 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 176 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 177 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 178 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 179 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest); |
| 180 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 181 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 182 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 183 | SDOperand CreateStackTemporary(MVT::ValueType VT); |
| 184 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 185 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, |
| 186 | SDOperand &Hi); |
| 187 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 188 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 190 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 191 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 192 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 193 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 194 | SDOperand LegalOp, |
| 195 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 196 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 197 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 198 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 199 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 201 | SDOperand ExpandBSWAP(SDOperand Op); |
| 202 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 203 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 204 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 205 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 206 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 208 | SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 209 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 211 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 212 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 213 | } |
| 214 | }; |
| 215 | } |
| 216 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 217 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 218 | /// specified mask and type. Targets can specify exactly which masks they |
| 219 | /// support and the code generator is tasked with not creating illegal masks. |
| 220 | /// |
| 221 | /// Note that this will also return true for shuffles that are promoted to a |
| 222 | /// different type. |
| 223 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 224 | SDOperand Mask) const { |
| 225 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 226 | default: return 0; |
| 227 | case TargetLowering::Legal: |
| 228 | case TargetLowering::Custom: |
| 229 | break; |
| 230 | case TargetLowering::Promote: { |
| 231 | // If this is promoted to a different type, convert the shuffle mask and |
| 232 | // ask if it is legal in the promoted type! |
| 233 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 234 | |
| 235 | // If we changed # elements, change the shuffle mask. |
| 236 | unsigned NumEltsGrowth = |
| 237 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 238 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 239 | if (NumEltsGrowth > 1) { |
| 240 | // Renumber the elements. |
| 241 | std::vector<SDOperand> Ops; |
| 242 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 243 | SDOperand InOp = Mask.getOperand(i); |
| 244 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 245 | if (InOp.getOpcode() == ISD::UNDEF) |
| 246 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 247 | else { |
| 248 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 249 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, Ops); |
| 254 | } |
| 255 | VT = NVT; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 260 | } |
| 261 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 262 | /// getScalarizedOpcode - Return the scalar opcode that corresponds to the |
| 263 | /// specified vector opcode. |
Chris Lattner | b89175f | 2005-11-19 05:51:46 +0000 | [diff] [blame] | 264 | static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 265 | switch (VecOp) { |
| 266 | default: assert(0 && "Don't know how to scalarize this opcode!"); |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 267 | case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; |
| 268 | case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; |
| 269 | case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; |
| 270 | case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV; |
| 271 | case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV; |
| 272 | case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0; |
| 273 | case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0; |
| 274 | case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 278 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 279 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 280 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 281 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 282 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 285 | /// ComputeTopDownOrdering - Add the specified node to the Order list if it has |
| 286 | /// not been visited yet and if all of its operands have already been visited. |
| 287 | static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, |
| 288 | std::map<SDNode*, unsigned> &Visited) { |
| 289 | if (++Visited[N] != N->getNumOperands()) |
| 290 | return; // Haven't visited all operands yet |
| 291 | |
| 292 | Order.push_back(N); |
| 293 | |
| 294 | if (N->hasOneUse()) { // Tail recurse in common case. |
| 295 | ComputeTopDownOrdering(*N->use_begin(), Order, Visited); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // Now that we have N in, add anything that uses it if all of their operands |
| 300 | // are now done. |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 301 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) |
| 302 | ComputeTopDownOrdering(*UI, Order, Visited); |
| 303 | } |
| 304 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 306 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 307 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 308 | IsLegalizingCall = false; |
| 309 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 310 | // The legalize process is inherently a bottom-up recursive process (users |
| 311 | // legalize their uses before themselves). Given infinite stack space, we |
| 312 | // could just start legalizing on the root and traverse the whole graph. In |
| 313 | // practice however, this causes us to run out of stack space on large basic |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 314 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 315 | // node is only legalized after all of its operands are legalized. |
| 316 | std::map<SDNode*, unsigned> Visited; |
| 317 | std::vector<SDNode*> Order; |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 319 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 320 | // entry node) that have no operands. |
| 321 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 322 | E = DAG.allnodes_end(); I != E; ++I) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 323 | if (I->getNumOperands() == 0) { |
| 324 | Visited[I] = 0 - 1U; |
| 325 | ComputeTopDownOrdering(I, Order, Visited); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 329 | assert(Order.size() == Visited.size() && |
| 330 | Order.size() == |
| 331 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 332 | "Error: DAG is cyclic!"); |
| 333 | Visited.clear(); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 334 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 335 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 336 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 337 | |
| 338 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 339 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 340 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 341 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 342 | |
| 343 | ExpandedNodes.clear(); |
| 344 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 345 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 346 | SplitNodes.clear(); |
| 347 | PackedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 348 | |
| 349 | // Remove dead nodes now. |
Chris Lattner | 62fd269 | 2005-01-07 21:09:37 +0000 | [diff] [blame] | 350 | DAG.RemoveDeadNodes(OldRoot.Val); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 353 | |
| 354 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 355 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 356 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 357 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 358 | return Node; |
| 359 | if (Node->use_empty()) |
| 360 | return 0; // No CallSeqEnd |
| 361 | |
| 362 | // The chain is usually at the end. |
| 363 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 364 | if (TheChain.getValueType() != MVT::Other) { |
| 365 | // Sometimes it's at the beginning. |
| 366 | TheChain = SDOperand(Node, 0); |
| 367 | if (TheChain.getValueType() != MVT::Other) { |
| 368 | // Otherwise, hunt for it. |
| 369 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 370 | if (Node->getValueType(i) == MVT::Other) { |
| 371 | TheChain = SDOperand(Node, i); |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | // Otherwise, we walked into a node without a chain. |
| 376 | if (TheChain.getValueType() != MVT::Other) |
| 377 | return 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 382 | E = Node->use_end(); UI != E; ++UI) { |
| 383 | |
| 384 | // Make sure to only follow users of our token chain. |
| 385 | SDNode *User = *UI; |
| 386 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 387 | if (User->getOperand(i) == TheChain) |
| 388 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 389 | return Result; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 395 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 396 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 397 | assert(Node && "Didn't find callseq_start for a call??"); |
| 398 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 399 | |
| 400 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 401 | "Node doesn't have a token chain argument!"); |
| 402 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 403 | } |
| 404 | |
| 405 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 406 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 407 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
| 408 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, |
| 409 | SDNode *Dest) { |
| 410 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 411 | |
| 412 | // If the first result of this node has been already legalized, then it cannot |
| 413 | // reach N. |
| 414 | switch (getTypeAction(N->getValueType(0))) { |
| 415 | case Legal: |
| 416 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 417 | break; |
| 418 | case Promote: |
| 419 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 420 | break; |
| 421 | case Expand: |
| 422 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | // Okay, this node has not already been legalized. Check and legalize all |
| 427 | // operands. If none lead to Dest, then we can legalize this node. |
| 428 | bool OperandsLeadToDest = false; |
| 429 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 430 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
| 431 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest); |
| 432 | |
| 433 | if (OperandsLeadToDest) return true; |
| 434 | |
| 435 | // Okay, this node looks safe, legalize it and return false. |
| 436 | switch (getTypeAction(N->getValueType(0))) { |
| 437 | case Legal: |
| 438 | LegalizeOp(SDOperand(N, 0)); |
| 439 | break; |
| 440 | case Promote: |
| 441 | PromoteOp(SDOperand(N, 0)); |
| 442 | break; |
| 443 | case Expand: { |
| 444 | SDOperand X, Y; |
| 445 | ExpandOp(SDOperand(N, 0), X, Y); |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 452 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 453 | /// appropriate for its type. |
| 454 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
| 455 | switch (getTypeAction(Op.getValueType())) { |
| 456 | default: assert(0 && "Bad type action!"); |
| 457 | case Legal: LegalizeOp(Op); break; |
| 458 | case Promote: PromoteOp(Op); break; |
| 459 | case Expand: |
| 460 | if (Op.getValueType() != MVT::Vector) { |
| 461 | SDOperand X, Y; |
| 462 | ExpandOp(Op, X, Y); |
| 463 | } else { |
| 464 | SDNode *N = Op.Val; |
| 465 | unsigned NumOps = N->getNumOperands(); |
| 466 | unsigned NumElements = |
| 467 | cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue(); |
| 468 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT(); |
| 469 | MVT::ValueType PackedVT = getVectorType(EVT, NumElements); |
| 470 | if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) { |
| 471 | // In the common case, this is a legal vector type, convert it to the |
| 472 | // packed operation and type now. |
| 473 | PackVectorOp(Op, PackedVT); |
| 474 | } else if (NumElements == 1) { |
| 475 | // Otherwise, if this is a single element vector, convert it to a |
| 476 | // scalar operation. |
| 477 | PackVectorOp(Op, EVT); |
| 478 | } else { |
| 479 | // Otherwise, this is a multiple element vector that isn't supported. |
| 480 | // Split it in half and legalize both parts. |
| 481 | SDOperand X, Y; |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 482 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 488 | |
| 489 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 490 | /// LegalizeOp - We know that the specified value has a legal type. |
| 491 | /// Recursively ensure that the operands have legal types, then return the |
| 492 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 493 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 494 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 495 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 496 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 498 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 499 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 500 | if (Node->getNumValues() > 1) { |
| 501 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 502 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 503 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 504 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 505 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 506 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 510 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 511 | // means that we always must cache transformed nodes. |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 512 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 513 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 514 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 515 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 516 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 517 | bool isCustom = false; |
| 518 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 519 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 520 | case ISD::FrameIndex: |
| 521 | case ISD::EntryToken: |
| 522 | case ISD::Register: |
| 523 | case ISD::BasicBlock: |
| 524 | case ISD::TargetFrameIndex: |
| 525 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 526 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 527 | case ISD::TargetConstantPool: |
| 528 | case ISD::TargetGlobalAddress: |
| 529 | case ISD::TargetExternalSymbol: |
| 530 | case ISD::VALUETYPE: |
| 531 | case ISD::SRCVALUE: |
| 532 | case ISD::STRING: |
| 533 | case ISD::CONDCODE: |
| 534 | // Primitives must all be legal. |
| 535 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 536 | "This must be legal!"); |
| 537 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 538 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 539 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 540 | // If this is a target node, legalize it by legalizing the operands then |
| 541 | // passing it through. |
| 542 | std::vector<SDOperand> Ops; |
| 543 | bool Changed = false; |
| 544 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 545 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 546 | Changed = Changed || Node->getOperand(i) != Ops.back(); |
| 547 | } |
| 548 | if (Changed) |
| 549 | if (Node->getNumValues() == 1) |
| 550 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); |
| 551 | else { |
| 552 | std::vector<MVT::ValueType> VTs(Node->value_begin(), |
| 553 | Node->value_end()); |
| 554 | Result = DAG.getNode(Node->getOpcode(), VTs, Ops); |
| 555 | } |
| 556 | |
| 557 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 558 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 559 | return Result.getValue(Op.ResNo); |
| 560 | } |
| 561 | // Otherwise this is an unhandled builtin node. splat. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 562 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 563 | assert(0 && "Do not know how to legalize this operator!"); |
| 564 | abort(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 565 | case ISD::GlobalAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 566 | case ISD::ExternalSymbol: |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 567 | case ISD::ConstantPool: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 568 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 569 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 570 | case TargetLowering::Custom: |
| 571 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 572 | if (Tmp1.Val) Result = Tmp1; |
| 573 | // FALLTHROUGH if the target doesn't want to lower this op after all. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 574 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 575 | break; |
| 576 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 577 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 578 | case ISD::AssertSext: |
| 579 | case ISD::AssertZext: |
| 580 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 581 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 582 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 583 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 584 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 585 | Result = Node->getOperand(Op.ResNo); |
| 586 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 587 | case ISD::CopyFromReg: |
| 588 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 589 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 590 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 591 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 592 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 593 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 594 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 595 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 596 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 597 | } else { |
| 598 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 599 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 600 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 601 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 602 | // Since CopyFromReg produces two values, make sure to remember that we |
| 603 | // legalized both of them. |
| 604 | AddLegalizedOperand(Op.getValue(0), Result); |
| 605 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 606 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 607 | case ISD::UNDEF: { |
| 608 | MVT::ValueType VT = Op.getValueType(); |
| 609 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 610 | default: assert(0 && "This action is not supported yet!"); |
| 611 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 612 | if (MVT::isInteger(VT)) |
| 613 | Result = DAG.getConstant(0, VT); |
| 614 | else if (MVT::isFloatingPoint(VT)) |
| 615 | Result = DAG.getConstantFP(0, VT); |
| 616 | else |
| 617 | assert(0 && "Unknown value type!"); |
| 618 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 619 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | break; |
| 623 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 625 | case ISD::INTRINSIC_W_CHAIN: |
| 626 | case ISD::INTRINSIC_WO_CHAIN: |
| 627 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 628 | std::vector<SDOperand> Ops; |
| 629 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 630 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 631 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 632 | |
| 633 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 634 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 635 | TargetLowering::Custom) { |
| 636 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 637 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (Result.Val->getNumValues() == 1) break; |
| 641 | |
| 642 | // Must have return value and chain result. |
| 643 | assert(Result.Val->getNumValues() == 2 && |
| 644 | "Cannot return more than two values!"); |
| 645 | |
| 646 | // Since loads produce two values, make sure to remember that we |
| 647 | // legalized both of them. |
| 648 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 649 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 650 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 651 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 652 | |
| 653 | case ISD::LOCATION: |
| 654 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 655 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 656 | |
| 657 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 658 | case TargetLowering::Promote: |
| 659 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 660 | case TargetLowering::Expand: { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 661 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 662 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
| 663 | bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other); |
| 664 | |
| 665 | if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) { |
| 666 | const std::string &FName = |
| 667 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 668 | const std::string &DirName = |
| 669 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 670 | unsigned SrcFile = DebugInfo->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 671 | |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 672 | std::vector<SDOperand> Ops; |
| 673 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 674 | SDOperand LineOp = Node->getOperand(1); |
| 675 | SDOperand ColOp = Node->getOperand(2); |
| 676 | |
| 677 | if (useDEBUG_LOC) { |
| 678 | Ops.push_back(LineOp); // line # |
| 679 | Ops.push_back(ColOp); // col # |
| 680 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
| 681 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops); |
| 682 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 683 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 684 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 685 | unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile); |
| 686 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
| 687 | Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops); |
| 688 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 689 | } else { |
| 690 | Result = Tmp1; // chain |
| 691 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 692 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 693 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 694 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 695 | if (Tmp1 != Node->getOperand(0) || |
| 696 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 697 | std::vector<SDOperand> Ops; |
| 698 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 699 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 700 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 701 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 702 | } else { |
| 703 | // Otherwise promote them. |
| 704 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 705 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 706 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 707 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 708 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 709 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 710 | } |
| 711 | break; |
| 712 | } |
| 713 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 714 | |
| 715 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 716 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 717 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 718 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 719 | case TargetLowering::Legal: |
| 720 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 721 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 722 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 723 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 724 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 725 | break; |
| 726 | } |
| 727 | break; |
| 728 | |
| 729 | case ISD::DEBUG_LABEL: |
| 730 | assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!"); |
| 731 | switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 732 | default: assert(0 && "This action is not supported yet!"); |
| 733 | case TargetLowering::Legal: |
| 734 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 735 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 736 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 737 | break; |
| 738 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 739 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 741 | case ISD::Constant: |
| 742 | // We know we don't need to expand constants here, constants only have one |
| 743 | // value and we check that it is fine above. |
| 744 | |
| 745 | // FIXME: Maybe we should handle things like targets that don't support full |
| 746 | // 32-bit immediates? |
| 747 | break; |
| 748 | case ISD::ConstantFP: { |
| 749 | // Spill FP immediates to the constant pool if the target cannot directly |
| 750 | // codegen them. Targets often have some immediate values that can be |
| 751 | // efficiently generated into an FP register without a load. We explicitly |
| 752 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 753 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 754 | |
| 755 | // Check to see if this FP immediate is already legal. |
| 756 | bool isLegal = false; |
| 757 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 758 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 759 | if (CFP->isExactlyValue(*I)) { |
| 760 | isLegal = true; |
| 761 | break; |
| 762 | } |
| 763 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 764 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 765 | if (isLegal) { |
| 766 | Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 771 | default: assert(0 && "This action is not supported yet!"); |
| 772 | case TargetLowering::Custom: |
| 773 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 774 | if (Tmp3.Val) { |
| 775 | Result = Tmp3; |
| 776 | break; |
| 777 | } |
| 778 | // FALLTHROUGH |
| 779 | case TargetLowering::Expand: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 780 | // Otherwise we need to spill the constant to memory. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 781 | bool Extend = false; |
| 782 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 783 | // If a FP immediate is precise when represented as a float and if the |
| 784 | // target can do an extending load from float to double, we put it into |
| 785 | // the constant pool as a float, even if it's is statically typed as a |
| 786 | // double. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 787 | MVT::ValueType VT = CFP->getValueType(0); |
| 788 | bool isDouble = VT == MVT::f64; |
| 789 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 790 | Type::FloatTy, CFP->getValue()); |
Chris Lattner | 99939d3 | 2005-01-28 22:58:25 +0000 | [diff] [blame] | 791 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 792 | // Only do this if the target has a native EXTLOAD instruction from |
| 793 | // f32. |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 794 | TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 795 | LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); |
| 796 | VT = MVT::f32; |
| 797 | Extend = true; |
| 798 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 800 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 801 | if (Extend) { |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 802 | Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 803 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 804 | } else { |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 805 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 806 | DAG.getSrcValue(NULL)); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 807 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 808 | } |
| 809 | break; |
| 810 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 811 | case ISD::TokenFactor: |
| 812 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 813 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 814 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 815 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 816 | } else if (Node->getNumOperands() == 3) { |
| 817 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 818 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 819 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 820 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 821 | } else { |
| 822 | std::vector<SDOperand> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 823 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 824 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 825 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 826 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 827 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 828 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 829 | |
| 830 | case ISD::FORMAL_ARGUMENTS: |
| 831 | // The only option for this is to custom lower it. |
| 832 | Result = TLI.LowerOperation(Result, DAG); |
| 833 | assert(Result.Val && "Target didn't custom lower ISD::FORMAL_ARGUMENTS!"); |
| 834 | break; |
| 835 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 836 | case ISD::BUILD_VECTOR: |
| 837 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 838 | default: assert(0 && "This action is not supported yet!"); |
| 839 | case TargetLowering::Custom: |
| 840 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 841 | if (Tmp3.Val) { |
| 842 | Result = Tmp3; |
| 843 | break; |
| 844 | } |
| 845 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 846 | case TargetLowering::Expand: |
| 847 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 848 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 849 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 850 | break; |
| 851 | case ISD::INSERT_VECTOR_ELT: |
| 852 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 853 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 854 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 855 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 856 | |
| 857 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 858 | Node->getValueType(0))) { |
| 859 | default: assert(0 && "This action is not supported yet!"); |
| 860 | case TargetLowering::Legal: |
| 861 | break; |
| 862 | case TargetLowering::Custom: |
| 863 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 864 | if (Tmp3.Val) { |
| 865 | Result = Tmp3; |
| 866 | break; |
| 867 | } |
| 868 | // FALLTHROUGH |
| 869 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame^] | 870 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 871 | // then a shuffle that inserts it into the right position in the vector. |
| 872 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 873 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 874 | Tmp1.getValueType(), Tmp2); |
| 875 | |
| 876 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 877 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
| 878 | MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT); |
| 879 | |
| 880 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 881 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 882 | // the RHS. |
| 883 | std::vector<SDOperand> ShufOps; |
| 884 | for (unsigned i = 0; i != NumElts; ++i) { |
| 885 | if (i != InsertPos->getValue()) |
| 886 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 887 | else |
| 888 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 889 | } |
| 890 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,ShufOps); |
| 891 | |
| 892 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 893 | Tmp1, ScVec, ShufMask); |
| 894 | Result = LegalizeOp(Result); |
| 895 | break; |
| 896 | } |
| 897 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 898 | // If the target doesn't support this, we have to spill the input vector |
| 899 | // to a temporary stack slot, update the element, then reload it. This is |
| 900 | // badness. We could also load the value into a vector register (either |
| 901 | // with a "move to register" or "extload into register" instruction, then |
| 902 | // permute it into place, if the idx is a constant and if the idx is |
| 903 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 904 | MVT::ValueType VT = Tmp1.getValueType(); |
| 905 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 906 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 907 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 908 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 909 | // Store the vector. |
| 910 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 911 | Tmp1, StackPtr, DAG.getSrcValue(NULL)); |
| 912 | |
| 913 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 914 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 915 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 916 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 917 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 918 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 919 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 920 | // Store the scalar value. |
| 921 | Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch, |
| 922 | Tmp2, StackPtr2, DAG.getSrcValue(NULL)); |
| 923 | // Load the updated vector. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 924 | Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | } |
| 928 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 929 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 930 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 931 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 932 | break; |
| 933 | } |
| 934 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 935 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 936 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 937 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 938 | Node->getValueType(0))) { |
| 939 | default: assert(0 && "This action is not supported yet!"); |
| 940 | case TargetLowering::Legal: |
| 941 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 942 | case TargetLowering::Custom: |
| 943 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 944 | if (Tmp3.Val) { |
| 945 | Result = Tmp3; |
| 946 | break; |
| 947 | } |
| 948 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 949 | case TargetLowering::Expand: |
| 950 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 951 | break; |
| 952 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 953 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 954 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 955 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 956 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 957 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 958 | |
| 959 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 960 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 961 | default: assert(0 && "Unknown operation action!"); |
| 962 | case TargetLowering::Legal: |
| 963 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 964 | "vector shuffle should not be created if not legal!"); |
| 965 | break; |
| 966 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 967 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 968 | if (Tmp3.Val) { |
| 969 | Result = Tmp3; |
| 970 | break; |
| 971 | } |
| 972 | // FALLTHROUGH |
| 973 | case TargetLowering::Expand: { |
| 974 | MVT::ValueType VT = Node->getValueType(0); |
| 975 | MVT::ValueType EltVT = MVT::getVectorBaseType(VT); |
| 976 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 977 | SDOperand Mask = Node->getOperand(2); |
| 978 | unsigned NumElems = Mask.getNumOperands(); |
| 979 | std::vector<SDOperand> Ops; |
| 980 | for (unsigned i = 0; i != NumElems; ++i) { |
| 981 | SDOperand Arg = Mask.getOperand(i); |
| 982 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 983 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 984 | } else { |
| 985 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 986 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 987 | if (Idx < NumElems) |
| 988 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 989 | DAG.getConstant(Idx, PtrVT))); |
| 990 | else |
| 991 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 992 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 993 | } |
| 994 | } |
| 995 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 996 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 997 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 998 | case TargetLowering::Promote: { |
| 999 | // Change base type to a different vector type. |
| 1000 | MVT::ValueType OVT = Node->getValueType(0); |
| 1001 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1002 | |
| 1003 | // Cast the two input vectors. |
| 1004 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1005 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1006 | |
| 1007 | // Convert the shuffle mask to the right # elements. |
| 1008 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1009 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1010 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1011 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1012 | break; |
| 1013 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1014 | } |
| 1015 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1016 | |
| 1017 | case ISD::EXTRACT_VECTOR_ELT: |
| 1018 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1019 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1020 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1021 | |
| 1022 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, |
| 1023 | Tmp1.getValueType())) { |
| 1024 | default: assert(0 && "This action is not supported yet!"); |
| 1025 | case TargetLowering::Legal: |
| 1026 | break; |
| 1027 | case TargetLowering::Custom: |
| 1028 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1029 | if (Tmp3.Val) { |
| 1030 | Result = Tmp3; |
| 1031 | break; |
| 1032 | } |
| 1033 | // FALLTHROUGH |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 1034 | case TargetLowering::Expand: |
| 1035 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | } |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1038 | break; |
| 1039 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 1040 | case ISD::VEXTRACT_VECTOR_ELT: |
| 1041 | Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1042 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1044 | case ISD::CALLSEQ_START: { |
| 1045 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1046 | |
| 1047 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1048 | // to this call start. This ensures that any libcalls that need be inserted |
| 1049 | // are inserted *before* the CALLSEQ_START. |
| 1050 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
| 1051 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node); |
| 1052 | |
| 1053 | // Now that we legalized all of the inputs (which may have inserted |
| 1054 | // libcalls) create the new CALLSEQ_START node. |
| 1055 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1056 | |
| 1057 | // Merge in the last call, to ensure that this call start after the last |
| 1058 | // call ended. |
| 1059 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1060 | Tmp1 = LegalizeOp(Tmp1); |
| 1061 | |
| 1062 | // Do not try to legalize the target-specific arguments (#1+). |
| 1063 | if (Tmp1 != Node->getOperand(0)) { |
| 1064 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1065 | Ops[0] = Tmp1; |
| 1066 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1067 | } |
| 1068 | |
| 1069 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1070 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1071 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1072 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1073 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1074 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1075 | // sequence have been legalized, legalize the call itself. During this |
| 1076 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1077 | // can overlap. |
| 1078 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1079 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1080 | // Note that we are selecting this call! |
| 1081 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1082 | IsLegalizingCall = true; |
| 1083 | |
| 1084 | // Legalize the call, starting from the CALLSEQ_END. |
| 1085 | LegalizeOp(LastCALLSEQ_END); |
| 1086 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1087 | return Result; |
| 1088 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1089 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1090 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1091 | // will cause this node to be legalized as well as handling libcalls right. |
| 1092 | if (LastCALLSEQ_END.Val != Node) { |
| 1093 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
| 1094 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 1095 | assert(I != LegalizedNodes.end() && |
| 1096 | "Legalizing the call start should have legalized this node!"); |
| 1097 | return I->second; |
| 1098 | } |
| 1099 | |
| 1100 | // Otherwise, the call start has been legalized and everything is going |
| 1101 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1102 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1103 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1104 | // an optional flag input. |
| 1105 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1106 | if (Tmp1 != Node->getOperand(0)) { |
| 1107 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1108 | Ops[0] = Tmp1; |
| 1109 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1110 | } |
| 1111 | } else { |
| 1112 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1113 | if (Tmp1 != Node->getOperand(0) || |
| 1114 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
| 1115 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1116 | Ops[0] = Tmp1; |
| 1117 | Ops.back() = Tmp2; |
| 1118 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1119 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1120 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1121 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1122 | // This finishes up call legalization. |
| 1123 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1124 | |
| 1125 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1126 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1127 | if (Node->getNumValues() == 2) |
| 1128 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1129 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1130 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1131 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1132 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1133 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1134 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1136 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1137 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1138 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1139 | Node->getValueType(0))) { |
| 1140 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1141 | case TargetLowering::Expand: { |
| 1142 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1143 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1144 | " not tell us which reg is the stack pointer!"); |
| 1145 | SDOperand Chain = Tmp1.getOperand(0); |
| 1146 | SDOperand Size = Tmp2.getOperand(1); |
| 1147 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 1148 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 1149 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1150 | Tmp1 = LegalizeOp(Tmp1); |
| 1151 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1155 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1156 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1157 | Tmp1 = LegalizeOp(Tmp3); |
| 1158 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1159 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1160 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1161 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1162 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1164 | // Since this op produce two values, make sure to remember that we |
| 1165 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1166 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1167 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1168 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1169 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1170 | case ISD::INLINEASM: |
| 1171 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain. |
| 1172 | Tmp2 = Node->getOperand(Node->getNumOperands()-1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1173 | if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists. |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1174 | Tmp2 = Tmp3 = SDOperand(0, 0); |
| 1175 | else |
| 1176 | Tmp3 = LegalizeOp(Tmp2); |
| 1177 | |
| 1178 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) { |
| 1179 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1180 | Ops[0] = Tmp1; |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1181 | if (Tmp3.Val) Ops.back() = Tmp3; |
| 1182 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | // INLINE asm returns a chain and flag, make sure to add both to the map. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1186 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1187 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1188 | return Result.getValue(Op.ResNo); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1189 | case ISD::BR: |
| 1190 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1191 | // Ensure that libcalls are emitted before a branch. |
| 1192 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1193 | Tmp1 = LegalizeOp(Tmp1); |
| 1194 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1195 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1196 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1197 | break; |
| 1198 | |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1199 | case ISD::BRCOND: |
| 1200 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1201 | // Ensure that libcalls are emitted before a return. |
| 1202 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1203 | Tmp1 = LegalizeOp(Tmp1); |
| 1204 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1205 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1206 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1207 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1208 | case Legal: |
| 1209 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1210 | break; |
| 1211 | case Promote: |
| 1212 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 1213 | break; |
| 1214 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1217 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1218 | |
| 1219 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1220 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1221 | case TargetLowering::Legal: break; |
| 1222 | case TargetLowering::Custom: |
| 1223 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1224 | if (Tmp1.Val) Result = Tmp1; |
| 1225 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1226 | case TargetLowering::Expand: |
| 1227 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1228 | // Node. |
| 1229 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1230 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1231 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1232 | Node->getOperand(2)); |
| 1233 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1234 | // Make sure the condition is either zero or one. It may have been |
| 1235 | // promoted from something else. |
Chris Lattner | 19c5c4c | 2006-01-31 05:04:52 +0000 | [diff] [blame] | 1236 | unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType()); |
| 1237 | if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1)) |
| 1238 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1239 | |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1240 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1241 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1242 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1243 | Node->getOperand(2)); |
| 1244 | } |
| 1245 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1246 | } |
| 1247 | break; |
| 1248 | case ISD::BR_CC: |
| 1249 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1250 | // Ensure that libcalls are emitted before a branch. |
| 1251 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1252 | Tmp1 = LegalizeOp(Tmp1); |
| 1253 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1254 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1255 | Tmp2 = Node->getOperand(2); // LHS |
| 1256 | Tmp3 = Node->getOperand(3); // RHS |
| 1257 | Tmp4 = Node->getOperand(1); // CC |
| 1258 | |
| 1259 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
| 1260 | |
| 1261 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1262 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1263 | // the result against zero to select between true and false values. |
| 1264 | if (Tmp3.Val == 0) { |
| 1265 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1266 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1267 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1268 | |
| 1269 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1270 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1272 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1273 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1274 | case TargetLowering::Legal: break; |
| 1275 | case TargetLowering::Custom: |
| 1276 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1277 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1278 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1280 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1281 | case ISD::LOAD: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1282 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1283 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1284 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1285 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1286 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1287 | Tmp3 = Result.getValue(0); |
| 1288 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1289 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1290 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1291 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1292 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1293 | case TargetLowering::Custom: |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1294 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1295 | if (Tmp1.Val) { |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1296 | Tmp3 = LegalizeOp(Tmp1); |
| 1297 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1298 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1299 | break; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1300 | case TargetLowering::Promote: { |
| 1301 | // Only promote a load of vector type to another. |
| 1302 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1303 | // Change base type to a different vector type. |
| 1304 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1305 | |
| 1306 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, Node->getOperand(2)); |
| 1307 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1308 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
| 1309 | break; |
| 1310 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1311 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1312 | // Since loads produce two values, make sure to remember that we |
| 1313 | // legalized both of them. |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1314 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1315 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1316 | return Op.ResNo ? Tmp4 : Tmp3; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1317 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1318 | case ISD::EXTLOAD: |
| 1319 | case ISD::SEXTLOAD: |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1320 | case ISD::ZEXTLOAD: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1321 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1322 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1324 | MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1325 | switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1326 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 1c51c6a | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 1327 | case TargetLowering::Promote: |
| 1328 | assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1329 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1330 | DAG.getValueType(MVT::i8)); |
| 1331 | Tmp1 = Result.getValue(0); |
| 1332 | Tmp2 = Result.getValue(1); |
| 1333 | break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1334 | case TargetLowering::Custom: |
| 1335 | isCustom = true; |
| 1336 | // FALLTHROUGH |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1337 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1338 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1339 | Node->getOperand(3)); |
| 1340 | Tmp1 = Result.getValue(0); |
| 1341 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1342 | |
| 1343 | if (isCustom) { |
| 1344 | Tmp3 = TLI.LowerOperation(Tmp3, DAG); |
| 1345 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1346 | Tmp1 = LegalizeOp(Tmp3); |
| 1347 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1350 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1351 | case TargetLowering::Expand: |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 1352 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1353 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1354 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); |
Andrew Lenharth | 3155908 | 2005-06-30 19:32:57 +0000 | [diff] [blame] | 1355 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1356 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1357 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1358 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1359 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1360 | assert(Node->getOpcode() != ISD::EXTLOAD && |
| 1361 | "EXTLOAD should always be supported!"); |
| 1362 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1363 | // zero/sign extend inreg. |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1364 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1365 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1366 | SDOperand ValRes; |
| 1367 | if (Node->getOpcode() == ISD::SEXTLOAD) |
| 1368 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1369 | Result, DAG.getValueType(SrcVT)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1370 | else |
| 1371 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1372 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1373 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1374 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1375 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1376 | // Since loads produce two values, make sure to remember that we legalized |
| 1377 | // both of them. |
| 1378 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1379 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1380 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1381 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1382 | case ISD::EXTRACT_ELEMENT: { |
| 1383 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1384 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1385 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1386 | case Legal: |
| 1387 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1388 | // 1 -> Hi |
| 1389 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1390 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1391 | TLI.getShiftAmountTy())); |
| 1392 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1393 | } else { |
| 1394 | // 0 -> Lo |
| 1395 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1396 | Node->getOperand(0)); |
| 1397 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | case Expand: |
| 1400 | // Get both the low and high parts. |
| 1401 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1402 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1403 | Result = Tmp2; // 1 -> Hi |
| 1404 | else |
| 1405 | Result = Tmp1; // 0 -> Lo |
| 1406 | break; |
| 1407 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1408 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1409 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1410 | |
| 1411 | case ISD::CopyToReg: |
| 1412 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1414 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1415 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1416 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1417 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1418 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1419 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1420 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1421 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1422 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1423 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1424 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1425 | Tmp3); |
| 1426 | } else { |
| 1427 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | // Since this produces two values, make sure to remember that we legalized |
| 1431 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1432 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1433 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1434 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1435 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1436 | break; |
| 1437 | |
| 1438 | case ISD::RET: |
| 1439 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1440 | |
| 1441 | // Ensure that libcalls are emitted before a return. |
| 1442 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1443 | Tmp1 = LegalizeOp(Tmp1); |
| 1444 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1446 | switch (Node->getNumOperands()) { |
| 1447 | case 2: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 1448 | Tmp2 = Node->getOperand(1); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1449 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1450 | case Legal: |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1451 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1452 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1453 | case Expand: |
| 1454 | if (Tmp2.getValueType() != MVT::Vector) { |
| 1455 | SDOperand Lo, Hi; |
| 1456 | ExpandOp(Tmp2, Lo, Hi); |
| 1457 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
| 1458 | } else { |
| 1459 | SDNode *InVal = Tmp2.Val; |
| 1460 | unsigned NumElems = |
| 1461 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1462 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1463 | |
| 1464 | // Figure out if there is a Packed type corresponding to this Vector |
| 1465 | // type. If so, convert to the packed type. |
| 1466 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1467 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1468 | // Turn this into a return of the packed type. |
| 1469 | Tmp2 = PackVectorOp(Tmp2, TVT); |
| 1470 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1471 | } else if (NumElems == 1) { |
| 1472 | // Turn this into a return of the scalar type. |
| 1473 | Tmp2 = PackVectorOp(Tmp2, EVT); |
| 1474 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1475 | |
| 1476 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 1477 | // should be returned in integer registers! |
| 1478 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1479 | // The scalarized value type may not be legal, e.g. it might require |
| 1480 | // promotion or expansion. Relegalize the return. |
| 1481 | Result = LegalizeOp(Result); |
| 1482 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1483 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 1484 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1485 | SDOperand Lo, Hi; |
| 1486 | SplitVectorOp(Tmp2, Lo, Hi); |
| 1487 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
| 1488 | Result = LegalizeOp(Result); |
| 1489 | } |
| 1490 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1491 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1492 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1493 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1494 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1495 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1496 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1497 | } |
| 1498 | break; |
| 1499 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1500 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1501 | break; |
| 1502 | default: { // ret <values> |
| 1503 | std::vector<SDOperand> NewValues; |
| 1504 | NewValues.push_back(Tmp1); |
| 1505 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1506 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1507 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1508 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | case Expand: { |
| 1511 | SDOperand Lo, Hi; |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1512 | assert(Node->getOperand(i).getValueType() != MVT::Vector && |
| 1513 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1514 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1515 | NewValues.push_back(Lo); |
| 1516 | NewValues.push_back(Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1517 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1518 | } |
| 1519 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1520 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1521 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (NewValues.size() == Node->getNumOperands()) |
| 1524 | Result = DAG.UpdateNodeOperands(Result, NewValues); |
| 1525 | else |
| 1526 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1527 | break; |
| 1528 | } |
| 1529 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1530 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1531 | if (Result.getOpcode() == ISD::RET) { |
| 1532 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1533 | default: assert(0 && "This action is not supported yet!"); |
| 1534 | case TargetLowering::Legal: break; |
| 1535 | case TargetLowering::Custom: |
| 1536 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1537 | if (Tmp1.Val) Result = Tmp1; |
| 1538 | break; |
| 1539 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1540 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1541 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1542 | case ISD::STORE: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1543 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1544 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1545 | |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1546 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1547 | // FIXME: We shouldn't do this for TargetConstantFP's. |
Chris Lattner | 06ac6ab | 2006-03-15 22:19:18 +0000 | [diff] [blame] | 1548 | // FIXME: move this to the DAG Combiner! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1549 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1550 | if (CFP->getValueType(0) == MVT::f32) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1551 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1552 | } else { |
| 1553 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1554 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1555 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1556 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
| 1557 | Node->getOperand(3)); |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1558 | break; |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1561 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1562 | case Legal: { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1563 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
| 1564 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1565 | Node->getOperand(3)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1567 | MVT::ValueType VT = Tmp3.getValueType(); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1568 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1569 | default: assert(0 && "This action is not supported yet!"); |
| 1570 | case TargetLowering::Legal: break; |
| 1571 | case TargetLowering::Custom: |
| 1572 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1573 | if (Tmp1.Val) Result = Tmp1; |
| 1574 | break; |
Chris Lattner | 2efce0a | 2006-04-16 01:36:45 +0000 | [diff] [blame] | 1575 | case TargetLowering::Promote: |
| 1576 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 1577 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 1578 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 1579 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1580 | Node->getOperand(3)); |
| 1581 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1582 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | } |
| 1585 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1586 | // Truncate the value and store the result. |
| 1587 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 1588 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1589 | Node->getOperand(3), |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1590 | DAG.getValueType(Node->getOperand(1).getValueType())); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1591 | break; |
| 1592 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1593 | case Expand: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1594 | unsigned IncrementSize = 0; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1595 | SDOperand Lo, Hi; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1596 | |
| 1597 | // If this is a vector type, then we have to calculate the increment as |
| 1598 | // the product of the element size in bytes, and the number of elements |
| 1599 | // in the high half of the vector. |
| 1600 | if (Node->getOperand(1).getValueType() == MVT::Vector) { |
| 1601 | SDNode *InVal = Node->getOperand(1).Val; |
| 1602 | unsigned NumElems = |
| 1603 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1604 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1605 | |
| 1606 | // Figure out if there is a Packed type corresponding to this Vector |
| 1607 | // type. If so, convert to the packed type. |
| 1608 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1609 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1610 | // Turn this into a normal store of the packed type. |
| 1611 | Tmp3 = PackVectorOp(Node->getOperand(1), TVT); |
| 1612 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1613 | Node->getOperand(3)); |
Chris Lattner | 2efce0a | 2006-04-16 01:36:45 +0000 | [diff] [blame] | 1614 | Result = LegalizeOp(Result); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | } else if (NumElems == 1) { |
| 1617 | // Turn this into a normal store of the scalar type. |
| 1618 | Tmp3 = PackVectorOp(Node->getOperand(1), EVT); |
| 1619 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1620 | Node->getOperand(3)); |
Chris Lattner | 2ae2e98 | 2006-03-31 17:37:22 +0000 | [diff] [blame] | 1621 | // The scalarized value type may not be legal, e.g. it might require |
| 1622 | // promotion or expansion. Relegalize the scalar store. |
| 1623 | Result = LegalizeOp(Result); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | } else { |
| 1626 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1627 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1628 | } |
| 1629 | } else { |
| 1630 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 1631 | IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | d9731af | 2006-03-31 18:20:46 +0000 | [diff] [blame] | 1633 | if (!TLI.isLittleEndian()) |
| 1634 | std::swap(Lo, Hi); |
| 1635 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1636 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1637 | Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, |
| 1638 | Node->getOperand(3)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1639 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1640 | getIntPtrConstant(IncrementSize)); |
| 1641 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1642 | "Pointers must be legal!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1643 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1644 | // wrong. |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1645 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, |
| 1646 | Node->getOperand(3)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1647 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1648 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1649 | } |
| 1650 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1651 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1652 | case ISD::PCMARKER: |
| 1653 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1654 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1655 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1656 | case ISD::STACKSAVE: |
| 1657 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1658 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1659 | Tmp1 = Result.getValue(0); |
| 1660 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1661 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1662 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1663 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1664 | case TargetLowering::Legal: break; |
| 1665 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1666 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1667 | if (Tmp3.Val) { |
| 1668 | Tmp1 = LegalizeOp(Tmp3); |
| 1669 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1670 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1671 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1672 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1673 | // Expand to CopyFromReg if the target set |
| 1674 | // StackPointerRegisterToSaveRestore. |
| 1675 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1676 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1677 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1678 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1679 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1680 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1681 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1682 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1683 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1684 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1685 | |
| 1686 | // Since stacksave produce two values, make sure to remember that we |
| 1687 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1688 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1689 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1690 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1692 | case ISD::STACKRESTORE: |
| 1693 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1694 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1695 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1696 | |
| 1697 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1698 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1699 | case TargetLowering::Legal: break; |
| 1700 | case TargetLowering::Custom: |
| 1701 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1702 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1703 | break; |
| 1704 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1705 | // Expand to CopyToReg if the target set |
| 1706 | // StackPointerRegisterToSaveRestore. |
| 1707 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1708 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1709 | } else { |
| 1710 | Result = Tmp1; |
| 1711 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1712 | break; |
| 1713 | } |
| 1714 | break; |
| 1715 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1716 | case ISD::READCYCLECOUNTER: |
| 1717 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1718 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1719 | |
| 1720 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1721 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1722 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1723 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1724 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1725 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1726 | case ISD::TRUNCSTORE: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1727 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1728 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1729 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1730 | assert(isTypeLegal(Node->getOperand(1).getValueType()) && |
| 1731 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1732 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1733 | |
| 1734 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1735 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1736 | if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 && |
| 1737 | TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) == |
| 1738 | TargetLowering::Promote) { |
| 1739 | // Promote the bool to a mask then store. |
| 1740 | Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2, |
| 1741 | DAG.getConstant(1, Tmp2.getValueType())); |
| 1742 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
| 1743 | Node->getOperand(3), DAG.getValueType(MVT::i8)); |
Chris Lattner | 13d58e7 | 2005-09-10 00:20:18 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1745 | } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1746 | Tmp3 != Node->getOperand(2)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1747 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 1748 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1749 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1750 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1751 | MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT(); |
| 1752 | switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) { |
| 1753 | default: assert(0 && "This action is not supported yet!"); |
| 1754 | case TargetLowering::Legal: break; |
| 1755 | case TargetLowering::Custom: |
| 1756 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1757 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1758 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1759 | } |
| 1760 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1761 | } |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1762 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1763 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1764 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1765 | case Legal: |
| 1766 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1767 | break; |
| 1768 | case Promote: |
| 1769 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1770 | break; |
| 1771 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1772 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1773 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1774 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1775 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1776 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1777 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1778 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1779 | case TargetLowering::Legal: break; |
| 1780 | case TargetLowering::Custom: { |
| 1781 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1782 | if (Tmp1.Val) Result = Tmp1; |
| 1783 | break; |
| 1784 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1785 | case TargetLowering::Expand: |
| 1786 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 1787 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 1788 | Tmp2, Tmp3, |
| 1789 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1790 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1791 | // Make sure the condition is either zero or one. It may have been |
| 1792 | // promoted from something else. |
Chris Lattner | 0e753d6 | 2006-01-30 04:22:28 +0000 | [diff] [blame] | 1793 | unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType()); |
| 1794 | if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1)) |
| 1795 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1796 | Result = DAG.getSelectCC(Tmp1, |
| 1797 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1798 | Tmp2, Tmp3, ISD::SETNE); |
| 1799 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1800 | break; |
| 1801 | case TargetLowering::Promote: { |
| 1802 | MVT::ValueType NVT = |
| 1803 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1804 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1805 | if (MVT::isVector(Tmp2.getValueType())) { |
| 1806 | ExtOp = ISD::BIT_CONVERT; |
| 1807 | TruncOp = ISD::BIT_CONVERT; |
| 1808 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1809 | ExtOp = ISD::ANY_EXTEND; |
| 1810 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1811 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1812 | ExtOp = ISD::FP_EXTEND; |
| 1813 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1814 | } |
| 1815 | // Promote each of the values to the new type. |
| 1816 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 1817 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 1818 | // Perform the larger operation, then round down. |
| 1819 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 1820 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1824 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1825 | case ISD::SELECT_CC: { |
| 1826 | Tmp1 = Node->getOperand(0); // LHS |
| 1827 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1828 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 1829 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1830 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1831 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1832 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 1833 | |
| 1834 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1835 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1836 | // the result against zero to select between true and false values. |
| 1837 | if (Tmp2.Val == 0) { |
| 1838 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 1839 | CC = DAG.getCondCode(ISD::SETNE); |
| 1840 | } |
| 1841 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 1842 | |
| 1843 | // Everything is legal, see if we should expand this op or something. |
| 1844 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 1845 | default: assert(0 && "This action is not supported yet!"); |
| 1846 | case TargetLowering::Legal: break; |
| 1847 | case TargetLowering::Custom: |
| 1848 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1849 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1850 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1851 | } |
| 1852 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1853 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1854 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1855 | Tmp1 = Node->getOperand(0); |
| 1856 | Tmp2 = Node->getOperand(1); |
| 1857 | Tmp3 = Node->getOperand(2); |
| 1858 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 1859 | |
| 1860 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 1861 | // not always be possible to return a true LHS & RHS. In this case, just |
| 1862 | // return the value we legalized, returned in the LHS |
| 1863 | if (Tmp2.Val == 0) { |
| 1864 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1865 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1866 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1867 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 1868 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1869 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 1870 | case TargetLowering::Custom: |
| 1871 | isCustom = true; |
| 1872 | // FALLTHROUGH. |
| 1873 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1874 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1875 | if (isCustom) { |
| 1876 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1877 | if (Tmp3.Val) Result = Tmp3; |
| 1878 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1879 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1880 | case TargetLowering::Promote: { |
| 1881 | // First step, figure out the appropriate operation to use. |
| 1882 | // Allow SETCC to not be supported for all legal data types |
| 1883 | // Mostly this targets FP |
| 1884 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
| 1885 | MVT::ValueType OldVT = NewInTy; |
| 1886 | |
| 1887 | // Scan for the appropriate larger type to use. |
| 1888 | while (1) { |
| 1889 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 1890 | |
| 1891 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 1892 | "Fell off of the edge of the integer world"); |
| 1893 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 1894 | "Fell off of the edge of the floating point world"); |
| 1895 | |
| 1896 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 1897 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1898 | break; |
| 1899 | } |
| 1900 | if (MVT::isInteger(NewInTy)) |
| 1901 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 1902 | else { |
| 1903 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 1904 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 1905 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1906 | Tmp1 = LegalizeOp(Tmp1); |
| 1907 | Tmp2 = LegalizeOp(Tmp2); |
| 1908 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 1909 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 1910 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1911 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1912 | case TargetLowering::Expand: |
| 1913 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 1914 | // rhs that selects between const 1 (true) and const 0 (false). |
| 1915 | MVT::ValueType VT = Node->getValueType(0); |
| 1916 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 1917 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
| 1918 | Node->getOperand(2)); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1921 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1922 | case ISD::MEMSET: |
| 1923 | case ISD::MEMCPY: |
| 1924 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1925 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1926 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 1927 | |
| 1928 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 1929 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 1930 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 1931 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1932 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1933 | break; |
| 1934 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1935 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1936 | break; |
| 1937 | } |
| 1938 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1939 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1940 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1941 | |
| 1942 | SDOperand Tmp4; |
| 1943 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 1944 | case Expand: { |
| 1945 | // Length is too big, just take the lo-part of the length. |
| 1946 | SDOperand HiPart; |
| 1947 | ExpandOp(Node->getOperand(3), HiPart, Tmp4); |
| 1948 | break; |
| 1949 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1950 | case Legal: |
| 1951 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1952 | break; |
| 1953 | case Promote: |
| 1954 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1955 | break; |
| 1956 | } |
| 1957 | |
| 1958 | SDOperand Tmp5; |
| 1959 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 1960 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 1961 | case Legal: |
| 1962 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 1963 | break; |
| 1964 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1965 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 1966 | break; |
| 1967 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1968 | |
| 1969 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 1970 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1971 | case TargetLowering::Custom: |
| 1972 | isCustom = true; |
| 1973 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1974 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1975 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1976 | if (isCustom) { |
| 1977 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1978 | if (Tmp1.Val) Result = Tmp1; |
| 1979 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1980 | break; |
| 1981 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1982 | // Otherwise, the target does not support this operation. Lower the |
| 1983 | // operation to an explicit libcall as appropriate. |
| 1984 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1985 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 1986 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1987 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 1988 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1989 | if (Node->getOpcode() == ISD::MEMSET) { |
| 1990 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 1991 | // Extend the (previously legalized) ubyte argument to be an int value |
| 1992 | // for the call. |
| 1993 | if (Tmp3.getValueType() > MVT::i32) |
| 1994 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 1995 | else |
| 1996 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1997 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 1998 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1999 | |
| 2000 | FnName = "memset"; |
| 2001 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2002 | Node->getOpcode() == ISD::MEMMOVE) { |
| 2003 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 2004 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 2005 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 2006 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2007 | } else { |
| 2008 | assert(0 && "Unknown op!"); |
| 2009 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2010 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2011 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 2012 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2013 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2014 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2017 | } |
| 2018 | break; |
| 2019 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2021 | case ISD::SHL_PARTS: |
| 2022 | case ISD::SRA_PARTS: |
| 2023 | case ISD::SRL_PARTS: { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2024 | std::vector<SDOperand> Ops; |
| 2025 | bool Changed = false; |
| 2026 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2027 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2028 | Changed |= Ops.back() != Node->getOperand(i); |
| 2029 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2030 | if (Changed) |
| 2031 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2032 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2033 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2034 | Node->getValueType(0))) { |
| 2035 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2036 | case TargetLowering::Legal: break; |
| 2037 | case TargetLowering::Custom: |
| 2038 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2039 | if (Tmp1.Val) { |
| 2040 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2041 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2042 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2043 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2044 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2045 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2046 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2047 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2048 | return RetVal; |
| 2049 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2050 | break; |
| 2051 | } |
| 2052 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2053 | // Since these produce multiple values, make sure to remember that we |
| 2054 | // legalized all of them. |
| 2055 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2056 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2057 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2058 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2059 | |
| 2060 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2061 | case ISD::ADD: |
| 2062 | case ISD::SUB: |
| 2063 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2064 | case ISD::MULHS: |
| 2065 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2066 | case ISD::UDIV: |
| 2067 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2068 | case ISD::AND: |
| 2069 | case ISD::OR: |
| 2070 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2071 | case ISD::SHL: |
| 2072 | case ISD::SRL: |
| 2073 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2074 | case ISD::FADD: |
| 2075 | case ISD::FSUB: |
| 2076 | case ISD::FMUL: |
| 2077 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2078 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2079 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2080 | case Expand: assert(0 && "Not possible"); |
| 2081 | case Legal: |
| 2082 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2083 | break; |
| 2084 | case Promote: |
| 2085 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2086 | break; |
| 2087 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2088 | |
| 2089 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2090 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2091 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2092 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2093 | case TargetLowering::Legal: break; |
| 2094 | case TargetLowering::Custom: |
| 2095 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2096 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2097 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2098 | case TargetLowering::Expand: { |
| 2099 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2100 | "Cannot expand this binary operator!"); |
| 2101 | // Expand the operation into a bunch of nasty scalar code. |
| 2102 | std::vector<SDOperand> Ops; |
| 2103 | MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0)); |
| 2104 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2105 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2106 | i != e; ++i) { |
| 2107 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2108 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2109 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2110 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2111 | } |
| 2112 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops); |
| 2113 | break; |
| 2114 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2115 | case TargetLowering::Promote: { |
| 2116 | switch (Node->getOpcode()) { |
| 2117 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2118 | case ISD::AND: |
| 2119 | case ISD::OR: |
| 2120 | case ISD::XOR: { |
| 2121 | MVT::ValueType OVT = Node->getValueType(0); |
| 2122 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2123 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 2124 | // Bit convert each of the values to the new type. |
| 2125 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 2126 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 2127 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2128 | // Bit convert the result back the original type. |
| 2129 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 2130 | break; |
| 2131 | } |
| 2132 | } |
| 2133 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2134 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2135 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2136 | |
| 2137 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2138 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2139 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2140 | case Expand: assert(0 && "Not possible"); |
| 2141 | case Legal: |
| 2142 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2143 | break; |
| 2144 | case Promote: |
| 2145 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2150 | |
| 2151 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2152 | default: assert(0 && "Operation not supported"); |
| 2153 | case TargetLowering::Custom: |
| 2154 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2155 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2156 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2157 | case TargetLowering::Legal: break; |
| 2158 | case TargetLowering::Expand: |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2159 | // If this target supports fabs/fneg natively, do this efficiently. |
| 2160 | if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) && |
| 2161 | TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) { |
| 2162 | // Get the sign bit of the RHS. |
| 2163 | MVT::ValueType IVT = |
| 2164 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2165 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2166 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2167 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2168 | // Get the absolute value of the result. |
| 2169 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2170 | // Select between the nabs and abs value based on the sign bit of |
| 2171 | // the input. |
| 2172 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2173 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2174 | AbsVal), |
| 2175 | AbsVal); |
| 2176 | Result = LegalizeOp(Result); |
| 2177 | break; |
| 2178 | } |
| 2179 | |
| 2180 | // Otherwise, do bitwise ops! |
| 2181 | |
| 2182 | // copysign -> copysignf/copysign libcall. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2183 | const char *FnName; |
| 2184 | if (Node->getValueType(0) == MVT::f32) { |
| 2185 | FnName = "copysignf"; |
| 2186 | if (Tmp2.getValueType() != MVT::f32) // Force operands to match type. |
| 2187 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2188 | DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2)); |
| 2189 | } else { |
| 2190 | FnName = "copysign"; |
| 2191 | if (Tmp2.getValueType() != MVT::f64) // Force operands to match type. |
| 2192 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2193 | DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2)); |
| 2194 | } |
| 2195 | SDOperand Dummy; |
| 2196 | Result = ExpandLibCall(FnName, Node, Dummy); |
| 2197 | break; |
| 2198 | } |
| 2199 | break; |
| 2200 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2201 | case ISD::ADDC: |
| 2202 | case ISD::SUBC: |
| 2203 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2204 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2205 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2206 | // Since this produces two values, make sure to remember that we legalized |
| 2207 | // both of them. |
| 2208 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2209 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2210 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2211 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2212 | case ISD::ADDE: |
| 2213 | case ISD::SUBE: |
| 2214 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2215 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2216 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2217 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2218 | // Since this produces two values, make sure to remember that we legalized |
| 2219 | // both of them. |
| 2220 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2221 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2222 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2223 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2224 | case ISD::BUILD_PAIR: { |
| 2225 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2226 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2227 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2228 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2229 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2230 | case TargetLowering::Promote: |
| 2231 | case TargetLowering::Custom: |
| 2232 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2233 | case TargetLowering::Legal: |
| 2234 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2235 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2236 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2237 | case TargetLowering::Expand: |
| 2238 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2239 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2240 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2241 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2242 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2243 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2244 | break; |
| 2245 | } |
| 2246 | break; |
| 2247 | } |
| 2248 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2249 | case ISD::UREM: |
| 2250 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2251 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2252 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2253 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2254 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2255 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2256 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2257 | case TargetLowering::Custom: |
| 2258 | isCustom = true; |
| 2259 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2260 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2261 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2262 | if (isCustom) { |
| 2263 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2264 | if (Tmp1.Val) Result = Tmp1; |
| 2265 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2266 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2267 | case TargetLowering::Expand: |
| 2268 | if (MVT::isInteger(Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2269 | // X % Y -> X-X/Y*Y |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2270 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2271 | unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2272 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 2273 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2274 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2275 | } else { |
| 2276 | // Floating point mod -> fmod libcall. |
| 2277 | const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; |
| 2278 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 2279 | Result = ExpandLibCall(FnName, Node, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2280 | } |
| 2281 | break; |
| 2282 | } |
| 2283 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2284 | case ISD::VAARG: { |
| 2285 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2286 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2287 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2288 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2289 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2290 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2291 | case TargetLowering::Custom: |
| 2292 | isCustom = true; |
| 2293 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2294 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2295 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2296 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2297 | Tmp1 = Result.getValue(1); |
| 2298 | |
| 2299 | if (isCustom) { |
| 2300 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2301 | if (Tmp2.Val) { |
| 2302 | Result = LegalizeOp(Tmp2); |
| 2303 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2304 | } |
| 2305 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2306 | break; |
| 2307 | case TargetLowering::Expand: { |
| 2308 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 2309 | Node->getOperand(2)); |
| 2310 | // Increment the pointer, VAList, to the next vaarg |
| 2311 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2312 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2313 | TLI.getPointerTy())); |
| 2314 | // Store the incremented VAList to the legalized pointer |
| 2315 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 2316 | Node->getOperand(2)); |
| 2317 | // Load the actual argument out of the pointer VAList |
| 2318 | Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2319 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2320 | Result = LegalizeOp(Result); |
| 2321 | break; |
| 2322 | } |
| 2323 | } |
| 2324 | // Since VAARG produces two values, make sure to remember that we |
| 2325 | // legalized both of them. |
| 2326 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2327 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2328 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | case ISD::VACOPY: |
| 2332 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2333 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2334 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2335 | |
| 2336 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2337 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2338 | case TargetLowering::Custom: |
| 2339 | isCustom = true; |
| 2340 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2341 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2342 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2343 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2344 | if (isCustom) { |
| 2345 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2346 | if (Tmp1.Val) Result = Tmp1; |
| 2347 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2348 | break; |
| 2349 | case TargetLowering::Expand: |
| 2350 | // This defaults to loading a pointer from the input and storing it to the |
| 2351 | // output, returning the chain. |
| 2352 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3)); |
| 2353 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2, |
| 2354 | Node->getOperand(4)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2355 | break; |
| 2356 | } |
| 2357 | break; |
| 2358 | |
| 2359 | case ISD::VAEND: |
| 2360 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2361 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2362 | |
| 2363 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2364 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2365 | case TargetLowering::Custom: |
| 2366 | isCustom = true; |
| 2367 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2368 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2369 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2370 | if (isCustom) { |
| 2371 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2372 | if (Tmp1.Val) Result = Tmp1; |
| 2373 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2374 | break; |
| 2375 | case TargetLowering::Expand: |
| 2376 | Result = Tmp1; // Default to a no-op, return the chain |
| 2377 | break; |
| 2378 | } |
| 2379 | break; |
| 2380 | |
| 2381 | case ISD::VASTART: |
| 2382 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2383 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2384 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2385 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2386 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2387 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2388 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2389 | case TargetLowering::Legal: break; |
| 2390 | case TargetLowering::Custom: |
| 2391 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2392 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2393 | break; |
| 2394 | } |
| 2395 | break; |
| 2396 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2397 | case ISD::ROTL: |
| 2398 | case ISD::ROTR: |
| 2399 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2400 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2401 | |
| 2402 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
| 2403 | "Cannot handle this yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2404 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2405 | break; |
| 2406 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2407 | case ISD::BSWAP: |
| 2408 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2409 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2410 | case TargetLowering::Custom: |
| 2411 | assert(0 && "Cannot custom legalize this yet!"); |
| 2412 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2413 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2414 | break; |
| 2415 | case TargetLowering::Promote: { |
| 2416 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2417 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2418 | unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2420 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2421 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2422 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2423 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2424 | break; |
| 2425 | } |
| 2426 | case TargetLowering::Expand: |
| 2427 | Result = ExpandBSWAP(Tmp1); |
| 2428 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2429 | } |
| 2430 | break; |
| 2431 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2432 | case ISD::CTPOP: |
| 2433 | case ISD::CTTZ: |
| 2434 | case ISD::CTLZ: |
| 2435 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2436 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2437 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2438 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2439 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | case TargetLowering::Promote: { |
| 2442 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2443 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2444 | |
| 2445 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2446 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2447 | // Perform the larger operation, then subtract if needed. |
| 2448 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2449 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2450 | case ISD::CTPOP: |
| 2451 | Result = Tmp1; |
| 2452 | break; |
| 2453 | case ISD::CTTZ: |
| 2454 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2455 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
| 2456 | DAG.getConstant(getSizeInBits(NVT), NVT), |
| 2457 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2458 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2459 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 2460 | break; |
| 2461 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2462 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2463 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2464 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2465 | getSizeInBits(OVT), NVT)); |
| 2466 | break; |
| 2467 | } |
| 2468 | break; |
| 2469 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2470 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2471 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2472 | break; |
| 2473 | } |
| 2474 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2475 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2476 | // Unary operators |
| 2477 | case ISD::FABS: |
| 2478 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2479 | case ISD::FSQRT: |
| 2480 | case ISD::FSIN: |
| 2481 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2482 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2483 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2484 | case TargetLowering::Promote: |
| 2485 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2486 | isCustom = true; |
| 2487 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2488 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2489 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2490 | if (isCustom) { |
| 2491 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2492 | if (Tmp1.Val) Result = Tmp1; |
| 2493 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2494 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2495 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2496 | switch (Node->getOpcode()) { |
| 2497 | default: assert(0 && "Unreachable!"); |
| 2498 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2499 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2500 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2501 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2502 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2503 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2504 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2505 | MVT::ValueType VT = Node->getValueType(0); |
| 2506 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2507 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2508 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2509 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2510 | break; |
| 2511 | } |
| 2512 | case ISD::FSQRT: |
| 2513 | case ISD::FSIN: |
| 2514 | case ISD::FCOS: { |
| 2515 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2516 | const char *FnName = 0; |
| 2517 | switch(Node->getOpcode()) { |
| 2518 | case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; |
| 2519 | case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; |
| 2520 | case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; |
| 2521 | default: assert(0 && "Unreachable!"); |
| 2522 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2523 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 2524 | Result = ExpandLibCall(FnName, Node, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2525 | break; |
| 2526 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2527 | } |
| 2528 | break; |
| 2529 | } |
| 2530 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2531 | |
| 2532 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2533 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2534 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2535 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2536 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2537 | Node->getOperand(0).getValueType())) { |
| 2538 | default: assert(0 && "Unknown operation action!"); |
| 2539 | case TargetLowering::Expand: |
| 2540 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2541 | break; |
| 2542 | case TargetLowering::Legal: |
| 2543 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2544 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | } |
| 2547 | } |
| 2548 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 2549 | case ISD::VBIT_CONVERT: { |
| 2550 | assert(Op.getOperand(0).getValueType() == MVT::Vector && |
| 2551 | "Can only have VBIT_CONVERT where input or output is MVT::Vector!"); |
| 2552 | |
| 2553 | // The input has to be a vector type, we have to either scalarize it, pack |
| 2554 | // it, or convert it based on whether the input vector type is legal. |
| 2555 | SDNode *InVal = Node->getOperand(0).Val; |
| 2556 | unsigned NumElems = |
| 2557 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 2558 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 2559 | |
| 2560 | // Figure out if there is a Packed type corresponding to this Vector |
| 2561 | // type. If so, convert to the packed type. |
| 2562 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 2563 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 2564 | // Turn this into a bit convert of the packed input. |
| 2565 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2566 | PackVectorOp(Node->getOperand(0), TVT)); |
| 2567 | break; |
| 2568 | } else if (NumElems == 1) { |
| 2569 | // Turn this into a bit convert of the scalar input. |
| 2570 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2571 | PackVectorOp(Node->getOperand(0), EVT)); |
| 2572 | break; |
| 2573 | } else { |
| 2574 | // FIXME: UNIMP! Store then reload |
| 2575 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 2576 | } |
| 2577 | } |
| 2578 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2579 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2580 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2581 | case ISD::UINT_TO_FP: { |
| 2582 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2583 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2584 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2585 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2586 | Node->getOperand(0).getValueType())) { |
| 2587 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2588 | case TargetLowering::Custom: |
| 2589 | isCustom = true; |
| 2590 | // FALLTHROUGH |
| 2591 | case TargetLowering::Legal: |
| 2592 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2593 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2594 | if (isCustom) { |
| 2595 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2596 | if (Tmp1.Val) Result = Tmp1; |
| 2597 | } |
| 2598 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2599 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2600 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2601 | LegalizeOp(Node->getOperand(0)), |
| 2602 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2603 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2604 | case TargetLowering::Promote: |
| 2605 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2606 | Node->getValueType(0), |
| 2607 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2608 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2609 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2610 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2611 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2612 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2613 | Node->getValueType(0), Node->getOperand(0)); |
| 2614 | break; |
| 2615 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2616 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2617 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2618 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2619 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2620 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2621 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2622 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2623 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2624 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2625 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | } |
| 2628 | break; |
| 2629 | } |
| 2630 | case ISD::TRUNCATE: |
| 2631 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2632 | case Legal: |
| 2633 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2634 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2635 | break; |
| 2636 | case Expand: |
| 2637 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2638 | |
| 2639 | // Since the result is legal, we should just be able to truncate the low |
| 2640 | // part of the source. |
| 2641 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2642 | break; |
| 2643 | case Promote: |
| 2644 | Result = PromoteOp(Node->getOperand(0)); |
| 2645 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2646 | break; |
| 2647 | } |
| 2648 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2650 | case ISD::FP_TO_SINT: |
| 2651 | case ISD::FP_TO_UINT: |
| 2652 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2653 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2654 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2655 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2656 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2657 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2658 | case TargetLowering::Custom: |
| 2659 | isCustom = true; |
| 2660 | // FALLTHROUGH |
| 2661 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2662 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2663 | if (isCustom) { |
| 2664 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2665 | if (Tmp1.Val) Result = Tmp1; |
| 2666 | } |
| 2667 | break; |
| 2668 | case TargetLowering::Promote: |
| 2669 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2670 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2671 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2672 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2673 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2674 | SDOperand True, False; |
| 2675 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2676 | MVT::ValueType NVT = Node->getValueType(0); |
| 2677 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2678 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2679 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2680 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2681 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2682 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2683 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2684 | Tmp2)); |
| 2685 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2686 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2687 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2688 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2689 | } else { |
| 2690 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2691 | } |
| 2692 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2693 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2694 | break; |
| 2695 | case Expand: |
| 2696 | assert(0 && "Shouldn't need to expand other operators here!"); |
| 2697 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2698 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2699 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 2700 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2704 | |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2705 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2706 | case ISD::ZERO_EXTEND: |
| 2707 | case ISD::SIGN_EXTEND: |
| 2708 | case ISD::FP_EXTEND: |
| 2709 | case ISD::FP_ROUND: |
| 2710 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2711 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2712 | case Legal: |
| 2713 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2714 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2715 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2716 | case Promote: |
| 2717 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2718 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2719 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2720 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2721 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2722 | case ISD::ZERO_EXTEND: |
| 2723 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2724 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2725 | Result = DAG.getZeroExtendInReg(Result, |
| 2726 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2727 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2728 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2729 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2730 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2731 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2732 | Result, |
| 2733 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2734 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2735 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2736 | Result = PromoteOp(Node->getOperand(0)); |
| 2737 | if (Result.getValueType() != Op.getValueType()) |
| 2738 | // Dynamically dead while we have only 2 FP types. |
| 2739 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 2740 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2741 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 2742 | Result = PromoteOp(Node->getOperand(0)); |
| 2743 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 2744 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2745 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2746 | } |
| 2747 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2748 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2749 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2750 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2751 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2752 | |
| 2753 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 2754 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2755 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 2756 | default: assert(0 && "This action not supported for this op yet!"); |
| 2757 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2758 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2761 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2762 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2763 | // NOTE: we could fall back on load/store here too for targets without |
| 2764 | // SAR. However, it is doubtful that any exist. |
| 2765 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 2766 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2767 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2768 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 2769 | Node->getOperand(0), ShiftCst); |
| 2770 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 2771 | Result, ShiftCst); |
| 2772 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 2773 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 2774 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 2775 | |
| 2776 | // NOTE: there is a choice here between constantly creating new stack |
| 2777 | // slots and always reusing the same one. We currently always create |
| 2778 | // new ones, as reuse may inhibit scheduling. |
| 2779 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 2780 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 2781 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 2782 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2783 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2784 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 2785 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 2786 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2787 | Node->getOperand(0), StackSlot, |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 2788 | DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 2789 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 2790 | Result, StackSlot, DAG.getSrcValue(NULL), |
| 2791 | ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2792 | } else { |
| 2793 | assert(0 && "Unknown op"); |
| 2794 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2795 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2796 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2797 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2798 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2799 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2800 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 2801 | assert(Result.getValueType() == Op.getValueType() && |
| 2802 | "Bad legalization!"); |
| 2803 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2804 | // Make sure that the generated code is itself legal. |
| 2805 | if (Result != Op) |
| 2806 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2807 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2808 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 2809 | // means that we always must cache transformed nodes. |
| 2810 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2811 | return Result; |
| 2812 | } |
| 2813 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2814 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 2815 | /// promote it to compute the value into a larger type. The produced value will |
| 2816 | /// have the correct bits for the low portion of the register, but no guarantee |
| 2817 | /// is made about the top bits: it may be zero, sign-extended, or garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2818 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 2819 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 2820 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2821 | assert(getTypeAction(VT) == Promote && |
| 2822 | "Caller should expand or legalize operands that are not promotable!"); |
| 2823 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 2824 | "Cannot promote to smaller type!"); |
| 2825 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2826 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2827 | SDOperand Result; |
| 2828 | SDNode *Node = Op.Val; |
| 2829 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 2830 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 2831 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2832 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2833 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2834 | case ISD::CopyFromReg: |
| 2835 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2836 | default: |
| 2837 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 2838 | assert(0 && "Do not know how to promote this operator!"); |
| 2839 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 2840 | case ISD::UNDEF: |
| 2841 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 2842 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2843 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 2844 | if (VT != MVT::i1) |
| 2845 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 2846 | else |
| 2847 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2848 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 2849 | break; |
| 2850 | case ISD::ConstantFP: |
| 2851 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 2852 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 2853 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 2854 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2855 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2856 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2857 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 2858 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2859 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 2860 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2861 | case ISD::TRUNCATE: |
| 2862 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2863 | case Legal: |
| 2864 | Result = LegalizeOp(Node->getOperand(0)); |
| 2865 | assert(Result.getValueType() >= NVT && |
| 2866 | "This truncation doesn't make sense!"); |
| 2867 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 2868 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 2869 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 2870 | case Promote: |
| 2871 | // The truncation is not required, because we don't guarantee anything |
| 2872 | // about high bits anyway. |
| 2873 | Result = PromoteOp(Node->getOperand(0)); |
| 2874 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2875 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 2876 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2877 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 2878 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2879 | } |
| 2880 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2881 | case ISD::SIGN_EXTEND: |
| 2882 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2883 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2884 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2885 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 2886 | case Legal: |
| 2887 | // Input is legal? Just do extend all the way to the larger type. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2888 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2889 | break; |
| 2890 | case Promote: |
| 2891 | // Promote the reg if it's smaller. |
| 2892 | Result = PromoteOp(Node->getOperand(0)); |
| 2893 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 2894 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 2895 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2896 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2897 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2898 | Result = DAG.getZeroExtendInReg(Result, |
| 2899 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
| 2902 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2903 | case ISD::BIT_CONVERT: |
| 2904 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2905 | Result = PromoteOp(Result); |
| 2906 | break; |
| 2907 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2908 | case ISD::FP_EXTEND: |
| 2909 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 2910 | case ISD::FP_ROUND: |
| 2911 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2912 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 2913 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 2914 | case Legal: |
| 2915 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2916 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2917 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2918 | break; |
| 2919 | } |
| 2920 | break; |
| 2921 | |
| 2922 | case ISD::SINT_TO_FP: |
| 2923 | case ISD::UINT_TO_FP: |
| 2924 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2925 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2926 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2927 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2928 | break; |
| 2929 | |
| 2930 | case Promote: |
| 2931 | Result = PromoteOp(Node->getOperand(0)); |
| 2932 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 2933 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2934 | Result, |
| 2935 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2936 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2937 | Result = DAG.getZeroExtendInReg(Result, |
| 2938 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2939 | // No extra round required here. |
| 2940 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2941 | break; |
| 2942 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2943 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 2944 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2945 | // Round if we cannot tolerate excess precision. |
| 2946 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2947 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2948 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2949 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2950 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 2953 | case ISD::SIGN_EXTEND_INREG: |
| 2954 | Result = PromoteOp(Node->getOperand(0)); |
| 2955 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 2956 | Node->getOperand(1)); |
| 2957 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2958 | case ISD::FP_TO_SINT: |
| 2959 | case ISD::FP_TO_UINT: |
| 2960 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2961 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2962 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2963 | break; |
| 2964 | case Promote: |
| 2965 | // The input result is prerounded, so we don't have to do anything |
| 2966 | // special. |
| 2967 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2968 | break; |
| 2969 | case Expand: |
| 2970 | assert(0 && "not implemented"); |
| 2971 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2972 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 2973 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 2974 | // we can use that instead. This allows us to generate better code for |
| 2975 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 2976 | // legal, such as PowerPC. |
| 2977 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2978 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 2979 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 2980 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2981 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 2982 | } else { |
| 2983 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2984 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2985 | break; |
| 2986 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2987 | case ISD::FABS: |
| 2988 | case ISD::FNEG: |
| 2989 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2990 | assert(Tmp1.getValueType() == NVT); |
| 2991 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2992 | // NOTE: we do not have to do any extra rounding here for |
| 2993 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 2994 | // precision, and these operations don't modify precision at all. |
| 2995 | break; |
| 2996 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2997 | case ISD::FSQRT: |
| 2998 | case ISD::FSIN: |
| 2999 | case ISD::FCOS: |
| 3000 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3001 | assert(Tmp1.getValueType() == NVT); |
| 3002 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3003 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3004 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3005 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3008 | case ISD::AND: |
| 3009 | case ISD::OR: |
| 3010 | case ISD::XOR: |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 3011 | // The input may have strange things in the top bits of the registers, but |
| 3012 | // these operations don't care. They may have weird bits going out, but |
| 3013 | // that too is okay if they are integer operations. |
| 3014 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3015 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3016 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3017 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3018 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3019 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3020 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3021 | case ISD::MUL: |
| 3022 | // The input may have strange things in the top bits of the registers, but |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3023 | // these operations don't care. They may have weird bits going out, but |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3024 | // that too is okay if they are integer operations. |
| 3025 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3026 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3027 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3028 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3029 | break; |
| 3030 | case ISD::FADD: |
| 3031 | case ISD::FSUB: |
| 3032 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3033 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3034 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3035 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3036 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3037 | |
| 3038 | // Floating point operations will give excess precision that we may not be |
| 3039 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 3040 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3041 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 3042 | // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C)) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3043 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3044 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3045 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3046 | break; |
| 3047 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3048 | case ISD::SDIV: |
| 3049 | case ISD::SREM: |
| 3050 | // These operators require that their input be sign extended. |
| 3051 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3052 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3053 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3054 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3055 | DAG.getValueType(VT)); |
| 3056 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3057 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3058 | } |
| 3059 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3060 | |
| 3061 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 3062 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3063 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3064 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3065 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3066 | case ISD::FDIV: |
| 3067 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3068 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3069 | // These operators require that their input be fp extended. |
| 3070 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3071 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3072 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3073 | |
| 3074 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3075 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3076 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3077 | DAG.getValueType(VT)); |
| 3078 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3079 | |
| 3080 | case ISD::UDIV: |
| 3081 | case ISD::UREM: |
| 3082 | // These operators require that their input be zero extended. |
| 3083 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3084 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3085 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3086 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3087 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3088 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3089 | break; |
| 3090 | |
| 3091 | case ISD::SHL: |
| 3092 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3093 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3094 | break; |
| 3095 | case ISD::SRA: |
| 3096 | // The input value must be properly sign extended. |
| 3097 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3098 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3099 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3100 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3101 | break; |
| 3102 | case ISD::SRL: |
| 3103 | // The input value must be properly zero extended. |
| 3104 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3105 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3106 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3107 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3108 | |
| 3109 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3110 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 3111 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3112 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 3113 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 3114 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 3115 | } else { |
| 3116 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 3117 | Node->getOperand(2)); |
| 3118 | // Increment the pointer, VAList, to the next vaarg |
| 3119 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3120 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3121 | TLI.getPointerTy())); |
| 3122 | // Store the incremented VAList to the legalized pointer |
| 3123 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 3124 | Node->getOperand(2)); |
| 3125 | // Load the actual argument out of the pointer VAList |
| 3126 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, |
| 3127 | DAG.getSrcValue(0), VT); |
| 3128 | } |
| 3129 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3130 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3131 | break; |
| 3132 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3133 | case ISD::LOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3134 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0), |
| 3135 | Node->getOperand(1), Node->getOperand(2), VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3136 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3137 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3138 | break; |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3139 | case ISD::SEXTLOAD: |
| 3140 | case ISD::ZEXTLOAD: |
| 3141 | case ISD::EXTLOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3142 | Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0), |
| 3143 | Node->getOperand(1), Node->getOperand(2), |
Chris Lattner | 8136cda | 2005-10-15 20:24:07 +0000 | [diff] [blame] | 3144 | cast<VTSDNode>(Node->getOperand(3))->getVT()); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3145 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3146 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3147 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3148 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3149 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3150 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3151 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3152 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3153 | case ISD::SELECT_CC: |
| 3154 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3155 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3156 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3157 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3158 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3159 | case ISD::BSWAP: |
| 3160 | Tmp1 = Node->getOperand(0); |
| 3161 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3162 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3163 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3164 | DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), |
| 3165 | TLI.getShiftAmountTy())); |
| 3166 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3167 | case ISD::CTPOP: |
| 3168 | case ISD::CTTZ: |
| 3169 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3170 | // Zero extend the argument |
| 3171 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3172 | // Perform the larger operation, then subtract if needed. |
| 3173 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3174 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3175 | case ISD::CTPOP: |
| 3176 | Result = Tmp1; |
| 3177 | break; |
| 3178 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3179 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3180 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3181 | DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3182 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3183 | DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3184 | break; |
| 3185 | case ISD::CTLZ: |
| 3186 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3187 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 3188 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3189 | getSizeInBits(VT), NVT)); |
| 3190 | break; |
| 3191 | } |
| 3192 | break; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3193 | case ISD::VEXTRACT_VECTOR_ELT: |
| 3194 | Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
| 3195 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3196 | case ISD::EXTRACT_VECTOR_ELT: |
| 3197 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3198 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3202 | |
| 3203 | // Make sure the result is itself legal. |
| 3204 | Result = LegalizeOp(Result); |
| 3205 | |
| 3206 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3207 | AddPromotedOperand(Op, Result); |
| 3208 | return Result; |
| 3209 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3210 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3211 | /// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a |
| 3212 | /// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based |
| 3213 | /// on the vector type. The return type of this matches the element type of the |
| 3214 | /// vector, which may not be legal for the target. |
| 3215 | SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3216 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3217 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3218 | // lower to a store then an indexed load. |
| 3219 | SDOperand Vec = Op.getOperand(0); |
| 3220 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3221 | |
| 3222 | SDNode *InVal = Vec.Val; |
| 3223 | unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 3224 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 3225 | |
| 3226 | // Figure out if there is a Packed type corresponding to this Vector |
| 3227 | // type. If so, convert to the packed type. |
| 3228 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3229 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 3230 | // Turn this into a packed extract_vector_elt operation. |
| 3231 | Vec = PackVectorOp(Vec, TVT); |
| 3232 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx); |
| 3233 | } else if (NumElems == 1) { |
| 3234 | // This must be an access of the only element. Return it. |
| 3235 | return PackVectorOp(Vec, EVT); |
| 3236 | } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) { |
| 3237 | SDOperand Lo, Hi; |
| 3238 | SplitVectorOp(Vec, Lo, Hi); |
| 3239 | if (CIdx->getValue() < NumElems/2) { |
| 3240 | Vec = Lo; |
| 3241 | } else { |
| 3242 | Vec = Hi; |
| 3243 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3244 | } |
| 3245 | |
| 3246 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3247 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3248 | return LowerVEXTRACT_VECTOR_ELT(Op); |
| 3249 | } else { |
| 3250 | // Variable index case for extract element. |
| 3251 | // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!! |
| 3252 | assert(0 && "unimp!"); |
| 3253 | return SDOperand(); |
| 3254 | } |
| 3255 | } |
| 3256 | |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3257 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3258 | /// memory traffic. |
| 3259 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3260 | SDOperand Vector = Op.getOperand(0); |
| 3261 | SDOperand Idx = Op.getOperand(1); |
| 3262 | |
| 3263 | // If the target doesn't support this, store the value to a temporary |
| 3264 | // stack slot, then LOAD the scalar element back out. |
| 3265 | SDOperand StackPtr = CreateStackTemporary(Vector.getValueType()); |
| 3266 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3267 | Vector, StackPtr, DAG.getSrcValue(NULL)); |
| 3268 | |
| 3269 | // Add the offset to the index. |
| 3270 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3271 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3272 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3273 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3274 | |
| 3275 | return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL)); |
| 3276 | } |
| 3277 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3278 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3279 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3280 | /// with condition CC on the current target. This usually involves legalizing |
| 3281 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3282 | /// there may be no choice but to create a new SetCC node to represent the |
| 3283 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3284 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3285 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3286 | SDOperand &RHS, |
| 3287 | SDOperand &CC) { |
| 3288 | SDOperand Tmp1, Tmp2, Result; |
| 3289 | |
| 3290 | switch (getTypeAction(LHS.getValueType())) { |
| 3291 | case Legal: |
| 3292 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3293 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3294 | break; |
| 3295 | case Promote: |
| 3296 | Tmp1 = PromoteOp(LHS); // LHS |
| 3297 | Tmp2 = PromoteOp(RHS); // RHS |
| 3298 | |
| 3299 | // If this is an FP compare, the operands have already been extended. |
| 3300 | if (MVT::isInteger(LHS.getValueType())) { |
| 3301 | MVT::ValueType VT = LHS.getValueType(); |
| 3302 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3303 | |
| 3304 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3305 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3306 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3307 | // it. |
| 3308 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3309 | default: assert(0 && "Unknown integer comparison!"); |
| 3310 | case ISD::SETEQ: |
| 3311 | case ISD::SETNE: |
| 3312 | case ISD::SETUGE: |
| 3313 | case ISD::SETUGT: |
| 3314 | case ISD::SETULE: |
| 3315 | case ISD::SETULT: |
| 3316 | // ALL of these operations will work if we either sign or zero extend |
| 3317 | // the operands (including the unsigned comparisons!). Zero extend is |
| 3318 | // usually a simpler/cheaper operation, so prefer it. |
| 3319 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3320 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 3321 | break; |
| 3322 | case ISD::SETGE: |
| 3323 | case ISD::SETGT: |
| 3324 | case ISD::SETLT: |
| 3325 | case ISD::SETLE: |
| 3326 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3327 | DAG.getValueType(VT)); |
| 3328 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3329 | DAG.getValueType(VT)); |
| 3330 | break; |
| 3331 | } |
| 3332 | } |
| 3333 | break; |
| 3334 | case Expand: |
| 3335 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 3336 | ExpandOp(LHS, LHSLo, LHSHi); |
| 3337 | ExpandOp(RHS, RHSLo, RHSHi); |
| 3338 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3339 | case ISD::SETEQ: |
| 3340 | case ISD::SETNE: |
| 3341 | if (RHSLo == RHSHi) |
| 3342 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 3343 | if (RHSCST->isAllOnesValue()) { |
| 3344 | // Comparison to -1. |
| 3345 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 3346 | Tmp2 = RHSLo; |
| 3347 | break; |
| 3348 | } |
| 3349 | |
| 3350 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 3351 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 3352 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3353 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3354 | break; |
| 3355 | default: |
| 3356 | // If this is a comparison of the sign bit, just look at the top part. |
| 3357 | // X > -1, x < 0 |
| 3358 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 3359 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 3360 | CST->getValue() == 0) || // X < 0 |
| 3361 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 3362 | CST->isAllOnesValue())) { // X > -1 |
| 3363 | Tmp1 = LHSHi; |
| 3364 | Tmp2 = RHSHi; |
| 3365 | break; |
| 3366 | } |
| 3367 | |
| 3368 | // FIXME: This generated code sucks. |
| 3369 | ISD::CondCode LowCC; |
| 3370 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3371 | default: assert(0 && "Unknown integer setcc!"); |
| 3372 | case ISD::SETLT: |
| 3373 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 3374 | case ISD::SETGT: |
| 3375 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 3376 | case ISD::SETLE: |
| 3377 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 3378 | case ISD::SETGE: |
| 3379 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 3380 | } |
| 3381 | |
| 3382 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 3383 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 3384 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 3385 | |
| 3386 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 3387 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
| 3388 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 3389 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 3390 | Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 3391 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 3392 | Result, Tmp1, Tmp2)); |
| 3393 | Tmp1 = Result; |
Nate Begeman | da06e9e | 2006-02-01 19:05:15 +0000 | [diff] [blame] | 3394 | Tmp2 = SDOperand(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | LHS = Tmp1; |
| 3398 | RHS = Tmp2; |
| 3399 | } |
| 3400 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3401 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 3402 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 3403 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3404 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 3405 | SDOperand SrcOp) { |
| 3406 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3407 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3408 | |
| 3409 | // Emit a store to the stack slot. |
| 3410 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | ed7b5ba | 2005-12-23 00:50:25 +0000 | [diff] [blame] | 3411 | SrcOp, FIPtr, DAG.getSrcValue(NULL)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3412 | // Result is a load from the stack slot. |
| 3413 | return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0)); |
| 3414 | } |
| 3415 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3416 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 3417 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 3418 | // then load the whole vector back out. |
| 3419 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
| 3420 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3421 | Node->getOperand(0), StackPtr, |
| 3422 | DAG.getSrcValue(NULL)); |
| 3423 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL)); |
| 3424 | } |
| 3425 | |
| 3426 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3427 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
| 3428 | /// support the operation, but do support the resultant packed vector type. |
| 3429 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 3430 | |
| 3431 | // If the only non-undef value is the low element, turn this into a |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3432 | // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3433 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3434 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3435 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3436 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 3437 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3438 | bool isConstant = true; |
| 3439 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 3440 | SplatValue.getOpcode() != ISD::UNDEF) |
| 3441 | isConstant = false; |
| 3442 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3443 | for (unsigned i = 1; i < NumElems; ++i) { |
| 3444 | SDOperand V = Node->getOperand(i); |
| 3445 | std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V); |
| 3446 | if (I != Values.end()) |
| 3447 | I->second.push_back(i); |
| 3448 | else |
| 3449 | Values[V].push_back(i); |
| 3450 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3451 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3452 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3453 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3454 | |
| 3455 | // If this isn't a constant element or an undef, we can't use a constant |
| 3456 | // pool load. |
| 3457 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 3458 | V.getOpcode() != ISD::UNDEF) |
| 3459 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | if (isOnlyLowElement) { |
| 3463 | // If the low element is an undef too, then this whole things is an undef. |
| 3464 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 3465 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 3466 | // Otherwise, turn this into a scalar_to_vector node. |
| 3467 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3468 | Node->getOperand(0)); |
| 3469 | } |
| 3470 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3471 | // If all elements are constants, create a load from the constant pool. |
| 3472 | if (isConstant) { |
| 3473 | MVT::ValueType VT = Node->getValueType(0); |
| 3474 | const Type *OpNTy = |
| 3475 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 3476 | std::vector<Constant*> CV; |
| 3477 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 3478 | if (ConstantFPSDNode *V = |
| 3479 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
| 3480 | CV.push_back(ConstantFP::get(OpNTy, V->getValue())); |
| 3481 | } else if (ConstantSDNode *V = |
| 3482 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
| 3483 | CV.push_back(ConstantUInt::get(OpNTy, V->getValue())); |
| 3484 | } else { |
| 3485 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 3486 | CV.push_back(UndefValue::get(OpNTy)); |
| 3487 | } |
| 3488 | } |
| 3489 | Constant *CP = ConstantPacked::get(CV); |
| 3490 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
| 3491 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 3492 | DAG.getSrcValue(NULL)); |
| 3493 | } |
| 3494 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3495 | if (SplatValue.Val) { // Splat of one value? |
| 3496 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 3497 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3498 | MVT::getIntVectorWithNumElements(NumElems); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3499 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3500 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3501 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec); |
| 3502 | |
| 3503 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3504 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3505 | // Get the splatted value into the low element of a vector register. |
| 3506 | SDOperand LowValVec = |
| 3507 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 3508 | |
| 3509 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 3510 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 3511 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 3512 | SplatMask); |
| 3513 | } |
| 3514 | } |
| 3515 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3516 | // If there are only two unique elements, we may be able to turn this into a |
| 3517 | // vector shuffle. |
| 3518 | if (Values.size() == 2) { |
| 3519 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 3520 | MVT::ValueType MaskVT = |
| 3521 | MVT::getIntVectorWithNumElements(NumElems); |
| 3522 | std::vector<SDOperand> MaskVec(NumElems); |
| 3523 | unsigned i = 0; |
| 3524 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3525 | E = Values.end(); I != E; ++I) { |
| 3526 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 3527 | EE = I->second.end(); II != EE; ++II) |
| 3528 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT)); |
| 3529 | i += NumElems; |
| 3530 | } |
| 3531 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 3532 | |
| 3533 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3534 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 3535 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3536 | std::vector<SDOperand> Ops; |
| 3537 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3538 | E = Values.end(); I != E; ++I) { |
| 3539 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3540 | I->first); |
| 3541 | Ops.push_back(Op); |
| 3542 | } |
| 3543 | Ops.push_back(ShuffleMask); |
| 3544 | |
| 3545 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
| 3546 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops); |
| 3547 | } |
| 3548 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3549 | |
| 3550 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 3551 | // aligned object on the stack, store each element into it, then load |
| 3552 | // the result as a vector. |
| 3553 | MVT::ValueType VT = Node->getValueType(0); |
| 3554 | // Create the stack frame object. |
| 3555 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 3556 | |
| 3557 | // Emit a store of each element to the stack slot. |
| 3558 | std::vector<SDOperand> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3559 | unsigned TypeByteSize = |
| 3560 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
| 3561 | unsigned VectorSize = MVT::getSizeInBits(VT)/8; |
| 3562 | // Store (in the right endianness) the elements to memory. |
| 3563 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3564 | // Ignore undef elements. |
| 3565 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 3566 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 3567 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3568 | |
| 3569 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 3570 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 3571 | |
| 3572 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3573 | Node->getOperand(i), Idx, |
| 3574 | DAG.getSrcValue(NULL))); |
| 3575 | } |
| 3576 | |
| 3577 | SDOperand StoreChain; |
| 3578 | if (!Stores.empty()) // Not all undef elements? |
| 3579 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 3580 | else |
| 3581 | StoreChain = DAG.getEntryNode(); |
| 3582 | |
| 3583 | // Result is a load from the stack slot. |
| 3584 | return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0)); |
| 3585 | } |
| 3586 | |
| 3587 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 3588 | /// specified value type. |
| 3589 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 3590 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 3591 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
| 3592 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); |
| 3593 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 3594 | } |
| 3595 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3596 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 3597 | SDOperand Op, SDOperand Amt, |
| 3598 | SDOperand &Lo, SDOperand &Hi) { |
| 3599 | // Expand the subcomponents. |
| 3600 | SDOperand LHSL, LHSH; |
| 3601 | ExpandOp(Op, LHSL, LHSH); |
| 3602 | |
| 3603 | std::vector<SDOperand> Ops; |
| 3604 | Ops.push_back(LHSL); |
| 3605 | Ops.push_back(LHSH); |
| 3606 | Ops.push_back(Amt); |
Chris Lattner | cc0675a | 2005-08-30 17:21:17 +0000 | [diff] [blame] | 3607 | std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 3608 | Lo = DAG.getNode(NodeOp, VTs, Ops); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3609 | Hi = Lo.getValue(1); |
| 3610 | } |
| 3611 | |
| 3612 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3613 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 3614 | /// smaller elements. If we can't find a way that is more efficient than a |
| 3615 | /// libcall on this target, return false. Otherwise, return true with the |
| 3616 | /// low-parts expanded into Lo and Hi. |
| 3617 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 3618 | SDOperand &Lo, SDOperand &Hi) { |
| 3619 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 3620 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3621 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3622 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3623 | SDOperand ShAmt = LegalizeOp(Amt); |
| 3624 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 3625 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 3626 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 3627 | |
| 3628 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 3629 | // and are disabled. |
| 3630 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 3631 | unsigned Cst = CN->getValue(); |
| 3632 | // Expand the incoming operand to be shifted, so that we have its parts |
| 3633 | SDOperand InL, InH; |
| 3634 | ExpandOp(Op, InL, InH); |
| 3635 | switch(Opc) { |
| 3636 | case ISD::SHL: |
| 3637 | if (Cst > VTBits) { |
| 3638 | Lo = DAG.getConstant(0, NVT); |
| 3639 | Hi = DAG.getConstant(0, NVT); |
| 3640 | } else if (Cst > NVTBits) { |
| 3641 | Lo = DAG.getConstant(0, NVT); |
| 3642 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3643 | } else if (Cst == NVTBits) { |
| 3644 | Lo = DAG.getConstant(0, NVT); |
| 3645 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3646 | } else { |
| 3647 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 3648 | Hi = DAG.getNode(ISD::OR, NVT, |
| 3649 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 3650 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3651 | } |
| 3652 | return true; |
| 3653 | case ISD::SRL: |
| 3654 | if (Cst > VTBits) { |
| 3655 | Lo = DAG.getConstant(0, NVT); |
| 3656 | Hi = DAG.getConstant(0, NVT); |
| 3657 | } else if (Cst > NVTBits) { |
| 3658 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 3659 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3660 | } else if (Cst == NVTBits) { |
| 3661 | Lo = InH; |
| 3662 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3663 | } else { |
| 3664 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3665 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3666 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3667 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3668 | } |
| 3669 | return true; |
| 3670 | case ISD::SRA: |
| 3671 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3672 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3673 | DAG.getConstant(NVTBits-1, ShTy)); |
| 3674 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3675 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3676 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3677 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3678 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3679 | } else if (Cst == NVTBits) { |
| 3680 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3681 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3682 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3683 | } else { |
| 3684 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3685 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3686 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3687 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3688 | } |
| 3689 | return true; |
| 3690 | } |
| 3691 | } |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3692 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3693 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3694 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3695 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3696 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 3697 | // does not fit into a register, return the lo part and set the hi part to the |
| 3698 | // by-reg argument. If it does fit into a single register, return the result |
| 3699 | // and leave the Hi part unset. |
| 3700 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 3701 | SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3702 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 3703 | // The input chain to this libcall is the entry node of the function. |
| 3704 | // Legalizing the call will automatically add the previous call to the |
| 3705 | // dependence. |
| 3706 | SDOperand InChain = DAG.getEntryNode(); |
| 3707 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3708 | TargetLowering::ArgListTy Args; |
| 3709 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3710 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 3711 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 3712 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 3713 | } |
| 3714 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3715 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3716 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3717 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3718 | std::pair<SDOperand,SDOperand> CallInfo = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 3719 | TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, |
| 3720 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3722 | // Legalize the call sequence, starting with the chain. This will advance |
| 3723 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 3724 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 3725 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3726 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3727 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3728 | default: assert(0 && "Unknown thing"); |
| 3729 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3730 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3731 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3732 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3733 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3734 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3735 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3736 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3740 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 3741 | /// destination type is legal. |
| 3742 | SDOperand SelectionDAGLegalize:: |
| 3743 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3744 | assert(isTypeLegal(DestTy) && "Destination type is not legal!"); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3745 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 3746 | "This is not an expansion!"); |
| 3747 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 3748 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3749 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3750 | assert(Source.getValueType() == MVT::i64 && |
| 3751 | "This only works for 64-bit -> FP"); |
| 3752 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 3753 | // incoming integer is set. To handle this, we dynamically test to see if |
| 3754 | // it is set, and, if so, add a fudge factor. |
| 3755 | SDOperand Lo, Hi; |
| 3756 | ExpandOp(Source, Lo, Hi); |
| 3757 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 3758 | // If this is unsigned, and not supported, first perform the conversion to |
| 3759 | // signed, then adjust the result if the sign bit is set. |
| 3760 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 3761 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 3762 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3763 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 3764 | DAG.getConstant(0, Hi.getValueType()), |
| 3765 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3766 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3767 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3768 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 3769 | uint64_t FF = 0x5f800000ULL; |
| 3770 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3771 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 3773 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3774 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3775 | SDOperand FudgeInReg; |
| 3776 | if (DestTy == MVT::f32) |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 3777 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3778 | DAG.getSrcValue(NULL)); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3779 | else { |
| 3780 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3781 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 3782 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3783 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 3784 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3785 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3786 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3787 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 3788 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 3789 | default: assert(0 && "This action not implemented for this operation!"); |
| 3790 | case TargetLowering::Legal: |
| 3791 | case TargetLowering::Expand: |
| 3792 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3793 | case TargetLowering::Custom: { |
| 3794 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 3795 | Source), DAG); |
| 3796 | if (NV.Val) |
| 3797 | return LegalizeOp(NV); |
| 3798 | break; // The target decided this was legal after all |
| 3799 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 3802 | // Expand the source, then glue it back together for the call. We must expand |
| 3803 | // the source in case it is shared (this pass of legalize must traverse it). |
| 3804 | SDOperand SrcLo, SrcHi; |
| 3805 | ExpandOp(Source, SrcLo, SrcHi); |
| 3806 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 3807 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3808 | const char *FnName = 0; |
| 3809 | if (DestTy == MVT::f32) |
| 3810 | FnName = "__floatdisf"; |
| 3811 | else { |
| 3812 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 3813 | FnName = "__floatdidf"; |
| 3814 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3815 | |
| 3816 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 3817 | SDOperand UnusedHiPart; |
Chris Lattner | 2512569 | 2006-02-17 04:32:33 +0000 | [diff] [blame] | 3818 | return ExpandLibCall(FnName, Source.Val, UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3819 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3820 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3821 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 3822 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 3823 | /// we expand it. At this point, we know that the result and operand types are |
| 3824 | /// legal for the target. |
| 3825 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 3826 | SDOperand Op0, |
| 3827 | MVT::ValueType DestVT) { |
| 3828 | if (Op0.getValueType() == MVT::i32) { |
| 3829 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 3830 | |
| 3831 | // get the stack frame index of a 8 byte buffer |
| 3832 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3833 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 3834 | // get address of 8 byte buffer |
| 3835 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 3836 | // word offset constant for Hi/Lo address computation |
| 3837 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 3838 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 3839 | SDOperand Hi = StackSlot; |
| 3840 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 3841 | if (TLI.isLittleEndian()) |
| 3842 | std::swap(Hi, Lo); |
| 3843 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3844 | // if signed map to unsigned space |
| 3845 | SDOperand Op0Mapped; |
| 3846 | if (isSigned) { |
| 3847 | // constant used to invert sign bit (signed to unsigned mapping) |
| 3848 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 3849 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 3850 | } else { |
| 3851 | Op0Mapped = Op0; |
| 3852 | } |
| 3853 | // store the lo of the constructed double - based on integer input |
| 3854 | SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3855 | Op0Mapped, Lo, DAG.getSrcValue(NULL)); |
| 3856 | // initial hi portion of constructed double |
| 3857 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 3858 | // store the hi of the constructed double - biased exponent |
| 3859 | SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1, |
| 3860 | InitialHi, Hi, DAG.getSrcValue(NULL)); |
| 3861 | // load the constructed double |
| 3862 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, |
| 3863 | DAG.getSrcValue(NULL)); |
| 3864 | // FP constant to bias correct the final result |
| 3865 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 3866 | BitsToDouble(0x4330000080000000ULL) |
| 3867 | : BitsToDouble(0x4330000000000000ULL), |
| 3868 | MVT::f64); |
| 3869 | // subtract the bias |
| 3870 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 3871 | // final result |
| 3872 | SDOperand Result; |
| 3873 | // handle final rounding |
| 3874 | if (DestVT == MVT::f64) { |
| 3875 | // do nothing |
| 3876 | Result = Sub; |
| 3877 | } else { |
| 3878 | // if f32 then cast to f32 |
| 3879 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 3880 | } |
| 3881 | return Result; |
| 3882 | } |
| 3883 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 3884 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 3885 | |
| 3886 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 3887 | DAG.getConstant(0, Op0.getValueType()), |
| 3888 | ISD::SETLT); |
| 3889 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3890 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3891 | SignSet, Four, Zero); |
| 3892 | |
| 3893 | // If the sign bit of the integer is set, the large number will be treated |
| 3894 | // as a negative number. To counteract this, the dynamic code adds an |
| 3895 | // offset depending on the data type. |
| 3896 | uint64_t FF; |
| 3897 | switch (Op0.getValueType()) { |
| 3898 | default: assert(0 && "Unsupported integer type!"); |
| 3899 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 3900 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 3901 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 3902 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 3903 | } |
| 3904 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3905 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
| 3906 | |
| 3907 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 3908 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3909 | SDOperand FudgeInReg; |
| 3910 | if (DestVT == MVT::f32) |
| 3911 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3912 | DAG.getSrcValue(NULL)); |
| 3913 | else { |
| 3914 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 3915 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 3916 | DAG.getEntryNode(), CPIdx, |
| 3917 | DAG.getSrcValue(NULL), MVT::f32)); |
| 3918 | } |
| 3919 | |
| 3920 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 3921 | } |
| 3922 | |
| 3923 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 3924 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 3925 | /// we promote it. At this point, we know that the result and operand types are |
| 3926 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 3927 | /// operation that takes a larger input. |
| 3928 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 3929 | MVT::ValueType DestVT, |
| 3930 | bool isSigned) { |
| 3931 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 3932 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 3933 | |
| 3934 | unsigned OpToUse = 0; |
| 3935 | |
| 3936 | // Scan for the appropriate larger type to use. |
| 3937 | while (1) { |
| 3938 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 3939 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 3940 | |
| 3941 | // If the target supports SINT_TO_FP of this type, use it. |
| 3942 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 3943 | default: break; |
| 3944 | case TargetLowering::Legal: |
| 3945 | if (!TLI.isTypeLegal(NewInTy)) |
| 3946 | break; // Can't use this datatype. |
| 3947 | // FALL THROUGH. |
| 3948 | case TargetLowering::Custom: |
| 3949 | OpToUse = ISD::SINT_TO_FP; |
| 3950 | break; |
| 3951 | } |
| 3952 | if (OpToUse) break; |
| 3953 | if (isSigned) continue; |
| 3954 | |
| 3955 | // If the target supports UINT_TO_FP of this type, use it. |
| 3956 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 3957 | default: break; |
| 3958 | case TargetLowering::Legal: |
| 3959 | if (!TLI.isTypeLegal(NewInTy)) |
| 3960 | break; // Can't use this datatype. |
| 3961 | // FALL THROUGH. |
| 3962 | case TargetLowering::Custom: |
| 3963 | OpToUse = ISD::UINT_TO_FP; |
| 3964 | break; |
| 3965 | } |
| 3966 | if (OpToUse) break; |
| 3967 | |
| 3968 | // Otherwise, try a larger type. |
| 3969 | } |
| 3970 | |
| 3971 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 3972 | // desired type then run the operation on it. |
| 3973 | return DAG.getNode(OpToUse, DestVT, |
| 3974 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 3975 | NewInTy, LegalOp)); |
| 3976 | } |
| 3977 | |
| 3978 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 3979 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 3980 | /// we promote it. At this point, we know that the result and operand types are |
| 3981 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 3982 | /// operation that returns a larger result. |
| 3983 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 3984 | MVT::ValueType DestVT, |
| 3985 | bool isSigned) { |
| 3986 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 3987 | MVT::ValueType NewOutTy = DestVT; |
| 3988 | |
| 3989 | unsigned OpToUse = 0; |
| 3990 | |
| 3991 | // Scan for the appropriate larger type to use. |
| 3992 | while (1) { |
| 3993 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 3994 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 3995 | |
| 3996 | // If the target supports FP_TO_SINT returning this type, use it. |
| 3997 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 3998 | default: break; |
| 3999 | case TargetLowering::Legal: |
| 4000 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4001 | break; // Can't use this datatype. |
| 4002 | // FALL THROUGH. |
| 4003 | case TargetLowering::Custom: |
| 4004 | OpToUse = ISD::FP_TO_SINT; |
| 4005 | break; |
| 4006 | } |
| 4007 | if (OpToUse) break; |
| 4008 | |
| 4009 | // If the target supports FP_TO_UINT of this type, use it. |
| 4010 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 4011 | default: break; |
| 4012 | case TargetLowering::Legal: |
| 4013 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4014 | break; // Can't use this datatype. |
| 4015 | // FALL THROUGH. |
| 4016 | case TargetLowering::Custom: |
| 4017 | OpToUse = ISD::FP_TO_UINT; |
| 4018 | break; |
| 4019 | } |
| 4020 | if (OpToUse) break; |
| 4021 | |
| 4022 | // Otherwise, try a larger type. |
| 4023 | } |
| 4024 | |
| 4025 | // Okay, we found the operation and type to use. Truncate the result of the |
| 4026 | // extended FP_TO_*INT operation to the desired size. |
| 4027 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 4028 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 4029 | } |
| 4030 | |
| 4031 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 4032 | /// |
| 4033 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 4034 | MVT::ValueType VT = Op.getValueType(); |
| 4035 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 4036 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 4037 | switch (VT) { |
| 4038 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 4039 | case MVT::i16: |
| 4040 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4041 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4042 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 4043 | case MVT::i32: |
| 4044 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4045 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4046 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4047 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4048 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 4049 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 4050 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4051 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4052 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4053 | case MVT::i64: |
| 4054 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4055 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4056 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4057 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4058 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4059 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4060 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4061 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4062 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 4063 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 4064 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 4065 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 4066 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 4067 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 4068 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 4069 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 4070 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4071 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4072 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 4073 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4074 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 4079 | /// |
| 4080 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 4081 | switch (Opc) { |
| 4082 | default: assert(0 && "Cannot expand this yet!"); |
| 4083 | case ISD::CTPOP: { |
| 4084 | static const uint64_t mask[6] = { |
| 4085 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 4086 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 4087 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 4088 | }; |
| 4089 | MVT::ValueType VT = Op.getValueType(); |
| 4090 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4091 | unsigned len = getSizeInBits(VT); |
| 4092 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4093 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 4094 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 4095 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4096 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 4097 | DAG.getNode(ISD::AND, VT, |
| 4098 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 4099 | } |
| 4100 | return Op; |
| 4101 | } |
| 4102 | case ISD::CTLZ: { |
| 4103 | // for now, we do this: |
| 4104 | // x = x | (x >> 1); |
| 4105 | // x = x | (x >> 2); |
| 4106 | // ... |
| 4107 | // x = x | (x >>16); |
| 4108 | // x = x | (x >>32); // for 64-bit input |
| 4109 | // return popcount(~x); |
| 4110 | // |
| 4111 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 4112 | MVT::ValueType VT = Op.getValueType(); |
| 4113 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4114 | unsigned len = getSizeInBits(VT); |
| 4115 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4116 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4117 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4118 | } |
| 4119 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4120 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4121 | } |
| 4122 | case ISD::CTTZ: { |
| 4123 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4124 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4125 | // { return 32 - nlz(~x & (x-1)); } |
| 4126 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 4127 | MVT::ValueType VT = Op.getValueType(); |
| 4128 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 4129 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 4130 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 4131 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 4132 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 4133 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 4134 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 4135 | return DAG.getNode(ISD::SUB, VT, |
| 4136 | DAG.getConstant(getSizeInBits(VT), VT), |
| 4137 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 4138 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 4139 | } |
| 4140 | } |
| 4141 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4142 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4143 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 4144 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 4145 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 4146 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 4147 | /// Lo/Hi values are returned. |
| 4148 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 4149 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4150 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4151 | SDNode *Node = Op.Val; |
| 4152 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Nate Begeman | ab48be3 | 2005-11-22 18:16:00 +0000 | [diff] [blame] | 4153 | assert((MVT::isInteger(VT) || VT == MVT::Vector) && |
| 4154 | "Cannot expand FP values!"); |
| 4155 | assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4156 | "Cannot expand to FP value or to larger int value!"); |
| 4157 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4158 | // See if we already expanded it. |
| 4159 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4160 | = ExpandedNodes.find(Op); |
| 4161 | if (I != ExpandedNodes.end()) { |
| 4162 | Lo = I->second.first; |
| 4163 | Hi = I->second.second; |
| 4164 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4167 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4168 | case ISD::CopyFromReg: |
| 4169 | assert(0 && "CopyFromReg must be legal!"); |
| 4170 | default: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4171 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 4172 | assert(0 && "Do not know how to expand this operator!"); |
| 4173 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4174 | case ISD::UNDEF: |
| 4175 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 4176 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4177 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4178 | case ISD::Constant: { |
| 4179 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 4180 | Lo = DAG.getConstant(Cst, NVT); |
| 4181 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 4182 | break; |
| 4183 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4184 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4185 | // Return the operands. |
| 4186 | Lo = Node->getOperand(0); |
| 4187 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4188 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4189 | |
| 4190 | case ISD::SIGN_EXTEND_INREG: |
| 4191 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4192 | // Sign extend the lo-part. |
| 4193 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4194 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 4195 | TLI.getShiftAmountTy())); |
| 4196 | // sext_inreg the low part if needed. |
| 4197 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 4198 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4199 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4200 | case ISD::BSWAP: { |
| 4201 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4202 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 4203 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 4204 | Lo = TempLo; |
| 4205 | break; |
| 4206 | } |
| 4207 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4208 | case ISD::CTPOP: |
| 4209 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 4210 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 4211 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 4212 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4213 | Hi = DAG.getConstant(0, NVT); |
| 4214 | break; |
| 4215 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4216 | case ISD::CTLZ: { |
| 4217 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4218 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4219 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4220 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4221 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 4222 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4223 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 4224 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 4225 | |
| 4226 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 4227 | Hi = DAG.getConstant(0, NVT); |
| 4228 | break; |
| 4229 | } |
| 4230 | |
| 4231 | case ISD::CTTZ: { |
| 4232 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4233 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4234 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4235 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4236 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 4237 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4238 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 4239 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 4240 | |
| 4241 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 4242 | Hi = DAG.getConstant(0, NVT); |
| 4243 | break; |
| 4244 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4245 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4246 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4247 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4248 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4249 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 4250 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 4251 | |
| 4252 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4253 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4254 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 4255 | if (!TLI.isLittleEndian()) |
| 4256 | std::swap(Lo, Hi); |
| 4257 | break; |
| 4258 | } |
| 4259 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4260 | case ISD::LOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4261 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4262 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 4263 | Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4264 | |
| 4265 | // Increment the pointer to the other half. |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 4266 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4267 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4268 | getIntPtrConstant(IncrementSize)); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4269 | // FIXME: This creates a bogus srcvalue! |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 4270 | Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 4271 | |
| 4272 | // Build a factor node to remember that this load is independent of the |
| 4273 | // other one. |
| 4274 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4275 | Hi.getValue(1)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4276 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4277 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4278 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4279 | if (!TLI.isLittleEndian()) |
| 4280 | std::swap(Lo, Hi); |
| 4281 | break; |
| 4282 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4283 | case ISD::AND: |
| 4284 | case ISD::OR: |
| 4285 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 4286 | SDOperand LL, LH, RL, RH; |
| 4287 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4288 | ExpandOp(Node->getOperand(1), RL, RH); |
| 4289 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 4290 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 4291 | break; |
| 4292 | } |
| 4293 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4294 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4295 | ExpandOp(Node->getOperand(1), LL, LH); |
| 4296 | ExpandOp(Node->getOperand(2), RL, RH); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4297 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
| 4298 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4299 | break; |
| 4300 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4301 | case ISD::SELECT_CC: { |
| 4302 | SDOperand TL, TH, FL, FH; |
| 4303 | ExpandOp(Node->getOperand(2), TL, TH); |
| 4304 | ExpandOp(Node->getOperand(3), FL, FH); |
| 4305 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4306 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
| 4307 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4308 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
| 4309 | break; |
| 4310 | } |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4311 | case ISD::SEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4312 | SDOperand Chain = Node->getOperand(0); |
| 4313 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4314 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4315 | |
| 4316 | if (EVT == NVT) |
| 4317 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4318 | else |
| 4319 | Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4320 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4321 | |
| 4322 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4323 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4324 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4325 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4326 | // part. |
| 4327 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 4328 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 4329 | TLI.getShiftAmountTy())); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4330 | break; |
| 4331 | } |
| 4332 | case ISD::ZEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4333 | SDOperand Chain = Node->getOperand(0); |
| 4334 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4335 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4336 | |
| 4337 | if (EVT == NVT) |
| 4338 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4339 | else |
| 4340 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4341 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4342 | |
| 4343 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4344 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4345 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4346 | // The high part is just a zero. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4347 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4348 | break; |
| 4349 | } |
| 4350 | case ISD::EXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4351 | SDOperand Chain = Node->getOperand(0); |
| 4352 | SDOperand Ptr = Node->getOperand(1); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4353 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4354 | |
| 4355 | if (EVT == NVT) |
| 4356 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4357 | else |
| 4358 | Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4359 | EVT); |
| 4360 | |
| 4361 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4362 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4363 | |
| 4364 | // The high part is undefined. |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4365 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4366 | break; |
| 4367 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4368 | case ISD::ANY_EXTEND: |
| 4369 | // The low part is any extension of the input (which degenerates to a copy). |
| 4370 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 4371 | // The high part is undefined. |
| 4372 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4373 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4374 | case ISD::SIGN_EXTEND: { |
| 4375 | // The low part is just a sign extension of the input (which degenerates to |
| 4376 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4377 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4378 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4379 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4380 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 4381 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4382 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4383 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4384 | break; |
| 4385 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4386 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4387 | // The low part is just a zero extension of the input (which degenerates to |
| 4388 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4389 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4390 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4391 | // The high part is just a zero. |
| 4392 | Hi = DAG.getConstant(0, NVT); |
| 4393 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4394 | |
| 4395 | case ISD::BIT_CONVERT: { |
| 4396 | SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0), |
| 4397 | Node->getOperand(0)); |
| 4398 | ExpandOp(Tmp, Lo, Hi); |
| 4399 | break; |
| 4400 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4401 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4402 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4403 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 4404 | TargetLowering::Custom && |
| 4405 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4406 | Lo = TLI.LowerOperation(Op, DAG); |
| 4407 | assert(Lo.Val && "Node must be custom expanded!"); |
| 4408 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4409 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4410 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4411 | break; |
| 4412 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4413 | // These operators cannot be expanded directly, emit them as calls to |
| 4414 | // library functions. |
| 4415 | case ISD::FP_TO_SINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4416 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4417 | SDOperand Op; |
| 4418 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4419 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4420 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4421 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4422 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4423 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4424 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 4425 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4426 | // Now that the custom expander is done, expand the result, which is still |
| 4427 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4428 | if (Op.Val) { |
| 4429 | ExpandOp(Op, Lo, Hi); |
| 4430 | break; |
| 4431 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4432 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4433 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4434 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4435 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4436 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4437 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4438 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4439 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4440 | case ISD::FP_TO_UINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4441 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4442 | SDOperand Op; |
| 4443 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4444 | case Expand: assert(0 && "cannot expand FP!"); |
| 4445 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4446 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 4447 | } |
| 4448 | |
| 4449 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 4450 | |
| 4451 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4452 | if (Op.Val) { |
| 4453 | ExpandOp(Op, Lo, Hi); |
| 4454 | break; |
| 4455 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4456 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4458 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4459 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4460 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4461 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4462 | break; |
| 4463 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4464 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4465 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4466 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4467 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4468 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4469 | Op = TLI.LowerOperation(Op, DAG); |
| 4470 | if (Op.Val) { |
| 4471 | // Now that the custom expander is done, expand the result, which is |
| 4472 | // still VT. |
| 4473 | ExpandOp(Op, Lo, Hi); |
| 4474 | break; |
| 4475 | } |
| 4476 | } |
| 4477 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4478 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4479 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4480 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4481 | |
| 4482 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4483 | TargetLowering::LegalizeAction Action = |
| 4484 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 4485 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4486 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4487 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4488 | break; |
| 4489 | } |
| 4490 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4491 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4492 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4493 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4494 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4495 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4496 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4497 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4498 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4499 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4500 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4501 | Op = TLI.LowerOperation(Op, DAG); |
| 4502 | if (Op.Val) { |
| 4503 | // Now that the custom expander is done, expand the result, which is |
| 4504 | // still VT. |
| 4505 | ExpandOp(Op, Lo, Hi); |
| 4506 | break; |
| 4507 | } |
| 4508 | } |
| 4509 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4510 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4511 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4512 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4513 | |
| 4514 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4515 | TargetLowering::LegalizeAction Action = |
| 4516 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 4517 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4518 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4519 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4520 | break; |
| 4521 | } |
| 4522 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4523 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4524 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4525 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4529 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4530 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4531 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4532 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4533 | Op = TLI.LowerOperation(Op, DAG); |
| 4534 | if (Op.Val) { |
| 4535 | // Now that the custom expander is done, expand the result, which is |
| 4536 | // still VT. |
| 4537 | ExpandOp(Op, Lo, Hi); |
| 4538 | break; |
| 4539 | } |
| 4540 | } |
| 4541 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4542 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4543 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4544 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4545 | |
| 4546 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4547 | TargetLowering::LegalizeAction Action = |
| 4548 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 4549 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4550 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4551 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4552 | break; |
| 4553 | } |
| 4554 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4555 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4556 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4557 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4558 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4559 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4560 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4561 | case ISD::SUB: { |
| 4562 | // If the target wants to custom expand this, let them. |
| 4563 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 4564 | TargetLowering::Custom) { |
| 4565 | Op = TLI.LowerOperation(Op, DAG); |
| 4566 | if (Op.Val) { |
| 4567 | ExpandOp(Op, Lo, Hi); |
| 4568 | break; |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | // Expand the subcomponents. |
| 4573 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 4574 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 4575 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 4576 | std::vector<MVT::ValueType> VTs; |
| 4577 | std::vector<SDOperand> LoOps, HiOps; |
| 4578 | VTs.push_back(LHSL.getValueType()); |
| 4579 | VTs.push_back(MVT::Flag); |
| 4580 | LoOps.push_back(LHSL); |
| 4581 | LoOps.push_back(RHSL); |
| 4582 | HiOps.push_back(LHSH); |
| 4583 | HiOps.push_back(RHSH); |
| 4584 | if (Node->getOpcode() == ISD::ADD) { |
| 4585 | Lo = DAG.getNode(ISD::ADDC, VTs, LoOps); |
| 4586 | HiOps.push_back(Lo.getValue(1)); |
| 4587 | Hi = DAG.getNode(ISD::ADDE, VTs, HiOps); |
| 4588 | } else { |
| 4589 | Lo = DAG.getNode(ISD::SUBC, VTs, LoOps); |
| 4590 | HiOps.push_back(Lo.getValue(1)); |
| 4591 | Hi = DAG.getNode(ISD::SUBE, VTs, HiOps); |
| 4592 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 4593 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4594 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4595 | case ISD::MUL: { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4596 | if (TLI.isOperationLegal(ISD::MULHU, NVT)) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4597 | SDOperand LL, LH, RL, RH; |
| 4598 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4599 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 4600 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
| 4601 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 4602 | // extended the sign bit of the low half through the upper half, and if so |
| 4603 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 4604 | // i64 x i64 multiply. |
| 4605 | if (TLI.isOperationLegal(ISD::MULHS, NVT) && |
| 4606 | // is RH an extension of the sign bit of RL? |
| 4607 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 4608 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 4609 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 4610 | // is LH an extension of the sign bit of LL? |
| 4611 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 4612 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 4613 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
| 4614 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
| 4615 | } else { |
| 4616 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 4617 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 4618 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 4619 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 4620 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 4621 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4622 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 4623 | } else { |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 4624 | Lo = ExpandLibCall("__muldi3" , Node, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4625 | } |
| 4626 | break; |
| 4627 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4628 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 4629 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 4630 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 4631 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 4634 | // Make sure the resultant values have been legalized themselves, unless this |
| 4635 | // is a type that requires multi-step expansion. |
| 4636 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 4637 | Lo = LegalizeOp(Lo); |
| 4638 | Hi = LegalizeOp(Hi); |
| 4639 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4640 | |
| 4641 | // Remember in a map if the values will be reused later. |
| 4642 | bool isNew = |
| 4643 | ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 4644 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4647 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 4648 | /// two smaller values of MVT::Vector type. |
| 4649 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 4650 | SDOperand &Hi) { |
| 4651 | assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); |
| 4652 | SDNode *Node = Op.Val; |
| 4653 | unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); |
| 4654 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 4655 | unsigned NewNumElts = NumElements/2; |
| 4656 | SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); |
| 4657 | SDOperand TypeNode = *(Node->op_end()-1); |
| 4658 | |
| 4659 | // See if we already split it. |
| 4660 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4661 | = SplitNodes.find(Op); |
| 4662 | if (I != SplitNodes.end()) { |
| 4663 | Lo = I->second.first; |
| 4664 | Hi = I->second.second; |
| 4665 | return; |
| 4666 | } |
| 4667 | |
| 4668 | switch (Node->getOpcode()) { |
Chris Lattner | 3824e50 | 2006-04-14 06:08:35 +0000 | [diff] [blame] | 4669 | default: Node->dump(); assert(0 && "Unhandled operation in SplitVectorOp!"); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4670 | case ISD::VBUILD_VECTOR: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4671 | std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts); |
| 4672 | LoOps.push_back(NewNumEltsNode); |
| 4673 | LoOps.push_back(TypeNode); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4674 | Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4675 | |
| 4676 | std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2); |
| 4677 | HiOps.push_back(NewNumEltsNode); |
| 4678 | HiOps.push_back(TypeNode); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4679 | Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4680 | break; |
| 4681 | } |
| 4682 | case ISD::VADD: |
| 4683 | case ISD::VSUB: |
| 4684 | case ISD::VMUL: |
| 4685 | case ISD::VSDIV: |
| 4686 | case ISD::VUDIV: |
| 4687 | case ISD::VAND: |
| 4688 | case ISD::VOR: |
| 4689 | case ISD::VXOR: { |
| 4690 | SDOperand LL, LH, RL, RH; |
| 4691 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 4692 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 4693 | |
| 4694 | Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, |
| 4695 | NewNumEltsNode, TypeNode); |
| 4696 | Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, |
| 4697 | NewNumEltsNode, TypeNode); |
| 4698 | break; |
| 4699 | } |
| 4700 | case ISD::VLOAD: { |
| 4701 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4702 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
| 4703 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 4704 | |
| 4705 | Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4706 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; |
| 4707 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4708 | getIntPtrConstant(IncrementSize)); |
| 4709 | // FIXME: This creates a bogus srcvalue! |
| 4710 | Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4711 | |
| 4712 | // Build a factor node to remember that this load is independent of the |
| 4713 | // other one. |
| 4714 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4715 | Hi.getValue(1)); |
| 4716 | |
| 4717 | // Remember that we legalized the chain. |
| 4718 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | } |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 4721 | case ISD::VBIT_CONVERT: { |
| 4722 | // We know the result is a vector. The input may be either a vector or a |
| 4723 | // scalar value. |
| 4724 | if (Op.getOperand(0).getValueType() != MVT::Vector) { |
| 4725 | // Lower to a store/load. FIXME: this could be improved probably. |
| 4726 | SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType()); |
| 4727 | |
| 4728 | SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 4729 | Op.getOperand(0), Ptr, DAG.getSrcValue(0)); |
| 4730 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 4731 | St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0)); |
| 4732 | SplitVectorOp(St, Lo, Hi); |
| 4733 | } else { |
| 4734 | // If the input is a vector type, we have to either scalarize it, pack it |
| 4735 | // or convert it based on whether the input vector type is legal. |
| 4736 | SDNode *InVal = Node->getOperand(0).Val; |
| 4737 | unsigned NumElems = |
| 4738 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 4739 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 4740 | |
| 4741 | // If the input is from a single element vector, scalarize the vector, |
| 4742 | // then treat like a scalar. |
| 4743 | if (NumElems == 1) { |
| 4744 | SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT); |
| 4745 | Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar, |
| 4746 | Op.getOperand(1), Op.getOperand(2)); |
| 4747 | SplitVectorOp(Scalar, Lo, Hi); |
| 4748 | } else { |
| 4749 | // Split the input vector. |
| 4750 | SplitVectorOp(Op.getOperand(0), Lo, Hi); |
| 4751 | |
| 4752 | // Convert each of the pieces now. |
| 4753 | Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo, |
| 4754 | NewNumEltsNode, TypeNode); |
| 4755 | Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi, |
| 4756 | NewNumEltsNode, TypeNode); |
| 4757 | } |
| 4758 | break; |
| 4759 | } |
| 4760 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | // Remember in a map if the values will be reused later. |
| 4764 | bool isNew = |
| 4765 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 4766 | assert(isNew && "Value already expanded?!?"); |
| 4767 | } |
| 4768 | |
| 4769 | |
| 4770 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 4771 | /// equivalent operation that returns a scalar (e.g. F32) or packed value |
| 4772 | /// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right |
| 4773 | /// type for the result. |
| 4774 | SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, |
| 4775 | MVT::ValueType NewVT) { |
| 4776 | assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); |
| 4777 | SDNode *Node = Op.Val; |
| 4778 | |
| 4779 | // See if we already packed it. |
| 4780 | std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); |
| 4781 | if (I != PackedNodes.end()) return I->second; |
| 4782 | |
| 4783 | SDOperand Result; |
| 4784 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 4785 | default: |
| 4786 | Node->dump(); std::cerr << "\n"; |
| 4787 | assert(0 && "Unknown vector operation in PackVectorOp!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4788 | case ISD::VADD: |
| 4789 | case ISD::VSUB: |
| 4790 | case ISD::VMUL: |
| 4791 | case ISD::VSDIV: |
| 4792 | case ISD::VUDIV: |
| 4793 | case ISD::VAND: |
| 4794 | case ISD::VOR: |
| 4795 | case ISD::VXOR: |
| 4796 | Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), |
| 4797 | NewVT, |
| 4798 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4799 | PackVectorOp(Node->getOperand(1), NewVT)); |
| 4800 | break; |
| 4801 | case ISD::VLOAD: { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 4802 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 4803 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4804 | |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 4805 | Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4806 | |
| 4807 | // Remember that we legalized the chain. |
| 4808 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 4809 | break; |
| 4810 | } |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4811 | case ISD::VBUILD_VECTOR: |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4812 | if (Node->getOperand(0).getValueType() == NewVT) { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4813 | // Returning a scalar? |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4814 | Result = Node->getOperand(0); |
| 4815 | } else { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4816 | // Returning a BUILD_VECTOR? |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 4817 | |
| 4818 | // If all elements of the build_vector are undefs, return an undef. |
| 4819 | bool AllUndef = true; |
| 4820 | for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i) |
| 4821 | if (Node->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 4822 | AllUndef = false; |
| 4823 | break; |
| 4824 | } |
| 4825 | if (AllUndef) { |
| 4826 | Result = DAG.getNode(ISD::UNDEF, NewVT); |
| 4827 | } else { |
| 4828 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2); |
| 4829 | Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops); |
| 4830 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4831 | } |
| 4832 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 4833 | case ISD::VINSERT_VECTOR_ELT: |
| 4834 | if (!MVT::isVector(NewVT)) { |
| 4835 | // Returning a scalar? Must be the inserted element. |
| 4836 | Result = Node->getOperand(1); |
| 4837 | } else { |
| 4838 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, |
| 4839 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4840 | Node->getOperand(1), Node->getOperand(2)); |
| 4841 | } |
| 4842 | break; |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 4843 | case ISD::VVECTOR_SHUFFLE: |
| 4844 | if (!MVT::isVector(NewVT)) { |
| 4845 | // Returning a scalar? Figure out if it is the LHS or RHS and return it. |
| 4846 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 4847 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 4848 | Result = PackVectorOp(Node->getOperand(1), NewVT); |
| 4849 | else |
| 4850 | Result = PackVectorOp(Node->getOperand(0), NewVT); |
| 4851 | } else { |
| 4852 | // Otherwise, return a VECTOR_SHUFFLE node. First convert the index |
| 4853 | // vector from a VBUILD_VECTOR to a BUILD_VECTOR. |
| 4854 | std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(), |
| 4855 | Node->getOperand(2).Val->op_end()-2); |
| 4856 | MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size()); |
| 4857 | SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx); |
| 4858 | |
| 4859 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, |
| 4860 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4861 | PackVectorOp(Node->getOperand(1), NewVT), BV); |
| 4862 | } |
| 4863 | break; |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 4864 | case ISD::VBIT_CONVERT: |
| 4865 | if (Op.getOperand(0).getValueType() != MVT::Vector) |
| 4866 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
| 4867 | else { |
| 4868 | // If the input is a vector type, we have to either scalarize it, pack it |
| 4869 | // or convert it based on whether the input vector type is legal. |
| 4870 | SDNode *InVal = Node->getOperand(0).Val; |
| 4871 | unsigned NumElems = |
| 4872 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 4873 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 4874 | |
| 4875 | // Figure out if there is a Packed type corresponding to this Vector |
| 4876 | // type. If so, convert to the packed type. |
| 4877 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 4878 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 4879 | // Turn this into a bit convert of the packed input. |
| 4880 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 4881 | PackVectorOp(Node->getOperand(0), TVT)); |
| 4882 | break; |
| 4883 | } else if (NumElems == 1) { |
| 4884 | // Turn this into a bit convert of the scalar input. |
| 4885 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 4886 | PackVectorOp(Node->getOperand(0), EVT)); |
| 4887 | break; |
| 4888 | } else { |
| 4889 | // FIXME: UNIMP! |
| 4890 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 4891 | } |
| 4892 | } |
Evan Cheng | db3c626 | 2006-04-10 18:54:36 +0000 | [diff] [blame] | 4893 | break; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 4894 | case ISD::VSELECT: |
| 4895 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
| 4896 | PackVectorOp(Op.getOperand(1), NewVT), |
| 4897 | PackVectorOp(Op.getOperand(2), NewVT)); |
| 4898 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | if (TLI.isTypeLegal(NewVT)) |
| 4902 | Result = LegalizeOp(Result); |
| 4903 | bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; |
| 4904 | assert(isNew && "Value already packed?"); |
| 4905 | return Result; |
| 4906 | } |
| 4907 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4908 | |
| 4909 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 4910 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4911 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 4912 | if (ViewLegalizeDAGs) viewGraph(); |
| 4913 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4914 | /// run - This is the main entry point to this class. |
| 4915 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4916 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |