Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Jim Laskey | 02659d2 | 2005-08-17 17:42:52 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include <iostream> |
Chris Lattner | 82299e7 | 2005-08-05 18:10:27 +0000 | [diff] [blame] | 24 | #include <set> |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 29 | /// hacks on it until the target machine can handle it. This involves |
| 30 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 31 | /// large sizes or splitting up large values into small values) as well as |
| 32 | /// eliminating operations the machine cannot handle. |
| 33 | /// |
| 34 | /// This code also does a small amount of optimization and recognition of idioms |
| 35 | /// as part of its processing. For example, if a target does not support a |
| 36 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 37 | /// will attempt merge setcc and brc instructions into brcc's. |
| 38 | /// |
| 39 | namespace { |
| 40 | class SelectionDAGLegalize { |
| 41 | TargetLowering &TLI; |
| 42 | SelectionDAG &DAG; |
| 43 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 44 | // Libcall insertion helpers. |
| 45 | |
| 46 | /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been |
| 47 | /// legalized. We use this to ensure that calls are properly serialized |
| 48 | /// against each other, including inserted libcalls. |
| 49 | SDOperand LastCALLSEQ_END; |
| 50 | |
| 51 | /// IsLegalizingCall - This member is used *only* for purposes of providing |
| 52 | /// helpful assertions that a libcall isn't created while another call is |
| 53 | /// being legalized (which could lead to non-serialized call sequences). |
| 54 | bool IsLegalizingCall; |
| 55 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 56 | enum LegalizeAction { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 57 | Legal, // The target natively supports this operation. |
| 58 | Promote, // This operation should be executed in a larger type. |
| 59 | Expand, // Try to expand this to other ops, otherwise use a libcall. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 60 | }; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 62 | /// ValueTypeActions - This is a bitvector that contains two bits for each |
| 63 | /// value type, where the two bits correspond to the LegalizeAction enum. |
| 64 | /// This can be queried with "getTypeAction(VT)". |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 65 | TargetLowering::ValueTypeActionImpl ValueTypeActions; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 67 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 68 | /// than one use, this map indicates what regularized operand to use. This |
| 69 | /// allows us to avoid legalizing the same thing more than once. |
| 70 | std::map<SDOperand, SDOperand> LegalizedNodes; |
| 71 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 72 | /// PromotedNodes - For nodes that are below legal width, and that have more |
| 73 | /// than one use, this map indicates what promoted value to use. This allows |
| 74 | /// us to avoid promoting the same thing more than once. |
| 75 | std::map<SDOperand, SDOperand> PromotedNodes; |
| 76 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 77 | /// ExpandedNodes - For nodes that need to be expanded this map indicates |
| 78 | /// which which operands are the expanded version of the input. This allows |
| 79 | /// us to avoid expanding the same node more than once. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 80 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes; |
| 81 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 82 | /// SplitNodes - For vector nodes that need to be split, this map indicates |
| 83 | /// which which operands are the split version of the input. This allows us |
| 84 | /// to avoid splitting the same node more than once. |
| 85 | std::map<SDOperand, std::pair<SDOperand, SDOperand> > SplitNodes; |
| 86 | |
| 87 | /// PackedNodes - For nodes that need to be packed from MVT::Vector types to |
| 88 | /// concrete packed types, this contains the mapping of ones we have already |
| 89 | /// processed to the result. |
| 90 | std::map<SDOperand, SDOperand> PackedNodes; |
| 91 | |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 92 | void AddLegalizedOperand(SDOperand From, SDOperand To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 93 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 94 | // If someone requests legalization of the new node, return itself. |
| 95 | if (From != To) |
| 96 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 98 | void AddPromotedOperand(SDOperand From, SDOperand To) { |
| 99 | bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second; |
| 100 | assert(isNew && "Got into the map somehow?"); |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 101 | // If someone requests legalization of the new node, return itself. |
| 102 | LegalizedNodes.insert(std::make_pair(To, To)); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 103 | } |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 105 | public: |
| 106 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 107 | SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 109 | /// getTypeAction - Return how we should legalize values of this type, either |
| 110 | /// it is already legal or we need to expand it into multiple registers of |
| 111 | /// smaller integer type, or we need to promote it to a larger type. |
| 112 | LegalizeAction getTypeAction(MVT::ValueType VT) const { |
Chris Lattner | 68a17fe | 2006-01-29 08:42:06 +0000 | [diff] [blame] | 113 | return (LegalizeAction)ValueTypeActions.getTypeAction(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /// isTypeLegal - Return true if this type is legal on this target. |
| 117 | /// |
| 118 | bool isTypeLegal(MVT::ValueType VT) const { |
| 119 | return getTypeAction(VT) == Legal; |
| 120 | } |
| 121 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 122 | void LegalizeDAG(); |
| 123 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 124 | private: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 125 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 126 | /// appropriate for its type. |
| 127 | void HandleOp(SDOperand Op); |
| 128 | |
| 129 | /// LegalizeOp - We know that the specified value has a legal type. |
| 130 | /// Recursively ensure that the operands have legal types, then return the |
| 131 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 132 | SDOperand LegalizeOp(SDOperand O); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 133 | |
| 134 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 135 | /// promote it to compute the value into a larger type. The produced value |
| 136 | /// will have the correct bits for the low portion of the register, but no |
| 137 | /// guarantee is made about the top bits: it may be zero, sign-extended, or |
| 138 | /// garbage. |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 139 | SDOperand PromoteOp(SDOperand O); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 140 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 141 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 142 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, |
| 143 | /// the LegalizeNodes map is filled in for any results that are not expanded, |
| 144 | /// the ExpandedNodes map is filled in for any results that are expanded, and |
| 145 | /// the Lo/Hi values are returned. This applies to integer types and Vector |
| 146 | /// types. |
| 147 | void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 148 | |
| 149 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 150 | /// two smaller values of MVT::Vector type. |
| 151 | void SplitVectorOp(SDOperand O, SDOperand &Lo, SDOperand &Hi); |
| 152 | |
| 153 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 154 | /// equivalent operation that returns a packed value (e.g. MVT::V4F32). When |
| 155 | /// this is called, we know that PackedVT is the right type for the result and |
| 156 | /// we know that this type is legal for the target. |
| 157 | SDOperand PackVectorOp(SDOperand O, MVT::ValueType PackedVT); |
| 158 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 159 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest); |
| 160 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 161 | void LegalizeSetCCOperands(SDOperand &LHS, SDOperand &RHS, SDOperand &CC); |
| 162 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 163 | SDOperand ExpandLibCall(const char *Name, SDNode *Node, |
| 164 | SDOperand &Hi); |
| 165 | SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, |
| 166 | SDOperand Source); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 168 | SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp); |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 169 | SDOperand ExpandLegalINT_TO_FP(bool isSigned, |
| 170 | SDOperand LegalOp, |
| 171 | MVT::ValueType DestVT); |
Nate Begeman | 5a8441e | 2005-07-16 02:02:34 +0000 | [diff] [blame] | 172 | SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT, |
| 173 | bool isSigned); |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 174 | SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT, |
| 175 | bool isSigned); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 177 | SDOperand ExpandBSWAP(SDOperand Op); |
| 178 | SDOperand ExpandBitCount(unsigned Opc, SDOperand Op); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 179 | bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt, |
| 180 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 181 | void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt, |
| 182 | SDOperand &Lo, SDOperand &Hi); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 184 | SDOperand getIntPtrConstant(uint64_t Val) { |
| 185 | return DAG.getConstant(Val, TLI.getPointerTy()); |
| 186 | } |
| 187 | }; |
| 188 | } |
| 189 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 190 | /// getScalarizedOpcode - Return the scalar opcode that corresponds to the |
| 191 | /// specified vector opcode. |
Chris Lattner | b89175f | 2005-11-19 05:51:46 +0000 | [diff] [blame] | 192 | static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) { |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 193 | switch (VecOp) { |
| 194 | default: assert(0 && "Don't know how to scalarize this opcode!"); |
Evan Cheng | 3e1ce5a | 2006-03-03 07:01:07 +0000 | [diff] [blame] | 195 | case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD; |
| 196 | case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB; |
| 197 | case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL; |
| 198 | case ISD::VSDIV: return MVT::isInteger(VT) ? ISD::SDIV: ISD::FDIV; |
| 199 | case ISD::VUDIV: return MVT::isInteger(VT) ? ISD::UDIV: ISD::FDIV; |
| 200 | case ISD::VAND: return MVT::isInteger(VT) ? ISD::AND : 0; |
| 201 | case ISD::VOR: return MVT::isInteger(VT) ? ISD::OR : 0; |
| 202 | case ISD::VXOR: return MVT::isInteger(VT) ? ISD::XOR : 0; |
Nate Begeman | 5fbb5d2 | 2005-11-19 00:36:38 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 206 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
| 207 | : TLI(dag.getTargetLoweringInfo()), DAG(dag), |
| 208 | ValueTypeActions(TLI.getValueTypeActions()) { |
Nate Begeman | 6a64861 | 2005-11-29 05:45:29 +0000 | [diff] [blame] | 209 | assert(MVT::LAST_VALUETYPE <= 32 && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 210 | "Too many value types for ValueTypeActions to hold!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 213 | /// ComputeTopDownOrdering - Add the specified node to the Order list if it has |
| 214 | /// not been visited yet and if all of its operands have already been visited. |
| 215 | static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order, |
| 216 | std::map<SDNode*, unsigned> &Visited) { |
| 217 | if (++Visited[N] != N->getNumOperands()) |
| 218 | return; // Haven't visited all operands yet |
| 219 | |
| 220 | Order.push_back(N); |
| 221 | |
| 222 | if (N->hasOneUse()) { // Tail recurse in common case. |
| 223 | ComputeTopDownOrdering(*N->use_begin(), Order, Visited); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Now that we have N in, add anything that uses it if all of their operands |
| 228 | // are now done. |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 229 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI) |
| 230 | ComputeTopDownOrdering(*UI, Order, Visited); |
| 231 | } |
| 232 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 234 | void SelectionDAGLegalize::LegalizeDAG() { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 235 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 236 | IsLegalizingCall = false; |
| 237 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 238 | // The legalize process is inherently a bottom-up recursive process (users |
| 239 | // legalize their uses before themselves). Given infinite stack space, we |
| 240 | // could just start legalizing on the root and traverse the whole graph. In |
| 241 | // 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] | 242 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 243 | // node is only legalized after all of its operands are legalized. |
| 244 | std::map<SDNode*, unsigned> Visited; |
| 245 | std::vector<SDNode*> Order; |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 247 | // Compute ordering from all of the leaves in the graphs, those (like the |
| 248 | // entry node) that have no operands. |
| 249 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
| 250 | E = DAG.allnodes_end(); I != E; ++I) { |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 251 | if (I->getNumOperands() == 0) { |
| 252 | Visited[I] = 0 - 1U; |
| 253 | ComputeTopDownOrdering(I, Order, Visited); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | de202b3 | 2005-11-09 23:47:37 +0000 | [diff] [blame] | 257 | assert(Order.size() == Visited.size() && |
| 258 | Order.size() == |
| 259 | (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) && |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 260 | "Error: DAG is cyclic!"); |
| 261 | Visited.clear(); |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 262 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 263 | for (unsigned i = 0, e = Order.size(); i != e; ++i) |
| 264 | HandleOp(SDOperand(Order[i], 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 265 | |
| 266 | // Finally, it's possible the root changed. Get the new root. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 267 | SDOperand OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 268 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 269 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 270 | |
| 271 | ExpandedNodes.clear(); |
| 272 | LegalizedNodes.clear(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 273 | PromotedNodes.clear(); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 274 | SplitNodes.clear(); |
| 275 | PackedNodes.clear(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 276 | |
| 277 | // Remove dead nodes now. |
Chris Lattner | 62fd269 | 2005-01-07 21:09:37 +0000 | [diff] [blame] | 278 | DAG.RemoveDeadNodes(OldRoot.Val); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 281 | |
| 282 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 283 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
| 284 | static SDNode *FindCallEndFromCallStart(SDNode *Node) { |
| 285 | if (Node->getOpcode() == ISD::CALLSEQ_END) |
| 286 | return Node; |
| 287 | if (Node->use_empty()) |
| 288 | return 0; // No CallSeqEnd |
| 289 | |
| 290 | // The chain is usually at the end. |
| 291 | SDOperand TheChain(Node, Node->getNumValues()-1); |
| 292 | if (TheChain.getValueType() != MVT::Other) { |
| 293 | // Sometimes it's at the beginning. |
| 294 | TheChain = SDOperand(Node, 0); |
| 295 | if (TheChain.getValueType() != MVT::Other) { |
| 296 | // Otherwise, hunt for it. |
| 297 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
| 298 | if (Node->getValueType(i) == MVT::Other) { |
| 299 | TheChain = SDOperand(Node, i); |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | // Otherwise, we walked into a node without a chain. |
| 304 | if (TheChain.getValueType() != MVT::Other) |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 310 | E = Node->use_end(); UI != E; ++UI) { |
| 311 | |
| 312 | // Make sure to only follow users of our token chain. |
| 313 | SDNode *User = *UI; |
| 314 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 315 | if (User->getOperand(i) == TheChain) |
| 316 | if (SDNode *Result = FindCallEndFromCallStart(User)) |
| 317 | return Result; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
| 323 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 324 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
| 325 | assert(Node && "Didn't find callseq_start for a call??"); |
| 326 | if (Node->getOpcode() == ISD::CALLSEQ_START) return Node; |
| 327 | |
| 328 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 329 | "Node doesn't have a token chain argument!"); |
| 330 | return FindCallStartFromCallEnd(Node->getOperand(0).Val); |
| 331 | } |
| 332 | |
| 333 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
| 334 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
| 335 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
| 336 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, |
| 337 | SDNode *Dest) { |
| 338 | if (N == Dest) return true; // N certainly leads to Dest :) |
| 339 | |
| 340 | // If the first result of this node has been already legalized, then it cannot |
| 341 | // reach N. |
| 342 | switch (getTypeAction(N->getValueType(0))) { |
| 343 | case Legal: |
| 344 | if (LegalizedNodes.count(SDOperand(N, 0))) return false; |
| 345 | break; |
| 346 | case Promote: |
| 347 | if (PromotedNodes.count(SDOperand(N, 0))) return false; |
| 348 | break; |
| 349 | case Expand: |
| 350 | if (ExpandedNodes.count(SDOperand(N, 0))) return false; |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | // Okay, this node has not already been legalized. Check and legalize all |
| 355 | // operands. If none lead to Dest, then we can legalize this node. |
| 356 | bool OperandsLeadToDest = false; |
| 357 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 358 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
| 359 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).Val, Dest); |
| 360 | |
| 361 | if (OperandsLeadToDest) return true; |
| 362 | |
| 363 | // Okay, this node looks safe, legalize it and return false. |
| 364 | switch (getTypeAction(N->getValueType(0))) { |
| 365 | case Legal: |
| 366 | LegalizeOp(SDOperand(N, 0)); |
| 367 | break; |
| 368 | case Promote: |
| 369 | PromoteOp(SDOperand(N, 0)); |
| 370 | break; |
| 371 | case Expand: { |
| 372 | SDOperand X, Y; |
| 373 | ExpandOp(SDOperand(N, 0), X, Y); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | return false; |
| 378 | } |
| 379 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 380 | /// HandleOp - Legalize, Promote, Expand or Pack the specified operand as |
| 381 | /// appropriate for its type. |
| 382 | void SelectionDAGLegalize::HandleOp(SDOperand Op) { |
| 383 | switch (getTypeAction(Op.getValueType())) { |
| 384 | default: assert(0 && "Bad type action!"); |
| 385 | case Legal: LegalizeOp(Op); break; |
| 386 | case Promote: PromoteOp(Op); break; |
| 387 | case Expand: |
| 388 | if (Op.getValueType() != MVT::Vector) { |
| 389 | SDOperand X, Y; |
| 390 | ExpandOp(Op, X, Y); |
| 391 | } else { |
| 392 | SDNode *N = Op.Val; |
| 393 | unsigned NumOps = N->getNumOperands(); |
| 394 | unsigned NumElements = |
| 395 | cast<ConstantSDNode>(N->getOperand(NumOps-2))->getValue(); |
| 396 | MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(NumOps-1))->getVT(); |
| 397 | MVT::ValueType PackedVT = getVectorType(EVT, NumElements); |
| 398 | if (PackedVT != MVT::Other && TLI.isTypeLegal(PackedVT)) { |
| 399 | // In the common case, this is a legal vector type, convert it to the |
| 400 | // packed operation and type now. |
| 401 | PackVectorOp(Op, PackedVT); |
| 402 | } else if (NumElements == 1) { |
| 403 | // Otherwise, if this is a single element vector, convert it to a |
| 404 | // scalar operation. |
| 405 | PackVectorOp(Op, EVT); |
| 406 | } else { |
| 407 | // Otherwise, this is a multiple element vector that isn't supported. |
| 408 | // Split it in half and legalize both parts. |
| 409 | SDOperand X, Y; |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame^] | 410 | SplitVectorOp(Op, X, Y); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | break; |
| 414 | } |
| 415 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 416 | |
| 417 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 418 | /// LegalizeOp - We know that the specified value has a legal type. |
| 419 | /// Recursively ensure that the operands have legal types, then return the |
| 420 | /// result. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 421 | SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 422 | assert(isTypeLegal(Op.getValueType()) && |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 423 | "Caller should expand or promote operands that are not legal!"); |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 424 | SDNode *Node = Op.Val; |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 426 | // If this operation defines any values that cannot be represented in a |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 427 | // register on this target, make sure to expand or promote them. |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 428 | if (Node->getNumValues() > 1) { |
| 429 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 430 | if (getTypeAction(Node->getValueType(i)) != Legal) { |
| 431 | HandleOp(Op.getValue(i)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 432 | assert(LegalizedNodes.count(Op) && |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 433 | "Handling didn't add legal operands!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 434 | return LegalizedNodes[Op]; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 438 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 439 | // means that we always must cache transformed nodes. |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 440 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 441 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 442 | |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 443 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 444 | SDOperand Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 445 | bool isCustom = false; |
| 446 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 447 | switch (Node->getOpcode()) { |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 448 | case ISD::FrameIndex: |
| 449 | case ISD::EntryToken: |
| 450 | case ISD::Register: |
| 451 | case ISD::BasicBlock: |
| 452 | case ISD::TargetFrameIndex: |
| 453 | case ISD::TargetConstant: |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 454 | case ISD::TargetConstantFP: |
| 455 | case ISD::TargetConstantVec: |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 456 | case ISD::TargetConstantPool: |
| 457 | case ISD::TargetGlobalAddress: |
| 458 | case ISD::TargetExternalSymbol: |
| 459 | case ISD::VALUETYPE: |
| 460 | case ISD::SRCVALUE: |
| 461 | case ISD::STRING: |
| 462 | case ISD::CONDCODE: |
| 463 | // Primitives must all be legal. |
| 464 | assert(TLI.isOperationLegal(Node->getValueType(0), Node->getValueType(0)) && |
| 465 | "This must be legal!"); |
| 466 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 467 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 468 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
| 469 | // If this is a target node, legalize it by legalizing the operands then |
| 470 | // passing it through. |
| 471 | std::vector<SDOperand> Ops; |
| 472 | bool Changed = false; |
| 473 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 474 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 475 | Changed = Changed || Node->getOperand(i) != Ops.back(); |
| 476 | } |
| 477 | if (Changed) |
| 478 | if (Node->getNumValues() == 1) |
| 479 | Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops); |
| 480 | else { |
| 481 | std::vector<MVT::ValueType> VTs(Node->value_begin(), |
| 482 | Node->value_end()); |
| 483 | Result = DAG.getNode(Node->getOpcode(), VTs, Ops); |
| 484 | } |
| 485 | |
| 486 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 487 | AddLegalizedOperand(Op.getValue(i), Result.getValue(i)); |
| 488 | return Result.getValue(Op.ResNo); |
| 489 | } |
| 490 | // Otherwise this is an unhandled builtin node. splat. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 491 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 492 | assert(0 && "Do not know how to legalize this operator!"); |
| 493 | abort(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 494 | case ISD::GlobalAddress: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 495 | case ISD::ExternalSymbol: |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 496 | case ISD::ConstantPool: // Nothing to do. |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 497 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 498 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 499 | case TargetLowering::Custom: |
| 500 | Tmp1 = TLI.LowerOperation(Op, DAG); |
| 501 | if (Tmp1.Val) Result = Tmp1; |
| 502 | // 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] | 503 | case TargetLowering::Legal: |
Chris Lattner | 0c8fbe3 | 2005-11-17 06:41:44 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 506 | break; |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 507 | case ISD::AssertSext: |
| 508 | case ISD::AssertZext: |
| 509 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 510 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 08951a3 | 2005-09-02 01:15:01 +0000 | [diff] [blame] | 511 | break; |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 512 | case ISD::MERGE_VALUES: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 513 | // Legalize eliminates MERGE_VALUES nodes. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 514 | Result = Node->getOperand(Op.ResNo); |
| 515 | break; |
Chris Lattner | 69a5215 | 2005-01-14 22:38:01 +0000 | [diff] [blame] | 516 | case ISD::CopyFromReg: |
| 517 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 518 | Result = Op.getValue(0); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 519 | if (Node->getNumValues() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 520 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 521 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 522 | assert(Node->getNumValues() == 3 && "Invalid copyfromreg!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 523 | if (Node->getNumOperands() == 3) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 524 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 525 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
| 526 | } else { |
| 527 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
| 528 | } |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 529 | AddLegalizedOperand(Op.getValue(2), Result.getValue(2)); |
| 530 | } |
Chris Lattner | 13c184d | 2005-01-28 06:27:38 +0000 | [diff] [blame] | 531 | // Since CopyFromReg produces two values, make sure to remember that we |
| 532 | // legalized both of them. |
| 533 | AddLegalizedOperand(Op.getValue(0), Result); |
| 534 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 535 | return Result.getValue(Op.ResNo); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 536 | case ISD::UNDEF: { |
| 537 | MVT::ValueType VT = Op.getValueType(); |
| 538 | switch (TLI.getOperationAction(ISD::UNDEF, VT)) { |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 539 | default: assert(0 && "This action is not supported yet!"); |
| 540 | case TargetLowering::Expand: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 541 | if (MVT::isInteger(VT)) |
| 542 | Result = DAG.getConstant(0, VT); |
| 543 | else if (MVT::isFloatingPoint(VT)) |
| 544 | Result = DAG.getConstantFP(0, VT); |
| 545 | else |
| 546 | assert(0 && "Unknown value type!"); |
| 547 | break; |
Nate Begeman | ea19cd5 | 2005-04-02 00:41:14 +0000 | [diff] [blame] | 548 | case TargetLowering::Legal: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 549 | break; |
| 550 | } |
| 551 | break; |
| 552 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 553 | |
| 554 | case ISD::LOCATION: |
| 555 | assert(Node->getNumOperands() == 5 && "Invalid LOCATION node!"); |
| 556 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain. |
| 557 | |
| 558 | switch (TLI.getOperationAction(ISD::LOCATION, MVT::Other)) { |
| 559 | case TargetLowering::Promote: |
| 560 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 561 | case TargetLowering::Expand: { |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 562 | MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 563 | bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other); |
| 564 | bool useDEBUG_LABEL = TLI.isOperationLegal(ISD::DEBUG_LABEL, MVT::Other); |
| 565 | |
| 566 | if (DebugInfo && (useDEBUG_LOC || useDEBUG_LABEL)) { |
| 567 | const std::string &FName = |
| 568 | cast<StringSDNode>(Node->getOperand(3))->getValue(); |
| 569 | const std::string &DirName = |
| 570 | cast<StringSDNode>(Node->getOperand(4))->getValue(); |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 571 | unsigned SrcFile = DebugInfo->RecordSource(DirName, FName); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 572 | |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 573 | std::vector<SDOperand> Ops; |
| 574 | Ops.push_back(Tmp1); // chain |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 575 | SDOperand LineOp = Node->getOperand(1); |
| 576 | SDOperand ColOp = Node->getOperand(2); |
| 577 | |
| 578 | if (useDEBUG_LOC) { |
| 579 | Ops.push_back(LineOp); // line # |
| 580 | Ops.push_back(ColOp); // col # |
| 581 | Ops.push_back(DAG.getConstant(SrcFile, MVT::i32)); // source file id |
| 582 | Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops); |
| 583 | } else { |
Jim Laskey | d0e58e3 | 2006-02-15 19:34:44 +0000 | [diff] [blame] | 584 | unsigned Line = cast<ConstantSDNode>(LineOp)->getValue(); |
| 585 | unsigned Col = cast<ConstantSDNode>(ColOp)->getValue(); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 586 | unsigned ID = DebugInfo->RecordLabel(Line, Col, SrcFile); |
| 587 | Ops.push_back(DAG.getConstant(ID, MVT::i32)); |
| 588 | Result = DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops); |
| 589 | } |
Jim Laskey | e81aecb | 2005-12-21 20:51:37 +0000 | [diff] [blame] | 590 | } else { |
| 591 | Result = Tmp1; // chain |
| 592 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 593 | break; |
Chris Lattner | e773673 | 2005-12-18 23:54:29 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 595 | case TargetLowering::Legal: |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 596 | if (Tmp1 != Node->getOperand(0) || |
| 597 | getTypeAction(Node->getOperand(1).getValueType()) == Promote) { |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 598 | std::vector<SDOperand> Ops; |
| 599 | Ops.push_back(Tmp1); |
Chris Lattner | 9ad17c9 | 2005-12-01 18:21:35 +0000 | [diff] [blame] | 600 | if (getTypeAction(Node->getOperand(1).getValueType()) == Legal) { |
| 601 | Ops.push_back(Node->getOperand(1)); // line # must be legal. |
| 602 | Ops.push_back(Node->getOperand(2)); // col # must be legal. |
| 603 | } else { |
| 604 | // Otherwise promote them. |
| 605 | Ops.push_back(PromoteOp(Node->getOperand(1))); |
| 606 | Ops.push_back(PromoteOp(Node->getOperand(2))); |
| 607 | } |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 608 | Ops.push_back(Node->getOperand(3)); // filename must be legal. |
| 609 | Ops.push_back(Node->getOperand(4)); // working dir # must be legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 610 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 611 | } |
| 612 | break; |
| 613 | } |
| 614 | break; |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 615 | |
| 616 | case ISD::DEBUG_LOC: |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 617 | assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!"); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 618 | switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) { |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 619 | default: assert(0 && "This action is not supported yet!"); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 620 | case TargetLowering::Legal: |
| 621 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 622 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #. |
| 623 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #. |
| 624 | Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 625 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | break; |
| 629 | |
| 630 | case ISD::DEBUG_LABEL: |
| 631 | assert(Node->getNumOperands() == 2 && "Invalid DEBUG_LABEL node!"); |
| 632 | switch (TLI.getOperationAction(ISD::DEBUG_LABEL, MVT::Other)) { |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 633 | default: assert(0 && "This action is not supported yet!"); |
| 634 | case TargetLowering::Legal: |
| 635 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 636 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the label id. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 637 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Jim Laskey | f5395ce | 2005-12-16 22:45:29 +0000 | [diff] [blame] | 638 | break; |
| 639 | } |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 640 | break; |
Chris Lattner | 36ce691 | 2005-11-29 06:21:05 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 642 | case ISD::Constant: |
| 643 | // We know we don't need to expand constants here, constants only have one |
| 644 | // value and we check that it is fine above. |
| 645 | |
| 646 | // FIXME: Maybe we should handle things like targets that don't support full |
| 647 | // 32-bit immediates? |
| 648 | break; |
| 649 | case ISD::ConstantFP: { |
| 650 | // Spill FP immediates to the constant pool if the target cannot directly |
| 651 | // codegen them. Targets often have some immediate values that can be |
| 652 | // efficiently generated into an FP register without a load. We explicitly |
| 653 | // leave these constants as ConstantFP nodes for the target to deal with. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 654 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
| 655 | |
| 656 | // Check to see if this FP immediate is already legal. |
| 657 | bool isLegal = false; |
| 658 | for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(), |
| 659 | E = TLI.legal_fpimm_end(); I != E; ++I) |
| 660 | if (CFP->isExactlyValue(*I)) { |
| 661 | isLegal = true; |
| 662 | break; |
| 663 | } |
| 664 | |
Chris Lattner | 3181a77 | 2006-01-29 06:26:56 +0000 | [diff] [blame] | 665 | // If this is a legal constant, turn it into a TargetConstantFP node. |
| 666 | if (isLegal) { |
| 667 | Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0)); |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) { |
| 672 | default: assert(0 && "This action is not supported yet!"); |
| 673 | case TargetLowering::Custom: |
| 674 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 675 | if (Tmp3.Val) { |
| 676 | Result = Tmp3; |
| 677 | break; |
| 678 | } |
| 679 | // FALLTHROUGH |
| 680 | case TargetLowering::Expand: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 681 | // Otherwise we need to spill the constant to memory. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 682 | bool Extend = false; |
| 683 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 684 | // If a FP immediate is precise when represented as a float and if the |
| 685 | // target can do an extending load from float to double, we put it into |
| 686 | // the constant pool as a float, even if it's is statically typed as a |
| 687 | // double. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 688 | MVT::ValueType VT = CFP->getValueType(0); |
| 689 | bool isDouble = VT == MVT::f64; |
| 690 | ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : |
| 691 | Type::FloatTy, CFP->getValue()); |
Chris Lattner | 99939d3 | 2005-01-28 22:58:25 +0000 | [diff] [blame] | 692 | if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) && |
| 693 | // Only do this if the target has a native EXTLOAD instruction from |
| 694 | // f32. |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 695 | TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 696 | LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy)); |
| 697 | VT = MVT::f32; |
| 698 | Extend = true; |
| 699 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 701 | SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 702 | if (Extend) { |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 703 | Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 704 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 705 | } else { |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 706 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 707 | DAG.getSrcValue(NULL)); |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 708 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 709 | } |
| 710 | break; |
| 711 | } |
Chris Lattner | 8ca05e0 | 2006-01-29 06:34:16 +0000 | [diff] [blame] | 712 | case ISD::ConstantVec: |
| 713 | switch (TLI.getOperationAction(ISD::ConstantVec, Node->getValueType(0))) { |
| 714 | default: assert(0 && "This action is not supported yet!"); |
| 715 | case TargetLowering::Custom: |
| 716 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 717 | if (Tmp3.Val) { |
| 718 | Result = Tmp3; |
| 719 | break; |
| 720 | } |
| 721 | // FALLTHROUGH |
| 722 | case TargetLowering::Expand: |
| 723 | // We assume that vector constants are not legal, and will be immediately |
| 724 | // spilled to the constant pool. |
| 725 | // |
| 726 | // Create a ConstantPacked, and put it in the constant pool. |
| 727 | MVT::ValueType VT = Node->getValueType(0); |
| 728 | const Type *OpNTy = |
| 729 | MVT::getTypeForValueType(Node->getOperand(0).getValueType()); |
| 730 | std::vector<Constant*> CV; |
| 731 | if (MVT::isFloatingPoint(VT)) { |
| 732 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 733 | double V = cast<ConstantFPSDNode>(Node->getOperand(i))->getValue(); |
| 734 | CV.push_back(ConstantFP::get(OpNTy, V)); |
| 735 | } |
| 736 | } else { |
| 737 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 738 | uint64_t V = cast<ConstantSDNode>(Node->getOperand(i))->getValue(); |
| 739 | CV.push_back(ConstantUInt::get(OpNTy, V)); |
| 740 | } |
| 741 | } |
| 742 | Constant *CP = ConstantPacked::get(CV); |
| 743 | SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
| 744 | Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, |
| 745 | DAG.getSrcValue(NULL)); |
| 746 | break; |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 747 | } |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 748 | break; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 749 | case ISD::TokenFactor: |
| 750 | if (Node->getNumOperands() == 2) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 751 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 752 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 753 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 754 | } else if (Node->getNumOperands() == 3) { |
| 755 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 756 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 757 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 758 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 759 | } else { |
| 760 | std::vector<SDOperand> Ops; |
Chris Lattner | 040c11c | 2005-11-09 18:48:57 +0000 | [diff] [blame] | 761 | // Legalize the operands. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 762 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 763 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 764 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 765 | } |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 766 | break; |
Chris Lattner | a385e9b | 2005-01-13 17:59:25 +0000 | [diff] [blame] | 767 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 768 | case ISD::CALLSEQ_START: { |
| 769 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
| 770 | |
| 771 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 772 | // to this call start. This ensures that any libcalls that need be inserted |
| 773 | // are inserted *before* the CALLSEQ_START. |
| 774 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
| 775 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).Val, Node); |
| 776 | |
| 777 | // Now that we legalized all of the inputs (which may have inserted |
| 778 | // libcalls) create the new CALLSEQ_START node. |
| 779 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 780 | |
| 781 | // Merge in the last call, to ensure that this call start after the last |
| 782 | // call ended. |
| 783 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 784 | Tmp1 = LegalizeOp(Tmp1); |
| 785 | |
| 786 | // Do not try to legalize the target-specific arguments (#1+). |
| 787 | if (Tmp1 != Node->getOperand(0)) { |
| 788 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 789 | Ops[0] = Tmp1; |
| 790 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 791 | } |
| 792 | |
| 793 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 794 | AddLegalizedOperand(Op.getValue(0), Result); |
| 795 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 796 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
| 797 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 798 | // Now that the callseq_start and all of the non-call nodes above this call |
| 799 | // sequence have been legalized, legalize the call itself. During this |
| 800 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 801 | // can overlap. |
| 802 | assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!"); |
| 803 | SDOperand InCallSEQ = LastCALLSEQ_END; |
| 804 | // Note that we are selecting this call! |
| 805 | LastCALLSEQ_END = SDOperand(CallEnd, 0); |
| 806 | IsLegalizingCall = true; |
| 807 | |
| 808 | // Legalize the call, starting from the CALLSEQ_END. |
| 809 | LegalizeOp(LastCALLSEQ_END); |
| 810 | assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!"); |
| 811 | return Result; |
| 812 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 813 | case ISD::CALLSEQ_END: |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 814 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. This |
| 815 | // will cause this node to be legalized as well as handling libcalls right. |
| 816 | if (LastCALLSEQ_END.Val != Node) { |
| 817 | LegalizeOp(SDOperand(FindCallStartFromCallEnd(Node), 0)); |
| 818 | std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op); |
| 819 | assert(I != LegalizedNodes.end() && |
| 820 | "Legalizing the call start should have legalized this node!"); |
| 821 | return I->second; |
| 822 | } |
| 823 | |
| 824 | // Otherwise, the call start has been legalized and everything is going |
| 825 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 826 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 827 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 828 | // an optional flag input. |
| 829 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){ |
| 830 | if (Tmp1 != Node->getOperand(0)) { |
| 831 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 832 | Ops[0] = Tmp1; |
| 833 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 834 | } |
| 835 | } else { |
| 836 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 837 | if (Tmp1 != Node->getOperand(0) || |
| 838 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
| 839 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 840 | Ops[0] = Tmp1; |
| 841 | Ops.back() = Tmp2; |
| 842 | Result = DAG.UpdateNodeOperands(Result, Ops); |
| 843 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 844 | } |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 845 | assert(IsLegalizingCall && "Call sequence imbalance between start/end?"); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 846 | // This finishes up call legalization. |
| 847 | IsLegalizingCall = false; |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 848 | |
| 849 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
| 850 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 851 | if (Node->getNumValues() == 2) |
| 852 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 853 | return Result.getValue(Op.ResNo); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 854 | case ISD::DYNAMIC_STACKALLOC: { |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 855 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 856 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size. |
| 857 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 858 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | fa404e8 | 2005-01-09 19:03:49 +0000 | [diff] [blame] | 859 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 860 | Tmp1 = Result.getValue(0); |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 861 | Tmp2 = Result.getValue(1); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 862 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 863 | Node->getValueType(0))) { |
| 864 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 865 | case TargetLowering::Expand: { |
| 866 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 867 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 868 | " not tell us which reg is the stack pointer!"); |
| 869 | SDOperand Chain = Tmp1.getOperand(0); |
| 870 | SDOperand Size = Tmp2.getOperand(1); |
| 871 | SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0)); |
| 872 | Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value |
| 873 | Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 874 | Tmp1 = LegalizeOp(Tmp1); |
| 875 | Tmp2 = LegalizeOp(Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 876 | break; |
| 877 | } |
| 878 | case TargetLowering::Custom: |
Chris Lattner | 5f65229 | 2006-01-15 08:43:08 +0000 | [diff] [blame] | 879 | Tmp3 = TLI.LowerOperation(Tmp1, DAG); |
| 880 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 881 | Tmp1 = LegalizeOp(Tmp3); |
| 882 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 883 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 884 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 885 | case TargetLowering::Legal: |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 886 | break; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 887 | } |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 888 | // Since this op produce two values, make sure to remember that we |
| 889 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 890 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 891 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
Chris Lattner | 903d278 | 2006-01-15 08:54:32 +0000 | [diff] [blame] | 892 | return Op.ResNo ? Tmp2 : Tmp1; |
Evan Cheng | a7dce3c | 2006-01-11 22:14:47 +0000 | [diff] [blame] | 893 | } |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 894 | case ISD::INLINEASM: |
| 895 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain. |
| 896 | Tmp2 = Node->getOperand(Node->getNumOperands()-1); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 897 | if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists. |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 898 | Tmp2 = Tmp3 = SDOperand(0, 0); |
| 899 | else |
| 900 | Tmp3 = LegalizeOp(Tmp2); |
| 901 | |
| 902 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) { |
| 903 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()); |
| 904 | Ops[0] = Tmp1; |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 905 | if (Tmp3.Val) Ops.back() = Tmp3; |
| 906 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | // 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] | 910 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | ce7518c | 2006-01-26 22:24:51 +0000 | [diff] [blame] | 911 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 912 | return Result.getValue(Op.ResNo); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 913 | case ISD::BR: |
| 914 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 915 | // Ensure that libcalls are emitted before a branch. |
| 916 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 917 | Tmp1 = LegalizeOp(Tmp1); |
| 918 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 919 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 920 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | c7af179 | 2005-01-07 22:12:08 +0000 | [diff] [blame] | 921 | break; |
| 922 | |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 923 | case ISD::BRCOND: |
| 924 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 925 | // Ensure that libcalls are emitted before a return. |
| 926 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 927 | Tmp1 = LegalizeOp(Tmp1); |
| 928 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 929 | |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 930 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 931 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 932 | case Legal: |
| 933 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition. |
| 934 | break; |
| 935 | case Promote: |
| 936 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition. |
| 937 | break; |
| 938 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 939 | |
| 940 | // Basic block destination (Op#2) is always legal. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 941 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 942 | |
| 943 | switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) { |
| 944 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 945 | case TargetLowering::Legal: break; |
| 946 | case TargetLowering::Custom: |
| 947 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 948 | if (Tmp1.Val) Result = Tmp1; |
| 949 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 950 | case TargetLowering::Expand: |
| 951 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 952 | // Node. |
| 953 | if (Tmp2.getOpcode() == ISD::SETCC) { |
| 954 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2), |
| 955 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 956 | Node->getOperand(2)); |
| 957 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 958 | // Make sure the condition is either zero or one. It may have been |
| 959 | // promoted from something else. |
Chris Lattner | 19c5c4c | 2006-01-31 05:04:52 +0000 | [diff] [blame] | 960 | unsigned NumBits = MVT::getSizeInBits(Tmp2.getValueType()); |
| 961 | if (!TLI.MaskedValueIsZero(Tmp2, (~0ULL >> (64-NumBits))^1)) |
| 962 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1); |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 963 | |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 964 | Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, |
| 965 | DAG.getCondCode(ISD::SETNE), Tmp2, |
| 966 | DAG.getConstant(0, Tmp2.getValueType()), |
| 967 | Node->getOperand(2)); |
| 968 | } |
| 969 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 970 | } |
| 971 | break; |
| 972 | case ISD::BR_CC: |
| 973 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 974 | // Ensure that libcalls are emitted before a branch. |
| 975 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 976 | Tmp1 = LegalizeOp(Tmp1); |
| 977 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 978 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 979 | Tmp2 = Node->getOperand(2); // LHS |
| 980 | Tmp3 = Node->getOperand(3); // RHS |
| 981 | Tmp4 = Node->getOperand(1); // CC |
| 982 | |
| 983 | LegalizeSetCCOperands(Tmp2, Tmp3, Tmp4); |
| 984 | |
| 985 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 986 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 987 | // the result against zero to select between true and false values. |
| 988 | if (Tmp3.Val == 0) { |
| 989 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 990 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 991 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 992 | |
| 993 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3, |
| 994 | Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 996 | switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) { |
| 997 | default: assert(0 && "Unexpected action for BR_CC!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 998 | case TargetLowering::Legal: break; |
| 999 | case TargetLowering::Custom: |
| 1000 | Tmp4 = TLI.LowerOperation(Result, DAG); |
| 1001 | if (Tmp4.Val) Result = Tmp4; |
Chris Lattner | 181b7a3 | 2005-12-17 23:46:46 +0000 | [diff] [blame] | 1002 | break; |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1003 | } |
Chris Lattner | c18ae4c | 2005-01-07 08:19:42 +0000 | [diff] [blame] | 1004 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1005 | case ISD::LOAD: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1006 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1007 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1008 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1009 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1010 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1011 | Tmp2 = Result.getValue(0); |
| 1012 | Tmp3 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1013 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1014 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
| 1015 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1016 | case TargetLowering::Legal: break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1017 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1018 | Tmp1 = TLI.LowerOperation(Tmp2, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1019 | if (Tmp1.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1020 | Tmp2 = LegalizeOp(Tmp1); |
| 1021 | Tmp3 = LegalizeOp(Tmp1.getValue(1)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1022 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1023 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1024 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1025 | // Since loads produce two values, make sure to remember that we |
| 1026 | // legalized both of them. |
| 1027 | AddLegalizedOperand(SDOperand(Node, 0), Tmp2); |
| 1028 | AddLegalizedOperand(SDOperand(Node, 1), Tmp3); |
| 1029 | return Op.ResNo ? Tmp3 : Tmp2; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1030 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1031 | case ISD::EXTLOAD: |
| 1032 | case ISD::SEXTLOAD: |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1033 | case ISD::ZEXTLOAD: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1034 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1035 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1037 | MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1038 | switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) { |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1039 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 1c51c6a | 2005-04-12 20:30:10 +0000 | [diff] [blame] | 1040 | case TargetLowering::Promote: |
| 1041 | assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1042 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1043 | DAG.getValueType(MVT::i8)); |
| 1044 | Tmp1 = Result.getValue(0); |
| 1045 | Tmp2 = Result.getValue(1); |
| 1046 | break; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1047 | case TargetLowering::Custom: |
| 1048 | isCustom = true; |
| 1049 | // FALLTHROUGH |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1050 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1051 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2), |
| 1052 | Node->getOperand(3)); |
| 1053 | Tmp1 = Result.getValue(0); |
| 1054 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (isCustom) { |
| 1057 | Tmp3 = TLI.LowerOperation(Tmp3, DAG); |
| 1058 | if (Tmp3.Val) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1059 | Tmp1 = LegalizeOp(Tmp3); |
| 1060 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1063 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1064 | case TargetLowering::Expand: |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 1065 | // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1066 | if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) { |
| 1067 | SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2)); |
Andrew Lenharth | 3155908 | 2005-06-30 19:32:57 +0000 | [diff] [blame] | 1068 | Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1069 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1070 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1071 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1072 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1073 | assert(Node->getOpcode() != ISD::EXTLOAD && |
| 1074 | "EXTLOAD should always be supported!"); |
| 1075 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1076 | // zero/sign extend inreg. |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 1077 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 1078 | Tmp1, Tmp2, Node->getOperand(2), SrcVT); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1079 | SDOperand ValRes; |
| 1080 | if (Node->getOpcode() == ISD::SEXTLOAD) |
| 1081 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 1082 | Result, DAG.getValueType(SrcVT)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 1083 | else |
| 1084 | ValRes = DAG.getZeroExtendInReg(Result, SrcVT); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1085 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1086 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1087 | break; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1088 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1089 | // Since loads produce two values, make sure to remember that we legalized |
| 1090 | // both of them. |
| 1091 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1092 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1093 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1094 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1095 | case ISD::EXTRACT_ELEMENT: { |
| 1096 | MVT::ValueType OpTy = Node->getOperand(0).getValueType(); |
| 1097 | switch (getTypeAction(OpTy)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1098 | default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!"); |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1099 | case Legal: |
| 1100 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) { |
| 1101 | // 1 -> Hi |
| 1102 | Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0), |
| 1103 | DAG.getConstant(MVT::getSizeInBits(OpTy)/2, |
| 1104 | TLI.getShiftAmountTy())); |
| 1105 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result); |
| 1106 | } else { |
| 1107 | // 0 -> Lo |
| 1108 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), |
| 1109 | Node->getOperand(0)); |
| 1110 | } |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1111 | break; |
| 1112 | case Expand: |
| 1113 | // Get both the low and high parts. |
| 1114 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 1115 | if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) |
| 1116 | Result = Tmp2; // 1 -> Hi |
| 1117 | else |
| 1118 | Result = Tmp1; // 0 -> Lo |
| 1119 | break; |
| 1120 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1121 | break; |
Nate Begeman | 5dc897b | 2005-10-19 00:06:56 +0000 | [diff] [blame] | 1122 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1123 | |
| 1124 | case ISD::CopyToReg: |
| 1125 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1126 | |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 1127 | assert(isTypeLegal(Node->getOperand(2).getValueType()) && |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1128 | "Register type must be legal!"); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1129 | // Legalize the incoming value (must be a legal type). |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 1130 | Tmp2 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1131 | if (Node->getNumValues() == 1) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1132 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1133 | } else { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1134 | assert(Node->getNumValues() == 2 && "Unknown CopyToReg"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1135 | if (Node->getNumOperands() == 4) { |
Chris Lattner | f1a47c3 | 2005-12-18 15:36:21 +0000 | [diff] [blame] | 1136 | Tmp3 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1137 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2, |
| 1138 | Tmp3); |
| 1139 | } else { |
| 1140 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // Since this produces two values, make sure to remember that we legalized |
| 1144 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1145 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1146 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1147 | return Result; |
Chris Lattner | 7310fb1 | 2005-12-18 15:27:43 +0000 | [diff] [blame] | 1148 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | |
| 1151 | case ISD::RET: |
| 1152 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Ensure that libcalls are emitted before a return. |
| 1155 | Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END); |
| 1156 | Tmp1 = LegalizeOp(Tmp1); |
| 1157 | LastCALLSEQ_END = DAG.getEntryNode(); |
| 1158 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1159 | switch (Node->getNumOperands()) { |
| 1160 | case 2: // ret val |
| 1161 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1162 | case Legal: |
| 1163 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1164 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | case Expand: { |
| 1167 | SDOperand Lo, Hi; |
| 1168 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 1169 | Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1170 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1171 | } |
| 1172 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1173 | Tmp2 = PromoteOp(Node->getOperand(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1174 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1175 | Result = LegalizeOp(Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1176 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1177 | } |
| 1178 | break; |
| 1179 | case 1: // ret void |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1180 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | default: { // ret <values> |
| 1183 | std::vector<SDOperand> NewValues; |
| 1184 | NewValues.push_back(Tmp1); |
| 1185 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1186 | switch (getTypeAction(Node->getOperand(i).getValueType())) { |
| 1187 | case Legal: |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 1188 | NewValues.push_back(LegalizeOp(Node->getOperand(i))); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1189 | break; |
| 1190 | case Expand: { |
| 1191 | SDOperand Lo, Hi; |
| 1192 | ExpandOp(Node->getOperand(i), Lo, Hi); |
| 1193 | NewValues.push_back(Lo); |
| 1194 | NewValues.push_back(Hi); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1195 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1196 | } |
| 1197 | case Promote: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 1198 | assert(0 && "Can't promote multiple return value yet!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1199 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (NewValues.size() == Node->getNumOperands()) |
| 1202 | Result = DAG.UpdateNodeOperands(Result, NewValues); |
| 1203 | else |
| 1204 | Result = DAG.getNode(ISD::RET, MVT::Other, NewValues); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | } |
| 1207 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 6862dbc | 2006-01-29 21:02:23 +0000 | [diff] [blame] | 1209 | if (Result.getOpcode() == ISD::RET) { |
| 1210 | switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) { |
| 1211 | default: assert(0 && "This action is not supported yet!"); |
| 1212 | case TargetLowering::Legal: break; |
| 1213 | case TargetLowering::Custom: |
| 1214 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1215 | if (Tmp1.Val) Result = Tmp1; |
| 1216 | break; |
| 1217 | } |
Evan Cheng | 17c428e | 2006-01-06 00:41:43 +0000 | [diff] [blame] | 1218 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1219 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1220 | case ISD::STORE: { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1221 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1222 | Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1223 | |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1224 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1225 | // FIXME: We shouldn't do this for TargetConstantFP's. |
Chris Lattner | 06ac6ab | 2006-03-15 22:19:18 +0000 | [diff] [blame] | 1226 | // FIXME: move this to the DAG Combiner! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1227 | if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){ |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1228 | if (CFP->getValueType(0) == MVT::f32) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1229 | Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1230 | } else { |
| 1231 | assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1232 | Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64); |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1233 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1234 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
| 1235 | Node->getOperand(3)); |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1236 | break; |
Chris Lattner | 5d2c6c7 | 2005-01-08 06:25:56 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1239 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1240 | case Legal: { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1241 | Tmp3 = LegalizeOp(Node->getOperand(1)); |
| 1242 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1243 | Node->getOperand(3)); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1245 | MVT::ValueType VT = Tmp3.getValueType(); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1246 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
| 1247 | default: assert(0 && "This action is not supported yet!"); |
| 1248 | case TargetLowering::Legal: break; |
| 1249 | case TargetLowering::Custom: |
| 1250 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1251 | if (Tmp1.Val) Result = Tmp1; |
| 1252 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1253 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | } |
| 1256 | case Promote: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1257 | // Truncate the value and store the result. |
| 1258 | Tmp3 = PromoteOp(Node->getOperand(1)); |
| 1259 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1260 | Node->getOperand(3), |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 1261 | DAG.getValueType(Node->getOperand(1).getValueType())); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1264 | case Expand: |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1265 | unsigned IncrementSize = 0; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1266 | SDOperand Lo, Hi; |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 1267 | |
| 1268 | // If this is a vector type, then we have to calculate the increment as |
| 1269 | // the product of the element size in bytes, and the number of elements |
| 1270 | // in the high half of the vector. |
| 1271 | if (Node->getOperand(1).getValueType() == MVT::Vector) { |
| 1272 | SDNode *InVal = Node->getOperand(1).Val; |
| 1273 | unsigned NumElems = |
| 1274 | cast<ConstantSDNode>(*(InVal->op_end()-2))->getValue(); |
| 1275 | MVT::ValueType EVT = cast<VTSDNode>(*(InVal->op_end()-1))->getVT(); |
| 1276 | |
| 1277 | // Figure out if there is a Packed type corresponding to this Vector |
| 1278 | // type. If so, convert to the packed type. |
| 1279 | MVT::ValueType TVT = MVT::getVectorType(EVT, NumElems); |
| 1280 | if (TVT != MVT::Other && TLI.isTypeLegal(TVT)) { |
| 1281 | // Turn this into a normal store of the packed type. |
| 1282 | Tmp3 = PackVectorOp(Node->getOperand(1), TVT); |
| 1283 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1284 | Node->getOperand(3)); |
| 1285 | break; |
| 1286 | } else if (NumElems == 1) { |
| 1287 | // Turn this into a normal store of the scalar type. |
| 1288 | Tmp3 = PackVectorOp(Node->getOperand(1), EVT); |
| 1289 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2, |
| 1290 | Node->getOperand(3)); |
| 1291 | break; |
| 1292 | } else { |
| 1293 | SplitVectorOp(Node->getOperand(1), Lo, Hi); |
| 1294 | IncrementSize = NumElems/2 * MVT::getSizeInBits(EVT)/8; |
| 1295 | } |
| 1296 | } else { |
| 1297 | ExpandOp(Node->getOperand(1), Lo, Hi); |
| 1298 | IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8; |
| 1299 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (!TLI.isLittleEndian()) |
| 1302 | std::swap(Lo, Hi); |
| 1303 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1304 | Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2, |
| 1305 | Node->getOperand(3)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1306 | Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2, |
| 1307 | getIntPtrConstant(IncrementSize)); |
| 1308 | assert(isTypeLegal(Tmp2.getValueType()) && |
| 1309 | "Pointers must be legal!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1310 | // FIXME: This sets the srcvalue of both halves to be the same, which is |
| 1311 | // wrong. |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 1312 | Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, |
| 1313 | Node->getOperand(3)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 1314 | Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi); |
| 1315 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1316 | } |
| 1317 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1318 | } |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1319 | case ISD::PCMARKER: |
| 1320 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1321 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Andrew Lenharth | 9576212 | 2005-03-31 21:24:06 +0000 | [diff] [blame] | 1322 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1323 | case ISD::STACKSAVE: |
| 1324 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1325 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 1326 | Tmp1 = Result.getValue(0); |
| 1327 | Tmp2 = Result.getValue(1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1328 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1329 | switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) { |
| 1330 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1331 | case TargetLowering::Legal: break; |
| 1332 | case TargetLowering::Custom: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1333 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1334 | if (Tmp3.Val) { |
| 1335 | Tmp1 = LegalizeOp(Tmp3); |
| 1336 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1337 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1338 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1339 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1340 | // Expand to CopyFromReg if the target set |
| 1341 | // StackPointerRegisterToSaveRestore. |
| 1342 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1343 | Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP, |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1344 | Node->getValueType(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1345 | Tmp2 = Tmp1.getValue(1); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1346 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1347 | Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0)); |
| 1348 | Tmp2 = Node->getOperand(0); |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1349 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1350 | break; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1351 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Since stacksave produce two values, make sure to remember that we |
| 1354 | // legalized both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1355 | AddLegalizedOperand(SDOperand(Node, 0), Tmp1); |
| 1356 | AddLegalizedOperand(SDOperand(Node, 1), Tmp2); |
| 1357 | return Op.ResNo ? Tmp2 : Tmp1; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1359 | case ISD::STACKRESTORE: |
| 1360 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1361 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1362 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1363 | |
| 1364 | switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) { |
| 1365 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1366 | case TargetLowering::Legal: break; |
| 1367 | case TargetLowering::Custom: |
| 1368 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1369 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | case TargetLowering::Expand: |
Chris Lattner | 4f0d8e4 | 2006-01-13 17:48:44 +0000 | [diff] [blame] | 1372 | // Expand to CopyToReg if the target set |
| 1373 | // StackPointerRegisterToSaveRestore. |
| 1374 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 1375 | Result = DAG.getCopyToReg(Tmp1, SP, Tmp2); |
| 1376 | } else { |
| 1377 | Result = Tmp1; |
| 1378 | } |
Chris Lattner | 140d53c | 2006-01-13 02:50:02 +0000 | [diff] [blame] | 1379 | break; |
| 1380 | } |
| 1381 | break; |
| 1382 | |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 1383 | case ISD::READCYCLECOUNTER: |
| 1384 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1385 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Since rdcc produce two values, make sure to remember that we legalized |
| 1388 | // both of them. |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1389 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
Andrew Lenharth | 49c709f | 2005-12-02 04:56:24 +0000 | [diff] [blame] | 1390 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1391 | return Result; |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 1392 | |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1393 | case ISD::TRUNCSTORE: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1394 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1395 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer. |
| 1396 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1397 | assert(isTypeLegal(Node->getOperand(1).getValueType()) && |
| 1398 | "Cannot handle illegal TRUNCSTORE yet!"); |
| 1399 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1400 | |
| 1401 | // The only promote case we handle is TRUNCSTORE:i1 X into |
| 1402 | // -> TRUNCSTORE:i8 (and X, 1) |
| 1403 | if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 && |
| 1404 | TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) == |
| 1405 | TargetLowering::Promote) { |
| 1406 | // Promote the bool to a mask then store. |
| 1407 | Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2, |
| 1408 | DAG.getConstant(1, Tmp2.getValueType())); |
| 1409 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3, |
| 1410 | Node->getOperand(3), DAG.getValueType(MVT::i8)); |
Chris Lattner | 13d58e7 | 2005-09-10 00:20:18 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1412 | } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || |
| 1413 | Tmp3 != Node->getOperand(2)) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1414 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 1415 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1416 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1418 | MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT(); |
| 1419 | switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) { |
| 1420 | default: assert(0 && "This action is not supported yet!"); |
| 1421 | case TargetLowering::Legal: break; |
| 1422 | case TargetLowering::Custom: |
| 1423 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1424 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1425 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 1426 | } |
| 1427 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1428 | } |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1429 | case ISD::SELECT: |
Chris Lattner | 47e9223 | 2005-01-18 19:27:06 +0000 | [diff] [blame] | 1430 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 1431 | case Expand: assert(0 && "It's impossible to expand bools"); |
| 1432 | case Legal: |
| 1433 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition. |
| 1434 | break; |
| 1435 | case Promote: |
| 1436 | Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition. |
| 1437 | break; |
| 1438 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1439 | Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal |
Chris Lattner | 2ee743f | 2005-01-14 22:08:15 +0000 | [diff] [blame] | 1440 | Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1441 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1442 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1443 | |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1444 | switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) { |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1445 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1446 | case TargetLowering::Legal: break; |
| 1447 | case TargetLowering::Custom: { |
| 1448 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1449 | if (Tmp1.Val) Result = Tmp1; |
| 1450 | break; |
| 1451 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1452 | case TargetLowering::Expand: |
| 1453 | if (Tmp1.getOpcode() == ISD::SETCC) { |
| 1454 | Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 1455 | Tmp2, Tmp3, |
| 1456 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
| 1457 | } else { |
Chris Lattner | 550b1e5 | 2005-08-21 18:03:09 +0000 | [diff] [blame] | 1458 | // Make sure the condition is either zero or one. It may have been |
| 1459 | // promoted from something else. |
Chris Lattner | 0e753d6 | 2006-01-30 04:22:28 +0000 | [diff] [blame] | 1460 | unsigned NumBits = MVT::getSizeInBits(Tmp1.getValueType()); |
| 1461 | if (!TLI.MaskedValueIsZero(Tmp1, (~0ULL >> (64-NumBits))^1)) |
| 1462 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1463 | Result = DAG.getSelectCC(Tmp1, |
| 1464 | DAG.getConstant(0, Tmp1.getValueType()), |
| 1465 | Tmp2, Tmp3, ISD::SETNE); |
| 1466 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1467 | break; |
| 1468 | case TargetLowering::Promote: { |
| 1469 | MVT::ValueType NVT = |
| 1470 | TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType()); |
| 1471 | unsigned ExtOp, TruncOp; |
| 1472 | if (MVT::isInteger(Tmp2.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1473 | ExtOp = ISD::ANY_EXTEND; |
| 1474 | TruncOp = ISD::TRUNCATE; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1475 | } else { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1476 | ExtOp = ISD::FP_EXTEND; |
| 1477 | TruncOp = ISD::FP_ROUND; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1478 | } |
| 1479 | // Promote each of the values to the new type. |
| 1480 | Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2); |
| 1481 | Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3); |
| 1482 | // Perform the larger operation, then round down. |
| 1483 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3); |
| 1484 | Result = DAG.getNode(TruncOp, Node->getValueType(0), Result); |
| 1485 | break; |
| 1486 | } |
| 1487 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1488 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1489 | case ISD::SELECT_CC: { |
| 1490 | Tmp1 = Node->getOperand(0); // LHS |
| 1491 | Tmp2 = Node->getOperand(1); // RHS |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1492 | Tmp3 = LegalizeOp(Node->getOperand(2)); // True |
| 1493 | Tmp4 = LegalizeOp(Node->getOperand(3)); // False |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1494 | SDOperand CC = Node->getOperand(4); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1495 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1496 | LegalizeSetCCOperands(Tmp1, Tmp2, CC); |
| 1497 | |
| 1498 | // If we didn't get both a LHS and RHS back from LegalizeSetCCOperands, |
| 1499 | // the LHS is a legal SETCC itself. In this case, we need to compare |
| 1500 | // the result against zero to select between true and false values. |
| 1501 | if (Tmp2.Val == 0) { |
| 1502 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 1503 | CC = DAG.getCondCode(ISD::SETNE); |
| 1504 | } |
| 1505 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC); |
| 1506 | |
| 1507 | // Everything is legal, see if we should expand this op or something. |
| 1508 | switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) { |
| 1509 | default: assert(0 && "This action is not supported yet!"); |
| 1510 | case TargetLowering::Legal: break; |
| 1511 | case TargetLowering::Custom: |
| 1512 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1513 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1514 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 1515 | } |
| 1516 | break; |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1517 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1518 | case ISD::SETCC: |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1519 | Tmp1 = Node->getOperand(0); |
| 1520 | Tmp2 = Node->getOperand(1); |
| 1521 | Tmp3 = Node->getOperand(2); |
| 1522 | LegalizeSetCCOperands(Tmp1, Tmp2, Tmp3); |
| 1523 | |
| 1524 | // If we had to Expand the SetCC operands into a SELECT node, then it may |
| 1525 | // not always be possible to return a true LHS & RHS. In this case, just |
| 1526 | // return the value we legalized, returned in the LHS |
| 1527 | if (Tmp2.Val == 0) { |
| 1528 | Result = Tmp1; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1529 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1530 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | 73e142f | 2006-01-30 22:43:50 +0000 | [diff] [blame] | 1532 | switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1533 | default: assert(0 && "Cannot handle this action for SETCC yet!"); |
| 1534 | case TargetLowering::Custom: |
| 1535 | isCustom = true; |
| 1536 | // FALLTHROUGH. |
| 1537 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1538 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1539 | if (isCustom) { |
| 1540 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1541 | if (Tmp3.Val) Result = Tmp3; |
| 1542 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1543 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1544 | case TargetLowering::Promote: { |
| 1545 | // First step, figure out the appropriate operation to use. |
| 1546 | // Allow SETCC to not be supported for all legal data types |
| 1547 | // Mostly this targets FP |
| 1548 | MVT::ValueType NewInTy = Node->getOperand(0).getValueType(); |
| 1549 | MVT::ValueType OldVT = NewInTy; |
| 1550 | |
| 1551 | // Scan for the appropriate larger type to use. |
| 1552 | while (1) { |
| 1553 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 1554 | |
| 1555 | assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) && |
| 1556 | "Fell off of the edge of the integer world"); |
| 1557 | assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) && |
| 1558 | "Fell off of the edge of the floating point world"); |
| 1559 | |
| 1560 | // If the target supports SETCC of this type, use it. |
Chris Lattner | 1ccf26a | 2005-12-22 05:23:45 +0000 | [diff] [blame] | 1561 | if (TLI.isOperationLegal(ISD::SETCC, NewInTy)) |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | } |
| 1564 | if (MVT::isInteger(NewInTy)) |
| 1565 | assert(0 && "Cannot promote Legal Integer SETCC yet"); |
| 1566 | else { |
| 1567 | Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1); |
| 1568 | Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2); |
| 1569 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1570 | Tmp1 = LegalizeOp(Tmp1); |
| 1571 | Tmp2 = LegalizeOp(Tmp2); |
| 1572 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Evan Cheng | 433f8ac | 2006-01-17 19:47:13 +0000 | [diff] [blame] | 1573 | Result = LegalizeOp(Result); |
Andrew Lenharth | 5e3efbc | 2005-08-29 20:46:51 +0000 | [diff] [blame] | 1574 | break; |
Andrew Lenharth | ae35575 | 2005-11-30 17:12:26 +0000 | [diff] [blame] | 1575 | } |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1576 | case TargetLowering::Expand: |
| 1577 | // Expand a setcc node into a select_cc of the same condition, lhs, and |
| 1578 | // rhs that selects between const 1 (true) and const 0 (false). |
| 1579 | MVT::ValueType VT = Node->getValueType(0); |
| 1580 | Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2, |
| 1581 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), |
| 1582 | Node->getOperand(2)); |
Nate Begeman | b942a3d | 2005-08-23 04:29:48 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1585 | break; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1586 | case ISD::MEMSET: |
| 1587 | case ISD::MEMCPY: |
| 1588 | case ISD::MEMMOVE: { |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1589 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1590 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer |
| 1591 | |
| 1592 | if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte |
| 1593 | switch (getTypeAction(Node->getOperand(2).getValueType())) { |
| 1594 | case Expand: assert(0 && "Cannot expand a byte!"); |
| 1595 | case Legal: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1596 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1597 | break; |
| 1598 | case Promote: |
Chris Lattner | deb692e | 2005-02-01 18:38:28 +0000 | [diff] [blame] | 1599 | Tmp3 = PromoteOp(Node->getOperand(2)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1600 | break; |
| 1601 | } |
| 1602 | } else { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1603 | Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer, |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1604 | } |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1605 | |
| 1606 | SDOperand Tmp4; |
| 1607 | switch (getTypeAction(Node->getOperand(3).getValueType())) { |
Chris Lattner | 6814f15 | 2005-07-13 01:42:45 +0000 | [diff] [blame] | 1608 | case Expand: { |
| 1609 | // Length is too big, just take the lo-part of the length. |
| 1610 | SDOperand HiPart; |
| 1611 | ExpandOp(Node->getOperand(3), HiPart, Tmp4); |
| 1612 | break; |
| 1613 | } |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1614 | case Legal: |
| 1615 | Tmp4 = LegalizeOp(Node->getOperand(3)); |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | case Promote: |
| 1618 | Tmp4 = PromoteOp(Node->getOperand(3)); |
Chris Lattner | 272455b | 2005-02-02 03:44:41 +0000 | [diff] [blame] | 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | SDOperand Tmp5; |
| 1623 | switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint |
| 1624 | case Expand: assert(0 && "Cannot expand this yet!"); |
| 1625 | case Legal: |
| 1626 | Tmp5 = LegalizeOp(Node->getOperand(4)); |
| 1627 | break; |
| 1628 | case Promote: |
Chris Lattner | e560521 | 2005-01-28 22:29:18 +0000 | [diff] [blame] | 1629 | Tmp5 = PromoteOp(Node->getOperand(4)); |
| 1630 | break; |
| 1631 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1632 | |
| 1633 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 1634 | default: assert(0 && "This action not implemented for this operation!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1635 | case TargetLowering::Custom: |
| 1636 | isCustom = true; |
| 1637 | // FALLTHROUGH |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1638 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1639 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1640 | if (isCustom) { |
| 1641 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1642 | if (Tmp1.Val) Result = Tmp1; |
| 1643 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1644 | break; |
| 1645 | case TargetLowering::Expand: { |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1646 | // Otherwise, the target does not support this operation. Lower the |
| 1647 | // operation to an explicit libcall as appropriate. |
| 1648 | MVT::ValueType IntPtr = TLI.getPointerTy(); |
| 1649 | const Type *IntPtrTy = TLI.getTargetData().getIntPtrType(); |
| 1650 | std::vector<std::pair<SDOperand, const Type*> > Args; |
| 1651 | |
Reid Spencer | 3bfbf4e | 2005-01-12 14:53:45 +0000 | [diff] [blame] | 1652 | const char *FnName = 0; |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1653 | if (Node->getOpcode() == ISD::MEMSET) { |
| 1654 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
Chris Lattner | dca7abe | 2006-02-20 06:38:35 +0000 | [diff] [blame] | 1655 | // Extend the (previously legalized) ubyte argument to be an int value |
| 1656 | // for the call. |
| 1657 | if (Tmp3.getValueType() > MVT::i32) |
| 1658 | Tmp3 = DAG.getNode(ISD::TRUNCATE, MVT::i32, Tmp3); |
| 1659 | else |
| 1660 | Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1661 | Args.push_back(std::make_pair(Tmp3, Type::IntTy)); |
| 1662 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1663 | |
| 1664 | FnName = "memset"; |
| 1665 | } else if (Node->getOpcode() == ISD::MEMCPY || |
| 1666 | Node->getOpcode() == ISD::MEMMOVE) { |
| 1667 | Args.push_back(std::make_pair(Tmp2, IntPtrTy)); |
| 1668 | Args.push_back(std::make_pair(Tmp3, IntPtrTy)); |
| 1669 | Args.push_back(std::make_pair(Tmp4, IntPtrTy)); |
| 1670 | FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy"; |
| 1671 | } else { |
| 1672 | assert(0 && "Unknown op!"); |
| 1673 | } |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1675 | std::pair<SDOperand,SDOperand> CallResult = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 1676 | TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false, |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1677 | DAG.getExternalSymbol(FnName, IntPtr), Args, DAG); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1678 | Result = CallResult.second; |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 1679 | break; |
| 1680 | } |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 1681 | } |
| 1682 | break; |
| 1683 | } |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 1685 | case ISD::SHL_PARTS: |
| 1686 | case ISD::SRA_PARTS: |
| 1687 | case ISD::SRL_PARTS: { |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1688 | std::vector<SDOperand> Ops; |
| 1689 | bool Changed = false; |
| 1690 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1691 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 1692 | Changed |= Ops.back() != Node->getOperand(i); |
| 1693 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1694 | if (Changed) |
| 1695 | Result = DAG.UpdateNodeOperands(Result, Ops); |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1696 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1697 | switch (TLI.getOperationAction(Node->getOpcode(), |
| 1698 | Node->getValueType(0))) { |
| 1699 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1700 | case TargetLowering::Legal: break; |
| 1701 | case TargetLowering::Custom: |
| 1702 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1703 | if (Tmp1.Val) { |
| 1704 | SDOperand Tmp2, RetVal(0, 0); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1705 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1706 | Tmp2 = LegalizeOp(Tmp1.getValue(i)); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1707 | AddLegalizedOperand(SDOperand(Node, i), Tmp2); |
| 1708 | if (i == Op.ResNo) |
Evan Cheng | 12f2274 | 2006-01-19 04:54:52 +0000 | [diff] [blame] | 1709 | RetVal = Tmp2; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1710 | } |
Chris Lattner | 269f8c0 | 2006-01-10 19:43:26 +0000 | [diff] [blame] | 1711 | assert(RetVal.Val && "Illegal result number"); |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1712 | return RetVal; |
| 1713 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | } |
| 1716 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1717 | // Since these produce multiple values, make sure to remember that we |
| 1718 | // legalized all of them. |
| 1719 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 1720 | AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i)); |
| 1721 | return Result.getValue(Op.ResNo); |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 1722 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 1723 | |
| 1724 | // Binary operators |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1725 | case ISD::ADD: |
| 1726 | case ISD::SUB: |
| 1727 | case ISD::MUL: |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 1728 | case ISD::MULHS: |
| 1729 | case ISD::MULHU: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1730 | case ISD::UDIV: |
| 1731 | case ISD::SDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1732 | case ISD::AND: |
| 1733 | case ISD::OR: |
| 1734 | case ISD::XOR: |
Chris Lattner | 03c0cf8 | 2005-01-07 21:45:56 +0000 | [diff] [blame] | 1735 | case ISD::SHL: |
| 1736 | case ISD::SRL: |
| 1737 | case ISD::SRA: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1738 | case ISD::FADD: |
| 1739 | case ISD::FSUB: |
| 1740 | case ISD::FMUL: |
| 1741 | case ISD::FDIV: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1742 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
Andrew Lenharth | f2eb139 | 2005-07-05 19:52:39 +0000 | [diff] [blame] | 1743 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1744 | case Expand: assert(0 && "Not possible"); |
| 1745 | case Legal: |
| 1746 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 1747 | break; |
| 1748 | case Promote: |
| 1749 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 1750 | break; |
| 1751 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1752 | |
| 1753 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1754 | |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 1755 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1756 | default: assert(0 && "Operation not supported"); |
| 1757 | case TargetLowering::Legal: break; |
| 1758 | case TargetLowering::Custom: |
| 1759 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1760 | if (Tmp1.Val) Result = Tmp1; |
Andrew Lenharth | 57030e3 | 2005-12-25 01:07:37 +0000 | [diff] [blame] | 1761 | break; |
Andrew Lenharth | e8f65f1 | 2005-12-24 23:42:32 +0000 | [diff] [blame] | 1762 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1763 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 1764 | |
| 1765 | case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type! |
| 1766 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 1767 | switch (getTypeAction(Node->getOperand(1).getValueType())) { |
| 1768 | case Expand: assert(0 && "Not possible"); |
| 1769 | case Legal: |
| 1770 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS. |
| 1771 | break; |
| 1772 | case Promote: |
| 1773 | Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS. |
| 1774 | break; |
| 1775 | } |
| 1776 | |
| 1777 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1778 | |
| 1779 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
| 1780 | default: assert(0 && "Operation not supported"); |
| 1781 | case TargetLowering::Custom: |
| 1782 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1783 | if (Tmp1.Val) Result = Tmp1; |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 1784 | break; |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 1785 | case TargetLowering::Legal: break; |
| 1786 | case TargetLowering::Expand: |
Chris Lattner | 8f4191d | 2006-03-13 06:08:38 +0000 | [diff] [blame] | 1787 | // If this target supports fabs/fneg natively, do this efficiently. |
| 1788 | if (TLI.isOperationLegal(ISD::FABS, Tmp1.getValueType()) && |
| 1789 | TLI.isOperationLegal(ISD::FNEG, Tmp1.getValueType())) { |
| 1790 | // Get the sign bit of the RHS. |
| 1791 | MVT::ValueType IVT = |
| 1792 | Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64; |
| 1793 | SDOperand SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2); |
| 1794 | SignBit = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 1795 | SignBit, DAG.getConstant(0, IVT), ISD::SETLT); |
| 1796 | // Get the absolute value of the result. |
| 1797 | SDOperand AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1); |
| 1798 | // Select between the nabs and abs value based on the sign bit of |
| 1799 | // the input. |
| 1800 | Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit, |
| 1801 | DAG.getNode(ISD::FNEG, AbsVal.getValueType(), |
| 1802 | AbsVal), |
| 1803 | AbsVal); |
| 1804 | Result = LegalizeOp(Result); |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | // Otherwise, do bitwise ops! |
| 1809 | |
| 1810 | // copysign -> copysignf/copysign libcall. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 1811 | const char *FnName; |
| 1812 | if (Node->getValueType(0) == MVT::f32) { |
| 1813 | FnName = "copysignf"; |
| 1814 | if (Tmp2.getValueType() != MVT::f32) // Force operands to match type. |
| 1815 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 1816 | DAG.getNode(ISD::FP_ROUND, MVT::f32, Tmp2)); |
| 1817 | } else { |
| 1818 | FnName = "copysign"; |
| 1819 | if (Tmp2.getValueType() != MVT::f64) // Force operands to match type. |
| 1820 | Result = DAG.UpdateNodeOperands(Result, Tmp1, |
| 1821 | DAG.getNode(ISD::FP_EXTEND, MVT::f64, Tmp2)); |
| 1822 | } |
| 1823 | SDOperand Dummy; |
| 1824 | Result = ExpandLibCall(FnName, Node, Dummy); |
| 1825 | break; |
| 1826 | } |
| 1827 | break; |
| 1828 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1829 | case ISD::ADDC: |
| 1830 | case ISD::SUBC: |
| 1831 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1832 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1833 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
| 1834 | // Since this produces two values, make sure to remember that we legalized |
| 1835 | // both of them. |
| 1836 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1837 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1838 | return Result; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1839 | |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1840 | case ISD::ADDE: |
| 1841 | case ISD::SUBE: |
| 1842 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 1843 | Tmp2 = LegalizeOp(Node->getOperand(1)); |
| 1844 | Tmp3 = LegalizeOp(Node->getOperand(2)); |
| 1845 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3); |
| 1846 | // Since this produces two values, make sure to remember that we legalized |
| 1847 | // both of them. |
| 1848 | AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0)); |
| 1849 | AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1)); |
| 1850 | return Result; |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 1851 | |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 1852 | case ISD::BUILD_PAIR: { |
| 1853 | MVT::ValueType PairTy = Node->getValueType(0); |
| 1854 | // TODO: handle the case where the Lo and Hi operands are not of legal type |
| 1855 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo |
| 1856 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi |
| 1857 | switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1858 | case TargetLowering::Promote: |
| 1859 | case TargetLowering::Custom: |
| 1860 | assert(0 && "Cannot promote/custom this yet!"); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 1861 | case TargetLowering::Legal: |
| 1862 | if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) |
| 1863 | Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2); |
| 1864 | break; |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 1865 | case TargetLowering::Expand: |
| 1866 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1); |
| 1867 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2); |
| 1868 | Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2, |
| 1869 | DAG.getConstant(MVT::getSizeInBits(PairTy)/2, |
| 1870 | TLI.getShiftAmountTy())); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1871 | Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2); |
Nate Begeman | 419f8b6 | 2005-10-18 00:27:41 +0000 | [diff] [blame] | 1872 | break; |
| 1873 | } |
| 1874 | break; |
| 1875 | } |
| 1876 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1877 | case ISD::UREM: |
| 1878 | case ISD::SREM: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 1879 | case ISD::FREM: |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1880 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 1881 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1882 | |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1883 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1884 | case TargetLowering::Promote: assert(0 && "Cannot promote this yet!"); |
| 1885 | case TargetLowering::Custom: |
| 1886 | isCustom = true; |
| 1887 | // FALLTHROUGH |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1888 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1889 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1890 | if (isCustom) { |
| 1891 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1892 | if (Tmp1.Val) Result = Tmp1; |
| 1893 | } |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1894 | break; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 1895 | case TargetLowering::Expand: |
| 1896 | if (MVT::isInteger(Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1897 | // X % Y -> X-X/Y*Y |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 1898 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1899 | unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV; |
Chris Lattner | 4c64dd7 | 2005-08-03 20:31:37 +0000 | [diff] [blame] | 1900 | Result = DAG.getNode(Opc, VT, Tmp1, Tmp2); |
| 1901 | Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2); |
| 1902 | Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result); |
| 1903 | } else { |
| 1904 | // Floating point mod -> fmod libcall. |
| 1905 | const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod"; |
| 1906 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 1907 | Result = ExpandLibCall(FnName, Node, Dummy); |
Nate Begeman | c105e19 | 2005-04-06 00:23:54 +0000 | [diff] [blame] | 1908 | } |
| 1909 | break; |
| 1910 | } |
| 1911 | break; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1912 | case ISD::VAARG: { |
| 1913 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1914 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 1915 | |
Chris Lattner | 5c62f33 | 2006-01-28 07:42:08 +0000 | [diff] [blame] | 1916 | MVT::ValueType VT = Node->getValueType(0); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1917 | switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) { |
| 1918 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1919 | case TargetLowering::Custom: |
| 1920 | isCustom = true; |
| 1921 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1922 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1923 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 1924 | Result = Result.getValue(0); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1925 | Tmp1 = Result.getValue(1); |
| 1926 | |
| 1927 | if (isCustom) { |
| 1928 | Tmp2 = TLI.LowerOperation(Result, DAG); |
| 1929 | if (Tmp2.Val) { |
| 1930 | Result = LegalizeOp(Tmp2); |
| 1931 | Tmp1 = LegalizeOp(Tmp2.getValue(1)); |
| 1932 | } |
| 1933 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1934 | break; |
| 1935 | case TargetLowering::Expand: { |
| 1936 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 1937 | Node->getOperand(2)); |
| 1938 | // Increment the pointer, VAList, to the next vaarg |
| 1939 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 1940 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 1941 | TLI.getPointerTy())); |
| 1942 | // Store the incremented VAList to the legalized pointer |
| 1943 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 1944 | Node->getOperand(2)); |
| 1945 | // Load the actual argument out of the pointer VAList |
| 1946 | Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1947 | Tmp1 = LegalizeOp(Result.getValue(1)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1948 | Result = LegalizeOp(Result); |
| 1949 | break; |
| 1950 | } |
| 1951 | } |
| 1952 | // Since VAARG produces two values, make sure to remember that we |
| 1953 | // legalized both of them. |
| 1954 | AddLegalizedOperand(SDOperand(Node, 0), Result); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1955 | AddLegalizedOperand(SDOperand(Node, 1), Tmp1); |
| 1956 | return Op.ResNo ? Tmp1 : Result; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | case ISD::VACOPY: |
| 1960 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1961 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer. |
| 1962 | Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer. |
| 1963 | |
| 1964 | switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) { |
| 1965 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1966 | case TargetLowering::Custom: |
| 1967 | isCustom = true; |
| 1968 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1969 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1970 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, |
| 1971 | Node->getOperand(3), Node->getOperand(4)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1972 | if (isCustom) { |
| 1973 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 1974 | if (Tmp1.Val) Result = Tmp1; |
| 1975 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1976 | break; |
| 1977 | case TargetLowering::Expand: |
| 1978 | // This defaults to loading a pointer from the input and storing it to the |
| 1979 | // output, returning the chain. |
| 1980 | Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3)); |
| 1981 | Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2, |
| 1982 | Node->getOperand(4)); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | } |
| 1985 | break; |
| 1986 | |
| 1987 | case ISD::VAEND: |
| 1988 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1989 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 1990 | |
| 1991 | switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) { |
| 1992 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1993 | case TargetLowering::Custom: |
| 1994 | isCustom = true; |
| 1995 | // FALLTHROUGH |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 1996 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1997 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1998 | if (isCustom) { |
| 1999 | Tmp1 = TLI.LowerOperation(Tmp1, DAG); |
| 2000 | if (Tmp1.Val) Result = Tmp1; |
| 2001 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2002 | break; |
| 2003 | case TargetLowering::Expand: |
| 2004 | Result = Tmp1; // Default to a no-op, return the chain |
| 2005 | break; |
| 2006 | } |
| 2007 | break; |
| 2008 | |
| 2009 | case ISD::VASTART: |
| 2010 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 2011 | Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
| 2012 | |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2013 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2)); |
| 2014 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2015 | switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) { |
| 2016 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2017 | case TargetLowering::Legal: break; |
| 2018 | case TargetLowering::Custom: |
| 2019 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2020 | if (Tmp1.Val) Result = Tmp1; |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 2021 | break; |
| 2022 | } |
| 2023 | break; |
| 2024 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2025 | case ISD::ROTL: |
| 2026 | case ISD::ROTR: |
| 2027 | Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS |
| 2028 | Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2029 | |
| 2030 | assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) && |
| 2031 | "Cannot handle this yet!"); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2032 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2); |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 2033 | break; |
| 2034 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2035 | case ISD::BSWAP: |
| 2036 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2037 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2038 | case TargetLowering::Custom: |
| 2039 | assert(0 && "Cannot custom legalize this yet!"); |
| 2040 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2041 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2042 | break; |
| 2043 | case TargetLowering::Promote: { |
| 2044 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2045 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
| 2046 | unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT); |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2048 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2049 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2050 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2051 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy())); |
| 2052 | break; |
| 2053 | } |
| 2054 | case TargetLowering::Expand: |
| 2055 | Result = ExpandBSWAP(Tmp1); |
| 2056 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2057 | } |
| 2058 | break; |
| 2059 | |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2060 | case ISD::CTPOP: |
| 2061 | case ISD::CTTZ: |
| 2062 | case ISD::CTLZ: |
| 2063 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Op |
| 2064 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2065 | case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!"); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2066 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2067 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2068 | break; |
| 2069 | case TargetLowering::Promote: { |
| 2070 | MVT::ValueType OVT = Tmp1.getValueType(); |
| 2071 | MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 2072 | |
| 2073 | // Zero extend the argument. |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2074 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2075 | // Perform the larger operation, then subtract if needed. |
| 2076 | Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2077 | switch (Node->getOpcode()) { |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2078 | case ISD::CTPOP: |
| 2079 | Result = Tmp1; |
| 2080 | break; |
| 2081 | case ISD::CTTZ: |
| 2082 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2083 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
| 2084 | DAG.getConstant(getSizeInBits(NVT), NVT), |
| 2085 | ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2086 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2087 | DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1); |
| 2088 | break; |
| 2089 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2090 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2091 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2092 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2093 | getSizeInBits(OVT), NVT)); |
| 2094 | break; |
| 2095 | } |
| 2096 | break; |
| 2097 | } |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2098 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2099 | Result = ExpandBitCount(Node->getOpcode(), Tmp1); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | } |
| 2102 | break; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2103 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2104 | // Unary operators |
| 2105 | case ISD::FABS: |
| 2106 | case ISD::FNEG: |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2107 | case ISD::FSQRT: |
| 2108 | case ISD::FSIN: |
| 2109 | case ISD::FCOS: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2110 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2111 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2112 | case TargetLowering::Promote: |
| 2113 | case TargetLowering::Custom: |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2114 | isCustom = true; |
| 2115 | // FALLTHROUGH |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2116 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2117 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Evan Cheng | 59ad781 | 2006-01-31 18:14:25 +0000 | [diff] [blame] | 2118 | if (isCustom) { |
| 2119 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2120 | if (Tmp1.Val) Result = Tmp1; |
| 2121 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2122 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2123 | case TargetLowering::Expand: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2124 | switch (Node->getOpcode()) { |
| 2125 | default: assert(0 && "Unreachable!"); |
| 2126 | case ISD::FNEG: |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2127 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 2128 | Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2129 | Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2130 | break; |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2131 | case ISD::FABS: { |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2132 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
| 2133 | MVT::ValueType VT = Node->getValueType(0); |
| 2134 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2135 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT); |
Chris Lattner | 4af6e0d | 2005-04-02 05:26:37 +0000 | [diff] [blame] | 2136 | Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1); |
| 2137 | Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2138 | break; |
| 2139 | } |
| 2140 | case ISD::FSQRT: |
| 2141 | case ISD::FSIN: |
| 2142 | case ISD::FCOS: { |
| 2143 | MVT::ValueType VT = Node->getValueType(0); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2144 | const char *FnName = 0; |
| 2145 | switch(Node->getOpcode()) { |
| 2146 | case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break; |
| 2147 | case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break; |
| 2148 | case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break; |
| 2149 | default: assert(0 && "Unreachable!"); |
| 2150 | } |
Nate Begeman | 2ac4fc0 | 2005-08-04 21:43:28 +0000 | [diff] [blame] | 2151 | SDOperand Dummy; |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 2152 | Result = ExpandLibCall(FnName, Node, Dummy); |
Chris Lattner | f76e7dc | 2005-04-30 04:43:14 +0000 | [diff] [blame] | 2153 | break; |
| 2154 | } |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2155 | } |
| 2156 | break; |
| 2157 | } |
| 2158 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2159 | |
| 2160 | case ISD::BIT_CONVERT: |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2161 | if (!isTypeLegal(Node->getOperand(0).getValueType())) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2162 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
Chris Lattner | 67993f7 | 2006-01-23 07:30:46 +0000 | [diff] [blame] | 2163 | } else { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2164 | switch (TLI.getOperationAction(ISD::BIT_CONVERT, |
| 2165 | Node->getOperand(0).getValueType())) { |
| 2166 | default: assert(0 && "Unknown operation action!"); |
| 2167 | case TargetLowering::Expand: |
| 2168 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2169 | break; |
| 2170 | case TargetLowering::Legal: |
| 2171 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2172 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2173 | break; |
| 2174 | } |
| 2175 | } |
| 2176 | break; |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2177 | // Conversion operators. The source and destination have different types. |
Chris Lattner | ae0aacb | 2005-01-08 08:08:56 +0000 | [diff] [blame] | 2178 | case ISD::SINT_TO_FP: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2179 | case ISD::UINT_TO_FP: { |
| 2180 | bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2181 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2182 | case Legal: |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2183 | switch (TLI.getOperationAction(Node->getOpcode(), |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2184 | Node->getOperand(0).getValueType())) { |
| 2185 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2186 | case TargetLowering::Custom: |
| 2187 | isCustom = true; |
| 2188 | // FALLTHROUGH |
| 2189 | case TargetLowering::Legal: |
| 2190 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2191 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2192 | if (isCustom) { |
| 2193 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2194 | if (Tmp1.Val) Result = Tmp1; |
| 2195 | } |
| 2196 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2197 | case TargetLowering::Expand: |
Jim Laskey | 6269ed1 | 2005-08-17 00:39:29 +0000 | [diff] [blame] | 2198 | Result = ExpandLegalINT_TO_FP(isSigned, |
| 2199 | LegalizeOp(Node->getOperand(0)), |
| 2200 | Node->getValueType(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2201 | break; |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2202 | case TargetLowering::Promote: |
| 2203 | Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)), |
| 2204 | Node->getValueType(0), |
| 2205 | isSigned); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2206 | break; |
Andrew Lenharth | 5b5b8c2 | 2005-11-30 06:43:03 +0000 | [diff] [blame] | 2207 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2208 | break; |
Chris Lattner | b00a642 | 2005-01-07 22:37:48 +0000 | [diff] [blame] | 2209 | case Expand: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2210 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 2211 | Node->getValueType(0), Node->getOperand(0)); |
| 2212 | break; |
| 2213 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2214 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2215 | if (isSigned) { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2216 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(), |
| 2217 | Tmp1, DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2218 | } else { |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2219 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, |
| 2220 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2221 | } |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2222 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
| 2223 | Result = LegalizeOp(Result); // The 'op' is not necessarily legal! |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2224 | break; |
| 2225 | } |
| 2226 | break; |
| 2227 | } |
| 2228 | case ISD::TRUNCATE: |
| 2229 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2230 | case Legal: |
| 2231 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2232 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2233 | break; |
| 2234 | case Expand: |
| 2235 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2236 | |
| 2237 | // Since the result is legal, we should just be able to truncate the low |
| 2238 | // part of the source. |
| 2239 | Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1); |
| 2240 | break; |
| 2241 | case Promote: |
| 2242 | Result = PromoteOp(Node->getOperand(0)); |
| 2243 | Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result); |
| 2244 | break; |
| 2245 | } |
| 2246 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2248 | case ISD::FP_TO_SINT: |
| 2249 | case ISD::FP_TO_UINT: |
| 2250 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2251 | case Legal: |
Chris Lattner | f1fa74e | 2005-07-30 00:04:12 +0000 | [diff] [blame] | 2252 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
| 2253 | |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2254 | switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){ |
| 2255 | default: assert(0 && "Unknown operation action!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2256 | case TargetLowering::Custom: |
| 2257 | isCustom = true; |
| 2258 | // FALLTHROUGH |
| 2259 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2260 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2261 | if (isCustom) { |
| 2262 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 2263 | if (Tmp1.Val) Result = Tmp1; |
| 2264 | } |
| 2265 | break; |
| 2266 | case TargetLowering::Promote: |
| 2267 | Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0), |
| 2268 | Node->getOpcode() == ISD::FP_TO_SINT); |
| 2269 | break; |
Chris Lattner | 1618beb | 2005-07-29 00:11:56 +0000 | [diff] [blame] | 2270 | case TargetLowering::Expand: |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2271 | if (Node->getOpcode() == ISD::FP_TO_UINT) { |
| 2272 | SDOperand True, False; |
| 2273 | MVT::ValueType VT = Node->getOperand(0).getValueType(); |
| 2274 | MVT::ValueType NVT = Node->getValueType(0); |
| 2275 | unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1; |
| 2276 | Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT); |
| 2277 | Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(), |
| 2278 | Node->getOperand(0), Tmp2, ISD::SETLT); |
| 2279 | True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0)); |
| 2280 | False = DAG.getNode(ISD::FP_TO_SINT, NVT, |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2281 | DAG.getNode(ISD::FSUB, VT, Node->getOperand(0), |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2282 | Tmp2)); |
| 2283 | False = DAG.getNode(ISD::XOR, NVT, False, |
| 2284 | DAG.getConstant(1ULL << ShiftAmt, NVT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2285 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False); |
| 2286 | break; |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2287 | } else { |
| 2288 | assert(0 && "Do not know how to expand FP_TO_SINT yet!"); |
| 2289 | } |
| 2290 | break; |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 2291 | } |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | case Expand: |
| 2294 | assert(0 && "Shouldn't need to expand other operators here!"); |
| 2295 | case Promote: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2296 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2297 | Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1)); |
| 2298 | Result = LegalizeOp(Result); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | } |
| 2301 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2303 | case ISD::ANY_EXTEND: |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2304 | case ISD::ZERO_EXTEND: |
| 2305 | case ISD::SIGN_EXTEND: |
| 2306 | case ISD::FP_EXTEND: |
| 2307 | case ISD::FP_ROUND: |
| 2308 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2309 | case Expand: assert(0 && "Shouldn't need to expand other operators here!"); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2310 | case Legal: |
| 2311 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2312 | Result = DAG.UpdateNodeOperands(Result, Tmp1); |
Chris Lattner | fa9c801 | 2005-07-28 23:31:12 +0000 | [diff] [blame] | 2313 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2314 | case Promote: |
| 2315 | switch (Node->getOpcode()) { |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2316 | case ISD::ANY_EXTEND: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2317 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2318 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2319 | break; |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2320 | case ISD::ZERO_EXTEND: |
| 2321 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2322 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2323 | Result = DAG.getZeroExtendInReg(Result, |
| 2324 | Node->getOperand(0).getValueType()); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2325 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2326 | case ISD::SIGN_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2327 | Result = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2328 | Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2329 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2330 | Result, |
| 2331 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2332 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2333 | case ISD::FP_EXTEND: |
Chris Lattner | 1713e73 | 2005-01-16 00:38:00 +0000 | [diff] [blame] | 2334 | Result = PromoteOp(Node->getOperand(0)); |
| 2335 | if (Result.getValueType() != Op.getValueType()) |
| 2336 | // Dynamically dead while we have only 2 FP types. |
| 2337 | Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result); |
| 2338 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2339 | case ISD::FP_ROUND: |
Chris Lattner | f8161d8 | 2005-01-16 05:06:12 +0000 | [diff] [blame] | 2340 | Result = PromoteOp(Node->getOperand(0)); |
| 2341 | Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result); |
| 2342 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2343 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2344 | } |
| 2345 | break; |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2346 | case ISD::FP_ROUND_INREG: |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2347 | case ISD::SIGN_EXTEND_INREG: { |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2348 | Tmp1 = LegalizeOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2349 | MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2350 | |
| 2351 | // If this operation is not supported, convert it to a shl/shr or load/store |
| 2352 | // pair. |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2353 | switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) { |
| 2354 | default: assert(0 && "This action not supported for this op yet!"); |
| 2355 | case TargetLowering::Legal: |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 2356 | Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1)); |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2357 | break; |
| 2358 | case TargetLowering::Expand: |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2359 | // If this is an integer extend and shifts are supported, do that. |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2360 | if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) { |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2361 | // NOTE: we could fall back on load/store here too for targets without |
| 2362 | // SAR. However, it is doubtful that any exist. |
| 2363 | unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) - |
| 2364 | MVT::getSizeInBits(ExtraVT); |
Chris Lattner | 27ff112 | 2005-01-22 00:31:52 +0000 | [diff] [blame] | 2365 | SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy()); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2366 | Result = DAG.getNode(ISD::SHL, Node->getValueType(0), |
| 2367 | Node->getOperand(0), ShiftCst); |
| 2368 | Result = DAG.getNode(ISD::SRA, Node->getValueType(0), |
| 2369 | Result, ShiftCst); |
| 2370 | } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) { |
| 2371 | // The only way we can lower this is to turn it into a STORETRUNC, |
| 2372 | // EXTLOAD pair, targetting a temporary location (a stack slot). |
| 2373 | |
| 2374 | // NOTE: there is a choice here between constantly creating new stack |
| 2375 | // slots and always reusing the same one. We currently always create |
| 2376 | // new ones, as reuse may inhibit scheduling. |
| 2377 | const Type *Ty = MVT::getTypeForValueType(ExtraVT); |
| 2378 | unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty); |
| 2379 | unsigned Align = TLI.getTargetData().getTypeAlignment(Ty); |
| 2380 | MachineFunction &MF = DAG.getMachineFunction(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2381 | int SSFI = |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2382 | MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align); |
| 2383 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 2384 | Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 2385 | Node->getOperand(0), StackSlot, |
Chris Lattner | 9fadb4c | 2005-07-10 00:29:18 +0000 | [diff] [blame] | 2386 | DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT)); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 2387 | Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0), |
| 2388 | Result, StackSlot, DAG.getSrcValue(NULL), |
| 2389 | ExtraVT); |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | assert(0 && "Unknown op"); |
| 2392 | } |
Chris Lattner | 55ba8fb | 2005-01-16 07:29:19 +0000 | [diff] [blame] | 2393 | break; |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2394 | } |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2395 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2396 | } |
Chris Lattner | 45b8caf | 2005-01-15 07:15:18 +0000 | [diff] [blame] | 2397 | } |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2398 | |
| 2399 | // Make sure that the generated code is itself legal. |
| 2400 | if (Result != Op) |
| 2401 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2403 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 2404 | // means that we always must cache transformed nodes. |
| 2405 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2406 | return Result; |
| 2407 | } |
| 2408 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2409 | /// PromoteOp - Given an operation that produces a value in an invalid type, |
| 2410 | /// promote it to compute the value into a larger type. The produced value will |
| 2411 | /// have the correct bits for the low portion of the register, but no guarantee |
| 2412 | /// 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] | 2413 | SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) { |
| 2414 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 2415 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2416 | assert(getTypeAction(VT) == Promote && |
| 2417 | "Caller should expand or legalize operands that are not promotable!"); |
| 2418 | assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) && |
| 2419 | "Cannot promote to smaller type!"); |
| 2420 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2421 | SDOperand Tmp1, Tmp2, Tmp3; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2422 | SDOperand Result; |
| 2423 | SDNode *Node = Op.Val; |
| 2424 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 2425 | std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op); |
| 2426 | if (I != PromotedNodes.end()) return I->second; |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2428 | switch (Node->getOpcode()) { |
Chris Lattner | d5d0f9b | 2005-08-16 21:55:35 +0000 | [diff] [blame] | 2429 | case ISD::CopyFromReg: |
| 2430 | assert(0 && "CopyFromReg must be legal!"); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2431 | default: |
| 2432 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 2433 | assert(0 && "Do not know how to promote this operator!"); |
| 2434 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 2435 | case ISD::UNDEF: |
| 2436 | Result = DAG.getNode(ISD::UNDEF, NVT); |
| 2437 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2438 | case ISD::Constant: |
Chris Lattner | ec176e3 | 2005-08-30 16:56:19 +0000 | [diff] [blame] | 2439 | if (VT != MVT::i1) |
| 2440 | Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op); |
| 2441 | else |
| 2442 | Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2443 | assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?"); |
| 2444 | break; |
| 2445 | case ISD::ConstantFP: |
| 2446 | Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op); |
| 2447 | assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?"); |
| 2448 | break; |
Chris Lattner | ef5cd1d | 2005-01-18 17:54:55 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2450 | case ISD::SETCC: |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2451 | assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??"); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2452 | Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0), |
| 2453 | Node->getOperand(1), Node->getOperand(2)); |
Chris Lattner | 82fbfb6 | 2005-01-18 02:59:52 +0000 | [diff] [blame] | 2454 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2455 | |
| 2456 | case ISD::TRUNCATE: |
| 2457 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2458 | case Legal: |
| 2459 | Result = LegalizeOp(Node->getOperand(0)); |
| 2460 | assert(Result.getValueType() >= NVT && |
| 2461 | "This truncation doesn't make sense!"); |
| 2462 | if (Result.getValueType() > NVT) // Truncate to NVT instead of VT |
| 2463 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Result); |
| 2464 | break; |
Chris Lattner | e76ad6d | 2005-01-28 22:52:50 +0000 | [diff] [blame] | 2465 | case Promote: |
| 2466 | // The truncation is not required, because we don't guarantee anything |
| 2467 | // about high bits anyway. |
| 2468 | Result = PromoteOp(Node->getOperand(0)); |
| 2469 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2470 | case Expand: |
Nate Begeman | 79e46ac | 2005-04-04 00:57:08 +0000 | [diff] [blame] | 2471 | ExpandOp(Node->getOperand(0), Tmp1, Tmp2); |
| 2472 | // Truncate the low part of the expanded value to the result type |
Chris Lattner | e21c305 | 2005-08-01 18:16:37 +0000 | [diff] [blame] | 2473 | Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2474 | } |
| 2475 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2476 | case ISD::SIGN_EXTEND: |
| 2477 | case ISD::ZERO_EXTEND: |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2478 | case ISD::ANY_EXTEND: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2479 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2480 | case Expand: assert(0 && "BUG: Smaller reg should have been promoted!"); |
| 2481 | case Legal: |
| 2482 | // 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] | 2483 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2484 | break; |
| 2485 | case Promote: |
| 2486 | // Promote the reg if it's smaller. |
| 2487 | Result = PromoteOp(Node->getOperand(0)); |
| 2488 | // The high bits are not guaranteed to be anything. Insert an extend. |
| 2489 | if (Node->getOpcode() == ISD::SIGN_EXTEND) |
Chris Lattner | 595dc54 | 2005-02-04 18:39:19 +0000 | [diff] [blame] | 2490 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2491 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 2492 | else if (Node->getOpcode() == ISD::ZERO_EXTEND) |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2493 | Result = DAG.getZeroExtendInReg(Result, |
| 2494 | Node->getOperand(0).getValueType()); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| 2497 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2498 | case ISD::BIT_CONVERT: |
| 2499 | Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0)); |
| 2500 | Result = PromoteOp(Result); |
| 2501 | break; |
| 2502 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2503 | case ISD::FP_EXTEND: |
| 2504 | assert(0 && "Case not implemented. Dynamically dead with 2 FP types!"); |
| 2505 | case ISD::FP_ROUND: |
| 2506 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2507 | case Expand: assert(0 && "BUG: Cannot expand FP regs!"); |
| 2508 | case Promote: assert(0 && "Unreachable with 2 FP types!"); |
| 2509 | case Legal: |
| 2510 | // Input is legal? Do an FP_ROUND_INREG. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2511 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2512 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2513 | break; |
| 2514 | } |
| 2515 | break; |
| 2516 | |
| 2517 | case ISD::SINT_TO_FP: |
| 2518 | case ISD::UINT_TO_FP: |
| 2519 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2520 | case Legal: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2521 | // No extra round required here. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2522 | Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2523 | break; |
| 2524 | |
| 2525 | case Promote: |
| 2526 | Result = PromoteOp(Node->getOperand(0)); |
| 2527 | if (Node->getOpcode() == ISD::SINT_TO_FP) |
| 2528 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(), |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2529 | Result, |
| 2530 | DAG.getValueType(Node->getOperand(0).getValueType())); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2531 | else |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2532 | Result = DAG.getZeroExtendInReg(Result, |
| 2533 | Node->getOperand(0).getValueType()); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2534 | // No extra round required here. |
| 2535 | Result = DAG.getNode(Node->getOpcode(), NVT, Result); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2536 | break; |
| 2537 | case Expand: |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2538 | Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT, |
| 2539 | Node->getOperand(0)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2540 | // Round if we cannot tolerate excess precision. |
| 2541 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2542 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2543 | DAG.getValueType(VT)); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2544 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2545 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2546 | break; |
| 2547 | |
Chris Lattner | 5e3c5b4 | 2005-12-09 17:32:47 +0000 | [diff] [blame] | 2548 | case ISD::SIGN_EXTEND_INREG: |
| 2549 | Result = PromoteOp(Node->getOperand(0)); |
| 2550 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, |
| 2551 | Node->getOperand(1)); |
| 2552 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2553 | case ISD::FP_TO_SINT: |
| 2554 | case ISD::FP_TO_UINT: |
| 2555 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 2556 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2557 | Tmp1 = Node->getOperand(0); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | case Promote: |
| 2560 | // The input result is prerounded, so we don't have to do anything |
| 2561 | // special. |
| 2562 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2563 | break; |
| 2564 | case Expand: |
| 2565 | assert(0 && "not implemented"); |
| 2566 | } |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2567 | // If we're promoting a UINT to a larger size, check to see if the new node |
| 2568 | // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since |
| 2569 | // we can use that instead. This allows us to generate better code for |
| 2570 | // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not |
| 2571 | // legal, such as PowerPC. |
| 2572 | if (Node->getOpcode() == ISD::FP_TO_UINT && |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 2573 | !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) && |
Nate Begeman | b7f6ef1 | 2005-10-25 23:47:25 +0000 | [diff] [blame] | 2574 | (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) || |
| 2575 | TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){ |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2576 | Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1); |
| 2577 | } else { |
| 2578 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2579 | } |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2580 | break; |
| 2581 | |
Chris Lattner | 2c8086f | 2005-04-02 05:00:07 +0000 | [diff] [blame] | 2582 | case ISD::FABS: |
| 2583 | case ISD::FNEG: |
| 2584 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2585 | assert(Tmp1.getValueType() == NVT); |
| 2586 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
| 2587 | // NOTE: we do not have to do any extra rounding here for |
| 2588 | // NoExcessFPPrecision, because we know the input will have the appropriate |
| 2589 | // precision, and these operations don't modify precision at all. |
| 2590 | break; |
| 2591 | |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2592 | case ISD::FSQRT: |
| 2593 | case ISD::FSIN: |
| 2594 | case ISD::FCOS: |
| 2595 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2596 | assert(Tmp1.getValueType() == NVT); |
| 2597 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2598 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2599 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2600 | DAG.getValueType(VT)); |
Chris Lattner | da6ba87 | 2005-04-28 21:44:33 +0000 | [diff] [blame] | 2601 | break; |
| 2602 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2603 | case ISD::AND: |
| 2604 | case ISD::OR: |
| 2605 | case ISD::XOR: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2606 | case ISD::ADD: |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2607 | case ISD::SUB: |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2608 | case ISD::MUL: |
| 2609 | // 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] | 2610 | // 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] | 2611 | // that too is okay if they are integer operations. |
| 2612 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2613 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2614 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 2615 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2616 | break; |
| 2617 | case ISD::FADD: |
| 2618 | case ISD::FSUB: |
| 2619 | case ISD::FMUL: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2620 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2621 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2622 | assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT); |
| 2623 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2624 | |
| 2625 | // Floating point operations will give excess precision that we may not be |
| 2626 | // able to tolerate. If we DO allow excess precision, just leave it, |
| 2627 | // otherwise excise it. |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2628 | // FIXME: Why would we need to round FP ops more than integer ones? |
| 2629 | // 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] | 2630 | if (NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2631 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2632 | DAG.getValueType(VT)); |
Chris Lattner | 0f69b29 | 2005-01-15 06:18:18 +0000 | [diff] [blame] | 2633 | break; |
| 2634 | |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2635 | case ISD::SDIV: |
| 2636 | case ISD::SREM: |
| 2637 | // These operators require that their input be sign extended. |
| 2638 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2639 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2640 | if (MVT::isInteger(NVT)) { |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2641 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 2642 | DAG.getValueType(VT)); |
| 2643 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 2644 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2645 | } |
| 2646 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2647 | |
| 2648 | // Perform FP_ROUND: this is probably overly pessimistic. |
| 2649 | if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision) |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2650 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2651 | DAG.getValueType(VT)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2652 | break; |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2653 | case ISD::FDIV: |
| 2654 | case ISD::FREM: |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2655 | case ISD::FCOPYSIGN: |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2656 | // These operators require that their input be fp extended. |
| 2657 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2658 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2659 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2660 | |
| 2661 | // Perform FP_ROUND: this is probably overly pessimistic. |
Chris Lattner | a09f848 | 2006-03-05 05:09:38 +0000 | [diff] [blame] | 2662 | if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN) |
Chris Lattner | 01b3d73 | 2005-09-28 22:28:18 +0000 | [diff] [blame] | 2663 | Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, |
| 2664 | DAG.getValueType(VT)); |
| 2665 | break; |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2666 | |
| 2667 | case ISD::UDIV: |
| 2668 | case ISD::UREM: |
| 2669 | // These operators require that their input be zero extended. |
| 2670 | Tmp1 = PromoteOp(Node->getOperand(0)); |
| 2671 | Tmp2 = PromoteOp(Node->getOperand(1)); |
| 2672 | assert(MVT::isInteger(NVT) && "Operators don't apply to FP!"); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2673 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 2674 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2675 | Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2); |
| 2676 | break; |
| 2677 | |
| 2678 | case ISD::SHL: |
| 2679 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2680 | Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | case ISD::SRA: |
| 2683 | // The input value must be properly sign extended. |
| 2684 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 15e4b01 | 2005-07-10 00:07:11 +0000 | [diff] [blame] | 2685 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 2686 | DAG.getValueType(VT)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2687 | Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2688 | break; |
| 2689 | case ISD::SRL: |
| 2690 | // The input value must be properly zero extended. |
| 2691 | Tmp1 = PromoteOp(Node->getOperand(0)); |
Chris Lattner | 2399356 | 2005-04-13 02:38:47 +0000 | [diff] [blame] | 2692 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2693 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1)); |
Chris Lattner | 8b6fa22 | 2005-01-15 22:16:26 +0000 | [diff] [blame] | 2694 | break; |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 2695 | |
| 2696 | case ISD::VAARG: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2697 | Tmp1 = Node->getOperand(0); // Get the chain. |
| 2698 | Tmp2 = Node->getOperand(1); // Get the pointer. |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 2699 | if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) { |
| 2700 | Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2)); |
| 2701 | Result = TLI.CustomPromoteOperation(Tmp3, DAG); |
| 2702 | } else { |
| 2703 | SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, |
| 2704 | Node->getOperand(2)); |
| 2705 | // Increment the pointer, VAList, to the next vaarg |
| 2706 | Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList, |
| 2707 | DAG.getConstant(MVT::getSizeInBits(VT)/8, |
| 2708 | TLI.getPointerTy())); |
| 2709 | // Store the incremented VAList to the legalized pointer |
| 2710 | Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2, |
| 2711 | Node->getOperand(2)); |
| 2712 | // Load the actual argument out of the pointer VAList |
| 2713 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, |
| 2714 | DAG.getSrcValue(0), VT); |
| 2715 | } |
| 2716 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2717 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Nate Begeman | 0aed784 | 2006-01-28 03:14:31 +0000 | [diff] [blame] | 2718 | break; |
| 2719 | |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2720 | case ISD::LOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2721 | Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0), |
| 2722 | Node->getOperand(1), Node->getOperand(2), VT); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2723 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2724 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2725 | break; |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 2726 | case ISD::SEXTLOAD: |
| 2727 | case ISD::ZEXTLOAD: |
| 2728 | case ISD::EXTLOAD: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2729 | Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0), |
| 2730 | Node->getOperand(1), Node->getOperand(2), |
Chris Lattner | 8136cda | 2005-10-15 20:24:07 +0000 | [diff] [blame] | 2731 | cast<VTSDNode>(Node->getOperand(3))->getVT()); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 2732 | // Remember that we legalized the chain. |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2733 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
Chris Lattner | 4c8f8f0 | 2005-10-13 20:07:41 +0000 | [diff] [blame] | 2734 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2735 | case ISD::SELECT: |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2736 | Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0 |
| 2737 | Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1 |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2738 | Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3); |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2739 | break; |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2740 | case ISD::SELECT_CC: |
| 2741 | Tmp2 = PromoteOp(Node->getOperand(2)); // True |
| 2742 | Tmp3 = PromoteOp(Node->getOperand(3)); // False |
| 2743 | Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2744 | Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4)); |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 2745 | break; |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 2746 | case ISD::BSWAP: |
| 2747 | Tmp1 = Node->getOperand(0); |
| 2748 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1); |
| 2749 | Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1); |
| 2750 | Result = DAG.getNode(ISD::SRL, NVT, Tmp1, |
| 2751 | DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT), |
| 2752 | TLI.getShiftAmountTy())); |
| 2753 | break; |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2754 | case ISD::CTPOP: |
| 2755 | case ISD::CTTZ: |
| 2756 | case ISD::CTLZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2757 | // Zero extend the argument |
| 2758 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2759 | // Perform the larger operation, then subtract if needed. |
| 2760 | Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2761 | switch(Node->getOpcode()) { |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2762 | case ISD::CTPOP: |
| 2763 | Result = Tmp1; |
| 2764 | break; |
| 2765 | case ISD::CTTZ: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2766 | // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Nate Begeman | d2558e3 | 2005-08-14 01:20:53 +0000 | [diff] [blame] | 2767 | Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 2768 | DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2769 | Result = DAG.getNode(ISD::SELECT, NVT, Tmp2, |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2770 | DAG.getConstant(getSizeInBits(VT), NVT), Tmp1); |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2771 | break; |
| 2772 | case ISD::CTLZ: |
| 2773 | //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 2774 | Result = DAG.getNode(ISD::SUB, NVT, Tmp1, |
| 2775 | DAG.getConstant(getSizeInBits(NVT) - |
Andrew Lenharth | fecf095 | 2005-05-04 19:11:05 +0000 | [diff] [blame] | 2776 | getSizeInBits(VT), NVT)); |
| 2777 | break; |
| 2778 | } |
| 2779 | break; |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | assert(Result.Val && "Didn't set a result!"); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 2783 | |
| 2784 | // Make sure the result is itself legal. |
| 2785 | Result = LegalizeOp(Result); |
| 2786 | |
| 2787 | // Remember that we promoted this! |
Chris Lattner | 03c8546 | 2005-01-15 05:21:40 +0000 | [diff] [blame] | 2788 | AddPromotedOperand(Op, Result); |
| 2789 | return Result; |
| 2790 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 2791 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2792 | /// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC |
| 2793 | /// with condition CC on the current target. This usually involves legalizing |
| 2794 | /// or promoting the arguments. In the case where LHS and RHS must be expanded, |
| 2795 | /// there may be no choice but to create a new SetCC node to represent the |
| 2796 | /// legalized value of setcc lhs, rhs. In this case, the value is returned in |
| 2797 | /// LHS, and the SDOperand returned in RHS has a nil SDNode value. |
| 2798 | void SelectionDAGLegalize::LegalizeSetCCOperands(SDOperand &LHS, |
| 2799 | SDOperand &RHS, |
| 2800 | SDOperand &CC) { |
| 2801 | SDOperand Tmp1, Tmp2, Result; |
| 2802 | |
| 2803 | switch (getTypeAction(LHS.getValueType())) { |
| 2804 | case Legal: |
| 2805 | Tmp1 = LegalizeOp(LHS); // LHS |
| 2806 | Tmp2 = LegalizeOp(RHS); // RHS |
| 2807 | break; |
| 2808 | case Promote: |
| 2809 | Tmp1 = PromoteOp(LHS); // LHS |
| 2810 | Tmp2 = PromoteOp(RHS); // RHS |
| 2811 | |
| 2812 | // If this is an FP compare, the operands have already been extended. |
| 2813 | if (MVT::isInteger(LHS.getValueType())) { |
| 2814 | MVT::ValueType VT = LHS.getValueType(); |
| 2815 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
| 2816 | |
| 2817 | // Otherwise, we have to insert explicit sign or zero extends. Note |
| 2818 | // that we could insert sign extends for ALL conditions, but zero extend |
| 2819 | // is cheaper on many machines (an AND instead of two shifts), so prefer |
| 2820 | // it. |
| 2821 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 2822 | default: assert(0 && "Unknown integer comparison!"); |
| 2823 | case ISD::SETEQ: |
| 2824 | case ISD::SETNE: |
| 2825 | case ISD::SETUGE: |
| 2826 | case ISD::SETUGT: |
| 2827 | case ISD::SETULE: |
| 2828 | case ISD::SETULT: |
| 2829 | // ALL of these operations will work if we either sign or zero extend |
| 2830 | // the operands (including the unsigned comparisons!). Zero extend is |
| 2831 | // usually a simpler/cheaper operation, so prefer it. |
| 2832 | Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT); |
| 2833 | Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT); |
| 2834 | break; |
| 2835 | case ISD::SETGE: |
| 2836 | case ISD::SETGT: |
| 2837 | case ISD::SETLT: |
| 2838 | case ISD::SETLE: |
| 2839 | Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, |
| 2840 | DAG.getValueType(VT)); |
| 2841 | Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, |
| 2842 | DAG.getValueType(VT)); |
| 2843 | break; |
| 2844 | } |
| 2845 | } |
| 2846 | break; |
| 2847 | case Expand: |
| 2848 | SDOperand LHSLo, LHSHi, RHSLo, RHSHi; |
| 2849 | ExpandOp(LHS, LHSLo, LHSHi); |
| 2850 | ExpandOp(RHS, RHSLo, RHSHi); |
| 2851 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 2852 | case ISD::SETEQ: |
| 2853 | case ISD::SETNE: |
| 2854 | if (RHSLo == RHSHi) |
| 2855 | if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) |
| 2856 | if (RHSCST->isAllOnesValue()) { |
| 2857 | // Comparison to -1. |
| 2858 | Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi); |
| 2859 | Tmp2 = RHSLo; |
| 2860 | break; |
| 2861 | } |
| 2862 | |
| 2863 | Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo); |
| 2864 | Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi); |
| 2865 | Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2); |
| 2866 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 2867 | break; |
| 2868 | default: |
| 2869 | // If this is a comparison of the sign bit, just look at the top part. |
| 2870 | // X > -1, x < 0 |
| 2871 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS)) |
| 2872 | if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT && |
| 2873 | CST->getValue() == 0) || // X < 0 |
| 2874 | (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT && |
| 2875 | CST->isAllOnesValue())) { // X > -1 |
| 2876 | Tmp1 = LHSHi; |
| 2877 | Tmp2 = RHSHi; |
| 2878 | break; |
| 2879 | } |
| 2880 | |
| 2881 | // FIXME: This generated code sucks. |
| 2882 | ISD::CondCode LowCC; |
| 2883 | switch (cast<CondCodeSDNode>(CC)->get()) { |
| 2884 | default: assert(0 && "Unknown integer setcc!"); |
| 2885 | case ISD::SETLT: |
| 2886 | case ISD::SETULT: LowCC = ISD::SETULT; break; |
| 2887 | case ISD::SETGT: |
| 2888 | case ISD::SETUGT: LowCC = ISD::SETUGT; break; |
| 2889 | case ISD::SETLE: |
| 2890 | case ISD::SETULE: LowCC = ISD::SETULE; break; |
| 2891 | case ISD::SETGE: |
| 2892 | case ISD::SETUGE: LowCC = ISD::SETUGE; break; |
| 2893 | } |
| 2894 | |
| 2895 | // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison |
| 2896 | // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands |
| 2897 | // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2; |
| 2898 | |
| 2899 | // NOTE: on targets without efficient SELECT of bools, we can always use |
| 2900 | // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3) |
| 2901 | Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC); |
| 2902 | Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi, CC); |
| 2903 | Result = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ); |
| 2904 | Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(), |
| 2905 | Result, Tmp1, Tmp2)); |
| 2906 | Tmp1 = Result; |
Nate Begeman | da06e9e | 2006-02-01 19:05:15 +0000 | [diff] [blame] | 2907 | Tmp2 = SDOperand(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | LHS = Tmp1; |
| 2911 | RHS = Tmp2; |
| 2912 | } |
| 2913 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2914 | /// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination. |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 2915 | /// The resultant code need not be legal. Note that SrcOp is the input operand |
| 2916 | /// to the BIT_CONVERT, not the BIT_CONVERT node itself. |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2917 | SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT, |
| 2918 | SDOperand SrcOp) { |
| 2919 | // Create the stack frame object. |
| 2920 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 2921 | unsigned ByteSize = MVT::getSizeInBits(DestVT)/8; |
Chris Lattner | 232348d | 2005-12-23 00:52:30 +0000 | [diff] [blame] | 2922 | int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2923 | SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy()); |
| 2924 | |
| 2925 | // Emit a store to the stack slot. |
| 2926 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
Chris Lattner | ed7b5ba | 2005-12-23 00:50:25 +0000 | [diff] [blame] | 2927 | SrcOp, FIPtr, DAG.getSrcValue(NULL)); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2928 | // Result is a load from the stack slot. |
| 2929 | return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0)); |
| 2930 | } |
| 2931 | |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2932 | void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp, |
| 2933 | SDOperand Op, SDOperand Amt, |
| 2934 | SDOperand &Lo, SDOperand &Hi) { |
| 2935 | // Expand the subcomponents. |
| 2936 | SDOperand LHSL, LHSH; |
| 2937 | ExpandOp(Op, LHSL, LHSH); |
| 2938 | |
| 2939 | std::vector<SDOperand> Ops; |
| 2940 | Ops.push_back(LHSL); |
| 2941 | Ops.push_back(LHSH); |
| 2942 | Ops.push_back(Amt); |
Chris Lattner | cc0675a | 2005-08-30 17:21:17 +0000 | [diff] [blame] | 2943 | std::vector<MVT::ValueType> VTs(2, LHSL.getValueType()); |
Chris Lattner | e89083a | 2005-05-14 07:25:05 +0000 | [diff] [blame] | 2944 | Lo = DAG.getNode(NodeOp, VTs, Ops); |
Chris Lattner | 5b359c6 | 2005-04-02 04:00:59 +0000 | [diff] [blame] | 2945 | Hi = Lo.getValue(1); |
| 2946 | } |
| 2947 | |
| 2948 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2949 | /// ExpandShift - Try to find a clever way to expand this shift operation out to |
| 2950 | /// smaller elements. If we can't find a way that is more efficient than a |
| 2951 | /// libcall on this target, return false. Otherwise, return true with the |
| 2952 | /// low-parts expanded into Lo and Hi. |
| 2953 | bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt, |
| 2954 | SDOperand &Lo, SDOperand &Hi) { |
| 2955 | assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) && |
| 2956 | "This is not a shift!"); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2957 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2958 | MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType()); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2959 | SDOperand ShAmt = LegalizeOp(Amt); |
| 2960 | MVT::ValueType ShTy = ShAmt.getValueType(); |
| 2961 | unsigned VTBits = MVT::getSizeInBits(Op.getValueType()); |
| 2962 | unsigned NVTBits = MVT::getSizeInBits(NVT); |
| 2963 | |
| 2964 | // Handle the case when Amt is an immediate. Other cases are currently broken |
| 2965 | // and are disabled. |
| 2966 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) { |
| 2967 | unsigned Cst = CN->getValue(); |
| 2968 | // Expand the incoming operand to be shifted, so that we have its parts |
| 2969 | SDOperand InL, InH; |
| 2970 | ExpandOp(Op, InL, InH); |
| 2971 | switch(Opc) { |
| 2972 | case ISD::SHL: |
| 2973 | if (Cst > VTBits) { |
| 2974 | Lo = DAG.getConstant(0, NVT); |
| 2975 | Hi = DAG.getConstant(0, NVT); |
| 2976 | } else if (Cst > NVTBits) { |
| 2977 | Lo = DAG.getConstant(0, NVT); |
| 2978 | Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2979 | } else if (Cst == NVTBits) { |
| 2980 | Lo = DAG.getConstant(0, NVT); |
| 2981 | Hi = InL; |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2982 | } else { |
| 2983 | Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy)); |
| 2984 | Hi = DAG.getNode(ISD::OR, NVT, |
| 2985 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)), |
| 2986 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 2987 | } |
| 2988 | return true; |
| 2989 | case ISD::SRL: |
| 2990 | if (Cst > VTBits) { |
| 2991 | Lo = DAG.getConstant(0, NVT); |
| 2992 | Hi = DAG.getConstant(0, NVT); |
| 2993 | } else if (Cst > NVTBits) { |
| 2994 | Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy)); |
| 2995 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 2996 | } else if (Cst == NVTBits) { |
| 2997 | Lo = InH; |
| 2998 | Hi = DAG.getConstant(0, NVT); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 2999 | } else { |
| 3000 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3001 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3002 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3003 | Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3004 | } |
| 3005 | return true; |
| 3006 | case ISD::SRA: |
| 3007 | if (Cst > VTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3008 | Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3009 | DAG.getConstant(NVTBits-1, ShTy)); |
| 3010 | } else if (Cst > NVTBits) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3011 | Lo = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3012 | DAG.getConstant(Cst-NVTBits, ShTy)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3013 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3014 | DAG.getConstant(NVTBits-1, ShTy)); |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3015 | } else if (Cst == NVTBits) { |
| 3016 | Lo = InH; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3017 | Hi = DAG.getNode(ISD::SRA, NVT, InH, |
Chris Lattner | ee27f57 | 2005-04-11 20:08:52 +0000 | [diff] [blame] | 3018 | DAG.getConstant(NVTBits-1, ShTy)); |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3019 | } else { |
| 3020 | Lo = DAG.getNode(ISD::OR, NVT, |
| 3021 | DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)), |
| 3022 | DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy))); |
| 3023 | Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy)); |
| 3024 | } |
| 3025 | return true; |
| 3026 | } |
| 3027 | } |
Nate Begeman | f1fe32e | 2005-04-06 21:13:14 +0000 | [diff] [blame] | 3028 | return false; |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3029 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3030 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3032 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 3033 | // does not fit into a register, return the lo part and set the hi part to the |
| 3034 | // by-reg argument. If it does fit into a single register, return the result |
| 3035 | // and leave the Hi part unset. |
| 3036 | SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node, |
| 3037 | SDOperand &Hi) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3038 | assert(!IsLegalizingCall && "Cannot overlap legalization of calls!"); |
| 3039 | // The input chain to this libcall is the entry node of the function. |
| 3040 | // Legalizing the call will automatically add the previous call to the |
| 3041 | // dependence. |
| 3042 | SDOperand InChain = DAG.getEntryNode(); |
| 3043 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3044 | TargetLowering::ArgListTy Args; |
| 3045 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 3046 | MVT::ValueType ArgVT = Node->getOperand(i).getValueType(); |
| 3047 | const Type *ArgTy = MVT::getTypeForValueType(ArgVT); |
| 3048 | Args.push_back(std::make_pair(Node->getOperand(i), ArgTy)); |
| 3049 | } |
| 3050 | SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3051 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3052 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3053 | const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0)); |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3054 | std::pair<SDOperand,SDOperand> CallInfo = |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 3055 | TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false, |
| 3056 | Callee, Args, DAG); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3058 | // Legalize the call sequence, starting with the chain. This will advance |
| 3059 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 3060 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 3061 | LegalizeOp(CallInfo.second); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3062 | SDOperand Result; |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3063 | switch (getTypeAction(CallInfo.first.getValueType())) { |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3064 | default: assert(0 && "Unknown thing"); |
| 3065 | case Legal: |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3066 | Result = CallInfo.first; |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3067 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3068 | case Expand: |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3069 | ExpandOp(CallInfo.first, Result, Hi); |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3070 | break; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3071 | } |
Chris Lattner | 99c25b8 | 2005-09-02 20:26:58 +0000 | [diff] [blame] | 3072 | return Result; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 3075 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3076 | /// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the |
| 3077 | /// destination type is legal. |
| 3078 | SDOperand SelectionDAGLegalize:: |
| 3079 | ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3080 | assert(isTypeLegal(DestTy) && "Destination type is not legal!"); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3081 | assert(getTypeAction(Source.getValueType()) == Expand && |
| 3082 | "This is not an expansion!"); |
| 3083 | assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!"); |
| 3084 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3085 | if (!isSigned) { |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3086 | assert(Source.getValueType() == MVT::i64 && |
| 3087 | "This only works for 64-bit -> FP"); |
| 3088 | // The 64-bit value loaded will be incorrectly if the 'sign bit' of the |
| 3089 | // incoming integer is set. To handle this, we dynamically test to see if |
| 3090 | // it is set, and, if so, add a fudge factor. |
| 3091 | SDOperand Lo, Hi; |
| 3092 | ExpandOp(Source, Lo, Hi); |
| 3093 | |
Chris Lattner | 66de05b | 2005-05-13 04:45:13 +0000 | [diff] [blame] | 3094 | // If this is unsigned, and not supported, first perform the conversion to |
| 3095 | // signed, then adjust the result if the sign bit is set. |
| 3096 | SDOperand SignedConv = ExpandIntToFP(true, DestTy, |
| 3097 | DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi)); |
| 3098 | |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3099 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi, |
| 3100 | DAG.getConstant(0, Hi.getValueType()), |
| 3101 | ISD::SETLT); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3102 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3103 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3104 | SignSet, Four, Zero); |
Chris Lattner | 383203b | 2005-05-12 18:52:34 +0000 | [diff] [blame] | 3105 | uint64_t FF = 0x5f800000ULL; |
| 3106 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3107 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 3109 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3110 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3111 | SDOperand FudgeInReg; |
| 3112 | if (DestTy == MVT::f32) |
Chris Lattner | 52d08bd | 2005-05-09 20:23:03 +0000 | [diff] [blame] | 3113 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3114 | DAG.getSrcValue(NULL)); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3115 | else { |
| 3116 | assert(DestTy == MVT::f64 && "Unexpected conversion"); |
Chris Lattner | 5f056bf | 2005-07-10 01:55:33 +0000 | [diff] [blame] | 3117 | FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), |
| 3118 | CPIdx, DAG.getSrcValue(NULL), MVT::f32); |
Chris Lattner | e9c35e7 | 2005-04-13 05:09:42 +0000 | [diff] [blame] | 3119 | } |
Chris Lattner | 473a990 | 2005-09-29 06:44:39 +0000 | [diff] [blame] | 3120 | return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3121 | } |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3123 | // Check to see if the target has a custom way to lower this. If so, use it. |
| 3124 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) { |
| 3125 | default: assert(0 && "This action not implemented for this operation!"); |
| 3126 | case TargetLowering::Legal: |
| 3127 | case TargetLowering::Expand: |
| 3128 | break; // This case is handled below. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3129 | case TargetLowering::Custom: { |
| 3130 | SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy, |
| 3131 | Source), DAG); |
| 3132 | if (NV.Val) |
| 3133 | return LegalizeOp(NV); |
| 3134 | break; // The target decided this was legal after all |
| 3135 | } |
Chris Lattner | a88a260 | 2005-05-14 05:33:54 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Chris Lattner | 13689e2 | 2005-05-12 07:00:44 +0000 | [diff] [blame] | 3138 | // Expand the source, then glue it back together for the call. We must expand |
| 3139 | // the source in case it is shared (this pass of legalize must traverse it). |
| 3140 | SDOperand SrcLo, SrcHi; |
| 3141 | ExpandOp(Source, SrcLo, SrcHi); |
| 3142 | Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi); |
| 3143 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 3144 | const char *FnName = 0; |
| 3145 | if (DestTy == MVT::f32) |
| 3146 | FnName = "__floatdisf"; |
| 3147 | else { |
| 3148 | assert(DestTy == MVT::f64 && "Unknown fp value type!"); |
| 3149 | FnName = "__floatdidf"; |
| 3150 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 3151 | |
| 3152 | Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source); |
| 3153 | SDOperand UnusedHiPart; |
Chris Lattner | 2512569 | 2006-02-17 04:32:33 +0000 | [diff] [blame] | 3154 | return ExpandLibCall(FnName, Source.Val, UnusedHiPart); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3155 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3156 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 3157 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 3158 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 3159 | /// we expand it. At this point, we know that the result and operand types are |
| 3160 | /// legal for the target. |
| 3161 | SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 3162 | SDOperand Op0, |
| 3163 | MVT::ValueType DestVT) { |
| 3164 | if (Op0.getValueType() == MVT::i32) { |
| 3165 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
| 3166 | |
| 3167 | // get the stack frame index of a 8 byte buffer |
| 3168 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3169 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 3170 | // get address of 8 byte buffer |
| 3171 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy()); |
| 3172 | // word offset constant for Hi/Lo address computation |
| 3173 | SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
| 3174 | // set up Hi and Lo (into buffer) address based on endian |
| 3175 | SDOperand Hi, Lo; |
| 3176 | if (TLI.isLittleEndian()) { |
| 3177 | Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); |
| 3178 | Lo = StackSlot; |
| 3179 | } else { |
| 3180 | Hi = StackSlot; |
| 3181 | Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff); |
| 3182 | } |
| 3183 | // if signed map to unsigned space |
| 3184 | SDOperand Op0Mapped; |
| 3185 | if (isSigned) { |
| 3186 | // constant used to invert sign bit (signed to unsigned mapping) |
| 3187 | SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 3188 | Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit); |
| 3189 | } else { |
| 3190 | Op0Mapped = Op0; |
| 3191 | } |
| 3192 | // store the lo of the constructed double - based on integer input |
| 3193 | SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 3194 | Op0Mapped, Lo, DAG.getSrcValue(NULL)); |
| 3195 | // initial hi portion of constructed double |
| 3196 | SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
| 3197 | // store the hi of the constructed double - biased exponent |
| 3198 | SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1, |
| 3199 | InitialHi, Hi, DAG.getSrcValue(NULL)); |
| 3200 | // load the constructed double |
| 3201 | SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, |
| 3202 | DAG.getSrcValue(NULL)); |
| 3203 | // FP constant to bias correct the final result |
| 3204 | SDOperand Bias = DAG.getConstantFP(isSigned ? |
| 3205 | BitsToDouble(0x4330000080000000ULL) |
| 3206 | : BitsToDouble(0x4330000000000000ULL), |
| 3207 | MVT::f64); |
| 3208 | // subtract the bias |
| 3209 | SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias); |
| 3210 | // final result |
| 3211 | SDOperand Result; |
| 3212 | // handle final rounding |
| 3213 | if (DestVT == MVT::f64) { |
| 3214 | // do nothing |
| 3215 | Result = Sub; |
| 3216 | } else { |
| 3217 | // if f32 then cast to f32 |
| 3218 | Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub); |
| 3219 | } |
| 3220 | return Result; |
| 3221 | } |
| 3222 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
| 3223 | SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0); |
| 3224 | |
| 3225 | SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0, |
| 3226 | DAG.getConstant(0, Op0.getValueType()), |
| 3227 | ISD::SETLT); |
| 3228 | SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4); |
| 3229 | SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(), |
| 3230 | SignSet, Four, Zero); |
| 3231 | |
| 3232 | // If the sign bit of the integer is set, the large number will be treated |
| 3233 | // as a negative number. To counteract this, the dynamic code adds an |
| 3234 | // offset depending on the data type. |
| 3235 | uint64_t FF; |
| 3236 | switch (Op0.getValueType()) { |
| 3237 | default: assert(0 && "Unsupported integer type!"); |
| 3238 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 3239 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 3240 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 3241 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 3242 | } |
| 3243 | if (TLI.isLittleEndian()) FF <<= 32; |
| 3244 | static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF); |
| 3245 | |
| 3246 | SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 3247 | CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset); |
| 3248 | SDOperand FudgeInReg; |
| 3249 | if (DestVT == MVT::f32) |
| 3250 | FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, |
| 3251 | DAG.getSrcValue(NULL)); |
| 3252 | else { |
| 3253 | assert(DestVT == MVT::f64 && "Unexpected conversion"); |
| 3254 | FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, |
| 3255 | DAG.getEntryNode(), CPIdx, |
| 3256 | DAG.getSrcValue(NULL), MVT::f32)); |
| 3257 | } |
| 3258 | |
| 3259 | return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg); |
| 3260 | } |
| 3261 | |
| 3262 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 3263 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 3264 | /// we promote it. At this point, we know that the result and operand types are |
| 3265 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 3266 | /// operation that takes a larger input. |
| 3267 | SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp, |
| 3268 | MVT::ValueType DestVT, |
| 3269 | bool isSigned) { |
| 3270 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
| 3271 | MVT::ValueType NewInTy = LegalOp.getValueType(); |
| 3272 | |
| 3273 | unsigned OpToUse = 0; |
| 3274 | |
| 3275 | // Scan for the appropriate larger type to use. |
| 3276 | while (1) { |
| 3277 | NewInTy = (MVT::ValueType)(NewInTy+1); |
| 3278 | assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!"); |
| 3279 | |
| 3280 | // If the target supports SINT_TO_FP of this type, use it. |
| 3281 | switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) { |
| 3282 | default: break; |
| 3283 | case TargetLowering::Legal: |
| 3284 | if (!TLI.isTypeLegal(NewInTy)) |
| 3285 | break; // Can't use this datatype. |
| 3286 | // FALL THROUGH. |
| 3287 | case TargetLowering::Custom: |
| 3288 | OpToUse = ISD::SINT_TO_FP; |
| 3289 | break; |
| 3290 | } |
| 3291 | if (OpToUse) break; |
| 3292 | if (isSigned) continue; |
| 3293 | |
| 3294 | // If the target supports UINT_TO_FP of this type, use it. |
| 3295 | switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) { |
| 3296 | default: break; |
| 3297 | case TargetLowering::Legal: |
| 3298 | if (!TLI.isTypeLegal(NewInTy)) |
| 3299 | break; // Can't use this datatype. |
| 3300 | // FALL THROUGH. |
| 3301 | case TargetLowering::Custom: |
| 3302 | OpToUse = ISD::UINT_TO_FP; |
| 3303 | break; |
| 3304 | } |
| 3305 | if (OpToUse) break; |
| 3306 | |
| 3307 | // Otherwise, try a larger type. |
| 3308 | } |
| 3309 | |
| 3310 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 3311 | // desired type then run the operation on it. |
| 3312 | return DAG.getNode(OpToUse, DestVT, |
| 3313 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
| 3314 | NewInTy, LegalOp)); |
| 3315 | } |
| 3316 | |
| 3317 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 3318 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 3319 | /// we promote it. At this point, we know that the result and operand types are |
| 3320 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 3321 | /// operation that returns a larger result. |
| 3322 | SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp, |
| 3323 | MVT::ValueType DestVT, |
| 3324 | bool isSigned) { |
| 3325 | // First step, figure out the appropriate FP_TO*INT operation to use. |
| 3326 | MVT::ValueType NewOutTy = DestVT; |
| 3327 | |
| 3328 | unsigned OpToUse = 0; |
| 3329 | |
| 3330 | // Scan for the appropriate larger type to use. |
| 3331 | while (1) { |
| 3332 | NewOutTy = (MVT::ValueType)(NewOutTy+1); |
| 3333 | assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!"); |
| 3334 | |
| 3335 | // If the target supports FP_TO_SINT returning this type, use it. |
| 3336 | switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) { |
| 3337 | default: break; |
| 3338 | case TargetLowering::Legal: |
| 3339 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3340 | break; // Can't use this datatype. |
| 3341 | // FALL THROUGH. |
| 3342 | case TargetLowering::Custom: |
| 3343 | OpToUse = ISD::FP_TO_SINT; |
| 3344 | break; |
| 3345 | } |
| 3346 | if (OpToUse) break; |
| 3347 | |
| 3348 | // If the target supports FP_TO_UINT of this type, use it. |
| 3349 | switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) { |
| 3350 | default: break; |
| 3351 | case TargetLowering::Legal: |
| 3352 | if (!TLI.isTypeLegal(NewOutTy)) |
| 3353 | break; // Can't use this datatype. |
| 3354 | // FALL THROUGH. |
| 3355 | case TargetLowering::Custom: |
| 3356 | OpToUse = ISD::FP_TO_UINT; |
| 3357 | break; |
| 3358 | } |
| 3359 | if (OpToUse) break; |
| 3360 | |
| 3361 | // Otherwise, try a larger type. |
| 3362 | } |
| 3363 | |
| 3364 | // Okay, we found the operation and type to use. Truncate the result of the |
| 3365 | // extended FP_TO_*INT operation to the desired size. |
| 3366 | return DAG.getNode(ISD::TRUNCATE, DestVT, |
| 3367 | DAG.getNode(OpToUse, NewOutTy, LegalOp)); |
| 3368 | } |
| 3369 | |
| 3370 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 3371 | /// |
| 3372 | SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) { |
| 3373 | MVT::ValueType VT = Op.getValueType(); |
| 3374 | MVT::ValueType SHVT = TLI.getShiftAmountTy(); |
| 3375 | SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
| 3376 | switch (VT) { |
| 3377 | default: assert(0 && "Unhandled Expand type in BSWAP!"); abort(); |
| 3378 | case MVT::i16: |
| 3379 | Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3380 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3381 | return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2); |
| 3382 | case MVT::i32: |
| 3383 | Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3384 | Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3385 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3386 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3387 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 3388 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 3389 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 3390 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 3391 | return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 3392 | case MVT::i64: |
| 3393 | Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT)); |
| 3394 | Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT)); |
| 3395 | Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3396 | Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3397 | Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT)); |
| 3398 | Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT)); |
| 3399 | Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT)); |
| 3400 | Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT)); |
| 3401 | Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 3402 | Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 3403 | Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 3404 | Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 3405 | Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 3406 | Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 3407 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7); |
| 3408 | Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5); |
| 3409 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3); |
| 3410 | Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1); |
| 3411 | Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6); |
| 3412 | Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2); |
| 3413 | return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4); |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 3418 | /// |
| 3419 | SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) { |
| 3420 | switch (Opc) { |
| 3421 | default: assert(0 && "Cannot expand this yet!"); |
| 3422 | case ISD::CTPOP: { |
| 3423 | static const uint64_t mask[6] = { |
| 3424 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 3425 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 3426 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 3427 | }; |
| 3428 | MVT::ValueType VT = Op.getValueType(); |
| 3429 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 3430 | unsigned len = getSizeInBits(VT); |
| 3431 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 3432 | //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8]) |
| 3433 | SDOperand Tmp2 = DAG.getConstant(mask[i], VT); |
| 3434 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 3435 | Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2), |
| 3436 | DAG.getNode(ISD::AND, VT, |
| 3437 | DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2)); |
| 3438 | } |
| 3439 | return Op; |
| 3440 | } |
| 3441 | case ISD::CTLZ: { |
| 3442 | // for now, we do this: |
| 3443 | // x = x | (x >> 1); |
| 3444 | // x = x | (x >> 2); |
| 3445 | // ... |
| 3446 | // x = x | (x >>16); |
| 3447 | // x = x | (x >>32); // for 64-bit input |
| 3448 | // return popcount(~x); |
| 3449 | // |
| 3450 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
| 3451 | MVT::ValueType VT = Op.getValueType(); |
| 3452 | MVT::ValueType ShVT = TLI.getShiftAmountTy(); |
| 3453 | unsigned len = getSizeInBits(VT); |
| 3454 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
| 3455 | SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
| 3456 | Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3)); |
| 3457 | } |
| 3458 | Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT)); |
| 3459 | return DAG.getNode(ISD::CTPOP, VT, Op); |
| 3460 | } |
| 3461 | case ISD::CTTZ: { |
| 3462 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 3463 | // unless the target has ctlz but not ctpop, in which case we use: |
| 3464 | // { return 32 - nlz(~x & (x-1)); } |
| 3465 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
| 3466 | MVT::ValueType VT = Op.getValueType(); |
| 3467 | SDOperand Tmp2 = DAG.getConstant(~0ULL, VT); |
| 3468 | SDOperand Tmp3 = DAG.getNode(ISD::AND, VT, |
| 3469 | DAG.getNode(ISD::XOR, VT, Op, Tmp2), |
| 3470 | DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT))); |
| 3471 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
| 3472 | if (!TLI.isOperationLegal(ISD::CTPOP, VT) && |
| 3473 | TLI.isOperationLegal(ISD::CTLZ, VT)) |
| 3474 | return DAG.getNode(ISD::SUB, VT, |
| 3475 | DAG.getConstant(getSizeInBits(VT), VT), |
| 3476 | DAG.getNode(ISD::CTLZ, VT, Tmp3)); |
| 3477 | return DAG.getNode(ISD::CTPOP, VT, Tmp3); |
| 3478 | } |
| 3479 | } |
| 3480 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3481 | |
| 3482 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3483 | /// ExpandOp - Expand the specified SDOperand into its two component pieces |
| 3484 | /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the |
| 3485 | /// LegalizeNodes map is filled in for any results that are not expanded, the |
| 3486 | /// ExpandedNodes map is filled in for any results that are expanded, and the |
| 3487 | /// Lo/Hi values are returned. |
| 3488 | void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){ |
| 3489 | MVT::ValueType VT = Op.getValueType(); |
Chris Lattner | 71c42a0 | 2005-01-16 01:11:45 +0000 | [diff] [blame] | 3490 | MVT::ValueType NVT = TLI.getTypeToTransformTo(VT); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3491 | SDNode *Node = Op.Val; |
| 3492 | assert(getTypeAction(VT) == Expand && "Not an expanded type!"); |
Nate Begeman | ab48be3 | 2005-11-22 18:16:00 +0000 | [diff] [blame] | 3493 | assert((MVT::isInteger(VT) || VT == MVT::Vector) && |
| 3494 | "Cannot expand FP values!"); |
| 3495 | assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) && |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3496 | "Cannot expand to FP value or to larger int value!"); |
| 3497 | |
Chris Lattner | 6fdcb25 | 2005-09-02 20:32:45 +0000 | [diff] [blame] | 3498 | // See if we already expanded it. |
| 3499 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 3500 | = ExpandedNodes.find(Op); |
| 3501 | if (I != ExpandedNodes.end()) { |
| 3502 | Lo = I->second.first; |
| 3503 | Hi = I->second.second; |
| 3504 | return; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3507 | switch (Node->getOpcode()) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3508 | case ISD::CopyFromReg: |
| 3509 | assert(0 && "CopyFromReg must be legal!"); |
| 3510 | default: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3511 | std::cerr << "NODE: "; Node->dump(); std::cerr << "\n"; |
| 3512 | assert(0 && "Do not know how to expand this operator!"); |
| 3513 | abort(); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 3514 | case ISD::UNDEF: |
| 3515 | Lo = DAG.getNode(ISD::UNDEF, NVT); |
| 3516 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 3517 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3518 | case ISD::Constant: { |
| 3519 | uint64_t Cst = cast<ConstantSDNode>(Node)->getValue(); |
| 3520 | Lo = DAG.getConstant(Cst, NVT); |
| 3521 | Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT); |
| 3522 | break; |
| 3523 | } |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 3524 | case ISD::BUILD_PAIR: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3525 | // Return the operands. |
| 3526 | Lo = Node->getOperand(0); |
| 3527 | Hi = Node->getOperand(1); |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 3528 | break; |
Chris Lattner | 58f7963 | 2005-12-12 22:27:43 +0000 | [diff] [blame] | 3529 | |
| 3530 | case ISD::SIGN_EXTEND_INREG: |
| 3531 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 3532 | // Sign extend the lo-part. |
| 3533 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 3534 | DAG.getConstant(MVT::getSizeInBits(NVT)-1, |
| 3535 | TLI.getShiftAmountTy())); |
| 3536 | // sext_inreg the low part if needed. |
| 3537 | Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1)); |
| 3538 | break; |
Chris Lattner | d4e50bb | 2005-03-28 22:03:13 +0000 | [diff] [blame] | 3539 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 3540 | case ISD::BSWAP: { |
| 3541 | ExpandOp(Node->getOperand(0), Lo, Hi); |
| 3542 | SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi); |
| 3543 | Hi = DAG.getNode(ISD::BSWAP, NVT, Lo); |
| 3544 | Lo = TempLo; |
| 3545 | break; |
| 3546 | } |
| 3547 | |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 3548 | case ISD::CTPOP: |
| 3549 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 9b583b4 | 2005-05-11 05:09:47 +0000 | [diff] [blame] | 3550 | Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L) |
| 3551 | DAG.getNode(ISD::CTPOP, NVT, Lo), |
| 3552 | DAG.getNode(ISD::CTPOP, NVT, Hi)); |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 3553 | Hi = DAG.getConstant(0, NVT); |
| 3554 | break; |
| 3555 | |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 3556 | case ISD::CTLZ: { |
| 3557 | // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 3558 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 3559 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 3560 | SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3561 | SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC, |
| 3562 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 3563 | SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo); |
| 3564 | LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC); |
| 3565 | |
| 3566 | Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart); |
| 3567 | Hi = DAG.getConstant(0, NVT); |
| 3568 | break; |
| 3569 | } |
| 3570 | |
| 3571 | case ISD::CTTZ: { |
| 3572 | // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32) |
Chris Lattner | 3becf20 | 2005-05-12 19:27:51 +0000 | [diff] [blame] | 3573 | ExpandOp(Node->getOperand(0), Lo, Hi); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 3574 | SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT); |
| 3575 | SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo); |
Chris Lattner | 7cf7e3f | 2005-08-09 20:20:18 +0000 | [diff] [blame] | 3576 | SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC, |
| 3577 | ISD::SETNE); |
Chris Lattner | 39a8f33 | 2005-05-12 19:05:01 +0000 | [diff] [blame] | 3578 | SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi); |
| 3579 | HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC); |
| 3580 | |
| 3581 | Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart); |
| 3582 | Hi = DAG.getConstant(0, NVT); |
| 3583 | break; |
| 3584 | } |
Chris Lattner | edb1add | 2005-05-11 04:51:16 +0000 | [diff] [blame] | 3585 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3586 | case ISD::VAARG: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3587 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 3588 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3589 | Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2)); |
| 3590 | Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2)); |
| 3591 | |
| 3592 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3593 | Hi = LegalizeOp(Hi); |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 3594 | AddLegalizedOperand(Op.getValue(1), Hi.getValue(1)); |
| 3595 | if (!TLI.isLittleEndian()) |
| 3596 | std::swap(Lo, Hi); |
| 3597 | break; |
| 3598 | } |
| 3599 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3600 | case ISD::LOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3601 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 3602 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 3603 | Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3604 | |
| 3605 | // Increment the pointer to the other half. |
Chris Lattner | 38d6be5 | 2005-01-09 19:43:23 +0000 | [diff] [blame] | 3606 | unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3607 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 3608 | getIntPtrConstant(IncrementSize)); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3609 | // FIXME: This creates a bogus srcvalue! |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 3610 | Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | ec39a45 | 2005-01-19 18:02:17 +0000 | [diff] [blame] | 3611 | |
| 3612 | // Build a factor node to remember that this load is independent of the |
| 3613 | // other one. |
| 3614 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 3615 | Hi.getValue(1)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3616 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3617 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3618 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3619 | if (!TLI.isLittleEndian()) |
| 3620 | std::swap(Lo, Hi); |
| 3621 | break; |
| 3622 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3623 | case ISD::AND: |
| 3624 | case ISD::OR: |
| 3625 | case ISD::XOR: { // Simple logical operators -> two trivial pieces. |
| 3626 | SDOperand LL, LH, RL, RH; |
| 3627 | ExpandOp(Node->getOperand(0), LL, LH); |
| 3628 | ExpandOp(Node->getOperand(1), RL, RH); |
| 3629 | Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL); |
| 3630 | Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH); |
| 3631 | break; |
| 3632 | } |
| 3633 | case ISD::SELECT: { |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3634 | SDOperand LL, LH, RL, RH; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3635 | ExpandOp(Node->getOperand(1), LL, LH); |
| 3636 | ExpandOp(Node->getOperand(2), RL, RH); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 3637 | Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL); |
| 3638 | Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3639 | break; |
| 3640 | } |
Nate Begeman | 9373a81 | 2005-08-10 20:51:12 +0000 | [diff] [blame] | 3641 | case ISD::SELECT_CC: { |
| 3642 | SDOperand TL, TH, FL, FH; |
| 3643 | ExpandOp(Node->getOperand(2), TL, TH); |
| 3644 | ExpandOp(Node->getOperand(3), FL, FH); |
| 3645 | Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 3646 | Node->getOperand(1), TL, FL, Node->getOperand(4)); |
| 3647 | Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0), |
| 3648 | Node->getOperand(1), TH, FH, Node->getOperand(4)); |
| 3649 | break; |
| 3650 | } |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3651 | case ISD::SEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3652 | SDOperand Chain = Node->getOperand(0); |
| 3653 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3654 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 3655 | |
| 3656 | if (EVT == NVT) |
| 3657 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 3658 | else |
| 3659 | Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 3660 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3661 | |
| 3662 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3663 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3664 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3665 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 3666 | // part. |
| 3667 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
| 3668 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, |
| 3669 | TLI.getShiftAmountTy())); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | } |
| 3672 | case ISD::ZEXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3673 | SDOperand Chain = Node->getOperand(0); |
| 3674 | SDOperand Ptr = Node->getOperand(1); |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3675 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 3676 | |
| 3677 | if (EVT == NVT) |
| 3678 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 3679 | else |
| 3680 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 3681 | EVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3682 | |
| 3683 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3684 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3685 | |
Nate Begeman | 144ff66 | 2005-10-13 17:15:37 +0000 | [diff] [blame] | 3686 | // The high part is just a zero. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3687 | Hi = DAG.getConstant(0, NVT); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | } |
| 3690 | case ISD::EXTLOAD: { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3691 | SDOperand Chain = Node->getOperand(0); |
| 3692 | SDOperand Ptr = Node->getOperand(1); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3693 | MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT(); |
| 3694 | |
| 3695 | if (EVT == NVT) |
| 3696 | Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2)); |
| 3697 | else |
| 3698 | Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2), |
| 3699 | EVT); |
| 3700 | |
| 3701 | // Remember that we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3702 | AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1))); |
Chris Lattner | 9ad8481 | 2005-10-13 21:44:47 +0000 | [diff] [blame] | 3703 | |
| 3704 | // The high part is undefined. |
Chris Lattner | 13c78e2 | 2005-09-02 00:18:10 +0000 | [diff] [blame] | 3705 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 3706 | break; |
| 3707 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3708 | case ISD::ANY_EXTEND: |
| 3709 | // The low part is any extension of the input (which degenerates to a copy). |
| 3710 | Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0)); |
| 3711 | // The high part is undefined. |
| 3712 | Hi = DAG.getNode(ISD::UNDEF, NVT); |
| 3713 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3714 | case ISD::SIGN_EXTEND: { |
| 3715 | // The low part is just a sign extension of the input (which degenerates to |
| 3716 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3717 | Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3718 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3719 | // The high part is obtained by SRA'ing all but one of the bits of the lo |
| 3720 | // part. |
Chris Lattner | 2dad454 | 2005-01-12 18:19:52 +0000 | [diff] [blame] | 3721 | unsigned LoSize = MVT::getSizeInBits(Lo.getValueType()); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3722 | Hi = DAG.getNode(ISD::SRA, NVT, Lo, |
| 3723 | DAG.getConstant(LoSize-1, TLI.getShiftAmountTy())); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3724 | break; |
| 3725 | } |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3726 | case ISD::ZERO_EXTEND: |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3727 | // The low part is just a zero extension of the input (which degenerates to |
| 3728 | // a copy). |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3729 | Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0)); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3730 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3731 | // The high part is just a zero. |
| 3732 | Hi = DAG.getConstant(0, NVT); |
| 3733 | break; |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 3734 | |
| 3735 | case ISD::BIT_CONVERT: { |
| 3736 | SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0), |
| 3737 | Node->getOperand(0)); |
| 3738 | ExpandOp(Tmp, Lo, Hi); |
| 3739 | break; |
| 3740 | } |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 3741 | |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3742 | case ISD::READCYCLECOUNTER: |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 3743 | assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) == |
| 3744 | TargetLowering::Custom && |
| 3745 | "Must custom expand ReadCycleCounter"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3746 | Lo = TLI.LowerOperation(Op, DAG); |
| 3747 | assert(Lo.Val && "Node must be custom expanded!"); |
| 3748 | Hi = Lo.getValue(1); |
Chris Lattner | 308575b | 2005-11-20 22:56:56 +0000 | [diff] [blame] | 3749 | AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain. |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3750 | LegalizeOp(Lo.getValue(2))); |
Andrew Lenharth | f70e30b | 2005-11-20 21:32:07 +0000 | [diff] [blame] | 3751 | break; |
| 3752 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3753 | // These operators cannot be expanded directly, emit them as calls to |
| 3754 | // library functions. |
| 3755 | case ISD::FP_TO_SINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 3756 | if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) { |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 3757 | SDOperand Op; |
| 3758 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3759 | case Expand: assert(0 && "cannot expand FP!"); |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3760 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 3761 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 3762 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3763 | |
Chris Lattner | f20d183 | 2005-07-30 01:40:57 +0000 | [diff] [blame] | 3764 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG); |
| 3765 | |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 3766 | // Now that the custom expander is done, expand the result, which is still |
| 3767 | // VT. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3768 | if (Op.Val) { |
| 3769 | ExpandOp(Op, Lo, Hi); |
| 3770 | break; |
| 3771 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 3772 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3773 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3774 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3775 | Lo = ExpandLibCall("__fixsfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3776 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3777 | Lo = ExpandLibCall("__fixdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3778 | break; |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3779 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3780 | case ISD::FP_TO_UINT: |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 3781 | if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3782 | SDOperand Op; |
| 3783 | switch (getTypeAction(Node->getOperand(0).getValueType())) { |
| 3784 | case Expand: assert(0 && "cannot expand FP!"); |
| 3785 | case Legal: Op = LegalizeOp(Node->getOperand(0)); break; |
| 3786 | case Promote: Op = PromoteOp (Node->getOperand(0)); break; |
| 3787 | } |
| 3788 | |
| 3789 | Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG); |
| 3790 | |
| 3791 | // Now that the custom expander is done, expand the result. |
Chris Lattner | 07dffd6 | 2005-08-26 00:14:16 +0000 | [diff] [blame] | 3792 | if (Op.Val) { |
| 3793 | ExpandOp(Op, Lo, Hi); |
| 3794 | break; |
| 3795 | } |
Chris Lattner | 80a3e94 | 2005-07-29 00:33:32 +0000 | [diff] [blame] | 3796 | } |
Jeff Cohen | d29b6aa | 2005-07-30 18:33:25 +0000 | [diff] [blame] | 3797 | |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3798 | if (Node->getOperand(0).getValueType() == MVT::f32) |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3799 | Lo = ExpandLibCall("__fixunssfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3800 | else |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3801 | Lo = ExpandLibCall("__fixunsdfdi", Node, Hi); |
Chris Lattner | 4e6c746 | 2005-01-08 19:27:05 +0000 | [diff] [blame] | 3802 | break; |
| 3803 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3804 | case ISD::SHL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3805 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3806 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3807 | if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3808 | SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3809 | Op = TLI.LowerOperation(Op, DAG); |
| 3810 | if (Op.Val) { |
| 3811 | // Now that the custom expander is done, expand the result, which is |
| 3812 | // still VT. |
| 3813 | ExpandOp(Op, Lo, Hi); |
| 3814 | break; |
| 3815 | } |
| 3816 | } |
| 3817 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3818 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3819 | if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3820 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3821 | |
| 3822 | // If this target supports SHL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3823 | TargetLowering::LegalizeAction Action = |
| 3824 | TLI.getOperationAction(ISD::SHL_PARTS, NVT); |
| 3825 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 3826 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3827 | ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3828 | break; |
| 3829 | } |
| 3830 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3831 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3832 | Lo = ExpandLibCall("__ashldi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3833 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3834 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3835 | |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3836 | case ISD::SRA: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3837 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3838 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3839 | if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3840 | SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3841 | Op = TLI.LowerOperation(Op, DAG); |
| 3842 | if (Op.Val) { |
| 3843 | // Now that the custom expander is done, expand the result, which is |
| 3844 | // still VT. |
| 3845 | ExpandOp(Op, Lo, Hi); |
| 3846 | break; |
| 3847 | } |
| 3848 | } |
| 3849 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3850 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3851 | if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3852 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3853 | |
| 3854 | // If this target supports SRA_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3855 | TargetLowering::LegalizeAction Action = |
| 3856 | TLI.getOperationAction(ISD::SRA_PARTS, NVT); |
| 3857 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 3858 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3859 | ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3860 | break; |
| 3861 | } |
| 3862 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3863 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3864 | Lo = ExpandLibCall("__ashrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3865 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | case ISD::SRL: { |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3869 | // If the target wants custom lowering, do so. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3870 | SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1)); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3871 | if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) { |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3872 | SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt); |
Chris Lattner | 50ec897 | 2005-08-31 19:01:53 +0000 | [diff] [blame] | 3873 | Op = TLI.LowerOperation(Op, DAG); |
| 3874 | if (Op.Val) { |
| 3875 | // Now that the custom expander is done, expand the result, which is |
| 3876 | // still VT. |
| 3877 | ExpandOp(Op, Lo, Hi); |
| 3878 | break; |
| 3879 | } |
| 3880 | } |
| 3881 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3882 | // If we can emit an efficient shift operation, do so now. |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3883 | if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi)) |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3884 | break; |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3885 | |
| 3886 | // If this target supports SRL_PARTS, use it. |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3887 | TargetLowering::LegalizeAction Action = |
| 3888 | TLI.getOperationAction(ISD::SRL_PARTS, NVT); |
| 3889 | if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) || |
| 3890 | Action == TargetLowering::Custom) { |
Chris Lattner | 348e93c | 2006-01-21 04:27:00 +0000 | [diff] [blame] | 3891 | ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi); |
Chris Lattner | 4759982 | 2005-04-02 03:38:53 +0000 | [diff] [blame] | 3892 | break; |
| 3893 | } |
| 3894 | |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3895 | // Otherwise, emit a libcall. |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3896 | Lo = ExpandLibCall("__lshrdi3", Node, Hi); |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3897 | break; |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3898 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 3899 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 3900 | case ISD::ADD: |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3901 | case ISD::SUB: { |
| 3902 | // If the target wants to custom expand this, let them. |
| 3903 | if (TLI.getOperationAction(Node->getOpcode(), VT) == |
| 3904 | TargetLowering::Custom) { |
| 3905 | Op = TLI.LowerOperation(Op, DAG); |
| 3906 | if (Op.Val) { |
| 3907 | ExpandOp(Op, Lo, Hi); |
| 3908 | break; |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | // Expand the subcomponents. |
| 3913 | SDOperand LHSL, LHSH, RHSL, RHSH; |
| 3914 | ExpandOp(Node->getOperand(0), LHSL, LHSH); |
| 3915 | ExpandOp(Node->getOperand(1), RHSL, RHSH); |
Nate Begeman | 551bf3f | 2006-02-17 05:43:56 +0000 | [diff] [blame] | 3916 | std::vector<MVT::ValueType> VTs; |
| 3917 | std::vector<SDOperand> LoOps, HiOps; |
| 3918 | VTs.push_back(LHSL.getValueType()); |
| 3919 | VTs.push_back(MVT::Flag); |
| 3920 | LoOps.push_back(LHSL); |
| 3921 | LoOps.push_back(RHSL); |
| 3922 | HiOps.push_back(LHSH); |
| 3923 | HiOps.push_back(RHSH); |
| 3924 | if (Node->getOpcode() == ISD::ADD) { |
| 3925 | Lo = DAG.getNode(ISD::ADDC, VTs, LoOps); |
| 3926 | HiOps.push_back(Lo.getValue(1)); |
| 3927 | Hi = DAG.getNode(ISD::ADDE, VTs, HiOps); |
| 3928 | } else { |
| 3929 | Lo = DAG.getNode(ISD::SUBC, VTs, LoOps); |
| 3930 | HiOps.push_back(Lo.getValue(1)); |
| 3931 | Hi = DAG.getNode(ISD::SUBE, VTs, HiOps); |
| 3932 | } |
Chris Lattner | 84f6788 | 2005-01-20 18:52:28 +0000 | [diff] [blame] | 3933 | break; |
Chris Lattner | 8137c9e | 2006-01-28 05:07:51 +0000 | [diff] [blame] | 3934 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 3935 | case ISD::MUL: { |
Chris Lattner | c9c60f6 | 2005-08-24 16:35:28 +0000 | [diff] [blame] | 3936 | if (TLI.isOperationLegal(ISD::MULHU, NVT)) { |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 3937 | SDOperand LL, LH, RL, RH; |
| 3938 | ExpandOp(Node->getOperand(0), LL, LH); |
| 3939 | ExpandOp(Node->getOperand(1), RL, RH); |
Nate Begeman | 56eb868 | 2005-08-30 02:44:00 +0000 | [diff] [blame] | 3940 | unsigned SH = MVT::getSizeInBits(RH.getValueType())-1; |
| 3941 | // MULHS implicitly sign extends its inputs. Check to see if ExpandOp |
| 3942 | // extended the sign bit of the low half through the upper half, and if so |
| 3943 | // emit a MULHS instead of the alternate sequence that is valid for any |
| 3944 | // i64 x i64 multiply. |
| 3945 | if (TLI.isOperationLegal(ISD::MULHS, NVT) && |
| 3946 | // is RH an extension of the sign bit of RL? |
| 3947 | RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL && |
| 3948 | RH.getOperand(1).getOpcode() == ISD::Constant && |
| 3949 | cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH && |
| 3950 | // is LH an extension of the sign bit of LL? |
| 3951 | LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL && |
| 3952 | LH.getOperand(1).getOpcode() == ISD::Constant && |
| 3953 | cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) { |
| 3954 | Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL); |
| 3955 | } else { |
| 3956 | Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL); |
| 3957 | RH = DAG.getNode(ISD::MUL, NVT, LL, RH); |
| 3958 | LH = DAG.getNode(ISD::MUL, NVT, LH, RL); |
| 3959 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH); |
| 3960 | Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH); |
| 3961 | } |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 3962 | Lo = DAG.getNode(ISD::MUL, NVT, LL, RL); |
| 3963 | } else { |
Chris Lattner | 9c6b4b8 | 2006-01-28 04:28:26 +0000 | [diff] [blame] | 3964 | Lo = ExpandLibCall("__muldi3" , Node, Hi); |
Nate Begeman | c7c1657 | 2005-04-11 03:01:51 +0000 | [diff] [blame] | 3965 | } |
| 3966 | break; |
| 3967 | } |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 3968 | case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break; |
| 3969 | case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break; |
| 3970 | case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break; |
| 3971 | case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Chris Lattner | 8339736 | 2005-12-21 18:02:52 +0000 | [diff] [blame] | 3974 | // Make sure the resultant values have been legalized themselves, unless this |
| 3975 | // is a type that requires multi-step expansion. |
| 3976 | if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) { |
| 3977 | Lo = LegalizeOp(Lo); |
| 3978 | Hi = LegalizeOp(Hi); |
| 3979 | } |
Evan Cheng | 05a2d56 | 2006-01-09 18:31:59 +0000 | [diff] [blame] | 3980 | |
| 3981 | // Remember in a map if the values will be reused later. |
| 3982 | bool isNew = |
| 3983 | ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 3984 | assert(isNew && "Value already expanded?!?"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 3987 | /// SplitVectorOp - Given an operand of MVT::Vector type, break it down into |
| 3988 | /// two smaller values of MVT::Vector type. |
| 3989 | void SelectionDAGLegalize::SplitVectorOp(SDOperand Op, SDOperand &Lo, |
| 3990 | SDOperand &Hi) { |
| 3991 | assert(Op.getValueType() == MVT::Vector && "Cannot split non-vector type!"); |
| 3992 | SDNode *Node = Op.Val; |
| 3993 | unsigned NumElements = cast<ConstantSDNode>(*(Node->op_end()-2))->getValue(); |
| 3994 | assert(NumElements > 1 && "Cannot split a single element vector!"); |
| 3995 | unsigned NewNumElts = NumElements/2; |
| 3996 | SDOperand NewNumEltsNode = DAG.getConstant(NewNumElts, MVT::i32); |
| 3997 | SDOperand TypeNode = *(Node->op_end()-1); |
| 3998 | |
| 3999 | // See if we already split it. |
| 4000 | std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I |
| 4001 | = SplitNodes.find(Op); |
| 4002 | if (I != SplitNodes.end()) { |
| 4003 | Lo = I->second.first; |
| 4004 | Hi = I->second.second; |
| 4005 | return; |
| 4006 | } |
| 4007 | |
| 4008 | switch (Node->getOpcode()) { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame^] | 4009 | default: assert(0 && "Unknown vector operation!"); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4010 | case ISD::VConstant: { |
| 4011 | std::vector<SDOperand> LoOps(Node->op_begin(), Node->op_begin()+NewNumElts); |
| 4012 | LoOps.push_back(NewNumEltsNode); |
| 4013 | LoOps.push_back(TypeNode); |
| 4014 | Lo = DAG.getNode(ISD::VConstant, MVT::Vector, LoOps); |
| 4015 | |
| 4016 | std::vector<SDOperand> HiOps(Node->op_begin()+NewNumElts, Node->op_end()-2); |
| 4017 | HiOps.push_back(NewNumEltsNode); |
| 4018 | HiOps.push_back(TypeNode); |
| 4019 | Hi = DAG.getNode(ISD::VConstant, MVT::Vector, HiOps); |
| 4020 | break; |
| 4021 | } |
| 4022 | case ISD::VADD: |
| 4023 | case ISD::VSUB: |
| 4024 | case ISD::VMUL: |
| 4025 | case ISD::VSDIV: |
| 4026 | case ISD::VUDIV: |
| 4027 | case ISD::VAND: |
| 4028 | case ISD::VOR: |
| 4029 | case ISD::VXOR: { |
| 4030 | SDOperand LL, LH, RL, RH; |
| 4031 | SplitVectorOp(Node->getOperand(0), LL, LH); |
| 4032 | SplitVectorOp(Node->getOperand(1), RL, RH); |
| 4033 | |
| 4034 | Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, |
| 4035 | NewNumEltsNode, TypeNode); |
| 4036 | Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, |
| 4037 | NewNumEltsNode, TypeNode); |
| 4038 | break; |
| 4039 | } |
| 4040 | case ISD::VLOAD: { |
| 4041 | SDOperand Ch = Node->getOperand(0); // Legalize the chain. |
| 4042 | SDOperand Ptr = Node->getOperand(1); // Legalize the pointer. |
| 4043 | MVT::ValueType EVT = cast<VTSDNode>(TypeNode)->getVT(); |
| 4044 | |
| 4045 | Lo = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4046 | unsigned IncrementSize = NewNumElts * MVT::getSizeInBits(EVT)/8; |
| 4047 | Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, |
| 4048 | getIntPtrConstant(IncrementSize)); |
| 4049 | // FIXME: This creates a bogus srcvalue! |
| 4050 | Hi = DAG.getVecLoad(NewNumElts, EVT, Ch, Ptr, Node->getOperand(2)); |
| 4051 | |
| 4052 | // Build a factor node to remember that this load is independent of the |
| 4053 | // other one. |
| 4054 | SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1), |
| 4055 | Hi.getValue(1)); |
| 4056 | |
| 4057 | // Remember that we legalized the chain. |
| 4058 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF)); |
| 4059 | if (!TLI.isLittleEndian()) |
| 4060 | std::swap(Lo, Hi); |
| 4061 | break; |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | // Remember in a map if the values will be reused later. |
| 4066 | bool isNew = |
| 4067 | SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second; |
| 4068 | assert(isNew && "Value already expanded?!?"); |
| 4069 | } |
| 4070 | |
| 4071 | |
| 4072 | /// PackVectorOp - Given an operand of MVT::Vector type, convert it into the |
| 4073 | /// equivalent operation that returns a scalar (e.g. F32) or packed value |
| 4074 | /// (e.g. MVT::V4F32). When this is called, we know that PackedVT is the right |
| 4075 | /// type for the result. |
| 4076 | SDOperand SelectionDAGLegalize::PackVectorOp(SDOperand Op, |
| 4077 | MVT::ValueType NewVT) { |
| 4078 | assert(Op.getValueType() == MVT::Vector && "Bad PackVectorOp invocation!"); |
| 4079 | SDNode *Node = Op.Val; |
| 4080 | |
| 4081 | // See if we already packed it. |
| 4082 | std::map<SDOperand, SDOperand>::iterator I = PackedNodes.find(Op); |
| 4083 | if (I != PackedNodes.end()) return I->second; |
| 4084 | |
| 4085 | SDOperand Result; |
| 4086 | switch (Node->getOpcode()) { |
| 4087 | default: assert(0 && "Unknown vector operation!"); |
| 4088 | case ISD::VADD: |
| 4089 | case ISD::VSUB: |
| 4090 | case ISD::VMUL: |
| 4091 | case ISD::VSDIV: |
| 4092 | case ISD::VUDIV: |
| 4093 | case ISD::VAND: |
| 4094 | case ISD::VOR: |
| 4095 | case ISD::VXOR: |
| 4096 | Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), NewVT), |
| 4097 | NewVT, |
| 4098 | PackVectorOp(Node->getOperand(0), NewVT), |
| 4099 | PackVectorOp(Node->getOperand(1), NewVT)); |
| 4100 | break; |
| 4101 | case ISD::VLOAD: { |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame^] | 4102 | SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 4103 | SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer. |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4104 | |
Chris Lattner | 4794a6b | 2006-03-19 00:07:49 +0000 | [diff] [blame^] | 4105 | Result = DAG.getLoad(NewVT, Ch, Ptr, Node->getOperand(2)); |
Chris Lattner | c702980 | 2006-03-18 01:44:44 +0000 | [diff] [blame] | 4106 | |
| 4107 | // Remember that we legalized the chain. |
| 4108 | AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1))); |
| 4109 | break; |
| 4110 | } |
| 4111 | case ISD::VConstant: |
| 4112 | if (!MVT::isVector(NewVT)) { |
| 4113 | Result = Node->getOperand(0); |
| 4114 | } else { |
| 4115 | // If type of bisected vector is legal, turn it into a ConstantVec (which |
| 4116 | // will be lowered to a ConstantPool or something else). Otherwise, bisect |
| 4117 | // the VConstant, and return each half as a new VConstant. |
| 4118 | std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end()-2); |
| 4119 | Result = DAG.getNode(ISD::ConstantVec, NewVT, Ops); |
| 4120 | } |
| 4121 | break; |
| 4122 | } |
| 4123 | |
| 4124 | if (TLI.isTypeLegal(NewVT)) |
| 4125 | Result = LegalizeOp(Result); |
| 4126 | bool isNew = PackedNodes.insert(std::make_pair(Op, Result)).second; |
| 4127 | assert(isNew && "Value already packed?"); |
| 4128 | return Result; |
| 4129 | } |
| 4130 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4131 | |
| 4132 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 4133 | // |
Chris Lattner | 9c32d3b | 2005-01-23 04:42:50 +0000 | [diff] [blame] | 4134 | void SelectionDAG::Legalize() { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4135 | /// run - This is the main entry point to this class. |
| 4136 | /// |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 4137 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |