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 | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 31 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 34 | #ifndef NDEBUG |
| 35 | static cl::opt<bool> |
| 36 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 37 | cl::desc("Pop up a window to show dags before legalize")); |
| 38 | #else |
| 39 | static const bool ViewLegalizeDAGs = 0; |
| 40 | #endif |
| 41 | |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 42 | namespace llvm { |
| 43 | template<> |
| 44 | struct DenseMapKeyInfo<SDOperand> { |
| 45 | static inline SDOperand getEmptyKey() { return SDOperand((SDNode*)-1, -1U); } |
| 46 | static inline SDOperand getTombstoneKey() { return SDOperand((SDNode*)-1, 0);} |
| 47 | static unsigned getHashValue(const SDOperand &Val) { |
| 48 | return DenseMapKeyInfo<void*>::getHashValue(Val.Val) + Val.ResNo; |
| 49 | } |
| 50 | static bool isPod() { return true; } |
| 51 | }; |
| 52 | } |
| 53 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 56 | /// hacks on it until the target machine can handle it. This involves |
| 57 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 58 | /// large sizes or splitting up large values into small values) as well as |
| 59 | /// eliminating operations the machine cannot handle. |
| 60 | /// |
| 61 | /// This code also does a small amount of optimization and recognition of idioms |
| 62 | /// as part of its processing. For example, if a target does not support a |
| 63 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 64 | /// will attempt merge setcc and brc instructions into brcc's. |
| 65 | /// |
| 66 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 67 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 68 | TargetLowering &TLI; |
| 69 | SelectionDAG &DAG; |
| 70 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 71 | // Libcall insertion helpers. |
| 72 | |
| 73 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 74 | /// legalized. We use this to ensure that calls are properly serialized |
| 75 | /// against each other, including inserted libcalls. |
| 76 | SDOperand LastCALLSEQ_END; |
| 77 | |
| 78 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 79 | /// helpful assertions that a libcall isn't created while another call is |
| 80 | /// being legalized (which could lead to non-serialized call sequences). |
| 81 | bool IsLegalizingCall; |
| 82 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 83 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 84 | Legal, // The target natively supports this operation. |
| 85 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 86 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 87 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 89 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 90 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 91 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 92 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 94 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 95 | /// than one use, this map indicates what regularized operand to use. This |
| 96 | /// allows us to avoid legalizing the same thing more than once. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 97 | DenseMap<SDOperand, SDOperand> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 99 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 100 | /// than one use, this map indicates what promoted value to use. This allows |
| 101 | /// us to avoid promoting the same thing more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 102 | DenseMap<SDOperand, SDOperand> PromotedNodes; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 103 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 104 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 105 | /// which which operands are the expanded version of the input. This allows |
| 106 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 107 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 108 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 109 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 110 | /// which which operands are the split version of the input. This allows us |
| 111 | /// to avoid splitting the same node more than once. |
| 112 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 113 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 114 | /// ScalarizedNodes - For nodes that need to be converted from vector types to |
| 115 | /// scalar types, this contains the mapping of ones we have already |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 116 | /// processed to the result. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 117 | std::map<SDOperand, SDOperand> ScalarizedNodes; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 119 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 120 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 121 | // If someone requests legalization of the new node, return itself. |
| 122 | if (From != To) |
| 123 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 125 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 126 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 127 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 128 | // If someone requests legalization of the new node, return itself. |
| 129 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 132 | public: |
| 133 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 134 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 136 | /// getTypeAction - Return how we should legalize values of this type, either |
| 137 | /// it is already legal or we need to expand it into multiple registers of |
| 138 | /// smaller integer type, or we need to promote it to a larger type. |
| 139 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 140 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /// isTypeLegal - Return true if this type is legal on this target. |
| 144 | /// |
| 145 | bool isTypeLegal(MVT::ValueType VT) const { |
| 146 | return getTypeAction(VT) == Legal; |
| 147 | } |
| 148 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 149 | void LegalizeDAG(); |
| 150 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 151 | private: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 152 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 153 | /// appropriate for its type. |
| 154 | void HandleOp(SDOperand Op); |
| 155 | |
| 156 | /// LegalizeOp - We know that the specified value has a legal type. |
| 157 | /// Recursively ensure that the operands have legal types, then return the |
| 158 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 159 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 160 | |
| 161 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 162 | /// promote it to compute the value into a larger type. The produced value |
| 163 | /// will have the correct bits for the low portion of the register, but no |
| 164 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 165 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 166 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 167 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 168 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 169 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 170 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 171 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 172 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 173 | /// types. |
| 174 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 175 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 176 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 177 | /// two smaller values. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 178 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 179 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 180 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 181 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 182 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 183 | SDOperand ScalarizeVectorOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 185 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 186 | /// specified mask and type. Targets can specify exactly which masks they |
| 187 | /// support and the code generator is tasked with not creating illegal masks. |
| 188 | /// |
| 189 | /// Note that this will also return true for shuffles that are promoted to a |
| 190 | /// different type. |
| 191 | /// |
| 192 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 193 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 194 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 195 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 196 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 197 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 198 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 199 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 200 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 201 | SDOperand CreateStackTemporary(MVT::ValueType VT); |
| 202 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 203 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 204 | SDOperand &Hi); |
| 205 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 206 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 208 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 209 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 210 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 211 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 212 | SDOperand LegalOp, |
| 213 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 214 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 215 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 216 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 217 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 219 | SDOperand ExpandBSWAP(SDOperand Op); |
| 220 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 221 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 222 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 223 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 224 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 225 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 226 | SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 227 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 229 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 230 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 231 | } |
| 232 | }; |
| 233 | } |
| 234 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 235 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 236 | /// specified mask and type. Targets can specify exactly which masks they |
| 237 | /// support and the code generator is tasked with not creating illegal masks. |
| 238 | /// |
| 239 | /// Note that this will also return true for shuffles that are promoted to a |
| 240 | /// different type. |
| 241 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 242 | SDOperand Mask) const { |
| 243 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 244 | default: return 0; |
| 245 | case TargetLowering::Legal: |
| 246 | case TargetLowering::Custom: |
| 247 | break; |
| 248 | case TargetLowering::Promote: { |
| 249 | // If this is promoted to a different type, convert the shuffle mask and |
| 250 | // ask if it is legal in the promoted type! |
| 251 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 252 | |
| 253 | // If we changed # elements, change the shuffle mask. |
| 254 | unsigned NumEltsGrowth = |
| 255 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 256 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 257 | if (NumEltsGrowth > 1) { |
| 258 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 259 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 260 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 261 | SDOperand InOp = Mask.getOperand(i); |
| 262 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 263 | if (InOp.getOpcode() == ISD::UNDEF) |
| 264 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 265 | else { |
| 266 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 267 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 268 | } |
| 269 | } |
| 270 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 271 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 272 | } |
| 273 | VT = NVT; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 278 | } |
| 279 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 280 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 281 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 282 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 283 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 284 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 287 | /// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order |
| 288 | /// contains all of a nodes operands before it contains the node. |
| 289 | static void ComputeTopDownOrdering(SelectionDAG &DAG, |
| 290 | SmallVector<SDNode*, 64> &Order) { |
| 291 | |
| 292 | DenseMap<SDNode*, unsigned> Visited; |
| 293 | std::vector<SDNode*> Worklist; |
| 294 | Worklist.reserve(128); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 295 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 296 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 297 | // entry node) that have no operands. |
| 298 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 299 | E = DAG.allnodes_end(); I != E; ++I) { |
| 300 | if (I->getNumOperands() == 0) { |
| 301 | Visited[I] = 0 - 1U; |
| 302 | Worklist.push_back(I); |
| 303 | } |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 306 | while (!Worklist.empty()) { |
| 307 | SDNode *N = Worklist.back(); |
| 308 | Worklist.pop_back(); |
| 309 | |
| 310 | if (++Visited[N] != N->getNumOperands()) |
| 311 | continue; // Haven't visited all operands yet |
| 312 | |
| 313 | Order.push_back(N); |
| 314 | |
| 315 | // Now that we have N in, add anything that uses it if all of their operands |
| 316 | // are now done. |
| 317 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 318 | UI != E; ++UI) |
| 319 | Worklist.push_back(*UI); |
| 320 | } |
| 321 | |
| 322 | assert(Order.size() == Visited.size() && |
| 323 | Order.size() == |
| 324 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
| 325 | "Error: DAG is cyclic!"); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 329 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 330 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 331 | IsLegalizingCall = false; |
| 332 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 333 | // The legalize process is inherently a bottom-up recursive process (users |
| 334 | // legalize their uses before themselves). Given infinite stack space, we |
| 335 | // could just start legalizing on the root and traverse the whole graph. In |
| 336 | // 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] | 337 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 338 | // node is only legalized after all of its operands are legalized. |
Chris Lattner | 0ed4417 | 2007-02-04 01:20:02 +0000 | [diff] [blame] | 339 | SmallVector<SDNode*, 64> Order; |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 340 | ComputeTopDownOrdering(DAG, Order); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 341 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 342 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 343 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 344 | |
| 345 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 346 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 347 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 348 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 349 | |
| 350 | ExpandedNodes.clear(); |
| 351 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 352 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 353 | SplitNodes.clear(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 354 | ScalarizedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 355 | |
| 356 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 357 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 360 | |
| 361 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 362 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 363 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 364 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 365 | return Node; |
| 366 | if (Node->use_empty()) |
| 367 | return 0; // No CallSeqEnd |
| 368 | |
| 369 | // The chain is usually at the end. |
| 370 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 371 | if (TheChain.getValueType() != MVT::Other) { |
| 372 | // Sometimes it's at the beginning. |
| 373 | TheChain = SDOperand(Node, 0); |
| 374 | if (TheChain.getValueType() != MVT::Other) { |
| 375 | // Otherwise, hunt for it. |
| 376 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 377 | if (Node->getValueType(i) == MVT::Other) { |
| 378 | TheChain = SDOperand(Node, i); |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | // Otherwise, we walked into a node without a chain. |
| 383 | if (TheChain.getValueType() != MVT::Other) |
| 384 | return 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 389 | E = Node->use_end(); UI != E; ++UI) { |
| 390 | |
| 391 | // Make sure to only follow users of our token chain. |
| 392 | SDNode *User = *UI; |
| 393 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 394 | if (User->getOperand(i) == TheChain) |
| 395 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 396 | return Result; |
| 397 | } |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 402 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 403 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 404 | assert(Node && "Didn't find callseq_start for a call??"); |
| 405 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 406 | |
| 407 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 408 | "Node doesn't have a token chain argument!"); |
| 409 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 410 | } |
| 411 | |
| 412 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 413 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 414 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 415 | /// |
| 416 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 417 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 418 | /// |
| 419 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 420 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 421 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 422 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 423 | // If we've already processed this node and it does lead to Dest, there is no |
| 424 | // need to reprocess it. |
| 425 | if (NodesLeadingTo.count(N)) return true; |
| 426 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 427 | // If the first result of this node has been already legalized, then it cannot |
| 428 | // reach N. |
| 429 | switch (getTypeAction(N->getValueType(0))) { |
| 430 | case Legal: |
| 431 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 432 | break; |
| 433 | case Promote: |
| 434 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 435 | break; |
| 436 | case Expand: |
| 437 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | // Okay, this node has not already been legalized. Check and legalize all |
| 442 | // operands. If none lead to Dest, then we can legalize this node. |
| 443 | bool OperandsLeadToDest = false; |
| 444 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 445 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 446 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 447 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 448 | if (OperandsLeadToDest) { |
| 449 | NodesLeadingTo.insert(N); |
| 450 | return true; |
| 451 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 452 | |
| 453 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 454 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 455 | return false; |
| 456 | } |
| 457 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 458 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 459 | /// appropriate for its type. |
| 460 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 461 | MVT::ValueType VT = Op.getValueType(); |
| 462 | switch (getTypeAction(VT)) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 463 | default: assert(0 && "Bad type action!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 464 | case Legal: (void)LegalizeOp(Op); break; |
| 465 | case Promote: (void)PromoteOp(Op); break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 466 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 467 | if (!MVT::isVector(VT)) { |
| 468 | // If this is an illegal scalar, expand it into its two component |
| 469 | // pieces. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 470 | SDOperand X, Y; |
| 471 | ExpandOp(Op, X, Y); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 472 | } else if (MVT::getVectorNumElements(VT) == 1) { |
| 473 | // If this is an illegal single element vector, convert it to a |
| 474 | // scalar operation. |
| 475 | (void)ScalarizeVectorOp(Op); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 476 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 477 | // Otherwise, this is an illegal multiple element vector. |
| 478 | // Split it in half and legalize both parts. |
| 479 | SDOperand X, Y; |
| 480 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 481 | } |
| 482 | break; |
| 483 | } |
| 484 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 485 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 486 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 487 | /// a load from the constant pool. |
| 488 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 489 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 490 | bool Extend = false; |
| 491 | |
| 492 | // If a FP immediate is precise when represented as a float and if the |
| 493 | // target can do an extending load from float to double, we put it into |
| 494 | // the constant pool as a float, even if it's is statically typed as a |
| 495 | // double. |
| 496 | MVT::ValueType VT = CFP->getValueType(0); |
| 497 | bool isDouble = VT == MVT::f64; |
| 498 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 499 | Type::FloatTy, CFP->getValue()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 500 | if (!UseCP) { |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 501 | double Val = LLVMC->getValue(); |
| 502 | return isDouble |
| 503 | ? DAG.getConstant(DoubleToBits(Val), MVT::i64) |
| 504 | : DAG.getConstant(FloatToBits(Val), MVT::i32); |
| 505 | } |
| 506 | |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 507 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 508 | // Only do this if the target has a native EXTLOAD instruction from f32. |
| 509 | TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) { |
| 510 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy)); |
| 511 | VT = MVT::f32; |
| 512 | Extend = true; |
| 513 | } |
| 514 | |
| 515 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
| 516 | if (Extend) { |
| 517 | return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 518 | CPIdx, NULL, 0, MVT::f32); |
| 519 | } else { |
| 520 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
| 521 | } |
| 522 | } |
| 523 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 524 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 525 | /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise |
| 526 | /// operations. |
| 527 | static |
| 528 | SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT, |
| 529 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 530 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 531 | MVT::ValueType SrcVT = Node->getOperand(1).getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 532 | assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) && |
| 533 | "fcopysign expansion only supported for f32 and f64"); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 534 | MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 535 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 536 | // First get the sign bit of second operand. |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 537 | SDOperand Mask1 = (SrcVT == MVT::f64) |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 538 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT) |
| 539 | : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 540 | Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 541 | SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 542 | SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 543 | // Shift right or sign-extend it if the two operands have different types. |
| 544 | int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT); |
| 545 | if (SizeDiff > 0) { |
| 546 | SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit, |
| 547 | DAG.getConstant(SizeDiff, TLI.getShiftAmountTy())); |
| 548 | SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit); |
| 549 | } else if (SizeDiff < 0) |
| 550 | SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 551 | |
| 552 | // Clear the sign bit of first operand. |
| 553 | SDOperand Mask2 = (VT == MVT::f64) |
| 554 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 555 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 556 | Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 557 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 558 | Result = DAG.getNode(ISD::AND, NVT, Result, Mask2); |
| 559 | |
| 560 | // Or the value with the sign bit. |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 561 | Result = DAG.getNode(ISD::OR, NVT, Result, SignBit); |
| 562 | return Result; |
| 563 | } |
| 564 | |
| 565 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 566 | /// LegalizeOp - We know that the specified value has a legal type. |
| 567 | /// Recursively ensure that the operands have legal types, then return the |
| 568 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 569 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 570 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 571 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 572 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 574 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 575 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 576 | if (Node->getNumValues() > 1) { |
| 577 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 578 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 579 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 580 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 581 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 582 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 586 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 587 | // means that we always must cache transformed nodes. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 588 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 589 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 590 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 591 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 592 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 593 | bool isCustom = false; |
| 594 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 595 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 596 | case ISD::FrameIndex: |
| 597 | case ISD::EntryToken: |
| 598 | case ISD::Register: |
| 599 | case ISD::BasicBlock: |
| 600 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 601 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 602 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 603 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 604 | case ISD::TargetConstantPool: |
| 605 | case ISD::TargetGlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 606 | case ISD::TargetGlobalTLSAddress: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 607 | case ISD::TargetExternalSymbol: |
| 608 | case ISD::VALUETYPE: |
| 609 | case ISD::SRCVALUE: |
| 610 | case ISD::STRING: |
| 611 | case ISD::CONDCODE: |
| 612 | // Primitives must all be legal. |
| 613 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 614 | "This must be legal!"); |
| 615 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 616 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 617 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 618 | // If this is a target node, legalize it by legalizing the operands then |
| 619 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 620 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 621 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 622 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 623 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 624 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 625 | |
| 626 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 627 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 628 | return Result.getValue(Op.ResNo); |
| 629 | } |
| 630 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 631 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 632 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 633 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 634 | assert(0 && "Do not know how to legalize this operator!"); |
| 635 | abort(); |
Lauro Ramos Venancio | 0d3b678 | 2007-04-20 23:02:39 +0000 | [diff] [blame] | 636 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 637 | case ISD::GlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 638 | case ISD::GlobalTLSAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 639 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 640 | case ISD::ConstantPool: |
| 641 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 642 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 643 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 644 | case TargetLowering::Custom: |
| 645 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 646 | if (Tmp1.Val) Result = Tmp1; |
| 647 | // 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] | 648 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 649 | break; |
| 650 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 651 | break; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 652 | case ISD::FRAMEADDR: |
| 653 | case ISD::RETURNADDR: |
| 654 | // The only option for these nodes is to custom lower them. If the target |
| 655 | // does not custom lower them, then return zero. |
| 656 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 657 | if (Tmp1.Val) |
| 658 | Result = Tmp1; |
| 659 | else |
| 660 | Result = DAG.getConstant(0, TLI.getPointerTy()); |
| 661 | break; |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 662 | case ISD::EXCEPTIONADDR: { |
| 663 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 664 | MVT::ValueType VT = Node->getValueType(0); |
| 665 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 666 | default: assert(0 && "This action is not supported yet!"); |
| 667 | case TargetLowering::Expand: { |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 668 | unsigned Reg = TLI.getExceptionAddressRegister(); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 669 | Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo); |
| 670 | } |
| 671 | break; |
| 672 | case TargetLowering::Custom: |
| 673 | Result = TLI.LowerOperation(Op, DAG); |
| 674 | if (Result.Val) break; |
| 675 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 676 | case TargetLowering::Legal: { |
| 677 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 }; |
| 678 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 679 | Ops, 2).getValue(Op.ResNo); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
| 682 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 683 | } |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 684 | break; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 685 | case ISD::EHSELECTION: { |
| 686 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 687 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 688 | MVT::ValueType VT = Node->getValueType(0); |
| 689 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 690 | default: assert(0 && "This action is not supported yet!"); |
| 691 | case TargetLowering::Expand: { |
| 692 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 693 | Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo); |
| 694 | } |
| 695 | break; |
| 696 | case TargetLowering::Custom: |
| 697 | Result = TLI.LowerOperation(Op, DAG); |
| 698 | if (Result.Val) break; |
| 699 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 700 | case TargetLowering::Legal: { |
| 701 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 }; |
| 702 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 703 | Ops, 2).getValue(Op.ResNo); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 704 | break; |
| 705 | } |
| 706 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 707 | } |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 708 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 709 | case ISD::AssertSext: |
| 710 | case ISD::AssertZext: |
| 711 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 712 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 713 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 714 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 715 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 716 | Result = Node->getOperand(Op.ResNo); |
| 717 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 718 | case ISD::CopyFromReg: |
| 719 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 720 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 721 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 722 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 723 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 724 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 725 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 726 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 727 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 728 | } else { |
| 729 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 730 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 731 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 732 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 733 | // Since CopyFromReg produces two values, make sure to remember that we |
| 734 | // legalized both of them. |
| 735 | AddLegalizedOperand(Op.getValue(0), Result); |
| 736 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 737 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 738 | case ISD::UNDEF: { |
| 739 | MVT::ValueType VT = Op.getValueType(); |
| 740 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 741 | default: assert(0 && "This action is not supported yet!"); |
| 742 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 743 | if (MVT::isInteger(VT)) |
| 744 | Result = DAG.getConstant(0, VT); |
| 745 | else if (MVT::isFloatingPoint(VT)) |
| 746 | Result = DAG.getConstantFP(0, VT); |
| 747 | else |
| 748 | assert(0 && "Unknown value type!"); |
| 749 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 750 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 751 | break; |
| 752 | } |
| 753 | break; |
| 754 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 756 | case ISD::INTRINSIC_W_CHAIN: |
| 757 | case ISD::INTRINSIC_WO_CHAIN: |
| 758 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 759 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 760 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 761 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 762 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 763 | |
| 764 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 765 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 766 | TargetLowering::Custom) { |
| 767 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 768 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (Result.Val->getNumValues() == 1) break; |
| 772 | |
| 773 | // Must have return value and chain result. |
| 774 | assert(Result.Val->getNumValues() == 2 && |
| 775 | "Cannot return more than two values!"); |
| 776 | |
| 777 | // Since loads produce two values, make sure to remember that we |
| 778 | // legalized both of them. |
| 779 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 780 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 781 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 783 | |
| 784 | case ISD::LOCATION: |
| 785 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 786 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 787 | |
| 788 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 789 | case TargetLowering::Promote: |
| 790 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 791 | case TargetLowering::Expand: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 792 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 793 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 794 | bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 795 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 796 | if (MMI && (useDEBUG_LOC || useLABEL)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 797 | const std::string &FName = |
| 798 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 799 | const std::string &DirName = |
| 800 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 801 | unsigned SrcFile = MMI->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 802 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 803 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 804 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 805 | SDOperand LineOp = Node->getOperand(1); |
| 806 | SDOperand ColOp = Node->getOperand(2); |
| 807 | |
| 808 | if (useDEBUG_LOC) { |
| 809 | Ops.push_back(LineOp); // line # |
| 810 | Ops.push_back(ColOp); // col # |
| 811 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 812 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 813 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 814 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 815 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 816 | unsigned ID = MMI->RecordLabel(Line, Col, SrcFile); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 817 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 818 | Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 819 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 820 | } else { |
| 821 | Result = Tmp1; // chain |
| 822 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 823 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 824 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 825 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 826 | if (Tmp1 != Node->getOperand(0) || |
| 827 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 828 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 829 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 830 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 831 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 832 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 833 | } else { |
| 834 | // Otherwise promote them. |
| 835 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 836 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 837 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 838 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 839 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 840 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 841 | } |
| 842 | break; |
| 843 | } |
| 844 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 845 | |
| 846 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 847 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 848 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 849 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 850 | case TargetLowering::Legal: |
| 851 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 852 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 853 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 854 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 855 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 856 | break; |
| 857 | } |
| 858 | break; |
| 859 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 860 | case ISD::LABEL: |
| 861 | assert(Node->getNumOperands() == 2 && "Invalid LABEL node!"); |
| 862 | switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 863 | default: assert(0 && "This action is not supported yet!"); |
| 864 | case TargetLowering::Legal: |
| 865 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 866 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 867 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 868 | break; |
Chris Lattner | a9569f1 | 2007-03-03 19:21:38 +0000 | [diff] [blame] | 869 | case TargetLowering::Expand: |
| 870 | Result = LegalizeOp(Node->getOperand(0)); |
| 871 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 872 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 873 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 874 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 875 | case ISD::Constant: |
| 876 | // We know we don't need to expand constants here, constants only have one |
| 877 | // value and we check that it is fine above. |
| 878 | |
| 879 | // FIXME: Maybe we should handle things like targets that don't support full |
| 880 | // 32-bit immediates? |
| 881 | break; |
| 882 | case ISD::ConstantFP: { |
| 883 | // Spill FP immediates to the constant pool if the target cannot directly |
| 884 | // codegen them. Targets often have some immediate values that can be |
| 885 | // efficiently generated into an FP register without a load. We explicitly |
| 886 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 887 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 888 | |
| 889 | // Check to see if this FP immediate is already legal. |
| 890 | bool isLegal = false; |
| 891 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 892 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 893 | if (CFP->isExactlyValue(*I)) { |
| 894 | isLegal = true; |
| 895 | break; |
| 896 | } |
| 897 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 898 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 899 | if (isLegal) { |
| 900 | Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 905 | default: assert(0 && "This action is not supported yet!"); |
| 906 | case TargetLowering::Custom: |
| 907 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 908 | if (Tmp3.Val) { |
| 909 | Result = Tmp3; |
| 910 | break; |
| 911 | } |
| 912 | // FALLTHROUGH |
| 913 | case TargetLowering::Expand: |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 914 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 915 | } |
| 916 | break; |
| 917 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 918 | case ISD::TokenFactor: |
| 919 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 920 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 921 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 922 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 923 | } else if (Node->getNumOperands() == 3) { |
| 924 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 925 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 926 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 927 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 928 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 929 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 930 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 931 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 932 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 933 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 934 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 935 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 936 | |
| 937 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 938 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 939 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 940 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 941 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
| 942 | assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() && |
| 943 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 944 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 945 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 946 | // 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] | 947 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
| 948 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 949 | if (Op.ResNo == i) |
| 950 | Tmp2 = Tmp1; |
| 951 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 952 | } |
| 953 | return Tmp2; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 954 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 955 | case ISD::BUILD_VECTOR: |
| 956 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 957 | default: assert(0 && "This action is not supported yet!"); |
| 958 | case TargetLowering::Custom: |
| 959 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 960 | if (Tmp3.Val) { |
| 961 | Result = Tmp3; |
| 962 | break; |
| 963 | } |
| 964 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 965 | case TargetLowering::Expand: |
| 966 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 967 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 968 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 969 | break; |
| 970 | case ISD::INSERT_VECTOR_ELT: |
| 971 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 972 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 973 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 974 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 975 | |
| 976 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 977 | Node->getValueType(0))) { |
| 978 | default: assert(0 && "This action is not supported yet!"); |
| 979 | case TargetLowering::Legal: |
| 980 | break; |
| 981 | case TargetLowering::Custom: |
| 982 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 983 | if (Tmp3.Val) { |
| 984 | Result = Tmp3; |
| 985 | break; |
| 986 | } |
| 987 | // FALLTHROUGH |
| 988 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 989 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 990 | // then a shuffle that inserts it into the right position in the vector. |
| 991 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 992 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 993 | Tmp1.getValueType(), Tmp2); |
| 994 | |
| 995 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 996 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 997 | MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 998 | |
| 999 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 1000 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 1001 | // the RHS. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1002 | SmallVector<SDOperand, 8> ShufOps; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1003 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1004 | if (i != InsertPos->getValue()) |
| 1005 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 1006 | else |
| 1007 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 1008 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1009 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 1010 | &ShufOps[0], ShufOps.size()); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1011 | |
| 1012 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 1013 | Tmp1, ScVec, ShufMask); |
| 1014 | Result = LegalizeOp(Result); |
| 1015 | break; |
| 1016 | } |
| 1017 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1018 | // If the target doesn't support this, we have to spill the input vector |
| 1019 | // to a temporary stack slot, update the element, then reload it. This is |
| 1020 | // badness. We could also load the value into a vector register (either |
| 1021 | // with a "move to register" or "extload into register" instruction, then |
| 1022 | // permute it into place, if the idx is a constant and if the idx is |
| 1023 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1024 | MVT::ValueType VT = Tmp1.getValueType(); |
| 1025 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 1026 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 1027 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1028 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1029 | // Store the vector. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1030 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1033 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 1034 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1035 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1036 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 1037 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 1038 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1039 | // Store the scalar value. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1040 | Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1041 | // Load the updated vector. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1042 | Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1047 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1048 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 1049 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 1050 | break; |
| 1051 | } |
| 1052 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1053 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 1054 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1055 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 1056 | Node->getValueType(0))) { |
| 1057 | default: assert(0 && "This action is not supported yet!"); |
| 1058 | case TargetLowering::Legal: |
| 1059 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 1060 | case TargetLowering::Custom: |
| 1061 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1062 | if (Tmp3.Val) { |
| 1063 | Result = Tmp3; |
| 1064 | break; |
| 1065 | } |
| 1066 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1067 | case TargetLowering::Expand: |
| 1068 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1071 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1072 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1073 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1074 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 1075 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1076 | |
| 1077 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1078 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 1079 | default: assert(0 && "Unknown operation action!"); |
| 1080 | case TargetLowering::Legal: |
| 1081 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 1082 | "vector shuffle should not be created if not legal!"); |
| 1083 | break; |
| 1084 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1085 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1086 | if (Tmp3.Val) { |
| 1087 | Result = Tmp3; |
| 1088 | break; |
| 1089 | } |
| 1090 | // FALLTHROUGH |
| 1091 | case TargetLowering::Expand: { |
| 1092 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1093 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1094 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1095 | SDOperand Mask = Node->getOperand(2); |
| 1096 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1097 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1098 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1099 | SDOperand Arg = Mask.getOperand(i); |
| 1100 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1101 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1102 | } else { |
| 1103 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1104 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1105 | if (Idx < NumElems) |
| 1106 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1107 | DAG.getConstant(Idx, PtrVT))); |
| 1108 | else |
| 1109 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1110 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1111 | } |
| 1112 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1113 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1114 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1115 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1116 | case TargetLowering::Promote: { |
| 1117 | // Change base type to a different vector type. |
| 1118 | MVT::ValueType OVT = Node->getValueType(0); |
| 1119 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1120 | |
| 1121 | // Cast the two input vectors. |
| 1122 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1123 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1124 | |
| 1125 | // Convert the shuffle mask to the right # elements. |
| 1126 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1127 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1128 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1129 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1130 | break; |
| 1131 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1132 | } |
| 1133 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1134 | |
| 1135 | case ISD::EXTRACT_VECTOR_ELT: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1136 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1137 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1138 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1139 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1142 | case ISD::EXTRACT_SUBVECTOR: |
| 1143 | Tmp1 = Node->getOperand(0); |
| 1144 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1145 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1146 | Result = ExpandEXTRACT_SUBVECTOR(Result); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 1147 | break; |
| 1148 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1149 | case ISD::CALLSEQ_START: { |
| 1150 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1151 | |
| 1152 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1153 | // to this call start. This ensures that any libcalls that need be inserted |
| 1154 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1155 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1156 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1157 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1158 | NodesLeadingTo); |
| 1159 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Now that we legalized all of the inputs (which may have inserted |
| 1162 | // libcalls) create the new CALLSEQ_START node. |
| 1163 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1164 | |
| 1165 | // Merge in the last call, to ensure that this call start after the last |
| 1166 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1167 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1168 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1169 | Tmp1 = LegalizeOp(Tmp1); |
| 1170 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1171 | |
| 1172 | // Do not try to legalize the target-specific arguments (#1+). |
| 1173 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1174 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1175 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1176 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1180 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1181 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1182 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1183 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1184 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1185 | // sequence have been legalized, legalize the call itself. During this |
| 1186 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1187 | // can overlap. |
| 1188 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1189 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1190 | // Note that we are selecting this call! |
| 1191 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1192 | IsLegalizingCall = true; |
| 1193 | |
| 1194 | // Legalize the call, starting from the CALLSEQ_END. |
| 1195 | LegalizeOp(LastCALLSEQ_END); |
| 1196 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1197 | return Result; |
| 1198 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1199 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1200 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1201 | // will cause this node to be legalized as well as handling libcalls right. |
| 1202 | if (LastCALLSEQ_END.Val != Node) { |
| 1203 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 1204 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1205 | assert(I != LegalizedNodes.end() && |
| 1206 | "Legalizing the call start should have legalized this node!"); |
| 1207 | return I->second; |
| 1208 | } |
| 1209 | |
| 1210 | // Otherwise, the call start has been legalized and everything is going |
| 1211 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1212 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1213 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1214 | // an optional flag input. |
| 1215 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1216 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1217 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1218 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1219 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } else { |
| 1222 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1223 | if (Tmp1 != Node->getOperand(0) || |
| 1224 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1225 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1226 | Ops[0] = Tmp1; |
| 1227 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1228 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1229 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1230 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1231 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1232 | // This finishes up call legalization. |
| 1233 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1234 | |
| 1235 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1236 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1237 | if (Node->getNumValues() == 2) |
| 1238 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1239 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1240 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1241 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1242 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1243 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1244 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1246 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1247 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1248 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1249 | Node->getValueType(0))) { |
| 1250 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1251 | case TargetLowering::Expand: { |
| 1252 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1253 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1254 | " not tell us which reg is the stack pointer!"); |
| 1255 | SDOperand Chain = Tmp1.getOperand(0); |
| 1256 | SDOperand Size = Tmp2.getOperand(1); |
| 1257 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 1258 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 1259 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1260 | Tmp1 = LegalizeOp(Tmp1); |
| 1261 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1265 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1266 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1267 | Tmp1 = LegalizeOp(Tmp3); |
| 1268 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1269 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1270 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1271 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1272 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1273 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1274 | // Since this op produce two values, make sure to remember that we |
| 1275 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1276 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1277 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1278 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1279 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1280 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1281 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1282 | bool Changed = false; |
| 1283 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1284 | // that need to be expanded or something. Note we skip the asm string and |
| 1285 | // all of the TargetConstant flags. |
| 1286 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1287 | Changed = Op != Ops[0]; |
| 1288 | Ops[0] = Op; |
| 1289 | |
| 1290 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1291 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1292 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1293 | for (++i; NumVals; ++i, --NumVals) { |
| 1294 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1295 | if (Op != Ops[i]) { |
| 1296 | Changed = true; |
| 1297 | Ops[i] = Op; |
| 1298 | } |
| 1299 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1300 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (HasInFlag) { |
| 1303 | Op = LegalizeOp(Ops.back()); |
| 1304 | Changed |= Op != Ops.back(); |
| 1305 | Ops.back() = Op; |
| 1306 | } |
| 1307 | |
| 1308 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1309 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1310 | |
| 1311 | // 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] | 1312 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1313 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1314 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1315 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1316 | case ISD::BR: |
| 1317 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1318 | // Ensure that libcalls are emitted before a branch. |
| 1319 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1320 | Tmp1 = LegalizeOp(Tmp1); |
| 1321 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1322 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1323 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1324 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1325 | case ISD::BRIND: |
| 1326 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1327 | // Ensure that libcalls are emitted before a branch. |
| 1328 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1329 | Tmp1 = LegalizeOp(Tmp1); |
| 1330 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1331 | |
| 1332 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1333 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1334 | case Legal: |
| 1335 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1336 | break; |
| 1337 | } |
| 1338 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1339 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1340 | case ISD::BR_JT: |
| 1341 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1342 | // Ensure that libcalls are emitted before a branch. |
| 1343 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1344 | Tmp1 = LegalizeOp(Tmp1); |
| 1345 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1346 | |
| 1347 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1348 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1349 | |
| 1350 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1351 | default: assert(0 && "This action is not supported yet!"); |
| 1352 | case TargetLowering::Legal: break; |
| 1353 | case TargetLowering::Custom: |
| 1354 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1355 | if (Tmp1.Val) Result = Tmp1; |
| 1356 | break; |
| 1357 | case TargetLowering::Expand: { |
| 1358 | SDOperand Chain = Result.getOperand(0); |
| 1359 | SDOperand Table = Result.getOperand(1); |
| 1360 | SDOperand Index = Result.getOperand(2); |
| 1361 | |
| 1362 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1363 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1364 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1365 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1366 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1367 | |
| 1368 | SDOperand LD; |
| 1369 | switch (EntrySize) { |
| 1370 | default: assert(0 && "Size of jump table not supported yet."); break; |
| 1371 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break; |
| 1372 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break; |
| 1373 | } |
| 1374 | |
| 1375 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1376 | // For PIC, the sequence is: |
| 1377 | // BRIND(load(Jumptable + index) + RelocBase) |
| 1378 | // RelocBase is the JumpTable on PPC and X86, GOT on Alpha |
| 1379 | SDOperand Reloc; |
| 1380 | if (TLI.usesGlobalOffsetTable()) |
| 1381 | Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy); |
| 1382 | else |
| 1383 | Reloc = Table; |
Evan Cheng | d063189 | 2006-10-31 02:31:00 +0000 | [diff] [blame] | 1384 | Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1385 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc); |
| 1386 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
| 1387 | } else { |
| 1388 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD); |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1393 | case ISD::BRCOND: |
| 1394 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1395 | // Ensure that libcalls are emitted before a return. |
| 1396 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1397 | Tmp1 = LegalizeOp(Tmp1); |
| 1398 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1399 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1400 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1401 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1402 | case Legal: |
| 1403 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1404 | break; |
| 1405 | case Promote: |
| 1406 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1407 | |
| 1408 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1409 | // that the value is properly zero extended. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1410 | if (!DAG.MaskedValueIsZero(Tmp2, |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1411 | MVT::getIntVTBitMask(Tmp2.getValueType())^1)) |
| 1412 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1417 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1418 | |
| 1419 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1420 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1421 | case TargetLowering::Legal: break; |
| 1422 | case TargetLowering::Custom: |
| 1423 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1424 | if (Tmp1.Val) Result = Tmp1; |
| 1425 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1426 | case TargetLowering::Expand: |
| 1427 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1428 | // Node. |
| 1429 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1430 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1431 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1432 | Node->getOperand(2)); |
| 1433 | } else { |
| 1434 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1435 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1436 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1437 | Node->getOperand(2)); |
| 1438 | } |
| 1439 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1440 | } |
| 1441 | break; |
| 1442 | case ISD::BR_CC: |
| 1443 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1444 | // Ensure that libcalls are emitted before a branch. |
| 1445 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1446 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1447 | Tmp2 = Node->getOperand(2); // LHS |
| 1448 | Tmp3 = Node->getOperand(3); // RHS |
| 1449 | Tmp4 = Node->getOperand(1); // CC |
| 1450 | |
| 1451 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1452 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1453 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1454 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1455 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1456 | // the result against zero to select between true and false values. |
| 1457 | if (Tmp3.Val == 0) { |
| 1458 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1459 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1460 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1461 | |
| 1462 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1463 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1465 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1466 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1467 | case TargetLowering::Legal: break; |
| 1468 | case TargetLowering::Custom: |
| 1469 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1470 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1471 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1472 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1473 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1474 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1475 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1476 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1477 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1478 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1479 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1480 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1481 | MVT::ValueType VT = Node->getValueType(0); |
| 1482 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1483 | Tmp3 = Result.getValue(0); |
| 1484 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1485 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1486 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1487 | default: assert(0 && "This action is not supported yet!"); |
| 1488 | case TargetLowering::Legal: break; |
| 1489 | case TargetLowering::Custom: |
| 1490 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1491 | if (Tmp1.Val) { |
| 1492 | Tmp3 = LegalizeOp(Tmp1); |
| 1493 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1494 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | case TargetLowering::Promote: { |
| 1497 | // Only promote a load of vector type to another. |
| 1498 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1499 | // Change base type to a different vector type. |
| 1500 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1501 | |
| 1502 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1503 | LD->getSrcValueOffset()); |
| 1504 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1505 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1506 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1507 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1508 | } |
| 1509 | // Since loads produce two values, make sure to remember that we |
| 1510 | // legalized both of them. |
| 1511 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1512 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1513 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1514 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 1515 | MVT::ValueType SrcVT = LD->getLoadedVT(); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1516 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 1517 | default: assert(0 && "This action is not supported yet!"); |
| 1518 | case TargetLowering::Promote: |
| 1519 | assert(SrcVT == MVT::i1 && |
| 1520 | "Can only promote extending LOAD from i1 -> i8!"); |
| 1521 | Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1522 | LD->getSrcValue(), LD->getSrcValueOffset(), |
| 1523 | MVT::i8); |
| 1524 | Tmp1 = Result.getValue(0); |
| 1525 | Tmp2 = Result.getValue(1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1526 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1527 | case TargetLowering::Custom: |
| 1528 | isCustom = true; |
| 1529 | // FALLTHROUGH |
| 1530 | case TargetLowering::Legal: |
| 1531 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1532 | Tmp1 = Result.getValue(0); |
| 1533 | Tmp2 = Result.getValue(1); |
| 1534 | |
| 1535 | if (isCustom) { |
| 1536 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1537 | if (Tmp3.Val) { |
| 1538 | Tmp1 = LegalizeOp(Tmp3); |
| 1539 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1540 | } |
| 1541 | } |
| 1542 | break; |
| 1543 | case TargetLowering::Expand: |
| 1544 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 1545 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1546 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 1547 | LD->getSrcValueOffset()); |
| 1548 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 1549 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1550 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1551 | break; |
| 1552 | } |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1553 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1554 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1555 | // zero/sign extend inreg. |
| 1556 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1557 | Tmp1, Tmp2, LD->getSrcValue(), |
| 1558 | LD->getSrcValueOffset(), SrcVT); |
| 1559 | SDOperand ValRes; |
| 1560 | if (ExtType == ISD::SEXTLOAD) |
| 1561 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1562 | Result, DAG.getValueType(SrcVT)); |
| 1563 | else |
| 1564 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 1565 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1566 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1567 | break; |
| 1568 | } |
| 1569 | // Since loads produce two values, make sure to remember that we legalized |
| 1570 | // both of them. |
| 1571 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1572 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1573 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1574 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1575 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1576 | case ISD::EXTRACT_ELEMENT: { |
| 1577 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1578 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1579 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1580 | case Legal: |
| 1581 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1582 | // 1 -> Hi |
| 1583 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1584 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1585 | TLI.getShiftAmountTy())); |
| 1586 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1587 | } else { |
| 1588 | // 0 -> Lo |
| 1589 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1590 | Node->getOperand(0)); |
| 1591 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1592 | break; |
| 1593 | case Expand: |
| 1594 | // Get both the low and high parts. |
| 1595 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1596 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1597 | Result = Tmp2; // 1 -> Hi |
| 1598 | else |
| 1599 | Result = Tmp1; // 0 -> Lo |
| 1600 | break; |
| 1601 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1602 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1603 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1604 | |
| 1605 | case ISD::CopyToReg: |
| 1606 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1608 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1609 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1610 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1611 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1612 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1613 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1614 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1615 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1616 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1617 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1618 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1619 | Tmp3); |
| 1620 | } else { |
| 1621 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | // Since this produces two values, make sure to remember that we legalized |
| 1625 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1626 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1627 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1628 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1629 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | |
| 1632 | case ISD::RET: |
| 1633 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1634 | |
| 1635 | // Ensure that libcalls are emitted before a return. |
| 1636 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1637 | Tmp1 = LegalizeOp(Tmp1); |
| 1638 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1640 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1641 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 1642 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1643 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1644 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1645 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1646 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1647 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1648 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1649 | if (!MVT::isVector(Tmp2.getValueType())) { |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1650 | SDOperand Lo, Hi; |
| 1651 | ExpandOp(Tmp2, Lo, Hi); |
Chris Lattner | edf2e8d | 2007-03-06 20:01:06 +0000 | [diff] [blame] | 1652 | |
| 1653 | // Big endian systems want the hi reg first. |
| 1654 | if (!TLI.isLittleEndian()) |
| 1655 | std::swap(Lo, Hi); |
| 1656 | |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1657 | if (Hi.Val) |
| 1658 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 1659 | else |
| 1660 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 1661 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1662 | } else { |
| 1663 | SDNode *InVal = Tmp2.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1664 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 1665 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1666 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1667 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1668 | // type. If so, convert to the vector type. |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1669 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1670 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1671 | // Turn this into a return of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1672 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1673 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1674 | } else if (NumElems == 1) { |
| 1675 | // Turn this into a return of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1676 | Tmp2 = ScalarizeVectorOp(Tmp2); |
| 1677 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1678 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1679 | |
| 1680 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 1681 | // should be returned in integer registers! |
| 1682 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1683 | // The scalarized value type may not be legal, e.g. it might require |
| 1684 | // promotion or expansion. Relegalize the return. |
| 1685 | Result = LegalizeOp(Result); |
| 1686 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1687 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 1688 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1689 | SDOperand Lo, Hi; |
| 1690 | SplitVectorOp(Tmp2, Lo, Hi); |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1691 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1692 | Result = LegalizeOp(Result); |
| 1693 | } |
| 1694 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1695 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1696 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1697 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1698 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1699 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1700 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1701 | } |
| 1702 | break; |
| 1703 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1704 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1705 | break; |
| 1706 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1707 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1708 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1709 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1710 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1711 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1712 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1713 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | case Expand: { |
| 1716 | SDOperand Lo, Hi; |
Dan Gohman | 6595cb3 | 2007-06-27 16:08:04 +0000 | [diff] [blame] | 1717 | assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) && |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1718 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1719 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1720 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1721 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1722 | if (Hi.Val) { |
| 1723 | NewValues.push_back(Hi); |
| 1724 | NewValues.push_back(Node->getOperand(i+1)); |
| 1725 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1726 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1727 | } |
| 1728 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1729 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1730 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1733 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1734 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1735 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 1736 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1737 | break; |
| 1738 | } |
| 1739 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1740 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1741 | if (Result.getOpcode() == ISD::RET) { |
| 1742 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1743 | default: assert(0 && "This action is not supported yet!"); |
| 1744 | case TargetLowering::Legal: break; |
| 1745 | case TargetLowering::Custom: |
| 1746 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1747 | if (Tmp1.Val) Result = Tmp1; |
| 1748 | break; |
| 1749 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1750 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1751 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1752 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1753 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1754 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1755 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1756 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1757 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1758 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 1759 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 1760 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 1761 | // to phase ordering between legalized code and the dag combiner. This |
| 1762 | // probably means that we need to integrate dag combiner and legalizer |
| 1763 | // together. |
| 1764 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
| 1765 | if (CFP->getValueType(0) == MVT::f32) { |
| 1766 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
| 1767 | } else { |
| 1768 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
| 1769 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
| 1770 | } |
| 1771 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1772 | ST->getSrcValueOffset()); |
| 1773 | break; |
| 1774 | } |
| 1775 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1776 | switch (getTypeAction(ST->getStoredVT())) { |
| 1777 | case Legal: { |
| 1778 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1779 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1780 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1781 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1782 | MVT::ValueType VT = Tmp3.getValueType(); |
| 1783 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1784 | default: assert(0 && "This action is not supported yet!"); |
| 1785 | case TargetLowering::Legal: break; |
| 1786 | case TargetLowering::Custom: |
| 1787 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1788 | if (Tmp1.Val) Result = Tmp1; |
| 1789 | break; |
| 1790 | case TargetLowering::Promote: |
| 1791 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 1792 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 1793 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 1794 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
| 1795 | ST->getSrcValue(), ST->getSrcValueOffset()); |
| 1796 | break; |
| 1797 | } |
| 1798 | break; |
| 1799 | } |
| 1800 | case Promote: |
| 1801 | // Truncate the value and store the result. |
| 1802 | Tmp3 = PromoteOp(ST->getValue()); |
| 1803 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1804 | ST->getSrcValueOffset(), ST->getStoredVT()); |
| 1805 | break; |
| 1806 | |
| 1807 | case Expand: |
| 1808 | unsigned IncrementSize = 0; |
| 1809 | SDOperand Lo, Hi; |
| 1810 | |
| 1811 | // If this is a vector type, then we have to calculate the increment as |
| 1812 | // the product of the element size in bytes, and the number of elements |
| 1813 | // in the high half of the vector. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1814 | if (MVT::isVector(ST->getValue().getValueType())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1815 | SDNode *InVal = ST->getValue().Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1816 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 1817 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1818 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1819 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1820 | // type. If so, convert to the vector type. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1821 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1822 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1823 | // Turn this into a normal store of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1824 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1825 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1826 | ST->getSrcValueOffset(), |
| 1827 | ST->isVolatile(), |
| 1828 | ST->getAlignment()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1829 | Result = LegalizeOp(Result); |
| 1830 | break; |
| 1831 | } else if (NumElems == 1) { |
| 1832 | // Turn this into a normal store of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1833 | Tmp3 = ScalarizeVectorOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1834 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1835 | ST->getSrcValueOffset(), |
| 1836 | ST->isVolatile(), |
| 1837 | ST->getAlignment()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1838 | // The scalarized value type may not be legal, e.g. it might require |
| 1839 | // promotion or expansion. Relegalize the scalar store. |
| 1840 | Result = LegalizeOp(Result); |
| 1841 | break; |
| 1842 | } else { |
| 1843 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1844 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1845 | } |
| 1846 | } else { |
| 1847 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1848 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (!TLI.isLittleEndian()) |
| 1851 | std::swap(Lo, Hi); |
| 1852 | } |
| 1853 | |
| 1854 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
Chris Lattner | b464c44 | 2007-05-05 19:39:05 +0000 | [diff] [blame] | 1855 | ST->getSrcValueOffset(), ST->isVolatile(), |
| 1856 | ST->getAlignment()); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 1857 | |
| 1858 | if (Hi.Val == NULL) { |
| 1859 | // Must be int <-> float one-to-one expansion. |
| 1860 | Result = Lo; |
| 1861 | break; |
| 1862 | } |
| 1863 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1864 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1865 | getIntPtrConstant(IncrementSize)); |
| 1866 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1867 | "Pointers must be legal!"); |
| 1868 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1869 | // wrong. |
| 1870 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
Chris Lattner | b464c44 | 2007-05-05 19:39:05 +0000 | [diff] [blame] | 1871 | ST->getSrcValueOffset(), ST->isVolatile(), |
| 1872 | std::min(ST->getAlignment(), IncrementSize)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1873 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1874 | break; |
| 1875 | } |
| 1876 | } else { |
| 1877 | // Truncating store |
| 1878 | assert(isTypeLegal(ST->getValue().getValueType()) && |
| 1879 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1880 | Tmp3 = LegalizeOp(ST->getValue()); |
| 1881 | |
| 1882 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1883 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1884 | if (ST->getStoredVT() == MVT::i1 && |
| 1885 | TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) { |
| 1886 | // Promote the bool to a mask then store. |
| 1887 | Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3, |
| 1888 | DAG.getConstant(1, Tmp3.getValueType())); |
| 1889 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1890 | ST->getSrcValueOffset(), MVT::i8); |
| 1891 | } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 1892 | Tmp2 != ST->getBasePtr()) { |
| 1893 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1894 | ST->getOffset()); |
| 1895 | } |
| 1896 | |
| 1897 | MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT(); |
| 1898 | switch (TLI.getStoreXAction(StVT)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1899 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1900 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1901 | case TargetLowering::Custom: |
| 1902 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1903 | if (Tmp1.Val) Result = Tmp1; |
| 1904 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1905 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1906 | } |
| 1907 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1908 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1909 | case ISD::PCMARKER: |
| 1910 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1911 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1912 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1913 | case ISD::STACKSAVE: |
| 1914 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1915 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1916 | Tmp1 = Result.getValue(0); |
| 1917 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1919 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1920 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1921 | case TargetLowering::Legal: break; |
| 1922 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1923 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1924 | if (Tmp3.Val) { |
| 1925 | Tmp1 = LegalizeOp(Tmp3); |
| 1926 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1927 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1928 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1929 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1930 | // Expand to CopyFromReg if the target set |
| 1931 | // StackPointerRegisterToSaveRestore. |
| 1932 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1933 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1934 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1935 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1936 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1937 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1938 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1939 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1940 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1941 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1942 | |
| 1943 | // Since stacksave produce two values, make sure to remember that we |
| 1944 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1945 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1946 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1947 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1948 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1949 | case ISD::STACKRESTORE: |
| 1950 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1951 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1952 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1953 | |
| 1954 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1955 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1956 | case TargetLowering::Legal: break; |
| 1957 | case TargetLowering::Custom: |
| 1958 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1959 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1962 | // Expand to CopyToReg if the target set |
| 1963 | // StackPointerRegisterToSaveRestore. |
| 1964 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1965 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1966 | } else { |
| 1967 | Result = Tmp1; |
| 1968 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1969 | break; |
| 1970 | } |
| 1971 | break; |
| 1972 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1973 | case ISD::READCYCLECOUNTER: |
| 1974 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1975 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1976 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 1977 | Node->getValueType(0))) { |
| 1978 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1979 | case TargetLowering::Legal: |
| 1980 | Tmp1 = Result.getValue(0); |
| 1981 | Tmp2 = Result.getValue(1); |
| 1982 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1983 | case TargetLowering::Custom: |
| 1984 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1985 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 1986 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 1987 | break; |
| 1988 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1989 | |
| 1990 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1991 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 1992 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1993 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1994 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1996 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1997 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1998 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1999 | case Legal: |
| 2000 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2001 | break; |
| 2002 | case Promote: |
| 2003 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2004 | // Make sure the condition is either zero or one. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2005 | if (!DAG.MaskedValueIsZero(Tmp1, |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2006 | MVT::getIntVTBitMask(Tmp1.getValueType())^1)) |
| 2007 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2008 | break; |
| 2009 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2010 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2011 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2013 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2014 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2015 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2016 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2017 | case TargetLowering::Legal: break; |
| 2018 | case TargetLowering::Custom: { |
| 2019 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2020 | if (Tmp1.Val) Result = Tmp1; |
| 2021 | break; |
| 2022 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2023 | case TargetLowering::Expand: |
| 2024 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 2025 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 2026 | Tmp2, Tmp3, |
| 2027 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 2028 | } else { |
| 2029 | Result = DAG.getSelectCC(Tmp1, |
| 2030 | DAG.getConstant(0, Tmp1.getValueType()), |
| 2031 | Tmp2, Tmp3, ISD::SETNE); |
| 2032 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2033 | break; |
| 2034 | case TargetLowering::Promote: { |
| 2035 | MVT::ValueType NVT = |
| 2036 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 2037 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 2038 | if (MVT::isVector(Tmp2.getValueType())) { |
| 2039 | ExtOp = ISD::BIT_CONVERT; |
| 2040 | TruncOp = ISD::BIT_CONVERT; |
| 2041 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2042 | ExtOp = ISD::ANY_EXTEND; |
| 2043 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2044 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2045 | ExtOp = ISD::FP_EXTEND; |
| 2046 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2047 | } |
| 2048 | // Promote each of the values to the new type. |
| 2049 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 2050 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 2051 | // Perform the larger operation, then round down. |
| 2052 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 2053 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2057 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2058 | case ISD::SELECT_CC: { |
| 2059 | Tmp1 = Node->getOperand(0); // LHS |
| 2060 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2061 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 2062 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2063 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2064 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2065 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 2066 | |
| 2067 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 2068 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 2069 | // the result against zero to select between true and false values. |
| 2070 | if (Tmp2.Val == 0) { |
| 2071 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2072 | CC = DAG.getCondCode(ISD::SETNE); |
| 2073 | } |
| 2074 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 2075 | |
| 2076 | // Everything is legal, see if we should expand this op or something. |
| 2077 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 2078 | default: assert(0 && "This action is not supported yet!"); |
| 2079 | case TargetLowering::Legal: break; |
| 2080 | case TargetLowering::Custom: |
| 2081 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2082 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2083 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2084 | } |
| 2085 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2086 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2087 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2088 | Tmp1 = Node->getOperand(0); |
| 2089 | Tmp2 = Node->getOperand(1); |
| 2090 | Tmp3 = Node->getOperand(2); |
| 2091 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 2092 | |
| 2093 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2094 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2095 | // return the value we legalized, returned in the LHS |
| 2096 | if (Tmp2.Val == 0) { |
| 2097 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2098 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2099 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2101 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2102 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2103 | case TargetLowering::Custom: |
| 2104 | isCustom = true; |
| 2105 | // FALLTHROUGH. |
| 2106 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2107 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2108 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2109 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2110 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2111 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2112 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2113 | case TargetLowering::Promote: { |
| 2114 | // First step, figure out the appropriate operation to use. |
| 2115 | // Allow SETCC to not be supported for all legal data types |
| 2116 | // Mostly this targets FP |
| 2117 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 2118 | MVT::ValueType OldVT = NewInTy; OldVT = OldVT; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2119 | |
| 2120 | // Scan for the appropriate larger type to use. |
| 2121 | while (1) { |
| 2122 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2123 | |
| 2124 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2125 | "Fell off of the edge of the integer world"); |
| 2126 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2127 | "Fell off of the edge of the floating point world"); |
| 2128 | |
| 2129 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2130 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2131 | break; |
| 2132 | } |
| 2133 | if (MVT::isInteger(NewInTy)) |
| 2134 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2135 | else { |
| 2136 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2137 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2138 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2139 | Tmp1 = LegalizeOp(Tmp1); |
| 2140 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2141 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2142 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2143 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2144 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2145 | case TargetLowering::Expand: |
| 2146 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2147 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2148 | MVT::ValueType VT = Node->getValueType(0); |
| 2149 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2150 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2151 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2152 | break; |
| 2153 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2154 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2155 | case ISD::MEMSET: |
| 2156 | case ISD::MEMCPY: |
| 2157 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2158 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2159 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 2160 | |
| 2161 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 2162 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 2163 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 2164 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2165 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2166 | break; |
| 2167 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2168 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2169 | break; |
| 2170 | } |
| 2171 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2172 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2173 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2174 | |
| 2175 | SDOperand Tmp4; |
| 2176 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2177 | case Expand: { |
| 2178 | // Length is too big, just take the lo-part of the length. |
| 2179 | SDOperand HiPart; |
Chris Lattner | fa9aa2b | 2006-11-07 04:11:44 +0000 | [diff] [blame] | 2180 | ExpandOp(Node->getOperand(3), Tmp4, HiPart); |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2181 | break; |
| 2182 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2183 | case Legal: |
| 2184 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | case Promote: |
| 2187 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | SDOperand Tmp5; |
| 2192 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 2193 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2194 | case Legal: |
| 2195 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 2196 | break; |
| 2197 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2198 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 2199 | break; |
| 2200 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2201 | |
| 2202 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2203 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2204 | case TargetLowering::Custom: |
| 2205 | isCustom = true; |
| 2206 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2207 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2208 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2209 | if (isCustom) { |
| 2210 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2211 | if (Tmp1.Val) Result = Tmp1; |
| 2212 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2213 | break; |
| 2214 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2215 | // Otherwise, the target does not support this operation. Lower the |
| 2216 | // operation to an explicit libcall as appropriate. |
| 2217 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2218 | const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2219 | TargetLowering::ArgListTy Args; |
| 2220 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2221 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 2222 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2223 | if (Node->getOpcode() == ISD::MEMSET) { |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2224 | Entry.Node = Tmp2; Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2225 | Args.push_back(Entry); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 2226 | // Extend the (previously legalized) ubyte argument to be an int value |
| 2227 | // for the call. |
| 2228 | if (Tmp3.getValueType() > MVT::i32) |
| 2229 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 2230 | else |
| 2231 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2232 | Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2233 | Args.push_back(Entry); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2234 | Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2235 | Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2236 | |
| 2237 | FnName = "memset"; |
| 2238 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2239 | Node->getOpcode() == ISD::MEMMOVE) { |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame] | 2240 | Entry.Ty = IntPtrTy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 2241 | Entry.Node = Tmp2; Args.push_back(Entry); |
| 2242 | Entry.Node = Tmp3; Args.push_back(Entry); |
| 2243 | Entry.Node = Tmp4; Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2244 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2245 | } else { |
| 2246 | assert(0 && "Unknown op!"); |
| 2247 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2249 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2250 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2251 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2252 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2255 | } |
| 2256 | break; |
| 2257 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2259 | case ISD::SHL_PARTS: |
| 2260 | case ISD::SRA_PARTS: |
| 2261 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2262 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2263 | bool Changed = false; |
| 2264 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2265 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2266 | Changed |= Ops.back() != Node->getOperand(i); |
| 2267 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2268 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2269 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2270 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2271 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2272 | Node->getValueType(0))) { |
| 2273 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2274 | case TargetLowering::Legal: break; |
| 2275 | case TargetLowering::Custom: |
| 2276 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2277 | if (Tmp1.Val) { |
| 2278 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2279 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2280 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2281 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2282 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2283 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2284 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2285 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2286 | return RetVal; |
| 2287 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2288 | break; |
| 2289 | } |
| 2290 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2291 | // Since these produce multiple values, make sure to remember that we |
| 2292 | // legalized all of them. |
| 2293 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2294 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2295 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2296 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2297 | |
| 2298 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2299 | case ISD::ADD: |
| 2300 | case ISD::SUB: |
| 2301 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2302 | case ISD::MULHS: |
| 2303 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2304 | case ISD::UDIV: |
| 2305 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2306 | case ISD::AND: |
| 2307 | case ISD::OR: |
| 2308 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2309 | case ISD::SHL: |
| 2310 | case ISD::SRL: |
| 2311 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2312 | case ISD::FADD: |
| 2313 | case ISD::FSUB: |
| 2314 | case ISD::FMUL: |
| 2315 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2316 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2317 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2318 | case Expand: assert(0 && "Not possible"); |
| 2319 | case Legal: |
| 2320 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2321 | break; |
| 2322 | case Promote: |
| 2323 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2324 | break; |
| 2325 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2326 | |
| 2327 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2328 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2329 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2330 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2331 | case TargetLowering::Legal: break; |
| 2332 | case TargetLowering::Custom: |
| 2333 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2334 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2335 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2336 | case TargetLowering::Expand: { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2337 | if (Node->getValueType(0) == MVT::i32) { |
| 2338 | switch (Node->getOpcode()) { |
| 2339 | default: assert(0 && "Do not know how to expand this integer BinOp!"); |
| 2340 | case ISD::UDIV: |
| 2341 | case ISD::SDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2342 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV |
| 2343 | ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2344 | SDOperand Dummy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2345 | bool isSigned = Node->getOpcode() == ISD::SDIV; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2346 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2347 | }; |
| 2348 | break; |
| 2349 | } |
| 2350 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2351 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2352 | "Cannot expand this binary operator!"); |
| 2353 | // Expand the operation into a bunch of nasty scalar code. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2354 | SmallVector<SDOperand, 8> Ops; |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2355 | MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0)); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2356 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2357 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2358 | i != e; ++i) { |
| 2359 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2360 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2361 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2362 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2363 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2364 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), |
| 2365 | &Ops[0], Ops.size()); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2368 | case TargetLowering::Promote: { |
| 2369 | switch (Node->getOpcode()) { |
| 2370 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2371 | case ISD::AND: |
| 2372 | case ISD::OR: |
| 2373 | case ISD::XOR: { |
| 2374 | MVT::ValueType OVT = Node->getValueType(0); |
| 2375 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2376 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 2377 | // Bit convert each of the values to the new type. |
| 2378 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 2379 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 2380 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2381 | // Bit convert the result back the original type. |
| 2382 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 2383 | break; |
| 2384 | } |
| 2385 | } |
| 2386 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2387 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2388 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2389 | |
| 2390 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2391 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2392 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2393 | case Expand: assert(0 && "Not possible"); |
| 2394 | case Legal: |
| 2395 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2396 | break; |
| 2397 | case Promote: |
| 2398 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2399 | break; |
| 2400 | } |
| 2401 | |
| 2402 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2403 | |
| 2404 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2405 | default: assert(0 && "Operation not supported"); |
| 2406 | case TargetLowering::Custom: |
| 2407 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2408 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2409 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2410 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2411 | case TargetLowering::Expand: { |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2412 | // If this target supports fabs/fneg natively and select is cheap, |
| 2413 | // do this efficiently. |
| 2414 | if (!TLI.isSelectExpensive() && |
| 2415 | TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) == |
| 2416 | TargetLowering::Legal && |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2417 | TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) == |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2418 | TargetLowering::Legal) { |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2419 | // Get the sign bit of the RHS. |
| 2420 | MVT::ValueType IVT = |
| 2421 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2422 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2423 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2424 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2425 | // Get the absolute value of the result. |
| 2426 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2427 | // Select between the nabs and abs value based on the sign bit of |
| 2428 | // the input. |
| 2429 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2430 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2431 | AbsVal), |
| 2432 | AbsVal); |
| 2433 | Result = LegalizeOp(Result); |
| 2434 | break; |
| 2435 | } |
| 2436 | |
| 2437 | // Otherwise, do bitwise ops! |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2438 | MVT::ValueType NVT = |
| 2439 | Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2440 | Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 2441 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result); |
| 2442 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2443 | break; |
| 2444 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2445 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2446 | break; |
| 2447 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2448 | case ISD::ADDC: |
| 2449 | case ISD::SUBC: |
| 2450 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2451 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2452 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2453 | // Since this produces two values, make sure to remember that we legalized |
| 2454 | // both of them. |
| 2455 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2456 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2457 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2458 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2459 | case ISD::ADDE: |
| 2460 | case ISD::SUBE: |
| 2461 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2462 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2463 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2464 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2465 | // Since this produces two values, make sure to remember that we legalized |
| 2466 | // both of them. |
| 2467 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2468 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2469 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2470 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2471 | case ISD::BUILD_PAIR: { |
| 2472 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2473 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2474 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2475 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2476 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2477 | case TargetLowering::Promote: |
| 2478 | case TargetLowering::Custom: |
| 2479 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2480 | case TargetLowering::Legal: |
| 2481 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2482 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2483 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2484 | case TargetLowering::Expand: |
| 2485 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2486 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2487 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2488 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2489 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2490 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2491 | break; |
| 2492 | } |
| 2493 | break; |
| 2494 | } |
| 2495 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2496 | case ISD::UREM: |
| 2497 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2498 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2499 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2500 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2501 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2502 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2503 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2504 | case TargetLowering::Custom: |
| 2505 | isCustom = true; |
| 2506 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2507 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2508 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2509 | if (isCustom) { |
| 2510 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2511 | if (Tmp1.Val) Result = Tmp1; |
| 2512 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2513 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2514 | case TargetLowering::Expand: |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2515 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2516 | bool isSigned = DivOpc == ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2517 | if (MVT::isInteger(Node->getValueType(0))) { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2518 | if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) == |
| 2519 | TargetLowering::Legal) { |
| 2520 | // X % Y -> X-X/Y*Y |
| 2521 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2522 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2523 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2524 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2525 | } else { |
| 2526 | assert(Node->getValueType(0) == MVT::i32 && |
| 2527 | "Cannot expand this binary operator!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2528 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM |
| 2529 | ? RTLIB::UREM_I32 : RTLIB::SREM_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2530 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2531 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2532 | } |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2533 | } else { |
| 2534 | // Floating point mod -> fmod libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2535 | RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32 |
| 2536 | ? RTLIB::REM_F32 : RTLIB::REM_F64; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2537 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2538 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2539 | false/*sign irrelevant*/, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2540 | } |
| 2541 | break; |
| 2542 | } |
| 2543 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2544 | case ISD::VAARG: { |
| 2545 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2546 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2547 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2548 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2549 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2550 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2551 | case TargetLowering::Custom: |
| 2552 | isCustom = true; |
| 2553 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2554 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2555 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2556 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2557 | Tmp1 = Result.getValue(1); |
| 2558 | |
| 2559 | if (isCustom) { |
| 2560 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2561 | if (Tmp2.Val) { |
| 2562 | Result = LegalizeOp(Tmp2); |
| 2563 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2564 | } |
| 2565 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2566 | break; |
| 2567 | case TargetLowering::Expand: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2568 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2569 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2570 | SV->getValue(), SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2571 | // Increment the pointer, VAList, to the next vaarg |
| 2572 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2573 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2574 | TLI.getPointerTy())); |
| 2575 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2576 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 2577 | SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2578 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2579 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2580 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2581 | Result = LegalizeOp(Result); |
| 2582 | break; |
| 2583 | } |
| 2584 | } |
| 2585 | // Since VAARG produces two values, make sure to remember that we |
| 2586 | // legalized both of them. |
| 2587 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2588 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2589 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | case ISD::VACOPY: |
| 2593 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2594 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2595 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2596 | |
| 2597 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2598 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2599 | case TargetLowering::Custom: |
| 2600 | isCustom = true; |
| 2601 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2602 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2603 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2604 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2605 | if (isCustom) { |
| 2606 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2607 | if (Tmp1.Val) Result = Tmp1; |
| 2608 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | case TargetLowering::Expand: |
| 2611 | // This defaults to loading a pointer from the input and storing it to the |
| 2612 | // output, returning the chain. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2613 | SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2614 | SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2615 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(), |
| 2616 | SVD->getOffset()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2617 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(), |
| 2618 | SVS->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2619 | break; |
| 2620 | } |
| 2621 | break; |
| 2622 | |
| 2623 | case ISD::VAEND: |
| 2624 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2625 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2626 | |
| 2627 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2628 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2629 | case TargetLowering::Custom: |
| 2630 | isCustom = true; |
| 2631 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2632 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2633 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2634 | if (isCustom) { |
| 2635 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2636 | if (Tmp1.Val) Result = Tmp1; |
| 2637 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | case TargetLowering::Expand: |
| 2640 | Result = Tmp1; // Default to a no-op, return the chain |
| 2641 | break; |
| 2642 | } |
| 2643 | break; |
| 2644 | |
| 2645 | case ISD::VASTART: |
| 2646 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2647 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2648 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2649 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2650 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2651 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2652 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2653 | case TargetLowering::Legal: break; |
| 2654 | case TargetLowering::Custom: |
| 2655 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2656 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | break; |
| 2660 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2661 | case ISD::ROTL: |
| 2662 | case ISD::ROTR: |
| 2663 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2664 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2665 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2666 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2667 | default: |
| 2668 | assert(0 && "ROTL/ROTR legalize operation not supported"); |
| 2669 | break; |
| 2670 | case TargetLowering::Legal: |
| 2671 | break; |
| 2672 | case TargetLowering::Custom: |
| 2673 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2674 | if (Tmp1.Val) Result = Tmp1; |
| 2675 | break; |
| 2676 | case TargetLowering::Promote: |
| 2677 | assert(0 && "Do not know how to promote ROTL/ROTR"); |
| 2678 | break; |
| 2679 | case TargetLowering::Expand: |
| 2680 | assert(0 && "Do not know how to expand ROTL/ROTR"); |
| 2681 | break; |
| 2682 | } |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2683 | break; |
| 2684 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2685 | case ISD::BSWAP: |
| 2686 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2687 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2688 | case TargetLowering::Custom: |
| 2689 | assert(0 && "Cannot custom legalize this yet!"); |
| 2690 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2691 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2692 | break; |
| 2693 | case TargetLowering::Promote: { |
| 2694 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2695 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2696 | unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2698 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2699 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2700 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2701 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2702 | break; |
| 2703 | } |
| 2704 | case TargetLowering::Expand: |
| 2705 | Result = ExpandBSWAP(Tmp1); |
| 2706 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2707 | } |
| 2708 | break; |
| 2709 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2710 | case ISD::CTPOP: |
| 2711 | case ISD::CTTZ: |
| 2712 | case ISD::CTLZ: |
| 2713 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2714 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2715 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2716 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2717 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2718 | break; |
| 2719 | case TargetLowering::Promote: { |
| 2720 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2721 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2722 | |
| 2723 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2724 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2725 | // Perform the larger operation, then subtract if needed. |
| 2726 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2727 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2728 | case ISD::CTPOP: |
| 2729 | Result = Tmp1; |
| 2730 | break; |
| 2731 | case ISD::CTTZ: |
| 2732 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2733 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2734 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2735 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2736 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2737 | DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2738 | break; |
| 2739 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2740 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2741 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2742 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 2743 | MVT::getSizeInBits(OVT), NVT)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2744 | break; |
| 2745 | } |
| 2746 | break; |
| 2747 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2748 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2749 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2750 | break; |
| 2751 | } |
| 2752 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2753 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2754 | // Unary operators |
| 2755 | case ISD::FABS: |
| 2756 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2757 | case ISD::FSQRT: |
| 2758 | case ISD::FSIN: |
| 2759 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2760 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2761 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2762 | case TargetLowering::Promote: |
| 2763 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2764 | isCustom = true; |
| 2765 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2766 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2767 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2768 | if (isCustom) { |
| 2769 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2770 | if (Tmp1.Val) Result = Tmp1; |
| 2771 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2772 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2773 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2774 | switch (Node->getOpcode()) { |
| 2775 | default: assert(0 && "Unreachable!"); |
| 2776 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2777 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2778 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2779 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2780 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2781 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2782 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2783 | MVT::ValueType VT = Node->getValueType(0); |
| 2784 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2785 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2786 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2787 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | } |
| 2790 | case ISD::FSQRT: |
| 2791 | case ISD::FSIN: |
| 2792 | case ISD::FCOS: { |
| 2793 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2794 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2795 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2796 | case ISD::FSQRT: |
| 2797 | LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64; |
| 2798 | break; |
| 2799 | case ISD::FSIN: |
| 2800 | LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 2801 | break; |
| 2802 | case ISD::FCOS: |
| 2803 | LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 2804 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2805 | default: assert(0 && "Unreachable!"); |
| 2806 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2807 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2808 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2809 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2810 | break; |
| 2811 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2812 | } |
| 2813 | break; |
| 2814 | } |
| 2815 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2816 | case ISD::FPOWI: { |
| 2817 | // We always lower FPOWI into a libcall. No target support it yet. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2818 | RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32 |
| 2819 | ? RTLIB::POWI_F32 : RTLIB::POWI_F64; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2820 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2821 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2822 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 2823 | break; |
| 2824 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2825 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2826 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2827 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2828 | } else if (MVT::isVector(Op.getOperand(0).getValueType())) { |
| 2829 | // The input has to be a vector type, we have to either scalarize it, pack |
| 2830 | // it, or convert it based on whether the input vector type is legal. |
| 2831 | SDNode *InVal = Node->getOperand(0).Val; |
| 2832 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 2833 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
| 2834 | |
| 2835 | // Figure out if there is a simple type corresponding to this Vector |
| 2836 | // type. If so, convert to the vector type. |
| 2837 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 2838 | if (TLI.isTypeLegal(TVT)) { |
| 2839 | // Turn this into a bit convert of the packed input. |
| 2840 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2841 | LegalizeOp(Node->getOperand(0))); |
| 2842 | break; |
| 2843 | } else if (NumElems == 1) { |
| 2844 | // Turn this into a bit convert of the scalar input. |
| 2845 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 2846 | ScalarizeVectorOp(Node->getOperand(0))); |
| 2847 | break; |
| 2848 | } else { |
| 2849 | // FIXME: UNIMP! Store then reload |
| 2850 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 2851 | } |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2852 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2853 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2854 | Node->getOperand(0).getValueType())) { |
| 2855 | default: assert(0 && "Unknown operation action!"); |
| 2856 | case TargetLowering::Expand: |
| 2857 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2858 | break; |
| 2859 | case TargetLowering::Legal: |
| 2860 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2861 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2862 | break; |
| 2863 | } |
| 2864 | } |
| 2865 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 2866 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2867 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2868 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2869 | case ISD::UINT_TO_FP: { |
| 2870 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2871 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2872 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2873 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2874 | Node->getOperand(0).getValueType())) { |
| 2875 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2876 | case TargetLowering::Custom: |
| 2877 | isCustom = true; |
| 2878 | // FALLTHROUGH |
| 2879 | case TargetLowering::Legal: |
| 2880 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2881 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2882 | if (isCustom) { |
| 2883 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2884 | if (Tmp1.Val) Result = Tmp1; |
| 2885 | } |
| 2886 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2887 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2888 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2889 | LegalizeOp(Node->getOperand(0)), |
| 2890 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2891 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2892 | case TargetLowering::Promote: |
| 2893 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2894 | Node->getValueType(0), |
| 2895 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2896 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2897 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2898 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2899 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2900 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2901 | Node->getValueType(0), Node->getOperand(0)); |
| 2902 | break; |
| 2903 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2904 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2905 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2906 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2907 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2908 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2909 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2910 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2911 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2912 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2913 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2914 | break; |
| 2915 | } |
| 2916 | break; |
| 2917 | } |
| 2918 | case ISD::TRUNCATE: |
| 2919 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2920 | case Legal: |
| 2921 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2922 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2923 | break; |
| 2924 | case Expand: |
| 2925 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2926 | |
| 2927 | // Since the result is legal, we should just be able to truncate the low |
| 2928 | // part of the source. |
| 2929 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2930 | break; |
| 2931 | case Promote: |
| 2932 | Result = PromoteOp(Node->getOperand(0)); |
| 2933 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2934 | break; |
| 2935 | } |
| 2936 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2937 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2938 | case ISD::FP_TO_SINT: |
| 2939 | case ISD::FP_TO_UINT: |
| 2940 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2941 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2942 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2943 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2944 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2945 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2946 | case TargetLowering::Custom: |
| 2947 | isCustom = true; |
| 2948 | // FALLTHROUGH |
| 2949 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2950 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2951 | if (isCustom) { |
| 2952 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2953 | if (Tmp1.Val) Result = Tmp1; |
| 2954 | } |
| 2955 | break; |
| 2956 | case TargetLowering::Promote: |
| 2957 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2958 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2959 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2960 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2961 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2962 | SDOperand True, False; |
| 2963 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2964 | MVT::ValueType NVT = Node->getValueType(0); |
| 2965 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2966 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2967 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2968 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2969 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2970 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2971 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2972 | Tmp2)); |
| 2973 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2974 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2975 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2976 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2977 | } else { |
| 2978 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2979 | } |
| 2980 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2981 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2982 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2983 | case Expand: { |
| 2984 | // Convert f32 / f64 to i32 / i64. |
| 2985 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2986 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2987 | switch (Node->getOpcode()) { |
| 2988 | case ISD::FP_TO_SINT: |
| 2989 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2990 | LC = (VT == MVT::i32) |
| 2991 | ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2992 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2993 | LC = (VT == MVT::i32) |
| 2994 | ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 2995 | break; |
| 2996 | case ISD::FP_TO_UINT: |
| 2997 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2998 | LC = (VT == MVT::i32) |
| 2999 | ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3000 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3001 | LC = (VT == MVT::i32) |
| 3002 | ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | default: assert(0 && "Unreachable!"); |
| 3005 | } |
| 3006 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3007 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3008 | false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3009 | break; |
| 3010 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3011 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3012 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3013 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 3014 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3015 | break; |
| 3016 | } |
| 3017 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3018 | |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame^] | 3019 | case ISD::FP_ROUND: |
| 3020 | if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) == |
| 3021 | TargetLowering::Expand) { |
| 3022 | // The only way we can lower this is to turn it into a TRUNCSTORE, |
| 3023 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3024 | |
| 3025 | // NOTE: there is a choice here between constantly creating new stack |
| 3026 | // slots and always reusing the same one. We currently always create |
| 3027 | // new ones, as reuse may inhibit scheduling. |
| 3028 | MVT::ValueType VT = Op.getValueType(); // 32 |
| 3029 | const Type *Ty = MVT::getTypeForValueType(VT); |
| 3030 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
| 3031 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 3032 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3033 | int SSFI = |
| 3034 | MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 3035 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 3036 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3037 | StackSlot, NULL, 0, VT); |
| 3038 | Result = DAG.getLoad(VT, Result, StackSlot, NULL, 0, VT); |
| 3039 | break; |
| 3040 | } |
| 3041 | // FALL THROUGH |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3042 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3043 | case ISD::ZERO_EXTEND: |
| 3044 | case ISD::SIGN_EXTEND: |
| 3045 | case ISD::FP_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3046 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3047 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3048 | case Legal: |
| 3049 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3050 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3051 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3052 | case Promote: |
| 3053 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3054 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3055 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3056 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3057 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3058 | case ISD::ZERO_EXTEND: |
| 3059 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3060 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3061 | Result = DAG.getZeroExtendInReg(Result, |
| 3062 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3063 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3064 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3065 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3066 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3067 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3068 | Result, |
| 3069 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3070 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3071 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3072 | Result = PromoteOp(Node->getOperand(0)); |
| 3073 | if (Result.getValueType() != Op.getValueType()) |
| 3074 | // Dynamically dead while we have only 2 FP types. |
| 3075 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 3076 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3077 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 3078 | Result = PromoteOp(Node->getOperand(0)); |
| 3079 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 3080 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3081 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3082 | } |
| 3083 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3084 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3085 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3086 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3087 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3088 | |
| 3089 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 3090 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3091 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 3092 | default: assert(0 && "This action not supported for this op yet!"); |
| 3093 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3094 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3095 | break; |
| 3096 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3097 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3098 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3099 | // NOTE: we could fall back on load/store here too for targets without |
| 3100 | // SAR. However, it is doubtful that any exist. |
| 3101 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 3102 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 3103 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3104 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 3105 | Node->getOperand(0), ShiftCst); |
| 3106 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 3107 | Result, ShiftCst); |
| 3108 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 3109 | // 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] | 3110 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3111 | |
| 3112 | // NOTE: there is a choice here between constantly creating new stack |
| 3113 | // slots and always reusing the same one. We currently always create |
| 3114 | // new ones, as reuse may inhibit scheduling. |
| 3115 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 3116 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 3117 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3118 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3119 | int SSFI = |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 3120 | MF.getFrameInfo()->CreateStackObject(TySize, Align); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3121 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3122 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3123 | StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3124 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3125 | Result, StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3126 | } else { |
| 3127 | assert(0 && "Unknown op"); |
| 3128 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3129 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3130 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3131 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3132 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3133 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3134 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3135 | assert(Result.getValueType() == Op.getValueType() && |
| 3136 | "Bad legalization!"); |
| 3137 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3138 | // Make sure that the generated code is itself legal. |
| 3139 | if (Result != Op) |
| 3140 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3141 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3142 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3143 | // means that we always must cache transformed nodes. |
| 3144 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3145 | return Result; |
| 3146 | } |
| 3147 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3148 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 3149 | /// promote it to compute the value into a larger type. The produced value will |
| 3150 | /// have the correct bits for the low portion of the register, but no guarantee |
| 3151 | /// 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] | 3152 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 3153 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3154 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3155 | assert(getTypeAction(VT) == Promote && |
| 3156 | "Caller should expand or legalize operands that are not promotable!"); |
| 3157 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 3158 | "Cannot promote to smaller type!"); |
| 3159 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3160 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3161 | SDOperand Result; |
| 3162 | SDNode *Node = Op.Val; |
| 3163 | |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 3164 | DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3165 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3166 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3167 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3168 | case ISD::CopyFromReg: |
| 3169 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3170 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3171 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 3172 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3173 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3174 | assert(0 && "Do not know how to promote this operator!"); |
| 3175 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 3176 | case ISD::UNDEF: |
| 3177 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 3178 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3179 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 3180 | if (VT != MVT::i1) |
| 3181 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 3182 | else |
| 3183 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3184 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 3185 | break; |
| 3186 | case ISD::ConstantFP: |
| 3187 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 3188 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 3189 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 3190 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3191 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3192 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3193 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 3194 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3195 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3196 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3197 | case ISD::TRUNCATE: |
| 3198 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3199 | case Legal: |
| 3200 | Result = LegalizeOp(Node->getOperand(0)); |
| 3201 | assert(Result.getValueType() >= NVT && |
| 3202 | "This truncation doesn't make sense!"); |
| 3203 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 3204 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 3205 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 3206 | case Promote: |
| 3207 | // The truncation is not required, because we don't guarantee anything |
| 3208 | // about high bits anyway. |
| 3209 | Result = PromoteOp(Node->getOperand(0)); |
| 3210 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3211 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 3212 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3213 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 3214 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3215 | } |
| 3216 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3217 | case ISD::SIGN_EXTEND: |
| 3218 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3219 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3220 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3221 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 3222 | case Legal: |
| 3223 | // 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] | 3224 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3225 | break; |
| 3226 | case Promote: |
| 3227 | // Promote the reg if it's smaller. |
| 3228 | Result = PromoteOp(Node->getOperand(0)); |
| 3229 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 3230 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 3231 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3232 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3233 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3234 | Result = DAG.getZeroExtendInReg(Result, |
| 3235 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3236 | break; |
| 3237 | } |
| 3238 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3239 | case ISD::BIT_CONVERT: |
| 3240 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 3241 | Result = PromoteOp(Result); |
| 3242 | break; |
| 3243 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3244 | case ISD::FP_EXTEND: |
| 3245 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 3246 | case ISD::FP_ROUND: |
| 3247 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3248 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 3249 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 3250 | case Legal: |
| 3251 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3252 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3253 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3254 | break; |
| 3255 | } |
| 3256 | break; |
| 3257 | |
| 3258 | case ISD::SINT_TO_FP: |
| 3259 | case ISD::UINT_TO_FP: |
| 3260 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3261 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3262 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3263 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3264 | break; |
| 3265 | |
| 3266 | case Promote: |
| 3267 | Result = PromoteOp(Node->getOperand(0)); |
| 3268 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 3269 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3270 | Result, |
| 3271 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3272 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3273 | Result = DAG.getZeroExtendInReg(Result, |
| 3274 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3275 | // No extra round required here. |
| 3276 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3277 | break; |
| 3278 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3279 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 3280 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3281 | // Round if we cannot tolerate excess precision. |
| 3282 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3283 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3284 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3285 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3286 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3287 | break; |
| 3288 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 3289 | case ISD::SIGN_EXTEND_INREG: |
| 3290 | Result = PromoteOp(Node->getOperand(0)); |
| 3291 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 3292 | Node->getOperand(1)); |
| 3293 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3294 | case ISD::FP_TO_SINT: |
| 3295 | case ISD::FP_TO_UINT: |
| 3296 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3297 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 3298 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3299 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3300 | break; |
| 3301 | case Promote: |
| 3302 | // The input result is prerounded, so we don't have to do anything |
| 3303 | // special. |
| 3304 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3305 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3306 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3307 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 3308 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 3309 | // we can use that instead. This allows us to generate better code for |
| 3310 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 3311 | // legal, such as PowerPC. |
| 3312 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3313 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 3314 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 3315 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3316 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 3317 | } else { |
| 3318 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3319 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3320 | break; |
| 3321 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3322 | case ISD::FABS: |
| 3323 | case ISD::FNEG: |
| 3324 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3325 | assert(Tmp1.getValueType() == NVT); |
| 3326 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3327 | // NOTE: we do not have to do any extra rounding here for |
| 3328 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 3329 | // precision, and these operations don't modify precision at all. |
| 3330 | break; |
| 3331 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3332 | case ISD::FSQRT: |
| 3333 | case ISD::FSIN: |
| 3334 | case ISD::FCOS: |
| 3335 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3336 | assert(Tmp1.getValueType() == NVT); |
| 3337 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3338 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3339 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3340 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3341 | break; |
| 3342 | |
Chris Lattner | 8b2d42c | 2007-03-03 23:43:21 +0000 | [diff] [blame] | 3343 | case ISD::FPOWI: { |
| 3344 | // Promote f32 powi to f64 powi. Note that this could insert a libcall |
| 3345 | // directly as well, which may be better. |
| 3346 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3347 | assert(Tmp1.getValueType() == NVT); |
| 3348 | Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1)); |
| 3349 | if (NoExcessFPPrecision) |
| 3350 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3351 | DAG.getValueType(VT)); |
| 3352 | break; |
| 3353 | } |
| 3354 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3355 | case ISD::AND: |
| 3356 | case ISD::OR: |
| 3357 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3358 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3359 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3360 | case ISD::MUL: |
| 3361 | // 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] | 3362 | // 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] | 3363 | // that too is okay if they are integer operations. |
| 3364 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3365 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3366 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3367 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3368 | break; |
| 3369 | case ISD::FADD: |
| 3370 | case ISD::FSUB: |
| 3371 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3372 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3373 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3374 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3375 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3376 | |
| 3377 | // Floating point operations will give excess precision that we may not be |
| 3378 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 3379 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3380 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 3381 | // 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] | 3382 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3383 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3384 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3385 | break; |
| 3386 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3387 | case ISD::SDIV: |
| 3388 | case ISD::SREM: |
| 3389 | // These operators require that their input be sign extended. |
| 3390 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3391 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3392 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3393 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3394 | DAG.getValueType(VT)); |
| 3395 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3396 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3397 | } |
| 3398 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3399 | |
| 3400 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 3401 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3402 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3403 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3404 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3405 | case ISD::FDIV: |
| 3406 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3407 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3408 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 3409 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3410 | case Legal: |
| 3411 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3412 | break; |
| 3413 | case Promote: |
| 3414 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3415 | break; |
| 3416 | case Expand: |
| 3417 | assert(0 && "not implemented"); |
| 3418 | } |
| 3419 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3420 | case Legal: |
| 3421 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3422 | break; |
| 3423 | case Promote: |
| 3424 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3425 | break; |
| 3426 | case Expand: |
| 3427 | assert(0 && "not implemented"); |
| 3428 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3429 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3430 | |
| 3431 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3432 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3433 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3434 | DAG.getValueType(VT)); |
| 3435 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3436 | |
| 3437 | case ISD::UDIV: |
| 3438 | case ISD::UREM: |
| 3439 | // These operators require that their input be zero extended. |
| 3440 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3441 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3442 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3443 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3444 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3445 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3446 | break; |
| 3447 | |
| 3448 | case ISD::SHL: |
| 3449 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3450 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | case ISD::SRA: |
| 3453 | // The input value must be properly sign extended. |
| 3454 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3455 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3456 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3457 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3458 | break; |
| 3459 | case ISD::SRL: |
| 3460 | // The input value must be properly zero extended. |
| 3461 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3462 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3463 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3464 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3465 | |
| 3466 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3467 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 3468 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3469 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 3470 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 3471 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 3472 | } else { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3473 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3474 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3475 | SV->getValue(), SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3476 | // Increment the pointer, VAList, to the next vaarg |
| 3477 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3478 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3479 | TLI.getPointerTy())); |
| 3480 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3481 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 3482 | SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3483 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3484 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3485 | } |
| 3486 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3487 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3490 | case ISD::LOAD: { |
| 3491 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 3492 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 3493 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 3494 | Result = DAG.getExtLoad(ExtType, NVT, |
| 3495 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 3496 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 3497 | LD->getLoadedVT()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3498 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3499 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3500 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3501 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3502 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3503 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3504 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3505 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3506 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3507 | case ISD::SELECT_CC: |
| 3508 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3509 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3510 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3511 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3512 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3513 | case ISD::BSWAP: |
| 3514 | Tmp1 = Node->getOperand(0); |
| 3515 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3516 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3517 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3518 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3519 | MVT::getSizeInBits(VT), |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3520 | TLI.getShiftAmountTy())); |
| 3521 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3522 | case ISD::CTPOP: |
| 3523 | case ISD::CTTZ: |
| 3524 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3525 | // Zero extend the argument |
| 3526 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3527 | // Perform the larger operation, then subtract if needed. |
| 3528 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3529 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3530 | case ISD::CTPOP: |
| 3531 | Result = Tmp1; |
| 3532 | break; |
| 3533 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3534 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3535 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3536 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
| 3537 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3538 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3539 | DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3540 | break; |
| 3541 | case ISD::CTLZ: |
| 3542 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3543 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3544 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3545 | MVT::getSizeInBits(VT), NVT)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3546 | break; |
| 3547 | } |
| 3548 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3549 | case ISD::EXTRACT_SUBVECTOR: |
| 3550 | Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op)); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3551 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3552 | case ISD::EXTRACT_VECTOR_ELT: |
| 3553 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3554 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3558 | |
| 3559 | // Make sure the result is itself legal. |
| 3560 | Result = LegalizeOp(Result); |
| 3561 | |
| 3562 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3563 | AddPromotedOperand(Op, Result); |
| 3564 | return Result; |
| 3565 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3566 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3567 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3568 | /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic, |
| 3569 | /// based on the vector type. The return type of this matches the element type |
| 3570 | /// of the vector, which may not be legal for the target. |
| 3571 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3572 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3573 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3574 | // lower to a store then an indexed load. |
| 3575 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3576 | SDOperand Idx = Op.getOperand(1); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3577 | |
| 3578 | SDNode *InVal = Vec.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3579 | MVT::ValueType TVT = InVal->getValueType(0); |
| 3580 | unsigned NumElems = MVT::getVectorNumElements(TVT); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3581 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3582 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) { |
| 3583 | default: assert(0 && "This action is not supported yet!"); |
| 3584 | case TargetLowering::Custom: { |
| 3585 | Vec = LegalizeOp(Vec); |
| 3586 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3587 | SDOperand Tmp3 = TLI.LowerOperation(Op, DAG); |
| 3588 | if (Tmp3.Val) |
| 3589 | return Tmp3; |
| 3590 | break; |
| 3591 | } |
| 3592 | case TargetLowering::Legal: |
| 3593 | if (isTypeLegal(TVT)) { |
| 3594 | Vec = LegalizeOp(Vec); |
| 3595 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3596 | Op = LegalizeOp(Op); |
| 3597 | } |
| 3598 | break; |
| 3599 | case TargetLowering::Expand: |
| 3600 | break; |
| 3601 | } |
| 3602 | |
| 3603 | if (NumElems == 1) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3604 | // This must be an access of the only element. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3605 | Op = ScalarizeVectorOp(Vec); |
| 3606 | } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) { |
| 3607 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3608 | SDOperand Lo, Hi; |
| 3609 | SplitVectorOp(Vec, Lo, Hi); |
| 3610 | if (CIdx->getValue() < NumElems/2) { |
| 3611 | Vec = Lo; |
| 3612 | } else { |
| 3613 | Vec = Hi; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3614 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, |
| 3615 | Idx.getValueType()); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3616 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3617 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3618 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3619 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3620 | Op = ExpandEXTRACT_VECTOR_ELT(Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3621 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3622 | // Store the value to a temporary stack slot, then LOAD the scalar |
| 3623 | // element back out. |
| 3624 | SDOperand StackPtr = CreateStackTemporary(Vec.getValueType()); |
| 3625 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0); |
| 3626 | |
| 3627 | // Add the offset to the index. |
| 3628 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3629 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3630 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3631 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3632 | |
| 3633 | Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3634 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3635 | return Op; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3638 | /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3639 | /// we assume the operation can be split if it is not already legal. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3640 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) { |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3641 | // We know that operand #0 is the Vec vector. For now we assume the index |
| 3642 | // is a constant and that the extracted result is a supported hardware type. |
| 3643 | SDOperand Vec = Op.getOperand(0); |
| 3644 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3645 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3646 | unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3647 | |
| 3648 | if (NumElems == MVT::getVectorNumElements(Op.getValueType())) { |
| 3649 | // This must be an access of the desired vector length. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3650 | return Vec; |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
| 3654 | SDOperand Lo, Hi; |
| 3655 | SplitVectorOp(Vec, Lo, Hi); |
| 3656 | if (CIdx->getValue() < NumElems/2) { |
| 3657 | Vec = Lo; |
| 3658 | } else { |
| 3659 | Vec = Hi; |
| 3660 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3661 | } |
| 3662 | |
| 3663 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3664 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3665 | return ExpandEXTRACT_SUBVECTOR(Op); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3668 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3669 | /// with condition CC on the current target. This usually involves legalizing |
| 3670 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3671 | /// there may be no choice but to create a new SetCC node to represent the |
| 3672 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3673 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3674 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3675 | SDOperand &RHS, |
| 3676 | SDOperand &CC) { |
| 3677 | SDOperand Tmp1, Tmp2, Result; |
| 3678 | |
| 3679 | switch (getTypeAction(LHS.getValueType())) { |
| 3680 | case Legal: |
| 3681 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3682 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3683 | break; |
| 3684 | case Promote: |
| 3685 | Tmp1 = PromoteOp(LHS); // LHS |
| 3686 | Tmp2 = PromoteOp(RHS); // RHS |
| 3687 | |
| 3688 | // If this is an FP compare, the operands have already been extended. |
| 3689 | if (MVT::isInteger(LHS.getValueType())) { |
| 3690 | MVT::ValueType VT = LHS.getValueType(); |
| 3691 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3692 | |
| 3693 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3694 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3695 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3696 | // it. |
| 3697 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3698 | default: assert(0 && "Unknown integer comparison!"); |
| 3699 | case ISD::SETEQ: |
| 3700 | case ISD::SETNE: |
| 3701 | case ISD::SETUGE: |
| 3702 | case ISD::SETUGT: |
| 3703 | case ISD::SETULE: |
| 3704 | case ISD::SETULT: |
| 3705 | // ALL of these operations will work if we either sign or zero extend |
| 3706 | // the operands (including the unsigned comparisons!). Zero extend is |
| 3707 | // usually a simpler/cheaper operation, so prefer it. |
| 3708 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3709 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 3710 | break; |
| 3711 | case ISD::SETGE: |
| 3712 | case ISD::SETGT: |
| 3713 | case ISD::SETLT: |
| 3714 | case ISD::SETLE: |
| 3715 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3716 | DAG.getValueType(VT)); |
| 3717 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3718 | DAG.getValueType(VT)); |
| 3719 | break; |
| 3720 | } |
| 3721 | } |
| 3722 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3723 | case Expand: { |
| 3724 | MVT::ValueType VT = LHS.getValueType(); |
| 3725 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 3726 | // Expand into one or more soft-fp libcall(s). |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3727 | RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3728 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3729 | case ISD::SETEQ: |
| 3730 | case ISD::SETOEQ: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3731 | LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3732 | break; |
| 3733 | case ISD::SETNE: |
| 3734 | case ISD::SETUNE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3735 | LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3736 | break; |
| 3737 | case ISD::SETGE: |
| 3738 | case ISD::SETOGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3739 | LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3740 | break; |
| 3741 | case ISD::SETLT: |
| 3742 | case ISD::SETOLT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3743 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3744 | break; |
| 3745 | case ISD::SETLE: |
| 3746 | case ISD::SETOLE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3747 | LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | case ISD::SETGT: |
| 3750 | case ISD::SETOGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3751 | LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3752 | break; |
| 3753 | case ISD::SETUO: |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3754 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
| 3755 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3756 | case ISD::SETO: |
Evan Cheng | 5efdecc | 2007-02-03 00:43:46 +0000 | [diff] [blame] | 3757 | LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3758 | break; |
| 3759 | default: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3760 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3761 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3762 | case ISD::SETONE: |
| 3763 | // SETONE = SETOLT | SETOGT |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3764 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3765 | // Fallthrough |
| 3766 | case ISD::SETUGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3767 | LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3768 | break; |
| 3769 | case ISD::SETUGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3770 | LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3771 | break; |
| 3772 | case ISD::SETULT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3773 | LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3774 | break; |
| 3775 | case ISD::SETULE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3776 | LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3777 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3778 | case ISD::SETUEQ: |
| 3779 | LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3780 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3781 | default: assert(0 && "Unsupported FP setcc!"); |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3786 | Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3787 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 3788 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3789 | Tmp2 = DAG.getConstant(0, MVT::i32); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3790 | CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1)); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3791 | if (LC2 != RTLIB::UNKNOWN_LIBCALL) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3792 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3793 | LHS = ExpandLibCall(TLI.getLibcallName(LC2), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3794 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 3795 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3796 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2, |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 3797 | DAG.getCondCode(TLI.getCmpLibcallCC(LC2))); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3798 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3799 | Tmp2 = SDOperand(); |
| 3800 | } |
| 3801 | LHS = Tmp1; |
| 3802 | RHS = Tmp2; |
| 3803 | return; |
| 3804 | } |
| 3805 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3806 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 3807 | ExpandOp(LHS, LHSLo, LHSHi); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3808 | ExpandOp(RHS, RHSLo, RHSHi); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3809 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3810 | case ISD::SETEQ: |
| 3811 | case ISD::SETNE: |
| 3812 | if (RHSLo == RHSHi) |
| 3813 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 3814 | if (RHSCST->isAllOnesValue()) { |
| 3815 | // Comparison to -1. |
| 3816 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 3817 | Tmp2 = RHSLo; |
| 3818 | break; |
| 3819 | } |
| 3820 | |
| 3821 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 3822 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 3823 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 3824 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3825 | break; |
| 3826 | default: |
| 3827 | // If this is a comparison of the sign bit, just look at the top part. |
| 3828 | // X > -1, x < 0 |
| 3829 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 3830 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 3831 | CST->getValue() == 0) || // X < 0 |
| 3832 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 3833 | CST->isAllOnesValue())) { // X > -1 |
| 3834 | Tmp1 = LHSHi; |
| 3835 | Tmp2 = RHSHi; |
| 3836 | break; |
| 3837 | } |
| 3838 | |
| 3839 | // FIXME: This generated code sucks. |
| 3840 | ISD::CondCode LowCC; |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 3841 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 3842 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3843 | default: assert(0 && "Unknown integer setcc!"); |
| 3844 | case ISD::SETLT: |
| 3845 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 3846 | case ISD::SETGT: |
| 3847 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 3848 | case ISD::SETLE: |
| 3849 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 3850 | case ISD::SETGE: |
| 3851 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 3852 | } |
| 3853 | |
| 3854 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 3855 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 3856 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 3857 | |
| 3858 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 3859 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 3860 | TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL); |
| 3861 | Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC, |
| 3862 | false, DagCombineInfo); |
| 3863 | if (!Tmp1.Val) |
| 3864 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 3865 | Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 3866 | CCCode, false, DagCombineInfo); |
| 3867 | if (!Tmp2.Val) |
| 3868 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 3869 | |
| 3870 | ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val); |
| 3871 | ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val); |
| 3872 | if ((Tmp1C && Tmp1C->getValue() == 0) || |
| 3873 | (Tmp2C && Tmp2C->getValue() == 0 && |
| 3874 | (CCCode == ISD::SETLE || CCCode == ISD::SETGE || |
| 3875 | CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) || |
| 3876 | (Tmp2C && Tmp2C->getValue() == 1 && |
| 3877 | (CCCode == ISD::SETLT || CCCode == ISD::SETGT || |
| 3878 | CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) { |
| 3879 | // low part is known false, returns high part. |
| 3880 | // For LE / GE, if high part is known false, ignore the low part. |
| 3881 | // For LT / GT, if high part is known true, ignore the low part. |
| 3882 | Tmp1 = Tmp2; |
| 3883 | Tmp2 = SDOperand(); |
| 3884 | } else { |
| 3885 | Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 3886 | ISD::SETEQ, false, DagCombineInfo); |
| 3887 | if (!Result.Val) |
| 3888 | Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 3889 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 3890 | Result, Tmp1, Tmp2)); |
| 3891 | Tmp1 = Result; |
| 3892 | Tmp2 = SDOperand(); |
| 3893 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 3896 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3897 | LHS = Tmp1; |
| 3898 | RHS = Tmp2; |
| 3899 | } |
| 3900 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3901 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 3902 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 3903 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3904 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 3905 | SDOperand SrcOp) { |
| 3906 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3907 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3908 | |
| 3909 | // Emit a store to the stack slot. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3910 | SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3911 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3912 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3915 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 3916 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 3917 | // then load the whole vector back out. |
| 3918 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 3919 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3920 | NULL, 0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3921 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3925 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
| 3926 | /// support the operation, but do support the resultant packed vector type. |
| 3927 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 3928 | |
| 3929 | // 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] | 3930 | // 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] | 3931 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3932 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3933 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3934 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 3935 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3936 | bool isConstant = true; |
| 3937 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 3938 | SplatValue.getOpcode() != ISD::UNDEF) |
| 3939 | isConstant = false; |
| 3940 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3941 | for (unsigned i = 1; i < NumElems; ++i) { |
| 3942 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 3943 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3944 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3945 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3946 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3947 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3948 | |
| 3949 | // If this isn't a constant element or an undef, we can't use a constant |
| 3950 | // pool load. |
| 3951 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 3952 | V.getOpcode() != ISD::UNDEF) |
| 3953 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | if (isOnlyLowElement) { |
| 3957 | // If the low element is an undef too, then this whole things is an undef. |
| 3958 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 3959 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 3960 | // Otherwise, turn this into a scalar_to_vector node. |
| 3961 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 3962 | Node->getOperand(0)); |
| 3963 | } |
| 3964 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3965 | // If all elements are constants, create a load from the constant pool. |
| 3966 | if (isConstant) { |
| 3967 | MVT::ValueType VT = Node->getValueType(0); |
| 3968 | const Type *OpNTy = |
| 3969 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 3970 | std::vector<Constant*> CV; |
| 3971 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 3972 | if (ConstantFPSDNode *V = |
| 3973 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
| 3974 | CV.push_back(ConstantFP::get(OpNTy, V->getValue())); |
| 3975 | } else if (ConstantSDNode *V = |
| 3976 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 3977 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3978 | } else { |
| 3979 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 3980 | CV.push_back(UndefValue::get(OpNTy)); |
| 3981 | } |
| 3982 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 3983 | Constant *CP = ConstantVector::get(CV); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3984 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3985 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3988 | if (SplatValue.Val) { // Splat of one value? |
| 3989 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 3990 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3991 | MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 3992 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 3993 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 3994 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 3995 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3996 | |
| 3997 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 3998 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 3999 | // Get the splatted value into the low element of a vector register. |
| 4000 | SDOperand LowValVec = |
| 4001 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 4002 | |
| 4003 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 4004 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 4005 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 4006 | SplatMask); |
| 4007 | } |
| 4008 | } |
| 4009 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4010 | // If there are only two unique elements, we may be able to turn this into a |
| 4011 | // vector shuffle. |
| 4012 | if (Values.size() == 2) { |
| 4013 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 4014 | MVT::ValueType MaskVT = |
| 4015 | MVT::getIntVectorWithNumElements(NumElems); |
| 4016 | std::vector<SDOperand> MaskVec(NumElems); |
| 4017 | unsigned i = 0; |
| 4018 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4019 | E = Values.end(); I != E; ++I) { |
| 4020 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 4021 | EE = I->second.end(); II != EE; ++II) |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4022 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4023 | i += NumElems; |
| 4024 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4025 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4026 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4027 | |
| 4028 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4029 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 4030 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4031 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4032 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4033 | E = Values.end(); I != E; ++I) { |
| 4034 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4035 | I->first); |
| 4036 | Ops.push_back(Op); |
| 4037 | } |
| 4038 | Ops.push_back(ShuffleMask); |
| 4039 | |
| 4040 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4041 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), |
| 4042 | &Ops[0], Ops.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4043 | } |
| 4044 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4045 | |
| 4046 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 4047 | // aligned object on the stack, store each element into it, then load |
| 4048 | // the result as a vector. |
| 4049 | MVT::ValueType VT = Node->getValueType(0); |
| 4050 | // Create the stack frame object. |
| 4051 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 4052 | |
| 4053 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4054 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4055 | unsigned TypeByteSize = |
| 4056 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4057 | // Store (in the right endianness) the elements to memory. |
| 4058 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4059 | // Ignore undef elements. |
| 4060 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 4061 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 4062 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4063 | |
| 4064 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 4065 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 4066 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4067 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4068 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | SDOperand StoreChain; |
| 4072 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4073 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4074 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4075 | else |
| 4076 | StoreChain = DAG.getEntryNode(); |
| 4077 | |
| 4078 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4079 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 4083 | /// specified value type. |
| 4084 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 4085 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 4086 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4087 | const Type *Ty = MVT::getTypeForValueType(VT); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4088 | unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4089 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4090 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 4091 | } |
| 4092 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4093 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 4094 | SDOperand Op, SDOperand Amt, |
| 4095 | SDOperand &Lo, SDOperand &Hi) { |
| 4096 | // Expand the subcomponents. |
| 4097 | SDOperand LHSL, LHSH; |
| 4098 | ExpandOp(Op, LHSL, LHSH); |
| 4099 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4100 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 4101 | MVT::ValueType VT = LHSL.getValueType(); |
| 4102 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4103 | Hi = Lo.getValue(1); |
| 4104 | } |
| 4105 | |
| 4106 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4107 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 4108 | /// smaller elements. If we can't find a way that is more efficient than a |
| 4109 | /// libcall on this target, return false. Otherwise, return true with the |
| 4110 | /// low-parts expanded into Lo and Hi. |
| 4111 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 4112 | SDOperand &Lo, SDOperand &Hi) { |
| 4113 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 4114 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4115 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4116 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4117 | SDOperand ShAmt = LegalizeOp(Amt); |
| 4118 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 4119 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 4120 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 4121 | |
| 4122 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 4123 | // and are disabled. |
| 4124 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 4125 | unsigned Cst = CN->getValue(); |
| 4126 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4127 | SDOperand InL, InH; |
| 4128 | ExpandOp(Op, InL, InH); |
| 4129 | switch(Opc) { |
| 4130 | case ISD::SHL: |
| 4131 | if (Cst > VTBits) { |
| 4132 | Lo = DAG.getConstant(0, NVT); |
| 4133 | Hi = DAG.getConstant(0, NVT); |
| 4134 | } else if (Cst > NVTBits) { |
| 4135 | Lo = DAG.getConstant(0, NVT); |
| 4136 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4137 | } else if (Cst == NVTBits) { |
| 4138 | Lo = DAG.getConstant(0, NVT); |
| 4139 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4140 | } else { |
| 4141 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 4142 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4143 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 4144 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4145 | } |
| 4146 | return true; |
| 4147 | case ISD::SRL: |
| 4148 | if (Cst > VTBits) { |
| 4149 | Lo = DAG.getConstant(0, NVT); |
| 4150 | Hi = DAG.getConstant(0, NVT); |
| 4151 | } else if (Cst > NVTBits) { |
| 4152 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 4153 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4154 | } else if (Cst == NVTBits) { |
| 4155 | Lo = InH; |
| 4156 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4157 | } else { |
| 4158 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4159 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4160 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4161 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4162 | } |
| 4163 | return true; |
| 4164 | case ISD::SRA: |
| 4165 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4166 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4167 | DAG.getConstant(NVTBits-1, ShTy)); |
| 4168 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4169 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4170 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4171 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4172 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4173 | } else if (Cst == NVTBits) { |
| 4174 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4175 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4176 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4177 | } else { |
| 4178 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4179 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4180 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4181 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4182 | } |
| 4183 | return true; |
| 4184 | } |
| 4185 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 4186 | |
| 4187 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 4188 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
| 4189 | uint64_t Mask = NVTBits, KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4190 | DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 4191 | |
| 4192 | // If we know that the high bit of the shift amount is one, then we can do |
| 4193 | // this as a couple of simple shifts. |
| 4194 | if (KnownOne & Mask) { |
| 4195 | // Mask out the high bit, which we know is set. |
| 4196 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
| 4197 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4198 | |
| 4199 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4200 | SDOperand InL, InH; |
| 4201 | ExpandOp(Op, InL, InH); |
| 4202 | switch(Opc) { |
| 4203 | case ISD::SHL: |
| 4204 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 4205 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 4206 | return true; |
| 4207 | case ISD::SRL: |
| 4208 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 4209 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 4210 | return true; |
| 4211 | case ISD::SRA: |
| 4212 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 4213 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4214 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 4215 | return true; |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | // If we know that the high bit of the shift amount is zero, then we can do |
| 4220 | // this as a couple of simple shifts. |
| 4221 | if (KnownZero & Mask) { |
| 4222 | // Compute 32-amt. |
| 4223 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 4224 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 4225 | Amt); |
| 4226 | |
| 4227 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4228 | SDOperand InL, InH; |
| 4229 | ExpandOp(Op, InL, InH); |
| 4230 | switch(Opc) { |
| 4231 | case ISD::SHL: |
| 4232 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 4233 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4234 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 4235 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 4236 | return true; |
| 4237 | case ISD::SRL: |
| 4238 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 4239 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4240 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4241 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4242 | return true; |
| 4243 | case ISD::SRA: |
| 4244 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 4245 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4246 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4247 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4248 | return true; |
| 4249 | } |
| 4250 | } |
| 4251 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4252 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4253 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4254 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4255 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4256 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 4257 | // does not fit into a register, return the lo part and set the hi part to the |
| 4258 | // by-reg argument. If it does fit into a single register, return the result |
| 4259 | // and leave the Hi part unset. |
| 4260 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4261 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4262 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 4263 | // The input chain to this libcall is the entry node of the function. |
| 4264 | // Legalizing the call will automatically add the previous call to the |
| 4265 | // dependence. |
| 4266 | SDOperand InChain = DAG.getEntryNode(); |
| 4267 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4268 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4269 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4270 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4271 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 4272 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4273 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 4274 | Entry.isSExt = isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4275 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4276 | } |
| 4277 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4278 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4279 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4280 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4281 | std::pair<SDOperand,SDOperand> CallInfo = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4282 | TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 4283 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 4284 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4285 | // Legalize the call sequence, starting with the chain. This will advance |
| 4286 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 4287 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 4288 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4289 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4290 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4291 | default: assert(0 && "Unknown thing"); |
| 4292 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4293 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4294 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4295 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4296 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4297 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4298 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4299 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4302 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4303 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation. |
| 4304 | /// |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4305 | SDOperand SelectionDAGLegalize:: |
| 4306 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4307 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 4308 | "This is not an expansion!"); |
| 4309 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 4310 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4311 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4312 | assert(Source.getValueType() == MVT::i64 && |
| 4313 | "This only works for 64-bit -> FP"); |
| 4314 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 4315 | // incoming integer is set. To handle this, we dynamically test to see if |
| 4316 | // it is set, and, if so, add a fudge factor. |
| 4317 | SDOperand Lo, Hi; |
| 4318 | ExpandOp(Source, Lo, Hi); |
| 4319 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 4320 | // If this is unsigned, and not supported, first perform the conversion to |
| 4321 | // signed, then adjust the result if the sign bit is set. |
| 4322 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 4323 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 4324 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4325 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 4326 | DAG.getConstant(0, Hi.getValueType()), |
| 4327 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4328 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4329 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4330 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 4331 | uint64_t FF = 0x5f800000ULL; |
| 4332 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4333 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4334 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 4335 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4336 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4337 | SDOperand FudgeInReg; |
| 4338 | if (DestTy == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4339 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4340 | else { |
| 4341 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4342 | // FIXME: Avoid the extend by construction the right constantpool? |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 4343 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4344 | CPIdx, NULL, 0, MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4345 | } |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4346 | MVT::ValueType SCVT = SignedConv.getValueType(); |
| 4347 | if (SCVT != DestTy) { |
| 4348 | // Destination type needs to be expanded as well. The FADD now we are |
| 4349 | // constructing will be expanded into a libcall. |
| 4350 | if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) { |
| 4351 | assert(SCVT == MVT::i32 && DestTy == MVT::f64); |
| 4352 | SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, |
| 4353 | SignedConv, SignedConv.getValue(1)); |
| 4354 | } |
| 4355 | SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv); |
| 4356 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 4357 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4358 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4359 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4360 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 4361 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 4362 | default: assert(0 && "This action not implemented for this operation!"); |
| 4363 | case TargetLowering::Legal: |
| 4364 | case TargetLowering::Expand: |
| 4365 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4366 | case TargetLowering::Custom: { |
| 4367 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 4368 | Source), DAG); |
| 4369 | if (NV.Val) |
| 4370 | return LegalizeOp(NV); |
| 4371 | break; // The target decided this was legal after all |
| 4372 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 4375 | // Expand the source, then glue it back together for the call. We must expand |
| 4376 | // the source in case it is shared (this pass of legalize must traverse it). |
| 4377 | SDOperand SrcLo, SrcHi; |
| 4378 | ExpandOp(Source, SrcLo, SrcHi); |
| 4379 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 4380 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4381 | RTLIB::Libcall LC; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4382 | if (DestTy == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4383 | LC = RTLIB::SINTTOFP_I64_F32; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4384 | else { |
| 4385 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4386 | LC = RTLIB::SINTTOFP_I64_F64; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4387 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4388 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4389 | assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4390 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 4391 | SDOperand UnusedHiPart; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4392 | return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned, |
| 4393 | UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4394 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4395 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4396 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 4397 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 4398 | /// we expand it. At this point, we know that the result and operand types are |
| 4399 | /// legal for the target. |
| 4400 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 4401 | SDOperand Op0, |
| 4402 | MVT::ValueType DestVT) { |
| 4403 | if (Op0.getValueType() == MVT::i32) { |
| 4404 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 4405 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4406 | // get the stack frame index of a 8 byte buffer, pessimistically aligned |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4407 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4408 | const Type *F64Type = MVT::getTypeForValueType(MVT::f64); |
| 4409 | unsigned StackAlign = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4410 | (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4411 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4412 | // get address of 8 byte buffer |
| 4413 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4414 | // word offset constant for Hi/Lo address computation |
| 4415 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 4416 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 4417 | SDOperand Hi = StackSlot; |
| 4418 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 4419 | if (TLI.isLittleEndian()) |
| 4420 | std::swap(Hi, Lo); |
| 4421 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4422 | // if signed map to unsigned space |
| 4423 | SDOperand Op0Mapped; |
| 4424 | if (isSigned) { |
| 4425 | // constant used to invert sign bit (signed to unsigned mapping) |
| 4426 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 4427 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 4428 | } else { |
| 4429 | Op0Mapped = Op0; |
| 4430 | } |
| 4431 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4432 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4433 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4434 | // initial hi portion of constructed double |
| 4435 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 4436 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4437 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4438 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4439 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4440 | // FP constant to bias correct the final result |
| 4441 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 4442 | BitsToDouble(0x4330000080000000ULL) |
| 4443 | : BitsToDouble(0x4330000000000000ULL), |
| 4444 | MVT::f64); |
| 4445 | // subtract the bias |
| 4446 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 4447 | // final result |
| 4448 | SDOperand Result; |
| 4449 | // handle final rounding |
| 4450 | if (DestVT == MVT::f64) { |
| 4451 | // do nothing |
| 4452 | Result = Sub; |
| 4453 | } else { |
| 4454 | // if f32 then cast to f32 |
| 4455 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 4456 | } |
| 4457 | return Result; |
| 4458 | } |
| 4459 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 4460 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 4461 | |
| 4462 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 4463 | DAG.getConstant(0, Op0.getValueType()), |
| 4464 | ISD::SETLT); |
| 4465 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4466 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4467 | SignSet, Four, Zero); |
| 4468 | |
| 4469 | // If the sign bit of the integer is set, the large number will be treated |
| 4470 | // as a negative number. To counteract this, the dynamic code adds an |
| 4471 | // offset depending on the data type. |
| 4472 | uint64_t FF; |
| 4473 | switch (Op0.getValueType()) { |
| 4474 | default: assert(0 && "Unsupported integer type!"); |
| 4475 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 4476 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 4477 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 4478 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 4479 | } |
| 4480 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4481 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4482 | |
| 4483 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 4484 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4485 | SDOperand FudgeInReg; |
| 4486 | if (DestVT == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4487 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4488 | else { |
| 4489 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 4490 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 4491 | DAG.getEntryNode(), CPIdx, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4492 | NULL, 0, MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 4496 | } |
| 4497 | |
| 4498 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 4499 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 4500 | /// we promote it. At this point, we know that the result and operand types are |
| 4501 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 4502 | /// operation that takes a larger input. |
| 4503 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 4504 | MVT::ValueType DestVT, |
| 4505 | bool isSigned) { |
| 4506 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 4507 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 4508 | |
| 4509 | unsigned OpToUse = 0; |
| 4510 | |
| 4511 | // Scan for the appropriate larger type to use. |
| 4512 | while (1) { |
| 4513 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 4514 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 4515 | |
| 4516 | // If the target supports SINT_TO_FP of this type, use it. |
| 4517 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 4518 | default: break; |
| 4519 | case TargetLowering::Legal: |
| 4520 | if (!TLI.isTypeLegal(NewInTy)) |
| 4521 | break; // Can't use this datatype. |
| 4522 | // FALL THROUGH. |
| 4523 | case TargetLowering::Custom: |
| 4524 | OpToUse = ISD::SINT_TO_FP; |
| 4525 | break; |
| 4526 | } |
| 4527 | if (OpToUse) break; |
| 4528 | if (isSigned) continue; |
| 4529 | |
| 4530 | // If the target supports UINT_TO_FP of this type, use it. |
| 4531 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 4532 | default: break; |
| 4533 | case TargetLowering::Legal: |
| 4534 | if (!TLI.isTypeLegal(NewInTy)) |
| 4535 | break; // Can't use this datatype. |
| 4536 | // FALL THROUGH. |
| 4537 | case TargetLowering::Custom: |
| 4538 | OpToUse = ISD::UINT_TO_FP; |
| 4539 | break; |
| 4540 | } |
| 4541 | if (OpToUse) break; |
| 4542 | |
| 4543 | // Otherwise, try a larger type. |
| 4544 | } |
| 4545 | |
| 4546 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 4547 | // desired type then run the operation on it. |
| 4548 | return DAG.getNode(OpToUse, DestVT, |
| 4549 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 4550 | NewInTy, LegalOp)); |
| 4551 | } |
| 4552 | |
| 4553 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 4554 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 4555 | /// we promote it. At this point, we know that the result and operand types are |
| 4556 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 4557 | /// operation that returns a larger result. |
| 4558 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 4559 | MVT::ValueType DestVT, |
| 4560 | bool isSigned) { |
| 4561 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 4562 | MVT::ValueType NewOutTy = DestVT; |
| 4563 | |
| 4564 | unsigned OpToUse = 0; |
| 4565 | |
| 4566 | // Scan for the appropriate larger type to use. |
| 4567 | while (1) { |
| 4568 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 4569 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 4570 | |
| 4571 | // If the target supports FP_TO_SINT returning this type, use it. |
| 4572 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 4573 | default: break; |
| 4574 | case TargetLowering::Legal: |
| 4575 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4576 | break; // Can't use this datatype. |
| 4577 | // FALL THROUGH. |
| 4578 | case TargetLowering::Custom: |
| 4579 | OpToUse = ISD::FP_TO_SINT; |
| 4580 | break; |
| 4581 | } |
| 4582 | if (OpToUse) break; |
| 4583 | |
| 4584 | // If the target supports FP_TO_UINT of this type, use it. |
| 4585 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 4586 | default: break; |
| 4587 | case TargetLowering::Legal: |
| 4588 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4589 | break; // Can't use this datatype. |
| 4590 | // FALL THROUGH. |
| 4591 | case TargetLowering::Custom: |
| 4592 | OpToUse = ISD::FP_TO_UINT; |
| 4593 | break; |
| 4594 | } |
| 4595 | if (OpToUse) break; |
| 4596 | |
| 4597 | // Otherwise, try a larger type. |
| 4598 | } |
| 4599 | |
| 4600 | // Okay, we found the operation and type to use. Truncate the result of the |
| 4601 | // extended FP_TO_*INT operation to the desired size. |
| 4602 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 4603 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 4604 | } |
| 4605 | |
| 4606 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 4607 | /// |
| 4608 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 4609 | MVT::ValueType VT = Op.getValueType(); |
| 4610 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 4611 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 4612 | switch (VT) { |
| 4613 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 4614 | case MVT::i16: |
| 4615 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4616 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4617 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 4618 | case MVT::i32: |
| 4619 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4620 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4621 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4622 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4623 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 4624 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 4625 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4626 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4627 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4628 | case MVT::i64: |
| 4629 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4630 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4631 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4632 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4633 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4634 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4635 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4636 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4637 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 4638 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 4639 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 4640 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 4641 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 4642 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 4643 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 4644 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 4645 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4646 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4647 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 4648 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4649 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 4654 | /// |
| 4655 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 4656 | switch (Opc) { |
| 4657 | default: assert(0 && "Cannot expand this yet!"); |
| 4658 | case ISD::CTPOP: { |
| 4659 | static const uint64_t mask[6] = { |
| 4660 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 4661 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 4662 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 4663 | }; |
| 4664 | MVT::ValueType VT = Op.getValueType(); |
| 4665 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4666 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4667 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4668 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 4669 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 4670 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4671 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 4672 | DAG.getNode(ISD::AND, VT, |
| 4673 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 4674 | } |
| 4675 | return Op; |
| 4676 | } |
| 4677 | case ISD::CTLZ: { |
| 4678 | // for now, we do this: |
| 4679 | // x = x | (x >> 1); |
| 4680 | // x = x | (x >> 2); |
| 4681 | // ... |
| 4682 | // x = x | (x >>16); |
| 4683 | // x = x | (x >>32); // for 64-bit input |
| 4684 | // return popcount(~x); |
| 4685 | // |
| 4686 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 4687 | MVT::ValueType VT = Op.getValueType(); |
| 4688 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4689 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4690 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4691 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4692 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4693 | } |
| 4694 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4695 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4696 | } |
| 4697 | case ISD::CTTZ: { |
| 4698 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4699 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4700 | // { return 32 - nlz(~x & (x-1)); } |
| 4701 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 4702 | MVT::ValueType VT = Op.getValueType(); |
| 4703 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 4704 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 4705 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 4706 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 4707 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 4708 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 4709 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 4710 | return DAG.getNode(ISD::SUB, VT, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4711 | DAG.getConstant(MVT::getSizeInBits(VT), VT), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4712 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 4713 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 4714 | } |
| 4715 | } |
| 4716 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4717 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4718 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 4719 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 4720 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 4721 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 4722 | /// Lo/Hi values are returned. |
| 4723 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 4724 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4725 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4726 | SDNode *Node = Op.Val; |
| 4727 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 4728 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4729 | MVT::isVector(VT)) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4730 | "Cannot expand to FP value or to larger int value!"); |
| 4731 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4732 | // See if we already expanded it. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 4733 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4734 | = ExpandedNodes.find(Op); |
| 4735 | if (I != ExpandedNodes.end()) { |
| 4736 | Lo = I->second.first; |
| 4737 | Hi = I->second.second; |
| 4738 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4741 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 4742 | case ISD::CopyFromReg: |
| 4743 | assert(0 && "CopyFromReg must be legal!"); |
| 4744 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4745 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 4746 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4747 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4748 | assert(0 && "Do not know how to expand this operator!"); |
| 4749 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4750 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 4751 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4752 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 4753 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4754 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4755 | case ISD::Constant: { |
| 4756 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 4757 | Lo = DAG.getConstant(Cst, NVT); |
| 4758 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 4759 | break; |
| 4760 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4761 | case ISD::ConstantFP: { |
| 4762 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 4763 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4764 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 4765 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4766 | break; |
| 4767 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4768 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4769 | // Return the operands. |
| 4770 | Lo = Node->getOperand(0); |
| 4771 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4772 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4773 | |
| 4774 | case ISD::SIGN_EXTEND_INREG: |
| 4775 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 4776 | // sext_inreg the low part if needed. |
| 4777 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 4778 | |
| 4779 | // The high part gets the sign extension from the lo-part. This handles |
| 4780 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4781 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4782 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 4783 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 4784 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 4785 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4786 | case ISD::BSWAP: { |
| 4787 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 4788 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 4789 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 4790 | Lo = TempLo; |
| 4791 | break; |
| 4792 | } |
| 4793 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4794 | case ISD::CTPOP: |
| 4795 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 4796 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 4797 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 4798 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4799 | Hi = DAG.getConstant(0, NVT); |
| 4800 | break; |
| 4801 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4802 | case ISD::CTLZ: { |
| 4803 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4804 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4805 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4806 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4807 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 4808 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4809 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 4810 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 4811 | |
| 4812 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 4813 | Hi = DAG.getConstant(0, NVT); |
| 4814 | break; |
| 4815 | } |
| 4816 | |
| 4817 | case ISD::CTTZ: { |
| 4818 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 4819 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4820 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 4821 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4822 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 4823 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 4824 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 4825 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 4826 | |
| 4827 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 4828 | Hi = DAG.getConstant(0, NVT); |
| 4829 | break; |
| 4830 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 4831 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4832 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4833 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4834 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4835 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 4836 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 4837 | |
| 4838 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4839 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 4840 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 4841 | if (!TLI.isLittleEndian()) |
| 4842 | std::swap(Lo, Hi); |
| 4843 | break; |
| 4844 | } |
| 4845 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4846 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4847 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 4848 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 4849 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 4850 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4851 | unsigned SVOffset = LD->getSrcValueOffset(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4852 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4853 | if (ExtType == ISD::NON_EXTLOAD) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4854 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4855 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4856 | // f32->i32 or f64->i64 one to one expansion. |
| 4857 | // Remember that we legalized the chain. |
| 4858 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 4859 | // Recursively expand the new load. |
| 4860 | if (getTypeAction(NVT) == Expand) |
| 4861 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 4862 | break; |
| 4863 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 4864 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4865 | // Increment the pointer to the other half. |
| 4866 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 4867 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4868 | getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4869 | SVOffset += IncrementSize; |
| 4870 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4871 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4872 | // Build a factor node to remember that this load is independent of the |
| 4873 | // other one. |
| 4874 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4875 | Hi.getValue(1)); |
| 4876 | |
| 4877 | // Remember that we legalized the chain. |
| 4878 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 4879 | if (!TLI.isLittleEndian()) |
| 4880 | std::swap(Lo, Hi); |
| 4881 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 4882 | MVT::ValueType EVT = LD->getLoadedVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 4883 | |
| 4884 | if (VT == MVT::f64 && EVT == MVT::f32) { |
| 4885 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 4886 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4887 | SVOffset); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 4888 | // Remember that we legalized the chain. |
| 4889 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 4890 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 4891 | break; |
| 4892 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4893 | |
| 4894 | if (EVT == NVT) |
| 4895 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4896 | SVOffset); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4897 | else |
| 4898 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4899 | SVOffset, EVT); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4900 | |
| 4901 | // Remember that we legalized the chain. |
| 4902 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 4903 | |
| 4904 | if (ExtType == ISD::SEXTLOAD) { |
| 4905 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 4906 | // lo part. |
| 4907 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 4908 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4909 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 4910 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 4911 | // The high part is just a zero. |
| 4912 | Hi = DAG.getConstant(0, NVT); |
| 4913 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 4914 | // The high part is undefined. |
| 4915 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4916 | } |
| 4917 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4918 | break; |
| 4919 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4920 | case ISD::AND: |
| 4921 | case ISD::OR: |
| 4922 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 4923 | SDOperand LL, LH, RL, RH; |
| 4924 | ExpandOp(Node->getOperand(0), LL, LH); |
| 4925 | ExpandOp(Node->getOperand(1), RL, RH); |
| 4926 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 4927 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 4928 | break; |
| 4929 | } |
| 4930 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4931 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4932 | ExpandOp(Node->getOperand(1), LL, LH); |
| 4933 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4934 | if (getTypeAction(NVT) == Expand) |
| 4935 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4936 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4937 | if (VT != MVT::f32) |
| 4938 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4939 | break; |
| 4940 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4941 | case ISD::SELECT_CC: { |
| 4942 | SDOperand TL, TH, FL, FH; |
| 4943 | ExpandOp(Node->getOperand(2), TL, TH); |
| 4944 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4945 | if (getTypeAction(NVT) == Expand) |
| 4946 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4947 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4948 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 4949 | if (VT != MVT::f32) |
| 4950 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 4951 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4952 | break; |
| 4953 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4954 | case ISD::ANY_EXTEND: |
| 4955 | // The low part is any extension of the input (which degenerates to a copy). |
| 4956 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 4957 | // The high part is undefined. |
| 4958 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 4959 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4960 | case ISD::SIGN_EXTEND: { |
| 4961 | // The low part is just a sign extension of the input (which degenerates to |
| 4962 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4963 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4964 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4965 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 4966 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 4967 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4968 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 4969 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4972 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4973 | // The low part is just a zero extension of the input (which degenerates to |
| 4974 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 4975 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4976 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4977 | // The high part is just a zero. |
| 4978 | Hi = DAG.getConstant(0, NVT); |
| 4979 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4980 | |
Chris Lattner | 4c948eb | 2007-02-13 23:55:16 +0000 | [diff] [blame] | 4981 | case ISD::TRUNCATE: { |
| 4982 | // The input value must be larger than this value. Expand *it*. |
| 4983 | SDOperand NewLo; |
| 4984 | ExpandOp(Node->getOperand(0), NewLo, Hi); |
| 4985 | |
| 4986 | // The low part is now either the right size, or it is closer. If not the |
| 4987 | // right size, make an illegal truncate so we recursively expand it. |
| 4988 | if (NewLo.getValueType() != Node->getValueType(0)) |
| 4989 | NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo); |
| 4990 | ExpandOp(NewLo, Lo, Hi); |
| 4991 | break; |
| 4992 | } |
| 4993 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4994 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 4995 | SDOperand Tmp; |
| 4996 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 4997 | // If the target wants to, allow it to lower this itself. |
| 4998 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4999 | case Expand: assert(0 && "cannot expand FP!"); |
| 5000 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 5001 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 5002 | } |
| 5003 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 5004 | } |
| 5005 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5006 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5007 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5008 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5009 | if (getTypeAction(NVT) == Expand) |
| 5010 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 5011 | break; |
| 5012 | } |
| 5013 | |
| 5014 | // If source operand will be expanded to the same type as VT, i.e. |
| 5015 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 5016 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 5017 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 5018 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5019 | break; |
| 5020 | } |
| 5021 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5022 | // Turn this into a load/store pair by default. |
| 5023 | if (Tmp.Val == 0) |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5024 | Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0)); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5025 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5026 | ExpandOp(Tmp, Lo, Hi); |
| 5027 | break; |
| 5028 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5029 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5030 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5031 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 5032 | TargetLowering::Custom && |
| 5033 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5034 | Lo = TLI.LowerOperation(Op, DAG); |
| 5035 | assert(Lo.Val && "Node must be custom expanded!"); |
| 5036 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5037 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5038 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5039 | break; |
| 5040 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5041 | // These operators cannot be expanded directly, emit them as calls to |
| 5042 | // library functions. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5043 | case ISD::FP_TO_SINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5044 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5045 | SDOperand Op; |
| 5046 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5047 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5048 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 5049 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5050 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5051 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5052 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 5053 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5054 | // Now that the custom expander is done, expand the result, which is still |
| 5055 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5056 | if (Op.Val) { |
| 5057 | ExpandOp(Op, Lo, Hi); |
| 5058 | break; |
| 5059 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5060 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5061 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5062 | RTLIB::Libcall LC; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5063 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5064 | LC = RTLIB::FPTOSINT_F32_I64; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5065 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5066 | LC = RTLIB::FPTOSINT_F64_I64; |
| 5067 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 5068 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5069 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5070 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5071 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5072 | case ISD::FP_TO_UINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5073 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5074 | SDOperand Op; |
| 5075 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5076 | case Expand: assert(0 && "cannot expand FP!"); |
| 5077 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 5078 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 5079 | } |
| 5080 | |
| 5081 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 5082 | |
| 5083 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5084 | if (Op.Val) { |
| 5085 | ExpandOp(Op, Lo, Hi); |
| 5086 | break; |
| 5087 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5088 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5089 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5090 | RTLIB::Libcall LC; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5091 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5092 | LC = RTLIB::FPTOUINT_F32_I64; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5093 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5094 | LC = RTLIB::FPTOUINT_F64_I64; |
| 5095 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 5096 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5097 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5098 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5099 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5100 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5101 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5102 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5103 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5104 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5105 | Op = TLI.LowerOperation(Op, DAG); |
| 5106 | if (Op.Val) { |
| 5107 | // Now that the custom expander is done, expand the result, which is |
| 5108 | // still VT. |
| 5109 | ExpandOp(Op, Lo, Hi); |
| 5110 | break; |
| 5111 | } |
| 5112 | } |
| 5113 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5114 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 5115 | // this X << 1 as X+X. |
| 5116 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
| 5117 | if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
| 5118 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 5119 | SDOperand LoOps[2], HiOps[3]; |
| 5120 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 5121 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 5122 | LoOps[1] = LoOps[0]; |
| 5123 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 5124 | |
| 5125 | HiOps[1] = HiOps[0]; |
| 5126 | HiOps[2] = Lo.getValue(1); |
| 5127 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 5128 | break; |
| 5129 | } |
| 5130 | } |
| 5131 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5132 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5133 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5134 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5135 | |
| 5136 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5137 | TargetLowering::LegalizeAction Action = |
| 5138 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 5139 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5140 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5141 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5142 | break; |
| 5143 | } |
| 5144 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5145 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5146 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node, |
| 5147 | false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5148 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5149 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5150 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5151 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5152 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5153 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5154 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5155 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5156 | Op = TLI.LowerOperation(Op, DAG); |
| 5157 | if (Op.Val) { |
| 5158 | // Now that the custom expander is done, expand the result, which is |
| 5159 | // still VT. |
| 5160 | ExpandOp(Op, Lo, Hi); |
| 5161 | break; |
| 5162 | } |
| 5163 | } |
| 5164 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5165 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5166 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5167 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5168 | |
| 5169 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5170 | TargetLowering::LegalizeAction Action = |
| 5171 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 5172 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5173 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5174 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5175 | break; |
| 5176 | } |
| 5177 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5178 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5179 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node, |
| 5180 | true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5181 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5185 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5186 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5187 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5188 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5189 | Op = TLI.LowerOperation(Op, DAG); |
| 5190 | if (Op.Val) { |
| 5191 | // Now that the custom expander is done, expand the result, which is |
| 5192 | // still VT. |
| 5193 | ExpandOp(Op, Lo, Hi); |
| 5194 | break; |
| 5195 | } |
| 5196 | } |
| 5197 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5198 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5199 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5200 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5201 | |
| 5202 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5203 | TargetLowering::LegalizeAction Action = |
| 5204 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 5205 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5206 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5207 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5208 | break; |
| 5209 | } |
| 5210 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5211 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5212 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node, |
| 5213 | false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5214 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5215 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5216 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5217 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5218 | case ISD::SUB: { |
| 5219 | // If the target wants to custom expand this, let them. |
| 5220 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 5221 | TargetLowering::Custom) { |
| 5222 | Op = TLI.LowerOperation(Op, DAG); |
| 5223 | if (Op.Val) { |
| 5224 | ExpandOp(Op, Lo, Hi); |
| 5225 | break; |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | // Expand the subcomponents. |
| 5230 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5231 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5232 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5233 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5234 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5235 | LoOps[0] = LHSL; |
| 5236 | LoOps[1] = RHSL; |
| 5237 | HiOps[0] = LHSH; |
| 5238 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5239 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5240 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5241 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5242 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5243 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5244 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5245 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5246 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5247 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 5248 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5249 | } |
Chris Lattner | b429f73 | 2007-05-17 18:15:41 +0000 | [diff] [blame] | 5250 | |
| 5251 | case ISD::ADDC: |
| 5252 | case ISD::SUBC: { |
| 5253 | // Expand the subcomponents. |
| 5254 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5255 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5256 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 5257 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5258 | SDOperand LoOps[2] = { LHSL, RHSL }; |
| 5259 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 5260 | |
| 5261 | if (Node->getOpcode() == ISD::ADDC) { |
| 5262 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 5263 | HiOps[2] = Lo.getValue(1); |
| 5264 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 5265 | } else { |
| 5266 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
| 5267 | HiOps[2] = Lo.getValue(1); |
| 5268 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
| 5269 | } |
| 5270 | // Remember that we legalized the flag. |
| 5271 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 5272 | break; |
| 5273 | } |
| 5274 | case ISD::ADDE: |
| 5275 | case ISD::SUBE: { |
| 5276 | // Expand the subcomponents. |
| 5277 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5278 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5279 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 5280 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5281 | SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) }; |
| 5282 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 5283 | |
| 5284 | Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3); |
| 5285 | HiOps[2] = Lo.getValue(1); |
| 5286 | Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3); |
| 5287 | |
| 5288 | // Remember that we legalized the flag. |
| 5289 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 5290 | break; |
| 5291 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5292 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5293 | // If the target wants to custom expand this, let them. |
| 5294 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 5295 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 5296 | if (New.Val) { |
| 5297 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5298 | break; |
| 5299 | } |
| 5300 | } |
| 5301 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5302 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 5303 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5304 | if (HasMULHS || HasMULHU) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5305 | SDOperand LL, LH, RL, RH; |
| 5306 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5307 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5308 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 5309 | // FIXME: Move this to the dag combiner. |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5310 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 5311 | // extended the sign bit of the low half through the upper half, and if so |
| 5312 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 5313 | // i64 x i64 multiply. |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5314 | if (HasMULHS && |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5315 | // is RH an extension of the sign bit of RL? |
| 5316 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 5317 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 5318 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 5319 | // is LH an extension of the sign bit of LL? |
| 5320 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 5321 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 5322 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5323 | // Low part: |
| 5324 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5325 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5326 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5327 | break; |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5328 | } else if (HasMULHU) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5329 | // Low part: |
| 5330 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5331 | |
| 5332 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5333 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 5334 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 5335 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 5336 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 5337 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5338 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5339 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5340 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5341 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5342 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node, |
| 5343 | false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5344 | break; |
| 5345 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5346 | case ISD::SDIV: |
| 5347 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi); |
| 5348 | break; |
| 5349 | case ISD::UDIV: |
| 5350 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi); |
| 5351 | break; |
| 5352 | case ISD::SREM: |
| 5353 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi); |
| 5354 | break; |
| 5355 | case ISD::UREM: |
| 5356 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi); |
| 5357 | break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 5358 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5359 | case ISD::FADD: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5360 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5361 | ? RTLIB::ADD_F32 : RTLIB::ADD_F64), |
| 5362 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5363 | break; |
| 5364 | case ISD::FSUB: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5365 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5366 | ? RTLIB::SUB_F32 : RTLIB::SUB_F64), |
| 5367 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5368 | break; |
| 5369 | case ISD::FMUL: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5370 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5371 | ? RTLIB::MUL_F32 : RTLIB::MUL_F64), |
| 5372 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5373 | break; |
| 5374 | case ISD::FDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5375 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5376 | ? RTLIB::DIV_F32 : RTLIB::DIV_F64), |
| 5377 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5378 | break; |
| 5379 | case ISD::FP_EXTEND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5380 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5381 | break; |
| 5382 | case ISD::FP_ROUND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5383 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5384 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5385 | case ISD::FSQRT: |
| 5386 | case ISD::FSIN: |
| 5387 | case ISD::FCOS: { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5388 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5389 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5390 | case ISD::FSQRT: |
| 5391 | LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : RTLIB::SQRT_F64; |
| 5392 | break; |
| 5393 | case ISD::FSIN: |
| 5394 | LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 5395 | break; |
| 5396 | case ISD::FCOS: |
| 5397 | LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 5398 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5399 | default: assert(0 && "Unreachable!"); |
| 5400 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5401 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5402 | break; |
| 5403 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 5404 | case ISD::FABS: { |
| 5405 | SDOperand Mask = (VT == MVT::f64) |
| 5406 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 5407 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 5408 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5409 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5410 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 5411 | if (getTypeAction(NVT) == Expand) |
| 5412 | ExpandOp(Lo, Lo, Hi); |
| 5413 | break; |
| 5414 | } |
| 5415 | case ISD::FNEG: { |
| 5416 | SDOperand Mask = (VT == MVT::f64) |
| 5417 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 5418 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 5419 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5420 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5421 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 5422 | if (getTypeAction(NVT) == Expand) |
| 5423 | ExpandOp(Lo, Lo, Hi); |
| 5424 | break; |
| 5425 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 5426 | case ISD::FCOPYSIGN: { |
| 5427 | Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 5428 | if (getTypeAction(NVT) == Expand) |
| 5429 | ExpandOp(Lo, Lo, Hi); |
| 5430 | break; |
| 5431 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5432 | case ISD::SINT_TO_FP: |
| 5433 | case ISD::UINT_TO_FP: { |
| 5434 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 5435 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5436 | RTLIB::Libcall LC; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5437 | if (Node->getOperand(0).getValueType() == MVT::i64) { |
| 5438 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5439 | LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5440 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5441 | LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5442 | } else { |
| 5443 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5444 | LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5445 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5446 | LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | // Promote the operand if needed. |
| 5450 | if (getTypeAction(SrcVT) == Promote) { |
| 5451 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 5452 | Tmp = isSigned |
| 5453 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 5454 | DAG.getValueType(SrcVT)) |
| 5455 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 5456 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 5457 | } |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5458 | |
| 5459 | const char *LibCall = TLI.getLibcallName(LC); |
| 5460 | if (LibCall) |
| 5461 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi); |
| 5462 | else { |
| 5463 | Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT, |
| 5464 | Node->getOperand(0)); |
| 5465 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 5466 | ExpandOp(Lo, Lo, Hi); |
| 5467 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5468 | break; |
| 5469 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5470 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5471 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5472 | // Make sure the resultant values have been legalized themselves, unless this |
| 5473 | // is a type that requires multi-step expansion. |
| 5474 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 5475 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5476 | if (Hi.Val) |
| 5477 | // Don't legalize the high part if it is expanded to a single node. |
| 5478 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5479 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5480 | |
| 5481 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5482 | bool isNew = ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5483 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5486 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 5487 | /// two smaller values, still of vector type. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5488 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 5489 | SDOperand &Hi) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5490 | assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5491 | SDNode *Node = Op.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5492 | unsigned NumElements = MVT::getVectorNumElements(Node->getValueType(0)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5493 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 5494 | unsigned NewNumElts = NumElements/2; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5495 | MVT::ValueType NewEltVT = MVT::getVectorElementType(Node->getValueType(0)); |
| 5496 | MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5497 | |
| 5498 | // See if we already split it. |
| 5499 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 5500 | = SplitNodes.find(Op); |
| 5501 | if (I != SplitNodes.end()) { |
| 5502 | Lo = I->second.first; |
| 5503 | Hi = I->second.second; |
| 5504 | return; |
| 5505 | } |
| 5506 | |
| 5507 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5508 | default: |
| 5509 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5510 | Node->dump(&DAG); |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5511 | #endif |
| 5512 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5513 | case ISD::BUILD_PAIR: |
| 5514 | Lo = Node->getOperand(0); |
| 5515 | Hi = Node->getOperand(1); |
| 5516 | break; |
| 5517 | case ISD::BUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5518 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5519 | Node->op_begin()+NewNumElts); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5520 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5521 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5522 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5523 | Node->op_end()); |
| 5524 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5525 | break; |
| 5526 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5527 | case ISD::CONCAT_VECTORS: { |
| 5528 | unsigned NewNumSubvectors = Node->getNumOperands() / 2; |
| 5529 | if (NewNumSubvectors == 1) { |
| 5530 | Lo = Node->getOperand(0); |
| 5531 | Hi = Node->getOperand(1); |
| 5532 | } else { |
| 5533 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5534 | Node->op_begin()+NewNumSubvectors); |
| 5535 | Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5536 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5537 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors, |
| 5538 | Node->op_end()); |
| 5539 | Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size()); |
| 5540 | } |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5541 | break; |
| 5542 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5543 | case ISD::ADD: |
| 5544 | case ISD::SUB: |
| 5545 | case ISD::MUL: |
| 5546 | case ISD::FADD: |
| 5547 | case ISD::FSUB: |
| 5548 | case ISD::FMUL: |
| 5549 | case ISD::SDIV: |
| 5550 | case ISD::UDIV: |
| 5551 | case ISD::FDIV: |
| 5552 | case ISD::AND: |
| 5553 | case ISD::OR: |
| 5554 | case ISD::XOR: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5555 | SDOperand LL, LH, RL, RH; |
| 5556 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 5557 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 5558 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5559 | Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL); |
| 5560 | Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5561 | break; |
| 5562 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5563 | case ISD::LOAD: { |
| 5564 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5565 | SDOperand Ch = LD->getChain(); |
| 5566 | SDOperand Ptr = LD->getBasePtr(); |
| 5567 | const Value *SV = LD->getSrcValue(); |
| 5568 | int SVOffset = LD->getSrcValueOffset(); |
| 5569 | unsigned Alignment = LD->getAlignment(); |
| 5570 | bool isVolatile = LD->isVolatile(); |
| 5571 | |
| 5572 | Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
| 5573 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5574 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 5575 | getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5576 | SVOffset += IncrementSize; |
| 5577 | if (Alignment > IncrementSize) |
| 5578 | Alignment = IncrementSize; |
| 5579 | Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5580 | |
| 5581 | // Build a factor node to remember that this load is independent of the |
| 5582 | // other one. |
| 5583 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5584 | Hi.getValue(1)); |
| 5585 | |
| 5586 | // Remember that we legalized the chain. |
| 5587 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5588 | break; |
| 5589 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5590 | case ISD::BIT_CONVERT: { |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5591 | // We know the result is a vector. The input may be either a vector or a |
| 5592 | // scalar value. |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5593 | SDOperand InOp = Node->getOperand(0); |
| 5594 | if (!MVT::isVector(InOp.getValueType()) || |
| 5595 | MVT::getVectorNumElements(InOp.getValueType()) == 1) { |
| 5596 | // The input is a scalar or single-element vector. |
| 5597 | // Lower to a store/load so that it can be split. |
| 5598 | // FIXME: this could be improved probably. |
| 5599 | SDOperand Ptr = CreateStackTemporary(InOp.getValueType()); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5600 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5601 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5602 | InOp, Ptr, NULL, 0); |
| 5603 | InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5604 | } |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5605 | // Split the vector and convert each of the pieces now. |
| 5606 | SplitVectorOp(InOp, Lo, Hi); |
| 5607 | Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo); |
| 5608 | Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi); |
| 5609 | break; |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5610 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5614 | bool isNew = |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5615 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5616 | assert(isNew && "Value already split?!?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 5620 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 5621 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 5622 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5623 | SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) { |
| 5624 | assert(MVT::isVector(Op.getValueType()) && |
| 5625 | "Bad ScalarizeVectorOp invocation!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5626 | SDNode *Node = Op.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5627 | MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType()); |
| 5628 | assert(MVT::getVectorNumElements(Op.getValueType()) == 1); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5629 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5630 | // See if we already scalarized it. |
| 5631 | std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op); |
| 5632 | if (I != ScalarizedNodes.end()) return I->second; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5633 | |
| 5634 | SDOperand Result; |
| 5635 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5636 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5637 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5638 | Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5639 | #endif |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5640 | assert(0 && "Unknown vector operation in ScalarizeVectorOp!"); |
| 5641 | case ISD::ADD: |
| 5642 | case ISD::FADD: |
| 5643 | case ISD::SUB: |
| 5644 | case ISD::FSUB: |
| 5645 | case ISD::MUL: |
| 5646 | case ISD::FMUL: |
| 5647 | case ISD::SDIV: |
| 5648 | case ISD::UDIV: |
| 5649 | case ISD::FDIV: |
| 5650 | case ISD::SREM: |
| 5651 | case ISD::UREM: |
| 5652 | case ISD::FREM: |
| 5653 | case ISD::AND: |
| 5654 | case ISD::OR: |
| 5655 | case ISD::XOR: |
| 5656 | Result = DAG.getNode(Node->getOpcode(), |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5657 | NewVT, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5658 | ScalarizeVectorOp(Node->getOperand(0)), |
| 5659 | ScalarizeVectorOp(Node->getOperand(1))); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5660 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5661 | case ISD::FNEG: |
| 5662 | case ISD::FABS: |
| 5663 | case ISD::FSQRT: |
| 5664 | case ISD::FSIN: |
| 5665 | case ISD::FCOS: |
| 5666 | Result = DAG.getNode(Node->getOpcode(), |
| 5667 | NewVT, |
| 5668 | ScalarizeVectorOp(Node->getOperand(0))); |
| 5669 | break; |
| 5670 | case ISD::LOAD: { |
| 5671 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5672 | SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 5673 | SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5674 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5675 | const Value *SV = LD->getSrcValue(); |
| 5676 | int SVOffset = LD->getSrcValueOffset(); |
| 5677 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, |
| 5678 | LD->isVolatile(), LD->getAlignment()); |
| 5679 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5680 | // Remember that we legalized the chain. |
| 5681 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 5682 | break; |
| 5683 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5684 | case ISD::BUILD_VECTOR: |
| 5685 | Result = Node->getOperand(0); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5686 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5687 | case ISD::INSERT_VECTOR_ELT: |
| 5688 | // Returning the inserted scalar element. |
| 5689 | Result = Node->getOperand(1); |
| 5690 | break; |
| 5691 | case ISD::CONCAT_VECTORS: |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5692 | assert(Node->getOperand(0).getValueType() == NewVT && |
| 5693 | "Concat of non-legal vectors not yet supported!"); |
| 5694 | Result = Node->getOperand(0); |
| 5695 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5696 | case ISD::VECTOR_SHUFFLE: { |
| 5697 | // Figure out if the scalar is the LHS or RHS and return it. |
| 5698 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 5699 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 5700 | Result = ScalarizeVectorOp(Node->getOperand(1)); |
| 5701 | else |
| 5702 | Result = ScalarizeVectorOp(Node->getOperand(0)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5703 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5704 | } |
| 5705 | case ISD::EXTRACT_SUBVECTOR: |
| 5706 | Result = Node->getOperand(0); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5707 | assert(Result.getValueType() == NewVT); |
| 5708 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5709 | case ISD::BIT_CONVERT: |
| 5710 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 5711 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5712 | case ISD::SELECT: |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 5713 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5714 | ScalarizeVectorOp(Op.getOperand(1)), |
| 5715 | ScalarizeVectorOp(Op.getOperand(2))); |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 5716 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | if (TLI.isTypeLegal(NewVT)) |
| 5720 | Result = LegalizeOp(Result); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5721 | bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second; |
| 5722 | assert(isNew && "Value already scalarized?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5723 | return Result; |
| 5724 | } |
| 5725 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5726 | |
| 5727 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 5728 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5729 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 5730 | if (ViewLegalizeDAGs) viewGraph(); |
| 5731 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5732 | /// run - This is the main entry point to this class. |
| 5733 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5734 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5735 | } |
| 5736 | |