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 | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 829 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 830 | case ISD::BUILD_VECTOR: |
| 831 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 832 | default: assert(0 && "This action is not supported yet!"); |
| 833 | case TargetLowering::Custom: |
| 834 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 835 | if (Tmp3.Val) { |
| 836 | Result = Tmp3; |
| 837 | break; |
| 838 | } |
| 839 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 840 | case TargetLowering::Expand: |
| 841 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 842 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 843 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 844 | break; |
| 845 | case ISD::INSERT_VECTOR_ELT: |
| 846 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 847 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 848 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 849 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 850 | |
| 851 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 852 | Node->getValueType(0))) { |
| 853 | default: assert(0 && "This action is not supported yet!"); |
| 854 | case TargetLowering::Legal: |
| 855 | break; |
| 856 | case TargetLowering::Custom: |
| 857 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 858 | if (Tmp3.Val) { |
| 859 | Result = Tmp3; |
| 860 | break; |
| 861 | } |
| 862 | // FALLTHROUGH |
| 863 | case TargetLowering::Expand: { |
| 864 | // If the target doesn't support this, we have to spill the input vector |
| 865 | // to a temporary stack slot, update the element, then reload it. This is |
| 866 | // badness. We could also load the value into a vector register (either |
| 867 | // with a "move to register" or "extload into register" instruction, then |
| 868 | // permute it into place, if the idx is a constant and if the idx is |
| 869 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 870 | MVT::ValueType VT = Tmp1.getValueType(); |
| 871 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 872 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 873 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 874 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 875 | // Store the vector. |
| 876 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 877 | Tmp1, StackPtr, DAG.getSrcValue(NULL)); |
| 878 | |
| 879 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 880 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 881 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 882 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 883 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 884 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 885 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 886 | // Store the scalar value. |
| 887 | Ch = DAG.getNode(ISD::STORE, MVT::Other, Ch, |
| 888 | Tmp2, StackPtr2, DAG.getSrcValue(NULL)); |
| 889 | // Load the updated vector. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 890 | Result = DAG.getLoad(VT, Ch, StackPtr, DAG.getSrcValue(NULL)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 891 | break; |
| 892 | } |
| 893 | } |
| 894 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 895 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 896 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 897 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 898 | break; |
| 899 | } |
| 900 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 901 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 902 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 903 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 904 | Node->getValueType(0))) { |
| 905 | default: assert(0 && "This action is not supported yet!"); |
| 906 | case TargetLowering::Legal: |
| 907 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 908 | case TargetLowering::Custom: |
| 909 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 910 | if (Tmp3.Val) { |
| 911 | Result = Tmp3; |
| 912 | break; |
| 913 | } |
| 914 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 915 | case TargetLowering::Expand: |
| 916 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 919 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 920 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 921 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 922 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 923 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 924 | |
| 925 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 926 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 927 | default: assert(0 && "Unknown operation action!"); |
| 928 | case TargetLowering::Legal: |
| 929 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 930 | "vector shuffle should not be created if not legal!"); |
| 931 | break; |
| 932 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 933 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 934 | if (Tmp3.Val) { |
| 935 | Result = Tmp3; |
| 936 | break; |
| 937 | } |
| 938 | // FALLTHROUGH |
| 939 | case TargetLowering::Expand: { |
| 940 | MVT::ValueType VT = Node->getValueType(0); |
| 941 | MVT::ValueType EltVT = MVT::getVectorBaseType(VT); |
| 942 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 943 | SDOperand Mask = Node->getOperand(2); |
| 944 | unsigned NumElems = Mask.getNumOperands(); |
| 945 | std::vector<SDOperand> Ops; |
| 946 | for (unsigned i = 0; i != NumElems; ++i) { |
| 947 | SDOperand Arg = Mask.getOperand(i); |
| 948 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 949 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 950 | } else { |
| 951 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 952 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 953 | if (Idx < NumElems) |
| 954 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 955 | DAG.getConstant(Idx, PtrVT))); |
| 956 | else |
| 957 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 958 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 959 | } |
| 960 | } |
| 961 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, Ops); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 962 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 963 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 964 | case TargetLowering::Promote: { |
| 965 | // Change base type to a different vector type. |
| 966 | MVT::ValueType OVT = Node->getValueType(0); |
| 967 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 968 | |
| 969 | // Cast the two input vectors. |
| 970 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 971 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 972 | |
| 973 | // Convert the shuffle mask to the right # elements. |
| 974 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 975 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 976 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 977 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 978 | break; |
| 979 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 980 | } |
| 981 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 982 | |
| 983 | case ISD::EXTRACT_VECTOR_ELT: |
| 984 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 985 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 986 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 987 | |
| 988 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, |
| 989 | Tmp1.getValueType())) { |
| 990 | default: assert(0 && "This action is not supported yet!"); |
| 991 | case TargetLowering::Legal: |
| 992 | break; |
| 993 | case TargetLowering::Custom: |
| 994 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 995 | if (Tmp3.Val) { |
| 996 | Result = Tmp3; |
| 997 | break; |
| 998 | } |
| 999 | // FALLTHROUGH |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 1000 | case TargetLowering::Expand: |
| 1001 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 1006 | case ISD::VEXTRACT_VECTOR_ELT: |
| 1007 | Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1008 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1010 | case ISD::CALLSEQ_START: { |
| 1011 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1012 | |
| 1013 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1014 | // to this call start. This ensures that any libcalls that need be inserted |
| 1015 | // are inserted *before* the CALLSEQ_START. |
| 1016 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
| 1017 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node); |
| 1018 | |
| 1019 | // Now that we legalized all of the inputs (which may have inserted |
| 1020 | // libcalls) create the new CALLSEQ_START node. |
| 1021 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1022 | |
| 1023 | // Merge in the last call, to ensure that this call start after the last |
| 1024 | // call ended. |
| 1025 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1026 | Tmp1 = LegalizeOp(Tmp1); |
| 1027 | |
| 1028 | // Do not try to legalize the target-specific arguments (#1+). |
| 1029 | if (Tmp1 != Node->getOperand(0)) { |
| 1030 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1031 | Ops[0] = Tmp1; |
| 1032 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1033 | } |
| 1034 | |
| 1035 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1036 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1037 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1038 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1039 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1040 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1041 | // sequence have been legalized, legalize the call itself. During this |
| 1042 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1043 | // can overlap. |
| 1044 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1045 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1046 | // Note that we are selecting this call! |
| 1047 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1048 | IsLegalizingCall = true; |
| 1049 | |
| 1050 | // Legalize the call, starting from the CALLSEQ_END. |
| 1051 | LegalizeOp(LastCALLSEQ_END); |
| 1052 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1053 | return Result; |
| 1054 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1055 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1056 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1057 | // will cause this node to be legalized as well as handling libcalls right. |
| 1058 | if (LastCALLSEQ_END.Val != Node) { |
| 1059 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
| 1060 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 1061 | assert(I != LegalizedNodes.end() && |
| 1062 | "Legalizing the call start should have legalized this node!"); |
| 1063 | return I->second; |
| 1064 | } |
| 1065 | |
| 1066 | // Otherwise, the call start has been legalized and everything is going |
| 1067 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1068 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1069 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1070 | // an optional flag input. |
| 1071 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1072 | if (Tmp1 != Node->getOperand(0)) { |
| 1073 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1074 | Ops[0] = Tmp1; |
| 1075 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1076 | } |
| 1077 | } else { |
| 1078 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1079 | if (Tmp1 != Node->getOperand(0) || |
| 1080 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
| 1081 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1082 | Ops[0] = Tmp1; |
| 1083 | Ops.back() = Tmp2; |
| 1084 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 1085 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1086 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1087 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1088 | // This finishes up call legalization. |
| 1089 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1090 | |
| 1091 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1092 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1093 | if (Node->getNumValues() == 2) |
| 1094 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1095 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1096 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1097 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1098 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1099 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1100 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1102 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1103 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1104 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1105 | Node->getValueType(0))) { |
| 1106 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1107 | case TargetLowering::Expand: { |
| 1108 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1109 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1110 | " not tell us which reg is the stack pointer!"); |
| 1111 | SDOperand Chain = Tmp1.getOperand(0); |
| 1112 | SDOperand Size = Tmp2.getOperand(1); |
| 1113 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 1114 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 1115 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1116 | Tmp1 = LegalizeOp(Tmp1); |
| 1117 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1118 | break; |
| 1119 | } |
| 1120 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1121 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1122 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1123 | Tmp1 = LegalizeOp(Tmp3); |
| 1124 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1125 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1126 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1127 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1128 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1129 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1130 | // Since this op produce two values, make sure to remember that we |
| 1131 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1132 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1133 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1134 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1135 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1136 | case ISD::INLINEASM: |
| 1137 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain. |
| 1138 | Tmp2 = Node->getOperand(Node->getNumOperands()-1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1139 | if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists. |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1140 | Tmp2 = Tmp3 = SDOperand(0, 0); |
| 1141 | else |
| 1142 | Tmp3 = LegalizeOp(Tmp2); |
| 1143 | |
| 1144 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) { |
| 1145 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 1146 | Ops[0] = Tmp1; |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1147 | if (Tmp3.Val) Ops.back() = Tmp3; |
| 1148 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | // 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] | 1152 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1153 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1154 | return Result.getValue(Op.ResNo); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1155 | case ISD::BR: |
| 1156 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1157 | // Ensure that libcalls are emitted before a branch. |
| 1158 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1159 | Tmp1 = LegalizeOp(Tmp1); |
| 1160 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1161 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1162 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1165 | case ISD::BRCOND: |
| 1166 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1167 | // Ensure that libcalls are emitted before a return. |
| 1168 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1169 | Tmp1 = LegalizeOp(Tmp1); |
| 1170 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1171 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1172 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1173 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1174 | case Legal: |
| 1175 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1176 | break; |
| 1177 | case Promote: |
| 1178 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 1179 | break; |
| 1180 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1183 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1184 | |
| 1185 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1186 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1187 | case TargetLowering::Legal: break; |
| 1188 | case TargetLowering::Custom: |
| 1189 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1190 | if (Tmp1.Val) Result = Tmp1; |
| 1191 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1192 | case TargetLowering::Expand: |
| 1193 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1194 | // Node. |
| 1195 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1196 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1197 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1198 | Node->getOperand(2)); |
| 1199 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1200 | // Make sure the condition is either zero or one. It may have been |
| 1201 | // promoted from something else. |
Chris Lattner | 19c5c4c | 2006-01-31 05:04:52 +0000 | [diff] [blame] | 1202 | unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType()); |
| 1203 | if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1)) |
| 1204 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1205 | |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1206 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1207 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1208 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1209 | Node->getOperand(2)); |
| 1210 | } |
| 1211 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1212 | } |
| 1213 | break; |
| 1214 | case ISD::BR_CC: |
| 1215 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1216 | // Ensure that libcalls are emitted before a branch. |
| 1217 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1218 | Tmp1 = LegalizeOp(Tmp1); |
| 1219 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1220 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1221 | Tmp2 = Node->getOperand(2); // LHS |
| 1222 | Tmp3 = Node->getOperand(3); // RHS |
| 1223 | Tmp4 = Node->getOperand(1); // CC |
| 1224 | |
| 1225 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
| 1226 | |
| 1227 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1228 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1229 | // the result against zero to select between true and false values. |
| 1230 | if (Tmp3.Val == 0) { |
| 1231 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1232 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1233 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1234 | |
| 1235 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1236 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1237 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1238 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1239 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1240 | case TargetLowering::Legal: break; |
| 1241 | case TargetLowering::Custom: |
| 1242 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1243 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1244 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1245 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1246 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1247 | case ISD::LOAD: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1248 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1249 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1250 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1251 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1252 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1253 | Tmp2 = Result.getValue(0); |
| 1254 | Tmp3 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1255 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1256 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1257 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1258 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1259 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1260 | Tmp1 = TLI.LowerOperation(Tmp2, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1261 | if (Tmp1.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1262 | Tmp2 = LegalizeOp(Tmp1); |
| 1263 | Tmp3 = LegalizeOp(Tmp1.getValue(1)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1264 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1265 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1266 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1267 | // Since loads produce two values, make sure to remember that we |
| 1268 | // legalized both of them. |
| 1269 | AddLegalizedOperand(SDOperand(Node, 0), Tmp2); |
| 1270 | AddLegalizedOperand(SDOperand(Node, 1), Tmp3); |
| 1271 | return Op.ResNo ? Tmp3 : Tmp2; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1272 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1273 | case ISD::EXTLOAD: |
| 1274 | case ISD::SEXTLOAD: |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1275 | case ISD::ZEXTLOAD: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1276 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1277 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1279 | MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1280 | switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1281 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 1c51c6a | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 1282 | case TargetLowering::Promote: |
| 1283 | assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1284 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1285 | DAG.getValueType(MVT::i8)); |
| 1286 | Tmp1 = Result.getValue(0); |
| 1287 | Tmp2 = Result.getValue(1); |
| 1288 | break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1289 | case TargetLowering::Custom: |
| 1290 | isCustom = true; |
| 1291 | // FALLTHROUGH |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1292 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1293 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1294 | Node->getOperand(3)); |
| 1295 | Tmp1 = Result.getValue(0); |
| 1296 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1297 | |
| 1298 | if (isCustom) { |
| 1299 | Tmp3 = TLI.LowerOperation(Tmp3, DAG); |
| 1300 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1301 | Tmp1 = LegalizeOp(Tmp3); |
| 1302 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1303 | } |
| 1304 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1305 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1306 | case TargetLowering::Expand: |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 1307 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1308 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1309 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); |
Andrew Lenharth | 3155908 | 2005-06-30 19:32:57 +0000 | [diff] [blame] | 1310 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1311 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1312 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1313 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1314 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1315 | assert(Node->getOpcode() != ISD::EXTLOAD && |
| 1316 | "EXTLOAD should always be supported!"); |
| 1317 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1318 | // zero/sign extend inreg. |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1319 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1320 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1321 | SDOperand ValRes; |
| 1322 | if (Node->getOpcode() == ISD::SEXTLOAD) |
| 1323 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1324 | Result, DAG.getValueType(SrcVT)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1325 | else |
| 1326 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1327 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1328 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1329 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1330 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1331 | // Since loads produce two values, make sure to remember that we legalized |
| 1332 | // both of them. |
| 1333 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1334 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1335 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1336 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1337 | case ISD::EXTRACT_ELEMENT: { |
| 1338 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1339 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1340 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1341 | case Legal: |
| 1342 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1343 | // 1 -> Hi |
| 1344 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1345 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1346 | TLI.getShiftAmountTy())); |
| 1347 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1348 | } else { |
| 1349 | // 0 -> Lo |
| 1350 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1351 | Node->getOperand(0)); |
| 1352 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1353 | break; |
| 1354 | case Expand: |
| 1355 | // Get both the low and high parts. |
| 1356 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1357 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1358 | Result = Tmp2; // 1 -> Hi |
| 1359 | else |
| 1360 | Result = Tmp1; // 0 -> Lo |
| 1361 | break; |
| 1362 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1363 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1364 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1365 | |
| 1366 | case ISD::CopyToReg: |
| 1367 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1369 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1370 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1371 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1372 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1373 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1374 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1375 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1376 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1377 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1378 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1379 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1380 | Tmp3); |
| 1381 | } else { |
| 1382 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | // Since this produces two values, make sure to remember that we legalized |
| 1386 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1387 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1388 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1389 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1390 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | |
| 1393 | case ISD::RET: |
| 1394 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Ensure that libcalls are emitted before a return. |
| 1397 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1398 | Tmp1 = LegalizeOp(Tmp1); |
| 1399 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1400 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1401 | switch (Node->getNumOperands()) { |
| 1402 | case 2: // ret val |
| 1403 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1404 | case Legal: |
| 1405 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1406 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1407 | break; |
| 1408 | case Expand: { |
| 1409 | SDOperand Lo, Hi; |
| 1410 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 1411 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1412 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1413 | } |
| 1414 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1415 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1416 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1417 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1418 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1419 | } |
| 1420 | break; |
| 1421 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1422 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1423 | break; |
| 1424 | default: { // ret <values> |
| 1425 | std::vector<SDOperand> NewValues; |
| 1426 | NewValues.push_back(Tmp1); |
| 1427 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1428 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1429 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1430 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | case Expand: { |
| 1433 | SDOperand Lo, Hi; |
| 1434 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1435 | NewValues.push_back(Lo); |
| 1436 | NewValues.push_back(Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1437 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1438 | } |
| 1439 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1440 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1441 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1442 | |
| 1443 | if (NewValues.size() == Node->getNumOperands()) |
| 1444 | Result = DAG.UpdateNodeOperands(Result, NewValues); |
| 1445 | else |
| 1446 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1447 | break; |
| 1448 | } |
| 1449 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1451 | if (Result.getOpcode() == ISD::RET) { |
| 1452 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1453 | default: assert(0 && "This action is not supported yet!"); |
| 1454 | case TargetLowering::Legal: break; |
| 1455 | case TargetLowering::Custom: |
| 1456 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1457 | if (Tmp1.Val) Result = Tmp1; |
| 1458 | break; |
| 1459 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1460 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1461 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1462 | case ISD::STORE: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1463 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1464 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1465 | |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1466 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1467 | // FIXME: We shouldn't do this for TargetConstantFP's. |
Chris Lattner | 06ac6ab | 2006-03-15 22:19:18 +0000 | [diff] [blame] | 1468 | // FIXME: move this to the DAG Combiner! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1469 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1470 | if (CFP->getValueType(0) == MVT::f32) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1471 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1472 | } else { |
| 1473 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1474 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1475 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1476 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
| 1477 | Node->getOperand(3)); |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1478 | break; |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1481 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1482 | case Legal: { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1483 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
| 1484 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1485 | Node->getOperand(3)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1486 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1487 | MVT::ValueType VT = Tmp3.getValueType(); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1488 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1489 | default: assert(0 && "This action is not supported yet!"); |
| 1490 | case TargetLowering::Legal: break; |
| 1491 | case TargetLowering::Custom: |
| 1492 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1493 | if (Tmp1.Val) Result = Tmp1; |
| 1494 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1495 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1499 | // Truncate the value and store the result. |
| 1500 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 1501 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1502 | Node->getOperand(3), |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1503 | DAG.getValueType(Node->getOperand(1).getValueType())); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1506 | case Expand: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1507 | unsigned IncrementSize = 0; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1508 | SDOperand Lo, Hi; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1509 | |
| 1510 | // If this is a vector type, then we have to calculate the increment as |
| 1511 | // the product of the element size in bytes, and the number of elements |
| 1512 | // in the high half of the vector. |
| 1513 | if (Node->getOperand(1).getValueType() == MVT::Vector) { |
| 1514 | SDNode *InVal = Node->getOperand(1).Val; |
| 1515 | unsigned NumElems = |
| 1516 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1517 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1518 | |
| 1519 | // Figure out if there is a Packed type corresponding to this Vector |
| 1520 | // type. If so, convert to the packed type. |
| 1521 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1522 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1523 | // Turn this into a normal store of the packed type. |
| 1524 | Tmp3 = PackVectorOp(Node->getOperand(1), TVT); |
| 1525 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1526 | Node->getOperand(3)); |
| 1527 | break; |
| 1528 | } else if (NumElems == 1) { |
| 1529 | // Turn this into a normal store of the scalar type. |
| 1530 | Tmp3 = PackVectorOp(Node->getOperand(1), EVT); |
| 1531 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1532 | Node->getOperand(3)); |
Chris Lattner | 2ae2e98 | 2006-03-31 17:37:22 +0000 | [diff] [blame] | 1533 | // The scalarized value type may not be legal, e.g. it might require |
| 1534 | // promotion or expansion. Relegalize the scalar store. |
| 1535 | Result = LegalizeOp(Result); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1536 | break; |
| 1537 | } else { |
| 1538 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1539 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1540 | } |
| 1541 | } else { |
| 1542 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 1543 | IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1544 | |
Chris Lattner | d9731af | 2006-03-31 18:20:46 +0000 | [diff] [blame] | 1545 | if (!TLI.isLittleEndian()) |
| 1546 | std::swap(Lo, Hi); |
| 1547 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1549 | Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, |
| 1550 | Node->getOperand(3)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1551 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1552 | getIntPtrConstant(IncrementSize)); |
| 1553 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1554 | "Pointers must be legal!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1555 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1556 | // wrong. |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1557 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, |
| 1558 | Node->getOperand(3)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1559 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1560 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1561 | } |
| 1562 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1563 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1564 | case ISD::PCMARKER: |
| 1565 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1566 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1567 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1568 | case ISD::STACKSAVE: |
| 1569 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1570 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1571 | Tmp1 = Result.getValue(0); |
| 1572 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1574 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1575 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1576 | case TargetLowering::Legal: break; |
| 1577 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1578 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1579 | if (Tmp3.Val) { |
| 1580 | Tmp1 = LegalizeOp(Tmp3); |
| 1581 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1582 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1583 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1584 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1585 | // Expand to CopyFromReg if the target set |
| 1586 | // StackPointerRegisterToSaveRestore. |
| 1587 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1588 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1589 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1590 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1591 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1592 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1593 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1594 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1595 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1596 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Since stacksave produce two values, make sure to remember that we |
| 1599 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1600 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1601 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1602 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1603 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1604 | case ISD::STACKRESTORE: |
| 1605 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1606 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1607 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1608 | |
| 1609 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1610 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1611 | case TargetLowering::Legal: break; |
| 1612 | case TargetLowering::Custom: |
| 1613 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1614 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1617 | // Expand to CopyToReg if the target set |
| 1618 | // StackPointerRegisterToSaveRestore. |
| 1619 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1620 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1621 | } else { |
| 1622 | Result = Tmp1; |
| 1623 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | } |
| 1626 | break; |
| 1627 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1628 | case ISD::READCYCLECOUNTER: |
| 1629 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1630 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1631 | |
| 1632 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1633 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1634 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1635 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1636 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1637 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1638 | case ISD::TRUNCSTORE: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1639 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1640 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1641 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1642 | assert(isTypeLegal(Node->getOperand(1).getValueType()) && |
| 1643 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1644 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1645 | |
| 1646 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1647 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1648 | if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 && |
| 1649 | TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) == |
| 1650 | TargetLowering::Promote) { |
| 1651 | // Promote the bool to a mask then store. |
| 1652 | Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2, |
| 1653 | DAG.getConstant(1, Tmp2.getValueType())); |
| 1654 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
| 1655 | Node->getOperand(3), DAG.getValueType(MVT::i8)); |
Chris Lattner | 13d58e7 | 2005-09-10 00:20:18 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1657 | } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1658 | Tmp3 != Node->getOperand(2)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1659 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 1660 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1661 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1662 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1663 | MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT(); |
| 1664 | switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) { |
| 1665 | default: assert(0 && "This action is not supported yet!"); |
| 1666 | case TargetLowering::Legal: break; |
| 1667 | case TargetLowering::Custom: |
| 1668 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1669 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1670 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1671 | } |
| 1672 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1673 | } |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1674 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1675 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1676 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1677 | case Legal: |
| 1678 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1679 | break; |
| 1680 | case Promote: |
| 1681 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1682 | break; |
| 1683 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1684 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1685 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1687 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1688 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1689 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1690 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1691 | case TargetLowering::Legal: break; |
| 1692 | case TargetLowering::Custom: { |
| 1693 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1694 | if (Tmp1.Val) Result = Tmp1; |
| 1695 | break; |
| 1696 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1697 | case TargetLowering::Expand: |
| 1698 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 1699 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 1700 | Tmp2, Tmp3, |
| 1701 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1702 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1703 | // Make sure the condition is either zero or one. It may have been |
| 1704 | // promoted from something else. |
Chris Lattner | 0e753d6 | 2006-01-30 04:22:28 +0000 | [diff] [blame] | 1705 | unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType()); |
| 1706 | if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1)) |
| 1707 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1708 | Result = DAG.getSelectCC(Tmp1, |
| 1709 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1710 | Tmp2, Tmp3, ISD::SETNE); |
| 1711 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1712 | break; |
| 1713 | case TargetLowering::Promote: { |
| 1714 | MVT::ValueType NVT = |
| 1715 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1716 | unsigned ExtOp, TruncOp; |
| 1717 | if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1718 | ExtOp = ISD::ANY_EXTEND; |
| 1719 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1720 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1721 | ExtOp = ISD::FP_EXTEND; |
| 1722 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1723 | } |
| 1724 | // Promote each of the values to the new type. |
| 1725 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 1726 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 1727 | // Perform the larger operation, then round down. |
| 1728 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 1729 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 1730 | break; |
| 1731 | } |
| 1732 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1733 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1734 | case ISD::SELECT_CC: { |
| 1735 | Tmp1 = Node->getOperand(0); // LHS |
| 1736 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1737 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 1738 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1739 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1740 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1741 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 1742 | |
| 1743 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1744 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1745 | // the result against zero to select between true and false values. |
| 1746 | if (Tmp2.Val == 0) { |
| 1747 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 1748 | CC = DAG.getCondCode(ISD::SETNE); |
| 1749 | } |
| 1750 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 1751 | |
| 1752 | // Everything is legal, see if we should expand this op or something. |
| 1753 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 1754 | default: assert(0 && "This action is not supported yet!"); |
| 1755 | case TargetLowering::Legal: break; |
| 1756 | case TargetLowering::Custom: |
| 1757 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1758 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1759 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1760 | } |
| 1761 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1762 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1763 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1764 | Tmp1 = Node->getOperand(0); |
| 1765 | Tmp2 = Node->getOperand(1); |
| 1766 | Tmp3 = Node->getOperand(2); |
| 1767 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 1768 | |
| 1769 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 1770 | // not always be possible to return a true LHS & RHS. In this case, just |
| 1771 | // return the value we legalized, returned in the LHS |
| 1772 | if (Tmp2.Val == 0) { |
| 1773 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1774 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1775 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 1777 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1778 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 1779 | case TargetLowering::Custom: |
| 1780 | isCustom = true; |
| 1781 | // FALLTHROUGH. |
| 1782 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1783 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1784 | if (isCustom) { |
| 1785 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1786 | if (Tmp3.Val) Result = Tmp3; |
| 1787 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1788 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1789 | case TargetLowering::Promote: { |
| 1790 | // First step, figure out the appropriate operation to use. |
| 1791 | // Allow SETCC to not be supported for all legal data types |
| 1792 | // Mostly this targets FP |
| 1793 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
| 1794 | MVT::ValueType OldVT = NewInTy; |
| 1795 | |
| 1796 | // Scan for the appropriate larger type to use. |
| 1797 | while (1) { |
| 1798 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 1799 | |
| 1800 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 1801 | "Fell off of the edge of the integer world"); |
| 1802 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 1803 | "Fell off of the edge of the floating point world"); |
| 1804 | |
| 1805 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 1806 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | } |
| 1809 | if (MVT::isInteger(NewInTy)) |
| 1810 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 1811 | else { |
| 1812 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 1813 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 1814 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1815 | Tmp1 = LegalizeOp(Tmp1); |
| 1816 | Tmp2 = LegalizeOp(Tmp2); |
| 1817 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 1818 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 1819 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1820 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1821 | case TargetLowering::Expand: |
| 1822 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 1823 | // rhs that selects between const 1 (true) and const 0 (false). |
| 1824 | MVT::ValueType VT = Node->getValueType(0); |
| 1825 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 1826 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
| 1827 | Node->getOperand(2)); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1828 | break; |
| 1829 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1830 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1831 | case ISD::MEMSET: |
| 1832 | case ISD::MEMCPY: |
| 1833 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1834 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1835 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 1836 | |
| 1837 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 1838 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 1839 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 1840 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1841 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1842 | break; |
| 1843 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1844 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1845 | break; |
| 1846 | } |
| 1847 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1848 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1849 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1850 | |
| 1851 | SDOperand Tmp4; |
| 1852 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 1853 | case Expand: { |
| 1854 | // Length is too big, just take the lo-part of the length. |
| 1855 | SDOperand HiPart; |
| 1856 | ExpandOp(Node->getOperand(3), HiPart, Tmp4); |
| 1857 | break; |
| 1858 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1859 | case Legal: |
| 1860 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1861 | break; |
| 1862 | case Promote: |
| 1863 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | } |
| 1866 | |
| 1867 | SDOperand Tmp5; |
| 1868 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 1869 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 1870 | case Legal: |
| 1871 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 1872 | break; |
| 1873 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1874 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 1875 | break; |
| 1876 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1877 | |
| 1878 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 1879 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1880 | case TargetLowering::Custom: |
| 1881 | isCustom = true; |
| 1882 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1883 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1884 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1885 | if (isCustom) { |
| 1886 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1887 | if (Tmp1.Val) Result = Tmp1; |
| 1888 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1889 | break; |
| 1890 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1891 | // Otherwise, the target does not support this operation. Lower the |
| 1892 | // operation to an explicit libcall as appropriate. |
| 1893 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1894 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 1895 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1896 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 1897 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1898 | if (Node->getOpcode() == ISD::MEMSET) { |
| 1899 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 1900 | // Extend the (previously legalized) ubyte argument to be an int value |
| 1901 | // for the call. |
| 1902 | if (Tmp3.getValueType() > MVT::i32) |
| 1903 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 1904 | else |
| 1905 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1906 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 1907 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1908 | |
| 1909 | FnName = "memset"; |
| 1910 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 1911 | Node->getOpcode() == ISD::MEMMOVE) { |
| 1912 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 1913 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 1914 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1915 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 1916 | } else { |
| 1917 | assert(0 && "Unknown op!"); |
| 1918 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1919 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1920 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1921 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1922 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1923 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1924 | break; |
| 1925 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1926 | } |
| 1927 | break; |
| 1928 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1930 | case ISD::SHL_PARTS: |
| 1931 | case ISD::SRA_PARTS: |
| 1932 | case ISD::SRL_PARTS: { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1933 | std::vector<SDOperand> Ops; |
| 1934 | bool Changed = false; |
| 1935 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1936 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1937 | Changed |= Ops.back() != Node->getOperand(i); |
| 1938 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1939 | if (Changed) |
| 1940 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1941 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1942 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1943 | Node->getValueType(0))) { |
| 1944 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1945 | case TargetLowering::Legal: break; |
| 1946 | case TargetLowering::Custom: |
| 1947 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1948 | if (Tmp1.Val) { |
| 1949 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1950 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1951 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1952 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 1953 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 1954 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1955 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 1956 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1957 | return RetVal; |
| 1958 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | } |
| 1961 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1962 | // Since these produce multiple values, make sure to remember that we |
| 1963 | // legalized all of them. |
| 1964 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 1965 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 1966 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1967 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1968 | |
| 1969 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1970 | case ISD::ADD: |
| 1971 | case ISD::SUB: |
| 1972 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 1973 | case ISD::MULHS: |
| 1974 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1975 | case ISD::UDIV: |
| 1976 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1977 | case ISD::AND: |
| 1978 | case ISD::OR: |
| 1979 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 1980 | case ISD::SHL: |
| 1981 | case ISD::SRL: |
| 1982 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1983 | case ISD::FADD: |
| 1984 | case ISD::FSUB: |
| 1985 | case ISD::FMUL: |
| 1986 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1987 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 1988 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1989 | case Expand: assert(0 && "Not possible"); |
| 1990 | case Legal: |
| 1991 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 1992 | break; |
| 1993 | case Promote: |
| 1994 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 1995 | break; |
| 1996 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1997 | |
| 1998 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1999 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2000 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2001 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2002 | case TargetLowering::Legal: break; |
| 2003 | case TargetLowering::Custom: |
| 2004 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2005 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2006 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2007 | case TargetLowering::Expand: { |
| 2008 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2009 | "Cannot expand this binary operator!"); |
| 2010 | // Expand the operation into a bunch of nasty scalar code. |
| 2011 | std::vector<SDOperand> Ops; |
| 2012 | MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0)); |
| 2013 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2014 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2015 | i != e; ++i) { |
| 2016 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2017 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2018 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2019 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2020 | } |
| 2021 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), Ops); |
| 2022 | break; |
| 2023 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2024 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2025 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2026 | |
| 2027 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2028 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2029 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2030 | case Expand: assert(0 && "Not possible"); |
| 2031 | case Legal: |
| 2032 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2033 | break; |
| 2034 | case Promote: |
| 2035 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2036 | break; |
| 2037 | } |
| 2038 | |
| 2039 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2040 | |
| 2041 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2042 | default: assert(0 && "Operation not supported"); |
| 2043 | case TargetLowering::Custom: |
| 2044 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2045 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2046 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2047 | case TargetLowering::Legal: break; |
| 2048 | case TargetLowering::Expand: |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2049 | // If this target supports fabs/fneg natively, do this efficiently. |
| 2050 | if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) && |
| 2051 | TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) { |
| 2052 | // Get the sign bit of the RHS. |
| 2053 | MVT::ValueType IVT = |
| 2054 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2055 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2056 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2057 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2058 | // Get the absolute value of the result. |
| 2059 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2060 | // Select between the nabs and abs value based on the sign bit of |
| 2061 | // the input. |
| 2062 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2063 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2064 | AbsVal), |
| 2065 | AbsVal); |
| 2066 | Result = LegalizeOp(Result); |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | // Otherwise, do bitwise ops! |
| 2071 | |
| 2072 | // copysign -> copysignf/copysign libcall. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2073 | const char *FnName; |
| 2074 | if (Node->getValueType(0) == MVT::f32) { |
| 2075 | FnName = "copysignf"; |
| 2076 | if (Tmp2.getValueType() != MVT::f32) // Force operands to match type. |
| 2077 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2078 | DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2)); |
| 2079 | } else { |
| 2080 | FnName = "copysign"; |
| 2081 | if (Tmp2.getValueType() != MVT::f64) // Force operands to match type. |
| 2082 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2083 | DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2)); |
| 2084 | } |
| 2085 | SDOperand Dummy; |
| 2086 | Result = ExpandLibCall(FnName, Node, Dummy); |
| 2087 | break; |
| 2088 | } |
| 2089 | break; |
| 2090 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2091 | case ISD::ADDC: |
| 2092 | case ISD::SUBC: |
| 2093 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2094 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2095 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2096 | // Since this produces two values, make sure to remember that we legalized |
| 2097 | // both of them. |
| 2098 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2099 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2100 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2101 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2102 | case ISD::ADDE: |
| 2103 | case ISD::SUBE: |
| 2104 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2105 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2106 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2107 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2108 | // Since this produces two values, make sure to remember that we legalized |
| 2109 | // both of them. |
| 2110 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2111 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2112 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2113 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2114 | case ISD::BUILD_PAIR: { |
| 2115 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2116 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2117 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2118 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2119 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2120 | case TargetLowering::Promote: |
| 2121 | case TargetLowering::Custom: |
| 2122 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2123 | case TargetLowering::Legal: |
| 2124 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2125 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2126 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2127 | case TargetLowering::Expand: |
| 2128 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2129 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2130 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2131 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2132 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2133 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2134 | break; |
| 2135 | } |
| 2136 | break; |
| 2137 | } |
| 2138 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2139 | case ISD::UREM: |
| 2140 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2141 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2142 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2143 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2144 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2145 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2146 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2147 | case TargetLowering::Custom: |
| 2148 | isCustom = true; |
| 2149 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2150 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2151 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2152 | if (isCustom) { |
| 2153 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2154 | if (Tmp1.Val) Result = Tmp1; |
| 2155 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2156 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2157 | case TargetLowering::Expand: |
| 2158 | if (MVT::isInteger(Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2159 | // X % Y -> X-X/Y*Y |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2160 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2161 | unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2162 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 2163 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2164 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2165 | } else { |
| 2166 | // Floating point mod -> fmod libcall. |
| 2167 | const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; |
| 2168 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 2169 | Result = ExpandLibCall(FnName, Node, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2170 | } |
| 2171 | break; |
| 2172 | } |
| 2173 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2174 | case ISD::VAARG: { |
| 2175 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2176 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2177 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2178 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2179 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2180 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2181 | case TargetLowering::Custom: |
| 2182 | isCustom = true; |
| 2183 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2184 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2185 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2186 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2187 | Tmp1 = Result.getValue(1); |
| 2188 | |
| 2189 | if (isCustom) { |
| 2190 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2191 | if (Tmp2.Val) { |
| 2192 | Result = LegalizeOp(Tmp2); |
| 2193 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2194 | } |
| 2195 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2196 | break; |
| 2197 | case TargetLowering::Expand: { |
| 2198 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 2199 | Node->getOperand(2)); |
| 2200 | // Increment the pointer, VAList, to the next vaarg |
| 2201 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2202 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2203 | TLI.getPointerTy())); |
| 2204 | // Store the incremented VAList to the legalized pointer |
| 2205 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 2206 | Node->getOperand(2)); |
| 2207 | // Load the actual argument out of the pointer VAList |
| 2208 | Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2209 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2210 | Result = LegalizeOp(Result); |
| 2211 | break; |
| 2212 | } |
| 2213 | } |
| 2214 | // Since VAARG produces two values, make sure to remember that we |
| 2215 | // legalized both of them. |
| 2216 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2217 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2218 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | case ISD::VACOPY: |
| 2222 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2223 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2224 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2225 | |
| 2226 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2227 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2228 | case TargetLowering::Custom: |
| 2229 | isCustom = true; |
| 2230 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2231 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2232 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2233 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2234 | if (isCustom) { |
| 2235 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2236 | if (Tmp1.Val) Result = Tmp1; |
| 2237 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2238 | break; |
| 2239 | case TargetLowering::Expand: |
| 2240 | // This defaults to loading a pointer from the input and storing it to the |
| 2241 | // output, returning the chain. |
| 2242 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3)); |
| 2243 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2, |
| 2244 | Node->getOperand(4)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | break; |
| 2248 | |
| 2249 | case ISD::VAEND: |
| 2250 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2251 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2252 | |
| 2253 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2254 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2255 | case TargetLowering::Custom: |
| 2256 | isCustom = true; |
| 2257 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2258 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2259 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2260 | if (isCustom) { |
| 2261 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2262 | if (Tmp1.Val) Result = Tmp1; |
| 2263 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2264 | break; |
| 2265 | case TargetLowering::Expand: |
| 2266 | Result = Tmp1; // Default to a no-op, return the chain |
| 2267 | break; |
| 2268 | } |
| 2269 | break; |
| 2270 | |
| 2271 | case ISD::VASTART: |
| 2272 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2273 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2274 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2275 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2276 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2277 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2278 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2279 | case TargetLowering::Legal: break; |
| 2280 | case TargetLowering::Custom: |
| 2281 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2282 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2283 | break; |
| 2284 | } |
| 2285 | break; |
| 2286 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2287 | case ISD::ROTL: |
| 2288 | case ISD::ROTR: |
| 2289 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2290 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2291 | |
| 2292 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
| 2293 | "Cannot handle this yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2294 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2297 | case ISD::BSWAP: |
| 2298 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2299 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2300 | case TargetLowering::Custom: |
| 2301 | assert(0 && "Cannot custom legalize this yet!"); |
| 2302 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2303 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2304 | break; |
| 2305 | case TargetLowering::Promote: { |
| 2306 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2307 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2308 | unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2309 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2310 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2311 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2312 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2313 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2314 | break; |
| 2315 | } |
| 2316 | case TargetLowering::Expand: |
| 2317 | Result = ExpandBSWAP(Tmp1); |
| 2318 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2319 | } |
| 2320 | break; |
| 2321 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2322 | case ISD::CTPOP: |
| 2323 | case ISD::CTTZ: |
| 2324 | case ISD::CTLZ: |
| 2325 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2326 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2327 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2328 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2329 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | case TargetLowering::Promote: { |
| 2332 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2333 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2334 | |
| 2335 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2336 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2337 | // Perform the larger operation, then subtract if needed. |
| 2338 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2339 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2340 | case ISD::CTPOP: |
| 2341 | Result = Tmp1; |
| 2342 | break; |
| 2343 | case ISD::CTTZ: |
| 2344 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2345 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
| 2346 | DAG.getConstant(getSizeInBits(NVT), NVT), |
| 2347 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2348 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2349 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 2350 | break; |
| 2351 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2352 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2353 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2354 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2355 | getSizeInBits(OVT), NVT)); |
| 2356 | break; |
| 2357 | } |
| 2358 | break; |
| 2359 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2360 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2361 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2362 | break; |
| 2363 | } |
| 2364 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2366 | // Unary operators |
| 2367 | case ISD::FABS: |
| 2368 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2369 | case ISD::FSQRT: |
| 2370 | case ISD::FSIN: |
| 2371 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2372 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2373 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2374 | case TargetLowering::Promote: |
| 2375 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2376 | isCustom = true; |
| 2377 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2378 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2379 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2380 | if (isCustom) { |
| 2381 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2382 | if (Tmp1.Val) Result = Tmp1; |
| 2383 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2384 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2385 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2386 | switch (Node->getOpcode()) { |
| 2387 | default: assert(0 && "Unreachable!"); |
| 2388 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2389 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2390 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2391 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2392 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2393 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2394 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2395 | MVT::ValueType VT = Node->getValueType(0); |
| 2396 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2397 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2398 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2399 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2400 | break; |
| 2401 | } |
| 2402 | case ISD::FSQRT: |
| 2403 | case ISD::FSIN: |
| 2404 | case ISD::FCOS: { |
| 2405 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2406 | const char *FnName = 0; |
| 2407 | switch(Node->getOpcode()) { |
| 2408 | case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; |
| 2409 | case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; |
| 2410 | case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; |
| 2411 | default: assert(0 && "Unreachable!"); |
| 2412 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2413 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 2414 | Result = ExpandLibCall(FnName, Node, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2417 | } |
| 2418 | break; |
| 2419 | } |
| 2420 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2421 | |
| 2422 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2423 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2424 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2425 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2426 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2427 | Node->getOperand(0).getValueType())) { |
| 2428 | default: assert(0 && "Unknown operation action!"); |
| 2429 | case TargetLowering::Expand: |
| 2430 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2431 | break; |
| 2432 | case TargetLowering::Legal: |
| 2433 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2434 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2435 | break; |
| 2436 | } |
| 2437 | } |
| 2438 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 2439 | case ISD::VBIT_CONVERT: { |
| 2440 | assert(Op.getOperand(0).getValueType() == MVT::Vector && |
| 2441 | "Can only have VBIT_CONVERT where input or output is MVT::Vector!"); |
| 2442 | |
| 2443 | // The input has to be a vector type, we have to either scalarize it, pack |
| 2444 | // it, or convert it based on whether the input vector type is legal. |
| 2445 | SDNode *InVal = Node->getOperand(0).Val; |
| 2446 | unsigned NumElems = |
| 2447 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 2448 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 2449 | |
| 2450 | // Figure out if there is a Packed type corresponding to this Vector |
| 2451 | // type. If so, convert to the packed type. |
| 2452 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 2453 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 2454 | // Turn this into a bit convert of the packed input. |
| 2455 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2456 | PackVectorOp(Node->getOperand(0), TVT)); |
| 2457 | break; |
| 2458 | } else if (NumElems == 1) { |
| 2459 | // Turn this into a bit convert of the scalar input. |
| 2460 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2461 | PackVectorOp(Node->getOperand(0), EVT)); |
| 2462 | break; |
| 2463 | } else { |
| 2464 | // FIXME: UNIMP! Store then reload |
| 2465 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 2466 | } |
| 2467 | } |
| 2468 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2469 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2470 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2471 | case ISD::UINT_TO_FP: { |
| 2472 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2473 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2474 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2475 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2476 | Node->getOperand(0).getValueType())) { |
| 2477 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2478 | case TargetLowering::Custom: |
| 2479 | isCustom = true; |
| 2480 | // FALLTHROUGH |
| 2481 | case TargetLowering::Legal: |
| 2482 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2483 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2484 | if (isCustom) { |
| 2485 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2486 | if (Tmp1.Val) Result = Tmp1; |
| 2487 | } |
| 2488 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2489 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2490 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2491 | LegalizeOp(Node->getOperand(0)), |
| 2492 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2493 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2494 | case TargetLowering::Promote: |
| 2495 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2496 | Node->getValueType(0), |
| 2497 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2498 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2499 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2500 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2501 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2502 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2503 | Node->getValueType(0), Node->getOperand(0)); |
| 2504 | break; |
| 2505 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2506 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2507 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2508 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2509 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2510 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2511 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2512 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2513 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2514 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2515 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2516 | break; |
| 2517 | } |
| 2518 | break; |
| 2519 | } |
| 2520 | case ISD::TRUNCATE: |
| 2521 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2522 | case Legal: |
| 2523 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2524 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2525 | break; |
| 2526 | case Expand: |
| 2527 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2528 | |
| 2529 | // Since the result is legal, we should just be able to truncate the low |
| 2530 | // part of the source. |
| 2531 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2532 | break; |
| 2533 | case Promote: |
| 2534 | Result = PromoteOp(Node->getOperand(0)); |
| 2535 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2536 | break; |
| 2537 | } |
| 2538 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2539 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2540 | case ISD::FP_TO_SINT: |
| 2541 | case ISD::FP_TO_UINT: |
| 2542 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2543 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2544 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2545 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2546 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2547 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2548 | case TargetLowering::Custom: |
| 2549 | isCustom = true; |
| 2550 | // FALLTHROUGH |
| 2551 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2552 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2553 | if (isCustom) { |
| 2554 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2555 | if (Tmp1.Val) Result = Tmp1; |
| 2556 | } |
| 2557 | break; |
| 2558 | case TargetLowering::Promote: |
| 2559 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2560 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2561 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2562 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2563 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2564 | SDOperand True, False; |
| 2565 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2566 | MVT::ValueType NVT = Node->getValueType(0); |
| 2567 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2568 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2569 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2570 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2571 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2572 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2573 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2574 | Tmp2)); |
| 2575 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2576 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2577 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2578 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2579 | } else { |
| 2580 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2581 | } |
| 2582 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2583 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2584 | break; |
| 2585 | case Expand: |
| 2586 | assert(0 && "Shouldn't need to expand other operators here!"); |
| 2587 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2588 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2589 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 2590 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
| 2593 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2595 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2596 | case ISD::ZERO_EXTEND: |
| 2597 | case ISD::SIGN_EXTEND: |
| 2598 | case ISD::FP_EXTEND: |
| 2599 | case ISD::FP_ROUND: |
| 2600 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2601 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2602 | case Legal: |
| 2603 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2604 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2605 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2606 | case Promote: |
| 2607 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2608 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2609 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2610 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2611 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2612 | case ISD::ZERO_EXTEND: |
| 2613 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2614 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2615 | Result = DAG.getZeroExtendInReg(Result, |
| 2616 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2617 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2618 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2619 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2620 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2621 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2622 | Result, |
| 2623 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2624 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2625 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2626 | Result = PromoteOp(Node->getOperand(0)); |
| 2627 | if (Result.getValueType() != Op.getValueType()) |
| 2628 | // Dynamically dead while we have only 2 FP types. |
| 2629 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 2630 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2631 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 2632 | Result = PromoteOp(Node->getOperand(0)); |
| 2633 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 2634 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2635 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2636 | } |
| 2637 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2638 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2639 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2640 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2641 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2642 | |
| 2643 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 2644 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2645 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 2646 | default: assert(0 && "This action not supported for this op yet!"); |
| 2647 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2648 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2649 | break; |
| 2650 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2651 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2652 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2653 | // NOTE: we could fall back on load/store here too for targets without |
| 2654 | // SAR. However, it is doubtful that any exist. |
| 2655 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 2656 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2657 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2658 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 2659 | Node->getOperand(0), ShiftCst); |
| 2660 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 2661 | Result, ShiftCst); |
| 2662 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 2663 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 2664 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 2665 | |
| 2666 | // NOTE: there is a choice here between constantly creating new stack |
| 2667 | // slots and always reusing the same one. We currently always create |
| 2668 | // new ones, as reuse may inhibit scheduling. |
| 2669 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 2670 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 2671 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 2672 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2673 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2674 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 2675 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 2676 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2677 | Node->getOperand(0), StackSlot, |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 2678 | DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 2679 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 2680 | Result, StackSlot, DAG.getSrcValue(NULL), |
| 2681 | ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2682 | } else { |
| 2683 | assert(0 && "Unknown op"); |
| 2684 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2685 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2686 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2687 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2688 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2689 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 2691 | assert(Result.getValueType() == Op.getValueType() && |
| 2692 | "Bad legalization!"); |
| 2693 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2694 | // Make sure that the generated code is itself legal. |
| 2695 | if (Result != Op) |
| 2696 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2698 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 2699 | // means that we always must cache transformed nodes. |
| 2700 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2701 | return Result; |
| 2702 | } |
| 2703 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2704 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 2705 | /// promote it to compute the value into a larger type. The produced value will |
| 2706 | /// have the correct bits for the low portion of the register, but no guarantee |
| 2707 | /// 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] | 2708 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 2709 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 2710 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2711 | assert(getTypeAction(VT) == Promote && |
| 2712 | "Caller should expand or legalize operands that are not promotable!"); |
| 2713 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 2714 | "Cannot promote to smaller type!"); |
| 2715 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2716 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2717 | SDOperand Result; |
| 2718 | SDNode *Node = Op.Val; |
| 2719 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 2720 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 2721 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2722 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2723 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2724 | case ISD::CopyFromReg: |
| 2725 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2726 | default: |
| 2727 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 2728 | assert(0 && "Do not know how to promote this operator!"); |
| 2729 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 2730 | case ISD::UNDEF: |
| 2731 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 2732 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2733 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 2734 | if (VT != MVT::i1) |
| 2735 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 2736 | else |
| 2737 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2738 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 2739 | break; |
| 2740 | case ISD::ConstantFP: |
| 2741 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 2742 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 2743 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 2744 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2745 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2746 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2747 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 2748 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2749 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2750 | |
| 2751 | case ISD::TRUNCATE: |
| 2752 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2753 | case Legal: |
| 2754 | Result = LegalizeOp(Node->getOperand(0)); |
| 2755 | assert(Result.getValueType() >= NVT && |
| 2756 | "This truncation doesn't make sense!"); |
| 2757 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 2758 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 2759 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 2760 | case Promote: |
| 2761 | // The truncation is not required, because we don't guarantee anything |
| 2762 | // about high bits anyway. |
| 2763 | Result = PromoteOp(Node->getOperand(0)); |
| 2764 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2765 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 2766 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2767 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 2768 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2769 | } |
| 2770 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2771 | case ISD::SIGN_EXTEND: |
| 2772 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2773 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2774 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2775 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 2776 | case Legal: |
| 2777 | // 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] | 2778 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2779 | break; |
| 2780 | case Promote: |
| 2781 | // Promote the reg if it's smaller. |
| 2782 | Result = PromoteOp(Node->getOperand(0)); |
| 2783 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 2784 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 2785 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2786 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2787 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2788 | Result = DAG.getZeroExtendInReg(Result, |
| 2789 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | } |
| 2792 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2793 | case ISD::BIT_CONVERT: |
| 2794 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2795 | Result = PromoteOp(Result); |
| 2796 | break; |
| 2797 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2798 | case ISD::FP_EXTEND: |
| 2799 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 2800 | case ISD::FP_ROUND: |
| 2801 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2802 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 2803 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 2804 | case Legal: |
| 2805 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2806 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2807 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2808 | break; |
| 2809 | } |
| 2810 | break; |
| 2811 | |
| 2812 | case ISD::SINT_TO_FP: |
| 2813 | case ISD::UINT_TO_FP: |
| 2814 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2815 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2816 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2817 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2818 | break; |
| 2819 | |
| 2820 | case Promote: |
| 2821 | Result = PromoteOp(Node->getOperand(0)); |
| 2822 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 2823 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2824 | Result, |
| 2825 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2826 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2827 | Result = DAG.getZeroExtendInReg(Result, |
| 2828 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2829 | // No extra round required here. |
| 2830 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2831 | break; |
| 2832 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2833 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 2834 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2835 | // Round if we cannot tolerate excess precision. |
| 2836 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2837 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2838 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2839 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2840 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2841 | break; |
| 2842 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 2843 | case ISD::SIGN_EXTEND_INREG: |
| 2844 | Result = PromoteOp(Node->getOperand(0)); |
| 2845 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 2846 | Node->getOperand(1)); |
| 2847 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2848 | case ISD::FP_TO_SINT: |
| 2849 | case ISD::FP_TO_UINT: |
| 2850 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2851 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2852 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | case Promote: |
| 2855 | // The input result is prerounded, so we don't have to do anything |
| 2856 | // special. |
| 2857 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2858 | break; |
| 2859 | case Expand: |
| 2860 | assert(0 && "not implemented"); |
| 2861 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2862 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 2863 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 2864 | // we can use that instead. This allows us to generate better code for |
| 2865 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 2866 | // legal, such as PowerPC. |
| 2867 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2868 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 2869 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 2870 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2871 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 2872 | } else { |
| 2873 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2874 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2875 | break; |
| 2876 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2877 | case ISD::FABS: |
| 2878 | case ISD::FNEG: |
| 2879 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2880 | assert(Tmp1.getValueType() == NVT); |
| 2881 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2882 | // NOTE: we do not have to do any extra rounding here for |
| 2883 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 2884 | // precision, and these operations don't modify precision at all. |
| 2885 | break; |
| 2886 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2887 | case ISD::FSQRT: |
| 2888 | case ISD::FSIN: |
| 2889 | case ISD::FCOS: |
| 2890 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2891 | assert(Tmp1.getValueType() == NVT); |
| 2892 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2893 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2894 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2895 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2896 | break; |
| 2897 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2898 | case ISD::AND: |
| 2899 | case ISD::OR: |
| 2900 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2901 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2902 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2903 | case ISD::MUL: |
| 2904 | // 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] | 2905 | // 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] | 2906 | // that too is okay if they are integer operations. |
| 2907 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2908 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2909 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 2910 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2911 | break; |
| 2912 | case ISD::FADD: |
| 2913 | case ISD::FSUB: |
| 2914 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2915 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2916 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2917 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 2918 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2919 | |
| 2920 | // Floating point operations will give excess precision that we may not be |
| 2921 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 2922 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2923 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 2924 | // 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] | 2925 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2926 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2927 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2928 | break; |
| 2929 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2930 | case ISD::SDIV: |
| 2931 | case ISD::SREM: |
| 2932 | // These operators require that their input be sign extended. |
| 2933 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2934 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2935 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2936 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 2937 | DAG.getValueType(VT)); |
| 2938 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 2939 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2940 | } |
| 2941 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2942 | |
| 2943 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 2944 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2945 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2946 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2947 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2948 | case ISD::FDIV: |
| 2949 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2950 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2951 | // These operators require that their input be fp extended. |
| 2952 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2953 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2954 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2955 | |
| 2956 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2957 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2958 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2959 | DAG.getValueType(VT)); |
| 2960 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2961 | |
| 2962 | case ISD::UDIV: |
| 2963 | case ISD::UREM: |
| 2964 | // These operators require that their input be zero extended. |
| 2965 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2966 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2967 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2968 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 2969 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2970 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2971 | break; |
| 2972 | |
| 2973 | case ISD::SHL: |
| 2974 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2975 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2976 | break; |
| 2977 | case ISD::SRA: |
| 2978 | // The input value must be properly sign extended. |
| 2979 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2980 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 2981 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2982 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2983 | break; |
| 2984 | case ISD::SRL: |
| 2985 | // The input value must be properly zero extended. |
| 2986 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2987 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2988 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2989 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 2990 | |
| 2991 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2992 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 2993 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 2994 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 2995 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 2996 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 2997 | } else { |
| 2998 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 2999 | Node->getOperand(2)); |
| 3000 | // Increment the pointer, VAList, to the next vaarg |
| 3001 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3002 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3003 | TLI.getPointerTy())); |
| 3004 | // Store the incremented VAList to the legalized pointer |
| 3005 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 3006 | Node->getOperand(2)); |
| 3007 | // Load the actual argument out of the pointer VAList |
| 3008 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, |
| 3009 | DAG.getSrcValue(0), VT); |
| 3010 | } |
| 3011 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3012 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3015 | case ISD::LOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3016 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0), |
| 3017 | Node->getOperand(1), Node->getOperand(2), VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3018 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3019 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3020 | break; |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3021 | case ISD::SEXTLOAD: |
| 3022 | case ISD::ZEXTLOAD: |
| 3023 | case ISD::EXTLOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3024 | Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0), |
| 3025 | Node->getOperand(1), Node->getOperand(2), |
Chris Lattner | 8136cda | 2005-10-15 20:24:07 +0000 | [diff] [blame] | 3026 | cast<VTSDNode>(Node->getOperand(3))->getVT()); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3027 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3028 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 3029 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3030 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3031 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3032 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3033 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3034 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3035 | case ISD::SELECT_CC: |
| 3036 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3037 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3038 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3039 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3040 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3041 | case ISD::BSWAP: |
| 3042 | Tmp1 = Node->getOperand(0); |
| 3043 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3044 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3045 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3046 | DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), |
| 3047 | TLI.getShiftAmountTy())); |
| 3048 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3049 | case ISD::CTPOP: |
| 3050 | case ISD::CTTZ: |
| 3051 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3052 | // Zero extend the argument |
| 3053 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3054 | // Perform the larger operation, then subtract if needed. |
| 3055 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3056 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3057 | case ISD::CTPOP: |
| 3058 | Result = Tmp1; |
| 3059 | break; |
| 3060 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3061 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3062 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3063 | DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3064 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3065 | DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3066 | break; |
| 3067 | case ISD::CTLZ: |
| 3068 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3069 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 3070 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3071 | getSizeInBits(VT), NVT)); |
| 3072 | break; |
| 3073 | } |
| 3074 | break; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3075 | case ISD::VEXTRACT_VECTOR_ELT: |
| 3076 | Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
| 3077 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3078 | case ISD::EXTRACT_VECTOR_ELT: |
| 3079 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3080 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3084 | |
| 3085 | // Make sure the result is itself legal. |
| 3086 | Result = LegalizeOp(Result); |
| 3087 | |
| 3088 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3089 | AddPromotedOperand(Op, Result); |
| 3090 | return Result; |
| 3091 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3092 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3093 | /// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a |
| 3094 | /// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based |
| 3095 | /// on the vector type. The return type of this matches the element type of the |
| 3096 | /// vector, which may not be legal for the target. |
| 3097 | SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3098 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3099 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3100 | // lower to a store then an indexed load. |
| 3101 | SDOperand Vec = Op.getOperand(0); |
| 3102 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3103 | |
| 3104 | SDNode *InVal = Vec.Val; |
| 3105 | unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 3106 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 3107 | |
| 3108 | // Figure out if there is a Packed type corresponding to this Vector |
| 3109 | // type. If so, convert to the packed type. |
| 3110 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3111 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 3112 | // Turn this into a packed extract_vector_elt operation. |
| 3113 | Vec = PackVectorOp(Vec, TVT); |
| 3114 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx); |
| 3115 | } else if (NumElems == 1) { |
| 3116 | // This must be an access of the only element. Return it. |
| 3117 | return PackVectorOp(Vec, EVT); |
| 3118 | } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) { |
| 3119 | SDOperand Lo, Hi; |
| 3120 | SplitVectorOp(Vec, Lo, Hi); |
| 3121 | if (CIdx->getValue() < NumElems/2) { |
| 3122 | Vec = Lo; |
| 3123 | } else { |
| 3124 | Vec = Hi; |
| 3125 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3126 | } |
| 3127 | |
| 3128 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3129 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3130 | return LowerVEXTRACT_VECTOR_ELT(Op); |
| 3131 | } else { |
| 3132 | // Variable index case for extract element. |
| 3133 | // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!! |
| 3134 | assert(0 && "unimp!"); |
| 3135 | return SDOperand(); |
| 3136 | } |
| 3137 | } |
| 3138 | |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3139 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3140 | /// memory traffic. |
| 3141 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3142 | SDOperand Vector = Op.getOperand(0); |
| 3143 | SDOperand Idx = Op.getOperand(1); |
| 3144 | |
| 3145 | // If the target doesn't support this, store the value to a temporary |
| 3146 | // stack slot, then LOAD the scalar element back out. |
| 3147 | SDOperand StackPtr = CreateStackTemporary(Vector.getValueType()); |
| 3148 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3149 | Vector, StackPtr, DAG.getSrcValue(NULL)); |
| 3150 | |
| 3151 | // Add the offset to the index. |
| 3152 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3153 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3154 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3155 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3156 | |
| 3157 | return DAG.getLoad(Op.getValueType(), Ch, StackPtr, DAG.getSrcValue(NULL)); |
| 3158 | } |
| 3159 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3160 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3161 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3162 | /// with condition CC on the current target. This usually involves legalizing |
| 3163 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3164 | /// there may be no choice but to create a new SetCC node to represent the |
| 3165 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3166 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3167 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3168 | SDOperand &RHS, |
| 3169 | SDOperand &CC) { |
| 3170 | SDOperand Tmp1, Tmp2, Result; |
| 3171 | |
| 3172 | switch (getTypeAction(LHS.getValueType())) { |
| 3173 | case Legal: |
| 3174 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3175 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3176 | break; |
| 3177 | case Promote: |
| 3178 | Tmp1 = PromoteOp(LHS); // LHS |
| 3179 | Tmp2 = PromoteOp(RHS); // RHS |
| 3180 | |
| 3181 | // If this is an FP compare, the operands have already been extended. |
| 3182 | if (MVT::isInteger(LHS.getValueType())) { |
| 3183 | MVT::ValueType VT = LHS.getValueType(); |
| 3184 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3185 | |
| 3186 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3187 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3188 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3189 | // it. |
| 3190 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3191 | default: assert(0 && "Unknown integer comparison!"); |
| 3192 | case ISD::SETEQ: |
| 3193 | case ISD::SETNE: |
| 3194 | case ISD::SETUGE: |
| 3195 | case ISD::SETUGT: |
| 3196 | case ISD::SETULE: |
| 3197 | case ISD::SETULT: |
| 3198 | // ALL of these operations will work if we either sign or zero extend |
| 3199 | // the operands (including the unsigned comparisons!). Zero extend is |
| 3200 | // usually a simpler/cheaper operation, so prefer it. |
| 3201 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3202 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 3203 | break; |
| 3204 | case ISD::SETGE: |
| 3205 | case ISD::SETGT: |
| 3206 | case ISD::SETLT: |
| 3207 | case ISD::SETLE: |
| 3208 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3209 | DAG.getValueType(VT)); |
| 3210 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3211 | DAG.getValueType(VT)); |
| 3212 | break; |
| 3213 | } |
| 3214 | } |
| 3215 | break; |
| 3216 | case Expand: |
| 3217 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 3218 | ExpandOp(LHS, LHSLo, LHSHi); |
| 3219 | ExpandOp(RHS, RHSLo, RHSHi); |
| 3220 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3221 | case ISD::SETEQ: |
| 3222 | case ISD::SETNE: |
| 3223 | if (RHSLo == RHSHi) |
| 3224 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 3225 | if (RHSCST->isAllOnesValue()) { |
| 3226 | // Comparison to -1. |
| 3227 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 3228 | Tmp2 = RHSLo; |
| 3229 | break; |
| 3230 | } |
| 3231 | |
| 3232 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 3233 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 3234 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3235 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3236 | break; |
| 3237 | default: |
| 3238 | // If this is a comparison of the sign bit, just look at the top part. |
| 3239 | // X > -1, x < 0 |
| 3240 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 3241 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 3242 | CST->getValue() == 0) || // X < 0 |
| 3243 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 3244 | CST->isAllOnesValue())) { // X > -1 |
| 3245 | Tmp1 = LHSHi; |
| 3246 | Tmp2 = RHSHi; |
| 3247 | break; |
| 3248 | } |
| 3249 | |
| 3250 | // FIXME: This generated code sucks. |
| 3251 | ISD::CondCode LowCC; |
| 3252 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3253 | default: assert(0 && "Unknown integer setcc!"); |
| 3254 | case ISD::SETLT: |
| 3255 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 3256 | case ISD::SETGT: |
| 3257 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 3258 | case ISD::SETLE: |
| 3259 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 3260 | case ISD::SETGE: |
| 3261 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 3262 | } |
| 3263 | |
| 3264 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 3265 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 3266 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 3267 | |
| 3268 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 3269 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
| 3270 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 3271 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 3272 | Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 3273 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 3274 | Result, Tmp1, Tmp2)); |
| 3275 | Tmp1 = Result; |
Nate Begeman | da06e9e | 2006-02-01 19:05:15 +0000 | [diff] [blame] | 3276 | Tmp2 = SDOperand(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3277 | } |
| 3278 | } |
| 3279 | LHS = Tmp1; |
| 3280 | RHS = Tmp2; |
| 3281 | } |
| 3282 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3283 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 3284 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 3285 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3286 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 3287 | SDOperand SrcOp) { |
| 3288 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3289 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3290 | |
| 3291 | // Emit a store to the stack slot. |
| 3292 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | ed7b5ba | 2005-12-23 00:50:25 +0000 | [diff] [blame] | 3293 | SrcOp, FIPtr, DAG.getSrcValue(NULL)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3294 | // Result is a load from the stack slot. |
| 3295 | return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0)); |
| 3296 | } |
| 3297 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3298 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 3299 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 3300 | // then load the whole vector back out. |
| 3301 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
| 3302 | SDOperand Ch = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3303 | Node->getOperand(0), StackPtr, |
| 3304 | DAG.getSrcValue(NULL)); |
| 3305 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,DAG.getSrcValue(NULL)); |
| 3306 | } |
| 3307 | |
| 3308 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3309 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
| 3310 | /// support the operation, but do support the resultant packed vector type. |
| 3311 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 3312 | |
| 3313 | // 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] | 3314 | // 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] | 3315 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3316 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3317 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3318 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 3319 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3320 | bool isConstant = true; |
| 3321 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 3322 | SplatValue.getOpcode() != ISD::UNDEF) |
| 3323 | isConstant = false; |
| 3324 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3325 | for (unsigned i = 1; i < NumElems; ++i) { |
| 3326 | SDOperand V = Node->getOperand(i); |
| 3327 | std::map<SDOperand, std::vector<unsigned> >::iterator I = Values.find(V); |
| 3328 | if (I != Values.end()) |
| 3329 | I->second.push_back(i); |
| 3330 | else |
| 3331 | Values[V].push_back(i); |
| 3332 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3333 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3334 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3335 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3336 | |
| 3337 | // If this isn't a constant element or an undef, we can't use a constant |
| 3338 | // pool load. |
| 3339 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 3340 | V.getOpcode() != ISD::UNDEF) |
| 3341 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | if (isOnlyLowElement) { |
| 3345 | // If the low element is an undef too, then this whole things is an undef. |
| 3346 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 3347 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 3348 | // Otherwise, turn this into a scalar_to_vector node. |
| 3349 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3350 | Node->getOperand(0)); |
| 3351 | } |
| 3352 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3353 | // If all elements are constants, create a load from the constant pool. |
| 3354 | if (isConstant) { |
| 3355 | MVT::ValueType VT = Node->getValueType(0); |
| 3356 | const Type *OpNTy = |
| 3357 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 3358 | std::vector<Constant*> CV; |
| 3359 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 3360 | if (ConstantFPSDNode *V = |
| 3361 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
| 3362 | CV.push_back(ConstantFP::get(OpNTy, V->getValue())); |
| 3363 | } else if (ConstantSDNode *V = |
| 3364 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
| 3365 | CV.push_back(ConstantUInt::get(OpNTy, V->getValue())); |
| 3366 | } else { |
| 3367 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 3368 | CV.push_back(UndefValue::get(OpNTy)); |
| 3369 | } |
| 3370 | } |
| 3371 | Constant *CP = ConstantPacked::get(CV); |
| 3372 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
| 3373 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 3374 | DAG.getSrcValue(NULL)); |
| 3375 | } |
| 3376 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3377 | if (SplatValue.Val) { // Splat of one value? |
| 3378 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 3379 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3380 | MVT::getIntVectorWithNumElements(NumElems); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3381 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3382 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3383 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec); |
| 3384 | |
| 3385 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3386 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3387 | // Get the splatted value into the low element of a vector register. |
| 3388 | SDOperand LowValVec = |
| 3389 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 3390 | |
| 3391 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 3392 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 3393 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 3394 | SplatMask); |
| 3395 | } |
| 3396 | } |
| 3397 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3398 | // If there are only two unique elements, we may be able to turn this into a |
| 3399 | // vector shuffle. |
| 3400 | if (Values.size() == 2) { |
| 3401 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 3402 | MVT::ValueType MaskVT = |
| 3403 | MVT::getIntVectorWithNumElements(NumElems); |
| 3404 | std::vector<SDOperand> MaskVec(NumElems); |
| 3405 | unsigned i = 0; |
| 3406 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3407 | E = Values.end(); I != E; ++I) { |
| 3408 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 3409 | EE = I->second.end(); II != EE; ++II) |
| 3410 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT)); |
| 3411 | i += NumElems; |
| 3412 | } |
| 3413 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec); |
| 3414 | |
| 3415 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3416 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 3417 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3418 | std::vector<SDOperand> Ops; |
| 3419 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3420 | E = Values.end(); I != E; ++I) { |
| 3421 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3422 | I->first); |
| 3423 | Ops.push_back(Op); |
| 3424 | } |
| 3425 | Ops.push_back(ShuffleMask); |
| 3426 | |
| 3427 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
| 3428 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops); |
| 3429 | } |
| 3430 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3431 | |
| 3432 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 3433 | // aligned object on the stack, store each element into it, then load |
| 3434 | // the result as a vector. |
| 3435 | MVT::ValueType VT = Node->getValueType(0); |
| 3436 | // Create the stack frame object. |
| 3437 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 3438 | |
| 3439 | // Emit a store of each element to the stack slot. |
| 3440 | std::vector<SDOperand> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3441 | unsigned TypeByteSize = |
| 3442 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
| 3443 | unsigned VectorSize = MVT::getSizeInBits(VT)/8; |
| 3444 | // Store (in the right endianness) the elements to memory. |
| 3445 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3446 | // Ignore undef elements. |
| 3447 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 3448 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 3449 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3450 | |
| 3451 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 3452 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 3453 | |
| 3454 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3455 | Node->getOperand(i), Idx, |
| 3456 | DAG.getSrcValue(NULL))); |
| 3457 | } |
| 3458 | |
| 3459 | SDOperand StoreChain; |
| 3460 | if (!Stores.empty()) // Not all undef elements? |
| 3461 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 3462 | else |
| 3463 | StoreChain = DAG.getEntryNode(); |
| 3464 | |
| 3465 | // Result is a load from the stack slot. |
| 3466 | return DAG.getLoad(VT, StoreChain, FIPtr, DAG.getSrcValue(0)); |
| 3467 | } |
| 3468 | |
| 3469 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 3470 | /// specified value type. |
| 3471 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 3472 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 3473 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
| 3474 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); |
| 3475 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 3476 | } |
| 3477 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3478 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 3479 | SDOperand Op, SDOperand Amt, |
| 3480 | SDOperand &Lo, SDOperand &Hi) { |
| 3481 | // Expand the subcomponents. |
| 3482 | SDOperand LHSL, LHSH; |
| 3483 | ExpandOp(Op, LHSL, LHSH); |
| 3484 | |
| 3485 | std::vector<SDOperand> Ops; |
| 3486 | Ops.push_back(LHSL); |
| 3487 | Ops.push_back(LHSH); |
| 3488 | Ops.push_back(Amt); |
Chris Lattner | cc0675a | 2005-08-30 17:21:17 +0000 | [diff] [blame] | 3489 | std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 3490 | Lo = DAG.getNode(NodeOp, VTs, Ops); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3491 | Hi = Lo.getValue(1); |
| 3492 | } |
| 3493 | |
| 3494 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3495 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 3496 | /// smaller elements. If we can't find a way that is more efficient than a |
| 3497 | /// libcall on this target, return false. Otherwise, return true with the |
| 3498 | /// low-parts expanded into Lo and Hi. |
| 3499 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 3500 | SDOperand &Lo, SDOperand &Hi) { |
| 3501 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 3502 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3503 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3504 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3505 | SDOperand ShAmt = LegalizeOp(Amt); |
| 3506 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 3507 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 3508 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 3509 | |
| 3510 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 3511 | // and are disabled. |
| 3512 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 3513 | unsigned Cst = CN->getValue(); |
| 3514 | // Expand the incoming operand to be shifted, so that we have its parts |
| 3515 | SDOperand InL, InH; |
| 3516 | ExpandOp(Op, InL, InH); |
| 3517 | switch(Opc) { |
| 3518 | case ISD::SHL: |
| 3519 | if (Cst > VTBits) { |
| 3520 | Lo = DAG.getConstant(0, NVT); |
| 3521 | Hi = DAG.getConstant(0, NVT); |
| 3522 | } else if (Cst > NVTBits) { |
| 3523 | Lo = DAG.getConstant(0, NVT); |
| 3524 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3525 | } else if (Cst == NVTBits) { |
| 3526 | Lo = DAG.getConstant(0, NVT); |
| 3527 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3528 | } else { |
| 3529 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 3530 | Hi = DAG.getNode(ISD::OR, NVT, |
| 3531 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 3532 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3533 | } |
| 3534 | return true; |
| 3535 | case ISD::SRL: |
| 3536 | if (Cst > VTBits) { |
| 3537 | Lo = DAG.getConstant(0, NVT); |
| 3538 | Hi = DAG.getConstant(0, NVT); |
| 3539 | } else if (Cst > NVTBits) { |
| 3540 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 3541 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3542 | } else if (Cst == NVTBits) { |
| 3543 | Lo = InH; |
| 3544 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3545 | } else { |
| 3546 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3547 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3548 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3549 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3550 | } |
| 3551 | return true; |
| 3552 | case ISD::SRA: |
| 3553 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3554 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3555 | DAG.getConstant(NVTBits-1, ShTy)); |
| 3556 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3557 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3558 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3559 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3560 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3561 | } else if (Cst == NVTBits) { |
| 3562 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3563 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3564 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3565 | } else { |
| 3566 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3567 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3568 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3569 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3570 | } |
| 3571 | return true; |
| 3572 | } |
| 3573 | } |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3574 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3575 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3576 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3577 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3578 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 3579 | // does not fit into a register, return the lo part and set the hi part to the |
| 3580 | // by-reg argument. If it does fit into a single register, return the result |
| 3581 | // and leave the Hi part unset. |
| 3582 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 3583 | SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3584 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 3585 | // The input chain to this libcall is the entry node of the function. |
| 3586 | // Legalizing the call will automatically add the previous call to the |
| 3587 | // dependence. |
| 3588 | SDOperand InChain = DAG.getEntryNode(); |
| 3589 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3590 | TargetLowering::ArgListTy Args; |
| 3591 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3592 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 3593 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 3594 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 3595 | } |
| 3596 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3597 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3598 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3599 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3600 | std::pair<SDOperand,SDOperand> CallInfo = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 3601 | TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, |
| 3602 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 3603 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3604 | // Legalize the call sequence, starting with the chain. This will advance |
| 3605 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 3606 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 3607 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3608 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3609 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3610 | default: assert(0 && "Unknown thing"); |
| 3611 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3612 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3613 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3614 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3615 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3616 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3617 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3618 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3621 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3622 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 3623 | /// destination type is legal. |
| 3624 | SDOperand SelectionDAGLegalize:: |
| 3625 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3626 | assert(isTypeLegal(DestTy) && "Destination type is not legal!"); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3627 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 3628 | "This is not an expansion!"); |
| 3629 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 3630 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3631 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3632 | assert(Source.getValueType() == MVT::i64 && |
| 3633 | "This only works for 64-bit -> FP"); |
| 3634 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 3635 | // incoming integer is set. To handle this, we dynamically test to see if |
| 3636 | // it is set, and, if so, add a fudge factor. |
| 3637 | SDOperand Lo, Hi; |
| 3638 | ExpandOp(Source, Lo, Hi); |
| 3639 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 3640 | // If this is unsigned, and not supported, first perform the conversion to |
| 3641 | // signed, then adjust the result if the sign bit is set. |
| 3642 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 3643 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 3644 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3645 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 3646 | DAG.getConstant(0, Hi.getValueType()), |
| 3647 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3648 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3649 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3650 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 3651 | uint64_t FF = 0x5f800000ULL; |
| 3652 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3653 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3654 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 3655 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3656 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3657 | SDOperand FudgeInReg; |
| 3658 | if (DestTy == MVT::f32) |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 3659 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3660 | DAG.getSrcValue(NULL)); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3661 | else { |
| 3662 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3663 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 3664 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3665 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 3666 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3667 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3668 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3669 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 3670 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 3671 | default: assert(0 && "This action not implemented for this operation!"); |
| 3672 | case TargetLowering::Legal: |
| 3673 | case TargetLowering::Expand: |
| 3674 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3675 | case TargetLowering::Custom: { |
| 3676 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 3677 | Source), DAG); |
| 3678 | if (NV.Val) |
| 3679 | return LegalizeOp(NV); |
| 3680 | break; // The target decided this was legal after all |
| 3681 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 3684 | // Expand the source, then glue it back together for the call. We must expand |
| 3685 | // the source in case it is shared (this pass of legalize must traverse it). |
| 3686 | SDOperand SrcLo, SrcHi; |
| 3687 | ExpandOp(Source, SrcLo, SrcHi); |
| 3688 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 3689 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3690 | const char *FnName = 0; |
| 3691 | if (DestTy == MVT::f32) |
| 3692 | FnName = "__floatdisf"; |
| 3693 | else { |
| 3694 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 3695 | FnName = "__floatdidf"; |
| 3696 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3697 | |
| 3698 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 3699 | SDOperand UnusedHiPart; |
Chris Lattner | 2512569 | 2006-02-17 04:32:33 +0000 | [diff] [blame] | 3700 | return ExpandLibCall(FnName, Source.Val, UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3701 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3702 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3703 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 3704 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 3705 | /// we expand it. At this point, we know that the result and operand types are |
| 3706 | /// legal for the target. |
| 3707 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 3708 | SDOperand Op0, |
| 3709 | MVT::ValueType DestVT) { |
| 3710 | if (Op0.getValueType() == MVT::i32) { |
| 3711 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 3712 | |
| 3713 | // get the stack frame index of a 8 byte buffer |
| 3714 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3715 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 3716 | // get address of 8 byte buffer |
| 3717 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 3718 | // word offset constant for Hi/Lo address computation |
| 3719 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 3720 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 3721 | SDOperand Hi = StackSlot; |
| 3722 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 3723 | if (TLI.isLittleEndian()) |
| 3724 | std::swap(Hi, Lo); |
| 3725 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3726 | // if signed map to unsigned space |
| 3727 | SDOperand Op0Mapped; |
| 3728 | if (isSigned) { |
| 3729 | // constant used to invert sign bit (signed to unsigned mapping) |
| 3730 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 3731 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 3732 | } else { |
| 3733 | Op0Mapped = Op0; |
| 3734 | } |
| 3735 | // store the lo of the constructed double - based on integer input |
| 3736 | SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3737 | Op0Mapped, Lo, DAG.getSrcValue(NULL)); |
| 3738 | // initial hi portion of constructed double |
| 3739 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 3740 | // store the hi of the constructed double - biased exponent |
| 3741 | SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1, |
| 3742 | InitialHi, Hi, DAG.getSrcValue(NULL)); |
| 3743 | // load the constructed double |
| 3744 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, |
| 3745 | DAG.getSrcValue(NULL)); |
| 3746 | // FP constant to bias correct the final result |
| 3747 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 3748 | BitsToDouble(0x4330000080000000ULL) |
| 3749 | : BitsToDouble(0x4330000000000000ULL), |
| 3750 | MVT::f64); |
| 3751 | // subtract the bias |
| 3752 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 3753 | // final result |
| 3754 | SDOperand Result; |
| 3755 | // handle final rounding |
| 3756 | if (DestVT == MVT::f64) { |
| 3757 | // do nothing |
| 3758 | Result = Sub; |
| 3759 | } else { |
| 3760 | // if f32 then cast to f32 |
| 3761 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 3762 | } |
| 3763 | return Result; |
| 3764 | } |
| 3765 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 3766 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 3767 | |
| 3768 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 3769 | DAG.getConstant(0, Op0.getValueType()), |
| 3770 | ISD::SETLT); |
| 3771 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3772 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3773 | SignSet, Four, Zero); |
| 3774 | |
| 3775 | // If the sign bit of the integer is set, the large number will be treated |
| 3776 | // as a negative number. To counteract this, the dynamic code adds an |
| 3777 | // offset depending on the data type. |
| 3778 | uint64_t FF; |
| 3779 | switch (Op0.getValueType()) { |
| 3780 | default: assert(0 && "Unsupported integer type!"); |
| 3781 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 3782 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 3783 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 3784 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 3785 | } |
| 3786 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3787 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
| 3788 | |
| 3789 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 3790 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3791 | SDOperand FudgeInReg; |
| 3792 | if (DestVT == MVT::f32) |
| 3793 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3794 | DAG.getSrcValue(NULL)); |
| 3795 | else { |
| 3796 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 3797 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 3798 | DAG.getEntryNode(), CPIdx, |
| 3799 | DAG.getSrcValue(NULL), MVT::f32)); |
| 3800 | } |
| 3801 | |
| 3802 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 3803 | } |
| 3804 | |
| 3805 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 3806 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 3807 | /// we promote it. At this point, we know that the result and operand types are |
| 3808 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 3809 | /// operation that takes a larger input. |
| 3810 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 3811 | MVT::ValueType DestVT, |
| 3812 | bool isSigned) { |
| 3813 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 3814 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 3815 | |
| 3816 | unsigned OpToUse = 0; |
| 3817 | |
| 3818 | // Scan for the appropriate larger type to use. |
| 3819 | while (1) { |
| 3820 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 3821 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 3822 | |
| 3823 | // If the target supports SINT_TO_FP of this type, use it. |
| 3824 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 3825 | default: break; |
| 3826 | case TargetLowering::Legal: |
| 3827 | if (!TLI.isTypeLegal(NewInTy)) |
| 3828 | break; // Can't use this datatype. |
| 3829 | // FALL THROUGH. |
| 3830 | case TargetLowering::Custom: |
| 3831 | OpToUse = ISD::SINT_TO_FP; |
| 3832 | break; |
| 3833 | } |
| 3834 | if (OpToUse) break; |
| 3835 | if (isSigned) continue; |
| 3836 | |
| 3837 | // If the target supports UINT_TO_FP of this type, use it. |
| 3838 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 3839 | default: break; |
| 3840 | case TargetLowering::Legal: |
| 3841 | if (!TLI.isTypeLegal(NewInTy)) |
| 3842 | break; // Can't use this datatype. |
| 3843 | // FALL THROUGH. |
| 3844 | case TargetLowering::Custom: |
| 3845 | OpToUse = ISD::UINT_TO_FP; |
| 3846 | break; |
| 3847 | } |
| 3848 | if (OpToUse) break; |
| 3849 | |
| 3850 | // Otherwise, try a larger type. |
| 3851 | } |
| 3852 | |
| 3853 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 3854 | // desired type then run the operation on it. |
| 3855 | return DAG.getNode(OpToUse, DestVT, |
| 3856 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 3857 | NewInTy, LegalOp)); |
| 3858 | } |
| 3859 | |
| 3860 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 3861 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 3862 | /// we promote it. At this point, we know that the result and operand types are |
| 3863 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 3864 | /// operation that returns a larger result. |
| 3865 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 3866 | MVT::ValueType DestVT, |
| 3867 | bool isSigned) { |
| 3868 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 3869 | MVT::ValueType NewOutTy = DestVT; |
| 3870 | |
| 3871 | unsigned OpToUse = 0; |
| 3872 | |
| 3873 | // Scan for the appropriate larger type to use. |
| 3874 | while (1) { |
| 3875 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 3876 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 3877 | |
| 3878 | // If the target supports FP_TO_SINT returning this type, use it. |
| 3879 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 3880 | default: break; |
| 3881 | case TargetLowering::Legal: |
| 3882 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3883 | break; // Can't use this datatype. |
| 3884 | // FALL THROUGH. |
| 3885 | case TargetLowering::Custom: |
| 3886 | OpToUse = ISD::FP_TO_SINT; |
| 3887 | break; |
| 3888 | } |
| 3889 | if (OpToUse) break; |
| 3890 | |
| 3891 | // If the target supports FP_TO_UINT of this type, use it. |
| 3892 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 3893 | default: break; |
| 3894 | case TargetLowering::Legal: |
| 3895 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3896 | break; // Can't use this datatype. |
| 3897 | // FALL THROUGH. |
| 3898 | case TargetLowering::Custom: |
| 3899 | OpToUse = ISD::FP_TO_UINT; |
| 3900 | break; |
| 3901 | } |
| 3902 | if (OpToUse) break; |
| 3903 | |
| 3904 | // Otherwise, try a larger type. |
| 3905 | } |
| 3906 | |
| 3907 | // Okay, we found the operation and type to use. Truncate the result of the |
| 3908 | // extended FP_TO_*INT operation to the desired size. |
| 3909 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 3910 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 3911 | } |
| 3912 | |
| 3913 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 3914 | /// |
| 3915 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 3916 | MVT::ValueType VT = Op.getValueType(); |
| 3917 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 3918 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 3919 | switch (VT) { |
| 3920 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 3921 | case MVT::i16: |
| 3922 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3923 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3924 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 3925 | case MVT::i32: |
| 3926 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3927 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3928 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3929 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3930 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 3931 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 3932 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 3933 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 3934 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 3935 | case MVT::i64: |
| 3936 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 3937 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 3938 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3939 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3940 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3941 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3942 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 3943 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 3944 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 3945 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 3946 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 3947 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 3948 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 3949 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 3950 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 3951 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 3952 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 3953 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 3954 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 3955 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 3956 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 3961 | /// |
| 3962 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 3963 | switch (Opc) { |
| 3964 | default: assert(0 && "Cannot expand this yet!"); |
| 3965 | case ISD::CTPOP: { |
| 3966 | static const uint64_t mask[6] = { |
| 3967 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 3968 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 3969 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 3970 | }; |
| 3971 | MVT::ValueType VT = Op.getValueType(); |
| 3972 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 3973 | unsigned len = getSizeInBits(VT); |
| 3974 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 3975 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 3976 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 3977 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 3978 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 3979 | DAG.getNode(ISD::AND, VT, |
| 3980 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 3981 | } |
| 3982 | return Op; |
| 3983 | } |
| 3984 | case ISD::CTLZ: { |
| 3985 | // for now, we do this: |
| 3986 | // x = x | (x >> 1); |
| 3987 | // x = x | (x >> 2); |
| 3988 | // ... |
| 3989 | // x = x | (x >>16); |
| 3990 | // x = x | (x >>32); // for 64-bit input |
| 3991 | // return popcount(~x); |
| 3992 | // |
| 3993 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 3994 | MVT::ValueType VT = Op.getValueType(); |
| 3995 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 3996 | unsigned len = getSizeInBits(VT); |
| 3997 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 3998 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 3999 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4000 | } |
| 4001 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4002 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4003 | } |
| 4004 | case ISD::CTTZ: { |
| 4005 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4006 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4007 | // { return 32 - nlz(~x & (x-1)); } |
| 4008 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 4009 | MVT::ValueType VT = Op.getValueType(); |
| 4010 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 4011 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 4012 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 4013 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 4014 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 4015 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 4016 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 4017 | return DAG.getNode(ISD::SUB, VT, |
| 4018 | DAG.getConstant(getSizeInBits(VT), VT), |
| 4019 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 4020 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 4021 | } |
| 4022 | } |
| 4023 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4024 | |
| 4025 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4026 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 4027 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 4028 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 4029 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 4030 | /// Lo/Hi values are returned. |
| 4031 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 4032 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4033 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4034 | SDNode *Node = Op.Val; |
| 4035 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Nate Begeman | ab48be3 | 2005-11-22 18:16:00 +0000 | [diff] [blame] | 4036 | assert((MVT::isInteger(VT) || VT == MVT::Vector) && |
| 4037 | "Cannot expand FP values!"); |
| 4038 | assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4039 | "Cannot expand to FP value or to larger int value!"); |
| 4040 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4041 | // See if we already expanded it. |
| 4042 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4043 | = ExpandedNodes.find(Op); |
| 4044 | if (I != ExpandedNodes.end()) { |
| 4045 | Lo = I->second.first; |
| 4046 | Hi = I->second.second; |
| 4047 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4050 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4051 | case ISD::CopyFromReg: |
| 4052 | assert(0 && "CopyFromReg must be legal!"); |
| 4053 | default: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4054 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 4055 | assert(0 && "Do not know how to expand this operator!"); |
| 4056 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4057 | case ISD::UNDEF: |
| 4058 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 4059 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4060 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4061 | case ISD::Constant: { |
| 4062 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 4063 | Lo = DAG.getConstant(Cst, NVT); |
| 4064 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 4065 | break; |
| 4066 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4067 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4068 | // Return the operands. |
| 4069 | Lo = Node->getOperand(0); |
| 4070 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4071 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4072 | |
| 4073 | case ISD::SIGN_EXTEND_INREG: |
| 4074 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4075 | // Sign extend the lo-part. |
| 4076 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4077 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 4078 | TLI.getShiftAmountTy())); |
| 4079 | // sext_inreg the low part if needed. |
| 4080 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 4081 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4082 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4083 | case ISD::BSWAP: { |
| 4084 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4085 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 4086 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 4087 | Lo = TempLo; |
| 4088 | break; |
| 4089 | } |
| 4090 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4091 | case ISD::CTPOP: |
| 4092 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 4093 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 4094 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 4095 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4096 | Hi = DAG.getConstant(0, NVT); |
| 4097 | break; |
| 4098 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4099 | case ISD::CTLZ: { |
| 4100 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4101 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4102 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4103 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4104 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 4105 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4106 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 4107 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 4108 | |
| 4109 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 4110 | Hi = DAG.getConstant(0, NVT); |
| 4111 | break; |
| 4112 | } |
| 4113 | |
| 4114 | case ISD::CTTZ: { |
| 4115 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4116 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4117 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4118 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4119 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 4120 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4121 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 4122 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 4123 | |
| 4124 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 4125 | Hi = DAG.getConstant(0, NVT); |
| 4126 | break; |
| 4127 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4128 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4129 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4130 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4131 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4132 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 4133 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 4134 | |
| 4135 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4136 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4137 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 4138 | if (!TLI.isLittleEndian()) |
| 4139 | std::swap(Lo, Hi); |
| 4140 | break; |
| 4141 | } |
| 4142 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4143 | case ISD::LOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4144 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4145 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 4146 | Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4147 | |
| 4148 | // Increment the pointer to the other half. |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 4149 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4150 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4151 | getIntPtrConstant(IncrementSize)); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4152 | // FIXME: This creates a bogus srcvalue! |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 4153 | Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 4154 | |
| 4155 | // Build a factor node to remember that this load is independent of the |
| 4156 | // other one. |
| 4157 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4158 | Hi.getValue(1)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4160 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4161 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4162 | if (!TLI.isLittleEndian()) |
| 4163 | std::swap(Lo, Hi); |
| 4164 | break; |
| 4165 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4166 | case ISD::AND: |
| 4167 | case ISD::OR: |
| 4168 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 4169 | SDOperand LL, LH, RL, RH; |
| 4170 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4171 | ExpandOp(Node->getOperand(1), RL, RH); |
| 4172 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 4173 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 4174 | break; |
| 4175 | } |
| 4176 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4177 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4178 | ExpandOp(Node->getOperand(1), LL, LH); |
| 4179 | ExpandOp(Node->getOperand(2), RL, RH); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4180 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
| 4181 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4182 | break; |
| 4183 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4184 | case ISD::SELECT_CC: { |
| 4185 | SDOperand TL, TH, FL, FH; |
| 4186 | ExpandOp(Node->getOperand(2), TL, TH); |
| 4187 | ExpandOp(Node->getOperand(3), FL, FH); |
| 4188 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4189 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
| 4190 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4191 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
| 4192 | break; |
| 4193 | } |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4194 | case ISD::SEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4195 | SDOperand Chain = Node->getOperand(0); |
| 4196 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4197 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4198 | |
| 4199 | if (EVT == NVT) |
| 4200 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4201 | else |
| 4202 | Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4203 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4204 | |
| 4205 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4206 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4207 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4208 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4209 | // part. |
| 4210 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 4211 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 4212 | TLI.getShiftAmountTy())); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4213 | break; |
| 4214 | } |
| 4215 | case ISD::ZEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4216 | SDOperand Chain = Node->getOperand(0); |
| 4217 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4218 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4219 | |
| 4220 | if (EVT == NVT) |
| 4221 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4222 | else |
| 4223 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4224 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4225 | |
| 4226 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4227 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4228 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 4229 | // The high part is just a zero. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4230 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4231 | break; |
| 4232 | } |
| 4233 | case ISD::EXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4234 | SDOperand Chain = Node->getOperand(0); |
| 4235 | SDOperand Ptr = Node->getOperand(1); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4236 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 4237 | |
| 4238 | if (EVT == NVT) |
| 4239 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 4240 | else |
| 4241 | Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 4242 | EVT); |
| 4243 | |
| 4244 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4245 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 4246 | |
| 4247 | // The high part is undefined. |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4248 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4249 | break; |
| 4250 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4251 | case ISD::ANY_EXTEND: |
| 4252 | // The low part is any extension of the input (which degenerates to a copy). |
| 4253 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 4254 | // The high part is undefined. |
| 4255 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4256 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4257 | case ISD::SIGN_EXTEND: { |
| 4258 | // The low part is just a sign extension of the input (which degenerates to |
| 4259 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4260 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4261 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4262 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4263 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 4264 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4265 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4266 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4267 | break; |
| 4268 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4269 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4270 | // The low part is just a zero extension of the input (which degenerates to |
| 4271 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4272 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4273 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4274 | // The high part is just a zero. |
| 4275 | Hi = DAG.getConstant(0, NVT); |
| 4276 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4277 | |
| 4278 | case ISD::BIT_CONVERT: { |
| 4279 | SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0), |
| 4280 | Node->getOperand(0)); |
| 4281 | ExpandOp(Tmp, Lo, Hi); |
| 4282 | break; |
| 4283 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4285 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4286 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 4287 | TargetLowering::Custom && |
| 4288 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4289 | Lo = TLI.LowerOperation(Op, DAG); |
| 4290 | assert(Lo.Val && "Node must be custom expanded!"); |
| 4291 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4292 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4293 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4294 | break; |
| 4295 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4296 | // These operators cannot be expanded directly, emit them as calls to |
| 4297 | // library functions. |
| 4298 | case ISD::FP_TO_SINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4299 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4300 | SDOperand Op; |
| 4301 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4302 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4303 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4304 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4305 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4306 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4307 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 4308 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4309 | // Now that the custom expander is done, expand the result, which is still |
| 4310 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4311 | if (Op.Val) { |
| 4312 | ExpandOp(Op, Lo, Hi); |
| 4313 | break; |
| 4314 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4315 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4316 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4317 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4318 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4319 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4320 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4321 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4322 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4323 | case ISD::FP_TO_UINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4324 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4325 | SDOperand Op; |
| 4326 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4327 | case Expand: assert(0 && "cannot expand FP!"); |
| 4328 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4329 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 4330 | } |
| 4331 | |
| 4332 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 4333 | |
| 4334 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4335 | if (Op.Val) { |
| 4336 | ExpandOp(Op, Lo, Hi); |
| 4337 | break; |
| 4338 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4339 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4340 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4341 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4342 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4343 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4344 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4345 | break; |
| 4346 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4347 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4348 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4349 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4350 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4351 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4352 | Op = TLI.LowerOperation(Op, DAG); |
| 4353 | if (Op.Val) { |
| 4354 | // Now that the custom expander is done, expand the result, which is |
| 4355 | // still VT. |
| 4356 | ExpandOp(Op, Lo, Hi); |
| 4357 | break; |
| 4358 | } |
| 4359 | } |
| 4360 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4361 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4362 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4363 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4364 | |
| 4365 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4366 | TargetLowering::LegalizeAction Action = |
| 4367 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 4368 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4369 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4370 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4371 | break; |
| 4372 | } |
| 4373 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4374 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4375 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4376 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4377 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4378 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4379 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4380 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4381 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4382 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4383 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4384 | Op = TLI.LowerOperation(Op, DAG); |
| 4385 | if (Op.Val) { |
| 4386 | // Now that the custom expander is done, expand the result, which is |
| 4387 | // still VT. |
| 4388 | ExpandOp(Op, Lo, Hi); |
| 4389 | break; |
| 4390 | } |
| 4391 | } |
| 4392 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4393 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4394 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4395 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4396 | |
| 4397 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4398 | TargetLowering::LegalizeAction Action = |
| 4399 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 4400 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4401 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4402 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | } |
| 4405 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4406 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4407 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4408 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4412 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4413 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4414 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4415 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4416 | Op = TLI.LowerOperation(Op, DAG); |
| 4417 | if (Op.Val) { |
| 4418 | // Now that the custom expander is done, expand the result, which is |
| 4419 | // still VT. |
| 4420 | ExpandOp(Op, Lo, Hi); |
| 4421 | break; |
| 4422 | } |
| 4423 | } |
| 4424 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4425 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4426 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4427 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4428 | |
| 4429 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4430 | TargetLowering::LegalizeAction Action = |
| 4431 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 4432 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4433 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4434 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4435 | break; |
| 4436 | } |
| 4437 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4438 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4439 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4440 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4441 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4442 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4443 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4444 | case ISD::SUB: { |
| 4445 | // If the target wants to custom expand this, let them. |
| 4446 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 4447 | TargetLowering::Custom) { |
| 4448 | Op = TLI.LowerOperation(Op, DAG); |
| 4449 | if (Op.Val) { |
| 4450 | ExpandOp(Op, Lo, Hi); |
| 4451 | break; |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | // Expand the subcomponents. |
| 4456 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 4457 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 4458 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 4459 | std::vector<MVT::ValueType> VTs; |
| 4460 | std::vector<SDOperand> LoOps, HiOps; |
| 4461 | VTs.push_back(LHSL.getValueType()); |
| 4462 | VTs.push_back(MVT::Flag); |
| 4463 | LoOps.push_back(LHSL); |
| 4464 | LoOps.push_back(RHSL); |
| 4465 | HiOps.push_back(LHSH); |
| 4466 | HiOps.push_back(RHSH); |
| 4467 | if (Node->getOpcode() == ISD::ADD) { |
| 4468 | Lo = DAG.getNode(ISD::ADDC, VTs, LoOps); |
| 4469 | HiOps.push_back(Lo.getValue(1)); |
| 4470 | Hi = DAG.getNode(ISD::ADDE, VTs, HiOps); |
| 4471 | } else { |
| 4472 | Lo = DAG.getNode(ISD::SUBC, VTs, LoOps); |
| 4473 | HiOps.push_back(Lo.getValue(1)); |
| 4474 | Hi = DAG.getNode(ISD::SUBE, VTs, HiOps); |
| 4475 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 4476 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4477 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4478 | case ISD::MUL: { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4479 | if (TLI.isOperationLegal(ISD::MULHU, NVT)) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4480 | SDOperand LL, LH, RL, RH; |
| 4481 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4482 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 4483 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
| 4484 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 4485 | // extended the sign bit of the low half through the upper half, and if so |
| 4486 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 4487 | // i64 x i64 multiply. |
| 4488 | if (TLI.isOperationLegal(ISD::MULHS, NVT) && |
| 4489 | // is RH an extension of the sign bit of RL? |
| 4490 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 4491 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 4492 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 4493 | // is LH an extension of the sign bit of LL? |
| 4494 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 4495 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 4496 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
| 4497 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
| 4498 | } else { |
| 4499 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 4500 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 4501 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 4502 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 4503 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 4504 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4505 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 4506 | } else { |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 4507 | Lo = ExpandLibCall("__muldi3" , Node, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 4508 | } |
| 4509 | break; |
| 4510 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4511 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 4512 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 4513 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 4514 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 4517 | // Make sure the resultant values have been legalized themselves, unless this |
| 4518 | // is a type that requires multi-step expansion. |
| 4519 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 4520 | Lo = LegalizeOp(Lo); |
| 4521 | Hi = LegalizeOp(Hi); |
| 4522 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4523 | |
| 4524 | // Remember in a map if the values will be reused later. |
| 4525 | bool isNew = |
| 4526 | ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 4527 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4530 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 4531 | /// two smaller values of MVT::Vector type. |
| 4532 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 4533 | SDOperand &Hi) { |
| 4534 | assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); |
| 4535 | SDNode *Node = Op.Val; |
| 4536 | unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); |
| 4537 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 4538 | unsigned NewNumElts = NumElements/2; |
| 4539 | SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); |
| 4540 | SDOperand TypeNode = *(Node->op_end()-1); |
| 4541 | |
| 4542 | // See if we already split it. |
| 4543 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4544 | = SplitNodes.find(Op); |
| 4545 | if (I != SplitNodes.end()) { |
| 4546 | Lo = I->second.first; |
| 4547 | Hi = I->second.second; |
| 4548 | return; |
| 4549 | } |
| 4550 | |
| 4551 | switch (Node->getOpcode()) { |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 4552 | default: Node->dump(); assert(0 && "Unknown vector operation!"); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4553 | case ISD::VBUILD_VECTOR: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4554 | std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts); |
| 4555 | LoOps.push_back(NewNumEltsNode); |
| 4556 | LoOps.push_back(TypeNode); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4557 | Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, LoOps); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4558 | |
| 4559 | std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2); |
| 4560 | HiOps.push_back(NewNumEltsNode); |
| 4561 | HiOps.push_back(TypeNode); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4562 | Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, HiOps); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4563 | break; |
| 4564 | } |
| 4565 | case ISD::VADD: |
| 4566 | case ISD::VSUB: |
| 4567 | case ISD::VMUL: |
| 4568 | case ISD::VSDIV: |
| 4569 | case ISD::VUDIV: |
| 4570 | case ISD::VAND: |
| 4571 | case ISD::VOR: |
| 4572 | case ISD::VXOR: { |
| 4573 | SDOperand LL, LH, RL, RH; |
| 4574 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 4575 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 4576 | |
| 4577 | Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, |
| 4578 | NewNumEltsNode, TypeNode); |
| 4579 | Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, |
| 4580 | NewNumEltsNode, TypeNode); |
| 4581 | break; |
| 4582 | } |
| 4583 | case ISD::VLOAD: { |
| 4584 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4585 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
| 4586 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 4587 | |
| 4588 | Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4589 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; |
| 4590 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4591 | getIntPtrConstant(IncrementSize)); |
| 4592 | // FIXME: This creates a bogus srcvalue! |
| 4593 | Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4594 | |
| 4595 | // Build a factor node to remember that this load is independent of the |
| 4596 | // other one. |
| 4597 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4598 | Hi.getValue(1)); |
| 4599 | |
| 4600 | // Remember that we legalized the chain. |
| 4601 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4602 | break; |
| 4603 | } |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 4604 | case ISD::VBIT_CONVERT: { |
| 4605 | // We know the result is a vector. The input may be either a vector or a |
| 4606 | // scalar value. |
| 4607 | if (Op.getOperand(0).getValueType() != MVT::Vector) { |
| 4608 | // Lower to a store/load. FIXME: this could be improved probably. |
| 4609 | SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType()); |
| 4610 | |
| 4611 | SDOperand St = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 4612 | Op.getOperand(0), Ptr, DAG.getSrcValue(0)); |
| 4613 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 4614 | St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0)); |
| 4615 | SplitVectorOp(St, Lo, Hi); |
| 4616 | } else { |
| 4617 | // If the input is a vector type, we have to either scalarize it, pack it |
| 4618 | // or convert it based on whether the input vector type is legal. |
| 4619 | SDNode *InVal = Node->getOperand(0).Val; |
| 4620 | unsigned NumElems = |
| 4621 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 4622 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 4623 | |
| 4624 | // If the input is from a single element vector, scalarize the vector, |
| 4625 | // then treat like a scalar. |
| 4626 | if (NumElems == 1) { |
| 4627 | SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT); |
| 4628 | Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar, |
| 4629 | Op.getOperand(1), Op.getOperand(2)); |
| 4630 | SplitVectorOp(Scalar, Lo, Hi); |
| 4631 | } else { |
| 4632 | // Split the input vector. |
| 4633 | SplitVectorOp(Op.getOperand(0), Lo, Hi); |
| 4634 | |
| 4635 | // Convert each of the pieces now. |
| 4636 | Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo, |
| 4637 | NewNumEltsNode, TypeNode); |
| 4638 | Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi, |
| 4639 | NewNumEltsNode, TypeNode); |
| 4640 | } |
| 4641 | break; |
| 4642 | } |
| 4643 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | // Remember in a map if the values will be reused later. |
| 4647 | bool isNew = |
| 4648 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 4649 | assert(isNew && "Value already expanded?!?"); |
| 4650 | } |
| 4651 | |
| 4652 | |
| 4653 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 4654 | /// equivalent operation that returns a scalar (e.g. F32) or packed value |
| 4655 | /// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right |
| 4656 | /// type for the result. |
| 4657 | SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, |
| 4658 | MVT::ValueType NewVT) { |
| 4659 | assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); |
| 4660 | SDNode *Node = Op.Val; |
| 4661 | |
| 4662 | // See if we already packed it. |
| 4663 | std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); |
| 4664 | if (I != PackedNodes.end()) return I->second; |
| 4665 | |
| 4666 | SDOperand Result; |
| 4667 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 4668 | default: |
| 4669 | Node->dump(); std::cerr << "\n"; |
| 4670 | assert(0 && "Unknown vector operation in PackVectorOp!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4671 | case ISD::VADD: |
| 4672 | case ISD::VSUB: |
| 4673 | case ISD::VMUL: |
| 4674 | case ISD::VSDIV: |
| 4675 | case ISD::VUDIV: |
| 4676 | case ISD::VAND: |
| 4677 | case ISD::VOR: |
| 4678 | case ISD::VXOR: |
| 4679 | Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), |
| 4680 | NewVT, |
| 4681 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4682 | PackVectorOp(Node->getOperand(1), NewVT)); |
| 4683 | break; |
| 4684 | case ISD::VLOAD: { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 4685 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 4686 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4687 | |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 4688 | Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4689 | |
| 4690 | // Remember that we legalized the chain. |
| 4691 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 4692 | break; |
| 4693 | } |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4694 | case ISD::VBUILD_VECTOR: |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 4695 | if (Node->getOperand(0).getValueType() == NewVT) { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4696 | // Returning a scalar? |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4697 | Result = Node->getOperand(0); |
| 4698 | } else { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 4699 | // Returning a BUILD_VECTOR? |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 4700 | |
| 4701 | // If all elements of the build_vector are undefs, return an undef. |
| 4702 | bool AllUndef = true; |
| 4703 | for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i) |
| 4704 | if (Node->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 4705 | AllUndef = false; |
| 4706 | break; |
| 4707 | } |
| 4708 | if (AllUndef) { |
| 4709 | Result = DAG.getNode(ISD::UNDEF, NewVT); |
| 4710 | } else { |
| 4711 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2); |
| 4712 | Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Ops); |
| 4713 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4714 | } |
| 4715 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 4716 | case ISD::VINSERT_VECTOR_ELT: |
| 4717 | if (!MVT::isVector(NewVT)) { |
| 4718 | // Returning a scalar? Must be the inserted element. |
| 4719 | Result = Node->getOperand(1); |
| 4720 | } else { |
| 4721 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, |
| 4722 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4723 | Node->getOperand(1), Node->getOperand(2)); |
| 4724 | } |
| 4725 | break; |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 4726 | case ISD::VVECTOR_SHUFFLE: |
| 4727 | if (!MVT::isVector(NewVT)) { |
| 4728 | // Returning a scalar? Figure out if it is the LHS or RHS and return it. |
| 4729 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 4730 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 4731 | Result = PackVectorOp(Node->getOperand(1), NewVT); |
| 4732 | else |
| 4733 | Result = PackVectorOp(Node->getOperand(0), NewVT); |
| 4734 | } else { |
| 4735 | // Otherwise, return a VECTOR_SHUFFLE node. First convert the index |
| 4736 | // vector from a VBUILD_VECTOR to a BUILD_VECTOR. |
| 4737 | std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(), |
| 4738 | Node->getOperand(2).Val->op_end()-2); |
| 4739 | MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size()); |
| 4740 | SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, BuildVecIdx); |
| 4741 | |
| 4742 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, |
| 4743 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4744 | PackVectorOp(Node->getOperand(1), NewVT), BV); |
| 4745 | } |
| 4746 | break; |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 4747 | case ISD::VBIT_CONVERT: |
| 4748 | if (Op.getOperand(0).getValueType() != MVT::Vector) |
| 4749 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
| 4750 | else { |
| 4751 | // If the input is a vector type, we have to either scalarize it, pack it |
| 4752 | // or convert it based on whether the input vector type is legal. |
| 4753 | SDNode *InVal = Node->getOperand(0).Val; |
| 4754 | unsigned NumElems = |
| 4755 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 4756 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 4757 | |
| 4758 | // Figure out if there is a Packed type corresponding to this Vector |
| 4759 | // type. If so, convert to the packed type. |
| 4760 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 4761 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 4762 | // Turn this into a bit convert of the packed input. |
| 4763 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 4764 | PackVectorOp(Node->getOperand(0), TVT)); |
| 4765 | break; |
| 4766 | } else if (NumElems == 1) { |
| 4767 | // Turn this into a bit convert of the scalar input. |
| 4768 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 4769 | PackVectorOp(Node->getOperand(0), EVT)); |
| 4770 | break; |
| 4771 | } else { |
| 4772 | // FIXME: UNIMP! |
| 4773 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 4774 | } |
| 4775 | } |
Evan Cheng | db3c626 | 2006-04-10 18:54:36 +0000 | [diff] [blame^] | 4776 | break; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 4777 | case ISD::VSELECT: |
| 4778 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
| 4779 | PackVectorOp(Op.getOperand(1), NewVT), |
| 4780 | PackVectorOp(Op.getOperand(2), NewVT)); |
| 4781 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | if (TLI.isTypeLegal(NewVT)) |
| 4785 | Result = LegalizeOp(Result); |
| 4786 | bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; |
| 4787 | assert(isNew && "Value already packed?"); |
| 4788 | return Result; |
| 4789 | } |
| 4790 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4791 | |
| 4792 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 4793 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4794 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 4795 | if (ViewLegalizeDAGs) viewGraph(); |
| 4796 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4797 | /// run - This is the main entry point to this class. |
| 4798 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4799 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |