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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 19 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetFrameInfo.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 25 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 27 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compiler.h" |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 34 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 37 | #ifndef NDEBUG |
| 38 | static cl::opt<bool> |
| 39 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 40 | cl::desc("Pop up a window to show dags before legalize")); |
| 41 | #else |
| 42 | static const bool ViewLegalizeDAGs = 0; |
| 43 | #endif |
| 44 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 47 | /// hacks on it until the target machine can handle it. This involves |
| 48 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 49 | /// large sizes or splitting up large values into small values) as well as |
| 50 | /// eliminating operations the machine cannot handle. |
| 51 | /// |
| 52 | /// This code also does a small amount of optimization and recognition of idioms |
| 53 | /// as part of its processing. For example, if a target does not support a |
| 54 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 55 | /// will attempt merge setcc and brc instructions into brcc's. |
| 56 | /// |
| 57 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 58 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 59 | TargetLowering &TLI; |
| 60 | SelectionDAG &DAG; |
| 61 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 62 | // Libcall insertion helpers. |
| 63 | |
| 64 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 65 | /// legalized. We use this to ensure that calls are properly serialized |
| 66 | /// against each other, including inserted libcalls. |
| 67 | SDOperand LastCALLSEQ_END; |
| 68 | |
| 69 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 70 | /// helpful assertions that a libcall isn't created while another call is |
| 71 | /// being legalized (which could lead to non-serialized call sequences). |
| 72 | bool IsLegalizingCall; |
| 73 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 74 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 75 | Legal, // The target natively supports this operation. |
| 76 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 77 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 78 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 80 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 81 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 82 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 83 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 85 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 86 | /// than one use, this map indicates what regularized operand to use. This |
| 87 | /// allows us to avoid legalizing the same thing more than once. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 88 | DenseMap<SDOperand, SDOperand> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 90 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 91 | /// than one use, this map indicates what promoted value to use. This allows |
| 92 | /// us to avoid promoting the same thing more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 93 | DenseMap<SDOperand, SDOperand> PromotedNodes; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 94 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 95 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 96 | /// which which operands are the expanded version of the input. This allows |
| 97 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 98 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 99 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 100 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 101 | /// which which operands are the split version of the input. This allows us |
| 102 | /// to avoid splitting the same node more than once. |
| 103 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 104 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 105 | /// ScalarizedNodes - For nodes that need to be converted from vector types to |
| 106 | /// scalar types, this contains the mapping of ones we have already |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 107 | /// processed to the result. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 108 | std::map<SDOperand, SDOperand> ScalarizedNodes; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 110 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 111 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 112 | // If someone requests legalization of the new node, return itself. |
| 113 | if (From != To) |
| 114 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 115 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 116 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 117 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 118 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 119 | // If someone requests legalization of the new node, return itself. |
| 120 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 123 | public: |
| 124 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 125 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 127 | /// getTypeAction - Return how we should legalize values of this type, either |
| 128 | /// it is already legal or we need to expand it into multiple registers of |
| 129 | /// smaller integer type, or we need to promote it to a larger type. |
| 130 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 131 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /// isTypeLegal - Return true if this type is legal on this target. |
| 135 | /// |
| 136 | bool isTypeLegal(MVT::ValueType VT) const { |
| 137 | return getTypeAction(VT) == Legal; |
| 138 | } |
| 139 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 140 | void LegalizeDAG(); |
| 141 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 142 | private: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 143 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 144 | /// appropriate for its type. |
| 145 | void HandleOp(SDOperand Op); |
| 146 | |
| 147 | /// LegalizeOp - We know that the specified value has a legal type. |
| 148 | /// Recursively ensure that the operands have legal types, then return the |
| 149 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 150 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 151 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 152 | /// UnrollVectorOp - We know that the given vector has a legal type, however |
| 153 | /// the operation it performs is not legal and is an operation that we have |
| 154 | /// no way of lowering. "Unroll" the vector, splitting out the scalars and |
| 155 | /// operating on each element individually. |
| 156 | SDOperand UnrollVectorOp(SDOperand O); |
| 157 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 158 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 159 | /// promote it to compute the value into a larger type. The produced value |
| 160 | /// will have the correct bits for the low portion of the register, but no |
| 161 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 162 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 163 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 164 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 165 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 166 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 167 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 168 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 169 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 170 | /// types. |
| 171 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 172 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 173 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 174 | /// two smaller values. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 175 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 176 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 177 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 178 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 179 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 180 | SDOperand ScalarizeVectorOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 181 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 182 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 183 | /// specified mask and type. Targets can specify exactly which masks they |
| 184 | /// support and the code generator is tasked with not creating illegal masks. |
| 185 | /// |
| 186 | /// Note that this will also return true for shuffles that are promoted to a |
| 187 | /// different type. |
| 188 | /// |
| 189 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 190 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 191 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 192 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 193 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 194 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 195 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 196 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 197 | |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 198 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 199 | SDOperand &Hi); |
| 200 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 201 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 203 | SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT, |
| 204 | MVT::ValueType DestVT); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 205 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 206 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 207 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 208 | SDOperand LegalOp, |
| 209 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 210 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 211 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 212 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 213 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 215 | SDOperand ExpandBSWAP(SDOperand Op); |
| 216 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 217 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 218 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 219 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 220 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 221 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 222 | SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 223 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 224 | }; |
| 225 | } |
| 226 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 227 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 228 | /// specified mask and type. Targets can specify exactly which masks they |
| 229 | /// support and the code generator is tasked with not creating illegal masks. |
| 230 | /// |
| 231 | /// Note that this will also return true for shuffles that are promoted to a |
| 232 | /// different type. |
| 233 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 234 | SDOperand Mask) const { |
| 235 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 236 | default: return 0; |
| 237 | case TargetLowering::Legal: |
| 238 | case TargetLowering::Custom: |
| 239 | break; |
| 240 | case TargetLowering::Promote: { |
| 241 | // If this is promoted to a different type, convert the shuffle mask and |
| 242 | // ask if it is legal in the promoted type! |
| 243 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 244 | |
| 245 | // If we changed # elements, change the shuffle mask. |
| 246 | unsigned NumEltsGrowth = |
| 247 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 248 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 249 | if (NumEltsGrowth > 1) { |
| 250 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 251 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 252 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 253 | SDOperand InOp = Mask.getOperand(i); |
| 254 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 255 | if (InOp.getOpcode() == ISD::UNDEF) |
| 256 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 257 | else { |
| 258 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 259 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 260 | } |
| 261 | } |
| 262 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 263 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 264 | } |
| 265 | VT = NVT; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 270 | } |
| 271 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 272 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 273 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 274 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 275 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 276 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 279 | /// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order |
| 280 | /// contains all of a nodes operands before it contains the node. |
| 281 | static void ComputeTopDownOrdering(SelectionDAG &DAG, |
| 282 | SmallVector<SDNode*, 64> &Order) { |
| 283 | |
| 284 | DenseMap<SDNode*, unsigned> Visited; |
| 285 | std::vector<SDNode*> Worklist; |
| 286 | Worklist.reserve(128); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 287 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 288 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 289 | // entry node) that have no operands. |
| 290 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 291 | E = DAG.allnodes_end(); I != E; ++I) { |
| 292 | if (I->getNumOperands() == 0) { |
| 293 | Visited[I] = 0 - 1U; |
| 294 | Worklist.push_back(I); |
| 295 | } |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 298 | while (!Worklist.empty()) { |
| 299 | SDNode *N = Worklist.back(); |
| 300 | Worklist.pop_back(); |
| 301 | |
| 302 | if (++Visited[N] != N->getNumOperands()) |
| 303 | continue; // Haven't visited all operands yet |
| 304 | |
| 305 | Order.push_back(N); |
| 306 | |
| 307 | // Now that we have N in, add anything that uses it if all of their operands |
| 308 | // are now done. |
| 309 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 310 | UI != E; ++UI) |
| 311 | Worklist.push_back(*UI); |
| 312 | } |
| 313 | |
| 314 | assert(Order.size() == Visited.size() && |
| 315 | Order.size() == |
| 316 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
| 317 | "Error: DAG is cyclic!"); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 321 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 322 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 323 | IsLegalizingCall = false; |
| 324 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 325 | // The legalize process is inherently a bottom-up recursive process (users |
| 326 | // legalize their uses before themselves). Given infinite stack space, we |
| 327 | // could just start legalizing on the root and traverse the whole graph. In |
| 328 | // 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] | 329 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 330 | // node is only legalized after all of its operands are legalized. |
Chris Lattner | 0ed4417 | 2007-02-04 01:20:02 +0000 | [diff] [blame] | 331 | SmallVector<SDNode*, 64> Order; |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 332 | ComputeTopDownOrdering(DAG, Order); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 333 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 334 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 335 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 336 | |
| 337 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 338 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 339 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 340 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 341 | |
| 342 | ExpandedNodes.clear(); |
| 343 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 344 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 345 | SplitNodes.clear(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 346 | ScalarizedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 347 | |
| 348 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 349 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 352 | |
| 353 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 354 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 355 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 356 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 357 | return Node; |
| 358 | if (Node->use_empty()) |
| 359 | return 0; // No CallSeqEnd |
| 360 | |
| 361 | // The chain is usually at the end. |
| 362 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 363 | if (TheChain.getValueType() != MVT::Other) { |
| 364 | // Sometimes it's at the beginning. |
| 365 | TheChain = SDOperand(Node, 0); |
| 366 | if (TheChain.getValueType() != MVT::Other) { |
| 367 | // Otherwise, hunt for it. |
| 368 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 369 | if (Node->getValueType(i) == MVT::Other) { |
| 370 | TheChain = SDOperand(Node, i); |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | // Otherwise, we walked into a node without a chain. |
| 375 | if (TheChain.getValueType() != MVT::Other) |
| 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 381 | E = Node->use_end(); UI != E; ++UI) { |
| 382 | |
| 383 | // Make sure to only follow users of our token chain. |
| 384 | SDNode *User = *UI; |
| 385 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 386 | if (User->getOperand(i) == TheChain) |
| 387 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 388 | return Result; |
| 389 | } |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 394 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 395 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 396 | assert(Node && "Didn't find callseq_start for a call??"); |
| 397 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 398 | |
| 399 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 400 | "Node doesn't have a token chain argument!"); |
| 401 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 402 | } |
| 403 | |
| 404 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 405 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 406 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 407 | /// |
| 408 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 409 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 410 | /// |
| 411 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 412 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 413 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 414 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 415 | // If we've already processed this node and it does lead to Dest, there is no |
| 416 | // need to reprocess it. |
| 417 | if (NodesLeadingTo.count(N)) return true; |
| 418 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 419 | // If the first result of this node has been already legalized, then it cannot |
| 420 | // reach N. |
| 421 | switch (getTypeAction(N->getValueType(0))) { |
| 422 | case Legal: |
| 423 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 424 | break; |
| 425 | case Promote: |
| 426 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 427 | break; |
| 428 | case Expand: |
| 429 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | // Okay, this node has not already been legalized. Check and legalize all |
| 434 | // operands. If none lead to Dest, then we can legalize this node. |
| 435 | bool OperandsLeadToDest = false; |
| 436 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 437 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 438 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 439 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 440 | if (OperandsLeadToDest) { |
| 441 | NodesLeadingTo.insert(N); |
| 442 | return true; |
| 443 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 444 | |
| 445 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 446 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 447 | return false; |
| 448 | } |
| 449 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 450 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 451 | /// appropriate for its type. |
| 452 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 453 | MVT::ValueType VT = Op.getValueType(); |
| 454 | switch (getTypeAction(VT)) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 455 | default: assert(0 && "Bad type action!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 456 | case Legal: (void)LegalizeOp(Op); break; |
| 457 | case Promote: (void)PromoteOp(Op); break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 458 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 459 | if (!MVT::isVector(VT)) { |
| 460 | // If this is an illegal scalar, expand it into its two component |
| 461 | // pieces. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 462 | SDOperand X, Y; |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 463 | if (Op.getOpcode() == ISD::TargetConstant) |
| 464 | break; // Allow illegal target nodes. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 465 | ExpandOp(Op, X, Y); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 466 | } else if (MVT::getVectorNumElements(VT) == 1) { |
| 467 | // If this is an illegal single element vector, convert it to a |
| 468 | // scalar operation. |
| 469 | (void)ScalarizeVectorOp(Op); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 470 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 471 | // Otherwise, this is an illegal multiple element vector. |
| 472 | // Split it in half and legalize both parts. |
| 473 | SDOperand X, Y; |
| 474 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 475 | } |
| 476 | break; |
| 477 | } |
| 478 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 479 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 480 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 481 | /// a load from the constant pool. |
| 482 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 483 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 484 | bool Extend = false; |
| 485 | |
| 486 | // If a FP immediate is precise when represented as a float and if the |
| 487 | // target can do an extending load from float to double, we put it into |
| 488 | // the constant pool as a float, even if it's is statically typed as a |
| 489 | // double. |
| 490 | MVT::ValueType VT = CFP->getValueType(0); |
| 491 | bool isDouble = VT == MVT::f64; |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 492 | ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT), |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 493 | CFP->getValueAPF()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 494 | if (!UseCP) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 495 | if (VT!=MVT::f64 && VT!=MVT::f32) |
| 496 | assert(0 && "Invalid type expansion"); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 497 | return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(), |
| 498 | isDouble ? MVT::i64 : MVT::i32); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 501 | if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) && |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 502 | // 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] | 503 | // Do not try to be clever about long doubles (so far) |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 504 | TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) { |
| 505 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy)); |
| 506 | VT = MVT::f32; |
| 507 | Extend = true; |
| 508 | } |
| 509 | |
| 510 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
| 511 | if (Extend) { |
| 512 | return DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 513 | CPIdx, &PseudoSourceValue::getConstantPool(), |
| 514 | 0, MVT::f32); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 515 | } else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 516 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 517 | &PseudoSourceValue::getConstantPool(), 0); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 521 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 522 | /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise |
| 523 | /// operations. |
| 524 | static |
| 525 | SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT, |
| 526 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 527 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 528 | MVT::ValueType SrcVT = Node->getOperand(1).getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 529 | assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) && |
| 530 | "fcopysign expansion only supported for f32 and f64"); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 531 | MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 532 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 533 | // First get the sign bit of second operand. |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 534 | SDOperand Mask1 = (SrcVT == MVT::f64) |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 535 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT) |
| 536 | : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 537 | Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 538 | SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 539 | SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 540 | // Shift right or sign-extend it if the two operands have different types. |
| 541 | int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT); |
| 542 | if (SizeDiff > 0) { |
| 543 | SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit, |
| 544 | DAG.getConstant(SizeDiff, TLI.getShiftAmountTy())); |
| 545 | SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit); |
| 546 | } else if (SizeDiff < 0) |
| 547 | SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 548 | |
| 549 | // Clear the sign bit of first operand. |
| 550 | SDOperand Mask2 = (VT == MVT::f64) |
| 551 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 552 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 553 | Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 554 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 555 | Result = DAG.getNode(ISD::AND, NVT, Result, Mask2); |
| 556 | |
| 557 | // Or the value with the sign bit. |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 558 | Result = DAG.getNode(ISD::OR, NVT, Result, SignBit); |
| 559 | return Result; |
| 560 | } |
| 561 | |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 562 | /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. |
| 563 | static |
| 564 | SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, |
| 565 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 566 | SDOperand Chain = ST->getChain(); |
| 567 | SDOperand Ptr = ST->getBasePtr(); |
| 568 | SDOperand Val = ST->getValue(); |
| 569 | MVT::ValueType VT = Val.getValueType(); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 570 | int Alignment = ST->getAlignment(); |
| 571 | int SVOffset = ST->getSrcValueOffset(); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 572 | if (MVT::isFloatingPoint(ST->getMemoryVT())) { |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 573 | // Expand to a bitconvert of the value to the integer type of the |
| 574 | // same size, then a (misaligned) int store. |
| 575 | MVT::ValueType intVT; |
| 576 | if (VT==MVT::f64) |
| 577 | intVT = MVT::i64; |
| 578 | else if (VT==MVT::f32) |
| 579 | intVT = MVT::i32; |
| 580 | else |
| 581 | assert(0 && "Unaligned load of unsupported floating point type"); |
| 582 | |
| 583 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val); |
| 584 | return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(), |
| 585 | SVOffset, ST->isVolatile(), Alignment); |
| 586 | } |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 587 | assert(MVT::isInteger(ST->getMemoryVT()) && |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 588 | "Unaligned store of unknown type."); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 589 | // Get the half-size VT |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 590 | MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 591 | int NumBits = MVT::getSizeInBits(NewStoredVT); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 592 | int IncrementSize = NumBits / 8; |
| 593 | |
| 594 | // Divide the stored value in two parts. |
| 595 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 596 | SDOperand Lo = Val; |
| 597 | SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount); |
| 598 | |
| 599 | // Store the two parts |
| 600 | SDOperand Store1, Store2; |
| 601 | Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr, |
| 602 | ST->getSrcValue(), SVOffset, NewStoredVT, |
| 603 | ST->isVolatile(), Alignment); |
| 604 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 605 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 606 | Alignment = MinAlign(Alignment, IncrementSize); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 607 | Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr, |
| 608 | ST->getSrcValue(), SVOffset + IncrementSize, |
| 609 | NewStoredVT, ST->isVolatile(), Alignment); |
| 610 | |
| 611 | return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2); |
| 612 | } |
| 613 | |
| 614 | /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. |
| 615 | static |
| 616 | SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, |
| 617 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 618 | int SVOffset = LD->getSrcValueOffset(); |
| 619 | SDOperand Chain = LD->getChain(); |
| 620 | SDOperand Ptr = LD->getBasePtr(); |
| 621 | MVT::ValueType VT = LD->getValueType(0); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 622 | MVT::ValueType LoadedVT = LD->getMemoryVT(); |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 623 | if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT)) { |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 624 | // Expand to a (misaligned) integer load of the same size, |
| 625 | // then bitconvert to floating point. |
| 626 | MVT::ValueType intVT; |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 627 | if (LoadedVT == MVT::f64) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 628 | intVT = MVT::i64; |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 629 | else if (LoadedVT == MVT::f32) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 630 | intVT = MVT::i32; |
| 631 | else |
| 632 | assert(0 && "Unaligned load of unsupported floating point type"); |
| 633 | |
| 634 | SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(), |
| 635 | SVOffset, LD->isVolatile(), |
| 636 | LD->getAlignment()); |
| 637 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad); |
| 638 | if (LoadedVT != VT) |
| 639 | Result = DAG.getNode(ISD::FP_EXTEND, VT, Result); |
| 640 | |
| 641 | SDOperand Ops[] = { Result, Chain }; |
| 642 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 643 | Ops, 2); |
| 644 | } |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 645 | assert((MVT::isInteger(LoadedVT) || MVT::isVector(LoadedVT)) && |
| 646 | "Unaligned load of unsupported type."); |
| 647 | |
| 648 | // Compute the new VT that is half the size of the old one. We either have an |
| 649 | // integer MVT or we have a vector MVT. |
| 650 | unsigned NumBits = MVT::getSizeInBits(LoadedVT); |
| 651 | MVT::ValueType NewLoadedVT; |
| 652 | if (!MVT::isVector(LoadedVT)) { |
| 653 | NewLoadedVT = MVT::getIntegerType(NumBits/2); |
| 654 | } else { |
| 655 | // FIXME: This is not right for <1 x anything> it is also not right for |
| 656 | // non-power-of-two vectors. |
| 657 | NewLoadedVT = MVT::getVectorType(MVT::getVectorElementType(LoadedVT), |
| 658 | MVT::getVectorNumElements(LoadedVT)/2); |
| 659 | } |
| 660 | NumBits >>= 1; |
| 661 | |
| 662 | unsigned Alignment = LD->getAlignment(); |
| 663 | unsigned IncrementSize = NumBits / 8; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 664 | ISD::LoadExtType HiExtType = LD->getExtensionType(); |
| 665 | |
| 666 | // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. |
| 667 | if (HiExtType == ISD::NON_EXTLOAD) |
| 668 | HiExtType = ISD::ZEXTLOAD; |
| 669 | |
| 670 | // Load the value in two parts |
| 671 | SDOperand Lo, Hi; |
| 672 | if (TLI.isLittleEndian()) { |
| 673 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 674 | SVOffset, NewLoadedVT, LD->isVolatile(), Alignment); |
| 675 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 676 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 677 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), |
| 678 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 679 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 680 | } else { |
| 681 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset, |
| 682 | NewLoadedVT,LD->isVolatile(), Alignment); |
| 683 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 684 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 685 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 686 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 687 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | // aggregate the two parts |
| 691 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 692 | SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount); |
| 693 | Result = DAG.getNode(ISD::OR, VT, Result, Lo); |
| 694 | |
| 695 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 696 | Hi.getValue(1)); |
| 697 | |
| 698 | SDOperand Ops[] = { Result, TF }; |
| 699 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2); |
| 700 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 701 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 702 | /// UnrollVectorOp - We know that the given vector has a legal type, however |
| 703 | /// the operation it performs is not legal and is an operation that we have |
| 704 | /// no way of lowering. "Unroll" the vector, splitting out the scalars and |
| 705 | /// operating on each element individually. |
| 706 | SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) { |
| 707 | MVT::ValueType VT = Op.getValueType(); |
| 708 | assert(isTypeLegal(VT) && |
| 709 | "Caller should expand or promote operands that are not legal!"); |
| 710 | assert(Op.Val->getNumValues() == 1 && |
| 711 | "Can't unroll a vector with multiple results!"); |
| 712 | unsigned NE = MVT::getVectorNumElements(VT); |
| 713 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
| 714 | |
| 715 | SmallVector<SDOperand, 8> Scalars; |
| 716 | SmallVector<SDOperand, 4> Operands(Op.getNumOperands()); |
| 717 | for (unsigned i = 0; i != NE; ++i) { |
| 718 | for (unsigned j = 0; j != Op.getNumOperands(); ++j) { |
| 719 | SDOperand Operand = Op.getOperand(j); |
| 720 | MVT::ValueType OperandVT = Operand.getValueType(); |
| 721 | if (MVT::isVector(OperandVT)) { |
| 722 | // A vector operand; extract a single element. |
| 723 | MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT); |
| 724 | Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 725 | OperandEltVT, |
| 726 | Operand, |
| 727 | DAG.getConstant(i, MVT::i32)); |
| 728 | } else { |
| 729 | // A scalar operand; just use it as is. |
| 730 | Operands[j] = Operand; |
| 731 | } |
| 732 | } |
| 733 | Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, |
| 734 | &Operands[0], Operands.size())); |
| 735 | } |
| 736 | |
| 737 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size()); |
| 738 | } |
| 739 | |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 740 | /// GetFPLibCall - Return the right libcall for the given floating point type. |
| 741 | static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT, |
| 742 | RTLIB::Libcall Call_F32, |
| 743 | RTLIB::Libcall Call_F64, |
| 744 | RTLIB::Libcall Call_F80, |
| 745 | RTLIB::Libcall Call_PPCF128) { |
| 746 | return |
| 747 | VT == MVT::f32 ? Call_F32 : |
| 748 | VT == MVT::f64 ? Call_F64 : |
| 749 | VT == MVT::f80 ? Call_F80 : |
| 750 | VT == MVT::ppcf128 ? Call_PPCF128 : |
| 751 | RTLIB::UNKNOWN_LIBCALL; |
| 752 | } |
| 753 | |
Dan Gohman | a346615 | 2007-07-13 20:14:11 +0000 | [diff] [blame] | 754 | /// LegalizeOp - We know that the specified value has a legal type, and |
| 755 | /// that its operands are legal. Now ensure that the operation itself |
| 756 | /// is legal, recursively ensuring that the operands' operations remain |
| 757 | /// legal. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 758 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 759 | if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. |
| 760 | return Op; |
| 761 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 762 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 763 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 764 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 766 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 767 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 768 | if (Node->getNumValues() > 1) { |
| 769 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 770 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 771 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 772 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 773 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 774 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 778 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 779 | // means that we always must cache transformed nodes. |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 780 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 781 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 782 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 783 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 784 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 785 | bool isCustom = false; |
| 786 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 787 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 788 | case ISD::FrameIndex: |
| 789 | case ISD::EntryToken: |
| 790 | case ISD::Register: |
| 791 | case ISD::BasicBlock: |
| 792 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 793 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 794 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 795 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 796 | case ISD::TargetConstantPool: |
| 797 | case ISD::TargetGlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 798 | case ISD::TargetGlobalTLSAddress: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 799 | case ISD::TargetExternalSymbol: |
| 800 | case ISD::VALUETYPE: |
| 801 | case ISD::SRCVALUE: |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 802 | case ISD::MEMOPERAND: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 803 | case ISD::STRING: |
| 804 | case ISD::CONDCODE: |
| 805 | // Primitives must all be legal. |
Duncan Sands | a7c97a7 | 2007-10-16 09:07:20 +0000 | [diff] [blame] | 806 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 807 | "This must be legal!"); |
| 808 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 809 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 810 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 811 | // If this is a target node, legalize it by legalizing the operands then |
| 812 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 813 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 814 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 815 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 816 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 817 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 818 | |
| 819 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 820 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 821 | return Result.getValue(Op.ResNo); |
| 822 | } |
| 823 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 824 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 825 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 826 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 827 | assert(0 && "Do not know how to legalize this operator!"); |
| 828 | abort(); |
Lauro Ramos Venancio | 0d3b678 | 2007-04-20 23:02:39 +0000 | [diff] [blame] | 829 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 830 | case ISD::GlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 831 | case ISD::GlobalTLSAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 832 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 833 | case ISD::ConstantPool: |
| 834 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 835 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 836 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 837 | case TargetLowering::Custom: |
| 838 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 839 | if (Tmp1.Val) Result = Tmp1; |
| 840 | // 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] | 841 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 842 | break; |
| 843 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 844 | break; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 845 | case ISD::FRAMEADDR: |
| 846 | case ISD::RETURNADDR: |
| 847 | // The only option for these nodes is to custom lower them. If the target |
| 848 | // does not custom lower them, then return zero. |
| 849 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 850 | if (Tmp1.Val) |
| 851 | Result = Tmp1; |
| 852 | else |
| 853 | Result = DAG.getConstant(0, TLI.getPointerTy()); |
| 854 | break; |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 855 | case ISD::FRAME_TO_ARGS_OFFSET: { |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 856 | MVT::ValueType VT = Node->getValueType(0); |
| 857 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 858 | default: assert(0 && "This action is not supported yet!"); |
| 859 | case TargetLowering::Custom: |
| 860 | Result = TLI.LowerOperation(Op, DAG); |
| 861 | if (Result.Val) break; |
| 862 | // Fall Thru |
| 863 | case TargetLowering::Legal: |
| 864 | Result = DAG.getConstant(0, VT); |
| 865 | break; |
| 866 | } |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 867 | } |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 868 | break; |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 869 | case ISD::EXCEPTIONADDR: { |
| 870 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 871 | MVT::ValueType VT = Node->getValueType(0); |
| 872 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 873 | default: assert(0 && "This action is not supported yet!"); |
| 874 | case TargetLowering::Expand: { |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 875 | unsigned Reg = TLI.getExceptionAddressRegister(); |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 876 | Result = DAG.getCopyFromReg(Tmp1, Reg, VT); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 877 | } |
| 878 | break; |
| 879 | case TargetLowering::Custom: |
| 880 | Result = TLI.LowerOperation(Op, DAG); |
| 881 | if (Result.Val) break; |
| 882 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 883 | case TargetLowering::Legal: { |
| 884 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 }; |
| 885 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 886 | Ops, 2); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 890 | } |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 891 | if (Result.Val->getNumValues() == 1) break; |
| 892 | |
| 893 | assert(Result.Val->getNumValues() == 2 && |
| 894 | "Cannot return more than two values!"); |
| 895 | |
| 896 | // Since we produced two values, make sure to remember that we |
| 897 | // legalized both of them. |
| 898 | Tmp1 = LegalizeOp(Result); |
| 899 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 900 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 901 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
| 902 | return Op.ResNo ? Tmp2 : Tmp1; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 903 | case ISD::EHSELECTION: { |
| 904 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 905 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 906 | MVT::ValueType VT = Node->getValueType(0); |
| 907 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 908 | default: assert(0 && "This action is not supported yet!"); |
| 909 | case TargetLowering::Expand: { |
| 910 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 911 | Result = DAG.getCopyFromReg(Tmp2, Reg, VT); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 912 | } |
| 913 | break; |
| 914 | case TargetLowering::Custom: |
| 915 | Result = TLI.LowerOperation(Op, DAG); |
| 916 | if (Result.Val) break; |
| 917 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 918 | case TargetLowering::Legal: { |
| 919 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 }; |
| 920 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 921 | Ops, 2); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 922 | break; |
| 923 | } |
| 924 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 925 | } |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 926 | if (Result.Val->getNumValues() == 1) break; |
| 927 | |
| 928 | assert(Result.Val->getNumValues() == 2 && |
| 929 | "Cannot return more than two values!"); |
| 930 | |
| 931 | // Since we produced two values, make sure to remember that we |
| 932 | // legalized both of them. |
| 933 | Tmp1 = LegalizeOp(Result); |
| 934 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 935 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 936 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
| 937 | return Op.ResNo ? Tmp2 : Tmp1; |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 938 | case ISD::EH_RETURN: { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 939 | MVT::ValueType VT = Node->getValueType(0); |
| 940 | // The only "good" option for this node is to custom lower it. |
| 941 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 942 | default: assert(0 && "This action is not supported at all!"); |
| 943 | case TargetLowering::Custom: |
| 944 | Result = TLI.LowerOperation(Op, DAG); |
| 945 | if (Result.Val) break; |
| 946 | // Fall Thru |
| 947 | case TargetLowering::Legal: |
| 948 | // Target does not know, how to lower this, lower to noop |
| 949 | Result = LegalizeOp(Node->getOperand(0)); |
| 950 | break; |
| 951 | } |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 952 | } |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 953 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 954 | case ISD::AssertSext: |
| 955 | case ISD::AssertZext: |
| 956 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 957 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 958 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 959 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 960 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 961 | Result = Node->getOperand(Op.ResNo); |
| 962 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 963 | case ISD::CopyFromReg: |
| 964 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 965 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 966 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 967 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 968 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 969 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 970 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 971 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 972 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 973 | } else { |
| 974 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 975 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 976 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 977 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 978 | // Since CopyFromReg produces two values, make sure to remember that we |
| 979 | // legalized both of them. |
| 980 | AddLegalizedOperand(Op.getValue(0), Result); |
| 981 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 982 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 983 | case ISD::UNDEF: { |
| 984 | MVT::ValueType VT = Op.getValueType(); |
| 985 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 986 | default: assert(0 && "This action is not supported yet!"); |
| 987 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 988 | if (MVT::isInteger(VT)) |
| 989 | Result = DAG.getConstant(0, VT); |
| 990 | else if (MVT::isFloatingPoint(VT)) |
Dale Johannesen | f41db21 | 2007-09-26 17:26:49 +0000 | [diff] [blame] | 991 | Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)), |
| 992 | VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 993 | else |
| 994 | assert(0 && "Unknown value type!"); |
| 995 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 996 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
| 999 | break; |
| 1000 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1002 | case ISD::INTRINSIC_W_CHAIN: |
| 1003 | case ISD::INTRINSIC_WO_CHAIN: |
| 1004 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1005 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1006 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1007 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1008 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1011 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 1012 | TargetLowering::Custom) { |
| 1013 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1014 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | if (Result.Val->getNumValues() == 1) break; |
| 1018 | |
| 1019 | // Must have return value and chain result. |
| 1020 | assert(Result.Val->getNumValues() == 2 && |
| 1021 | "Cannot return more than two values!"); |
| 1022 | |
| 1023 | // Since loads produce two values, make sure to remember that we |
| 1024 | // legalized both of them. |
| 1025 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1026 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1027 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1028 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1029 | |
| 1030 | case ISD::LOCATION: |
| 1031 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 1032 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 1033 | |
| 1034 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 1035 | case TargetLowering::Promote: |
| 1036 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1037 | case TargetLowering::Expand: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1038 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1039 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1040 | bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1041 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1042 | if (MMI && (useDEBUG_LOC || useLABEL)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1043 | const std::string &FName = |
| 1044 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 1045 | const std::string &DirName = |
| 1046 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1047 | unsigned SrcFile = MMI->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1049 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 1050 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1051 | SDOperand LineOp = Node->getOperand(1); |
| 1052 | SDOperand ColOp = Node->getOperand(2); |
| 1053 | |
| 1054 | if (useDEBUG_LOC) { |
| 1055 | Ops.push_back(LineOp); // line # |
| 1056 | Ops.push_back(ColOp); // col # |
| 1057 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1058 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1059 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 1060 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 1061 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Evan Cheng | a647c92 | 2008-02-01 02:05:57 +0000 | [diff] [blame] | 1062 | unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1063 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1064 | Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label |
| 1065 | Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1066 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 1067 | } else { |
| 1068 | Result = Tmp1; // chain |
| 1069 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1070 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 1071 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1072 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 1073 | if (Tmp1 != Node->getOperand(0) || |
| 1074 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1075 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1076 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 1077 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 1078 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 1079 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 1080 | } else { |
| 1081 | // Otherwise promote them. |
| 1082 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 1083 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 1084 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1085 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 1086 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1087 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | break; |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1092 | |
| 1093 | case ISD::DECLARE: |
| 1094 | assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!"); |
| 1095 | switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) { |
| 1096 | default: assert(0 && "This action is not supported yet!"); |
| 1097 | case TargetLowering::Legal: |
| 1098 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1099 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address. |
| 1100 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable. |
| 1101 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1102 | break; |
| 1103 | } |
| 1104 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1105 | |
| 1106 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1107 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1108 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1109 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1110 | case TargetLowering::Legal: |
| 1111 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1112 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 1113 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 1114 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1115 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | } |
| 1118 | break; |
| 1119 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1120 | case ISD::LABEL: |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1121 | assert(Node->getNumOperands() == 3 && "Invalid LABEL node!"); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1122 | switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1123 | default: assert(0 && "This action is not supported yet!"); |
| 1124 | case TargetLowering::Legal: |
| 1125 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1126 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1127 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand. |
| 1128 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1129 | break; |
Chris Lattner | a9569f1 | 2007-03-03 19:21:38 +0000 | [diff] [blame] | 1130 | case TargetLowering::Expand: |
| 1131 | Result = LegalizeOp(Node->getOperand(0)); |
| 1132 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1133 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1134 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1135 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1136 | case ISD::Constant: { |
| 1137 | ConstantSDNode *CN = cast<ConstantSDNode>(Node); |
| 1138 | unsigned opAction = |
| 1139 | TLI.getOperationAction(ISD::Constant, CN->getValueType(0)); |
| 1140 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1141 | // We know we don't need to expand constants here, constants only have one |
| 1142 | // value and we check that it is fine above. |
| 1143 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1144 | if (opAction == TargetLowering::Custom) { |
| 1145 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1146 | if (Tmp1.Val) |
| 1147 | Result = Tmp1; |
| 1148 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1149 | break; |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1150 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1151 | case ISD::ConstantFP: { |
| 1152 | // Spill FP immediates to the constant pool if the target cannot directly |
| 1153 | // codegen them. Targets often have some immediate values that can be |
| 1154 | // efficiently generated into an FP register without a load. We explicitly |
| 1155 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1156 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 1157 | |
| 1158 | // Check to see if this FP immediate is already legal. |
| 1159 | bool isLegal = false; |
| 1160 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 1161 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 1162 | if (CFP->isExactlyValue(*I)) { |
| 1163 | isLegal = true; |
| 1164 | break; |
| 1165 | } |
| 1166 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1167 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 1168 | if (isLegal) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 1169 | Result = DAG.getTargetConstantFP(CFP->getValueAPF(), |
| 1170 | CFP->getValueType(0)); |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 1175 | default: assert(0 && "This action is not supported yet!"); |
| 1176 | case TargetLowering::Custom: |
| 1177 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1178 | if (Tmp3.Val) { |
| 1179 | Result = Tmp3; |
| 1180 | break; |
| 1181 | } |
| 1182 | // FALLTHROUGH |
| 1183 | case TargetLowering::Expand: |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 1184 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1185 | } |
| 1186 | break; |
| 1187 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1188 | case ISD::TokenFactor: |
| 1189 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1190 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1191 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1192 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1193 | } else if (Node->getNumOperands() == 3) { |
| 1194 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1195 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1196 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1197 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1198 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1199 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1200 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1201 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1202 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1203 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1204 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1205 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1206 | |
| 1207 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1208 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1209 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1210 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 1211 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1212 | |
| 1213 | // The number of incoming and outgoing values should match; unless the final |
| 1214 | // outgoing value is a flag. |
| 1215 | assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() || |
| 1216 | (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 && |
| 1217 | Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) == |
| 1218 | MVT::Flag)) && |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1219 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1221 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1222 | // 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] | 1223 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1224 | if (Tmp3.Val->getValueType(i) == MVT::Flag) |
| 1225 | continue; |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1226 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1227 | if (Op.ResNo == i) |
| 1228 | Tmp2 = Tmp1; |
| 1229 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 1230 | } |
| 1231 | return Tmp2; |
Christopher Lamb | 557c363 | 2007-07-26 07:34:40 +0000 | [diff] [blame] | 1232 | case ISD::EXTRACT_SUBREG: { |
| 1233 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1234 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1)); |
| 1235 | assert(idx && "Operand must be a constant"); |
| 1236 | Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1237 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1238 | } |
| 1239 | break; |
| 1240 | case ISD::INSERT_SUBREG: { |
| 1241 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1242 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1243 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2)); |
| 1244 | assert(idx && "Operand must be a constant"); |
| 1245 | Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1246 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1247 | } |
| 1248 | break; |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1249 | case ISD::BUILD_VECTOR: |
| 1250 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1251 | default: assert(0 && "This action is not supported yet!"); |
| 1252 | case TargetLowering::Custom: |
| 1253 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1254 | if (Tmp3.Val) { |
| 1255 | Result = Tmp3; |
| 1256 | break; |
| 1257 | } |
| 1258 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1259 | case TargetLowering::Expand: |
| 1260 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1261 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1262 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | case ISD::INSERT_VECTOR_ELT: |
| 1265 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
| 1266 | Tmp2 = LegalizeOp(Node->getOperand(1)); // InVal |
| 1267 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
| 1268 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1269 | |
| 1270 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 1271 | Node->getValueType(0))) { |
| 1272 | default: assert(0 && "This action is not supported yet!"); |
| 1273 | case TargetLowering::Legal: |
| 1274 | break; |
| 1275 | case TargetLowering::Custom: |
Nate Begeman | 2281a99 | 2008-01-05 20:47:37 +0000 | [diff] [blame] | 1276 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1277 | if (Tmp4.Val) { |
| 1278 | Result = Tmp4; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | } |
| 1281 | // FALLTHROUGH |
| 1282 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1283 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 1284 | // then a shuffle that inserts it into the right position in the vector. |
| 1285 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
| 1286 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 1287 | Tmp1.getValueType(), Tmp2); |
| 1288 | |
| 1289 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 1290 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1291 | MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1292 | |
| 1293 | // We generate a shuffle of InVec and ScVec, so the shuffle mask should |
| 1294 | // be 0,1,2,3,4,5... with the appropriate element replaced with elt 0 of |
| 1295 | // the RHS. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1296 | SmallVector<SDOperand, 8> ShufOps; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1297 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1298 | if (i != InsertPos->getValue()) |
| 1299 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 1300 | else |
| 1301 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 1302 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1303 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 1304 | &ShufOps[0], ShufOps.size()); |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1305 | |
| 1306 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 1307 | Tmp1, ScVec, ShufMask); |
| 1308 | Result = LegalizeOp(Result); |
| 1309 | break; |
| 1310 | } |
| 1311 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1312 | // If the target doesn't support this, we have to spill the input vector |
| 1313 | // to a temporary stack slot, update the element, then reload it. This is |
| 1314 | // badness. We could also load the value into a vector register (either |
| 1315 | // with a "move to register" or "extload into register" instruction, then |
| 1316 | // permute it into place, if the idx is a constant and if the idx is |
| 1317 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1318 | MVT::ValueType VT = Tmp1.getValueType(); |
| 1319 | MVT::ValueType EltVT = Tmp2.getValueType(); |
| 1320 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 1321 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 1322 | SDOperand StackPtr = DAG.CreateStackTemporary(VT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 1323 | |
| 1324 | FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr.Val); |
| 1325 | assert(StackPtrFI); |
| 1326 | int SPFI = StackPtrFI->getIndex(); |
| 1327 | |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1328 | // Store the vector. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 1329 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, |
| 1330 | &PseudoSourceValue::getFixedStack(), |
| 1331 | SPFI); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1334 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 1335 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1336 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1337 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 1338 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 1339 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1340 | // Store the scalar value. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 1341 | Ch = DAG.getStore(Ch, Tmp2, StackPtr2, |
| 1342 | &PseudoSourceValue::getFixedStack(), SPFI); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1343 | // Load the updated vector. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 1344 | Result = DAG.getLoad(VT, Ch, StackPtr, |
| 1345 | &PseudoSourceValue::getFixedStack(), SPFI); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1350 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1351 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 1352 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 1353 | break; |
| 1354 | } |
| 1355 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1356 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 1357 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1358 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 1359 | Node->getValueType(0))) { |
| 1360 | default: assert(0 && "This action is not supported yet!"); |
| 1361 | case TargetLowering::Legal: |
| 1362 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 1363 | case TargetLowering::Custom: |
| 1364 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1365 | if (Tmp3.Val) { |
| 1366 | Result = Tmp3; |
| 1367 | break; |
| 1368 | } |
| 1369 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1370 | case TargetLowering::Expand: |
| 1371 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1372 | break; |
| 1373 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1374 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1375 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1376 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1377 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 1378 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1379 | |
| 1380 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1381 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 1382 | default: assert(0 && "Unknown operation action!"); |
| 1383 | case TargetLowering::Legal: |
| 1384 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 1385 | "vector shuffle should not be created if not legal!"); |
| 1386 | break; |
| 1387 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1388 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1389 | if (Tmp3.Val) { |
| 1390 | Result = Tmp3; |
| 1391 | break; |
| 1392 | } |
| 1393 | // FALLTHROUGH |
| 1394 | case TargetLowering::Expand: { |
| 1395 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1396 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1397 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1398 | SDOperand Mask = Node->getOperand(2); |
| 1399 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1400 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1401 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1402 | SDOperand Arg = Mask.getOperand(i); |
| 1403 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1404 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1405 | } else { |
| 1406 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1407 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1408 | if (Idx < NumElems) |
| 1409 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1410 | DAG.getConstant(Idx, PtrVT))); |
| 1411 | else |
| 1412 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1413 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1414 | } |
| 1415 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1416 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1417 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1418 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1419 | case TargetLowering::Promote: { |
| 1420 | // Change base type to a different vector type. |
| 1421 | MVT::ValueType OVT = Node->getValueType(0); |
| 1422 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1423 | |
| 1424 | // Cast the two input vectors. |
| 1425 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1426 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1427 | |
| 1428 | // Convert the shuffle mask to the right # elements. |
| 1429 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1430 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1431 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1432 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1433 | break; |
| 1434 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1435 | } |
| 1436 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1437 | |
| 1438 | case ISD::EXTRACT_VECTOR_ELT: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1439 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1440 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1441 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1442 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1443 | break; |
| 1444 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1445 | case ISD::EXTRACT_SUBVECTOR: |
| 1446 | Tmp1 = Node->getOperand(0); |
| 1447 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1448 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1449 | Result = ExpandEXTRACT_SUBVECTOR(Result); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 1450 | break; |
| 1451 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1452 | case ISD::CALLSEQ_START: { |
| 1453 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1454 | |
| 1455 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1456 | // to this call start. This ensures that any libcalls that need be inserted |
| 1457 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1458 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1459 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1460 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1461 | NodesLeadingTo); |
| 1462 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1463 | |
| 1464 | // Now that we legalized all of the inputs (which may have inserted |
| 1465 | // libcalls) create the new CALLSEQ_START node. |
| 1466 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1467 | |
| 1468 | // Merge in the last call, to ensure that this call start after the last |
| 1469 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1470 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1471 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1472 | Tmp1 = LegalizeOp(Tmp1); |
| 1473 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Do not try to legalize the target-specific arguments (#1+). |
| 1476 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1477 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1478 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1479 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1483 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1484 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1485 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1486 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1487 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1488 | // sequence have been legalized, legalize the call itself. During this |
| 1489 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1490 | // can overlap. |
| 1491 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1492 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1493 | // Note that we are selecting this call! |
| 1494 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1495 | IsLegalizingCall = true; |
| 1496 | |
| 1497 | // Legalize the call, starting from the CALLSEQ_END. |
| 1498 | LegalizeOp(LastCALLSEQ_END); |
| 1499 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1500 | return Result; |
| 1501 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1502 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1503 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1504 | // will cause this node to be legalized as well as handling libcalls right. |
| 1505 | if (LastCALLSEQ_END.Val != Node) { |
| 1506 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
Chris Lattner | 718071c | 2007-02-04 00:50:02 +0000 | [diff] [blame] | 1507 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1508 | assert(I != LegalizedNodes.end() && |
| 1509 | "Legalizing the call start should have legalized this node!"); |
| 1510 | return I->second; |
| 1511 | } |
| 1512 | |
| 1513 | // Otherwise, the call start has been legalized and everything is going |
| 1514 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1515 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1516 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1517 | // an optional flag input. |
| 1518 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1519 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1520 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1521 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1522 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1523 | } |
| 1524 | } else { |
| 1525 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1526 | if (Tmp1 != Node->getOperand(0) || |
| 1527 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1528 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1529 | Ops[0] = Tmp1; |
| 1530 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1531 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1532 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1533 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1534 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1535 | // This finishes up call legalization. |
| 1536 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1537 | |
| 1538 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1539 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1540 | if (Node->getNumValues() == 2) |
| 1541 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1542 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1543 | case ISD::DYNAMIC_STACKALLOC: { |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1544 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1545 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1546 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1547 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1548 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1550 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1551 | Tmp2 = Result.getValue(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1552 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1553 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1554 | case TargetLowering::Expand: { |
| 1555 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1556 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1557 | " not tell us which reg is the stack pointer!"); |
| 1558 | SDOperand Chain = Tmp1.getOperand(0); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Chain the dynamic stack allocation so that it doesn't modify the stack |
| 1561 | // pointer when other instructions are using the stack. |
| 1562 | Chain = DAG.getCALLSEQ_START(Chain, |
| 1563 | DAG.getConstant(0, TLI.getPointerTy())); |
| 1564 | |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1565 | SDOperand Size = Tmp2.getOperand(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1566 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT); |
| 1567 | Chain = SP.getValue(1); |
| 1568 | unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue(); |
| 1569 | unsigned StackAlign = |
| 1570 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1571 | if (Align > StackAlign) |
Evan Cheng | 3e20bba | 2007-08-17 18:02:22 +0000 | [diff] [blame] | 1572 | SP = DAG.getNode(ISD::AND, VT, SP, |
| 1573 | DAG.getConstant(-(uint64_t)Align, VT)); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1574 | Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1575 | Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain |
| 1576 | |
| 1577 | Tmp2 = |
| 1578 | DAG.getCALLSEQ_END(Chain, |
| 1579 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1580 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1581 | SDOperand()); |
| 1582 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1583 | Tmp1 = LegalizeOp(Tmp1); |
| 1584 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | } |
| 1587 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1588 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1589 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1590 | Tmp1 = LegalizeOp(Tmp3); |
| 1591 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1592 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1593 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1594 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1595 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1596 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1597 | // Since this op produce two values, make sure to remember that we |
| 1598 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1599 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1600 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1601 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1602 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1603 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1604 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1605 | bool Changed = false; |
| 1606 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1607 | // that need to be expanded or something. Note we skip the asm string and |
| 1608 | // all of the TargetConstant flags. |
| 1609 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1610 | Changed = Op != Ops[0]; |
| 1611 | Ops[0] = Op; |
| 1612 | |
| 1613 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1614 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1615 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1616 | for (++i; NumVals; ++i, --NumVals) { |
| 1617 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1618 | if (Op != Ops[i]) { |
| 1619 | Changed = true; |
| 1620 | Ops[i] = Op; |
| 1621 | } |
| 1622 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1623 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1624 | |
| 1625 | if (HasInFlag) { |
| 1626 | Op = LegalizeOp(Ops.back()); |
| 1627 | Changed |= Op != Ops.back(); |
| 1628 | Ops.back() = Op; |
| 1629 | } |
| 1630 | |
| 1631 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1632 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1633 | |
| 1634 | // 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] | 1635 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1636 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1637 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1638 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1639 | case ISD::BR: |
| 1640 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1641 | // Ensure that libcalls are emitted before a branch. |
| 1642 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1643 | Tmp1 = LegalizeOp(Tmp1); |
| 1644 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1645 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1646 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1647 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1648 | case ISD::BRIND: |
| 1649 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1650 | // Ensure that libcalls are emitted before a branch. |
| 1651 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1652 | Tmp1 = LegalizeOp(Tmp1); |
| 1653 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1654 | |
| 1655 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1656 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1657 | case Legal: |
| 1658 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1659 | break; |
| 1660 | } |
| 1661 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1662 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1663 | case ISD::BR_JT: |
| 1664 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1665 | // Ensure that libcalls are emitted before a branch. |
| 1666 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1667 | Tmp1 = LegalizeOp(Tmp1); |
| 1668 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1669 | |
| 1670 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1671 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1672 | |
| 1673 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1674 | default: assert(0 && "This action is not supported yet!"); |
| 1675 | case TargetLowering::Legal: break; |
| 1676 | case TargetLowering::Custom: |
| 1677 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1678 | if (Tmp1.Val) Result = Tmp1; |
| 1679 | break; |
| 1680 | case TargetLowering::Expand: { |
| 1681 | SDOperand Chain = Result.getOperand(0); |
| 1682 | SDOperand Table = Result.getOperand(1); |
| 1683 | SDOperand Index = Result.getOperand(2); |
| 1684 | |
| 1685 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1686 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1687 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1688 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1689 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1690 | |
| 1691 | SDOperand LD; |
| 1692 | switch (EntrySize) { |
| 1693 | default: assert(0 && "Size of jump table not supported yet."); break; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 1694 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, |
| 1695 | &PseudoSourceValue::getJumpTable(), 0); break; |
| 1696 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, |
| 1697 | &PseudoSourceValue::getJumpTable(), 0); break; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1700 | Addr = LD; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1701 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1702 | // For PIC, the sequence is: |
| 1703 | // BRIND(load(Jumptable + index) + RelocBase) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1704 | // RelocBase can be JumpTable, GOT or some sort of global base. |
| 1705 | if (PTy != MVT::i32) |
| 1706 | Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr); |
| 1707 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, |
| 1708 | TLI.getPICJumpTableRelocBase(Table, DAG)); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1709 | } |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1710 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1714 | case ISD::BRCOND: |
| 1715 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1716 | // Ensure that libcalls are emitted before a return. |
| 1717 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1718 | Tmp1 = LegalizeOp(Tmp1); |
| 1719 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1720 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1721 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1722 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1723 | case Legal: |
| 1724 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1725 | break; |
| 1726 | case Promote: |
| 1727 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1728 | |
| 1729 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1730 | // that the value is properly zero extended. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1731 | if (!DAG.MaskedValueIsZero(Tmp2, |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1732 | MVT::getIntVTBitMask(Tmp2.getValueType())^1)) |
| 1733 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1734 | break; |
| 1735 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1738 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1739 | |
| 1740 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1741 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1742 | case TargetLowering::Legal: break; |
| 1743 | case TargetLowering::Custom: |
| 1744 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1745 | if (Tmp1.Val) Result = Tmp1; |
| 1746 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1747 | case TargetLowering::Expand: |
| 1748 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1749 | // Node. |
| 1750 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1751 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1752 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1753 | Node->getOperand(2)); |
| 1754 | } else { |
| 1755 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1756 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1757 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1758 | Node->getOperand(2)); |
| 1759 | } |
| 1760 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1761 | } |
| 1762 | break; |
| 1763 | case ISD::BR_CC: |
| 1764 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1765 | // Ensure that libcalls are emitted before a branch. |
| 1766 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1767 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1768 | Tmp2 = Node->getOperand(2); // LHS |
| 1769 | Tmp3 = Node->getOperand(3); // RHS |
| 1770 | Tmp4 = Node->getOperand(1); // CC |
| 1771 | |
| 1772 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1773 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1774 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1775 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1776 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1777 | // the result against zero to select between true and false values. |
| 1778 | if (Tmp3.Val == 0) { |
| 1779 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1780 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1781 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1782 | |
| 1783 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1784 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1785 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1786 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1787 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1788 | case TargetLowering::Legal: break; |
| 1789 | case TargetLowering::Custom: |
| 1790 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1791 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1792 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1793 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1794 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1795 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1796 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1797 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1798 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1799 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1800 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1801 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1802 | MVT::ValueType VT = Node->getValueType(0); |
| 1803 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1804 | Tmp3 = Result.getValue(0); |
| 1805 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1806 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1807 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1808 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1809 | case TargetLowering::Legal: |
| 1810 | // If this is an unaligned load and the target doesn't support it, |
| 1811 | // expand it. |
| 1812 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1813 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1814 | getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT())); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1815 | if (LD->getAlignment() < ABIAlignment){ |
| 1816 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 1817 | TLI); |
| 1818 | Tmp3 = Result.getOperand(0); |
| 1819 | Tmp4 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1820 | Tmp3 = LegalizeOp(Tmp3); |
| 1821 | Tmp4 = LegalizeOp(Tmp4); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1825 | case TargetLowering::Custom: |
| 1826 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1827 | if (Tmp1.Val) { |
| 1828 | Tmp3 = LegalizeOp(Tmp1); |
| 1829 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1830 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | case TargetLowering::Promote: { |
| 1833 | // Only promote a load of vector type to another. |
| 1834 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1835 | // Change base type to a different vector type. |
| 1836 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1837 | |
| 1838 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1839 | LD->getSrcValueOffset(), |
| 1840 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1841 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1842 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1843 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1844 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1845 | } |
| 1846 | // Since loads produce two values, make sure to remember that we |
| 1847 | // legalized both of them. |
| 1848 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1849 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1850 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1851 | } else { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1852 | MVT::ValueType SrcVT = LD->getMemoryVT(); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1853 | unsigned SrcWidth = MVT::getSizeInBits(SrcVT); |
| 1854 | int SVOffset = LD->getSrcValueOffset(); |
| 1855 | unsigned Alignment = LD->getAlignment(); |
| 1856 | bool isVolatile = LD->isVolatile(); |
| 1857 | |
| 1858 | if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) && |
| 1859 | // Some targets pretend to have an i1 loading operation, and actually |
| 1860 | // load an i8. This trick is correct for ZEXTLOAD because the top 7 |
| 1861 | // bits are guaranteed to be zero; it helps the optimizers understand |
| 1862 | // that these bits are zero. It is also useful for EXTLOAD, since it |
| 1863 | // tells the optimizers that those bits are undefined. It would be |
| 1864 | // nice to have an effective generic way of getting these benefits... |
| 1865 | // Until such a way is found, don't insist on promoting i1 here. |
| 1866 | (SrcVT != MVT::i1 || |
| 1867 | TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) { |
| 1868 | // Promote to a byte-sized load if not loading an integral number of |
| 1869 | // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. |
| 1870 | unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT); |
| 1871 | MVT::ValueType NVT = MVT::getIntegerType(NewWidth); |
| 1872 | SDOperand Ch; |
| 1873 | |
| 1874 | // The extra bits are guaranteed to be zero, since we stored them that |
| 1875 | // way. A zext load from NVT thus automatically gives zext from SrcVT. |
| 1876 | |
| 1877 | ISD::LoadExtType NewExtType = |
| 1878 | ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| 1879 | |
| 1880 | Result = DAG.getExtLoad(NewExtType, Node->getValueType(0), |
| 1881 | Tmp1, Tmp2, LD->getSrcValue(), SVOffset, |
| 1882 | NVT, isVolatile, Alignment); |
| 1883 | |
| 1884 | Ch = Result.getValue(1); // The chain. |
| 1885 | |
| 1886 | if (ExtType == ISD::SEXTLOAD) |
| 1887 | // Having the top bits zero doesn't help when sign extending. |
| 1888 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1889 | Result, DAG.getValueType(SrcVT)); |
| 1890 | else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) |
| 1891 | // All the top bits are guaranteed to be zero - inform the optimizers. |
| 1892 | Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result, |
| 1893 | DAG.getValueType(SrcVT)); |
| 1894 | |
| 1895 | Tmp1 = LegalizeOp(Result); |
| 1896 | Tmp2 = LegalizeOp(Ch); |
| 1897 | } else if (SrcWidth & (SrcWidth - 1)) { |
| 1898 | // If not loading a power-of-2 number of bits, expand as two loads. |
| 1899 | assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) && |
| 1900 | "Unsupported extload!"); |
| 1901 | unsigned RoundWidth = 1 << Log2_32(SrcWidth); |
| 1902 | assert(RoundWidth < SrcWidth); |
| 1903 | unsigned ExtraWidth = SrcWidth - RoundWidth; |
| 1904 | assert(ExtraWidth < RoundWidth); |
| 1905 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 1906 | "Load size not an integral number of bytes!"); |
| 1907 | MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth); |
| 1908 | MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth); |
| 1909 | SDOperand Lo, Hi, Ch; |
| 1910 | unsigned IncrementSize; |
| 1911 | |
| 1912 | if (TLI.isLittleEndian()) { |
| 1913 | // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) |
| 1914 | // Load the bottom RoundWidth bits. |
| 1915 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2, |
| 1916 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 1917 | Alignment); |
| 1918 | |
| 1919 | // Load the remaining ExtraWidth bits. |
| 1920 | IncrementSize = RoundWidth / 8; |
| 1921 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1922 | DAG.getIntPtrConstant(IncrementSize)); |
| 1923 | Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1924 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 1925 | ExtraVT, isVolatile, |
| 1926 | MinAlign(Alignment, IncrementSize)); |
| 1927 | |
| 1928 | // Build a factor node to remember that this load is independent of the |
| 1929 | // other one. |
| 1930 | Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 1931 | Hi.getValue(1)); |
| 1932 | |
| 1933 | // Move the top bits to the right place. |
| 1934 | Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi, |
| 1935 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
| 1936 | |
| 1937 | // Join the hi and lo parts. |
| 1938 | Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1939 | } else { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1940 | // Big endian - avoid unaligned loads. |
| 1941 | // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 |
| 1942 | // Load the top RoundWidth bits. |
| 1943 | Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 1944 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 1945 | Alignment); |
| 1946 | |
| 1947 | // Load the remaining ExtraWidth bits. |
| 1948 | IncrementSize = RoundWidth / 8; |
| 1949 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1950 | DAG.getIntPtrConstant(IncrementSize)); |
| 1951 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2, |
| 1952 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 1953 | ExtraVT, isVolatile, |
| 1954 | MinAlign(Alignment, IncrementSize)); |
| 1955 | |
| 1956 | // Build a factor node to remember that this load is independent of the |
| 1957 | // other one. |
| 1958 | Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 1959 | Hi.getValue(1)); |
| 1960 | |
| 1961 | // Move the top bits to the right place. |
| 1962 | Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi, |
| 1963 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
| 1964 | |
| 1965 | // Join the hi and lo parts. |
| 1966 | Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi); |
| 1967 | } |
| 1968 | |
| 1969 | Tmp1 = LegalizeOp(Result); |
| 1970 | Tmp2 = LegalizeOp(Ch); |
| 1971 | } else { |
| 1972 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 1973 | default: assert(0 && "This action is not supported yet!"); |
| 1974 | case TargetLowering::Custom: |
| 1975 | isCustom = true; |
| 1976 | // FALLTHROUGH |
| 1977 | case TargetLowering::Legal: |
| 1978 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1979 | Tmp1 = Result.getValue(0); |
| 1980 | Tmp2 = Result.getValue(1); |
| 1981 | |
| 1982 | if (isCustom) { |
| 1983 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1984 | if (Tmp3.Val) { |
| 1985 | Tmp1 = LegalizeOp(Tmp3); |
| 1986 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1987 | } |
| 1988 | } else { |
| 1989 | // If this is an unaligned load and the target doesn't support it, |
| 1990 | // expand it. |
| 1991 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1992 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1993 | getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT())); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1994 | if (LD->getAlignment() < ABIAlignment){ |
| 1995 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 1996 | TLI); |
| 1997 | Tmp1 = Result.getOperand(0); |
| 1998 | Tmp2 = Result.getOperand(1); |
| 1999 | Tmp1 = LegalizeOp(Tmp1); |
| 2000 | Tmp2 = LegalizeOp(Tmp2); |
| 2001 | } |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2002 | } |
| 2003 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2004 | break; |
| 2005 | case TargetLowering::Expand: |
| 2006 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 2007 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 2008 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 2009 | LD->getSrcValueOffset(), |
| 2010 | LD->isVolatile(), LD->getAlignment()); |
| 2011 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 2012 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 2013 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 2014 | break; |
| 2015 | } |
| 2016 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
| 2017 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 2018 | // zero/sign extend inreg. |
| 2019 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 2020 | Tmp1, Tmp2, LD->getSrcValue(), |
| 2021 | LD->getSrcValueOffset(), SrcVT, |
| 2022 | LD->isVolatile(), LD->getAlignment()); |
| 2023 | SDOperand ValRes; |
| 2024 | if (ExtType == ISD::SEXTLOAD) |
| 2025 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 2026 | Result, DAG.getValueType(SrcVT)); |
| 2027 | else |
| 2028 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 2029 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 2030 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2031 | break; |
| 2032 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2033 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2034 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2035 | // Since loads produce two values, make sure to remember that we legalized |
| 2036 | // both of them. |
| 2037 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2038 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 2039 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 2040 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 2041 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2042 | case ISD::EXTRACT_ELEMENT: { |
| 2043 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 2044 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2045 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2046 | case Legal: |
| 2047 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 2048 | // 1 -> Hi |
| 2049 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 2050 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 2051 | TLI.getShiftAmountTy())); |
| 2052 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 2053 | } else { |
| 2054 | // 0 -> Lo |
| 2055 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 2056 | Node->getOperand(0)); |
| 2057 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2058 | break; |
| 2059 | case Expand: |
| 2060 | // Get both the low and high parts. |
| 2061 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2062 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 2063 | Result = Tmp2; // 1 -> Hi |
| 2064 | else |
| 2065 | Result = Tmp1; // 0 -> Lo |
| 2066 | break; |
| 2067 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2068 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2069 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2070 | |
| 2071 | case ISD::CopyToReg: |
| 2072 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2073 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2074 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2075 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2076 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2077 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2078 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2079 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2080 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2081 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2082 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2083 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2084 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 2085 | Tmp3); |
| 2086 | } else { |
| 2087 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | // Since this produces two values, make sure to remember that we legalized |
| 2091 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2092 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2093 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2094 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2095 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | |
| 2098 | case ISD::RET: |
| 2099 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 2100 | |
| 2101 | // Ensure that libcalls are emitted before a return. |
| 2102 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 2103 | Tmp1 = LegalizeOp(Tmp1); |
| 2104 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2106 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2107 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 2108 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2109 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2110 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2111 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2112 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2113 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2114 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2115 | if (!MVT::isVector(Tmp2.getValueType())) { |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2116 | SDOperand Lo, Hi; |
| 2117 | ExpandOp(Tmp2, Lo, Hi); |
Chris Lattner | edf2e8d | 2007-03-06 20:01:06 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Big endian systems want the hi reg first. |
| 2120 | if (!TLI.isLittleEndian()) |
| 2121 | std::swap(Lo, Hi); |
| 2122 | |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 2123 | if (Hi.Val) |
| 2124 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 2125 | else |
| 2126 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 2127 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2128 | } else { |
| 2129 | SDNode *InVal = Tmp2.Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 2130 | int InIx = Tmp2.ResNo; |
| 2131 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx)); |
| 2132 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx)); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2133 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2134 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2135 | // type. If so, convert to the vector type. |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2136 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2137 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2138 | // Turn this into a return of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2139 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2140 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2141 | } else if (NumElems == 1) { |
| 2142 | // Turn this into a return of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2143 | Tmp2 = ScalarizeVectorOp(Tmp2); |
| 2144 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2145 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2146 | |
| 2147 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 2148 | // should be returned in integer registers! |
| 2149 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2150 | // The scalarized value type may not be legal, e.g. it might require |
| 2151 | // promotion or expansion. Relegalize the return. |
| 2152 | Result = LegalizeOp(Result); |
| 2153 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2154 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 2155 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2156 | SDOperand Lo, Hi; |
| 2157 | SplitVectorOp(Tmp2, Lo, Hi); |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 2158 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2159 | Result = LegalizeOp(Result); |
| 2160 | } |
| 2161 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2162 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2163 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2164 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2165 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2166 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2167 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2168 | } |
| 2169 | break; |
| 2170 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2171 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2172 | break; |
| 2173 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2174 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2175 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2176 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2177 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 2178 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2179 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2180 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2181 | break; |
| 2182 | case Expand: { |
| 2183 | SDOperand Lo, Hi; |
Dan Gohman | 6595cb3 | 2007-06-27 16:08:04 +0000 | [diff] [blame] | 2184 | assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) && |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2185 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2186 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 2187 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2188 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 2189 | if (Hi.Val) { |
| 2190 | NewValues.push_back(Hi); |
| 2191 | NewValues.push_back(Node->getOperand(i+1)); |
| 2192 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2193 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2194 | } |
| 2195 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2196 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2197 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2198 | |
| 2199 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2200 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2201 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2202 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 2203 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2204 | break; |
| 2205 | } |
| 2206 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 2207 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 2208 | if (Result.getOpcode() == ISD::RET) { |
| 2209 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 2210 | default: assert(0 && "This action is not supported yet!"); |
| 2211 | case TargetLowering::Legal: break; |
| 2212 | case TargetLowering::Custom: |
| 2213 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2214 | if (Tmp1.Val) Result = Tmp1; |
| 2215 | break; |
| 2216 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 2217 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2218 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2219 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2220 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 2221 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 2222 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2223 | int SVOffset = ST->getSrcValueOffset(); |
| 2224 | unsigned Alignment = ST->getAlignment(); |
| 2225 | bool isVolatile = ST->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2226 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2227 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2228 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 2229 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 2230 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 2231 | // to phase ordering between legalized code and the dag combiner. This |
| 2232 | // probably means that we need to integrate dag combiner and legalizer |
| 2233 | // together. |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2234 | // We generally can't do this one for long doubles. |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 2235 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2236 | if (CFP->getValueType(0) == MVT::f32 && |
| 2237 | getTypeAction(MVT::i32) == Legal) { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 2238 | Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF(). |
| 2239 | convertToAPInt().getZExtValue(), |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2240 | MVT::i32); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2241 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2242 | SVOffset, isVolatile, Alignment); |
| 2243 | break; |
| 2244 | } else if (CFP->getValueType(0) == MVT::f64) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2245 | // If this target supports 64-bit registers, do a single 64-bit store. |
| 2246 | if (getTypeAction(MVT::i64) == Legal) { |
| 2247 | Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt(). |
| 2248 | getZExtValue(), MVT::i64); |
| 2249 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2250 | SVOffset, isVolatile, Alignment); |
| 2251 | break; |
| 2252 | } else if (getTypeAction(MVT::i32) == Legal) { |
| 2253 | // Otherwise, if the target supports 32-bit registers, use 2 32-bit |
| 2254 | // stores. If the target supports neither 32- nor 64-bits, this |
| 2255 | // xform is certainly not worth it. |
| 2256 | uint64_t IntVal =CFP->getValueAPF().convertToAPInt().getZExtValue(); |
| 2257 | SDOperand Lo = DAG.getConstant(uint32_t(IntVal), MVT::i32); |
| 2258 | SDOperand Hi = DAG.getConstant(uint32_t(IntVal >>32), MVT::i32); |
| 2259 | if (!TLI.isLittleEndian()) std::swap(Lo, Hi); |
| 2260 | |
| 2261 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
| 2262 | SVOffset, isVolatile, Alignment); |
| 2263 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2264 | DAG.getIntPtrConstant(4)); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2265 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4, |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2266 | isVolatile, MinAlign(Alignment, 4U)); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2267 | |
| 2268 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2269 | break; |
| 2270 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2271 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2274 | switch (getTypeAction(ST->getMemoryVT())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2275 | case Legal: { |
| 2276 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2277 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2278 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2279 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2280 | MVT::ValueType VT = Tmp3.getValueType(); |
| 2281 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 2282 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2283 | case TargetLowering::Legal: |
| 2284 | // If this is an unaligned store and the target doesn't support it, |
| 2285 | // expand it. |
| 2286 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2287 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2288 | getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT())); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2289 | if (ST->getAlignment() < ABIAlignment) |
| 2290 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2291 | TLI); |
| 2292 | } |
| 2293 | break; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2294 | case TargetLowering::Custom: |
| 2295 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2296 | if (Tmp1.Val) Result = Tmp1; |
| 2297 | break; |
| 2298 | case TargetLowering::Promote: |
| 2299 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 2300 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 2301 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 2302 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2303 | ST->getSrcValue(), SVOffset, isVolatile, |
| 2304 | Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | } |
| 2307 | break; |
| 2308 | } |
| 2309 | case Promote: |
| 2310 | // Truncate the value and store the result. |
| 2311 | Tmp3 = PromoteOp(ST->getValue()); |
| 2312 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2313 | SVOffset, ST->getMemoryVT(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2314 | isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2315 | break; |
| 2316 | |
| 2317 | case Expand: |
| 2318 | unsigned IncrementSize = 0; |
| 2319 | SDOperand Lo, Hi; |
| 2320 | |
| 2321 | // If this is a vector type, then we have to calculate the increment as |
| 2322 | // the product of the element size in bytes, and the number of elements |
| 2323 | // in the high half of the vector. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2324 | if (MVT::isVector(ST->getValue().getValueType())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2325 | SDNode *InVal = ST->getValue().Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 2326 | int InIx = ST->getValue().ResNo; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2327 | MVT::ValueType InVT = InVal->getValueType(InIx); |
| 2328 | unsigned NumElems = MVT::getVectorNumElements(InVT); |
| 2329 | MVT::ValueType EVT = MVT::getVectorElementType(InVT); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2330 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2331 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2332 | // type. If so, convert to the vector type. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2333 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2334 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2335 | // Turn this into a normal store of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2336 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2337 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2338 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2339 | Result = LegalizeOp(Result); |
| 2340 | break; |
| 2341 | } else if (NumElems == 1) { |
| 2342 | // Turn this into a normal store of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2343 | Tmp3 = ScalarizeVectorOp(Node->getOperand(1)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2344 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2345 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2346 | // The scalarized value type may not be legal, e.g. it might require |
| 2347 | // promotion or expansion. Relegalize the scalar store. |
| 2348 | Result = LegalizeOp(Result); |
| 2349 | break; |
| 2350 | } else { |
| 2351 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 2352 | IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) * |
| 2353 | MVT::getSizeInBits(EVT)/8; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2354 | } |
| 2355 | } else { |
| 2356 | ExpandOp(Node->getOperand(1), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2357 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2358 | |
| 2359 | if (!TLI.isLittleEndian()) |
| 2360 | std::swap(Lo, Hi); |
| 2361 | } |
| 2362 | |
| 2363 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2364 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2365 | |
| 2366 | if (Hi.Val == NULL) { |
| 2367 | // Must be int <-> float one-to-one expansion. |
| 2368 | Result = Lo; |
| 2369 | break; |
| 2370 | } |
| 2371 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2372 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2373 | DAG.getIntPtrConstant(IncrementSize)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2374 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 2375 | "Pointers must be legal!"); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2376 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2377 | Alignment = MinAlign(Alignment, IncrementSize); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2378 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2379 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2380 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2381 | break; |
| 2382 | } |
| 2383 | } else { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 2384 | switch (getTypeAction(ST->getValue().getValueType())) { |
| 2385 | case Legal: |
| 2386 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2387 | break; |
| 2388 | case Promote: |
| 2389 | // We can promote the value, the truncstore will still take care of it. |
| 2390 | Tmp3 = PromoteOp(ST->getValue()); |
| 2391 | break; |
| 2392 | case Expand: |
| 2393 | // Just store the low part. This may become a non-trunc store, so make |
| 2394 | // sure to use getTruncStore, not UpdateNodeOperands below. |
| 2395 | ExpandOp(ST->getValue(), Tmp3, Tmp4); |
| 2396 | return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2397 | SVOffset, MVT::i8, isVolatile, Alignment); |
| 2398 | } |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2399 | |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2400 | MVT::ValueType StVT = ST->getMemoryVT(); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2401 | unsigned StWidth = MVT::getSizeInBits(StVT); |
| 2402 | |
| 2403 | if (StWidth != MVT::getStoreSizeInBits(StVT)) { |
| 2404 | // Promote to a byte-sized store with upper bits zero if not |
| 2405 | // storing an integral number of bytes. For example, promote |
| 2406 | // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) |
| 2407 | MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT)); |
| 2408 | Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT); |
| 2409 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2410 | SVOffset, NVT, isVolatile, Alignment); |
| 2411 | } else if (StWidth & (StWidth - 1)) { |
| 2412 | // If not storing a power-of-2 number of bits, expand as two stores. |
| 2413 | assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) && |
| 2414 | "Unsupported truncstore!"); |
| 2415 | unsigned RoundWidth = 1 << Log2_32(StWidth); |
| 2416 | assert(RoundWidth < StWidth); |
| 2417 | unsigned ExtraWidth = StWidth - RoundWidth; |
| 2418 | assert(ExtraWidth < RoundWidth); |
| 2419 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 2420 | "Store size not an integral number of bytes!"); |
| 2421 | MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth); |
| 2422 | MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth); |
| 2423 | SDOperand Lo, Hi; |
| 2424 | unsigned IncrementSize; |
| 2425 | |
| 2426 | if (TLI.isLittleEndian()) { |
| 2427 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) |
| 2428 | // Store the bottom RoundWidth bits. |
| 2429 | Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2430 | SVOffset, RoundVT, |
| 2431 | isVolatile, Alignment); |
| 2432 | |
| 2433 | // Store the remaining ExtraWidth bits. |
| 2434 | IncrementSize = RoundWidth / 8; |
| 2435 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2436 | DAG.getIntPtrConstant(IncrementSize)); |
| 2437 | Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3, |
| 2438 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
| 2439 | Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
| 2440 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 2441 | MinAlign(Alignment, IncrementSize)); |
| 2442 | } else { |
| 2443 | // Big endian - avoid unaligned stores. |
| 2444 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X |
| 2445 | // Store the top RoundWidth bits. |
| 2446 | Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3, |
| 2447 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
| 2448 | Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset, |
| 2449 | RoundVT, isVolatile, Alignment); |
| 2450 | |
| 2451 | // Store the remaining ExtraWidth bits. |
| 2452 | IncrementSize = RoundWidth / 8; |
| 2453 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2454 | DAG.getIntPtrConstant(IncrementSize)); |
| 2455 | Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2456 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 2457 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2458 | } |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2459 | |
| 2460 | // The order of the stores doesn't matter. |
| 2461 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2462 | } else { |
| 2463 | if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 2464 | Tmp2 != ST->getBasePtr()) |
| 2465 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2466 | ST->getOffset()); |
| 2467 | |
| 2468 | switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { |
| 2469 | default: assert(0 && "This action is not supported yet!"); |
| 2470 | case TargetLowering::Legal: |
| 2471 | // If this is an unaligned store and the target doesn't support it, |
| 2472 | // expand it. |
| 2473 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2474 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2475 | getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT())); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2476 | if (ST->getAlignment() < ABIAlignment) |
| 2477 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2478 | TLI); |
| 2479 | } |
| 2480 | break; |
| 2481 | case TargetLowering::Custom: |
| 2482 | Result = TLI.LowerOperation(Result, DAG); |
| 2483 | break; |
| 2484 | case Expand: |
| 2485 | // TRUNCSTORE:i16 i32 -> STORE i16 |
| 2486 | assert(isTypeLegal(StVT) && "Do not know how to expand this store!"); |
| 2487 | Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3); |
| 2488 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset, |
| 2489 | isVolatile, Alignment); |
| 2490 | break; |
| 2491 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2492 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2493 | } |
| 2494 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2495 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2496 | case ISD::PCMARKER: |
| 2497 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2498 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2499 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2500 | case ISD::STACKSAVE: |
| 2501 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2502 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2503 | Tmp1 = Result.getValue(0); |
| 2504 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2505 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2506 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 2507 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2508 | case TargetLowering::Legal: break; |
| 2509 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2510 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 2511 | if (Tmp3.Val) { |
| 2512 | Tmp1 = LegalizeOp(Tmp3); |
| 2513 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2514 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2515 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2516 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2517 | // Expand to CopyFromReg if the target set |
| 2518 | // StackPointerRegisterToSaveRestore. |
| 2519 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2520 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2521 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2522 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2523 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2524 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 2525 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2526 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2527 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2528 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Since stacksave produce two values, make sure to remember that we |
| 2531 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2532 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2533 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 2534 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2535 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2536 | case ISD::STACKRESTORE: |
| 2537 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2538 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2539 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2540 | |
| 2541 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 2542 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2543 | case TargetLowering::Legal: break; |
| 2544 | case TargetLowering::Custom: |
| 2545 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2546 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2547 | break; |
| 2548 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2549 | // Expand to CopyToReg if the target set |
| 2550 | // StackPointerRegisterToSaveRestore. |
| 2551 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 2552 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 2553 | } else { |
| 2554 | Result = Tmp1; |
| 2555 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2556 | break; |
| 2557 | } |
| 2558 | break; |
| 2559 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2560 | case ISD::READCYCLECOUNTER: |
| 2561 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2562 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2563 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 2564 | Node->getValueType(0))) { |
| 2565 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2566 | case TargetLowering::Legal: |
| 2567 | Tmp1 = Result.getValue(0); |
| 2568 | Tmp2 = Result.getValue(1); |
| 2569 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2570 | case TargetLowering::Custom: |
| 2571 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2572 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 2573 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2574 | break; |
| 2575 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 2576 | |
| 2577 | // Since rdcc produce two values, make sure to remember that we legalized |
| 2578 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2579 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2580 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2581 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2583 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2584 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2585 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 2586 | case Legal: |
| 2587 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2588 | break; |
| 2589 | case Promote: |
| 2590 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2591 | // Make sure the condition is either zero or one. |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2592 | if (!DAG.MaskedValueIsZero(Tmp1, |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2593 | MVT::getIntVTBitMask(Tmp1.getValueType())^1)) |
| 2594 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2595 | break; |
| 2596 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2597 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2598 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2600 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2601 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2602 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2603 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2604 | case TargetLowering::Legal: break; |
| 2605 | case TargetLowering::Custom: { |
| 2606 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2607 | if (Tmp1.Val) Result = Tmp1; |
| 2608 | break; |
| 2609 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2610 | case TargetLowering::Expand: |
| 2611 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 2612 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 2613 | Tmp2, Tmp3, |
| 2614 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 2615 | } else { |
| 2616 | Result = DAG.getSelectCC(Tmp1, |
| 2617 | DAG.getConstant(0, Tmp1.getValueType()), |
| 2618 | Tmp2, Tmp3, ISD::SETNE); |
| 2619 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2620 | break; |
| 2621 | case TargetLowering::Promote: { |
| 2622 | MVT::ValueType NVT = |
| 2623 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 2624 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 2625 | if (MVT::isVector(Tmp2.getValueType())) { |
| 2626 | ExtOp = ISD::BIT_CONVERT; |
| 2627 | TruncOp = ISD::BIT_CONVERT; |
| 2628 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2629 | ExtOp = ISD::ANY_EXTEND; |
| 2630 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2631 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2632 | ExtOp = ISD::FP_EXTEND; |
| 2633 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2634 | } |
| 2635 | // Promote each of the values to the new type. |
| 2636 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 2637 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 2638 | // Perform the larger operation, then round down. |
| 2639 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2640 | if (TruncOp != ISD::FP_ROUND) |
| 2641 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 2642 | else |
| 2643 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result, |
| 2644 | DAG.getIntPtrConstant(0)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | } |
| 2647 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2648 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2649 | case ISD::SELECT_CC: { |
| 2650 | Tmp1 = Node->getOperand(0); // LHS |
| 2651 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2652 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 2653 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2654 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2655 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2656 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 2657 | |
| 2658 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 2659 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 2660 | // the result against zero to select between true and false values. |
| 2661 | if (Tmp2.Val == 0) { |
| 2662 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2663 | CC = DAG.getCondCode(ISD::SETNE); |
| 2664 | } |
| 2665 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 2666 | |
| 2667 | // Everything is legal, see if we should expand this op or something. |
| 2668 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 2669 | default: assert(0 && "This action is not supported yet!"); |
| 2670 | case TargetLowering::Legal: break; |
| 2671 | case TargetLowering::Custom: |
| 2672 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2673 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2674 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2675 | } |
| 2676 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2677 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2678 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2679 | Tmp1 = Node->getOperand(0); |
| 2680 | Tmp2 = Node->getOperand(1); |
| 2681 | Tmp3 = Node->getOperand(2); |
| 2682 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 2683 | |
| 2684 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2685 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2686 | // return the value we legalized, returned in the LHS |
| 2687 | if (Tmp2.Val == 0) { |
| 2688 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2689 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2690 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2692 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2693 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2694 | case TargetLowering::Custom: |
| 2695 | isCustom = true; |
| 2696 | // FALLTHROUGH. |
| 2697 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2698 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2699 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2700 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2701 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2702 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2703 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2704 | case TargetLowering::Promote: { |
| 2705 | // First step, figure out the appropriate operation to use. |
| 2706 | // Allow SETCC to not be supported for all legal data types |
| 2707 | // Mostly this targets FP |
| 2708 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 2709 | MVT::ValueType OldVT = NewInTy; OldVT = OldVT; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2710 | |
| 2711 | // Scan for the appropriate larger type to use. |
| 2712 | while (1) { |
| 2713 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2714 | |
| 2715 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2716 | "Fell off of the edge of the integer world"); |
| 2717 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2718 | "Fell off of the edge of the floating point world"); |
| 2719 | |
| 2720 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2721 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2722 | break; |
| 2723 | } |
| 2724 | if (MVT::isInteger(NewInTy)) |
| 2725 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2726 | else { |
| 2727 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2728 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2729 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2730 | Tmp1 = LegalizeOp(Tmp1); |
| 2731 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2732 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2733 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2734 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2735 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2736 | case TargetLowering::Expand: |
| 2737 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2738 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2739 | MVT::ValueType VT = Node->getValueType(0); |
| 2740 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2741 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2742 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2743 | break; |
| 2744 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2745 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2746 | case ISD::MEMSET: |
| 2747 | case ISD::MEMCPY: |
| 2748 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2749 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2750 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 2751 | |
| 2752 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 2753 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 2754 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 2755 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2756 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2757 | break; |
| 2758 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 2759 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
| 2762 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2763 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2764 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2765 | |
| 2766 | SDOperand Tmp4; |
| 2767 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2768 | case Expand: { |
| 2769 | // Length is too big, just take the lo-part of the length. |
| 2770 | SDOperand HiPart; |
Chris Lattner | fa9aa2b | 2006-11-07 04:11:44 +0000 | [diff] [blame] | 2771 | ExpandOp(Node->getOperand(3), Tmp4, HiPart); |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 2772 | break; |
| 2773 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2774 | case Legal: |
| 2775 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2776 | break; |
| 2777 | case Promote: |
| 2778 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 2779 | break; |
| 2780 | } |
| 2781 | |
| 2782 | SDOperand Tmp5; |
| 2783 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 2784 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2785 | case Legal: |
| 2786 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 2787 | break; |
| 2788 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 2789 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 2790 | break; |
| 2791 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2792 | |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 2793 | SDOperand Tmp6; |
| 2794 | switch (getTypeAction(Node->getOperand(5).getValueType())) { // bool |
| 2795 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 2796 | case Legal: |
| 2797 | Tmp6 = LegalizeOp(Node->getOperand(5)); |
| 2798 | break; |
| 2799 | case Promote: |
| 2800 | Tmp6 = PromoteOp(Node->getOperand(5)); |
| 2801 | break; |
| 2802 | } |
| 2803 | |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2804 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 2805 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2806 | case TargetLowering::Custom: |
| 2807 | isCustom = true; |
| 2808 | // FALLTHROUGH |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 2809 | case TargetLowering::Legal: { |
| 2810 | SDOperand Ops[] = { Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6 }; |
| 2811 | Result = DAG.UpdateNodeOperands(Result, Ops, 6); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2812 | if (isCustom) { |
| 2813 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2814 | if (Tmp1.Val) Result = Tmp1; |
| 2815 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2816 | break; |
Rafael Espindola | 5c0d6ed | 2007-10-19 10:41:11 +0000 | [diff] [blame] | 2817 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2818 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2819 | // Otherwise, the target does not support this operation. Lower the |
| 2820 | // operation to an explicit libcall as appropriate. |
| 2821 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 2822 | const Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2823 | TargetLowering::ArgListTy Args; |
| 2824 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2825 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 2826 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2827 | if (Node->getOpcode() == ISD::MEMSET) { |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2828 | Entry.Node = Tmp2; Entry.Ty = IntPtrTy; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2829 | Args.push_back(Entry); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 2830 | // Extend the (previously legalized) ubyte argument to be an int value |
| 2831 | // for the call. |
| 2832 | if (Tmp3.getValueType() > MVT::i32) |
| 2833 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 2834 | else |
| 2835 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2836 | Entry.Node = Tmp3; Entry.Ty = Type::Int32Ty; Entry.isSExt = true; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2837 | Args.push_back(Entry); |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2838 | Entry.Node = Tmp4; Entry.Ty = IntPtrTy; Entry.isSExt = false; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2839 | Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2840 | |
| 2841 | FnName = "memset"; |
| 2842 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 2843 | Node->getOpcode() == ISD::MEMMOVE) { |
Anton Korobeynikov | b25fe82 | 2007-02-01 08:39:52 +0000 | [diff] [blame] | 2844 | Entry.Ty = IntPtrTy; |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 2845 | Entry.Node = Tmp2; Args.push_back(Entry); |
| 2846 | Entry.Node = Tmp3; Args.push_back(Entry); |
| 2847 | Entry.Node = Tmp4; Args.push_back(Entry); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2848 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 2849 | } else { |
| 2850 | assert(0 && "Unknown op!"); |
| 2851 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2852 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2853 | std::pair<SDOperand,SDOperand> CallResult = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2854 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2855 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2856 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2857 | break; |
| 2858 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 2859 | } |
| 2860 | break; |
| 2861 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2862 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2863 | case ISD::SHL_PARTS: |
| 2864 | case ISD::SRA_PARTS: |
| 2865 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2866 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2867 | bool Changed = false; |
| 2868 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2869 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2870 | Changed |= Ops.back() != Node->getOperand(i); |
| 2871 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2872 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2873 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2874 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2875 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2876 | Node->getValueType(0))) { |
| 2877 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2878 | case TargetLowering::Legal: break; |
| 2879 | case TargetLowering::Custom: |
| 2880 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2881 | if (Tmp1.Val) { |
| 2882 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2883 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2884 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2885 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2886 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2887 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2888 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2889 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2890 | return RetVal; |
| 2891 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2892 | break; |
| 2893 | } |
| 2894 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2895 | // Since these produce multiple values, make sure to remember that we |
| 2896 | // legalized all of them. |
| 2897 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2898 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2899 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2900 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2901 | |
| 2902 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2903 | case ISD::ADD: |
| 2904 | case ISD::SUB: |
| 2905 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2906 | case ISD::MULHS: |
| 2907 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2908 | case ISD::UDIV: |
| 2909 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2910 | case ISD::AND: |
| 2911 | case ISD::OR: |
| 2912 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2913 | case ISD::SHL: |
| 2914 | case ISD::SRL: |
| 2915 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2916 | case ISD::FADD: |
| 2917 | case ISD::FSUB: |
| 2918 | case ISD::FMUL: |
| 2919 | case ISD::FDIV: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2920 | case ISD::FPOW: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2921 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2922 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2923 | case Expand: assert(0 && "Not possible"); |
| 2924 | case Legal: |
| 2925 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2926 | break; |
| 2927 | case Promote: |
| 2928 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2929 | break; |
| 2930 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2931 | |
| 2932 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2933 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2934 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2935 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2936 | case TargetLowering::Legal: break; |
| 2937 | case TargetLowering::Custom: |
| 2938 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2939 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2940 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2941 | case TargetLowering::Expand: { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2942 | MVT::ValueType VT = Op.getValueType(); |
| 2943 | |
| 2944 | // See if multiply or divide can be lowered using two-result operations. |
| 2945 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 2946 | if (Node->getOpcode() == ISD::MUL) { |
| 2947 | // We just need the low half of the multiply; try both the signed |
| 2948 | // and unsigned forms. If the target supports both SMUL_LOHI and |
| 2949 | // UMUL_LOHI, form a preference by checking which forms of plain |
| 2950 | // MULH it supports. |
| 2951 | bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT); |
| 2952 | bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT); |
| 2953 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT); |
| 2954 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT); |
| 2955 | unsigned OpToUse = 0; |
| 2956 | if (HasSMUL_LOHI && !HasMULHS) { |
| 2957 | OpToUse = ISD::SMUL_LOHI; |
| 2958 | } else if (HasUMUL_LOHI && !HasMULHU) { |
| 2959 | OpToUse = ISD::UMUL_LOHI; |
| 2960 | } else if (HasSMUL_LOHI) { |
| 2961 | OpToUse = ISD::SMUL_LOHI; |
| 2962 | } else if (HasUMUL_LOHI) { |
| 2963 | OpToUse = ISD::UMUL_LOHI; |
| 2964 | } |
| 2965 | if (OpToUse) { |
| 2966 | Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0); |
| 2967 | break; |
| 2968 | } |
| 2969 | } |
| 2970 | if (Node->getOpcode() == ISD::MULHS && |
| 2971 | TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) { |
| 2972 | Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1); |
| 2973 | break; |
| 2974 | } |
| 2975 | if (Node->getOpcode() == ISD::MULHU && |
| 2976 | TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) { |
| 2977 | Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1); |
| 2978 | break; |
| 2979 | } |
| 2980 | if (Node->getOpcode() == ISD::SDIV && |
| 2981 | TLI.isOperationLegal(ISD::SDIVREM, VT)) { |
| 2982 | Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0); |
| 2983 | break; |
| 2984 | } |
| 2985 | if (Node->getOpcode() == ISD::UDIV && |
| 2986 | TLI.isOperationLegal(ISD::UDIVREM, VT)) { |
| 2987 | Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0); |
| 2988 | break; |
| 2989 | } |
| 2990 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2991 | // Check to see if we have a libcall for this operator. |
| 2992 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 2993 | bool isSigned = false; |
| 2994 | switch (Node->getOpcode()) { |
| 2995 | case ISD::UDIV: |
| 2996 | case ISD::SDIV: |
| 2997 | if (VT == MVT::i32) { |
| 2998 | LC = Node->getOpcode() == ISD::UDIV |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2999 | ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32; |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3000 | isSigned = Node->getOpcode() == ISD::SDIV; |
| 3001 | } |
| 3002 | break; |
| 3003 | case ISD::FPOW: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3004 | LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, |
| 3005 | RTLIB::POW_PPCF128); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3006 | break; |
| 3007 | default: break; |
| 3008 | } |
| 3009 | if (LC != RTLIB::UNKNOWN_LIBCALL) { |
| 3010 | SDOperand Dummy; |
| 3011 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | } |
| 3014 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 3015 | assert(MVT::isVector(Node->getValueType(0)) && |
| 3016 | "Cannot expand this binary operator!"); |
| 3017 | // Expand the operation into a bunch of nasty scalar code. |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3018 | Result = LegalizeOp(UnrollVectorOp(Op)); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 3019 | break; |
| 3020 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 3021 | case TargetLowering::Promote: { |
| 3022 | switch (Node->getOpcode()) { |
| 3023 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 3024 | case ISD::AND: |
| 3025 | case ISD::OR: |
| 3026 | case ISD::XOR: { |
| 3027 | MVT::ValueType OVT = Node->getValueType(0); |
| 3028 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 3029 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 3030 | // Bit convert each of the values to the new type. |
| 3031 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 3032 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 3033 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3034 | // Bit convert the result back the original type. |
| 3035 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 3036 | break; |
| 3037 | } |
| 3038 | } |
| 3039 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 3040 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3041 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3042 | |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3043 | case ISD::SMUL_LOHI: |
| 3044 | case ISD::UMUL_LOHI: |
| 3045 | case ISD::SDIVREM: |
| 3046 | case ISD::UDIVREM: |
| 3047 | // These nodes will only be produced by target-specific lowering, so |
| 3048 | // they shouldn't be here if they aren't legal. |
Duncan Sands | a7c97a7 | 2007-10-16 09:07:20 +0000 | [diff] [blame] | 3049 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3050 | "This must be legal!"); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3051 | |
| 3052 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3053 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 3054 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3055 | break; |
| 3056 | |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3057 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 3058 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3059 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3060 | case Expand: assert(0 && "Not possible"); |
| 3061 | case Legal: |
| 3062 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 3063 | break; |
| 3064 | case Promote: |
| 3065 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 3066 | break; |
| 3067 | } |
| 3068 | |
| 3069 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 3070 | |
| 3071 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 3072 | default: assert(0 && "Operation not supported"); |
| 3073 | case TargetLowering::Custom: |
| 3074 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3075 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 3076 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3077 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3078 | case TargetLowering::Expand: { |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 3079 | // If this target supports fabs/fneg natively and select is cheap, |
| 3080 | // do this efficiently. |
| 3081 | if (!TLI.isSelectExpensive() && |
| 3082 | TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) == |
| 3083 | TargetLowering::Legal && |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3084 | TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) == |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 3085 | TargetLowering::Legal) { |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 3086 | // Get the sign bit of the RHS. |
| 3087 | MVT::ValueType IVT = |
| 3088 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 3089 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 3090 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 3091 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 3092 | // Get the absolute value of the result. |
| 3093 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 3094 | // Select between the nabs and abs value based on the sign bit of |
| 3095 | // the input. |
| 3096 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 3097 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 3098 | AbsVal), |
| 3099 | AbsVal); |
| 3100 | Result = LegalizeOp(Result); |
| 3101 | break; |
| 3102 | } |
| 3103 | |
| 3104 | // Otherwise, do bitwise ops! |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3105 | MVT::ValueType NVT = |
| 3106 | Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64; |
| 3107 | Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 3108 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result); |
| 3109 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3110 | break; |
| 3111 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3112 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3113 | break; |
| 3114 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3115 | case ISD::ADDC: |
| 3116 | case ISD::SUBC: |
| 3117 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3118 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3119 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 3120 | // Since this produces two values, make sure to remember that we legalized |
| 3121 | // both of them. |
| 3122 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 3123 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 3124 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3125 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3126 | case ISD::ADDE: |
| 3127 | case ISD::SUBE: |
| 3128 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3129 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3130 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 3131 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 3132 | // Since this produces two values, make sure to remember that we legalized |
| 3133 | // both of them. |
| 3134 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 3135 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 3136 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3137 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3138 | case ISD::BUILD_PAIR: { |
| 3139 | MVT::ValueType PairTy = Node->getValueType(0); |
| 3140 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 3141 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 3142 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 3143 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3144 | case TargetLowering::Promote: |
| 3145 | case TargetLowering::Custom: |
| 3146 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3147 | case TargetLowering::Legal: |
| 3148 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 3149 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 3150 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3151 | case TargetLowering::Expand: |
| 3152 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 3153 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 3154 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 3155 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 3156 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3157 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3158 | break; |
| 3159 | } |
| 3160 | break; |
| 3161 | } |
| 3162 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3163 | case ISD::UREM: |
| 3164 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3165 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3166 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3167 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3168 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3169 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3170 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 3171 | case TargetLowering::Custom: |
| 3172 | isCustom = true; |
| 3173 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3174 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3175 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3176 | if (isCustom) { |
| 3177 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3178 | if (Tmp1.Val) Result = Tmp1; |
| 3179 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3180 | break; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3181 | case TargetLowering::Expand: { |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 3182 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3183 | bool isSigned = DivOpc == ISD::SDIV; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3184 | MVT::ValueType VT = Node->getValueType(0); |
| 3185 | |
| 3186 | // See if remainder can be lowered using two-result operations. |
| 3187 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 3188 | if (Node->getOpcode() == ISD::SREM && |
| 3189 | TLI.isOperationLegal(ISD::SDIVREM, VT)) { |
| 3190 | Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1); |
| 3191 | break; |
| 3192 | } |
| 3193 | if (Node->getOpcode() == ISD::UREM && |
| 3194 | TLI.isOperationLegal(ISD::UDIVREM, VT)) { |
| 3195 | Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1); |
| 3196 | break; |
| 3197 | } |
| 3198 | |
| 3199 | if (MVT::isInteger(VT)) { |
| 3200 | if (TLI.getOperationAction(DivOpc, VT) == |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3201 | TargetLowering::Legal) { |
| 3202 | // X % Y -> X-X/Y*Y |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 3203 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3204 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 3205 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3206 | } else if (MVT::isVector(VT)) { |
| 3207 | Result = LegalizeOp(UnrollVectorOp(Op)); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3208 | } else { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3209 | assert(VT == MVT::i32 && |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3210 | "Cannot expand this binary operator!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3211 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM |
| 3212 | ? RTLIB::UREM_I32 : RTLIB::SREM_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3213 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3214 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3215 | } |
Dan Gohman | 0d97426 | 2007-11-06 22:11:54 +0000 | [diff] [blame] | 3216 | } else { |
| 3217 | assert(MVT::isFloatingPoint(VT) && |
| 3218 | "remainder op must have integer or floating-point type"); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3219 | if (MVT::isVector(VT)) { |
| 3220 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3221 | } else { |
| 3222 | // Floating point mod -> fmod libcall. |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3223 | RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64, |
| 3224 | RTLIB::REM_F80, RTLIB::REM_PPCF128); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3225 | SDOperand Dummy; |
| 3226 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3227 | false/*sign irrelevant*/, Dummy); |
| 3228 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3229 | } |
| 3230 | break; |
| 3231 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3232 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3233 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3234 | case ISD::VAARG: { |
| 3235 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3236 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3237 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 3238 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3239 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 3240 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3241 | case TargetLowering::Custom: |
| 3242 | isCustom = true; |
| 3243 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3244 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3245 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 3246 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3247 | Tmp1 = Result.getValue(1); |
| 3248 | |
| 3249 | if (isCustom) { |
| 3250 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 3251 | if (Tmp2.Val) { |
| 3252 | Result = LegalizeOp(Tmp2); |
| 3253 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 3254 | } |
| 3255 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3256 | break; |
| 3257 | case TargetLowering::Expand: { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 3258 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
| 3259 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3260 | // Increment the pointer, VAList, to the next vaarg |
| 3261 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3262 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3263 | TLI.getPointerTy())); |
| 3264 | // Store the incremented VAList to the legalized pointer |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 3265 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3266 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3267 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3268 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3269 | Result = LegalizeOp(Result); |
| 3270 | break; |
| 3271 | } |
| 3272 | } |
| 3273 | // Since VAARG produces two values, make sure to remember that we |
| 3274 | // legalized both of them. |
| 3275 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3276 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 3277 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | case ISD::VACOPY: |
| 3281 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3282 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 3283 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 3284 | |
| 3285 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 3286 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3287 | case TargetLowering::Custom: |
| 3288 | isCustom = true; |
| 3289 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3290 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3291 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 3292 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3293 | if (isCustom) { |
| 3294 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3295 | if (Tmp1.Val) Result = Tmp1; |
| 3296 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3297 | break; |
| 3298 | case TargetLowering::Expand: |
| 3299 | // This defaults to loading a pointer from the input and storing it to the |
| 3300 | // output, returning the chain. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 3301 | const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue(); |
| 3302 | const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue(); |
| 3303 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0); |
| 3304 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | } |
| 3307 | break; |
| 3308 | |
| 3309 | case ISD::VAEND: |
| 3310 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3311 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3312 | |
| 3313 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 3314 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3315 | case TargetLowering::Custom: |
| 3316 | isCustom = true; |
| 3317 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3318 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3319 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3320 | if (isCustom) { |
| 3321 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 3322 | if (Tmp1.Val) Result = Tmp1; |
| 3323 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3324 | break; |
| 3325 | case TargetLowering::Expand: |
| 3326 | Result = Tmp1; // Default to a no-op, return the chain |
| 3327 | break; |
| 3328 | } |
| 3329 | break; |
| 3330 | |
| 3331 | case ISD::VASTART: |
| 3332 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3333 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3334 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3335 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 3336 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3337 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 3338 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3339 | case TargetLowering::Legal: break; |
| 3340 | case TargetLowering::Custom: |
| 3341 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3342 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3343 | break; |
| 3344 | } |
| 3345 | break; |
| 3346 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 3347 | case ISD::ROTL: |
| 3348 | case ISD::ROTR: |
| 3349 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3350 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3351 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3352 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 3353 | default: |
| 3354 | assert(0 && "ROTL/ROTR legalize operation not supported"); |
| 3355 | break; |
| 3356 | case TargetLowering::Legal: |
| 3357 | break; |
| 3358 | case TargetLowering::Custom: |
| 3359 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3360 | if (Tmp1.Val) Result = Tmp1; |
| 3361 | break; |
| 3362 | case TargetLowering::Promote: |
| 3363 | assert(0 && "Do not know how to promote ROTL/ROTR"); |
| 3364 | break; |
| 3365 | case TargetLowering::Expand: |
| 3366 | assert(0 && "Do not know how to expand ROTL/ROTR"); |
| 3367 | break; |
| 3368 | } |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 3369 | break; |
| 3370 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3371 | case ISD::BSWAP: |
| 3372 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 3373 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3374 | case TargetLowering::Custom: |
| 3375 | assert(0 && "Cannot custom legalize this yet!"); |
| 3376 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3377 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3378 | break; |
| 3379 | case TargetLowering::Promote: { |
| 3380 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 3381 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3382 | unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3383 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3384 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3385 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3386 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3387 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 3388 | break; |
| 3389 | } |
| 3390 | case TargetLowering::Expand: |
| 3391 | Result = ExpandBSWAP(Tmp1); |
| 3392 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3393 | } |
| 3394 | break; |
| 3395 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3396 | case ISD::CTPOP: |
| 3397 | case ISD::CTTZ: |
| 3398 | case ISD::CTLZ: |
| 3399 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 3400 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3401 | case TargetLowering::Custom: |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3402 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3403 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3404 | if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) == |
Scott Michel | 335f4f7 | 2007-08-02 02:22:46 +0000 | [diff] [blame] | 3405 | TargetLowering::Custom) { |
| 3406 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3407 | if (Tmp1.Val) { |
| 3408 | Result = Tmp1; |
| 3409 | } |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3410 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3411 | break; |
| 3412 | case TargetLowering::Promote: { |
| 3413 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 3414 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 3415 | |
| 3416 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3417 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3418 | // Perform the larger operation, then subtract if needed. |
| 3419 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3420 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3421 | case ISD::CTPOP: |
| 3422 | Result = Tmp1; |
| 3423 | break; |
| 3424 | case ISD::CTTZ: |
| 3425 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3426 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3427 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3428 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3429 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3430 | DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3431 | break; |
| 3432 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3433 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3434 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3435 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3436 | MVT::getSizeInBits(OVT), NVT)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3437 | break; |
| 3438 | } |
| 3439 | break; |
| 3440 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3441 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3442 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3443 | break; |
| 3444 | } |
| 3445 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3446 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3447 | // Unary operators |
| 3448 | case ISD::FABS: |
| 3449 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3450 | case ISD::FSQRT: |
| 3451 | case ISD::FSIN: |
| 3452 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3453 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3454 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3455 | case TargetLowering::Promote: |
| 3456 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3457 | isCustom = true; |
| 3458 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3459 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3460 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3461 | if (isCustom) { |
| 3462 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3463 | if (Tmp1.Val) Result = Tmp1; |
| 3464 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3465 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3466 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3467 | switch (Node->getOpcode()) { |
| 3468 | default: assert(0 && "Unreachable!"); |
| 3469 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3470 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 3471 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3472 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3473 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3474 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3475 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 3476 | MVT::ValueType VT = Node->getValueType(0); |
| 3477 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3478 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3479 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 3480 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | } |
| 3483 | case ISD::FSQRT: |
| 3484 | case ISD::FSIN: |
| 3485 | case ISD::FCOS: { |
| 3486 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3487 | |
| 3488 | // Expand unsupported unary vector operators by unrolling them. |
| 3489 | if (MVT::isVector(VT)) { |
| 3490 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3491 | break; |
| 3492 | } |
| 3493 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3494 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3495 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3496 | case ISD::FSQRT: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3497 | LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 3498 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3499 | break; |
| 3500 | case ISD::FSIN: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3501 | LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 3502 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3503 | break; |
| 3504 | case ISD::FCOS: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3505 | LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64, |
| 3506 | RTLIB::COS_F80, RTLIB::COS_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3507 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3508 | default: assert(0 && "Unreachable!"); |
| 3509 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 3510 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3511 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3512 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3513 | break; |
| 3514 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3515 | } |
| 3516 | break; |
| 3517 | } |
| 3518 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3519 | case ISD::FPOWI: { |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3520 | MVT::ValueType VT = Node->getValueType(0); |
| 3521 | |
| 3522 | // Expand unsupported unary vector operators by unrolling them. |
| 3523 | if (MVT::isVector(VT)) { |
| 3524 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3525 | break; |
| 3526 | } |
| 3527 | |
| 3528 | // We always lower FPOWI into a libcall. No target support for it yet. |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3529 | RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, |
| 3530 | RTLIB::POWI_F80, RTLIB::POWI_PPCF128); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3531 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3532 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3533 | false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3534 | break; |
| 3535 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3536 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3537 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 3538 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3539 | Node->getValueType(0)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3540 | } else if (MVT::isVector(Op.getOperand(0).getValueType())) { |
| 3541 | // The input has to be a vector type, we have to either scalarize it, pack |
| 3542 | // it, or convert it based on whether the input vector type is legal. |
| 3543 | SDNode *InVal = Node->getOperand(0).Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 3544 | int InIx = Node->getOperand(0).ResNo; |
| 3545 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx)); |
| 3546 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3547 | |
| 3548 | // Figure out if there is a simple type corresponding to this Vector |
| 3549 | // type. If so, convert to the vector type. |
| 3550 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3551 | if (TLI.isTypeLegal(TVT)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 3552 | // Turn this into a bit convert of the vector input. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3553 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3554 | LegalizeOp(Node->getOperand(0))); |
| 3555 | break; |
| 3556 | } else if (NumElems == 1) { |
| 3557 | // Turn this into a bit convert of the scalar input. |
| 3558 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3559 | ScalarizeVectorOp(Node->getOperand(0))); |
| 3560 | break; |
| 3561 | } else { |
| 3562 | // FIXME: UNIMP! Store then reload |
| 3563 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 3564 | } |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3565 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3566 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 3567 | Node->getOperand(0).getValueType())) { |
| 3568 | default: assert(0 && "Unknown operation action!"); |
| 3569 | case TargetLowering::Expand: |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 3570 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3571 | Node->getValueType(0)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3572 | break; |
| 3573 | case TargetLowering::Legal: |
| 3574 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3575 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3576 | break; |
| 3577 | } |
| 3578 | } |
| 3579 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 3580 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3581 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 3582 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3583 | case ISD::UINT_TO_FP: { |
| 3584 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3585 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3586 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3587 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3588 | Node->getOperand(0).getValueType())) { |
| 3589 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3590 | case TargetLowering::Custom: |
| 3591 | isCustom = true; |
| 3592 | // FALLTHROUGH |
| 3593 | case TargetLowering::Legal: |
| 3594 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3595 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3596 | if (isCustom) { |
| 3597 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3598 | if (Tmp1.Val) Result = Tmp1; |
| 3599 | } |
| 3600 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3601 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 3602 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 3603 | LegalizeOp(Node->getOperand(0)), |
| 3604 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3605 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3606 | case TargetLowering::Promote: |
| 3607 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 3608 | Node->getValueType(0), |
| 3609 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3610 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 3611 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3612 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 3613 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3614 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 3615 | Node->getValueType(0), Node->getOperand(0)); |
| 3616 | break; |
| 3617 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3618 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3619 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3620 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 3621 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3622 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3623 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 3624 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3625 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3626 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3627 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3628 | break; |
| 3629 | } |
| 3630 | break; |
| 3631 | } |
| 3632 | case ISD::TRUNCATE: |
| 3633 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3634 | case Legal: |
| 3635 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3636 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3637 | break; |
| 3638 | case Expand: |
| 3639 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3640 | |
| 3641 | // Since the result is legal, we should just be able to truncate the low |
| 3642 | // part of the source. |
| 3643 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 3644 | break; |
| 3645 | case Promote: |
| 3646 | Result = PromoteOp(Node->getOperand(0)); |
| 3647 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 3648 | break; |
| 3649 | } |
| 3650 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3651 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3652 | case ISD::FP_TO_SINT: |
| 3653 | case ISD::FP_TO_UINT: |
| 3654 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3655 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 3656 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3657 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3658 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 3659 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3660 | case TargetLowering::Custom: |
| 3661 | isCustom = true; |
| 3662 | // FALLTHROUGH |
| 3663 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3664 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3665 | if (isCustom) { |
| 3666 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3667 | if (Tmp1.Val) Result = Tmp1; |
| 3668 | } |
| 3669 | break; |
| 3670 | case TargetLowering::Promote: |
| 3671 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 3672 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 3673 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3674 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3675 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 3676 | SDOperand True, False; |
| 3677 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 3678 | MVT::ValueType NVT = Node->getValueType(0); |
Dale Johannesen | f4d4832 | 2007-09-19 17:53:26 +0000 | [diff] [blame] | 3679 | unsigned ShiftAmt = MVT::getSizeInBits(NVT)-1; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3680 | const uint64_t zero[] = {0, 0}; |
| 3681 | APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero)); |
| 3682 | uint64_t x = 1ULL << ShiftAmt; |
Neil Booth | ccf596a | 2007-10-07 11:45:55 +0000 | [diff] [blame] | 3683 | (void)apf.convertFromZeroExtendedInteger |
| 3684 | (&x, MVT::getSizeInBits(NVT), false, APFloat::rmNearestTiesToEven); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3685 | Tmp2 = DAG.getConstantFP(apf, VT); |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3686 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 3687 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 3688 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 3689 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3690 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3691 | Tmp2)); |
| 3692 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 3693 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3694 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 3695 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3696 | } else { |
| 3697 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 3698 | } |
| 3699 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3700 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3701 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3702 | case Expand: { |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3703 | MVT::ValueType VT = Op.getValueType(); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3704 | MVT::ValueType OVT = Node->getOperand(0).getValueType(); |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 3705 | // Convert ppcf128 to i32 |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3706 | if (OVT == MVT::ppcf128 && VT == MVT::i32) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3707 | if (Node->getOpcode() == ISD::FP_TO_SINT) { |
| 3708 | Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128, |
| 3709 | Node->getOperand(0), DAG.getValueType(MVT::f64)); |
| 3710 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result, |
| 3711 | DAG.getIntPtrConstant(1)); |
| 3712 | Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result); |
| 3713 | } else { |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 3714 | const uint64_t TwoE31[] = {0x41e0000000000000LL, 0}; |
| 3715 | APFloat apf = APFloat(APInt(128, 2, TwoE31)); |
| 3716 | Tmp2 = DAG.getConstantFP(apf, OVT); |
| 3717 | // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X |
| 3718 | // FIXME: generated code sucks. |
| 3719 | Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2, |
| 3720 | DAG.getNode(ISD::ADD, MVT::i32, |
| 3721 | DAG.getNode(ISD::FP_TO_SINT, VT, |
| 3722 | DAG.getNode(ISD::FSUB, OVT, |
| 3723 | Node->getOperand(0), Tmp2)), |
| 3724 | DAG.getConstant(0x80000000, MVT::i32)), |
| 3725 | DAG.getNode(ISD::FP_TO_SINT, VT, |
| 3726 | Node->getOperand(0)), |
| 3727 | DAG.getCondCode(ISD::SETGE)); |
| 3728 | } |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3729 | break; |
| 3730 | } |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 3731 | // Convert f32 / f64 to i32 / i64. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3732 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3733 | switch (Node->getOpcode()) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3734 | case ISD::FP_TO_SINT: { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3735 | if (OVT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3736 | LC = (VT == MVT::i32) |
| 3737 | ? RTLIB::FPTOSINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3738 | else if (OVT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3739 | LC = (VT == MVT::i32) |
| 3740 | ? RTLIB::FPTOSINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 3741 | else if (OVT == MVT::f80) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3742 | assert(VT == MVT::i64); |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 3743 | LC = RTLIB::FPTOSINT_F80_I64; |
| 3744 | } |
| 3745 | else if (OVT == MVT::ppcf128) { |
| 3746 | assert(VT == MVT::i64); |
| 3747 | LC = RTLIB::FPTOSINT_PPCF128_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3748 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3749 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3750 | } |
| 3751 | case ISD::FP_TO_UINT: { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3752 | if (OVT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3753 | LC = (VT == MVT::i32) |
| 3754 | ? RTLIB::FPTOUINT_F32_I32 : RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3755 | else if (OVT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3756 | LC = (VT == MVT::i32) |
| 3757 | ? RTLIB::FPTOUINT_F64_I32 : RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 3758 | else if (OVT == MVT::f80) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3759 | LC = (VT == MVT::i32) |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 3760 | ? RTLIB::FPTOUINT_F80_I32 : RTLIB::FPTOUINT_F80_I64; |
| 3761 | } |
| 3762 | else if (OVT == MVT::ppcf128) { |
| 3763 | assert(VT == MVT::i64); |
| 3764 | LC = RTLIB::FPTOUINT_PPCF128_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3765 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3766 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3767 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3768 | default: assert(0 && "Unreachable!"); |
| 3769 | } |
| 3770 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3771 | Result = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 3772 | false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3773 | break; |
| 3774 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3775 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3776 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3777 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 3778 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3779 | break; |
| 3780 | } |
| 3781 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3782 | |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3783 | case ISD::FP_EXTEND: { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3784 | MVT::ValueType DstVT = Op.getValueType(); |
| 3785 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3786 | if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) { |
| 3787 | // The only other way we can lower this is to turn it into a STORE, |
| 3788 | // LOAD pair, targetting a temporary location (a stack slot). |
| 3789 | Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT); |
| 3790 | break; |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3791 | } |
| 3792 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3793 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
| 3794 | case Legal: |
| 3795 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3796 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3797 | break; |
| 3798 | case Promote: |
| 3799 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3800 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1); |
| 3801 | break; |
| 3802 | } |
| 3803 | break; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3804 | } |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3805 | case ISD::FP_ROUND: { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3806 | MVT::ValueType DstVT = Op.getValueType(); |
| 3807 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3808 | if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) { |
| 3809 | if (SrcVT == MVT::ppcf128) { |
Dale Johannesen | 713ed3f | 2008-01-20 01:18:38 +0000 | [diff] [blame] | 3810 | SDOperand Lo; |
| 3811 | ExpandOp(Node->getOperand(0), Lo, Result); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3812 | // Round it the rest of the way (e.g. to f32) if needed. |
Dale Johannesen | 713ed3f | 2008-01-20 01:18:38 +0000 | [diff] [blame] | 3813 | if (DstVT!=MVT::f64) |
| 3814 | Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3815 | break; |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3816 | } |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3817 | // The only other way we can lower this is to turn it into a STORE, |
| 3818 | // LOAD pair, targetting a temporary location (a stack slot). |
| 3819 | Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT); |
| 3820 | break; |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 3821 | } |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3822 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3823 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
| 3824 | case Legal: |
| 3825 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3826 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3827 | break; |
| 3828 | case Promote: |
| 3829 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3830 | Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1, |
| 3831 | Node->getOperand(1)); |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3832 | break; |
| 3833 | } |
| 3834 | break; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3835 | } |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3836 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3837 | case ISD::ZERO_EXTEND: |
| 3838 | case ISD::SIGN_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3839 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3840 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3841 | case Legal: |
| 3842 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3843 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3844 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3845 | case Promote: |
| 3846 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3847 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3848 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3849 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3850 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3851 | case ISD::ZERO_EXTEND: |
| 3852 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3853 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3854 | Result = DAG.getZeroExtendInReg(Result, |
| 3855 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3856 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3857 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3858 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3859 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3860 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3861 | Result, |
| 3862 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3863 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3864 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3865 | } |
| 3866 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3867 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3868 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3869 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3870 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3871 | |
| 3872 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 3873 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3874 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 3875 | default: assert(0 && "This action not supported for this op yet!"); |
| 3876 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3877 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3878 | break; |
| 3879 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3880 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3881 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3882 | // NOTE: we could fall back on load/store here too for targets without |
| 3883 | // SAR. However, it is doubtful that any exist. |
| 3884 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 3885 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 3886 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3887 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 3888 | Node->getOperand(0), ShiftCst); |
| 3889 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 3890 | Result, ShiftCst); |
| 3891 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 3892 | // 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] | 3893 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3894 | |
| 3895 | // NOTE: there is a choice here between constantly creating new stack |
| 3896 | // slots and always reusing the same one. We currently always create |
| 3897 | // new ones, as reuse may inhibit scheduling. |
Chris Lattner | a66bb39 | 2008-01-16 07:51:34 +0000 | [diff] [blame] | 3898 | Result = EmitStackConvert(Node->getOperand(0), ExtraVT, |
| 3899 | Node->getValueType(0)); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3900 | } else { |
| 3901 | assert(0 && "Unknown op"); |
| 3902 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3903 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3904 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3905 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3906 | } |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3907 | case ISD::TRAMPOLINE: { |
| 3908 | SDOperand Ops[6]; |
| 3909 | for (unsigned i = 0; i != 6; ++i) |
| 3910 | Ops[i] = LegalizeOp(Node->getOperand(i)); |
| 3911 | Result = DAG.UpdateNodeOperands(Result, Ops, 6); |
| 3912 | // The only option for this node is to custom lower it. |
| 3913 | Result = TLI.LowerOperation(Result, DAG); |
| 3914 | assert(Result.Val && "Should always custom lower!"); |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 3915 | |
| 3916 | // Since trampoline produces two values, make sure to remember that we |
| 3917 | // legalized both of them. |
| 3918 | Tmp1 = LegalizeOp(Result.getValue(1)); |
| 3919 | Result = LegalizeOp(Result); |
| 3920 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 3921 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 3922 | return Op.ResNo ? Tmp1 : Result; |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3923 | } |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 3924 | case ISD::FLT_ROUNDS_: { |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 3925 | MVT::ValueType VT = Node->getValueType(0); |
| 3926 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 3927 | default: assert(0 && "This action not supported for this op yet!"); |
| 3928 | case TargetLowering::Custom: |
| 3929 | Result = TLI.LowerOperation(Op, DAG); |
| 3930 | if (Result.Val) break; |
| 3931 | // Fall Thru |
| 3932 | case TargetLowering::Legal: |
| 3933 | // If this operation is not supported, lower it to constant 1 |
| 3934 | Result = DAG.getConstant(1, VT); |
| 3935 | break; |
| 3936 | } |
| 3937 | } |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3938 | case ISD::TRAP: { |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3939 | MVT::ValueType VT = Node->getValueType(0); |
| 3940 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 3941 | default: assert(0 && "This action not supported for this op yet!"); |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3942 | case TargetLowering::Legal: |
| 3943 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3944 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3945 | break; |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3946 | case TargetLowering::Custom: |
| 3947 | Result = TLI.LowerOperation(Op, DAG); |
| 3948 | if (Result.Val) break; |
| 3949 | // Fall Thru |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3950 | case TargetLowering::Expand: |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3951 | // If this operation is not supported, lower it to 'abort()' call |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3952 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3953 | TargetLowering::ArgListTy Args; |
| 3954 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3955 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, false, CallingConv::C, false, |
Chris Lattner | 034f12e | 2008-01-15 22:09:33 +0000 | [diff] [blame] | 3956 | DAG.getExternalSymbol("abort", TLI.getPointerTy()), |
| 3957 | Args, DAG); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3958 | Result = CallResult.second; |
| 3959 | break; |
| 3960 | } |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3961 | break; |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3962 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3963 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3964 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3965 | assert(Result.getValueType() == Op.getValueType() && |
| 3966 | "Bad legalization!"); |
| 3967 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3968 | // Make sure that the generated code is itself legal. |
| 3969 | if (Result != Op) |
| 3970 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3971 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3972 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3973 | // means that we always must cache transformed nodes. |
| 3974 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3975 | return Result; |
| 3976 | } |
| 3977 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 3978 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 3979 | /// promote it to compute the value into a larger type. The produced value will |
| 3980 | /// have the correct bits for the low portion of the register, but no guarantee |
| 3981 | /// 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] | 3982 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 3983 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3984 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3985 | assert(getTypeAction(VT) == Promote && |
| 3986 | "Caller should expand or legalize operands that are not promotable!"); |
| 3987 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 3988 | "Cannot promote to smaller type!"); |
| 3989 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3990 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3991 | SDOperand Result; |
| 3992 | SDNode *Node = Op.Val; |
| 3993 | |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 3994 | DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3995 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3996 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3997 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 3998 | case ISD::CopyFromReg: |
| 3999 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4000 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4001 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 4002 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4003 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4004 | assert(0 && "Do not know how to promote this operator!"); |
| 4005 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4006 | case ISD::UNDEF: |
| 4007 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 4008 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4009 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 4010 | if (VT != MVT::i1) |
| 4011 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 4012 | else |
| 4013 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4014 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 4015 | break; |
| 4016 | case ISD::ConstantFP: |
| 4017 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 4018 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 4019 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 4020 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 4021 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4022 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 4023 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 4024 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 4025 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4026 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4027 | case ISD::TRUNCATE: |
| 4028 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4029 | case Legal: |
| 4030 | Result = LegalizeOp(Node->getOperand(0)); |
| 4031 | assert(Result.getValueType() >= NVT && |
| 4032 | "This truncation doesn't make sense!"); |
| 4033 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 4034 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 4035 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 4036 | case Promote: |
| 4037 | // The truncation is not required, because we don't guarantee anything |
| 4038 | // about high bits anyway. |
| 4039 | Result = PromoteOp(Node->getOperand(0)); |
| 4040 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4041 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 4042 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 4043 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 4044 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4045 | } |
| 4046 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4047 | case ISD::SIGN_EXTEND: |
| 4048 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4049 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4050 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4051 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 4052 | case Legal: |
| 4053 | // 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] | 4054 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4055 | break; |
| 4056 | case Promote: |
| 4057 | // Promote the reg if it's smaller. |
| 4058 | Result = PromoteOp(Node->getOperand(0)); |
| 4059 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 4060 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 4061 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4062 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4063 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4064 | Result = DAG.getZeroExtendInReg(Result, |
| 4065 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4066 | break; |
| 4067 | } |
| 4068 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4069 | case ISD::BIT_CONVERT: |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4070 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 4071 | Node->getValueType(0)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4072 | Result = PromoteOp(Result); |
| 4073 | break; |
| 4074 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4075 | case ISD::FP_EXTEND: |
| 4076 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 4077 | case ISD::FP_ROUND: |
| 4078 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4079 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 4080 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 4081 | case Legal: |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4082 | if (Node->getConstantOperandVal(1) == 0) { |
| 4083 | // Input is legal? Do an FP_ROUND_INREG. |
| 4084 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
| 4085 | DAG.getValueType(VT)); |
| 4086 | } else { |
| 4087 | // Just remove the truncate, it isn't affecting the value. |
| 4088 | Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0), |
| 4089 | Node->getOperand(1)); |
| 4090 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4091 | break; |
| 4092 | } |
| 4093 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4094 | case ISD::SINT_TO_FP: |
| 4095 | case ISD::UINT_TO_FP: |
| 4096 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4097 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4098 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4099 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4100 | break; |
| 4101 | |
| 4102 | case Promote: |
| 4103 | Result = PromoteOp(Node->getOperand(0)); |
| 4104 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 4105 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4106 | Result, |
| 4107 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4108 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4109 | Result = DAG.getZeroExtendInReg(Result, |
| 4110 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4111 | // No extra round required here. |
| 4112 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4113 | break; |
| 4114 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4115 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 4116 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4117 | // Round if we cannot tolerate excess precision. |
| 4118 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4119 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4120 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4121 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4122 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4123 | break; |
| 4124 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 4125 | case ISD::SIGN_EXTEND_INREG: |
| 4126 | Result = PromoteOp(Node->getOperand(0)); |
| 4127 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 4128 | Node->getOperand(1)); |
| 4129 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4130 | case ISD::FP_TO_SINT: |
| 4131 | case ISD::FP_TO_UINT: |
| 4132 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4133 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 4134 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4135 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4136 | break; |
| 4137 | case Promote: |
| 4138 | // The input result is prerounded, so we don't have to do anything |
| 4139 | // special. |
| 4140 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4141 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4142 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 4143 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 4144 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 4145 | // we can use that instead. This allows us to generate better code for |
| 4146 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 4147 | // legal, such as PowerPC. |
| 4148 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4149 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 4150 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 4151 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 4152 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 4153 | } else { |
| 4154 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 4155 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4156 | break; |
| 4157 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 4158 | case ISD::FABS: |
| 4159 | case ISD::FNEG: |
| 4160 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4161 | assert(Tmp1.getValueType() == NVT); |
| 4162 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 4163 | // NOTE: we do not have to do any extra rounding here for |
| 4164 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 4165 | // precision, and these operations don't modify precision at all. |
| 4166 | break; |
| 4167 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 4168 | case ISD::FSQRT: |
| 4169 | case ISD::FSIN: |
| 4170 | case ISD::FCOS: |
| 4171 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4172 | assert(Tmp1.getValueType() == NVT); |
| 4173 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4174 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4175 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4176 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 4177 | break; |
| 4178 | |
Chris Lattner | 8b2d42c | 2007-03-03 23:43:21 +0000 | [diff] [blame] | 4179 | case ISD::FPOWI: { |
| 4180 | // Promote f32 powi to f64 powi. Note that this could insert a libcall |
| 4181 | // directly as well, which may be better. |
| 4182 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4183 | assert(Tmp1.getValueType() == NVT); |
| 4184 | Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1)); |
| 4185 | if (NoExcessFPPrecision) |
| 4186 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4187 | DAG.getValueType(VT)); |
| 4188 | break; |
| 4189 | } |
| 4190 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4191 | case ISD::AND: |
| 4192 | case ISD::OR: |
| 4193 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4194 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4195 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4196 | case ISD::MUL: |
| 4197 | // 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] | 4198 | // 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] | 4199 | // that too is okay if they are integer operations. |
| 4200 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4201 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4202 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 4203 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4204 | break; |
| 4205 | case ISD::FADD: |
| 4206 | case ISD::FSUB: |
| 4207 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4208 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4209 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4210 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 4211 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4212 | |
| 4213 | // Floating point operations will give excess precision that we may not be |
| 4214 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 4215 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4216 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 4217 | // 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] | 4218 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4219 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4220 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4221 | break; |
| 4222 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4223 | case ISD::SDIV: |
| 4224 | case ISD::SREM: |
| 4225 | // These operators require that their input be sign extended. |
| 4226 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4227 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4228 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4229 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4230 | DAG.getValueType(VT)); |
| 4231 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 4232 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4233 | } |
| 4234 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4235 | |
| 4236 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 4237 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4238 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4239 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4240 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4241 | case ISD::FDIV: |
| 4242 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 4243 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4244 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4245 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4246 | case Expand: assert(0 && "not implemented"); |
| 4247 | case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break; |
| 4248 | case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break; |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4249 | } |
| 4250 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4251 | case Expand: assert(0 && "not implemented"); |
| 4252 | case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break; |
| 4253 | case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break; |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4254 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4255 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4256 | |
| 4257 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 4258 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4259 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4260 | DAG.getValueType(VT)); |
| 4261 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4262 | |
| 4263 | case ISD::UDIV: |
| 4264 | case ISD::UREM: |
| 4265 | // These operators require that their input be zero extended. |
| 4266 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4267 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4268 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4269 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 4270 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4271 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4272 | break; |
| 4273 | |
| 4274 | case ISD::SHL: |
| 4275 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4276 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4277 | break; |
| 4278 | case ISD::SRA: |
| 4279 | // The input value must be properly sign extended. |
| 4280 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4281 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4282 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4283 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4284 | break; |
| 4285 | case ISD::SRL: |
| 4286 | // The input value must be properly zero extended. |
| 4287 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4288 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4289 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4290 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4291 | |
| 4292 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4293 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 4294 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4295 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 4296 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 4297 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 4298 | } else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4299 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
| 4300 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4301 | // Increment the pointer, VAList, to the next vaarg |
| 4302 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 4303 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 4304 | TLI.getPointerTy())); |
| 4305 | // Store the incremented VAList to the legalized pointer |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4306 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4307 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4308 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4309 | } |
| 4310 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4311 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4312 | break; |
| 4313 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4314 | case ISD::LOAD: { |
| 4315 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 4316 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 4317 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 4318 | Result = DAG.getExtLoad(ExtType, NVT, |
| 4319 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 4320 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 4321 | LD->getMemoryVT(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 4322 | LD->isVolatile(), |
| 4323 | LD->getAlignment()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4324 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4325 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4326 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4327 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4328 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4329 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 4330 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4331 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4332 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4333 | case ISD::SELECT_CC: |
| 4334 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 4335 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 4336 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4337 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4338 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4339 | case ISD::BSWAP: |
| 4340 | Tmp1 = Node->getOperand(0); |
| 4341 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 4342 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 4343 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4344 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 4345 | MVT::getSizeInBits(VT), |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4346 | TLI.getShiftAmountTy())); |
| 4347 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4348 | case ISD::CTPOP: |
| 4349 | case ISD::CTTZ: |
| 4350 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4351 | // Zero extend the argument |
| 4352 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4353 | // Perform the larger operation, then subtract if needed. |
| 4354 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4355 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4356 | case ISD::CTPOP: |
| 4357 | Result = Tmp1; |
| 4358 | break; |
| 4359 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4360 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 4361 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4362 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
| 4363 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4364 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4365 | DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4366 | break; |
| 4367 | case ISD::CTLZ: |
| 4368 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4369 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4370 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 4371 | MVT::getSizeInBits(VT), NVT)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4372 | break; |
| 4373 | } |
| 4374 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4375 | case ISD::EXTRACT_SUBVECTOR: |
| 4376 | Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op)); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4377 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 4378 | case ISD::EXTRACT_VECTOR_ELT: |
| 4379 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 4380 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4384 | |
| 4385 | // Make sure the result is itself legal. |
| 4386 | Result = LegalizeOp(Result); |
| 4387 | |
| 4388 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4389 | AddPromotedOperand(Op, Result); |
| 4390 | return Result; |
| 4391 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4392 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4393 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 4394 | /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic, |
| 4395 | /// based on the vector type. The return type of this matches the element type |
| 4396 | /// of the vector, which may not be legal for the target. |
| 4397 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4398 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 4399 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 4400 | // lower to a store then an indexed load. |
| 4401 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4402 | SDOperand Idx = Op.getOperand(1); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4403 | |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 4404 | MVT::ValueType TVT = Vec.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4405 | unsigned NumElems = MVT::getVectorNumElements(TVT); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4406 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4407 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) { |
| 4408 | default: assert(0 && "This action is not supported yet!"); |
| 4409 | case TargetLowering::Custom: { |
| 4410 | Vec = LegalizeOp(Vec); |
| 4411 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 4412 | SDOperand Tmp3 = TLI.LowerOperation(Op, DAG); |
| 4413 | if (Tmp3.Val) |
| 4414 | return Tmp3; |
| 4415 | break; |
| 4416 | } |
| 4417 | case TargetLowering::Legal: |
| 4418 | if (isTypeLegal(TVT)) { |
| 4419 | Vec = LegalizeOp(Vec); |
| 4420 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Christopher Lamb | 844228a | 2007-07-26 03:33:13 +0000 | [diff] [blame] | 4421 | return Op; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4422 | } |
| 4423 | break; |
| 4424 | case TargetLowering::Expand: |
| 4425 | break; |
| 4426 | } |
| 4427 | |
| 4428 | if (NumElems == 1) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4429 | // This must be an access of the only element. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4430 | Op = ScalarizeVectorOp(Vec); |
| 4431 | } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) { |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4432 | unsigned NumLoElts = 1 << Log2_32(NumElems-1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4433 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4434 | SDOperand Lo, Hi; |
| 4435 | SplitVectorOp(Vec, Lo, Hi); |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4436 | if (CIdx->getValue() < NumLoElts) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4437 | Vec = Lo; |
| 4438 | } else { |
| 4439 | Vec = Hi; |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4440 | Idx = DAG.getConstant(CIdx->getValue() - NumLoElts, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4441 | Idx.getValueType()); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4442 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4443 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4444 | // It's now an extract from the appropriate high or low part. Recurse. |
| 4445 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4446 | Op = ExpandEXTRACT_VECTOR_ELT(Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4447 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4448 | // Store the value to a temporary stack slot, then LOAD the scalar |
| 4449 | // element back out. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4450 | SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4451 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0); |
| 4452 | |
| 4453 | // Add the offset to the index. |
| 4454 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 4455 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 4456 | DAG.getConstant(EltSize, Idx.getValueType())); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4457 | |
| 4458 | if (MVT::getSizeInBits(Idx.getValueType()) > |
| 4459 | MVT::getSizeInBits(TLI.getPointerTy())) |
Chris Lattner | f185e67 | 2007-10-19 16:47:35 +0000 | [diff] [blame] | 4460 | Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4461 | else |
Chris Lattner | f185e67 | 2007-10-19 16:47:35 +0000 | [diff] [blame] | 4462 | Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4463 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4464 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 4465 | |
| 4466 | Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4467 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4468 | return Op; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4471 | /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4472 | /// 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] | 4473 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) { |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4474 | // We know that operand #0 is the Vec vector. For now we assume the index |
| 4475 | // is a constant and that the extracted result is a supported hardware type. |
| 4476 | SDOperand Vec = Op.getOperand(0); |
| 4477 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 4478 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4479 | unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4480 | |
| 4481 | if (NumElems == MVT::getVectorNumElements(Op.getValueType())) { |
| 4482 | // This must be an access of the desired vector length. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4483 | return Vec; |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
| 4487 | SDOperand Lo, Hi; |
| 4488 | SplitVectorOp(Vec, Lo, Hi); |
| 4489 | if (CIdx->getValue() < NumElems/2) { |
| 4490 | Vec = Lo; |
| 4491 | } else { |
| 4492 | Vec = Hi; |
| 4493 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 4494 | } |
| 4495 | |
| 4496 | // It's now an extract from the appropriate high or low part. Recurse. |
| 4497 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4498 | return ExpandEXTRACT_SUBVECTOR(Op); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4501 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 4502 | /// with condition CC on the current target. This usually involves legalizing |
| 4503 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 4504 | /// there may be no choice but to create a new SetCC node to represent the |
| 4505 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 4506 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 4507 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 4508 | SDOperand &RHS, |
| 4509 | SDOperand &CC) { |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4510 | SDOperand Tmp1, Tmp2, Tmp3, Result; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4511 | |
| 4512 | switch (getTypeAction(LHS.getValueType())) { |
| 4513 | case Legal: |
| 4514 | Tmp1 = LegalizeOp(LHS); // LHS |
| 4515 | Tmp2 = LegalizeOp(RHS); // RHS |
| 4516 | break; |
| 4517 | case Promote: |
| 4518 | Tmp1 = PromoteOp(LHS); // LHS |
| 4519 | Tmp2 = PromoteOp(RHS); // RHS |
| 4520 | |
| 4521 | // If this is an FP compare, the operands have already been extended. |
| 4522 | if (MVT::isInteger(LHS.getValueType())) { |
| 4523 | MVT::ValueType VT = LHS.getValueType(); |
| 4524 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 4525 | |
| 4526 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 4527 | // that we could insert sign extends for ALL conditions, but zero extend |
| 4528 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 4529 | // it. |
| 4530 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4531 | default: assert(0 && "Unknown integer comparison!"); |
| 4532 | case ISD::SETEQ: |
| 4533 | case ISD::SETNE: |
| 4534 | case ISD::SETUGE: |
| 4535 | case ISD::SETUGT: |
| 4536 | case ISD::SETULE: |
| 4537 | case ISD::SETULT: |
| 4538 | // ALL of these operations will work if we either sign or zero extend |
| 4539 | // the operands (including the unsigned comparisons!). Zero extend is |
| 4540 | // usually a simpler/cheaper operation, so prefer it. |
| 4541 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 4542 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 4543 | break; |
| 4544 | case ISD::SETGE: |
| 4545 | case ISD::SETGT: |
| 4546 | case ISD::SETLT: |
| 4547 | case ISD::SETLE: |
| 4548 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4549 | DAG.getValueType(VT)); |
| 4550 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 4551 | DAG.getValueType(VT)); |
| 4552 | break; |
| 4553 | } |
| 4554 | } |
| 4555 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4556 | case Expand: { |
| 4557 | MVT::ValueType VT = LHS.getValueType(); |
| 4558 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4559 | // Expand into one or more soft-fp libcall(s). |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4560 | RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4561 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4562 | case ISD::SETEQ: |
| 4563 | case ISD::SETOEQ: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4564 | LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4565 | break; |
| 4566 | case ISD::SETNE: |
| 4567 | case ISD::SETUNE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4568 | LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4569 | break; |
| 4570 | case ISD::SETGE: |
| 4571 | case ISD::SETOGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4572 | LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4573 | break; |
| 4574 | case ISD::SETLT: |
| 4575 | case ISD::SETOLT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4576 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4577 | break; |
| 4578 | case ISD::SETLE: |
| 4579 | case ISD::SETOLE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4580 | LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4581 | break; |
| 4582 | case ISD::SETGT: |
| 4583 | case ISD::SETOGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4584 | LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4585 | break; |
| 4586 | case ISD::SETUO: |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4587 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
| 4588 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4589 | case ISD::SETO: |
Evan Cheng | 5efdecc | 2007-02-03 00:43:46 +0000 | [diff] [blame] | 4590 | LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4591 | break; |
| 4592 | default: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4593 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4594 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4595 | case ISD::SETONE: |
| 4596 | // SETONE = SETOLT | SETOGT |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4597 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4598 | // Fallthrough |
| 4599 | case ISD::SETUGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4600 | LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4601 | break; |
| 4602 | case ISD::SETUGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4603 | LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4604 | break; |
| 4605 | case ISD::SETULT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4606 | LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4607 | break; |
| 4608 | case ISD::SETULE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4609 | LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4610 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4611 | case ISD::SETUEQ: |
| 4612 | LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4613 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4614 | default: assert(0 && "Unsupported FP setcc!"); |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | SDOperand Dummy; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4619 | Tmp1 = ExpandLibCall(TLI.getLibcallName(LC1), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4620 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4621 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4622 | Tmp2 = DAG.getConstant(0, MVT::i32); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4623 | CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1)); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4624 | if (LC2 != RTLIB::UNKNOWN_LIBCALL) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4625 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), Tmp1, Tmp2, CC); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4626 | LHS = ExpandLibCall(TLI.getLibcallName(LC2), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4627 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4628 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4629 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHS, Tmp2, |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4630 | DAG.getCondCode(TLI.getCmpLibcallCC(LC2))); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4631 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4632 | Tmp2 = SDOperand(); |
| 4633 | } |
| 4634 | LHS = Tmp1; |
| 4635 | RHS = Tmp2; |
| 4636 | return; |
| 4637 | } |
| 4638 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4639 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 4640 | ExpandOp(LHS, LHSLo, LHSHi); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4641 | ExpandOp(RHS, RHSLo, RHSHi); |
| 4642 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 4643 | |
| 4644 | if (VT==MVT::ppcf128) { |
| 4645 | // FIXME: This generated code sucks. We want to generate |
| 4646 | // FCMP crN, hi1, hi2 |
| 4647 | // BNE crN, L: |
| 4648 | // FCMP crN, lo1, lo2 |
| 4649 | // The following can be improved, but not that much. |
| 4650 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 4651 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode); |
| 4652 | Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4653 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE); |
| 4654 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode); |
| 4655 | Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4656 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3); |
| 4657 | Tmp2 = SDOperand(); |
| 4658 | break; |
| 4659 | } |
| 4660 | |
| 4661 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4662 | case ISD::SETEQ: |
| 4663 | case ISD::SETNE: |
| 4664 | if (RHSLo == RHSHi) |
| 4665 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 4666 | if (RHSCST->isAllOnesValue()) { |
| 4667 | // Comparison to -1. |
| 4668 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 4669 | Tmp2 = RHSLo; |
| 4670 | break; |
| 4671 | } |
| 4672 | |
| 4673 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 4674 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 4675 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4676 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 4677 | break; |
| 4678 | default: |
| 4679 | // If this is a comparison of the sign bit, just look at the top part. |
| 4680 | // X > -1, x < 0 |
| 4681 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 4682 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 4683 | CST->getValue() == 0) || // X < 0 |
| 4684 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 4685 | CST->isAllOnesValue())) { // X > -1 |
| 4686 | Tmp1 = LHSHi; |
| 4687 | Tmp2 = RHSHi; |
| 4688 | break; |
| 4689 | } |
| 4690 | |
| 4691 | // FIXME: This generated code sucks. |
| 4692 | ISD::CondCode LowCC; |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4693 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4694 | default: assert(0 && "Unknown integer setcc!"); |
| 4695 | case ISD::SETLT: |
| 4696 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 4697 | case ISD::SETGT: |
| 4698 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 4699 | case ISD::SETLE: |
| 4700 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 4701 | case ISD::SETGE: |
| 4702 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 4703 | } |
| 4704 | |
| 4705 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 4706 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 4707 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 4708 | |
| 4709 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 4710 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4711 | TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL); |
| 4712 | Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC, |
| 4713 | false, DagCombineInfo); |
| 4714 | if (!Tmp1.Val) |
| 4715 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 4716 | Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 4717 | CCCode, false, DagCombineInfo); |
| 4718 | if (!Tmp2.Val) |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4719 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,CC); |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4720 | |
| 4721 | ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val); |
| 4722 | ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val); |
| 4723 | if ((Tmp1C && Tmp1C->getValue() == 0) || |
| 4724 | (Tmp2C && Tmp2C->getValue() == 0 && |
| 4725 | (CCCode == ISD::SETLE || CCCode == ISD::SETGE || |
| 4726 | CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) || |
| 4727 | (Tmp2C && Tmp2C->getValue() == 1 && |
| 4728 | (CCCode == ISD::SETLT || CCCode == ISD::SETGT || |
| 4729 | CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) { |
| 4730 | // low part is known false, returns high part. |
| 4731 | // For LE / GE, if high part is known false, ignore the low part. |
| 4732 | // For LT / GT, if high part is known true, ignore the low part. |
| 4733 | Tmp1 = Tmp2; |
| 4734 | Tmp2 = SDOperand(); |
| 4735 | } else { |
| 4736 | Result = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, |
| 4737 | ISD::SETEQ, false, DagCombineInfo); |
| 4738 | if (!Result.Val) |
| 4739 | Result=DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 4740 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 4741 | Result, Tmp1, Tmp2)); |
| 4742 | Tmp1 = Result; |
| 4743 | Tmp2 = SDOperand(); |
| 4744 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4745 | } |
| 4746 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4747 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4748 | LHS = Tmp1; |
| 4749 | RHS = Tmp2; |
| 4750 | } |
| 4751 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4752 | /// EmitStackConvert - Emit a store/load combination to the stack. This stores |
| 4753 | /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does |
| 4754 | /// a load from the stack slot to DestVT, extending it if needed. |
| 4755 | /// The resultant code need not be legal. |
| 4756 | SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp, |
| 4757 | MVT::ValueType SlotVT, |
| 4758 | MVT::ValueType DestVT) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4759 | // Create the stack frame object. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4760 | SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT); |
| 4761 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4762 | FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(FIPtr); |
| 4763 | assert(StackPtrFI); |
| 4764 | int SPFI = StackPtrFI->getIndex(); |
| 4765 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4766 | unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType()); |
| 4767 | unsigned SlotSize = MVT::getSizeInBits(SlotVT); |
| 4768 | unsigned DestSize = MVT::getSizeInBits(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4770 | // Emit a store to the stack slot. Use a truncstore if the input value is |
| 4771 | // later than DestVT. |
| 4772 | SDOperand Store; |
| 4773 | if (SrcSize > SlotSize) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4774 | Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, |
| 4775 | &PseudoSourceValue::getFixedStack(), |
| 4776 | SPFI, SlotVT); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4777 | else { |
| 4778 | assert(SrcSize == SlotSize && "Invalid store"); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4779 | Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, |
| 4780 | &PseudoSourceValue::getFixedStack(), |
| 4781 | SPFI, SlotVT); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4784 | // Result is a load from the stack slot. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4785 | if (SlotSize == DestSize) |
| 4786 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
| 4787 | |
| 4788 | assert(SlotSize < DestSize && "Unknown extension!"); |
| 4789 | return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4792 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 4793 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 4794 | // then load the whole vector back out. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4795 | SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4796 | |
| 4797 | FrameIndexSDNode *StackPtrFI = dyn_cast<FrameIndexSDNode>(StackPtr); |
| 4798 | assert(StackPtrFI); |
| 4799 | int SPFI = StackPtrFI->getIndex(); |
| 4800 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4801 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4802 | &PseudoSourceValue::getFixedStack(), SPFI); |
| 4803 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, |
| 4804 | &PseudoSourceValue::getFixedStack(), SPFI); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4808 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 4809 | /// support the operation, but do support the resultant vector type. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4810 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 4811 | |
| 4812 | // 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] | 4813 | // 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] | 4814 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4815 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4816 | SDOperand SplatValue = Node->getOperand(0); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4817 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 4818 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4819 | bool isConstant = true; |
| 4820 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 4821 | SplatValue.getOpcode() != ISD::UNDEF) |
| 4822 | isConstant = false; |
| 4823 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4824 | for (unsigned i = 1; i < NumElems; ++i) { |
| 4825 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 4826 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4827 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4828 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4829 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4830 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4831 | |
| 4832 | // If this isn't a constant element or an undef, we can't use a constant |
| 4833 | // pool load. |
| 4834 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 4835 | V.getOpcode() != ISD::UNDEF) |
| 4836 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | if (isOnlyLowElement) { |
| 4840 | // If the low element is an undef too, then this whole things is an undef. |
| 4841 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 4842 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 4843 | // Otherwise, turn this into a scalar_to_vector node. |
| 4844 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4845 | Node->getOperand(0)); |
| 4846 | } |
| 4847 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4848 | // If all elements are constants, create a load from the constant pool. |
| 4849 | if (isConstant) { |
| 4850 | MVT::ValueType VT = Node->getValueType(0); |
| 4851 | const Type *OpNTy = |
| 4852 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 4853 | std::vector<Constant*> CV; |
| 4854 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 4855 | if (ConstantFPSDNode *V = |
| 4856 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 4857 | CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4858 | } else if (ConstantSDNode *V = |
| 4859 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4860 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4861 | } else { |
| 4862 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 4863 | CV.push_back(UndefValue::get(OpNTy)); |
| 4864 | } |
| 4865 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 4866 | Constant *CP = ConstantVector::get(CV); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4867 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 4868 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 4869 | &PseudoSourceValue::getConstantPool(), 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4872 | if (SplatValue.Val) { // Splat of one value? |
| 4873 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 4874 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4875 | MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4876 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4877 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4878 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4879 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4880 | |
| 4881 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4882 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4883 | // Get the splatted value into the low element of a vector register. |
| 4884 | SDOperand LowValVec = |
| 4885 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 4886 | |
| 4887 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 4888 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 4889 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 4890 | SplatMask); |
| 4891 | } |
| 4892 | } |
| 4893 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4894 | // If there are only two unique elements, we may be able to turn this into a |
| 4895 | // vector shuffle. |
| 4896 | if (Values.size() == 2) { |
| 4897 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
| 4898 | MVT::ValueType MaskVT = |
| 4899 | MVT::getIntVectorWithNumElements(NumElems); |
| 4900 | std::vector<SDOperand> MaskVec(NumElems); |
| 4901 | unsigned i = 0; |
| 4902 | for (std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4903 | E = Values.end(); I != E; ++I) { |
| 4904 | for (std::vector<unsigned>::iterator II = I->second.begin(), |
| 4905 | EE = I->second.end(); II != EE; ++II) |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4906 | MaskVec[*II] = DAG.getConstant(i, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4907 | i += NumElems; |
| 4908 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4909 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4910 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4911 | |
| 4912 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4913 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 4914 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4915 | SmallVector<SDOperand, 8> Ops; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4916 | for(std::map<SDOperand,std::vector<unsigned> >::iterator I=Values.begin(), |
| 4917 | E = Values.end(); I != E; ++I) { |
| 4918 | SDOperand Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4919 | I->first); |
| 4920 | Ops.push_back(Op); |
| 4921 | } |
| 4922 | Ops.push_back(ShuffleMask); |
| 4923 | |
| 4924 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4925 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), |
| 4926 | &Ops[0], Ops.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4927 | } |
| 4928 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4929 | |
| 4930 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 4931 | // aligned object on the stack, store each element into it, then load |
| 4932 | // the result as a vector. |
| 4933 | MVT::ValueType VT = Node->getValueType(0); |
| 4934 | // Create the stack frame object. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4935 | SDOperand FIPtr = DAG.CreateStackTemporary(VT); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4936 | |
| 4937 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4938 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4939 | unsigned TypeByteSize = |
| 4940 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4941 | // Store (in the right endianness) the elements to memory. |
| 4942 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 4943 | // Ignore undef elements. |
| 4944 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 4945 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 4946 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4947 | |
| 4948 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 4949 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 4950 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4951 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4952 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | SDOperand StoreChain; |
| 4956 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4957 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 4958 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4959 | else |
| 4960 | StoreChain = DAG.getEntryNode(); |
| 4961 | |
| 4962 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4963 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4966 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 4967 | SDOperand Op, SDOperand Amt, |
| 4968 | SDOperand &Lo, SDOperand &Hi) { |
| 4969 | // Expand the subcomponents. |
| 4970 | SDOperand LHSL, LHSH; |
| 4971 | ExpandOp(Op, LHSL, LHSH); |
| 4972 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4973 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 4974 | MVT::ValueType VT = LHSL.getValueType(); |
| 4975 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 4976 | Hi = Lo.getValue(1); |
| 4977 | } |
| 4978 | |
| 4979 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4980 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 4981 | /// smaller elements. If we can't find a way that is more efficient than a |
| 4982 | /// libcall on this target, return false. Otherwise, return true with the |
| 4983 | /// low-parts expanded into Lo and Hi. |
| 4984 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 4985 | SDOperand &Lo, SDOperand &Hi) { |
| 4986 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 4987 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4988 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 4989 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4990 | SDOperand ShAmt = LegalizeOp(Amt); |
| 4991 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 4992 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 4993 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 4994 | |
Chris Lattner | 7a3c855 | 2007-10-14 20:35:12 +0000 | [diff] [blame] | 4995 | // Handle the case when Amt is an immediate. |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 4996 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 4997 | unsigned Cst = CN->getValue(); |
| 4998 | // Expand the incoming operand to be shifted, so that we have its parts |
| 4999 | SDOperand InL, InH; |
| 5000 | ExpandOp(Op, InL, InH); |
| 5001 | switch(Opc) { |
| 5002 | case ISD::SHL: |
| 5003 | if (Cst > VTBits) { |
| 5004 | Lo = DAG.getConstant(0, NVT); |
| 5005 | Hi = DAG.getConstant(0, NVT); |
| 5006 | } else if (Cst > NVTBits) { |
| 5007 | Lo = DAG.getConstant(0, NVT); |
| 5008 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5009 | } else if (Cst == NVTBits) { |
| 5010 | Lo = DAG.getConstant(0, NVT); |
| 5011 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5012 | } else { |
| 5013 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 5014 | Hi = DAG.getNode(ISD::OR, NVT, |
| 5015 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 5016 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5017 | } |
| 5018 | return true; |
| 5019 | case ISD::SRL: |
| 5020 | if (Cst > VTBits) { |
| 5021 | Lo = DAG.getConstant(0, NVT); |
| 5022 | Hi = DAG.getConstant(0, NVT); |
| 5023 | } else if (Cst > NVTBits) { |
| 5024 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 5025 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5026 | } else if (Cst == NVTBits) { |
| 5027 | Lo = InH; |
| 5028 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5029 | } else { |
| 5030 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5031 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 5032 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5033 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 5034 | } |
| 5035 | return true; |
| 5036 | case ISD::SRA: |
| 5037 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5038 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5039 | DAG.getConstant(NVTBits-1, ShTy)); |
| 5040 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5041 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5042 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5043 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5044 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5045 | } else if (Cst == NVTBits) { |
| 5046 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5047 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5048 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5049 | } else { |
| 5050 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5051 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 5052 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5053 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 5054 | } |
| 5055 | return true; |
| 5056 | } |
| 5057 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5058 | |
| 5059 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 5060 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
| 5061 | uint64_t Mask = NVTBits, KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5062 | DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5063 | |
| 5064 | // If we know that the high bit of the shift amount is one, then we can do |
| 5065 | // this as a couple of simple shifts. |
| 5066 | if (KnownOne & Mask) { |
| 5067 | // Mask out the high bit, which we know is set. |
| 5068 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
| 5069 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 5070 | |
| 5071 | // Expand the incoming operand to be shifted, so that we have its parts |
| 5072 | SDOperand InL, InH; |
| 5073 | ExpandOp(Op, InL, InH); |
| 5074 | switch(Opc) { |
| 5075 | case ISD::SHL: |
| 5076 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 5077 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 5078 | return true; |
| 5079 | case ISD::SRL: |
| 5080 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 5081 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 5082 | return true; |
| 5083 | case ISD::SRA: |
| 5084 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 5085 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 5086 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 5087 | return true; |
| 5088 | } |
| 5089 | } |
| 5090 | |
| 5091 | // If we know that the high bit of the shift amount is zero, then we can do |
| 5092 | // this as a couple of simple shifts. |
| 5093 | if (KnownZero & Mask) { |
| 5094 | // Compute 32-amt. |
| 5095 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 5096 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 5097 | Amt); |
| 5098 | |
| 5099 | // Expand the incoming operand to be shifted, so that we have its parts |
| 5100 | SDOperand InL, InH; |
| 5101 | ExpandOp(Op, InL, InH); |
| 5102 | switch(Opc) { |
| 5103 | case ISD::SHL: |
| 5104 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 5105 | Hi = DAG.getNode(ISD::OR, NVT, |
| 5106 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 5107 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 5108 | return true; |
| 5109 | case ISD::SRL: |
| 5110 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 5111 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5112 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 5113 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 5114 | return true; |
| 5115 | case ISD::SRA: |
| 5116 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 5117 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5118 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 5119 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 5120 | return true; |
| 5121 | } |
| 5122 | } |
| 5123 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5124 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5125 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5126 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5127 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5128 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 5129 | // does not fit into a register, return the lo part and set the hi part to the |
| 5130 | // by-reg argument. If it does fit into a single register, return the result |
| 5131 | // and leave the Hi part unset. |
| 5132 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5133 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5134 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 5135 | // The input chain to this libcall is the entry node of the function. |
| 5136 | // Legalizing the call will automatically add the previous call to the |
| 5137 | // dependence. |
| 5138 | SDOperand InChain = DAG.getEntryNode(); |
| 5139 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5140 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5141 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5142 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 5143 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 5144 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5145 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 5146 | Entry.isSExt = isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5147 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5148 | } |
| 5149 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5150 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5151 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5152 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5153 | std::pair<SDOperand,SDOperand> CallInfo = |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5154 | TLI.LowerCallTo(InChain, RetTy, isSigned, false, CallingConv::C, false, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 5155 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 5156 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5157 | // Legalize the call sequence, starting with the chain. This will advance |
| 5158 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 5159 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 5160 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5161 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5162 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5163 | default: assert(0 && "Unknown thing"); |
| 5164 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5165 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5166 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5167 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5168 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5169 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5170 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5171 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5174 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5175 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation. |
| 5176 | /// |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5177 | SDOperand SelectionDAGLegalize:: |
| 5178 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5179 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 5180 | "This is not an expansion!"); |
| 5181 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 5182 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5183 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5184 | assert(Source.getValueType() == MVT::i64 && |
| 5185 | "This only works for 64-bit -> FP"); |
| 5186 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 5187 | // incoming integer is set. To handle this, we dynamically test to see if |
| 5188 | // it is set, and, if so, add a fudge factor. |
| 5189 | SDOperand Lo, Hi; |
| 5190 | ExpandOp(Source, Lo, Hi); |
| 5191 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 5192 | // If this is unsigned, and not supported, first perform the conversion to |
| 5193 | // signed, then adjust the result if the sign bit is set. |
| 5194 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 5195 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 5196 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5197 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 5198 | DAG.getConstant(0, Hi.getValueType()), |
| 5199 | ISD::SETLT); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5200 | SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5201 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 5202 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 5203 | uint64_t FF = 0x5f800000ULL; |
| 5204 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5205 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5206 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 5207 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5208 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 5209 | SDOperand FudgeInReg; |
| 5210 | if (DestTy == MVT::f32) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 5211 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 5212 | &PseudoSourceValue::getConstantPool(), 0); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5213 | else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32)) |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5214 | // FIXME: Avoid the extend by construction the right constantpool? |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5215 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 5216 | CPIdx, |
| 5217 | &PseudoSourceValue::getConstantPool(), 0, |
| 5218 | MVT::f32); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 5219 | else |
| 5220 | assert(0 && "Unexpected conversion"); |
| 5221 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5222 | MVT::ValueType SCVT = SignedConv.getValueType(); |
| 5223 | if (SCVT != DestTy) { |
| 5224 | // Destination type needs to be expanded as well. The FADD now we are |
| 5225 | // constructing will be expanded into a libcall. |
| 5226 | if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) { |
| 5227 | assert(SCVT == MVT::i32 && DestTy == MVT::f64); |
| 5228 | SignedConv = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, |
| 5229 | SignedConv, SignedConv.getValue(1)); |
| 5230 | } |
| 5231 | SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv); |
| 5232 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 5233 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5234 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5235 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 5236 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 5237 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 5238 | default: assert(0 && "This action not implemented for this operation!"); |
| 5239 | case TargetLowering::Legal: |
| 5240 | case TargetLowering::Expand: |
| 5241 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5242 | case TargetLowering::Custom: { |
| 5243 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 5244 | Source), DAG); |
| 5245 | if (NV.Val) |
| 5246 | return LegalizeOp(NV); |
| 5247 | break; // The target decided this was legal after all |
| 5248 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 5249 | } |
| 5250 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 5251 | // Expand the source, then glue it back together for the call. We must expand |
| 5252 | // the source in case it is shared (this pass of legalize must traverse it). |
| 5253 | SDOperand SrcLo, SrcHi; |
| 5254 | ExpandOp(Source, SrcLo, SrcHi); |
| 5255 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 5256 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5257 | RTLIB::Libcall LC; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5258 | if (DestTy == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5259 | LC = RTLIB::SINTTOFP_I64_F32; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5260 | else { |
| 5261 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5262 | LC = RTLIB::SINTTOFP_I64_F64; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5263 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5264 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5265 | 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] | 5266 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 5267 | SDOperand UnusedHiPart; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5268 | return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, isSigned, |
| 5269 | UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5270 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5271 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5272 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 5273 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 5274 | /// we expand it. At this point, we know that the result and operand types are |
| 5275 | /// legal for the target. |
| 5276 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 5277 | SDOperand Op0, |
| 5278 | MVT::ValueType DestVT) { |
| 5279 | if (Op0.getValueType() == MVT::i32) { |
| 5280 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 5281 | |
Chris Lattner | 23594d4 | 2008-01-16 07:03:22 +0000 | [diff] [blame] | 5282 | // Get the stack frame index of a 8 byte buffer. |
| 5283 | SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64); |
| 5284 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5285 | // word offset constant for Hi/Lo address computation |
| 5286 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 5287 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 5288 | SDOperand Hi = StackSlot; |
| 5289 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 5290 | if (TLI.isLittleEndian()) |
| 5291 | std::swap(Hi, Lo); |
| 5292 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5293 | // if signed map to unsigned space |
| 5294 | SDOperand Op0Mapped; |
| 5295 | if (isSigned) { |
| 5296 | // constant used to invert sign bit (signed to unsigned mapping) |
| 5297 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 5298 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 5299 | } else { |
| 5300 | Op0Mapped = Op0; |
| 5301 | } |
| 5302 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5303 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5304 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5305 | // initial hi portion of constructed double |
| 5306 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 5307 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5308 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5309 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5310 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5311 | // FP constant to bias correct the final result |
| 5312 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 5313 | BitsToDouble(0x4330000080000000ULL) |
| 5314 | : BitsToDouble(0x4330000000000000ULL), |
| 5315 | MVT::f64); |
| 5316 | // subtract the bias |
| 5317 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 5318 | // final result |
| 5319 | SDOperand Result; |
| 5320 | // handle final rounding |
| 5321 | if (DestVT == MVT::f64) { |
| 5322 | // do nothing |
| 5323 | Result = Sub; |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5324 | } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5325 | Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub, |
| 5326 | DAG.getIntPtrConstant(0)); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5327 | } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) { |
| 5328 | Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5329 | } |
| 5330 | return Result; |
| 5331 | } |
| 5332 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 5333 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 5334 | |
| 5335 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 5336 | DAG.getConstant(0, Op0.getValueType()), |
| 5337 | ISD::SETLT); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5338 | SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5339 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 5340 | SignSet, Four, Zero); |
| 5341 | |
| 5342 | // If the sign bit of the integer is set, the large number will be treated |
| 5343 | // as a negative number. To counteract this, the dynamic code adds an |
| 5344 | // offset depending on the data type. |
| 5345 | uint64_t FF; |
| 5346 | switch (Op0.getValueType()) { |
| 5347 | default: assert(0 && "Unsupported integer type!"); |
| 5348 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 5349 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 5350 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 5351 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 5352 | } |
| 5353 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5354 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5355 | |
| 5356 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 5357 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 5358 | SDOperand FudgeInReg; |
| 5359 | if (DestVT == MVT::f32) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 5360 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 5361 | &PseudoSourceValue::getConstantPool(), 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5362 | else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 5363 | FudgeInReg = |
| 5364 | LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, |
| 5365 | DAG.getEntryNode(), CPIdx, |
| 5366 | &PseudoSourceValue::getConstantPool(), 0, |
| 5367 | MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 5371 | } |
| 5372 | |
| 5373 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 5374 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 5375 | /// we promote it. At this point, we know that the result and operand types are |
| 5376 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 5377 | /// operation that takes a larger input. |
| 5378 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 5379 | MVT::ValueType DestVT, |
| 5380 | bool isSigned) { |
| 5381 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 5382 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 5383 | |
| 5384 | unsigned OpToUse = 0; |
| 5385 | |
| 5386 | // Scan for the appropriate larger type to use. |
| 5387 | while (1) { |
| 5388 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 5389 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 5390 | |
| 5391 | // If the target supports SINT_TO_FP of this type, use it. |
| 5392 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 5393 | default: break; |
| 5394 | case TargetLowering::Legal: |
| 5395 | if (!TLI.isTypeLegal(NewInTy)) |
| 5396 | break; // Can't use this datatype. |
| 5397 | // FALL THROUGH. |
| 5398 | case TargetLowering::Custom: |
| 5399 | OpToUse = ISD::SINT_TO_FP; |
| 5400 | break; |
| 5401 | } |
| 5402 | if (OpToUse) break; |
| 5403 | if (isSigned) continue; |
| 5404 | |
| 5405 | // If the target supports UINT_TO_FP of this type, use it. |
| 5406 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 5407 | default: break; |
| 5408 | case TargetLowering::Legal: |
| 5409 | if (!TLI.isTypeLegal(NewInTy)) |
| 5410 | break; // Can't use this datatype. |
| 5411 | // FALL THROUGH. |
| 5412 | case TargetLowering::Custom: |
| 5413 | OpToUse = ISD::UINT_TO_FP; |
| 5414 | break; |
| 5415 | } |
| 5416 | if (OpToUse) break; |
| 5417 | |
| 5418 | // Otherwise, try a larger type. |
| 5419 | } |
| 5420 | |
| 5421 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 5422 | // desired type then run the operation on it. |
| 5423 | return DAG.getNode(OpToUse, DestVT, |
| 5424 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 5425 | NewInTy, LegalOp)); |
| 5426 | } |
| 5427 | |
| 5428 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 5429 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 5430 | /// we promote it. At this point, we know that the result and operand types are |
| 5431 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 5432 | /// operation that returns a larger result. |
| 5433 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 5434 | MVT::ValueType DestVT, |
| 5435 | bool isSigned) { |
| 5436 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 5437 | MVT::ValueType NewOutTy = DestVT; |
| 5438 | |
| 5439 | unsigned OpToUse = 0; |
| 5440 | |
| 5441 | // Scan for the appropriate larger type to use. |
| 5442 | while (1) { |
| 5443 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 5444 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 5445 | |
| 5446 | // If the target supports FP_TO_SINT returning this type, use it. |
| 5447 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 5448 | default: break; |
| 5449 | case TargetLowering::Legal: |
| 5450 | if (!TLI.isTypeLegal(NewOutTy)) |
| 5451 | break; // Can't use this datatype. |
| 5452 | // FALL THROUGH. |
| 5453 | case TargetLowering::Custom: |
| 5454 | OpToUse = ISD::FP_TO_SINT; |
| 5455 | break; |
| 5456 | } |
| 5457 | if (OpToUse) break; |
| 5458 | |
| 5459 | // If the target supports FP_TO_UINT of this type, use it. |
| 5460 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 5461 | default: break; |
| 5462 | case TargetLowering::Legal: |
| 5463 | if (!TLI.isTypeLegal(NewOutTy)) |
| 5464 | break; // Can't use this datatype. |
| 5465 | // FALL THROUGH. |
| 5466 | case TargetLowering::Custom: |
| 5467 | OpToUse = ISD::FP_TO_UINT; |
| 5468 | break; |
| 5469 | } |
| 5470 | if (OpToUse) break; |
| 5471 | |
| 5472 | // Otherwise, try a larger type. |
| 5473 | } |
| 5474 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5475 | |
| 5476 | // Okay, we found the operation and type to use. |
| 5477 | SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp); |
| 5478 | |
| 5479 | // If the operation produces an invalid type, it must be custom lowered. Use |
| 5480 | // the target lowering hooks to expand it. Just keep the low part of the |
| 5481 | // expanded operation, we know that we're truncating anyway. |
| 5482 | if (getTypeAction(NewOutTy) == Expand) { |
| 5483 | Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0); |
| 5484 | assert(Operation.Val && "Didn't return anything"); |
| 5485 | } |
| 5486 | |
| 5487 | // Truncate the result of the extended FP_TO_*INT operation to the desired |
| 5488 | // size. |
| 5489 | return DAG.getNode(ISD::TRUNCATE, DestVT, Operation); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 5493 | /// |
| 5494 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 5495 | MVT::ValueType VT = Op.getValueType(); |
| 5496 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 5497 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 5498 | switch (VT) { |
| 5499 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 5500 | case MVT::i16: |
| 5501 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5502 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5503 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 5504 | case MVT::i32: |
| 5505 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5506 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5507 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5508 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5509 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 5510 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 5511 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 5512 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 5513 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 5514 | case MVT::i64: |
| 5515 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 5516 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 5517 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5518 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5519 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5520 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5521 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 5522 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 5523 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 5524 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 5525 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 5526 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 5527 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 5528 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 5529 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 5530 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 5531 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 5532 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 5533 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 5534 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 5535 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 5540 | /// |
| 5541 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 5542 | switch (Opc) { |
| 5543 | default: assert(0 && "Cannot expand this yet!"); |
| 5544 | case ISD::CTPOP: { |
| 5545 | static const uint64_t mask[6] = { |
| 5546 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 5547 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 5548 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 5549 | }; |
| 5550 | MVT::ValueType VT = Op.getValueType(); |
| 5551 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5552 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5553 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 5554 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 5555 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 5556 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 5557 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 5558 | DAG.getNode(ISD::AND, VT, |
| 5559 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 5560 | } |
| 5561 | return Op; |
| 5562 | } |
| 5563 | case ISD::CTLZ: { |
| 5564 | // for now, we do this: |
| 5565 | // x = x | (x >> 1); |
| 5566 | // x = x | (x >> 2); |
| 5567 | // ... |
| 5568 | // x = x | (x >>16); |
| 5569 | // x = x | (x >>32); // for 64-bit input |
| 5570 | // return popcount(~x); |
| 5571 | // |
| 5572 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 5573 | MVT::ValueType VT = Op.getValueType(); |
| 5574 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5575 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5576 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 5577 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 5578 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 5579 | } |
| 5580 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 5581 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 5582 | } |
| 5583 | case ISD::CTTZ: { |
| 5584 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 5585 | // unless the target has ctlz but not ctpop, in which case we use: |
| 5586 | // { return 32 - nlz(~x & (x-1)); } |
| 5587 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 5588 | MVT::ValueType VT = Op.getValueType(); |
| 5589 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 5590 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 5591 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 5592 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 5593 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 5594 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 5595 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 5596 | return DAG.getNode(ISD::SUB, VT, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5597 | DAG.getConstant(MVT::getSizeInBits(VT), VT), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5598 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 5599 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 5600 | } |
| 5601 | } |
| 5602 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5603 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5604 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 5605 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 5606 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 5607 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 5608 | /// Lo/Hi values are returned. |
| 5609 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 5610 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 5611 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5612 | SDNode *Node = Op.Val; |
| 5613 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5614 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5615 | MVT::isVector(VT)) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5616 | "Cannot expand to FP value or to larger int value!"); |
| 5617 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5618 | // See if we already expanded it. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 5619 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5620 | = ExpandedNodes.find(Op); |
| 5621 | if (I != ExpandedNodes.end()) { |
| 5622 | Lo = I->second.first; |
| 5623 | Hi = I->second.second; |
| 5624 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5627 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5628 | case ISD::CopyFromReg: |
| 5629 | assert(0 && "CopyFromReg must be legal!"); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 5630 | case ISD::FP_ROUND_INREG: |
| 5631 | if (VT == MVT::ppcf128 && |
| 5632 | TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) == |
| 5633 | TargetLowering::Custom) { |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 5634 | SDOperand SrcLo, SrcHi, Src; |
| 5635 | ExpandOp(Op.getOperand(0), SrcLo, SrcHi); |
| 5636 | Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi); |
| 5637 | SDOperand Result = TLI.LowerOperation( |
| 5638 | DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 5639 | assert(Result.Val->getOpcode() == ISD::BUILD_PAIR); |
| 5640 | Lo = Result.Val->getOperand(0); |
| 5641 | Hi = Result.Val->getOperand(1); |
| 5642 | break; |
| 5643 | } |
| 5644 | // fall through |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5645 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5646 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5647 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5648 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5649 | assert(0 && "Do not know how to expand this operator!"); |
| 5650 | abort(); |
Dale Johannesen | 25f1d08 | 2007-10-31 00:32:36 +0000 | [diff] [blame] | 5651 | case ISD::EXTRACT_VECTOR_ELT: |
| 5652 | assert(VT==MVT::i64 && "Do not know how to expand this operator!"); |
| 5653 | // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types. |
| 5654 | Lo = ExpandEXTRACT_VECTOR_ELT(Op); |
| 5655 | return ExpandOp(Lo, Lo, Hi); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5656 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 5657 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5658 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 5659 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5660 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5661 | case ISD::Constant: { |
| 5662 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 5663 | Lo = DAG.getConstant(Cst, NVT); |
| 5664 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 5665 | break; |
| 5666 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5667 | case ISD::ConstantFP: { |
| 5668 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 5669 | if (CFP->getValueType(0) == MVT::ppcf128) { |
| 5670 | APInt api = CFP->getValueAPF().convertToAPInt(); |
| 5671 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])), |
| 5672 | MVT::f64); |
| 5673 | Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])), |
| 5674 | MVT::f64); |
| 5675 | break; |
| 5676 | } |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 5677 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5678 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 5679 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5680 | break; |
| 5681 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5682 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5683 | // Return the operands. |
| 5684 | Lo = Node->getOperand(0); |
| 5685 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5686 | break; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5687 | |
| 5688 | case ISD::MERGE_VALUES: |
Chris Lattner | c58d558 | 2007-11-24 19:12:15 +0000 | [diff] [blame] | 5689 | if (Node->getNumValues() == 1) { |
| 5690 | ExpandOp(Op.getOperand(0), Lo, Hi); |
| 5691 | break; |
| 5692 | } |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5693 | // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y) |
| 5694 | assert(Op.ResNo == 0 && Node->getNumValues() == 2 && |
| 5695 | Op.getValue(1).getValueType() == MVT::Other && |
| 5696 | "unhandled MERGE_VALUES"); |
| 5697 | ExpandOp(Op.getOperand(0), Lo, Hi); |
| 5698 | // Remember that we legalized the chain. |
| 5699 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1))); |
| 5700 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5701 | |
| 5702 | case ISD::SIGN_EXTEND_INREG: |
| 5703 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 5704 | // sext_inreg the low part if needed. |
| 5705 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 5706 | |
| 5707 | // The high part gets the sign extension from the lo-part. This handles |
| 5708 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5709 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5710 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 5711 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5712 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5713 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 5714 | case ISD::BSWAP: { |
| 5715 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 5716 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 5717 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 5718 | Lo = TempLo; |
| 5719 | break; |
| 5720 | } |
| 5721 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5722 | case ISD::CTPOP: |
| 5723 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 5724 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 5725 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 5726 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5727 | Hi = DAG.getConstant(0, NVT); |
| 5728 | break; |
| 5729 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5730 | case ISD::CTLZ: { |
| 5731 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5732 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5733 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5734 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5735 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 5736 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5737 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 5738 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 5739 | |
| 5740 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 5741 | Hi = DAG.getConstant(0, NVT); |
| 5742 | break; |
| 5743 | } |
| 5744 | |
| 5745 | case ISD::CTTZ: { |
| 5746 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5747 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5748 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5749 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5750 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 5751 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5752 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 5753 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 5754 | |
| 5755 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 5756 | Hi = DAG.getConstant(0, NVT); |
| 5757 | break; |
| 5758 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5759 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5760 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5761 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 5762 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5763 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 5764 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 5765 | |
| 5766 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5767 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5768 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 5769 | if (!TLI.isLittleEndian()) |
| 5770 | std::swap(Lo, Hi); |
| 5771 | break; |
| 5772 | } |
| 5773 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5774 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5775 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5776 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 5777 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 5778 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5779 | int SVOffset = LD->getSrcValueOffset(); |
| 5780 | unsigned Alignment = LD->getAlignment(); |
| 5781 | bool isVolatile = LD->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5782 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5783 | if (ExtType == ISD::NON_EXTLOAD) { |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5784 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5785 | isVolatile, Alignment); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5786 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5787 | // f32->i32 or f64->i64 one to one expansion. |
| 5788 | // Remember that we legalized the chain. |
| 5789 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5790 | // Recursively expand the new load. |
| 5791 | if (getTypeAction(NVT) == Expand) |
| 5792 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5793 | break; |
| 5794 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 5795 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5796 | // Increment the pointer to the other half. |
| 5797 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 5798 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5799 | DAG.getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5800 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 5801 | Alignment = MinAlign(Alignment, IncrementSize); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5802 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5803 | isVolatile, Alignment); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5804 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5805 | // Build a factor node to remember that this load is independent of the |
| 5806 | // other one. |
| 5807 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5808 | Hi.getValue(1)); |
| 5809 | |
| 5810 | // Remember that we legalized the chain. |
| 5811 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 5812 | if (!TLI.isLittleEndian()) |
| 5813 | std::swap(Lo, Hi); |
| 5814 | } else { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5815 | MVT::ValueType EVT = LD->getMemoryVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5816 | |
Dale Johannesen | b6210fc | 2007-10-19 20:29:00 +0000 | [diff] [blame] | 5817 | if ((VT == MVT::f64 && EVT == MVT::f32) || |
| 5818 | (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) { |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5819 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 5820 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5821 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5822 | // Remember that we legalized the chain. |
| 5823 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 5824 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 5825 | break; |
| 5826 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5827 | |
| 5828 | if (EVT == NVT) |
| 5829 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5830 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5831 | else |
| 5832 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5833 | SVOffset, EVT, isVolatile, |
| 5834 | Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5835 | |
| 5836 | // Remember that we legalized the chain. |
| 5837 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 5838 | |
| 5839 | if (ExtType == ISD::SEXTLOAD) { |
| 5840 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 5841 | // lo part. |
| 5842 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 5843 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5844 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 5845 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 5846 | // The high part is just a zero. |
| 5847 | Hi = DAG.getConstant(0, NVT); |
| 5848 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 5849 | // The high part is undefined. |
| 5850 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5851 | } |
| 5852 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5853 | break; |
| 5854 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5855 | case ISD::AND: |
| 5856 | case ISD::OR: |
| 5857 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 5858 | SDOperand LL, LH, RL, RH; |
| 5859 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5860 | ExpandOp(Node->getOperand(1), RL, RH); |
| 5861 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 5862 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 5863 | break; |
| 5864 | } |
| 5865 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5866 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5867 | ExpandOp(Node->getOperand(1), LL, LH); |
| 5868 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5869 | if (getTypeAction(NVT) == Expand) |
| 5870 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5871 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5872 | if (VT != MVT::f32) |
| 5873 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5874 | break; |
| 5875 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5876 | case ISD::SELECT_CC: { |
| 5877 | SDOperand TL, TH, FL, FH; |
| 5878 | ExpandOp(Node->getOperand(2), TL, TH); |
| 5879 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5880 | if (getTypeAction(NVT) == Expand) |
| 5881 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5882 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5883 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5884 | if (VT != MVT::f32) |
| 5885 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5886 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5887 | break; |
| 5888 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5889 | case ISD::ANY_EXTEND: |
| 5890 | // The low part is any extension of the input (which degenerates to a copy). |
| 5891 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 5892 | // The high part is undefined. |
| 5893 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5894 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5895 | case ISD::SIGN_EXTEND: { |
| 5896 | // The low part is just a sign extension of the input (which degenerates to |
| 5897 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5898 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5899 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5900 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 5901 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 5902 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5903 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5904 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5905 | break; |
| 5906 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5907 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5908 | // The low part is just a zero extension of the input (which degenerates to |
| 5909 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5910 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5911 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5912 | // The high part is just a zero. |
| 5913 | Hi = DAG.getConstant(0, NVT); |
| 5914 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5915 | |
Chris Lattner | 4c948eb | 2007-02-13 23:55:16 +0000 | [diff] [blame] | 5916 | case ISD::TRUNCATE: { |
| 5917 | // The input value must be larger than this value. Expand *it*. |
| 5918 | SDOperand NewLo; |
| 5919 | ExpandOp(Node->getOperand(0), NewLo, Hi); |
| 5920 | |
| 5921 | // The low part is now either the right size, or it is closer. If not the |
| 5922 | // right size, make an illegal truncate so we recursively expand it. |
| 5923 | if (NewLo.getValueType() != Node->getValueType(0)) |
| 5924 | NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo); |
| 5925 | ExpandOp(NewLo, Lo, Hi); |
| 5926 | break; |
| 5927 | } |
| 5928 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5929 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5930 | SDOperand Tmp; |
| 5931 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 5932 | // If the target wants to, allow it to lower this itself. |
| 5933 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5934 | case Expand: assert(0 && "cannot expand FP!"); |
| 5935 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 5936 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 5937 | } |
| 5938 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 5939 | } |
| 5940 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5941 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 5942 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5943 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5944 | if (getTypeAction(NVT) == Expand) |
| 5945 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 5946 | break; |
| 5947 | } |
| 5948 | |
| 5949 | // If source operand will be expanded to the same type as VT, i.e. |
| 5950 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 5951 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 5952 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 5953 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5954 | break; |
| 5955 | } |
| 5956 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5957 | // Turn this into a load/store pair by default. |
| 5958 | if (Tmp.Val == 0) |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 5959 | Tmp = EmitStackConvert(Node->getOperand(0), VT, VT); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 5960 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 5961 | ExpandOp(Tmp, Lo, Hi); |
| 5962 | break; |
| 5963 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5964 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5965 | case ISD::READCYCLECOUNTER: { |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5966 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 5967 | TargetLowering::Custom && |
| 5968 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5969 | SDOperand Tmp = TLI.LowerOperation(Op, DAG); |
| 5970 | assert(Tmp.Val && "Node must be custom expanded!"); |
| 5971 | ExpandOp(Tmp.getValue(0), Lo, Hi); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 5972 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5973 | LegalizeOp(Tmp.getValue(1))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5974 | break; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5975 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 5976 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5977 | // These operators cannot be expanded directly, emit them as calls to |
| 5978 | // library functions. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5979 | case ISD::FP_TO_SINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5980 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5981 | SDOperand Op; |
| 5982 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 5983 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5984 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 5985 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5986 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5987 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 5988 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 5989 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5990 | // Now that the custom expander is done, expand the result, which is still |
| 5991 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5992 | if (Op.Val) { |
| 5993 | ExpandOp(Op, Lo, Hi); |
| 5994 | break; |
| 5995 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 5996 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 5997 | |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 5998 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 5999 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6000 | LC = RTLIB::FPTOSINT_F32_I64; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 6001 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6002 | LC = RTLIB::FPTOSINT_F64_I64; |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 6003 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6004 | LC = RTLIB::FPTOSINT_F80_I64; |
| 6005 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6006 | LC = RTLIB::FPTOSINT_PPCF128_I64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6007 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 6008 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6009 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6010 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6011 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6012 | case ISD::FP_TO_UINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6013 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6014 | SDOperand Op; |
| 6015 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 6016 | case Expand: assert(0 && "cannot expand FP!"); |
| 6017 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 6018 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 6019 | } |
| 6020 | |
| 6021 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 6022 | |
| 6023 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 6024 | if (Op.Val) { |
| 6025 | ExpandOp(Op, Lo, Hi); |
| 6026 | break; |
| 6027 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6028 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6029 | |
Evan Cheng | daccea18 | 2007-10-05 01:09:32 +0000 | [diff] [blame] | 6030 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6031 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6032 | LC = RTLIB::FPTOUINT_F32_I64; |
Dale Johannesen | 72292f0 | 2007-09-28 18:44:17 +0000 | [diff] [blame] | 6033 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6034 | LC = RTLIB::FPTOUINT_F64_I64; |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 6035 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6036 | LC = RTLIB::FPTOUINT_F80_I64; |
| 6037 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6038 | LC = RTLIB::FPTOUINT_PPCF128_I64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6039 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, |
| 6040 | false/*sign irrelevant*/, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6041 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6042 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6043 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6044 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6045 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6046 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6047 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6048 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6049 | Op = TLI.LowerOperation(Op, DAG); |
| 6050 | if (Op.Val) { |
| 6051 | // Now that the custom expander is done, expand the result, which is |
| 6052 | // still VT. |
| 6053 | ExpandOp(Op, Lo, Hi); |
| 6054 | break; |
| 6055 | } |
| 6056 | } |
| 6057 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6058 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 6059 | // this X << 1 as X+X. |
| 6060 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
| 6061 | if (ShAmt->getValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
| 6062 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 6063 | SDOperand LoOps[2], HiOps[3]; |
| 6064 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 6065 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 6066 | LoOps[1] = LoOps[0]; |
| 6067 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 6068 | |
| 6069 | HiOps[1] = HiOps[0]; |
| 6070 | HiOps[2] = Lo.getValue(1); |
| 6071 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 6072 | break; |
| 6073 | } |
| 6074 | } |
| 6075 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6076 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6077 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6078 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6079 | |
| 6080 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6081 | TargetLowering::LegalizeAction Action = |
| 6082 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 6083 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6084 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6085 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6086 | break; |
| 6087 | } |
| 6088 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6089 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6090 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SHL_I64), Node, |
| 6091 | false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6092 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6093 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6094 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6095 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6096 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6097 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6098 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6099 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6100 | Op = TLI.LowerOperation(Op, DAG); |
| 6101 | if (Op.Val) { |
| 6102 | // Now that the custom expander is done, expand the result, which is |
| 6103 | // still VT. |
| 6104 | ExpandOp(Op, Lo, Hi); |
| 6105 | break; |
| 6106 | } |
| 6107 | } |
| 6108 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6109 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6110 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6111 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6112 | |
| 6113 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6114 | TargetLowering::LegalizeAction Action = |
| 6115 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 6116 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6117 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6118 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6119 | break; |
| 6120 | } |
| 6121 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6122 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6123 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRA_I64), Node, |
| 6124 | true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6125 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
| 6128 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6129 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6130 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6131 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6132 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6133 | Op = TLI.LowerOperation(Op, DAG); |
| 6134 | if (Op.Val) { |
| 6135 | // Now that the custom expander is done, expand the result, which is |
| 6136 | // still VT. |
| 6137 | ExpandOp(Op, Lo, Hi); |
| 6138 | break; |
| 6139 | } |
| 6140 | } |
| 6141 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6142 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6143 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6144 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6145 | |
| 6146 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6147 | TargetLowering::LegalizeAction Action = |
| 6148 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 6149 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6150 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6151 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6152 | break; |
| 6153 | } |
| 6154 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6155 | // Otherwise, emit a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6156 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), Node, |
| 6157 | false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6158 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6159 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6160 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 6161 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6162 | case ISD::SUB: { |
| 6163 | // If the target wants to custom expand this, let them. |
| 6164 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 6165 | TargetLowering::Custom) { |
| 6166 | Op = TLI.LowerOperation(Op, DAG); |
| 6167 | if (Op.Val) { |
| 6168 | ExpandOp(Op, Lo, Hi); |
| 6169 | break; |
| 6170 | } |
| 6171 | } |
| 6172 | |
| 6173 | // Expand the subcomponents. |
| 6174 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6175 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6176 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6177 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6178 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6179 | LoOps[0] = LHSL; |
| 6180 | LoOps[1] = RHSL; |
| 6181 | HiOps[0] = LHSH; |
| 6182 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6183 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6184 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6185 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6186 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6187 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6188 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6189 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6190 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6191 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 6192 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6193 | } |
Chris Lattner | b429f73 | 2007-05-17 18:15:41 +0000 | [diff] [blame] | 6194 | |
| 6195 | case ISD::ADDC: |
| 6196 | case ISD::SUBC: { |
| 6197 | // Expand the subcomponents. |
| 6198 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6199 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6200 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 6201 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6202 | SDOperand LoOps[2] = { LHSL, RHSL }; |
| 6203 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 6204 | |
| 6205 | if (Node->getOpcode() == ISD::ADDC) { |
| 6206 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 6207 | HiOps[2] = Lo.getValue(1); |
| 6208 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 6209 | } else { |
| 6210 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
| 6211 | HiOps[2] = Lo.getValue(1); |
| 6212 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
| 6213 | } |
| 6214 | // Remember that we legalized the flag. |
| 6215 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 6216 | break; |
| 6217 | } |
| 6218 | case ISD::ADDE: |
| 6219 | case ISD::SUBE: { |
| 6220 | // Expand the subcomponents. |
| 6221 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6222 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6223 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 6224 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6225 | SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) }; |
| 6226 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 6227 | |
| 6228 | Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3); |
| 6229 | HiOps[2] = Lo.getValue(1); |
| 6230 | Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3); |
| 6231 | |
| 6232 | // Remember that we legalized the flag. |
| 6233 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 6234 | break; |
| 6235 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6236 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 6237 | // If the target wants to custom expand this, let them. |
| 6238 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 6239 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 6240 | if (New.Val) { |
| 6241 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 6242 | break; |
| 6243 | } |
| 6244 | } |
| 6245 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 6246 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 6247 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6248 | bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT); |
| 6249 | bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT); |
| 6250 | if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6251 | SDOperand LL, LH, RL, RH; |
| 6252 | ExpandOp(Node->getOperand(0), LL, LH); |
| 6253 | ExpandOp(Node->getOperand(1), RL, RH); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6254 | unsigned BitSize = MVT::getSizeInBits(RH.getValueType()); |
| 6255 | unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0)); |
| 6256 | unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1)); |
| 6257 | // FIXME: generalize this to handle other bit sizes |
| 6258 | if (LHSSB == 32 && RHSSB == 32 && |
| 6259 | DAG.MaskedValueIsZero(Op.getOperand(0), 0xFFFFFFFF00000000ULL) && |
| 6260 | DAG.MaskedValueIsZero(Op.getOperand(1), 0xFFFFFFFF00000000ULL)) { |
| 6261 | // The inputs are both zero-extended. |
| 6262 | if (HasUMUL_LOHI) { |
| 6263 | // We can emit a umul_lohi. |
| 6264 | Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL); |
| 6265 | Hi = SDOperand(Lo.Val, 1); |
| 6266 | break; |
| 6267 | } |
| 6268 | if (HasMULHU) { |
| 6269 | // We can emit a mulhu+mul. |
| 6270 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6271 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 6272 | break; |
| 6273 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6274 | } |
| 6275 | if (LHSSB > BitSize && RHSSB > BitSize) { |
| 6276 | // The input values are both sign-extended. |
| 6277 | if (HasSMUL_LOHI) { |
| 6278 | // We can emit a smul_lohi. |
| 6279 | Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL); |
| 6280 | Hi = SDOperand(Lo.Val, 1); |
| 6281 | break; |
| 6282 | } |
| 6283 | if (HasMULHS) { |
| 6284 | // We can emit a mulhs+mul. |
| 6285 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6286 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
| 6287 | break; |
| 6288 | } |
| 6289 | } |
| 6290 | if (HasUMUL_LOHI) { |
| 6291 | // Lo,Hi = umul LHS, RHS. |
| 6292 | SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, |
| 6293 | DAG.getVTList(NVT, NVT), LL, RL); |
| 6294 | Lo = UMulLOHI; |
| 6295 | Hi = UMulLOHI.getValue(1); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 6296 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 6297 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 6298 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 6299 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 6300 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 6301 | } |
Dale Johannesen | 8eadd5a | 2007-10-24 22:26:08 +0000 | [diff] [blame] | 6302 | if (HasMULHU) { |
| 6303 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6304 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 6305 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 6306 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 6307 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 6308 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 6309 | break; |
| 6310 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6311 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 6312 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6313 | // If nothing else, we can make a libcall. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6314 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), Node, |
| 6315 | false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6316 | break; |
| 6317 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6318 | case ISD::SDIV: |
| 6319 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SDIV_I64), Node, true, Hi); |
| 6320 | break; |
| 6321 | case ISD::UDIV: |
| 6322 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UDIV_I64), Node, true, Hi); |
| 6323 | break; |
| 6324 | case ISD::SREM: |
| 6325 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SREM_I64), Node, true, Hi); |
| 6326 | break; |
| 6327 | case ISD::UREM: |
| 6328 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::UREM_I64), Node, true, Hi); |
| 6329 | break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 6330 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6331 | case ISD::FADD: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6332 | Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::ADD_F32, |
| 6333 | RTLIB::ADD_F64, |
| 6334 | RTLIB::ADD_F80, |
| 6335 | RTLIB::ADD_PPCF128)), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6336 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6337 | break; |
| 6338 | case ISD::FSUB: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6339 | Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::SUB_F32, |
| 6340 | RTLIB::SUB_F64, |
| 6341 | RTLIB::SUB_F80, |
| 6342 | RTLIB::SUB_PPCF128)), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6343 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6344 | break; |
| 6345 | case ISD::FMUL: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6346 | Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::MUL_F32, |
| 6347 | RTLIB::MUL_F64, |
| 6348 | RTLIB::MUL_F80, |
| 6349 | RTLIB::MUL_PPCF128)), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6350 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6351 | break; |
| 6352 | case ISD::FDIV: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6353 | Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::DIV_F32, |
| 6354 | RTLIB::DIV_F64, |
| 6355 | RTLIB::DIV_F80, |
| 6356 | RTLIB::DIV_PPCF128)), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6357 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6358 | break; |
| 6359 | case ISD::FP_EXTEND: |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6360 | if (VT == MVT::ppcf128) { |
| 6361 | assert(Node->getOperand(0).getValueType()==MVT::f32 || |
| 6362 | Node->getOperand(0).getValueType()==MVT::f64); |
| 6363 | const uint64_t zero = 0; |
| 6364 | if (Node->getOperand(0).getValueType()==MVT::f32) |
| 6365 | Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0)); |
| 6366 | else |
| 6367 | Hi = Node->getOperand(0); |
| 6368 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6369 | break; |
| 6370 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6371 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPEXT_F32_F64), Node, true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6372 | break; |
| 6373 | case ISD::FP_ROUND: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6374 | Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::FPROUND_F64_F32),Node,true,Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6375 | break; |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 6376 | case ISD::FPOWI: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6377 | Lo = ExpandLibCall(TLI.getLibcallName(GetFPLibCall(VT, RTLIB::POWI_F32, |
| 6378 | RTLIB::POWI_F64, |
| 6379 | RTLIB::POWI_F80, |
| 6380 | RTLIB::POWI_PPCF128)), |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 6381 | Node, false, Hi); |
| 6382 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6383 | case ISD::FSQRT: |
| 6384 | case ISD::FSIN: |
| 6385 | case ISD::FCOS: { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6386 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6387 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6388 | case ISD::FSQRT: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6389 | LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 6390 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6391 | break; |
| 6392 | case ISD::FSIN: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6393 | LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 6394 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6395 | break; |
| 6396 | case ISD::FCOS: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6397 | LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64, |
| 6398 | RTLIB::COS_F80, RTLIB::COS_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6399 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6400 | default: assert(0 && "Unreachable!"); |
| 6401 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6402 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6403 | break; |
| 6404 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6405 | case ISD::FABS: { |
Dale Johannesen | f646774 | 2007-10-12 19:02:17 +0000 | [diff] [blame] | 6406 | if (VT == MVT::ppcf128) { |
| 6407 | SDOperand Tmp; |
| 6408 | ExpandOp(Node->getOperand(0), Lo, Tmp); |
| 6409 | Hi = DAG.getNode(ISD::FABS, NVT, Tmp); |
| 6410 | // lo = hi==fabs(hi) ? lo : -lo; |
| 6411 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp, |
| 6412 | Lo, DAG.getNode(ISD::FNEG, NVT, Lo), |
| 6413 | DAG.getCondCode(ISD::SETEQ)); |
| 6414 | break; |
| 6415 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6416 | SDOperand Mask = (VT == MVT::f64) |
| 6417 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 6418 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 6419 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 6420 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 6421 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 6422 | if (getTypeAction(NVT) == Expand) |
| 6423 | ExpandOp(Lo, Lo, Hi); |
| 6424 | break; |
| 6425 | } |
| 6426 | case ISD::FNEG: { |
Dale Johannesen | f646774 | 2007-10-12 19:02:17 +0000 | [diff] [blame] | 6427 | if (VT == MVT::ppcf128) { |
| 6428 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 6429 | Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo); |
| 6430 | Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi); |
| 6431 | break; |
| 6432 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6433 | SDOperand Mask = (VT == MVT::f64) |
| 6434 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 6435 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 6436 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 6437 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 6438 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 6439 | if (getTypeAction(NVT) == Expand) |
| 6440 | ExpandOp(Lo, Lo, Hi); |
| 6441 | break; |
| 6442 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 6443 | case ISD::FCOPYSIGN: { |
| 6444 | Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 6445 | if (getTypeAction(NVT) == Expand) |
| 6446 | ExpandOp(Lo, Lo, Hi); |
| 6447 | break; |
| 6448 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6449 | case ISD::SINT_TO_FP: |
| 6450 | case ISD::UINT_TO_FP: { |
| 6451 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 6452 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6453 | if (VT == MVT::ppcf128 && SrcVT != MVT::i64) { |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6454 | static uint64_t zero = 0; |
| 6455 | if (isSigned) { |
| 6456 | Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64, |
| 6457 | Node->getOperand(0))); |
| 6458 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6459 | } else { |
| 6460 | static uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 }; |
| 6461 | Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64, |
| 6462 | Node->getOperand(0))); |
| 6463 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6464 | Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6465 | // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32 |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6466 | ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0), |
| 6467 | DAG.getConstant(0, MVT::i32), |
| 6468 | DAG.getNode(ISD::FADD, MVT::ppcf128, Hi, |
| 6469 | DAG.getConstantFP( |
| 6470 | APFloat(APInt(128, 2, TwoE32)), |
| 6471 | MVT::ppcf128)), |
| 6472 | Hi, |
| 6473 | DAG.getCondCode(ISD::SETLT)), |
| 6474 | Lo, Hi); |
| 6475 | } |
| 6476 | break; |
| 6477 | } |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6478 | if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) { |
| 6479 | // si64->ppcf128 done by libcall, below |
| 6480 | static uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 }; |
| 6481 | ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)), |
| 6482 | Lo, Hi); |
| 6483 | Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
| 6484 | // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64 |
| 6485 | ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0), |
| 6486 | DAG.getConstant(0, MVT::i64), |
| 6487 | DAG.getNode(ISD::FADD, MVT::ppcf128, Hi, |
| 6488 | DAG.getConstantFP( |
| 6489 | APFloat(APInt(128, 2, TwoE64)), |
| 6490 | MVT::ppcf128)), |
| 6491 | Hi, |
| 6492 | DAG.getCondCode(ISD::SETLT)), |
| 6493 | Lo, Hi); |
| 6494 | break; |
| 6495 | } |
Evan Cheng | 64f638d | 2007-09-27 07:35:39 +0000 | [diff] [blame] | 6496 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6497 | if (Node->getOperand(0).getValueType() == MVT::i64) { |
| 6498 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6499 | LC = isSigned ? RTLIB::SINTTOFP_I64_F32 : RTLIB::UINTTOFP_I64_F32; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 6500 | else if (VT == MVT::f64) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6501 | LC = isSigned ? RTLIB::SINTTOFP_I64_F64 : RTLIB::UINTTOFP_I64_F64; |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 6502 | else if (VT == MVT::f80) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 6503 | assert(isSigned); |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 6504 | LC = RTLIB::SINTTOFP_I64_F80; |
| 6505 | } |
| 6506 | else if (VT == MVT::ppcf128) { |
| 6507 | assert(isSigned); |
| 6508 | LC = RTLIB::SINTTOFP_I64_PPCF128; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 6509 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6510 | } else { |
| 6511 | if (VT == MVT::f32) |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6512 | LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6513 | else |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6514 | LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64; |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | // Promote the operand if needed. |
| 6518 | if (getTypeAction(SrcVT) == Promote) { |
| 6519 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 6520 | Tmp = isSigned |
| 6521 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 6522 | DAG.getValueType(SrcVT)) |
| 6523 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 6524 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 6525 | } |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 6526 | |
| 6527 | const char *LibCall = TLI.getLibcallName(LC); |
| 6528 | if (LibCall) |
| 6529 | Lo = ExpandLibCall(TLI.getLibcallName(LC), Node, isSigned, Hi); |
| 6530 | else { |
| 6531 | Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT, |
| 6532 | Node->getOperand(0)); |
| 6533 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 6534 | ExpandOp(Lo, Lo, Hi); |
| 6535 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6536 | break; |
| 6537 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6538 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6539 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 6540 | // Make sure the resultant values have been legalized themselves, unless this |
| 6541 | // is a type that requires multi-step expansion. |
| 6542 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 6543 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 6544 | if (Hi.Val) |
| 6545 | // Don't legalize the high part if it is expanded to a single node. |
| 6546 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 6547 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6548 | |
| 6549 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 6550 | 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] | 6551 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6554 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 6555 | /// two smaller values, still of vector type. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6556 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 6557 | SDOperand &Hi) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6558 | assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6559 | SDNode *Node = Op.Val; |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 6560 | unsigned NumElements = MVT::getVectorNumElements(Op.getValueType()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6561 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6562 | |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 6563 | MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6564 | |
| 6565 | unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1); |
| 6566 | unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo; |
| 6567 | |
| 6568 | MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo); |
| 6569 | MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi); |
| 6570 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6571 | // See if we already split it. |
| 6572 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 6573 | = SplitNodes.find(Op); |
| 6574 | if (I != SplitNodes.end()) { |
| 6575 | Lo = I->second.first; |
| 6576 | Hi = I->second.second; |
| 6577 | return; |
| 6578 | } |
| 6579 | |
| 6580 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6581 | default: |
| 6582 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 6583 | Node->dump(&DAG); |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6584 | #endif |
| 6585 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Chris Lattner | daf9bc8 | 2007-11-19 20:21:32 +0000 | [diff] [blame] | 6586 | case ISD::UNDEF: |
| 6587 | Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo); |
| 6588 | Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi); |
| 6589 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6590 | case ISD::BUILD_PAIR: |
| 6591 | Lo = Node->getOperand(0); |
| 6592 | Hi = Node->getOperand(1); |
| 6593 | break; |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6594 | case ISD::INSERT_VECTOR_ELT: { |
| 6595 | SplitVectorOp(Node->getOperand(0), Lo, Hi); |
| 6596 | unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue(); |
| 6597 | SDOperand ScalarOp = Node->getOperand(1); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6598 | if (Index < NewNumElts_Lo) |
| 6599 | Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp, |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6600 | DAG.getConstant(Index, TLI.getPointerTy())); |
| 6601 | else |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6602 | Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp, |
| 6603 | DAG.getConstant(Index - NewNumElts_Lo, |
| 6604 | TLI.getPointerTy())); |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6605 | break; |
| 6606 | } |
Chris Lattner | 6c9c680 | 2007-11-19 21:16:54 +0000 | [diff] [blame] | 6607 | case ISD::VECTOR_SHUFFLE: { |
| 6608 | // Build the low part. |
| 6609 | SDOperand Mask = Node->getOperand(2); |
| 6610 | SmallVector<SDOperand, 8> Ops; |
| 6611 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 6612 | |
| 6613 | // Insert all of the elements from the input that are needed. We use |
| 6614 | // buildvector of extractelement here because the input vectors will have |
| 6615 | // to be legalized, so this makes the code simpler. |
| 6616 | for (unsigned i = 0; i != NewNumElts_Lo; ++i) { |
| 6617 | unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue(); |
| 6618 | SDOperand InVec = Node->getOperand(0); |
| 6619 | if (Idx >= NumElements) { |
| 6620 | InVec = Node->getOperand(1); |
| 6621 | Idx -= NumElements; |
| 6622 | } |
| 6623 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec, |
| 6624 | DAG.getConstant(Idx, PtrVT))); |
| 6625 | } |
| 6626 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size()); |
| 6627 | Ops.clear(); |
| 6628 | |
| 6629 | for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) { |
| 6630 | unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getValue(); |
| 6631 | SDOperand InVec = Node->getOperand(0); |
| 6632 | if (Idx >= NumElements) { |
| 6633 | InVec = Node->getOperand(1); |
| 6634 | Idx -= NumElements; |
| 6635 | } |
| 6636 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec, |
| 6637 | DAG.getConstant(Idx, PtrVT))); |
| 6638 | } |
| 6639 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size()); |
| 6640 | break; |
| 6641 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6642 | case ISD::BUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6643 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6644 | Node->op_begin()+NewNumElts_Lo); |
| 6645 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6646 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6647 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6648 | Node->op_end()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6649 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6650 | break; |
| 6651 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6652 | case ISD::CONCAT_VECTORS: { |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6653 | // FIXME: Handle non-power-of-two vectors? |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6654 | unsigned NewNumSubvectors = Node->getNumOperands() / 2; |
| 6655 | if (NewNumSubvectors == 1) { |
| 6656 | Lo = Node->getOperand(0); |
| 6657 | Hi = Node->getOperand(1); |
| 6658 | } else { |
| 6659 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 6660 | Node->op_begin()+NewNumSubvectors); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6661 | Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6662 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6663 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors, |
| 6664 | Node->op_end()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6665 | Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6666 | } |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6667 | break; |
| 6668 | } |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6669 | case ISD::SELECT: { |
| 6670 | SDOperand Cond = Node->getOperand(0); |
| 6671 | |
| 6672 | SDOperand LL, LH, RL, RH; |
| 6673 | SplitVectorOp(Node->getOperand(1), LL, LH); |
| 6674 | SplitVectorOp(Node->getOperand(2), RL, RH); |
| 6675 | |
| 6676 | if (MVT::isVector(Cond.getValueType())) { |
| 6677 | // Handle a vector merge. |
| 6678 | SDOperand CL, CH; |
| 6679 | SplitVectorOp(Cond, CL, CH); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6680 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL); |
| 6681 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH); |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6682 | } else { |
| 6683 | // Handle a simple select with vector operands. |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6684 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL); |
| 6685 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH); |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6686 | } |
| 6687 | break; |
| 6688 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6689 | case ISD::ADD: |
| 6690 | case ISD::SUB: |
| 6691 | case ISD::MUL: |
| 6692 | case ISD::FADD: |
| 6693 | case ISD::FSUB: |
| 6694 | case ISD::FMUL: |
| 6695 | case ISD::SDIV: |
| 6696 | case ISD::UDIV: |
| 6697 | case ISD::FDIV: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6698 | case ISD::FPOW: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6699 | case ISD::AND: |
| 6700 | case ISD::OR: |
Dan Gohman | 089617d | 2007-11-19 15:15:03 +0000 | [diff] [blame] | 6701 | case ISD::XOR: |
| 6702 | case ISD::UREM: |
| 6703 | case ISD::SREM: |
| 6704 | case ISD::FREM: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6705 | SDOperand LL, LH, RL, RH; |
| 6706 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 6707 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 6708 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6709 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL); |
| 6710 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6711 | break; |
| 6712 | } |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6713 | case ISD::FPOWI: { |
| 6714 | SDOperand L, H; |
| 6715 | SplitVectorOp(Node->getOperand(0), L, H); |
| 6716 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6717 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1)); |
| 6718 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1)); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6719 | break; |
| 6720 | } |
| 6721 | case ISD::CTTZ: |
| 6722 | case ISD::CTLZ: |
| 6723 | case ISD::CTPOP: |
| 6724 | case ISD::FNEG: |
| 6725 | case ISD::FABS: |
| 6726 | case ISD::FSQRT: |
| 6727 | case ISD::FSIN: |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 6728 | case ISD::FCOS: |
| 6729 | case ISD::FP_TO_SINT: |
| 6730 | case ISD::FP_TO_UINT: |
| 6731 | case ISD::SINT_TO_FP: |
| 6732 | case ISD::UINT_TO_FP: { |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6733 | SDOperand L, H; |
| 6734 | SplitVectorOp(Node->getOperand(0), L, H); |
| 6735 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6736 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L); |
| 6737 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6738 | break; |
| 6739 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6740 | case ISD::LOAD: { |
| 6741 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 6742 | SDOperand Ch = LD->getChain(); |
| 6743 | SDOperand Ptr = LD->getBasePtr(); |
| 6744 | const Value *SV = LD->getSrcValue(); |
| 6745 | int SVOffset = LD->getSrcValueOffset(); |
| 6746 | unsigned Alignment = LD->getAlignment(); |
| 6747 | bool isVolatile = LD->isVolatile(); |
| 6748 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6749 | Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
| 6750 | unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6751 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6752 | DAG.getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6753 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 6754 | Alignment = MinAlign(Alignment, IncrementSize); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6755 | Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6756 | |
| 6757 | // Build a factor node to remember that this load is independent of the |
| 6758 | // other one. |
| 6759 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 6760 | Hi.getValue(1)); |
| 6761 | |
| 6762 | // Remember that we legalized the chain. |
| 6763 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6764 | break; |
| 6765 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6766 | case ISD::BIT_CONVERT: { |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6767 | // We know the result is a vector. The input may be either a vector or a |
| 6768 | // scalar value. |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6769 | SDOperand InOp = Node->getOperand(0); |
| 6770 | if (!MVT::isVector(InOp.getValueType()) || |
| 6771 | MVT::getVectorNumElements(InOp.getValueType()) == 1) { |
| 6772 | // The input is a scalar or single-element vector. |
| 6773 | // Lower to a store/load so that it can be split. |
| 6774 | // FIXME: this could be improved probably. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 6775 | SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType()); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 6776 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr.Val); |
| 6777 | assert(FI && "Expecting CreateStackTemporary to return a frame index.\n"); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6778 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 6779 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame^] | 6780 | InOp, Ptr, |
| 6781 | &PseudoSourceValue::getFixedStack(), |
| 6782 | FI->getIndex()); |
| 6783 | InOp = DAG.getLoad(Op.getValueType(), St, Ptr, |
| 6784 | &PseudoSourceValue::getFixedStack(), |
| 6785 | FI->getIndex()); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6786 | } |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6787 | // Split the vector and convert each of the pieces now. |
| 6788 | SplitVectorOp(InOp, Lo, Hi); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6789 | Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo); |
| 6790 | Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi); |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6791 | break; |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6792 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 6796 | bool isNew = |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6797 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6798 | assert(isNew && "Value already split?!?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 6802 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 6803 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 6804 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6805 | SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) { |
| 6806 | assert(MVT::isVector(Op.getValueType()) && |
| 6807 | "Bad ScalarizeVectorOp invocation!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6808 | SDNode *Node = Op.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6809 | MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType()); |
| 6810 | assert(MVT::getVectorNumElements(Op.getValueType()) == 1); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6811 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6812 | // See if we already scalarized it. |
| 6813 | std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op); |
| 6814 | if (I != ScalarizedNodes.end()) return I->second; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6815 | |
| 6816 | SDOperand Result; |
| 6817 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 6818 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6819 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 6820 | Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6821 | #endif |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6822 | assert(0 && "Unknown vector operation in ScalarizeVectorOp!"); |
| 6823 | case ISD::ADD: |
| 6824 | case ISD::FADD: |
| 6825 | case ISD::SUB: |
| 6826 | case ISD::FSUB: |
| 6827 | case ISD::MUL: |
| 6828 | case ISD::FMUL: |
| 6829 | case ISD::SDIV: |
| 6830 | case ISD::UDIV: |
| 6831 | case ISD::FDIV: |
| 6832 | case ISD::SREM: |
| 6833 | case ISD::UREM: |
| 6834 | case ISD::FREM: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6835 | case ISD::FPOW: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6836 | case ISD::AND: |
| 6837 | case ISD::OR: |
| 6838 | case ISD::XOR: |
| 6839 | Result = DAG.getNode(Node->getOpcode(), |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6840 | NewVT, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6841 | ScalarizeVectorOp(Node->getOperand(0)), |
| 6842 | ScalarizeVectorOp(Node->getOperand(1))); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6843 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6844 | case ISD::FNEG: |
| 6845 | case ISD::FABS: |
| 6846 | case ISD::FSQRT: |
| 6847 | case ISD::FSIN: |
| 6848 | case ISD::FCOS: |
| 6849 | Result = DAG.getNode(Node->getOpcode(), |
| 6850 | NewVT, |
| 6851 | ScalarizeVectorOp(Node->getOperand(0))); |
| 6852 | break; |
Dan Gohman | 9e04c82 | 2007-10-12 14:13:46 +0000 | [diff] [blame] | 6853 | case ISD::FPOWI: |
| 6854 | Result = DAG.getNode(Node->getOpcode(), |
| 6855 | NewVT, |
| 6856 | ScalarizeVectorOp(Node->getOperand(0)), |
| 6857 | Node->getOperand(1)); |
| 6858 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6859 | case ISD::LOAD: { |
| 6860 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 6861 | SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 6862 | SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6863 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6864 | const Value *SV = LD->getSrcValue(); |
| 6865 | int SVOffset = LD->getSrcValueOffset(); |
| 6866 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, |
| 6867 | LD->isVolatile(), LD->getAlignment()); |
| 6868 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6869 | // Remember that we legalized the chain. |
| 6870 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 6871 | break; |
| 6872 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6873 | case ISD::BUILD_VECTOR: |
| 6874 | Result = Node->getOperand(0); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6875 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6876 | case ISD::INSERT_VECTOR_ELT: |
| 6877 | // Returning the inserted scalar element. |
| 6878 | Result = Node->getOperand(1); |
| 6879 | break; |
| 6880 | case ISD::CONCAT_VECTORS: |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6881 | assert(Node->getOperand(0).getValueType() == NewVT && |
| 6882 | "Concat of non-legal vectors not yet supported!"); |
| 6883 | Result = Node->getOperand(0); |
| 6884 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6885 | case ISD::VECTOR_SHUFFLE: { |
| 6886 | // Figure out if the scalar is the LHS or RHS and return it. |
| 6887 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 6888 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 6889 | Result = ScalarizeVectorOp(Node->getOperand(1)); |
| 6890 | else |
| 6891 | Result = ScalarizeVectorOp(Node->getOperand(0)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 6892 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6893 | } |
| 6894 | case ISD::EXTRACT_SUBVECTOR: |
| 6895 | Result = Node->getOperand(0); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6896 | assert(Result.getValueType() == NewVT); |
| 6897 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6898 | case ISD::BIT_CONVERT: |
| 6899 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 6900 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6901 | case ISD::SELECT: |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 6902 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6903 | ScalarizeVectorOp(Op.getOperand(1)), |
| 6904 | ScalarizeVectorOp(Op.getOperand(2))); |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 6905 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | if (TLI.isTypeLegal(NewVT)) |
| 6909 | Result = LegalizeOp(Result); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6910 | bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second; |
| 6911 | assert(isNew && "Value already scalarized?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6912 | return Result; |
| 6913 | } |
| 6914 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6915 | |
| 6916 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 6917 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 6918 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 6919 | if (ViewLegalizeDAGs) viewGraph(); |
| 6920 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6921 | /// run - This is the main entry point to this class. |
| 6922 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 6923 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |