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" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 29 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 32 | #ifndef NDEBUG |
| 33 | static cl::opt<bool> |
| 34 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 35 | cl::desc("Pop up a window to show dags before legalize")); |
| 36 | #else |
| 37 | static const bool ViewLegalizeDAGs = 0; |
| 38 | #endif |
| 39 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 42 | /// hacks on it until the target machine can handle it. This involves |
| 43 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 44 | /// large sizes or splitting up large values into small values) as well as |
| 45 | /// eliminating operations the machine cannot handle. |
| 46 | /// |
| 47 | /// This code also does a small amount of optimization and recognition of idioms |
| 48 | /// as part of its processing. For example, if a target does not support a |
| 49 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 50 | /// will attempt merge setcc and brc instructions into brcc's. |
| 51 | /// |
| 52 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 53 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 54 | TargetLowering &TLI; |
| 55 | SelectionDAG &DAG; |
| 56 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 57 | // Libcall insertion helpers. |
| 58 | |
| 59 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 60 | /// legalized. We use this to ensure that calls are properly serialized |
| 61 | /// against each other, including inserted libcalls. |
| 62 | SDOperand LastCALLSEQ_END; |
| 63 | |
| 64 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 65 | /// helpful assertions that a libcall isn't created while another call is |
| 66 | /// being legalized (which could lead to non-serialized call sequences). |
| 67 | bool IsLegalizingCall; |
| 68 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 69 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 70 | Legal, // The target natively supports this operation. |
| 71 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 72 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 73 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 75 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 76 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 77 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 78 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 80 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 81 | /// than one use, this map indicates what regularized operand to use. This |
| 82 | /// allows us to avoid legalizing the same thing more than once. |
| 83 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 84 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 85 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 86 | /// than one use, this map indicates what promoted value to use. This allows |
| 87 | /// us to avoid promoting the same thing more than once. |
| 88 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 89 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 90 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 91 | /// which which operands are the expanded version of the input. This allows |
| 92 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 93 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 94 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 95 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 96 | /// which which operands are the split version of the input. This allows us |
| 97 | /// to avoid splitting the same node more than once. |
| 98 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 99 | |
| 100 | /// PackedNodes - For nodes that need to be packed from MVT::Vector types to |
| 101 | /// concrete packed types, this contains the mapping of ones we have already |
| 102 | /// processed to the result. |
| 103 | std::map<SDOperand, SDOperand> PackedNodes; |
| 104 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 105 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 106 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 107 | // If someone requests legalization of the new node, return itself. |
| 108 | if (From != To) |
| 109 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 111 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 112 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 113 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 114 | // If someone requests legalization of the new node, return itself. |
| 115 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 118 | public: |
| 119 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 120 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 122 | /// getTypeAction - Return how we should legalize values of this type, either |
| 123 | /// it is already legal or we need to expand it into multiple registers of |
| 124 | /// smaller integer type, or we need to promote it to a larger type. |
| 125 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 126 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /// isTypeLegal - Return true if this type is legal on this target. |
| 130 | /// |
| 131 | bool isTypeLegal(MVT::ValueType VT) const { |
| 132 | return getTypeAction(VT) == Legal; |
| 133 | } |
| 134 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 135 | void LegalizeDAG(); |
| 136 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 137 | private: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 138 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 139 | /// appropriate for its type. |
| 140 | void HandleOp(SDOperand Op); |
| 141 | |
| 142 | /// LegalizeOp - We know that the specified value has a legal type. |
| 143 | /// Recursively ensure that the operands have legal types, then return the |
| 144 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 145 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 146 | |
| 147 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 148 | /// promote it to compute the value into a larger type. The produced value |
| 149 | /// will have the correct bits for the low portion of the register, but no |
| 150 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 151 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 152 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 153 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 154 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 155 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 156 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 157 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 158 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 159 | /// types. |
| 160 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 161 | |
| 162 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 163 | /// two smaller values of MVT::Vector type. |
| 164 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 165 | |
| 166 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 167 | /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When |
| 168 | /// this is called, we know that PackedVT is the right type for the result and |
| 169 | /// we know that this type is legal for the target. |
| 170 | SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT); |
| 171 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 172 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 173 | /// specified mask and type. Targets can specify exactly which masks they |
| 174 | /// support and the code generator is tasked with not creating illegal masks. |
| 175 | /// |
| 176 | /// Note that this will also return true for shuffles that are promoted to a |
| 177 | /// different type. |
| 178 | /// |
| 179 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 180 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 181 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 182 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 183 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
| 184 | std::set<SDNode*> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 185 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 186 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 187 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 188 | SDOperand CreateStackTemporary(MVT::ValueType VT); |
| 189 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 190 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 191 | SDOperand &Hi); |
| 192 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 193 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 195 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 196 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 197 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 198 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 199 | SDOperand LegalOp, |
| 200 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 201 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 202 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 203 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 204 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 206 | SDOperand ExpandBSWAP(SDOperand Op); |
| 207 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 208 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 209 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 210 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 211 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 213 | SDOperand LowerVEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 214 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 216 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 217 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 218 | } |
| 219 | }; |
| 220 | } |
| 221 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 222 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 223 | /// specified mask and type. Targets can specify exactly which masks they |
| 224 | /// support and the code generator is tasked with not creating illegal masks. |
| 225 | /// |
| 226 | /// Note that this will also return true for shuffles that are promoted to a |
| 227 | /// different type. |
| 228 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 229 | SDOperand Mask) const { |
| 230 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 231 | default: return 0; |
| 232 | case TargetLowering::Legal: |
| 233 | case TargetLowering::Custom: |
| 234 | break; |
| 235 | case TargetLowering::Promote: { |
| 236 | // If this is promoted to a different type, convert the shuffle mask and |
| 237 | // ask if it is legal in the promoted type! |
| 238 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 239 | |
| 240 | // If we changed # elements, change the shuffle mask. |
| 241 | unsigned NumEltsGrowth = |
| 242 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 243 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 244 | if (NumEltsGrowth > 1) { |
| 245 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 246 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 247 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 248 | SDOperand InOp = Mask.getOperand(i); |
| 249 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 250 | if (InOp.getOpcode() == ISD::UNDEF) |
| 251 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 252 | else { |
| 253 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 254 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 255 | } |
| 256 | } |
| 257 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 258 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 259 | } |
| 260 | VT = NVT; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 265 | } |
| 266 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 267 | /// getScalarizedOpcode - Return the scalar opcode that corresponds to the |
| 268 | /// specified vector opcode. |
Chris Lattner | b89175f | 2005-11-19 05:51:46 +0000 | [diff] [blame] | 269 | static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 270 | switch (VecOp) { |
| 271 | default: assert(0 && "Don't know how to scalarize this opcode!"); |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 272 | case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; |
| 273 | case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; |
| 274 | case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; |
| 275 | case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV; |
| 276 | case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV; |
| 277 | case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0; |
| 278 | case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0; |
| 279 | case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 283 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 284 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 285 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 286 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 287 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 290 | /// ComputeTopDownOrdering - Add the specified node to the Order list if it has |
| 291 | /// not been visited yet and if all of its operands have already been visited. |
| 292 | static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, |
| 293 | std::map<SDNode*, unsigned> &Visited) { |
| 294 | if (++Visited[N] != N->getNumOperands()) |
| 295 | return; // Haven't visited all operands yet |
| 296 | |
| 297 | Order.push_back(N); |
| 298 | |
| 299 | if (N->hasOneUse()) { // Tail recurse in common case. |
| 300 | ComputeTopDownOrdering(*N->use_begin(), Order, Visited); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // Now that we have N in, add anything that uses it if all of their operands |
| 305 | // are now done. |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 306 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) |
| 307 | ComputeTopDownOrdering(*UI, Order, Visited); |
| 308 | } |
| 309 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 310 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 311 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 312 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 313 | IsLegalizingCall = false; |
| 314 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 315 | // The legalize process is inherently a bottom-up recursive process (users |
| 316 | // legalize their uses before themselves). Given infinite stack space, we |
| 317 | // could just start legalizing on the root and traverse the whole graph. In |
| 318 | // 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] | 319 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 320 | // node is only legalized after all of its operands are legalized. |
| 321 | std::map<SDNode*, unsigned> Visited; |
| 322 | std::vector<SDNode*> Order; |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 324 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 325 | // entry node) that have no operands. |
| 326 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 327 | E = DAG.allnodes_end(); I != E; ++I) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 328 | if (I->getNumOperands() == 0) { |
| 329 | Visited[I] = 0 - 1U; |
| 330 | ComputeTopDownOrdering(I, Order, Visited); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 334 | assert(Order.size() == Visited.size() && |
| 335 | Order.size() == |
| 336 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 337 | "Error: DAG is cyclic!"); |
| 338 | Visited.clear(); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 339 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 340 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 341 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 342 | |
| 343 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 344 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 345 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 346 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 347 | |
| 348 | ExpandedNodes.clear(); |
| 349 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 350 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 351 | SplitNodes.clear(); |
| 352 | PackedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 353 | |
| 354 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 355 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 358 | |
| 359 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 360 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 361 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 362 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 363 | return Node; |
| 364 | if (Node->use_empty()) |
| 365 | return 0; // No CallSeqEnd |
| 366 | |
| 367 | // The chain is usually at the end. |
| 368 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 369 | if (TheChain.getValueType() != MVT::Other) { |
| 370 | // Sometimes it's at the beginning. |
| 371 | TheChain = SDOperand(Node, 0); |
| 372 | if (TheChain.getValueType() != MVT::Other) { |
| 373 | // Otherwise, hunt for it. |
| 374 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 375 | if (Node->getValueType(i) == MVT::Other) { |
| 376 | TheChain = SDOperand(Node, i); |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | // Otherwise, we walked into a node without a chain. |
| 381 | if (TheChain.getValueType() != MVT::Other) |
| 382 | return 0; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 387 | E = Node->use_end(); UI != E; ++UI) { |
| 388 | |
| 389 | // Make sure to only follow users of our token chain. |
| 390 | SDNode *User = *UI; |
| 391 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 392 | if (User->getOperand(i) == TheChain) |
| 393 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 394 | return Result; |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 400 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 401 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 402 | assert(Node && "Didn't find callseq_start for a call??"); |
| 403 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 404 | |
| 405 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 406 | "Node doesn't have a token chain argument!"); |
| 407 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 408 | } |
| 409 | |
| 410 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 411 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 412 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 413 | /// |
| 414 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 415 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 416 | /// |
| 417 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
| 418 | std::set<SDNode*> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 419 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 420 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 421 | // If we've already processed this node and it does lead to Dest, there is no |
| 422 | // need to reprocess it. |
| 423 | if (NodesLeadingTo.count(N)) return true; |
| 424 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 425 | // If the first result of this node has been already legalized, then it cannot |
| 426 | // reach N. |
| 427 | switch (getTypeAction(N->getValueType(0))) { |
| 428 | case Legal: |
| 429 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 430 | break; |
| 431 | case Promote: |
| 432 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 433 | break; |
| 434 | case Expand: |
| 435 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | // Okay, this node has not already been legalized. Check and legalize all |
| 440 | // operands. If none lead to Dest, then we can legalize this node. |
| 441 | bool OperandsLeadToDest = false; |
| 442 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 443 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 444 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 445 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 446 | if (OperandsLeadToDest) { |
| 447 | NodesLeadingTo.insert(N); |
| 448 | return true; |
| 449 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 450 | |
| 451 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 452 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 453 | return false; |
| 454 | } |
| 455 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 456 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 457 | /// appropriate for its type. |
| 458 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
| 459 | switch (getTypeAction(Op.getValueType())) { |
| 460 | default: assert(0 && "Bad type action!"); |
| 461 | case Legal: LegalizeOp(Op); break; |
| 462 | case Promote: PromoteOp(Op); break; |
| 463 | case Expand: |
| 464 | if (Op.getValueType() != MVT::Vector) { |
| 465 | SDOperand X, Y; |
| 466 | ExpandOp(Op, X, Y); |
| 467 | } else { |
| 468 | SDNode *N = Op.Val; |
| 469 | unsigned NumOps = N->getNumOperands(); |
| 470 | unsigned NumElements = |
| 471 | cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue(); |
| 472 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT(); |
| 473 | MVT::ValueType PackedVT = getVectorType(EVT, NumElements); |
| 474 | if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) { |
| 475 | // In the common case, this is a legal vector type, convert it to the |
| 476 | // packed operation and type now. |
| 477 | PackVectorOp(Op, PackedVT); |
| 478 | } else if (NumElements == 1) { |
| 479 | // Otherwise, if this is a single element vector, convert it to a |
| 480 | // scalar operation. |
| 481 | PackVectorOp(Op, EVT); |
| 482 | } else { |
| 483 | // Otherwise, this is a multiple element vector that isn't supported. |
| 484 | // Split it in half and legalize both parts. |
| 485 | SDOperand X, Y; |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 486 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | break; |
| 490 | } |
| 491 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 492 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 493 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 494 | /// a load from the constant pool. |
| 495 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 496 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 497 | bool Extend = false; |
| 498 | |
| 499 | // If a FP immediate is precise when represented as a float and if the |
| 500 | // target can do an extending load from float to double, we put it into |
| 501 | // the constant pool as a float, even if it's is statically typed as a |
| 502 | // double. |
| 503 | MVT::ValueType VT = CFP->getValueType(0); |
| 504 | bool isDouble = VT == MVT::f64; |
| 505 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 506 | Type::FloatTy, CFP->getValue()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 507 | if (!UseCP) { |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 508 | double Val = LLVMC->getValue(); |
| 509 | return isDouble |
| 510 | ? DAG.getConstant(DoubleToBits(Val), MVT::i64) |
| 511 | : DAG.getConstant(FloatToBits(Val), MVT::i32); |
| 512 | } |
| 513 | |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 514 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 515 | // Only do this if the target has a native EXTLOAD instruction from f32. |
| 516 | TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) { |
| 517 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy)); |
| 518 | VT = MVT::f32; |
| 519 | Extend = true; |
| 520 | } |
| 521 | |
| 522 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
| 523 | if (Extend) { |
| 524 | return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 525 | CPIdx, NULL, 0, MVT::f32); |
| 526 | } else { |
| 527 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
| 528 | } |
| 529 | } |
| 530 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 531 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 532 | /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise |
| 533 | /// operations. |
| 534 | static |
| 535 | SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT, |
| 536 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 537 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 538 | MVT::ValueType SrcVT = Node->getOperand(1).getValueType(); |
| 539 | MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 540 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 541 | // First get the sign bit of second operand. |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 542 | SDOperand Mask1 = (SrcVT == MVT::f64) |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 543 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT) |
| 544 | : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 545 | Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 546 | SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 547 | SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 548 | // Shift right or sign-extend it if the two operands have different types. |
| 549 | int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT); |
| 550 | if (SizeDiff > 0) { |
| 551 | SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit, |
| 552 | DAG.getConstant(SizeDiff, TLI.getShiftAmountTy())); |
| 553 | SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit); |
| 554 | } else if (SizeDiff < 0) |
| 555 | SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 556 | |
| 557 | // Clear the sign bit of first operand. |
| 558 | SDOperand Mask2 = (VT == MVT::f64) |
| 559 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 560 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 561 | Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 562 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 563 | Result = DAG.getNode(ISD::AND, NVT, Result, Mask2); |
| 564 | |
| 565 | // Or the value with the sign bit. |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 566 | Result = DAG.getNode(ISD::OR, NVT, Result, SignBit); |
| 567 | return Result; |
| 568 | } |
| 569 | |
| 570 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 571 | /// LegalizeOp - We know that the specified value has a legal type. |
| 572 | /// Recursively ensure that the operands have legal types, then return the |
| 573 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 574 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 575 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 576 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 577 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 579 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 580 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 581 | if (Node->getNumValues() > 1) { |
| 582 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 583 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 584 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 585 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 586 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 587 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 591 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 592 | // means that we always must cache transformed nodes. |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 593 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 594 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 595 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 596 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 597 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 598 | bool isCustom = false; |
| 599 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 600 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 601 | case ISD::FrameIndex: |
| 602 | case ISD::EntryToken: |
| 603 | case ISD::Register: |
| 604 | case ISD::BasicBlock: |
| 605 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 606 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 607 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 608 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 609 | case ISD::TargetConstantPool: |
| 610 | case ISD::TargetGlobalAddress: |
| 611 | case ISD::TargetExternalSymbol: |
| 612 | case ISD::VALUETYPE: |
| 613 | case ISD::SRCVALUE: |
| 614 | case ISD::STRING: |
| 615 | case ISD::CONDCODE: |
Andrew Lenharth | 82c3d8f | 2006-10-11 04:29:42 +0000 | [diff] [blame] | 616 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 617 | // Primitives must all be legal. |
| 618 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 619 | "This must be legal!"); |
| 620 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 621 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 622 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 623 | // If this is a target node, legalize it by legalizing the operands then |
| 624 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 625 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 626 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 627 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 628 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 629 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 630 | |
| 631 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 632 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 633 | return Result.getValue(Op.ResNo); |
| 634 | } |
| 635 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 636 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 637 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 638 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 639 | assert(0 && "Do not know how to legalize this operator!"); |
| 640 | abort(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 641 | case ISD::GlobalAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 642 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 643 | case ISD::ConstantPool: |
| 644 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 645 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 646 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 647 | case TargetLowering::Custom: |
| 648 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 649 | if (Tmp1.Val) Result = Tmp1; |
| 650 | // 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] | 651 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 652 | break; |
| 653 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 654 | break; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 655 | case ISD::FRAMEADDR: |
| 656 | case ISD::RETURNADDR: |
| 657 | // The only option for these nodes is to custom lower them. If the target |
| 658 | // does not custom lower them, then return zero. |
| 659 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 660 | if (Tmp1.Val) |
| 661 | Result = Tmp1; |
| 662 | else |
| 663 | Result = DAG.getConstant(0, TLI.getPointerTy()); |
| 664 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 665 | case ISD::AssertSext: |
| 666 | case ISD::AssertZext: |
| 667 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 668 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 669 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 670 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 671 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 672 | Result = Node->getOperand(Op.ResNo); |
| 673 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 674 | case ISD::CopyFromReg: |
| 675 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 676 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 677 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 678 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 679 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 680 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 681 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 682 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 683 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 684 | } else { |
| 685 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 686 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 687 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 688 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 689 | // Since CopyFromReg produces two values, make sure to remember that we |
| 690 | // legalized both of them. |
| 691 | AddLegalizedOperand(Op.getValue(0), Result); |
| 692 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 693 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 694 | case ISD::UNDEF: { |
| 695 | MVT::ValueType VT = Op.getValueType(); |
| 696 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 697 | default: assert(0 && "This action is not supported yet!"); |
| 698 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 699 | if (MVT::isInteger(VT)) |
| 700 | Result = DAG.getConstant(0, VT); |
| 701 | else if (MVT::isFloatingPoint(VT)) |
| 702 | Result = DAG.getConstantFP(0, VT); |
| 703 | else |
| 704 | assert(0 && "Unknown value type!"); |
| 705 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 706 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 707 | break; |
| 708 | } |
| 709 | break; |
| 710 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 712 | case ISD::INTRINSIC_W_CHAIN: |
| 713 | case ISD::INTRINSIC_WO_CHAIN: |
| 714 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 715 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 716 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 717 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 718 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 719 | |
| 720 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 721 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 722 | TargetLowering::Custom) { |
| 723 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 724 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (Result.Val->getNumValues() == 1) break; |
| 728 | |
| 729 | // Must have return value and chain result. |
| 730 | assert(Result.Val->getNumValues() == 2 && |
| 731 | "Cannot return more than two values!"); |
| 732 | |
| 733 | // Since loads produce two values, make sure to remember that we |
| 734 | // legalized both of them. |
| 735 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 736 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 737 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 739 | |
| 740 | case ISD::LOCATION: |
| 741 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 742 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 743 | |
| 744 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 745 | case TargetLowering::Promote: |
| 746 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 747 | case TargetLowering::Expand: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 748 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 749 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 750 | bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 751 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 752 | if (MMI && (useDEBUG_LOC || useLABEL)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 753 | const std::string &FName = |
| 754 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 755 | const std::string &DirName = |
| 756 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 757 | unsigned SrcFile = MMI->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 758 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 759 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 760 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 761 | SDOperand LineOp = Node->getOperand(1); |
| 762 | SDOperand ColOp = Node->getOperand(2); |
| 763 | |
| 764 | if (useDEBUG_LOC) { |
| 765 | Ops.push_back(LineOp); // line # |
| 766 | Ops.push_back(ColOp); // col # |
| 767 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 768 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 769 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 770 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 771 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 772 | unsigned ID = MMI->RecordLabel(Line, Col, SrcFile); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 773 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 774 | Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 775 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 776 | } else { |
| 777 | Result = Tmp1; // chain |
| 778 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 779 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 780 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 781 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 782 | if (Tmp1 != Node->getOperand(0) || |
| 783 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 784 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 785 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 786 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 787 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 788 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 789 | } else { |
| 790 | // Otherwise promote them. |
| 791 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 792 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 793 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 794 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 795 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 796 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 797 | } |
| 798 | break; |
| 799 | } |
| 800 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 801 | |
| 802 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 803 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 804 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 805 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 806 | case TargetLowering::Legal: |
| 807 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 808 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 809 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 810 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 811 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | break; |
| 815 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 816 | case ISD::LABEL: |
| 817 | assert(Node->getNumOperands() == 2 && "Invalid LABEL node!"); |
| 818 | switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 819 | default: assert(0 && "This action is not supported yet!"); |
| 820 | case TargetLowering::Legal: |
| 821 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 822 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 823 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 824 | break; |
| 825 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 826 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 828 | case ISD::Constant: |
| 829 | // We know we don't need to expand constants here, constants only have one |
| 830 | // value and we check that it is fine above. |
| 831 | |
| 832 | // FIXME: Maybe we should handle things like targets that don't support full |
| 833 | // 32-bit immediates? |
| 834 | break; |
| 835 | case ISD::ConstantFP: { |
| 836 | // Spill FP immediates to the constant pool if the target cannot directly |
| 837 | // codegen them. Targets often have some immediate values that can be |
| 838 | // efficiently generated into an FP register without a load. We explicitly |
| 839 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 840 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 841 | |
| 842 | // Check to see if this FP immediate is already legal. |
| 843 | bool isLegal = false; |
| 844 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 845 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 846 | if (CFP->isExactlyValue(*I)) { |
| 847 | isLegal = true; |
| 848 | break; |
| 849 | } |
| 850 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 851 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 852 | if (isLegal) { |
| 853 | Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); |
| 854 | break; |
| 855 | } |
| 856 | |
| 857 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 858 | default: assert(0 && "This action is not supported yet!"); |
| 859 | case TargetLowering::Custom: |
| 860 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 861 | if (Tmp3.Val) { |
| 862 | Result = Tmp3; |
| 863 | break; |
| 864 | } |
| 865 | // FALLTHROUGH |
| 866 | case TargetLowering::Expand: |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 867 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 868 | } |
| 869 | break; |
| 870 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 871 | case ISD::TokenFactor: |
| 872 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 873 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 874 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 875 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 876 | } else if (Node->getNumOperands() == 3) { |
| 877 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 878 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 879 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 880 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 881 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 882 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 883 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 884 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 885 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 886 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 888 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 889 | |
| 890 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 891 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 892 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 893 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 894 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
| 895 | assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() && |
| 896 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 897 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 898 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 899 | // 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] | 900 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
| 901 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 902 | if (Op.ResNo == i) |
| 903 | Tmp2 = Tmp1; |
| 904 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 905 | } |
| 906 | return Tmp2; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 907 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 908 | case ISD::BUILD_VECTOR: |
| 909 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 910 | default: assert(0 && "This action is not supported yet!"); |
| 911 | case TargetLowering::Custom: |
| 912 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 913 | if (Tmp3.Val) { |
| 914 | Result = Tmp3; |
| 915 | break; |
| 916 | } |
| 917 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 918 | case TargetLowering::Expand: |
| 919 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 920 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 921 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 922 | break; |
| 923 | case ISD::INSERT_VECTOR_ELT: |
| 924 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 925 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 926 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 927 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 928 | |
| 929 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 930 | Node->getValueType(0))) { |
| 931 | default: assert(0 && "This action is not supported yet!"); |
| 932 | case TargetLowering::Legal: |
| 933 | break; |
| 934 | case TargetLowering::Custom: |
| 935 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 936 | if (Tmp3.Val) { |
| 937 | Result = Tmp3; |
| 938 | break; |
| 939 | } |
| 940 | // FALLTHROUGH |
| 941 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 942 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 943 | // then a shuffle that inserts it into the right position in the vector. |
| 944 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 945 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 946 | Tmp1.getValueType(), Tmp2); |
| 947 | |
| 948 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 949 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
| 950 | MVT::ValueType ShufMaskEltVT = MVT::getVectorBaseType(ShufMaskVT); |
| 951 | |
| 952 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 953 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 954 | // the RHS. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 955 | SmallVector<SDOperand, 8> ShufOps; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 956 | for (unsigned i = 0; i != NumElts; ++i) { |
| 957 | if (i != InsertPos->getValue()) |
| 958 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 959 | else |
| 960 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 961 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 962 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 963 | &ShufOps[0], ShufOps.size()); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 964 | |
| 965 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 966 | Tmp1, ScVec, ShufMask); |
| 967 | Result = LegalizeOp(Result); |
| 968 | break; |
| 969 | } |
| 970 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 971 | // If the target doesn't support this, we have to spill the input vector |
| 972 | // to a temporary stack slot, update the element, then reload it. This is |
| 973 | // badness. We could also load the value into a vector register (either |
| 974 | // with a "move to register" or "extload into register" instruction, then |
| 975 | // permute it into place, if the idx is a constant and if the idx is |
| 976 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 977 | MVT::ValueType VT = Tmp1.getValueType(); |
| 978 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 979 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 980 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 981 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 982 | // Store the vector. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 983 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 984 | |
| 985 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 986 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 987 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 988 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 989 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 990 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 991 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 992 | // Store the scalar value. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 993 | Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 994 | // Load the updated vector. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 995 | Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | } |
| 999 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1000 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1001 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 1002 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 1003 | break; |
| 1004 | } |
| 1005 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1006 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 1007 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1008 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 1009 | Node->getValueType(0))) { |
| 1010 | default: assert(0 && "This action is not supported yet!"); |
| 1011 | case TargetLowering::Legal: |
| 1012 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 1013 | case TargetLowering::Custom: |
| 1014 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1015 | if (Tmp3.Val) { |
| 1016 | Result = Tmp3; |
| 1017 | break; |
| 1018 | } |
| 1019 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1020 | case TargetLowering::Expand: |
| 1021 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1022 | break; |
| 1023 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1024 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1025 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1026 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1027 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 1028 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1029 | |
| 1030 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1031 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 1032 | default: assert(0 && "Unknown operation action!"); |
| 1033 | case TargetLowering::Legal: |
| 1034 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 1035 | "vector shuffle should not be created if not legal!"); |
| 1036 | break; |
| 1037 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1038 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1039 | if (Tmp3.Val) { |
| 1040 | Result = Tmp3; |
| 1041 | break; |
| 1042 | } |
| 1043 | // FALLTHROUGH |
| 1044 | case TargetLowering::Expand: { |
| 1045 | MVT::ValueType VT = Node->getValueType(0); |
| 1046 | MVT::ValueType EltVT = MVT::getVectorBaseType(VT); |
| 1047 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1048 | SDOperand Mask = Node->getOperand(2); |
| 1049 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1050 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1051 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1052 | SDOperand Arg = Mask.getOperand(i); |
| 1053 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1054 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1055 | } else { |
| 1056 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1057 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1058 | if (Idx < NumElems) |
| 1059 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1060 | DAG.getConstant(Idx, PtrVT))); |
| 1061 | else |
| 1062 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1063 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1064 | } |
| 1065 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1066 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1067 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1068 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1069 | case TargetLowering::Promote: { |
| 1070 | // Change base type to a different vector type. |
| 1071 | MVT::ValueType OVT = Node->getValueType(0); |
| 1072 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1073 | |
| 1074 | // Cast the two input vectors. |
| 1075 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1076 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1077 | |
| 1078 | // Convert the shuffle mask to the right # elements. |
| 1079 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1080 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1081 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1082 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1083 | break; |
| 1084 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1085 | } |
| 1086 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1087 | |
| 1088 | case ISD::EXTRACT_VECTOR_ELT: |
| 1089 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1090 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1091 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1092 | |
| 1093 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, |
| 1094 | Tmp1.getValueType())) { |
| 1095 | default: assert(0 && "This action is not supported yet!"); |
| 1096 | case TargetLowering::Legal: |
| 1097 | break; |
| 1098 | case TargetLowering::Custom: |
| 1099 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1100 | if (Tmp3.Val) { |
| 1101 | Result = Tmp3; |
| 1102 | break; |
| 1103 | } |
| 1104 | // FALLTHROUGH |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 1105 | case TargetLowering::Expand: |
| 1106 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | e35c218 | 2006-03-21 21:02:03 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 1111 | case ISD::VEXTRACT_VECTOR_ELT: |
| 1112 | Result = LegalizeOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1113 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1115 | case ISD::CALLSEQ_START: { |
| 1116 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1117 | |
| 1118 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1119 | // to this call start. This ensures that any libcalls that need be inserted |
| 1120 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1121 | {std::set<SDNode*> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1122 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1123 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1124 | NodesLeadingTo); |
| 1125 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Now that we legalized all of the inputs (which may have inserted |
| 1128 | // libcalls) create the new CALLSEQ_START node. |
| 1129 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1130 | |
| 1131 | // Merge in the last call, to ensure that this call start after the last |
| 1132 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1133 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1134 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1135 | Tmp1 = LegalizeOp(Tmp1); |
| 1136 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Do not try to legalize the target-specific arguments (#1+). |
| 1139 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1140 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1141 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1142 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1146 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1147 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1148 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1149 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1150 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1151 | // sequence have been legalized, legalize the call itself. During this |
| 1152 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1153 | // can overlap. |
| 1154 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1155 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1156 | // Note that we are selecting this call! |
| 1157 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1158 | IsLegalizingCall = true; |
| 1159 | |
| 1160 | // Legalize the call, starting from the CALLSEQ_END. |
| 1161 | LegalizeOp(LastCALLSEQ_END); |
| 1162 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1163 | return Result; |
| 1164 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1165 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1166 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1167 | // will cause this node to be legalized as well as handling libcalls right. |
| 1168 | if (LastCALLSEQ_END.Val != Node) { |
| 1169 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
| 1170 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 1171 | assert(I != LegalizedNodes.end() && |
| 1172 | "Legalizing the call start should have legalized this node!"); |
| 1173 | return I->second; |
| 1174 | } |
| 1175 | |
| 1176 | // Otherwise, the call start has been legalized and everything is going |
| 1177 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1178 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1179 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1180 | // an optional flag input. |
| 1181 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1182 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1183 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1184 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1185 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } else { |
| 1188 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1189 | if (Tmp1 != Node->getOperand(0) || |
| 1190 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1191 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1192 | Ops[0] = Tmp1; |
| 1193 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1194 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1195 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1196 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1197 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1198 | // This finishes up call legalization. |
| 1199 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1200 | |
| 1201 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1202 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1203 | if (Node->getNumValues() == 2) |
| 1204 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1205 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1206 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1207 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1208 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1209 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1210 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1212 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1213 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1214 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1215 | Node->getValueType(0))) { |
| 1216 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1217 | case TargetLowering::Expand: { |
| 1218 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1219 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1220 | " not tell us which reg is the stack pointer!"); |
| 1221 | SDOperand Chain = Tmp1.getOperand(0); |
| 1222 | SDOperand Size = Tmp2.getOperand(1); |
| 1223 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 1224 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 1225 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1226 | Tmp1 = LegalizeOp(Tmp1); |
| 1227 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1231 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1232 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1233 | Tmp1 = LegalizeOp(Tmp3); |
| 1234 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1236 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1237 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1238 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1239 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1240 | // Since this op produce two values, make sure to remember that we |
| 1241 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1242 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1243 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1244 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1245 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1246 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1247 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1248 | bool Changed = false; |
| 1249 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1250 | // that need to be expanded or something. Note we skip the asm string and |
| 1251 | // all of the TargetConstant flags. |
| 1252 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1253 | Changed = Op != Ops[0]; |
| 1254 | Ops[0] = Op; |
| 1255 | |
| 1256 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1257 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1258 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1259 | for (++i; NumVals; ++i, --NumVals) { |
| 1260 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1261 | if (Op != Ops[i]) { |
| 1262 | Changed = true; |
| 1263 | Ops[i] = Op; |
| 1264 | } |
| 1265 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1266 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1267 | |
| 1268 | if (HasInFlag) { |
| 1269 | Op = LegalizeOp(Ops.back()); |
| 1270 | Changed |= Op != Ops.back(); |
| 1271 | Ops.back() = Op; |
| 1272 | } |
| 1273 | |
| 1274 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1275 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1276 | |
| 1277 | // 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] | 1278 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1279 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1280 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1281 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1282 | case ISD::BR: |
| 1283 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1284 | // Ensure that libcalls are emitted before a branch. |
| 1285 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1286 | Tmp1 = LegalizeOp(Tmp1); |
| 1287 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1288 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1289 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1290 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1291 | case ISD::BRIND: |
| 1292 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1293 | // Ensure that libcalls are emitted before a branch. |
| 1294 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1295 | Tmp1 = LegalizeOp(Tmp1); |
| 1296 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1297 | |
| 1298 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1299 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1300 | case Legal: |
| 1301 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1302 | break; |
| 1303 | } |
| 1304 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1305 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1306 | case ISD::BR_JT: |
| 1307 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1308 | // Ensure that libcalls are emitted before a branch. |
| 1309 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1310 | Tmp1 = LegalizeOp(Tmp1); |
| 1311 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1312 | |
| 1313 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1314 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1315 | |
| 1316 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1317 | default: assert(0 && "This action is not supported yet!"); |
| 1318 | case TargetLowering::Legal: break; |
| 1319 | case TargetLowering::Custom: |
| 1320 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1321 | if (Tmp1.Val) Result = Tmp1; |
| 1322 | break; |
| 1323 | case TargetLowering::Expand: { |
| 1324 | SDOperand Chain = Result.getOperand(0); |
| 1325 | SDOperand Table = Result.getOperand(1); |
| 1326 | SDOperand Index = Result.getOperand(2); |
| 1327 | |
| 1328 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1329 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1330 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1331 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1332 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1333 | |
| 1334 | SDOperand LD; |
| 1335 | switch (EntrySize) { |
| 1336 | default: assert(0 && "Size of jump table not supported yet."); break; |
| 1337 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break; |
| 1338 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break; |
| 1339 | } |
| 1340 | |
| 1341 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1342 | // For PIC, the sequence is: |
| 1343 | // BRIND(load(Jumptable + index) + RelocBase) |
| 1344 | // RelocBase is the JumpTable on PPC and X86, GOT on Alpha |
| 1345 | SDOperand Reloc; |
| 1346 | if (TLI.usesGlobalOffsetTable()) |
| 1347 | Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy); |
| 1348 | else |
| 1349 | Reloc = Table; |
Evan Cheng | d063189 | 2006-10-31 02:31:00 +0000 | [diff] [blame] | 1350 | Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1351 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc); |
| 1352 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
| 1353 | } else { |
| 1354 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD); |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1359 | case ISD::BRCOND: |
| 1360 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1361 | // Ensure that libcalls are emitted before a return. |
| 1362 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1363 | Tmp1 = LegalizeOp(Tmp1); |
| 1364 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1365 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1366 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1367 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1368 | case Legal: |
| 1369 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1370 | break; |
| 1371 | case Promote: |
| 1372 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1373 | |
| 1374 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1375 | // that the value is properly zero extended. |
| 1376 | if (!TLI.MaskedValueIsZero(Tmp2, |
| 1377 | MVT::getIntVTBitMask(Tmp2.getValueType())^1)) |
| 1378 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1383 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1384 | |
| 1385 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1386 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1387 | case TargetLowering::Legal: break; |
| 1388 | case TargetLowering::Custom: |
| 1389 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1390 | if (Tmp1.Val) Result = Tmp1; |
| 1391 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1392 | case TargetLowering::Expand: |
| 1393 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1394 | // Node. |
| 1395 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1396 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1397 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1398 | Node->getOperand(2)); |
| 1399 | } else { |
| 1400 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1401 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1402 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1403 | Node->getOperand(2)); |
| 1404 | } |
| 1405 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1406 | } |
| 1407 | break; |
| 1408 | case ISD::BR_CC: |
| 1409 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1410 | // Ensure that libcalls are emitted before a branch. |
| 1411 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1412 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1413 | Tmp2 = Node->getOperand(2); // LHS |
| 1414 | Tmp3 = Node->getOperand(3); // RHS |
| 1415 | Tmp4 = Node->getOperand(1); // CC |
| 1416 | |
| 1417 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1418 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1419 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1420 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1421 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1422 | // the result against zero to select between true and false values. |
| 1423 | if (Tmp3.Val == 0) { |
| 1424 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1425 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1426 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1427 | |
| 1428 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1429 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1430 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1431 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1432 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1433 | case TargetLowering::Legal: break; |
| 1434 | case TargetLowering::Custom: |
| 1435 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1436 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1437 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1438 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1439 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1440 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1441 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1442 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1443 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1444 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1445 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1446 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1447 | MVT::ValueType VT = Node->getValueType(0); |
| 1448 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1449 | Tmp3 = Result.getValue(0); |
| 1450 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1451 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1452 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1453 | default: assert(0 && "This action is not supported yet!"); |
| 1454 | case TargetLowering::Legal: break; |
| 1455 | case TargetLowering::Custom: |
| 1456 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1457 | if (Tmp1.Val) { |
| 1458 | Tmp3 = LegalizeOp(Tmp1); |
| 1459 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1460 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | case TargetLowering::Promote: { |
| 1463 | // Only promote a load of vector type to another. |
| 1464 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1465 | // Change base type to a different vector type. |
| 1466 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1467 | |
| 1468 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1469 | LD->getSrcValueOffset()); |
| 1470 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1471 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1472 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1473 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | // Since loads produce two values, make sure to remember that we |
| 1476 | // legalized both of them. |
| 1477 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1478 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1479 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1480 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 1481 | MVT::ValueType SrcVT = LD->getLoadedVT(); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1482 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 1483 | default: assert(0 && "This action is not supported yet!"); |
| 1484 | case TargetLowering::Promote: |
| 1485 | assert(SrcVT == MVT::i1 && |
| 1486 | "Can only promote extending LOAD from i1 -> i8!"); |
| 1487 | Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1488 | LD->getSrcValue(), LD->getSrcValueOffset(), |
| 1489 | MVT::i8); |
| 1490 | Tmp1 = Result.getValue(0); |
| 1491 | Tmp2 = Result.getValue(1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1492 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1493 | case TargetLowering::Custom: |
| 1494 | isCustom = true; |
| 1495 | // FALLTHROUGH |
| 1496 | case TargetLowering::Legal: |
| 1497 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1498 | Tmp1 = Result.getValue(0); |
| 1499 | Tmp2 = Result.getValue(1); |
| 1500 | |
| 1501 | if (isCustom) { |
| 1502 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1503 | if (Tmp3.Val) { |
| 1504 | Tmp1 = LegalizeOp(Tmp3); |
| 1505 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1506 | } |
| 1507 | } |
| 1508 | break; |
| 1509 | case TargetLowering::Expand: |
| 1510 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 1511 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1512 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1513 | LD->getSrcValueOffset()); |
| 1514 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 1515 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1516 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1517 | break; |
| 1518 | } |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1519 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1520 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1521 | // zero/sign extend inreg. |
| 1522 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1523 | Tmp1, Tmp2, LD->getSrcValue(), |
| 1524 | LD->getSrcValueOffset(), SrcVT); |
| 1525 | SDOperand ValRes; |
| 1526 | if (ExtType == ISD::SEXTLOAD) |
| 1527 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1528 | Result, DAG.getValueType(SrcVT)); |
| 1529 | else |
| 1530 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 1531 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1532 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1533 | break; |
| 1534 | } |
| 1535 | // Since loads produce two values, make sure to remember that we legalized |
| 1536 | // both of them. |
| 1537 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1538 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1539 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1540 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1541 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1542 | case ISD::EXTRACT_ELEMENT: { |
| 1543 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1544 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1545 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1546 | case Legal: |
| 1547 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1548 | // 1 -> Hi |
| 1549 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1550 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1551 | TLI.getShiftAmountTy())); |
| 1552 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1553 | } else { |
| 1554 | // 0 -> Lo |
| 1555 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1556 | Node->getOperand(0)); |
| 1557 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | case Expand: |
| 1560 | // Get both the low and high parts. |
| 1561 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1562 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1563 | Result = Tmp2; // 1 -> Hi |
| 1564 | else |
| 1565 | Result = Tmp1; // 0 -> Lo |
| 1566 | break; |
| 1567 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1568 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1569 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1570 | |
| 1571 | case ISD::CopyToReg: |
| 1572 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1574 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1575 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1576 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1577 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1578 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1579 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1580 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1581 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1582 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1583 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1584 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1585 | Tmp3); |
| 1586 | } else { |
| 1587 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | // Since this produces two values, make sure to remember that we legalized |
| 1591 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1592 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1593 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1594 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1595 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | |
| 1598 | case ISD::RET: |
| 1599 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Ensure that libcalls are emitted before a return. |
| 1602 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1603 | Tmp1 = LegalizeOp(Tmp1); |
| 1604 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1605 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1606 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1607 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 1608 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1609 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1610 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1611 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1612 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1613 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1614 | case Expand: |
| 1615 | if (Tmp2.getValueType() != MVT::Vector) { |
| 1616 | SDOperand Lo, Hi; |
| 1617 | ExpandOp(Tmp2, Lo, Hi); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1618 | if (Hi.Val) |
| 1619 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 1620 | else |
| 1621 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 1622 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1623 | } else { |
| 1624 | SDNode *InVal = Tmp2.Val; |
| 1625 | unsigned NumElems = |
| 1626 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1627 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1628 | |
| 1629 | // Figure out if there is a Packed type corresponding to this Vector |
| 1630 | // type. If so, convert to the packed type. |
| 1631 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1632 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1633 | // Turn this into a return of the packed type. |
| 1634 | Tmp2 = PackVectorOp(Tmp2, TVT); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1635 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1636 | } else if (NumElems == 1) { |
| 1637 | // Turn this into a return of the scalar type. |
| 1638 | Tmp2 = PackVectorOp(Tmp2, EVT); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1639 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1640 | |
| 1641 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 1642 | // should be returned in integer registers! |
| 1643 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1644 | // The scalarized value type may not be legal, e.g. it might require |
| 1645 | // promotion or expansion. Relegalize the return. |
| 1646 | Result = LegalizeOp(Result); |
| 1647 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1648 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 1649 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1650 | SDOperand Lo, Hi; |
| 1651 | SplitVectorOp(Tmp2, Lo, Hi); |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1652 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1653 | Result = LegalizeOp(Result); |
| 1654 | } |
| 1655 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1656 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1657 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1658 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1659 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1660 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1661 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1662 | } |
| 1663 | break; |
| 1664 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1665 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1668 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1669 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1670 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1671 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1672 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1673 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1674 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1675 | break; |
| 1676 | case Expand: { |
| 1677 | SDOperand Lo, Hi; |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1678 | assert(Node->getOperand(i).getValueType() != MVT::Vector && |
| 1679 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1680 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1681 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1682 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1683 | if (Hi.Val) { |
| 1684 | NewValues.push_back(Hi); |
| 1685 | NewValues.push_back(Node->getOperand(i+1)); |
| 1686 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1687 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1688 | } |
| 1689 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1690 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1691 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1692 | |
| 1693 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1694 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1695 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1696 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 1697 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1698 | break; |
| 1699 | } |
| 1700 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1702 | if (Result.getOpcode() == ISD::RET) { |
| 1703 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1704 | default: assert(0 && "This action is not supported yet!"); |
| 1705 | case TargetLowering::Legal: break; |
| 1706 | case TargetLowering::Custom: |
| 1707 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1708 | if (Tmp1.Val) Result = Tmp1; |
| 1709 | break; |
| 1710 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1711 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1712 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1713 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1714 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1715 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1716 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1717 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1718 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1719 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 1720 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 1721 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 1722 | // to phase ordering between legalized code and the dag combiner. This |
| 1723 | // probably means that we need to integrate dag combiner and legalizer |
| 1724 | // together. |
| 1725 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
| 1726 | if (CFP->getValueType(0) == MVT::f32) { |
| 1727 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
| 1728 | } else { |
| 1729 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 1730 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
| 1731 | } |
| 1732 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1733 | ST->getSrcValueOffset()); |
| 1734 | break; |
| 1735 | } |
| 1736 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1737 | switch (getTypeAction(ST->getStoredVT())) { |
| 1738 | case Legal: { |
| 1739 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1740 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1741 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1742 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1743 | MVT::ValueType VT = Tmp3.getValueType(); |
| 1744 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1745 | default: assert(0 && "This action is not supported yet!"); |
| 1746 | case TargetLowering::Legal: break; |
| 1747 | case TargetLowering::Custom: |
| 1748 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1749 | if (Tmp1.Val) Result = Tmp1; |
| 1750 | break; |
| 1751 | case TargetLowering::Promote: |
| 1752 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 1753 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 1754 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 1755 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
| 1756 | ST->getSrcValue(), ST->getSrcValueOffset()); |
| 1757 | break; |
| 1758 | } |
| 1759 | break; |
| 1760 | } |
| 1761 | case Promote: |
| 1762 | // Truncate the value and store the result. |
| 1763 | Tmp3 = PromoteOp(ST->getValue()); |
| 1764 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1765 | ST->getSrcValueOffset(), ST->getStoredVT()); |
| 1766 | break; |
| 1767 | |
| 1768 | case Expand: |
| 1769 | unsigned IncrementSize = 0; |
| 1770 | SDOperand Lo, Hi; |
| 1771 | |
| 1772 | // If this is a vector type, then we have to calculate the increment as |
| 1773 | // the product of the element size in bytes, and the number of elements |
| 1774 | // in the high half of the vector. |
| 1775 | if (ST->getValue().getValueType() == MVT::Vector) { |
| 1776 | SDNode *InVal = ST->getValue().Val; |
| 1777 | unsigned NumElems = |
| 1778 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1779 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1780 | |
| 1781 | // Figure out if there is a Packed type corresponding to this Vector |
| 1782 | // type. If so, convert to the packed type. |
| 1783 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1784 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1785 | // Turn this into a normal store of the packed type. |
| 1786 | Tmp3 = PackVectorOp(Node->getOperand(1), TVT); |
| 1787 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1788 | ST->getSrcValueOffset()); |
| 1789 | Result = LegalizeOp(Result); |
| 1790 | break; |
| 1791 | } else if (NumElems == 1) { |
| 1792 | // Turn this into a normal store of the scalar type. |
| 1793 | Tmp3 = PackVectorOp(Node->getOperand(1), EVT); |
| 1794 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1795 | ST->getSrcValueOffset()); |
| 1796 | // The scalarized value type may not be legal, e.g. it might require |
| 1797 | // promotion or expansion. Relegalize the scalar store. |
| 1798 | Result = LegalizeOp(Result); |
| 1799 | break; |
| 1800 | } else { |
| 1801 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1802 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1803 | } |
| 1804 | } else { |
| 1805 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1806 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1807 | |
| 1808 | if (!TLI.isLittleEndian()) |
| 1809 | std::swap(Lo, Hi); |
| 1810 | } |
| 1811 | |
| 1812 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
| 1813 | ST->getSrcValueOffset()); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1814 | |
| 1815 | if (Hi.Val == NULL) { |
| 1816 | // Must be int <-> float one-to-one expansion. |
| 1817 | Result = Lo; |
| 1818 | break; |
| 1819 | } |
| 1820 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1821 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1822 | getIntPtrConstant(IncrementSize)); |
| 1823 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1824 | "Pointers must be legal!"); |
| 1825 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1826 | // wrong. |
| 1827 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
| 1828 | ST->getSrcValueOffset()); |
| 1829 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1830 | break; |
| 1831 | } |
| 1832 | } else { |
| 1833 | // Truncating store |
| 1834 | assert(isTypeLegal(ST->getValue().getValueType()) && |
| 1835 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1836 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1837 | |
| 1838 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1839 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1840 | if (ST->getStoredVT() == MVT::i1 && |
| 1841 | TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) { |
| 1842 | // Promote the bool to a mask then store. |
| 1843 | Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3, |
| 1844 | DAG.getConstant(1, Tmp3.getValueType())); |
| 1845 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1846 | ST->getSrcValueOffset(), MVT::i8); |
| 1847 | } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 1848 | Tmp2 != ST->getBasePtr()) { |
| 1849 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1850 | ST->getOffset()); |
| 1851 | } |
| 1852 | |
| 1853 | MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT(); |
| 1854 | switch (TLI.getStoreXAction(StVT)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1855 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1856 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1857 | case TargetLowering::Custom: |
| 1858 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1859 | if (Tmp1.Val) Result = Tmp1; |
| 1860 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1861 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1862 | } |
| 1863 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1864 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1865 | case ISD::PCMARKER: |
| 1866 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1867 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1868 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1869 | case ISD::STACKSAVE: |
| 1870 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1871 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1872 | Tmp1 = Result.getValue(0); |
| 1873 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1875 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1876 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1877 | case TargetLowering::Legal: break; |
| 1878 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1879 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1880 | if (Tmp3.Val) { |
| 1881 | Tmp1 = LegalizeOp(Tmp3); |
| 1882 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1883 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1884 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1885 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1886 | // Expand to CopyFromReg if the target set |
| 1887 | // StackPointerRegisterToSaveRestore. |
| 1888 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1889 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1890 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1891 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1892 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1893 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1894 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1895 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1896 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1897 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1898 | |
| 1899 | // Since stacksave produce two values, make sure to remember that we |
| 1900 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1901 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1902 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1903 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1904 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1905 | case ISD::STACKRESTORE: |
| 1906 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1907 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1908 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1909 | |
| 1910 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1911 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1912 | case TargetLowering::Legal: break; |
| 1913 | case TargetLowering::Custom: |
| 1914 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1915 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1916 | break; |
| 1917 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1918 | // Expand to CopyToReg if the target set |
| 1919 | // StackPointerRegisterToSaveRestore. |
| 1920 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1921 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1922 | } else { |
| 1923 | Result = Tmp1; |
| 1924 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1925 | break; |
| 1926 | } |
| 1927 | break; |
| 1928 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1929 | case ISD::READCYCLECOUNTER: |
| 1930 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1931 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1932 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 1933 | Node->getValueType(0))) { |
| 1934 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1935 | case TargetLowering::Legal: |
| 1936 | Tmp1 = Result.getValue(0); |
| 1937 | Tmp2 = Result.getValue(1); |
| 1938 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1939 | case TargetLowering::Custom: |
| 1940 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1941 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 1942 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1943 | break; |
| 1944 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1945 | |
| 1946 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1947 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1948 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1949 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1950 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1951 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1952 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1953 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1954 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1955 | case Legal: |
| 1956 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1957 | break; |
| 1958 | case Promote: |
| 1959 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 1960 | // Make sure the condition is either zero or one. |
| 1961 | if (!TLI.MaskedValueIsZero(Tmp1, |
| 1962 | MVT::getIntVTBitMask(Tmp1.getValueType())^1)) |
| 1963 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1966 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1967 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1968 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1969 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1970 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1971 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1972 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1973 | case TargetLowering::Legal: break; |
| 1974 | case TargetLowering::Custom: { |
| 1975 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1976 | if (Tmp1.Val) Result = Tmp1; |
| 1977 | break; |
| 1978 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1979 | case TargetLowering::Expand: |
| 1980 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 1981 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 1982 | Tmp2, Tmp3, |
| 1983 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1984 | } else { |
| 1985 | Result = DAG.getSelectCC(Tmp1, |
| 1986 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1987 | Tmp2, Tmp3, ISD::SETNE); |
| 1988 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1989 | break; |
| 1990 | case TargetLowering::Promote: { |
| 1991 | MVT::ValueType NVT = |
| 1992 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1993 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 1994 | if (MVT::isVector(Tmp2.getValueType())) { |
| 1995 | ExtOp = ISD::BIT_CONVERT; |
| 1996 | TruncOp = ISD::BIT_CONVERT; |
| 1997 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1998 | ExtOp = ISD::ANY_EXTEND; |
| 1999 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2000 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2001 | ExtOp = ISD::FP_EXTEND; |
| 2002 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2003 | } |
| 2004 | // Promote each of the values to the new type. |
| 2005 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 2006 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 2007 | // Perform the larger operation, then round down. |
| 2008 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 2009 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 2010 | break; |
| 2011 | } |
| 2012 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2013 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2014 | case ISD::SELECT_CC: { |
| 2015 | Tmp1 = Node->getOperand(0); // LHS |
| 2016 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2017 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 2018 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2019 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2020 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2021 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 2022 | |
| 2023 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 2024 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 2025 | // the result against zero to select between true and false values. |
| 2026 | if (Tmp2.Val == 0) { |
| 2027 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2028 | CC = DAG.getCondCode(ISD::SETNE); |
| 2029 | } |
| 2030 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 2031 | |
| 2032 | // Everything is legal, see if we should expand this op or something. |
| 2033 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 2034 | default: assert(0 && "This action is not supported yet!"); |
| 2035 | case TargetLowering::Legal: break; |
| 2036 | case TargetLowering::Custom: |
| 2037 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2038 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2039 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2040 | } |
| 2041 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2042 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2043 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2044 | Tmp1 = Node->getOperand(0); |
| 2045 | Tmp2 = Node->getOperand(1); |
| 2046 | Tmp3 = Node->getOperand(2); |
| 2047 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 2048 | |
| 2049 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2050 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2051 | // return the value we legalized, returned in the LHS |
| 2052 | if (Tmp2.Val == 0) { |
| 2053 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2054 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2055 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2057 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2058 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2059 | case TargetLowering::Custom: |
| 2060 | isCustom = true; |
| 2061 | // FALLTHROUGH. |
| 2062 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2063 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2064 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2065 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2066 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2067 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2068 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2069 | case TargetLowering::Promote: { |
| 2070 | // First step, figure out the appropriate operation to use. |
| 2071 | // Allow SETCC to not be supported for all legal data types |
| 2072 | // Mostly this targets FP |
| 2073 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
| 2074 | MVT::ValueType OldVT = NewInTy; |
| 2075 | |
| 2076 | // Scan for the appropriate larger type to use. |
| 2077 | while (1) { |
| 2078 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2079 | |
| 2080 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2081 | "Fell off of the edge of the integer world"); |
| 2082 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2083 | "Fell off of the edge of the floating point world"); |
| 2084 | |
| 2085 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2086 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
| 2089 | if (MVT::isInteger(NewInTy)) |
| 2090 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2091 | else { |
| 2092 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2093 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2094 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2095 | Tmp1 = LegalizeOp(Tmp1); |
| 2096 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2097 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2098 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2099 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2100 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2101 | case TargetLowering::Expand: |
| 2102 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2103 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2104 | MVT::ValueType VT = Node->getValueType(0); |
| 2105 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2106 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2107 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2108 | break; |
| 2109 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2110 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2111 | case ISD::MEMSET: |
| 2112 | case ISD::MEMCPY: |
| 2113 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2114 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2115 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 2116 | |
| 2117 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 2118 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 2119 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 2120 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2121 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2122 | break; |
| 2123 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2124 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2125 | break; |
| 2126 | } |
| 2127 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2128 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2129 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2130 | |
| 2131 | SDOperand Tmp4; |
| 2132 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2133 | case Expand: { |
| 2134 | // Length is too big, just take the lo-part of the length. |
| 2135 | SDOperand HiPart; |
Chris Lattner | fa9aa2b | 2006-11-07 04:11:44 +0000 | [diff] [blame] | 2136 | ExpandOp(Node->getOperand(3), Tmp4, HiPart); |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2137 | break; |
| 2138 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2139 | case Legal: |
| 2140 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2141 | break; |
| 2142 | case Promote: |
| 2143 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2144 | break; |
| 2145 | } |
| 2146 | |
| 2147 | SDOperand Tmp5; |
| 2148 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 2149 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2150 | case Legal: |
| 2151 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 2152 | break; |
| 2153 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2154 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 2155 | break; |
| 2156 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2157 | |
| 2158 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2159 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2160 | case TargetLowering::Custom: |
| 2161 | isCustom = true; |
| 2162 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2163 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2164 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2165 | if (isCustom) { |
| 2166 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2167 | if (Tmp1.Val) Result = Tmp1; |
| 2168 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2171 | // Otherwise, the target does not support this operation. Lower the |
| 2172 | // operation to an explicit libcall as appropriate. |
| 2173 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2174 | const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2175 | TargetLowering::ArgListTy Args; |
| 2176 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2177 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 2178 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2179 | if (Node->getOpcode() == ISD::MEMSET) { |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 2180 | Entry.Node = Tmp2; Entry.isSigned = false; Entry.Ty = IntPtrTy; |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame^] | 2181 | Entry.isInReg = false; Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2182 | Args.push_back(Entry); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 2183 | // Extend the (previously legalized) ubyte argument to be an int value |
| 2184 | // for the call. |
| 2185 | if (Tmp3.getValueType() > MVT::i32) |
| 2186 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 2187 | else |
| 2188 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2189 | Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSigned = true; |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame^] | 2190 | Entry.isInReg = false; Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2191 | Args.push_back(Entry); |
| 2192 | Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSigned = false; |
| 2193 | Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2194 | |
| 2195 | FnName = "memset"; |
| 2196 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2197 | Node->getOpcode() == ISD::MEMMOVE) { |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame^] | 2198 | Entry.Ty = IntPtrTy; |
| 2199 | Entry.isSigned = false; Entry.isInReg = false; Entry.isSRet = false; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 2200 | Entry.Node = Tmp2; Args.push_back(Entry); |
| 2201 | Entry.Node = Tmp3; Args.push_back(Entry); |
| 2202 | Entry.Node = Tmp4; Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2203 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2204 | } else { |
| 2205 | assert(0 && "Unknown op!"); |
| 2206 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2207 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2208 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2209 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2210 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2211 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2212 | break; |
| 2213 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2214 | } |
| 2215 | break; |
| 2216 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2217 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2218 | case ISD::SHL_PARTS: |
| 2219 | case ISD::SRA_PARTS: |
| 2220 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2221 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2222 | bool Changed = false; |
| 2223 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2224 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2225 | Changed |= Ops.back() != Node->getOperand(i); |
| 2226 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2227 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2228 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2229 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2230 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2231 | Node->getValueType(0))) { |
| 2232 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2233 | case TargetLowering::Legal: break; |
| 2234 | case TargetLowering::Custom: |
| 2235 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2236 | if (Tmp1.Val) { |
| 2237 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2238 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2239 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2240 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2241 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2242 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2243 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2244 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2245 | return RetVal; |
| 2246 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2247 | break; |
| 2248 | } |
| 2249 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2250 | // Since these produce multiple values, make sure to remember that we |
| 2251 | // legalized all of them. |
| 2252 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2253 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2254 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2255 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2256 | |
| 2257 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2258 | case ISD::ADD: |
| 2259 | case ISD::SUB: |
| 2260 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2261 | case ISD::MULHS: |
| 2262 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2263 | case ISD::UDIV: |
| 2264 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2265 | case ISD::AND: |
| 2266 | case ISD::OR: |
| 2267 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2268 | case ISD::SHL: |
| 2269 | case ISD::SRL: |
| 2270 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2271 | case ISD::FADD: |
| 2272 | case ISD::FSUB: |
| 2273 | case ISD::FMUL: |
| 2274 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2275 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2276 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2277 | case Expand: assert(0 && "Not possible"); |
| 2278 | case Legal: |
| 2279 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2280 | break; |
| 2281 | case Promote: |
| 2282 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2283 | break; |
| 2284 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2285 | |
| 2286 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2287 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2288 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2289 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2290 | case TargetLowering::Legal: break; |
| 2291 | case TargetLowering::Custom: |
| 2292 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2293 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2294 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2295 | case TargetLowering::Expand: { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2296 | if (Node->getValueType(0) == MVT::i32) { |
| 2297 | switch (Node->getOpcode()) { |
| 2298 | default: assert(0 && "Do not know how to expand this integer BinOp!"); |
| 2299 | case ISD::UDIV: |
| 2300 | case ISD::SDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2301 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV |
| 2302 | ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2303 | SDOperand Dummy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2304 | bool isSigned = Node->getOpcode() == ISD::SDIV; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2305 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2306 | }; |
| 2307 | break; |
| 2308 | } |
| 2309 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2310 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2311 | "Cannot expand this binary operator!"); |
| 2312 | // Expand the operation into a bunch of nasty scalar code. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2313 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2314 | MVT::ValueType EltVT = MVT::getVectorBaseType(Node->getValueType(0)); |
| 2315 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2316 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2317 | i != e; ++i) { |
| 2318 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2319 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2320 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2321 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2322 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2323 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), |
| 2324 | &Ops[0], Ops.size()); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2325 | break; |
| 2326 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2327 | case TargetLowering::Promote: { |
| 2328 | switch (Node->getOpcode()) { |
| 2329 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2330 | case ISD::AND: |
| 2331 | case ISD::OR: |
| 2332 | case ISD::XOR: { |
| 2333 | MVT::ValueType OVT = Node->getValueType(0); |
| 2334 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2335 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 2336 | // Bit convert each of the values to the new type. |
| 2337 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 2338 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 2339 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2340 | // Bit convert the result back the original type. |
| 2341 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 2342 | break; |
| 2343 | } |
| 2344 | } |
| 2345 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2346 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2347 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2348 | |
| 2349 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2350 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2351 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2352 | case Expand: assert(0 && "Not possible"); |
| 2353 | case Legal: |
| 2354 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2355 | break; |
| 2356 | case Promote: |
| 2357 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2358 | break; |
| 2359 | } |
| 2360 | |
| 2361 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2362 | |
| 2363 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2364 | default: assert(0 && "Operation not supported"); |
| 2365 | case TargetLowering::Custom: |
| 2366 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2367 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2368 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2369 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2370 | case TargetLowering::Expand: { |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2371 | // If this target supports fabs/fneg natively and select is cheap, |
| 2372 | // do this efficiently. |
| 2373 | if (!TLI.isSelectExpensive() && |
| 2374 | TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) == |
| 2375 | TargetLowering::Legal && |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2376 | TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) == |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2377 | TargetLowering::Legal) { |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2378 | // Get the sign bit of the RHS. |
| 2379 | MVT::ValueType IVT = |
| 2380 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2381 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2382 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2383 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2384 | // Get the absolute value of the result. |
| 2385 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2386 | // Select between the nabs and abs value based on the sign bit of |
| 2387 | // the input. |
| 2388 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2389 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2390 | AbsVal), |
| 2391 | AbsVal); |
| 2392 | Result = LegalizeOp(Result); |
| 2393 | break; |
| 2394 | } |
| 2395 | |
| 2396 | // Otherwise, do bitwise ops! |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2397 | MVT::ValueType NVT = |
| 2398 | Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2399 | Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 2400 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result); |
| 2401 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2404 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2405 | break; |
| 2406 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2407 | case ISD::ADDC: |
| 2408 | case ISD::SUBC: |
| 2409 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2410 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2411 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2412 | // Since this produces two values, make sure to remember that we legalized |
| 2413 | // both of them. |
| 2414 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2415 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2416 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2417 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2418 | case ISD::ADDE: |
| 2419 | case ISD::SUBE: |
| 2420 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2421 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2422 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2423 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2424 | // Since this produces two values, make sure to remember that we legalized |
| 2425 | // both of them. |
| 2426 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2427 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2428 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2429 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2430 | case ISD::BUILD_PAIR: { |
| 2431 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2432 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2433 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2434 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2435 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2436 | case TargetLowering::Promote: |
| 2437 | case TargetLowering::Custom: |
| 2438 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2439 | case TargetLowering::Legal: |
| 2440 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2441 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2442 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2443 | case TargetLowering::Expand: |
| 2444 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2445 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2446 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2447 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2448 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2449 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2450 | break; |
| 2451 | } |
| 2452 | break; |
| 2453 | } |
| 2454 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2455 | case ISD::UREM: |
| 2456 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2457 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2458 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2459 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2460 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2461 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2462 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2463 | case TargetLowering::Custom: |
| 2464 | isCustom = true; |
| 2465 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2466 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2467 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2468 | if (isCustom) { |
| 2469 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2470 | if (Tmp1.Val) Result = Tmp1; |
| 2471 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2472 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2473 | case TargetLowering::Expand: |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2474 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2475 | bool isSigned = DivOpc == ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2476 | if (MVT::isInteger(Node->getValueType(0))) { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2477 | if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) == |
| 2478 | TargetLowering::Legal) { |
| 2479 | // X % Y -> X-X/Y*Y |
| 2480 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2481 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2482 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2483 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2484 | } else { |
| 2485 | assert(Node->getValueType(0) == MVT::i32 && |
| 2486 | "Cannot expand this binary operator!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2487 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM |
| 2488 | ? RTLIB::UREM_I32 : RTLIB::SREM_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2489 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2490 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2491 | } |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2492 | } else { |
| 2493 | // Floating point mod -> fmod libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2494 | RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32 |
| 2495 | ? RTLIB::REM_F32 : RTLIB::REM_F64; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2496 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2497 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2498 | false/*sign irrelevant*/, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2499 | } |
| 2500 | break; |
| 2501 | } |
| 2502 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2503 | case ISD::VAARG: { |
| 2504 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2505 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2506 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2507 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2508 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2509 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2510 | case TargetLowering::Custom: |
| 2511 | isCustom = true; |
| 2512 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2513 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2514 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2515 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2516 | Tmp1 = Result.getValue(1); |
| 2517 | |
| 2518 | if (isCustom) { |
| 2519 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2520 | if (Tmp2.Val) { |
| 2521 | Result = LegalizeOp(Tmp2); |
| 2522 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2523 | } |
| 2524 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2525 | break; |
| 2526 | case TargetLowering::Expand: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2527 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2528 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2529 | SV->getValue(), SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2530 | // Increment the pointer, VAList, to the next vaarg |
| 2531 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2532 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2533 | TLI.getPointerTy())); |
| 2534 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2535 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 2536 | SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2537 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2538 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2539 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2540 | Result = LegalizeOp(Result); |
| 2541 | break; |
| 2542 | } |
| 2543 | } |
| 2544 | // Since VAARG produces two values, make sure to remember that we |
| 2545 | // legalized both of them. |
| 2546 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2547 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2548 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | case ISD::VACOPY: |
| 2552 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2553 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2554 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2555 | |
| 2556 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2557 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2558 | case TargetLowering::Custom: |
| 2559 | isCustom = true; |
| 2560 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2561 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2562 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2563 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2564 | if (isCustom) { |
| 2565 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2566 | if (Tmp1.Val) Result = Tmp1; |
| 2567 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | case TargetLowering::Expand: |
| 2570 | // This defaults to loading a pointer from the input and storing it to the |
| 2571 | // output, returning the chain. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2572 | SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2573 | SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2574 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(), |
| 2575 | SVD->getOffset()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2576 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(), |
| 2577 | SVS->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | } |
| 2580 | break; |
| 2581 | |
| 2582 | case ISD::VAEND: |
| 2583 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2584 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2585 | |
| 2586 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2587 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2588 | case TargetLowering::Custom: |
| 2589 | isCustom = true; |
| 2590 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2591 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2592 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2593 | if (isCustom) { |
| 2594 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2595 | if (Tmp1.Val) Result = Tmp1; |
| 2596 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2597 | break; |
| 2598 | case TargetLowering::Expand: |
| 2599 | Result = Tmp1; // Default to a no-op, return the chain |
| 2600 | break; |
| 2601 | } |
| 2602 | break; |
| 2603 | |
| 2604 | case ISD::VASTART: |
| 2605 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2606 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2607 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2608 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2609 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2610 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2611 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2612 | case TargetLowering::Legal: break; |
| 2613 | case TargetLowering::Custom: |
| 2614 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2615 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2616 | break; |
| 2617 | } |
| 2618 | break; |
| 2619 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2620 | case ISD::ROTL: |
| 2621 | case ISD::ROTR: |
| 2622 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2623 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2624 | |
| 2625 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
| 2626 | "Cannot handle this yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2627 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2628 | break; |
| 2629 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2630 | case ISD::BSWAP: |
| 2631 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2632 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2633 | case TargetLowering::Custom: |
| 2634 | assert(0 && "Cannot custom legalize this yet!"); |
| 2635 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2636 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | case TargetLowering::Promote: { |
| 2639 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2640 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2641 | unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2643 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2644 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2645 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2646 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2647 | break; |
| 2648 | } |
| 2649 | case TargetLowering::Expand: |
| 2650 | Result = ExpandBSWAP(Tmp1); |
| 2651 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2652 | } |
| 2653 | break; |
| 2654 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2655 | case ISD::CTPOP: |
| 2656 | case ISD::CTTZ: |
| 2657 | case ISD::CTLZ: |
| 2658 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2659 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2660 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2661 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2662 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2663 | break; |
| 2664 | case TargetLowering::Promote: { |
| 2665 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2666 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2667 | |
| 2668 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2669 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2670 | // Perform the larger operation, then subtract if needed. |
| 2671 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2672 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2673 | case ISD::CTPOP: |
| 2674 | Result = Tmp1; |
| 2675 | break; |
| 2676 | case ISD::CTTZ: |
| 2677 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2678 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
| 2679 | DAG.getConstant(getSizeInBits(NVT), NVT), |
| 2680 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2681 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2682 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 2683 | break; |
| 2684 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2685 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2686 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2687 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2688 | getSizeInBits(OVT), NVT)); |
| 2689 | break; |
| 2690 | } |
| 2691 | break; |
| 2692 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2693 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2694 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2695 | break; |
| 2696 | } |
| 2697 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2698 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2699 | // Unary operators |
| 2700 | case ISD::FABS: |
| 2701 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2702 | case ISD::FSQRT: |
| 2703 | case ISD::FSIN: |
| 2704 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2705 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2706 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2707 | case TargetLowering::Promote: |
| 2708 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2709 | isCustom = true; |
| 2710 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2711 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2712 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2713 | if (isCustom) { |
| 2714 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2715 | if (Tmp1.Val) Result = Tmp1; |
| 2716 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2717 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2718 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2719 | switch (Node->getOpcode()) { |
| 2720 | default: assert(0 && "Unreachable!"); |
| 2721 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2722 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2723 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2724 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2725 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2726 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2727 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2728 | MVT::ValueType VT = Node->getValueType(0); |
| 2729 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2730 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2731 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2732 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2733 | break; |
| 2734 | } |
| 2735 | case ISD::FSQRT: |
| 2736 | case ISD::FSIN: |
| 2737 | case ISD::FCOS: { |
| 2738 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2739 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2740 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2741 | case ISD::FSQRT: |
| 2742 | LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64; |
| 2743 | break; |
| 2744 | case ISD::FSIN: |
| 2745 | LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 2746 | break; |
| 2747 | case ISD::FCOS: |
| 2748 | LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 2749 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2750 | default: assert(0 && "Unreachable!"); |
| 2751 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2752 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2753 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2754 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2755 | break; |
| 2756 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2757 | } |
| 2758 | break; |
| 2759 | } |
| 2760 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2761 | case ISD::FPOWI: { |
| 2762 | // We always lower FPOWI into a libcall. No target support it yet. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2763 | RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32 |
| 2764 | ? RTLIB::POWI_F32 : RTLIB::POWI_F64; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2765 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2766 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2767 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2768 | break; |
| 2769 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2770 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2771 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2772 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2773 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2774 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2775 | Node->getOperand(0).getValueType())) { |
| 2776 | default: assert(0 && "Unknown operation action!"); |
| 2777 | case TargetLowering::Expand: |
| 2778 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2779 | break; |
| 2780 | case TargetLowering::Legal: |
| 2781 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2782 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2783 | break; |
| 2784 | } |
| 2785 | } |
| 2786 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 2787 | case ISD::VBIT_CONVERT: { |
| 2788 | assert(Op.getOperand(0).getValueType() == MVT::Vector && |
| 2789 | "Can only have VBIT_CONVERT where input or output is MVT::Vector!"); |
| 2790 | |
| 2791 | // The input has to be a vector type, we have to either scalarize it, pack |
| 2792 | // it, or convert it based on whether the input vector type is legal. |
| 2793 | SDNode *InVal = Node->getOperand(0).Val; |
| 2794 | unsigned NumElems = |
| 2795 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 2796 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 2797 | |
| 2798 | // Figure out if there is a Packed type corresponding to this Vector |
| 2799 | // type. If so, convert to the packed type. |
| 2800 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 2801 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 2802 | // Turn this into a bit convert of the packed input. |
| 2803 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2804 | PackVectorOp(Node->getOperand(0), TVT)); |
| 2805 | break; |
| 2806 | } else if (NumElems == 1) { |
| 2807 | // Turn this into a bit convert of the scalar input. |
| 2808 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2809 | PackVectorOp(Node->getOperand(0), EVT)); |
| 2810 | break; |
| 2811 | } else { |
| 2812 | // FIXME: UNIMP! Store then reload |
| 2813 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 2814 | } |
| 2815 | } |
| 2816 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2817 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2818 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2819 | case ISD::UINT_TO_FP: { |
| 2820 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2821 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2822 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2823 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2824 | Node->getOperand(0).getValueType())) { |
| 2825 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2826 | case TargetLowering::Custom: |
| 2827 | isCustom = true; |
| 2828 | // FALLTHROUGH |
| 2829 | case TargetLowering::Legal: |
| 2830 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2831 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2832 | if (isCustom) { |
| 2833 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2834 | if (Tmp1.Val) Result = Tmp1; |
| 2835 | } |
| 2836 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2837 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2838 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2839 | LegalizeOp(Node->getOperand(0)), |
| 2840 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2841 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2842 | case TargetLowering::Promote: |
| 2843 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2844 | Node->getValueType(0), |
| 2845 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2846 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2847 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2848 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2849 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2850 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2851 | Node->getValueType(0), Node->getOperand(0)); |
| 2852 | break; |
| 2853 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2854 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2855 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2856 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2857 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2858 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2859 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2860 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2861 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2862 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2863 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2864 | break; |
| 2865 | } |
| 2866 | break; |
| 2867 | } |
| 2868 | case ISD::TRUNCATE: |
| 2869 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2870 | case Legal: |
| 2871 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2872 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2873 | break; |
| 2874 | case Expand: |
| 2875 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2876 | |
| 2877 | // Since the result is legal, we should just be able to truncate the low |
| 2878 | // part of the source. |
| 2879 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2880 | break; |
| 2881 | case Promote: |
| 2882 | Result = PromoteOp(Node->getOperand(0)); |
| 2883 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2884 | break; |
| 2885 | } |
| 2886 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2887 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2888 | case ISD::FP_TO_SINT: |
| 2889 | case ISD::FP_TO_UINT: |
| 2890 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2891 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2892 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2893 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2894 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2895 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2896 | case TargetLowering::Custom: |
| 2897 | isCustom = true; |
| 2898 | // FALLTHROUGH |
| 2899 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2900 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2901 | if (isCustom) { |
| 2902 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2903 | if (Tmp1.Val) Result = Tmp1; |
| 2904 | } |
| 2905 | break; |
| 2906 | case TargetLowering::Promote: |
| 2907 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2908 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2909 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2910 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2911 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2912 | SDOperand True, False; |
| 2913 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2914 | MVT::ValueType NVT = Node->getValueType(0); |
| 2915 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2916 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2917 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2918 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2919 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2920 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2921 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2922 | Tmp2)); |
| 2923 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2924 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2925 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2926 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2927 | } else { |
| 2928 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2929 | } |
| 2930 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2931 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2932 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2933 | case Expand: { |
| 2934 | // Convert f32 / f64 to i32 / i64. |
| 2935 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2936 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2937 | switch (Node->getOpcode()) { |
| 2938 | case ISD::FP_TO_SINT: |
| 2939 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2940 | LC = (VT == MVT::i32) |
| 2941 | ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2942 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2943 | LC = (VT == MVT::i32) |
| 2944 | ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2945 | break; |
| 2946 | case ISD::FP_TO_UINT: |
| 2947 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2948 | LC = (VT == MVT::i32) |
| 2949 | ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2950 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2951 | LC = (VT == MVT::i32) |
| 2952 | ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2953 | break; |
| 2954 | default: assert(0 && "Unreachable!"); |
| 2955 | } |
| 2956 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2957 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2958 | false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2959 | break; |
| 2960 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2961 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2962 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2963 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 2964 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2965 | break; |
| 2966 | } |
| 2967 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2968 | |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2969 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2970 | case ISD::ZERO_EXTEND: |
| 2971 | case ISD::SIGN_EXTEND: |
| 2972 | case ISD::FP_EXTEND: |
| 2973 | case ISD::FP_ROUND: |
| 2974 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2975 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2976 | case Legal: |
| 2977 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2978 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2979 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2980 | case Promote: |
| 2981 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2982 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2983 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2984 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2985 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2986 | case ISD::ZERO_EXTEND: |
| 2987 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2988 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2989 | Result = DAG.getZeroExtendInReg(Result, |
| 2990 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2991 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2992 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2993 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2994 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2995 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2996 | Result, |
| 2997 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2998 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2999 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3000 | Result = PromoteOp(Node->getOperand(0)); |
| 3001 | if (Result.getValueType() != Op.getValueType()) |
| 3002 | // Dynamically dead while we have only 2 FP types. |
| 3003 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 3004 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3005 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 3006 | Result = PromoteOp(Node->getOperand(0)); |
| 3007 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 3008 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3009 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3010 | } |
| 3011 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3012 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3013 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3014 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3015 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3016 | |
| 3017 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 3018 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3019 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 3020 | default: assert(0 && "This action not supported for this op yet!"); |
| 3021 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3022 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3023 | break; |
| 3024 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3025 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3026 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3027 | // NOTE: we could fall back on load/store here too for targets without |
| 3028 | // SAR. However, it is doubtful that any exist. |
| 3029 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 3030 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 3031 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3032 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 3033 | Node->getOperand(0), ShiftCst); |
| 3034 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 3035 | Result, ShiftCst); |
| 3036 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 3037 | // 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] | 3038 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3039 | |
| 3040 | // NOTE: there is a choice here between constantly creating new stack |
| 3041 | // slots and always reusing the same one. We currently always create |
| 3042 | // new ones, as reuse may inhibit scheduling. |
| 3043 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 3044 | unsigned TySize = (unsigned)TLI.getTargetData()->getTypeSize(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 3045 | unsigned Align = TLI.getTargetData()->getTypeAlignmentPref(Ty); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3046 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3047 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3048 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 3049 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3050 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3051 | StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3052 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3053 | Result, StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3054 | } else { |
| 3055 | assert(0 && "Unknown op"); |
| 3056 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3057 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3058 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3059 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3060 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3061 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3062 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3063 | assert(Result.getValueType() == Op.getValueType() && |
| 3064 | "Bad legalization!"); |
| 3065 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3066 | // Make sure that the generated code is itself legal. |
| 3067 | if (Result != Op) |
| 3068 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3069 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3070 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3071 | // means that we always must cache transformed nodes. |
| 3072 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3073 | return Result; |
| 3074 | } |
| 3075 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3076 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 3077 | /// promote it to compute the value into a larger type. The produced value will |
| 3078 | /// have the correct bits for the low portion of the register, but no guarantee |
| 3079 | /// 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] | 3080 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 3081 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3082 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3083 | assert(getTypeAction(VT) == Promote && |
| 3084 | "Caller should expand or legalize operands that are not promotable!"); |
| 3085 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 3086 | "Cannot promote to smaller type!"); |
| 3087 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3088 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3089 | SDOperand Result; |
| 3090 | SDNode *Node = Op.Val; |
| 3091 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3092 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 3093 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3095 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3096 | case ISD::CopyFromReg: |
| 3097 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3098 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3099 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 3100 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3101 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3102 | assert(0 && "Do not know how to promote this operator!"); |
| 3103 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 3104 | case ISD::UNDEF: |
| 3105 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 3106 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3107 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 3108 | if (VT != MVT::i1) |
| 3109 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 3110 | else |
| 3111 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3112 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 3113 | break; |
| 3114 | case ISD::ConstantFP: |
| 3115 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 3116 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 3117 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 3118 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3119 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3120 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3121 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 3122 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3123 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3124 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3125 | case ISD::TRUNCATE: |
| 3126 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3127 | case Legal: |
| 3128 | Result = LegalizeOp(Node->getOperand(0)); |
| 3129 | assert(Result.getValueType() >= NVT && |
| 3130 | "This truncation doesn't make sense!"); |
| 3131 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 3132 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 3133 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 3134 | case Promote: |
| 3135 | // The truncation is not required, because we don't guarantee anything |
| 3136 | // about high bits anyway. |
| 3137 | Result = PromoteOp(Node->getOperand(0)); |
| 3138 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3139 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 3140 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3141 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 3142 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3143 | } |
| 3144 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3145 | case ISD::SIGN_EXTEND: |
| 3146 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3147 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3148 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3149 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 3150 | case Legal: |
| 3151 | // 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] | 3152 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3153 | break; |
| 3154 | case Promote: |
| 3155 | // Promote the reg if it's smaller. |
| 3156 | Result = PromoteOp(Node->getOperand(0)); |
| 3157 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 3158 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 3159 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3160 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3161 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3162 | Result = DAG.getZeroExtendInReg(Result, |
| 3163 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3164 | break; |
| 3165 | } |
| 3166 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3167 | case ISD::BIT_CONVERT: |
| 3168 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 3169 | Result = PromoteOp(Result); |
| 3170 | break; |
| 3171 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3172 | case ISD::FP_EXTEND: |
| 3173 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 3174 | case ISD::FP_ROUND: |
| 3175 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3176 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 3177 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 3178 | case Legal: |
| 3179 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3180 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3181 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3182 | break; |
| 3183 | } |
| 3184 | break; |
| 3185 | |
| 3186 | case ISD::SINT_TO_FP: |
| 3187 | case ISD::UINT_TO_FP: |
| 3188 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3189 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3190 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3191 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3192 | break; |
| 3193 | |
| 3194 | case Promote: |
| 3195 | Result = PromoteOp(Node->getOperand(0)); |
| 3196 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 3197 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3198 | Result, |
| 3199 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3200 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3201 | Result = DAG.getZeroExtendInReg(Result, |
| 3202 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3203 | // No extra round required here. |
| 3204 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3207 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 3208 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3209 | // Round if we cannot tolerate excess precision. |
| 3210 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3211 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3212 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3213 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3214 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3215 | break; |
| 3216 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 3217 | case ISD::SIGN_EXTEND_INREG: |
| 3218 | Result = PromoteOp(Node->getOperand(0)); |
| 3219 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 3220 | Node->getOperand(1)); |
| 3221 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3222 | case ISD::FP_TO_SINT: |
| 3223 | case ISD::FP_TO_UINT: |
| 3224 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3225 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 3226 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3227 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3228 | break; |
| 3229 | case Promote: |
| 3230 | // The input result is prerounded, so we don't have to do anything |
| 3231 | // special. |
| 3232 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3233 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3234 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3235 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 3236 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 3237 | // we can use that instead. This allows us to generate better code for |
| 3238 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 3239 | // legal, such as PowerPC. |
| 3240 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3241 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 3242 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 3243 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3244 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 3245 | } else { |
| 3246 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3247 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3248 | break; |
| 3249 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3250 | case ISD::FABS: |
| 3251 | case ISD::FNEG: |
| 3252 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3253 | assert(Tmp1.getValueType() == NVT); |
| 3254 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3255 | // NOTE: we do not have to do any extra rounding here for |
| 3256 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 3257 | // precision, and these operations don't modify precision at all. |
| 3258 | break; |
| 3259 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3260 | case ISD::FSQRT: |
| 3261 | case ISD::FSIN: |
| 3262 | case ISD::FCOS: |
| 3263 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3264 | assert(Tmp1.getValueType() == NVT); |
| 3265 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3266 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3267 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3268 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3269 | break; |
| 3270 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3271 | case ISD::AND: |
| 3272 | case ISD::OR: |
| 3273 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3274 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3275 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3276 | case ISD::MUL: |
| 3277 | // 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] | 3278 | // 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] | 3279 | // that too is okay if they are integer operations. |
| 3280 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3281 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3282 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3283 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3284 | break; |
| 3285 | case ISD::FADD: |
| 3286 | case ISD::FSUB: |
| 3287 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3288 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3289 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3290 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3291 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3292 | |
| 3293 | // Floating point operations will give excess precision that we may not be |
| 3294 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 3295 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3296 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 3297 | // 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] | 3298 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3299 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3300 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3301 | break; |
| 3302 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3303 | case ISD::SDIV: |
| 3304 | case ISD::SREM: |
| 3305 | // These operators require that their input be sign extended. |
| 3306 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3307 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3308 | if (MVT::isInteger(NVT)) { |
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)); |
| 3311 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3312 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3313 | } |
| 3314 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3315 | |
| 3316 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 3317 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3318 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3319 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3320 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3321 | case ISD::FDIV: |
| 3322 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3323 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3324 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 3325 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3326 | case Legal: |
| 3327 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3328 | break; |
| 3329 | case Promote: |
| 3330 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3331 | break; |
| 3332 | case Expand: |
| 3333 | assert(0 && "not implemented"); |
| 3334 | } |
| 3335 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3336 | case Legal: |
| 3337 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3338 | break; |
| 3339 | case Promote: |
| 3340 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3341 | break; |
| 3342 | case Expand: |
| 3343 | assert(0 && "not implemented"); |
| 3344 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3345 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3346 | |
| 3347 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3348 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3349 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3350 | DAG.getValueType(VT)); |
| 3351 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3352 | |
| 3353 | case ISD::UDIV: |
| 3354 | case ISD::UREM: |
| 3355 | // These operators require that their input be zero extended. |
| 3356 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3357 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3358 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3359 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3360 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3361 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3362 | break; |
| 3363 | |
| 3364 | case ISD::SHL: |
| 3365 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3366 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3367 | break; |
| 3368 | case ISD::SRA: |
| 3369 | // The input value must be properly sign extended. |
| 3370 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3371 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3372 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3373 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3374 | break; |
| 3375 | case ISD::SRL: |
| 3376 | // The input value must be properly zero extended. |
| 3377 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3378 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3379 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3380 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3381 | |
| 3382 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3383 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 3384 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3385 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 3386 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 3387 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 3388 | } else { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3389 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3390 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3391 | SV->getValue(), SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3392 | // Increment the pointer, VAList, to the next vaarg |
| 3393 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3394 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3395 | TLI.getPointerTy())); |
| 3396 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3397 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 3398 | SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3399 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3400 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3401 | } |
| 3402 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3403 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3404 | break; |
| 3405 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3406 | case ISD::LOAD: { |
| 3407 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 3408 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 3409 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 3410 | Result = DAG.getExtLoad(ExtType, NVT, |
| 3411 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 3412 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 3413 | LD->getLoadedVT()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3414 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3415 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3416 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3417 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3418 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3419 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3420 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3421 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3422 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3423 | case ISD::SELECT_CC: |
| 3424 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3425 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3426 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3427 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3428 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3429 | case ISD::BSWAP: |
| 3430 | Tmp1 = Node->getOperand(0); |
| 3431 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3432 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3433 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3434 | DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), |
| 3435 | TLI.getShiftAmountTy())); |
| 3436 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3437 | case ISD::CTPOP: |
| 3438 | case ISD::CTTZ: |
| 3439 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3440 | // Zero extend the argument |
| 3441 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3442 | // Perform the larger operation, then subtract if needed. |
| 3443 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3444 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3445 | case ISD::CTPOP: |
| 3446 | Result = Tmp1; |
| 3447 | break; |
| 3448 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3449 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3450 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3451 | DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3452 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3453 | DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3454 | break; |
| 3455 | case ISD::CTLZ: |
| 3456 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3457 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 3458 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3459 | getSizeInBits(VT), NVT)); |
| 3460 | break; |
| 3461 | } |
| 3462 | break; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3463 | case ISD::VEXTRACT_VECTOR_ELT: |
| 3464 | Result = PromoteOp(LowerVEXTRACT_VECTOR_ELT(Op)); |
| 3465 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3466 | case ISD::EXTRACT_VECTOR_ELT: |
| 3467 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3468 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3472 | |
| 3473 | // Make sure the result is itself legal. |
| 3474 | Result = LegalizeOp(Result); |
| 3475 | |
| 3476 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3477 | AddPromotedOperand(Op, Result); |
| 3478 | return Result; |
| 3479 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3480 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3481 | /// LowerVEXTRACT_VECTOR_ELT - Lower a VEXTRACT_VECTOR_ELT operation into a |
| 3482 | /// EXTRACT_VECTOR_ELT operation, to memory operations, or to scalar code based |
| 3483 | /// on the vector type. The return type of this matches the element type of the |
| 3484 | /// vector, which may not be legal for the target. |
| 3485 | SDOperand SelectionDAGLegalize::LowerVEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3486 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3487 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3488 | // lower to a store then an indexed load. |
| 3489 | SDOperand Vec = Op.getOperand(0); |
| 3490 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3491 | |
| 3492 | SDNode *InVal = Vec.Val; |
| 3493 | unsigned NumElems = cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 3494 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 3495 | |
| 3496 | // Figure out if there is a Packed type corresponding to this Vector |
| 3497 | // type. If so, convert to the packed type. |
| 3498 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3499 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 3500 | // Turn this into a packed extract_vector_elt operation. |
| 3501 | Vec = PackVectorOp(Vec, TVT); |
| 3502 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Op.getValueType(), Vec, Idx); |
| 3503 | } else if (NumElems == 1) { |
| 3504 | // This must be an access of the only element. Return it. |
| 3505 | return PackVectorOp(Vec, EVT); |
| 3506 | } else if (ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) { |
| 3507 | SDOperand Lo, Hi; |
| 3508 | SplitVectorOp(Vec, Lo, Hi); |
| 3509 | if (CIdx->getValue() < NumElems/2) { |
| 3510 | Vec = Lo; |
| 3511 | } else { |
| 3512 | Vec = Hi; |
| 3513 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3514 | } |
| 3515 | |
| 3516 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3517 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3518 | return LowerVEXTRACT_VECTOR_ELT(Op); |
| 3519 | } else { |
| 3520 | // Variable index case for extract element. |
| 3521 | // FIXME: IMPLEMENT STORE/LOAD lowering. Need alignment of stack slot!! |
| 3522 | assert(0 && "unimp!"); |
| 3523 | return SDOperand(); |
| 3524 | } |
| 3525 | } |
| 3526 | |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3527 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3528 | /// memory traffic. |
| 3529 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
| 3530 | SDOperand Vector = Op.getOperand(0); |
| 3531 | SDOperand Idx = Op.getOperand(1); |
| 3532 | |
| 3533 | // If the target doesn't support this, store the value to a temporary |
| 3534 | // stack slot, then LOAD the scalar element back out. |
| 3535 | SDOperand StackPtr = CreateStackTemporary(Vector.getValueType()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3536 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vector, StackPtr, NULL, 0); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3537 | |
| 3538 | // Add the offset to the index. |
| 3539 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3540 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3541 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3542 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3543 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3544 | return DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3547 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3548 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3549 | /// with condition CC on the current target. This usually involves legalizing |
| 3550 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3551 | /// there may be no choice but to create a new SetCC node to represent the |
| 3552 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3553 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3554 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3555 | SDOperand &RHS, |
| 3556 | SDOperand &CC) { |
| 3557 | SDOperand Tmp1, Tmp2, Result; |
| 3558 | |
| 3559 | switch (getTypeAction(LHS.getValueType())) { |
| 3560 | case Legal: |
| 3561 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3562 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3563 | break; |
| 3564 | case Promote: |
| 3565 | Tmp1 = PromoteOp(LHS); // LHS |
| 3566 | Tmp2 = PromoteOp(RHS); // RHS |
| 3567 | |
| 3568 | // If this is an FP compare, the operands have already been extended. |
| 3569 | if (MVT::isInteger(LHS.getValueType())) { |
| 3570 | MVT::ValueType VT = LHS.getValueType(); |
| 3571 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3572 | |
| 3573 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3574 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3575 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3576 | // it. |
| 3577 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3578 | default: assert(0 && "Unknown integer comparison!"); |
| 3579 | case ISD::SETEQ: |
| 3580 | case ISD::SETNE: |
| 3581 | case ISD::SETUGE: |
| 3582 | case ISD::SETUGT: |
| 3583 | case ISD::SETULE: |
| 3584 | case ISD::SETULT: |
| 3585 | // ALL of these operations will work if we either sign or zero extend |
| 3586 | // the operands (including the unsigned comparisons!). Zero extend is |
| 3587 | // usually a simpler/cheaper operation, so prefer it. |
| 3588 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3589 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 3590 | break; |
| 3591 | case ISD::SETGE: |
| 3592 | case ISD::SETGT: |
| 3593 | case ISD::SETLT: |
| 3594 | case ISD::SETLE: |
| 3595 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3596 | DAG.getValueType(VT)); |
| 3597 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3598 | DAG.getValueType(VT)); |
| 3599 | break; |
| 3600 | } |
| 3601 | } |
| 3602 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3603 | case Expand: { |
| 3604 | MVT::ValueType VT = LHS.getValueType(); |
| 3605 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 3606 | // Expand into one or more soft-fp libcall(s). |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3607 | RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3608 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3609 | case ISD::SETEQ: |
| 3610 | case ISD::SETOEQ: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3611 | LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3612 | break; |
| 3613 | case ISD::SETNE: |
| 3614 | case ISD::SETUNE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3615 | LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | case ISD::SETGE: |
| 3618 | case ISD::SETOGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3619 | LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3620 | break; |
| 3621 | case ISD::SETLT: |
| 3622 | case ISD::SETOLT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3623 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3624 | break; |
| 3625 | case ISD::SETLE: |
| 3626 | case ISD::SETOLE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3627 | LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3628 | break; |
| 3629 | case ISD::SETGT: |
| 3630 | case ISD::SETOGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3631 | LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3632 | break; |
| 3633 | case ISD::SETUO: |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3634 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
| 3635 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3636 | case ISD::SETO: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3637 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | default: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3640 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3641 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3642 | case ISD::SETONE: |
| 3643 | // SETONE = SETOLT | SETOGT |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3644 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3645 | // Fallthrough |
| 3646 | case ISD::SETUGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3647 | LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3648 | break; |
| 3649 | case ISD::SETUGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3650 | LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3651 | break; |
| 3652 | case ISD::SETULT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3653 | LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3654 | break; |
| 3655 | case ISD::SETULE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3656 | LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3657 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3658 | case ISD::SETUEQ: |
| 3659 | LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3660 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3661 | default: assert(0 && "Unsupported FP setcc!"); |
| 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3666 | Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3667 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 3668 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3669 | Tmp2 = DAG.getConstant(0, MVT::i32); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3670 | CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1)); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3671 | if (LC2 != RTLIB::UNKNOWN_LIBCALL) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3672 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3673 | LHS = ExpandLibCall(TLI.getLibcallName(LC2), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3674 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 3675 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3676 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2, |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3677 | DAG.getCondCode(TLI.getCmpLibcallCC(LC2))); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3678 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3679 | Tmp2 = SDOperand(); |
| 3680 | } |
| 3681 | LHS = Tmp1; |
| 3682 | RHS = Tmp2; |
| 3683 | return; |
| 3684 | } |
| 3685 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3686 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 3687 | ExpandOp(LHS, LHSLo, LHSHi); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3688 | ExpandOp(RHS, RHSLo, RHSHi); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3689 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3690 | case ISD::SETEQ: |
| 3691 | case ISD::SETNE: |
| 3692 | if (RHSLo == RHSHi) |
| 3693 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 3694 | if (RHSCST->isAllOnesValue()) { |
| 3695 | // Comparison to -1. |
| 3696 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 3697 | Tmp2 = RHSLo; |
| 3698 | break; |
| 3699 | } |
| 3700 | |
| 3701 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 3702 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 3703 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3704 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3705 | break; |
| 3706 | default: |
| 3707 | // If this is a comparison of the sign bit, just look at the top part. |
| 3708 | // X > -1, x < 0 |
| 3709 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 3710 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 3711 | CST->getValue() == 0) || // X < 0 |
| 3712 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 3713 | CST->isAllOnesValue())) { // X > -1 |
| 3714 | Tmp1 = LHSHi; |
| 3715 | Tmp2 = RHSHi; |
| 3716 | break; |
| 3717 | } |
| 3718 | |
| 3719 | // FIXME: This generated code sucks. |
| 3720 | ISD::CondCode LowCC; |
| 3721 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3722 | default: assert(0 && "Unknown integer setcc!"); |
| 3723 | case ISD::SETLT: |
| 3724 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 3725 | case ISD::SETGT: |
| 3726 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 3727 | case ISD::SETLE: |
| 3728 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 3729 | case ISD::SETGE: |
| 3730 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 3731 | } |
| 3732 | |
| 3733 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 3734 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 3735 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 3736 | |
| 3737 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 3738 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
| 3739 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 3740 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 3741 | Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 3742 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 3743 | Result, Tmp1, Tmp2)); |
| 3744 | Tmp1 = Result; |
Nate Begeman | da06e9e | 2006-02-01 19:05:15 +0000 | [diff] [blame] | 3745 | Tmp2 = SDOperand(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3746 | } |
| 3747 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3748 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3749 | LHS = Tmp1; |
| 3750 | RHS = Tmp2; |
| 3751 | } |
| 3752 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3753 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 3754 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 3755 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3756 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 3757 | SDOperand SrcOp) { |
| 3758 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3759 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3760 | |
| 3761 | // Emit a store to the stack slot. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3762 | SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3763 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3764 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3767 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 3768 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 3769 | // then load the whole vector back out. |
| 3770 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3771 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3772 | NULL, 0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3773 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
| 3776 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3777 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
| 3778 | /// support the operation, but do support the resultant packed vector type. |
| 3779 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 3780 | |
| 3781 | // 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] | 3782 | // 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] | 3783 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3784 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3785 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3786 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 3787 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3788 | bool isConstant = true; |
| 3789 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 3790 | SplatValue.getOpcode() != ISD::UNDEF) |
| 3791 | isConstant = false; |
| 3792 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3793 | for (unsigned i = 1; i < NumElems; ++i) { |
| 3794 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 3795 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3796 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3797 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3798 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3799 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3800 | |
| 3801 | // If this isn't a constant element or an undef, we can't use a constant |
| 3802 | // pool load. |
| 3803 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 3804 | V.getOpcode() != ISD::UNDEF) |
| 3805 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | if (isOnlyLowElement) { |
| 3809 | // If the low element is an undef too, then this whole things is an undef. |
| 3810 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 3811 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 3812 | // Otherwise, turn this into a scalar_to_vector node. |
| 3813 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3814 | Node->getOperand(0)); |
| 3815 | } |
| 3816 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3817 | // If all elements are constants, create a load from the constant pool. |
| 3818 | if (isConstant) { |
| 3819 | MVT::ValueType VT = Node->getValueType(0); |
| 3820 | const Type *OpNTy = |
| 3821 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 3822 | std::vector<Constant*> CV; |
| 3823 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 3824 | if (ConstantFPSDNode *V = |
| 3825 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
| 3826 | CV.push_back(ConstantFP::get(OpNTy, V->getValue())); |
| 3827 | } else if (ConstantSDNode *V = |
| 3828 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3829 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3830 | } else { |
| 3831 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 3832 | CV.push_back(UndefValue::get(OpNTy)); |
| 3833 | } |
| 3834 | } |
| 3835 | Constant *CP = ConstantPacked::get(CV); |
| 3836 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3837 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3840 | if (SplatValue.Val) { // Splat of one value? |
| 3841 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 3842 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3843 | MVT::getIntVectorWithNumElements(NumElems); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3844 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3845 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3846 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3847 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3848 | |
| 3849 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3850 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3851 | // Get the splatted value into the low element of a vector register. |
| 3852 | SDOperand LowValVec = |
| 3853 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 3854 | |
| 3855 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 3856 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 3857 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 3858 | SplatMask); |
| 3859 | } |
| 3860 | } |
| 3861 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3862 | // If there are only two unique elements, we may be able to turn this into a |
| 3863 | // vector shuffle. |
| 3864 | if (Values.size() == 2) { |
| 3865 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 3866 | MVT::ValueType MaskVT = |
| 3867 | MVT::getIntVectorWithNumElements(NumElems); |
| 3868 | std::vector<SDOperand> MaskVec(NumElems); |
| 3869 | unsigned i = 0; |
| 3870 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3871 | E = Values.end(); I != E; ++I) { |
| 3872 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 3873 | EE = I->second.end(); II != EE; ++II) |
| 3874 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorBaseType(MaskVT)); |
| 3875 | i += NumElems; |
| 3876 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3877 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3878 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3879 | |
| 3880 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3881 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 3882 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3883 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3884 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 3885 | E = Values.end(); I != E; ++I) { |
| 3886 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3887 | I->first); |
| 3888 | Ops.push_back(Op); |
| 3889 | } |
| 3890 | Ops.push_back(ShuffleMask); |
| 3891 | |
| 3892 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3893 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), |
| 3894 | &Ops[0], Ops.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3895 | } |
| 3896 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3897 | |
| 3898 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 3899 | // aligned object on the stack, store each element into it, then load |
| 3900 | // the result as a vector. |
| 3901 | MVT::ValueType VT = Node->getValueType(0); |
| 3902 | // Create the stack frame object. |
| 3903 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 3904 | |
| 3905 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3906 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3907 | unsigned TypeByteSize = |
| 3908 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3909 | // Store (in the right endianness) the elements to memory. |
| 3910 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3911 | // Ignore undef elements. |
| 3912 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 3913 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 3914 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3915 | |
| 3916 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 3917 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 3918 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3919 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3920 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | SDOperand StoreChain; |
| 3924 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3925 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 3926 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3927 | else |
| 3928 | StoreChain = DAG.getEntryNode(); |
| 3929 | |
| 3930 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3931 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 3935 | /// specified value type. |
| 3936 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 3937 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 3938 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 3939 | const Type *Ty = MVT::getTypeForValueType(VT); |
| 3940 | unsigned StackAlign = (unsigned)TLI.getTargetData()->getTypeAlignmentPref(Ty); |
| 3941 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3942 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 3943 | } |
| 3944 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3945 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 3946 | SDOperand Op, SDOperand Amt, |
| 3947 | SDOperand &Lo, SDOperand &Hi) { |
| 3948 | // Expand the subcomponents. |
| 3949 | SDOperand LHSL, LHSH; |
| 3950 | ExpandOp(Op, LHSL, LHSH); |
| 3951 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3952 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 3953 | MVT::ValueType VT = LHSL.getValueType(); |
| 3954 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 3955 | Hi = Lo.getValue(1); |
| 3956 | } |
| 3957 | |
| 3958 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3959 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 3960 | /// smaller elements. If we can't find a way that is more efficient than a |
| 3961 | /// libcall on this target, return false. Otherwise, return true with the |
| 3962 | /// low-parts expanded into Lo and Hi. |
| 3963 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 3964 | SDOperand &Lo, SDOperand &Hi) { |
| 3965 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 3966 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3967 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3968 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3969 | SDOperand ShAmt = LegalizeOp(Amt); |
| 3970 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 3971 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 3972 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 3973 | |
| 3974 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 3975 | // and are disabled. |
| 3976 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 3977 | unsigned Cst = CN->getValue(); |
| 3978 | // Expand the incoming operand to be shifted, so that we have its parts |
| 3979 | SDOperand InL, InH; |
| 3980 | ExpandOp(Op, InL, InH); |
| 3981 | switch(Opc) { |
| 3982 | case ISD::SHL: |
| 3983 | if (Cst > VTBits) { |
| 3984 | Lo = DAG.getConstant(0, NVT); |
| 3985 | Hi = DAG.getConstant(0, NVT); |
| 3986 | } else if (Cst > NVTBits) { |
| 3987 | Lo = DAG.getConstant(0, NVT); |
| 3988 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3989 | } else if (Cst == NVTBits) { |
| 3990 | Lo = DAG.getConstant(0, NVT); |
| 3991 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3992 | } else { |
| 3993 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 3994 | Hi = DAG.getNode(ISD::OR, NVT, |
| 3995 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 3996 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3997 | } |
| 3998 | return true; |
| 3999 | case ISD::SRL: |
| 4000 | if (Cst > VTBits) { |
| 4001 | Lo = DAG.getConstant(0, NVT); |
| 4002 | Hi = DAG.getConstant(0, NVT); |
| 4003 | } else if (Cst > NVTBits) { |
| 4004 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 4005 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4006 | } else if (Cst == NVTBits) { |
| 4007 | Lo = InH; |
| 4008 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4009 | } else { |
| 4010 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4011 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4012 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4013 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4014 | } |
| 4015 | return true; |
| 4016 | case ISD::SRA: |
| 4017 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4018 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4019 | DAG.getConstant(NVTBits-1, ShTy)); |
| 4020 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4021 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4022 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4023 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4024 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4025 | } else if (Cst == NVTBits) { |
| 4026 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4027 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4028 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4029 | } else { |
| 4030 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4031 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4032 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4033 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4034 | } |
| 4035 | return true; |
| 4036 | } |
| 4037 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 4038 | |
| 4039 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 4040 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
| 4041 | uint64_t Mask = NVTBits, KnownZero, KnownOne; |
| 4042 | TLI.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
| 4043 | |
| 4044 | // If we know that the high bit of the shift amount is one, then we can do |
| 4045 | // this as a couple of simple shifts. |
| 4046 | if (KnownOne & Mask) { |
| 4047 | // Mask out the high bit, which we know is set. |
| 4048 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
| 4049 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4050 | |
| 4051 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4052 | SDOperand InL, InH; |
| 4053 | ExpandOp(Op, InL, InH); |
| 4054 | switch(Opc) { |
| 4055 | case ISD::SHL: |
| 4056 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 4057 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 4058 | return true; |
| 4059 | case ISD::SRL: |
| 4060 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 4061 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 4062 | return true; |
| 4063 | case ISD::SRA: |
| 4064 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 4065 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4066 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 4067 | return true; |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | // If we know that the high bit of the shift amount is zero, then we can do |
| 4072 | // this as a couple of simple shifts. |
| 4073 | if (KnownZero & Mask) { |
| 4074 | // Compute 32-amt. |
| 4075 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 4076 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 4077 | Amt); |
| 4078 | |
| 4079 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4080 | SDOperand InL, InH; |
| 4081 | ExpandOp(Op, InL, InH); |
| 4082 | switch(Opc) { |
| 4083 | case ISD::SHL: |
| 4084 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 4085 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4086 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 4087 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 4088 | return true; |
| 4089 | case ISD::SRL: |
| 4090 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 4091 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4092 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4093 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4094 | return true; |
| 4095 | case ISD::SRA: |
| 4096 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 4097 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4098 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4099 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4100 | return true; |
| 4101 | } |
| 4102 | } |
| 4103 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4104 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4105 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4106 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4107 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4108 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 4109 | // does not fit into a register, return the lo part and set the hi part to the |
| 4110 | // by-reg argument. If it does fit into a single register, return the result |
| 4111 | // and leave the Hi part unset. |
| 4112 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4113 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4114 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 4115 | // The input chain to this libcall is the entry node of the function. |
| 4116 | // Legalizing the call will automatically add the previous call to the |
| 4117 | // dependence. |
| 4118 | SDOperand InChain = DAG.getEntryNode(); |
| 4119 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4120 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4121 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4122 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4123 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 4124 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4125 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame^] | 4126 | Entry.isSigned = isSigned; Entry.isInReg = false; Entry.isSRet = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4127 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4128 | } |
| 4129 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4130 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4131 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4132 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4133 | std::pair<SDOperand,SDOperand> CallInfo = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4134 | TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 4135 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 4136 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4137 | // Legalize the call sequence, starting with the chain. This will advance |
| 4138 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 4139 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 4140 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4141 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4142 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4143 | default: assert(0 && "Unknown thing"); |
| 4144 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4145 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4146 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4147 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4148 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4149 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4150 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4151 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4155 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 4156 | /// destination type is legal. |
| 4157 | SDOperand SelectionDAGLegalize:: |
| 4158 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4159 | assert(isTypeLegal(DestTy) && "Destination type is not legal!"); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4160 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 4161 | "This is not an expansion!"); |
| 4162 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 4163 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4164 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4165 | assert(Source.getValueType() == MVT::i64 && |
| 4166 | "This only works for 64-bit -> FP"); |
| 4167 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 4168 | // incoming integer is set. To handle this, we dynamically test to see if |
| 4169 | // it is set, and, if so, add a fudge factor. |
| 4170 | SDOperand Lo, Hi; |
| 4171 | ExpandOp(Source, Lo, Hi); |
| 4172 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 4173 | // If this is unsigned, and not supported, first perform the conversion to |
| 4174 | // signed, then adjust the result if the sign bit is set. |
| 4175 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 4176 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 4177 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4178 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 4179 | DAG.getConstant(0, Hi.getValueType()), |
| 4180 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4181 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4182 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4183 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 4184 | uint64_t FF = 0x5f800000ULL; |
| 4185 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4186 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4187 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 4188 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4189 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4190 | SDOperand FudgeInReg; |
| 4191 | if (DestTy == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4192 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4193 | else { |
| 4194 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 4195 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4196 | CPIdx, NULL, 0, MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4197 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 4198 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4199 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4200 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4201 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 4202 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 4203 | default: assert(0 && "This action not implemented for this operation!"); |
| 4204 | case TargetLowering::Legal: |
| 4205 | case TargetLowering::Expand: |
| 4206 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4207 | case TargetLowering::Custom: { |
| 4208 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 4209 | Source), DAG); |
| 4210 | if (NV.Val) |
| 4211 | return LegalizeOp(NV); |
| 4212 | break; // The target decided this was legal after all |
| 4213 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 4216 | // Expand the source, then glue it back together for the call. We must expand |
| 4217 | // the source in case it is shared (this pass of legalize must traverse it). |
| 4218 | SDOperand SrcLo, SrcHi; |
| 4219 | ExpandOp(Source, SrcLo, SrcHi); |
| 4220 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 4221 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4222 | RTLIB::Libcall LC; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4223 | if (DestTy == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4224 | LC = RTLIB::SINTTOFP_I64_F32; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4225 | else { |
| 4226 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4227 | LC = RTLIB::SINTTOFP_I64_F64; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4228 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4229 | |
| 4230 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 4231 | SDOperand UnusedHiPart; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4232 | return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned, |
| 4233 | UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4234 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4235 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4236 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 4237 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 4238 | /// we expand it. At this point, we know that the result and operand types are |
| 4239 | /// legal for the target. |
| 4240 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 4241 | SDOperand Op0, |
| 4242 | MVT::ValueType DestVT) { |
| 4243 | if (Op0.getValueType() == MVT::i32) { |
| 4244 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 4245 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4246 | // get the stack frame index of a 8 byte buffer, pessimistically aligned |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4247 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4248 | const Type *F64Type = MVT::getTypeForValueType(MVT::f64); |
| 4249 | unsigned StackAlign = |
| 4250 | (unsigned)TLI.getTargetData()->getTypeAlignmentPref(F64Type); |
| 4251 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4252 | // get address of 8 byte buffer |
| 4253 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4254 | // word offset constant for Hi/Lo address computation |
| 4255 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 4256 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 4257 | SDOperand Hi = StackSlot; |
| 4258 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 4259 | if (TLI.isLittleEndian()) |
| 4260 | std::swap(Hi, Lo); |
| 4261 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4262 | // if signed map to unsigned space |
| 4263 | SDOperand Op0Mapped; |
| 4264 | if (isSigned) { |
| 4265 | // constant used to invert sign bit (signed to unsigned mapping) |
| 4266 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 4267 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 4268 | } else { |
| 4269 | Op0Mapped = Op0; |
| 4270 | } |
| 4271 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4272 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4273 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4274 | // initial hi portion of constructed double |
| 4275 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 4276 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4277 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4278 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4279 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4280 | // FP constant to bias correct the final result |
| 4281 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 4282 | BitsToDouble(0x4330000080000000ULL) |
| 4283 | : BitsToDouble(0x4330000000000000ULL), |
| 4284 | MVT::f64); |
| 4285 | // subtract the bias |
| 4286 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 4287 | // final result |
| 4288 | SDOperand Result; |
| 4289 | // handle final rounding |
| 4290 | if (DestVT == MVT::f64) { |
| 4291 | // do nothing |
| 4292 | Result = Sub; |
| 4293 | } else { |
| 4294 | // if f32 then cast to f32 |
| 4295 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 4296 | } |
| 4297 | return Result; |
| 4298 | } |
| 4299 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 4300 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 4301 | |
| 4302 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 4303 | DAG.getConstant(0, Op0.getValueType()), |
| 4304 | ISD::SETLT); |
| 4305 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4306 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4307 | SignSet, Four, Zero); |
| 4308 | |
| 4309 | // If the sign bit of the integer is set, the large number will be treated |
| 4310 | // as a negative number. To counteract this, the dynamic code adds an |
| 4311 | // offset depending on the data type. |
| 4312 | uint64_t FF; |
| 4313 | switch (Op0.getValueType()) { |
| 4314 | default: assert(0 && "Unsupported integer type!"); |
| 4315 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 4316 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 4317 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 4318 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 4319 | } |
| 4320 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4321 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4322 | |
| 4323 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 4324 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4325 | SDOperand FudgeInReg; |
| 4326 | if (DestVT == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4327 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4328 | else { |
| 4329 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 4330 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 4331 | DAG.getEntryNode(), CPIdx, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4332 | NULL, 0, MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 4336 | } |
| 4337 | |
| 4338 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 4339 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 4340 | /// we promote it. At this point, we know that the result and operand types are |
| 4341 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 4342 | /// operation that takes a larger input. |
| 4343 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 4344 | MVT::ValueType DestVT, |
| 4345 | bool isSigned) { |
| 4346 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 4347 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 4348 | |
| 4349 | unsigned OpToUse = 0; |
| 4350 | |
| 4351 | // Scan for the appropriate larger type to use. |
| 4352 | while (1) { |
| 4353 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 4354 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 4355 | |
| 4356 | // If the target supports SINT_TO_FP of this type, use it. |
| 4357 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 4358 | default: break; |
| 4359 | case TargetLowering::Legal: |
| 4360 | if (!TLI.isTypeLegal(NewInTy)) |
| 4361 | break; // Can't use this datatype. |
| 4362 | // FALL THROUGH. |
| 4363 | case TargetLowering::Custom: |
| 4364 | OpToUse = ISD::SINT_TO_FP; |
| 4365 | break; |
| 4366 | } |
| 4367 | if (OpToUse) break; |
| 4368 | if (isSigned) continue; |
| 4369 | |
| 4370 | // If the target supports UINT_TO_FP of this type, use it. |
| 4371 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 4372 | default: break; |
| 4373 | case TargetLowering::Legal: |
| 4374 | if (!TLI.isTypeLegal(NewInTy)) |
| 4375 | break; // Can't use this datatype. |
| 4376 | // FALL THROUGH. |
| 4377 | case TargetLowering::Custom: |
| 4378 | OpToUse = ISD::UINT_TO_FP; |
| 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. Zero extend our input to the |
| 4387 | // desired type then run the operation on it. |
| 4388 | return DAG.getNode(OpToUse, DestVT, |
| 4389 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 4390 | NewInTy, LegalOp)); |
| 4391 | } |
| 4392 | |
| 4393 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 4394 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 4395 | /// we promote it. At this point, we know that the result and operand types are |
| 4396 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 4397 | /// operation that returns a larger result. |
| 4398 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 4399 | MVT::ValueType DestVT, |
| 4400 | bool isSigned) { |
| 4401 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 4402 | MVT::ValueType NewOutTy = DestVT; |
| 4403 | |
| 4404 | unsigned OpToUse = 0; |
| 4405 | |
| 4406 | // Scan for the appropriate larger type to use. |
| 4407 | while (1) { |
| 4408 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 4409 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 4410 | |
| 4411 | // If the target supports FP_TO_SINT returning this type, use it. |
| 4412 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 4413 | default: break; |
| 4414 | case TargetLowering::Legal: |
| 4415 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4416 | break; // Can't use this datatype. |
| 4417 | // FALL THROUGH. |
| 4418 | case TargetLowering::Custom: |
| 4419 | OpToUse = ISD::FP_TO_SINT; |
| 4420 | break; |
| 4421 | } |
| 4422 | if (OpToUse) break; |
| 4423 | |
| 4424 | // If the target supports FP_TO_UINT of this type, use it. |
| 4425 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 4426 | default: break; |
| 4427 | case TargetLowering::Legal: |
| 4428 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4429 | break; // Can't use this datatype. |
| 4430 | // FALL THROUGH. |
| 4431 | case TargetLowering::Custom: |
| 4432 | OpToUse = ISD::FP_TO_UINT; |
| 4433 | break; |
| 4434 | } |
| 4435 | if (OpToUse) break; |
| 4436 | |
| 4437 | // Otherwise, try a larger type. |
| 4438 | } |
| 4439 | |
| 4440 | // Okay, we found the operation and type to use. Truncate the result of the |
| 4441 | // extended FP_TO_*INT operation to the desired size. |
| 4442 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 4443 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 4444 | } |
| 4445 | |
| 4446 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 4447 | /// |
| 4448 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 4449 | MVT::ValueType VT = Op.getValueType(); |
| 4450 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 4451 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 4452 | switch (VT) { |
| 4453 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 4454 | case MVT::i16: |
| 4455 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4456 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4457 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 4458 | case MVT::i32: |
| 4459 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4460 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4461 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4462 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4463 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 4464 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 4465 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4466 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4467 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4468 | case MVT::i64: |
| 4469 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4470 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4471 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4472 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4473 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4474 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4475 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4476 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4477 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 4478 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 4479 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 4480 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 4481 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 4482 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 4483 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 4484 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 4485 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4486 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4487 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 4488 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4489 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 4494 | /// |
| 4495 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 4496 | switch (Opc) { |
| 4497 | default: assert(0 && "Cannot expand this yet!"); |
| 4498 | case ISD::CTPOP: { |
| 4499 | static const uint64_t mask[6] = { |
| 4500 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 4501 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 4502 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 4503 | }; |
| 4504 | MVT::ValueType VT = Op.getValueType(); |
| 4505 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4506 | unsigned len = getSizeInBits(VT); |
| 4507 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4508 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 4509 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 4510 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4511 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 4512 | DAG.getNode(ISD::AND, VT, |
| 4513 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 4514 | } |
| 4515 | return Op; |
| 4516 | } |
| 4517 | case ISD::CTLZ: { |
| 4518 | // for now, we do this: |
| 4519 | // x = x | (x >> 1); |
| 4520 | // x = x | (x >> 2); |
| 4521 | // ... |
| 4522 | // x = x | (x >>16); |
| 4523 | // x = x | (x >>32); // for 64-bit input |
| 4524 | // return popcount(~x); |
| 4525 | // |
| 4526 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 4527 | MVT::ValueType VT = Op.getValueType(); |
| 4528 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 4529 | unsigned len = getSizeInBits(VT); |
| 4530 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4531 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4532 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4533 | } |
| 4534 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4535 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4536 | } |
| 4537 | case ISD::CTTZ: { |
| 4538 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4539 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4540 | // { return 32 - nlz(~x & (x-1)); } |
| 4541 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 4542 | MVT::ValueType VT = Op.getValueType(); |
| 4543 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 4544 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 4545 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 4546 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 4547 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 4548 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 4549 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 4550 | return DAG.getNode(ISD::SUB, VT, |
| 4551 | DAG.getConstant(getSizeInBits(VT), VT), |
| 4552 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 4553 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 4554 | } |
| 4555 | } |
| 4556 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4557 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4558 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 4559 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 4560 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 4561 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 4562 | /// Lo/Hi values are returned. |
| 4563 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 4564 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4565 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4566 | SDNode *Node = Op.Val; |
| 4567 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4568 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
| 4569 | VT == MVT::Vector) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4570 | "Cannot expand to FP value or to larger int value!"); |
| 4571 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4572 | // See if we already expanded it. |
| 4573 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4574 | = ExpandedNodes.find(Op); |
| 4575 | if (I != ExpandedNodes.end()) { |
| 4576 | Lo = I->second.first; |
| 4577 | Hi = I->second.second; |
| 4578 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4579 | } |
| 4580 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4581 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4582 | case ISD::CopyFromReg: |
| 4583 | assert(0 && "CopyFromReg must be legal!"); |
| 4584 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4585 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 4586 | cerr << "NODE: "; Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4587 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4588 | assert(0 && "Do not know how to expand this operator!"); |
| 4589 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4590 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 4591 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4592 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 4593 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4594 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4595 | case ISD::Constant: { |
| 4596 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 4597 | Lo = DAG.getConstant(Cst, NVT); |
| 4598 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 4599 | break; |
| 4600 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4601 | case ISD::ConstantFP: { |
| 4602 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 4603 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4604 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 4605 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4606 | break; |
| 4607 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4608 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4609 | // Return the operands. |
| 4610 | Lo = Node->getOperand(0); |
| 4611 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4612 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4613 | |
| 4614 | case ISD::SIGN_EXTEND_INREG: |
| 4615 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 4616 | // sext_inreg the low part if needed. |
| 4617 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 4618 | |
| 4619 | // The high part gets the sign extension from the lo-part. This handles |
| 4620 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4621 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4622 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 4623 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4624 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4625 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4626 | case ISD::BSWAP: { |
| 4627 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4628 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 4629 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 4630 | Lo = TempLo; |
| 4631 | break; |
| 4632 | } |
| 4633 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4634 | case ISD::CTPOP: |
| 4635 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 4636 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 4637 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 4638 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4639 | Hi = DAG.getConstant(0, NVT); |
| 4640 | break; |
| 4641 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4642 | case ISD::CTLZ: { |
| 4643 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4644 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4645 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4646 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4647 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 4648 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4649 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 4650 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 4651 | |
| 4652 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 4653 | Hi = DAG.getConstant(0, NVT); |
| 4654 | break; |
| 4655 | } |
| 4656 | |
| 4657 | case ISD::CTTZ: { |
| 4658 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4659 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4660 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4661 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4662 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 4663 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4664 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 4665 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 4666 | |
| 4667 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 4668 | Hi = DAG.getConstant(0, NVT); |
| 4669 | break; |
| 4670 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4671 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4672 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4673 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4674 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4675 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 4676 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 4677 | |
| 4678 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4679 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4680 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 4681 | if (!TLI.isLittleEndian()) |
| 4682 | std::swap(Lo, Hi); |
| 4683 | break; |
| 4684 | } |
| 4685 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4686 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4687 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 4688 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 4689 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 4690 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4691 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4692 | if (ExtType == ISD::NON_EXTLOAD) { |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 4693 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset()); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4694 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4695 | // f32->i32 or f64->i64 one to one expansion. |
| 4696 | // Remember that we legalized the chain. |
| 4697 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4698 | // Recursively expand the new load. |
| 4699 | if (getTypeAction(NVT) == Expand) |
| 4700 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4701 | break; |
| 4702 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 4703 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4704 | // Increment the pointer to the other half. |
| 4705 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 4706 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4707 | getIntPtrConstant(IncrementSize)); |
| 4708 | // FIXME: This creates a bogus srcvalue! |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 4709 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(),LD->getSrcValueOffset()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4710 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4711 | // Build a factor node to remember that this load is independent of the |
| 4712 | // other one. |
| 4713 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4714 | Hi.getValue(1)); |
| 4715 | |
| 4716 | // Remember that we legalized the chain. |
| 4717 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 4718 | if (!TLI.isLittleEndian()) |
| 4719 | std::swap(Lo, Hi); |
| 4720 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 4721 | MVT::ValueType EVT = LD->getLoadedVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 4722 | |
| 4723 | if (VT == MVT::f64 && EVT == MVT::f32) { |
| 4724 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 4725 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
| 4726 | LD->getSrcValueOffset()); |
| 4727 | // Remember that we legalized the chain. |
| 4728 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 4729 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 4730 | break; |
| 4731 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4732 | |
| 4733 | if (EVT == NVT) |
| 4734 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
| 4735 | LD->getSrcValueOffset()); |
| 4736 | else |
| 4737 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
| 4738 | LD->getSrcValueOffset(), EVT); |
| 4739 | |
| 4740 | // Remember that we legalized the chain. |
| 4741 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 4742 | |
| 4743 | if (ExtType == ISD::SEXTLOAD) { |
| 4744 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 4745 | // lo part. |
| 4746 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 4747 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4748 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 4749 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 4750 | // The high part is just a zero. |
| 4751 | Hi = DAG.getConstant(0, NVT); |
| 4752 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 4753 | // The high part is undefined. |
| 4754 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4755 | } |
| 4756 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4757 | break; |
| 4758 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4759 | case ISD::AND: |
| 4760 | case ISD::OR: |
| 4761 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 4762 | SDOperand LL, LH, RL, RH; |
| 4763 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4764 | ExpandOp(Node->getOperand(1), RL, RH); |
| 4765 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 4766 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 4767 | break; |
| 4768 | } |
| 4769 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4770 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4771 | ExpandOp(Node->getOperand(1), LL, LH); |
| 4772 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4773 | if (getTypeAction(NVT) == Expand) |
| 4774 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4775 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4776 | if (VT != MVT::f32) |
| 4777 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4778 | break; |
| 4779 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4780 | case ISD::SELECT_CC: { |
| 4781 | SDOperand TL, TH, FL, FH; |
| 4782 | ExpandOp(Node->getOperand(2), TL, TH); |
| 4783 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4784 | if (getTypeAction(NVT) == Expand) |
| 4785 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4786 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4787 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4788 | if (VT != MVT::f32) |
| 4789 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4790 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4791 | break; |
| 4792 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4793 | case ISD::ANY_EXTEND: |
| 4794 | // The low part is any extension of the input (which degenerates to a copy). |
| 4795 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 4796 | // The high part is undefined. |
| 4797 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4798 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4799 | case ISD::SIGN_EXTEND: { |
| 4800 | // The low part is just a sign extension of the input (which degenerates to |
| 4801 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4802 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4803 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4804 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4805 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 4806 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4807 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4808 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4809 | break; |
| 4810 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4811 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4812 | // The low part is just a zero extension of the input (which degenerates to |
| 4813 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4814 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4816 | // The high part is just a zero. |
| 4817 | Hi = DAG.getConstant(0, NVT); |
| 4818 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4819 | |
| 4820 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4821 | SDOperand Tmp; |
| 4822 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 4823 | // If the target wants to, allow it to lower this itself. |
| 4824 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4825 | case Expand: assert(0 && "cannot expand FP!"); |
| 4826 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 4827 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 4828 | } |
| 4829 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 4830 | } |
| 4831 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4832 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 4833 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4834 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4835 | if (getTypeAction(NVT) == Expand) |
| 4836 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 4837 | break; |
| 4838 | } |
| 4839 | |
| 4840 | // If source operand will be expanded to the same type as VT, i.e. |
| 4841 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 4842 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 4843 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 4844 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4845 | break; |
| 4846 | } |
| 4847 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4848 | // Turn this into a load/store pair by default. |
| 4849 | if (Tmp.Val == 0) |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 4850 | Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0)); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4851 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4852 | ExpandOp(Tmp, Lo, Hi); |
| 4853 | break; |
| 4854 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4855 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4856 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4857 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 4858 | TargetLowering::Custom && |
| 4859 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4860 | Lo = TLI.LowerOperation(Op, DAG); |
| 4861 | assert(Lo.Val && "Node must be custom expanded!"); |
| 4862 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 4863 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4864 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 4865 | break; |
| 4866 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4867 | // These operators cannot be expanded directly, emit them as calls to |
| 4868 | // library functions. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4869 | case ISD::FP_TO_SINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4870 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4871 | SDOperand Op; |
| 4872 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4873 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4874 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4875 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4876 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4877 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 4878 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 4879 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4880 | // Now that the custom expander is done, expand the result, which is still |
| 4881 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4882 | if (Op.Val) { |
| 4883 | ExpandOp(Op, Lo, Hi); |
| 4884 | break; |
| 4885 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4886 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4887 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4888 | RTLIB::Libcall LC; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4889 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4890 | LC = RTLIB::FPTOSINT_F32_I64; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4891 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4892 | LC = RTLIB::FPTOSINT_F64_I64; |
| 4893 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 4894 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4895 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4896 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4897 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4898 | case ISD::FP_TO_UINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4899 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4900 | SDOperand Op; |
| 4901 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4902 | case Expand: assert(0 && "cannot expand FP!"); |
| 4903 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 4904 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 4905 | } |
| 4906 | |
| 4907 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 4908 | |
| 4909 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4910 | if (Op.Val) { |
| 4911 | ExpandOp(Op, Lo, Hi); |
| 4912 | break; |
| 4913 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 4914 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 4915 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4916 | RTLIB::Libcall LC; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4917 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4918 | LC = RTLIB::FPTOUINT_F32_I64; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4919 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4920 | LC = RTLIB::FPTOUINT_F64_I64; |
| 4921 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 4922 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4923 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4924 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 4925 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4926 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4927 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4928 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4929 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4930 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4931 | Op = TLI.LowerOperation(Op, DAG); |
| 4932 | if (Op.Val) { |
| 4933 | // Now that the custom expander is done, expand the result, which is |
| 4934 | // still VT. |
| 4935 | ExpandOp(Op, Lo, Hi); |
| 4936 | break; |
| 4937 | } |
| 4938 | } |
| 4939 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 4940 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 4941 | // this X << 1 as X+X. |
| 4942 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
| 4943 | if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
| 4944 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 4945 | SDOperand LoOps[2], HiOps[3]; |
| 4946 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 4947 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 4948 | LoOps[1] = LoOps[0]; |
| 4949 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 4950 | |
| 4951 | HiOps[1] = HiOps[0]; |
| 4952 | HiOps[2] = Lo.getValue(1); |
| 4953 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 4954 | break; |
| 4955 | } |
| 4956 | } |
| 4957 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4958 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4959 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4960 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4961 | |
| 4962 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4963 | TargetLowering::LegalizeAction Action = |
| 4964 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 4965 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4966 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4967 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4968 | break; |
| 4969 | } |
| 4970 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4971 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4972 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node, |
| 4973 | false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4974 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4975 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4976 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4977 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4978 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4979 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4980 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4981 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 4982 | Op = TLI.LowerOperation(Op, DAG); |
| 4983 | if (Op.Val) { |
| 4984 | // Now that the custom expander is done, expand the result, which is |
| 4985 | // still VT. |
| 4986 | ExpandOp(Op, Lo, Hi); |
| 4987 | break; |
| 4988 | } |
| 4989 | } |
| 4990 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4991 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4992 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4993 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 4994 | |
| 4995 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 4996 | TargetLowering::LegalizeAction Action = |
| 4997 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 4998 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 4999 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5000 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5001 | break; |
| 5002 | } |
| 5003 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5004 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5005 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node, |
| 5006 | true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5007 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5011 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5012 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5013 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5014 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5015 | Op = TLI.LowerOperation(Op, DAG); |
| 5016 | if (Op.Val) { |
| 5017 | // Now that the custom expander is done, expand the result, which is |
| 5018 | // still VT. |
| 5019 | ExpandOp(Op, Lo, Hi); |
| 5020 | break; |
| 5021 | } |
| 5022 | } |
| 5023 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5024 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5025 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5026 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5027 | |
| 5028 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5029 | TargetLowering::LegalizeAction Action = |
| 5030 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 5031 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5032 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5033 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5034 | break; |
| 5035 | } |
| 5036 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5037 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5038 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node, |
| 5039 | false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5040 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5041 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5042 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5043 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5044 | case ISD::SUB: { |
| 5045 | // If the target wants to custom expand this, let them. |
| 5046 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 5047 | TargetLowering::Custom) { |
| 5048 | Op = TLI.LowerOperation(Op, DAG); |
| 5049 | if (Op.Val) { |
| 5050 | ExpandOp(Op, Lo, Hi); |
| 5051 | break; |
| 5052 | } |
| 5053 | } |
| 5054 | |
| 5055 | // Expand the subcomponents. |
| 5056 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5057 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5058 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5059 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5060 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5061 | LoOps[0] = LHSL; |
| 5062 | LoOps[1] = RHSL; |
| 5063 | HiOps[0] = LHSH; |
| 5064 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5065 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5066 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5067 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5068 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5069 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5070 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5071 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5072 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5073 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 5074 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5075 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5076 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5077 | // If the target wants to custom expand this, let them. |
| 5078 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 5079 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 5080 | if (New.Val) { |
| 5081 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5082 | break; |
| 5083 | } |
| 5084 | } |
| 5085 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5086 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 5087 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5088 | if (HasMULHS || HasMULHU) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5089 | SDOperand LL, LH, RL, RH; |
| 5090 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5091 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5092 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 5093 | // FIXME: Move this to the dag combiner. |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5094 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 5095 | // extended the sign bit of the low half through the upper half, and if so |
| 5096 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 5097 | // i64 x i64 multiply. |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5098 | if (HasMULHS && |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5099 | // is RH an extension of the sign bit of RL? |
| 5100 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 5101 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 5102 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 5103 | // is LH an extension of the sign bit of LL? |
| 5104 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 5105 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 5106 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5107 | // Low part: |
| 5108 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5109 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5110 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5111 | break; |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5112 | } else if (HasMULHU) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5113 | // Low part: |
| 5114 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5115 | |
| 5116 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5117 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 5118 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 5119 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 5120 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 5121 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5122 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5123 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5124 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5125 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5126 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node, |
| 5127 | false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5128 | break; |
| 5129 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5130 | case ISD::SDIV: |
| 5131 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi); |
| 5132 | break; |
| 5133 | case ISD::UDIV: |
| 5134 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi); |
| 5135 | break; |
| 5136 | case ISD::SREM: |
| 5137 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi); |
| 5138 | break; |
| 5139 | case ISD::UREM: |
| 5140 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi); |
| 5141 | break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 5142 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5143 | case ISD::FADD: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5144 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5145 | ? RTLIB::ADD_F32 : RTLIB::ADD_F64), |
| 5146 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5147 | break; |
| 5148 | case ISD::FSUB: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5149 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5150 | ? RTLIB::SUB_F32 : RTLIB::SUB_F64), |
| 5151 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5152 | break; |
| 5153 | case ISD::FMUL: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5154 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5155 | ? RTLIB::MUL_F32 : RTLIB::MUL_F64), |
| 5156 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5157 | break; |
| 5158 | case ISD::FDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5159 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5160 | ? RTLIB::DIV_F32 : RTLIB::DIV_F64), |
| 5161 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5162 | break; |
| 5163 | case ISD::FP_EXTEND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5164 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5165 | break; |
| 5166 | case ISD::FP_ROUND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5167 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5168 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5169 | case ISD::FSQRT: |
| 5170 | case ISD::FSIN: |
| 5171 | case ISD::FCOS: { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5172 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5173 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5174 | case ISD::FSQRT: |
| 5175 | LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64; |
| 5176 | break; |
| 5177 | case ISD::FSIN: |
| 5178 | LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 5179 | break; |
| 5180 | case ISD::FCOS: |
| 5181 | LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 5182 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5183 | default: assert(0 && "Unreachable!"); |
| 5184 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5185 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5186 | break; |
| 5187 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 5188 | case ISD::FABS: { |
| 5189 | SDOperand Mask = (VT == MVT::f64) |
| 5190 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 5191 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 5192 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5193 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5194 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 5195 | if (getTypeAction(NVT) == Expand) |
| 5196 | ExpandOp(Lo, Lo, Hi); |
| 5197 | break; |
| 5198 | } |
| 5199 | case ISD::FNEG: { |
| 5200 | SDOperand Mask = (VT == MVT::f64) |
| 5201 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 5202 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 5203 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5204 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5205 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 5206 | if (getTypeAction(NVT) == Expand) |
| 5207 | ExpandOp(Lo, Lo, Hi); |
| 5208 | break; |
| 5209 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 5210 | case ISD::FCOPYSIGN: { |
| 5211 | Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 5212 | if (getTypeAction(NVT) == Expand) |
| 5213 | ExpandOp(Lo, Lo, Hi); |
| 5214 | break; |
| 5215 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5216 | case ISD::SINT_TO_FP: |
| 5217 | case ISD::UINT_TO_FP: { |
| 5218 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 5219 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5220 | RTLIB::Libcall LC; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5221 | if (Node->getOperand(0).getValueType() == MVT::i64) { |
| 5222 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5223 | LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5224 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5225 | LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5226 | } else { |
| 5227 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5228 | LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5229 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5230 | LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5231 | } |
| 5232 | |
| 5233 | // Promote the operand if needed. |
| 5234 | if (getTypeAction(SrcVT) == Promote) { |
| 5235 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 5236 | Tmp = isSigned |
| 5237 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 5238 | DAG.getValueType(SrcVT)) |
| 5239 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 5240 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 5241 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5242 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi); |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5243 | break; |
| 5244 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5245 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5246 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5247 | // Make sure the resultant values have been legalized themselves, unless this |
| 5248 | // is a type that requires multi-step expansion. |
| 5249 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 5250 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5251 | if (Hi.Val) |
| 5252 | // Don't legalize the high part if it is expanded to a single node. |
| 5253 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5254 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5255 | |
| 5256 | // Remember in a map if the values will be reused later. |
| 5257 | bool isNew = |
| 5258 | ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 5259 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5262 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 5263 | /// two smaller values of MVT::Vector type. |
| 5264 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 5265 | SDOperand &Hi) { |
| 5266 | assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); |
| 5267 | SDNode *Node = Op.Val; |
| 5268 | unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); |
| 5269 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 5270 | unsigned NewNumElts = NumElements/2; |
| 5271 | SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); |
| 5272 | SDOperand TypeNode = *(Node->op_end()-1); |
| 5273 | |
| 5274 | // See if we already split it. |
| 5275 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 5276 | = SplitNodes.find(Op); |
| 5277 | if (I != SplitNodes.end()) { |
| 5278 | Lo = I->second.first; |
| 5279 | Hi = I->second.second; |
| 5280 | return; |
| 5281 | } |
| 5282 | |
| 5283 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5284 | default: |
| 5285 | #ifndef NDEBUG |
| 5286 | Node->dump(); |
| 5287 | #endif |
| 5288 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5289 | case ISD::VBUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5290 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5291 | Node->op_begin()+NewNumElts); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5292 | LoOps.push_back(NewNumEltsNode); |
| 5293 | LoOps.push_back(TypeNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5294 | Lo = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5295 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5296 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts, |
| 5297 | Node->op_end()-2); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5298 | HiOps.push_back(NewNumEltsNode); |
| 5299 | HiOps.push_back(TypeNode); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5300 | Hi = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5301 | break; |
| 5302 | } |
| 5303 | case ISD::VADD: |
| 5304 | case ISD::VSUB: |
| 5305 | case ISD::VMUL: |
| 5306 | case ISD::VSDIV: |
| 5307 | case ISD::VUDIV: |
| 5308 | case ISD::VAND: |
| 5309 | case ISD::VOR: |
| 5310 | case ISD::VXOR: { |
| 5311 | SDOperand LL, LH, RL, RH; |
| 5312 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 5313 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 5314 | |
| 5315 | Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, |
| 5316 | NewNumEltsNode, TypeNode); |
| 5317 | Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, |
| 5318 | NewNumEltsNode, TypeNode); |
| 5319 | break; |
| 5320 | } |
| 5321 | case ISD::VLOAD: { |
| 5322 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 5323 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
| 5324 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 5325 | |
| 5326 | Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 5327 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; |
| 5328 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 5329 | getIntPtrConstant(IncrementSize)); |
| 5330 | // FIXME: This creates a bogus srcvalue! |
| 5331 | Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 5332 | |
| 5333 | // Build a factor node to remember that this load is independent of the |
| 5334 | // other one. |
| 5335 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5336 | Hi.getValue(1)); |
| 5337 | |
| 5338 | // Remember that we legalized the chain. |
| 5339 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5340 | break; |
| 5341 | } |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5342 | case ISD::VBIT_CONVERT: { |
| 5343 | // We know the result is a vector. The input may be either a vector or a |
| 5344 | // scalar value. |
| 5345 | if (Op.getOperand(0).getValueType() != MVT::Vector) { |
| 5346 | // Lower to a store/load. FIXME: this could be improved probably. |
| 5347 | SDOperand Ptr = CreateStackTemporary(Op.getOperand(0).getValueType()); |
| 5348 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5349 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5350 | Op.getOperand(0), Ptr, NULL, 0); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5351 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 5352 | St = DAG.getVecLoad(NumElements, EVT, St, Ptr, DAG.getSrcValue(0)); |
| 5353 | SplitVectorOp(St, Lo, Hi); |
| 5354 | } else { |
| 5355 | // If the input is a vector type, we have to either scalarize it, pack it |
| 5356 | // or convert it based on whether the input vector type is legal. |
| 5357 | SDNode *InVal = Node->getOperand(0).Val; |
| 5358 | unsigned NumElems = |
| 5359 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 5360 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 5361 | |
| 5362 | // If the input is from a single element vector, scalarize the vector, |
| 5363 | // then treat like a scalar. |
| 5364 | if (NumElems == 1) { |
| 5365 | SDOperand Scalar = PackVectorOp(Op.getOperand(0), EVT); |
| 5366 | Scalar = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Scalar, |
| 5367 | Op.getOperand(1), Op.getOperand(2)); |
| 5368 | SplitVectorOp(Scalar, Lo, Hi); |
| 5369 | } else { |
| 5370 | // Split the input vector. |
| 5371 | SplitVectorOp(Op.getOperand(0), Lo, Hi); |
| 5372 | |
| 5373 | // Convert each of the pieces now. |
| 5374 | Lo = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Lo, |
| 5375 | NewNumEltsNode, TypeNode); |
| 5376 | Hi = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Hi, |
| 5377 | NewNumEltsNode, TypeNode); |
| 5378 | } |
| 5379 | break; |
| 5380 | } |
| 5381 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5382 | } |
| 5383 | |
| 5384 | // Remember in a map if the values will be reused later. |
| 5385 | bool isNew = |
| 5386 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 5387 | assert(isNew && "Value already expanded?!?"); |
| 5388 | } |
| 5389 | |
| 5390 | |
| 5391 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 5392 | /// equivalent operation that returns a scalar (e.g. F32) or packed value |
| 5393 | /// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right |
| 5394 | /// type for the result. |
| 5395 | SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, |
| 5396 | MVT::ValueType NewVT) { |
| 5397 | assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); |
| 5398 | SDNode *Node = Op.Val; |
| 5399 | |
| 5400 | // See if we already packed it. |
| 5401 | std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); |
| 5402 | if (I != PackedNodes.end()) return I->second; |
| 5403 | |
| 5404 | SDOperand Result; |
| 5405 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5406 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5407 | #ifndef NDEBUG |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 5408 | Node->dump(); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5409 | #endif |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5410 | assert(0 && "Unknown vector operation in PackVectorOp!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5411 | case ISD::VADD: |
| 5412 | case ISD::VSUB: |
| 5413 | case ISD::VMUL: |
| 5414 | case ISD::VSDIV: |
| 5415 | case ISD::VUDIV: |
| 5416 | case ISD::VAND: |
| 5417 | case ISD::VOR: |
| 5418 | case ISD::VXOR: |
| 5419 | Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), |
| 5420 | NewVT, |
| 5421 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5422 | PackVectorOp(Node->getOperand(1), NewVT)); |
| 5423 | break; |
| 5424 | case ISD::VLOAD: { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame] | 5425 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 5426 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5427 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5428 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
| 5429 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV->getValue(), SV->getOffset()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5430 | |
| 5431 | // Remember that we legalized the chain. |
| 5432 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 5433 | break; |
| 5434 | } |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5435 | case ISD::VBUILD_VECTOR: |
Chris Lattner | 70c2a61 | 2006-03-31 02:06:56 +0000 | [diff] [blame] | 5436 | if (Node->getOperand(0).getValueType() == NewVT) { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5437 | // Returning a scalar? |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5438 | Result = Node->getOperand(0); |
| 5439 | } else { |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 5440 | // Returning a BUILD_VECTOR? |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 5441 | |
| 5442 | // If all elements of the build_vector are undefs, return an undef. |
| 5443 | bool AllUndef = true; |
| 5444 | for (unsigned i = 0, e = Node->getNumOperands()-2; i != e; ++i) |
| 5445 | if (Node->getOperand(i).getOpcode() != ISD::UNDEF) { |
| 5446 | AllUndef = false; |
| 5447 | break; |
| 5448 | } |
| 5449 | if (AllUndef) { |
| 5450 | Result = DAG.getNode(ISD::UNDEF, NewVT); |
| 5451 | } else { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5452 | Result = DAG.getNode(ISD::BUILD_VECTOR, NewVT, Node->op_begin(), |
| 5453 | Node->getNumOperands()-2); |
Chris Lattner | 17614ea | 2006-04-08 05:34:25 +0000 | [diff] [blame] | 5454 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5455 | } |
| 5456 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5457 | case ISD::VINSERT_VECTOR_ELT: |
| 5458 | if (!MVT::isVector(NewVT)) { |
| 5459 | // Returning a scalar? Must be the inserted element. |
| 5460 | Result = Node->getOperand(1); |
| 5461 | } else { |
| 5462 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, |
| 5463 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5464 | Node->getOperand(1), Node->getOperand(2)); |
| 5465 | } |
| 5466 | break; |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 5467 | case ISD::VVECTOR_SHUFFLE: |
| 5468 | if (!MVT::isVector(NewVT)) { |
| 5469 | // Returning a scalar? Figure out if it is the LHS or RHS and return it. |
| 5470 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 5471 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 5472 | Result = PackVectorOp(Node->getOperand(1), NewVT); |
| 5473 | else |
| 5474 | Result = PackVectorOp(Node->getOperand(0), NewVT); |
| 5475 | } else { |
| 5476 | // Otherwise, return a VECTOR_SHUFFLE node. First convert the index |
| 5477 | // vector from a VBUILD_VECTOR to a BUILD_VECTOR. |
| 5478 | std::vector<SDOperand> BuildVecIdx(Node->getOperand(2).Val->op_begin(), |
| 5479 | Node->getOperand(2).Val->op_end()-2); |
| 5480 | MVT::ValueType BVT = MVT::getIntVectorWithNumElements(BuildVecIdx.size()); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5481 | SDOperand BV = DAG.getNode(ISD::BUILD_VECTOR, BVT, |
| 5482 | Node->getOperand(2).Val->op_begin(), |
| 5483 | Node->getOperand(2).Val->getNumOperands()-2); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 5484 | |
| 5485 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, |
| 5486 | PackVectorOp(Node->getOperand(0), NewVT), |
| 5487 | PackVectorOp(Node->getOperand(1), NewVT), BV); |
| 5488 | } |
| 5489 | break; |
Chris Lattner | e25ca69 | 2006-03-22 20:09:35 +0000 | [diff] [blame] | 5490 | case ISD::VBIT_CONVERT: |
| 5491 | if (Op.getOperand(0).getValueType() != MVT::Vector) |
| 5492 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
| 5493 | else { |
| 5494 | // If the input is a vector type, we have to either scalarize it, pack it |
| 5495 | // or convert it based on whether the input vector type is legal. |
| 5496 | SDNode *InVal = Node->getOperand(0).Val; |
| 5497 | unsigned NumElems = |
| 5498 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 5499 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 5500 | |
| 5501 | // Figure out if there is a Packed type corresponding to this Vector |
| 5502 | // type. If so, convert to the packed type. |
| 5503 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 5504 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 5505 | // Turn this into a bit convert of the packed input. |
| 5506 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 5507 | PackVectorOp(Node->getOperand(0), TVT)); |
| 5508 | break; |
| 5509 | } else if (NumElems == 1) { |
| 5510 | // Turn this into a bit convert of the scalar input. |
| 5511 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, |
| 5512 | PackVectorOp(Node->getOperand(0), EVT)); |
| 5513 | break; |
| 5514 | } else { |
| 5515 | // FIXME: UNIMP! |
| 5516 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 5517 | } |
| 5518 | } |
Evan Cheng | db3c626 | 2006-04-10 18:54:36 +0000 | [diff] [blame] | 5519 | break; |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 5520 | case ISD::VSELECT: |
| 5521 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
| 5522 | PackVectorOp(Op.getOperand(1), NewVT), |
| 5523 | PackVectorOp(Op.getOperand(2), NewVT)); |
| 5524 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
| 5527 | if (TLI.isTypeLegal(NewVT)) |
| 5528 | Result = LegalizeOp(Result); |
| 5529 | bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; |
| 5530 | assert(isNew && "Value already packed?"); |
| 5531 | return Result; |
| 5532 | } |
| 5533 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5534 | |
| 5535 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 5536 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5537 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 5538 | if (ViewLegalizeDAGs) viewGraph(); |
| 5539 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5540 | /// run - This is the main entry point to this class. |
| 5541 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5542 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |