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