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" |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 25 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 32 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 35 | #ifndef NDEBUG |
| 36 | static cl::opt<bool> |
| 37 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 38 | cl::desc("Pop up a window to show dags before legalize")); |
| 39 | #else |
| 40 | static const bool ViewLegalizeDAGs = 0; |
| 41 | #endif |
| 42 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 45 | /// hacks on it until the target machine can handle it. This involves |
| 46 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 47 | /// large sizes or splitting up large values into small values) as well as |
| 48 | /// eliminating operations the machine cannot handle. |
| 49 | /// |
| 50 | /// This code also does a small amount of optimization and recognition of idioms |
| 51 | /// as part of its processing. For example, if a target does not support a |
| 52 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 53 | /// will attempt merge setcc and brc instructions into brcc's. |
| 54 | /// |
| 55 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 56 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 57 | TargetLowering &TLI; |
| 58 | SelectionDAG &DAG; |
| 59 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 60 | // Libcall insertion helpers. |
| 61 | |
| 62 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 63 | /// legalized. We use this to ensure that calls are properly serialized |
| 64 | /// against each other, including inserted libcalls. |
| 65 | SDOperand LastCALLSEQ_END; |
| 66 | |
| 67 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 68 | /// helpful assertions that a libcall isn't created while another call is |
| 69 | /// being legalized (which could lead to non-serialized call sequences). |
| 70 | bool IsLegalizingCall; |
| 71 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 72 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 73 | Legal, // The target natively supports this operation. |
| 74 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 75 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 76 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 78 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 79 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 80 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 81 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 83 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 84 | /// than one use, this map indicates what regularized operand to use. This |
| 85 | /// allows us to avoid legalizing the same thing more than once. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 86 | DenseMap<SDOperand, SDOperand> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 88 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 89 | /// than one use, this map indicates what promoted value to use. This allows |
| 90 | /// us to avoid promoting the same thing more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 91 | DenseMap<SDOperand, SDOperand> PromotedNodes; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 92 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 93 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 94 | /// which which operands are the expanded version of the input. This allows |
| 95 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 96 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 97 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 98 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 99 | /// which which operands are the split version of the input. This allows us |
| 100 | /// to avoid splitting the same node more than once. |
| 101 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 102 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 103 | /// ScalarizedNodes - For nodes that need to be converted from vector types to |
| 104 | /// scalar types, this contains the mapping of ones we have already |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 105 | /// processed to the result. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 106 | std::map<SDOperand, SDOperand> ScalarizedNodes; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 108 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 109 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 110 | // If someone requests legalization of the new node, return itself. |
| 111 | if (From != To) |
| 112 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 114 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 115 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 116 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 117 | // If someone requests legalization of the new node, return itself. |
| 118 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 119 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 121 | public: |
| 122 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 123 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 125 | /// getTypeAction - Return how we should legalize values of this type, either |
| 126 | /// it is already legal or we need to expand it into multiple registers of |
| 127 | /// smaller integer type, or we need to promote it to a larger type. |
| 128 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 129 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /// isTypeLegal - Return true if this type is legal on this target. |
| 133 | /// |
| 134 | bool isTypeLegal(MVT::ValueType VT) const { |
| 135 | return getTypeAction(VT) == Legal; |
| 136 | } |
| 137 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 138 | void LegalizeDAG(); |
| 139 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 140 | private: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 141 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 142 | /// appropriate for its type. |
| 143 | void HandleOp(SDOperand Op); |
| 144 | |
| 145 | /// LegalizeOp - We know that the specified value has a legal type. |
| 146 | /// Recursively ensure that the operands have legal types, then return the |
| 147 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 148 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 149 | |
| 150 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 151 | /// promote it to compute the value into a larger type. The produced value |
| 152 | /// will have the correct bits for the low portion of the register, but no |
| 153 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 154 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 155 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 156 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 157 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 158 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 159 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 160 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 161 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 162 | /// types. |
| 163 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 164 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 165 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 166 | /// two smaller values. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 167 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 168 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 169 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 170 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 171 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 172 | SDOperand ScalarizeVectorOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 174 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 175 | /// specified mask and type. Targets can specify exactly which masks they |
| 176 | /// support and the code generator is tasked with not creating illegal masks. |
| 177 | /// |
| 178 | /// Note that this will also return true for shuffles that are promoted to a |
| 179 | /// different type. |
| 180 | /// |
| 181 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 182 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 183 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 184 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 185 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 186 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 187 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 188 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 189 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 190 | SDOperand CreateStackTemporary(MVT::ValueType VT); |
| 191 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 192 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 193 | SDOperand &Hi); |
| 194 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 195 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 196 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 197 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 198 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 199 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 200 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 201 | SDOperand LegalOp, |
| 202 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 203 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 204 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 205 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 206 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 208 | SDOperand ExpandBSWAP(SDOperand Op); |
| 209 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 210 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 211 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 212 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 213 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 214 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 215 | SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 216 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 218 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 219 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 220 | } |
| 221 | }; |
| 222 | } |
| 223 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 224 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 225 | /// specified mask and type. Targets can specify exactly which masks they |
| 226 | /// support and the code generator is tasked with not creating illegal masks. |
| 227 | /// |
| 228 | /// Note that this will also return true for shuffles that are promoted to a |
| 229 | /// different type. |
| 230 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 231 | SDOperand Mask) const { |
| 232 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 233 | default: return 0; |
| 234 | case TargetLowering::Legal: |
| 235 | case TargetLowering::Custom: |
| 236 | break; |
| 237 | case TargetLowering::Promote: { |
| 238 | // If this is promoted to a different type, convert the shuffle mask and |
| 239 | // ask if it is legal in the promoted type! |
| 240 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 241 | |
| 242 | // If we changed # elements, change the shuffle mask. |
| 243 | unsigned NumEltsGrowth = |
| 244 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 245 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 246 | if (NumEltsGrowth > 1) { |
| 247 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 248 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 249 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 250 | SDOperand InOp = Mask.getOperand(i); |
| 251 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 252 | if (InOp.getOpcode() == ISD::UNDEF) |
| 253 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 254 | else { |
| 255 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 256 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 257 | } |
| 258 | } |
| 259 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 260 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 261 | } |
| 262 | VT = NVT; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 267 | } |
| 268 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 269 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 270 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 271 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 272 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 273 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 276 | /// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order |
| 277 | /// contains all of a nodes operands before it contains the node. |
| 278 | static void ComputeTopDownOrdering(SelectionDAG &DAG, |
| 279 | SmallVector<SDNode*, 64> &Order) { |
| 280 | |
| 281 | DenseMap<SDNode*, unsigned> Visited; |
| 282 | std::vector<SDNode*> Worklist; |
| 283 | Worklist.reserve(128); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 284 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 285 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 286 | // entry node) that have no operands. |
| 287 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 288 | E = DAG.allnodes_end(); I != E; ++I) { |
| 289 | if (I->getNumOperands() == 0) { |
| 290 | Visited[I] = 0 - 1U; |
| 291 | Worklist.push_back(I); |
| 292 | } |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 295 | while (!Worklist.empty()) { |
| 296 | SDNode *N = Worklist.back(); |
| 297 | Worklist.pop_back(); |
| 298 | |
| 299 | if (++Visited[N] != N->getNumOperands()) |
| 300 | continue; // Haven't visited all operands yet |
| 301 | |
| 302 | Order.push_back(N); |
| 303 | |
| 304 | // Now that we have N in, add anything that uses it if all of their operands |
| 305 | // are now done. |
| 306 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 307 | UI != E; ++UI) |
| 308 | Worklist.push_back(*UI); |
| 309 | } |
| 310 | |
| 311 | assert(Order.size() == Visited.size() && |
| 312 | Order.size() == |
| 313 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
| 314 | "Error: DAG is cyclic!"); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 318 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 319 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 320 | IsLegalizingCall = false; |
| 321 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 322 | // The legalize process is inherently a bottom-up recursive process (users |
| 323 | // legalize their uses before themselves). Given infinite stack space, we |
| 324 | // could just start legalizing on the root and traverse the whole graph. In |
| 325 | // 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] | 326 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 327 | // node is only legalized after all of its operands are legalized. |
Chris Lattner | 0ed4417 | 2007-02-04 01:20:02 +0000 | [diff] [blame] | 328 | SmallVector<SDNode*, 64> Order; |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 329 | ComputeTopDownOrdering(DAG, Order); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 330 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 331 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 332 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 333 | |
| 334 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 335 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 336 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 337 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 338 | |
| 339 | ExpandedNodes.clear(); |
| 340 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 341 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 342 | SplitNodes.clear(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 343 | ScalarizedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 344 | |
| 345 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 346 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 349 | |
| 350 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 351 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 352 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 353 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 354 | return Node; |
| 355 | if (Node->use_empty()) |
| 356 | return 0; // No CallSeqEnd |
| 357 | |
| 358 | // The chain is usually at the end. |
| 359 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 360 | if (TheChain.getValueType() != MVT::Other) { |
| 361 | // Sometimes it's at the beginning. |
| 362 | TheChain = SDOperand(Node, 0); |
| 363 | if (TheChain.getValueType() != MVT::Other) { |
| 364 | // Otherwise, hunt for it. |
| 365 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 366 | if (Node->getValueType(i) == MVT::Other) { |
| 367 | TheChain = SDOperand(Node, i); |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | // Otherwise, we walked into a node without a chain. |
| 372 | if (TheChain.getValueType() != MVT::Other) |
| 373 | return 0; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 378 | E = Node->use_end(); UI != E; ++UI) { |
| 379 | |
| 380 | // Make sure to only follow users of our token chain. |
| 381 | SDNode *User = *UI; |
| 382 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 383 | if (User->getOperand(i) == TheChain) |
| 384 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 385 | return Result; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 391 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 392 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 393 | assert(Node && "Didn't find callseq_start for a call??"); |
| 394 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 395 | |
| 396 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 397 | "Node doesn't have a token chain argument!"); |
| 398 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 399 | } |
| 400 | |
| 401 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 402 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 403 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 404 | /// |
| 405 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 406 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 407 | /// |
| 408 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 409 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 410 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 411 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 412 | // If we've already processed this node and it does lead to Dest, there is no |
| 413 | // need to reprocess it. |
| 414 | if (NodesLeadingTo.count(N)) return true; |
| 415 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 416 | // If the first result of this node has been already legalized, then it cannot |
| 417 | // reach N. |
| 418 | switch (getTypeAction(N->getValueType(0))) { |
| 419 | case Legal: |
| 420 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 421 | break; |
| 422 | case Promote: |
| 423 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 424 | break; |
| 425 | case Expand: |
| 426 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | // Okay, this node has not already been legalized. Check and legalize all |
| 431 | // operands. If none lead to Dest, then we can legalize this node. |
| 432 | bool OperandsLeadToDest = false; |
| 433 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 434 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 435 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 436 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 437 | if (OperandsLeadToDest) { |
| 438 | NodesLeadingTo.insert(N); |
| 439 | return true; |
| 440 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 441 | |
| 442 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 443 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 447 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 448 | /// appropriate for its type. |
| 449 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 450 | MVT::ValueType VT = Op.getValueType(); |
| 451 | switch (getTypeAction(VT)) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 452 | default: assert(0 && "Bad type action!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 453 | case Legal: (void)LegalizeOp(Op); break; |
| 454 | case Promote: (void)PromoteOp(Op); break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 455 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 456 | if (!MVT::isVector(VT)) { |
| 457 | // If this is an illegal scalar, expand it into its two component |
| 458 | // pieces. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 459 | SDOperand X, Y; |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 460 | if (Op.getOpcode() == ISD::TargetConstant) |
| 461 | break; // Allow illegal target nodes. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 462 | ExpandOp(Op, X, Y); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 463 | } else if (MVT::getVectorNumElements(VT) == 1) { |
| 464 | // If this is an illegal single element vector, convert it to a |
| 465 | // scalar operation. |
| 466 | (void)ScalarizeVectorOp(Op); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 467 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 468 | // Otherwise, this is an illegal multiple element vector. |
| 469 | // Split it in half and legalize both parts. |
| 470 | SDOperand X, Y; |
| 471 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 472 | } |
| 473 | break; |
| 474 | } |
| 475 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 476 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 477 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 478 | /// a load from the constant pool. |
| 479 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 480 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 481 | bool Extend = false; |
| 482 | |
| 483 | // If a FP immediate is precise when represented as a float and if the |
| 484 | // target can do an extending load from float to double, we put it into |
| 485 | // the constant pool as a float, even if it's is statically typed as a |
| 486 | // double. |
| 487 | MVT::ValueType VT = CFP->getValueType(0); |
| 488 | bool isDouble = VT == MVT::f64; |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 489 | ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT), |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 490 | CFP->getValueAPF()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 491 | if (!UseCP) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 492 | if (VT!=MVT::f64 && VT!=MVT::f32) |
| 493 | assert(0 && "Invalid type expansion"); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 494 | return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(), |
| 495 | isDouble ? MVT::i64 : MVT::i32); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 498 | if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) && |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 499 | // Only do this if the target has a native EXTLOAD instruction from f32. |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 500 | // Do not try to be clever about long doubles (so far) |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 501 | TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) { |
| 502 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy)); |
| 503 | VT = MVT::f32; |
| 504 | Extend = true; |
| 505 | } |
| 506 | |
| 507 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
| 508 | if (Extend) { |
| 509 | return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 510 | CPIdx, NULL, 0, MVT::f32); |
| 511 | } else { |
| 512 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
| 513 | } |
| 514 | } |
| 515 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 516 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 517 | /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise |
| 518 | /// operations. |
| 519 | static |
| 520 | SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT, |
| 521 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 522 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 523 | MVT::ValueType SrcVT = Node->getOperand(1).getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 524 | assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) && |
| 525 | "fcopysign expansion only supported for f32 and f64"); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 526 | MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 527 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 528 | // First get the sign bit of second operand. |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 529 | SDOperand Mask1 = (SrcVT == MVT::f64) |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 530 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT) |
| 531 | : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 532 | Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 533 | SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 534 | SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 535 | // Shift right or sign-extend it if the two operands have different types. |
| 536 | int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT); |
| 537 | if (SizeDiff > 0) { |
| 538 | SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit, |
| 539 | DAG.getConstant(SizeDiff, TLI.getShiftAmountTy())); |
| 540 | SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit); |
| 541 | } else if (SizeDiff < 0) |
| 542 | SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 543 | |
| 544 | // Clear the sign bit of first operand. |
| 545 | SDOperand Mask2 = (VT == MVT::f64) |
| 546 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 547 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 548 | Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 549 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 550 | Result = DAG.getNode(ISD::AND, NVT, Result, Mask2); |
| 551 | |
| 552 | // Or the value with the sign bit. |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 553 | Result = DAG.getNode(ISD::OR, NVT, Result, SignBit); |
| 554 | return Result; |
| 555 | } |
| 556 | |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 557 | /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. |
| 558 | static |
| 559 | SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, |
| 560 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 561 | SDOperand Chain = ST->getChain(); |
| 562 | SDOperand Ptr = ST->getBasePtr(); |
| 563 | SDOperand Val = ST->getValue(); |
| 564 | MVT::ValueType VT = Val.getValueType(); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 565 | int Alignment = ST->getAlignment(); |
| 566 | int SVOffset = ST->getSrcValueOffset(); |
| 567 | if (MVT::isFloatingPoint(ST->getStoredVT())) { |
| 568 | // Expand to a bitconvert of the value to the integer type of the |
| 569 | // same size, then a (misaligned) int store. |
| 570 | MVT::ValueType intVT; |
| 571 | if (VT==MVT::f64) |
| 572 | intVT = MVT::i64; |
| 573 | else if (VT==MVT::f32) |
| 574 | intVT = MVT::i32; |
| 575 | else |
| 576 | assert(0 && "Unaligned load of unsupported floating point type"); |
| 577 | |
| 578 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val); |
| 579 | return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(), |
| 580 | SVOffset, ST->isVolatile(), Alignment); |
| 581 | } |
| 582 | assert(MVT::isInteger(ST->getStoredVT()) && |
| 583 | "Unaligned store of unknown type."); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 584 | // Get the half-size VT |
| 585 | MVT::ValueType NewStoredVT = ST->getStoredVT() - 1; |
| 586 | int NumBits = MVT::getSizeInBits(NewStoredVT); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 587 | int IncrementSize = NumBits / 8; |
| 588 | |
| 589 | // Divide the stored value in two parts. |
| 590 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 591 | SDOperand Lo = Val; |
| 592 | SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount); |
| 593 | |
| 594 | // Store the two parts |
| 595 | SDOperand Store1, Store2; |
| 596 | Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr, |
| 597 | ST->getSrcValue(), SVOffset, NewStoredVT, |
| 598 | ST->isVolatile(), Alignment); |
| 599 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 600 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 601 | Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr, |
| 602 | ST->getSrcValue(), SVOffset + IncrementSize, |
| 603 | NewStoredVT, ST->isVolatile(), Alignment); |
| 604 | |
| 605 | return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2); |
| 606 | } |
| 607 | |
| 608 | /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. |
| 609 | static |
| 610 | SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, |
| 611 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 612 | int SVOffset = LD->getSrcValueOffset(); |
| 613 | SDOperand Chain = LD->getChain(); |
| 614 | SDOperand Ptr = LD->getBasePtr(); |
| 615 | MVT::ValueType VT = LD->getValueType(0); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 616 | MVT::ValueType LoadedVT = LD->getLoadedVT(); |
| 617 | if (MVT::isFloatingPoint(VT)) { |
| 618 | // Expand to a (misaligned) integer load of the same size, |
| 619 | // then bitconvert to floating point. |
| 620 | MVT::ValueType intVT; |
| 621 | if (LoadedVT==MVT::f64) |
| 622 | intVT = MVT::i64; |
| 623 | else if (LoadedVT==MVT::f32) |
| 624 | intVT = MVT::i32; |
| 625 | else |
| 626 | assert(0 && "Unaligned load of unsupported floating point type"); |
| 627 | |
| 628 | SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(), |
| 629 | SVOffset, LD->isVolatile(), |
| 630 | LD->getAlignment()); |
| 631 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad); |
| 632 | if (LoadedVT != VT) |
| 633 | Result = DAG.getNode(ISD::FP_EXTEND, VT, Result); |
| 634 | |
| 635 | SDOperand Ops[] = { Result, Chain }; |
| 636 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 637 | Ops, 2); |
| 638 | } |
| 639 | assert(MVT::isInteger(LoadedVT) && "Unaligned load of unsupported type."); |
| 640 | MVT::ValueType NewLoadedVT = LoadedVT - 1; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 641 | int NumBits = MVT::getSizeInBits(NewLoadedVT); |
| 642 | int Alignment = LD->getAlignment(); |
| 643 | int IncrementSize = NumBits / 8; |
| 644 | ISD::LoadExtType HiExtType = LD->getExtensionType(); |
| 645 | |
| 646 | // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. |
| 647 | if (HiExtType == ISD::NON_EXTLOAD) |
| 648 | HiExtType = ISD::ZEXTLOAD; |
| 649 | |
| 650 | // Load the value in two parts |
| 651 | SDOperand Lo, Hi; |
| 652 | if (TLI.isLittleEndian()) { |
| 653 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 654 | SVOffset, NewLoadedVT, LD->isVolatile(), Alignment); |
| 655 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 656 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 657 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), |
| 658 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
| 659 | Alignment); |
| 660 | } else { |
| 661 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset, |
| 662 | NewLoadedVT,LD->isVolatile(), Alignment); |
| 663 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 664 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 665 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 666 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
| 667 | Alignment); |
| 668 | } |
| 669 | |
| 670 | // aggregate the two parts |
| 671 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 672 | SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount); |
| 673 | Result = DAG.getNode(ISD::OR, VT, Result, Lo); |
| 674 | |
| 675 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 676 | Hi.getValue(1)); |
| 677 | |
| 678 | SDOperand Ops[] = { Result, TF }; |
| 679 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2); |
| 680 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 681 | |
Dan Gohman | a346615 | 2007-07-13 20:14:11 +0000 | [diff] [blame] | 682 | /// LegalizeOp - We know that the specified value has a legal type, and |
| 683 | /// that its operands are legal. Now ensure that the operation itself |
| 684 | /// is legal, recursively ensuring that the operands' operations remain |
| 685 | /// legal. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 686 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 687 | if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. |
| 688 | return Op; |
| 689 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 690 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 691 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 692 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 694 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 695 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 696 | if (Node->getNumValues() > 1) { |
| 697 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 698 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 699 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 700 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 701 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 702 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 706 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 707 | // means that we always must cache transformed nodes. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 708 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 709 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 710 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 711 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 712 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 713 | bool isCustom = false; |
| 714 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 715 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 716 | case ISD::FrameIndex: |
| 717 | case ISD::EntryToken: |
| 718 | case ISD::Register: |
| 719 | case ISD::BasicBlock: |
| 720 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 721 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 722 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 723 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 724 | case ISD::TargetConstantPool: |
| 725 | case ISD::TargetGlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 726 | case ISD::TargetGlobalTLSAddress: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 727 | case ISD::TargetExternalSymbol: |
| 728 | case ISD::VALUETYPE: |
| 729 | case ISD::SRCVALUE: |
| 730 | case ISD::STRING: |
| 731 | case ISD::CONDCODE: |
| 732 | // Primitives must all be legal. |
| 733 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 734 | "This must be legal!"); |
| 735 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 736 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 737 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 738 | // If this is a target node, legalize it by legalizing the operands then |
| 739 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 740 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 741 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 742 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 743 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 744 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 745 | |
| 746 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 747 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 748 | return Result.getValue(Op.ResNo); |
| 749 | } |
| 750 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 751 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 752 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 753 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 754 | assert(0 && "Do not know how to legalize this operator!"); |
| 755 | abort(); |
Lauro Ramos Venancio | 0d3b678 | 2007-04-20 23:02:39 +0000 | [diff] [blame] | 756 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 757 | case ISD::GlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 758 | case ISD::GlobalTLSAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 759 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 760 | case ISD::ConstantPool: |
| 761 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 762 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 763 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 764 | case TargetLowering::Custom: |
| 765 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 766 | if (Tmp1.Val) Result = Tmp1; |
| 767 | // 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] | 768 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 769 | break; |
| 770 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 771 | break; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 772 | case ISD::FRAMEADDR: |
| 773 | case ISD::RETURNADDR: |
| 774 | // The only option for these nodes is to custom lower them. If the target |
| 775 | // does not custom lower them, then return zero. |
| 776 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 777 | if (Tmp1.Val) |
| 778 | Result = Tmp1; |
| 779 | else |
| 780 | Result = DAG.getConstant(0, TLI.getPointerTy()); |
| 781 | break; |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 782 | case ISD::FRAME_TO_ARGS_OFFSET: { |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 783 | MVT::ValueType VT = Node->getValueType(0); |
| 784 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 785 | default: assert(0 && "This action is not supported yet!"); |
| 786 | case TargetLowering::Custom: |
| 787 | Result = TLI.LowerOperation(Op, DAG); |
| 788 | if (Result.Val) break; |
| 789 | // Fall Thru |
| 790 | case TargetLowering::Legal: |
| 791 | Result = DAG.getConstant(0, VT); |
| 792 | break; |
| 793 | } |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 794 | } |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 795 | break; |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 796 | case ISD::EXCEPTIONADDR: { |
| 797 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 798 | MVT::ValueType VT = Node->getValueType(0); |
| 799 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 800 | default: assert(0 && "This action is not supported yet!"); |
| 801 | case TargetLowering::Expand: { |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 802 | unsigned Reg = TLI.getExceptionAddressRegister(); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 803 | Result = DAG.getCopyFromReg(Tmp1, Reg, VT).getValue(Op.ResNo); |
| 804 | } |
| 805 | break; |
| 806 | case TargetLowering::Custom: |
| 807 | Result = TLI.LowerOperation(Op, DAG); |
| 808 | if (Result.Val) break; |
| 809 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 810 | case TargetLowering::Legal: { |
| 811 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 }; |
| 812 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 813 | Ops, 2).getValue(Op.ResNo); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 817 | } |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 818 | break; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 819 | case ISD::EHSELECTION: { |
| 820 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 821 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 822 | MVT::ValueType VT = Node->getValueType(0); |
| 823 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 824 | default: assert(0 && "This action is not supported yet!"); |
| 825 | case TargetLowering::Expand: { |
| 826 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 827 | Result = DAG.getCopyFromReg(Tmp2, Reg, VT).getValue(Op.ResNo); |
| 828 | } |
| 829 | break; |
| 830 | case TargetLowering::Custom: |
| 831 | Result = TLI.LowerOperation(Op, DAG); |
| 832 | if (Result.Val) break; |
| 833 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 834 | case TargetLowering::Legal: { |
| 835 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 }; |
| 836 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 837 | Ops, 2).getValue(Op.ResNo); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 841 | } |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 842 | break; |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 843 | case ISD::EH_RETURN: { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 844 | MVT::ValueType VT = Node->getValueType(0); |
| 845 | // The only "good" option for this node is to custom lower it. |
| 846 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 847 | default: assert(0 && "This action is not supported at all!"); |
| 848 | case TargetLowering::Custom: |
| 849 | Result = TLI.LowerOperation(Op, DAG); |
| 850 | if (Result.Val) break; |
| 851 | // Fall Thru |
| 852 | case TargetLowering::Legal: |
| 853 | // Target does not know, how to lower this, lower to noop |
| 854 | Result = LegalizeOp(Node->getOperand(0)); |
| 855 | break; |
| 856 | } |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 857 | } |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 858 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 859 | case ISD::AssertSext: |
| 860 | case ISD::AssertZext: |
| 861 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 862 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 863 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 864 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 865 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 866 | Result = Node->getOperand(Op.ResNo); |
| 867 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 868 | case ISD::CopyFromReg: |
| 869 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 870 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 871 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 872 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 873 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 874 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 875 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 876 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 877 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 878 | } else { |
| 879 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 880 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 881 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 882 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 883 | // Since CopyFromReg produces two values, make sure to remember that we |
| 884 | // legalized both of them. |
| 885 | AddLegalizedOperand(Op.getValue(0), Result); |
| 886 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 887 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 888 | case ISD::UNDEF: { |
| 889 | MVT::ValueType VT = Op.getValueType(); |
| 890 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 891 | default: assert(0 && "This action is not supported yet!"); |
| 892 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 893 | if (MVT::isInteger(VT)) |
| 894 | Result = DAG.getConstant(0, VT); |
| 895 | else if (MVT::isFloatingPoint(VT)) |
Dale Johannesen | f41db21 | 2007-09-26 17:26:49 +0000 | [diff] [blame] | 896 | Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)), |
| 897 | VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 898 | else |
| 899 | assert(0 && "Unknown value type!"); |
| 900 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 901 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 902 | break; |
| 903 | } |
| 904 | break; |
| 905 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 907 | case ISD::INTRINSIC_W_CHAIN: |
| 908 | case ISD::INTRINSIC_WO_CHAIN: |
| 909 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 910 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 911 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 912 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 913 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 914 | |
| 915 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 916 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 917 | TargetLowering::Custom) { |
| 918 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 919 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | if (Result.Val->getNumValues() == 1) break; |
| 923 | |
| 924 | // Must have return value and chain result. |
| 925 | assert(Result.Val->getNumValues() == 2 && |
| 926 | "Cannot return more than two values!"); |
| 927 | |
| 928 | // Since loads produce two values, make sure to remember that we |
| 929 | // legalized both of them. |
| 930 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 931 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 932 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 934 | |
| 935 | case ISD::LOCATION: |
| 936 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 937 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 938 | |
| 939 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 940 | case TargetLowering::Promote: |
| 941 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 942 | case TargetLowering::Expand: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 943 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 944 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 945 | bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 946 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 947 | if (MMI && (useDEBUG_LOC || useLABEL)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 948 | const std::string &FName = |
| 949 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 950 | const std::string &DirName = |
| 951 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 952 | unsigned SrcFile = MMI->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 953 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 954 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 955 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 956 | SDOperand LineOp = Node->getOperand(1); |
| 957 | SDOperand ColOp = Node->getOperand(2); |
| 958 | |
| 959 | if (useDEBUG_LOC) { |
| 960 | Ops.push_back(LineOp); // line # |
| 961 | Ops.push_back(ColOp); // col # |
| 962 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 963 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 964 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 965 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 966 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 967 | unsigned ID = MMI->RecordLabel(Line, Col, SrcFile); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 968 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 969 | Result = DAG.getNode(ISD::LABEL, MVT::Other,&Ops[0],Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 970 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 971 | } else { |
| 972 | Result = Tmp1; // chain |
| 973 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 974 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 975 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 976 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 977 | if (Tmp1 != Node->getOperand(0) || |
| 978 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 979 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 980 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 981 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 982 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 983 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 984 | } else { |
| 985 | // Otherwise promote them. |
| 986 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 987 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 988 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 989 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 990 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 991 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 992 | } |
| 993 | break; |
| 994 | } |
| 995 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 996 | |
| 997 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 998 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 999 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1000 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1001 | case TargetLowering::Legal: |
| 1002 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1003 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 1004 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 1005 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1006 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | } |
| 1009 | break; |
| 1010 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1011 | case ISD::LABEL: |
| 1012 | assert(Node->getNumOperands() == 2 && "Invalid LABEL node!"); |
| 1013 | switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1014 | default: assert(0 && "This action is not supported yet!"); |
| 1015 | case TargetLowering::Legal: |
| 1016 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1017 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1018 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1019 | break; |
Chris Lattner | a9569f1 | 2007-03-03 19:21:38 +0000 | [diff] [blame] | 1020 | case TargetLowering::Expand: |
| 1021 | Result = LegalizeOp(Node->getOperand(0)); |
| 1022 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1023 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1024 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1025 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1026 | case ISD::Constant: { |
| 1027 | ConstantSDNode *CN = cast<ConstantSDNode>(Node); |
| 1028 | unsigned opAction = |
| 1029 | TLI.getOperationAction(ISD::Constant, CN->getValueType(0)); |
| 1030 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1031 | // We know we don't need to expand constants here, constants only have one |
| 1032 | // value and we check that it is fine above. |
| 1033 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1034 | if (opAction == TargetLowering::Custom) { |
| 1035 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1036 | if (Tmp1.Val) |
| 1037 | Result = Tmp1; |
| 1038 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1039 | break; |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1041 | case ISD::ConstantFP: { |
| 1042 | // Spill FP immediates to the constant pool if the target cannot directly |
| 1043 | // codegen them. Targets often have some immediate values that can be |
| 1044 | // efficiently generated into an FP register without a load. We explicitly |
| 1045 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1046 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 1047 | |
| 1048 | // Check to see if this FP immediate is already legal. |
| 1049 | bool isLegal = false; |
| 1050 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 1051 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 1052 | if (CFP->isExactlyValue(*I)) { |
| 1053 | isLegal = true; |
| 1054 | break; |
| 1055 | } |
| 1056 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1057 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 1058 | if (isLegal) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1059 | Result = DAG.getTargetConstantFP(CFP->getValueAPF(), |
| 1060 | CFP->getValueType(0)); |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 1065 | default: assert(0 && "This action is not supported yet!"); |
| 1066 | case TargetLowering::Custom: |
| 1067 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1068 | if (Tmp3.Val) { |
| 1069 | Result = Tmp3; |
| 1070 | break; |
| 1071 | } |
| 1072 | // FALLTHROUGH |
| 1073 | case TargetLowering::Expand: |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 1074 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1075 | } |
| 1076 | break; |
| 1077 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1078 | case ISD::TokenFactor: |
| 1079 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1080 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1081 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1082 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1083 | } else if (Node->getNumOperands() == 3) { |
| 1084 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1085 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1086 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1087 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1088 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1089 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1090 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1091 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1092 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1093 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1094 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1095 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1096 | |
| 1097 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1098 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1099 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1100 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 1101 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
| 1102 | assert(Tmp3.Val->getNumValues() == Result.Val->getNumValues() && |
| 1103 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1105 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1106 | // 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] | 1107 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
| 1108 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1109 | if (Op.ResNo == i) |
| 1110 | Tmp2 = Tmp1; |
| 1111 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 1112 | } |
| 1113 | return Tmp2; |
Christopher Lamb | 557c363 | 2007-07-26 07:34:40 +0000 | [diff] [blame] | 1114 | case ISD::EXTRACT_SUBREG: { |
| 1115 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1116 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1)); |
| 1117 | assert(idx && "Operand must be a constant"); |
| 1118 | Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1119 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1120 | } |
| 1121 | break; |
| 1122 | case ISD::INSERT_SUBREG: { |
| 1123 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1124 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1125 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2)); |
| 1126 | assert(idx && "Operand must be a constant"); |
| 1127 | Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1128 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1129 | } |
| 1130 | break; |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1131 | case ISD::BUILD_VECTOR: |
| 1132 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1133 | default: assert(0 && "This action is not supported yet!"); |
| 1134 | case TargetLowering::Custom: |
| 1135 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1136 | if (Tmp3.Val) { |
| 1137 | Result = Tmp3; |
| 1138 | break; |
| 1139 | } |
| 1140 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1141 | case TargetLowering::Expand: |
| 1142 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1143 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1144 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | case ISD::INSERT_VECTOR_ELT: |
| 1147 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 1148 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 1149 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 1150 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1151 | |
| 1152 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 1153 | Node->getValueType(0))) { |
| 1154 | default: assert(0 && "This action is not supported yet!"); |
| 1155 | case TargetLowering::Legal: |
| 1156 | break; |
| 1157 | case TargetLowering::Custom: |
| 1158 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1159 | if (Tmp3.Val) { |
| 1160 | Result = Tmp3; |
| 1161 | break; |
| 1162 | } |
| 1163 | // FALLTHROUGH |
| 1164 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1165 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 1166 | // then a shuffle that inserts it into the right position in the vector. |
| 1167 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 1168 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 1169 | Tmp1.getValueType(), Tmp2); |
| 1170 | |
| 1171 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 1172 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1173 | MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1174 | |
| 1175 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 1176 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 1177 | // the RHS. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1178 | SmallVector<SDOperand, 8> ShufOps; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1179 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1180 | if (i != InsertPos->getValue()) |
| 1181 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 1182 | else |
| 1183 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 1184 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1185 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 1186 | &ShufOps[0], ShufOps.size()); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1187 | |
| 1188 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 1189 | Tmp1, ScVec, ShufMask); |
| 1190 | Result = LegalizeOp(Result); |
| 1191 | break; |
| 1192 | } |
| 1193 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1194 | // If the target doesn't support this, we have to spill the input vector |
| 1195 | // to a temporary stack slot, update the element, then reload it. This is |
| 1196 | // badness. We could also load the value into a vector register (either |
| 1197 | // with a "move to register" or "extload into register" instruction, then |
| 1198 | // permute it into place, if the idx is a constant and if the idx is |
| 1199 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1200 | MVT::ValueType VT = Tmp1.getValueType(); |
| 1201 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 1202 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 1203 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1204 | SDOperand StackPtr = CreateStackTemporary(VT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1205 | // Store the vector. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1206 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1207 | |
| 1208 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1209 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 1210 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1211 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1212 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 1213 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 1214 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1215 | // Store the scalar value. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1216 | Ch = DAG.getStore(Ch, Tmp2, StackPtr2, NULL, 0); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1217 | // Load the updated vector. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1218 | Result = DAG.getLoad(VT, Ch, StackPtr, NULL, 0); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1223 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1224 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 1225 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 1226 | break; |
| 1227 | } |
| 1228 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1229 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 1230 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1231 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 1232 | Node->getValueType(0))) { |
| 1233 | default: assert(0 && "This action is not supported yet!"); |
| 1234 | case TargetLowering::Legal: |
| 1235 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 1236 | case TargetLowering::Custom: |
| 1237 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1238 | if (Tmp3.Val) { |
| 1239 | Result = Tmp3; |
| 1240 | break; |
| 1241 | } |
| 1242 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1243 | case TargetLowering::Expand: |
| 1244 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1245 | break; |
| 1246 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1247 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1248 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1249 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1250 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 1251 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1252 | |
| 1253 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1254 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 1255 | default: assert(0 && "Unknown operation action!"); |
| 1256 | case TargetLowering::Legal: |
| 1257 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 1258 | "vector shuffle should not be created if not legal!"); |
| 1259 | break; |
| 1260 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1261 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1262 | if (Tmp3.Val) { |
| 1263 | Result = Tmp3; |
| 1264 | break; |
| 1265 | } |
| 1266 | // FALLTHROUGH |
| 1267 | case TargetLowering::Expand: { |
| 1268 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1269 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1270 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1271 | SDOperand Mask = Node->getOperand(2); |
| 1272 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1273 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1274 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1275 | SDOperand Arg = Mask.getOperand(i); |
| 1276 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1277 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1278 | } else { |
| 1279 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1280 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1281 | if (Idx < NumElems) |
| 1282 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1283 | DAG.getConstant(Idx, PtrVT))); |
| 1284 | else |
| 1285 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1286 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1287 | } |
| 1288 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1289 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1290 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1291 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1292 | case TargetLowering::Promote: { |
| 1293 | // Change base type to a different vector type. |
| 1294 | MVT::ValueType OVT = Node->getValueType(0); |
| 1295 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1296 | |
| 1297 | // Cast the two input vectors. |
| 1298 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1299 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1300 | |
| 1301 | // Convert the shuffle mask to the right # elements. |
| 1302 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1303 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1304 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1305 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1306 | break; |
| 1307 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1308 | } |
| 1309 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1310 | |
| 1311 | case ISD::EXTRACT_VECTOR_ELT: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1312 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1313 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1314 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1315 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1316 | break; |
| 1317 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1318 | case ISD::EXTRACT_SUBVECTOR: |
| 1319 | Tmp1 = Node->getOperand(0); |
| 1320 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1321 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1322 | Result = ExpandEXTRACT_SUBVECTOR(Result); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 1323 | break; |
| 1324 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1325 | case ISD::CALLSEQ_START: { |
| 1326 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1327 | |
| 1328 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1329 | // to this call start. This ensures that any libcalls that need be inserted |
| 1330 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1331 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1332 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1333 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1334 | NodesLeadingTo); |
| 1335 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Now that we legalized all of the inputs (which may have inserted |
| 1338 | // libcalls) create the new CALLSEQ_START node. |
| 1339 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1340 | |
| 1341 | // Merge in the last call, to ensure that this call start after the last |
| 1342 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1343 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1344 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1345 | Tmp1 = LegalizeOp(Tmp1); |
| 1346 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1347 | |
| 1348 | // Do not try to legalize the target-specific arguments (#1+). |
| 1349 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1350 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1351 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1352 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1356 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1357 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1358 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1359 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1360 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1361 | // sequence have been legalized, legalize the call itself. During this |
| 1362 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1363 | // can overlap. |
| 1364 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1365 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1366 | // Note that we are selecting this call! |
| 1367 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1368 | IsLegalizingCall = true; |
| 1369 | |
| 1370 | // Legalize the call, starting from the CALLSEQ_END. |
| 1371 | LegalizeOp(LastCALLSEQ_END); |
| 1372 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1373 | return Result; |
| 1374 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1375 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1376 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1377 | // will cause this node to be legalized as well as handling libcalls right. |
| 1378 | if (LastCALLSEQ_END.Val != Node) { |
| 1379 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 1380 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1381 | assert(I != LegalizedNodes.end() && |
| 1382 | "Legalizing the call start should have legalized this node!"); |
| 1383 | return I->second; |
| 1384 | } |
| 1385 | |
| 1386 | // Otherwise, the call start has been legalized and everything is going |
| 1387 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1388 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1389 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1390 | // an optional flag input. |
| 1391 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1392 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1393 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1394 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1395 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } else { |
| 1398 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1399 | if (Tmp1 != Node->getOperand(0) || |
| 1400 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1401 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1402 | Ops[0] = Tmp1; |
| 1403 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1404 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1405 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1406 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1407 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1408 | // This finishes up call legalization. |
| 1409 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1410 | |
| 1411 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1412 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1413 | if (Node->getNumValues() == 2) |
| 1414 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1415 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1416 | case ISD::DYNAMIC_STACKALLOC: { |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1417 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1418 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1419 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1420 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1421 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1423 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1424 | Tmp2 = Result.getValue(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1425 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1426 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1427 | case TargetLowering::Expand: { |
| 1428 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1429 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1430 | " not tell us which reg is the stack pointer!"); |
| 1431 | SDOperand Chain = Tmp1.getOperand(0); |
| 1432 | SDOperand Size = Tmp2.getOperand(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1433 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT); |
| 1434 | Chain = SP.getValue(1); |
| 1435 | unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue(); |
| 1436 | unsigned StackAlign = |
| 1437 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1438 | if (Align > StackAlign) |
Evan Cheng | 3e20bba | 2007-08-17 18:02:22 +0000 | [diff] [blame] | 1439 | SP = DAG.getNode(ISD::AND, VT, SP, |
| 1440 | DAG.getConstant(-(uint64_t)Align, VT)); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1441 | Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value |
| 1442 | Tmp2 = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1443 | Tmp1 = LegalizeOp(Tmp1); |
| 1444 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1448 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1449 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1450 | Tmp1 = LegalizeOp(Tmp3); |
| 1451 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1452 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1453 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1454 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1455 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1456 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1457 | // Since this op produce two values, make sure to remember that we |
| 1458 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1459 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1460 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1461 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1462 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1463 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1464 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1465 | bool Changed = false; |
| 1466 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1467 | // that need to be expanded or something. Note we skip the asm string and |
| 1468 | // all of the TargetConstant flags. |
| 1469 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1470 | Changed = Op != Ops[0]; |
| 1471 | Ops[0] = Op; |
| 1472 | |
| 1473 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1474 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1475 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1476 | for (++i; NumVals; ++i, --NumVals) { |
| 1477 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1478 | if (Op != Ops[i]) { |
| 1479 | Changed = true; |
| 1480 | Ops[i] = Op; |
| 1481 | } |
| 1482 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1483 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1484 | |
| 1485 | if (HasInFlag) { |
| 1486 | Op = LegalizeOp(Ops.back()); |
| 1487 | Changed |= Op != Ops.back(); |
| 1488 | Ops.back() = Op; |
| 1489 | } |
| 1490 | |
| 1491 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1492 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1493 | |
| 1494 | // 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] | 1495 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1496 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1497 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1498 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1499 | case ISD::BR: |
| 1500 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1501 | // Ensure that libcalls are emitted before a branch. |
| 1502 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1503 | Tmp1 = LegalizeOp(Tmp1); |
| 1504 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1505 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1506 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1507 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1508 | case ISD::BRIND: |
| 1509 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1510 | // Ensure that libcalls are emitted before a branch. |
| 1511 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1512 | Tmp1 = LegalizeOp(Tmp1); |
| 1513 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1514 | |
| 1515 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1516 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1517 | case Legal: |
| 1518 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1519 | break; |
| 1520 | } |
| 1521 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1522 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1523 | case ISD::BR_JT: |
| 1524 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1525 | // Ensure that libcalls are emitted before a branch. |
| 1526 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1527 | Tmp1 = LegalizeOp(Tmp1); |
| 1528 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1529 | |
| 1530 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1531 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1532 | |
| 1533 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1534 | default: assert(0 && "This action is not supported yet!"); |
| 1535 | case TargetLowering::Legal: break; |
| 1536 | case TargetLowering::Custom: |
| 1537 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1538 | if (Tmp1.Val) Result = Tmp1; |
| 1539 | break; |
| 1540 | case TargetLowering::Expand: { |
| 1541 | SDOperand Chain = Result.getOperand(0); |
| 1542 | SDOperand Table = Result.getOperand(1); |
| 1543 | SDOperand Index = Result.getOperand(2); |
| 1544 | |
| 1545 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1546 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1547 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1548 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1549 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1550 | |
| 1551 | SDOperand LD; |
| 1552 | switch (EntrySize) { |
| 1553 | default: assert(0 && "Size of jump table not supported yet."); break; |
| 1554 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, NULL, 0); break; |
| 1555 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, NULL, 0); break; |
| 1556 | } |
| 1557 | |
| 1558 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1559 | // For PIC, the sequence is: |
| 1560 | // BRIND(load(Jumptable + index) + RelocBase) |
| 1561 | // RelocBase is the JumpTable on PPC and X86, GOT on Alpha |
| 1562 | SDOperand Reloc; |
| 1563 | if (TLI.usesGlobalOffsetTable()) |
| 1564 | Reloc = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, PTy); |
| 1565 | else |
| 1566 | Reloc = Table; |
Evan Cheng | d063189 | 2006-10-31 02:31:00 +0000 | [diff] [blame] | 1567 | Addr = (PTy != MVT::i32) ? DAG.getNode(ISD::SIGN_EXTEND, PTy, LD) : LD; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1568 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, Reloc); |
| 1569 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
| 1570 | } else { |
| 1571 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), LD); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1576 | case ISD::BRCOND: |
| 1577 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1578 | // Ensure that libcalls are emitted before a return. |
| 1579 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1580 | Tmp1 = LegalizeOp(Tmp1); |
| 1581 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1582 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1583 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1584 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1585 | case Legal: |
| 1586 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1587 | break; |
| 1588 | case Promote: |
| 1589 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1590 | |
| 1591 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1592 | // that the value is properly zero extended. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1593 | if (!DAG.MaskedValueIsZero(Tmp2, |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1594 | MVT::getIntVTBitMask(Tmp2.getValueType())^1)) |
| 1595 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1598 | |
| 1599 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1600 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1601 | |
| 1602 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1603 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1604 | case TargetLowering::Legal: break; |
| 1605 | case TargetLowering::Custom: |
| 1606 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1607 | if (Tmp1.Val) Result = Tmp1; |
| 1608 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1609 | case TargetLowering::Expand: |
| 1610 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1611 | // Node. |
| 1612 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1613 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1614 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1615 | Node->getOperand(2)); |
| 1616 | } else { |
| 1617 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1618 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1619 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1620 | Node->getOperand(2)); |
| 1621 | } |
| 1622 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1623 | } |
| 1624 | break; |
| 1625 | case ISD::BR_CC: |
| 1626 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1627 | // Ensure that libcalls are emitted before a branch. |
| 1628 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1629 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1630 | Tmp2 = Node->getOperand(2); // LHS |
| 1631 | Tmp3 = Node->getOperand(3); // RHS |
| 1632 | Tmp4 = Node->getOperand(1); // CC |
| 1633 | |
| 1634 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1635 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1636 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1637 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1638 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1639 | // the result against zero to select between true and false values. |
| 1640 | if (Tmp3.Val == 0) { |
| 1641 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1642 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1643 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1644 | |
| 1645 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1646 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1648 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1649 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1650 | case TargetLowering::Legal: break; |
| 1651 | case TargetLowering::Custom: |
| 1652 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1653 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1654 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1655 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1656 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1657 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1658 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1659 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1660 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1661 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1662 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1663 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1664 | MVT::ValueType VT = Node->getValueType(0); |
| 1665 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1666 | Tmp3 = Result.getValue(0); |
| 1667 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1668 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1669 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1670 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1671 | case TargetLowering::Legal: |
| 1672 | // If this is an unaligned load and the target doesn't support it, |
| 1673 | // expand it. |
| 1674 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1675 | unsigned ABIAlignment = TLI.getTargetData()-> |
| 1676 | getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT())); |
| 1677 | if (LD->getAlignment() < ABIAlignment){ |
| 1678 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 1679 | TLI); |
| 1680 | Tmp3 = Result.getOperand(0); |
| 1681 | Tmp4 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1682 | Tmp3 = LegalizeOp(Tmp3); |
| 1683 | Tmp4 = LegalizeOp(Tmp4); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1687 | case TargetLowering::Custom: |
| 1688 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1689 | if (Tmp1.Val) { |
| 1690 | Tmp3 = LegalizeOp(Tmp1); |
| 1691 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1692 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1693 | break; |
| 1694 | case TargetLowering::Promote: { |
| 1695 | // Only promote a load of vector type to another. |
| 1696 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1697 | // Change base type to a different vector type. |
| 1698 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1699 | |
| 1700 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1701 | LD->getSrcValueOffset(), |
| 1702 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1703 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1704 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1705 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1706 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1707 | } |
| 1708 | // Since loads produce two values, make sure to remember that we |
| 1709 | // legalized both of them. |
| 1710 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1711 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1712 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1713 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 1714 | MVT::ValueType SrcVT = LD->getLoadedVT(); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1715 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 1716 | default: assert(0 && "This action is not supported yet!"); |
| 1717 | case TargetLowering::Promote: |
| 1718 | assert(SrcVT == MVT::i1 && |
| 1719 | "Can only promote extending LOAD from i1 -> i8!"); |
| 1720 | Result = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1721 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1722 | MVT::i8, LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1723 | Tmp1 = Result.getValue(0); |
| 1724 | Tmp2 = Result.getValue(1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1725 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1726 | case TargetLowering::Custom: |
| 1727 | isCustom = true; |
| 1728 | // FALLTHROUGH |
| 1729 | case TargetLowering::Legal: |
| 1730 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1731 | Tmp1 = Result.getValue(0); |
| 1732 | Tmp2 = Result.getValue(1); |
| 1733 | |
| 1734 | if (isCustom) { |
| 1735 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1736 | if (Tmp3.Val) { |
| 1737 | Tmp1 = LegalizeOp(Tmp3); |
| 1738 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1739 | } |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1740 | } else { |
| 1741 | // If this is an unaligned load and the target doesn't support it, |
| 1742 | // expand it. |
| 1743 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1744 | unsigned ABIAlignment = TLI.getTargetData()-> |
| 1745 | getABITypeAlignment(MVT::getTypeForValueType(LD->getLoadedVT())); |
| 1746 | if (LD->getAlignment() < ABIAlignment){ |
| 1747 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 1748 | TLI); |
| 1749 | Tmp1 = Result.getOperand(0); |
| 1750 | Tmp2 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1751 | Tmp1 = LegalizeOp(Tmp1); |
| 1752 | Tmp2 = LegalizeOp(Tmp2); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1753 | } |
| 1754 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1755 | } |
| 1756 | break; |
| 1757 | case TargetLowering::Expand: |
| 1758 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 1759 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1760 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1761 | LD->getSrcValueOffset(), |
| 1762 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1763 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 1764 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1765 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1766 | break; |
| 1767 | } |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1768 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1769 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1770 | // zero/sign extend inreg. |
| 1771 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1772 | Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1773 | LD->getSrcValueOffset(), SrcVT, |
| 1774 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1775 | SDOperand ValRes; |
| 1776 | if (ExtType == ISD::SEXTLOAD) |
| 1777 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1778 | Result, DAG.getValueType(SrcVT)); |
| 1779 | else |
| 1780 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 1781 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1782 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1783 | break; |
| 1784 | } |
| 1785 | // Since loads produce two values, make sure to remember that we legalized |
| 1786 | // both of them. |
| 1787 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1788 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1789 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1790 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1791 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1792 | case ISD::EXTRACT_ELEMENT: { |
| 1793 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1794 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1795 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1796 | case Legal: |
| 1797 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1798 | // 1 -> Hi |
| 1799 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1800 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1801 | TLI.getShiftAmountTy())); |
| 1802 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1803 | } else { |
| 1804 | // 0 -> Lo |
| 1805 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1806 | Node->getOperand(0)); |
| 1807 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1808 | break; |
| 1809 | case Expand: |
| 1810 | // Get both the low and high parts. |
| 1811 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1812 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1813 | Result = Tmp2; // 1 -> Hi |
| 1814 | else |
| 1815 | Result = Tmp1; // 0 -> Lo |
| 1816 | break; |
| 1817 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1818 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1819 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1820 | |
| 1821 | case ISD::CopyToReg: |
| 1822 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1823 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1824 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1825 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1826 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1827 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1828 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1829 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1830 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1831 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1832 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1833 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1834 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1835 | Tmp3); |
| 1836 | } else { |
| 1837 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Since this produces two values, make sure to remember that we legalized |
| 1841 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1842 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1843 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1844 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1845 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1846 | break; |
| 1847 | |
| 1848 | case ISD::RET: |
| 1849 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Ensure that libcalls are emitted before a return. |
| 1852 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1853 | Tmp1 = LegalizeOp(Tmp1); |
| 1854 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1855 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1856 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1857 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 1858 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1859 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1860 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1861 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1862 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1863 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1864 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1865 | if (!MVT::isVector(Tmp2.getValueType())) { |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1866 | SDOperand Lo, Hi; |
| 1867 | ExpandOp(Tmp2, Lo, Hi); |
Chris Lattner | edf2e8d | 2007-03-06 20:01:06 +0000 | [diff] [blame] | 1868 | |
| 1869 | // Big endian systems want the hi reg first. |
| 1870 | if (!TLI.isLittleEndian()) |
| 1871 | std::swap(Lo, Hi); |
| 1872 | |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1873 | if (Hi.Val) |
| 1874 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 1875 | else |
| 1876 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 1877 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1878 | } else { |
| 1879 | SDNode *InVal = Tmp2.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1880 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 1881 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1882 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1883 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1884 | // type. If so, convert to the vector type. |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1885 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1886 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1887 | // Turn this into a return of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1888 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1889 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1890 | } else if (NumElems == 1) { |
| 1891 | // Turn this into a return of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1892 | Tmp2 = ScalarizeVectorOp(Tmp2); |
| 1893 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1894 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1895 | |
| 1896 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 1897 | // should be returned in integer registers! |
| 1898 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1899 | // The scalarized value type may not be legal, e.g. it might require |
| 1900 | // promotion or expansion. Relegalize the return. |
| 1901 | Result = LegalizeOp(Result); |
| 1902 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1903 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 1904 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1905 | SDOperand Lo, Hi; |
| 1906 | SplitVectorOp(Tmp2, Lo, Hi); |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 1907 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 1908 | Result = LegalizeOp(Result); |
| 1909 | } |
| 1910 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1911 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1912 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1913 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1914 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1915 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1916 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1917 | } |
| 1918 | break; |
| 1919 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1920 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1921 | break; |
| 1922 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1923 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1924 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1925 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1926 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1927 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1928 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1929 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1930 | break; |
| 1931 | case Expand: { |
| 1932 | SDOperand Lo, Hi; |
Dan Gohman | 6595cb3 | 2007-06-27 16:08:04 +0000 | [diff] [blame] | 1933 | assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) && |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 1934 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1935 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1936 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 1937 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 1938 | if (Hi.Val) { |
| 1939 | NewValues.push_back(Hi); |
| 1940 | NewValues.push_back(Node->getOperand(i+1)); |
| 1941 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1942 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1943 | } |
| 1944 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1945 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1946 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1949 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1950 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1951 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 1952 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1953 | break; |
| 1954 | } |
| 1955 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1956 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1957 | if (Result.getOpcode() == ISD::RET) { |
| 1958 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1959 | default: assert(0 && "This action is not supported yet!"); |
| 1960 | case TargetLowering::Legal: break; |
| 1961 | case TargetLowering::Custom: |
| 1962 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1963 | if (Tmp1.Val) Result = Tmp1; |
| 1964 | break; |
| 1965 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1966 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1967 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1968 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1969 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1970 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1971 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1972 | int SVOffset = ST->getSrcValueOffset(); |
| 1973 | unsigned Alignment = ST->getAlignment(); |
| 1974 | bool isVolatile = ST->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1975 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1976 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1977 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 1978 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 1979 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 1980 | // to phase ordering between legalized code and the dag combiner. This |
| 1981 | // probably means that we need to integrate dag combiner and legalizer |
| 1982 | // together. |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1983 | // We generally can't do this one for long doubles. |
| 1984 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1985 | if (CFP->getValueType(0) == MVT::f32) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1986 | Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
| 1987 | convertToAPInt().getZExtValue(), |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 1988 | MVT::i32); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1989 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1990 | SVOffset, isVolatile, Alignment); |
| 1991 | break; |
| 1992 | } else if (CFP->getValueType(0) == MVT::f64) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1993 | Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt(). |
| 1994 | getZExtValue(), MVT::i64); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 1995 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 1996 | SVOffset, isVolatile, Alignment); |
| 1997 | break; |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1998 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2001 | switch (getTypeAction(ST->getStoredVT())) { |
| 2002 | case Legal: { |
| 2003 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2004 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2005 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2006 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2007 | MVT::ValueType VT = Tmp3.getValueType(); |
| 2008 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 2009 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2010 | case TargetLowering::Legal: |
| 2011 | // If this is an unaligned store and the target doesn't support it, |
| 2012 | // expand it. |
| 2013 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2014 | unsigned ABIAlignment = TLI.getTargetData()-> |
| 2015 | getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT())); |
| 2016 | if (ST->getAlignment() < ABIAlignment) |
| 2017 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2018 | TLI); |
| 2019 | } |
| 2020 | break; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2021 | case TargetLowering::Custom: |
| 2022 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2023 | if (Tmp1.Val) Result = Tmp1; |
| 2024 | break; |
| 2025 | case TargetLowering::Promote: |
| 2026 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 2027 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 2028 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 2029 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2030 | ST->getSrcValue(), SVOffset, isVolatile, |
| 2031 | Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2032 | break; |
| 2033 | } |
| 2034 | break; |
| 2035 | } |
| 2036 | case Promote: |
| 2037 | // Truncate the value and store the result. |
| 2038 | Tmp3 = PromoteOp(ST->getValue()); |
| 2039 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2040 | SVOffset, ST->getStoredVT(), |
| 2041 | isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2042 | break; |
| 2043 | |
| 2044 | case Expand: |
| 2045 | unsigned IncrementSize = 0; |
| 2046 | SDOperand Lo, Hi; |
| 2047 | |
| 2048 | // If this is a vector type, then we have to calculate the increment as |
| 2049 | // the product of the element size in bytes, and the number of elements |
| 2050 | // in the high half of the vector. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2051 | if (MVT::isVector(ST->getValue().getValueType())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2052 | SDNode *InVal = ST->getValue().Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2053 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 2054 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2055 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2056 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2057 | // type. If so, convert to the vector type. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2058 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2059 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2060 | // Turn this into a normal store of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2061 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2062 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2063 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2064 | Result = LegalizeOp(Result); |
| 2065 | break; |
| 2066 | } else if (NumElems == 1) { |
| 2067 | // Turn this into a normal store of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2068 | Tmp3 = ScalarizeVectorOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2069 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2070 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2071 | // The scalarized value type may not be legal, e.g. it might require |
| 2072 | // promotion or expansion. Relegalize the scalar store. |
| 2073 | Result = LegalizeOp(Result); |
| 2074 | break; |
| 2075 | } else { |
| 2076 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 2077 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 2078 | } |
| 2079 | } else { |
| 2080 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2081 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2082 | |
| 2083 | if (!TLI.isLittleEndian()) |
| 2084 | std::swap(Lo, Hi); |
| 2085 | } |
| 2086 | |
| 2087 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2088 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2089 | |
| 2090 | if (Hi.Val == NULL) { |
| 2091 | // Must be int <-> float one-to-one expansion. |
| 2092 | Result = Lo; |
| 2093 | break; |
| 2094 | } |
| 2095 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2096 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2097 | getIntPtrConstant(IncrementSize)); |
| 2098 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 2099 | "Pointers must be legal!"); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2100 | SVOffset += IncrementSize; |
| 2101 | if (Alignment > IncrementSize) |
| 2102 | Alignment = IncrementSize; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2103 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2104 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2105 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2106 | break; |
| 2107 | } |
| 2108 | } else { |
| 2109 | // Truncating store |
| 2110 | assert(isTypeLegal(ST->getValue().getValueType()) && |
| 2111 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 2112 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2113 | |
| 2114 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 2115 | // -> TRUNCSTORE:i8 (and X, 1) |
| 2116 | if (ST->getStoredVT() == MVT::i1 && |
| 2117 | TLI.getStoreXAction(MVT::i1) == TargetLowering::Promote) { |
| 2118 | // Promote the bool to a mask then store. |
| 2119 | Tmp3 = DAG.getNode(ISD::AND, Tmp3.getValueType(), Tmp3, |
| 2120 | DAG.getConstant(1, Tmp3.getValueType())); |
| 2121 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2122 | SVOffset, MVT::i8, |
| 2123 | isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2124 | } else if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 2125 | Tmp2 != ST->getBasePtr()) { |
| 2126 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2127 | ST->getOffset()); |
| 2128 | } |
| 2129 | |
| 2130 | MVT::ValueType StVT = cast<StoreSDNode>(Result.Val)->getStoredVT(); |
| 2131 | switch (TLI.getStoreXAction(StVT)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2132 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2133 | case TargetLowering::Legal: |
| 2134 | // If this is an unaligned store and the target doesn't support it, |
| 2135 | // expand it. |
| 2136 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2137 | unsigned ABIAlignment = TLI.getTargetData()-> |
| 2138 | getABITypeAlignment(MVT::getTypeForValueType(ST->getStoredVT())); |
| 2139 | if (ST->getAlignment() < ABIAlignment) |
| 2140 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2141 | TLI); |
| 2142 | } |
| 2143 | break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2144 | case TargetLowering::Custom: |
| 2145 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2146 | if (Tmp1.Val) Result = Tmp1; |
| 2147 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2148 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2149 | } |
| 2150 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2151 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2152 | case ISD::PCMARKER: |
| 2153 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2154 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2155 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2156 | case ISD::STACKSAVE: |
| 2157 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2158 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2159 | Tmp1 = Result.getValue(0); |
| 2160 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2162 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 2163 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2164 | case TargetLowering::Legal: break; |
| 2165 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2166 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 2167 | if (Tmp3.Val) { |
| 2168 | Tmp1 = LegalizeOp(Tmp3); |
| 2169 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2170 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2171 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2172 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2173 | // Expand to CopyFromReg if the target set |
| 2174 | // StackPointerRegisterToSaveRestore. |
| 2175 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2176 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2177 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2178 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2179 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2180 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 2181 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2182 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2183 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2184 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Since stacksave produce two values, make sure to remember that we |
| 2187 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2188 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2189 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 2190 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2192 | case ISD::STACKRESTORE: |
| 2193 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2194 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2195 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2196 | |
| 2197 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 2198 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2199 | case TargetLowering::Legal: break; |
| 2200 | case TargetLowering::Custom: |
| 2201 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2202 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2205 | // Expand to CopyToReg if the target set |
| 2206 | // StackPointerRegisterToSaveRestore. |
| 2207 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 2208 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 2209 | } else { |
| 2210 | Result = Tmp1; |
| 2211 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2212 | break; |
| 2213 | } |
| 2214 | break; |
| 2215 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2216 | case ISD::READCYCLECOUNTER: |
| 2217 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2218 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2219 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 2220 | Node->getValueType(0))) { |
| 2221 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2222 | case TargetLowering::Legal: |
| 2223 | Tmp1 = Result.getValue(0); |
| 2224 | Tmp2 = Result.getValue(1); |
| 2225 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2226 | case TargetLowering::Custom: |
| 2227 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2228 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 2229 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2230 | break; |
| 2231 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 2232 | |
| 2233 | // Since rdcc produce two values, make sure to remember that we legalized |
| 2234 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2235 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2236 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2237 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2239 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2240 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2241 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 2242 | case Legal: |
| 2243 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2244 | break; |
| 2245 | case Promote: |
| 2246 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2247 | // Make sure the condition is either zero or one. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2248 | if (!DAG.MaskedValueIsZero(Tmp1, |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2249 | MVT::getIntVTBitMask(Tmp1.getValueType())^1)) |
| 2250 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2251 | break; |
| 2252 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2253 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2254 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2256 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2257 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2258 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2259 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2260 | case TargetLowering::Legal: break; |
| 2261 | case TargetLowering::Custom: { |
| 2262 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2263 | if (Tmp1.Val) Result = Tmp1; |
| 2264 | break; |
| 2265 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2266 | case TargetLowering::Expand: |
| 2267 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 2268 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 2269 | Tmp2, Tmp3, |
| 2270 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 2271 | } else { |
| 2272 | Result = DAG.getSelectCC(Tmp1, |
| 2273 | DAG.getConstant(0, Tmp1.getValueType()), |
| 2274 | Tmp2, Tmp3, ISD::SETNE); |
| 2275 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2276 | break; |
| 2277 | case TargetLowering::Promote: { |
| 2278 | MVT::ValueType NVT = |
| 2279 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 2280 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 2281 | if (MVT::isVector(Tmp2.getValueType())) { |
| 2282 | ExtOp = ISD::BIT_CONVERT; |
| 2283 | TruncOp = ISD::BIT_CONVERT; |
| 2284 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2285 | ExtOp = ISD::ANY_EXTEND; |
| 2286 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2287 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2288 | ExtOp = ISD::FP_EXTEND; |
| 2289 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2290 | } |
| 2291 | // Promote each of the values to the new type. |
| 2292 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 2293 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 2294 | // Perform the larger operation, then round down. |
| 2295 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 2296 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2300 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2301 | case ISD::SELECT_CC: { |
| 2302 | Tmp1 = Node->getOperand(0); // LHS |
| 2303 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2304 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 2305 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2306 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2307 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2308 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 2309 | |
| 2310 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 2311 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 2312 | // the result against zero to select between true and false values. |
| 2313 | if (Tmp2.Val == 0) { |
| 2314 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2315 | CC = DAG.getCondCode(ISD::SETNE); |
| 2316 | } |
| 2317 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 2318 | |
| 2319 | // Everything is legal, see if we should expand this op or something. |
| 2320 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 2321 | default: assert(0 && "This action is not supported yet!"); |
| 2322 | case TargetLowering::Legal: break; |
| 2323 | case TargetLowering::Custom: |
| 2324 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2325 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2326 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2327 | } |
| 2328 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2329 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2330 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2331 | Tmp1 = Node->getOperand(0); |
| 2332 | Tmp2 = Node->getOperand(1); |
| 2333 | Tmp3 = Node->getOperand(2); |
| 2334 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 2335 | |
| 2336 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2337 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2338 | // return the value we legalized, returned in the LHS |
| 2339 | if (Tmp2.Val == 0) { |
| 2340 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2341 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2342 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2344 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2345 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2346 | case TargetLowering::Custom: |
| 2347 | isCustom = true; |
| 2348 | // FALLTHROUGH. |
| 2349 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2350 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2351 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2352 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2353 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2354 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2355 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2356 | case TargetLowering::Promote: { |
| 2357 | // First step, figure out the appropriate operation to use. |
| 2358 | // Allow SETCC to not be supported for all legal data types |
| 2359 | // Mostly this targets FP |
| 2360 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 2361 | MVT::ValueType OldVT = NewInTy; OldVT = OldVT; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2362 | |
| 2363 | // Scan for the appropriate larger type to use. |
| 2364 | while (1) { |
| 2365 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2366 | |
| 2367 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2368 | "Fell off of the edge of the integer world"); |
| 2369 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2370 | "Fell off of the edge of the floating point world"); |
| 2371 | |
| 2372 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2373 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2374 | break; |
| 2375 | } |
| 2376 | if (MVT::isInteger(NewInTy)) |
| 2377 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2378 | else { |
| 2379 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2380 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2381 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2382 | Tmp1 = LegalizeOp(Tmp1); |
| 2383 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2384 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2385 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2386 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2387 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2388 | case TargetLowering::Expand: |
| 2389 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2390 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2391 | MVT::ValueType VT = Node->getValueType(0); |
| 2392 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2393 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2394 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2395 | break; |
| 2396 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2397 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2398 | case ISD::MEMSET: |
| 2399 | case ISD::MEMCPY: |
| 2400 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2401 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2402 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 2403 | |
| 2404 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 2405 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 2406 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 2407 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2408 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2409 | break; |
| 2410 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2411 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2412 | break; |
| 2413 | } |
| 2414 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2415 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2416 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2417 | |
| 2418 | SDOperand Tmp4; |
| 2419 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2420 | case Expand: { |
| 2421 | // Length is too big, just take the lo-part of the length. |
| 2422 | SDOperand HiPart; |
Chris Lattner | fa9aa2b | 2006-11-07 04:11:44 +0000 | [diff] [blame] | 2423 | ExpandOp(Node->getOperand(3), Tmp4, HiPart); |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2424 | break; |
| 2425 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2426 | case Legal: |
| 2427 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | case Promote: |
| 2430 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2431 | break; |
| 2432 | } |
| 2433 | |
| 2434 | SDOperand Tmp5; |
| 2435 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 2436 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2437 | case Legal: |
| 2438 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 2439 | break; |
| 2440 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2441 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 2442 | break; |
| 2443 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2444 | |
| 2445 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2446 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2447 | case TargetLowering::Custom: |
| 2448 | isCustom = true; |
| 2449 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2450 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2451 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2452 | if (isCustom) { |
| 2453 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2454 | if (Tmp1.Val) Result = Tmp1; |
| 2455 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2458 | // Otherwise, the target does not support this operation. Lower the |
| 2459 | // operation to an explicit libcall as appropriate. |
| 2460 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2461 | const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2462 | TargetLowering::ArgListTy Args; |
| 2463 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2464 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 2465 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2466 | if (Node->getOpcode() == ISD::MEMSET) { |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2467 | Entry.Node = Tmp2; Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2468 | Args.push_back(Entry); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 2469 | // Extend the (previously legalized) ubyte argument to be an int value |
| 2470 | // for the call. |
| 2471 | if (Tmp3.getValueType() > MVT::i32) |
| 2472 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 2473 | else |
| 2474 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2475 | Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2476 | Args.push_back(Entry); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2477 | Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2478 | Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2479 | |
| 2480 | FnName = "memset"; |
| 2481 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2482 | Node->getOpcode() == ISD::MEMMOVE) { |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame] | 2483 | Entry.Ty = IntPtrTy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 2484 | Entry.Node = Tmp2; Args.push_back(Entry); |
| 2485 | Entry.Node = Tmp3; Args.push_back(Entry); |
| 2486 | Entry.Node = Tmp4; Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2487 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2488 | } else { |
| 2489 | assert(0 && "Unknown op!"); |
| 2490 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2492 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2493 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2494 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2495 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2498 | } |
| 2499 | break; |
| 2500 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2501 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2502 | case ISD::SHL_PARTS: |
| 2503 | case ISD::SRA_PARTS: |
| 2504 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2505 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2506 | bool Changed = false; |
| 2507 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2508 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2509 | Changed |= Ops.back() != Node->getOperand(i); |
| 2510 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2511 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2512 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2513 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2514 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2515 | Node->getValueType(0))) { |
| 2516 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2517 | case TargetLowering::Legal: break; |
| 2518 | case TargetLowering::Custom: |
| 2519 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2520 | if (Tmp1.Val) { |
| 2521 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2522 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2523 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2524 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2525 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2526 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2527 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2528 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2529 | return RetVal; |
| 2530 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2531 | break; |
| 2532 | } |
| 2533 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2534 | // Since these produce multiple values, make sure to remember that we |
| 2535 | // legalized all of them. |
| 2536 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2537 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2538 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2539 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2540 | |
| 2541 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2542 | case ISD::ADD: |
| 2543 | case ISD::SUB: |
| 2544 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2545 | case ISD::MULHS: |
| 2546 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2547 | case ISD::UDIV: |
| 2548 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2549 | case ISD::AND: |
| 2550 | case ISD::OR: |
| 2551 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2552 | case ISD::SHL: |
| 2553 | case ISD::SRL: |
| 2554 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2555 | case ISD::FADD: |
| 2556 | case ISD::FSUB: |
| 2557 | case ISD::FMUL: |
| 2558 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2559 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2560 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2561 | case Expand: assert(0 && "Not possible"); |
| 2562 | case Legal: |
| 2563 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2564 | break; |
| 2565 | case Promote: |
| 2566 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2567 | break; |
| 2568 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2569 | |
| 2570 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2571 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2572 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2573 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2574 | case TargetLowering::Legal: break; |
| 2575 | case TargetLowering::Custom: |
| 2576 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2577 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2578 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2579 | case TargetLowering::Expand: { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2580 | if (Node->getValueType(0) == MVT::i32) { |
| 2581 | switch (Node->getOpcode()) { |
| 2582 | default: assert(0 && "Do not know how to expand this integer BinOp!"); |
| 2583 | case ISD::UDIV: |
| 2584 | case ISD::SDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2585 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UDIV |
| 2586 | ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2587 | SDOperand Dummy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2588 | bool isSigned = Node->getOpcode() == ISD::SDIV; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2589 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2590 | }; |
| 2591 | break; |
| 2592 | } |
| 2593 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2594 | assert(MVT::isVector(Node->getValueType(0)) && |
| 2595 | "Cannot expand this binary operator!"); |
| 2596 | // Expand the operation into a bunch of nasty scalar code. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2597 | SmallVector<SDOperand, 8> Ops; |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 2598 | MVT::ValueType EltVT = MVT::getVectorElementType(Node->getValueType(0)); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2599 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 2600 | for (unsigned i = 0, e = MVT::getVectorNumElements(Node->getValueType(0)); |
| 2601 | i != e; ++i) { |
| 2602 | SDOperand Idx = DAG.getConstant(i, PtrVT); |
| 2603 | SDOperand LHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, Idx); |
| 2604 | SDOperand RHS = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, Idx); |
| 2605 | Ops.push_back(DAG.getNode(Node->getOpcode(), EltVT, LHS, RHS)); |
| 2606 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2607 | Result = DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0), |
| 2608 | &Ops[0], Ops.size()); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 2611 | case TargetLowering::Promote: { |
| 2612 | switch (Node->getOpcode()) { |
| 2613 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 2614 | case ISD::AND: |
| 2615 | case ISD::OR: |
| 2616 | case ISD::XOR: { |
| 2617 | MVT::ValueType OVT = Node->getValueType(0); |
| 2618 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2619 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 2620 | // Bit convert each of the values to the new type. |
| 2621 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 2622 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 2623 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2624 | // Bit convert the result back the original type. |
| 2625 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 2626 | break; |
| 2627 | } |
| 2628 | } |
| 2629 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2630 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2631 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2632 | |
| 2633 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 2634 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2635 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2636 | case Expand: assert(0 && "Not possible"); |
| 2637 | case Legal: |
| 2638 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2639 | break; |
| 2640 | case Promote: |
| 2641 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2642 | break; |
| 2643 | } |
| 2644 | |
| 2645 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2646 | |
| 2647 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2648 | default: assert(0 && "Operation not supported"); |
| 2649 | case TargetLowering::Custom: |
| 2650 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2651 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2652 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2653 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2654 | case TargetLowering::Expand: { |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2655 | // If this target supports fabs/fneg natively and select is cheap, |
| 2656 | // do this efficiently. |
| 2657 | if (!TLI.isSelectExpensive() && |
| 2658 | TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) == |
| 2659 | TargetLowering::Legal && |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2660 | TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) == |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 2661 | TargetLowering::Legal) { |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 2662 | // Get the sign bit of the RHS. |
| 2663 | MVT::ValueType IVT = |
| 2664 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2665 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 2666 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2667 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 2668 | // Get the absolute value of the result. |
| 2669 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 2670 | // Select between the nabs and abs value based on the sign bit of |
| 2671 | // the input. |
| 2672 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 2673 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 2674 | AbsVal), |
| 2675 | AbsVal); |
| 2676 | Result = LegalizeOp(Result); |
| 2677 | break; |
| 2678 | } |
| 2679 | |
| 2680 | // Otherwise, do bitwise ops! |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2681 | MVT::ValueType NVT = |
| 2682 | Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64; |
| 2683 | Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 2684 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result); |
| 2685 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 2688 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2689 | break; |
| 2690 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2691 | case ISD::ADDC: |
| 2692 | case ISD::SUBC: |
| 2693 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2694 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2695 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 2696 | // Since this produces two values, make sure to remember that we legalized |
| 2697 | // both of them. |
| 2698 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2699 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2700 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2701 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2702 | case ISD::ADDE: |
| 2703 | case ISD::SUBE: |
| 2704 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2705 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 2706 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 2707 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 2708 | // Since this produces two values, make sure to remember that we legalized |
| 2709 | // both of them. |
| 2710 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 2711 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 2712 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 2713 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2714 | case ISD::BUILD_PAIR: { |
| 2715 | MVT::ValueType PairTy = Node->getValueType(0); |
| 2716 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 2717 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 2718 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 2719 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2720 | case TargetLowering::Promote: |
| 2721 | case TargetLowering::Custom: |
| 2722 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2723 | case TargetLowering::Legal: |
| 2724 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 2725 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 2726 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2727 | case TargetLowering::Expand: |
| 2728 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 2729 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 2730 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 2731 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 2732 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2733 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 2734 | break; |
| 2735 | } |
| 2736 | break; |
| 2737 | } |
| 2738 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2739 | case ISD::UREM: |
| 2740 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2741 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2742 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2743 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2744 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2745 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2746 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 2747 | case TargetLowering::Custom: |
| 2748 | isCustom = true; |
| 2749 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2750 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2751 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2752 | if (isCustom) { |
| 2753 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2754 | if (Tmp1.Val) Result = Tmp1; |
| 2755 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2756 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2757 | case TargetLowering::Expand: |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2758 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2759 | bool isSigned = DivOpc == ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2760 | if (MVT::isInteger(Node->getValueType(0))) { |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2761 | if (TLI.getOperationAction(DivOpc, Node->getValueType(0)) == |
| 2762 | TargetLowering::Legal) { |
| 2763 | // X % Y -> X-X/Y*Y |
| 2764 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 2765 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2766 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 2767 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 2768 | } else { |
| 2769 | assert(Node->getValueType(0) == MVT::i32 && |
| 2770 | "Cannot expand this binary operator!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2771 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM |
| 2772 | ? RTLIB::UREM_I32 : RTLIB::SREM_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2773 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2774 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2775 | } |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2776 | } else { |
| 2777 | // Floating point mod -> fmod libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2778 | RTLIB::Libcall LC = Node->getValueType(0) == MVT::f32 |
| 2779 | ? RTLIB::REM_F32 : RTLIB::REM_F64; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 2780 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2781 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 2782 | false/*sign irrelevant*/, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 2783 | } |
| 2784 | break; |
| 2785 | } |
| 2786 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2787 | case ISD::VAARG: { |
| 2788 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2789 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2790 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 2791 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2792 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2793 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2794 | case TargetLowering::Custom: |
| 2795 | isCustom = true; |
| 2796 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2797 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2798 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2799 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2800 | Tmp1 = Result.getValue(1); |
| 2801 | |
| 2802 | if (isCustom) { |
| 2803 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 2804 | if (Tmp2.Val) { |
| 2805 | Result = LegalizeOp(Tmp2); |
| 2806 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 2807 | } |
| 2808 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | case TargetLowering::Expand: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2811 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2812 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2813 | SV->getValue(), SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2814 | // Increment the pointer, VAList, to the next vaarg |
| 2815 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2816 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2817 | TLI.getPointerTy())); |
| 2818 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2819 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 2820 | SV->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2821 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2822 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2823 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2824 | Result = LegalizeOp(Result); |
| 2825 | break; |
| 2826 | } |
| 2827 | } |
| 2828 | // Since VAARG produces two values, make sure to remember that we |
| 2829 | // legalized both of them. |
| 2830 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2831 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 2832 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | case ISD::VACOPY: |
| 2836 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2837 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 2838 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 2839 | |
| 2840 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 2841 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2842 | case TargetLowering::Custom: |
| 2843 | isCustom = true; |
| 2844 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2845 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2846 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 2847 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2848 | if (isCustom) { |
| 2849 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2850 | if (Tmp1.Val) Result = Tmp1; |
| 2851 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2852 | break; |
| 2853 | case TargetLowering::Expand: |
| 2854 | // This defaults to loading a pointer from the input and storing it to the |
| 2855 | // output, returning the chain. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2856 | SrcValueSDNode *SVD = cast<SrcValueSDNode>(Node->getOperand(3)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2857 | SrcValueSDNode *SVS = cast<SrcValueSDNode>(Node->getOperand(4)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2858 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, SVD->getValue(), |
| 2859 | SVD->getOffset()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2860 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, SVS->getValue(), |
| 2861 | SVS->getOffset()); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2862 | break; |
| 2863 | } |
| 2864 | break; |
| 2865 | |
| 2866 | case ISD::VAEND: |
| 2867 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2868 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2869 | |
| 2870 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 2871 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2872 | case TargetLowering::Custom: |
| 2873 | isCustom = true; |
| 2874 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2875 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2876 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2877 | if (isCustom) { |
| 2878 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2879 | if (Tmp1.Val) Result = Tmp1; |
| 2880 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2881 | break; |
| 2882 | case TargetLowering::Expand: |
| 2883 | Result = Tmp1; // Default to a no-op, return the chain |
| 2884 | break; |
| 2885 | } |
| 2886 | break; |
| 2887 | |
| 2888 | case ISD::VASTART: |
| 2889 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2890 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2891 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2892 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2893 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2894 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2895 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2896 | case TargetLowering::Legal: break; |
| 2897 | case TargetLowering::Custom: |
| 2898 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2899 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2900 | break; |
| 2901 | } |
| 2902 | break; |
| 2903 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2904 | case ISD::ROTL: |
| 2905 | case ISD::ROTR: |
| 2906 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2907 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2908 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 2909 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 2910 | default: |
| 2911 | assert(0 && "ROTL/ROTR legalize operation not supported"); |
| 2912 | break; |
| 2913 | case TargetLowering::Legal: |
| 2914 | break; |
| 2915 | case TargetLowering::Custom: |
| 2916 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2917 | if (Tmp1.Val) Result = Tmp1; |
| 2918 | break; |
| 2919 | case TargetLowering::Promote: |
| 2920 | assert(0 && "Do not know how to promote ROTL/ROTR"); |
| 2921 | break; |
| 2922 | case TargetLowering::Expand: |
| 2923 | assert(0 && "Do not know how to expand ROTL/ROTR"); |
| 2924 | break; |
| 2925 | } |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2926 | break; |
| 2927 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2928 | case ISD::BSWAP: |
| 2929 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2930 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2931 | case TargetLowering::Custom: |
| 2932 | assert(0 && "Cannot custom legalize this yet!"); |
| 2933 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2934 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2935 | break; |
| 2936 | case TargetLowering::Promote: { |
| 2937 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2938 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2939 | unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2940 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2941 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2942 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2943 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2944 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2945 | break; |
| 2946 | } |
| 2947 | case TargetLowering::Expand: |
| 2948 | Result = ExpandBSWAP(Tmp1); |
| 2949 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2950 | } |
| 2951 | break; |
| 2952 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2953 | case ISD::CTPOP: |
| 2954 | case ISD::CTTZ: |
| 2955 | case ISD::CTLZ: |
| 2956 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2957 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 2958 | case TargetLowering::Custom: |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2959 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2960 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 2961 | if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) == |
Scott Michel | 335f4f7 | 2007-08-02 02:22:46 +0000 | [diff] [blame] | 2962 | TargetLowering::Custom) { |
| 2963 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2964 | if (Tmp1.Val) { |
| 2965 | Result = Tmp1; |
| 2966 | } |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 2967 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2968 | break; |
| 2969 | case TargetLowering::Promote: { |
| 2970 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2971 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2972 | |
| 2973 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2974 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2975 | // Perform the larger operation, then subtract if needed. |
| 2976 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2977 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2978 | case ISD::CTPOP: |
| 2979 | Result = Tmp1; |
| 2980 | break; |
| 2981 | case ISD::CTTZ: |
| 2982 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2983 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2984 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2985 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2986 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 2987 | DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2990 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2991 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 2992 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 2993 | MVT::getSizeInBits(OVT), NVT)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2994 | break; |
| 2995 | } |
| 2996 | break; |
| 2997 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2998 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2999 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3000 | break; |
| 3001 | } |
| 3002 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3003 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3004 | // Unary operators |
| 3005 | case ISD::FABS: |
| 3006 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3007 | case ISD::FSQRT: |
| 3008 | case ISD::FSIN: |
| 3009 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3010 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3011 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3012 | case TargetLowering::Promote: |
| 3013 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3014 | isCustom = true; |
| 3015 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3016 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3017 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3018 | if (isCustom) { |
| 3019 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3020 | if (Tmp1.Val) Result = Tmp1; |
| 3021 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3022 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3023 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3024 | switch (Node->getOpcode()) { |
| 3025 | default: assert(0 && "Unreachable!"); |
| 3026 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3027 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 3028 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3029 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3030 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3031 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3032 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 3033 | MVT::ValueType VT = Node->getValueType(0); |
| 3034 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3035 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3036 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 3037 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3038 | break; |
| 3039 | } |
| 3040 | case ISD::FSQRT: |
| 3041 | case ISD::FSIN: |
| 3042 | case ISD::FCOS: { |
| 3043 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3044 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3045 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3046 | case ISD::FSQRT: |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 3047 | LC = VT == MVT::f32 ? RTLIB::SQRT_F32 : |
| 3048 | VT == MVT::f64 ? RTLIB::SQRT_F64 : RTLIB::SQRT_LD; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | case ISD::FSIN: |
| 3051 | LC = VT == MVT::f32 ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 3052 | break; |
| 3053 | case ISD::FCOS: |
| 3054 | LC = VT == MVT::f32 ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 3055 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3056 | default: assert(0 && "Unreachable!"); |
| 3057 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 3058 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3059 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3060 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3061 | break; |
| 3062 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3063 | } |
| 3064 | break; |
| 3065 | } |
| 3066 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3067 | case ISD::FPOWI: { |
| 3068 | // We always lower FPOWI into a libcall. No target support it yet. |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 3069 | RTLIB::Libcall LC = |
| 3070 | Node->getValueType(0) == MVT::f32 ? RTLIB::POWI_F32 : |
| 3071 | Node->getValueType(0) == MVT::f64 ? RTLIB::POWI_F64 : |
| 3072 | RTLIB::POWI_LD; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3073 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3074 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3075 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3076 | break; |
| 3077 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3078 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3079 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3080 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3081 | } else if (MVT::isVector(Op.getOperand(0).getValueType())) { |
| 3082 | // The input has to be a vector type, we have to either scalarize it, pack |
| 3083 | // it, or convert it based on whether the input vector type is legal. |
| 3084 | SDNode *InVal = Node->getOperand(0).Val; |
| 3085 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(0)); |
| 3086 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(0)); |
| 3087 | |
| 3088 | // Figure out if there is a simple type corresponding to this Vector |
| 3089 | // type. If so, convert to the vector type. |
| 3090 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3091 | if (TLI.isTypeLegal(TVT)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 3092 | // Turn this into a bit convert of the vector input. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3093 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3094 | LegalizeOp(Node->getOperand(0))); |
| 3095 | break; |
| 3096 | } else if (NumElems == 1) { |
| 3097 | // Turn this into a bit convert of the scalar input. |
| 3098 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3099 | ScalarizeVectorOp(Node->getOperand(0))); |
| 3100 | break; |
| 3101 | } else { |
| 3102 | // FIXME: UNIMP! Store then reload |
| 3103 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 3104 | } |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3105 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3106 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 3107 | Node->getOperand(0).getValueType())) { |
| 3108 | default: assert(0 && "Unknown operation action!"); |
| 3109 | case TargetLowering::Expand: |
| 3110 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 3111 | break; |
| 3112 | case TargetLowering::Legal: |
| 3113 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3114 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3115 | break; |
| 3116 | } |
| 3117 | } |
| 3118 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3120 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 3121 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3122 | case ISD::UINT_TO_FP: { |
| 3123 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3124 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3125 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3126 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3127 | Node->getOperand(0).getValueType())) { |
| 3128 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3129 | case TargetLowering::Custom: |
| 3130 | isCustom = true; |
| 3131 | // FALLTHROUGH |
| 3132 | case TargetLowering::Legal: |
| 3133 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3134 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3135 | if (isCustom) { |
| 3136 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3137 | if (Tmp1.Val) Result = Tmp1; |
| 3138 | } |
| 3139 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3140 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 3141 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 3142 | LegalizeOp(Node->getOperand(0)), |
| 3143 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3144 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3145 | case TargetLowering::Promote: |
| 3146 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 3147 | Node->getValueType(0), |
| 3148 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3149 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 3150 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3151 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 3152 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3153 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 3154 | Node->getValueType(0), Node->getOperand(0)); |
| 3155 | break; |
| 3156 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3157 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3158 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3159 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 3160 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3161 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3162 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 3163 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3164 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3165 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3166 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3167 | break; |
| 3168 | } |
| 3169 | break; |
| 3170 | } |
| 3171 | case ISD::TRUNCATE: |
| 3172 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3173 | case Legal: |
| 3174 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3175 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3176 | break; |
| 3177 | case Expand: |
| 3178 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3179 | |
| 3180 | // Since the result is legal, we should just be able to truncate the low |
| 3181 | // part of the source. |
| 3182 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 3183 | break; |
| 3184 | case Promote: |
| 3185 | Result = PromoteOp(Node->getOperand(0)); |
| 3186 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 3187 | break; |
| 3188 | } |
| 3189 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3190 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3191 | case ISD::FP_TO_SINT: |
| 3192 | case ISD::FP_TO_UINT: |
| 3193 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3194 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 3195 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3196 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3197 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 3198 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3199 | case TargetLowering::Custom: |
| 3200 | isCustom = true; |
| 3201 | // FALLTHROUGH |
| 3202 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3203 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3204 | if (isCustom) { |
| 3205 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3206 | if (Tmp1.Val) Result = Tmp1; |
| 3207 | } |
| 3208 | break; |
| 3209 | case TargetLowering::Promote: |
| 3210 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 3211 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 3212 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3213 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3214 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 3215 | SDOperand True, False; |
| 3216 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 3217 | MVT::ValueType NVT = Node->getValueType(0); |
Dale Johannesen | f4d4832 | 2007-09-19 17:53:26 +0000 | [diff] [blame] | 3218 | unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3219 | const uint64_t zero[] = {0, 0}; |
| 3220 | APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero)); |
| 3221 | uint64_t x = 1ULL << ShiftAmt; |
Dale Johannesen | 910993e | 2007-09-21 22:09:37 +0000 | [diff] [blame] | 3222 | (void)apf.convertFromInteger(&x, MVT::getSizeInBits(NVT), false, |
Dale Johannesen | 88216af | 2007-09-30 18:19:03 +0000 | [diff] [blame] | 3223 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3224 | Tmp2 = DAG.getConstantFP(apf, VT); |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3225 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 3226 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 3227 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 3228 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3229 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3230 | Tmp2)); |
| 3231 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 3232 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3233 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 3234 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3235 | } else { |
| 3236 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 3237 | } |
| 3238 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3239 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3240 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3241 | case Expand: { |
| 3242 | // Convert f32 / f64 to i32 / i64. |
| 3243 | MVT::ValueType VT = Op.getValueType(); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3244 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3245 | switch (Node->getOpcode()) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3246 | case ISD::FP_TO_SINT: { |
| 3247 | MVT::ValueType OVT = Node->getOperand(0).getValueType(); |
| 3248 | if (OVT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3249 | LC = (VT == MVT::i32) |
| 3250 | ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3251 | else if (OVT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3252 | LC = (VT == MVT::i32) |
| 3253 | ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3254 | else if (OVT == MVT::f80 || OVT == MVT::f128 || OVT == MVT::ppcf128) { |
| 3255 | assert(VT == MVT::i64); |
| 3256 | LC = RTLIB::FPTOSINT_LD_I64; |
| 3257 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3258 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3259 | } |
| 3260 | case ISD::FP_TO_UINT: { |
| 3261 | MVT::ValueType OVT = Node->getOperand(0).getValueType(); |
| 3262 | if (OVT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3263 | LC = (VT == MVT::i32) |
| 3264 | ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3265 | else if (OVT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3266 | LC = (VT == MVT::i32) |
| 3267 | ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3268 | else if (OVT == MVT::f80 || OVT == MVT::f128 || OVT == MVT::ppcf128) { |
| 3269 | LC = (VT == MVT::i32) |
| 3270 | ? RTLIB::FPTOUINT_LD_I32 : RTLIB::FPTOUINT_LD_I64; |
| 3271 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3272 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3273 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3274 | default: assert(0 && "Unreachable!"); |
| 3275 | } |
| 3276 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3277 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3278 | false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3279 | break; |
| 3280 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3281 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3282 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3283 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 3284 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3285 | break; |
| 3286 | } |
| 3287 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3288 | |
Dale Johannesen | ab081c7 | 2007-08-09 17:27:48 +0000 | [diff] [blame] | 3289 | case ISD::FP_EXTEND: |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3290 | case ISD::FP_ROUND: { |
| 3291 | MVT::ValueType newVT = Op.getValueType(); |
| 3292 | MVT::ValueType oldVT = Op.getOperand(0).getValueType(); |
| 3293 | if (TLI.getConvertAction(oldVT, newVT) == TargetLowering::Expand) { |
Dale Johannesen | ab081c7 | 2007-08-09 17:27:48 +0000 | [diff] [blame] | 3294 | // The only way we can lower this is to turn it into a STORE, |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3295 | // LOAD pair, targetting a temporary location (a stack slot). |
| 3296 | |
| 3297 | // NOTE: there is a choice here between constantly creating new stack |
| 3298 | // slots and always reusing the same one. We currently always create |
| 3299 | // new ones, as reuse may inhibit scheduling. |
Dale Johannesen | ab081c7 | 2007-08-09 17:27:48 +0000 | [diff] [blame] | 3300 | MVT::ValueType slotVT = |
| 3301 | (Node->getOpcode() == ISD::FP_EXTEND) ? oldVT : newVT; |
| 3302 | const Type *Ty = MVT::getTypeForValueType(slotVT); |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3303 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
| 3304 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
| 3305 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3306 | int SSFI = |
| 3307 | MF.getFrameInfo()->CreateStackObject(TySize, Align); |
| 3308 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Dale Johannesen | ab081c7 | 2007-08-09 17:27:48 +0000 | [diff] [blame] | 3309 | if (Node->getOpcode() == ISD::FP_EXTEND) { |
| 3310 | Result = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3311 | StackSlot, NULL, 0); |
| 3312 | Result = DAG.getExtLoad(ISD::EXTLOAD, newVT, |
| 3313 | Result, StackSlot, NULL, 0, oldVT); |
| 3314 | } else { |
| 3315 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3316 | StackSlot, NULL, 0, newVT); |
| 3317 | Result = DAG.getLoad(newVT, Result, StackSlot, NULL, 0, newVT); |
| 3318 | } |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3319 | break; |
| 3320 | } |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 3321 | } |
| 3322 | // FALL THROUGH |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3323 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3324 | case ISD::ZERO_EXTEND: |
| 3325 | case ISD::SIGN_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3326 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3327 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3328 | case Legal: |
| 3329 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3330 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3331 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3332 | case Promote: |
| 3333 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3334 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3335 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3336 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3337 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3338 | case ISD::ZERO_EXTEND: |
| 3339 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3340 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3341 | Result = DAG.getZeroExtendInReg(Result, |
| 3342 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3343 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3344 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3345 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3346 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3347 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3348 | Result, |
| 3349 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3350 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3351 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3352 | Result = PromoteOp(Node->getOperand(0)); |
| 3353 | if (Result.getValueType() != Op.getValueType()) |
| 3354 | // Dynamically dead while we have only 2 FP types. |
| 3355 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 3356 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3357 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 3358 | Result = PromoteOp(Node->getOperand(0)); |
| 3359 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 3360 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3361 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3362 | } |
| 3363 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3364 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3365 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3366 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3367 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3368 | |
| 3369 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 3370 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3371 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 3372 | default: assert(0 && "This action not supported for this op yet!"); |
| 3373 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3374 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3375 | break; |
| 3376 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3377 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3378 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3379 | // NOTE: we could fall back on load/store here too for targets without |
| 3380 | // SAR. However, it is doubtful that any exist. |
| 3381 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 3382 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 3383 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3384 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 3385 | Node->getOperand(0), ShiftCst); |
| 3386 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 3387 | Result, ShiftCst); |
| 3388 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 3389 | // 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] | 3390 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3391 | |
| 3392 | // NOTE: there is a choice here between constantly creating new stack |
| 3393 | // slots and always reusing the same one. We currently always create |
| 3394 | // new ones, as reuse may inhibit scheduling. |
| 3395 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 3396 | uint64_t TySize = TLI.getTargetData()->getTypeSize(Ty); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 3397 | unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3398 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3399 | int SSFI = |
Chris Lattner | 44b2c50 | 2007-04-28 06:42:38 +0000 | [diff] [blame] | 3400 | MF.getFrameInfo()->CreateStackObject(TySize, Align); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3401 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3402 | Result = DAG.getTruncStore(DAG.getEntryNode(), Node->getOperand(0), |
| 3403 | StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3404 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3405 | Result, StackSlot, NULL, 0, ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3406 | } else { |
| 3407 | assert(0 && "Unknown op"); |
| 3408 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3409 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3410 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3411 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3412 | } |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3413 | case ISD::TRAMPOLINE: { |
| 3414 | SDOperand Ops[6]; |
| 3415 | for (unsigned i = 0; i != 6; ++i) |
| 3416 | Ops[i] = LegalizeOp(Node->getOperand(i)); |
| 3417 | Result = DAG.UpdateNodeOperands(Result, Ops, 6); |
| 3418 | // The only option for this node is to custom lower it. |
| 3419 | Result = TLI.LowerOperation(Result, DAG); |
| 3420 | assert(Result.Val && "Should always custom lower!"); |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 3421 | |
| 3422 | // Since trampoline produces two values, make sure to remember that we |
| 3423 | // legalized both of them. |
| 3424 | Tmp1 = LegalizeOp(Result.getValue(1)); |
| 3425 | Result = LegalizeOp(Result); |
| 3426 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 3427 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 3428 | return Op.ResNo ? Tmp1 : Result; |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3429 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3430 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3431 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3432 | assert(Result.getValueType() == Op.getValueType() && |
| 3433 | "Bad legalization!"); |
| 3434 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3435 | // Make sure that the generated code is itself legal. |
| 3436 | if (Result != Op) |
| 3437 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3438 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3439 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3440 | // means that we always must cache transformed nodes. |
| 3441 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3442 | return Result; |
| 3443 | } |
| 3444 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3445 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 3446 | /// promote it to compute the value into a larger type. The produced value will |
| 3447 | /// have the correct bits for the low portion of the register, but no guarantee |
| 3448 | /// 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] | 3449 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 3450 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3451 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3452 | assert(getTypeAction(VT) == Promote && |
| 3453 | "Caller should expand or legalize operands that are not promotable!"); |
| 3454 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 3455 | "Cannot promote to smaller type!"); |
| 3456 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3457 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3458 | SDOperand Result; |
| 3459 | SDNode *Node = Op.Val; |
| 3460 | |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 3461 | DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3462 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3463 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3464 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3465 | case ISD::CopyFromReg: |
| 3466 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3467 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3468 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 3469 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 3470 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3471 | assert(0 && "Do not know how to promote this operator!"); |
| 3472 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 3473 | case ISD::UNDEF: |
| 3474 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 3475 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3476 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 3477 | if (VT != MVT::i1) |
| 3478 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 3479 | else |
| 3480 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3481 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 3482 | break; |
| 3483 | case ISD::ConstantFP: |
| 3484 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 3485 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 3486 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 3487 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3488 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3489 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3490 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 3491 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 3492 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 3493 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3494 | case ISD::TRUNCATE: |
| 3495 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3496 | case Legal: |
| 3497 | Result = LegalizeOp(Node->getOperand(0)); |
| 3498 | assert(Result.getValueType() >= NVT && |
| 3499 | "This truncation doesn't make sense!"); |
| 3500 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 3501 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 3502 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 3503 | case Promote: |
| 3504 | // The truncation is not required, because we don't guarantee anything |
| 3505 | // about high bits anyway. |
| 3506 | Result = PromoteOp(Node->getOperand(0)); |
| 3507 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3508 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 3509 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3510 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 3511 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3512 | } |
| 3513 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3514 | case ISD::SIGN_EXTEND: |
| 3515 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3516 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3517 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3518 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 3519 | case Legal: |
| 3520 | // 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] | 3521 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3522 | break; |
| 3523 | case Promote: |
| 3524 | // Promote the reg if it's smaller. |
| 3525 | Result = PromoteOp(Node->getOperand(0)); |
| 3526 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 3527 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 3528 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3529 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3530 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3531 | Result = DAG.getZeroExtendInReg(Result, |
| 3532 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3536 | case ISD::BIT_CONVERT: |
| 3537 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 3538 | Result = PromoteOp(Result); |
| 3539 | break; |
| 3540 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3541 | case ISD::FP_EXTEND: |
| 3542 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 3543 | case ISD::FP_ROUND: |
| 3544 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3545 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 3546 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 3547 | case Legal: |
| 3548 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3549 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3550 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3551 | break; |
| 3552 | } |
| 3553 | break; |
| 3554 | |
| 3555 | case ISD::SINT_TO_FP: |
| 3556 | case ISD::UINT_TO_FP: |
| 3557 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3558 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3559 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3560 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3561 | break; |
| 3562 | |
| 3563 | case Promote: |
| 3564 | Result = PromoteOp(Node->getOperand(0)); |
| 3565 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 3566 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3567 | Result, |
| 3568 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3569 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3570 | Result = DAG.getZeroExtendInReg(Result, |
| 3571 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3572 | // No extra round required here. |
| 3573 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3574 | break; |
| 3575 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3576 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 3577 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3578 | // Round if we cannot tolerate excess precision. |
| 3579 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3580 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3581 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3582 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3583 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3584 | break; |
| 3585 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 3586 | case ISD::SIGN_EXTEND_INREG: |
| 3587 | Result = PromoteOp(Node->getOperand(0)); |
| 3588 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 3589 | Node->getOperand(1)); |
| 3590 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3591 | case ISD::FP_TO_SINT: |
| 3592 | case ISD::FP_TO_UINT: |
| 3593 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3594 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 3595 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3596 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3597 | break; |
| 3598 | case Promote: |
| 3599 | // The input result is prerounded, so we don't have to do anything |
| 3600 | // special. |
| 3601 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3602 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3603 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3604 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 3605 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 3606 | // we can use that instead. This allows us to generate better code for |
| 3607 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 3608 | // legal, such as PowerPC. |
| 3609 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3610 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 3611 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 3612 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3613 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 3614 | } else { |
| 3615 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3616 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3617 | break; |
| 3618 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3619 | case ISD::FABS: |
| 3620 | case ISD::FNEG: |
| 3621 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3622 | assert(Tmp1.getValueType() == NVT); |
| 3623 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 3624 | // NOTE: we do not have to do any extra rounding here for |
| 3625 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 3626 | // precision, and these operations don't modify precision at all. |
| 3627 | break; |
| 3628 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3629 | case ISD::FSQRT: |
| 3630 | case ISD::FSIN: |
| 3631 | case ISD::FCOS: |
| 3632 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3633 | assert(Tmp1.getValueType() == NVT); |
| 3634 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3635 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3636 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3637 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3638 | break; |
| 3639 | |
Chris Lattner | 8b2d42c | 2007-03-03 23:43:21 +0000 | [diff] [blame] | 3640 | case ISD::FPOWI: { |
| 3641 | // Promote f32 powi to f64 powi. Note that this could insert a libcall |
| 3642 | // directly as well, which may be better. |
| 3643 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3644 | assert(Tmp1.getValueType() == NVT); |
| 3645 | Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1)); |
| 3646 | if (NoExcessFPPrecision) |
| 3647 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3648 | DAG.getValueType(VT)); |
| 3649 | break; |
| 3650 | } |
| 3651 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3652 | case ISD::AND: |
| 3653 | case ISD::OR: |
| 3654 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3655 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3656 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3657 | case ISD::MUL: |
| 3658 | // 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] | 3659 | // 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] | 3660 | // that too is okay if they are integer operations. |
| 3661 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3662 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3663 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3664 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3665 | break; |
| 3666 | case ISD::FADD: |
| 3667 | case ISD::FSUB: |
| 3668 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3669 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3670 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3671 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 3672 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3673 | |
| 3674 | // Floating point operations will give excess precision that we may not be |
| 3675 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 3676 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3677 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 3678 | // 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] | 3679 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3680 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3681 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3682 | break; |
| 3683 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3684 | case ISD::SDIV: |
| 3685 | case ISD::SREM: |
| 3686 | // These operators require that their input be sign extended. |
| 3687 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3688 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3689 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3690 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3691 | DAG.getValueType(VT)); |
| 3692 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 3693 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3694 | } |
| 3695 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3696 | |
| 3697 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 3698 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3699 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3700 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3701 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3702 | case ISD::FDIV: |
| 3703 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3704 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3705 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 3706 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3707 | case Legal: |
| 3708 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3709 | break; |
| 3710 | case Promote: |
| 3711 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3712 | break; |
| 3713 | case Expand: |
| 3714 | assert(0 && "not implemented"); |
| 3715 | } |
| 3716 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3717 | case Legal: |
| 3718 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3719 | break; |
| 3720 | case Promote: |
| 3721 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3722 | break; |
| 3723 | case Expand: |
| 3724 | assert(0 && "not implemented"); |
| 3725 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3726 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3727 | |
| 3728 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3729 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3730 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 3731 | DAG.getValueType(VT)); |
| 3732 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3733 | |
| 3734 | case ISD::UDIV: |
| 3735 | case ISD::UREM: |
| 3736 | // These operators require that their input be zero extended. |
| 3737 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3738 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 3739 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3740 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 3741 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3742 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3743 | break; |
| 3744 | |
| 3745 | case ISD::SHL: |
| 3746 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3747 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3748 | break; |
| 3749 | case ISD::SRA: |
| 3750 | // The input value must be properly sign extended. |
| 3751 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3752 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 3753 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3754 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3755 | break; |
| 3756 | case ISD::SRL: |
| 3757 | // The input value must be properly zero extended. |
| 3758 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3759 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3760 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3761 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3762 | |
| 3763 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3764 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 3765 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3766 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 3767 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 3768 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 3769 | } else { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3770 | SrcValueSDNode *SV = cast<SrcValueSDNode>(Node->getOperand(2)); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3771 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3772 | SV->getValue(), SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3773 | // Increment the pointer, VAList, to the next vaarg |
| 3774 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3775 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3776 | TLI.getPointerTy())); |
| 3777 | // Store the incremented VAList to the legalized pointer |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 3778 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, SV->getValue(), |
| 3779 | SV->getOffset()); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3780 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3781 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3782 | } |
| 3783 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3784 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 3785 | break; |
| 3786 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3787 | case ISD::LOAD: { |
| 3788 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 3789 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 3790 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 3791 | Result = DAG.getExtLoad(ExtType, NVT, |
| 3792 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 3793 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 3794 | LD->getLoadedVT(), |
| 3795 | LD->isVolatile(), |
| 3796 | LD->getAlignment()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3797 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3798 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3799 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3800 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3801 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3802 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 3803 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3804 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3805 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3806 | case ISD::SELECT_CC: |
| 3807 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 3808 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 3809 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3810 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3811 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3812 | case ISD::BSWAP: |
| 3813 | Tmp1 = Node->getOperand(0); |
| 3814 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3815 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3816 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3817 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3818 | MVT::getSizeInBits(VT), |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3819 | TLI.getShiftAmountTy())); |
| 3820 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3821 | case ISD::CTPOP: |
| 3822 | case ISD::CTTZ: |
| 3823 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3824 | // Zero extend the argument |
| 3825 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3826 | // Perform the larger operation, then subtract if needed. |
| 3827 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3828 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3829 | case ISD::CTPOP: |
| 3830 | Result = Tmp1; |
| 3831 | break; |
| 3832 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3833 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3834 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3835 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
| 3836 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3837 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3838 | DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3839 | break; |
| 3840 | case ISD::CTLZ: |
| 3841 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3842 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3843 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3844 | MVT::getSizeInBits(VT), NVT)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 3845 | break; |
| 3846 | } |
| 3847 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3848 | case ISD::EXTRACT_SUBVECTOR: |
| 3849 | Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op)); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3850 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 3851 | case ISD::EXTRACT_VECTOR_ELT: |
| 3852 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 3853 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3857 | |
| 3858 | // Make sure the result is itself legal. |
| 3859 | Result = LegalizeOp(Result); |
| 3860 | |
| 3861 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3862 | AddPromotedOperand(Op, Result); |
| 3863 | return Result; |
| 3864 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3865 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3866 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 3867 | /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic, |
| 3868 | /// based on the vector type. The return type of this matches the element type |
| 3869 | /// of the vector, which may not be legal for the target. |
| 3870 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3871 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 3872 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 3873 | // lower to a store then an indexed load. |
| 3874 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3875 | SDOperand Idx = Op.getOperand(1); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3876 | |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 3877 | MVT::ValueType TVT = Vec.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3878 | unsigned NumElems = MVT::getVectorNumElements(TVT); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3879 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3880 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) { |
| 3881 | default: assert(0 && "This action is not supported yet!"); |
| 3882 | case TargetLowering::Custom: { |
| 3883 | Vec = LegalizeOp(Vec); |
| 3884 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 3885 | SDOperand Tmp3 = TLI.LowerOperation(Op, DAG); |
| 3886 | if (Tmp3.Val) |
| 3887 | return Tmp3; |
| 3888 | break; |
| 3889 | } |
| 3890 | case TargetLowering::Legal: |
| 3891 | if (isTypeLegal(TVT)) { |
| 3892 | Vec = LegalizeOp(Vec); |
| 3893 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Christopher Lamb | 844228a | 2007-07-26 03:33:13 +0000 | [diff] [blame] | 3894 | return Op; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3895 | } |
| 3896 | break; |
| 3897 | case TargetLowering::Expand: |
| 3898 | break; |
| 3899 | } |
| 3900 | |
| 3901 | if (NumElems == 1) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3902 | // This must be an access of the only element. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3903 | Op = ScalarizeVectorOp(Vec); |
| 3904 | } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) { |
| 3905 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3906 | SDOperand Lo, Hi; |
| 3907 | SplitVectorOp(Vec, Lo, Hi); |
| 3908 | if (CIdx->getValue() < NumElems/2) { |
| 3909 | Vec = Lo; |
| 3910 | } else { |
| 3911 | Vec = Hi; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3912 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, |
| 3913 | Idx.getValueType()); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3914 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3915 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3916 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3917 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3918 | Op = ExpandEXTRACT_VECTOR_ELT(Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3919 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3920 | // Store the value to a temporary stack slot, then LOAD the scalar |
| 3921 | // element back out. |
| 3922 | SDOperand StackPtr = CreateStackTemporary(Vec.getValueType()); |
| 3923 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0); |
| 3924 | |
| 3925 | // Add the offset to the index. |
| 3926 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 3927 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 3928 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 3929 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 3930 | |
| 3931 | Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3932 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3933 | return Op; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3936 | /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3937 | /// 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] | 3938 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) { |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3939 | // We know that operand #0 is the Vec vector. For now we assume the index |
| 3940 | // is a constant and that the extracted result is a supported hardware type. |
| 3941 | SDOperand Vec = Op.getOperand(0); |
| 3942 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 3943 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3944 | unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3945 | |
| 3946 | if (NumElems == MVT::getVectorNumElements(Op.getValueType())) { |
| 3947 | // This must be an access of the desired vector length. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3948 | return Vec; |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
| 3952 | SDOperand Lo, Hi; |
| 3953 | SplitVectorOp(Vec, Lo, Hi); |
| 3954 | if (CIdx->getValue() < NumElems/2) { |
| 3955 | Vec = Lo; |
| 3956 | } else { |
| 3957 | Vec = Hi; |
| 3958 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 3959 | } |
| 3960 | |
| 3961 | // It's now an extract from the appropriate high or low part. Recurse. |
| 3962 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3963 | return ExpandEXTRACT_SUBVECTOR(Op); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 3966 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 3967 | /// with condition CC on the current target. This usually involves legalizing |
| 3968 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 3969 | /// there may be no choice but to create a new SetCC node to represent the |
| 3970 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 3971 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 3972 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 3973 | SDOperand &RHS, |
| 3974 | SDOperand &CC) { |
| 3975 | SDOperand Tmp1, Tmp2, Result; |
| 3976 | |
| 3977 | switch (getTypeAction(LHS.getValueType())) { |
| 3978 | case Legal: |
| 3979 | Tmp1 = LegalizeOp(LHS); // LHS |
| 3980 | Tmp2 = LegalizeOp(RHS); // RHS |
| 3981 | break; |
| 3982 | case Promote: |
| 3983 | Tmp1 = PromoteOp(LHS); // LHS |
| 3984 | Tmp2 = PromoteOp(RHS); // RHS |
| 3985 | |
| 3986 | // If this is an FP compare, the operands have already been extended. |
| 3987 | if (MVT::isInteger(LHS.getValueType())) { |
| 3988 | MVT::ValueType VT = LHS.getValueType(); |
| 3989 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 3990 | |
| 3991 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 3992 | // that we could insert sign extends for ALL conditions, but zero extend |
| 3993 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 3994 | // it. |
| 3995 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 3996 | default: assert(0 && "Unknown integer comparison!"); |
| 3997 | case ISD::SETEQ: |
| 3998 | case ISD::SETNE: |
| 3999 | case ISD::SETUGE: |
| 4000 | case ISD::SETUGT: |
| 4001 | case ISD::SETULE: |
| 4002 | case ISD::SETULT: |
| 4003 | // ALL of these operations will work if we either sign or zero extend |
| 4004 | // the operands (including the unsigned comparisons!). Zero extend is |
| 4005 | // usually a simpler/cheaper operation, so prefer it. |
| 4006 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 4007 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 4008 | break; |
| 4009 | case ISD::SETGE: |
| 4010 | case ISD::SETGT: |
| 4011 | case ISD::SETLT: |
| 4012 | case ISD::SETLE: |
| 4013 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4014 | DAG.getValueType(VT)); |
| 4015 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 4016 | DAG.getValueType(VT)); |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4021 | case Expand: { |
| 4022 | MVT::ValueType VT = LHS.getValueType(); |
| 4023 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4024 | // Expand into one or more soft-fp libcall(s). |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4025 | RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4026 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4027 | case ISD::SETEQ: |
| 4028 | case ISD::SETOEQ: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4029 | LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4030 | break; |
| 4031 | case ISD::SETNE: |
| 4032 | case ISD::SETUNE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4033 | LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4034 | break; |
| 4035 | case ISD::SETGE: |
| 4036 | case ISD::SETOGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4037 | LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4038 | break; |
| 4039 | case ISD::SETLT: |
| 4040 | case ISD::SETOLT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4041 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4042 | break; |
| 4043 | case ISD::SETLE: |
| 4044 | case ISD::SETOLE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4045 | LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4046 | break; |
| 4047 | case ISD::SETGT: |
| 4048 | case ISD::SETOGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4049 | LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4050 | break; |
| 4051 | case ISD::SETUO: |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4052 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
| 4053 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4054 | case ISD::SETO: |
Evan Cheng | 5efdecc | 2007-02-03 00:43:46 +0000 | [diff] [blame] | 4055 | LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4056 | break; |
| 4057 | default: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4058 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4059 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4060 | case ISD::SETONE: |
| 4061 | // SETONE = SETOLT | SETOGT |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4062 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4063 | // Fallthrough |
| 4064 | case ISD::SETUGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4065 | LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4066 | break; |
| 4067 | case ISD::SETUGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4068 | LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4069 | break; |
| 4070 | case ISD::SETULT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4071 | LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4072 | break; |
| 4073 | case ISD::SETULE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4074 | LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4075 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4076 | case ISD::SETUEQ: |
| 4077 | LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4078 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4079 | default: assert(0 && "Unsupported FP setcc!"); |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4084 | Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4085 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4086 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4087 | Tmp2 = DAG.getConstant(0, MVT::i32); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4088 | CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1)); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4089 | if (LC2 != RTLIB::UNKNOWN_LIBCALL) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4090 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4091 | LHS = ExpandLibCall(TLI.getLibcallName(LC2), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4092 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4093 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4094 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2, |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4095 | DAG.getCondCode(TLI.getCmpLibcallCC(LC2))); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4096 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4097 | Tmp2 = SDOperand(); |
| 4098 | } |
| 4099 | LHS = Tmp1; |
| 4100 | RHS = Tmp2; |
| 4101 | return; |
| 4102 | } |
| 4103 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4104 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 4105 | ExpandOp(LHS, LHSLo, LHSHi); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4106 | ExpandOp(RHS, RHSLo, RHSHi); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4107 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4108 | case ISD::SETEQ: |
| 4109 | case ISD::SETNE: |
| 4110 | if (RHSLo == RHSHi) |
| 4111 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 4112 | if (RHSCST->isAllOnesValue()) { |
| 4113 | // Comparison to -1. |
| 4114 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 4115 | Tmp2 = RHSLo; |
| 4116 | break; |
| 4117 | } |
| 4118 | |
| 4119 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 4120 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 4121 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4122 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 4123 | break; |
| 4124 | default: |
| 4125 | // If this is a comparison of the sign bit, just look at the top part. |
| 4126 | // X > -1, x < 0 |
| 4127 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 4128 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 4129 | CST->getValue() == 0) || // X < 0 |
| 4130 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 4131 | CST->isAllOnesValue())) { // X > -1 |
| 4132 | Tmp1 = LHSHi; |
| 4133 | Tmp2 = RHSHi; |
| 4134 | break; |
| 4135 | } |
| 4136 | |
| 4137 | // FIXME: This generated code sucks. |
| 4138 | ISD::CondCode LowCC; |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4139 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 4140 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4141 | default: assert(0 && "Unknown integer setcc!"); |
| 4142 | case ISD::SETLT: |
| 4143 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 4144 | case ISD::SETGT: |
| 4145 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 4146 | case ISD::SETLE: |
| 4147 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 4148 | case ISD::SETGE: |
| 4149 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 4150 | } |
| 4151 | |
| 4152 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 4153 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 4154 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 4155 | |
| 4156 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 4157 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4158 | TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL); |
| 4159 | Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC, |
| 4160 | false, DagCombineInfo); |
| 4161 | if (!Tmp1.Val) |
| 4162 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 4163 | Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 4164 | CCCode, false, DagCombineInfo); |
| 4165 | if (!Tmp2.Val) |
| 4166 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 4167 | |
| 4168 | ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val); |
| 4169 | ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val); |
| 4170 | if ((Tmp1C && Tmp1C->getValue() == 0) || |
| 4171 | (Tmp2C && Tmp2C->getValue() == 0 && |
| 4172 | (CCCode == ISD::SETLE || CCCode == ISD::SETGE || |
| 4173 | CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) || |
| 4174 | (Tmp2C && Tmp2C->getValue() == 1 && |
| 4175 | (CCCode == ISD::SETLT || CCCode == ISD::SETGT || |
| 4176 | CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) { |
| 4177 | // low part is known false, returns high part. |
| 4178 | // For LE / GE, if high part is known false, ignore the low part. |
| 4179 | // For LT / GT, if high part is known true, ignore the low part. |
| 4180 | Tmp1 = Tmp2; |
| 4181 | Tmp2 = SDOperand(); |
| 4182 | } else { |
| 4183 | Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 4184 | ISD::SETEQ, false, DagCombineInfo); |
| 4185 | if (!Result.Val) |
| 4186 | Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 4187 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 4188 | Result, Tmp1, Tmp2)); |
| 4189 | Tmp1 = Result; |
| 4190 | Tmp2 = SDOperand(); |
| 4191 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4192 | } |
| 4193 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4194 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4195 | LHS = Tmp1; |
| 4196 | RHS = Tmp2; |
| 4197 | } |
| 4198 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4199 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 4200 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 4201 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4202 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 4203 | SDOperand SrcOp) { |
| 4204 | // Create the stack frame object. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4205 | SDOperand FIPtr = CreateStackTemporary(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4206 | |
| 4207 | // Emit a store to the stack slot. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4208 | SDOperand Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4209 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4210 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4213 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 4214 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 4215 | // then load the whole vector back out. |
| 4216 | SDOperand StackPtr = CreateStackTemporary(Node->getValueType(0)); |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4217 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4218 | NULL, 0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4219 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, NULL, 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4223 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 4224 | /// support the operation, but do support the resultant vector type. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4225 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 4226 | |
| 4227 | // 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] | 4228 | // 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] | 4229 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4230 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4231 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4232 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 4233 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4234 | bool isConstant = true; |
| 4235 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 4236 | SplatValue.getOpcode() != ISD::UNDEF) |
| 4237 | isConstant = false; |
| 4238 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4239 | for (unsigned i = 1; i < NumElems; ++i) { |
| 4240 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 4241 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4242 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4243 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4244 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4245 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4246 | |
| 4247 | // If this isn't a constant element or an undef, we can't use a constant |
| 4248 | // pool load. |
| 4249 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 4250 | V.getOpcode() != ISD::UNDEF) |
| 4251 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | if (isOnlyLowElement) { |
| 4255 | // If the low element is an undef too, then this whole things is an undef. |
| 4256 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 4257 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 4258 | // Otherwise, turn this into a scalar_to_vector node. |
| 4259 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4260 | Node->getOperand(0)); |
| 4261 | } |
| 4262 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4263 | // If all elements are constants, create a load from the constant pool. |
| 4264 | if (isConstant) { |
| 4265 | MVT::ValueType VT = Node->getValueType(0); |
| 4266 | const Type *OpNTy = |
| 4267 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 4268 | std::vector<Constant*> CV; |
| 4269 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 4270 | if (ConstantFPSDNode *V = |
| 4271 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 4272 | CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4273 | } else if (ConstantSDNode *V = |
| 4274 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4275 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4276 | } else { |
| 4277 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 4278 | CV.push_back(UndefValue::get(OpNTy)); |
| 4279 | } |
| 4280 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 4281 | Constant *CP = ConstantVector::get(CV); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4282 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4283 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4286 | if (SplatValue.Val) { // Splat of one value? |
| 4287 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 4288 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4289 | MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4290 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4291 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4292 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4293 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4294 | |
| 4295 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4296 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4297 | // Get the splatted value into the low element of a vector register. |
| 4298 | SDOperand LowValVec = |
| 4299 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 4300 | |
| 4301 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 4302 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 4303 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 4304 | SplatMask); |
| 4305 | } |
| 4306 | } |
| 4307 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4308 | // If there are only two unique elements, we may be able to turn this into a |
| 4309 | // vector shuffle. |
| 4310 | if (Values.size() == 2) { |
| 4311 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 4312 | MVT::ValueType MaskVT = |
| 4313 | MVT::getIntVectorWithNumElements(NumElems); |
| 4314 | std::vector<SDOperand> MaskVec(NumElems); |
| 4315 | unsigned i = 0; |
| 4316 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4317 | E = Values.end(); I != E; ++I) { |
| 4318 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 4319 | EE = I->second.end(); II != EE; ++II) |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4320 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4321 | i += NumElems; |
| 4322 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4323 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4324 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4325 | |
| 4326 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4327 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 4328 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4329 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4330 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4331 | E = Values.end(); I != E; ++I) { |
| 4332 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4333 | I->first); |
| 4334 | Ops.push_back(Op); |
| 4335 | } |
| 4336 | Ops.push_back(ShuffleMask); |
| 4337 | |
| 4338 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4339 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), |
| 4340 | &Ops[0], Ops.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4341 | } |
| 4342 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4343 | |
| 4344 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 4345 | // aligned object on the stack, store each element into it, then load |
| 4346 | // the result as a vector. |
| 4347 | MVT::ValueType VT = Node->getValueType(0); |
| 4348 | // Create the stack frame object. |
| 4349 | SDOperand FIPtr = CreateStackTemporary(VT); |
| 4350 | |
| 4351 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4352 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4353 | unsigned TypeByteSize = |
| 4354 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4355 | // Store (in the right endianness) the elements to memory. |
| 4356 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4357 | // Ignore undef elements. |
| 4358 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 4359 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 4360 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4361 | |
| 4362 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 4363 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 4364 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4365 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4366 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | SDOperand StoreChain; |
| 4370 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4371 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4372 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4373 | else |
| 4374 | StoreChain = DAG.getEntryNode(); |
| 4375 | |
| 4376 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4377 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | /// CreateStackTemporary - Create a stack temporary, suitable for holding the |
| 4381 | /// specified value type. |
| 4382 | SDOperand SelectionDAGLegalize::CreateStackTemporary(MVT::ValueType VT) { |
| 4383 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 4384 | unsigned ByteSize = MVT::getSizeInBits(VT)/8; |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4385 | const Type *Ty = MVT::getTypeForValueType(VT); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4386 | unsigned StackAlign = (unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4387 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4388 | return DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 4389 | } |
| 4390 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4391 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 4392 | SDOperand Op, SDOperand Amt, |
| 4393 | SDOperand &Lo, SDOperand &Hi) { |
| 4394 | // Expand the subcomponents. |
| 4395 | SDOperand LHSL, LHSH; |
| 4396 | ExpandOp(Op, LHSL, LHSH); |
| 4397 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4398 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 4399 | MVT::ValueType VT = LHSL.getValueType(); |
| 4400 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4401 | Hi = Lo.getValue(1); |
| 4402 | } |
| 4403 | |
| 4404 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4405 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 4406 | /// smaller elements. If we can't find a way that is more efficient than a |
| 4407 | /// libcall on this target, return false. Otherwise, return true with the |
| 4408 | /// low-parts expanded into Lo and Hi. |
| 4409 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 4410 | SDOperand &Lo, SDOperand &Hi) { |
| 4411 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 4412 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4413 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4414 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4415 | SDOperand ShAmt = LegalizeOp(Amt); |
| 4416 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 4417 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 4418 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 4419 | |
| 4420 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 4421 | // and are disabled. |
| 4422 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 4423 | unsigned Cst = CN->getValue(); |
| 4424 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4425 | SDOperand InL, InH; |
| 4426 | ExpandOp(Op, InL, InH); |
| 4427 | switch(Opc) { |
| 4428 | case ISD::SHL: |
| 4429 | if (Cst > VTBits) { |
| 4430 | Lo = DAG.getConstant(0, NVT); |
| 4431 | Hi = DAG.getConstant(0, NVT); |
| 4432 | } else if (Cst > NVTBits) { |
| 4433 | Lo = DAG.getConstant(0, NVT); |
| 4434 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4435 | } else if (Cst == NVTBits) { |
| 4436 | Lo = DAG.getConstant(0, NVT); |
| 4437 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4438 | } else { |
| 4439 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 4440 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4441 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 4442 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4443 | } |
| 4444 | return true; |
| 4445 | case ISD::SRL: |
| 4446 | if (Cst > VTBits) { |
| 4447 | Lo = DAG.getConstant(0, NVT); |
| 4448 | Hi = DAG.getConstant(0, NVT); |
| 4449 | } else if (Cst > NVTBits) { |
| 4450 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 4451 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4452 | } else if (Cst == NVTBits) { |
| 4453 | Lo = InH; |
| 4454 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4455 | } else { |
| 4456 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4457 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4458 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4459 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4460 | } |
| 4461 | return true; |
| 4462 | case ISD::SRA: |
| 4463 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4464 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4465 | DAG.getConstant(NVTBits-1, ShTy)); |
| 4466 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4467 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4468 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4469 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4470 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4471 | } else if (Cst == NVTBits) { |
| 4472 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4473 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 4474 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4475 | } else { |
| 4476 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4477 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 4478 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 4479 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 4480 | } |
| 4481 | return true; |
| 4482 | } |
| 4483 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 4486 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
| 4487 | uint64_t Mask = NVTBits, KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 4488 | DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 4489 | |
| 4490 | // If we know that the high bit of the shift amount is one, then we can do |
| 4491 | // this as a couple of simple shifts. |
| 4492 | if (KnownOne & Mask) { |
| 4493 | // Mask out the high bit, which we know is set. |
| 4494 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
| 4495 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4496 | |
| 4497 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4498 | SDOperand InL, InH; |
| 4499 | ExpandOp(Op, InL, InH); |
| 4500 | switch(Opc) { |
| 4501 | case ISD::SHL: |
| 4502 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 4503 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 4504 | return true; |
| 4505 | case ISD::SRL: |
| 4506 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 4507 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 4508 | return true; |
| 4509 | case ISD::SRA: |
| 4510 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 4511 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 4512 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 4513 | return true; |
| 4514 | } |
| 4515 | } |
| 4516 | |
| 4517 | // If we know that the high bit of the shift amount is zero, then we can do |
| 4518 | // this as a couple of simple shifts. |
| 4519 | if (KnownZero & Mask) { |
| 4520 | // Compute 32-amt. |
| 4521 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 4522 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 4523 | Amt); |
| 4524 | |
| 4525 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4526 | SDOperand InL, InH; |
| 4527 | ExpandOp(Op, InL, InH); |
| 4528 | switch(Opc) { |
| 4529 | case ISD::SHL: |
| 4530 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 4531 | Hi = DAG.getNode(ISD::OR, NVT, |
| 4532 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 4533 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 4534 | return true; |
| 4535 | case ISD::SRL: |
| 4536 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 4537 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4538 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4539 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4540 | return true; |
| 4541 | case ISD::SRA: |
| 4542 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 4543 | Lo = DAG.getNode(ISD::OR, NVT, |
| 4544 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 4545 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 4546 | return true; |
| 4547 | } |
| 4548 | } |
| 4549 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4550 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4551 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4552 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4553 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4554 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 4555 | // does not fit into a register, return the lo part and set the hi part to the |
| 4556 | // by-reg argument. If it does fit into a single register, return the result |
| 4557 | // and leave the Hi part unset. |
| 4558 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4559 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4560 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 4561 | // The input chain to this libcall is the entry node of the function. |
| 4562 | // Legalizing the call will automatically add the previous call to the |
| 4563 | // dependence. |
| 4564 | SDOperand InChain = DAG.getEntryNode(); |
| 4565 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4566 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4567 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4568 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4569 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 4570 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4571 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 4572 | Entry.isSExt = isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4573 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4574 | } |
| 4575 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4576 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4577 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4578 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4579 | std::pair<SDOperand,SDOperand> CallInfo = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4580 | TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 4581 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4583 | // Legalize the call sequence, starting with the chain. This will advance |
| 4584 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 4585 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 4586 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4587 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4588 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4589 | default: assert(0 && "Unknown thing"); |
| 4590 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4591 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4592 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4593 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4594 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4595 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4596 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 4597 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4600 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4601 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation. |
| 4602 | /// |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4603 | SDOperand SelectionDAGLegalize:: |
| 4604 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4605 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 4606 | "This is not an expansion!"); |
| 4607 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 4608 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4609 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4610 | assert(Source.getValueType() == MVT::i64 && |
| 4611 | "This only works for 64-bit -> FP"); |
| 4612 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 4613 | // incoming integer is set. To handle this, we dynamically test to see if |
| 4614 | // it is set, and, if so, add a fudge factor. |
| 4615 | SDOperand Lo, Hi; |
| 4616 | ExpandOp(Source, Lo, Hi); |
| 4617 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 4618 | // If this is unsigned, and not supported, first perform the conversion to |
| 4619 | // signed, then adjust the result if the sign bit is set. |
| 4620 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 4621 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 4622 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4623 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 4624 | DAG.getConstant(0, Hi.getValueType()), |
| 4625 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4626 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4627 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4628 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 4629 | uint64_t FF = 0x5f800000ULL; |
| 4630 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4631 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4632 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 4633 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 4634 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4635 | SDOperand FudgeInReg; |
| 4636 | if (DestTy == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4637 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 4638 | else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32)) |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4639 | // FIXME: Avoid the extend by construction the right constantpool? |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 4640 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(), |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 4641 | CPIdx, NULL, 0, MVT::f32); |
| 4642 | else |
| 4643 | assert(0 && "Unexpected conversion"); |
| 4644 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4645 | MVT::ValueType SCVT = SignedConv.getValueType(); |
| 4646 | if (SCVT != DestTy) { |
| 4647 | // Destination type needs to be expanded as well. The FADD now we are |
| 4648 | // constructing will be expanded into a libcall. |
| 4649 | if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) { |
| 4650 | assert(SCVT == MVT::i32 && DestTy == MVT::f64); |
| 4651 | SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, |
| 4652 | SignedConv, SignedConv.getValue(1)); |
| 4653 | } |
| 4654 | SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv); |
| 4655 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 4656 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4657 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4658 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4659 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 4660 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 4661 | default: assert(0 && "This action not implemented for this operation!"); |
| 4662 | case TargetLowering::Legal: |
| 4663 | case TargetLowering::Expand: |
| 4664 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 4665 | case TargetLowering::Custom: { |
| 4666 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 4667 | Source), DAG); |
| 4668 | if (NV.Val) |
| 4669 | return LegalizeOp(NV); |
| 4670 | break; // The target decided this was legal after all |
| 4671 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 4672 | } |
| 4673 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 4674 | // Expand the source, then glue it back together for the call. We must expand |
| 4675 | // the source in case it is shared (this pass of legalize must traverse it). |
| 4676 | SDOperand SrcLo, SrcHi; |
| 4677 | ExpandOp(Source, SrcLo, SrcHi); |
| 4678 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 4679 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4680 | RTLIB::Libcall LC; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4681 | if (DestTy == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4682 | LC = RTLIB::SINTTOFP_I64_F32; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4683 | else { |
| 4684 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4685 | LC = RTLIB::SINTTOFP_I64_F64; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 4686 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 4687 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 4688 | 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] | 4689 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 4690 | SDOperand UnusedHiPart; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4691 | return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned, |
| 4692 | UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4693 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 4694 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4695 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 4696 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 4697 | /// we expand it. At this point, we know that the result and operand types are |
| 4698 | /// legal for the target. |
| 4699 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 4700 | SDOperand Op0, |
| 4701 | MVT::ValueType DestVT) { |
| 4702 | if (Op0.getValueType() == MVT::i32) { |
| 4703 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 4704 | |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4705 | // get the stack frame index of a 8 byte buffer, pessimistically aligned |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4706 | MachineFunction &MF = DAG.getMachineFunction(); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4707 | const Type *F64Type = MVT::getTypeForValueType(MVT::f64); |
| 4708 | unsigned StackAlign = |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 4709 | (unsigned)TLI.getTargetData()->getPrefTypeAlignment(F64Type); |
Chris Lattner | 58092e3 | 2007-01-20 22:35:55 +0000 | [diff] [blame] | 4710 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, StackAlign); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4711 | // get address of 8 byte buffer |
| 4712 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 4713 | // word offset constant for Hi/Lo address computation |
| 4714 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 4715 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 4716 | SDOperand Hi = StackSlot; |
| 4717 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 4718 | if (TLI.isLittleEndian()) |
| 4719 | std::swap(Hi, Lo); |
| 4720 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4721 | // if signed map to unsigned space |
| 4722 | SDOperand Op0Mapped; |
| 4723 | if (isSigned) { |
| 4724 | // constant used to invert sign bit (signed to unsigned mapping) |
| 4725 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 4726 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 4727 | } else { |
| 4728 | Op0Mapped = Op0; |
| 4729 | } |
| 4730 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4731 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4732 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4733 | // initial hi portion of constructed double |
| 4734 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 4735 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4736 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4737 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4738 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4739 | // FP constant to bias correct the final result |
| 4740 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 4741 | BitsToDouble(0x4330000080000000ULL) |
| 4742 | : BitsToDouble(0x4330000000000000ULL), |
| 4743 | MVT::f64); |
| 4744 | // subtract the bias |
| 4745 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 4746 | // final result |
| 4747 | SDOperand Result; |
| 4748 | // handle final rounding |
| 4749 | if (DestVT == MVT::f64) { |
| 4750 | // do nothing |
| 4751 | Result = Sub; |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 4752 | } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) { |
| 4753 | Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub); |
| 4754 | } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) { |
| 4755 | Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4756 | } |
| 4757 | return Result; |
| 4758 | } |
| 4759 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 4760 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 4761 | |
| 4762 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 4763 | DAG.getConstant(0, Op0.getValueType()), |
| 4764 | ISD::SETLT); |
| 4765 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 4766 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 4767 | SignSet, Four, Zero); |
| 4768 | |
| 4769 | // If the sign bit of the integer is set, the large number will be treated |
| 4770 | // as a negative number. To counteract this, the dynamic code adds an |
| 4771 | // offset depending on the data type. |
| 4772 | uint64_t FF; |
| 4773 | switch (Op0.getValueType()) { |
| 4774 | default: assert(0 && "Unsupported integer type!"); |
| 4775 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 4776 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 4777 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 4778 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 4779 | } |
| 4780 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4781 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4782 | |
| 4783 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 4784 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 4785 | SDOperand FudgeInReg; |
| 4786 | if (DestVT == MVT::f32) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4787 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4788 | else { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 4789 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4790 | DAG.getEntryNode(), CPIdx, |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4791 | NULL, 0, MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 4795 | } |
| 4796 | |
| 4797 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 4798 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 4799 | /// we promote it. At this point, we know that the result and operand types are |
| 4800 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 4801 | /// operation that takes a larger input. |
| 4802 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 4803 | MVT::ValueType DestVT, |
| 4804 | bool isSigned) { |
| 4805 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 4806 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 4807 | |
| 4808 | unsigned OpToUse = 0; |
| 4809 | |
| 4810 | // Scan for the appropriate larger type to use. |
| 4811 | while (1) { |
| 4812 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 4813 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 4814 | |
| 4815 | // If the target supports SINT_TO_FP of this type, use it. |
| 4816 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 4817 | default: break; |
| 4818 | case TargetLowering::Legal: |
| 4819 | if (!TLI.isTypeLegal(NewInTy)) |
| 4820 | break; // Can't use this datatype. |
| 4821 | // FALL THROUGH. |
| 4822 | case TargetLowering::Custom: |
| 4823 | OpToUse = ISD::SINT_TO_FP; |
| 4824 | break; |
| 4825 | } |
| 4826 | if (OpToUse) break; |
| 4827 | if (isSigned) continue; |
| 4828 | |
| 4829 | // If the target supports UINT_TO_FP of this type, use it. |
| 4830 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 4831 | default: break; |
| 4832 | case TargetLowering::Legal: |
| 4833 | if (!TLI.isTypeLegal(NewInTy)) |
| 4834 | break; // Can't use this datatype. |
| 4835 | // FALL THROUGH. |
| 4836 | case TargetLowering::Custom: |
| 4837 | OpToUse = ISD::UINT_TO_FP; |
| 4838 | break; |
| 4839 | } |
| 4840 | if (OpToUse) break; |
| 4841 | |
| 4842 | // Otherwise, try a larger type. |
| 4843 | } |
| 4844 | |
| 4845 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 4846 | // desired type then run the operation on it. |
| 4847 | return DAG.getNode(OpToUse, DestVT, |
| 4848 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 4849 | NewInTy, LegalOp)); |
| 4850 | } |
| 4851 | |
| 4852 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 4853 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 4854 | /// we promote it. At this point, we know that the result and operand types are |
| 4855 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 4856 | /// operation that returns a larger result. |
| 4857 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 4858 | MVT::ValueType DestVT, |
| 4859 | bool isSigned) { |
| 4860 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 4861 | MVT::ValueType NewOutTy = DestVT; |
| 4862 | |
| 4863 | unsigned OpToUse = 0; |
| 4864 | |
| 4865 | // Scan for the appropriate larger type to use. |
| 4866 | while (1) { |
| 4867 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 4868 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 4869 | |
| 4870 | // If the target supports FP_TO_SINT returning this type, use it. |
| 4871 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 4872 | default: break; |
| 4873 | case TargetLowering::Legal: |
| 4874 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4875 | break; // Can't use this datatype. |
| 4876 | // FALL THROUGH. |
| 4877 | case TargetLowering::Custom: |
| 4878 | OpToUse = ISD::FP_TO_SINT; |
| 4879 | break; |
| 4880 | } |
| 4881 | if (OpToUse) break; |
| 4882 | |
| 4883 | // If the target supports FP_TO_UINT of this type, use it. |
| 4884 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 4885 | default: break; |
| 4886 | case TargetLowering::Legal: |
| 4887 | if (!TLI.isTypeLegal(NewOutTy)) |
| 4888 | break; // Can't use this datatype. |
| 4889 | // FALL THROUGH. |
| 4890 | case TargetLowering::Custom: |
| 4891 | OpToUse = ISD::FP_TO_UINT; |
| 4892 | break; |
| 4893 | } |
| 4894 | if (OpToUse) break; |
| 4895 | |
| 4896 | // Otherwise, try a larger type. |
| 4897 | } |
| 4898 | |
| 4899 | // Okay, we found the operation and type to use. Truncate the result of the |
| 4900 | // extended FP_TO_*INT operation to the desired size. |
| 4901 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 4902 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 4903 | } |
| 4904 | |
| 4905 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 4906 | /// |
| 4907 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 4908 | MVT::ValueType VT = Op.getValueType(); |
| 4909 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 4910 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 4911 | switch (VT) { |
| 4912 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 4913 | case MVT::i16: |
| 4914 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4915 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4916 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 4917 | case MVT::i32: |
| 4918 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4919 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4920 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4921 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4922 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 4923 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 4924 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4925 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4926 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4927 | case MVT::i64: |
| 4928 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4929 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4930 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4931 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4932 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 4933 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 4934 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 4935 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 4936 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 4937 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 4938 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 4939 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 4940 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 4941 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 4942 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 4943 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 4944 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 4945 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 4946 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 4947 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 4948 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 4953 | /// |
| 4954 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 4955 | switch (Opc) { |
| 4956 | default: assert(0 && "Cannot expand this yet!"); |
| 4957 | case ISD::CTPOP: { |
| 4958 | static const uint64_t mask[6] = { |
| 4959 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 4960 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 4961 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 4962 | }; |
| 4963 | MVT::ValueType VT = Op.getValueType(); |
| 4964 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4965 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4966 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4967 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 4968 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 4969 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4970 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 4971 | DAG.getNode(ISD::AND, VT, |
| 4972 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 4973 | } |
| 4974 | return Op; |
| 4975 | } |
| 4976 | case ISD::CTLZ: { |
| 4977 | // for now, we do this: |
| 4978 | // x = x | (x >> 1); |
| 4979 | // x = x | (x >> 2); |
| 4980 | // ... |
| 4981 | // x = x | (x >>16); |
| 4982 | // x = x | (x >>32); // for 64-bit input |
| 4983 | // return popcount(~x); |
| 4984 | // |
| 4985 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 4986 | MVT::ValueType VT = Op.getValueType(); |
| 4987 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4988 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 4989 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 4990 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 4991 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 4992 | } |
| 4993 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 4994 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 4995 | } |
| 4996 | case ISD::CTTZ: { |
| 4997 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 4998 | // unless the target has ctlz but not ctpop, in which case we use: |
| 4999 | // { return 32 - nlz(~x & (x-1)); } |
| 5000 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 5001 | MVT::ValueType VT = Op.getValueType(); |
| 5002 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 5003 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 5004 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 5005 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 5006 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 5007 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 5008 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 5009 | return DAG.getNode(ISD::SUB, VT, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5010 | DAG.getConstant(MVT::getSizeInBits(VT), VT), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5011 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 5012 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 5013 | } |
| 5014 | } |
| 5015 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5016 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5017 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 5018 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 5019 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 5020 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 5021 | /// Lo/Hi values are returned. |
| 5022 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 5023 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 5024 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5025 | SDNode *Node = Op.Val; |
| 5026 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5027 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5028 | MVT::isVector(VT)) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5029 | "Cannot expand to FP value or to larger int value!"); |
| 5030 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5031 | // See if we already expanded it. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5032 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5033 | = ExpandedNodes.find(Op); |
| 5034 | if (I != ExpandedNodes.end()) { |
| 5035 | Lo = I->second.first; |
| 5036 | Hi = I->second.second; |
| 5037 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5040 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5041 | case ISD::CopyFromReg: |
| 5042 | assert(0 && "CopyFromReg must be legal!"); |
| 5043 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5044 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5045 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5046 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5047 | assert(0 && "Do not know how to expand this operator!"); |
| 5048 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5049 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 5050 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5051 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 5052 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5053 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5054 | case ISD::Constant: { |
| 5055 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 5056 | Lo = DAG.getConstant(Cst, NVT); |
| 5057 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 5058 | break; |
| 5059 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5060 | case ISD::ConstantFP: { |
| 5061 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 5062 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5063 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 5064 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5065 | break; |
| 5066 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5067 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5068 | // Return the operands. |
| 5069 | Lo = Node->getOperand(0); |
| 5070 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5071 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5072 | |
| 5073 | case ISD::SIGN_EXTEND_INREG: |
| 5074 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 5075 | // sext_inreg the low part if needed. |
| 5076 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 5077 | |
| 5078 | // The high part gets the sign extension from the lo-part. This handles |
| 5079 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5080 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5081 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 5082 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5083 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5084 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 5085 | case ISD::BSWAP: { |
| 5086 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 5087 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 5088 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 5089 | Lo = TempLo; |
| 5090 | break; |
| 5091 | } |
| 5092 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5093 | case ISD::CTPOP: |
| 5094 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 5095 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 5096 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 5097 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5098 | Hi = DAG.getConstant(0, NVT); |
| 5099 | break; |
| 5100 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5101 | case ISD::CTLZ: { |
| 5102 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5103 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5104 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5105 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5106 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 5107 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5108 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 5109 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 5110 | |
| 5111 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 5112 | Hi = DAG.getConstant(0, NVT); |
| 5113 | break; |
| 5114 | } |
| 5115 | |
| 5116 | case ISD::CTTZ: { |
| 5117 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5118 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5119 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5120 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5121 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 5122 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5123 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 5124 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 5125 | |
| 5126 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 5127 | Hi = DAG.getConstant(0, NVT); |
| 5128 | break; |
| 5129 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5130 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5131 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5132 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 5133 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5134 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 5135 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 5136 | |
| 5137 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5138 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5139 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 5140 | if (!TLI.isLittleEndian()) |
| 5141 | std::swap(Lo, Hi); |
| 5142 | break; |
| 5143 | } |
| 5144 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5145 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5146 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5147 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 5148 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 5149 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5150 | int SVOffset = LD->getSrcValueOffset(); |
| 5151 | unsigned Alignment = LD->getAlignment(); |
| 5152 | bool isVolatile = LD->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5153 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5154 | if (ExtType == ISD::NON_EXTLOAD) { |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5155 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5156 | isVolatile, Alignment); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5157 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5158 | // f32->i32 or f64->i64 one to one expansion. |
| 5159 | // Remember that we legalized the chain. |
| 5160 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5161 | // Recursively expand the new load. |
| 5162 | if (getTypeAction(NVT) == Expand) |
| 5163 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 5166 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5167 | // Increment the pointer to the other half. |
| 5168 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 5169 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 5170 | getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5171 | SVOffset += IncrementSize; |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5172 | if (Alignment > IncrementSize) |
| 5173 | Alignment = IncrementSize; |
| 5174 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5175 | isVolatile, Alignment); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5176 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5177 | // Build a factor node to remember that this load is independent of the |
| 5178 | // other one. |
| 5179 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5180 | Hi.getValue(1)); |
| 5181 | |
| 5182 | // Remember that we legalized the chain. |
| 5183 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 5184 | if (!TLI.isLittleEndian()) |
| 5185 | std::swap(Lo, Hi); |
| 5186 | } else { |
Evan Cheng | 2e49f09 | 2006-10-11 07:10:22 +0000 | [diff] [blame] | 5187 | MVT::ValueType EVT = LD->getLoadedVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5188 | |
| 5189 | if (VT == MVT::f64 && EVT == MVT::f32) { |
| 5190 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 5191 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5192 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5193 | // Remember that we legalized the chain. |
| 5194 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 5195 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 5196 | break; |
| 5197 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5198 | |
| 5199 | if (EVT == NVT) |
| 5200 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5201 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5202 | else |
| 5203 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5204 | SVOffset, EVT, isVolatile, |
| 5205 | Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5206 | |
| 5207 | // Remember that we legalized the chain. |
| 5208 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 5209 | |
| 5210 | if (ExtType == ISD::SEXTLOAD) { |
| 5211 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 5212 | // lo part. |
| 5213 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 5214 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5215 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 5216 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 5217 | // The high part is just a zero. |
| 5218 | Hi = DAG.getConstant(0, NVT); |
| 5219 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 5220 | // The high part is undefined. |
| 5221 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5222 | } |
| 5223 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5224 | break; |
| 5225 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5226 | case ISD::AND: |
| 5227 | case ISD::OR: |
| 5228 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 5229 | SDOperand LL, LH, RL, RH; |
| 5230 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5231 | ExpandOp(Node->getOperand(1), RL, RH); |
| 5232 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 5233 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 5234 | break; |
| 5235 | } |
| 5236 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5237 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5238 | ExpandOp(Node->getOperand(1), LL, LH); |
| 5239 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5240 | if (getTypeAction(NVT) == Expand) |
| 5241 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5242 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5243 | if (VT != MVT::f32) |
| 5244 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5245 | break; |
| 5246 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5247 | case ISD::SELECT_CC: { |
| 5248 | SDOperand TL, TH, FL, FH; |
| 5249 | ExpandOp(Node->getOperand(2), TL, TH); |
| 5250 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5251 | if (getTypeAction(NVT) == Expand) |
| 5252 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5253 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5254 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5255 | if (VT != MVT::f32) |
| 5256 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5257 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5258 | break; |
| 5259 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5260 | case ISD::ANY_EXTEND: |
| 5261 | // The low part is any extension of the input (which degenerates to a copy). |
| 5262 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 5263 | // The high part is undefined. |
| 5264 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5265 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5266 | case ISD::SIGN_EXTEND: { |
| 5267 | // The low part is just a sign extension of the input (which degenerates to |
| 5268 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5269 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5270 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5271 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 5272 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 5273 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5274 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5275 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5276 | break; |
| 5277 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5278 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5279 | // The low part is just a zero extension of the input (which degenerates to |
| 5280 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5281 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5282 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5283 | // The high part is just a zero. |
| 5284 | Hi = DAG.getConstant(0, NVT); |
| 5285 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5286 | |
Chris Lattner | 4c948eb | 2007-02-13 23:55:16 +0000 | [diff] [blame] | 5287 | case ISD::TRUNCATE: { |
| 5288 | // The input value must be larger than this value. Expand *it*. |
| 5289 | SDOperand NewLo; |
| 5290 | ExpandOp(Node->getOperand(0), NewLo, Hi); |
| 5291 | |
| 5292 | // The low part is now either the right size, or it is closer. If not the |
| 5293 | // right size, make an illegal truncate so we recursively expand it. |
| 5294 | if (NewLo.getValueType() != Node->getValueType(0)) |
| 5295 | NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo); |
| 5296 | ExpandOp(NewLo, Lo, Hi); |
| 5297 | break; |
| 5298 | } |
| 5299 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5300 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5301 | SDOperand Tmp; |
| 5302 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 5303 | // If the target wants to, allow it to lower this itself. |
| 5304 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5305 | case Expand: assert(0 && "cannot expand FP!"); |
| 5306 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 5307 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 5308 | } |
| 5309 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 5310 | } |
| 5311 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5312 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5313 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5314 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5315 | if (getTypeAction(NVT) == Expand) |
| 5316 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 5317 | break; |
| 5318 | } |
| 5319 | |
| 5320 | // If source operand will be expanded to the same type as VT, i.e. |
| 5321 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 5322 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 5323 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 5324 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5325 | break; |
| 5326 | } |
| 5327 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5328 | // Turn this into a load/store pair by default. |
| 5329 | if (Tmp.Val == 0) |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5330 | Tmp = ExpandBIT_CONVERT(VT, Node->getOperand(0)); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5331 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5332 | ExpandOp(Tmp, Lo, Hi); |
| 5333 | break; |
| 5334 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5335 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5336 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5337 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 5338 | TargetLowering::Custom && |
| 5339 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5340 | Lo = TLI.LowerOperation(Op, DAG); |
| 5341 | assert(Lo.Val && "Node must be custom expanded!"); |
| 5342 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5343 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5344 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5345 | break; |
| 5346 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5347 | // These operators cannot be expanded directly, emit them as calls to |
| 5348 | // library functions. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5349 | case ISD::FP_TO_SINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5350 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5351 | SDOperand Op; |
| 5352 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5353 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5354 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 5355 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5356 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5357 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5358 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 5359 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5360 | // Now that the custom expander is done, expand the result, which is still |
| 5361 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5362 | if (Op.Val) { |
| 5363 | ExpandOp(Op, Lo, Hi); |
| 5364 | break; |
| 5365 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5366 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5367 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5368 | RTLIB::Libcall LC; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5369 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5370 | LC = RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 5371 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5372 | LC = RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 5373 | else |
| 5374 | LC = RTLIB::FPTOSINT_LD_I64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5375 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 5376 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5377 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5378 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5379 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5380 | case ISD::FP_TO_UINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5381 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5382 | SDOperand Op; |
| 5383 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5384 | case Expand: assert(0 && "cannot expand FP!"); |
| 5385 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 5386 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 5387 | } |
| 5388 | |
| 5389 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 5390 | |
| 5391 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5392 | if (Op.Val) { |
| 5393 | ExpandOp(Op, Lo, Hi); |
| 5394 | break; |
| 5395 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5396 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5397 | |
Evan Cheng | daccea18 | 2007-10-05 01:09:32 +0000 | [diff] [blame^] | 5398 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5399 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5400 | LC = RTLIB::FPTOUINT_F32_I64; |
Dale Johannesen | 72292f0 | 2007-09-28 18:44:17 +0000 | [diff] [blame] | 5401 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5402 | LC = RTLIB::FPTOUINT_F64_I64; |
Dale Johannesen | 72292f0 | 2007-09-28 18:44:17 +0000 | [diff] [blame] | 5403 | else if (Node->getOperand(0).getValueType() == MVT::f80 || |
| 5404 | Node->getOperand(0).getValueType() == MVT::f128 || |
| 5405 | Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 5406 | LC = RTLIB::FPTOUINT_LD_I64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5407 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 5408 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5409 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5410 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5411 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5412 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5413 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5414 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5415 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5416 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5417 | Op = TLI.LowerOperation(Op, DAG); |
| 5418 | if (Op.Val) { |
| 5419 | // Now that the custom expander is done, expand the result, which is |
| 5420 | // still VT. |
| 5421 | ExpandOp(Op, Lo, Hi); |
| 5422 | break; |
| 5423 | } |
| 5424 | } |
| 5425 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5426 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 5427 | // this X << 1 as X+X. |
| 5428 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
| 5429 | if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
| 5430 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 5431 | SDOperand LoOps[2], HiOps[3]; |
| 5432 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 5433 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 5434 | LoOps[1] = LoOps[0]; |
| 5435 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 5436 | |
| 5437 | HiOps[1] = HiOps[0]; |
| 5438 | HiOps[2] = Lo.getValue(1); |
| 5439 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 5440 | break; |
| 5441 | } |
| 5442 | } |
| 5443 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5444 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5445 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5446 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5447 | |
| 5448 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5449 | TargetLowering::LegalizeAction Action = |
| 5450 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 5451 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5452 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5453 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | } |
| 5456 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5457 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5458 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node, |
| 5459 | false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5460 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5461 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5462 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5463 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5464 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5465 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5466 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5467 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5468 | Op = TLI.LowerOperation(Op, DAG); |
| 5469 | if (Op.Val) { |
| 5470 | // Now that the custom expander is done, expand the result, which is |
| 5471 | // still VT. |
| 5472 | ExpandOp(Op, Lo, Hi); |
| 5473 | break; |
| 5474 | } |
| 5475 | } |
| 5476 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5477 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5478 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5479 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5480 | |
| 5481 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5482 | TargetLowering::LegalizeAction Action = |
| 5483 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 5484 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5485 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5486 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5487 | break; |
| 5488 | } |
| 5489 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5490 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5491 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node, |
| 5492 | true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5493 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5497 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5498 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5499 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5500 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 5501 | Op = TLI.LowerOperation(Op, DAG); |
| 5502 | if (Op.Val) { |
| 5503 | // Now that the custom expander is done, expand the result, which is |
| 5504 | // still VT. |
| 5505 | ExpandOp(Op, Lo, Hi); |
| 5506 | break; |
| 5507 | } |
| 5508 | } |
| 5509 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5510 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5511 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5512 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5513 | |
| 5514 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5515 | TargetLowering::LegalizeAction Action = |
| 5516 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 5517 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 5518 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5519 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 5520 | break; |
| 5521 | } |
| 5522 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5523 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5524 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node, |
| 5525 | false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5526 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5527 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5528 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5529 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5530 | case ISD::SUB: { |
| 5531 | // If the target wants to custom expand this, let them. |
| 5532 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 5533 | TargetLowering::Custom) { |
| 5534 | Op = TLI.LowerOperation(Op, DAG); |
| 5535 | if (Op.Val) { |
| 5536 | ExpandOp(Op, Lo, Hi); |
| 5537 | break; |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | // Expand the subcomponents. |
| 5542 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5543 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5544 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5545 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5546 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5547 | LoOps[0] = LHSL; |
| 5548 | LoOps[1] = RHSL; |
| 5549 | HiOps[0] = LHSH; |
| 5550 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5551 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5552 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5553 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5554 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5555 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5556 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5557 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 5558 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 5559 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 5560 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5561 | } |
Chris Lattner | b429f73 | 2007-05-17 18:15:41 +0000 | [diff] [blame] | 5562 | |
| 5563 | case ISD::ADDC: |
| 5564 | case ISD::SUBC: { |
| 5565 | // Expand the subcomponents. |
| 5566 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5567 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5568 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 5569 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5570 | SDOperand LoOps[2] = { LHSL, RHSL }; |
| 5571 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 5572 | |
| 5573 | if (Node->getOpcode() == ISD::ADDC) { |
| 5574 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 5575 | HiOps[2] = Lo.getValue(1); |
| 5576 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 5577 | } else { |
| 5578 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
| 5579 | HiOps[2] = Lo.getValue(1); |
| 5580 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
| 5581 | } |
| 5582 | // Remember that we legalized the flag. |
| 5583 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 5584 | break; |
| 5585 | } |
| 5586 | case ISD::ADDE: |
| 5587 | case ISD::SUBE: { |
| 5588 | // Expand the subcomponents. |
| 5589 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 5590 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 5591 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 5592 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 5593 | SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) }; |
| 5594 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 5595 | |
| 5596 | Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3); |
| 5597 | HiOps[2] = Lo.getValue(1); |
| 5598 | Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3); |
| 5599 | |
| 5600 | // Remember that we legalized the flag. |
| 5601 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 5602 | break; |
| 5603 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5604 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5605 | // If the target wants to custom expand this, let them. |
| 5606 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 5607 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 5608 | if (New.Val) { |
| 5609 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 5610 | break; |
| 5611 | } |
| 5612 | } |
| 5613 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5614 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 5615 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5616 | if (HasMULHS || HasMULHU) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5617 | SDOperand LL, LH, RL, RH; |
| 5618 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5619 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5620 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
Nate Begeman | 2cbba89 | 2006-12-11 02:23:46 +0000 | [diff] [blame] | 5621 | // FIXME: Move this to the dag combiner. |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5622 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 5623 | // extended the sign bit of the low half through the upper half, and if so |
| 5624 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 5625 | // i64 x i64 multiply. |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5626 | if (HasMULHS && |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5627 | // is RH an extension of the sign bit of RL? |
| 5628 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 5629 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 5630 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 5631 | // is LH an extension of the sign bit of LL? |
| 5632 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 5633 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 5634 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5635 | // Low part: |
| 5636 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5637 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5638 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5639 | break; |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5640 | } else if (HasMULHU) { |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5641 | // Low part: |
| 5642 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 5643 | |
| 5644 | // High part: |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5645 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 5646 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 5647 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 5648 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 5649 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 5650 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 5651 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5652 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 5653 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5654 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node, |
| 5655 | false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 5656 | break; |
| 5657 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5658 | case ISD::SDIV: |
| 5659 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi); |
| 5660 | break; |
| 5661 | case ISD::UDIV: |
| 5662 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi); |
| 5663 | break; |
| 5664 | case ISD::SREM: |
| 5665 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi); |
| 5666 | break; |
| 5667 | case ISD::UREM: |
| 5668 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi); |
| 5669 | break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 5670 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5671 | case ISD::FADD: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5672 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5673 | ? RTLIB::ADD_F32 : RTLIB::ADD_F64), |
| 5674 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5675 | break; |
| 5676 | case ISD::FSUB: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5677 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5678 | ? RTLIB::SUB_F32 : RTLIB::SUB_F64), |
| 5679 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5680 | break; |
| 5681 | case ISD::FMUL: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5682 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5683 | ? RTLIB::MUL_F32 : RTLIB::MUL_F64), |
| 5684 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5685 | break; |
| 5686 | case ISD::FDIV: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5687 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) |
| 5688 | ? RTLIB::DIV_F32 : RTLIB::DIV_F64), |
| 5689 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5690 | break; |
| 5691 | case ISD::FP_EXTEND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5692 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5693 | break; |
| 5694 | case ISD::FP_ROUND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5695 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5696 | break; |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 5697 | case ISD::FPOWI: |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 5698 | Lo = ExpandLibCall(TLI.getLibcallName((VT == MVT::f32) ? RTLIB::POWI_F32 : |
| 5699 | (VT == MVT::f64) ? RTLIB::POWI_F64 : |
| 5700 | RTLIB::POWI_LD), |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 5701 | Node, false, Hi); |
| 5702 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5703 | case ISD::FSQRT: |
| 5704 | case ISD::FSIN: |
| 5705 | case ISD::FCOS: { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5706 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5707 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5708 | case ISD::FSQRT: |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 5709 | LC = (VT == MVT::f32) ? RTLIB::SQRT_F32 : |
| 5710 | (VT == MVT::f64) ? RTLIB::SQRT_F64 : RTLIB::SQRT_LD; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5711 | break; |
| 5712 | case ISD::FSIN: |
| 5713 | LC = (VT == MVT::f32) ? RTLIB::SIN_F32 : RTLIB::SIN_F64; |
| 5714 | break; |
| 5715 | case ISD::FCOS: |
| 5716 | LC = (VT == MVT::f32) ? RTLIB::COS_F32 : RTLIB::COS_F64; |
| 5717 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5718 | default: assert(0 && "Unreachable!"); |
| 5719 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5720 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5721 | break; |
| 5722 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 5723 | case ISD::FABS: { |
| 5724 | SDOperand Mask = (VT == MVT::f64) |
| 5725 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 5726 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 5727 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5728 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5729 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 5730 | if (getTypeAction(NVT) == Expand) |
| 5731 | ExpandOp(Lo, Lo, Hi); |
| 5732 | break; |
| 5733 | } |
| 5734 | case ISD::FNEG: { |
| 5735 | SDOperand Mask = (VT == MVT::f64) |
| 5736 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 5737 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 5738 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 5739 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 5740 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 5741 | if (getTypeAction(NVT) == Expand) |
| 5742 | ExpandOp(Lo, Lo, Hi); |
| 5743 | break; |
| 5744 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 5745 | case ISD::FCOPYSIGN: { |
| 5746 | Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 5747 | if (getTypeAction(NVT) == Expand) |
| 5748 | ExpandOp(Lo, Lo, Hi); |
| 5749 | break; |
| 5750 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5751 | case ISD::SINT_TO_FP: |
| 5752 | case ISD::UINT_TO_FP: { |
| 5753 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 5754 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
Evan Cheng | 64f638d | 2007-09-27 07:35:39 +0000 | [diff] [blame] | 5755 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5756 | if (Node->getOperand(0).getValueType() == MVT::i64) { |
| 5757 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5758 | LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 5759 | else if (VT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5760 | LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 5761 | else if (VT == MVT::f80 || VT == MVT::f128 || VT == MVT::ppcf128) { |
| 5762 | assert(isSigned); |
| 5763 | LC = RTLIB::SINTTOFP_I64_LD; |
| 5764 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5765 | } else { |
| 5766 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5767 | LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5768 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5769 | LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | // Promote the operand if needed. |
| 5773 | if (getTypeAction(SrcVT) == Promote) { |
| 5774 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 5775 | Tmp = isSigned |
| 5776 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 5777 | DAG.getValueType(SrcVT)) |
| 5778 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 5779 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 5780 | } |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5781 | |
| 5782 | const char *LibCall = TLI.getLibcallName(LC); |
| 5783 | if (LibCall) |
| 5784 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi); |
| 5785 | else { |
| 5786 | Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT, |
| 5787 | Node->getOperand(0)); |
| 5788 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 5789 | ExpandOp(Lo, Lo, Hi); |
| 5790 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 5791 | break; |
| 5792 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 5793 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5794 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5795 | // Make sure the resultant values have been legalized themselves, unless this |
| 5796 | // is a type that requires multi-step expansion. |
| 5797 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 5798 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5799 | if (Hi.Val) |
| 5800 | // Don't legalize the high part if it is expanded to a single node. |
| 5801 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 5802 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 5803 | |
| 5804 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5805 | 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] | 5806 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5807 | } |
| 5808 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5809 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 5810 | /// two smaller values, still of vector type. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5811 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 5812 | SDOperand &Hi) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5813 | assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5814 | SDNode *Node = Op.Val; |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 5815 | unsigned NumElements = MVT::getVectorNumElements(Op.getValueType()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5816 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 5817 | unsigned NewNumElts = NumElements/2; |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 5818 | MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5819 | MVT::ValueType NewVT = MVT::getVectorType(NewEltVT, NewNumElts); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5820 | |
| 5821 | // See if we already split it. |
| 5822 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 5823 | = SplitNodes.find(Op); |
| 5824 | if (I != SplitNodes.end()) { |
| 5825 | Lo = I->second.first; |
| 5826 | Hi = I->second.second; |
| 5827 | return; |
| 5828 | } |
| 5829 | |
| 5830 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5831 | default: |
| 5832 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5833 | Node->dump(&DAG); |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5834 | #endif |
| 5835 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5836 | case ISD::BUILD_PAIR: |
| 5837 | Lo = Node->getOperand(0); |
| 5838 | Hi = Node->getOperand(1); |
| 5839 | break; |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 5840 | case ISD::INSERT_VECTOR_ELT: { |
| 5841 | SplitVectorOp(Node->getOperand(0), Lo, Hi); |
| 5842 | unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue(); |
| 5843 | SDOperand ScalarOp = Node->getOperand(1); |
| 5844 | if (Index < NewNumElts) |
| 5845 | Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Lo, ScalarOp, |
| 5846 | DAG.getConstant(Index, TLI.getPointerTy())); |
| 5847 | else |
| 5848 | Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT, Hi, ScalarOp, |
| 5849 | DAG.getConstant(Index - NewNumElts, TLI.getPointerTy())); |
| 5850 | break; |
| 5851 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5852 | case ISD::BUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5853 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5854 | Node->op_begin()+NewNumElts); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5855 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5856 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5857 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5858 | Node->op_end()); |
| 5859 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5860 | break; |
| 5861 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5862 | case ISD::CONCAT_VECTORS: { |
| 5863 | unsigned NewNumSubvectors = Node->getNumOperands() / 2; |
| 5864 | if (NewNumSubvectors == 1) { |
| 5865 | Lo = Node->getOperand(0); |
| 5866 | Hi = Node->getOperand(1); |
| 5867 | } else { |
| 5868 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 5869 | Node->op_begin()+NewNumSubvectors); |
| 5870 | Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &LoOps[0], LoOps.size()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5871 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5872 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors, |
| 5873 | Node->op_end()); |
| 5874 | Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT, &HiOps[0], HiOps.size()); |
| 5875 | } |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 5876 | break; |
| 5877 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5878 | case ISD::ADD: |
| 5879 | case ISD::SUB: |
| 5880 | case ISD::MUL: |
| 5881 | case ISD::FADD: |
| 5882 | case ISD::FSUB: |
| 5883 | case ISD::FMUL: |
| 5884 | case ISD::SDIV: |
| 5885 | case ISD::UDIV: |
| 5886 | case ISD::FDIV: |
| 5887 | case ISD::AND: |
| 5888 | case ISD::OR: |
| 5889 | case ISD::XOR: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5890 | SDOperand LL, LH, RL, RH; |
| 5891 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 5892 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 5893 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5894 | Lo = DAG.getNode(Node->getOpcode(), NewVT, LL, RL); |
| 5895 | Hi = DAG.getNode(Node->getOpcode(), NewVT, LH, RH); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5896 | break; |
| 5897 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5898 | case ISD::LOAD: { |
| 5899 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5900 | SDOperand Ch = LD->getChain(); |
| 5901 | SDOperand Ptr = LD->getBasePtr(); |
| 5902 | const Value *SV = LD->getSrcValue(); |
| 5903 | int SVOffset = LD->getSrcValueOffset(); |
| 5904 | unsigned Alignment = LD->getAlignment(); |
| 5905 | bool isVolatile = LD->isVolatile(); |
| 5906 | |
| 5907 | Lo = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
| 5908 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(NewEltVT)/8; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5909 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 5910 | getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5911 | SVOffset += IncrementSize; |
| 5912 | if (Alignment > IncrementSize) |
| 5913 | Alignment = IncrementSize; |
| 5914 | Hi = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5915 | |
| 5916 | // Build a factor node to remember that this load is independent of the |
| 5917 | // other one. |
| 5918 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5919 | Hi.getValue(1)); |
| 5920 | |
| 5921 | // Remember that we legalized the chain. |
| 5922 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5923 | break; |
| 5924 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5925 | case ISD::BIT_CONVERT: { |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5926 | // We know the result is a vector. The input may be either a vector or a |
| 5927 | // scalar value. |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5928 | SDOperand InOp = Node->getOperand(0); |
| 5929 | if (!MVT::isVector(InOp.getValueType()) || |
| 5930 | MVT::getVectorNumElements(InOp.getValueType()) == 1) { |
| 5931 | // The input is a scalar or single-element vector. |
| 5932 | // Lower to a store/load so that it can be split. |
| 5933 | // FIXME: this could be improved probably. |
| 5934 | SDOperand Ptr = CreateStackTemporary(InOp.getValueType()); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5935 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5936 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5937 | InOp, Ptr, NULL, 0); |
| 5938 | InOp = DAG.getLoad(Op.getValueType(), St, Ptr, NULL, 0); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5939 | } |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5940 | // Split the vector and convert each of the pieces now. |
| 5941 | SplitVectorOp(InOp, Lo, Hi); |
| 5942 | Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT, Lo); |
| 5943 | Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT, Hi); |
| 5944 | break; |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 5945 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5949 | bool isNew = |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5950 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 5951 | assert(isNew && "Value already split?!?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 5955 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 5956 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 5957 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5958 | SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) { |
| 5959 | assert(MVT::isVector(Op.getValueType()) && |
| 5960 | "Bad ScalarizeVectorOp invocation!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5961 | SDNode *Node = Op.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5962 | MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType()); |
| 5963 | assert(MVT::getVectorNumElements(Op.getValueType()) == 1); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5964 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5965 | // See if we already scalarized it. |
| 5966 | std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op); |
| 5967 | if (I != ScalarizedNodes.end()) return I->second; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5968 | |
| 5969 | SDOperand Result; |
| 5970 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 5971 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5972 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5973 | Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5974 | #endif |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5975 | assert(0 && "Unknown vector operation in ScalarizeVectorOp!"); |
| 5976 | case ISD::ADD: |
| 5977 | case ISD::FADD: |
| 5978 | case ISD::SUB: |
| 5979 | case ISD::FSUB: |
| 5980 | case ISD::MUL: |
| 5981 | case ISD::FMUL: |
| 5982 | case ISD::SDIV: |
| 5983 | case ISD::UDIV: |
| 5984 | case ISD::FDIV: |
| 5985 | case ISD::SREM: |
| 5986 | case ISD::UREM: |
| 5987 | case ISD::FREM: |
| 5988 | case ISD::AND: |
| 5989 | case ISD::OR: |
| 5990 | case ISD::XOR: |
| 5991 | Result = DAG.getNode(Node->getOpcode(), |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5992 | NewVT, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5993 | ScalarizeVectorOp(Node->getOperand(0)), |
| 5994 | ScalarizeVectorOp(Node->getOperand(1))); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 5995 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5996 | case ISD::FNEG: |
| 5997 | case ISD::FABS: |
| 5998 | case ISD::FSQRT: |
| 5999 | case ISD::FSIN: |
| 6000 | case ISD::FCOS: |
| 6001 | Result = DAG.getNode(Node->getOpcode(), |
| 6002 | NewVT, |
| 6003 | ScalarizeVectorOp(Node->getOperand(0))); |
| 6004 | break; |
| 6005 | case ISD::LOAD: { |
| 6006 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 6007 | SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 6008 | SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6009 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6010 | const Value *SV = LD->getSrcValue(); |
| 6011 | int SVOffset = LD->getSrcValueOffset(); |
| 6012 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, |
| 6013 | LD->isVolatile(), LD->getAlignment()); |
| 6014 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6015 | // Remember that we legalized the chain. |
| 6016 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 6017 | break; |
| 6018 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6019 | case ISD::BUILD_VECTOR: |
| 6020 | Result = Node->getOperand(0); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6021 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6022 | case ISD::INSERT_VECTOR_ELT: |
| 6023 | // Returning the inserted scalar element. |
| 6024 | Result = Node->getOperand(1); |
| 6025 | break; |
| 6026 | case ISD::CONCAT_VECTORS: |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6027 | assert(Node->getOperand(0).getValueType() == NewVT && |
| 6028 | "Concat of non-legal vectors not yet supported!"); |
| 6029 | Result = Node->getOperand(0); |
| 6030 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6031 | case ISD::VECTOR_SHUFFLE: { |
| 6032 | // Figure out if the scalar is the LHS or RHS and return it. |
| 6033 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 6034 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 6035 | Result = ScalarizeVectorOp(Node->getOperand(1)); |
| 6036 | else |
| 6037 | Result = ScalarizeVectorOp(Node->getOperand(0)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 6038 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6039 | } |
| 6040 | case ISD::EXTRACT_SUBVECTOR: |
| 6041 | Result = Node->getOperand(0); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6042 | assert(Result.getValueType() == NewVT); |
| 6043 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6044 | case ISD::BIT_CONVERT: |
| 6045 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 6046 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6047 | case ISD::SELECT: |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 6048 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6049 | ScalarizeVectorOp(Op.getOperand(1)), |
| 6050 | ScalarizeVectorOp(Op.getOperand(2))); |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 6051 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | if (TLI.isTypeLegal(NewVT)) |
| 6055 | Result = LegalizeOp(Result); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6056 | bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second; |
| 6057 | assert(isNew && "Value already scalarized?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6058 | return Result; |
| 6059 | } |
| 6060 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6061 | |
| 6062 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 6063 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 6064 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 6065 | if (ViewLegalizeDAGs) viewGraph(); |
| 6066 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6067 | /// run - This is the main entry point to this class. |
| 6068 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 6069 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |