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" |
Dan Gohman | 707e018 | 2008-04-12 04:36:06 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetSubtarget.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 26 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 28 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallPtrSet.h" |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 35 | #include <map> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 38 | #ifndef NDEBUG |
| 39 | static cl::opt<bool> |
| 40 | ViewLegalizeDAGs("view-legalize-dags", cl::Hidden, |
| 41 | cl::desc("Pop up a window to show dags before legalize")); |
| 42 | #else |
| 43 | static const bool ViewLegalizeDAGs = 0; |
| 44 | #endif |
| 45 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 48 | /// hacks on it until the target machine can handle it. This involves |
| 49 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 50 | /// large sizes or splitting up large values into small values) as well as |
| 51 | /// eliminating operations the machine cannot handle. |
| 52 | /// |
| 53 | /// This code also does a small amount of optimization and recognition of idioms |
| 54 | /// as part of its processing. For example, if a target does not support a |
| 55 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 56 | /// will attempt merge setcc and brc instructions into brcc's. |
| 57 | /// |
| 58 | namespace { |
Chris Lattner | 360e820 | 2006-06-28 21:58:30 +0000 | [diff] [blame] | 59 | class VISIBILITY_HIDDEN SelectionDAGLegalize { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 60 | TargetLowering &TLI; |
| 61 | SelectionDAG &DAG; |
| 62 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 63 | // Libcall insertion helpers. |
| 64 | |
| 65 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 66 | /// legalized. We use this to ensure that calls are properly serialized |
| 67 | /// against each other, including inserted libcalls. |
| 68 | SDOperand LastCALLSEQ_END; |
| 69 | |
| 70 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 71 | /// helpful assertions that a libcall isn't created while another call is |
| 72 | /// being legalized (which could lead to non-serialized call sequences). |
| 73 | bool IsLegalizingCall; |
| 74 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 75 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 76 | Legal, // The target natively supports this operation. |
| 77 | Promote, // This operation should be executed in a larger type. |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 78 | Expand // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 79 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 81 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 82 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 83 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 84 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 86 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 87 | /// than one use, this map indicates what regularized operand to use. This |
| 88 | /// allows us to avoid legalizing the same thing more than once. |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 89 | DenseMap<SDOperand, SDOperand> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 91 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 92 | /// than one use, this map indicates what promoted value to use. This allows |
| 93 | /// us to avoid promoting the same thing more than once. |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 94 | DenseMap<SDOperand, SDOperand> PromotedNodes; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 95 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 96 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 97 | /// which which operands are the expanded version of the input. This allows |
| 98 | /// us to avoid expanding the same node more than once. |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 99 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 100 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 101 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 102 | /// which which operands are the split version of the input. This allows us |
| 103 | /// to avoid splitting the same node more than once. |
| 104 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 105 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 106 | /// ScalarizedNodes - For nodes that need to be converted from vector types to |
| 107 | /// scalar types, this contains the mapping of ones we have already |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 108 | /// processed to the result. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 109 | std::map<SDOperand, SDOperand> ScalarizedNodes; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 111 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 112 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 113 | // If someone requests legalization of the new node, return itself. |
| 114 | if (From != To) |
| 115 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 117 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 118 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 119 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 120 | // If someone requests legalization of the new node, return itself. |
| 121 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 124 | public: |
| 125 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 126 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 128 | /// getTypeAction - Return how we should legalize values of this type, either |
| 129 | /// it is already legal or we need to expand it into multiple registers of |
| 130 | /// smaller integer type, or we need to promote it to a larger type. |
| 131 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 132 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /// isTypeLegal - Return true if this type is legal on this target. |
| 136 | /// |
| 137 | bool isTypeLegal(MVT::ValueType VT) const { |
| 138 | return getTypeAction(VT) == Legal; |
| 139 | } |
| 140 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 141 | void LegalizeDAG(); |
| 142 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 143 | private: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 144 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 145 | /// appropriate for its type. |
| 146 | void HandleOp(SDOperand Op); |
| 147 | |
| 148 | /// LegalizeOp - We know that the specified value has a legal type. |
| 149 | /// Recursively ensure that the operands have legal types, then return the |
| 150 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 151 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 152 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 153 | /// UnrollVectorOp - We know that the given vector has a legal type, however |
| 154 | /// the operation it performs is not legal and is an operation that we have |
| 155 | /// no way of lowering. "Unroll" the vector, splitting out the scalars and |
| 156 | /// operating on each element individually. |
| 157 | SDOperand UnrollVectorOp(SDOperand O); |
| 158 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 159 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 160 | /// promote it to compute the value into a larger type. The produced value |
| 161 | /// will have the correct bits for the low portion of the register, but no |
| 162 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 163 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 164 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 165 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 166 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 167 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 168 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 169 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 170 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 171 | /// types. |
| 172 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 173 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 174 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 175 | /// two smaller values. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 176 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 177 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 178 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 179 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 180 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 181 | SDOperand ScalarizeVectorOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 183 | /// isShuffleLegal - Return true if a vector shuffle is legal with the |
| 184 | /// specified mask and type. Targets can specify exactly which masks they |
| 185 | /// support and the code generator is tasked with not creating illegal masks. |
| 186 | /// |
| 187 | /// Note that this will also return true for shuffles that are promoted to a |
| 188 | /// different type. |
| 189 | /// |
| 190 | /// If this is a legal shuffle, this method returns the (possibly promoted) |
| 191 | /// build_vector Mask. If it's not a legal shuffle, it returns null. |
| 192 | SDNode *isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const; |
| 193 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 194 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 195 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 196 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 197 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 198 | |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 199 | SDOperand ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned, |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 200 | SDOperand &Hi); |
| 201 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 202 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 204 | SDOperand EmitStackConvert(SDOperand SrcOp, MVT::ValueType SlotVT, |
| 205 | MVT::ValueType DestVT); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 206 | SDOperand ExpandBUILD_VECTOR(SDNode *Node); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 207 | SDOperand ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 208 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 209 | SDOperand LegalOp, |
| 210 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 211 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 212 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 213 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 214 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 216 | SDOperand ExpandBSWAP(SDOperand Op); |
| 217 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 218 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 219 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 220 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 221 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 222 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 223 | SDOperand ExpandEXTRACT_SUBVECTOR(SDOperand Op); |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 224 | SDOperand ExpandEXTRACT_VECTOR_ELT(SDOperand Op); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 225 | }; |
| 226 | } |
| 227 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 228 | /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the |
| 229 | /// specified mask and type. Targets can specify exactly which masks they |
| 230 | /// support and the code generator is tasked with not creating illegal masks. |
| 231 | /// |
| 232 | /// Note that this will also return true for shuffles that are promoted to a |
| 233 | /// different type. |
| 234 | SDNode *SelectionDAGLegalize::isShuffleLegal(MVT::ValueType VT, |
| 235 | SDOperand Mask) const { |
| 236 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) { |
| 237 | default: return 0; |
| 238 | case TargetLowering::Legal: |
| 239 | case TargetLowering::Custom: |
| 240 | break; |
| 241 | case TargetLowering::Promote: { |
| 242 | // If this is promoted to a different type, convert the shuffle mask and |
| 243 | // ask if it is legal in the promoted type! |
| 244 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT); |
| 245 | |
| 246 | // If we changed # elements, change the shuffle mask. |
| 247 | unsigned NumEltsGrowth = |
| 248 | MVT::getVectorNumElements(NVT) / MVT::getVectorNumElements(VT); |
| 249 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 250 | if (NumEltsGrowth > 1) { |
| 251 | // Renumber the elements. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 252 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 253 | for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) { |
| 254 | SDOperand InOp = Mask.getOperand(i); |
| 255 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
| 256 | if (InOp.getOpcode() == ISD::UNDEF) |
| 257 | Ops.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 258 | else { |
| 259 | unsigned InEltNo = cast<ConstantSDNode>(InOp)->getValue(); |
| 260 | Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, MVT::i32)); |
| 261 | } |
| 262 | } |
| 263 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 264 | Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 265 | } |
| 266 | VT = NVT; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.Val : 0; |
| 271 | } |
| 272 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 273 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 274 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 275 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 276 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 277 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 280 | /// ComputeTopDownOrdering - Compute a top-down ordering of the dag, where Order |
| 281 | /// contains all of a nodes operands before it contains the node. |
| 282 | static void ComputeTopDownOrdering(SelectionDAG &DAG, |
| 283 | SmallVector<SDNode*, 64> &Order) { |
| 284 | |
| 285 | DenseMap<SDNode*, unsigned> Visited; |
| 286 | std::vector<SDNode*> Worklist; |
| 287 | Worklist.reserve(128); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 288 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 289 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 290 | // entry node) that have no operands. |
| 291 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 292 | E = DAG.allnodes_end(); I != E; ++I) { |
| 293 | if (I->getNumOperands() == 0) { |
| 294 | Visited[I] = 0 - 1U; |
| 295 | Worklist.push_back(I); |
| 296 | } |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 299 | while (!Worklist.empty()) { |
| 300 | SDNode *N = Worklist.back(); |
| 301 | Worklist.pop_back(); |
| 302 | |
| 303 | if (++Visited[N] != N->getNumOperands()) |
| 304 | continue; // Haven't visited all operands yet |
| 305 | |
| 306 | Order.push_back(N); |
| 307 | |
| 308 | // Now that we have N in, add anything that uses it if all of their operands |
| 309 | // are now done. |
| 310 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 311 | UI != E; ++UI) |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 312 | Worklist.push_back(UI->getUser()); |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | assert(Order.size() == Visited.size() && |
| 316 | Order.size() == |
| 317 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
| 318 | "Error: DAG is cyclic!"); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 322 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 323 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 324 | IsLegalizingCall = false; |
| 325 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 326 | // The legalize process is inherently a bottom-up recursive process (users |
| 327 | // legalize their uses before themselves). Given infinite stack space, we |
| 328 | // could just start legalizing on the root and traverse the whole graph. In |
| 329 | // 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] | 330 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 331 | // node is only legalized after all of its operands are legalized. |
Chris Lattner | 0ed4417 | 2007-02-04 01:20:02 +0000 | [diff] [blame] | 332 | SmallVector<SDNode*, 64> Order; |
Chris Lattner | e10e6f7 | 2007-06-18 21:28:10 +0000 | [diff] [blame] | 333 | ComputeTopDownOrdering(DAG, Order); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 334 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 335 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 336 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 337 | |
| 338 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 339 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 340 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 341 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 342 | |
| 343 | ExpandedNodes.clear(); |
| 344 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 345 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 346 | SplitNodes.clear(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 347 | ScalarizedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 348 | |
| 349 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 350 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 353 | |
| 354 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 355 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 356 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 357 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 358 | return Node; |
| 359 | if (Node->use_empty()) |
| 360 | return 0; // No CallSeqEnd |
| 361 | |
| 362 | // The chain is usually at the end. |
| 363 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 364 | if (TheChain.getValueType() != MVT::Other) { |
| 365 | // Sometimes it's at the beginning. |
| 366 | TheChain = SDOperand(Node, 0); |
| 367 | if (TheChain.getValueType() != MVT::Other) { |
| 368 | // Otherwise, hunt for it. |
| 369 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 370 | if (Node->getValueType(i) == MVT::Other) { |
| 371 | TheChain = SDOperand(Node, i); |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | // Otherwise, we walked into a node without a chain. |
| 376 | if (TheChain.getValueType() != MVT::Other) |
| 377 | return 0; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 382 | E = Node->use_end(); UI != E; ++UI) { |
| 383 | |
| 384 | // Make sure to only follow users of our token chain. |
Roman Levenstein | dc1adac | 2008-04-07 10:06:32 +0000 | [diff] [blame] | 385 | SDNode *User = UI->getUser(); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 386 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 387 | if (User->getOperand(i) == TheChain) |
| 388 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 389 | return Result; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 395 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 396 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 397 | assert(Node && "Didn't find callseq_start for a call??"); |
| 398 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 399 | |
| 400 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 401 | "Node doesn't have a token chain argument!"); |
| 402 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 403 | } |
| 404 | |
| 405 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 406 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 407 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 408 | /// |
| 409 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 410 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 411 | /// |
| 412 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 413 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 414 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 415 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 416 | // If we've already processed this node and it does lead to Dest, there is no |
| 417 | // need to reprocess it. |
| 418 | if (NodesLeadingTo.count(N)) return true; |
| 419 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 420 | // If the first result of this node has been already legalized, then it cannot |
| 421 | // reach N. |
| 422 | switch (getTypeAction(N->getValueType(0))) { |
| 423 | case Legal: |
| 424 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 425 | break; |
| 426 | case Promote: |
| 427 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 428 | break; |
| 429 | case Expand: |
| 430 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | // Okay, this node has not already been legalized. Check and legalize all |
| 435 | // operands. If none lead to Dest, then we can legalize this node. |
| 436 | bool OperandsLeadToDest = false; |
| 437 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 438 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 439 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest, NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 440 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 441 | if (OperandsLeadToDest) { |
| 442 | NodesLeadingTo.insert(N); |
| 443 | return true; |
| 444 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 445 | |
| 446 | // Okay, this node looks safe, legalize it and return false. |
Chris Lattner | 80edfb3 | 2006-04-17 22:10:08 +0000 | [diff] [blame] | 447 | HandleOp(SDOperand(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 448 | return false; |
| 449 | } |
| 450 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 451 | /// HandleOp - Legalize, Promote, or Expand the specified operand as |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 452 | /// appropriate for its type. |
| 453 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 454 | MVT::ValueType VT = Op.getValueType(); |
| 455 | switch (getTypeAction(VT)) { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 456 | default: assert(0 && "Bad type action!"); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 457 | case Legal: (void)LegalizeOp(Op); break; |
| 458 | case Promote: (void)PromoteOp(Op); break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 459 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 460 | if (!MVT::isVector(VT)) { |
| 461 | // If this is an illegal scalar, expand it into its two component |
| 462 | // pieces. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 463 | SDOperand X, Y; |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 464 | if (Op.getOpcode() == ISD::TargetConstant) |
| 465 | break; // Allow illegal target nodes. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 466 | ExpandOp(Op, X, Y); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 467 | } else if (MVT::getVectorNumElements(VT) == 1) { |
| 468 | // If this is an illegal single element vector, convert it to a |
| 469 | // scalar operation. |
| 470 | (void)ScalarizeVectorOp(Op); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 471 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 472 | // Otherwise, this is an illegal multiple element vector. |
| 473 | // Split it in half and legalize both parts. |
| 474 | SDOperand X, Y; |
| 475 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 476 | } |
| 477 | break; |
| 478 | } |
| 479 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 480 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 481 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 482 | /// a load from the constant pool. |
| 483 | static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 484 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 485 | bool Extend = false; |
| 486 | |
| 487 | // If a FP immediate is precise when represented as a float and if the |
| 488 | // target can do an extending load from float to double, we put it into |
| 489 | // the constant pool as a float, even if it's is statically typed as a |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 490 | // double. This shrinks FP constants and canonicalizes them for targets where |
| 491 | // an FP extending load is the same cost as a normal load (such as on the x87 |
| 492 | // fp stack or PPC FP unit). |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 493 | MVT::ValueType VT = CFP->getValueType(0); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 494 | ConstantFP *LLVMC = ConstantFP::get(MVT::getTypeForValueType(VT), |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 495 | CFP->getValueAPF()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 496 | if (!UseCP) { |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 497 | if (VT!=MVT::f64 && VT!=MVT::f32) |
| 498 | assert(0 && "Invalid type expansion"); |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 499 | return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt(), |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 500 | (VT == MVT::f64) ? MVT::i64 : MVT::i32); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 503 | MVT::ValueType OrigVT = VT; |
| 504 | MVT::ValueType SVT = VT; |
| 505 | while (SVT != MVT::f32) { |
| 506 | SVT = (unsigned)SVT - 1; |
| 507 | if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) && |
| 508 | // Only do this if the target has a native EXTLOAD instruction from |
| 509 | // smaller type. |
Evan Cheng | 6fd599f | 2008-03-05 01:30:59 +0000 | [diff] [blame] | 510 | TLI.isLoadXLegal(ISD::EXTLOAD, SVT) && |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 511 | TLI.ShouldShrinkFPConstant(OrigVT)) { |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 512 | const Type *SType = MVT::getTypeForValueType(SVT); |
| 513 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); |
| 514 | VT = SVT; |
| 515 | Extend = true; |
| 516 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 520 | if (Extend) |
| 521 | return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(), |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 522 | CPIdx, PseudoSourceValue::getConstantPool(), |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 523 | 0, VT); |
| 524 | return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx, |
| 525 | PseudoSourceValue::getConstantPool(), 0); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 528 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 529 | /// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise |
| 530 | /// operations. |
| 531 | static |
| 532 | SDOperand ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT::ValueType NVT, |
| 533 | SelectionDAG &DAG, TargetLowering &TLI) { |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 534 | MVT::ValueType VT = Node->getValueType(0); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 535 | MVT::ValueType SrcVT = Node->getOperand(1).getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 536 | assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) && |
| 537 | "fcopysign expansion only supported for f32 and f64"); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 538 | MVT::ValueType SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32; |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 539 | |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 540 | // First get the sign bit of second operand. |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 541 | SDOperand Mask1 = (SrcVT == MVT::f64) |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 542 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT) |
| 543 | : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 544 | Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 545 | SDOperand SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 546 | SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 547 | // Shift right or sign-extend it if the two operands have different types. |
| 548 | int SizeDiff = MVT::getSizeInBits(SrcNVT) - MVT::getSizeInBits(NVT); |
| 549 | if (SizeDiff > 0) { |
| 550 | SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit, |
| 551 | DAG.getConstant(SizeDiff, TLI.getShiftAmountTy())); |
| 552 | SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit); |
| 553 | } else if (SizeDiff < 0) |
| 554 | SignBit = DAG.getNode(ISD::SIGN_EXTEND, NVT, SignBit); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 555 | |
| 556 | // Clear the sign bit of first operand. |
| 557 | SDOperand Mask2 = (VT == MVT::f64) |
| 558 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 559 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 560 | Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2); |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 561 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 068c5f4 | 2007-01-05 21:31:51 +0000 | [diff] [blame] | 562 | Result = DAG.getNode(ISD::AND, NVT, Result, Mask2); |
| 563 | |
| 564 | // Or the value with the sign bit. |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 565 | Result = DAG.getNode(ISD::OR, NVT, Result, SignBit); |
| 566 | return Result; |
| 567 | } |
| 568 | |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 569 | /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. |
| 570 | static |
| 571 | SDOperand ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, |
| 572 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 573 | SDOperand Chain = ST->getChain(); |
| 574 | SDOperand Ptr = ST->getBasePtr(); |
| 575 | SDOperand Val = ST->getValue(); |
| 576 | MVT::ValueType VT = Val.getValueType(); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 577 | int Alignment = ST->getAlignment(); |
| 578 | int SVOffset = ST->getSrcValueOffset(); |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 579 | if (MVT::isFloatingPoint(ST->getMemoryVT()) || |
| 580 | MVT::isVector(ST->getMemoryVT())) { |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 581 | // Expand to a bitconvert of the value to the integer type of the |
| 582 | // same size, then a (misaligned) int store. |
| 583 | MVT::ValueType intVT; |
Dale Johannesen | 3c8b59c | 2008-03-01 03:40:57 +0000 | [diff] [blame] | 584 | if (MVT::is128BitVector(VT) || VT == MVT::ppcf128 || VT == MVT::f128) |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 585 | intVT = MVT::i128; |
Dale Johannesen | 3c8b59c | 2008-03-01 03:40:57 +0000 | [diff] [blame] | 586 | else if (MVT::is64BitVector(VT) || VT==MVT::f64) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 587 | intVT = MVT::i64; |
| 588 | else if (VT==MVT::f32) |
| 589 | intVT = MVT::i32; |
| 590 | else |
Dale Johannesen | cd9f174 | 2008-02-28 18:36:51 +0000 | [diff] [blame] | 591 | assert(0 && "Unaligned store of unsupported type"); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 592 | |
| 593 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val); |
| 594 | return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(), |
| 595 | SVOffset, ST->isVolatile(), Alignment); |
| 596 | } |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 597 | assert(MVT::isInteger(ST->getMemoryVT()) && |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 598 | !MVT::isVector(ST->getMemoryVT()) && |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 599 | "Unaligned store of unknown type."); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 600 | // Get the half-size VT |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 601 | MVT::ValueType NewStoredVT = ST->getMemoryVT() - 1; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 602 | int NumBits = MVT::getSizeInBits(NewStoredVT); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 603 | int IncrementSize = NumBits / 8; |
| 604 | |
| 605 | // Divide the stored value in two parts. |
| 606 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 607 | SDOperand Lo = Val; |
| 608 | SDOperand Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount); |
| 609 | |
| 610 | // Store the two parts |
| 611 | SDOperand Store1, Store2; |
| 612 | Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr, |
| 613 | ST->getSrcValue(), SVOffset, NewStoredVT, |
| 614 | ST->isVolatile(), Alignment); |
| 615 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 616 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 617 | Alignment = MinAlign(Alignment, IncrementSize); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 618 | Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr, |
| 619 | ST->getSrcValue(), SVOffset + IncrementSize, |
| 620 | NewStoredVT, ST->isVolatile(), Alignment); |
| 621 | |
| 622 | return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2); |
| 623 | } |
| 624 | |
| 625 | /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. |
| 626 | static |
| 627 | SDOperand ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, |
| 628 | TargetLowering &TLI) { |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 629 | int SVOffset = LD->getSrcValueOffset(); |
| 630 | SDOperand Chain = LD->getChain(); |
| 631 | SDOperand Ptr = LD->getBasePtr(); |
| 632 | MVT::ValueType VT = LD->getValueType(0); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 633 | MVT::ValueType LoadedVT = LD->getMemoryVT(); |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 634 | if (MVT::isFloatingPoint(VT) || MVT::isVector(VT)) { |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 635 | // Expand to a (misaligned) integer load of the same size, |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 636 | // then bitconvert to floating point or vector. |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 637 | MVT::ValueType intVT; |
Dale Johannesen | 3c8b59c | 2008-03-01 03:40:57 +0000 | [diff] [blame] | 638 | if (MVT::is128BitVector(LoadedVT) || |
| 639 | LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128) |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 640 | intVT = MVT::i128; |
Dale Johannesen | 3c8b59c | 2008-03-01 03:40:57 +0000 | [diff] [blame] | 641 | else if (MVT::is64BitVector(LoadedVT) || LoadedVT == MVT::f64) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 642 | intVT = MVT::i64; |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 643 | else if (LoadedVT == MVT::f32) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 644 | intVT = MVT::i32; |
| 645 | else |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 646 | assert(0 && "Unaligned load of unsupported type"); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 647 | |
| 648 | SDOperand newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(), |
| 649 | SVOffset, LD->isVolatile(), |
| 650 | LD->getAlignment()); |
| 651 | SDOperand Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad); |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 652 | if (MVT::isFloatingPoint(VT) && LoadedVT != VT) |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 653 | Result = DAG.getNode(ISD::FP_EXTEND, VT, Result); |
| 654 | |
| 655 | SDOperand Ops[] = { Result, Chain }; |
| 656 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
| 657 | Ops, 2); |
| 658 | } |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 659 | assert(MVT::isInteger(LoadedVT) && !MVT::isVector(LoadedVT) && |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 660 | "Unaligned load of unsupported type."); |
| 661 | |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 662 | // Compute the new VT that is half the size of the old one. This is an |
| 663 | // integer MVT. |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 664 | unsigned NumBits = MVT::getSizeInBits(LoadedVT); |
| 665 | MVT::ValueType NewLoadedVT; |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 666 | NewLoadedVT = MVT::getIntegerType(NumBits/2); |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 667 | NumBits >>= 1; |
| 668 | |
| 669 | unsigned Alignment = LD->getAlignment(); |
| 670 | unsigned IncrementSize = NumBits / 8; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 671 | ISD::LoadExtType HiExtType = LD->getExtensionType(); |
| 672 | |
| 673 | // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. |
| 674 | if (HiExtType == ISD::NON_EXTLOAD) |
| 675 | HiExtType = ISD::ZEXTLOAD; |
| 676 | |
| 677 | // Load the value in two parts |
| 678 | SDOperand Lo, Hi; |
| 679 | if (TLI.isLittleEndian()) { |
| 680 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 681 | SVOffset, NewLoadedVT, LD->isVolatile(), Alignment); |
| 682 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 683 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 684 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), |
| 685 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 686 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 687 | } else { |
| 688 | Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset, |
| 689 | NewLoadedVT,LD->isVolatile(), Alignment); |
| 690 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 691 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
| 692 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(), |
| 693 | SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(), |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 694 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // aggregate the two parts |
| 698 | SDOperand ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy()); |
| 699 | SDOperand Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount); |
| 700 | Result = DAG.getNode(ISD::OR, VT, Result, Lo); |
| 701 | |
| 702 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 703 | Hi.getValue(1)); |
| 704 | |
| 705 | SDOperand Ops[] = { Result, TF }; |
| 706 | return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), Ops, 2); |
| 707 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 708 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 709 | /// UnrollVectorOp - We know that the given vector has a legal type, however |
| 710 | /// the operation it performs is not legal and is an operation that we have |
| 711 | /// no way of lowering. "Unroll" the vector, splitting out the scalars and |
| 712 | /// operating on each element individually. |
| 713 | SDOperand SelectionDAGLegalize::UnrollVectorOp(SDOperand Op) { |
| 714 | MVT::ValueType VT = Op.getValueType(); |
| 715 | assert(isTypeLegal(VT) && |
| 716 | "Caller should expand or promote operands that are not legal!"); |
| 717 | assert(Op.Val->getNumValues() == 1 && |
| 718 | "Can't unroll a vector with multiple results!"); |
| 719 | unsigned NE = MVT::getVectorNumElements(VT); |
| 720 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
| 721 | |
| 722 | SmallVector<SDOperand, 8> Scalars; |
| 723 | SmallVector<SDOperand, 4> Operands(Op.getNumOperands()); |
| 724 | for (unsigned i = 0; i != NE; ++i) { |
| 725 | for (unsigned j = 0; j != Op.getNumOperands(); ++j) { |
| 726 | SDOperand Operand = Op.getOperand(j); |
| 727 | MVT::ValueType OperandVT = Operand.getValueType(); |
| 728 | if (MVT::isVector(OperandVT)) { |
| 729 | // A vector operand; extract a single element. |
| 730 | MVT::ValueType OperandEltVT = MVT::getVectorElementType(OperandVT); |
| 731 | Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, |
| 732 | OperandEltVT, |
| 733 | Operand, |
| 734 | DAG.getConstant(i, MVT::i32)); |
| 735 | } else { |
| 736 | // A scalar operand; just use it as is. |
| 737 | Operands[j] = Operand; |
| 738 | } |
| 739 | } |
| 740 | Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, |
| 741 | &Operands[0], Operands.size())); |
| 742 | } |
| 743 | |
| 744 | return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size()); |
| 745 | } |
| 746 | |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 747 | /// GetFPLibCall - Return the right libcall for the given floating point type. |
| 748 | static RTLIB::Libcall GetFPLibCall(MVT::ValueType VT, |
| 749 | RTLIB::Libcall Call_F32, |
| 750 | RTLIB::Libcall Call_F64, |
| 751 | RTLIB::Libcall Call_F80, |
| 752 | RTLIB::Libcall Call_PPCF128) { |
| 753 | return |
| 754 | VT == MVT::f32 ? Call_F32 : |
| 755 | VT == MVT::f64 ? Call_F64 : |
| 756 | VT == MVT::f80 ? Call_F80 : |
| 757 | VT == MVT::ppcf128 ? Call_PPCF128 : |
| 758 | RTLIB::UNKNOWN_LIBCALL; |
| 759 | } |
| 760 | |
Dan Gohman | a346615 | 2007-07-13 20:14:11 +0000 | [diff] [blame] | 761 | /// LegalizeOp - We know that the specified value has a legal type, and |
| 762 | /// that its operands are legal. Now ensure that the operation itself |
| 763 | /// is legal, recursively ensuring that the operands' operations remain |
| 764 | /// legal. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 765 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 766 | if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. |
| 767 | return Op; |
| 768 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 769 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 770 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 771 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 773 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 774 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 775 | if (Node->getNumValues() > 1) { |
| 776 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 777 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 778 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 779 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 780 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 781 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 785 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 786 | // means that we always must cache transformed nodes. |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 787 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 788 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 789 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 790 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 791 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 792 | bool isCustom = false; |
| 793 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 794 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 795 | case ISD::FrameIndex: |
| 796 | case ISD::EntryToken: |
| 797 | case ISD::Register: |
| 798 | case ISD::BasicBlock: |
| 799 | case ISD::TargetFrameIndex: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 800 | case ISD::TargetJumpTable: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 801 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 802 | case ISD::TargetConstantFP: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 803 | case ISD::TargetConstantPool: |
| 804 | case ISD::TargetGlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 805 | case ISD::TargetGlobalTLSAddress: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 806 | case ISD::TargetExternalSymbol: |
| 807 | case ISD::VALUETYPE: |
| 808 | case ISD::SRCVALUE: |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 809 | case ISD::MEMOPERAND: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 810 | case ISD::STRING: |
| 811 | case ISD::CONDCODE: |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 812 | case ISD::ARG_FLAGS: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 813 | // Primitives must all be legal. |
Duncan Sands | a7c97a7 | 2007-10-16 09:07:20 +0000 | [diff] [blame] | 814 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 815 | "This must be legal!"); |
| 816 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 817 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 818 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 819 | // If this is a target node, legalize it by legalizing the operands then |
| 820 | // passing it through. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 821 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 822 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 823 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 824 | |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 825 | Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size()); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 826 | |
| 827 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 828 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 829 | return Result.getValue(Op.ResNo); |
| 830 | } |
| 831 | // Otherwise this is an unhandled builtin node. splat. |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 832 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 833 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 834 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 835 | assert(0 && "Do not know how to legalize this operator!"); |
| 836 | abort(); |
Lauro Ramos Venancio | 0d3b678 | 2007-04-20 23:02:39 +0000 | [diff] [blame] | 837 | case ISD::GLOBAL_OFFSET_TABLE: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 838 | case ISD::GlobalAddress: |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 839 | case ISD::GlobalTLSAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 840 | case ISD::ExternalSymbol: |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 841 | case ISD::ConstantPool: |
| 842 | case ISD::JumpTable: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 843 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 844 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 845 | case TargetLowering::Custom: |
| 846 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 847 | if (Tmp1.Val) Result = Tmp1; |
| 848 | // 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] | 849 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 850 | break; |
| 851 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 852 | break; |
Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 853 | case ISD::FRAMEADDR: |
| 854 | case ISD::RETURNADDR: |
| 855 | // The only option for these nodes is to custom lower them. If the target |
| 856 | // does not custom lower them, then return zero. |
| 857 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 858 | if (Tmp1.Val) |
| 859 | Result = Tmp1; |
| 860 | else |
| 861 | Result = DAG.getConstant(0, TLI.getPointerTy()); |
| 862 | break; |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 863 | case ISD::FRAME_TO_ARGS_OFFSET: { |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 864 | MVT::ValueType VT = Node->getValueType(0); |
| 865 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 866 | default: assert(0 && "This action is not supported yet!"); |
| 867 | case TargetLowering::Custom: |
| 868 | Result = TLI.LowerOperation(Op, DAG); |
| 869 | if (Result.Val) break; |
| 870 | // Fall Thru |
| 871 | case TargetLowering::Legal: |
| 872 | Result = DAG.getConstant(0, VT); |
| 873 | break; |
| 874 | } |
Anton Korobeynikov | 055c544 | 2007-08-29 23:18:48 +0000 | [diff] [blame] | 875 | } |
Anton Korobeynikov | 066f7b4 | 2007-08-29 19:28:29 +0000 | [diff] [blame] | 876 | break; |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 877 | case ISD::EXCEPTIONADDR: { |
| 878 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 879 | MVT::ValueType VT = Node->getValueType(0); |
| 880 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 881 | default: assert(0 && "This action is not supported yet!"); |
| 882 | case TargetLowering::Expand: { |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 883 | unsigned Reg = TLI.getExceptionAddressRegister(); |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 884 | Result = DAG.getCopyFromReg(Tmp1, Reg, VT); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 885 | } |
| 886 | break; |
| 887 | case TargetLowering::Custom: |
| 888 | Result = TLI.LowerOperation(Op, DAG); |
| 889 | if (Result.Val) break; |
| 890 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 891 | case TargetLowering::Legal: { |
| 892 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp1 }; |
| 893 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 894 | Ops, 2); |
Jim Laskey | 2bc210d | 2007-02-22 15:37:19 +0000 | [diff] [blame] | 895 | break; |
| 896 | } |
| 897 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 898 | } |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 899 | if (Result.Val->getNumValues() == 1) break; |
| 900 | |
| 901 | assert(Result.Val->getNumValues() == 2 && |
| 902 | "Cannot return more than two values!"); |
| 903 | |
| 904 | // Since we produced two values, make sure to remember that we |
| 905 | // legalized both of them. |
| 906 | Tmp1 = LegalizeOp(Result); |
| 907 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 908 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 909 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
| 910 | return Op.ResNo ? Tmp2 : Tmp1; |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 911 | case ISD::EHSELECTION: { |
| 912 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 913 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 914 | MVT::ValueType VT = Node->getValueType(0); |
| 915 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 916 | default: assert(0 && "This action is not supported yet!"); |
| 917 | case TargetLowering::Expand: { |
| 918 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 919 | Result = DAG.getCopyFromReg(Tmp2, Reg, VT); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 920 | } |
| 921 | break; |
| 922 | case TargetLowering::Custom: |
| 923 | Result = TLI.LowerOperation(Op, DAG); |
| 924 | if (Result.Val) break; |
| 925 | // Fall Thru |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 926 | case TargetLowering::Legal: { |
| 927 | SDOperand Ops[] = { DAG.getConstant(0, VT), Tmp2 }; |
| 928 | Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(VT, MVT::Other), |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 929 | Ops, 2); |
Jim Laskey | 8782d48 | 2007-02-28 20:43:58 +0000 | [diff] [blame] | 930 | break; |
| 931 | } |
| 932 | } |
Chris Lattner | eb7f34f | 2007-04-27 17:12:52 +0000 | [diff] [blame] | 933 | } |
Duncan Sands | b027fa0 | 2007-12-31 18:35:50 +0000 | [diff] [blame] | 934 | if (Result.Val->getNumValues() == 1) break; |
| 935 | |
| 936 | assert(Result.Val->getNumValues() == 2 && |
| 937 | "Cannot return more than two values!"); |
| 938 | |
| 939 | // Since we produced two values, make sure to remember that we |
| 940 | // legalized both of them. |
| 941 | Tmp1 = LegalizeOp(Result); |
| 942 | Tmp2 = LegalizeOp(Result.getValue(1)); |
| 943 | AddLegalizedOperand(Op.getValue(0), Tmp1); |
| 944 | AddLegalizedOperand(Op.getValue(1), Tmp2); |
| 945 | return Op.ResNo ? Tmp2 : Tmp1; |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 946 | case ISD::EH_RETURN: { |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 947 | MVT::ValueType VT = Node->getValueType(0); |
| 948 | // The only "good" option for this node is to custom lower it. |
| 949 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 950 | default: assert(0 && "This action is not supported at all!"); |
| 951 | case TargetLowering::Custom: |
| 952 | Result = TLI.LowerOperation(Op, DAG); |
| 953 | if (Result.Val) break; |
| 954 | // Fall Thru |
| 955 | case TargetLowering::Legal: |
| 956 | // Target does not know, how to lower this, lower to noop |
| 957 | Result = LegalizeOp(Node->getOperand(0)); |
| 958 | break; |
| 959 | } |
Nick Lewycky | 6d4b711 | 2007-07-14 15:11:14 +0000 | [diff] [blame] | 960 | } |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 961 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 962 | case ISD::AssertSext: |
| 963 | case ISD::AssertZext: |
| 964 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 965 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 966 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 967 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 968 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 969 | Result = Node->getOperand(Op.ResNo); |
| 970 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 971 | case ISD::CopyFromReg: |
| 972 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 973 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 974 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 975 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 976 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 977 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 978 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 979 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 980 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 981 | } else { |
| 982 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 983 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 984 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 985 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 986 | // Since CopyFromReg produces two values, make sure to remember that we |
| 987 | // legalized both of them. |
| 988 | AddLegalizedOperand(Op.getValue(0), Result); |
| 989 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 990 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 991 | case ISD::UNDEF: { |
| 992 | MVT::ValueType VT = Op.getValueType(); |
| 993 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 994 | default: assert(0 && "This action is not supported yet!"); |
| 995 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 996 | if (MVT::isInteger(VT)) |
| 997 | Result = DAG.getConstant(0, VT); |
| 998 | else if (MVT::isFloatingPoint(VT)) |
Dale Johannesen | f41db21 | 2007-09-26 17:26:49 +0000 | [diff] [blame] | 999 | Result = DAG.getConstantFP(APFloat(APInt(MVT::getSizeInBits(VT), 0)), |
| 1000 | VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1001 | else |
| 1002 | assert(0 && "Unknown value type!"); |
| 1003 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 1004 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } |
| 1007 | break; |
| 1008 | } |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1010 | case ISD::INTRINSIC_W_CHAIN: |
| 1011 | case ISD::INTRINSIC_WO_CHAIN: |
| 1012 | case ISD::INTRINSIC_VOID: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1013 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1014 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1015 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1016 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Allow the target to custom lower its intrinsics if it wants to. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 1019 | if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) == |
Chris Lattner | 10d7fa6 | 2006-03-26 09:12:51 +0000 | [diff] [blame] | 1020 | TargetLowering::Custom) { |
| 1021 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1022 | if (Tmp3.Val) Result = Tmp3; |
Chris Lattner | 13fc2f1 | 2006-03-27 20:28:29 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | if (Result.Val->getNumValues() == 1) break; |
| 1026 | |
| 1027 | // Must have return value and chain result. |
| 1028 | assert(Result.Val->getNumValues() == 2 && |
| 1029 | "Cannot return more than two values!"); |
| 1030 | |
| 1031 | // Since loads produce two values, make sure to remember that we |
| 1032 | // legalized both of them. |
| 1033 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1034 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1035 | return Result.getValue(Op.ResNo); |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 1036 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1037 | |
| 1038 | case ISD::LOCATION: |
| 1039 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 1040 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 1041 | |
| 1042 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 1043 | case TargetLowering::Promote: |
| 1044 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1045 | case TargetLowering::Expand: { |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1046 | MachineModuleInfo *MMI = DAG.getMachineModuleInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1047 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1048 | bool useLABEL = TLI.isOperationLegal(ISD::LABEL, MVT::Other); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1049 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1050 | if (MMI && (useDEBUG_LOC || useLABEL)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1051 | const std::string &FName = |
| 1052 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 1053 | const std::string &DirName = |
| 1054 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 1055 | unsigned SrcFile = MMI->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1056 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1057 | SmallVector<SDOperand, 8> Ops; |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 1058 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1059 | SDOperand LineOp = Node->getOperand(1); |
| 1060 | SDOperand ColOp = Node->getOperand(2); |
| 1061 | |
| 1062 | if (useDEBUG_LOC) { |
| 1063 | Ops.push_back(LineOp); // line # |
| 1064 | Ops.push_back(ColOp); // col # |
| 1065 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1066 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1067 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 1068 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 1069 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Evan Cheng | a647c92 | 2008-02-01 02:05:57 +0000 | [diff] [blame] | 1070 | unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1071 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1072 | Ops.push_back(DAG.getConstant(0, MVT::i32)); // a debug label |
| 1073 | Result = DAG.getNode(ISD::LABEL, MVT::Other, &Ops[0], Ops.size()); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1074 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 1075 | } else { |
| 1076 | Result = Tmp1; // chain |
| 1077 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1078 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 1079 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1080 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 1081 | if (Tmp1 != Node->getOperand(0) || |
| 1082 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1083 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1084 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 1085 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 1086 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 1087 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 1088 | } else { |
| 1089 | // Otherwise promote them. |
| 1090 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 1091 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 1092 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1093 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 1094 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1095 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | break; |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1100 | |
| 1101 | case ISD::DECLARE: |
| 1102 | assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!"); |
| 1103 | switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) { |
| 1104 | default: assert(0 && "This action is not supported yet!"); |
| 1105 | case TargetLowering::Legal: |
| 1106 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1107 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address. |
| 1108 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable. |
| 1109 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1110 | break; |
Chris Lattner | e07415d | 2008-02-28 05:53:40 +0000 | [diff] [blame] | 1111 | case TargetLowering::Expand: |
| 1112 | Result = LegalizeOp(Node->getOperand(0)); |
| 1113 | break; |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1114 | } |
| 1115 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1116 | |
| 1117 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1118 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1119 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1120 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1121 | case TargetLowering::Legal: |
| 1122 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1123 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 1124 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 1125 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1126 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | } |
| 1129 | break; |
| 1130 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1131 | case ISD::LABEL: |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1132 | assert(Node->getNumOperands() == 3 && "Invalid LABEL node!"); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1133 | switch (TLI.getOperationAction(ISD::LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 1134 | default: assert(0 && "This action is not supported yet!"); |
| 1135 | case TargetLowering::Legal: |
| 1136 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1137 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Evan Cheng | bb81d97 | 2008-01-31 09:59:15 +0000 | [diff] [blame] | 1138 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the "flavor" operand. |
| 1139 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1140 | break; |
Chris Lattner | a9569f1 | 2007-03-03 19:21:38 +0000 | [diff] [blame] | 1141 | case TargetLowering::Expand: |
| 1142 | Result = LegalizeOp(Node->getOperand(0)); |
| 1143 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 1144 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1145 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 1146 | |
Evan Cheng | 27b7db5 | 2008-03-08 00:58:38 +0000 | [diff] [blame] | 1147 | case ISD::PREFETCH: |
| 1148 | assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!"); |
| 1149 | switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) { |
| 1150 | default: assert(0 && "This action is not supported yet!"); |
| 1151 | case TargetLowering::Legal: |
| 1152 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1153 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address. |
| 1154 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier. |
| 1155 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier. |
| 1156 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
| 1157 | break; |
| 1158 | case TargetLowering::Expand: |
| 1159 | // It's a noop. |
| 1160 | Result = LegalizeOp(Node->getOperand(0)); |
| 1161 | break; |
| 1162 | } |
| 1163 | break; |
| 1164 | |
Andrew Lenharth | 22c5c1b | 2008-02-16 01:24:58 +0000 | [diff] [blame] | 1165 | case ISD::MEMBARRIER: { |
| 1166 | assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!"); |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 1167 | switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) { |
| 1168 | default: assert(0 && "This action is not supported yet!"); |
| 1169 | case TargetLowering::Legal: { |
| 1170 | SDOperand Ops[6]; |
| 1171 | Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Duncan Sands | e90a615 | 2008-02-27 08:53:44 +0000 | [diff] [blame] | 1172 | for (int x = 1; x < 6; ++x) { |
| 1173 | Ops[x] = Node->getOperand(x); |
| 1174 | if (!isTypeLegal(Ops[x].getValueType())) |
| 1175 | Ops[x] = PromoteOp(Ops[x]); |
| 1176 | } |
Andrew Lenharth | d497d9f | 2008-02-16 14:46:26 +0000 | [diff] [blame] | 1177 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6); |
| 1178 | break; |
| 1179 | } |
| 1180 | case TargetLowering::Expand: |
| 1181 | //There is no libgcc call for this op |
| 1182 | Result = Node->getOperand(0); // Noop |
| 1183 | break; |
| 1184 | } |
Andrew Lenharth | 22c5c1b | 2008-02-16 01:24:58 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | } |
| 1187 | |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 1188 | case ISD::ATOMIC_LCS: |
| 1189 | case ISD::ATOMIC_LAS: |
| 1190 | case ISD::ATOMIC_SWAP: { |
| 1191 | assert(((Node->getNumOperands() == 4 && Node->getOpcode() == ISD::ATOMIC_LCS) || |
| 1192 | (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_LAS) || |
| 1193 | (Node->getNumOperands() == 3 && Node->getOpcode() == ISD::ATOMIC_SWAP)) && |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 1194 | "Invalid Atomic node!"); |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 1195 | int num = Node->getOpcode() == ISD::ATOMIC_LCS ? 4 : 3; |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 1196 | SDOperand Ops[4]; |
| 1197 | for (int x = 0; x < num; ++x) |
| 1198 | Ops[x] = LegalizeOp(Node->getOperand(x)); |
| 1199 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], num); |
| 1200 | |
| 1201 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 1202 | default: assert(0 && "This action is not supported yet!"); |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 1203 | case TargetLowering::Custom: |
| 1204 | Result = TLI.LowerOperation(Result, DAG); |
| 1205 | break; |
| 1206 | case TargetLowering::Legal: |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 1209 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1210 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1211 | return Result.getValue(Op.ResNo); |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1214 | case ISD::Constant: { |
| 1215 | ConstantSDNode *CN = cast<ConstantSDNode>(Node); |
| 1216 | unsigned opAction = |
| 1217 | TLI.getOperationAction(ISD::Constant, CN->getValueType(0)); |
| 1218 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1219 | // We know we don't need to expand constants here, constants only have one |
| 1220 | // value and we check that it is fine above. |
| 1221 | |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1222 | if (opAction == TargetLowering::Custom) { |
| 1223 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1224 | if (Tmp1.Val) |
| 1225 | Result = Tmp1; |
| 1226 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1227 | break; |
Scott Michel | c1513d2 | 2007-08-08 23:23:31 +0000 | [diff] [blame] | 1228 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1229 | case ISD::ConstantFP: { |
| 1230 | // Spill FP immediates to the constant pool if the target cannot directly |
| 1231 | // codegen them. Targets often have some immediate values that can be |
| 1232 | // efficiently generated into an FP register without a load. We explicitly |
| 1233 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1234 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 1235 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1236 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 1237 | default: assert(0 && "This action is not supported yet!"); |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1238 | case TargetLowering::Legal: |
| 1239 | break; |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 1240 | case TargetLowering::Custom: |
| 1241 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1242 | if (Tmp3.Val) { |
| 1243 | Result = Tmp3; |
| 1244 | break; |
| 1245 | } |
| 1246 | // FALLTHROUGH |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1247 | case TargetLowering::Expand: { |
| 1248 | // Check to see if this FP immediate is already legal. |
| 1249 | bool isLegal = false; |
| 1250 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 1251 | E = TLI.legal_fpimm_end(); I != E; ++I) { |
| 1252 | if (CFP->isExactlyValue(*I)) { |
| 1253 | isLegal = true; |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 1258 | if (isLegal) |
| 1259 | break; |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 1260 | Result = ExpandConstantFP(CFP, true, DAG, TLI); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1261 | } |
Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 1262 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | } |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1265 | case ISD::TokenFactor: |
| 1266 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1267 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1268 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1269 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1270 | } else if (Node->getNumOperands() == 3) { |
| 1271 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1272 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1273 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1274 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1275 | } else { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1276 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 1277 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1278 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1279 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1280 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1281 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 1282 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1283 | |
| 1284 | case ISD::FORMAL_ARGUMENTS: |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1285 | case ISD::CALL: |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 1286 | // The only option for this is to custom lower it. |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1287 | Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG); |
| 1288 | assert(Tmp3.Val && "Target didn't custom lower this node!"); |
Dale Johannesen | 0ea0356 | 2008-03-05 19:14:03 +0000 | [diff] [blame] | 1289 | // A call within a calling sequence must be legalized to something |
| 1290 | // other than the normal CALLSEQ_END. Violating this gets Legalize |
| 1291 | // into an infinite loop. |
| 1292 | assert ((!IsLegalizingCall || |
| 1293 | Node->getOpcode() != ISD::CALL || |
| 1294 | Tmp3.Val->getOpcode() != ISD::CALLSEQ_END) && |
| 1295 | "Nested CALLSEQ_START..CALLSEQ_END not supported."); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1296 | |
| 1297 | // The number of incoming and outgoing values should match; unless the final |
| 1298 | // outgoing value is a flag. |
| 1299 | assert((Tmp3.Val->getNumValues() == Result.Val->getNumValues() || |
| 1300 | (Tmp3.Val->getNumValues() == Result.Val->getNumValues() + 1 && |
| 1301 | Tmp3.Val->getValueType(Tmp3.Val->getNumValues() - 1) == |
| 1302 | MVT::Flag)) && |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1303 | "Lowering call/formal_arguments produced unexpected # results!"); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1304 | |
Chris Lattner | f4ec817 | 2006-05-16 22:53:20 +0000 | [diff] [blame] | 1305 | // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1306 | // 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] | 1307 | for (unsigned i = 0, e = Tmp3.Val->getNumValues(); i != e; ++i) { |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1308 | if (Tmp3.Val->getValueType(i) == MVT::Flag) |
| 1309 | continue; |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1310 | Tmp1 = LegalizeOp(Tmp3.getValue(i)); |
Chris Lattner | e2e4173 | 2006-05-16 05:49:56 +0000 | [diff] [blame] | 1311 | if (Op.ResNo == i) |
| 1312 | Tmp2 = Tmp1; |
| 1313 | AddLegalizedOperand(SDOperand(Node, i), Tmp1); |
| 1314 | } |
| 1315 | return Tmp2; |
Christopher Lamb | 557c363 | 2007-07-26 07:34:40 +0000 | [diff] [blame] | 1316 | case ISD::EXTRACT_SUBREG: { |
| 1317 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1318 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1)); |
| 1319 | assert(idx && "Operand must be a constant"); |
| 1320 | Tmp2 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1321 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1322 | } |
| 1323 | break; |
| 1324 | case ISD::INSERT_SUBREG: { |
| 1325 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1326 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1327 | ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2)); |
| 1328 | assert(idx && "Operand must be a constant"); |
| 1329 | Tmp3 = DAG.getTargetConstant(idx->getValue(), idx->getValueType(0)); |
| 1330 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1331 | } |
| 1332 | break; |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1333 | case ISD::BUILD_VECTOR: |
| 1334 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1335 | default: assert(0 && "This action is not supported yet!"); |
| 1336 | case TargetLowering::Custom: |
| 1337 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1338 | if (Tmp3.Val) { |
| 1339 | Result = Tmp3; |
| 1340 | break; |
| 1341 | } |
| 1342 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1343 | case TargetLowering::Expand: |
| 1344 | Result = ExpandBUILD_VECTOR(Result.Val); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1345 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1346 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1347 | break; |
| 1348 | case ISD::INSERT_VECTOR_ELT: |
| 1349 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1350 | Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo |
Nate Begeman | 0325d90 | 2008-02-13 06:43:04 +0000 | [diff] [blame] | 1351 | |
| 1352 | // The type of the value to insert may not be legal, even though the vector |
| 1353 | // type is legal. Legalize/Promote accordingly. We do not handle Expand |
| 1354 | // here. |
| 1355 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1356 | default: assert(0 && "Cannot expand insert element operand"); |
| 1357 | case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break; |
| 1358 | case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break; |
| 1359 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1360 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1361 | |
| 1362 | switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT, |
| 1363 | Node->getValueType(0))) { |
| 1364 | default: assert(0 && "This action is not supported yet!"); |
| 1365 | case TargetLowering::Legal: |
| 1366 | break; |
| 1367 | case TargetLowering::Custom: |
Nate Begeman | 2281a99 | 2008-01-05 20:47:37 +0000 | [diff] [blame] | 1368 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1369 | if (Tmp4.Val) { |
| 1370 | Result = Tmp4; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1371 | break; |
| 1372 | } |
| 1373 | // FALLTHROUGH |
| 1374 | case TargetLowering::Expand: { |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1375 | // If the insert index is a constant, codegen this as a scalar_to_vector, |
| 1376 | // then a shuffle that inserts it into the right position in the vector. |
| 1377 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) { |
Nate Begeman | 0325d90 | 2008-02-13 06:43:04 +0000 | [diff] [blame] | 1378 | // SCALAR_TO_VECTOR requires that the type of the value being inserted |
| 1379 | // match the element type of the vector being created. |
| 1380 | if (Tmp2.getValueType() == |
| 1381 | MVT::getVectorElementType(Op.getValueType())) { |
| 1382 | SDOperand ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, |
| 1383 | Tmp1.getValueType(), Tmp2); |
| 1384 | |
| 1385 | unsigned NumElts = MVT::getVectorNumElements(Tmp1.getValueType()); |
| 1386 | MVT::ValueType ShufMaskVT = MVT::getIntVectorWithNumElements(NumElts); |
| 1387 | MVT::ValueType ShufMaskEltVT = MVT::getVectorElementType(ShufMaskVT); |
| 1388 | |
| 1389 | // We generate a shuffle of InVec and ScVec, so the shuffle mask |
| 1390 | // should be 0,1,2,3,4,5... with the appropriate element replaced with |
| 1391 | // elt 0 of the RHS. |
| 1392 | SmallVector<SDOperand, 8> ShufOps; |
| 1393 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1394 | if (i != InsertPos->getValue()) |
| 1395 | ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT)); |
| 1396 | else |
| 1397 | ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT)); |
| 1398 | } |
| 1399 | SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT, |
| 1400 | &ShufOps[0], ShufOps.size()); |
| 1401 | |
| 1402 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(), |
| 1403 | Tmp1, ScVec, ShufMask); |
| 1404 | Result = LegalizeOp(Result); |
| 1405 | break; |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1406 | } |
Chris Lattner | 8d5a894 | 2006-04-17 19:21:01 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1409 | // If the target doesn't support this, we have to spill the input vector |
| 1410 | // to a temporary stack slot, update the element, then reload it. This is |
| 1411 | // badness. We could also load the value into a vector register (either |
| 1412 | // with a "move to register" or "extload into register" instruction, then |
| 1413 | // permute it into place, if the idx is a constant and if the idx is |
| 1414 | // supported by the target. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1415 | MVT::ValueType VT = Tmp1.getValueType(); |
Nate Begeman | 0325d90 | 2008-02-13 06:43:04 +0000 | [diff] [blame] | 1416 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1417 | MVT::ValueType IdxVT = Tmp3.getValueType(); |
| 1418 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 1419 | SDOperand StackPtr = DAG.CreateStackTemporary(VT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1420 | |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 1421 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr.Val); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1422 | int SPFI = StackPtrFI->getIndex(); |
| 1423 | |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1424 | // Store the vector. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1425 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1426 | PseudoSourceValue::getFixedStack(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1427 | SPFI); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1428 | |
| 1429 | // Truncate or zero extend offset to target pointer type. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1430 | unsigned CastOpc = (IdxVT > PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
| 1431 | Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1432 | // Add the offset to the index. |
Evan Cheng | f6bf87f | 2006-04-08 01:46:37 +0000 | [diff] [blame] | 1433 | unsigned EltSize = MVT::getSizeInBits(EltVT)/8; |
| 1434 | Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 1435 | SDOperand StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1436 | // Store the scalar value. |
Nate Begeman | 0325d90 | 2008-02-13 06:43:04 +0000 | [diff] [blame] | 1437 | Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2, |
| 1438 | PseudoSourceValue::getFixedStack(), SPFI, EltVT); |
Evan Cheng | eb0b461 | 2006-03-31 01:27:51 +0000 | [diff] [blame] | 1439 | // Load the updated vector. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1440 | Result = DAG.getLoad(VT, Ch, StackPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1441 | PseudoSourceValue::getFixedStack(), SPFI); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | } |
| 1444 | } |
| 1445 | break; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1446 | case ISD::SCALAR_TO_VECTOR: |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1447 | if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) { |
| 1448 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
| 1449 | break; |
| 1450 | } |
| 1451 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1452 | Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal |
| 1453 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1454 | switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR, |
| 1455 | Node->getValueType(0))) { |
| 1456 | default: assert(0 && "This action is not supported yet!"); |
| 1457 | case TargetLowering::Legal: |
| 1458 | break; |
Chris Lattner | 4d3abee | 2006-03-19 06:47:21 +0000 | [diff] [blame] | 1459 | case TargetLowering::Custom: |
| 1460 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1461 | if (Tmp3.Val) { |
| 1462 | Result = Tmp3; |
| 1463 | break; |
| 1464 | } |
| 1465 | // FALLTHROUGH |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1466 | case TargetLowering::Expand: |
| 1467 | Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1470 | break; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1471 | case ISD::VECTOR_SHUFFLE: |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1472 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors, |
| 1473 | Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask. |
| 1474 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1475 | |
| 1476 | // Allow targets to custom lower the SHUFFLEs they support. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1477 | switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) { |
| 1478 | default: assert(0 && "Unknown operation action!"); |
| 1479 | case TargetLowering::Legal: |
| 1480 | assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) && |
| 1481 | "vector shuffle should not be created if not legal!"); |
| 1482 | break; |
| 1483 | case TargetLowering::Custom: |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1484 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1485 | if (Tmp3.Val) { |
| 1486 | Result = Tmp3; |
| 1487 | break; |
| 1488 | } |
| 1489 | // FALLTHROUGH |
| 1490 | case TargetLowering::Expand: { |
| 1491 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 1492 | MVT::ValueType EltVT = MVT::getVectorElementType(VT); |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1493 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 1494 | SDOperand Mask = Node->getOperand(2); |
| 1495 | unsigned NumElems = Mask.getNumOperands(); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1496 | SmallVector<SDOperand,8> Ops; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1497 | for (unsigned i = 0; i != NumElems; ++i) { |
| 1498 | SDOperand Arg = Mask.getOperand(i); |
| 1499 | if (Arg.getOpcode() == ISD::UNDEF) { |
| 1500 | Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT)); |
| 1501 | } else { |
| 1502 | assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!"); |
| 1503 | unsigned Idx = cast<ConstantSDNode>(Arg)->getValue(); |
| 1504 | if (Idx < NumElems) |
| 1505 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1, |
| 1506 | DAG.getConstant(Idx, PtrVT))); |
| 1507 | else |
| 1508 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2, |
| 1509 | DAG.getConstant(Idx - NumElems, PtrVT))); |
| 1510 | } |
| 1511 | } |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 1512 | Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size()); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1513 | break; |
Evan Cheng | 18dd6d0 | 2006-04-05 06:07:11 +0000 | [diff] [blame] | 1514 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 1515 | case TargetLowering::Promote: { |
| 1516 | // Change base type to a different vector type. |
| 1517 | MVT::ValueType OVT = Node->getValueType(0); |
| 1518 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 1519 | |
| 1520 | // Cast the two input vectors. |
| 1521 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 1522 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 1523 | |
| 1524 | // Convert the shuffle mask to the right # elements. |
| 1525 | Tmp3 = SDOperand(isShuffleLegal(OVT, Node->getOperand(2)), 0); |
| 1526 | assert(Tmp3.Val && "Shuffle not legal?"); |
| 1527 | Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3); |
| 1528 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 1529 | break; |
| 1530 | } |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 1531 | } |
| 1532 | break; |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1533 | |
| 1534 | case ISD::EXTRACT_VECTOR_ELT: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1535 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1536 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1537 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1538 | Result = ExpandEXTRACT_VECTOR_ELT(Result); |
Chris Lattner | 384504c | 2006-03-21 20:44:12 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 1541 | case ISD::EXTRACT_SUBVECTOR: |
| 1542 | Tmp1 = Node->getOperand(0); |
| 1543 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1544 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1545 | Result = ExpandEXTRACT_SUBVECTOR(Result); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 1546 | break; |
| 1547 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1548 | case ISD::CALLSEQ_START: { |
| 1549 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 1550 | |
| 1551 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1552 | // to this call start. This ensures that any libcalls that need be inserted |
| 1553 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1554 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1555 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1556 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node, |
| 1557 | NodesLeadingTo); |
| 1558 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Now that we legalized all of the inputs (which may have inserted |
| 1561 | // libcalls) create the new CALLSEQ_START node. |
| 1562 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1563 | |
| 1564 | // Merge in the last call, to ensure that this call start after the last |
| 1565 | // call ended. |
Chris Lattner | c5d7d7c | 2006-05-17 18:00:08 +0000 | [diff] [blame] | 1566 | if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) { |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1567 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1568 | Tmp1 = LegalizeOp(Tmp1); |
| 1569 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1570 | |
| 1571 | // Do not try to legalize the target-specific arguments (#1+). |
| 1572 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1573 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1574 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1575 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1579 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1580 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1581 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 1582 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1583 | // Now that the callseq_start and all of the non-call nodes above this call |
| 1584 | // sequence have been legalized, legalize the call itself. During this |
| 1585 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1586 | // can overlap. |
| 1587 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 1588 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 1589 | // Note that we are selecting this call! |
| 1590 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 1591 | IsLegalizingCall = true; |
| 1592 | |
| 1593 | // Legalize the call, starting from the CALLSEQ_END. |
| 1594 | LegalizeOp(LastCALLSEQ_END); |
| 1595 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 1596 | return Result; |
| 1597 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1598 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1599 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 1600 | // will cause this node to be legalized as well as handling libcalls right. |
| 1601 | if (LastCALLSEQ_END.Val != Node) { |
| 1602 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 1603 | DenseMap<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1604 | assert(I != LegalizedNodes.end() && |
| 1605 | "Legalizing the call start should have legalized this node!"); |
| 1606 | return I->second; |
| 1607 | } |
| 1608 | |
| 1609 | // Otherwise, the call start has been legalized and everything is going |
| 1610 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1611 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1612 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1613 | // an optional flag input. |
| 1614 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 1615 | if (Tmp1 != Node->getOperand(0)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1616 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1617 | Ops[0] = Tmp1; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1618 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1619 | } |
| 1620 | } else { |
| 1621 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1622 | if (Tmp1 != Node->getOperand(0) || |
| 1623 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1624 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1625 | Ops[0] = Tmp1; |
| 1626 | Ops.back() = Tmp2; |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1627 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1628 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1629 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1630 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1631 | // This finishes up call legalization. |
| 1632 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1633 | |
| 1634 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 1635 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1636 | if (Node->getNumValues() == 2) |
| 1637 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1638 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1639 | case ISD::DYNAMIC_STACKALLOC: { |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1640 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1641 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1642 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 1643 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1644 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 1645 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1646 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1647 | Tmp2 = Result.getValue(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1648 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1649 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1650 | case TargetLowering::Expand: { |
| 1651 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1652 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1653 | " not tell us which reg is the stack pointer!"); |
| 1654 | SDOperand Chain = Tmp1.getOperand(0); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1655 | |
| 1656 | // Chain the dynamic stack allocation so that it doesn't modify the stack |
| 1657 | // pointer when other instructions are using the stack. |
| 1658 | Chain = DAG.getCALLSEQ_START(Chain, |
| 1659 | DAG.getConstant(0, TLI.getPointerTy())); |
| 1660 | |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1661 | SDOperand Size = Tmp2.getOperand(1); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1662 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, VT); |
| 1663 | Chain = SP.getValue(1); |
| 1664 | unsigned Align = cast<ConstantSDNode>(Tmp3)->getValue(); |
| 1665 | unsigned StackAlign = |
| 1666 | TLI.getTargetMachine().getFrameInfo()->getStackAlignment(); |
| 1667 | if (Align > StackAlign) |
Evan Cheng | 3e20bba | 2007-08-17 18:02:22 +0000 | [diff] [blame] | 1668 | SP = DAG.getNode(ISD::AND, VT, SP, |
| 1669 | DAG.getConstant(-(uint64_t)Align, VT)); |
Evan Cheng | 61bbbab | 2007-08-16 23:50:06 +0000 | [diff] [blame] | 1670 | Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 1671 | Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain |
| 1672 | |
| 1673 | Tmp2 = |
| 1674 | DAG.getCALLSEQ_END(Chain, |
| 1675 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1676 | DAG.getConstant(0, TLI.getPointerTy()), |
| 1677 | SDOperand()); |
| 1678 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1679 | Tmp1 = LegalizeOp(Tmp1); |
| 1680 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1681 | break; |
| 1682 | } |
| 1683 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 1684 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 1685 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1686 | Tmp1 = LegalizeOp(Tmp3); |
| 1687 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1688 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1689 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1690 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1691 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1692 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1693 | // Since this op produce two values, make sure to remember that we |
| 1694 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1695 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1696 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 1697 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 1698 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1699 | case ISD::INLINEASM: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1700 | SmallVector<SDOperand, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1701 | bool Changed = false; |
| 1702 | // Legalize all of the operands of the inline asm, in case they are nodes |
| 1703 | // that need to be expanded or something. Note we skip the asm string and |
| 1704 | // all of the TargetConstant flags. |
| 1705 | SDOperand Op = LegalizeOp(Ops[0]); |
| 1706 | Changed = Op != Ops[0]; |
| 1707 | Ops[0] = Op; |
| 1708 | |
| 1709 | bool HasInFlag = Ops.back().getValueType() == MVT::Flag; |
| 1710 | for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) { |
| 1711 | unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getValue() >> 3; |
| 1712 | for (++i; NumVals; ++i, --NumVals) { |
| 1713 | SDOperand Op = LegalizeOp(Ops[i]); |
| 1714 | if (Op != Ops[i]) { |
| 1715 | Changed = true; |
| 1716 | Ops[i] = Op; |
| 1717 | } |
| 1718 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1719 | } |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1720 | |
| 1721 | if (HasInFlag) { |
| 1722 | Op = LegalizeOp(Ops.back()); |
| 1723 | Changed |= Op != Ops.back(); |
| 1724 | Ops.back() = Op; |
| 1725 | } |
| 1726 | |
| 1727 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 1728 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1729 | |
| 1730 | // 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] | 1731 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 1732 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1733 | return Result.getValue(Op.ResNo); |
Chris Lattner | 25a022c | 2006-07-11 01:40:09 +0000 | [diff] [blame] | 1734 | } |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1735 | case ISD::BR: |
| 1736 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1737 | // Ensure that libcalls are emitted before a branch. |
| 1738 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1739 | Tmp1 = LegalizeOp(Tmp1); |
| 1740 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1741 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1742 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 1743 | break; |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1744 | case ISD::BRIND: |
| 1745 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1746 | // Ensure that libcalls are emitted before a branch. |
| 1747 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1748 | Tmp1 = LegalizeOp(Tmp1); |
| 1749 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1750 | |
| 1751 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1752 | default: assert(0 && "Indirect target must be legal type (pointer)!"); |
| 1753 | case Legal: |
| 1754 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1755 | break; |
| 1756 | } |
| 1757 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1758 | break; |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1759 | case ISD::BR_JT: |
| 1760 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1761 | // Ensure that libcalls are emitted before a branch. |
| 1762 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1763 | Tmp1 = LegalizeOp(Tmp1); |
| 1764 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1765 | |
| 1766 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node. |
| 1767 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1768 | |
| 1769 | switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) { |
| 1770 | default: assert(0 && "This action is not supported yet!"); |
| 1771 | case TargetLowering::Legal: break; |
| 1772 | case TargetLowering::Custom: |
| 1773 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1774 | if (Tmp1.Val) Result = Tmp1; |
| 1775 | break; |
| 1776 | case TargetLowering::Expand: { |
| 1777 | SDOperand Chain = Result.getOperand(0); |
| 1778 | SDOperand Table = Result.getOperand(1); |
| 1779 | SDOperand Index = Result.getOperand(2); |
| 1780 | |
| 1781 | MVT::ValueType PTy = TLI.getPointerTy(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1782 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1783 | unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize(); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1784 | Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy)); |
| 1785 | SDOperand Addr = DAG.getNode(ISD::ADD, PTy, Index, Table); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1786 | |
| 1787 | SDOperand LD; |
| 1788 | switch (EntrySize) { |
| 1789 | default: assert(0 && "Size of jump table not supported yet."); break; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1790 | case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1791 | PseudoSourceValue::getJumpTable(), 0); break; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1792 | case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 1793 | PseudoSourceValue::getJumpTable(), 0); break; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1796 | Addr = LD; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 1797 | if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1798 | // For PIC, the sequence is: |
| 1799 | // BRIND(load(Jumptable + index) + RelocBase) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1800 | // RelocBase can be JumpTable, GOT or some sort of global base. |
| 1801 | if (PTy != MVT::i32) |
| 1802 | Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr); |
| 1803 | Addr = DAG.getNode(ISD::ADD, PTy, Addr, |
| 1804 | TLI.getPICJumpTableRelocBase(Table, DAG)); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1805 | } |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1806 | Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr); |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 1807 | } |
| 1808 | } |
| 1809 | break; |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1810 | case ISD::BRCOND: |
| 1811 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1812 | // Ensure that libcalls are emitted before a return. |
| 1813 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1814 | Tmp1 = LegalizeOp(Tmp1); |
| 1815 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1816 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1817 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1818 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1819 | case Legal: |
| 1820 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 1821 | break; |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1822 | case Promote: { |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1823 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1824 | |
| 1825 | // The top bits of the promoted condition are not necessarily zero, ensure |
| 1826 | // that the value is properly zero extended. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1827 | unsigned BitWidth = Tmp2.getValueSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 1828 | if (!DAG.MaskedValueIsZero(Tmp2, |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1829 | APInt::getHighBitsSet(BitWidth, BitWidth-1))) |
Chris Lattner | f990817 | 2006-11-27 04:39:56 +0000 | [diff] [blame] | 1830 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | } |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 1833 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1836 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1837 | |
| 1838 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 1839 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1840 | case TargetLowering::Legal: break; |
| 1841 | case TargetLowering::Custom: |
| 1842 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1843 | if (Tmp1.Val) Result = Tmp1; |
| 1844 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1845 | case TargetLowering::Expand: |
| 1846 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 1847 | // Node. |
| 1848 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 1849 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 1850 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 1851 | Node->getOperand(2)); |
| 1852 | } else { |
| 1853 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 1854 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 1855 | DAG.getConstant(0, Tmp2.getValueType()), |
| 1856 | Node->getOperand(2)); |
| 1857 | } |
| 1858 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1859 | } |
| 1860 | break; |
| 1861 | case ISD::BR_CC: |
| 1862 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1863 | // Ensure that libcalls are emitted before a branch. |
| 1864 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1865 | Tmp1 = LegalizeOp(Tmp1); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1866 | Tmp2 = Node->getOperand(2); // LHS |
| 1867 | Tmp3 = Node->getOperand(3); // RHS |
| 1868 | Tmp4 = Node->getOperand(1); // CC |
| 1869 | |
| 1870 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
Evan Cheng | 722cb36 | 2006-12-18 22:55:34 +0000 | [diff] [blame] | 1871 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1872 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1873 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1874 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1875 | // the result against zero to select between true and false values. |
| 1876 | if (Tmp3.Val == 0) { |
| 1877 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 1878 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1879 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1880 | |
| 1881 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 1882 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1884 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 1885 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1886 | case TargetLowering::Legal: break; |
| 1887 | case TargetLowering::Custom: |
| 1888 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1889 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1890 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1891 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1892 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1893 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1894 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1895 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1896 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1897 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1898 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1899 | if (ExtType == ISD::NON_EXTLOAD) { |
| 1900 | MVT::ValueType VT = Node->getValueType(0); |
| 1901 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 1902 | Tmp3 = Result.getValue(0); |
| 1903 | Tmp4 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1904 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1905 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1906 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1907 | case TargetLowering::Legal: |
| 1908 | // If this is an unaligned load and the target doesn't support it, |
| 1909 | // expand it. |
| 1910 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 1911 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1912 | getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT())); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1913 | if (LD->getAlignment() < ABIAlignment){ |
| 1914 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 1915 | TLI); |
| 1916 | Tmp3 = Result.getOperand(0); |
| 1917 | Tmp4 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1918 | Tmp3 = LegalizeOp(Tmp3); |
| 1919 | Tmp4 = LegalizeOp(Tmp4); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1923 | case TargetLowering::Custom: |
| 1924 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
| 1925 | if (Tmp1.Val) { |
| 1926 | Tmp3 = LegalizeOp(Tmp1); |
| 1927 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1928 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1929 | break; |
| 1930 | case TargetLowering::Promote: { |
| 1931 | // Only promote a load of vector type to another. |
| 1932 | assert(MVT::isVector(VT) && "Cannot promote this load!"); |
| 1933 | // Change base type to a different vector type. |
| 1934 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
| 1935 | |
| 1936 | Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1937 | LD->getSrcValueOffset(), |
| 1938 | LD->isVolatile(), LD->getAlignment()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1939 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1)); |
| 1940 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1941 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1942 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1943 | } |
| 1944 | // Since loads produce two values, make sure to remember that we |
| 1945 | // legalized both of them. |
| 1946 | AddLegalizedOperand(SDOperand(Node, 0), Tmp3); |
| 1947 | AddLegalizedOperand(SDOperand(Node, 1), Tmp4); |
| 1948 | return Op.ResNo ? Tmp4 : Tmp3; |
| 1949 | } else { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1950 | MVT::ValueType SrcVT = LD->getMemoryVT(); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1951 | unsigned SrcWidth = MVT::getSizeInBits(SrcVT); |
| 1952 | int SVOffset = LD->getSrcValueOffset(); |
| 1953 | unsigned Alignment = LD->getAlignment(); |
| 1954 | bool isVolatile = LD->isVolatile(); |
| 1955 | |
| 1956 | if (SrcWidth != MVT::getStoreSizeInBits(SrcVT) && |
| 1957 | // Some targets pretend to have an i1 loading operation, and actually |
| 1958 | // load an i8. This trick is correct for ZEXTLOAD because the top 7 |
| 1959 | // bits are guaranteed to be zero; it helps the optimizers understand |
| 1960 | // that these bits are zero. It is also useful for EXTLOAD, since it |
| 1961 | // tells the optimizers that those bits are undefined. It would be |
| 1962 | // nice to have an effective generic way of getting these benefits... |
| 1963 | // Until such a way is found, don't insist on promoting i1 here. |
| 1964 | (SrcVT != MVT::i1 || |
| 1965 | TLI.getLoadXAction(ExtType, MVT::i1) == TargetLowering::Promote)) { |
| 1966 | // Promote to a byte-sized load if not loading an integral number of |
| 1967 | // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. |
| 1968 | unsigned NewWidth = MVT::getStoreSizeInBits(SrcVT); |
| 1969 | MVT::ValueType NVT = MVT::getIntegerType(NewWidth); |
| 1970 | SDOperand Ch; |
| 1971 | |
| 1972 | // The extra bits are guaranteed to be zero, since we stored them that |
| 1973 | // way. A zext load from NVT thus automatically gives zext from SrcVT. |
| 1974 | |
| 1975 | ISD::LoadExtType NewExtType = |
| 1976 | ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
| 1977 | |
| 1978 | Result = DAG.getExtLoad(NewExtType, Node->getValueType(0), |
| 1979 | Tmp1, Tmp2, LD->getSrcValue(), SVOffset, |
| 1980 | NVT, isVolatile, Alignment); |
| 1981 | |
| 1982 | Ch = Result.getValue(1); // The chain. |
| 1983 | |
| 1984 | if (ExtType == ISD::SEXTLOAD) |
| 1985 | // Having the top bits zero doesn't help when sign extending. |
| 1986 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 1987 | Result, DAG.getValueType(SrcVT)); |
| 1988 | else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) |
| 1989 | // All the top bits are guaranteed to be zero - inform the optimizers. |
| 1990 | Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result, |
| 1991 | DAG.getValueType(SrcVT)); |
| 1992 | |
| 1993 | Tmp1 = LegalizeOp(Result); |
| 1994 | Tmp2 = LegalizeOp(Ch); |
| 1995 | } else if (SrcWidth & (SrcWidth - 1)) { |
| 1996 | // If not loading a power-of-2 number of bits, expand as two loads. |
| 1997 | assert(MVT::isExtendedVT(SrcVT) && !MVT::isVector(SrcVT) && |
| 1998 | "Unsupported extload!"); |
| 1999 | unsigned RoundWidth = 1 << Log2_32(SrcWidth); |
| 2000 | assert(RoundWidth < SrcWidth); |
| 2001 | unsigned ExtraWidth = SrcWidth - RoundWidth; |
| 2002 | assert(ExtraWidth < RoundWidth); |
| 2003 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 2004 | "Load size not an integral number of bytes!"); |
| 2005 | MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth); |
| 2006 | MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth); |
| 2007 | SDOperand Lo, Hi, Ch; |
| 2008 | unsigned IncrementSize; |
| 2009 | |
| 2010 | if (TLI.isLittleEndian()) { |
| 2011 | // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) |
| 2012 | // Load the bottom RoundWidth bits. |
| 2013 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2, |
| 2014 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 2015 | Alignment); |
| 2016 | |
| 2017 | // Load the remaining ExtraWidth bits. |
| 2018 | IncrementSize = RoundWidth / 8; |
| 2019 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2020 | DAG.getIntPtrConstant(IncrementSize)); |
| 2021 | Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 2022 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 2023 | ExtraVT, isVolatile, |
| 2024 | MinAlign(Alignment, IncrementSize)); |
| 2025 | |
| 2026 | // Build a factor node to remember that this load is independent of the |
| 2027 | // other one. |
| 2028 | Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 2029 | Hi.getValue(1)); |
| 2030 | |
| 2031 | // Move the top bits to the right place. |
| 2032 | Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi, |
| 2033 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
| 2034 | |
| 2035 | // Join the hi and lo parts. |
| 2036 | Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2037 | } else { |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2038 | // Big endian - avoid unaligned loads. |
| 2039 | // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 |
| 2040 | // Load the top RoundWidth bits. |
| 2041 | Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2, |
| 2042 | LD->getSrcValue(), SVOffset, RoundVT, isVolatile, |
| 2043 | Alignment); |
| 2044 | |
| 2045 | // Load the remaining ExtraWidth bits. |
| 2046 | IncrementSize = RoundWidth / 8; |
| 2047 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2048 | DAG.getIntPtrConstant(IncrementSize)); |
| 2049 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2, |
| 2050 | LD->getSrcValue(), SVOffset + IncrementSize, |
| 2051 | ExtraVT, isVolatile, |
| 2052 | MinAlign(Alignment, IncrementSize)); |
| 2053 | |
| 2054 | // Build a factor node to remember that this load is independent of the |
| 2055 | // other one. |
| 2056 | Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 2057 | Hi.getValue(1)); |
| 2058 | |
| 2059 | // Move the top bits to the right place. |
| 2060 | Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi, |
| 2061 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
| 2062 | |
| 2063 | // Join the hi and lo parts. |
| 2064 | Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi); |
| 2065 | } |
| 2066 | |
| 2067 | Tmp1 = LegalizeOp(Result); |
| 2068 | Tmp2 = LegalizeOp(Ch); |
| 2069 | } else { |
| 2070 | switch (TLI.getLoadXAction(ExtType, SrcVT)) { |
| 2071 | default: assert(0 && "This action is not supported yet!"); |
| 2072 | case TargetLowering::Custom: |
| 2073 | isCustom = true; |
| 2074 | // FALLTHROUGH |
| 2075 | case TargetLowering::Legal: |
| 2076 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset()); |
| 2077 | Tmp1 = Result.getValue(0); |
| 2078 | Tmp2 = Result.getValue(1); |
| 2079 | |
| 2080 | if (isCustom) { |
| 2081 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 2082 | if (Tmp3.Val) { |
| 2083 | Tmp1 = LegalizeOp(Tmp3); |
| 2084 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 2085 | } |
| 2086 | } else { |
| 2087 | // If this is an unaligned load and the target doesn't support it, |
| 2088 | // expand it. |
| 2089 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2090 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2091 | getABITypeAlignment(MVT::getTypeForValueType(LD->getMemoryVT())); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2092 | if (LD->getAlignment() < ABIAlignment){ |
| 2093 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.Val), DAG, |
| 2094 | TLI); |
| 2095 | Tmp1 = Result.getOperand(0); |
| 2096 | Tmp2 = Result.getOperand(1); |
| 2097 | Tmp1 = LegalizeOp(Tmp1); |
| 2098 | Tmp2 = LegalizeOp(Tmp2); |
| 2099 | } |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2100 | } |
| 2101 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | case TargetLowering::Expand: |
| 2104 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 2105 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 2106 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(), |
| 2107 | LD->getSrcValueOffset(), |
| 2108 | LD->isVolatile(), LD->getAlignment()); |
| 2109 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
| 2110 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 2111 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 2112 | break; |
| 2113 | } |
| 2114 | assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!"); |
| 2115 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 2116 | // zero/sign extend inreg. |
| 2117 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 2118 | Tmp1, Tmp2, LD->getSrcValue(), |
| 2119 | LD->getSrcValueOffset(), SrcVT, |
| 2120 | LD->isVolatile(), LD->getAlignment()); |
| 2121 | SDOperand ValRes; |
| 2122 | if (ExtType == ISD::SEXTLOAD) |
| 2123 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
| 2124 | Result, DAG.getValueType(SrcVT)); |
| 2125 | else |
| 2126 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
| 2127 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 2128 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2129 | break; |
| 2130 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2131 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 2132 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 2133 | // Since loads produce two values, make sure to remember that we legalized |
| 2134 | // both of them. |
| 2135 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2136 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 2137 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 2138 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 2139 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2140 | case ISD::EXTRACT_ELEMENT: { |
| 2141 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 2142 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2143 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2144 | case Legal: |
| 2145 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 2146 | // 1 -> Hi |
| 2147 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 2148 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 2149 | TLI.getShiftAmountTy())); |
| 2150 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 2151 | } else { |
| 2152 | // 0 -> Lo |
| 2153 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 2154 | Node->getOperand(0)); |
| 2155 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | case Expand: |
| 2158 | // Get both the low and high parts. |
| 2159 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2160 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 2161 | Result = Tmp2; // 1 -> Hi |
| 2162 | else |
| 2163 | Result = Tmp1; // 0 -> Lo |
| 2164 | break; |
| 2165 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2166 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 2167 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2168 | |
| 2169 | case ISD::CopyToReg: |
| 2170 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2172 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2173 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2174 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2175 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2176 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2177 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2178 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2179 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2180 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 2181 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2182 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 2183 | Tmp3); |
| 2184 | } else { |
| 2185 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | // Since this produces two values, make sure to remember that we legalized |
| 2189 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2190 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2191 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2192 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 2193 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2194 | break; |
| 2195 | |
| 2196 | case ISD::RET: |
| 2197 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 2198 | |
| 2199 | // Ensure that libcalls are emitted before a return. |
| 2200 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 2201 | Tmp1 = LegalizeOp(Tmp1); |
| 2202 | LastCALLSEQ_END = DAG.getEntryNode(); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2203 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2204 | switch (Node->getNumOperands()) { |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2205 | case 3: // ret val |
Evan Cheng | 98f8aeb | 2006-04-11 06:33:39 +0000 | [diff] [blame] | 2206 | Tmp2 = Node->getOperand(1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2207 | Tmp3 = Node->getOperand(2); // Signness |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2208 | switch (getTypeAction(Tmp2.getValueType())) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2209 | case Legal: |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2210 | Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2211 | break; |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2212 | case Expand: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2213 | if (!MVT::isVector(Tmp2.getValueType())) { |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2214 | SDOperand Lo, Hi; |
| 2215 | ExpandOp(Tmp2, Lo, Hi); |
Chris Lattner | edf2e8d | 2007-03-06 20:01:06 +0000 | [diff] [blame] | 2216 | |
| 2217 | // Big endian systems want the hi reg first. |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 2218 | if (TLI.isBigEndian()) |
Chris Lattner | edf2e8d | 2007-03-06 20:01:06 +0000 | [diff] [blame] | 2219 | std::swap(Lo, Hi); |
| 2220 | |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 2221 | if (Hi.Val) |
| 2222 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
| 2223 | else |
| 2224 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3); |
Chris Lattner | f921a51 | 2006-08-21 20:24:53 +0000 | [diff] [blame] | 2225 | Result = LegalizeOp(Result); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2226 | } else { |
| 2227 | SDNode *InVal = Tmp2.Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 2228 | int InIx = Tmp2.ResNo; |
| 2229 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx)); |
| 2230 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx)); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2231 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2232 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2233 | // type. If so, convert to the vector type. |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2234 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2235 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2236 | // Turn this into a return of the vector type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2237 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2238 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2239 | } else if (NumElems == 1) { |
| 2240 | // Turn this into a return of the scalar type. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2241 | Tmp2 = ScalarizeVectorOp(Tmp2); |
| 2242 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2243 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2244 | |
| 2245 | // FIXME: Returns of gcc generic vectors smaller than a legal type |
| 2246 | // should be returned in integer registers! |
| 2247 | |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2248 | // The scalarized value type may not be legal, e.g. it might require |
| 2249 | // promotion or expansion. Relegalize the return. |
| 2250 | Result = LegalizeOp(Result); |
| 2251 | } else { |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2252 | // FIXME: Returns of gcc generic vectors larger than a legal vector |
| 2253 | // type should be returned by reference! |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2254 | SDOperand Lo, Hi; |
| 2255 | SplitVectorOp(Tmp2, Lo, Hi); |
Chris Lattner | fea997a | 2007-02-01 04:55:59 +0000 | [diff] [blame] | 2256 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3); |
Chris Lattner | f87324e | 2006-04-11 01:31:51 +0000 | [diff] [blame] | 2257 | Result = LegalizeOp(Result); |
| 2258 | } |
| 2259 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2260 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2261 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2262 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2263 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2264 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2265 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2266 | } |
| 2267 | break; |
| 2268 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2269 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2270 | break; |
| 2271 | default: { // ret <values> |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2272 | SmallVector<SDOperand, 8> NewValues; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2273 | NewValues.push_back(Tmp1); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2274 | for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2275 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 2276 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 2277 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2278 | NewValues.push_back(Node->getOperand(i+1)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | case Expand: { |
| 2281 | SDOperand Lo, Hi; |
Dan Gohman | 6595cb3 | 2007-06-27 16:08:04 +0000 | [diff] [blame] | 2282 | assert(!MVT::isExtendedVT(Node->getOperand(i).getValueType()) && |
Chris Lattner | b49e52c | 2006-04-11 02:00:08 +0000 | [diff] [blame] | 2283 | "FIXME: TODO: implement returning non-legal vector types!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2284 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 2285 | NewValues.push_back(Lo); |
Evan Cheng | 8e7d056 | 2006-05-26 23:09:09 +0000 | [diff] [blame] | 2286 | NewValues.push_back(Node->getOperand(i+1)); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 2287 | if (Hi.Val) { |
| 2288 | NewValues.push_back(Hi); |
| 2289 | NewValues.push_back(Node->getOperand(i+1)); |
| 2290 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2291 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2292 | } |
| 2293 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2294 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2295 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2296 | |
| 2297 | if (NewValues.size() == Node->getNumOperands()) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2298 | Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size()); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2299 | else |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2300 | Result = DAG.getNode(ISD::RET, MVT::Other, |
| 2301 | &NewValues[0], NewValues.size()); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2302 | break; |
| 2303 | } |
| 2304 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 2305 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 2306 | if (Result.getOpcode() == ISD::RET) { |
| 2307 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 2308 | default: assert(0 && "This action is not supported yet!"); |
| 2309 | case TargetLowering::Legal: break; |
| 2310 | case TargetLowering::Custom: |
| 2311 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2312 | if (Tmp1.Val) Result = Tmp1; |
| 2313 | break; |
| 2314 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 2315 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2316 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2317 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2318 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 2319 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 2320 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2321 | int SVOffset = ST->getSrcValueOffset(); |
| 2322 | unsigned Alignment = ST->getAlignment(); |
| 2323 | bool isVolatile = ST->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2324 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2325 | if (!ST->isTruncatingStore()) { |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2326 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 2327 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 2328 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 2329 | // to phase ordering between legalized code and the dag combiner. This |
| 2330 | // probably means that we need to integrate dag combiner and legalizer |
| 2331 | // together. |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2332 | // We generally can't do this one for long doubles. |
Chris Lattner | 2b4c279 | 2007-10-13 06:35:54 +0000 | [diff] [blame] | 2333 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2334 | if (CFP->getValueType(0) == MVT::f32 && |
| 2335 | getTypeAction(MVT::i32) == Legal) { |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 2336 | Tmp3 = DAG.getConstant(CFP->getValueAPF(). |
| 2337 | convertToAPInt().zextOrTrunc(32), |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 2338 | MVT::i32); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 2339 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2340 | SVOffset, isVolatile, Alignment); |
| 2341 | break; |
| 2342 | } else if (CFP->getValueType(0) == MVT::f64) { |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2343 | // If this target supports 64-bit registers, do a single 64-bit store. |
| 2344 | if (getTypeAction(MVT::i64) == Legal) { |
| 2345 | Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt(). |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 2346 | zextOrTrunc(64), MVT::i64); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2347 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2348 | SVOffset, isVolatile, Alignment); |
| 2349 | break; |
| 2350 | } else if (getTypeAction(MVT::i32) == Legal) { |
| 2351 | // Otherwise, if the target supports 32-bit registers, use 2 32-bit |
| 2352 | // stores. If the target supports neither 32- nor 64-bits, this |
| 2353 | // xform is certainly not worth it. |
Dan Gohman | 6cf9b8a | 2008-03-11 00:11:06 +0000 | [diff] [blame] | 2354 | const APInt &IntVal =CFP->getValueAPF().convertToAPInt(); |
| 2355 | SDOperand Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32); |
| 2356 | SDOperand Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 2357 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2358 | |
| 2359 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
| 2360 | SVOffset, isVolatile, Alignment); |
| 2361 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2362 | DAG.getIntPtrConstant(4)); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2363 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4, |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2364 | isVolatile, MinAlign(Alignment, 4U)); |
Chris Lattner | 3cb9351 | 2007-10-15 05:46:06 +0000 | [diff] [blame] | 2365 | |
| 2366 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2367 | break; |
| 2368 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2369 | } |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2372 | switch (getTypeAction(ST->getMemoryVT())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2373 | case Legal: { |
| 2374 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2375 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2376 | ST->getOffset()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2377 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2378 | MVT::ValueType VT = Tmp3.getValueType(); |
| 2379 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 2380 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2381 | case TargetLowering::Legal: |
| 2382 | // If this is an unaligned store and the target doesn't support it, |
| 2383 | // expand it. |
| 2384 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2385 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2386 | getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT())); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2387 | if (ST->getAlignment() < ABIAlignment) |
| 2388 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2389 | TLI); |
| 2390 | } |
| 2391 | break; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2392 | case TargetLowering::Custom: |
| 2393 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2394 | if (Tmp1.Val) Result = Tmp1; |
| 2395 | break; |
| 2396 | case TargetLowering::Promote: |
| 2397 | assert(MVT::isVector(VT) && "Unknown legal promote case!"); |
| 2398 | Tmp3 = DAG.getNode(ISD::BIT_CONVERT, |
| 2399 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
| 2400 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2401 | ST->getSrcValue(), SVOffset, isVolatile, |
| 2402 | Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2403 | break; |
| 2404 | } |
| 2405 | break; |
| 2406 | } |
| 2407 | case Promote: |
| 2408 | // Truncate the value and store the result. |
| 2409 | Tmp3 = PromoteOp(ST->getValue()); |
| 2410 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2411 | SVOffset, ST->getMemoryVT(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2412 | isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2413 | break; |
| 2414 | |
| 2415 | case Expand: |
| 2416 | unsigned IncrementSize = 0; |
| 2417 | SDOperand Lo, Hi; |
| 2418 | |
| 2419 | // If this is a vector type, then we have to calculate the increment as |
| 2420 | // the product of the element size in bytes, and the number of elements |
| 2421 | // in the high half of the vector. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2422 | if (MVT::isVector(ST->getValue().getValueType())) { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2423 | SDNode *InVal = ST->getValue().Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 2424 | int InIx = ST->getValue().ResNo; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2425 | MVT::ValueType InVT = InVal->getValueType(InIx); |
| 2426 | unsigned NumElems = MVT::getVectorNumElements(InVT); |
| 2427 | MVT::ValueType EVT = MVT::getVectorElementType(InVT); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2428 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2429 | // Figure out if there is a simple type corresponding to this Vector |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2430 | // type. If so, convert to the vector type. |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2431 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 2432 | if (TLI.isTypeLegal(TVT)) { |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 2433 | // Turn this into a normal store of the vector type. |
Dan Gohman | 21be384 | 2008-02-15 18:11:59 +0000 | [diff] [blame] | 2434 | Tmp3 = LegalizeOp(ST->getValue()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2435 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2436 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2437 | Result = LegalizeOp(Result); |
| 2438 | break; |
| 2439 | } else if (NumElems == 1) { |
| 2440 | // Turn this into a normal store of the scalar type. |
Dan Gohman | 21be384 | 2008-02-15 18:11:59 +0000 | [diff] [blame] | 2441 | Tmp3 = ScalarizeVectorOp(ST->getValue()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2442 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2443 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2444 | // The scalarized value type may not be legal, e.g. it might require |
| 2445 | // promotion or expansion. Relegalize the scalar store. |
| 2446 | Result = LegalizeOp(Result); |
| 2447 | break; |
| 2448 | } else { |
Dan Gohman | 21be384 | 2008-02-15 18:11:59 +0000 | [diff] [blame] | 2449 | SplitVectorOp(ST->getValue(), Lo, Hi); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 2450 | IncrementSize = MVT::getVectorNumElements(Lo.Val->getValueType(0)) * |
| 2451 | MVT::getSizeInBits(EVT)/8; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } else { |
Dan Gohman | 21be384 | 2008-02-15 18:11:59 +0000 | [diff] [blame] | 2454 | ExpandOp(ST->getValue(), Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2455 | IncrementSize = Hi.Val ? MVT::getSizeInBits(Hi.getValueType())/8 : 0; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2456 | |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 2457 | if (TLI.isBigEndian()) |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2458 | std::swap(Lo, Hi); |
| 2459 | } |
| 2460 | |
| 2461 | Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2462 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 2463 | |
| 2464 | if (Hi.Val == NULL) { |
| 2465 | // Must be int <-> float one-to-one expansion. |
| 2466 | Result = Lo; |
| 2467 | break; |
| 2468 | } |
| 2469 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2470 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2471 | DAG.getIntPtrConstant(IncrementSize)); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2472 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 2473 | "Pointers must be legal!"); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2474 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 2475 | Alignment = MinAlign(Alignment, IncrementSize); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2476 | Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 2477 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2478 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2479 | break; |
| 2480 | } |
| 2481 | } else { |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 2482 | switch (getTypeAction(ST->getValue().getValueType())) { |
| 2483 | case Legal: |
| 2484 | Tmp3 = LegalizeOp(ST->getValue()); |
| 2485 | break; |
| 2486 | case Promote: |
| 2487 | // We can promote the value, the truncstore will still take care of it. |
| 2488 | Tmp3 = PromoteOp(ST->getValue()); |
| 2489 | break; |
| 2490 | case Expand: |
| 2491 | // Just store the low part. This may become a non-trunc store, so make |
| 2492 | // sure to use getTruncStore, not UpdateNodeOperands below. |
| 2493 | ExpandOp(ST->getValue(), Tmp3, Tmp4); |
| 2494 | return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2495 | SVOffset, MVT::i8, isVolatile, Alignment); |
| 2496 | } |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 2497 | |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2498 | MVT::ValueType StVT = ST->getMemoryVT(); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2499 | unsigned StWidth = MVT::getSizeInBits(StVT); |
| 2500 | |
| 2501 | if (StWidth != MVT::getStoreSizeInBits(StVT)) { |
| 2502 | // Promote to a byte-sized store with upper bits zero if not |
| 2503 | // storing an integral number of bytes. For example, promote |
| 2504 | // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) |
| 2505 | MVT::ValueType NVT = MVT::getIntegerType(MVT::getStoreSizeInBits(StVT)); |
| 2506 | Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT); |
| 2507 | Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2508 | SVOffset, NVT, isVolatile, Alignment); |
| 2509 | } else if (StWidth & (StWidth - 1)) { |
| 2510 | // If not storing a power-of-2 number of bits, expand as two stores. |
| 2511 | assert(MVT::isExtendedVT(StVT) && !MVT::isVector(StVT) && |
| 2512 | "Unsupported truncstore!"); |
| 2513 | unsigned RoundWidth = 1 << Log2_32(StWidth); |
| 2514 | assert(RoundWidth < StWidth); |
| 2515 | unsigned ExtraWidth = StWidth - RoundWidth; |
| 2516 | assert(ExtraWidth < RoundWidth); |
| 2517 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 2518 | "Store size not an integral number of bytes!"); |
| 2519 | MVT::ValueType RoundVT = MVT::getIntegerType(RoundWidth); |
| 2520 | MVT::ValueType ExtraVT = MVT::getIntegerType(ExtraWidth); |
| 2521 | SDOperand Lo, Hi; |
| 2522 | unsigned IncrementSize; |
| 2523 | |
| 2524 | if (TLI.isLittleEndian()) { |
| 2525 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) |
| 2526 | // Store the bottom RoundWidth bits. |
| 2527 | Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2528 | SVOffset, RoundVT, |
| 2529 | isVolatile, Alignment); |
| 2530 | |
| 2531 | // Store the remaining ExtraWidth bits. |
| 2532 | IncrementSize = RoundWidth / 8; |
| 2533 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2534 | DAG.getIntPtrConstant(IncrementSize)); |
| 2535 | Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3, |
| 2536 | DAG.getConstant(RoundWidth, TLI.getShiftAmountTy())); |
| 2537 | Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), |
| 2538 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 2539 | MinAlign(Alignment, IncrementSize)); |
| 2540 | } else { |
| 2541 | // Big endian - avoid unaligned stores. |
| 2542 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X |
| 2543 | // Store the top RoundWidth bits. |
| 2544 | Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3, |
| 2545 | DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy())); |
| 2546 | Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset, |
| 2547 | RoundVT, isVolatile, Alignment); |
| 2548 | |
| 2549 | // Store the remaining ExtraWidth bits. |
| 2550 | IncrementSize = RoundWidth / 8; |
| 2551 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 2552 | DAG.getIntPtrConstant(IncrementSize)); |
| 2553 | Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), |
| 2554 | SVOffset + IncrementSize, ExtraVT, isVolatile, |
| 2555 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 2556 | } |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2557 | |
| 2558 | // The order of the stores doesn't matter. |
| 2559 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 2560 | } else { |
| 2561 | if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 2562 | Tmp2 != ST->getBasePtr()) |
| 2563 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 2564 | ST->getOffset()); |
| 2565 | |
| 2566 | switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { |
| 2567 | default: assert(0 && "This action is not supported yet!"); |
| 2568 | case TargetLowering::Legal: |
| 2569 | // If this is an unaligned store and the target doesn't support it, |
| 2570 | // expand it. |
| 2571 | if (!TLI.allowsUnalignedMemoryAccesses()) { |
| 2572 | unsigned ABIAlignment = TLI.getTargetData()-> |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 2573 | getABITypeAlignment(MVT::getTypeForValueType(ST->getMemoryVT())); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 2574 | if (ST->getAlignment() < ABIAlignment) |
| 2575 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.Val), DAG, |
| 2576 | TLI); |
| 2577 | } |
| 2578 | break; |
| 2579 | case TargetLowering::Custom: |
| 2580 | Result = TLI.LowerOperation(Result, DAG); |
| 2581 | break; |
| 2582 | case Expand: |
| 2583 | // TRUNCSTORE:i16 i32 -> STORE i16 |
| 2584 | assert(isTypeLegal(StVT) && "Do not know how to expand this store!"); |
| 2585 | Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3); |
| 2586 | Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset, |
| 2587 | isVolatile, Alignment); |
| 2588 | break; |
| 2589 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2590 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2591 | } |
| 2592 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 2593 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2594 | case ISD::PCMARKER: |
| 2595 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2596 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 2597 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2598 | case ISD::STACKSAVE: |
| 2599 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2600 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2601 | Tmp1 = Result.getValue(0); |
| 2602 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2603 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2604 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 2605 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2606 | case TargetLowering::Legal: break; |
| 2607 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2608 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 2609 | if (Tmp3.Val) { |
| 2610 | Tmp1 = LegalizeOp(Tmp3); |
| 2611 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2612 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2613 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2614 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2615 | // Expand to CopyFromReg if the target set |
| 2616 | // StackPointerRegisterToSaveRestore. |
| 2617 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2618 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2619 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2620 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2621 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2622 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 2623 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2624 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2625 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2626 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2627 | |
| 2628 | // Since stacksave produce two values, make sure to remember that we |
| 2629 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2630 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2631 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 2632 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2634 | case ISD::STACKRESTORE: |
| 2635 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2636 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2637 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2638 | |
| 2639 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 2640 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2641 | case TargetLowering::Legal: break; |
| 2642 | case TargetLowering::Custom: |
| 2643 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2644 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 2647 | // Expand to CopyToReg if the target set |
| 2648 | // StackPointerRegisterToSaveRestore. |
| 2649 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 2650 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 2651 | } else { |
| 2652 | Result = Tmp1; |
| 2653 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 2654 | break; |
| 2655 | } |
| 2656 | break; |
| 2657 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 2658 | case ISD::READCYCLECOUNTER: |
| 2659 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2660 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2661 | switch (TLI.getOperationAction(ISD::READCYCLECOUNTER, |
| 2662 | Node->getValueType(0))) { |
| 2663 | default: assert(0 && "This action is not supported yet!"); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2664 | case TargetLowering::Legal: |
| 2665 | Tmp1 = Result.getValue(0); |
| 2666 | Tmp2 = Result.getValue(1); |
| 2667 | break; |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2668 | case TargetLowering::Custom: |
| 2669 | Result = TLI.LowerOperation(Result, DAG); |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2670 | Tmp1 = LegalizeOp(Result.getValue(0)); |
| 2671 | Tmp2 = LegalizeOp(Result.getValue(1)); |
Evan Cheng | f0b3ba6 | 2006-11-29 08:26:18 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | } |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 2674 | |
| 2675 | // Since rdcc produce two values, make sure to remember that we legalized |
| 2676 | // both of them. |
Evan Cheng | 6a16c5a | 2006-11-29 19:13:47 +0000 | [diff] [blame] | 2677 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 2678 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2679 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 2680 | |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2681 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2682 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2683 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 2684 | case Legal: |
| 2685 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 2686 | break; |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2687 | case Promote: { |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2688 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2689 | // Make sure the condition is either zero or one. |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2690 | unsigned BitWidth = Tmp1.getValueSizeInBits(); |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 2691 | if (!DAG.MaskedValueIsZero(Tmp1, |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2692 | APInt::getHighBitsSet(BitWidth, BitWidth-1))) |
Chris Lattner | b6c8060 | 2006-11-28 01:03:30 +0000 | [diff] [blame] | 2693 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 2694 | break; |
| 2695 | } |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 2696 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2697 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 2698 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2699 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2700 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2701 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2702 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2703 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2704 | case TargetLowering::Legal: break; |
| 2705 | case TargetLowering::Custom: { |
| 2706 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2707 | if (Tmp1.Val) Result = Tmp1; |
| 2708 | break; |
| 2709 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2710 | case TargetLowering::Expand: |
| 2711 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 2712 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 2713 | Tmp2, Tmp3, |
| 2714 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 2715 | } else { |
| 2716 | Result = DAG.getSelectCC(Tmp1, |
| 2717 | DAG.getConstant(0, Tmp1.getValueType()), |
| 2718 | Tmp2, Tmp3, ISD::SETNE); |
| 2719 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2720 | break; |
| 2721 | case TargetLowering::Promote: { |
| 2722 | MVT::ValueType NVT = |
| 2723 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 2724 | unsigned ExtOp, TruncOp; |
Evan Cheng | 41f6cbb | 2006-04-12 16:33:18 +0000 | [diff] [blame] | 2725 | if (MVT::isVector(Tmp2.getValueType())) { |
| 2726 | ExtOp = ISD::BIT_CONVERT; |
| 2727 | TruncOp = ISD::BIT_CONVERT; |
| 2728 | } else if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2729 | ExtOp = ISD::ANY_EXTEND; |
| 2730 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2731 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2732 | ExtOp = ISD::FP_EXTEND; |
| 2733 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2734 | } |
| 2735 | // Promote each of the values to the new type. |
| 2736 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 2737 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 2738 | // Perform the larger operation, then round down. |
| 2739 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2740 | if (TruncOp != ISD::FP_ROUND) |
| 2741 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 2742 | else |
| 2743 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result, |
| 2744 | DAG.getIntPtrConstant(0)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2745 | break; |
| 2746 | } |
| 2747 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2748 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2749 | case ISD::SELECT_CC: { |
| 2750 | Tmp1 = Node->getOperand(0); // LHS |
| 2751 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2752 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 2753 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2754 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2755 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2756 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 2757 | |
| 2758 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 2759 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 2760 | // the result against zero to select between true and false values. |
| 2761 | if (Tmp2.Val == 0) { |
| 2762 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2763 | CC = DAG.getCondCode(ISD::SETNE); |
| 2764 | } |
| 2765 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 2766 | |
| 2767 | // Everything is legal, see if we should expand this op or something. |
| 2768 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 2769 | default: assert(0 && "This action is not supported yet!"); |
| 2770 | case TargetLowering::Legal: break; |
| 2771 | case TargetLowering::Custom: |
| 2772 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2773 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2774 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2775 | } |
| 2776 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2777 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2778 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2779 | Tmp1 = Node->getOperand(0); |
| 2780 | Tmp2 = Node->getOperand(1); |
| 2781 | Tmp3 = Node->getOperand(2); |
| 2782 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 2783 | |
| 2784 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 2785 | // not always be possible to return a true LHS & RHS. In this case, just |
| 2786 | // return the value we legalized, returned in the LHS |
| 2787 | if (Tmp2.Val == 0) { |
| 2788 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2789 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2790 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 2792 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2793 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 2794 | case TargetLowering::Custom: |
| 2795 | isCustom = true; |
| 2796 | // FALLTHROUGH. |
| 2797 | case TargetLowering::Legal: |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2798 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2799 | if (isCustom) { |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2800 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 2801 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2802 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2803 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2804 | case TargetLowering::Promote: { |
| 2805 | // First step, figure out the appropriate operation to use. |
| 2806 | // Allow SETCC to not be supported for all legal data types |
| 2807 | // Mostly this targets FP |
| 2808 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 2809 | MVT::ValueType OldVT = NewInTy; OldVT = OldVT; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2810 | |
| 2811 | // Scan for the appropriate larger type to use. |
| 2812 | while (1) { |
| 2813 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 2814 | |
| 2815 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 2816 | "Fell off of the edge of the integer world"); |
| 2817 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 2818 | "Fell off of the edge of the floating point world"); |
| 2819 | |
| 2820 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 2821 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2822 | break; |
| 2823 | } |
| 2824 | if (MVT::isInteger(NewInTy)) |
| 2825 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 2826 | else { |
| 2827 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 2828 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 2829 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2830 | Tmp1 = LegalizeOp(Tmp1); |
| 2831 | Tmp2 = LegalizeOp(Tmp2); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2832 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 2833 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 2834 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 2835 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2836 | case TargetLowering::Expand: |
| 2837 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 2838 | // rhs that selects between const 1 (true) and const 0 (false). |
| 2839 | MVT::ValueType VT = Node->getValueType(0); |
| 2840 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 2841 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 2842 | Tmp3); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 2843 | break; |
| 2844 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2845 | break; |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2847 | case ISD::SHL_PARTS: |
| 2848 | case ISD::SRA_PARTS: |
| 2849 | case ISD::SRL_PARTS: { |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2850 | SmallVector<SDOperand, 8> Ops; |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2851 | bool Changed = false; |
| 2852 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2853 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 2854 | Changed |= Ops.back() != Node->getOperand(i); |
| 2855 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2856 | if (Changed) |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 2857 | Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size()); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2858 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2859 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 2860 | Node->getValueType(0))) { |
| 2861 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2862 | case TargetLowering::Legal: break; |
| 2863 | case TargetLowering::Custom: |
| 2864 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2865 | if (Tmp1.Val) { |
| 2866 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2867 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2868 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2869 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 2870 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 2871 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2872 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 2873 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2874 | return RetVal; |
| 2875 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 2876 | break; |
| 2877 | } |
| 2878 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2879 | // Since these produce multiple values, make sure to remember that we |
| 2880 | // legalized all of them. |
| 2881 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 2882 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 2883 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 2884 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2885 | |
| 2886 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2887 | case ISD::ADD: |
| 2888 | case ISD::SUB: |
| 2889 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 2890 | case ISD::MULHS: |
| 2891 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2892 | case ISD::UDIV: |
| 2893 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2894 | case ISD::AND: |
| 2895 | case ISD::OR: |
| 2896 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 2897 | case ISD::SHL: |
| 2898 | case ISD::SRL: |
| 2899 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2900 | case ISD::FADD: |
| 2901 | case ISD::FSUB: |
| 2902 | case ISD::FMUL: |
| 2903 | case ISD::FDIV: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2904 | case ISD::FPOW: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2905 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 2906 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 2907 | case Expand: assert(0 && "Not possible"); |
| 2908 | case Legal: |
| 2909 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 2910 | break; |
| 2911 | case Promote: |
| 2912 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 2913 | break; |
| 2914 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2915 | |
| 2916 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2917 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 2918 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2919 | default: assert(0 && "BinOp legalize operation not supported"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2920 | case TargetLowering::Legal: break; |
| 2921 | case TargetLowering::Custom: |
| 2922 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2923 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 2924 | break; |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2925 | case TargetLowering::Expand: { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2926 | MVT::ValueType VT = Op.getValueType(); |
| 2927 | |
| 2928 | // See if multiply or divide can be lowered using two-result operations. |
| 2929 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 2930 | if (Node->getOpcode() == ISD::MUL) { |
| 2931 | // We just need the low half of the multiply; try both the signed |
| 2932 | // and unsigned forms. If the target supports both SMUL_LOHI and |
| 2933 | // UMUL_LOHI, form a preference by checking which forms of plain |
| 2934 | // MULH it supports. |
| 2935 | bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT); |
| 2936 | bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT); |
| 2937 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT); |
| 2938 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT); |
| 2939 | unsigned OpToUse = 0; |
| 2940 | if (HasSMUL_LOHI && !HasMULHS) { |
| 2941 | OpToUse = ISD::SMUL_LOHI; |
| 2942 | } else if (HasUMUL_LOHI && !HasMULHU) { |
| 2943 | OpToUse = ISD::UMUL_LOHI; |
| 2944 | } else if (HasSMUL_LOHI) { |
| 2945 | OpToUse = ISD::SMUL_LOHI; |
| 2946 | } else if (HasUMUL_LOHI) { |
| 2947 | OpToUse = ISD::UMUL_LOHI; |
| 2948 | } |
| 2949 | if (OpToUse) { |
| 2950 | Result = SDOperand(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).Val, 0); |
| 2951 | break; |
| 2952 | } |
| 2953 | } |
| 2954 | if (Node->getOpcode() == ISD::MULHS && |
| 2955 | TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) { |
| 2956 | Result = SDOperand(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1); |
| 2957 | break; |
| 2958 | } |
| 2959 | if (Node->getOpcode() == ISD::MULHU && |
| 2960 | TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) { |
| 2961 | Result = SDOperand(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).Val, 1); |
| 2962 | break; |
| 2963 | } |
| 2964 | if (Node->getOpcode() == ISD::SDIV && |
| 2965 | TLI.isOperationLegal(ISD::SDIVREM, VT)) { |
| 2966 | Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 0); |
| 2967 | break; |
| 2968 | } |
| 2969 | if (Node->getOpcode() == ISD::UDIV && |
| 2970 | TLI.isOperationLegal(ISD::UDIVREM, VT)) { |
| 2971 | Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 0); |
| 2972 | break; |
| 2973 | } |
| 2974 | |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2975 | // Check to see if we have a libcall for this operator. |
| 2976 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 2977 | bool isSigned = false; |
| 2978 | switch (Node->getOpcode()) { |
| 2979 | case ISD::UDIV: |
| 2980 | case ISD::SDIV: |
| 2981 | if (VT == MVT::i32) { |
| 2982 | LC = Node->getOpcode() == ISD::UDIV |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 2983 | ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32; |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2984 | isSigned = Node->getOpcode() == ISD::SDIV; |
| 2985 | } |
| 2986 | break; |
| 2987 | case ISD::FPOW: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 2988 | LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, |
| 2989 | RTLIB::POW_PPCF128); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 2990 | break; |
| 2991 | default: break; |
| 2992 | } |
| 2993 | if (LC != RTLIB::UNKNOWN_LIBCALL) { |
| 2994 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 2995 | Result = ExpandLibCall(LC, Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 2996 | break; |
| 2997 | } |
| 2998 | |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 2999 | assert(MVT::isVector(Node->getValueType(0)) && |
| 3000 | "Cannot expand this binary operator!"); |
| 3001 | // Expand the operation into a bunch of nasty scalar code. |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3002 | Result = LegalizeOp(UnrollVectorOp(Op)); |
Chris Lattner | bc70cf8 | 2006-04-02 03:57:31 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | } |
Evan Cheng | cc98761 | 2006-04-12 21:20:24 +0000 | [diff] [blame] | 3005 | case TargetLowering::Promote: { |
| 3006 | switch (Node->getOpcode()) { |
| 3007 | default: assert(0 && "Do not know how to promote this BinOp!"); |
| 3008 | case ISD::AND: |
| 3009 | case ISD::OR: |
| 3010 | case ISD::XOR: { |
| 3011 | MVT::ValueType OVT = Node->getValueType(0); |
| 3012 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 3013 | assert(MVT::isVector(OVT) && "Cannot promote this BinOp!"); |
| 3014 | // Bit convert each of the values to the new type. |
| 3015 | Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1); |
| 3016 | Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2); |
| 3017 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 3018 | // Bit convert the result back the original type. |
| 3019 | Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result); |
| 3020 | break; |
| 3021 | } |
| 3022 | } |
| 3023 | } |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 3024 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3025 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3026 | |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3027 | case ISD::SMUL_LOHI: |
| 3028 | case ISD::UMUL_LOHI: |
| 3029 | case ISD::SDIVREM: |
| 3030 | case ISD::UDIVREM: |
| 3031 | // These nodes will only be produced by target-specific lowering, so |
| 3032 | // they shouldn't be here if they aren't legal. |
Duncan Sands | a7c97a7 | 2007-10-16 09:07:20 +0000 | [diff] [blame] | 3033 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3034 | "This must be legal!"); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3035 | |
| 3036 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3037 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
| 3038 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Dan Gohman | e14ea86 | 2007-10-05 14:17:22 +0000 | [diff] [blame] | 3039 | break; |
| 3040 | |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3041 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 3042 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3043 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 3044 | case Expand: assert(0 && "Not possible"); |
| 3045 | case Legal: |
| 3046 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 3047 | break; |
| 3048 | case Promote: |
| 3049 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 3050 | break; |
| 3051 | } |
| 3052 | |
| 3053 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 3054 | |
| 3055 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 3056 | default: assert(0 && "Operation not supported"); |
| 3057 | case TargetLowering::Custom: |
| 3058 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3059 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 3060 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3061 | case TargetLowering::Legal: break; |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3062 | case TargetLowering::Expand: { |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 3063 | // If this target supports fabs/fneg natively and select is cheap, |
| 3064 | // do this efficiently. |
| 3065 | if (!TLI.isSelectExpensive() && |
| 3066 | TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) == |
| 3067 | TargetLowering::Legal && |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3068 | TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) == |
Evan Cheng | 636c753 | 2007-01-05 23:33:44 +0000 | [diff] [blame] | 3069 | TargetLowering::Legal) { |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 3070 | // Get the sign bit of the RHS. |
| 3071 | MVT::ValueType IVT = |
| 3072 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 3073 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 3074 | SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit), |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 3075 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 3076 | // Get the absolute value of the result. |
| 3077 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 3078 | // Select between the nabs and abs value based on the sign bit of |
| 3079 | // the input. |
| 3080 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 3081 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 3082 | AbsVal), |
| 3083 | AbsVal); |
| 3084 | Result = LegalizeOp(Result); |
| 3085 | break; |
| 3086 | } |
| 3087 | |
| 3088 | // Otherwise, do bitwise ops! |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3089 | MVT::ValueType NVT = |
| 3090 | Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64; |
| 3091 | Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 3092 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result); |
| 3093 | Result = LegalizeOp(Result); |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3094 | break; |
| 3095 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 3096 | } |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 3097 | break; |
| 3098 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3099 | case ISD::ADDC: |
| 3100 | case ISD::SUBC: |
| 3101 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3102 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3103 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 3104 | // Since this produces two values, make sure to remember that we legalized |
| 3105 | // both of them. |
| 3106 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 3107 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 3108 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3109 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3110 | case ISD::ADDE: |
| 3111 | case ISD::SUBE: |
| 3112 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3113 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 3114 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 3115 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 3116 | // Since this produces two values, make sure to remember that we legalized |
| 3117 | // both of them. |
| 3118 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 3119 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 3120 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3121 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3122 | case ISD::BUILD_PAIR: { |
| 3123 | MVT::ValueType PairTy = Node->getValueType(0); |
| 3124 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 3125 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 3126 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 3127 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3128 | case TargetLowering::Promote: |
| 3129 | case TargetLowering::Custom: |
| 3130 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3131 | case TargetLowering::Legal: |
| 3132 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 3133 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 3134 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3135 | case TargetLowering::Expand: |
| 3136 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 3137 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 3138 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 3139 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 3140 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3141 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 3142 | break; |
| 3143 | } |
| 3144 | break; |
| 3145 | } |
| 3146 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3147 | case ISD::UREM: |
| 3148 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3149 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3150 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3151 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3152 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3153 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3154 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 3155 | case TargetLowering::Custom: |
| 3156 | isCustom = true; |
| 3157 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3158 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3159 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3160 | if (isCustom) { |
| 3161 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3162 | if (Tmp1.Val) Result = Tmp1; |
| 3163 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3164 | break; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3165 | case TargetLowering::Expand: { |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 3166 | unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 3167 | bool isSigned = DivOpc == ISD::SDIV; |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3168 | MVT::ValueType VT = Node->getValueType(0); |
| 3169 | |
| 3170 | // See if remainder can be lowered using two-result operations. |
| 3171 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 3172 | if (Node->getOpcode() == ISD::SREM && |
| 3173 | TLI.isOperationLegal(ISD::SDIVREM, VT)) { |
| 3174 | Result = SDOperand(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).Val, 1); |
| 3175 | break; |
| 3176 | } |
| 3177 | if (Node->getOpcode() == ISD::UREM && |
| 3178 | TLI.isOperationLegal(ISD::UDIVREM, VT)) { |
| 3179 | Result = SDOperand(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).Val, 1); |
| 3180 | break; |
| 3181 | } |
| 3182 | |
| 3183 | if (MVT::isInteger(VT)) { |
| 3184 | if (TLI.getOperationAction(DivOpc, VT) == |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3185 | TargetLowering::Legal) { |
| 3186 | // X % Y -> X-X/Y*Y |
Evan Cheng | 6b5578f | 2006-09-18 23:28:33 +0000 | [diff] [blame] | 3187 | Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3188 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 3189 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3190 | } else if (MVT::isVector(VT)) { |
| 3191 | Result = LegalizeOp(UnrollVectorOp(Op)); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3192 | } else { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3193 | assert(VT == MVT::i32 && |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3194 | "Cannot expand this binary operator!"); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3195 | RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM |
| 3196 | ? RTLIB::UREM_I32 : RTLIB::SREM_I32; |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3197 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 3198 | Result = ExpandLibCall(LC, Node, isSigned, Dummy); |
Evan Cheng | 52cc1ea | 2006-09-18 21:49:04 +0000 | [diff] [blame] | 3199 | } |
Dan Gohman | 0d97426 | 2007-11-06 22:11:54 +0000 | [diff] [blame] | 3200 | } else { |
| 3201 | assert(MVT::isFloatingPoint(VT) && |
| 3202 | "remainder op must have integer or floating-point type"); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3203 | if (MVT::isVector(VT)) { |
| 3204 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3205 | } else { |
| 3206 | // Floating point mod -> fmod libcall. |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3207 | RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64, |
| 3208 | RTLIB::REM_F80, RTLIB::REM_PPCF128); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3209 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 3210 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Dan Gohman | 8017631 | 2007-11-05 23:35:22 +0000 | [diff] [blame] | 3211 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3212 | } |
| 3213 | break; |
| 3214 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 3215 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 3216 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3217 | case ISD::VAARG: { |
| 3218 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3219 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3220 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 3221 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3222 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 3223 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3224 | case TargetLowering::Custom: |
| 3225 | isCustom = true; |
| 3226 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3227 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3228 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 3229 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3230 | Tmp1 = Result.getValue(1); |
| 3231 | |
| 3232 | if (isCustom) { |
| 3233 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 3234 | if (Tmp2.Val) { |
| 3235 | Result = LegalizeOp(Tmp2); |
| 3236 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 3237 | } |
| 3238 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3239 | break; |
| 3240 | case TargetLowering::Expand: { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3241 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
| 3242 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3243 | // Increment the pointer, VAList, to the next vaarg |
| 3244 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 3245 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 3246 | TLI.getPointerTy())); |
| 3247 | // Store the incremented VAList to the legalized pointer |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3248 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3249 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 3250 | Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3251 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3252 | Result = LegalizeOp(Result); |
| 3253 | break; |
| 3254 | } |
| 3255 | } |
| 3256 | // Since VAARG produces two values, make sure to remember that we |
| 3257 | // legalized both of them. |
| 3258 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3259 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 3260 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
| 3263 | case ISD::VACOPY: |
| 3264 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3265 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 3266 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 3267 | |
| 3268 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 3269 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3270 | case TargetLowering::Custom: |
| 3271 | isCustom = true; |
| 3272 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3273 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3274 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 3275 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3276 | if (isCustom) { |
| 3277 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3278 | if (Tmp1.Val) Result = Tmp1; |
| 3279 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3280 | break; |
| 3281 | case TargetLowering::Expand: |
| 3282 | // This defaults to loading a pointer from the input and storing it to the |
| 3283 | // output, returning the chain. |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3284 | const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue(); |
| 3285 | const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue(); |
| 3286 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VD, 0); |
| 3287 | Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VS, 0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3288 | break; |
| 3289 | } |
| 3290 | break; |
| 3291 | |
| 3292 | case ISD::VAEND: |
| 3293 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3294 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3295 | |
| 3296 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 3297 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3298 | case TargetLowering::Custom: |
| 3299 | isCustom = true; |
| 3300 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3301 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3302 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3303 | if (isCustom) { |
| 3304 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 3305 | if (Tmp1.Val) Result = Tmp1; |
| 3306 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | case TargetLowering::Expand: |
| 3309 | Result = Tmp1; // Default to a no-op, return the chain |
| 3310 | break; |
| 3311 | } |
| 3312 | break; |
| 3313 | |
| 3314 | case ISD::VASTART: |
| 3315 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 3316 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 3317 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3318 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 3319 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3320 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 3321 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3322 | case TargetLowering::Legal: break; |
| 3323 | case TargetLowering::Custom: |
| 3324 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3325 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | } |
| 3328 | break; |
| 3329 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 3330 | case ISD::ROTL: |
| 3331 | case ISD::ROTR: |
| 3332 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 3333 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3334 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Scott Michel | c9dc114 | 2007-04-02 21:36:32 +0000 | [diff] [blame] | 3335 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 3336 | default: |
| 3337 | assert(0 && "ROTL/ROTR legalize operation not supported"); |
| 3338 | break; |
| 3339 | case TargetLowering::Legal: |
| 3340 | break; |
| 3341 | case TargetLowering::Custom: |
| 3342 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3343 | if (Tmp1.Val) Result = Tmp1; |
| 3344 | break; |
| 3345 | case TargetLowering::Promote: |
| 3346 | assert(0 && "Do not know how to promote ROTL/ROTR"); |
| 3347 | break; |
| 3348 | case TargetLowering::Expand: |
| 3349 | assert(0 && "Do not know how to expand ROTL/ROTR"); |
| 3350 | break; |
| 3351 | } |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 3352 | break; |
| 3353 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3354 | case ISD::BSWAP: |
| 3355 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 3356 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3357 | case TargetLowering::Custom: |
| 3358 | assert(0 && "Cannot custom legalize this yet!"); |
| 3359 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3360 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3361 | break; |
| 3362 | case TargetLowering::Promote: { |
| 3363 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 3364 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3365 | unsigned DiffBits = MVT::getSizeInBits(NVT) - MVT::getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3367 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3368 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 3369 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 3370 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 3371 | break; |
| 3372 | } |
| 3373 | case TargetLowering::Expand: |
| 3374 | Result = ExpandBSWAP(Tmp1); |
| 3375 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3376 | } |
| 3377 | break; |
| 3378 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3379 | case ISD::CTPOP: |
| 3380 | case ISD::CTTZ: |
| 3381 | case ISD::CTLZ: |
| 3382 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 3383 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3384 | case TargetLowering::Custom: |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3385 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3386 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3387 | if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) == |
Scott Michel | 335f4f7 | 2007-08-02 02:22:46 +0000 | [diff] [blame] | 3388 | TargetLowering::Custom) { |
| 3389 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3390 | if (Tmp1.Val) { |
| 3391 | Result = Tmp1; |
| 3392 | } |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3393 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3394 | break; |
| 3395 | case TargetLowering::Promote: { |
| 3396 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 3397 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 3398 | |
| 3399 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3400 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 3401 | // Perform the larger operation, then subtract if needed. |
| 3402 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3403 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3404 | case ISD::CTPOP: |
| 3405 | Result = Tmp1; |
| 3406 | break; |
| 3407 | case ISD::CTTZ: |
| 3408 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 3409 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3410 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3411 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3412 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Scott Michel | 910b66d | 2007-07-30 21:00:31 +0000 | [diff] [blame] | 3413 | DAG.getConstant(MVT::getSizeInBits(OVT),NVT), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3414 | break; |
| 3415 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3416 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3417 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 3418 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 3419 | MVT::getSizeInBits(OVT), NVT)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3420 | break; |
| 3421 | } |
| 3422 | break; |
| 3423 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3424 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3425 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 3426 | break; |
| 3427 | } |
| 3428 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 3429 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3430 | // Unary operators |
| 3431 | case ISD::FABS: |
| 3432 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 3433 | case ISD::FSQRT: |
| 3434 | case ISD::FSIN: |
| 3435 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3436 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3437 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3438 | case TargetLowering::Promote: |
| 3439 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3440 | isCustom = true; |
| 3441 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3442 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3443 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 3444 | if (isCustom) { |
| 3445 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3446 | if (Tmp1.Val) Result = Tmp1; |
| 3447 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3448 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3449 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3450 | switch (Node->getOpcode()) { |
| 3451 | default: assert(0 && "Unreachable!"); |
| 3452 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3453 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 3454 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3455 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3456 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3457 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3458 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 3459 | MVT::ValueType VT = Node->getValueType(0); |
| 3460 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 3461 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 3462 | ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 3463 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 3464 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3465 | break; |
| 3466 | } |
| 3467 | case ISD::FSQRT: |
| 3468 | case ISD::FSIN: |
| 3469 | case ISD::FCOS: { |
| 3470 | MVT::ValueType VT = Node->getValueType(0); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3471 | |
| 3472 | // Expand unsupported unary vector operators by unrolling them. |
| 3473 | if (MVT::isVector(VT)) { |
| 3474 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3475 | break; |
| 3476 | } |
| 3477 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3478 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3479 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3480 | case ISD::FSQRT: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3481 | LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 3482 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | case ISD::FSIN: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3485 | LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 3486 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3487 | break; |
| 3488 | case ISD::FCOS: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 3489 | LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64, |
| 3490 | RTLIB::COS_F80, RTLIB::COS_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3491 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3492 | default: assert(0 && "Unreachable!"); |
| 3493 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 3494 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 3495 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3498 | } |
| 3499 | break; |
| 3500 | } |
| 3501 | break; |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3502 | case ISD::FPOWI: { |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 3503 | MVT::ValueType VT = Node->getValueType(0); |
| 3504 | |
| 3505 | // Expand unsupported unary vector operators by unrolling them. |
| 3506 | if (MVT::isVector(VT)) { |
| 3507 | Result = LegalizeOp(UnrollVectorOp(Op)); |
| 3508 | break; |
| 3509 | } |
| 3510 | |
| 3511 | // 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] | 3512 | RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, |
| 3513 | RTLIB::POWI_F80, RTLIB::POWI_PPCF128); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3514 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 3515 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Chris Lattner | 6ddf8ed | 2006-09-09 06:03:30 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | } |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3518 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3519 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 3520 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3521 | Node->getValueType(0)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3522 | } else if (MVT::isVector(Op.getOperand(0).getValueType())) { |
| 3523 | // The input has to be a vector type, we have to either scalarize it, pack |
| 3524 | // it, or convert it based on whether the input vector type is legal. |
| 3525 | SDNode *InVal = Node->getOperand(0).Val; |
Dale Johannesen | e526962 | 2007-10-20 00:07:52 +0000 | [diff] [blame] | 3526 | int InIx = Node->getOperand(0).ResNo; |
| 3527 | unsigned NumElems = MVT::getVectorNumElements(InVal->getValueType(InIx)); |
| 3528 | MVT::ValueType EVT = MVT::getVectorElementType(InVal->getValueType(InIx)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3529 | |
| 3530 | // Figure out if there is a simple type corresponding to this Vector |
| 3531 | // type. If so, convert to the vector type. |
| 3532 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 3533 | if (TLI.isTypeLegal(TVT)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 3534 | // Turn this into a bit convert of the vector input. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 3535 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3536 | LegalizeOp(Node->getOperand(0))); |
| 3537 | break; |
| 3538 | } else if (NumElems == 1) { |
| 3539 | // Turn this into a bit convert of the scalar input. |
| 3540 | Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), |
| 3541 | ScalarizeVectorOp(Node->getOperand(0))); |
| 3542 | break; |
| 3543 | } else { |
| 3544 | // FIXME: UNIMP! Store then reload |
| 3545 | assert(0 && "Cast from unsupported vector type not implemented yet!"); |
| 3546 | } |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 3547 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3548 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 3549 | Node->getOperand(0).getValueType())) { |
| 3550 | default: assert(0 && "Unknown operation action!"); |
| 3551 | case TargetLowering::Expand: |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 3552 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3553 | Node->getValueType(0)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3554 | break; |
| 3555 | case TargetLowering::Legal: |
| 3556 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3557 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3558 | break; |
| 3559 | } |
| 3560 | } |
| 3561 | break; |
Chris Lattner | d1f04d4 | 2006-03-24 02:26:29 +0000 | [diff] [blame] | 3562 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 3563 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 3564 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3565 | case ISD::UINT_TO_FP: { |
| 3566 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3567 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3568 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3569 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3570 | Node->getOperand(0).getValueType())) { |
| 3571 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3572 | case TargetLowering::Custom: |
| 3573 | isCustom = true; |
| 3574 | // FALLTHROUGH |
| 3575 | case TargetLowering::Legal: |
| 3576 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3577 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3578 | if (isCustom) { |
| 3579 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3580 | if (Tmp1.Val) Result = Tmp1; |
| 3581 | } |
| 3582 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3583 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 3584 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 3585 | LegalizeOp(Node->getOperand(0)), |
| 3586 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3587 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3588 | case TargetLowering::Promote: |
| 3589 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 3590 | Node->getValueType(0), |
| 3591 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3592 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 3593 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3594 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 3595 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3596 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 3597 | Node->getValueType(0), Node->getOperand(0)); |
| 3598 | break; |
| 3599 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3600 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3601 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3602 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 3603 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3604 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3605 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 3606 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3607 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3608 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3609 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3610 | break; |
| 3611 | } |
| 3612 | break; |
| 3613 | } |
| 3614 | case ISD::TRUNCATE: |
| 3615 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3616 | case Legal: |
| 3617 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3618 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3619 | break; |
| 3620 | case Expand: |
| 3621 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 3622 | |
| 3623 | // Since the result is legal, we should just be able to truncate the low |
| 3624 | // part of the source. |
| 3625 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 3626 | break; |
| 3627 | case Promote: |
| 3628 | Result = PromoteOp(Node->getOperand(0)); |
| 3629 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 3630 | break; |
| 3631 | } |
| 3632 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3633 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3634 | case ISD::FP_TO_SINT: |
| 3635 | case ISD::FP_TO_UINT: |
| 3636 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3637 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 3638 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3639 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3640 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 3641 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3642 | case TargetLowering::Custom: |
| 3643 | isCustom = true; |
| 3644 | // FALLTHROUGH |
| 3645 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3646 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3647 | if (isCustom) { |
| 3648 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 3649 | if (Tmp1.Val) Result = Tmp1; |
| 3650 | } |
| 3651 | break; |
| 3652 | case TargetLowering::Promote: |
| 3653 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 3654 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 3655 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 3656 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3657 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 3658 | SDOperand True, False; |
| 3659 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 3660 | MVT::ValueType NVT = Node->getValueType(0); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3661 | const uint64_t zero[] = {0, 0}; |
| 3662 | APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero)); |
Dan Gohman | c7773bf | 2008-02-29 01:44:25 +0000 | [diff] [blame] | 3663 | APInt x = APInt::getSignBit(MVT::getSizeInBits(NVT)); |
| 3664 | (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3665 | Tmp2 = DAG.getConstantFP(apf, VT); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 3666 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3667 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 3668 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 3669 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 3670 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3671 | Tmp2)); |
| 3672 | False = DAG.getNode(ISD::XOR, NVT, False, |
Dan Gohman | c7773bf | 2008-02-29 01:44:25 +0000 | [diff] [blame] | 3673 | DAG.getConstant(x, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3674 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 3675 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 3676 | } else { |
| 3677 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 3678 | } |
| 3679 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3680 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3681 | break; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3682 | case Expand: { |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3683 | MVT::ValueType VT = Op.getValueType(); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3684 | MVT::ValueType OVT = Node->getOperand(0).getValueType(); |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 3685 | // Convert ppcf128 to i32 |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3686 | if (OVT == MVT::ppcf128 && VT == MVT::i32) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3687 | if (Node->getOpcode() == ISD::FP_TO_SINT) { |
| 3688 | Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128, |
| 3689 | Node->getOperand(0), DAG.getValueType(MVT::f64)); |
| 3690 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result, |
| 3691 | DAG.getIntPtrConstant(1)); |
| 3692 | Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result); |
| 3693 | } else { |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 3694 | const uint64_t TwoE31[] = {0x41e0000000000000LL, 0}; |
| 3695 | APFloat apf = APFloat(APInt(128, 2, TwoE31)); |
| 3696 | Tmp2 = DAG.getConstantFP(apf, OVT); |
| 3697 | // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X |
| 3698 | // FIXME: generated code sucks. |
| 3699 | Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2, |
| 3700 | DAG.getNode(ISD::ADD, MVT::i32, |
| 3701 | DAG.getNode(ISD::FP_TO_SINT, VT, |
| 3702 | DAG.getNode(ISD::FSUB, OVT, |
| 3703 | Node->getOperand(0), Tmp2)), |
| 3704 | DAG.getConstant(0x80000000, MVT::i32)), |
| 3705 | DAG.getNode(ISD::FP_TO_SINT, VT, |
| 3706 | Node->getOperand(0)), |
| 3707 | DAG.getCondCode(ISD::SETGE)); |
| 3708 | } |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 3709 | break; |
| 3710 | } |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 3711 | // Convert f32 / f64 to i32 / i64 / i128. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 3712 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3713 | switch (Node->getOpcode()) { |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3714 | case ISD::FP_TO_SINT: { |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 3715 | if (VT == MVT::i32) { |
| 3716 | if (OVT == MVT::f32) |
| 3717 | LC = RTLIB::FPTOSINT_F32_I32; |
| 3718 | else if (OVT == MVT::f64) |
| 3719 | LC = RTLIB::FPTOSINT_F64_I32; |
| 3720 | else |
| 3721 | assert(0 && "Unexpected i32-to-fp conversion!"); |
| 3722 | } else if (VT == MVT::i64) { |
| 3723 | if (OVT == MVT::f32) |
| 3724 | LC = RTLIB::FPTOSINT_F32_I64; |
| 3725 | else if (OVT == MVT::f64) |
| 3726 | LC = RTLIB::FPTOSINT_F64_I64; |
| 3727 | else if (OVT == MVT::f80) |
| 3728 | LC = RTLIB::FPTOSINT_F80_I64; |
| 3729 | else if (OVT == MVT::ppcf128) |
| 3730 | LC = RTLIB::FPTOSINT_PPCF128_I64; |
| 3731 | else |
| 3732 | assert(0 && "Unexpected i64-to-fp conversion!"); |
| 3733 | } else if (VT == MVT::i128) { |
| 3734 | if (OVT == MVT::f32) |
| 3735 | LC = RTLIB::FPTOSINT_F32_I128; |
| 3736 | else if (OVT == MVT::f64) |
| 3737 | LC = RTLIB::FPTOSINT_F64_I128; |
| 3738 | else if (OVT == MVT::f80) |
| 3739 | LC = RTLIB::FPTOSINT_F80_I128; |
| 3740 | else if (OVT == MVT::ppcf128) |
| 3741 | LC = RTLIB::FPTOSINT_PPCF128_I128; |
| 3742 | else |
| 3743 | assert(0 && "Unexpected i128-to-fp conversion!"); |
| 3744 | } else { |
| 3745 | assert(0 && "Unexpectd int-to-fp conversion!"); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3746 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3747 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3748 | } |
| 3749 | case ISD::FP_TO_UINT: { |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 3750 | if (VT == MVT::i32) { |
| 3751 | if (OVT == MVT::f32) |
| 3752 | LC = RTLIB::FPTOUINT_F32_I32; |
| 3753 | else if (OVT == MVT::f64) |
| 3754 | LC = RTLIB::FPTOUINT_F64_I32; |
| 3755 | else if (OVT == MVT::f80) |
| 3756 | LC = RTLIB::FPTOUINT_F80_I32; |
| 3757 | else |
| 3758 | assert(0 && "Unexpected i32-to-fp conversion!"); |
| 3759 | } else if (VT == MVT::i64) { |
| 3760 | if (OVT == MVT::f32) |
| 3761 | LC = RTLIB::FPTOUINT_F32_I64; |
| 3762 | else if (OVT == MVT::f64) |
| 3763 | LC = RTLIB::FPTOUINT_F64_I64; |
| 3764 | else if (OVT == MVT::f80) |
| 3765 | LC = RTLIB::FPTOUINT_F80_I64; |
| 3766 | else if (OVT == MVT::ppcf128) |
| 3767 | LC = RTLIB::FPTOUINT_PPCF128_I64; |
| 3768 | else |
| 3769 | assert(0 && "Unexpected i64-to-fp conversion!"); |
| 3770 | } else if (VT == MVT::i128) { |
| 3771 | if (OVT == MVT::f32) |
| 3772 | LC = RTLIB::FPTOUINT_F32_I128; |
| 3773 | else if (OVT == MVT::f64) |
| 3774 | LC = RTLIB::FPTOUINT_F64_I128; |
| 3775 | else if (OVT == MVT::f80) |
| 3776 | LC = RTLIB::FPTOUINT_F80_I128; |
| 3777 | else if (OVT == MVT::ppcf128) |
| 3778 | LC = RTLIB::FPTOUINT_PPCF128_I128; |
| 3779 | else |
| 3780 | assert(0 && "Unexpected i128-to-fp conversion!"); |
| 3781 | } else { |
| 3782 | assert(0 && "Unexpectd int-to-fp conversion!"); |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3783 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3784 | break; |
Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 3785 | } |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3786 | default: assert(0 && "Unreachable!"); |
| 3787 | } |
| 3788 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 3789 | Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy); |
Evan Cheng | 6af00d5 | 2006-12-13 01:57:55 +0000 | [diff] [blame] | 3790 | break; |
| 3791 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3792 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3793 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3794 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 3795 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3796 | break; |
| 3797 | } |
| 3798 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3800 | case ISD::FP_EXTEND: { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3801 | MVT::ValueType DstVT = Op.getValueType(); |
| 3802 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3803 | if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) { |
| 3804 | // The only other way we can lower this is to turn it into a STORE, |
| 3805 | // LOAD pair, targetting a temporary location (a stack slot). |
| 3806 | Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT); |
| 3807 | break; |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3808 | } |
| 3809 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3810 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
| 3811 | case Legal: |
| 3812 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3813 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3814 | break; |
| 3815 | case Promote: |
| 3816 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3817 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1); |
| 3818 | break; |
| 3819 | } |
| 3820 | break; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3821 | } |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3822 | case ISD::FP_ROUND: { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3823 | MVT::ValueType DstVT = Op.getValueType(); |
| 3824 | MVT::ValueType SrcVT = Op.getOperand(0).getValueType(); |
| 3825 | if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) { |
| 3826 | if (SrcVT == MVT::ppcf128) { |
Dale Johannesen | 713ed3f | 2008-01-20 01:18:38 +0000 | [diff] [blame] | 3827 | SDOperand Lo; |
| 3828 | ExpandOp(Node->getOperand(0), Lo, Result); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3829 | // 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] | 3830 | if (DstVT!=MVT::f64) |
| 3831 | Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3832 | break; |
Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 3833 | } |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3834 | // The only other way we can lower this is to turn it into a STORE, |
| 3835 | // LOAD pair, targetting a temporary location (a stack slot). |
| 3836 | Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT); |
| 3837 | break; |
Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 3838 | } |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3839 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3840 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
| 3841 | case Legal: |
| 3842 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3843 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3844 | break; |
| 3845 | case Promote: |
| 3846 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3847 | Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1, |
| 3848 | Node->getOperand(1)); |
Chris Lattner | f2670a8 | 2008-01-16 06:57:07 +0000 | [diff] [blame] | 3849 | break; |
| 3850 | } |
| 3851 | break; |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 3852 | } |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3853 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3854 | case ISD::ZERO_EXTEND: |
| 3855 | case ISD::SIGN_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3856 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3857 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3858 | case Legal: |
| 3859 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Scott Michel | 0123b7d | 2008-02-15 23:05:48 +0000 | [diff] [blame] | 3860 | if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) == |
| 3861 | TargetLowering::Custom) { |
| 3862 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 3863 | if (Tmp2.Val) { |
| 3864 | Tmp1 = Tmp2; |
| 3865 | } |
| 3866 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3867 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 3868 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3869 | case Promote: |
| 3870 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3871 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3872 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 3873 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3874 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3875 | case ISD::ZERO_EXTEND: |
| 3876 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3877 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3878 | Result = DAG.getZeroExtendInReg(Result, |
| 3879 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3880 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3881 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3882 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3883 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3884 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3885 | Result, |
| 3886 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 3887 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 3888 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3889 | } |
| 3890 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3891 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3892 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3893 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 3894 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3895 | |
| 3896 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 3897 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3898 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 3899 | default: assert(0 && "This action not supported for this op yet!"); |
| 3900 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 3901 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3902 | break; |
| 3903 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3904 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 3905 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3906 | // NOTE: we could fall back on load/store here too for targets without |
| 3907 | // SAR. However, it is doubtful that any exist. |
| 3908 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 3909 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 3910 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3911 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 3912 | Node->getOperand(0), ShiftCst); |
| 3913 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 3914 | Result, ShiftCst); |
| 3915 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
Jim Laskey | 2d84c4c | 2006-10-11 17:52:19 +0000 | [diff] [blame] | 3916 | // 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] | 3917 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 3918 | |
| 3919 | // NOTE: there is a choice here between constantly creating new stack |
| 3920 | // slots and always reusing the same one. We currently always create |
| 3921 | // new ones, as reuse may inhibit scheduling. |
Chris Lattner | a66bb39 | 2008-01-16 07:51:34 +0000 | [diff] [blame] | 3922 | Result = EmitStackConvert(Node->getOperand(0), ExtraVT, |
| 3923 | Node->getValueType(0)); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3924 | } else { |
| 3925 | assert(0 && "Unknown op"); |
| 3926 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 3927 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3928 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 3929 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3930 | } |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3931 | case ISD::TRAMPOLINE: { |
| 3932 | SDOperand Ops[6]; |
| 3933 | for (unsigned i = 0; i != 6; ++i) |
| 3934 | Ops[i] = LegalizeOp(Node->getOperand(i)); |
| 3935 | Result = DAG.UpdateNodeOperands(Result, Ops, 6); |
| 3936 | // The only option for this node is to custom lower it. |
| 3937 | Result = TLI.LowerOperation(Result, DAG); |
| 3938 | assert(Result.Val && "Should always custom lower!"); |
Duncan Sands | f7331b3 | 2007-09-11 14:10:23 +0000 | [diff] [blame] | 3939 | |
| 3940 | // Since trampoline produces two values, make sure to remember that we |
| 3941 | // legalized both of them. |
| 3942 | Tmp1 = LegalizeOp(Result.getValue(1)); |
| 3943 | Result = LegalizeOp(Result); |
| 3944 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
| 3945 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 3946 | return Op.ResNo ? Tmp1 : Result; |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 3947 | } |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 3948 | case ISD::FLT_ROUNDS_: { |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 3949 | MVT::ValueType VT = Node->getValueType(0); |
| 3950 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 3951 | default: assert(0 && "This action not supported for this op yet!"); |
| 3952 | case TargetLowering::Custom: |
| 3953 | Result = TLI.LowerOperation(Op, DAG); |
| 3954 | if (Result.Val) break; |
| 3955 | // Fall Thru |
| 3956 | case TargetLowering::Legal: |
| 3957 | // If this operation is not supported, lower it to constant 1 |
| 3958 | Result = DAG.getConstant(1, VT); |
| 3959 | break; |
| 3960 | } |
| 3961 | } |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3962 | case ISD::TRAP: { |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3963 | MVT::ValueType VT = Node->getValueType(0); |
| 3964 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 3965 | default: assert(0 && "This action not supported for this op yet!"); |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3966 | case TargetLowering::Legal: |
| 3967 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 3968 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 3969 | break; |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3970 | case TargetLowering::Custom: |
| 3971 | Result = TLI.LowerOperation(Op, DAG); |
| 3972 | if (Result.Val) break; |
| 3973 | // Fall Thru |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3974 | case TargetLowering::Expand: |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3975 | // If this operation is not supported, lower it to 'abort()' call |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3976 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3977 | TargetLowering::ArgListTy Args; |
| 3978 | std::pair<SDOperand,SDOperand> CallResult = |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 3979 | TLI.LowerCallTo(Tmp1, Type::VoidTy, |
| 3980 | false, false, false, CallingConv::C, false, |
Chris Lattner | 034f12e | 2008-01-15 22:09:33 +0000 | [diff] [blame] | 3981 | DAG.getExternalSymbol("abort", TLI.getPointerTy()), |
| 3982 | Args, DAG); |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3983 | Result = CallResult.second; |
| 3984 | break; |
| 3985 | } |
Chris Lattner | 41bab0b | 2008-01-15 21:58:08 +0000 | [diff] [blame] | 3986 | break; |
Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 3987 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 3988 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3989 | |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 3990 | assert(Result.getValueType() == Op.getValueType() && |
| 3991 | "Bad legalization!"); |
| 3992 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3993 | // Make sure that the generated code is itself legal. |
| 3994 | if (Result != Op) |
| 3995 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3996 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 3997 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 3998 | // means that we always must cache transformed nodes. |
| 3999 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4000 | return Result; |
| 4001 | } |
| 4002 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4003 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 4004 | /// promote it to compute the value into a larger type. The produced value will |
| 4005 | /// have the correct bits for the low portion of the register, but no guarantee |
| 4006 | /// 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] | 4007 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 4008 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 4009 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4010 | assert(getTypeAction(VT) == Promote && |
| 4011 | "Caller should expand or legalize operands that are not promotable!"); |
| 4012 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 4013 | "Cannot promote to smaller type!"); |
| 4014 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4015 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4016 | SDOperand Result; |
| 4017 | SDNode *Node = Op.Val; |
| 4018 | |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 4019 | DenseMap<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 4020 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 4021 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4022 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 4023 | case ISD::CopyFromReg: |
| 4024 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4025 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4026 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 4027 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 4028 | #endif |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4029 | assert(0 && "Do not know how to promote this operator!"); |
| 4030 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 4031 | case ISD::UNDEF: |
| 4032 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 4033 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4034 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 4035 | if (VT != MVT::i1) |
| 4036 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 4037 | else |
| 4038 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4039 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 4040 | break; |
| 4041 | case ISD::ConstantFP: |
| 4042 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 4043 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 4044 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 4045 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 4046 | case ISD::SETCC: |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4047 | assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0))) |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4048 | && "SetCC type is not legal??"); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4049 | Result = DAG.getNode(ISD::SETCC, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4050 | TLI.getSetCCResultType(Node->getOperand(0)), |
| 4051 | Node->getOperand(0), Node->getOperand(1), |
| 4052 | Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 4053 | break; |
Chris Lattner | fdfded5 | 2006-04-12 16:20:43 +0000 | [diff] [blame] | 4054 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4055 | case ISD::TRUNCATE: |
| 4056 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4057 | case Legal: |
| 4058 | Result = LegalizeOp(Node->getOperand(0)); |
| 4059 | assert(Result.getValueType() >= NVT && |
| 4060 | "This truncation doesn't make sense!"); |
| 4061 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 4062 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 4063 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 4064 | case Promote: |
| 4065 | // The truncation is not required, because we don't guarantee anything |
| 4066 | // about high bits anyway. |
| 4067 | Result = PromoteOp(Node->getOperand(0)); |
| 4068 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4069 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 4070 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 4071 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 4072 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4073 | } |
| 4074 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4075 | case ISD::SIGN_EXTEND: |
| 4076 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4077 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4078 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4079 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 4080 | case Legal: |
| 4081 | // 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] | 4082 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4083 | break; |
| 4084 | case Promote: |
| 4085 | // Promote the reg if it's smaller. |
| 4086 | Result = PromoteOp(Node->getOperand(0)); |
| 4087 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 4088 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 4089 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4090 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 4091 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4092 | Result = DAG.getZeroExtendInReg(Result, |
| 4093 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4094 | break; |
| 4095 | } |
| 4096 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4097 | case ISD::BIT_CONVERT: |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4098 | Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 4099 | Node->getValueType(0)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4100 | Result = PromoteOp(Result); |
| 4101 | break; |
| 4102 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4103 | case ISD::FP_EXTEND: |
| 4104 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 4105 | case ISD::FP_ROUND: |
| 4106 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4107 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 4108 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 4109 | case Legal: |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4110 | if (Node->getConstantOperandVal(1) == 0) { |
| 4111 | // Input is legal? Do an FP_ROUND_INREG. |
| 4112 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
| 4113 | DAG.getValueType(VT)); |
| 4114 | } else { |
| 4115 | // Just remove the truncate, it isn't affecting the value. |
| 4116 | Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0), |
| 4117 | Node->getOperand(1)); |
| 4118 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4119 | break; |
| 4120 | } |
| 4121 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4122 | case ISD::SINT_TO_FP: |
| 4123 | case ISD::UINT_TO_FP: |
| 4124 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4125 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4126 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4127 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4128 | break; |
| 4129 | |
| 4130 | case Promote: |
| 4131 | Result = PromoteOp(Node->getOperand(0)); |
| 4132 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 4133 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4134 | Result, |
| 4135 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4136 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4137 | Result = DAG.getZeroExtendInReg(Result, |
| 4138 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4139 | // No extra round required here. |
| 4140 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4141 | break; |
| 4142 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4143 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 4144 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4145 | // Round if we cannot tolerate excess precision. |
| 4146 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4147 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4148 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 4149 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4150 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4151 | break; |
| 4152 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 4153 | case ISD::SIGN_EXTEND_INREG: |
| 4154 | Result = PromoteOp(Node->getOperand(0)); |
| 4155 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 4156 | Node->getOperand(1)); |
| 4157 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4158 | case ISD::FP_TO_SINT: |
| 4159 | case ISD::FP_TO_UINT: |
| 4160 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 4161 | case Legal: |
Evan Cheng | 0b1b9dc | 2006-12-16 02:10:30 +0000 | [diff] [blame] | 4162 | case Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4163 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4164 | break; |
| 4165 | case Promote: |
| 4166 | // The input result is prerounded, so we don't have to do anything |
| 4167 | // special. |
| 4168 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4169 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4170 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 4171 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 4172 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 4173 | // we can use that instead. This allows us to generate better code for |
| 4174 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 4175 | // legal, such as PowerPC. |
| 4176 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 4177 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 4178 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 4179 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 4180 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 4181 | } else { |
| 4182 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 4183 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4184 | break; |
| 4185 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 4186 | case ISD::FABS: |
| 4187 | case ISD::FNEG: |
| 4188 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4189 | assert(Tmp1.getValueType() == NVT); |
| 4190 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 4191 | // NOTE: we do not have to do any extra rounding here for |
| 4192 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 4193 | // precision, and these operations don't modify precision at all. |
| 4194 | break; |
| 4195 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 4196 | case ISD::FSQRT: |
| 4197 | case ISD::FSIN: |
| 4198 | case ISD::FCOS: |
| 4199 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4200 | assert(Tmp1.getValueType() == NVT); |
| 4201 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4202 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4203 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4204 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 4205 | break; |
| 4206 | |
Chris Lattner | 8b2d42c | 2007-03-03 23:43:21 +0000 | [diff] [blame] | 4207 | case ISD::FPOWI: { |
| 4208 | // Promote f32 powi to f64 powi. Note that this could insert a libcall |
| 4209 | // directly as well, which may be better. |
| 4210 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4211 | assert(Tmp1.getValueType() == NVT); |
| 4212 | Result = DAG.getNode(ISD::FPOWI, NVT, Tmp1, Node->getOperand(1)); |
| 4213 | if (NoExcessFPPrecision) |
| 4214 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4215 | DAG.getValueType(VT)); |
| 4216 | break; |
| 4217 | } |
| 4218 | |
Andrew Lenharth | ab0b949 | 2008-02-21 06:45:13 +0000 | [diff] [blame] | 4219 | case ISD::ATOMIC_LCS: { |
| 4220 | Tmp2 = PromoteOp(Node->getOperand(2)); |
| 4221 | Tmp3 = PromoteOp(Node->getOperand(3)); |
| 4222 | Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0), |
| 4223 | Node->getOperand(1), Tmp2, Tmp3, |
| 4224 | cast<AtomicSDNode>(Node)->getVT()); |
| 4225 | // Remember that we legalized the chain. |
| 4226 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 4227 | break; |
| 4228 | } |
| 4229 | case ISD::ATOMIC_LAS: |
| 4230 | case ISD::ATOMIC_SWAP: { |
| 4231 | Tmp2 = PromoteOp(Node->getOperand(2)); |
| 4232 | Result = DAG.getAtomic(Node->getOpcode(), Node->getOperand(0), |
| 4233 | Node->getOperand(1), Tmp2, |
| 4234 | cast<AtomicSDNode>(Node)->getVT()); |
| 4235 | // Remember that we legalized the chain. |
| 4236 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 4237 | break; |
| 4238 | } |
| 4239 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4240 | case ISD::AND: |
| 4241 | case ISD::OR: |
| 4242 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4243 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4244 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4245 | case ISD::MUL: |
| 4246 | // 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] | 4247 | // 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] | 4248 | // that too is okay if they are integer operations. |
| 4249 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4250 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4251 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 4252 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4253 | break; |
| 4254 | case ISD::FADD: |
| 4255 | case ISD::FSUB: |
| 4256 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4257 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4258 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4259 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 4260 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4261 | |
| 4262 | // Floating point operations will give excess precision that we may not be |
| 4263 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 4264 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4265 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 4266 | // 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] | 4267 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4268 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4269 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 4270 | break; |
| 4271 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4272 | case ISD::SDIV: |
| 4273 | case ISD::SREM: |
| 4274 | // These operators require that their input be sign extended. |
| 4275 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4276 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4277 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4278 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4279 | DAG.getValueType(VT)); |
| 4280 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 4281 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4282 | } |
| 4283 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4284 | |
| 4285 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 4286 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4287 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4288 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4289 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4290 | case ISD::FDIV: |
| 4291 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 4292 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4293 | // These operators require that their input be fp extended. |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4294 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4295 | case Expand: assert(0 && "not implemented"); |
| 4296 | case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break; |
| 4297 | case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break; |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4298 | } |
| 4299 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4300 | case Expand: assert(0 && "not implemented"); |
| 4301 | case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break; |
| 4302 | case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break; |
Nate Begeman | ec57fd9 | 2006-05-09 18:20:51 +0000 | [diff] [blame] | 4303 | } |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4304 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4305 | |
| 4306 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 4307 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 4308 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 4309 | DAG.getValueType(VT)); |
| 4310 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4311 | |
| 4312 | case ISD::UDIV: |
| 4313 | case ISD::UREM: |
| 4314 | // These operators require that their input be zero extended. |
| 4315 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 4316 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 4317 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4318 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 4319 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4320 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 4321 | break; |
| 4322 | |
| 4323 | case ISD::SHL: |
| 4324 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4325 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4326 | break; |
| 4327 | case ISD::SRA: |
| 4328 | // The input value must be properly sign extended. |
| 4329 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 4330 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4331 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4332 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | case ISD::SRL: |
| 4335 | // The input value must be properly zero extended. |
| 4336 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 4337 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4338 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 4339 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4340 | |
| 4341 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4342 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 4343 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4344 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 4345 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 4346 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 4347 | } else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4348 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
| 4349 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4350 | // Increment the pointer, VAList, to the next vaarg |
| 4351 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 4352 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 4353 | TLI.getPointerTy())); |
| 4354 | // Store the incremented VAList to the legalized pointer |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4355 | Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4356 | // Load the actual argument out of the pointer VAList |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4357 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4358 | } |
| 4359 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4360 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 4361 | break; |
| 4362 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4363 | case ISD::LOAD: { |
| 4364 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
Evan Cheng | 62f2a3c | 2006-10-10 07:51:21 +0000 | [diff] [blame] | 4365 | ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node) |
| 4366 | ? ISD::EXTLOAD : LD->getExtensionType(); |
| 4367 | Result = DAG.getExtLoad(ExtType, NVT, |
| 4368 | LD->getChain(), LD->getBasePtr(), |
Chris Lattner | 55b5708 | 2006-10-10 18:54:19 +0000 | [diff] [blame] | 4369 | LD->getSrcValue(), LD->getSrcValueOffset(), |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 4370 | LD->getMemoryVT(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 4371 | LD->isVolatile(), |
| 4372 | LD->getAlignment()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4373 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4374 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4375 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4376 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4377 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4378 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 4379 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4380 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4381 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4382 | case ISD::SELECT_CC: |
| 4383 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 4384 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 4385 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4386 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 4387 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4388 | case ISD::BSWAP: |
| 4389 | Tmp1 = Node->getOperand(0); |
| 4390 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 4391 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 4392 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4393 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 4394 | MVT::getSizeInBits(VT), |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 4395 | TLI.getShiftAmountTy())); |
| 4396 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4397 | case ISD::CTPOP: |
| 4398 | case ISD::CTTZ: |
| 4399 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4400 | // Zero extend the argument |
| 4401 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4402 | // Perform the larger operation, then subtract if needed. |
| 4403 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4404 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4405 | case ISD::CTPOP: |
| 4406 | Result = Tmp1; |
| 4407 | break; |
| 4408 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4409 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4410 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4411 | DAG.getConstant(MVT::getSizeInBits(NVT), NVT), |
| 4412 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4413 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4414 | DAG.getConstant(MVT::getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4415 | break; |
| 4416 | case ISD::CTLZ: |
| 4417 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 4418 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 4419 | DAG.getConstant(MVT::getSizeInBits(NVT) - |
| 4420 | MVT::getSizeInBits(VT), NVT)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 4421 | break; |
| 4422 | } |
| 4423 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4424 | case ISD::EXTRACT_SUBVECTOR: |
| 4425 | Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op)); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4426 | break; |
Chris Lattner | 4aab2f4 | 2006-04-02 05:06:04 +0000 | [diff] [blame] | 4427 | case ISD::EXTRACT_VECTOR_ELT: |
| 4428 | Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op)); |
| 4429 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4433 | |
| 4434 | // Make sure the result is itself legal. |
| 4435 | Result = LegalizeOp(Result); |
| 4436 | |
| 4437 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 4438 | AddPromotedOperand(Op, Result); |
| 4439 | return Result; |
| 4440 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4441 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4442 | /// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into |
| 4443 | /// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic, |
| 4444 | /// based on the vector type. The return type of this matches the element type |
| 4445 | /// of the vector, which may not be legal for the target. |
| 4446 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDOperand Op) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4447 | // We know that operand #0 is the Vec vector. If the index is a constant |
| 4448 | // or if the invec is a supported hardware type, we can use it. Otherwise, |
| 4449 | // lower to a store then an indexed load. |
| 4450 | SDOperand Vec = Op.getOperand(0); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4451 | SDOperand Idx = Op.getOperand(1); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4452 | |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 4453 | MVT::ValueType TVT = Vec.getValueType(); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4454 | unsigned NumElems = MVT::getVectorNumElements(TVT); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4455 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4456 | switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) { |
| 4457 | default: assert(0 && "This action is not supported yet!"); |
| 4458 | case TargetLowering::Custom: { |
| 4459 | Vec = LegalizeOp(Vec); |
| 4460 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
| 4461 | SDOperand Tmp3 = TLI.LowerOperation(Op, DAG); |
| 4462 | if (Tmp3.Val) |
| 4463 | return Tmp3; |
| 4464 | break; |
| 4465 | } |
| 4466 | case TargetLowering::Legal: |
| 4467 | if (isTypeLegal(TVT)) { |
| 4468 | Vec = LegalizeOp(Vec); |
| 4469 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Christopher Lamb | 844228a | 2007-07-26 03:33:13 +0000 | [diff] [blame] | 4470 | return Op; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4471 | } |
| 4472 | break; |
| 4473 | case TargetLowering::Expand: |
| 4474 | break; |
| 4475 | } |
| 4476 | |
| 4477 | if (NumElems == 1) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4478 | // This must be an access of the only element. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4479 | Op = ScalarizeVectorOp(Vec); |
| 4480 | } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) { |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4481 | unsigned NumLoElts = 1 << Log2_32(NumElems-1); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4482 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4483 | SDOperand Lo, Hi; |
| 4484 | SplitVectorOp(Vec, Lo, Hi); |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4485 | if (CIdx->getValue() < NumLoElts) { |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4486 | Vec = Lo; |
| 4487 | } else { |
| 4488 | Vec = Hi; |
Nate Begeman | 55030dc | 2008-01-29 02:24:00 +0000 | [diff] [blame] | 4489 | Idx = DAG.getConstant(CIdx->getValue() - NumLoElts, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4490 | Idx.getValueType()); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4491 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4492 | |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4493 | // It's now an extract from the appropriate high or low part. Recurse. |
| 4494 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4495 | Op = ExpandEXTRACT_VECTOR_ELT(Op); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4496 | } else { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4497 | // Store the value to a temporary stack slot, then LOAD the scalar |
| 4498 | // element back out. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4499 | SDOperand StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4500 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0); |
| 4501 | |
| 4502 | // Add the offset to the index. |
| 4503 | unsigned EltSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 4504 | Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx, |
| 4505 | DAG.getConstant(EltSize, Idx.getValueType())); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4506 | |
| 4507 | if (MVT::getSizeInBits(Idx.getValueType()) > |
| 4508 | MVT::getSizeInBits(TLI.getPointerTy())) |
Chris Lattner | f185e67 | 2007-10-19 16:47:35 +0000 | [diff] [blame] | 4509 | Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4510 | else |
Chris Lattner | f185e67 | 2007-10-19 16:47:35 +0000 | [diff] [blame] | 4511 | Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx); |
Bill Wendling | 90bfc2d | 2007-10-18 08:32:37 +0000 | [diff] [blame] | 4512 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4513 | StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr); |
| 4514 | |
| 4515 | Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0); |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4516 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4517 | return Op; |
Chris Lattner | 1597221 | 2006-03-31 17:55:51 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4520 | /// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4521 | /// 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] | 4522 | SDOperand SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDOperand Op) { |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4523 | // We know that operand #0 is the Vec vector. For now we assume the index |
| 4524 | // is a constant and that the extracted result is a supported hardware type. |
| 4525 | SDOperand Vec = Op.getOperand(0); |
| 4526 | SDOperand Idx = LegalizeOp(Op.getOperand(1)); |
| 4527 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4528 | unsigned NumElems = MVT::getVectorNumElements(Vec.getValueType()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4529 | |
| 4530 | if (NumElems == MVT::getVectorNumElements(Op.getValueType())) { |
| 4531 | // This must be an access of the desired vector length. Return it. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4532 | return Vec; |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx); |
| 4536 | SDOperand Lo, Hi; |
| 4537 | SplitVectorOp(Vec, Lo, Hi); |
| 4538 | if (CIdx->getValue() < NumElems/2) { |
| 4539 | Vec = Lo; |
| 4540 | } else { |
| 4541 | Vec = Hi; |
| 4542 | Idx = DAG.getConstant(CIdx->getValue() - NumElems/2, Idx.getValueType()); |
| 4543 | } |
| 4544 | |
| 4545 | // It's now an extract from the appropriate high or low part. Recurse. |
| 4546 | Op = DAG.UpdateNodeOperands(Op, Vec, Idx); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 4547 | return ExpandEXTRACT_SUBVECTOR(Op); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4550 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 4551 | /// with condition CC on the current target. This usually involves legalizing |
| 4552 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 4553 | /// there may be no choice but to create a new SetCC node to represent the |
| 4554 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 4555 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 4556 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 4557 | SDOperand &RHS, |
| 4558 | SDOperand &CC) { |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4559 | SDOperand Tmp1, Tmp2, Tmp3, Result; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4560 | |
| 4561 | switch (getTypeAction(LHS.getValueType())) { |
| 4562 | case Legal: |
| 4563 | Tmp1 = LegalizeOp(LHS); // LHS |
| 4564 | Tmp2 = LegalizeOp(RHS); // RHS |
| 4565 | break; |
| 4566 | case Promote: |
| 4567 | Tmp1 = PromoteOp(LHS); // LHS |
| 4568 | Tmp2 = PromoteOp(RHS); // RHS |
| 4569 | |
| 4570 | // If this is an FP compare, the operands have already been extended. |
| 4571 | if (MVT::isInteger(LHS.getValueType())) { |
| 4572 | MVT::ValueType VT = LHS.getValueType(); |
| 4573 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 4574 | |
| 4575 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 4576 | // that we could insert sign extends for ALL conditions, but zero extend |
| 4577 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 4578 | // it. |
| 4579 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4580 | default: assert(0 && "Unknown integer comparison!"); |
| 4581 | case ISD::SETEQ: |
| 4582 | case ISD::SETNE: |
| 4583 | case ISD::SETUGE: |
| 4584 | case ISD::SETUGT: |
| 4585 | case ISD::SETULE: |
| 4586 | case ISD::SETULT: |
| 4587 | // ALL of these operations will work if we either sign or zero extend |
| 4588 | // the operands (including the unsigned comparisons!). Zero extend is |
| 4589 | // usually a simpler/cheaper operation, so prefer it. |
| 4590 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 4591 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 4592 | break; |
| 4593 | case ISD::SETGE: |
| 4594 | case ISD::SETGT: |
| 4595 | case ISD::SETLT: |
| 4596 | case ISD::SETLE: |
| 4597 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 4598 | DAG.getValueType(VT)); |
| 4599 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 4600 | DAG.getValueType(VT)); |
| 4601 | break; |
| 4602 | } |
| 4603 | } |
| 4604 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4605 | case Expand: { |
| 4606 | MVT::ValueType VT = LHS.getValueType(); |
| 4607 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 4608 | // Expand into one or more soft-fp libcall(s). |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4609 | RTLIB::Libcall LC1, LC2 = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4610 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4611 | case ISD::SETEQ: |
| 4612 | case ISD::SETOEQ: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4613 | LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4614 | break; |
| 4615 | case ISD::SETNE: |
| 4616 | case ISD::SETUNE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4617 | LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4618 | break; |
| 4619 | case ISD::SETGE: |
| 4620 | case ISD::SETOGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4621 | LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4622 | break; |
| 4623 | case ISD::SETLT: |
| 4624 | case ISD::SETOLT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4625 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4626 | break; |
| 4627 | case ISD::SETLE: |
| 4628 | case ISD::SETOLE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4629 | LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4630 | break; |
| 4631 | case ISD::SETGT: |
| 4632 | case ISD::SETOGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4633 | LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4634 | break; |
| 4635 | case ISD::SETUO: |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4636 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
| 4637 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4638 | case ISD::SETO: |
Evan Cheng | 5efdecc | 2007-02-03 00:43:46 +0000 | [diff] [blame] | 4639 | LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4640 | break; |
| 4641 | default: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4642 | LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4643 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 4644 | case ISD::SETONE: |
| 4645 | // SETONE = SETOLT | SETOGT |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4646 | LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4647 | // Fallthrough |
| 4648 | case ISD::SETUGT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4649 | LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4650 | break; |
| 4651 | case ISD::SETUGE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4652 | LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4653 | break; |
| 4654 | case ISD::SETULT: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4655 | LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4656 | break; |
| 4657 | case ISD::SETULE: |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4658 | LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4659 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4660 | case ISD::SETUEQ: |
| 4661 | LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4662 | break; |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4663 | default: assert(0 && "Unsupported FP setcc!"); |
| 4664 | } |
| 4665 | } |
| 4666 | |
| 4667 | SDOperand Dummy; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 4668 | Tmp1 = ExpandLibCall(LC1, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 4669 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4670 | false /*sign irrelevant*/, Dummy); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4671 | Tmp2 = DAG.getConstant(0, MVT::i32); |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4672 | CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1)); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 4673 | if (LC2 != RTLIB::UNKNOWN_LIBCALL) { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4674 | Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4675 | CC); |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 4676 | LHS = ExpandLibCall(LC2, |
| 4677 | DAG.getNode(ISD::MERGE_VALUES, VT, LHS, RHS).Val, |
Reid Spencer | b47b25c | 2007-01-03 04:22:32 +0000 | [diff] [blame] | 4678 | false /*sign irrelevant*/, Dummy); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4679 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2, |
Evan Cheng | d385fd6 | 2007-01-31 09:29:11 +0000 | [diff] [blame] | 4680 | DAG.getCondCode(TLI.getCmpLibcallCC(LC2))); |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4681 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4682 | Tmp2 = SDOperand(); |
| 4683 | } |
| 4684 | LHS = Tmp1; |
| 4685 | RHS = Tmp2; |
| 4686 | return; |
| 4687 | } |
| 4688 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4689 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 4690 | ExpandOp(LHS, LHSLo, LHSHi); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4691 | ExpandOp(RHS, RHSLo, RHSHi); |
| 4692 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 4693 | |
| 4694 | if (VT==MVT::ppcf128) { |
| 4695 | // FIXME: This generated code sucks. We want to generate |
| 4696 | // FCMP crN, hi1, hi2 |
| 4697 | // BNE crN, L: |
| 4698 | // FCMP crN, lo1, lo2 |
| 4699 | // The following can be improved, but not that much. |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4700 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ); |
| 4701 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4702 | Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4703 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE); |
| 4704 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 4705 | Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4706 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3); |
| 4707 | Tmp2 = SDOperand(); |
| 4708 | break; |
| 4709 | } |
| 4710 | |
| 4711 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4712 | case ISD::SETEQ: |
| 4713 | case ISD::SETNE: |
| 4714 | if (RHSLo == RHSHi) |
| 4715 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 4716 | if (RHSCST->isAllOnesValue()) { |
| 4717 | // Comparison to -1. |
| 4718 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 4719 | Tmp2 = RHSLo; |
| 4720 | break; |
| 4721 | } |
| 4722 | |
| 4723 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 4724 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 4725 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 4726 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 4727 | break; |
| 4728 | default: |
| 4729 | // If this is a comparison of the sign bit, just look at the top part. |
| 4730 | // X > -1, x < 0 |
| 4731 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 4732 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4733 | CST->isNullValue()) || // X < 0 |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4734 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 4735 | CST->isAllOnesValue())) { // X > -1 |
| 4736 | Tmp1 = LHSHi; |
| 4737 | Tmp2 = RHSHi; |
| 4738 | break; |
| 4739 | } |
| 4740 | |
| 4741 | // FIXME: This generated code sucks. |
| 4742 | ISD::CondCode LowCC; |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4743 | switch (CCCode) { |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4744 | default: assert(0 && "Unknown integer setcc!"); |
| 4745 | case ISD::SETLT: |
| 4746 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 4747 | case ISD::SETGT: |
| 4748 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 4749 | case ISD::SETLE: |
| 4750 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 4751 | case ISD::SETGE: |
| 4752 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 4753 | } |
| 4754 | |
| 4755 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 4756 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 4757 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 4758 | |
| 4759 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 4760 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4761 | TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4762 | Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4763 | LowCC, false, DagCombineInfo); |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4764 | if (!Tmp1.Val) |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4765 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC); |
| 4766 | Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4767 | CCCode, false, DagCombineInfo); |
| 4768 | if (!Tmp2.Val) |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4769 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4770 | RHSHi,CC); |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4771 | |
| 4772 | ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val); |
| 4773 | ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val); |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4774 | if ((Tmp1C && Tmp1C->isNullValue()) || |
| 4775 | (Tmp2C && Tmp2C->isNullValue() && |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4776 | (CCCode == ISD::SETLE || CCCode == ISD::SETGE || |
| 4777 | CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) || |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 4778 | (Tmp2C && Tmp2C->getAPIntValue() == 1 && |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4779 | (CCCode == ISD::SETLT || CCCode == ISD::SETGT || |
| 4780 | CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) { |
| 4781 | // low part is known false, returns high part. |
| 4782 | // For LE / GE, if high part is known false, ignore the low part. |
| 4783 | // For LT / GT, if high part is known true, ignore the low part. |
| 4784 | Tmp1 = Tmp2; |
| 4785 | Tmp2 = SDOperand(); |
| 4786 | } else { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4787 | Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4788 | ISD::SETEQ, false, DagCombineInfo); |
| 4789 | if (!Result.Val) |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 4790 | Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 4791 | ISD::SETEQ); |
Evan Cheng | 2e67781 | 2007-02-08 22:16:19 +0000 | [diff] [blame] | 4792 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 4793 | Result, Tmp1, Tmp2)); |
| 4794 | Tmp1 = Result; |
| 4795 | Tmp2 = SDOperand(); |
| 4796 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4797 | } |
| 4798 | } |
Evan Cheng | 2b49c50 | 2006-12-15 02:59:56 +0000 | [diff] [blame] | 4799 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 4800 | LHS = Tmp1; |
| 4801 | RHS = Tmp2; |
| 4802 | } |
| 4803 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4804 | /// EmitStackConvert - Emit a store/load combination to the stack. This stores |
| 4805 | /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does |
| 4806 | /// a load from the stack slot to DestVT, extending it if needed. |
| 4807 | /// The resultant code need not be legal. |
| 4808 | SDOperand SelectionDAGLegalize::EmitStackConvert(SDOperand SrcOp, |
| 4809 | MVT::ValueType SlotVT, |
| 4810 | MVT::ValueType DestVT) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4811 | // Create the stack frame object. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4812 | SDOperand FIPtr = DAG.CreateStackTemporary(SlotVT); |
| 4813 | |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 4814 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4815 | int SPFI = StackPtrFI->getIndex(); |
| 4816 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4817 | unsigned SrcSize = MVT::getSizeInBits(SrcOp.getValueType()); |
| 4818 | unsigned SlotSize = MVT::getSizeInBits(SlotVT); |
| 4819 | unsigned DestSize = MVT::getSizeInBits(DestVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4820 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4821 | // Emit a store to the stack slot. Use a truncstore if the input value is |
| 4822 | // later than DestVT. |
| 4823 | SDOperand Store; |
| 4824 | if (SrcSize > SlotSize) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4825 | Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4826 | PseudoSourceValue::getFixedStack(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4827 | SPFI, SlotVT); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4828 | else { |
| 4829 | assert(SrcSize == SlotSize && "Invalid store"); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4830 | Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4831 | PseudoSourceValue::getFixedStack(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4832 | SPFI, SlotVT); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4835 | // Result is a load from the stack slot. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 4836 | if (SlotSize == DestSize) |
| 4837 | return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0); |
| 4838 | |
| 4839 | assert(SlotSize < DestSize && "Unknown extension!"); |
| 4840 | return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4843 | SDOperand SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
| 4844 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 4845 | // then load the whole vector back out. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 4846 | SDOperand StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4847 | |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 4848 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4849 | int SPFI = StackPtrFI->getIndex(); |
| 4850 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 4851 | SDOperand Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4852 | PseudoSourceValue::getFixedStack(), SPFI); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4853 | return DAG.getLoad(Node->getValueType(0), Ch, StackPtr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4854 | PseudoSourceValue::getFixedStack(), SPFI); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4858 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 4859 | /// support the operation, but do support the resultant vector type. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4860 | SDOperand SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
| 4861 | |
| 4862 | // 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] | 4863 | // 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] | 4864 | unsigned NumElems = Node->getNumOperands(); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4865 | bool isOnlyLowElement = true; |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4866 | SDOperand SplatValue = Node->getOperand(0); |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4867 | |
| 4868 | // FIXME: it would be far nicer to change this into map<SDOperand,uint64_t> |
| 4869 | // and use a bitmask instead of a list of elements. |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4870 | std::map<SDOperand, std::vector<unsigned> > Values; |
| 4871 | Values[SplatValue].push_back(0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4872 | bool isConstant = true; |
| 4873 | if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) && |
| 4874 | SplatValue.getOpcode() != ISD::UNDEF) |
| 4875 | isConstant = false; |
| 4876 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4877 | for (unsigned i = 1; i < NumElems; ++i) { |
| 4878 | SDOperand V = Node->getOperand(i); |
Chris Lattner | 89a1b38 | 2006-04-19 23:17:50 +0000 | [diff] [blame] | 4879 | Values[V].push_back(i); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4880 | if (V.getOpcode() != ISD::UNDEF) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4881 | isOnlyLowElement = false; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4882 | if (SplatValue != V) |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4883 | SplatValue = SDOperand(0,0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4884 | |
| 4885 | // If this isn't a constant element or an undef, we can't use a constant |
| 4886 | // pool load. |
| 4887 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) && |
| 4888 | V.getOpcode() != ISD::UNDEF) |
| 4889 | isConstant = false; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | if (isOnlyLowElement) { |
| 4893 | // If the low element is an undef too, then this whole things is an undef. |
| 4894 | if (Node->getOperand(0).getOpcode() == ISD::UNDEF) |
| 4895 | return DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 4896 | // Otherwise, turn this into a scalar_to_vector node. |
| 4897 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), |
| 4898 | Node->getOperand(0)); |
| 4899 | } |
| 4900 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4901 | // If all elements are constants, create a load from the constant pool. |
| 4902 | if (isConstant) { |
| 4903 | MVT::ValueType VT = Node->getValueType(0); |
| 4904 | const Type *OpNTy = |
| 4905 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 4906 | std::vector<Constant*> CV; |
| 4907 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
| 4908 | if (ConstantFPSDNode *V = |
| 4909 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 4910 | CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4911 | } else if (ConstantSDNode *V = |
| 4912 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 4913 | CV.push_back(ConstantInt::get(OpNTy, V->getValue())); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4914 | } else { |
| 4915 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
| 4916 | CV.push_back(UndefValue::get(OpNTy)); |
| 4917 | } |
| 4918 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 4919 | Constant *CP = ConstantVector::get(CV); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4920 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4921 | return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 4922 | PseudoSourceValue::getConstantPool(), 0); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4925 | if (SplatValue.Val) { // Splat of one value? |
| 4926 | // Build the shuffle constant vector: <0, 0, 0, 0> |
| 4927 | MVT::ValueType MaskVT = |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4928 | MVT::getIntVectorWithNumElements(NumElems); |
Dan Gohman | 51eaa86 | 2007-06-14 22:58:02 +0000 | [diff] [blame] | 4929 | SDOperand Zero = DAG.getConstant(0, MVT::getVectorElementType(MaskVT)); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4930 | std::vector<SDOperand> ZeroVec(NumElems, Zero); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4931 | SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4932 | &ZeroVec[0], ZeroVec.size()); |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4933 | |
| 4934 | // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4935 | if (isShuffleLegal(Node->getValueType(0), SplatMask)) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 4936 | // Get the splatted value into the low element of a vector register. |
| 4937 | SDOperand LowValVec = |
| 4938 | DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue); |
| 4939 | |
| 4940 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
| 4941 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec, |
| 4942 | DAG.getNode(ISD::UNDEF, Node->getValueType(0)), |
| 4943 | SplatMask); |
| 4944 | } |
| 4945 | } |
| 4946 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4947 | // If there are only two unique elements, we may be able to turn this into a |
| 4948 | // vector shuffle. |
| 4949 | if (Values.size() == 2) { |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4950 | // Get the two values in deterministic order. |
| 4951 | SDOperand Val1 = Node->getOperand(1); |
| 4952 | SDOperand Val2; |
| 4953 | std::map<SDOperand, std::vector<unsigned> >::iterator MI = Values.begin(); |
| 4954 | if (MI->first != Val1) |
| 4955 | Val2 = MI->first; |
| 4956 | else |
| 4957 | Val2 = (++MI)->first; |
| 4958 | |
| 4959 | // If Val1 is an undef, make sure end ends up as Val2, to ensure that our |
| 4960 | // vector shuffle has the undef vector on the RHS. |
| 4961 | if (Val1.getOpcode() == ISD::UNDEF) |
| 4962 | std::swap(Val1, Val2); |
| 4963 | |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4964 | // Build the shuffle constant vector: e.g. <0, 4, 0, 4> |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4965 | MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems); |
| 4966 | MVT::ValueType MaskEltVT = MVT::getVectorElementType(MaskVT); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4967 | std::vector<SDOperand> MaskVec(NumElems); |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4968 | |
| 4969 | // Set elements of the shuffle mask for Val1. |
| 4970 | std::vector<unsigned> &Val1Elts = Values[Val1]; |
| 4971 | for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i) |
| 4972 | MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT); |
| 4973 | |
| 4974 | // Set elements of the shuffle mask for Val2. |
| 4975 | std::vector<unsigned> &Val2Elts = Values[Val2]; |
| 4976 | for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i) |
| 4977 | if (Val2.getOpcode() != ISD::UNDEF) |
| 4978 | MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT); |
| 4979 | else |
| 4980 | MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT); |
| 4981 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 4982 | SDOperand ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, |
| 4983 | &MaskVec[0], MaskVec.size()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4984 | |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4985 | // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it. |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 4986 | if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) && |
| 4987 | isShuffleLegal(Node->getValueType(0), ShuffleMask)) { |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4988 | Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1); |
| 4989 | Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2); |
| 4990 | SDOperand Ops[] = { Val1, Val2, ShuffleMask }; |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4991 | |
| 4992 | // Return shuffle(LoValVec, HiValVec, <0,1,0,1>) |
Chris Lattner | f9d95c8 | 2008-03-09 00:29:42 +0000 | [diff] [blame] | 4993 | return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 4994 | } |
| 4995 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 4996 | |
| 4997 | // Otherwise, we can't handle this case efficiently. Allocate a sufficiently |
| 4998 | // aligned object on the stack, store each element into it, then load |
| 4999 | // the result as a vector. |
| 5000 | MVT::ValueType VT = Node->getValueType(0); |
| 5001 | // Create the stack frame object. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 5002 | SDOperand FIPtr = DAG.CreateStackTemporary(VT); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5003 | |
| 5004 | // Emit a store of each element to the stack slot. |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5005 | SmallVector<SDOperand, 8> Stores; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5006 | unsigned TypeByteSize = |
| 5007 | MVT::getSizeInBits(Node->getOperand(0).getValueType())/8; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5008 | // Store (in the right endianness) the elements to memory. |
| 5009 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 5010 | // Ignore undef elements. |
| 5011 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 5012 | |
Chris Lattner | 841c882 | 2006-03-22 01:46:54 +0000 | [diff] [blame] | 5013 | unsigned Offset = TypeByteSize*i; |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5014 | |
| 5015 | SDOperand Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 5016 | Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx); |
| 5017 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5018 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5019 | NULL, 0)); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | SDOperand StoreChain; |
| 5023 | if (!Stores.empty()) // Not all undef elements? |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5024 | StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other, |
| 5025 | &Stores[0], Stores.size()); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5026 | else |
| 5027 | StoreChain = DAG.getEntryNode(); |
| 5028 | |
| 5029 | // Result is a load from the stack slot. |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5030 | return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 5033 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 5034 | SDOperand Op, SDOperand Amt, |
| 5035 | SDOperand &Lo, SDOperand &Hi) { |
| 5036 | // Expand the subcomponents. |
| 5037 | SDOperand LHSL, LHSH; |
| 5038 | ExpandOp(Op, LHSL, LHSH); |
| 5039 | |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 5040 | SDOperand Ops[] = { LHSL, LHSH, Amt }; |
Chris Lattner | f9f37fc | 2006-08-14 23:53:35 +0000 | [diff] [blame] | 5041 | MVT::ValueType VT = LHSL.getValueType(); |
| 5042 | Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 5043 | Hi = Lo.getValue(1); |
| 5044 | } |
| 5045 | |
| 5046 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5047 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 5048 | /// smaller elements. If we can't find a way that is more efficient than a |
| 5049 | /// libcall on this target, return false. Otherwise, return true with the |
| 5050 | /// low-parts expanded into Lo and Hi. |
| 5051 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 5052 | SDOperand &Lo, SDOperand &Hi) { |
| 5053 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 5054 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5055 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5056 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5057 | SDOperand ShAmt = LegalizeOp(Amt); |
| 5058 | MVT::ValueType ShTy = ShAmt.getValueType(); |
Dan Gohman | 91dc17b | 2008-02-20 16:57:27 +0000 | [diff] [blame] | 5059 | unsigned ShBits = MVT::getSizeInBits(ShTy); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5060 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 5061 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 5062 | |
Chris Lattner | 7a3c855 | 2007-10-14 20:35:12 +0000 | [diff] [blame] | 5063 | // Handle the case when Amt is an immediate. |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5064 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 5065 | unsigned Cst = CN->getValue(); |
| 5066 | // Expand the incoming operand to be shifted, so that we have its parts |
| 5067 | SDOperand InL, InH; |
| 5068 | ExpandOp(Op, InL, InH); |
| 5069 | switch(Opc) { |
| 5070 | case ISD::SHL: |
| 5071 | if (Cst > VTBits) { |
| 5072 | Lo = DAG.getConstant(0, NVT); |
| 5073 | Hi = DAG.getConstant(0, NVT); |
| 5074 | } else if (Cst > NVTBits) { |
| 5075 | Lo = DAG.getConstant(0, NVT); |
| 5076 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5077 | } else if (Cst == NVTBits) { |
| 5078 | Lo = DAG.getConstant(0, NVT); |
| 5079 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5080 | } else { |
| 5081 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 5082 | Hi = DAG.getNode(ISD::OR, NVT, |
| 5083 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 5084 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5085 | } |
| 5086 | return true; |
| 5087 | case ISD::SRL: |
| 5088 | if (Cst > VTBits) { |
| 5089 | Lo = DAG.getConstant(0, NVT); |
| 5090 | Hi = DAG.getConstant(0, NVT); |
| 5091 | } else if (Cst > NVTBits) { |
| 5092 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 5093 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5094 | } else if (Cst == NVTBits) { |
| 5095 | Lo = InH; |
| 5096 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5097 | } else { |
| 5098 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5099 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 5100 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5101 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 5102 | } |
| 5103 | return true; |
| 5104 | case ISD::SRA: |
| 5105 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5106 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5107 | DAG.getConstant(NVTBits-1, ShTy)); |
| 5108 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5109 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5110 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5111 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5112 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5113 | } else if (Cst == NVTBits) { |
| 5114 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5115 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 5116 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5117 | } else { |
| 5118 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5119 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 5120 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 5121 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 5122 | } |
| 5123 | return true; |
| 5124 | } |
| 5125 | } |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5126 | |
| 5127 | // Okay, the shift amount isn't constant. However, if we can tell that it is |
| 5128 | // >= 32 or < 32, we can still simplify it, without knowing the actual value. |
Dan Gohman | 91dc17b | 2008-02-20 16:57:27 +0000 | [diff] [blame] | 5129 | APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits)); |
| 5130 | APInt KnownZero, KnownOne; |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5131 | DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne); |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5132 | |
Dan Gohman | 9e255b7 | 2008-02-22 01:12:31 +0000 | [diff] [blame] | 5133 | // If we know that if any of the high bits of the shift amount are one, then |
| 5134 | // we can do this as a couple of simple shifts. |
Dan Gohman | 91dc17b | 2008-02-20 16:57:27 +0000 | [diff] [blame] | 5135 | if (KnownOne.intersects(Mask)) { |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5136 | // Mask out the high bit, which we know is set. |
| 5137 | Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt, |
Dan Gohman | 91dc17b | 2008-02-20 16:57:27 +0000 | [diff] [blame] | 5138 | DAG.getConstant(~Mask, Amt.getValueType())); |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5139 | |
| 5140 | // Expand the incoming operand to be shifted, so that we have its parts |
| 5141 | SDOperand InL, InH; |
| 5142 | ExpandOp(Op, InL, InH); |
| 5143 | switch(Opc) { |
| 5144 | case ISD::SHL: |
| 5145 | Lo = DAG.getConstant(0, NVT); // Low part is zero. |
| 5146 | Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part. |
| 5147 | return true; |
| 5148 | case ISD::SRL: |
| 5149 | Hi = DAG.getConstant(0, NVT); // Hi part is zero. |
| 5150 | Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part. |
| 5151 | return true; |
| 5152 | case ISD::SRA: |
| 5153 | Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part. |
| 5154 | DAG.getConstant(NVTBits-1, Amt.getValueType())); |
| 5155 | Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part. |
| 5156 | return true; |
| 5157 | } |
| 5158 | } |
| 5159 | |
Dan Gohman | 9e255b7 | 2008-02-22 01:12:31 +0000 | [diff] [blame] | 5160 | // If we know that the high bits of the shift amount are all zero, then we can |
| 5161 | // do this as a couple of simple shifts. |
| 5162 | if ((KnownZero & Mask) == Mask) { |
Chris Lattner | 0ea26ca | 2006-09-20 03:38:48 +0000 | [diff] [blame] | 5163 | // Compute 32-amt. |
| 5164 | SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(), |
| 5165 | DAG.getConstant(NVTBits, Amt.getValueType()), |
| 5166 | Amt); |
| 5167 | |
| 5168 | // Expand the incoming operand to be shifted, so that we have its parts |
| 5169 | SDOperand InL, InH; |
| 5170 | ExpandOp(Op, InL, InH); |
| 5171 | switch(Opc) { |
| 5172 | case ISD::SHL: |
| 5173 | Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt); |
| 5174 | Hi = DAG.getNode(ISD::OR, NVT, |
| 5175 | DAG.getNode(ISD::SHL, NVT, InH, Amt), |
| 5176 | DAG.getNode(ISD::SRL, NVT, InL, Amt2)); |
| 5177 | return true; |
| 5178 | case ISD::SRL: |
| 5179 | Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt); |
| 5180 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5181 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 5182 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 5183 | return true; |
| 5184 | case ISD::SRA: |
| 5185 | Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt); |
| 5186 | Lo = DAG.getNode(ISD::OR, NVT, |
| 5187 | DAG.getNode(ISD::SRL, NVT, InL, Amt), |
| 5188 | DAG.getNode(ISD::SHL, NVT, InH, Amt2)); |
| 5189 | return true; |
| 5190 | } |
| 5191 | } |
| 5192 | |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 5193 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5194 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5195 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5196 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5197 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 5198 | // does not fit into a register, return the lo part and set the hi part to the |
| 5199 | // by-reg argument. If it does fit into a single register, return the result |
| 5200 | // and leave the Hi part unset. |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 5201 | SDOperand SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5202 | bool isSigned, SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5203 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 5204 | // The input chain to this libcall is the entry node of the function. |
| 5205 | // Legalizing the call will automatically add the previous call to the |
| 5206 | // dependence. |
| 5207 | SDOperand InChain = DAG.getEntryNode(); |
| 5208 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5209 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5210 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5211 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 5212 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 5213 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5214 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 5215 | Entry.isSExt = isSigned; |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 5216 | Entry.isZExt = !isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5217 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5218 | } |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 5219 | SDOperand Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
| 5220 | TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5221 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5222 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5223 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5224 | std::pair<SDOperand,SDOperand> CallInfo = |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 5225 | TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, CallingConv::C, |
| 5226 | false, Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5228 | // Legalize the call sequence, starting with the chain. This will advance |
| 5229 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 5230 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 5231 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5232 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5233 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5234 | default: assert(0 && "Unknown thing"); |
| 5235 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5236 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5237 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5238 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5239 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5240 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5241 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 5242 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 5245 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5246 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation. |
| 5247 | /// |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5248 | SDOperand SelectionDAGLegalize:: |
| 5249 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5250 | MVT::ValueType SourceVT = Source.getValueType(); |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5251 | bool ExpandSource = getTypeAction(SourceVT) == Expand; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5252 | |
Evan Cheng | 9845eb5 | 2008-04-01 02:18:22 +0000 | [diff] [blame] | 5253 | // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc. |
| 5254 | if (!isSigned && SourceVT != MVT::i32) { |
Dan Gohman | 34bc178 | 2008-03-05 02:07:31 +0000 | [diff] [blame] | 5255 | // The integer value loaded will be incorrectly if the 'sign bit' of the |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5256 | // incoming integer is set. To handle this, we dynamically test to see if |
| 5257 | // it is set, and, if so, add a fudge factor. |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5258 | SDOperand Hi; |
| 5259 | if (ExpandSource) { |
| 5260 | SDOperand Lo; |
| 5261 | ExpandOp(Source, Lo, Hi); |
| 5262 | Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi); |
| 5263 | } else { |
| 5264 | // The comparison for the sign bit will use the entire operand. |
| 5265 | Hi = Source; |
| 5266 | } |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5267 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 5268 | // If this is unsigned, and not supported, first perform the conversion to |
| 5269 | // signed, then adjust the result if the sign bit is set. |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5270 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source); |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 5271 | |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 5272 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5273 | DAG.getConstant(0, Hi.getValueType()), |
| 5274 | ISD::SETLT); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5275 | SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5276 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 5277 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 5278 | uint64_t FF = 0x5f800000ULL; |
| 5279 | if (TLI.isLittleEndian()) FF <<= 32; |
Dan Gohman | 34bc178 | 2008-03-05 02:07:31 +0000 | [diff] [blame] | 5280 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5281 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 5282 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 5283 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 5284 | SDOperand FudgeInReg; |
| 5285 | if (DestTy == MVT::f32) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5286 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5287 | PseudoSourceValue::getConstantPool(), 0); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5288 | else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32)) |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5289 | // FIXME: Avoid the extend by construction the right constantpool? |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5290 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5291 | CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5292 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5293 | MVT::f32); |
Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 5294 | else |
| 5295 | assert(0 && "Unexpected conversion"); |
| 5296 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5297 | MVT::ValueType SCVT = SignedConv.getValueType(); |
| 5298 | if (SCVT != DestTy) { |
| 5299 | // Destination type needs to be expanded as well. The FADD now we are |
| 5300 | // constructing will be expanded into a libcall. |
| 5301 | if (MVT::getSizeInBits(SCVT) != MVT::getSizeInBits(DestTy)) { |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5302 | assert(MVT::getSizeInBits(SCVT) * 2 == MVT::getSizeInBits(DestTy)); |
| 5303 | SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy, |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5304 | SignedConv, SignedConv.getValue(1)); |
| 5305 | } |
| 5306 | SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv); |
| 5307 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 5308 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5309 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5310 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 5311 | // Check to see if the target has a custom way to lower this. If so, use it. |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5312 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) { |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 5313 | default: assert(0 && "This action not implemented for this operation!"); |
| 5314 | case TargetLowering::Legal: |
| 5315 | case TargetLowering::Expand: |
| 5316 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 5317 | case TargetLowering::Custom: { |
| 5318 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 5319 | Source), DAG); |
| 5320 | if (NV.Val) |
| 5321 | return LegalizeOp(NV); |
| 5322 | break; // The target decided this was legal after all |
| 5323 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 5326 | // Expand the source, then glue it back together for the call. We must expand |
| 5327 | // the source in case it is shared (this pass of legalize must traverse it). |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5328 | if (ExpandSource) { |
| 5329 | SDOperand SrcLo, SrcHi; |
| 5330 | ExpandOp(Source, SrcLo, SrcHi); |
| 5331 | Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi); |
| 5332 | } |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 5333 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 5334 | RTLIB::Libcall LC; |
Evan Cheng | 110cf48 | 2008-04-01 01:50:16 +0000 | [diff] [blame] | 5335 | if (SourceVT == MVT::i32) { |
| 5336 | if (DestTy == MVT::f32) |
Evan Cheng | db45d1c | 2008-04-01 02:00:09 +0000 | [diff] [blame] | 5337 | LC = isSigned ? RTLIB::SINTTOFP_I32_F32 : RTLIB::UINTTOFP_I32_F32; |
Evan Cheng | 110cf48 | 2008-04-01 01:50:16 +0000 | [diff] [blame] | 5338 | else { |
| 5339 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 5340 | LC = isSigned ? RTLIB::SINTTOFP_I32_F64 : RTLIB::UINTTOFP_I32_F64; |
| 5341 | } |
| 5342 | } else if (SourceVT == MVT::i64) { |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5343 | if (DestTy == MVT::f32) |
| 5344 | LC = RTLIB::SINTTOFP_I64_F32; |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5345 | else if (DestTy == MVT::f64) |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5346 | LC = RTLIB::SINTTOFP_I64_F64; |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 5347 | else if (DestTy == MVT::f80) |
| 5348 | LC = RTLIB::SINTTOFP_I64_F80; |
| 5349 | else { |
| 5350 | assert(DestTy == MVT::ppcf128 && "Unknown fp value type!"); |
| 5351 | LC = RTLIB::SINTTOFP_I64_PPCF128; |
Dan Gohman | d91446d | 2008-03-05 01:08:17 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } else if (SourceVT == MVT::i128) { |
| 5354 | if (DestTy == MVT::f32) |
| 5355 | LC = RTLIB::SINTTOFP_I128_F32; |
| 5356 | else if (DestTy == MVT::f64) |
| 5357 | LC = RTLIB::SINTTOFP_I128_F64; |
| 5358 | else if (DestTy == MVT::f80) |
| 5359 | LC = RTLIB::SINTTOFP_I128_F80; |
| 5360 | else { |
| 5361 | assert(DestTy == MVT::ppcf128 && "Unknown fp value type!"); |
| 5362 | LC = RTLIB::SINTTOFP_I128_PPCF128; |
| 5363 | } |
| 5364 | } else { |
| 5365 | assert(0 && "Unknown int value type"); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 5366 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 5367 | |
Evan Cheng | 4c6cfad | 2007-04-27 07:33:31 +0000 | [diff] [blame] | 5368 | 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] | 5369 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 5370 | SDOperand HiPart; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 5371 | SDOperand Result = ExpandLibCall(LC, Source.Val, isSigned, HiPart); |
Evan Cheng | 110cf48 | 2008-04-01 01:50:16 +0000 | [diff] [blame] | 5372 | if (Result.getValueType() != DestTy && HiPart.Val) |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 5373 | Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart); |
| 5374 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 5375 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5376 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5377 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 5378 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 5379 | /// we expand it. At this point, we know that the result and operand types are |
| 5380 | /// legal for the target. |
| 5381 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 5382 | SDOperand Op0, |
| 5383 | MVT::ValueType DestVT) { |
| 5384 | if (Op0.getValueType() == MVT::i32) { |
| 5385 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 5386 | |
Chris Lattner | 23594d4 | 2008-01-16 07:03:22 +0000 | [diff] [blame] | 5387 | // Get the stack frame index of a 8 byte buffer. |
| 5388 | SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64); |
| 5389 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5390 | // word offset constant for Hi/Lo address computation |
| 5391 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 5392 | // set up Hi and Lo (into buffer) address based on endian |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 5393 | SDOperand Hi = StackSlot; |
| 5394 | SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff); |
| 5395 | if (TLI.isLittleEndian()) |
| 5396 | std::swap(Hi, Lo); |
| 5397 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5398 | // if signed map to unsigned space |
| 5399 | SDOperand Op0Mapped; |
| 5400 | if (isSigned) { |
| 5401 | // constant used to invert sign bit (signed to unsigned mapping) |
| 5402 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 5403 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 5404 | } else { |
| 5405 | Op0Mapped = Op0; |
| 5406 | } |
| 5407 | // store the lo of the constructed double - based on integer input |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 5408 | SDOperand Store1 = DAG.getStore(DAG.getEntryNode(), |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5409 | Op0Mapped, Lo, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5410 | // initial hi portion of constructed double |
| 5411 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 5412 | // store the hi of the constructed double - biased exponent |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 5413 | SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5414 | // load the constructed double |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5415 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5416 | // FP constant to bias correct the final result |
| 5417 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 5418 | BitsToDouble(0x4330000080000000ULL) |
| 5419 | : BitsToDouble(0x4330000000000000ULL), |
| 5420 | MVT::f64); |
| 5421 | // subtract the bias |
| 5422 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 5423 | // final result |
| 5424 | SDOperand Result; |
| 5425 | // handle final rounding |
| 5426 | if (DestVT == MVT::f64) { |
| 5427 | // do nothing |
| 5428 | Result = Sub; |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5429 | } else if (MVT::getSizeInBits(DestVT) < MVT::getSizeInBits(MVT::f64)) { |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5430 | Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub, |
| 5431 | DAG.getIntPtrConstant(0)); |
Dale Johannesen | 118cd9d | 2007-09-16 16:51:49 +0000 | [diff] [blame] | 5432 | } else if (MVT::getSizeInBits(DestVT) > MVT::getSizeInBits(MVT::f64)) { |
| 5433 | Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5434 | } |
| 5435 | return Result; |
| 5436 | } |
| 5437 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 5438 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 5439 | |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 5440 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0, |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5441 | DAG.getConstant(0, Op0.getValueType()), |
| 5442 | ISD::SETLT); |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5443 | SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5444 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 5445 | SignSet, Four, Zero); |
| 5446 | |
| 5447 | // If the sign bit of the integer is set, the large number will be treated |
| 5448 | // as a negative number. To counteract this, the dynamic code adds an |
| 5449 | // offset depending on the data type. |
| 5450 | uint64_t FF; |
| 5451 | switch (Op0.getValueType()) { |
| 5452 | default: assert(0 && "Unsupported integer type!"); |
| 5453 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 5454 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 5455 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 5456 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 5457 | } |
| 5458 | if (TLI.isLittleEndian()) FF <<= 32; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 5459 | static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5460 | |
| 5461 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 5462 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 5463 | SDOperand FudgeInReg; |
| 5464 | if (DestVT == MVT::f32) |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5465 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5466 | PseudoSourceValue::getConstantPool(), 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5467 | else { |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5468 | FudgeInReg = |
| 5469 | LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT, |
| 5470 | DAG.getEntryNode(), CPIdx, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 5471 | PseudoSourceValue::getConstantPool(), 0, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 5472 | MVT::f32)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 5476 | } |
| 5477 | |
| 5478 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 5479 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 5480 | /// we promote it. At this point, we know that the result and operand types are |
| 5481 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 5482 | /// operation that takes a larger input. |
| 5483 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 5484 | MVT::ValueType DestVT, |
| 5485 | bool isSigned) { |
| 5486 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 5487 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 5488 | |
| 5489 | unsigned OpToUse = 0; |
| 5490 | |
| 5491 | // Scan for the appropriate larger type to use. |
| 5492 | while (1) { |
| 5493 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 5494 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 5495 | |
| 5496 | // If the target supports SINT_TO_FP of this type, use it. |
| 5497 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 5498 | default: break; |
| 5499 | case TargetLowering::Legal: |
| 5500 | if (!TLI.isTypeLegal(NewInTy)) |
| 5501 | break; // Can't use this datatype. |
| 5502 | // FALL THROUGH. |
| 5503 | case TargetLowering::Custom: |
| 5504 | OpToUse = ISD::SINT_TO_FP; |
| 5505 | break; |
| 5506 | } |
| 5507 | if (OpToUse) break; |
| 5508 | if (isSigned) continue; |
| 5509 | |
| 5510 | // If the target supports UINT_TO_FP of this type, use it. |
| 5511 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 5512 | default: break; |
| 5513 | case TargetLowering::Legal: |
| 5514 | if (!TLI.isTypeLegal(NewInTy)) |
| 5515 | break; // Can't use this datatype. |
| 5516 | // FALL THROUGH. |
| 5517 | case TargetLowering::Custom: |
| 5518 | OpToUse = ISD::UINT_TO_FP; |
| 5519 | break; |
| 5520 | } |
| 5521 | if (OpToUse) break; |
| 5522 | |
| 5523 | // Otherwise, try a larger type. |
| 5524 | } |
| 5525 | |
| 5526 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 5527 | // desired type then run the operation on it. |
| 5528 | return DAG.getNode(OpToUse, DestVT, |
| 5529 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 5530 | NewInTy, LegalOp)); |
| 5531 | } |
| 5532 | |
| 5533 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 5534 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 5535 | /// we promote it. At this point, we know that the result and operand types are |
| 5536 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 5537 | /// operation that returns a larger result. |
| 5538 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 5539 | MVT::ValueType DestVT, |
| 5540 | bool isSigned) { |
| 5541 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 5542 | MVT::ValueType NewOutTy = DestVT; |
| 5543 | |
| 5544 | unsigned OpToUse = 0; |
| 5545 | |
| 5546 | // Scan for the appropriate larger type to use. |
| 5547 | while (1) { |
| 5548 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 5549 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 5550 | |
| 5551 | // If the target supports FP_TO_SINT returning this type, use it. |
| 5552 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 5553 | default: break; |
| 5554 | case TargetLowering::Legal: |
| 5555 | if (!TLI.isTypeLegal(NewOutTy)) |
| 5556 | break; // Can't use this datatype. |
| 5557 | // FALL THROUGH. |
| 5558 | case TargetLowering::Custom: |
| 5559 | OpToUse = ISD::FP_TO_SINT; |
| 5560 | break; |
| 5561 | } |
| 5562 | if (OpToUse) break; |
| 5563 | |
| 5564 | // If the target supports FP_TO_UINT of this type, use it. |
| 5565 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 5566 | default: break; |
| 5567 | case TargetLowering::Legal: |
| 5568 | if (!TLI.isTypeLegal(NewOutTy)) |
| 5569 | break; // Can't use this datatype. |
| 5570 | // FALL THROUGH. |
| 5571 | case TargetLowering::Custom: |
| 5572 | OpToUse = ISD::FP_TO_UINT; |
| 5573 | break; |
| 5574 | } |
| 5575 | if (OpToUse) break; |
| 5576 | |
| 5577 | // Otherwise, try a larger type. |
| 5578 | } |
| 5579 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5580 | |
| 5581 | // Okay, we found the operation and type to use. |
| 5582 | SDOperand Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp); |
| 5583 | |
| 5584 | // If the operation produces an invalid type, it must be custom lowered. Use |
| 5585 | // the target lowering hooks to expand it. Just keep the low part of the |
| 5586 | // expanded operation, we know that we're truncating anyway. |
| 5587 | if (getTypeAction(NewOutTy) == Expand) { |
| 5588 | Operation = SDOperand(TLI.ExpandOperationResult(Operation.Val, DAG), 0); |
| 5589 | assert(Operation.Val && "Didn't return anything"); |
| 5590 | } |
| 5591 | |
| 5592 | // Truncate the result of the extended FP_TO_*INT operation to the desired |
| 5593 | // size. |
| 5594 | return DAG.getNode(ISD::TRUNCATE, DestVT, Operation); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 5598 | /// |
| 5599 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 5600 | MVT::ValueType VT = Op.getValueType(); |
| 5601 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 5602 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 5603 | switch (VT) { |
| 5604 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 5605 | case MVT::i16: |
| 5606 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5607 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5608 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 5609 | case MVT::i32: |
| 5610 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5611 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5612 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5613 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5614 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 5615 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 5616 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 5617 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 5618 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 5619 | case MVT::i64: |
| 5620 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 5621 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 5622 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5623 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5624 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 5625 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 5626 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 5627 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 5628 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 5629 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 5630 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 5631 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 5632 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 5633 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 5634 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 5635 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 5636 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 5637 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 5638 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 5639 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 5640 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 5641 | } |
| 5642 | } |
| 5643 | |
| 5644 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 5645 | /// |
| 5646 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 5647 | switch (Opc) { |
| 5648 | default: assert(0 && "Cannot expand this yet!"); |
| 5649 | case ISD::CTPOP: { |
| 5650 | static const uint64_t mask[6] = { |
| 5651 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 5652 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 5653 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 5654 | }; |
| 5655 | MVT::ValueType VT = Op.getValueType(); |
| 5656 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5657 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5658 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 5659 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 5660 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 5661 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 5662 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 5663 | DAG.getNode(ISD::AND, VT, |
| 5664 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 5665 | } |
| 5666 | return Op; |
| 5667 | } |
| 5668 | case ISD::CTLZ: { |
| 5669 | // for now, we do this: |
| 5670 | // x = x | (x >> 1); |
| 5671 | // x = x | (x >> 2); |
| 5672 | // ... |
| 5673 | // x = x | (x >>16); |
| 5674 | // x = x | (x >>32); // for 64-bit input |
| 5675 | // return popcount(~x); |
| 5676 | // |
| 5677 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 5678 | MVT::ValueType VT = Op.getValueType(); |
| 5679 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5680 | unsigned len = MVT::getSizeInBits(VT); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5681 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 5682 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 5683 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 5684 | } |
| 5685 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 5686 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 5687 | } |
| 5688 | case ISD::CTTZ: { |
| 5689 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 5690 | // unless the target has ctlz but not ctpop, in which case we use: |
| 5691 | // { return 32 - nlz(~x & (x-1)); } |
| 5692 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 5693 | MVT::ValueType VT = Op.getValueType(); |
| 5694 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 5695 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 5696 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 5697 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 5698 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 5699 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 5700 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 5701 | return DAG.getNode(ISD::SUB, VT, |
Dan Gohman | b55757e | 2007-05-18 17:52:13 +0000 | [diff] [blame] | 5702 | DAG.getConstant(MVT::getSizeInBits(VT), VT), |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 5703 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 5704 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 5705 | } |
| 5706 | } |
| 5707 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 5708 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5709 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 5710 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 5711 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 5712 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 5713 | /// Lo/Hi values are returned. |
| 5714 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 5715 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 5716 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5717 | SDNode *Node = Op.Val; |
| 5718 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 5719 | assert(((MVT::isInteger(NVT) && NVT < VT) || MVT::isFloatingPoint(VT) || |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5720 | MVT::isVector(VT)) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5721 | "Cannot expand to FP value or to larger int value!"); |
| 5722 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5723 | // See if we already expanded it. |
Roman Levenstein | 9cac525 | 2008-04-16 16:15:27 +0000 | [diff] [blame] | 5724 | DenseMap<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 5725 | = ExpandedNodes.find(Op); |
| 5726 | if (I != ExpandedNodes.end()) { |
| 5727 | Lo = I->second.first; |
| 5728 | Hi = I->second.second; |
| 5729 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5732 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5733 | case ISD::CopyFromReg: |
| 5734 | assert(0 && "CopyFromReg must be legal!"); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 5735 | case ISD::FP_ROUND_INREG: |
| 5736 | if (VT == MVT::ppcf128 && |
| 5737 | TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) == |
| 5738 | TargetLowering::Custom) { |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 5739 | SDOperand SrcLo, SrcHi, Src; |
| 5740 | ExpandOp(Op.getOperand(0), SrcLo, SrcHi); |
| 5741 | Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi); |
| 5742 | SDOperand Result = TLI.LowerOperation( |
| 5743 | DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG); |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 5744 | assert(Result.Val->getOpcode() == ISD::BUILD_PAIR); |
| 5745 | Lo = Result.Val->getOperand(0); |
| 5746 | Hi = Result.Val->getOperand(1); |
| 5747 | break; |
| 5748 | } |
| 5749 | // fall through |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 5750 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5751 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 5752 | cerr << "NODE: "; Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 5753 | #endif |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5754 | assert(0 && "Do not know how to expand this operator!"); |
| 5755 | abort(); |
Dan Gohman | 1953ecb | 2008-02-27 01:52:30 +0000 | [diff] [blame] | 5756 | case ISD::EXTRACT_ELEMENT: |
| 5757 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 5758 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 5759 | return ExpandOp(Hi, Lo, Hi); |
Dan Gohman | 18714ae | 2008-02-27 19:44:57 +0000 | [diff] [blame] | 5760 | return ExpandOp(Lo, Lo, Hi); |
Dale Johannesen | 25f1d08 | 2007-10-31 00:32:36 +0000 | [diff] [blame] | 5761 | case ISD::EXTRACT_VECTOR_ELT: |
| 5762 | assert(VT==MVT::i64 && "Do not know how to expand this operator!"); |
| 5763 | // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types. |
| 5764 | Lo = ExpandEXTRACT_VECTOR_ELT(Op); |
| 5765 | return ExpandOp(Lo, Lo, Hi); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5766 | case ISD::UNDEF: |
Evan Cheng | aa975c1 | 2006-12-16 02:20:50 +0000 | [diff] [blame] | 5767 | NVT = TLI.getTypeToExpandTo(VT); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 5768 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 5769 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5770 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5771 | case ISD::Constant: { |
Dan Gohman | 050f550 | 2008-03-03 22:20:46 +0000 | [diff] [blame] | 5772 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 5773 | const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue(); |
| 5774 | Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT); |
| 5775 | Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5776 | break; |
| 5777 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5778 | case ISD::ConstantFP: { |
| 5779 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Dale Johannesen | a471c2e | 2007-10-11 18:07:22 +0000 | [diff] [blame] | 5780 | if (CFP->getValueType(0) == MVT::ppcf128) { |
| 5781 | APInt api = CFP->getValueAPF().convertToAPInt(); |
| 5782 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])), |
| 5783 | MVT::f64); |
| 5784 | Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])), |
| 5785 | MVT::f64); |
| 5786 | break; |
| 5787 | } |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 5788 | Lo = ExpandConstantFP(CFP, false, DAG, TLI); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5789 | if (getTypeAction(Lo.getValueType()) == Expand) |
| 5790 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5791 | break; |
| 5792 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5793 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5794 | // Return the operands. |
| 5795 | Lo = Node->getOperand(0); |
| 5796 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5797 | break; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5798 | |
| 5799 | case ISD::MERGE_VALUES: |
Chris Lattner | c58d558 | 2007-11-24 19:12:15 +0000 | [diff] [blame] | 5800 | if (Node->getNumValues() == 1) { |
| 5801 | ExpandOp(Op.getOperand(0), Lo, Hi); |
| 5802 | break; |
| 5803 | } |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 5804 | // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y) |
| 5805 | assert(Op.ResNo == 0 && Node->getNumValues() == 2 && |
| 5806 | Op.getValue(1).getValueType() == MVT::Other && |
| 5807 | "unhandled MERGE_VALUES"); |
| 5808 | ExpandOp(Op.getOperand(0), Lo, Hi); |
| 5809 | // Remember that we legalized the chain. |
| 5810 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1))); |
| 5811 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5812 | |
| 5813 | case ISD::SIGN_EXTEND_INREG: |
| 5814 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 4bdd275 | 2006-10-06 17:34:12 +0000 | [diff] [blame] | 5815 | // sext_inreg the low part if needed. |
| 5816 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 5817 | |
| 5818 | // The high part gets the sign extension from the lo-part. This handles |
| 5819 | // things like sextinreg V:i64 from i8. |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5820 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5821 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 5822 | TLI.getShiftAmountTy())); |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 5823 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 5824 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 5825 | case ISD::BSWAP: { |
| 5826 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 5827 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 5828 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 5829 | Lo = TempLo; |
| 5830 | break; |
| 5831 | } |
| 5832 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5833 | case ISD::CTPOP: |
| 5834 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 5835 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 5836 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 5837 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5838 | Hi = DAG.getConstant(0, NVT); |
| 5839 | break; |
| 5840 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5841 | case ISD::CTLZ: { |
| 5842 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5843 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5844 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5845 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 5846 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5847 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5848 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 5849 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 5850 | |
| 5851 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 5852 | Hi = DAG.getConstant(0, NVT); |
| 5853 | break; |
| 5854 | } |
| 5855 | |
| 5856 | case ISD::CTTZ: { |
| 5857 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 5858 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5859 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 5860 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 5861 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 5862 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 5863 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 5864 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 5865 | |
| 5866 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 5867 | Hi = DAG.getConstant(0, NVT); |
| 5868 | break; |
| 5869 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 5870 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5871 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5872 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 5873 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5874 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 5875 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 5876 | |
| 5877 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 5878 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5879 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 5880 | if (TLI.isBigEndian()) |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 5881 | std::swap(Lo, Hi); |
| 5882 | break; |
| 5883 | } |
| 5884 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5885 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5886 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 5887 | SDOperand Ch = LD->getChain(); // Legalize the chain. |
| 5888 | SDOperand Ptr = LD->getBasePtr(); // Legalize the pointer. |
| 5889 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5890 | int SVOffset = LD->getSrcValueOffset(); |
| 5891 | unsigned Alignment = LD->getAlignment(); |
| 5892 | bool isVolatile = LD->isVolatile(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5893 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5894 | if (ExtType == ISD::NON_EXTLOAD) { |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5895 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5896 | isVolatile, Alignment); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5897 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 5898 | // f32->i32 or f64->i64 one to one expansion. |
| 5899 | // Remember that we legalized the chain. |
| 5900 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 5901 | // Recursively expand the new load. |
| 5902 | if (getTypeAction(NVT) == Expand) |
| 5903 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 5904 | break; |
| 5905 | } |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 5906 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5907 | // Increment the pointer to the other half. |
| 5908 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
| 5909 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 5910 | DAG.getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 5911 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 5912 | Alignment = MinAlign(Alignment, IncrementSize); |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5913 | Hi = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), SVOffset, |
| 5914 | isVolatile, Alignment); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 5915 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5916 | // Build a factor node to remember that this load is independent of the |
| 5917 | // other one. |
| 5918 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 5919 | Hi.getValue(1)); |
| 5920 | |
| 5921 | // Remember that we legalized the chain. |
| 5922 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Duncan Sands | 0753fc1 | 2008-02-11 10:37:04 +0000 | [diff] [blame] | 5923 | if (TLI.isBigEndian()) |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5924 | std::swap(Lo, Hi); |
| 5925 | } else { |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 5926 | MVT::ValueType EVT = LD->getMemoryVT(); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5927 | |
Dale Johannesen | b6210fc | 2007-10-19 20:29:00 +0000 | [diff] [blame] | 5928 | if ((VT == MVT::f64 && EVT == MVT::f32) || |
| 5929 | (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) { |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5930 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
| 5931 | SDOperand Load = DAG.getLoad(EVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5932 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 548f611 | 2006-12-13 03:19:57 +0000 | [diff] [blame] | 5933 | // Remember that we legalized the chain. |
| 5934 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Load.getValue(1))); |
| 5935 | ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi); |
| 5936 | break; |
| 5937 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5938 | |
| 5939 | if (EVT == NVT) |
| 5940 | Lo = DAG.getLoad(NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5941 | SVOffset, isVolatile, Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5942 | else |
| 5943 | Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, LD->getSrcValue(), |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 5944 | SVOffset, EVT, isVolatile, |
| 5945 | Alignment); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 5946 | |
| 5947 | // Remember that we legalized the chain. |
| 5948 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
| 5949 | |
| 5950 | if (ExtType == ISD::SEXTLOAD) { |
| 5951 | // The high part is obtained by SRA'ing all but one of the bits of the |
| 5952 | // lo part. |
| 5953 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 5954 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 5955 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
| 5956 | } else if (ExtType == ISD::ZEXTLOAD) { |
| 5957 | // The high part is just a zero. |
| 5958 | Hi = DAG.getConstant(0, NVT); |
| 5959 | } else /* if (ExtType == ISD::EXTLOAD) */ { |
| 5960 | // The high part is undefined. |
| 5961 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 5962 | } |
| 5963 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5966 | case ISD::AND: |
| 5967 | case ISD::OR: |
| 5968 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 5969 | SDOperand LL, LH, RL, RH; |
| 5970 | ExpandOp(Node->getOperand(0), LL, LH); |
| 5971 | ExpandOp(Node->getOperand(1), RL, RH); |
| 5972 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 5973 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 5974 | break; |
| 5975 | } |
| 5976 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5977 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5978 | ExpandOp(Node->getOperand(1), LL, LH); |
| 5979 | ExpandOp(Node->getOperand(2), RL, RH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5980 | if (getTypeAction(NVT) == Expand) |
| 5981 | NVT = TLI.getTypeToExpandTo(NVT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 5982 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5983 | if (VT != MVT::f32) |
| 5984 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 5985 | break; |
| 5986 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5987 | case ISD::SELECT_CC: { |
| 5988 | SDOperand TL, TH, FL, FH; |
| 5989 | ExpandOp(Node->getOperand(2), TL, TH); |
| 5990 | ExpandOp(Node->getOperand(3), FL, FH); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5991 | if (getTypeAction(NVT) == Expand) |
| 5992 | NVT = TLI.getTypeToExpandTo(NVT); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5993 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5994 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 5995 | if (VT != MVT::f32) |
| 5996 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 5997 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 5998 | break; |
| 5999 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6000 | case ISD::ANY_EXTEND: |
| 6001 | // The low part is any extension of the input (which degenerates to a copy). |
| 6002 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 6003 | // The high part is undefined. |
| 6004 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 6005 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6006 | case ISD::SIGN_EXTEND: { |
| 6007 | // The low part is just a sign extension of the input (which degenerates to |
| 6008 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6009 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 6010 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6011 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 6012 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 6013 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6014 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 6015 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6016 | break; |
| 6017 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6018 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6019 | // The low part is just a zero extension of the input (which degenerates to |
| 6020 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6021 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 6022 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6023 | // The high part is just a zero. |
| 6024 | Hi = DAG.getConstant(0, NVT); |
| 6025 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 6026 | |
Chris Lattner | 4c948eb | 2007-02-13 23:55:16 +0000 | [diff] [blame] | 6027 | case ISD::TRUNCATE: { |
| 6028 | // The input value must be larger than this value. Expand *it*. |
| 6029 | SDOperand NewLo; |
| 6030 | ExpandOp(Node->getOperand(0), NewLo, Hi); |
| 6031 | |
| 6032 | // The low part is now either the right size, or it is closer. If not the |
| 6033 | // right size, make an illegal truncate so we recursively expand it. |
| 6034 | if (NewLo.getValueType() != Node->getValueType(0)) |
| 6035 | NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo); |
| 6036 | ExpandOp(NewLo, Lo, Hi); |
| 6037 | break; |
| 6038 | } |
| 6039 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 6040 | case ISD::BIT_CONVERT: { |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 6041 | SDOperand Tmp; |
| 6042 | if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){ |
| 6043 | // If the target wants to, allow it to lower this itself. |
| 6044 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 6045 | case Expand: assert(0 && "cannot expand FP!"); |
| 6046 | case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break; |
| 6047 | case Promote: Tmp = PromoteOp (Node->getOperand(0)); break; |
| 6048 | } |
| 6049 | Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG); |
| 6050 | } |
| 6051 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6052 | // f32 / f64 must be expanded to i32 / i64. |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 6053 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 6054 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
Evan Cheng | 19103b1 | 2006-12-15 22:42:55 +0000 | [diff] [blame] | 6055 | if (getTypeAction(NVT) == Expand) |
| 6056 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7b2b5c8 | 2006-12-12 19:53:13 +0000 | [diff] [blame] | 6057 | break; |
| 6058 | } |
| 6059 | |
| 6060 | // If source operand will be expanded to the same type as VT, i.e. |
| 6061 | // i64 <- f64, i32 <- f32, expand the source operand instead. |
| 6062 | MVT::ValueType VT0 = Node->getOperand(0).getValueType(); |
| 6063 | if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) { |
| 6064 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6065 | break; |
| 6066 | } |
| 6067 | |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 6068 | // Turn this into a load/store pair by default. |
| 6069 | if (Tmp.Val == 0) |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 6070 | Tmp = EmitStackConvert(Node->getOperand(0), VT, VT); |
Chris Lattner | f3f333d | 2006-09-09 00:20:27 +0000 | [diff] [blame] | 6071 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 6072 | ExpandOp(Tmp, Lo, Hi); |
| 6073 | break; |
| 6074 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 6075 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6076 | case ISD::READCYCLECOUNTER: { |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 6077 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 6078 | TargetLowering::Custom && |
| 6079 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6080 | SDOperand Tmp = TLI.LowerOperation(Op, DAG); |
| 6081 | assert(Tmp.Val && "Node must be custom expanded!"); |
| 6082 | ExpandOp(Tmp.getValue(0), Lo, Hi); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 6083 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6084 | LegalizeOp(Tmp.getValue(1))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 6085 | break; |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 6086 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 6087 | |
Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 6088 | case ISD::ATOMIC_LCS: { |
| 6089 | SDOperand Tmp = TLI.LowerOperation(Op, DAG); |
| 6090 | assert(Tmp.Val && "Node must be custom expanded!"); |
| 6091 | ExpandOp(Tmp.getValue(0), Lo, Hi); |
| 6092 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
| 6093 | LegalizeOp(Tmp.getValue(1))); |
| 6094 | break; |
| 6095 | } |
| 6096 | |
| 6097 | |
| 6098 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6099 | // These operators cannot be expanded directly, emit them as calls to |
| 6100 | // library functions. |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6101 | case ISD::FP_TO_SINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6102 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 6103 | SDOperand Op; |
| 6104 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 6105 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6106 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 6107 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 6108 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6109 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 6110 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 6111 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6112 | // Now that the custom expander is done, expand the result, which is still |
| 6113 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 6114 | if (Op.Val) { |
| 6115 | ExpandOp(Op, Lo, Hi); |
| 6116 | break; |
| 6117 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6118 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6119 | |
Dale Johannesen | 161e897 | 2007-10-05 20:04:43 +0000 | [diff] [blame] | 6120 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6121 | if (VT == MVT::i64) { |
| 6122 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 6123 | LC = RTLIB::FPTOSINT_F32_I64; |
| 6124 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
| 6125 | LC = RTLIB::FPTOSINT_F64_I64; |
| 6126 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6127 | LC = RTLIB::FPTOSINT_F80_I64; |
| 6128 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6129 | LC = RTLIB::FPTOSINT_PPCF128_I64; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6130 | Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi); |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6131 | } else if (VT == MVT::i128) { |
| 6132 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 6133 | LC = RTLIB::FPTOSINT_F32_I128; |
| 6134 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
| 6135 | LC = RTLIB::FPTOSINT_F64_I128; |
| 6136 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6137 | LC = RTLIB::FPTOSINT_F80_I128; |
| 6138 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6139 | LC = RTLIB::FPTOSINT_PPCF128_I128; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6140 | Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi); |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6141 | } else { |
| 6142 | assert(0 && "Unexpected uint-to-fp conversion!"); |
| 6143 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6144 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6145 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6146 | |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6147 | case ISD::FP_TO_UINT: { |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6148 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6149 | SDOperand Op; |
| 6150 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 6151 | case Expand: assert(0 && "cannot expand FP!"); |
| 6152 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 6153 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 6154 | } |
| 6155 | |
| 6156 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 6157 | |
| 6158 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 6159 | if (Op.Val) { |
| 6160 | ExpandOp(Op, Lo, Hi); |
| 6161 | break; |
| 6162 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 6163 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 6164 | |
Evan Cheng | daccea18 | 2007-10-05 01:09:32 +0000 | [diff] [blame] | 6165 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6166 | if (VT == MVT::i64) { |
| 6167 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 6168 | LC = RTLIB::FPTOUINT_F32_I64; |
| 6169 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
| 6170 | LC = RTLIB::FPTOUINT_F64_I64; |
| 6171 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6172 | LC = RTLIB::FPTOUINT_F80_I64; |
| 6173 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6174 | LC = RTLIB::FPTOUINT_PPCF128_I64; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6175 | Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi); |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6176 | } else if (VT == MVT::i128) { |
| 6177 | if (Node->getOperand(0).getValueType() == MVT::f32) |
| 6178 | LC = RTLIB::FPTOUINT_F32_I128; |
| 6179 | else if (Node->getOperand(0).getValueType() == MVT::f64) |
| 6180 | LC = RTLIB::FPTOUINT_F64_I128; |
| 6181 | else if (Node->getOperand(0).getValueType() == MVT::f80) |
| 6182 | LC = RTLIB::FPTOUINT_F80_I128; |
| 6183 | else if (Node->getOperand(0).getValueType() == MVT::ppcf128) |
| 6184 | LC = RTLIB::FPTOUINT_PPCF128_I128; |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6185 | Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi); |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6186 | } else { |
| 6187 | assert(0 && "Unexpected uint-to-fp conversion!"); |
| 6188 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6189 | break; |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6190 | } |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 6191 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6192 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6193 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6194 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6195 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6196 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6197 | Op = TLI.LowerOperation(Op, DAG); |
| 6198 | if (Op.Val) { |
| 6199 | // Now that the custom expander is done, expand the result, which is |
| 6200 | // still VT. |
| 6201 | ExpandOp(Op, Lo, Hi); |
| 6202 | break; |
| 6203 | } |
| 6204 | } |
| 6205 | |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6206 | // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit |
| 6207 | // this X << 1 as X+X. |
| 6208 | if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) { |
Dan Gohman | 002e5d0 | 2008-03-13 22:13:53 +0000 | [diff] [blame] | 6209 | if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) && |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6210 | TLI.isOperationLegal(ISD::ADDE, NVT)) { |
| 6211 | SDOperand LoOps[2], HiOps[3]; |
| 6212 | ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]); |
| 6213 | SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag); |
| 6214 | LoOps[1] = LoOps[0]; |
| 6215 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 6216 | |
| 6217 | HiOps[1] = HiOps[0]; |
| 6218 | HiOps[2] = Lo.getValue(1); |
| 6219 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 6220 | break; |
| 6221 | } |
| 6222 | } |
| 6223 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6224 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6225 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6226 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6227 | |
| 6228 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6229 | TargetLowering::LegalizeAction Action = |
| 6230 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 6231 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6232 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6233 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6234 | break; |
| 6235 | } |
| 6236 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6237 | // Otherwise, emit a libcall. |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6238 | Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6239 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6240 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6241 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6242 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6243 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6244 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6245 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6246 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6247 | Op = TLI.LowerOperation(Op, DAG); |
| 6248 | if (Op.Val) { |
| 6249 | // Now that the custom expander is done, expand the result, which is |
| 6250 | // still VT. |
| 6251 | ExpandOp(Op, Lo, Hi); |
| 6252 | break; |
| 6253 | } |
| 6254 | } |
| 6255 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6256 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6257 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6258 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6259 | |
| 6260 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6261 | TargetLowering::LegalizeAction Action = |
| 6262 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 6263 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6264 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6265 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6266 | break; |
| 6267 | } |
| 6268 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6269 | // Otherwise, emit a libcall. |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6270 | Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6271 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6275 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6276 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6277 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6278 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 6279 | Op = TLI.LowerOperation(Op, DAG); |
| 6280 | if (Op.Val) { |
| 6281 | // Now that the custom expander is done, expand the result, which is |
| 6282 | // still VT. |
| 6283 | ExpandOp(Op, Lo, Hi); |
| 6284 | break; |
| 6285 | } |
| 6286 | } |
| 6287 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6288 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6289 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6290 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6291 | |
| 6292 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6293 | TargetLowering::LegalizeAction Action = |
| 6294 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 6295 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 6296 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 6297 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 6298 | break; |
| 6299 | } |
| 6300 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6301 | // Otherwise, emit a libcall. |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6302 | Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6303 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6304 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 6305 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 6306 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6307 | case ISD::SUB: { |
| 6308 | // If the target wants to custom expand this, let them. |
| 6309 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 6310 | TargetLowering::Custom) { |
| 6311 | Op = TLI.LowerOperation(Op, DAG); |
| 6312 | if (Op.Val) { |
| 6313 | ExpandOp(Op, Lo, Hi); |
| 6314 | break; |
| 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | // Expand the subcomponents. |
| 6319 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6320 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6321 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6322 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6323 | SDOperand LoOps[2], HiOps[3]; |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6324 | LoOps[0] = LHSL; |
| 6325 | LoOps[1] = RHSL; |
| 6326 | HiOps[0] = LHSH; |
| 6327 | HiOps[1] = RHSH; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6328 | if (Node->getOpcode() == ISD::ADD) { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6329 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6330 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6331 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6332 | } else { |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6333 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6334 | HiOps[2] = Lo.getValue(1); |
Chris Lattner | 79980b0 | 2006-09-13 03:50:39 +0000 | [diff] [blame] | 6335 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 6336 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 6337 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 6338 | } |
Chris Lattner | b429f73 | 2007-05-17 18:15:41 +0000 | [diff] [blame] | 6339 | |
| 6340 | case ISD::ADDC: |
| 6341 | case ISD::SUBC: { |
| 6342 | // Expand the subcomponents. |
| 6343 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6344 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6345 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 6346 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6347 | SDOperand LoOps[2] = { LHSL, RHSL }; |
| 6348 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 6349 | |
| 6350 | if (Node->getOpcode() == ISD::ADDC) { |
| 6351 | Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2); |
| 6352 | HiOps[2] = Lo.getValue(1); |
| 6353 | Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3); |
| 6354 | } else { |
| 6355 | Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2); |
| 6356 | HiOps[2] = Lo.getValue(1); |
| 6357 | Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3); |
| 6358 | } |
| 6359 | // Remember that we legalized the flag. |
| 6360 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 6361 | break; |
| 6362 | } |
| 6363 | case ISD::ADDE: |
| 6364 | case ISD::SUBE: { |
| 6365 | // Expand the subcomponents. |
| 6366 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 6367 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 6368 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
| 6369 | SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag); |
| 6370 | SDOperand LoOps[3] = { LHSL, RHSL, Node->getOperand(2) }; |
| 6371 | SDOperand HiOps[3] = { LHSH, RHSH }; |
| 6372 | |
| 6373 | Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3); |
| 6374 | HiOps[2] = Lo.getValue(1); |
| 6375 | Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3); |
| 6376 | |
| 6377 | // Remember that we legalized the flag. |
| 6378 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1))); |
| 6379 | break; |
| 6380 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6381 | case ISD::MUL: { |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 6382 | // If the target wants to custom expand this, let them. |
| 6383 | if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8829dc8 | 2006-09-16 05:08:34 +0000 | [diff] [blame] | 6384 | SDOperand New = TLI.LowerOperation(Op, DAG); |
| 6385 | if (New.Val) { |
| 6386 | ExpandOp(New, Lo, Hi); |
Chris Lattner | 7d7bffe | 2006-09-16 00:09:24 +0000 | [diff] [blame] | 6387 | break; |
| 6388 | } |
| 6389 | } |
| 6390 | |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 6391 | bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT); |
| 6392 | bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6393 | bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT); |
| 6394 | bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT); |
| 6395 | if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6396 | SDOperand LL, LH, RL, RH; |
| 6397 | ExpandOp(Node->getOperand(0), LL, LH); |
| 6398 | ExpandOp(Node->getOperand(1), RL, RH); |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 6399 | unsigned OuterBitSize = Op.getValueSizeInBits(); |
| 6400 | unsigned InnerBitSize = RH.getValueSizeInBits(); |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6401 | unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0)); |
| 6402 | unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1)); |
Dan Gohman | 76c605b | 2008-03-10 20:42:19 +0000 | [diff] [blame] | 6403 | APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize); |
| 6404 | if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) && |
| 6405 | DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6406 | // The inputs are both zero-extended. |
| 6407 | if (HasUMUL_LOHI) { |
| 6408 | // We can emit a umul_lohi. |
| 6409 | Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL); |
| 6410 | Hi = SDOperand(Lo.Val, 1); |
| 6411 | break; |
| 6412 | } |
| 6413 | if (HasMULHU) { |
| 6414 | // We can emit a mulhu+mul. |
| 6415 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6416 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 6417 | break; |
| 6418 | } |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6419 | } |
Dan Gohman | 2e68b6f | 2008-02-25 21:11:39 +0000 | [diff] [blame] | 6420 | if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) { |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6421 | // The input values are both sign-extended. |
| 6422 | if (HasSMUL_LOHI) { |
| 6423 | // We can emit a smul_lohi. |
| 6424 | Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL); |
| 6425 | Hi = SDOperand(Lo.Val, 1); |
| 6426 | break; |
| 6427 | } |
| 6428 | if (HasMULHS) { |
| 6429 | // We can emit a mulhs+mul. |
| 6430 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6431 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
| 6432 | break; |
| 6433 | } |
| 6434 | } |
| 6435 | if (HasUMUL_LOHI) { |
| 6436 | // Lo,Hi = umul LHS, RHS. |
| 6437 | SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, |
| 6438 | DAG.getVTList(NVT, NVT), LL, RL); |
| 6439 | Lo = UMulLOHI; |
| 6440 | Hi = UMulLOHI.getValue(1); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 6441 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 6442 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 6443 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 6444 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
Chris Lattner | a89654b | 2006-09-16 00:21:44 +0000 | [diff] [blame] | 6445 | break; |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 6446 | } |
Dale Johannesen | 8eadd5a | 2007-10-24 22:26:08 +0000 | [diff] [blame] | 6447 | if (HasMULHU) { |
| 6448 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 6449 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 6450 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 6451 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 6452 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 6453 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 6454 | break; |
| 6455 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6456 | } |
Evan Cheng | 3f4fd0f | 2006-09-01 18:17:58 +0000 | [diff] [blame] | 6457 | |
Dan Gohman | 525178c | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 6458 | // If nothing else, we can make a libcall. |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6459 | Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 6460 | break; |
| 6461 | } |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6462 | case ISD::SDIV: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6463 | Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6464 | break; |
| 6465 | case ISD::UDIV: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6466 | Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6467 | break; |
| 6468 | case ISD::SREM: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6469 | Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6470 | break; |
| 6471 | case ISD::UREM: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6472 | Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6473 | break; |
Evan Cheng | 5c9ce18 | 2006-12-12 21:51:17 +0000 | [diff] [blame] | 6474 | |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6475 | case ISD::FADD: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6476 | Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32, |
| 6477 | RTLIB::ADD_F64, |
| 6478 | RTLIB::ADD_F80, |
| 6479 | RTLIB::ADD_PPCF128), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6480 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6481 | break; |
| 6482 | case ISD::FSUB: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6483 | Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32, |
| 6484 | RTLIB::SUB_F64, |
| 6485 | RTLIB::SUB_F80, |
| 6486 | RTLIB::SUB_PPCF128), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6487 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6488 | break; |
| 6489 | case ISD::FMUL: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6490 | Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32, |
| 6491 | RTLIB::MUL_F64, |
| 6492 | RTLIB::MUL_F80, |
| 6493 | RTLIB::MUL_PPCF128), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6494 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6495 | break; |
| 6496 | case ISD::FDIV: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6497 | Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32, |
| 6498 | RTLIB::DIV_F64, |
| 6499 | RTLIB::DIV_F80, |
| 6500 | RTLIB::DIV_PPCF128), |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6501 | Node, false, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6502 | break; |
| 6503 | case ISD::FP_EXTEND: |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6504 | if (VT == MVT::ppcf128) { |
| 6505 | assert(Node->getOperand(0).getValueType()==MVT::f32 || |
| 6506 | Node->getOperand(0).getValueType()==MVT::f64); |
| 6507 | const uint64_t zero = 0; |
| 6508 | if (Node->getOperand(0).getValueType()==MVT::f32) |
| 6509 | Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0)); |
| 6510 | else |
| 6511 | Hi = Node->getOperand(0); |
| 6512 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6513 | break; |
| 6514 | } |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6515 | Lo = ExpandLibCall(RTLIB::FPEXT_F32_F64, Node, true, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6516 | break; |
| 6517 | case ISD::FP_ROUND: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6518 | Lo = ExpandLibCall(RTLIB::FPROUND_F64_F32, Node, true, Hi); |
Evan Cheng | 1a8f1fe | 2006-12-09 02:42:38 +0000 | [diff] [blame] | 6519 | break; |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 6520 | case ISD::FPOWI: |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6521 | Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::POWI_F32, |
| 6522 | RTLIB::POWI_F64, |
| 6523 | RTLIB::POWI_F80, |
| 6524 | RTLIB::POWI_PPCF128), |
Lauro Ramos Venancio | c90f089 | 2007-08-15 22:13:27 +0000 | [diff] [blame] | 6525 | Node, false, Hi); |
| 6526 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6527 | case ISD::FSQRT: |
| 6528 | case ISD::FSIN: |
| 6529 | case ISD::FCOS: { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6530 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6531 | switch(Node->getOpcode()) { |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6532 | case ISD::FSQRT: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6533 | LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 6534 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6535 | break; |
| 6536 | case ISD::FSIN: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6537 | LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 6538 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6539 | break; |
| 6540 | case ISD::FCOS: |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 6541 | LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64, |
| 6542 | RTLIB::COS_F80, RTLIB::COS_PPCF128); |
Evan Cheng | 5696622 | 2007-01-12 02:11:51 +0000 | [diff] [blame] | 6543 | break; |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6544 | default: assert(0 && "Unreachable!"); |
| 6545 | } |
Duncan Sands | 460a14e | 2008-04-12 17:14:18 +0000 | [diff] [blame] | 6546 | Lo = ExpandLibCall(LC, Node, false, Hi); |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6547 | break; |
| 6548 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6549 | case ISD::FABS: { |
Dale Johannesen | f646774 | 2007-10-12 19:02:17 +0000 | [diff] [blame] | 6550 | if (VT == MVT::ppcf128) { |
| 6551 | SDOperand Tmp; |
| 6552 | ExpandOp(Node->getOperand(0), Lo, Tmp); |
| 6553 | Hi = DAG.getNode(ISD::FABS, NVT, Tmp); |
| 6554 | // lo = hi==fabs(hi) ? lo : -lo; |
| 6555 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp, |
| 6556 | Lo, DAG.getNode(ISD::FNEG, NVT, Lo), |
| 6557 | DAG.getCondCode(ISD::SETEQ)); |
| 6558 | break; |
| 6559 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6560 | SDOperand Mask = (VT == MVT::f64) |
| 6561 | ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT) |
| 6562 | : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT); |
| 6563 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 6564 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 6565 | Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask); |
| 6566 | if (getTypeAction(NVT) == Expand) |
| 6567 | ExpandOp(Lo, Lo, Hi); |
| 6568 | break; |
| 6569 | } |
| 6570 | case ISD::FNEG: { |
Dale Johannesen | f646774 | 2007-10-12 19:02:17 +0000 | [diff] [blame] | 6571 | if (VT == MVT::ppcf128) { |
| 6572 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 6573 | Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo); |
| 6574 | Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi); |
| 6575 | break; |
| 6576 | } |
Evan Cheng | 966bf24 | 2006-12-16 00:52:40 +0000 | [diff] [blame] | 6577 | SDOperand Mask = (VT == MVT::f64) |
| 6578 | ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT) |
| 6579 | : DAG.getConstantFP(BitsToFloat(1U << 31), VT); |
| 6580 | Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask); |
| 6581 | Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0)); |
| 6582 | Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask); |
| 6583 | if (getTypeAction(NVT) == Expand) |
| 6584 | ExpandOp(Lo, Lo, Hi); |
| 6585 | break; |
| 6586 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 6587 | case ISD::FCOPYSIGN: { |
| 6588 | Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI); |
| 6589 | if (getTypeAction(NVT) == Expand) |
| 6590 | ExpandOp(Lo, Lo, Hi); |
| 6591 | break; |
| 6592 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6593 | case ISD::SINT_TO_FP: |
| 6594 | case ISD::UINT_TO_FP: { |
| 6595 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
| 6596 | MVT::ValueType SrcVT = Node->getOperand(0).getValueType(); |
Dale Johannesen | cf49819 | 2008-03-18 17:28:38 +0000 | [diff] [blame] | 6597 | |
| 6598 | // Promote the operand if needed. Do this before checking for |
| 6599 | // ppcf128 so conversions of i16 and i8 work. |
| 6600 | if (getTypeAction(SrcVT) == Promote) { |
| 6601 | SDOperand Tmp = PromoteOp(Node->getOperand(0)); |
| 6602 | Tmp = isSigned |
| 6603 | ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp, |
| 6604 | DAG.getValueType(SrcVT)) |
| 6605 | : DAG.getZeroExtendInReg(Tmp, SrcVT); |
| 6606 | Node = DAG.UpdateNodeOperands(Op, Tmp).Val; |
| 6607 | SrcVT = Node->getOperand(0).getValueType(); |
| 6608 | } |
| 6609 | |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6610 | if (VT == MVT::ppcf128 && SrcVT == MVT::i32) { |
Dan Gohman | f6283fd | 2008-02-25 21:39:34 +0000 | [diff] [blame] | 6611 | static const uint64_t zero = 0; |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6612 | if (isSigned) { |
| 6613 | Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64, |
| 6614 | Node->getOperand(0))); |
| 6615 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6616 | } else { |
Dan Gohman | f6283fd | 2008-02-25 21:39:34 +0000 | [diff] [blame] | 6617 | static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 }; |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6618 | Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64, |
| 6619 | Node->getOperand(0))); |
| 6620 | Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64); |
| 6621 | Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6622 | // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32 |
Dale Johannesen | ca68aaa | 2007-10-12 01:37:08 +0000 | [diff] [blame] | 6623 | ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0), |
| 6624 | DAG.getConstant(0, MVT::i32), |
| 6625 | DAG.getNode(ISD::FADD, MVT::ppcf128, Hi, |
| 6626 | DAG.getConstantFP( |
| 6627 | APFloat(APInt(128, 2, TwoE32)), |
| 6628 | MVT::ppcf128)), |
| 6629 | Hi, |
| 6630 | DAG.getCondCode(ISD::SETLT)), |
| 6631 | Lo, Hi); |
| 6632 | } |
| 6633 | break; |
| 6634 | } |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6635 | if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) { |
| 6636 | // si64->ppcf128 done by libcall, below |
Dan Gohman | f6283fd | 2008-02-25 21:39:34 +0000 | [diff] [blame] | 6637 | static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 }; |
Dale Johannesen | 6e63e09 | 2007-10-12 17:52:03 +0000 | [diff] [blame] | 6638 | ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)), |
| 6639 | Lo, Hi); |
| 6640 | Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi); |
| 6641 | // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64 |
| 6642 | ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0), |
| 6643 | DAG.getConstant(0, MVT::i64), |
| 6644 | DAG.getNode(ISD::FADD, MVT::ppcf128, Hi, |
| 6645 | DAG.getConstantFP( |
| 6646 | APFloat(APInt(128, 2, TwoE64)), |
| 6647 | MVT::ppcf128)), |
| 6648 | Hi, |
| 6649 | DAG.getCondCode(ISD::SETLT)), |
| 6650 | Lo, Hi); |
| 6651 | break; |
| 6652 | } |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6653 | |
Dan Gohman | a2e9485 | 2008-03-10 23:03:31 +0000 | [diff] [blame] | 6654 | Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT, |
| 6655 | Node->getOperand(0)); |
Evan Cheng | 110cf48 | 2008-04-01 01:50:16 +0000 | [diff] [blame] | 6656 | if (getTypeAction(Lo.getValueType()) == Expand) |
Evan Cheng | 6ad2f93 | 2008-04-01 01:51:26 +0000 | [diff] [blame] | 6657 | // float to i32 etc. can be 'expanded' to a single node. |
Evan Cheng | 110cf48 | 2008-04-01 01:50:16 +0000 | [diff] [blame] | 6658 | ExpandOp(Lo, Lo, Hi); |
Evan Cheng | 7df28dc | 2006-12-19 01:44:04 +0000 | [diff] [blame] | 6659 | break; |
| 6660 | } |
Evan Cheng | 98ff3b9 | 2006-12-13 02:38:13 +0000 | [diff] [blame] | 6661 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6662 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 6663 | // Make sure the resultant values have been legalized themselves, unless this |
| 6664 | // is a type that requires multi-step expansion. |
| 6665 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 6666 | Lo = LegalizeOp(Lo); |
Evan Cheng | 13acce3 | 2006-12-11 19:27:14 +0000 | [diff] [blame] | 6667 | if (Hi.Val) |
| 6668 | // Don't legalize the high part if it is expanded to a single node. |
| 6669 | Hi = LegalizeOp(Hi); |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 6670 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 6671 | |
| 6672 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 6673 | 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] | 6674 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6677 | /// SplitVectorOp - Given an operand of vector type, break it down into |
| 6678 | /// two smaller values, still of vector type. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6679 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 6680 | SDOperand &Hi) { |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6681 | assert(MVT::isVector(Op.getValueType()) && "Cannot split non-vector type!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6682 | SDNode *Node = Op.Val; |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 6683 | unsigned NumElements = MVT::getVectorNumElements(Op.getValueType()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6684 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6685 | |
Dan Gohman | e40c7b0 | 2007-09-24 15:54:53 +0000 | [diff] [blame] | 6686 | MVT::ValueType NewEltVT = MVT::getVectorElementType(Op.getValueType()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6687 | |
| 6688 | unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1); |
| 6689 | unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo; |
| 6690 | |
| 6691 | MVT::ValueType NewVT_Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo); |
| 6692 | MVT::ValueType NewVT_Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi); |
| 6693 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6694 | // See if we already split it. |
| 6695 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 6696 | = SplitNodes.find(Op); |
| 6697 | if (I != SplitNodes.end()) { |
| 6698 | Lo = I->second.first; |
| 6699 | Hi = I->second.second; |
| 6700 | return; |
| 6701 | } |
| 6702 | |
| 6703 | switch (Node->getOpcode()) { |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6704 | default: |
| 6705 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 6706 | Node->dump(&DAG); |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6707 | #endif |
| 6708 | assert(0 && "Unhandled operation in SplitVectorOp!"); |
Chris Lattner | daf9bc8 | 2007-11-19 20:21:32 +0000 | [diff] [blame] | 6709 | case ISD::UNDEF: |
| 6710 | Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo); |
| 6711 | Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi); |
| 6712 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6713 | case ISD::BUILD_PAIR: |
| 6714 | Lo = Node->getOperand(0); |
| 6715 | Hi = Node->getOperand(1); |
| 6716 | break; |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6717 | case ISD::INSERT_VECTOR_ELT: { |
| 6718 | SplitVectorOp(Node->getOperand(0), Lo, Hi); |
| 6719 | unsigned Index = cast<ConstantSDNode>(Node->getOperand(2))->getValue(); |
| 6720 | SDOperand ScalarOp = Node->getOperand(1); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6721 | if (Index < NewNumElts_Lo) |
| 6722 | Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 6723 | DAG.getIntPtrConstant(Index)); |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6724 | else |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6725 | Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp, |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 6726 | DAG.getIntPtrConstant(Index - NewNumElts_Lo)); |
Dan Gohman | 9fe4662 | 2007-09-28 23:53:40 +0000 | [diff] [blame] | 6727 | break; |
| 6728 | } |
Chris Lattner | 6c9c680 | 2007-11-19 21:16:54 +0000 | [diff] [blame] | 6729 | case ISD::VECTOR_SHUFFLE: { |
| 6730 | // Build the low part. |
| 6731 | SDOperand Mask = Node->getOperand(2); |
| 6732 | SmallVector<SDOperand, 8> Ops; |
| 6733 | MVT::ValueType PtrVT = TLI.getPointerTy(); |
| 6734 | |
| 6735 | // Insert all of the elements from the input that are needed. We use |
| 6736 | // buildvector of extractelement here because the input vectors will have |
| 6737 | // to be legalized, so this makes the code simpler. |
| 6738 | for (unsigned i = 0; i != NewNumElts_Lo; ++i) { |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 6739 | SDOperand IdxNode = Mask.getOperand(i); |
| 6740 | if (IdxNode.getOpcode() == ISD::UNDEF) { |
| 6741 | Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT)); |
| 6742 | continue; |
| 6743 | } |
| 6744 | unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue(); |
Chris Lattner | 6c9c680 | 2007-11-19 21:16:54 +0000 | [diff] [blame] | 6745 | SDOperand InVec = Node->getOperand(0); |
| 6746 | if (Idx >= NumElements) { |
| 6747 | InVec = Node->getOperand(1); |
| 6748 | Idx -= NumElements; |
| 6749 | } |
| 6750 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec, |
| 6751 | DAG.getConstant(Idx, PtrVT))); |
| 6752 | } |
| 6753 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size()); |
| 6754 | Ops.clear(); |
| 6755 | |
| 6756 | for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) { |
Nate Begeman | 5922f56 | 2008-03-14 00:53:31 +0000 | [diff] [blame] | 6757 | SDOperand IdxNode = Mask.getOperand(i); |
| 6758 | if (IdxNode.getOpcode() == ISD::UNDEF) { |
| 6759 | Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT)); |
| 6760 | continue; |
| 6761 | } |
| 6762 | unsigned Idx = cast<ConstantSDNode>(IdxNode)->getValue(); |
Chris Lattner | 6c9c680 | 2007-11-19 21:16:54 +0000 | [diff] [blame] | 6763 | SDOperand InVec = Node->getOperand(0); |
| 6764 | if (Idx >= NumElements) { |
| 6765 | InVec = Node->getOperand(1); |
| 6766 | Idx -= NumElements; |
| 6767 | } |
| 6768 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec, |
| 6769 | DAG.getConstant(Idx, PtrVT))); |
| 6770 | } |
| 6771 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size()); |
| 6772 | break; |
| 6773 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6774 | case ISD::BUILD_VECTOR: { |
Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 6775 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6776 | Node->op_begin()+NewNumElts_Lo); |
| 6777 | Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6778 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6779 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumElts_Lo, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6780 | Node->op_end()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6781 | Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size()); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6782 | break; |
| 6783 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6784 | case ISD::CONCAT_VECTORS: { |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6785 | // FIXME: Handle non-power-of-two vectors? |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6786 | unsigned NewNumSubvectors = Node->getNumOperands() / 2; |
| 6787 | if (NewNumSubvectors == 1) { |
| 6788 | Lo = Node->getOperand(0); |
| 6789 | Hi = Node->getOperand(1); |
| 6790 | } else { |
| 6791 | SmallVector<SDOperand, 8> LoOps(Node->op_begin(), |
| 6792 | Node->op_begin()+NewNumSubvectors); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6793 | Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size()); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6794 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6795 | SmallVector<SDOperand, 8> HiOps(Node->op_begin()+NewNumSubvectors, |
| 6796 | Node->op_end()); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6797 | Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size()); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6798 | } |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 6799 | break; |
| 6800 | } |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6801 | case ISD::SELECT: { |
| 6802 | SDOperand Cond = Node->getOperand(0); |
| 6803 | |
| 6804 | SDOperand LL, LH, RL, RH; |
| 6805 | SplitVectorOp(Node->getOperand(1), LL, LH); |
| 6806 | SplitVectorOp(Node->getOperand(2), RL, RH); |
| 6807 | |
| 6808 | if (MVT::isVector(Cond.getValueType())) { |
| 6809 | // Handle a vector merge. |
| 6810 | SDOperand CL, CH; |
| 6811 | SplitVectorOp(Cond, CL, CH); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6812 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL); |
| 6813 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH); |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6814 | } else { |
| 6815 | // Handle a simple select with vector operands. |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6816 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL); |
| 6817 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH); |
Dan Gohman | c623096 | 2007-10-17 14:48:28 +0000 | [diff] [blame] | 6818 | } |
| 6819 | break; |
| 6820 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6821 | case ISD::ADD: |
| 6822 | case ISD::SUB: |
| 6823 | case ISD::MUL: |
| 6824 | case ISD::FADD: |
| 6825 | case ISD::FSUB: |
| 6826 | case ISD::FMUL: |
| 6827 | case ISD::SDIV: |
| 6828 | case ISD::UDIV: |
| 6829 | case ISD::FDIV: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6830 | case ISD::FPOW: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6831 | case ISD::AND: |
| 6832 | case ISD::OR: |
Dan Gohman | 089617d | 2007-11-19 15:15:03 +0000 | [diff] [blame] | 6833 | case ISD::XOR: |
| 6834 | case ISD::UREM: |
| 6835 | case ISD::SREM: |
| 6836 | case ISD::FREM: { |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6837 | SDOperand LL, LH, RL, RH; |
| 6838 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 6839 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 6840 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6841 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL); |
| 6842 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6843 | break; |
| 6844 | } |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6845 | case ISD::FPOWI: { |
| 6846 | SDOperand L, H; |
| 6847 | SplitVectorOp(Node->getOperand(0), L, H); |
| 6848 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6849 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1)); |
| 6850 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1)); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6851 | break; |
| 6852 | } |
| 6853 | case ISD::CTTZ: |
| 6854 | case ISD::CTLZ: |
| 6855 | case ISD::CTPOP: |
| 6856 | case ISD::FNEG: |
| 6857 | case ISD::FABS: |
| 6858 | case ISD::FSQRT: |
| 6859 | case ISD::FSIN: |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 6860 | case ISD::FCOS: |
| 6861 | case ISD::FP_TO_SINT: |
| 6862 | case ISD::FP_TO_UINT: |
| 6863 | case ISD::SINT_TO_FP: |
| 6864 | case ISD::UINT_TO_FP: { |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6865 | SDOperand L, H; |
| 6866 | SplitVectorOp(Node->getOperand(0), L, H); |
| 6867 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6868 | Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L); |
| 6869 | Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6870 | break; |
| 6871 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6872 | case ISD::LOAD: { |
| 6873 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 6874 | SDOperand Ch = LD->getChain(); |
| 6875 | SDOperand Ptr = LD->getBasePtr(); |
| 6876 | const Value *SV = LD->getSrcValue(); |
| 6877 | int SVOffset = LD->getSrcValueOffset(); |
| 6878 | unsigned Alignment = LD->getAlignment(); |
| 6879 | bool isVolatile = LD->isVolatile(); |
| 6880 | |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6881 | Lo = DAG.getLoad(NewVT_Lo, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
| 6882 | unsigned IncrementSize = NewNumElts_Lo * MVT::getSizeInBits(NewEltVT)/8; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6883 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 6884 | DAG.getIntPtrConstant(IncrementSize)); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6885 | SVOffset += IncrementSize; |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 6886 | Alignment = MinAlign(Alignment, IncrementSize); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6887 | Hi = DAG.getLoad(NewVT_Hi, Ch, Ptr, SV, SVOffset, isVolatile, Alignment); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6888 | |
| 6889 | // Build a factor node to remember that this load is independent of the |
| 6890 | // other one. |
| 6891 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 6892 | Hi.getValue(1)); |
| 6893 | |
| 6894 | // Remember that we legalized the chain. |
| 6895 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6896 | break; |
| 6897 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6898 | case ISD::BIT_CONVERT: { |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6899 | // We know the result is a vector. The input may be either a vector or a |
| 6900 | // scalar value. |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6901 | SDOperand InOp = Node->getOperand(0); |
| 6902 | if (!MVT::isVector(InOp.getValueType()) || |
| 6903 | MVT::getVectorNumElements(InOp.getValueType()) == 1) { |
| 6904 | // The input is a scalar or single-element vector. |
| 6905 | // Lower to a store/load so that it can be split. |
| 6906 | // FIXME: this could be improved probably. |
Chris Lattner | 85dd3be | 2007-10-15 17:48:57 +0000 | [diff] [blame] | 6907 | SDOperand Ptr = DAG.CreateStackTemporary(InOp.getValueType()); |
Dan Gohman | bbbbb9c | 2008-02-11 18:58:42 +0000 | [diff] [blame] | 6908 | FrameIndexSDNode *FI = cast<FrameIndexSDNode>(Ptr.Val); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6909 | |
Evan Cheng | 786225a | 2006-10-05 23:01:46 +0000 | [diff] [blame] | 6910 | SDOperand St = DAG.getStore(DAG.getEntryNode(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6911 | InOp, Ptr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 6912 | PseudoSourceValue::getFixedStack(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6913 | FI->getIndex()); |
| 6914 | InOp = DAG.getLoad(Op.getValueType(), St, Ptr, |
Dan Gohman | 3069b87 | 2008-02-07 18:41:25 +0000 | [diff] [blame] | 6915 | PseudoSourceValue::getFixedStack(), |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 6916 | FI->getIndex()); |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6917 | } |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6918 | // Split the vector and convert each of the pieces now. |
| 6919 | SplitVectorOp(InOp, Lo, Hi); |
Nate Begeman | 5db1afb | 2007-11-15 21:15:26 +0000 | [diff] [blame] | 6920 | Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo); |
| 6921 | Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi); |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6922 | break; |
Chris Lattner | 7692eb4 | 2006-03-23 21:16:34 +0000 | [diff] [blame] | 6923 | } |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6924 | } |
| 6925 | |
| 6926 | // Remember in a map if the values will be reused later. |
Chris Lattner | 40030bf | 2007-02-04 01:17:38 +0000 | [diff] [blame] | 6927 | bool isNew = |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6928 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
Dan Gohman | 10a7aa6 | 2007-06-29 00:09:08 +0000 | [diff] [blame] | 6929 | assert(isNew && "Value already split?!?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6930 | } |
| 6931 | |
| 6932 | |
Dan Gohman | 89b20c0 | 2007-06-27 14:06:22 +0000 | [diff] [blame] | 6933 | /// ScalarizeVectorOp - Given an operand of single-element vector type |
| 6934 | /// (e.g. v1f32), convert it into the equivalent operation that returns a |
| 6935 | /// scalar (e.g. f32) value. |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6936 | SDOperand SelectionDAGLegalize::ScalarizeVectorOp(SDOperand Op) { |
| 6937 | assert(MVT::isVector(Op.getValueType()) && |
| 6938 | "Bad ScalarizeVectorOp invocation!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6939 | SDNode *Node = Op.Val; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6940 | MVT::ValueType NewVT = MVT::getVectorElementType(Op.getValueType()); |
| 6941 | assert(MVT::getVectorNumElements(Op.getValueType()) == 1); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6942 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6943 | // See if we already scalarized it. |
| 6944 | std::map<SDOperand, SDOperand>::iterator I = ScalarizedNodes.find(Op); |
| 6945 | if (I != ScalarizedNodes.end()) return I->second; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6946 | |
| 6947 | SDOperand Result; |
| 6948 | switch (Node->getOpcode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 6949 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6950 | #ifndef NDEBUG |
Dan Gohman | 3983358 | 2007-06-04 16:17:33 +0000 | [diff] [blame] | 6951 | Node->dump(&DAG); cerr << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 6952 | #endif |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6953 | assert(0 && "Unknown vector operation in ScalarizeVectorOp!"); |
| 6954 | case ISD::ADD: |
| 6955 | case ISD::FADD: |
| 6956 | case ISD::SUB: |
| 6957 | case ISD::FSUB: |
| 6958 | case ISD::MUL: |
| 6959 | case ISD::FMUL: |
| 6960 | case ISD::SDIV: |
| 6961 | case ISD::UDIV: |
| 6962 | case ISD::FDIV: |
| 6963 | case ISD::SREM: |
| 6964 | case ISD::UREM: |
| 6965 | case ISD::FREM: |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 6966 | case ISD::FPOW: |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6967 | case ISD::AND: |
| 6968 | case ISD::OR: |
| 6969 | case ISD::XOR: |
| 6970 | Result = DAG.getNode(Node->getOpcode(), |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6971 | NewVT, |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6972 | ScalarizeVectorOp(Node->getOperand(0)), |
| 6973 | ScalarizeVectorOp(Node->getOperand(1))); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6974 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6975 | case ISD::FNEG: |
| 6976 | case ISD::FABS: |
| 6977 | case ISD::FSQRT: |
| 6978 | case ISD::FSIN: |
| 6979 | case ISD::FCOS: |
| 6980 | Result = DAG.getNode(Node->getOpcode(), |
| 6981 | NewVT, |
| 6982 | ScalarizeVectorOp(Node->getOperand(0))); |
| 6983 | break; |
Dan Gohman | 9e04c82 | 2007-10-12 14:13:46 +0000 | [diff] [blame] | 6984 | case ISD::FPOWI: |
| 6985 | Result = DAG.getNode(Node->getOpcode(), |
| 6986 | NewVT, |
| 6987 | ScalarizeVectorOp(Node->getOperand(0)), |
| 6988 | Node->getOperand(1)); |
| 6989 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6990 | case ISD::LOAD: { |
| 6991 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 6992 | SDOperand Ch = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 6993 | SDOperand Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 6994 | |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 6995 | const Value *SV = LD->getSrcValue(); |
| 6996 | int SVOffset = LD->getSrcValueOffset(); |
| 6997 | Result = DAG.getLoad(NewVT, Ch, Ptr, SV, SVOffset, |
| 6998 | LD->isVolatile(), LD->getAlignment()); |
| 6999 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 7000 | // Remember that we legalized the chain. |
| 7001 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 7002 | break; |
| 7003 | } |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7004 | case ISD::BUILD_VECTOR: |
| 7005 | Result = Node->getOperand(0); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 7006 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7007 | case ISD::INSERT_VECTOR_ELT: |
| 7008 | // Returning the inserted scalar element. |
| 7009 | Result = Node->getOperand(1); |
| 7010 | break; |
| 7011 | case ISD::CONCAT_VECTORS: |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 7012 | assert(Node->getOperand(0).getValueType() == NewVT && |
| 7013 | "Concat of non-legal vectors not yet supported!"); |
| 7014 | Result = Node->getOperand(0); |
| 7015 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7016 | case ISD::VECTOR_SHUFFLE: { |
| 7017 | // Figure out if the scalar is the LHS or RHS and return it. |
| 7018 | SDOperand EltNum = Node->getOperand(2).getOperand(0); |
| 7019 | if (cast<ConstantSDNode>(EltNum)->getValue()) |
| 7020 | Result = ScalarizeVectorOp(Node->getOperand(1)); |
| 7021 | else |
| 7022 | Result = ScalarizeVectorOp(Node->getOperand(0)); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 7023 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7024 | } |
| 7025 | case ISD::EXTRACT_SUBVECTOR: |
| 7026 | Result = Node->getOperand(0); |
Dan Gohman | 6595635 | 2007-06-13 15:12:02 +0000 | [diff] [blame] | 7027 | assert(Result.getValueType() == NewVT); |
| 7028 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7029 | case ISD::BIT_CONVERT: |
| 7030 | Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op.getOperand(0)); |
Chris Lattner | 5b2316e | 2006-03-28 20:24:43 +0000 | [diff] [blame] | 7031 | break; |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7032 | case ISD::SELECT: |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 7033 | Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0), |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7034 | ScalarizeVectorOp(Op.getOperand(1)), |
| 7035 | ScalarizeVectorOp(Op.getOperand(2))); |
Chris Lattner | b22e35a | 2006-04-08 22:22:57 +0000 | [diff] [blame] | 7036 | break; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
| 7039 | if (TLI.isTypeLegal(NewVT)) |
| 7040 | Result = LegalizeOp(Result); |
Dan Gohman | 7f32156 | 2007-06-25 16:23:39 +0000 | [diff] [blame] | 7041 | bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second; |
| 7042 | assert(isNew && "Value already scalarized?"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 7043 | return Result; |
| 7044 | } |
| 7045 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 7046 | |
| 7047 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 7048 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 7049 | void SelectionDAG::Legalize() { |
Chris Lattner | 5e46a19 | 2006-04-02 03:07:27 +0000 | [diff] [blame] | 7050 | if (ViewLegalizeDAGs) viewGraph(); |
| 7051 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 7052 | /// run - This is the main entry point to this class. |
| 7053 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 7054 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 7055 | } |
| 7056 | |