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" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 22 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
| 25 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 28 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 31 | #ifndef NDEBUG |
| 32 | static cl::opt<bool> |
| 33 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 34 | cl::desc("Pop up a window to show dags before legalize")); |
| 35 | #else |
| 36 | static const bool ViewLegalizeDAGs = 0; |
| 37 | #endif |
| 38 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 41 | /// hacks on it until the target machine can handle it. This involves |
| 42 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 43 | /// large sizes or splitting up large values into small values) as well as |
| 44 | /// eliminating operations the machine cannot handle. |
| 45 | /// |
| 46 | /// This code also does a small amount of optimization and recognition of idioms |
| 47 | /// as part of its processing. For example, if a target does not support a |
| 48 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 49 | /// will attempt merge setcc and brc instructions into brcc's. |
| 50 | /// |
| 51 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 52 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 53 | TargetLowering &TLI; |
| 54 | SelectionDAG &DAG; |
| 55 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 56 | // Libcall insertion helpers. |
| 57 | |
| 58 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 59 | /// legalized. We use this to ensure that calls are properly serialized |
| 60 | /// against each other, including inserted libcalls. |
| 61 | SDOperand LastCALLSEQ_END; |
| 62 | |
| 63 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 64 | /// helpful assertions that a libcall isn't created while another call is |
| 65 | /// being legalized (which could lead to non-serialized call sequences). |
| 66 | bool IsLegalizingCall; |
| 67 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 68 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 69 | Legal, // The target natively supports this operation. |
| 70 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 71 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 72 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 74 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 75 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 76 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 77 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 79 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 80 | /// than one use, this map indicates what regularized operand to use. This |
| 81 | /// allows us to avoid legalizing the same thing more than once. |
| 82 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 83 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 84 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 85 | /// than one use, this map indicates what promoted value to use. This allows |
| 86 | /// us to avoid promoting the same thing more than once. |
| 87 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 88 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 89 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 90 | /// which which operands are the expanded version of the input. This allows |
| 91 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 92 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 93 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 94 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 95 | /// which which operands are the split version of the input. This allows us |
| 96 | /// to avoid splitting the same node more than once. |
| 97 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 98 | |
| 99 | /// PackedNodes - For nodes that need to be packed from MVT::Vector types to |
| 100 | /// concrete packed types, this contains the mapping of ones we have already |
| 101 | /// processed to the result. |
| 102 | std::map<SDOperand, SDOperand> PackedNodes; |
| 103 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 104 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 105 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 106 | // If someone requests legalization of the new node, return itself. |
| 107 | if (From != To) |
| 108 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 110 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 111 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 112 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 113 | // If someone requests legalization of the new node, return itself. |
| 114 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 117 | public: |
| 118 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 119 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 121 | /// getTypeAction - Return how we should legalize values of this type, either |
| 122 | /// it is already legal or we need to expand it into multiple registers of |
| 123 | /// smaller integer type, or we need to promote it to a larger type. |
| 124 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 125 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /// isTypeLegal - Return true if this type is legal on this target. |
| 129 | /// |
| 130 | bool isTypeLegal(MVT::ValueType VT) const { |
| 131 | return getTypeAction(VT) == Legal; |
| 132 | } |
| 133 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 134 | void LegalizeDAG(); |
| 135 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 136 | private: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 137 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 138 | /// appropriate for its type. |
| 139 | void HandleOp(SDOperand Op); |
| 140 | |
| 141 | /// LegalizeOp - We know that the specified value has a legal type. |
| 142 | /// Recursively ensure that the operands have legal types, then return the |
| 143 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 144 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 145 | |
| 146 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 147 | /// promote it to compute the value into a larger type. The produced value |
| 148 | /// will have the correct bits for the low portion of the register, but no |
| 149 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 150 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 151 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 152 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 153 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 154 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 155 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 156 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 157 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 158 | /// types. |
| 159 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 160 | |
| 161 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 162 | /// two smaller values of MVT::Vector type. |
| 163 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 164 | |
| 165 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 166 | /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When |
| 167 | /// this is called, we know that PackedVT is the right type for the result and |
| 168 | /// we know that this type is legal for the target. |
| 169 | SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT); |
| 170 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 171 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 172 | /// specified mask and type. Targets can specify exactly which masks they |
| 173 | /// support and the code generator is tasked with not creating illegal masks. |
| 174 | /// |
| 175 | /// Note that this will also return true for shuffles that are promoted to a |
| 176 | /// different type. |
| 177 | /// |
| 178 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 179 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 180 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 181 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 182 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
| 183 | std::set<SDNode*> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 184 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 185 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 186 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 187 | SDOperand CreateStackTemporary(MVT::ValueType VT); |
| 188 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 189 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 190 | SDOperand &Hi); |
| 191 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 192 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 194 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 195 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 196 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 197 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 198 | SDOperand LegalOp, |
| 199 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 200 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 201 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 202 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 203 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 205 | SDOperand ExpandBSWAP(SDOperand Op); |
| 206 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 207 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 208 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 209 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 210 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 212 | SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 213 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 215 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 216 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 217 | } |
| 218 | }; |
| 219 | } |
| 220 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 221 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 222 | /// specified mask and type. Targets can specify exactly which masks they |
| 223 | /// support and the code generator is tasked with not creating illegal masks. |
| 224 | /// |
| 225 | /// Note that this will also return true for shuffles that are promoted to a |
| 226 | /// different type. |
| 227 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 228 | SDOperand Mask) const { |
| 229 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 230 | default: return 0; |
| 231 | case TargetLowering::Legal: |
| 232 | case TargetLowering::Custom: |
| 233 | break; |
| 234 | case TargetLowering::Promote: { |
| 235 | // If this is promoted to a different type, convert the shuffle mask and |
| 236 | // ask if it is legal in the promoted type! |
| 237 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 238 | |
| 239 | // If we changed # elements, change the shuffle mask. |
| 240 | unsigned NumEltsGrowth = |
| 241 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 242 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 243 | if (NumEltsGrowth > 1) { |
| 244 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 245 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 246 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 247 | SDOperand InOp = Mask.getOperand(i); |
| 248 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 249 | if (InOp.getOpcode() == ISD::UNDEF) |
| 250 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 251 | else { |
| 252 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 253 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 254 | } |
| 255 | } |
| 256 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 257 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 258 | } |
| 259 | VT = NVT; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 264 | } |
| 265 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 266 | /// getScalarizedOpcode - Return the scalar opcode that corresponds to the |
| 267 | /// specified vector opcode. |
Chris Lattner | b89175f | 2005-11-19 05:51:46 +0000 | [diff] [blame] | 268 | static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 269 | switch (VecOp) { |
| 270 | default: assert(0 && "Don't know how to scalarize this opcode!"); |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 271 | case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; |
| 272 | case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; |
| 273 | case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; |
| 274 | case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV; |
| 275 | case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV; |
| 276 | case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0; |
| 277 | case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0; |
| 278 | case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 282 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 283 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 284 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 285 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 286 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 289 | /// ComputeTopDownOrdering - Add the specified node to the Order list if it has |
| 290 | /// not been visited yet and if all of its operands have already been visited. |
| 291 | static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, |
| 292 | std::map<SDNode*, unsigned> &Visited) { |
| 293 | if (++Visited[N] != N->getNumOperands()) |
| 294 | return; // Haven't visited all operands yet |
| 295 | |
| 296 | Order.push_back(N); |
| 297 | |
| 298 | if (N->hasOneUse()) { // Tail recurse in common case. |
| 299 | ComputeTopDownOrdering(*N->use_begin(), Order, Visited); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | // Now that we have N in, add anything that uses it if all of their operands |
| 304 | // are now done. |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 305 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) |
| 306 | ComputeTopDownOrdering(*UI, Order, Visited); |
| 307 | } |
| 308 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 310 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 311 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 312 | IsLegalizingCall = false; |
| 313 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 314 | // The legalize process is inherently a bottom-up recursive process (users |
| 315 | // legalize their uses before themselves). Given infinite stack space, we |
| 316 | // could just start legalizing on the root and traverse the whole graph. In |
| 317 | // 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] | 318 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 319 | // node is only legalized after all of its operands are legalized. |
| 320 | std::map<SDNode*, unsigned> Visited; |
| 321 | std::vector<SDNode*> Order; |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 323 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 324 | // entry node) that have no operands. |
| 325 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 326 | E = DAG.allnodes_end(); I != E; ++I) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 327 | if (I->getNumOperands() == 0) { |
| 328 | Visited[I] = 0 - 1U; |
| 329 | ComputeTopDownOrdering(I, Order, Visited); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 333 | assert(Order.size() == Visited.size() && |
| 334 | Order.size() == |
| 335 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 336 | "Error: DAG is cyclic!"); |
| 337 | Visited.clear(); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 338 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 339 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 340 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 341 | |
| 342 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 343 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 344 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 345 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 346 | |
| 347 | ExpandedNodes.clear(); |
| 348 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 349 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 350 | SplitNodes.clear(); |
| 351 | PackedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 352 | |
| 353 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 354 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 357 | |
| 358 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 359 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 360 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 361 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 362 | return Node; |
| 363 | if (Node->use_empty()) |
| 364 | return 0; // No CallSeqEnd |
| 365 | |
| 366 | // The chain is usually at the end. |
| 367 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 368 | if (TheChain.getValueType() != MVT::Other) { |
| 369 | // Sometimes it's at the beginning. |
| 370 | TheChain = SDOperand(Node, 0); |
| 371 | if (TheChain.getValueType() != MVT::Other) { |
| 372 | // Otherwise, hunt for it. |
| 373 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 374 | if (Node->getValueType(i) == MVT::Other) { |
| 375 | TheChain = SDOperand(Node, i); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | // Otherwise, we walked into a node without a chain. |
| 380 | if (TheChain.getValueType() != MVT::Other) |
| 381 | return 0; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 386 | E = Node->use_end(); UI != E; ++UI) { |
| 387 | |
| 388 | // Make sure to only follow users of our token chain. |
| 389 | SDNode *User = *UI; |
| 390 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 391 | if (User->getOperand(i) == TheChain) |
| 392 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 393 | return Result; |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 399 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 400 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 401 | assert(Node && "Didn't find callseq_start for a call??"); |
| 402 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 403 | |
| 404 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 405 | "Node doesn't have a token chain argument!"); |
| 406 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 407 | } |
| 408 | |
| 409 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 410 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 411 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 412 | /// |
| 413 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 414 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 415 | /// |
| 416 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
| 417 | std::set<SDNode*> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 418 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 419 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 420 | // If we've already processed this node and it does lead to Dest, there is no |
| 421 | // need to reprocess it. |
| 422 | if (NodesLeadingTo.count(N)) return true; |
| 423 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 424 | // If the first result of this node has been already legalized, then it cannot |
| 425 | // reach N. |
| 426 | switch (getTypeAction(N->getValueType(0))) { |
| 427 | case Legal: |
| 428 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 429 | break; |
| 430 | case Promote: |
| 431 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 432 | break; |
| 433 | case Expand: |
| 434 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | // Okay, this node has not already been legalized. Check and legalize all |
| 439 | // operands. If none lead to Dest, then we can legalize this node. |
| 440 | bool OperandsLeadToDest = false; |
| 441 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 442 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 443 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 444 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 445 | if (OperandsLeadToDest) { |
| 446 | NodesLeadingTo.insert(N); |
| 447 | return true; |
| 448 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 449 | |
| 450 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 451 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 452 | return false; |
| 453 | } |
| 454 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 455 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 456 | /// appropriate for its type. |
| 457 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
| 458 | switch (getTypeAction(Op.getValueType())) { |
| 459 | default: assert(0 && "Bad type action!"); |
| 460 | case Legal: LegalizeOp(Op); break; |
| 461 | case Promote: PromoteOp(Op); break; |
| 462 | case Expand: |
| 463 | if (Op.getValueType() != MVT::Vector) { |
| 464 | SDOperand X, Y; |
| 465 | ExpandOp(Op, X, Y); |
| 466 | } else { |
| 467 | SDNode *N = Op.Val; |
| 468 | unsigned NumOps = N->getNumOperands(); |
| 469 | unsigned NumElements = |
| 470 | cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue(); |
| 471 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT(); |
| 472 | MVT::ValueType PackedVT = getVectorType(EVT, NumElements); |
| 473 | if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) { |
| 474 | // In the common case, this is a legal vector type, convert it to the |
| 475 | // packed operation and type now. |
| 476 | PackVectorOp(Op, PackedVT); |
| 477 | } else if (NumElements == 1) { |
| 478 | // Otherwise, if this is a single element vector, convert it to a |
| 479 | // scalar operation. |
| 480 | PackVectorOp(Op, EVT); |
| 481 | } else { |
| 482 | // Otherwise, this is a multiple element vector that isn't supported. |
| 483 | // Split it in half and legalize both parts. |
| 484 | SDOperand X, Y; |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 485 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | break; |
| 489 | } |
| 490 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 491 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 492 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 493 | /// a load from the constant pool. |
| 494 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 495 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 496 | bool Extend = false; |
| 497 | |
| 498 | // If a FP immediate is precise when represented as a float and if the |
| 499 | // target can do an extending load from float to double, we put it into |
| 500 | // the constant pool as a float, even if it's is statically typed as a |
| 501 | // double. |
| 502 | MVT::ValueType VT = CFP->getValueType(0); |
| 503 | bool isDouble = VT == MVT::f64; |
| 504 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 505 | Type::FloatTy, CFP->getValue()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 506 | if (!UseCP) { |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 507 | double Val = LLVMC->getValue(); |
| 508 | return isDouble |
| 509 | ? DAG.getConstant(DoubleToBits(Val), MVT::i64) |
| 510 | : DAG.getConstant(FloatToBits(Val), MVT::i32); |
| 511 | } |
| 512 | |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 513 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 514 | // Only do this if the target has a native EXTLOAD instruction from f32. |
| 515 | TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) { |
| 516 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy)); |
| 517 | VT = MVT::f32; |
| 518 | Extend = true; |
| 519 | } |
| 520 | |
| 521 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
| 522 | if (Extend) { |
| 523 | return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 524 | CPIdx, NULL, 0, MVT::f32); |
| 525 | } else { |
| 526 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
| 527 | } |
| 528 | } |
| 529 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 530 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 531 | /// LegalizeOp - We know that the specified value has a legal type. |
| 532 | /// Recursively ensure that the operands have legal types, then return the |
| 533 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 534 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 535 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 536 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 537 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 539 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 540 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 541 | if (Node->getNumValues() > 1) { |
| 542 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 543 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 544 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 545 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 546 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 547 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 551 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 552 | // means that we always must cache transformed nodes. |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 553 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 554 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 555 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 556 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 557 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 558 | bool isCustom = false; |
| 559 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 560 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 561 | case ISD::FrameIndex: |
| 562 | case ISD::EntryToken: |
| 563 | case ISD::Register: |
| 564 | case ISD::BasicBlock: |
| 565 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 566 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 567 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 568 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 569 | case ISD::TargetConstantPool: |
| 570 | case ISD::TargetGlobalAddress: |
| 571 | case ISD::TargetExternalSymbol: |
| 572 | case ISD::VALUETYPE: |
| 573 | case ISD::SRCVALUE: |
| 574 | case ISD::STRING: |
| 575 | case ISD::CONDCODE: |
Andrew Lenharth | 82c3d8f | 2006-10-11 04:29:42 +0000 | [diff] [blame] | 576 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 577 | // Primitives must all be legal. |
| 578 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 579 | "This must be legal!"); |
| 580 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 581 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 582 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 583 | // If this is a target node, legalize it by legalizing the operands then |
| 584 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 585 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 586 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 587 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 588 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 589 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 590 | |
| 591 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 592 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 593 | return Result.getValue(Op.ResNo); |
| 594 | } |
| 595 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 596 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 597 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 598 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 599 | assert(0 && "Do not know how to legalize this operator!"); |
| 600 | abort(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 601 | case ISD::GlobalAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 602 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 603 | case ISD::ConstantPool: |
| 604 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 605 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 606 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 607 | case TargetLowering::Custom: |
| 608 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 609 | if (Tmp1.Val) Result = Tmp1; |
| 610 | // 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] | 611 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 612 | break; |
| 613 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 614 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 615 | case ISD::AssertSext: |
| 616 | case ISD::AssertZext: |
| 617 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 618 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 619 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 620 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 621 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 622 | Result = Node->getOperand(Op.ResNo); |
| 623 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 624 | case ISD::CopyFromReg: |
| 625 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 626 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 627 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 628 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 629 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 630 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 631 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 632 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 633 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 634 | } else { |
| 635 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 636 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 637 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 638 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 639 | // Since CopyFromReg produces two values, make sure to remember that we |
| 640 | // legalized both of them. |
| 641 | AddLegalizedOperand(Op.getValue(0), Result); |
| 642 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 643 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 644 | case ISD::UNDEF: { |
| 645 | MVT::ValueType VT = Op.getValueType(); |
| 646 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 647 | default: assert(0 && "This action is not supported yet!"); |
| 648 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 649 | if (MVT::isInteger(VT)) |
| 650 | Result = DAG.getConstant(0, VT); |
| 651 | else if (MVT::isFloatingPoint(VT)) |
| 652 | Result = DAG.getConstantFP(0, VT); |
| 653 | else |
| 654 | assert(0 && "Unknown value type!"); |
| 655 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 656 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | break; |
| 660 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 662 | case ISD::INTRINSIC_W_CHAIN: |
| 663 | case ISD::INTRINSIC_WO_CHAIN: |
| 664 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 665 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 666 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 667 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 668 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 669 | |
| 670 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 671 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 672 | TargetLowering::Custom) { |
| 673 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 674 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (Result.Val->getNumValues() == 1) break; |
| 678 | |
| 679 | // Must have return value and chain result. |
| 680 | assert(Result.Val->getNumValues() == 2 && |
| 681 | "Cannot return more than two values!"); |
| 682 | |
| 683 | // Since loads produce two values, make sure to remember that we |
| 684 | // legalized both of them. |
| 685 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 686 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 687 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 689 | |
| 690 | case ISD::LOCATION: |
| 691 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 692 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 693 | |
| 694 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 695 | case TargetLowering::Promote: |
| 696 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 697 | case TargetLowering::Expand: { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 698 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 699 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
| 700 | bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other); |
| 701 | |
| 702 | if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) { |
| 703 | const std::string &FName = |
| 704 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 705 | const std::string &DirName = |
| 706 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 707 | unsigned SrcFile = DebugInfo->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 708 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 709 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 710 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 711 | SDOperand LineOp = Node->getOperand(1); |
| 712 | SDOperand ColOp = Node->getOperand(2); |
| 713 | |
| 714 | if (useDEBUG_LOC) { |
| 715 | Ops.push_back(LineOp); // line # |
| 716 | Ops.push_back(ColOp); // col # |
| 717 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 718 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 719 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 720 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 721 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 722 | unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile); |
| 723 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 724 | Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other,&Ops[0],Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 725 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 726 | } else { |
| 727 | Result = Tmp1; // chain |
| 728 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 729 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 730 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 731 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 732 | if (Tmp1 != Node->getOperand(0) || |
| 733 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 734 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 735 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 736 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 737 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 738 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 739 | } else { |
| 740 | // Otherwise promote them. |
| 741 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 742 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 743 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 744 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 745 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 746 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 747 | } |
| 748 | break; |
| 749 | } |
| 750 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 751 | |
| 752 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 753 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 754 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 755 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 756 | case TargetLowering::Legal: |
| 757 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 758 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 759 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 760 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 761 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 762 | break; |
| 763 | } |
| 764 | break; |
| 765 | |
| 766 | case ISD::DEBUG_LABEL: |
| 767 | assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!"); |
| 768 | switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 769 | default: assert(0 && "This action is not supported yet!"); |
| 770 | case TargetLowering::Legal: |
| 771 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 772 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 773 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 774 | break; |
| 775 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 776 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 778 | case ISD::Constant: |
| 779 | // We know we don't need to expand constants here, constants only have one |
| 780 | // value and we check that it is fine above. |
| 781 | |
| 782 | // FIXME: Maybe we should handle things like targets that don't support full |
| 783 | // 32-bit immediates? |
| 784 | break; |
| 785 | case ISD::ConstantFP: { |
| 786 | // Spill FP immediates to the constant pool if the target cannot directly |
| 787 | // codegen them. Targets often have some immediate values that can be |
| 788 | // efficiently generated into an FP register without a load. We explicitly |
| 789 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 790 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 791 | |
| 792 | // Check to see if this FP immediate is already legal. |
| 793 | bool isLegal = false; |
| 794 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 795 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 796 | if (CFP->isExactlyValue(*I)) { |
| 797 | isLegal = true; |
| 798 | break; |
| 799 | } |
| 800 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 801 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 802 | if (isLegal) { |
| 803 | Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 808 | default: assert(0 && "This action is not supported yet!"); |
| 809 | case TargetLowering::Custom: |
| 810 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 811 | if (Tmp3.Val) { |
| 812 | Result = Tmp3; |
| 813 | break; |
| 814 | } |
| 815 | // FALLTHROUGH |
| 816 | case TargetLowering::Expand: |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 817 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 818 | } |
| 819 | break; |
| 820 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 821 | case ISD::TokenFactor: |
| 822 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 823 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 824 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 825 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 826 | } else if (Node->getNumOperands() == 3) { |
| 827 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 828 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 829 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 830 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 831 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 832 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 833 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 834 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 835 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 836 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 837 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 838 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 839 | |
| 840 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 841 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 842 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 843 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 844 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
| 845 | assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() && |
| 846 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 847 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 848 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 849 | // remember that we legalized all of them, so it doesn't get relegalized. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 850 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
| 851 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 852 | if (Op.ResNo == i) |
| 853 | Tmp2 = Tmp1; |
| 854 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 855 | } |
| 856 | return Tmp2; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 857 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 858 | case ISD::BUILD_VECTOR: |
| 859 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 860 | default: assert(0 && "This action is not supported yet!"); |
| 861 | case TargetLowering::Custom: |
| 862 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 863 | if (Tmp3.Val) { |
| 864 | Result = Tmp3; |
| 865 | break; |
| 866 | } |
| 867 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 868 | case TargetLowering::Expand: |
| 869 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 870 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 871 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 872 | break; |
| 873 | case ISD::INSERT_VECTOR_ELT: |
| 874 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 875 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 876 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 877 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 878 | |
| 879 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 880 | Node->getValueType(0))) { |
| 881 | default: assert(0 && "This action is not supported yet!"); |
| 882 | case TargetLowering::Legal: |
| 883 | break; |
| 884 | case TargetLowering::Custom: |
| 885 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 886 | if (Tmp3.Val) { |
| 887 | Result = Tmp3; |
| 888 | break; |
| 889 | } |
| 890 | // FALLTHROUGH |
| 891 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 892 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 893 | // then a shuffle that inserts it into the right position in the vector. |
| 894 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 895 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 896 | Tmp1.getValueType(), Tmp2); |
| 897 | |
| 898 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 899 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
| 900 | MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT); |
| 901 | |
| 902 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 903 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 904 | // the RHS. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 905 | SmallVector<SDOperand, 8> ShufOps; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 906 | for (unsigned i = 0; i != NumElts; ++i) { |
| 907 | if (i != InsertPos->getValue()) |
| 908 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 909 | else |
| 910 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 911 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 912 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 913 | &ShufOps[0], ShufOps.size()); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 914 | |
| 915 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 916 | Tmp1, ScVec, ShufMask); |
| 917 | Result = LegalizeOp(Result); |
| 918 | break; |
| 919 | } |
| 920 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 921 | // If the target doesn't support this, we have to spill the input vector |
| 922 | // to a temporary stack slot, update the element, then reload it. This is |
| 923 | // badness. We could also load the value into a vector register (either |
| 924 | // with a "move to register" or "extload into register" instruction, then |
| 925 | // permute it into place, if the idx is a constant and if the idx is |
| 926 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 927 | MVT::ValueType VT = Tmp1.getValueType(); |
| 928 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 929 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 930 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 931 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 932 | // Store the vector. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 933 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 934 | |
| 935 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 936 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 937 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 938 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 939 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 940 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 941 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 942 | // Store the scalar value. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 943 | Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 944 | // Load the updated vector. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 945 | Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | } |
| 949 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 950 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 951 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 952 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 953 | break; |
| 954 | } |
| 955 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 956 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 957 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 958 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 959 | Node->getValueType(0))) { |
| 960 | default: assert(0 && "This action is not supported yet!"); |
| 961 | case TargetLowering::Legal: |
| 962 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 963 | case TargetLowering::Custom: |
| 964 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 965 | if (Tmp3.Val) { |
| 966 | Result = Tmp3; |
| 967 | break; |
| 968 | } |
| 969 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 970 | case TargetLowering::Expand: |
| 971 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 972 | break; |
| 973 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 974 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 975 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 976 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 977 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 978 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 979 | |
| 980 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 981 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 982 | default: assert(0 && "Unknown operation action!"); |
| 983 | case TargetLowering::Legal: |
| 984 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 985 | "vector shuffle should not be created if not legal!"); |
| 986 | break; |
| 987 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 988 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 989 | if (Tmp3.Val) { |
| 990 | Result = Tmp3; |
| 991 | break; |
| 992 | } |
| 993 | // FALLTHROUGH |
| 994 | case TargetLowering::Expand: { |
| 995 | MVT::ValueType VT = Node->getValueType(0); |
| 996 | MVT::ValueType EltVT = MVT::getVectorBaseType(VT); |
| 997 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 998 | SDOperand Mask = Node->getOperand(2); |
| 999 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1000 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1001 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1002 | SDOperand Arg = Mask.getOperand(i); |
| 1003 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1004 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1005 | } else { |
| 1006 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1007 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1008 | if (Idx < NumElems) |
| 1009 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1010 | DAG.getConstant(Idx, PtrVT))); |
| 1011 | else |
| 1012 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1013 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1014 | } |
| 1015 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1016 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1017 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1018 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1019 | case TargetLowering::Promote: { |
| 1020 | // Change base type to a different vector type. |
| 1021 | MVT::ValueType OVT = Node->getValueType(0); |
| 1022 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1023 | |
| 1024 | // Cast the two input vectors. |
| 1025 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1026 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1027 | |
| 1028 | // Convert the shuffle mask to the right # elements. |
| 1029 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1030 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1031 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1032 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1033 | break; |
| 1034 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1035 | } |
| 1036 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1037 | |
| 1038 | case ISD::EXTRACT_VECTOR_ELT: |
| 1039 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1040 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1041 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1042 | |
| 1043 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, |
| 1044 | Tmp1.getValueType())) { |
| 1045 | default: assert(0 && "This action is not supported yet!"); |
| 1046 | case TargetLowering::Legal: |
| 1047 | break; |
| 1048 | case TargetLowering::Custom: |
| 1049 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1050 | if (Tmp3.Val) { |
| 1051 | Result = Tmp3; |
| 1052 | break; |
| 1053 | } |
| 1054 | // FALLTHROUGH |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 1055 | case TargetLowering::Expand: |
| 1056 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | } |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1059 | break; |
| 1060 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 1061 | case ISD::VEXTRACT_VECTOR_ELT: |
| 1062 | Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1063 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1064 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1065 | case ISD::CALLSEQ_START: { |
| 1066 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1067 | |
| 1068 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1069 | // to this call start. This ensures that any libcalls that need be inserted |
| 1070 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1071 | {std::set<SDNode*> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1072 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1073 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1074 | NodesLeadingTo); |
| 1075 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Now that we legalized all of the inputs (which may have inserted |
| 1078 | // libcalls) create the new CALLSEQ_START node. |
| 1079 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1080 | |
| 1081 | // Merge in the last call, to ensure that this call start after the last |
| 1082 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1083 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1084 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1085 | Tmp1 = LegalizeOp(Tmp1); |
| 1086 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Do not try to legalize the target-specific arguments (#1+). |
| 1089 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1090 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1091 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1092 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1096 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1097 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1098 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1099 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1100 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1101 | // sequence have been legalized, legalize the call itself. During this |
| 1102 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1103 | // can overlap. |
| 1104 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1105 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1106 | // Note that we are selecting this call! |
| 1107 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1108 | IsLegalizingCall = true; |
| 1109 | |
| 1110 | // Legalize the call, starting from the CALLSEQ_END. |
| 1111 | LegalizeOp(LastCALLSEQ_END); |
| 1112 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1113 | return Result; |
| 1114 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1115 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1116 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1117 | // will cause this node to be legalized as well as handling libcalls right. |
| 1118 | if (LastCALLSEQ_END.Val != Node) { |
| 1119 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
| 1120 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 1121 | assert(I != LegalizedNodes.end() && |
| 1122 | "Legalizing the call start should have legalized this node!"); |
| 1123 | return I->second; |
| 1124 | } |
| 1125 | |
| 1126 | // Otherwise, the call start has been legalized and everything is going |
| 1127 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1128 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1129 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1130 | // an optional flag input. |
| 1131 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1132 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1133 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1134 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1135 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } else { |
| 1138 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1139 | if (Tmp1 != Node->getOperand(0) || |
| 1140 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1141 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1142 | Ops[0] = Tmp1; |
| 1143 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1144 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1145 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1146 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1147 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1148 | // This finishes up call legalization. |
| 1149 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1150 | |
| 1151 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1152 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1153 | if (Node->getNumValues() == 2) |
| 1154 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1155 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1156 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1157 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1158 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1159 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1160 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1162 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1163 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1164 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1165 | Node->getValueType(0))) { |
| 1166 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1167 | case TargetLowering::Expand: { |
| 1168 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1169 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1170 | " not tell us which reg is the stack pointer!"); |
| 1171 | SDOperand Chain = Tmp1.getOperand(0); |
| 1172 | SDOperand Size = Tmp2.getOperand(1); |
| 1173 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 1174 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 1175 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1176 | Tmp1 = LegalizeOp(Tmp1); |
| 1177 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | } |
| 1180 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1181 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1182 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1183 | Tmp1 = LegalizeOp(Tmp3); |
| 1184 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1185 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1186 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1187 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1188 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1189 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1190 | // Since this op produce two values, make sure to remember that we |
| 1191 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1192 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1193 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1194 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1195 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1196 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1197 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1198 | bool Changed = false; |
| 1199 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1200 | // that need to be expanded or something. Note we skip the asm string and |
| 1201 | // all of the TargetConstant flags. |
| 1202 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1203 | Changed = Op != Ops[0]; |
| 1204 | Ops[0] = Op; |
| 1205 | |
| 1206 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1207 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1208 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1209 | for (++i; NumVals; ++i, --NumVals) { |
| 1210 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1211 | if (Op != Ops[i]) { |
| 1212 | Changed = true; |
| 1213 | Ops[i] = Op; |
| 1214 | } |
| 1215 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1216 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (HasInFlag) { |
| 1219 | Op = LegalizeOp(Ops.back()); |
| 1220 | Changed |= Op != Ops.back(); |
| 1221 | Ops.back() = Op; |
| 1222 | } |
| 1223 | |
| 1224 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1225 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1226 | |
| 1227 | // 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] | 1228 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1229 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1230 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1232 | case ISD::BR: |
| 1233 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1234 | // Ensure that libcalls are emitted before a branch. |
| 1235 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1236 | Tmp1 = LegalizeOp(Tmp1); |
| 1237 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1238 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1239 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1240 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1241 | case ISD::BRIND: |
| 1242 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1243 | // Ensure that libcalls are emitted before a branch. |
| 1244 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1245 | Tmp1 = LegalizeOp(Tmp1); |
| 1246 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1247 | |
| 1248 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1249 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1250 | case Legal: |
| 1251 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1252 | break; |
| 1253 | } |
| 1254 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1255 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1256 | case ISD::BR_JT: |
| 1257 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1258 | // Ensure that libcalls are emitted before a branch. |
| 1259 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1260 | Tmp1 = LegalizeOp(Tmp1); |
| 1261 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1262 | |
| 1263 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1264 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1265 | |
| 1266 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1267 | default: assert(0 && "This action is not supported yet!"); |
| 1268 | case TargetLowering::Legal: break; |
| 1269 | case TargetLowering::Custom: |
| 1270 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1271 | if (Tmp1.Val) Result = Tmp1; |
| 1272 | break; |
| 1273 | case TargetLowering::Expand: { |
| 1274 | SDOperand Chain = Result.getOperand(0); |
| 1275 | SDOperand Table = Result.getOperand(1); |
| 1276 | SDOperand Index = Result.getOperand(2); |
| 1277 | |
| 1278 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1279 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1280 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1281 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1282 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1283 | |
| 1284 | SDOperand LD; |
| 1285 | switch (EntrySize) { |
| 1286 | default: assert(0 && "Size of jump table not supported yet."); break; |
| 1287 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break; |
| 1288 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break; |
| 1289 | } |
| 1290 | |
| 1291 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1292 | // For PIC, the sequence is: |
| 1293 | // BRIND(load(Jumptable + index) + RelocBase) |
| 1294 | // RelocBase is the JumpTable on PPC and X86, GOT on Alpha |
| 1295 | SDOperand Reloc; |
| 1296 | if (TLI.usesGlobalOffsetTable()) |
| 1297 | Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy); |
| 1298 | else |
| 1299 | Reloc = Table; |
Evan Cheng | d063189 | 2006-10-31 02:31:00 +0000 | [diff] [blame] | 1300 | Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1301 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc); |
| 1302 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
| 1303 | } else { |
| 1304 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD); |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1309 | case ISD::BRCOND: |
| 1310 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1311 | // Ensure that libcalls are emitted before a return. |
| 1312 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1313 | Tmp1 = LegalizeOp(Tmp1); |
| 1314 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1315 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1316 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1317 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1318 | case Legal: |
| 1319 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1320 | break; |
| 1321 | case Promote: |
| 1322 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1323 | |
| 1324 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1325 | // that the value is properly zero extended. |
| 1326 | if (!TLI.MaskedValueIsZero(Tmp2, |
| 1327 | MVT::getIntVTBitMask(Tmp2.getValueType())^1)) |
| 1328 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1333 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1334 | |
| 1335 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1336 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1337 | case TargetLowering::Legal: break; |
| 1338 | case TargetLowering::Custom: |
| 1339 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1340 | if (Tmp1.Val) Result = Tmp1; |
| 1341 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1342 | case TargetLowering::Expand: |
| 1343 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1344 | // Node. |
| 1345 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1346 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1347 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1348 | Node->getOperand(2)); |
| 1349 | } else { |
| 1350 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1351 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1352 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1353 | Node->getOperand(2)); |
| 1354 | } |
| 1355 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1356 | } |
| 1357 | break; |
| 1358 | case ISD::BR_CC: |
| 1359 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1360 | // Ensure that libcalls are emitted before a branch. |
| 1361 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1362 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1363 | Tmp2 = Node->getOperand(2); // LHS |
| 1364 | Tmp3 = Node->getOperand(3); // RHS |
| 1365 | Tmp4 = Node->getOperand(1); // CC |
| 1366 | |
| 1367 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1368 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1369 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1370 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1371 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1372 | // the result against zero to select between true and false values. |
| 1373 | if (Tmp3.Val == 0) { |
| 1374 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1375 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1376 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1377 | |
| 1378 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1379 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1381 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1382 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1383 | case TargetLowering::Legal: break; |
| 1384 | case TargetLowering::Custom: |
| 1385 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1386 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1387 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1388 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1389 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1390 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1391 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1392 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1393 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1394 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1395 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1396 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1397 | MVT::ValueType VT = Node->getValueType(0); |
| 1398 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1399 | Tmp3 = Result.getValue(0); |
| 1400 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1401 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1402 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1403 | default: assert(0 && "This action is not supported yet!"); |
| 1404 | case TargetLowering::Legal: break; |
| 1405 | case TargetLowering::Custom: |
| 1406 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1407 | if (Tmp1.Val) { |
| 1408 | Tmp3 = LegalizeOp(Tmp1); |
| 1409 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1410 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | case TargetLowering::Promote: { |
| 1413 | // Only promote a load of vector type to another. |
| 1414 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1415 | // Change base type to a different vector type. |
| 1416 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1417 | |
| 1418 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1419 | LD->getSrcValueOffset()); |
| 1420 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1421 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1422 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1423 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1424 | } |
| 1425 | // Since loads produce two values, make sure to remember that we |
| 1426 | // legalized both of them. |
| 1427 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1428 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1429 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1430 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 1431 | MVT::ValueType SrcVT = LD->getLoadedVT(); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1432 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 1433 | default: assert(0 && "This action is not supported yet!"); |
| 1434 | case TargetLowering::Promote: |
| 1435 | assert(SrcVT == MVT::i1 && |
| 1436 | "Can only promote extending LOAD from i1 -> i8!"); |
| 1437 | Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1438 | LD->getSrcValue(), LD->getSrcValueOffset(), |
| 1439 | MVT::i8); |
| 1440 | Tmp1 = Result.getValue(0); |
| 1441 | Tmp2 = Result.getValue(1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1442 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1443 | case TargetLowering::Custom: |
| 1444 | isCustom = true; |
| 1445 | // FALLTHROUGH |
| 1446 | case TargetLowering::Legal: |
| 1447 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1448 | Tmp1 = Result.getValue(0); |
| 1449 | Tmp2 = Result.getValue(1); |
| 1450 | |
| 1451 | if (isCustom) { |
| 1452 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1453 | if (Tmp3.Val) { |
| 1454 | Tmp1 = LegalizeOp(Tmp3); |
| 1455 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1456 | } |
| 1457 | } |
| 1458 | break; |
| 1459 | case TargetLowering::Expand: |
| 1460 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 1461 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1462 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1463 | LD->getSrcValueOffset()); |
| 1464 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 1465 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1466 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1467 | break; |
| 1468 | } |
| 1469 | assert(ExtType != ISD::EXTLOAD && "EXTLOAD should always be supported!"); |
| 1470 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1471 | // zero/sign extend inreg. |
| 1472 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1473 | Tmp1, Tmp2, LD->getSrcValue(), |
| 1474 | LD->getSrcValueOffset(), SrcVT); |
| 1475 | SDOperand ValRes; |
| 1476 | if (ExtType == ISD::SEXTLOAD) |
| 1477 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1478 | Result, DAG.getValueType(SrcVT)); |
| 1479 | else |
| 1480 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 1481 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1482 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1483 | break; |
| 1484 | } |
| 1485 | // Since loads produce two values, make sure to remember that we legalized |
| 1486 | // both of them. |
| 1487 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1488 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1489 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1490 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1491 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1492 | case ISD::EXTRACT_ELEMENT: { |
| 1493 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1494 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1495 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1496 | case Legal: |
| 1497 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1498 | // 1 -> Hi |
| 1499 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1500 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1501 | TLI.getShiftAmountTy())); |
| 1502 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1503 | } else { |
| 1504 | // 0 -> Lo |
| 1505 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1506 | Node->getOperand(0)); |
| 1507 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1508 | break; |
| 1509 | case Expand: |
| 1510 | // Get both the low and high parts. |
| 1511 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1512 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1513 | Result = Tmp2; // 1 -> Hi |
| 1514 | else |
| 1515 | Result = Tmp1; // 0 -> Lo |
| 1516 | break; |
| 1517 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1518 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1519 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1520 | |
| 1521 | case ISD::CopyToReg: |
| 1522 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1523 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1524 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1525 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1526 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1527 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1528 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1529 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1530 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1531 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1532 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1533 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1534 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1535 | Tmp3); |
| 1536 | } else { |
| 1537 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | // Since this produces two values, make sure to remember that we legalized |
| 1541 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1542 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1543 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1544 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1545 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1546 | break; |
| 1547 | |
| 1548 | case ISD::RET: |
| 1549 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1550 | |
| 1551 | // Ensure that libcalls are emitted before a return. |
| 1552 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1553 | Tmp1 = LegalizeOp(Tmp1); |
| 1554 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1555 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1556 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1557 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 1558 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1559 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1560 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1561 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1562 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1563 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1564 | case Expand: |
| 1565 | if (Tmp2.getValueType() != MVT::Vector) { |
| 1566 | SDOperand Lo, Hi; |
| 1567 | ExpandOp(Tmp2, Lo, Hi); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1568 | if (Hi.Val) |
| 1569 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 1570 | else |
| 1571 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 1572 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1573 | } else { |
| 1574 | SDNode *InVal = Tmp2.Val; |
| 1575 | unsigned NumElems = |
| 1576 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1577 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1578 | |
| 1579 | // Figure out if there is a Packed type corresponding to this Vector |
| 1580 | // type. If so, convert to the packed type. |
| 1581 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1582 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1583 | // Turn this into a return of the packed type. |
| 1584 | Tmp2 = PackVectorOp(Tmp2, TVT); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1585 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1586 | } else if (NumElems == 1) { |
| 1587 | // Turn this into a return of the scalar type. |
| 1588 | Tmp2 = PackVectorOp(Tmp2, EVT); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1589 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1590 | |
| 1591 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 1592 | // should be returned in integer registers! |
| 1593 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1594 | // The scalarized value type may not be legal, e.g. it might require |
| 1595 | // promotion or expansion. Relegalize the return. |
| 1596 | Result = LegalizeOp(Result); |
| 1597 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1598 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 1599 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1600 | SDOperand Lo, Hi; |
| 1601 | SplitVectorOp(Tmp2, Lo, Hi); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1602 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1603 | Result = LegalizeOp(Result); |
| 1604 | } |
| 1605 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1606 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1607 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1608 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1609 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1610 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1611 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1612 | } |
| 1613 | break; |
| 1614 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1615 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1618 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1619 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1620 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1621 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1622 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1623 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1624 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1625 | break; |
| 1626 | case Expand: { |
| 1627 | SDOperand Lo, Hi; |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1628 | assert(Node->getOperand(i).getValueType() != MVT::Vector && |
| 1629 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1630 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1631 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1632 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1633 | if (Hi.Val) { |
| 1634 | NewValues.push_back(Hi); |
| 1635 | NewValues.push_back(Node->getOperand(i+1)); |
| 1636 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1637 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1638 | } |
| 1639 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1640 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1641 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1642 | |
| 1643 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1644 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1645 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1646 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 1647 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1648 | break; |
| 1649 | } |
| 1650 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1652 | if (Result.getOpcode() == ISD::RET) { |
| 1653 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1654 | default: assert(0 && "This action is not supported yet!"); |
| 1655 | case TargetLowering::Legal: break; |
| 1656 | case TargetLowering::Custom: |
| 1657 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1658 | if (Tmp1.Val) Result = Tmp1; |
| 1659 | break; |
| 1660 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1661 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1662 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1663 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1664 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1665 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1666 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1667 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1668 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1669 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 1670 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 1671 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 1672 | // to phase ordering between legalized code and the dag combiner. This |
| 1673 | // probably means that we need to integrate dag combiner and legalizer |
| 1674 | // together. |
| 1675 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
| 1676 | if (CFP->getValueType(0) == MVT::f32) { |
| 1677 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
| 1678 | } else { |
| 1679 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 1680 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
| 1681 | } |
| 1682 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1683 | ST->getSrcValueOffset()); |
| 1684 | break; |
| 1685 | } |
| 1686 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1687 | switch (getTypeAction(ST->getStoredVT())) { |
| 1688 | case Legal: { |
| 1689 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1690 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1691 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1692 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1693 | MVT::ValueType VT = Tmp3.getValueType(); |
| 1694 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1695 | default: assert(0 && "This action is not supported yet!"); |
| 1696 | case TargetLowering::Legal: break; |
| 1697 | case TargetLowering::Custom: |
| 1698 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1699 | if (Tmp1.Val) Result = Tmp1; |
| 1700 | break; |
| 1701 | case TargetLowering::Promote: |
| 1702 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 1703 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 1704 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 1705 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
| 1706 | ST->getSrcValue(), ST->getSrcValueOffset()); |
| 1707 | break; |
| 1708 | } |
| 1709 | break; |
| 1710 | } |
| 1711 | case Promote: |
| 1712 | // Truncate the value and store the result. |
| 1713 | Tmp3 = PromoteOp(ST->getValue()); |
| 1714 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1715 | ST->getSrcValueOffset(), ST->getStoredVT()); |
| 1716 | break; |
| 1717 | |
| 1718 | case Expand: |
| 1719 | unsigned IncrementSize = 0; |
| 1720 | SDOperand Lo, Hi; |
| 1721 | |
| 1722 | // If this is a vector type, then we have to calculate the increment as |
| 1723 | // the product of the element size in bytes, and the number of elements |
| 1724 | // in the high half of the vector. |
| 1725 | if (ST->getValue().getValueType() == MVT::Vector) { |
| 1726 | SDNode *InVal = ST->getValue().Val; |
| 1727 | unsigned NumElems = |
| 1728 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1729 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1730 | |
| 1731 | // Figure out if there is a Packed type corresponding to this Vector |
| 1732 | // type. If so, convert to the packed type. |
| 1733 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1734 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1735 | // Turn this into a normal store of the packed type. |
| 1736 | Tmp3 = PackVectorOp(Node->getOperand(1), TVT); |
| 1737 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1738 | ST->getSrcValueOffset()); |
| 1739 | Result = LegalizeOp(Result); |
| 1740 | break; |
| 1741 | } else if (NumElems == 1) { |
| 1742 | // Turn this into a normal store of the scalar type. |
| 1743 | Tmp3 = PackVectorOp(Node->getOperand(1), EVT); |
| 1744 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1745 | ST->getSrcValueOffset()); |
| 1746 | // The scalarized value type may not be legal, e.g. it might require |
| 1747 | // promotion or expansion. Relegalize the scalar store. |
| 1748 | Result = LegalizeOp(Result); |
| 1749 | break; |
| 1750 | } else { |
| 1751 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1752 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1753 | } |
| 1754 | } else { |
| 1755 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1756 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1757 | |
| 1758 | if (!TLI.isLittleEndian()) |
| 1759 | std::swap(Lo, Hi); |
| 1760 | } |
| 1761 | |
| 1762 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
| 1763 | ST->getSrcValueOffset()); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (Hi.Val == NULL) { |
| 1766 | // Must be int <-> float one-to-one expansion. |
| 1767 | Result = Lo; |
| 1768 | break; |
| 1769 | } |
| 1770 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1771 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1772 | getIntPtrConstant(IncrementSize)); |
| 1773 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1774 | "Pointers must be legal!"); |
| 1775 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1776 | // wrong. |
| 1777 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
| 1778 | ST->getSrcValueOffset()); |
| 1779 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1780 | break; |
| 1781 | } |
| 1782 | } else { |
| 1783 | // Truncating store |
| 1784 | assert(isTypeLegal(ST->getValue().getValueType()) && |
| 1785 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1786 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1787 | |
| 1788 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1789 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1790 | if (ST->getStoredVT() == MVT::i1 && |
| 1791 | TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) { |
| 1792 | // Promote the bool to a mask then store. |
| 1793 | Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3, |
| 1794 | DAG.getConstant(1, Tmp3.getValueType())); |
| 1795 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1796 | ST->getSrcValueOffset(), MVT::i8); |
| 1797 | } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 1798 | Tmp2 != ST->getBasePtr()) { |
| 1799 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1800 | ST->getOffset()); |
| 1801 | } |
| 1802 | |
| 1803 | MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT(); |
| 1804 | switch (TLI.getStoreXAction(StVT)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1805 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1806 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1807 | case TargetLowering::Custom: |
| 1808 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1809 | if (Tmp1.Val) Result = Tmp1; |
| 1810 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1811 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1812 | } |
| 1813 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1814 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1815 | case ISD::PCMARKER: |
| 1816 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1817 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1818 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1819 | case ISD::STACKSAVE: |
| 1820 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1821 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1822 | Tmp1 = Result.getValue(0); |
| 1823 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1825 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1826 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1827 | case TargetLowering::Legal: break; |
| 1828 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1829 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1830 | if (Tmp3.Val) { |
| 1831 | Tmp1 = LegalizeOp(Tmp3); |
| 1832 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1833 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1834 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1835 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1836 | // Expand to CopyFromReg if the target set |
| 1837 | // StackPointerRegisterToSaveRestore. |
| 1838 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1839 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1840 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1841 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1842 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1843 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1844 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1845 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1846 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1847 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1848 | |
| 1849 | // Since stacksave produce two values, make sure to remember that we |
| 1850 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1851 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1852 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1853 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1855 | case ISD::STACKRESTORE: |
| 1856 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1857 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1858 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1859 | |
| 1860 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1861 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1862 | case TargetLowering::Legal: break; |
| 1863 | case TargetLowering::Custom: |
| 1864 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1865 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1866 | break; |
| 1867 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1868 | // Expand to CopyToReg if the target set |
| 1869 | // StackPointerRegisterToSaveRestore. |
| 1870 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1871 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1872 | } else { |
| 1873 | Result = Tmp1; |
| 1874 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1875 | break; |
| 1876 | } |
| 1877 | break; |
| 1878 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1879 | case ISD::READCYCLECOUNTER: |
| 1880 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1881 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1882 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 1883 | Node->getValueType(0))) { |
| 1884 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1885 | case TargetLowering::Legal: |
| 1886 | Tmp1 = Result.getValue(0); |
| 1887 | Tmp2 = Result.getValue(1); |
| 1888 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1889 | case TargetLowering::Custom: |
| 1890 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1891 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 1892 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1893 | break; |
| 1894 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1895 | |
| 1896 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1897 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1898 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1899 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1900 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1901 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1902 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1903 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1904 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1905 | case Legal: |
| 1906 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1907 | break; |
| 1908 | case Promote: |
| 1909 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 1910 | // Make sure the condition is either zero or one. |
| 1911 | if (!TLI.MaskedValueIsZero(Tmp1, |
| 1912 | MVT::getIntVTBitMask(Tmp1.getValueType())^1)) |
| 1913 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1916 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1917 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1919 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1920 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1921 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1922 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1923 | case TargetLowering::Legal: break; |
| 1924 | case TargetLowering::Custom: { |
| 1925 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1926 | if (Tmp1.Val) Result = Tmp1; |
| 1927 | break; |
| 1928 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1929 | case TargetLowering::Expand: |
| 1930 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 1931 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 1932 | Tmp2, Tmp3, |
| 1933 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1934 | } else { |
| 1935 | Result = DAG.getSelectCC(Tmp1, |
| 1936 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1937 | Tmp2, Tmp3, ISD::SETNE); |
| 1938 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | case TargetLowering::Promote: { |
| 1941 | MVT::ValueType NVT = |
| 1942 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1943 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1944 | if (MVT::isVector(Tmp2.getValueType())) { |
| 1945 | ExtOp = ISD::BIT_CONVERT; |
| 1946 | TruncOp = ISD::BIT_CONVERT; |
| 1947 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1948 | ExtOp = ISD::ANY_EXTEND; |
| 1949 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1950 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1951 | ExtOp = ISD::FP_EXTEND; |
| 1952 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1953 | } |
| 1954 | // Promote each of the values to the new type. |
| 1955 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 1956 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 1957 | // Perform the larger operation, then round down. |
| 1958 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 1959 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 1960 | break; |
| 1961 | } |
| 1962 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1963 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1964 | case ISD::SELECT_CC: { |
| 1965 | Tmp1 = Node->getOperand(0); // LHS |
| 1966 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1967 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 1968 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1969 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1970 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1971 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 1972 | |
| 1973 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1974 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1975 | // the result against zero to select between true and false values. |
| 1976 | if (Tmp2.Val == 0) { |
| 1977 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 1978 | CC = DAG.getCondCode(ISD::SETNE); |
| 1979 | } |
| 1980 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 1981 | |
| 1982 | // Everything is legal, see if we should expand this op or something. |
| 1983 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 1984 | default: assert(0 && "This action is not supported yet!"); |
| 1985 | case TargetLowering::Legal: break; |
| 1986 | case TargetLowering::Custom: |
| 1987 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1988 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1989 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1990 | } |
| 1991 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1992 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1993 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1994 | Tmp1 = Node->getOperand(0); |
| 1995 | Tmp2 = Node->getOperand(1); |
| 1996 | Tmp3 = Node->getOperand(2); |
| 1997 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 1998 | |
| 1999 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2000 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2001 | // return the value we legalized, returned in the LHS |
| 2002 | if (Tmp2.Val == 0) { |
| 2003 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2004 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2005 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2007 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2008 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2009 | case TargetLowering::Custom: |
| 2010 | isCustom = true; |
| 2011 | // FALLTHROUGH. |
| 2012 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2013 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2014 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2015 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2016 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2017 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2018 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2019 | case TargetLowering::Promote: { |
| 2020 | // First step, figure out the appropriate operation to use. |
| 2021 | // Allow SETCC to not be supported for all legal data types |
| 2022 | // Mostly this targets FP |
| 2023 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
| 2024 | MVT::ValueType OldVT = NewInTy; |
| 2025 | |
| 2026 | // Scan for the appropriate larger type to use. |
| 2027 | while (1) { |
| 2028 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2029 | |
| 2030 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2031 | "Fell off of the edge of the integer world"); |
| 2032 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2033 | "Fell off of the edge of the floating point world"); |
| 2034 | |
| 2035 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2036 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | } |
| 2039 | if (MVT::isInteger(NewInTy)) |
| 2040 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2041 | else { |
| 2042 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2043 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2044 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2045 | Tmp1 = LegalizeOp(Tmp1); |
| 2046 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2047 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2048 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2049 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2050 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2051 | case TargetLowering::Expand: |
| 2052 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2053 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2054 | MVT::ValueType VT = Node->getValueType(0); |
| 2055 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2056 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2057 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2058 | break; |
| 2059 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2060 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2061 | case ISD::MEMSET: |
| 2062 | case ISD::MEMCPY: |
| 2063 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2064 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2065 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 2066 | |
| 2067 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 2068 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 2069 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 2070 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2071 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2072 | break; |
| 2073 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2074 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2075 | break; |
| 2076 | } |
| 2077 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2078 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2079 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2080 | |
| 2081 | SDOperand Tmp4; |
| 2082 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2083 | case Expand: { |
| 2084 | // Length is too big, just take the lo-part of the length. |
| 2085 | SDOperand HiPart; |
Chris Lattner | fa9aa2b | 2006-11-07 04:11:44 +0000 | [diff] [blame] | 2086 | ExpandOp(Node->getOperand(3), Tmp4, HiPart); |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2089 | case Legal: |
| 2090 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | case Promote: |
| 2093 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | |
| 2097 | SDOperand Tmp5; |
| 2098 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 2099 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2100 | case Legal: |
| 2101 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 2102 | break; |
| 2103 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2104 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 2105 | break; |
| 2106 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2107 | |
| 2108 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2109 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2110 | case TargetLowering::Custom: |
| 2111 | isCustom = true; |
| 2112 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2113 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2114 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2115 | if (isCustom) { |
| 2116 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2117 | if (Tmp1.Val) Result = Tmp1; |
| 2118 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2119 | break; |
| 2120 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2121 | // Otherwise, the target does not support this operation. Lower the |
| 2122 | // operation to an explicit libcall as appropriate. |
| 2123 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2124 | const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2125 | TargetLowering::ArgListTy Args; |
| 2126 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2127 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 2128 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2129 | if (Node->getOpcode() == ISD::MEMSET) { |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2130 | Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2131 | Args.push_back(Entry); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 2132 | // Extend the (previously legalized) ubyte argument to be an int value |
| 2133 | // for the call. |
| 2134 | if (Tmp3.getValueType() > MVT::i32) |
| 2135 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 2136 | else |
| 2137 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2138 | Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true; |
| 2139 | Args.push_back(Entry); |
| 2140 | Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false; |
| 2141 | Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2142 | |
| 2143 | FnName = "memset"; |
| 2144 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2145 | Node->getOpcode() == ISD::MEMMOVE) { |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2146 | Entry.Ty = IntPtrTy; Entry.isSigned = false; |
| 2147 | Entry.Node = Tmp2; Args.push_back(Entry); |
| 2148 | Entry.Node = Tmp3; Args.push_back(Entry); |
| 2149 | Entry.Node = Tmp4; Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2150 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2151 | } else { |
| 2152 | assert(0 && "Unknown op!"); |
| 2153 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2154 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2155 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2156 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2157 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2158 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2159 | break; |
| 2160 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2161 | } |
| 2162 | break; |
| 2163 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2164 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2165 | case ISD::SHL_PARTS: |
| 2166 | case ISD::SRA_PARTS: |
| 2167 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2168 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2169 | bool Changed = false; |
| 2170 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2171 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2172 | Changed |= Ops.back() != Node->getOperand(i); |
| 2173 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2174 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2175 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2176 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2177 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2178 | Node->getValueType(0))) { |
| 2179 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2180 | case TargetLowering::Legal: break; |
| 2181 | case TargetLowering::Custom: |
| 2182 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2183 | if (Tmp1.Val) { |
| 2184 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2185 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2186 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2187 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2188 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2189 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2190 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2191 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2192 | return RetVal; |
| 2193 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | } |
| 2196 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2197 | // Since these produce multiple values, make sure to remember that we |
| 2198 | // legalized all of them. |
| 2199 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2200 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2201 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2202 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2203 | |
| 2204 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2205 | case ISD::ADD: |
| 2206 | case ISD::SUB: |
| 2207 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2208 | case ISD::MULHS: |
| 2209 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2210 | case ISD::UDIV: |
| 2211 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2212 | case ISD::AND: |
| 2213 | case ISD::OR: |
| 2214 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2215 | case ISD::SHL: |
| 2216 | case ISD::SRL: |
| 2217 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2218 | case ISD::FADD: |
| 2219 | case ISD::FSUB: |
| 2220 | case ISD::FMUL: |
| 2221 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2222 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2223 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2224 | case Expand: assert(0 && "Not possible"); |
| 2225 | case Legal: |
| 2226 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2227 | break; |
| 2228 | case Promote: |
| 2229 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2230 | break; |
| 2231 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2232 | |
| 2233 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2234 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2235 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2236 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2237 | case TargetLowering::Legal: break; |
| 2238 | case TargetLowering::Custom: |
| 2239 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2240 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2241 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2242 | case TargetLowering::Expand: { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2243 | if (Node->getValueType(0) == MVT::i32) { |
| 2244 | switch (Node->getOpcode()) { |
| 2245 | default: assert(0 && "Do not know how to expand this integer BinOp!"); |
| 2246 | case ISD::UDIV: |
| 2247 | case ISD::SDIV: |
| 2248 | const char *FnName = Node->getOpcode() == ISD::UDIV |
| 2249 | ? "__udivsi3" : "__divsi3"; |
| 2250 | SDOperand Dummy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2251 | bool isSigned = Node->getOpcode() == ISD::SDIV; |
| 2252 | Result = ExpandLibCall(FnName, Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2253 | }; |
| 2254 | break; |
| 2255 | } |
| 2256 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2257 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2258 | "Cannot expand this binary operator!"); |
| 2259 | // Expand the operation into a bunch of nasty scalar code. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2260 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2261 | MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0)); |
| 2262 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2263 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2264 | i != e; ++i) { |
| 2265 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2266 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2267 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2268 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2269 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2270 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), |
| 2271 | &Ops[0], Ops.size()); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2274 | case TargetLowering::Promote: { |
| 2275 | switch (Node->getOpcode()) { |
| 2276 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2277 | case ISD::AND: |
| 2278 | case ISD::OR: |
| 2279 | case ISD::XOR: { |
| 2280 | MVT::ValueType OVT = Node->getValueType(0); |
| 2281 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2282 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 2283 | // Bit convert each of the values to the new type. |
| 2284 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 2285 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 2286 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2287 | // Bit convert the result back the original type. |
| 2288 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 2289 | break; |
| 2290 | } |
| 2291 | } |
| 2292 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2293 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2294 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2295 | |
| 2296 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2297 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2298 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2299 | case Expand: assert(0 && "Not possible"); |
| 2300 | case Legal: |
| 2301 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2302 | break; |
| 2303 | case Promote: |
| 2304 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2305 | break; |
| 2306 | } |
| 2307 | |
| 2308 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2309 | |
| 2310 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2311 | default: assert(0 && "Operation not supported"); |
| 2312 | case TargetLowering::Custom: |
| 2313 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2314 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2315 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2316 | case TargetLowering::Legal: break; |
| 2317 | case TargetLowering::Expand: |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2318 | // If this target supports fabs/fneg natively, do this efficiently. |
| 2319 | if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) && |
| 2320 | TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) { |
| 2321 | // Get the sign bit of the RHS. |
| 2322 | MVT::ValueType IVT = |
| 2323 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2324 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2325 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2326 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2327 | // Get the absolute value of the result. |
| 2328 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2329 | // Select between the nabs and abs value based on the sign bit of |
| 2330 | // the input. |
| 2331 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2332 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2333 | AbsVal), |
| 2334 | AbsVal); |
| 2335 | Result = LegalizeOp(Result); |
| 2336 | break; |
| 2337 | } |
| 2338 | |
| 2339 | // Otherwise, do bitwise ops! |
| 2340 | |
| 2341 | // copysign -> copysignf/copysign libcall. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2342 | const char *FnName; |
| 2343 | if (Node->getValueType(0) == MVT::f32) { |
| 2344 | FnName = "copysignf"; |
| 2345 | if (Tmp2.getValueType() != MVT::f32) // Force operands to match type. |
| 2346 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2347 | DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2)); |
| 2348 | } else { |
| 2349 | FnName = "copysign"; |
| 2350 | if (Tmp2.getValueType() != MVT::f64) // Force operands to match type. |
| 2351 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 2352 | DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2)); |
| 2353 | } |
| 2354 | SDOperand Dummy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2355 | Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2356 | break; |
| 2357 | } |
| 2358 | break; |
| 2359 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2360 | case ISD::ADDC: |
| 2361 | case ISD::SUBC: |
| 2362 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2363 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2364 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2365 | // Since this produces two values, make sure to remember that we legalized |
| 2366 | // both of them. |
| 2367 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2368 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2369 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2370 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2371 | case ISD::ADDE: |
| 2372 | case ISD::SUBE: |
| 2373 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2374 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2375 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2376 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2377 | // Since this produces two values, make sure to remember that we legalized |
| 2378 | // both of them. |
| 2379 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2380 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2381 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2382 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2383 | case ISD::BUILD_PAIR: { |
| 2384 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2385 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2386 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2387 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2388 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2389 | case TargetLowering::Promote: |
| 2390 | case TargetLowering::Custom: |
| 2391 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2392 | case TargetLowering::Legal: |
| 2393 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2394 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2395 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2396 | case TargetLowering::Expand: |
| 2397 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2398 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2399 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2400 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2401 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2402 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2403 | break; |
| 2404 | } |
| 2405 | break; |
| 2406 | } |
| 2407 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2408 | case ISD::UREM: |
| 2409 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2410 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2411 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2412 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2413 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2414 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2415 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2416 | case TargetLowering::Custom: |
| 2417 | isCustom = true; |
| 2418 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2419 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2420 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2421 | if (isCustom) { |
| 2422 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2423 | if (Tmp1.Val) Result = Tmp1; |
| 2424 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2425 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2426 | case TargetLowering::Expand: |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2427 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2428 | bool isSigned = DivOpc == ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2429 | if (MVT::isInteger(Node->getValueType(0))) { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2430 | if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) == |
| 2431 | TargetLowering::Legal) { |
| 2432 | // X % Y -> X-X/Y*Y |
| 2433 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2434 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2435 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2436 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2437 | } else { |
| 2438 | assert(Node->getValueType(0) == MVT::i32 && |
| 2439 | "Cannot expand this binary operator!"); |
| 2440 | const char *FnName = Node->getOpcode() == ISD::UREM |
| 2441 | ? "__umodsi3" : "__modsi3"; |
| 2442 | SDOperand Dummy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2443 | Result = ExpandLibCall(FnName, Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2444 | } |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2445 | } else { |
| 2446 | // Floating point mod -> fmod libcall. |
| 2447 | const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; |
| 2448 | SDOperand Dummy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2449 | Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2450 | } |
| 2451 | break; |
| 2452 | } |
| 2453 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2454 | case ISD::VAARG: { |
| 2455 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2456 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2457 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2458 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2459 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2460 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2461 | case TargetLowering::Custom: |
| 2462 | isCustom = true; |
| 2463 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2464 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2465 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2466 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2467 | Tmp1 = Result.getValue(1); |
| 2468 | |
| 2469 | if (isCustom) { |
| 2470 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2471 | if (Tmp2.Val) { |
| 2472 | Result = LegalizeOp(Tmp2); |
| 2473 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2474 | } |
| 2475 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2476 | break; |
| 2477 | case TargetLowering::Expand: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2478 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2479 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2480 | SV->getValue(), SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2481 | // Increment the pointer, VAList, to the next vaarg |
| 2482 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2483 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2484 | TLI.getPointerTy())); |
| 2485 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2486 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 2487 | SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2488 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2489 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2490 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2491 | Result = LegalizeOp(Result); |
| 2492 | break; |
| 2493 | } |
| 2494 | } |
| 2495 | // Since VAARG produces two values, make sure to remember that we |
| 2496 | // legalized both of them. |
| 2497 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2498 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2499 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | case ISD::VACOPY: |
| 2503 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2504 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2505 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2506 | |
| 2507 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2508 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2509 | case TargetLowering::Custom: |
| 2510 | isCustom = true; |
| 2511 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2512 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2513 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2514 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2515 | if (isCustom) { |
| 2516 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2517 | if (Tmp1.Val) Result = Tmp1; |
| 2518 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2519 | break; |
| 2520 | case TargetLowering::Expand: |
| 2521 | // This defaults to loading a pointer from the input and storing it to the |
| 2522 | // output, returning the chain. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2523 | SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2524 | SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2525 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(), |
| 2526 | SVD->getOffset()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2527 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(), |
| 2528 | SVS->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | } |
| 2531 | break; |
| 2532 | |
| 2533 | case ISD::VAEND: |
| 2534 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2535 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2536 | |
| 2537 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2538 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2539 | case TargetLowering::Custom: |
| 2540 | isCustom = true; |
| 2541 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2542 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2543 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2544 | if (isCustom) { |
| 2545 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2546 | if (Tmp1.Val) Result = Tmp1; |
| 2547 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2548 | break; |
| 2549 | case TargetLowering::Expand: |
| 2550 | Result = Tmp1; // Default to a no-op, return the chain |
| 2551 | break; |
| 2552 | } |
| 2553 | break; |
| 2554 | |
| 2555 | case ISD::VASTART: |
| 2556 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2557 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2558 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2559 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2560 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2561 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2562 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2563 | case TargetLowering::Legal: break; |
| 2564 | case TargetLowering::Custom: |
| 2565 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2566 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | } |
| 2569 | break; |
| 2570 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2571 | case ISD::ROTL: |
| 2572 | case ISD::ROTR: |
| 2573 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2574 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2575 | |
| 2576 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
| 2577 | "Cannot handle this yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2578 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2579 | break; |
| 2580 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2581 | case ISD::BSWAP: |
| 2582 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2583 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2584 | case TargetLowering::Custom: |
| 2585 | assert(0 && "Cannot custom legalize this yet!"); |
| 2586 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2587 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2588 | break; |
| 2589 | case TargetLowering::Promote: { |
| 2590 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2591 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2592 | unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2593 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2594 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2595 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2596 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2597 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2598 | break; |
| 2599 | } |
| 2600 | case TargetLowering::Expand: |
| 2601 | Result = ExpandBSWAP(Tmp1); |
| 2602 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2603 | } |
| 2604 | break; |
| 2605 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2606 | case ISD::CTPOP: |
| 2607 | case ISD::CTTZ: |
| 2608 | case ISD::CTLZ: |
| 2609 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2610 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2611 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2612 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2613 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2614 | break; |
| 2615 | case TargetLowering::Promote: { |
| 2616 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2617 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2618 | |
| 2619 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2620 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2621 | // Perform the larger operation, then subtract if needed. |
| 2622 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2623 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2624 | case ISD::CTPOP: |
| 2625 | Result = Tmp1; |
| 2626 | break; |
| 2627 | case ISD::CTTZ: |
| 2628 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2629 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
| 2630 | DAG.getConstant(getSizeInBits(NVT), NVT), |
| 2631 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2632 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2633 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 2634 | break; |
| 2635 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2636 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2637 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2638 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2639 | getSizeInBits(OVT), NVT)); |
| 2640 | break; |
| 2641 | } |
| 2642 | break; |
| 2643 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2644 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2645 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2646 | break; |
| 2647 | } |
| 2648 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2649 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2650 | // Unary operators |
| 2651 | case ISD::FABS: |
| 2652 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2653 | case ISD::FSQRT: |
| 2654 | case ISD::FSIN: |
| 2655 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2656 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2657 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2658 | case TargetLowering::Promote: |
| 2659 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2660 | isCustom = true; |
| 2661 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2662 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2663 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2664 | if (isCustom) { |
| 2665 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2666 | if (Tmp1.Val) Result = Tmp1; |
| 2667 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2668 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2669 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2670 | switch (Node->getOpcode()) { |
| 2671 | default: assert(0 && "Unreachable!"); |
| 2672 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2673 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2674 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2675 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2676 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2677 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2678 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2679 | MVT::ValueType VT = Node->getValueType(0); |
| 2680 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2681 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2682 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2683 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2684 | break; |
| 2685 | } |
| 2686 | case ISD::FSQRT: |
| 2687 | case ISD::FSIN: |
| 2688 | case ISD::FCOS: { |
| 2689 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2690 | const char *FnName = 0; |
| 2691 | switch(Node->getOpcode()) { |
| 2692 | case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; |
| 2693 | case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; |
| 2694 | case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; |
| 2695 | default: assert(0 && "Unreachable!"); |
| 2696 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2697 | SDOperand Dummy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2698 | Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2699 | break; |
| 2700 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2701 | } |
| 2702 | break; |
| 2703 | } |
| 2704 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2705 | case ISD::FPOWI: { |
| 2706 | // We always lower FPOWI into a libcall. No target support it yet. |
| 2707 | const char *FnName = Node->getValueType(0) == MVT::f32 |
| 2708 | ? "__powisf2" : "__powidf2"; |
| 2709 | SDOperand Dummy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2710 | Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2711 | break; |
| 2712 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2713 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2714 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2715 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2716 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2717 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2718 | Node->getOperand(0).getValueType())) { |
| 2719 | default: assert(0 && "Unknown operation action!"); |
| 2720 | case TargetLowering::Expand: |
| 2721 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2722 | break; |
| 2723 | case TargetLowering::Legal: |
| 2724 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2725 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | } |
| 2728 | } |
| 2729 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 2730 | case ISD::VBIT_CONVERT: { |
| 2731 | assert(Op.getOperand(0).getValueType() == MVT::Vector && |
| 2732 | "Can only have VBIT_CONVERT where input or output is MVT::Vector!"); |
| 2733 | |
| 2734 | // The input has to be a vector type, we have to either scalarize it, pack |
| 2735 | // it, or convert it based on whether the input vector type is legal. |
| 2736 | SDNode *InVal = Node->getOperand(0).Val; |
| 2737 | unsigned NumElems = |
| 2738 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 2739 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 2740 | |
| 2741 | // Figure out if there is a Packed type corresponding to this Vector |
| 2742 | // type. If so, convert to the packed type. |
| 2743 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 2744 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 2745 | // Turn this into a bit convert of the packed input. |
| 2746 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2747 | PackVectorOp(Node->getOperand(0), TVT)); |
| 2748 | break; |
| 2749 | } else if (NumElems == 1) { |
| 2750 | // Turn this into a bit convert of the scalar input. |
| 2751 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2752 | PackVectorOp(Node->getOperand(0), EVT)); |
| 2753 | break; |
| 2754 | } else { |
| 2755 | // FIXME: UNIMP! Store then reload |
| 2756 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 2757 | } |
| 2758 | } |
| 2759 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2760 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2761 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2762 | case ISD::UINT_TO_FP: { |
| 2763 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2764 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2765 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2766 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2767 | Node->getOperand(0).getValueType())) { |
| 2768 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2769 | case TargetLowering::Custom: |
| 2770 | isCustom = true; |
| 2771 | // FALLTHROUGH |
| 2772 | case TargetLowering::Legal: |
| 2773 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2774 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2775 | if (isCustom) { |
| 2776 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2777 | if (Tmp1.Val) Result = Tmp1; |
| 2778 | } |
| 2779 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2780 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2781 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2782 | LegalizeOp(Node->getOperand(0)), |
| 2783 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2784 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2785 | case TargetLowering::Promote: |
| 2786 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2787 | Node->getValueType(0), |
| 2788 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2789 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2790 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2791 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2792 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2793 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2794 | Node->getValueType(0), Node->getOperand(0)); |
| 2795 | break; |
| 2796 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2797 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2798 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2799 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2800 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2801 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2802 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2803 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2804 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2805 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2806 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2807 | break; |
| 2808 | } |
| 2809 | break; |
| 2810 | } |
| 2811 | case ISD::TRUNCATE: |
| 2812 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2813 | case Legal: |
| 2814 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2815 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2816 | break; |
| 2817 | case Expand: |
| 2818 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2819 | |
| 2820 | // Since the result is legal, we should just be able to truncate the low |
| 2821 | // part of the source. |
| 2822 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2823 | break; |
| 2824 | case Promote: |
| 2825 | Result = PromoteOp(Node->getOperand(0)); |
| 2826 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2827 | break; |
| 2828 | } |
| 2829 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2830 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2831 | case ISD::FP_TO_SINT: |
| 2832 | case ISD::FP_TO_UINT: |
| 2833 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2834 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2835 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2836 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2837 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2838 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2839 | case TargetLowering::Custom: |
| 2840 | isCustom = true; |
| 2841 | // FALLTHROUGH |
| 2842 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2843 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2844 | if (isCustom) { |
| 2845 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2846 | if (Tmp1.Val) Result = Tmp1; |
| 2847 | } |
| 2848 | break; |
| 2849 | case TargetLowering::Promote: |
| 2850 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2851 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2852 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2853 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2854 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2855 | SDOperand True, False; |
| 2856 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2857 | MVT::ValueType NVT = Node->getValueType(0); |
| 2858 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2859 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2860 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2861 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2862 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2863 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2864 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2865 | Tmp2)); |
| 2866 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2867 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2868 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2869 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2870 | } else { |
| 2871 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2872 | } |
| 2873 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2874 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2875 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2876 | case Expand: { |
| 2877 | // Convert f32 / f64 to i32 / i64. |
| 2878 | MVT::ValueType VT = Op.getValueType(); |
| 2879 | const char *FnName = 0; |
| 2880 | switch (Node->getOpcode()) { |
| 2881 | case ISD::FP_TO_SINT: |
| 2882 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 2883 | FnName = (VT == MVT::i32) ? "__fixsfsi" : "__fixsfdi"; |
| 2884 | else |
| 2885 | FnName = (VT == MVT::i32) ? "__fixdfsi" : "__fixdfdi"; |
| 2886 | break; |
| 2887 | case ISD::FP_TO_UINT: |
| 2888 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 2889 | FnName = (VT == MVT::i32) ? "__fixunssfsi" : "__fixunssfdi"; |
| 2890 | else |
| 2891 | FnName = (VT == MVT::i32) ? "__fixunsdfsi" : "__fixunsdfdi"; |
| 2892 | break; |
| 2893 | default: assert(0 && "Unreachable!"); |
| 2894 | } |
| 2895 | SDOperand Dummy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 2896 | Result = ExpandLibCall(FnName, Node, false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2897 | break; |
| 2898 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2899 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2900 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2901 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 2902 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2903 | break; |
| 2904 | } |
| 2905 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2907 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2908 | case ISD::ZERO_EXTEND: |
| 2909 | case ISD::SIGN_EXTEND: |
| 2910 | case ISD::FP_EXTEND: |
| 2911 | case ISD::FP_ROUND: |
| 2912 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2913 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2914 | case Legal: |
| 2915 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2916 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2917 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2918 | case Promote: |
| 2919 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2920 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2921 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2922 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2923 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2924 | case ISD::ZERO_EXTEND: |
| 2925 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2926 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2927 | Result = DAG.getZeroExtendInReg(Result, |
| 2928 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2929 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2930 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2931 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2932 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2933 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2934 | Result, |
| 2935 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2936 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2937 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2938 | Result = PromoteOp(Node->getOperand(0)); |
| 2939 | if (Result.getValueType() != Op.getValueType()) |
| 2940 | // Dynamically dead while we have only 2 FP types. |
| 2941 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 2942 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2943 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 2944 | Result = PromoteOp(Node->getOperand(0)); |
| 2945 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 2946 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2947 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2948 | } |
| 2949 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2950 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2951 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2952 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2953 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2954 | |
| 2955 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 2956 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2957 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 2958 | default: assert(0 && "This action not supported for this op yet!"); |
| 2959 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2960 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2961 | break; |
| 2962 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2963 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2964 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2965 | // NOTE: we could fall back on load/store here too for targets without |
| 2966 | // SAR. However, it is doubtful that any exist. |
| 2967 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 2968 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2969 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2970 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 2971 | Node->getOperand(0), ShiftCst); |
| 2972 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 2973 | Result, ShiftCst); |
| 2974 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 2975 | // The only way we can lower this is to turn it into a TRUNCSTORE, |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2976 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 2977 | |
| 2978 | // NOTE: there is a choice here between constantly creating new stack |
| 2979 | // slots and always reusing the same one. We currently always create |
| 2980 | // new ones, as reuse may inhibit scheduling. |
| 2981 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2982 | unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty); |
| 2983 | unsigned Align = TLI.getTargetData()->getTypeAlignment(Ty); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2984 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2985 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2986 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 2987 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2988 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 2989 | StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 2990 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2991 | Result, StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2992 | } else { |
| 2993 | assert(0 && "Unknown op"); |
| 2994 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2995 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2996 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2997 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2998 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2999 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3000 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3001 | assert(Result.getValueType() == Op.getValueType() && |
| 3002 | "Bad legalization!"); |
| 3003 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3004 | // Make sure that the generated code is itself legal. |
| 3005 | if (Result != Op) |
| 3006 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3007 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3008 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3009 | // means that we always must cache transformed nodes. |
| 3010 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3011 | return Result; |
| 3012 | } |
| 3013 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3014 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 3015 | /// promote it to compute the value into a larger type. The produced value will |
| 3016 | /// have the correct bits for the low portion of the register, but no guarantee |
| 3017 | /// 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] | 3018 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 3019 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3020 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3021 | assert(getTypeAction(VT) == Promote && |
| 3022 | "Caller should expand or legalize operands that are not promotable!"); |
| 3023 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 3024 | "Cannot promote to smaller type!"); |
| 3025 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3026 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3027 | SDOperand Result; |
| 3028 | SDNode *Node = Op.Val; |
| 3029 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3030 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 3031 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3033 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3034 | case ISD::CopyFromReg: |
| 3035 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3036 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3037 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3038 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3039 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3040 | assert(0 && "Do not know how to promote this operator!"); |
| 3041 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 3042 | case ISD::UNDEF: |
| 3043 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 3044 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3045 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 3046 | if (VT != MVT::i1) |
| 3047 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 3048 | else |
| 3049 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3050 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 3051 | break; |
| 3052 | case ISD::ConstantFP: |
| 3053 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 3054 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 3055 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 3056 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3057 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3058 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3059 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 3060 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3061 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3062 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3063 | case ISD::TRUNCATE: |
| 3064 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3065 | case Legal: |
| 3066 | Result = LegalizeOp(Node->getOperand(0)); |
| 3067 | assert(Result.getValueType() >= NVT && |
| 3068 | "This truncation doesn't make sense!"); |
| 3069 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 3070 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 3071 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 3072 | case Promote: |
| 3073 | // The truncation is not required, because we don't guarantee anything |
| 3074 | // about high bits anyway. |
| 3075 | Result = PromoteOp(Node->getOperand(0)); |
| 3076 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3077 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 3078 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3079 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 3080 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3081 | } |
| 3082 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3083 | case ISD::SIGN_EXTEND: |
| 3084 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3085 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3086 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3087 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 3088 | case Legal: |
| 3089 | // 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] | 3090 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | case Promote: |
| 3093 | // Promote the reg if it's smaller. |
| 3094 | Result = PromoteOp(Node->getOperand(0)); |
| 3095 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 3096 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 3097 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3098 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3099 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3100 | Result = DAG.getZeroExtendInReg(Result, |
| 3101 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3102 | break; |
| 3103 | } |
| 3104 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3105 | case ISD::BIT_CONVERT: |
| 3106 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 3107 | Result = PromoteOp(Result); |
| 3108 | break; |
| 3109 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3110 | case ISD::FP_EXTEND: |
| 3111 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 3112 | case ISD::FP_ROUND: |
| 3113 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3114 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 3115 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 3116 | case Legal: |
| 3117 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3118 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3119 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3120 | break; |
| 3121 | } |
| 3122 | break; |
| 3123 | |
| 3124 | case ISD::SINT_TO_FP: |
| 3125 | case ISD::UINT_TO_FP: |
| 3126 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3127 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3128 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3129 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3130 | break; |
| 3131 | |
| 3132 | case Promote: |
| 3133 | Result = PromoteOp(Node->getOperand(0)); |
| 3134 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 3135 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3136 | Result, |
| 3137 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3138 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3139 | Result = DAG.getZeroExtendInReg(Result, |
| 3140 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3141 | // No extra round required here. |
| 3142 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3145 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 3146 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3147 | // Round if we cannot tolerate excess precision. |
| 3148 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3149 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3150 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3151 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3152 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3153 | break; |
| 3154 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 3155 | case ISD::SIGN_EXTEND_INREG: |
| 3156 | Result = PromoteOp(Node->getOperand(0)); |
| 3157 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 3158 | Node->getOperand(1)); |
| 3159 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3160 | case ISD::FP_TO_SINT: |
| 3161 | case ISD::FP_TO_UINT: |
| 3162 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3163 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 3164 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3165 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3166 | break; |
| 3167 | case Promote: |
| 3168 | // The input result is prerounded, so we don't have to do anything |
| 3169 | // special. |
| 3170 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3171 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3172 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3173 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 3174 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 3175 | // we can use that instead. This allows us to generate better code for |
| 3176 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 3177 | // legal, such as PowerPC. |
| 3178 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3179 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 3180 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 3181 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3182 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 3183 | } else { |
| 3184 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3185 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3186 | break; |
| 3187 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3188 | case ISD::FABS: |
| 3189 | case ISD::FNEG: |
| 3190 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3191 | assert(Tmp1.getValueType() == NVT); |
| 3192 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3193 | // NOTE: we do not have to do any extra rounding here for |
| 3194 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 3195 | // precision, and these operations don't modify precision at all. |
| 3196 | break; |
| 3197 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3198 | case ISD::FSQRT: |
| 3199 | case ISD::FSIN: |
| 3200 | case ISD::FCOS: |
| 3201 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3202 | assert(Tmp1.getValueType() == NVT); |
| 3203 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3204 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3205 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3206 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3207 | break; |
| 3208 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3209 | case ISD::AND: |
| 3210 | case ISD::OR: |
| 3211 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3212 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3213 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3214 | case ISD::MUL: |
| 3215 | // 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] | 3216 | // 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] | 3217 | // that too is okay if they are integer operations. |
| 3218 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3219 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3220 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3221 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3222 | break; |
| 3223 | case ISD::FADD: |
| 3224 | case ISD::FSUB: |
| 3225 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3226 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3227 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3228 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3229 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3230 | |
| 3231 | // Floating point operations will give excess precision that we may not be |
| 3232 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 3233 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3234 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 3235 | // 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] | 3236 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3237 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3238 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3239 | break; |
| 3240 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3241 | case ISD::SDIV: |
| 3242 | case ISD::SREM: |
| 3243 | // These operators require that their input be sign extended. |
| 3244 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3245 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3246 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3247 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3248 | DAG.getValueType(VT)); |
| 3249 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3250 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3251 | } |
| 3252 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3253 | |
| 3254 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 3255 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3256 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3257 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3258 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3259 | case ISD::FDIV: |
| 3260 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3261 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3262 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 3263 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3264 | case Legal: |
| 3265 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3266 | break; |
| 3267 | case Promote: |
| 3268 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3269 | break; |
| 3270 | case Expand: |
| 3271 | assert(0 && "not implemented"); |
| 3272 | } |
| 3273 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3274 | case Legal: |
| 3275 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3276 | break; |
| 3277 | case Promote: |
| 3278 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3279 | break; |
| 3280 | case Expand: |
| 3281 | assert(0 && "not implemented"); |
| 3282 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3283 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3284 | |
| 3285 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3286 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3287 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3288 | DAG.getValueType(VT)); |
| 3289 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3290 | |
| 3291 | case ISD::UDIV: |
| 3292 | case ISD::UREM: |
| 3293 | // These operators require that their input be zero extended. |
| 3294 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3295 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3296 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3297 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3298 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3299 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3300 | break; |
| 3301 | |
| 3302 | case ISD::SHL: |
| 3303 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3304 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | case ISD::SRA: |
| 3307 | // The input value must be properly sign extended. |
| 3308 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3309 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3310 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3311 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3312 | break; |
| 3313 | case ISD::SRL: |
| 3314 | // The input value must be properly zero extended. |
| 3315 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3316 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3317 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3318 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3319 | |
| 3320 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3321 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 3322 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3323 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 3324 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 3325 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 3326 | } else { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3327 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3328 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3329 | SV->getValue(), SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3330 | // Increment the pointer, VAList, to the next vaarg |
| 3331 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3332 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3333 | TLI.getPointerTy())); |
| 3334 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3335 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 3336 | SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3337 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3338 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3339 | } |
| 3340 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3341 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3344 | case ISD::LOAD: { |
| 3345 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 3346 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 3347 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 3348 | Result = DAG.getExtLoad(ExtType, NVT, |
| 3349 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 3350 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 3351 | LD->getLoadedVT()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3352 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3353 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3354 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3355 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3356 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3357 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3358 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3359 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3360 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3361 | case ISD::SELECT_CC: |
| 3362 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3363 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3364 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3365 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3366 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3367 | case ISD::BSWAP: |
| 3368 | Tmp1 = Node->getOperand(0); |
| 3369 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3370 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3371 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3372 | DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), |
| 3373 | TLI.getShiftAmountTy())); |
| 3374 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3375 | case ISD::CTPOP: |
| 3376 | case ISD::CTTZ: |
| 3377 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3378 | // Zero extend the argument |
| 3379 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3380 | // Perform the larger operation, then subtract if needed. |
| 3381 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3382 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3383 | case ISD::CTPOP: |
| 3384 | Result = Tmp1; |
| 3385 | break; |
| 3386 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3387 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3388 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3389 | DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3390 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3391 | DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3392 | break; |
| 3393 | case ISD::CTLZ: |
| 3394 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3395 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 3396 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3397 | getSizeInBits(VT), NVT)); |
| 3398 | break; |
| 3399 | } |
| 3400 | break; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3401 | case ISD::VEXTRACT_VECTOR_ELT: |
| 3402 | Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
| 3403 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3404 | case ISD::EXTRACT_VECTOR_ELT: |
| 3405 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3406 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Make sure the result is itself legal. |
| 3412 | Result = LegalizeOp(Result); |
| 3413 | |
| 3414 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3415 | AddPromotedOperand(Op, Result); |
| 3416 | return Result; |
| 3417 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3418 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3419 | /// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a |
| 3420 | /// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based |
| 3421 | /// on the vector type. The return type of this matches the element type of the |
| 3422 | /// vector, which may not be legal for the target. |
| 3423 | SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3424 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3425 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3426 | // lower to a store then an indexed load. |
| 3427 | SDOperand Vec = Op.getOperand(0); |
| 3428 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3429 | |
| 3430 | SDNode *InVal = Vec.Val; |
| 3431 | unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 3432 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 3433 | |
| 3434 | // Figure out if there is a Packed type corresponding to this Vector |
| 3435 | // type. If so, convert to the packed type. |
| 3436 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3437 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 3438 | // Turn this into a packed extract_vector_elt operation. |
| 3439 | Vec = PackVectorOp(Vec, TVT); |
| 3440 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx); |
| 3441 | } else if (NumElems == 1) { |
| 3442 | // This must be an access of the only element. Return it. |
| 3443 | return PackVectorOp(Vec, EVT); |
| 3444 | } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) { |
| 3445 | SDOperand Lo, Hi; |
| 3446 | SplitVectorOp(Vec, Lo, Hi); |
| 3447 | if (CIdx->getValue() < NumElems/2) { |
| 3448 | Vec = Lo; |
| 3449 | } else { |
| 3450 | Vec = Hi; |
| 3451 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3452 | } |
| 3453 | |
| 3454 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3455 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3456 | return LowerVEXTRACT_VECTOR_ELT(Op); |
| 3457 | } else { |
| 3458 | // Variable index case for extract element. |
| 3459 | // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!! |
| 3460 | assert(0 && "unimp!"); |
| 3461 | return SDOperand(); |
| 3462 | } |
| 3463 | } |
| 3464 | |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3465 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3466 | /// memory traffic. |
| 3467 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3468 | SDOperand Vector = Op.getOperand(0); |
| 3469 | SDOperand Idx = Op.getOperand(1); |
| 3470 | |
| 3471 | // If the target doesn't support this, store the value to a temporary |
| 3472 | // stack slot, then LOAD the scalar element back out. |
| 3473 | SDOperand StackPtr = CreateStackTemporary(Vector.getValueType()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3474 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3475 | |
| 3476 | // Add the offset to the index. |
| 3477 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3478 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3479 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3480 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3481 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3482 | return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3483 | } |
| 3484 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3485 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3486 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3487 | /// with condition CC on the current target. This usually involves legalizing |
| 3488 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3489 | /// there may be no choice but to create a new SetCC node to represent the |
| 3490 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3491 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3492 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3493 | SDOperand &RHS, |
| 3494 | SDOperand &CC) { |
| 3495 | SDOperand Tmp1, Tmp2, Result; |
| 3496 | |
| 3497 | switch (getTypeAction(LHS.getValueType())) { |
| 3498 | case Legal: |
| 3499 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3500 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3501 | break; |
| 3502 | case Promote: |
| 3503 | Tmp1 = PromoteOp(LHS); // LHS |
| 3504 | Tmp2 = PromoteOp(RHS); // RHS |
| 3505 | |
| 3506 | // If this is an FP compare, the operands have already been extended. |
| 3507 | if (MVT::isInteger(LHS.getValueType())) { |
| 3508 | MVT::ValueType VT = LHS.getValueType(); |
| 3509 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3510 | |
| 3511 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3512 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3513 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3514 | // it. |
| 3515 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3516 | default: assert(0 && "Unknown integer comparison!"); |
| 3517 | case ISD::SETEQ: |
| 3518 | case ISD::SETNE: |
| 3519 | case ISD::SETUGE: |
| 3520 | case ISD::SETUGT: |
| 3521 | case ISD::SETULE: |
| 3522 | case ISD::SETULT: |
| 3523 | // ALL of these operations will work if we either sign or zero extend |
| 3524 | // the operands (including the unsigned comparisons!). Zero extend is |
| 3525 | // usually a simpler/cheaper operation, so prefer it. |
| 3526 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3527 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 3528 | break; |
| 3529 | case ISD::SETGE: |
| 3530 | case ISD::SETGT: |
| 3531 | case ISD::SETLT: |
| 3532 | case ISD::SETLE: |
| 3533 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3534 | DAG.getValueType(VT)); |
| 3535 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3536 | DAG.getValueType(VT)); |
| 3537 | break; |
| 3538 | } |
| 3539 | } |
| 3540 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3541 | case Expand: { |
| 3542 | MVT::ValueType VT = LHS.getValueType(); |
| 3543 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 3544 | // Expand into one or more soft-fp libcall(s). |
| 3545 | const char *FnName1 = NULL, *FnName2 = NULL; |
Chris Lattner | 2d53a32 | 2006-12-15 07:36:19 +0000 | [diff] [blame] | 3546 | ISD::CondCode CC1, CC2 = ISD::SETCC_INVALID; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3547 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3548 | case ISD::SETEQ: |
| 3549 | case ISD::SETOEQ: |
| 3550 | FnName1 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2"; |
| 3551 | CC1 = ISD::SETEQ; |
| 3552 | break; |
| 3553 | case ISD::SETNE: |
| 3554 | case ISD::SETUNE: |
| 3555 | FnName1 = (VT == MVT::f32) ? "__nesf2" : "__nedf2"; |
| 3556 | CC1 = ISD::SETNE; |
| 3557 | break; |
| 3558 | case ISD::SETGE: |
| 3559 | case ISD::SETOGE: |
| 3560 | FnName1 = (VT == MVT::f32) ? "__gesf2" : "__gedf2"; |
| 3561 | CC1 = ISD::SETGE; |
| 3562 | break; |
| 3563 | case ISD::SETLT: |
| 3564 | case ISD::SETOLT: |
| 3565 | FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2"; |
| 3566 | CC1 = ISD::SETLT; |
| 3567 | break; |
| 3568 | case ISD::SETLE: |
| 3569 | case ISD::SETOLE: |
| 3570 | FnName1 = (VT == MVT::f32) ? "__lesf2" : "__ledf2"; |
| 3571 | CC1 = ISD::SETLE; |
| 3572 | break; |
| 3573 | case ISD::SETGT: |
| 3574 | case ISD::SETOGT: |
| 3575 | FnName1 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2"; |
| 3576 | CC1 = ISD::SETGT; |
| 3577 | break; |
| 3578 | case ISD::SETUO: |
| 3579 | case ISD::SETO: |
| 3580 | FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2"; |
| 3581 | CC1 = cast<CondCodeSDNode>(CC)->get() == ISD::SETO |
| 3582 | ? ISD::SETEQ : ISD::SETNE; |
| 3583 | break; |
| 3584 | default: |
| 3585 | FnName1 = (VT == MVT::f32) ? "__unordsf2" : "__unorddf2"; |
| 3586 | CC1 = ISD::SETNE; |
| 3587 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3588 | case ISD::SETONE: |
| 3589 | // SETONE = SETOLT | SETOGT |
| 3590 | FnName1 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2"; |
| 3591 | CC1 = ISD::SETLT; |
| 3592 | // Fallthrough |
| 3593 | case ISD::SETUGT: |
| 3594 | FnName2 = (VT == MVT::f32) ? "__gtsf2" : "__gtdf2"; |
| 3595 | CC2 = ISD::SETGT; |
| 3596 | break; |
| 3597 | case ISD::SETUGE: |
| 3598 | FnName2 = (VT == MVT::f32) ? "__gesf2" : "__gedf2"; |
| 3599 | CC2 = ISD::SETGE; |
| 3600 | break; |
| 3601 | case ISD::SETULT: |
| 3602 | FnName2 = (VT == MVT::f32) ? "__ltsf2" : "__ltdf2"; |
| 3603 | CC2 = ISD::SETLT; |
| 3604 | break; |
| 3605 | case ISD::SETULE: |
| 3606 | FnName2 = (VT == MVT::f32) ? "__lesf2" : "__ledf2"; |
| 3607 | CC2 = ISD::SETLE; |
| 3608 | break; |
| 3609 | case ISD::SETUEQ: |
| 3610 | FnName2 = (VT == MVT::f32) ? "__eqsf2" : "__eqdf2"; |
| 3611 | CC2 = ISD::SETEQ; |
| 3612 | break; |
| 3613 | default: assert(0 && "Unsupported FP setcc!"); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | SDOperand Dummy; |
| 3618 | Tmp1 = ExpandLibCall(FnName1, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3619 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 3620 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3621 | Tmp2 = DAG.getConstant(0, MVT::i32); |
| 3622 | CC = DAG.getCondCode(CC1); |
| 3623 | if (FnName2) { |
| 3624 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC); |
| 3625 | LHS = ExpandLibCall(FnName2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3626 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 3627 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3628 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2, |
| 3629 | DAG.getCondCode(CC2)); |
| 3630 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3631 | Tmp2 = SDOperand(); |
| 3632 | } |
| 3633 | LHS = Tmp1; |
| 3634 | RHS = Tmp2; |
| 3635 | return; |
| 3636 | } |
| 3637 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3638 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 3639 | ExpandOp(LHS, LHSLo, LHSHi); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3640 | ExpandOp(RHS, RHSLo, RHSHi); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3641 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3642 | case ISD::SETEQ: |
| 3643 | case ISD::SETNE: |
| 3644 | if (RHSLo == RHSHi) |
| 3645 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 3646 | if (RHSCST->isAllOnesValue()) { |
| 3647 | // Comparison to -1. |
| 3648 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 3649 | Tmp2 = RHSLo; |
| 3650 | break; |
| 3651 | } |
| 3652 | |
| 3653 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 3654 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 3655 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3656 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3657 | break; |
| 3658 | default: |
| 3659 | // If this is a comparison of the sign bit, just look at the top part. |
| 3660 | // X > -1, x < 0 |
| 3661 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 3662 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 3663 | CST->getValue() == 0) || // X < 0 |
| 3664 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 3665 | CST->isAllOnesValue())) { // X > -1 |
| 3666 | Tmp1 = LHSHi; |
| 3667 | Tmp2 = RHSHi; |
| 3668 | break; |
| 3669 | } |
| 3670 | |
| 3671 | // FIXME: This generated code sucks. |
| 3672 | ISD::CondCode LowCC; |
| 3673 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3674 | default: assert(0 && "Unknown integer setcc!"); |
| 3675 | case ISD::SETLT: |
| 3676 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 3677 | case ISD::SETGT: |
| 3678 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 3679 | case ISD::SETLE: |
| 3680 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 3681 | case ISD::SETGE: |
| 3682 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 3683 | } |
| 3684 | |
| 3685 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 3686 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 3687 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 3688 | |
| 3689 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 3690 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
| 3691 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 3692 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 3693 | Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 3694 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 3695 | Result, Tmp1, Tmp2)); |
| 3696 | Tmp1 = Result; |
Nate Begeman | da06e9e | 2006-02-01 19:05:15 +0000 | [diff] [blame] | 3697 | Tmp2 = SDOperand(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3698 | } |
| 3699 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3700 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3701 | LHS = Tmp1; |
| 3702 | RHS = Tmp2; |
| 3703 | } |
| 3704 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3705 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 3706 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 3707 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3708 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 3709 | SDOperand SrcOp) { |
| 3710 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3711 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3712 | |
| 3713 | // Emit a store to the stack slot. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3714 | SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3715 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3716 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3719 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 3720 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 3721 | // then load the whole vector back out. |
| 3722 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3723 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3724 | NULL, 0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3725 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3729 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
| 3730 | /// support the operation, but do support the resultant packed vector type. |
| 3731 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 3732 | |
| 3733 | // 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] | 3734 | // 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] | 3735 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3736 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3737 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3738 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 3739 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3740 | bool isConstant = true; |
| 3741 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 3742 | SplatValue.getOpcode() != ISD::UNDEF) |
| 3743 | isConstant = false; |
| 3744 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3745 | for (unsigned i = 1; i < NumElems; ++i) { |
| 3746 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 3747 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3748 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3749 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3750 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3751 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3752 | |
| 3753 | // If this isn't a constant element or an undef, we can't use a constant |
| 3754 | // pool load. |
| 3755 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 3756 | V.getOpcode() != ISD::UNDEF) |
| 3757 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | if (isOnlyLowElement) { |
| 3761 | // If the low element is an undef too, then this whole things is an undef. |
| 3762 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 3763 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 3764 | // Otherwise, turn this into a scalar_to_vector node. |
| 3765 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3766 | Node->getOperand(0)); |
| 3767 | } |
| 3768 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3769 | // If all elements are constants, create a load from the constant pool. |
| 3770 | if (isConstant) { |
| 3771 | MVT::ValueType VT = Node->getValueType(0); |
| 3772 | const Type *OpNTy = |
| 3773 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 3774 | std::vector<Constant*> CV; |
| 3775 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 3776 | if (ConstantFPSDNode *V = |
| 3777 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
| 3778 | CV.push_back(ConstantFP::get(OpNTy, V->getValue())); |
| 3779 | } else if (ConstantSDNode *V = |
| 3780 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3781 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3782 | } else { |
| 3783 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 3784 | CV.push_back(UndefValue::get(OpNTy)); |
| 3785 | } |
| 3786 | } |
| 3787 | Constant *CP = ConstantPacked::get(CV); |
| 3788 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3789 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3790 | } |
| 3791 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3792 | if (SplatValue.Val) { // Splat of one value? |
| 3793 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 3794 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3795 | MVT::getIntVectorWithNumElements(NumElems); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3796 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3797 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3798 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3799 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3800 | |
| 3801 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3802 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3803 | // Get the splatted value into the low element of a vector register. |
| 3804 | SDOperand LowValVec = |
| 3805 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 3806 | |
| 3807 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 3808 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 3809 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 3810 | SplatMask); |
| 3811 | } |
| 3812 | } |
| 3813 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3814 | // If there are only two unique elements, we may be able to turn this into a |
| 3815 | // vector shuffle. |
| 3816 | if (Values.size() == 2) { |
| 3817 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 3818 | MVT::ValueType MaskVT = |
| 3819 | MVT::getIntVectorWithNumElements(NumElems); |
| 3820 | std::vector<SDOperand> MaskVec(NumElems); |
| 3821 | unsigned i = 0; |
| 3822 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3823 | E = Values.end(); I != E; ++I) { |
| 3824 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 3825 | EE = I->second.end(); II != EE; ++II) |
| 3826 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT)); |
| 3827 | i += NumElems; |
| 3828 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3829 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3830 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3831 | |
| 3832 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3833 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 3834 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3835 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3836 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3837 | E = Values.end(); I != E; ++I) { |
| 3838 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3839 | I->first); |
| 3840 | Ops.push_back(Op); |
| 3841 | } |
| 3842 | Ops.push_back(ShuffleMask); |
| 3843 | |
| 3844 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3845 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), |
| 3846 | &Ops[0], Ops.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3847 | } |
| 3848 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3849 | |
| 3850 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 3851 | // aligned object on the stack, store each element into it, then load |
| 3852 | // the result as a vector. |
| 3853 | MVT::ValueType VT = Node->getValueType(0); |
| 3854 | // Create the stack frame object. |
| 3855 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 3856 | |
| 3857 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3858 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3859 | unsigned TypeByteSize = |
| 3860 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3861 | // Store (in the right endianness) the elements to memory. |
| 3862 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3863 | // Ignore undef elements. |
| 3864 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 3865 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 3866 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3867 | |
| 3868 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 3869 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 3870 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3871 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3872 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | SDOperand StoreChain; |
| 3876 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3877 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3878 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3879 | else |
| 3880 | StoreChain = DAG.getEntryNode(); |
| 3881 | |
| 3882 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3883 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 3887 | /// specified value type. |
| 3888 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 3889 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 3890 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
| 3891 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); |
| 3892 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 3893 | } |
| 3894 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3895 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 3896 | SDOperand Op, SDOperand Amt, |
| 3897 | SDOperand &Lo, SDOperand &Hi) { |
| 3898 | // Expand the subcomponents. |
| 3899 | SDOperand LHSL, LHSH; |
| 3900 | ExpandOp(Op, LHSL, LHSH); |
| 3901 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3902 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3903 | MVT::ValueType VT = LHSL.getValueType(); |
| 3904 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3905 | Hi = Lo.getValue(1); |
| 3906 | } |
| 3907 | |
| 3908 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3909 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 3910 | /// smaller elements. If we can't find a way that is more efficient than a |
| 3911 | /// libcall on this target, return false. Otherwise, return true with the |
| 3912 | /// low-parts expanded into Lo and Hi. |
| 3913 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 3914 | SDOperand &Lo, SDOperand &Hi) { |
| 3915 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 3916 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3917 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3918 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3919 | SDOperand ShAmt = LegalizeOp(Amt); |
| 3920 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 3921 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 3922 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 3923 | |
| 3924 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 3925 | // and are disabled. |
| 3926 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 3927 | unsigned Cst = CN->getValue(); |
| 3928 | // Expand the incoming operand to be shifted, so that we have its parts |
| 3929 | SDOperand InL, InH; |
| 3930 | ExpandOp(Op, InL, InH); |
| 3931 | switch(Opc) { |
| 3932 | case ISD::SHL: |
| 3933 | if (Cst > VTBits) { |
| 3934 | Lo = DAG.getConstant(0, NVT); |
| 3935 | Hi = DAG.getConstant(0, NVT); |
| 3936 | } else if (Cst > NVTBits) { |
| 3937 | Lo = DAG.getConstant(0, NVT); |
| 3938 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3939 | } else if (Cst == NVTBits) { |
| 3940 | Lo = DAG.getConstant(0, NVT); |
| 3941 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3942 | } else { |
| 3943 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 3944 | Hi = DAG.getNode(ISD::OR, NVT, |
| 3945 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 3946 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3947 | } |
| 3948 | return true; |
| 3949 | case ISD::SRL: |
| 3950 | if (Cst > VTBits) { |
| 3951 | Lo = DAG.getConstant(0, NVT); |
| 3952 | Hi = DAG.getConstant(0, NVT); |
| 3953 | } else if (Cst > NVTBits) { |
| 3954 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 3955 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3956 | } else if (Cst == NVTBits) { |
| 3957 | Lo = InH; |
| 3958 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3959 | } else { |
| 3960 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3961 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3962 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3963 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3964 | } |
| 3965 | return true; |
| 3966 | case ISD::SRA: |
| 3967 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3968 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3969 | DAG.getConstant(NVTBits-1, ShTy)); |
| 3970 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3971 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3972 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3973 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3974 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3975 | } else if (Cst == NVTBits) { |
| 3976 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3977 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3978 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3979 | } else { |
| 3980 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3981 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3982 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3983 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3984 | } |
| 3985 | return true; |
| 3986 | } |
| 3987 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 3988 | |
| 3989 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 3990 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
| 3991 | uint64_t Mask = NVTBits, KnownZero, KnownOne; |
| 3992 | TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
| 3993 | |
| 3994 | // If we know that the high bit of the shift amount is one, then we can do |
| 3995 | // this as a couple of simple shifts. |
| 3996 | if (KnownOne & Mask) { |
| 3997 | // Mask out the high bit, which we know is set. |
| 3998 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
| 3999 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4000 | |
| 4001 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4002 | SDOperand InL, InH; |
| 4003 | ExpandOp(Op, InL, InH); |
| 4004 | switch(Opc) { |
| 4005 | case ISD::SHL: |
| 4006 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 4007 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 4008 | return true; |
| 4009 | case ISD::SRL: |
| 4010 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 4011 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 4012 | return true; |
| 4013 | case ISD::SRA: |
| 4014 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 4015 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4016 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 4017 | return true; |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | // If we know that the high bit of the shift amount is zero, then we can do |
| 4022 | // this as a couple of simple shifts. |
| 4023 | if (KnownZero & Mask) { |
| 4024 | // Compute 32-amt. |
| 4025 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 4026 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 4027 | Amt); |
| 4028 | |
| 4029 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4030 | SDOperand InL, InH; |
| 4031 | ExpandOp(Op, InL, InH); |
| 4032 | switch(Opc) { |
| 4033 | case ISD::SHL: |
| 4034 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 4035 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4036 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 4037 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 4038 | return true; |
| 4039 | case ISD::SRL: |
| 4040 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 4041 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4042 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4043 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4044 | return true; |
| 4045 | case ISD::SRA: |
| 4046 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 4047 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4048 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4049 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4050 | return true; |
| 4051 | } |
| 4052 | } |
| 4053 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4054 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4055 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4056 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4057 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4058 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 4059 | // does not fit into a register, return the lo part and set the hi part to the |
| 4060 | // by-reg argument. If it does fit into a single register, return the result |
| 4061 | // and leave the Hi part unset. |
| 4062 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4063 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4064 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 4065 | // The input chain to this libcall is the entry node of the function. |
| 4066 | // Legalizing the call will automatically add the previous call to the |
| 4067 | // dependence. |
| 4068 | SDOperand InChain = DAG.getEntryNode(); |
| 4069 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4070 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4071 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4072 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4073 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 4074 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4075 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
| 4076 | Entry.isSigned = isSigned; |
| 4077 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4078 | } |
| 4079 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4080 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4081 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4082 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4083 | std::pair<SDOperand,SDOperand> CallInfo = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4084 | TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 4085 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 4086 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4087 | // Legalize the call sequence, starting with the chain. This will advance |
| 4088 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 4089 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 4090 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4091 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4092 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4093 | default: assert(0 && "Unknown thing"); |
| 4094 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4095 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4096 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4097 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4098 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4099 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4100 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4101 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4105 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 4106 | /// destination type is legal. |
| 4107 | SDOperand SelectionDAGLegalize:: |
| 4108 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4109 | assert(isTypeLegal(DestTy) && "Destination type is not legal!"); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4110 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 4111 | "This is not an expansion!"); |
| 4112 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 4113 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4114 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4115 | assert(Source.getValueType() == MVT::i64 && |
| 4116 | "This only works for 64-bit -> FP"); |
| 4117 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 4118 | // incoming integer is set. To handle this, we dynamically test to see if |
| 4119 | // it is set, and, if so, add a fudge factor. |
| 4120 | SDOperand Lo, Hi; |
| 4121 | ExpandOp(Source, Lo, Hi); |
| 4122 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 4123 | // If this is unsigned, and not supported, first perform the conversion to |
| 4124 | // signed, then adjust the result if the sign bit is set. |
| 4125 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 4126 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 4127 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4128 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 4129 | DAG.getConstant(0, Hi.getValueType()), |
| 4130 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4131 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4132 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4133 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 4134 | uint64_t FF = 0x5f800000ULL; |
| 4135 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4136 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4137 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 4138 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4139 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4140 | SDOperand FudgeInReg; |
| 4141 | if (DestTy == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4142 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4143 | else { |
| 4144 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 4145 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4146 | CPIdx, NULL, 0, MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4147 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 4148 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4149 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4150 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4151 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 4152 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 4153 | default: assert(0 && "This action not implemented for this operation!"); |
| 4154 | case TargetLowering::Legal: |
| 4155 | case TargetLowering::Expand: |
| 4156 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4157 | case TargetLowering::Custom: { |
| 4158 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 4159 | Source), DAG); |
| 4160 | if (NV.Val) |
| 4161 | return LegalizeOp(NV); |
| 4162 | break; // The target decided this was legal after all |
| 4163 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 4166 | // Expand the source, then glue it back together for the call. We must expand |
| 4167 | // the source in case it is shared (this pass of legalize must traverse it). |
| 4168 | SDOperand SrcLo, SrcHi; |
| 4169 | ExpandOp(Source, SrcLo, SrcHi); |
| 4170 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 4171 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4172 | const char *FnName = 0; |
| 4173 | if (DestTy == MVT::f32) |
| 4174 | FnName = "__floatdisf"; |
| 4175 | else { |
| 4176 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 4177 | FnName = "__floatdidf"; |
| 4178 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4179 | |
| 4180 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 4181 | SDOperand UnusedHiPart; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4182 | return ExpandLibCall(FnName, Source.Val, isSigned, UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4183 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4184 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4185 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 4186 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 4187 | /// we expand it. At this point, we know that the result and operand types are |
| 4188 | /// legal for the target. |
| 4189 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 4190 | SDOperand Op0, |
| 4191 | MVT::ValueType DestVT) { |
| 4192 | if (Op0.getValueType() == MVT::i32) { |
| 4193 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 4194 | |
| 4195 | // get the stack frame index of a 8 byte buffer |
| 4196 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4197 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 4198 | // get address of 8 byte buffer |
| 4199 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4200 | // word offset constant for Hi/Lo address computation |
| 4201 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 4202 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 4203 | SDOperand Hi = StackSlot; |
| 4204 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 4205 | if (TLI.isLittleEndian()) |
| 4206 | std::swap(Hi, Lo); |
| 4207 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4208 | // if signed map to unsigned space |
| 4209 | SDOperand Op0Mapped; |
| 4210 | if (isSigned) { |
| 4211 | // constant used to invert sign bit (signed to unsigned mapping) |
| 4212 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 4213 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 4214 | } else { |
| 4215 | Op0Mapped = Op0; |
| 4216 | } |
| 4217 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4218 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4219 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4220 | // initial hi portion of constructed double |
| 4221 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 4222 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4223 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4224 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4225 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4226 | // FP constant to bias correct the final result |
| 4227 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 4228 | BitsToDouble(0x4330000080000000ULL) |
| 4229 | : BitsToDouble(0x4330000000000000ULL), |
| 4230 | MVT::f64); |
| 4231 | // subtract the bias |
| 4232 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 4233 | // final result |
| 4234 | SDOperand Result; |
| 4235 | // handle final rounding |
| 4236 | if (DestVT == MVT::f64) { |
| 4237 | // do nothing |
| 4238 | Result = Sub; |
| 4239 | } else { |
| 4240 | // if f32 then cast to f32 |
| 4241 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 4242 | } |
| 4243 | return Result; |
| 4244 | } |
| 4245 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 4246 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 4247 | |
| 4248 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 4249 | DAG.getConstant(0, Op0.getValueType()), |
| 4250 | ISD::SETLT); |
| 4251 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4252 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4253 | SignSet, Four, Zero); |
| 4254 | |
| 4255 | // If the sign bit of the integer is set, the large number will be treated |
| 4256 | // as a negative number. To counteract this, the dynamic code adds an |
| 4257 | // offset depending on the data type. |
| 4258 | uint64_t FF; |
| 4259 | switch (Op0.getValueType()) { |
| 4260 | default: assert(0 && "Unsupported integer type!"); |
| 4261 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 4262 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 4263 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 4264 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 4265 | } |
| 4266 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4267 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4268 | |
| 4269 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 4270 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4271 | SDOperand FudgeInReg; |
| 4272 | if (DestVT == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4273 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4274 | else { |
| 4275 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 4276 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 4277 | DAG.getEntryNode(), CPIdx, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4278 | NULL, 0, MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 4282 | } |
| 4283 | |
| 4284 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 4285 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 4286 | /// we promote it. At this point, we know that the result and operand types are |
| 4287 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 4288 | /// operation that takes a larger input. |
| 4289 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 4290 | MVT::ValueType DestVT, |
| 4291 | bool isSigned) { |
| 4292 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 4293 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 4294 | |
| 4295 | unsigned OpToUse = 0; |
| 4296 | |
| 4297 | // Scan for the appropriate larger type to use. |
| 4298 | while (1) { |
| 4299 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 4300 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 4301 | |
| 4302 | // If the target supports SINT_TO_FP of this type, use it. |
| 4303 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 4304 | default: break; |
| 4305 | case TargetLowering::Legal: |
| 4306 | if (!TLI.isTypeLegal(NewInTy)) |
| 4307 | break; // Can't use this datatype. |
| 4308 | // FALL THROUGH. |
| 4309 | case TargetLowering::Custom: |
| 4310 | OpToUse = ISD::SINT_TO_FP; |
| 4311 | break; |
| 4312 | } |
| 4313 | if (OpToUse) break; |
| 4314 | if (isSigned) continue; |
| 4315 | |
| 4316 | // If the target supports UINT_TO_FP of this type, use it. |
| 4317 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 4318 | default: break; |
| 4319 | case TargetLowering::Legal: |
| 4320 | if (!TLI.isTypeLegal(NewInTy)) |
| 4321 | break; // Can't use this datatype. |
| 4322 | // FALL THROUGH. |
| 4323 | case TargetLowering::Custom: |
| 4324 | OpToUse = ISD::UINT_TO_FP; |
| 4325 | break; |
| 4326 | } |
| 4327 | if (OpToUse) break; |
| 4328 | |
| 4329 | // Otherwise, try a larger type. |
| 4330 | } |
| 4331 | |
| 4332 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 4333 | // desired type then run the operation on it. |
| 4334 | return DAG.getNode(OpToUse, DestVT, |
| 4335 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 4336 | NewInTy, LegalOp)); |
| 4337 | } |
| 4338 | |
| 4339 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 4340 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 4341 | /// we promote it. At this point, we know that the result and operand types are |
| 4342 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 4343 | /// operation that returns a larger result. |
| 4344 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 4345 | MVT::ValueType DestVT, |
| 4346 | bool isSigned) { |
| 4347 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 4348 | MVT::ValueType NewOutTy = DestVT; |
| 4349 | |
| 4350 | unsigned OpToUse = 0; |
| 4351 | |
| 4352 | // Scan for the appropriate larger type to use. |
| 4353 | while (1) { |
| 4354 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 4355 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 4356 | |
| 4357 | // If the target supports FP_TO_SINT returning this type, use it. |
| 4358 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 4359 | default: break; |
| 4360 | case TargetLowering::Legal: |
| 4361 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4362 | break; // Can't use this datatype. |
| 4363 | // FALL THROUGH. |
| 4364 | case TargetLowering::Custom: |
| 4365 | OpToUse = ISD::FP_TO_SINT; |
| 4366 | break; |
| 4367 | } |
| 4368 | if (OpToUse) break; |
| 4369 | |
| 4370 | // If the target supports FP_TO_UINT of this type, use it. |
| 4371 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 4372 | default: break; |
| 4373 | case TargetLowering::Legal: |
| 4374 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4375 | break; // Can't use this datatype. |
| 4376 | // FALL THROUGH. |
| 4377 | case TargetLowering::Custom: |
| 4378 | OpToUse = ISD::FP_TO_UINT; |
| 4379 | break; |
| 4380 | } |
| 4381 | if (OpToUse) break; |
| 4382 | |
| 4383 | // Otherwise, try a larger type. |
| 4384 | } |
| 4385 | |
| 4386 | // Okay, we found the operation and type to use. Truncate the result of the |
| 4387 | // extended FP_TO_*INT operation to the desired size. |
| 4388 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 4389 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 4390 | } |
| 4391 | |
| 4392 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 4393 | /// |
| 4394 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 4395 | MVT::ValueType VT = Op.getValueType(); |
| 4396 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 4397 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 4398 | switch (VT) { |
| 4399 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 4400 | case MVT::i16: |
| 4401 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4402 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4403 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 4404 | case MVT::i32: |
| 4405 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4406 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4407 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4408 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4409 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 4410 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 4411 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4412 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4413 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4414 | case MVT::i64: |
| 4415 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4416 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4417 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4418 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4419 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4420 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4421 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4422 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4423 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 4424 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 4425 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 4426 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 4427 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 4428 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 4429 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 4430 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 4431 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4432 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4433 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 4434 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4435 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 4440 | /// |
| 4441 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 4442 | switch (Opc) { |
| 4443 | default: assert(0 && "Cannot expand this yet!"); |
| 4444 | case ISD::CTPOP: { |
| 4445 | static const uint64_t mask[6] = { |
| 4446 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 4447 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 4448 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 4449 | }; |
| 4450 | MVT::ValueType VT = Op.getValueType(); |
| 4451 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4452 | unsigned len = getSizeInBits(VT); |
| 4453 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4454 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 4455 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 4456 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4457 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 4458 | DAG.getNode(ISD::AND, VT, |
| 4459 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 4460 | } |
| 4461 | return Op; |
| 4462 | } |
| 4463 | case ISD::CTLZ: { |
| 4464 | // for now, we do this: |
| 4465 | // x = x | (x >> 1); |
| 4466 | // x = x | (x >> 2); |
| 4467 | // ... |
| 4468 | // x = x | (x >>16); |
| 4469 | // x = x | (x >>32); // for 64-bit input |
| 4470 | // return popcount(~x); |
| 4471 | // |
| 4472 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 4473 | MVT::ValueType VT = Op.getValueType(); |
| 4474 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4475 | unsigned len = getSizeInBits(VT); |
| 4476 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4477 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4478 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4479 | } |
| 4480 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4481 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4482 | } |
| 4483 | case ISD::CTTZ: { |
| 4484 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4485 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4486 | // { return 32 - nlz(~x & (x-1)); } |
| 4487 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 4488 | MVT::ValueType VT = Op.getValueType(); |
| 4489 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 4490 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 4491 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 4492 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 4493 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 4494 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 4495 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 4496 | return DAG.getNode(ISD::SUB, VT, |
| 4497 | DAG.getConstant(getSizeInBits(VT), VT), |
| 4498 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 4499 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 4500 | } |
| 4501 | } |
| 4502 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4503 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4504 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 4505 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 4506 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 4507 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 4508 | /// Lo/Hi values are returned. |
| 4509 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 4510 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4511 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4512 | SDNode *Node = Op.Val; |
| 4513 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4514 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
| 4515 | VT == MVT::Vector) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4516 | "Cannot expand to FP value or to larger int value!"); |
| 4517 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4518 | // See if we already expanded it. |
| 4519 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4520 | = ExpandedNodes.find(Op); |
| 4521 | if (I != ExpandedNodes.end()) { |
| 4522 | Lo = I->second.first; |
| 4523 | Hi = I->second.second; |
| 4524 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4527 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4528 | case ISD::CopyFromReg: |
| 4529 | assert(0 && "CopyFromReg must be legal!"); |
| 4530 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4531 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4532 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4533 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4534 | assert(0 && "Do not know how to expand this operator!"); |
| 4535 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4536 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 4537 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4538 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 4539 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4540 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4541 | case ISD::Constant: { |
| 4542 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 4543 | Lo = DAG.getConstant(Cst, NVT); |
| 4544 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 4545 | break; |
| 4546 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4547 | case ISD::ConstantFP: { |
| 4548 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 4549 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4550 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 4551 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4552 | break; |
| 4553 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4554 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4555 | // Return the operands. |
| 4556 | Lo = Node->getOperand(0); |
| 4557 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4558 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4559 | |
| 4560 | case ISD::SIGN_EXTEND_INREG: |
| 4561 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 4562 | // sext_inreg the low part if needed. |
| 4563 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 4564 | |
| 4565 | // The high part gets the sign extension from the lo-part. This handles |
| 4566 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4567 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4568 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 4569 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4570 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4571 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4572 | case ISD::BSWAP: { |
| 4573 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4574 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 4575 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 4576 | Lo = TempLo; |
| 4577 | break; |
| 4578 | } |
| 4579 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4580 | case ISD::CTPOP: |
| 4581 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 4582 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 4583 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 4584 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4585 | Hi = DAG.getConstant(0, NVT); |
| 4586 | break; |
| 4587 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4588 | case ISD::CTLZ: { |
| 4589 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4590 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4591 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4592 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4593 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 4594 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4595 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 4596 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 4597 | |
| 4598 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 4599 | Hi = DAG.getConstant(0, NVT); |
| 4600 | break; |
| 4601 | } |
| 4602 | |
| 4603 | case ISD::CTTZ: { |
| 4604 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4605 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4606 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4607 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4608 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 4609 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4610 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 4611 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 4612 | |
| 4613 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 4614 | Hi = DAG.getConstant(0, NVT); |
| 4615 | break; |
| 4616 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4617 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4618 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4619 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4620 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4621 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 4622 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 4623 | |
| 4624 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4625 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4626 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 4627 | if (!TLI.isLittleEndian()) |
| 4628 | std::swap(Lo, Hi); |
| 4629 | break; |
| 4630 | } |
| 4631 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4632 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4633 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 4634 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 4635 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 4636 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4637 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4638 | if (ExtType == ISD::NON_EXTLOAD) { |
| 4639 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset()); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4640 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4641 | // f32->i32 or f64->i64 one to one expansion. |
| 4642 | // Remember that we legalized the chain. |
| 4643 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4644 | // Recursively expand the new load. |
| 4645 | if (getTypeAction(NVT) == Expand) |
| 4646 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4647 | break; |
| 4648 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 4649 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4650 | // Increment the pointer to the other half. |
| 4651 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 4652 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4653 | getIntPtrConstant(IncrementSize)); |
| 4654 | // FIXME: This creates a bogus srcvalue! |
| 4655 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), LD->getSrcValueOffset()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4656 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4657 | // Build a factor node to remember that this load is independent of the |
| 4658 | // other one. |
| 4659 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4660 | Hi.getValue(1)); |
| 4661 | |
| 4662 | // Remember that we legalized the chain. |
| 4663 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 4664 | if (!TLI.isLittleEndian()) |
| 4665 | std::swap(Lo, Hi); |
| 4666 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 4667 | MVT::ValueType EVT = LD->getLoadedVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 4668 | |
| 4669 | if (VT == MVT::f64 && EVT == MVT::f32) { |
| 4670 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 4671 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
| 4672 | LD->getSrcValueOffset()); |
| 4673 | // Remember that we legalized the chain. |
| 4674 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 4675 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 4676 | break; |
| 4677 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4678 | |
| 4679 | if (EVT == NVT) |
| 4680 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
| 4681 | LD->getSrcValueOffset()); |
| 4682 | else |
| 4683 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
| 4684 | LD->getSrcValueOffset(), EVT); |
| 4685 | |
| 4686 | // Remember that we legalized the chain. |
| 4687 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 4688 | |
| 4689 | if (ExtType == ISD::SEXTLOAD) { |
| 4690 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 4691 | // lo part. |
| 4692 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 4693 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4694 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 4695 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 4696 | // The high part is just a zero. |
| 4697 | Hi = DAG.getConstant(0, NVT); |
| 4698 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 4699 | // The high part is undefined. |
| 4700 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4701 | } |
| 4702 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4703 | break; |
| 4704 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4705 | case ISD::AND: |
| 4706 | case ISD::OR: |
| 4707 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 4708 | SDOperand LL, LH, RL, RH; |
| 4709 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4710 | ExpandOp(Node->getOperand(1), RL, RH); |
| 4711 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 4712 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 4713 | break; |
| 4714 | } |
| 4715 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4716 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4717 | ExpandOp(Node->getOperand(1), LL, LH); |
| 4718 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4719 | if (getTypeAction(NVT) == Expand) |
| 4720 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4721 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4722 | if (VT != MVT::f32) |
| 4723 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4724 | break; |
| 4725 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4726 | case ISD::SELECT_CC: { |
| 4727 | SDOperand TL, TH, FL, FH; |
| 4728 | ExpandOp(Node->getOperand(2), TL, TH); |
| 4729 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4730 | if (getTypeAction(NVT) == Expand) |
| 4731 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4732 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4733 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4734 | if (VT != MVT::f32) |
| 4735 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4736 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4737 | break; |
| 4738 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4739 | case ISD::ANY_EXTEND: |
| 4740 | // The low part is any extension of the input (which degenerates to a copy). |
| 4741 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 4742 | // The high part is undefined. |
| 4743 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4744 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4745 | case ISD::SIGN_EXTEND: { |
| 4746 | // The low part is just a sign extension of the input (which degenerates to |
| 4747 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4748 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4749 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4750 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4751 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 4752 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4753 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4754 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4755 | break; |
| 4756 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4757 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4758 | // The low part is just a zero extension of the input (which degenerates to |
| 4759 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4760 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4761 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4762 | // The high part is just a zero. |
| 4763 | Hi = DAG.getConstant(0, NVT); |
| 4764 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4765 | |
| 4766 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4767 | SDOperand Tmp; |
| 4768 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 4769 | // If the target wants to, allow it to lower this itself. |
| 4770 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4771 | case Expand: assert(0 && "cannot expand FP!"); |
| 4772 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 4773 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 4774 | } |
| 4775 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 4776 | } |
| 4777 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4778 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 4779 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4780 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4781 | if (getTypeAction(NVT) == Expand) |
| 4782 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 4783 | break; |
| 4784 | } |
| 4785 | |
| 4786 | // If source operand will be expanded to the same type as VT, i.e. |
| 4787 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 4788 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 4789 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 4790 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4791 | break; |
| 4792 | } |
| 4793 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4794 | // Turn this into a load/store pair by default. |
| 4795 | if (Tmp.Val == 0) |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 4796 | Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0)); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4797 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4798 | ExpandOp(Tmp, Lo, Hi); |
| 4799 | break; |
| 4800 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4801 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4802 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4803 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 4804 | TargetLowering::Custom && |
| 4805 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4806 | Lo = TLI.LowerOperation(Op, DAG); |
| 4807 | assert(Lo.Val && "Node must be custom expanded!"); |
| 4808 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4809 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4810 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4811 | break; |
| 4812 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4813 | // These operators cannot be expanded directly, emit them as calls to |
| 4814 | // library functions. |
| 4815 | case ISD::FP_TO_SINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4816 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4817 | SDOperand Op; |
| 4818 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4819 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4820 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4821 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4822 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4823 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4824 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 4825 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4826 | // Now that the custom expander is done, expand the result, which is still |
| 4827 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4828 | if (Op.Val) { |
| 4829 | ExpandOp(Op, Lo, Hi); |
| 4830 | break; |
| 4831 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4832 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4833 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4834 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4835 | Lo = ExpandLibCall("__fixsfdi", Node, false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4836 | else |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4837 | Lo = ExpandLibCall("__fixdfdi", Node, false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4838 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4839 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4840 | case ISD::FP_TO_UINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4841 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4842 | SDOperand Op; |
| 4843 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4844 | case Expand: assert(0 && "cannot expand FP!"); |
| 4845 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4846 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 4847 | } |
| 4848 | |
| 4849 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 4850 | |
| 4851 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4852 | if (Op.Val) { |
| 4853 | ExpandOp(Op, Lo, Hi); |
| 4854 | break; |
| 4855 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4856 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4857 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4858 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4859 | Lo = ExpandLibCall("__fixunssfdi", Node, false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4860 | else |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4861 | Lo = ExpandLibCall("__fixunsdfdi", Node, false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4862 | break; |
| 4863 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4864 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4865 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4866 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4867 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4868 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4869 | Op = TLI.LowerOperation(Op, DAG); |
| 4870 | if (Op.Val) { |
| 4871 | // Now that the custom expander is done, expand the result, which is |
| 4872 | // still VT. |
| 4873 | ExpandOp(Op, Lo, Hi); |
| 4874 | break; |
| 4875 | } |
| 4876 | } |
| 4877 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 4878 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 4879 | // this X << 1 as X+X. |
| 4880 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
| 4881 | if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
| 4882 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 4883 | SDOperand LoOps[2], HiOps[3]; |
| 4884 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 4885 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 4886 | LoOps[1] = LoOps[0]; |
| 4887 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 4888 | |
| 4889 | HiOps[1] = HiOps[0]; |
| 4890 | HiOps[2] = Lo.getValue(1); |
| 4891 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4896 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4897 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4898 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4899 | |
| 4900 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4901 | TargetLowering::LegalizeAction Action = |
| 4902 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 4903 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4904 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4905 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | } |
| 4908 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4909 | // Otherwise, emit a libcall. |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4910 | Lo = ExpandLibCall("__ashldi3", Node, false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4911 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4912 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4913 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4914 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4915 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4916 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4917 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4918 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4919 | Op = TLI.LowerOperation(Op, DAG); |
| 4920 | if (Op.Val) { |
| 4921 | // Now that the custom expander is done, expand the result, which is |
| 4922 | // still VT. |
| 4923 | ExpandOp(Op, Lo, Hi); |
| 4924 | break; |
| 4925 | } |
| 4926 | } |
| 4927 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4928 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4929 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4930 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4931 | |
| 4932 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4933 | TargetLowering::LegalizeAction Action = |
| 4934 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 4935 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4936 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4937 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4938 | break; |
| 4939 | } |
| 4940 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4941 | // Otherwise, emit a libcall. |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4942 | Lo = ExpandLibCall("__ashrdi3", Node, true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4943 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4947 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4948 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4949 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4950 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4951 | Op = TLI.LowerOperation(Op, DAG); |
| 4952 | if (Op.Val) { |
| 4953 | // Now that the custom expander is done, expand the result, which is |
| 4954 | // still VT. |
| 4955 | ExpandOp(Op, Lo, Hi); |
| 4956 | break; |
| 4957 | } |
| 4958 | } |
| 4959 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4960 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4961 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4962 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4963 | |
| 4964 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4965 | TargetLowering::LegalizeAction Action = |
| 4966 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 4967 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4968 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4969 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | } |
| 4972 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4973 | // Otherwise, emit a libcall. |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 4974 | Lo = ExpandLibCall("__lshrdi3", Node, false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4975 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4976 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4977 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4978 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4979 | case ISD::SUB: { |
| 4980 | // If the target wants to custom expand this, let them. |
| 4981 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 4982 | TargetLowering::Custom) { |
| 4983 | Op = TLI.LowerOperation(Op, DAG); |
| 4984 | if (Op.Val) { |
| 4985 | ExpandOp(Op, Lo, Hi); |
| 4986 | break; |
| 4987 | } |
| 4988 | } |
| 4989 | |
| 4990 | // Expand the subcomponents. |
| 4991 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 4992 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 4993 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 4994 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 4995 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4996 | LoOps[0] = LHSL; |
| 4997 | LoOps[1] = RHSL; |
| 4998 | HiOps[0] = LHSH; |
| 4999 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5000 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5001 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5002 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5003 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5004 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5005 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5006 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5007 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5008 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 5009 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5010 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5011 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5012 | // If the target wants to custom expand this, let them. |
| 5013 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 5014 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 5015 | if (New.Val) { |
| 5016 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5017 | break; |
| 5018 | } |
| 5019 | } |
| 5020 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5021 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 5022 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5023 | if (HasMULHS || HasMULHU) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5024 | SDOperand LL, LH, RL, RH; |
| 5025 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5026 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5027 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 5028 | // FIXME: Move this to the dag combiner. |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5029 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 5030 | // extended the sign bit of the low half through the upper half, and if so |
| 5031 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 5032 | // i64 x i64 multiply. |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5033 | if (HasMULHS && |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5034 | // is RH an extension of the sign bit of RL? |
| 5035 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 5036 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 5037 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 5038 | // is LH an extension of the sign bit of LL? |
| 5039 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 5040 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 5041 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5042 | // Low part: |
| 5043 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5044 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5045 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5046 | break; |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5047 | } else if (HasMULHU) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5048 | // Low part: |
| 5049 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5050 | |
| 5051 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5052 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 5053 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 5054 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 5055 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 5056 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5057 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5058 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5059 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5060 | |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame^] | 5061 | Lo = ExpandLibCall("__muldi3" , Node, false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5062 | break; |
| 5063 | } |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5064 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, true, Hi); break; |
| 5065 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, false, Hi); break; |
| 5066 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, true, Hi); break; |
| 5067 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, false, Hi); break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 5068 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5069 | case ISD::FADD: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5070 | Lo = ExpandLibCall(((VT == MVT::f32) ? "__addsf3" : "__adddf3"), Node, |
| 5071 | false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5072 | break; |
| 5073 | case ISD::FSUB: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5074 | Lo = ExpandLibCall(((VT == MVT::f32) ? "__subsf3" : "__subdf3"), Node, |
| 5075 | false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5076 | break; |
| 5077 | case ISD::FMUL: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5078 | Lo = ExpandLibCall(((VT == MVT::f32) ? "__mulsf3" : "__muldf3"), Node, |
| 5079 | false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5080 | break; |
| 5081 | case ISD::FDIV: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5082 | Lo = ExpandLibCall(((VT == MVT::f32) ? "__divsf3" : "__divdf3"), Node, |
| 5083 | false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5084 | break; |
| 5085 | case ISD::FP_EXTEND: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5086 | Lo = ExpandLibCall("__extendsfdf2", Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5087 | break; |
| 5088 | case ISD::FP_ROUND: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5089 | Lo = ExpandLibCall("__truncdfsf2", Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5090 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5091 | case ISD::FSQRT: |
| 5092 | case ISD::FSIN: |
| 5093 | case ISD::FCOS: { |
| 5094 | const char *FnName = 0; |
| 5095 | switch(Node->getOpcode()) { |
| 5096 | case ISD::FSQRT: FnName = (VT == MVT::f32) ? "sqrtf" : "sqrt"; break; |
| 5097 | case ISD::FSIN: FnName = (VT == MVT::f32) ? "sinf" : "sin"; break; |
| 5098 | case ISD::FCOS: FnName = (VT == MVT::f32) ? "cosf" : "cos"; break; |
| 5099 | default: assert(0 && "Unreachable!"); |
| 5100 | } |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5101 | Lo = ExpandLibCall(FnName, Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5102 | break; |
| 5103 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 5104 | case ISD::FABS: { |
| 5105 | SDOperand Mask = (VT == MVT::f64) |
| 5106 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 5107 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 5108 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5109 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5110 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 5111 | if (getTypeAction(NVT) == Expand) |
| 5112 | ExpandOp(Lo, Lo, Hi); |
| 5113 | break; |
| 5114 | } |
| 5115 | case ISD::FNEG: { |
| 5116 | SDOperand Mask = (VT == MVT::f64) |
| 5117 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 5118 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 5119 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5120 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5121 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 5122 | if (getTypeAction(NVT) == Expand) |
| 5123 | ExpandOp(Lo, Lo, Hi); |
| 5124 | break; |
| 5125 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5126 | case ISD::SINT_TO_FP: |
| 5127 | case ISD::UINT_TO_FP: { |
| 5128 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 5129 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
| 5130 | const char *FnName = 0; |
| 5131 | if (Node->getOperand(0).getValueType() == MVT::i64) { |
| 5132 | if (VT == MVT::f32) |
| 5133 | FnName = isSigned ? "__floatdisf" : "__floatundisf"; |
| 5134 | else |
| 5135 | FnName = isSigned ? "__floatdidf" : "__floatundidf"; |
| 5136 | } else { |
| 5137 | if (VT == MVT::f32) |
| 5138 | FnName = isSigned ? "__floatsisf" : "__floatunsisf"; |
| 5139 | else |
| 5140 | FnName = isSigned ? "__floatsidf" : "__floatunsidf"; |
| 5141 | } |
| 5142 | |
| 5143 | // Promote the operand if needed. |
| 5144 | if (getTypeAction(SrcVT) == Promote) { |
| 5145 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 5146 | Tmp = isSigned |
| 5147 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 5148 | DAG.getValueType(SrcVT)) |
| 5149 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 5150 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 5151 | } |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5152 | Lo = ExpandLibCall(FnName, Node, isSigned, Hi); |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5153 | break; |
| 5154 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5155 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5156 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5157 | // Make sure the resultant values have been legalized themselves, unless this |
| 5158 | // is a type that requires multi-step expansion. |
| 5159 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 5160 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5161 | if (Hi.Val) |
| 5162 | // Don't legalize the high part if it is expanded to a single node. |
| 5163 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5164 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5165 | |
| 5166 | // Remember in a map if the values will be reused later. |
| 5167 | bool isNew = |
| 5168 | ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 5169 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5172 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 5173 | /// two smaller values of MVT::Vector type. |
| 5174 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 5175 | SDOperand &Hi) { |
| 5176 | assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); |
| 5177 | SDNode *Node = Op.Val; |
| 5178 | unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); |
| 5179 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 5180 | unsigned NewNumElts = NumElements/2; |
| 5181 | SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); |
| 5182 | SDOperand TypeNode = *(Node->op_end()-1); |
| 5183 | |
| 5184 | // See if we already split it. |
| 5185 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 5186 | = SplitNodes.find(Op); |
| 5187 | if (I != SplitNodes.end()) { |
| 5188 | Lo = I->second.first; |
| 5189 | Hi = I->second.second; |
| 5190 | return; |
| 5191 | } |
| 5192 | |
| 5193 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5194 | default: |
| 5195 | #ifndef NDEBUG |
| 5196 | Node->dump(); |
| 5197 | #endif |
| 5198 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5199 | case ISD::VBUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5200 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5201 | Node->op_begin()+NewNumElts); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5202 | LoOps.push_back(NewNumEltsNode); |
| 5203 | LoOps.push_back(TypeNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5204 | Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5205 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5206 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts, |
| 5207 | Node->op_end()-2); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5208 | HiOps.push_back(NewNumEltsNode); |
| 5209 | HiOps.push_back(TypeNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5210 | Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5211 | break; |
| 5212 | } |
| 5213 | case ISD::VADD: |
| 5214 | case ISD::VSUB: |
| 5215 | case ISD::VMUL: |
| 5216 | case ISD::VSDIV: |
| 5217 | case ISD::VUDIV: |
| 5218 | case ISD::VAND: |
| 5219 | case ISD::VOR: |
| 5220 | case ISD::VXOR: { |
| 5221 | SDOperand LL, LH, RL, RH; |
| 5222 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 5223 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 5224 | |
| 5225 | Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, |
| 5226 | NewNumEltsNode, TypeNode); |
| 5227 | Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, |
| 5228 | NewNumEltsNode, TypeNode); |
| 5229 | break; |
| 5230 | } |
| 5231 | case ISD::VLOAD: { |
| 5232 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 5233 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
| 5234 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 5235 | |
| 5236 | Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 5237 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; |
| 5238 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 5239 | getIntPtrConstant(IncrementSize)); |
| 5240 | // FIXME: This creates a bogus srcvalue! |
| 5241 | Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 5242 | |
| 5243 | // Build a factor node to remember that this load is independent of the |
| 5244 | // other one. |
| 5245 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5246 | Hi.getValue(1)); |
| 5247 | |
| 5248 | // Remember that we legalized the chain. |
| 5249 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5250 | break; |
| 5251 | } |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5252 | case ISD::VBIT_CONVERT: { |
| 5253 | // We know the result is a vector. The input may be either a vector or a |
| 5254 | // scalar value. |
| 5255 | if (Op.getOperand(0).getValueType() != MVT::Vector) { |
| 5256 | // Lower to a store/load. FIXME: this could be improved probably. |
| 5257 | SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType()); |
| 5258 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5259 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5260 | Op.getOperand(0), Ptr, NULL, 0); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5261 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 5262 | St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0)); |
| 5263 | SplitVectorOp(St, Lo, Hi); |
| 5264 | } else { |
| 5265 | // If the input is a vector type, we have to either scalarize it, pack it |
| 5266 | // or convert it based on whether the input vector type is legal. |
| 5267 | SDNode *InVal = Node->getOperand(0).Val; |
| 5268 | unsigned NumElems = |
| 5269 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 5270 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 5271 | |
| 5272 | // If the input is from a single element vector, scalarize the vector, |
| 5273 | // then treat like a scalar. |
| 5274 | if (NumElems == 1) { |
| 5275 | SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT); |
| 5276 | Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar, |
| 5277 | Op.getOperand(1), Op.getOperand(2)); |
| 5278 | SplitVectorOp(Scalar, Lo, Hi); |
| 5279 | } else { |
| 5280 | // Split the input vector. |
| 5281 | SplitVectorOp(Op.getOperand(0), Lo, Hi); |
| 5282 | |
| 5283 | // Convert each of the pieces now. |
| 5284 | Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo, |
| 5285 | NewNumEltsNode, TypeNode); |
| 5286 | Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi, |
| 5287 | NewNumEltsNode, TypeNode); |
| 5288 | } |
| 5289 | break; |
| 5290 | } |
| 5291 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
| 5294 | // Remember in a map if the values will be reused later. |
| 5295 | bool isNew = |
| 5296 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 5297 | assert(isNew && "Value already expanded?!?"); |
| 5298 | } |
| 5299 | |
| 5300 | |
| 5301 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 5302 | /// equivalent operation that returns a scalar (e.g. F32) or packed value |
| 5303 | /// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right |
| 5304 | /// type for the result. |
| 5305 | SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, |
| 5306 | MVT::ValueType NewVT) { |
| 5307 | assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); |
| 5308 | SDNode *Node = Op.Val; |
| 5309 | |
| 5310 | // See if we already packed it. |
| 5311 | std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); |
| 5312 | if (I != PackedNodes.end()) return I->second; |
| 5313 | |
| 5314 | SDOperand Result; |
| 5315 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5316 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5317 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 5318 | Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5319 | #endif |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5320 | assert(0 && "Unknown vector operation in PackVectorOp!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5321 | case ISD::VADD: |
| 5322 | case ISD::VSUB: |
| 5323 | case ISD::VMUL: |
| 5324 | case ISD::VSDIV: |
| 5325 | case ISD::VUDIV: |
| 5326 | case ISD::VAND: |
| 5327 | case ISD::VOR: |
| 5328 | case ISD::VXOR: |
| 5329 | Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), |
| 5330 | NewVT, |
| 5331 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5332 | PackVectorOp(Node->getOperand(1), NewVT)); |
| 5333 | break; |
| 5334 | case ISD::VLOAD: { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 5335 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 5336 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5337 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5338 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
| 5339 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5340 | |
| 5341 | // Remember that we legalized the chain. |
| 5342 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 5343 | break; |
| 5344 | } |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5345 | case ISD::VBUILD_VECTOR: |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 5346 | if (Node->getOperand(0).getValueType() == NewVT) { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5347 | // Returning a scalar? |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5348 | Result = Node->getOperand(0); |
| 5349 | } else { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5350 | // Returning a BUILD_VECTOR? |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 5351 | |
| 5352 | // If all elements of the build_vector are undefs, return an undef. |
| 5353 | bool AllUndef = true; |
| 5354 | for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i) |
| 5355 | if (Node->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 5356 | AllUndef = false; |
| 5357 | break; |
| 5358 | } |
| 5359 | if (AllUndef) { |
| 5360 | Result = DAG.getNode(ISD::UNDEF, NewVT); |
| 5361 | } else { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5362 | Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(), |
| 5363 | Node->getNumOperands()-2); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 5364 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5365 | } |
| 5366 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5367 | case ISD::VINSERT_VECTOR_ELT: |
| 5368 | if (!MVT::isVector(NewVT)) { |
| 5369 | // Returning a scalar? Must be the inserted element. |
| 5370 | Result = Node->getOperand(1); |
| 5371 | } else { |
| 5372 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, |
| 5373 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5374 | Node->getOperand(1), Node->getOperand(2)); |
| 5375 | } |
| 5376 | break; |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 5377 | case ISD::VVECTOR_SHUFFLE: |
| 5378 | if (!MVT::isVector(NewVT)) { |
| 5379 | // Returning a scalar? Figure out if it is the LHS or RHS and return it. |
| 5380 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 5381 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 5382 | Result = PackVectorOp(Node->getOperand(1), NewVT); |
| 5383 | else |
| 5384 | Result = PackVectorOp(Node->getOperand(0), NewVT); |
| 5385 | } else { |
| 5386 | // Otherwise, return a VECTOR_SHUFFLE node. First convert the index |
| 5387 | // vector from a VBUILD_VECTOR to a BUILD_VECTOR. |
| 5388 | std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(), |
| 5389 | Node->getOperand(2).Val->op_end()-2); |
| 5390 | MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5391 | SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, |
| 5392 | Node->getOperand(2).Val->op_begin(), |
| 5393 | Node->getOperand(2).Val->getNumOperands()-2); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 5394 | |
| 5395 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, |
| 5396 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5397 | PackVectorOp(Node->getOperand(1), NewVT), BV); |
| 5398 | } |
| 5399 | break; |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 5400 | case ISD::VBIT_CONVERT: |
| 5401 | if (Op.getOperand(0).getValueType() != MVT::Vector) |
| 5402 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
| 5403 | else { |
| 5404 | // If the input is a vector type, we have to either scalarize it, pack it |
| 5405 | // or convert it based on whether the input vector type is legal. |
| 5406 | SDNode *InVal = Node->getOperand(0).Val; |
| 5407 | unsigned NumElems = |
| 5408 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 5409 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 5410 | |
| 5411 | // Figure out if there is a Packed type corresponding to this Vector |
| 5412 | // type. If so, convert to the packed type. |
| 5413 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 5414 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 5415 | // Turn this into a bit convert of the packed input. |
| 5416 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 5417 | PackVectorOp(Node->getOperand(0), TVT)); |
| 5418 | break; |
| 5419 | } else if (NumElems == 1) { |
| 5420 | // Turn this into a bit convert of the scalar input. |
| 5421 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 5422 | PackVectorOp(Node->getOperand(0), EVT)); |
| 5423 | break; |
| 5424 | } else { |
| 5425 | // FIXME: UNIMP! |
| 5426 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 5427 | } |
| 5428 | } |
Evan Cheng | db3c626 | 2006-04-10 18:54:36 +0000 | [diff] [blame] | 5429 | break; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 5430 | case ISD::VSELECT: |
| 5431 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
| 5432 | PackVectorOp(Op.getOperand(1), NewVT), |
| 5433 | PackVectorOp(Op.getOperand(2), NewVT)); |
| 5434 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5435 | } |
| 5436 | |
| 5437 | if (TLI.isTypeLegal(NewVT)) |
| 5438 | Result = LegalizeOp(Result); |
| 5439 | bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; |
| 5440 | assert(isNew && "Value already packed?"); |
| 5441 | return Result; |
| 5442 | } |
| 5443 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5444 | |
| 5445 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 5446 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5447 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 5448 | if (ViewLegalizeDAGs) viewGraph(); |
| 5449 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5450 | /// run - This is the main entry point to this class. |
| 5451 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5452 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |