blob: 89d764cc6cd664f946969088422cf592d8d0ed9a [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// 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 Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using 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///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
Nate Begeman89b049a2005-11-29 05:45:29 +000055 unsigned long long ValueTypeActions;
Chris Lattnerdc750592005-01-07 07:47:09 +000056
Chris Lattnerdc750592005-01-07 07:47:09 +000057 /// LegalizedNodes - For nodes that are of legal width, and that have more
58 /// than one use, this map indicates what regularized operand to use. This
59 /// allows us to avoid legalizing the same thing more than once.
60 std::map<SDOperand, SDOperand> LegalizedNodes;
61
Chris Lattner1f2c9d82005-01-15 05:21:40 +000062 /// PromotedNodes - For nodes that are below legal width, and that have more
63 /// than one use, this map indicates what promoted value to use. This allows
64 /// us to avoid promoting the same thing more than once.
65 std::map<SDOperand, SDOperand> PromotedNodes;
66
Chris Lattnerdc750592005-01-07 07:47:09 +000067 /// ExpandedNodes - For nodes that need to be expanded, and which have more
68 /// than one use, this map indicates which which operands are the expanded
69 /// version of the input. This allows us to avoid expanding the same node
70 /// more than once.
71 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
72
Chris Lattnerea4ca942005-01-07 22:28:47 +000073 void AddLegalizedOperand(SDOperand From, SDOperand To) {
Chris Lattner2af3ee42005-12-20 00:53:54 +000074 LegalizedNodes.insert(std::make_pair(From, To));
75 // If someone requests legalization of the new node, return itself.
76 if (From != To)
77 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattnerea4ca942005-01-07 22:28:47 +000078 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000079 void AddPromotedOperand(SDOperand From, SDOperand To) {
80 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
Chris Lattner2af3ee42005-12-20 00:53:54 +000082 // If someone requests legalization of the new node, return itself.
83 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner1f2c9d82005-01-15 05:21:40 +000084 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000089
Chris Lattnerdc750592005-01-07 07:47:09 +000090 /// getTypeAction - Return how we should legalize values of this type, either
91 /// it is already legal or we need to expand it into multiple registers of
92 /// smaller integer type, or we need to promote it to a larger type.
93 LegalizeAction getTypeAction(MVT::ValueType VT) const {
94 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
95 }
96
97 /// isTypeLegal - Return true if this type is legal on this target.
98 ///
99 bool isTypeLegal(MVT::ValueType VT) const {
100 return getTypeAction(VT) == Legal;
101 }
102
Chris Lattnerdc750592005-01-07 07:47:09 +0000103 void LegalizeDAG();
104
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000105private:
106
Chris Lattnerdc750592005-01-07 07:47:09 +0000107 SDOperand LegalizeOp(SDOperand O);
108 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000109 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000110
Chris Lattneraac464e2005-01-21 06:05:23 +0000111 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
112 SDOperand &Hi);
113 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
114 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000115
Chris Lattner36e663d2005-12-23 00:16:34 +0000116 SDOperand ExpandBIT_CONVERT(MVT::ValueType DestVT, SDOperand SrcOp);
Jim Laskeyf2516a92005-08-17 00:39:29 +0000117 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
118 SDOperand LegalOp,
119 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000120 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
121 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000122 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
123 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000124
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000125 SDOperand ExpandBSWAP(SDOperand Op);
126 SDOperand ExpandBitCount(unsigned Opc, SDOperand Op);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
Chris Lattnera5bf1032005-05-12 04:49:08 +0000131 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
Chris Lattner301015a2005-11-19 05:51:46 +0000139static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000140 switch (VecOp) {
141 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-11-19 00:36:38 +0000142 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 Lattnerdc750592005-01-07 07:47:09 +0000147
Chris Lattner4add7e32005-01-23 04:42:50 +0000148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
149 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
150 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman89b049a2005-11-29 05:45:29 +0000151 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattnerdc750592005-01-07 07:47:09 +0000152 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000153}
154
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000155/// 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.
157static 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 Lattner4bbbb9e2005-10-06 01:20:27 +0000171 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
172 ComputeTopDownOrdering(*UI, Order, Visited);
173}
174
Chris Lattner44fe26f2005-07-29 00:11:56 +0000175
Chris Lattnerdc750592005-01-07 07:47:09 +0000176void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000177 // 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 Lattner4bbbb9e2005-10-06 01:20:27 +0000181 // 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 Lattner9cfccfb2005-10-02 17:49:46 +0000185
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000186 // 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 Lattnerbf4f23322005-11-09 23:47:37 +0000190 if (I->getNumOperands() == 0) {
191 Visited[I] = 0 - 1U;
192 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000193 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000194 }
195
Chris Lattnerbf4f23322005-11-09 23:47:37 +0000196 assert(Order.size() == Visited.size() &&
197 Order.size() ==
198 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000199 "Error: DAG is cyclic!");
200 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000201
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000202 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 Lattnerdc750592005-01-07 07:47:09 +0000221 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000222 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
223 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000224
225 ExpandedNodes.clear();
226 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000227 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000228
229 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000230 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000231}
232
233SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000234 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000235 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000236 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000237
Chris Lattnerdc750592005-01-07 07:47:09 +0000238 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000239 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000240 if (Node->getNumValues() > 1) {
241 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
242 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000243 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 Lattner1f2c9d82005-01-15 05:21:40 +0000252 PromoteOp(Op.getValue(i));
253 assert(LegalizedNodes.count(Op) &&
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000254 "Promotion didn't add legal operands!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000255 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000256 }
257 }
258
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000259 // Note that LegalizeOp may be reentered even from single-use nodes, which
260 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000261 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
262 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000263
Nate Begemane5b86d72005-08-10 20:51:12 +0000264 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000265 SDOperand Result = Op;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000266 bool isCustom = false;
267
Chris Lattnerdc750592005-01-07 07:47:09 +0000268 switch (Node->getOpcode()) {
Chris Lattnereb637512006-01-28 08:31:04 +0000269 case ISD::FrameIndex:
270 case ISD::EntryToken:
271 case ISD::Register:
272 case ISD::BasicBlock:
273 case ISD::TargetFrameIndex:
274 case ISD::TargetConstant:
Chris Lattner758b0ac2006-01-29 06:26:56 +0000275 case ISD::TargetConstantFP:
276 case ISD::TargetConstantVec:
Chris Lattnereb637512006-01-28 08:31:04 +0000277 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 Lattnerdc750592005-01-07 07:47:09 +0000288 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000289 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 Lattnerdc750592005-01-07 07:47:09 +0000312 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
313 assert(0 && "Do not know how to legalize this operator!");
314 abort();
Chris Lattnerdc750592005-01-07 07:47:09 +0000315 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000316 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000317 case ISD::ConstantPool: // Nothing to do.
Chris Lattner45ca1c02005-11-17 06:41:44 +0000318 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
319 default: assert(0 && "This action is not supported yet!");
Chris Lattnereb637512006-01-28 08:31:04 +0000320 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 Lattner45ca1c02005-11-17 06:41:44 +0000324 case TargetLowering::Legal:
Chris Lattner45ca1c02005-11-17 06:41:44 +0000325 break;
326 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000327 break;
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000328 case ISD::AssertSext:
329 case ISD::AssertZext:
330 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000331 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerd9af1aa2005-09-02 01:15:01 +0000332 break;
Chris Lattner44c28c22005-11-20 22:56:56 +0000333 case ISD::MERGE_VALUES:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000334 // Legalize eliminates MERGE_VALUES nodes.
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000335 Result = Node->getOperand(Op.ResNo);
336 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000337 case ISD::CopyFromReg:
338 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000339 Result = Op.getValue(0);
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000340 if (Node->getNumValues() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000341 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000342 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000343 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000344 if (Node->getNumOperands() == 3) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000345 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000346 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
347 } else {
348 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
349 }
Chris Lattnere3c67e92005-12-18 15:27:43 +0000350 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
351 }
Chris Lattnereb6614d2005-01-28 06:27:38 +0000352 // 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 Begemancda9aa72005-04-01 22:34:39 +0000357 case ISD::UNDEF: {
358 MVT::ValueType VT = Op.getValueType();
359 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000360 default: assert(0 && "This action is not supported yet!");
361 case TargetLowering::Expand:
Nate Begemancda9aa72005-04-01 22:34:39 +0000362 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 Begeman69d39432005-04-02 00:41:14 +0000369 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000370 break;
371 }
372 break;
373 }
Chris Lattner435b4022005-11-29 06:21:05 +0000374
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 Laskey7c462762005-12-16 22:45:29 +0000382 case TargetLowering::Expand: {
Jim Laskey219d5592006-01-04 22:28:25 +0000383 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey762e9ec2006-01-05 01:25:28 +0000384 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 Laskeyb9966022006-01-17 17:31:53 +0000392 unsigned SrcFile = DebugInfo->RecordSource(DirName, FName);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000393
Jim Laskey9e296be2005-12-21 20:51:37 +0000394 std::vector<SDOperand> Ops;
395 Ops.push_back(Tmp1); // chain
Jim Laskey762e9ec2006-01-05 01:25:28 +0000396 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 Laskey9e296be2005-12-21 20:51:37 +0000411 } else {
412 Result = Tmp1; // chain
413 }
Chris Lattner435b4022005-11-29 06:21:05 +0000414 break;
Chris Lattnerc06da622005-12-18 23:54:29 +0000415 }
Chris Lattner435b4022005-11-29 06:21:05 +0000416 case TargetLowering::Legal:
Chris Lattner05b0b452005-12-01 18:21:35 +0000417 if (Tmp1 != Node->getOperand(0) ||
418 getTypeAction(Node->getOperand(1).getValueType()) == Promote) {
Chris Lattner435b4022005-11-29 06:21:05 +0000419 std::vector<SDOperand> Ops;
420 Ops.push_back(Tmp1);
Chris Lattner05b0b452005-12-01 18:21:35 +0000421 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 Lattner435b4022005-11-29 06:21:05 +0000429 Ops.push_back(Node->getOperand(3)); // filename must be legal.
430 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000431 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner435b4022005-11-29 06:21:05 +0000432 }
433 break;
434 }
435 break;
Jim Laskey7c462762005-12-16 22:45:29 +0000436
437 case ISD::DEBUG_LOC:
Jim Laskey762e9ec2006-01-05 01:25:28 +0000438 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskey7c462762005-12-16 22:45:29 +0000439 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskey7c462762005-12-16 22:45:29 +0000440 default: assert(0 && "This action is not supported yet!");
Jim Laskey762e9ec2006-01-05 01:25:28 +0000441 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 Lattnerd02b0542006-01-28 10:58:55 +0000446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskey762e9ec2006-01-05 01:25:28 +0000447 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 Laskey762e9ec2006-01-05 01:25:28 +0000454 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 Lattnerd02b0542006-01-28 10:58:55 +0000458 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Jim Laskey7c462762005-12-16 22:45:29 +0000459 break;
460 }
461 break;
Chris Lattner435b4022005-11-29 06:21:05 +0000462
Chris Lattnerdc750592005-01-07 07:47:09 +0000463 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 Lattnerdc750592005-01-07 07:47:09 +0000475 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 Lattner758b0ac2006-01-29 06:26:56 +0000486 // 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 Lattnerdc750592005-01-07 07:47:09 +0000502 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000503 bool Extend = false;
504
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000505 // 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 Lattnerdc750592005-01-07 07:47:09 +0000509 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 Lattnerbc7497d2005-01-28 22:58:25 +0000513 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
514 // Only do this if the target has a native EXTLOAD instruction from
515 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000516 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000517 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
518 VT = MVT::f32;
519 Extend = true;
520 }
Misha Brukman835702a2005-04-21 22:36:52 +0000521
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000522 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000523 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000524 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
525 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000526 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000527 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
528 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000529 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000530 }
531 break;
532 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000533 case ISD::ConstantVec: {
534 // We assume that vector constants are not legal, and will be immediately
535 // spilled to the constant pool.
536 //
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000537 // FIXME: Allow custom lowering to TargetConstantVec's.
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000538 //
539 // Create a ConstantPacked, and put it in the constant pool.
540 std::vector<Constant*> CV;
541 MVT::ValueType VT = Node->getValueType(0);
542 for (unsigned I = 0, E = Node->getNumOperands(); I < E; ++I) {
543 SDOperand OpN = Node->getOperand(I);
544 const Type* OpNTy = MVT::getTypeForValueType(OpN.getValueType());
545 if (MVT::isFloatingPoint(VT))
546 CV.push_back(ConstantFP::get(OpNTy,
547 cast<ConstantFPSDNode>(OpN)->getValue()));
548 else
549 CV.push_back(ConstantUInt::get(OpNTy,
550 cast<ConstantSDNode>(OpN)->getValue()));
551 }
552 Constant *CP = ConstantPacked::get(CV);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000553 SDOperand CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000554 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
555 break;
556 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000557 case ISD::TokenFactor:
558 if (Node->getNumOperands() == 2) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000559 Tmp1 = LegalizeOp(Node->getOperand(0));
560 Tmp2 = LegalizeOp(Node->getOperand(1));
561 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
562 } else if (Node->getNumOperands() == 3) {
563 Tmp1 = LegalizeOp(Node->getOperand(0));
564 Tmp2 = LegalizeOp(Node->getOperand(1));
565 Tmp3 = LegalizeOp(Node->getOperand(2));
566 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000567 } else {
568 std::vector<SDOperand> Ops;
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000569 // Legalize the operands.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000570 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
571 Ops.push_back(LegalizeOp(Node->getOperand(i)));
572 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000573 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000574 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000575
Chris Lattner2dce7032005-05-12 23:24:06 +0000576 case ISD::CALLSEQ_START:
577 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000579 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000580 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000581 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000582 Node->setAdjCallChain(Tmp1);
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +0000583
584 // If this has a flag input, do legalize it.
585 if (Node->getOperand(Node->getNumOperands()-1).getValueType() == MVT::Flag){
586 Tmp1 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
587 if (Tmp1 != Node->getOperand(Node->getNumOperands()-1))
588 Node->setAdjCallFlag(Tmp1);
589 }
Nate Begeman54fb5002005-10-04 00:37:37 +0000590
Chris Lattner2dce7032005-05-12 23:24:06 +0000591 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000592 // nodes are treated specially and are mutated in place. This makes the dag
593 // legalization process more efficient and also makes libcall insertion
594 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000595 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000596 case ISD::DYNAMIC_STACKALLOC: {
Chris Lattnerec26b482005-01-09 19:03:49 +0000597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
598 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
599 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerec26b482005-01-09 19:03:49 +0000601
Chris Lattnerd02b0542006-01-28 10:58:55 +0000602 Tmp1 = Result.getValue(0);
Chris Lattner2d591422006-01-15 08:43:08 +0000603 Tmp2 = Result.getValue(1);
Evan Cheng7f4ec822006-01-11 22:14:47 +0000604 switch (TLI.getOperationAction(Node->getOpcode(),
605 Node->getValueType(0))) {
606 default: assert(0 && "This action is not supported yet!");
Chris Lattner59b82f92006-01-15 08:54:32 +0000607 case TargetLowering::Expand: {
608 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
609 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
610 " not tell us which reg is the stack pointer!");
611 SDOperand Chain = Tmp1.getOperand(0);
612 SDOperand Size = Tmp2.getOperand(1);
613 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, Node->getValueType(0));
614 Tmp1 = DAG.getNode(ISD::SUB, Node->getValueType(0), SP, Size); // Value
615 Tmp2 = DAG.getCopyToReg(SP.getValue(1), SPReg, Tmp1); // Output chain
Chris Lattnerd02b0542006-01-28 10:58:55 +0000616 Tmp1 = LegalizeOp(Tmp1);
617 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000618 break;
619 }
620 case TargetLowering::Custom:
Chris Lattner2d591422006-01-15 08:43:08 +0000621 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
622 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000623 Tmp1 = LegalizeOp(Tmp3);
624 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Cheng7f4ec822006-01-11 22:14:47 +0000625 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000626 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000627 case TargetLowering::Legal:
Chris Lattner59b82f92006-01-15 08:54:32 +0000628 break;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000629 }
Chris Lattner59b82f92006-01-15 08:54:32 +0000630 // Since this op produce two values, make sure to remember that we
631 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000632 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
633 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
Chris Lattner59b82f92006-01-15 08:54:32 +0000634 return Op.ResNo ? Tmp2 : Tmp1;
Evan Cheng7f4ec822006-01-11 22:14:47 +0000635 }
Chris Lattner476e67b2006-01-26 22:24:51 +0000636 case ISD::INLINEASM:
637 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize Chain.
638 Tmp2 = Node->getOperand(Node->getNumOperands()-1);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000639 if (Tmp2.getValueType() == MVT::Flag) // Legalize Flag if it exists.
Chris Lattner476e67b2006-01-26 22:24:51 +0000640 Tmp2 = Tmp3 = SDOperand(0, 0);
641 else
642 Tmp3 = LegalizeOp(Tmp2);
643
644 if (Tmp1 != Node->getOperand(0) || Tmp2 != Tmp3) {
645 std::vector<SDOperand> Ops(Node->op_begin(), Node->op_end());
646 Ops[0] = Tmp1;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000647 if (Tmp3.Val) Ops.back() = Tmp3;
648 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner476e67b2006-01-26 22:24:51 +0000649 }
650
651 // INLINE asm returns a chain and flag, make sure to add both to the map.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000652 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner476e67b2006-01-26 22:24:51 +0000653 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
654 return Result.getValue(Op.ResNo);
Chris Lattner68a12142005-01-07 22:12:08 +0000655 case ISD::BR:
656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000657 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner68a12142005-01-07 22:12:08 +0000658 break;
659
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000660 case ISD::BRCOND:
661 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000662
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000663 switch (getTypeAction(Node->getOperand(1).getValueType())) {
664 case Expand: assert(0 && "It's impossible to expand bools");
665 case Legal:
666 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
667 break;
668 case Promote:
669 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
670 break;
671 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000672
673 // Basic block destination (Op#2) is always legal.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000674 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000675
676 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
677 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000678 case TargetLowering::Legal: break;
679 case TargetLowering::Custom:
680 Tmp1 = TLI.LowerOperation(Result, DAG);
681 if (Tmp1.Val) Result = Tmp1;
682 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000683 case TargetLowering::Expand:
684 // Expand brcond's setcc into its constituent parts and create a BR_CC
685 // Node.
686 if (Tmp2.getOpcode() == ISD::SETCC) {
687 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
688 Tmp2.getOperand(0), Tmp2.getOperand(1),
689 Node->getOperand(2));
690 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000691 // Make sure the condition is either zero or one. It may have been
692 // promoted from something else.
693 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
694
Nate Begeman371e4952005-08-16 19:49:35 +0000695 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
696 DAG.getCondCode(ISD::SETNE), Tmp2,
697 DAG.getConstant(0, Tmp2.getValueType()),
698 Node->getOperand(2));
699 }
700 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000701 }
702 break;
703 case ISD::BR_CC:
704 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000705 if (!isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000706 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
707 Node->getOperand(2), // LHS
708 Node->getOperand(3), // RHS
709 Node->getOperand(1)));
710 // If we get a SETCC back from legalizing the SETCC node we just
711 // created, then use its LHS, RHS, and CC directly in creating a new
712 // node. Otherwise, select between the true and false value based on
713 // comparing the result of the legalized with zero.
714 if (Tmp2.getOpcode() == ISD::SETCC) {
715 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
716 Tmp2.getOperand(0), Tmp2.getOperand(1),
717 Node->getOperand(4));
718 } else {
719 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
720 DAG.getCondCode(ISD::SETNE),
721 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
722 Node->getOperand(4));
723 }
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000724 break;
725 }
726
727 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
728 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
729
Chris Lattnerd02b0542006-01-28 10:58:55 +0000730 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
731 Tmp3, Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000732
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000733 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
734 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000735 case TargetLowering::Legal: break;
736 case TargetLowering::Custom:
737 Tmp4 = TLI.LowerOperation(Result, DAG);
738 if (Tmp4.Val) Result = Tmp4;
Chris Lattnerbf0bd992005-12-17 23:46:46 +0000739 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000740 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000741 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000742 case ISD::BRCONDTWOWAY:
743 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
744 switch (getTypeAction(Node->getOperand(1).getValueType())) {
745 case Expand: assert(0 && "It's impossible to expand bools");
746 case Legal:
747 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
748 break;
749 case Promote:
750 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
751 break;
752 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000753
Chris Lattnerfd986782005-04-09 03:30:19 +0000754 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
755 // pair.
756 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
757 case TargetLowering::Promote:
758 default: assert(0 && "This action is not supported yet!");
759 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000760 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
761 Node->getOperand(3));
Chris Lattnerfd986782005-04-09 03:30:19 +0000762 break;
763 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000764 // If BRTWOWAY_CC is legal for this target, then simply expand this node
765 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
766 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000767 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000768 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattner678da982006-01-29 06:00:45 +0000769 Tmp3 = Tmp2.getOperand(0);
770 Tmp4 = Tmp2.getOperand(1);
771 Tmp2 = Tmp2.getOperand(2);
Nate Begeman371e4952005-08-16 19:49:35 +0000772 } else {
Chris Lattner678da982006-01-29 06:00:45 +0000773 Tmp3 = Tmp2;
774 Tmp4 = DAG.getConstant(0, Tmp2.getValueType());
775 Tmp2 = DAG.getCondCode(ISD::SETNE);
Nate Begeman371e4952005-08-16 19:49:35 +0000776 }
Chris Lattner678da982006-01-29 06:00:45 +0000777 std::vector<SDOperand> Ops;
778 Ops.push_back(Tmp1);
779 Ops.push_back(Tmp2);
780 Ops.push_back(Tmp3);
781 Ops.push_back(Tmp4);
782 Ops.push_back(Node->getOperand(2));
783 Ops.push_back(Node->getOperand(3));
784 Result = DAG.getNode(ISD::BRTWOWAY_CC, MVT::Other, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000785 } else {
786 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000787 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000788 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
789 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000790 break;
791 }
792 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000793 case ISD::BRTWOWAY_CC:
794 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000795 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000796 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
797 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
798 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
799 Tmp3 != Node->getOperand(3)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000800 std::vector<SDOperand> Ops;
801 Ops.push_back(Tmp1);
802 Ops.push_back(Node->getOperand(1));
803 Ops.push_back(Tmp2);
804 Ops.push_back(Tmp3);
805 Ops.push_back(Node->getOperand(4));
806 Ops.push_back(Node->getOperand(5));
807 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000808 }
809 break;
810 } else {
811 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
812 Node->getOperand(2), // LHS
813 Node->getOperand(3), // RHS
814 Node->getOperand(1)));
815 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
816 // pair.
817 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
818 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000819 case TargetLowering::Legal: {
Nate Begeman371e4952005-08-16 19:49:35 +0000820 // If we get a SETCC back from legalizing the SETCC node we just
821 // created, then use its LHS, RHS, and CC directly in creating a new
822 // node. Otherwise, select between the true and false value based on
823 // comparing the result of the legalized with zero.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000824 std::vector<SDOperand> Ops;
825 Ops.push_back(Tmp1);
Nate Begeman371e4952005-08-16 19:49:35 +0000826 if (Tmp2.getOpcode() == ISD::SETCC) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000827 Ops.push_back(Tmp2.getOperand(2));
828 Ops.push_back(Tmp2.getOperand(0));
829 Ops.push_back(Tmp2.getOperand(1));
Nate Begeman371e4952005-08-16 19:49:35 +0000830 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000831 Ops.push_back(DAG.getCondCode(ISD::SETNE));
832 Ops.push_back(Tmp2);
833 Ops.push_back(DAG.getConstant(0, Tmp2.getValueType()));
Nate Begeman371e4952005-08-16 19:49:35 +0000834 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000835 Ops.push_back(Node->getOperand(4));
836 Ops.push_back(Node->getOperand(5));
837 Result = DAG.UpdateNodeOperands(Result, Ops);
Nate Begeman371e4952005-08-16 19:49:35 +0000838 break;
Chris Lattnerd02b0542006-01-28 10:58:55 +0000839 }
Nate Begeman371e4952005-08-16 19:49:35 +0000840 case TargetLowering::Expand:
841 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
842 Node->getOperand(4));
843 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
844 break;
845 }
846 }
847 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000848 case ISD::LOAD: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000849 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
850 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000851
Evan Cheng31d15fa2005-12-23 07:29:34 +0000852 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000853 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
854 Tmp2 = Result.getValue(0);
855 Tmp3 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000856
Evan Cheng31d15fa2005-12-23 07:29:34 +0000857 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
858 default: assert(0 && "This action is not supported yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000859 case TargetLowering::Legal: break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000860 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000861 Tmp1 = TLI.LowerOperation(Tmp2, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000862 if (Tmp1.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000863 Tmp2 = LegalizeOp(Tmp1);
864 Tmp3 = LegalizeOp(Tmp1.getValue(1));
Evan Cheng31d15fa2005-12-23 07:29:34 +0000865 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000866 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000867 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000868 // Since loads produce two values, make sure to remember that we
869 // legalized both of them.
870 AddLegalizedOperand(SDOperand(Node, 0), Tmp2);
871 AddLegalizedOperand(SDOperand(Node, 1), Tmp3);
872 return Op.ResNo ? Tmp3 : Tmp2;
Evan Cheng31d15fa2005-12-23 07:29:34 +0000873 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000874 case ISD::EXTLOAD:
875 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000876 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000877 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
878 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000879
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000880 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000881 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000882 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000883 case TargetLowering::Promote:
884 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
886 DAG.getValueType(MVT::i8));
887 Tmp1 = Result.getValue(0);
888 Tmp2 = Result.getValue(1);
889 break;
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000890 case TargetLowering::Custom:
891 isCustom = true;
892 // FALLTHROUGH
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000893 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +0000894 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2),
895 Node->getOperand(3));
896 Tmp1 = Result.getValue(0);
897 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000898
899 if (isCustom) {
900 Tmp3 = TLI.LowerOperation(Tmp3, DAG);
901 if (Tmp3.Val) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000902 Tmp1 = LegalizeOp(Tmp3);
903 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner9dcce6d2006-01-28 07:39:30 +0000904 }
905 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000906 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000907 case TargetLowering::Expand:
Chris Lattner2af3ee42005-12-20 00:53:54 +0000908 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000909 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
910 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000911 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000912 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
913 Tmp2 = LegalizeOp(Load.getValue(1));
914 break;
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000915 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000916 assert(Node->getOpcode() != ISD::EXTLOAD &&
917 "EXTLOAD should always be supported!");
918 // Turn the unsupported load into an EXTLOAD followed by an explicit
919 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000920 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
921 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000922 SDOperand ValRes;
923 if (Node->getOpcode() == ISD::SEXTLOAD)
924 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000925 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000926 else
927 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnerd02b0542006-01-28 10:58:55 +0000928 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
929 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
930 break;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000931 }
Chris Lattnerd02b0542006-01-28 10:58:55 +0000932 // Since loads produce two values, make sure to remember that we legalized
933 // both of them.
934 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
935 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
936 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000937 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000938 case ISD::EXTRACT_ELEMENT: {
939 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
940 switch (getTypeAction(OpTy)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000941 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5172ce62005-10-19 00:06:56 +0000942 case Legal:
943 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
944 // 1 -> Hi
945 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
946 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
947 TLI.getShiftAmountTy()));
948 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
949 } else {
950 // 0 -> Lo
951 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
952 Node->getOperand(0));
953 }
Nate Begeman5172ce62005-10-19 00:06:56 +0000954 break;
955 case Expand:
956 // Get both the low and high parts.
957 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
958 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
959 Result = Tmp2; // 1 -> Hi
960 else
961 Result = Tmp1; // 0 -> Lo
962 break;
963 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000964 break;
Nate Begeman5172ce62005-10-19 00:06:56 +0000965 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000966
967 case ISD::CopyToReg:
968 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000969
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000970 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +0000971 "Register type must be legal!");
Chris Lattnere3c67e92005-12-18 15:27:43 +0000972 // Legalize the incoming value (must be a legal type).
Chris Lattner33182322005-08-16 21:55:35 +0000973 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000974 if (Node->getNumValues() == 1) {
Chris Lattnerd02b0542006-01-28 10:58:55 +0000975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000976 } else {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000977 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerd02b0542006-01-28 10:58:55 +0000978 if (Node->getNumOperands() == 4) {
Chris Lattnerebcfa0c22005-12-18 15:36:21 +0000979 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000980 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
981 Tmp3);
982 } else {
983 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattnere3c67e92005-12-18 15:27:43 +0000984 }
985
986 // Since this produces two values, make sure to remember that we legalized
987 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +0000988 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnere3c67e92005-12-18 15:27:43 +0000989 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +0000990 return Result;
Chris Lattnere3c67e92005-12-18 15:27:43 +0000991 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000992 break;
993
994 case ISD::RET:
995 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
996 switch (Node->getNumOperands()) {
997 case 2: // ret val
998 switch (getTypeAction(Node->getOperand(1).getValueType())) {
999 case Legal:
1000 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001001 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001002 break;
1003 case Expand: {
1004 SDOperand Lo, Hi;
1005 ExpandOp(Node->getOperand(1), Lo, Hi);
1006 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001007 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001008 }
1009 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001010 Tmp2 = PromoteOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001011 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1012 Result = LegalizeOp(Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001013 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001014 }
1015 break;
1016 case 1: // ret void
Chris Lattnerd02b0542006-01-28 10:58:55 +00001017 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerdc750592005-01-07 07:47:09 +00001018 break;
1019 default: { // ret <values>
1020 std::vector<SDOperand> NewValues;
1021 NewValues.push_back(Tmp1);
1022 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1023 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1024 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001025 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001026 break;
1027 case Expand: {
1028 SDOperand Lo, Hi;
1029 ExpandOp(Node->getOperand(i), Lo, Hi);
1030 NewValues.push_back(Lo);
1031 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001032 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001033 }
1034 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001035 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001036 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001037
1038 if (NewValues.size() == Node->getNumOperands())
1039 Result = DAG.UpdateNodeOperands(Result, NewValues);
1040 else
1041 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
Chris Lattnerdc750592005-01-07 07:47:09 +00001042 break;
1043 }
1044 }
Evan Chengf35b1c82006-01-06 00:41:43 +00001045
Chris Lattnerfae8afb2006-01-06 05:47:48 +00001046 switch (TLI.getOperationAction(Node->getOpcode(),
1047 Node->getValueType(0))) {
Evan Chengf35b1c82006-01-06 00:41:43 +00001048 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001049 case TargetLowering::Legal: break;
1050 case TargetLowering::Custom:
1051 Tmp1 = TLI.LowerOperation(Result, DAG);
1052 if (Tmp1.Val) Result = Tmp1;
Evan Chengf35b1c82006-01-06 00:41:43 +00001053 break;
1054 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001055 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001056 case ISD::STORE: {
Chris Lattnerdc750592005-01-07 07:47:09 +00001057 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1058 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1059
Chris Lattnere69daaf2005-01-08 06:25:56 +00001060 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001061 // FIXME: We shouldn't do this for TargetConstantFP's.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001062 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001063 if (CFP->getValueType(0) == MVT::f32) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001064 Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001065 } else {
1066 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001067 Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
Chris Lattnere69daaf2005-01-08 06:25:56 +00001068 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001069 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Tmp3, Tmp2,
1070 Node->getOperand(3));
Chris Lattnerf9a1e3a2006-01-24 05:48:21 +00001071 break;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001072 }
1073
Chris Lattnerdc750592005-01-07 07:47:09 +00001074 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1075 case Legal: {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001076 Tmp3 = LegalizeOp(Node->getOperand(1));
1077 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
1078 Node->getOperand(3));
Evan Cheng31d15fa2005-12-23 07:29:34 +00001079
Chris Lattnerd02b0542006-01-28 10:58:55 +00001080 MVT::ValueType VT = Tmp3.getValueType();
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001081 switch (TLI.getOperationAction(ISD::STORE, VT)) {
1082 default: assert(0 && "This action is not supported yet!");
1083 case TargetLowering::Legal: break;
1084 case TargetLowering::Custom:
1085 Tmp1 = TLI.LowerOperation(Result, DAG);
1086 if (Tmp1.Val) Result = Tmp1;
1087 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001088 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001089 break;
1090 }
1091 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001092 // Truncate the value and store the result.
1093 Tmp3 = PromoteOp(Node->getOperand(1));
1094 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001095 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001096 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001097 break;
1098
Chris Lattnerdc750592005-01-07 07:47:09 +00001099 case Expand:
1100 SDOperand Lo, Hi;
1101 ExpandOp(Node->getOperand(1), Lo, Hi);
1102
1103 if (!TLI.isLittleEndian())
1104 std::swap(Lo, Hi);
1105
Chris Lattner55e9cde2005-05-11 04:51:16 +00001106 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1107 Node->getOperand(3));
Nate Begemand37c1312005-11-22 18:16:00 +00001108 // If this is a vector type, then we have to calculate the increment as
1109 // the product of the element size in bytes, and the number of elements
1110 // in the high half of the vector.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001111 unsigned IncrementSize;
Nate Begemand37c1312005-11-22 18:16:00 +00001112 if (MVT::Vector == Hi.getValueType()) {
1113 unsigned NumElems = cast<ConstantSDNode>(Hi.getOperand(2))->getValue();
1114 MVT::ValueType EVT = cast<VTSDNode>(Hi.getOperand(3))->getVT();
1115 IncrementSize = NumElems * MVT::getSizeInBits(EVT)/8;
1116 } else {
1117 IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1118 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001119 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1120 getIntPtrConstant(IncrementSize));
1121 assert(isTypeLegal(Tmp2.getValueType()) &&
1122 "Pointers must be legal!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001123 // FIXME: This sets the srcvalue of both halves to be the same, which is
1124 // wrong.
Chris Lattner55e9cde2005-05-11 04:51:16 +00001125 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1126 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001127 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1128 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001129 }
1130 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001131 }
Andrew Lenharthdec53922005-03-31 21:24:06 +00001132 case ISD::PCMARKER:
1133 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001134 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001135 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001136 case ISD::STACKSAVE:
1137 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001138 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1139 Tmp1 = Result.getValue(0);
1140 Tmp2 = Result.getValue(1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001141
Chris Lattnerb3266452006-01-13 02:50:02 +00001142 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
1143 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001144 case TargetLowering::Legal: break;
1145 case TargetLowering::Custom:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001146 Tmp3 = TLI.LowerOperation(Result, DAG);
1147 if (Tmp3.Val) {
1148 Tmp1 = LegalizeOp(Tmp3);
1149 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattnerb3266452006-01-13 02:50:02 +00001150 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001151 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001152 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001153 // Expand to CopyFromReg if the target set
1154 // StackPointerRegisterToSaveRestore.
1155 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001156 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattnered9b3e12006-01-13 17:48:44 +00001157 Node->getValueType(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001158 Tmp2 = Tmp1.getValue(1);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001159 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001160 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
1161 Tmp2 = Node->getOperand(0);
Chris Lattnered9b3e12006-01-13 17:48:44 +00001162 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001163 break;
Chris Lattnerb3266452006-01-13 02:50:02 +00001164 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001165
1166 // Since stacksave produce two values, make sure to remember that we
1167 // legalized both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001168 AddLegalizedOperand(SDOperand(Node, 0), Tmp1);
1169 AddLegalizedOperand(SDOperand(Node, 1), Tmp2);
1170 return Op.ResNo ? Tmp2 : Tmp1;
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001171
Chris Lattnerb3266452006-01-13 02:50:02 +00001172 case ISD::STACKRESTORE:
1173 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1174 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001175 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerb3266452006-01-13 02:50:02 +00001176
1177 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
1178 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001179 case TargetLowering::Legal: break;
1180 case TargetLowering::Custom:
1181 Tmp1 = TLI.LowerOperation(Result, DAG);
1182 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerb3266452006-01-13 02:50:02 +00001183 break;
1184 case TargetLowering::Expand:
Chris Lattnered9b3e12006-01-13 17:48:44 +00001185 // Expand to CopyToReg if the target set
1186 // StackPointerRegisterToSaveRestore.
1187 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
1188 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
1189 } else {
1190 Result = Tmp1;
1191 }
Chris Lattnerb3266452006-01-13 02:50:02 +00001192 break;
1193 }
1194 break;
1195
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001196 case ISD::READCYCLECOUNTER:
1197 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerd02b0542006-01-28 10:58:55 +00001198 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth73420b32005-12-02 04:56:24 +00001199
1200 // Since rdcc produce two values, make sure to remember that we legalized
1201 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001202 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Andrew Lenharth73420b32005-12-02 04:56:24 +00001203 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001204 return Result;
Andrew Lenharth627cbd42005-11-20 21:32:07 +00001205
Evan Cheng31d15fa2005-12-23 07:29:34 +00001206 case ISD::TRUNCSTORE: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001207 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1208 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1209
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001210 assert(isTypeLegal(Node->getOperand(1).getValueType()) &&
1211 "Cannot handle illegal TRUNCSTORE yet!");
1212 Tmp2 = LegalizeOp(Node->getOperand(1));
1213
1214 // The only promote case we handle is TRUNCSTORE:i1 X into
1215 // -> TRUNCSTORE:i8 (and X, 1)
1216 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1217 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1218 TargetLowering::Promote) {
1219 // Promote the bool to a mask then store.
1220 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1221 DAG.getConstant(1, Tmp2.getValueType()));
1222 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1223 Node->getOperand(3), DAG.getValueType(MVT::i8));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001224
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001225 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1226 Tmp3 != Node->getOperand(2)) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00001227 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1228 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001229 }
Evan Cheng31d15fa2005-12-23 07:29:34 +00001230
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001231 MVT::ValueType StVT = cast<VTSDNode>(Result.Val->getOperand(4))->getVT();
1232 switch (TLI.getOperationAction(Result.Val->getOpcode(), StVT)) {
1233 default: assert(0 && "This action is not supported yet!");
1234 case TargetLowering::Legal: break;
1235 case TargetLowering::Custom:
1236 Tmp1 = TLI.LowerOperation(Result, DAG);
1237 if (Tmp1.Val) Result = Tmp1;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001238 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001239 }
1240 break;
Evan Cheng31d15fa2005-12-23 07:29:34 +00001241 }
Chris Lattner39c67442005-01-14 22:08:15 +00001242 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001243 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1244 case Expand: assert(0 && "It's impossible to expand bools");
1245 case Legal:
1246 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1247 break;
1248 case Promote:
1249 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1250 break;
1251 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001252 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001253 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001254
Chris Lattnerd02b0542006-01-28 10:58:55 +00001255 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001256
Nate Begeman987121a2005-08-23 04:29:48 +00001257 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001258 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001259 case TargetLowering::Legal: break;
1260 case TargetLowering::Custom: {
1261 Tmp1 = TLI.LowerOperation(Result, DAG);
1262 if (Tmp1.Val) Result = Tmp1;
1263 break;
1264 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001265 case TargetLowering::Expand:
1266 if (Tmp1.getOpcode() == ISD::SETCC) {
1267 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1268 Tmp2, Tmp3,
1269 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1270 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001271 // Make sure the condition is either zero or one. It may have been
1272 // promoted from something else.
1273 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001274 Result = DAG.getSelectCC(Tmp1,
1275 DAG.getConstant(0, Tmp1.getValueType()),
1276 Tmp2, Tmp3, ISD::SETNE);
1277 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001278 break;
1279 case TargetLowering::Promote: {
1280 MVT::ValueType NVT =
1281 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1282 unsigned ExtOp, TruncOp;
1283 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001284 ExtOp = ISD::ANY_EXTEND;
1285 TruncOp = ISD::TRUNCATE;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001286 } else {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001287 ExtOp = ISD::FP_EXTEND;
1288 TruncOp = ISD::FP_ROUND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001289 }
1290 // Promote each of the values to the new type.
1291 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1292 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1293 // Perform the larger operation, then round down.
1294 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1295 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1296 break;
1297 }
1298 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001299 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001300 case ISD::SELECT_CC:
1301 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1302 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1303
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001304 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001305 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1306 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerd02b0542006-01-28 10:58:55 +00001307
1308 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4,
1309 Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001310
Chris Lattner5f573412005-08-26 00:23:59 +00001311 // Everything is legal, see if we should expand this op or something.
1312 switch (TLI.getOperationAction(ISD::SELECT_CC,
1313 Node->getOperand(0).getValueType())) {
1314 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001315 case TargetLowering::Legal: break;
1316 case TargetLowering::Custom:
1317 Tmp1 = TLI.LowerOperation(Result, DAG);
1318 if (Tmp1.Val) Result = Tmp1;
Chris Lattner5f573412005-08-26 00:23:59 +00001319 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001320 }
1321 break;
1322 } else {
1323 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1324 Node->getOperand(0), // LHS
1325 Node->getOperand(1), // RHS
1326 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001327 // If we get a SETCC back from legalizing the SETCC node we just
1328 // created, then use its LHS, RHS, and CC directly in creating a new
1329 // node. Otherwise, select between the true and false value based on
1330 // comparing the result of the legalized with zero.
1331 if (Tmp1.getOpcode() == ISD::SETCC) {
1332 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1333 Tmp1.getOperand(0), Tmp1.getOperand(1),
1334 Tmp3, Tmp4, Tmp1.getOperand(2));
1335 } else {
1336 Result = DAG.getSelectCC(Tmp1,
1337 DAG.getConstant(0, Tmp1.getValueType()),
1338 Tmp3, Tmp4, ISD::SETNE);
1339 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001340 }
1341 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001342 case ISD::SETCC:
1343 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1344 case Legal:
1345 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1346 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001347 break;
1348 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001349 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1350 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1351
1352 // If this is an FP compare, the operands have already been extended.
1353 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1354 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001355 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001356
1357 // Otherwise, we have to insert explicit sign or zero extends. Note
1358 // that we could insert sign extends for ALL conditions, but zero extend
1359 // is cheaper on many machines (an AND instead of two shifts), so prefer
1360 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001361 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001362 default: assert(0 && "Unknown integer comparison!");
1363 case ISD::SETEQ:
1364 case ISD::SETNE:
1365 case ISD::SETUGE:
1366 case ISD::SETUGT:
1367 case ISD::SETULE:
1368 case ISD::SETULT:
1369 // ALL of these operations will work if we either sign or zero extend
1370 // the operands (including the unsigned comparisons!). Zero extend is
1371 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001372 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1373 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001374 break;
1375 case ISD::SETGE:
1376 case ISD::SETGT:
1377 case ISD::SETLT:
1378 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001379 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1380 DAG.getValueType(VT));
1381 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1382 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001383 break;
1384 }
Chris Lattner4d978642005-01-15 22:16:26 +00001385 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001386 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001387 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001388 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1389 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1390 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001391 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001392 case ISD::SETEQ:
1393 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001394 if (RHSLo == RHSHi)
1395 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1396 if (RHSCST->isAllOnesValue()) {
1397 // Comparison to -1.
1398 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001399 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001400 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001401 }
1402
Chris Lattnerdc750592005-01-07 07:47:09 +00001403 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1404 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1405 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001406 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001407 break;
1408 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001409 // If this is a comparison of the sign bit, just look at the top part.
1410 // X > -1, x < 0
1411 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001412 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001413 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001414 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001415 (CST->isAllOnesValue()))) { // X > -1
1416 Tmp1 = LHSHi;
1417 Tmp2 = RHSHi;
1418 break;
1419 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001420
Chris Lattnerdc750592005-01-07 07:47:09 +00001421 // FIXME: This generated code sucks.
1422 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001423 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001424 default: assert(0 && "Unknown integer setcc!");
1425 case ISD::SETLT:
1426 case ISD::SETULT: LowCC = ISD::SETULT; break;
1427 case ISD::SETGT:
1428 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1429 case ISD::SETLE:
1430 case ISD::SETULE: LowCC = ISD::SETULE; break;
1431 case ISD::SETGE:
1432 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1433 }
Misha Brukman835702a2005-04-21 22:36:52 +00001434
Chris Lattnerdc750592005-01-07 07:47:09 +00001435 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1436 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1437 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1438
1439 // NOTE: on targets without efficient SELECT of bools, we can always use
1440 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001441 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1442 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1443 Node->getOperand(2));
1444 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001445 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1446 Result, Tmp1, Tmp2));
Chris Lattner2af3ee42005-12-20 00:53:54 +00001447 AddLegalizedOperand(SDOperand(Node, 0), Result);
Nate Begeman987121a2005-08-23 04:29:48 +00001448 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001449 }
1450 }
Nate Begeman987121a2005-08-23 04:29:48 +00001451
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001452 switch (TLI.getOperationAction(ISD::SETCC,
1453 Node->getOperand(0).getValueType())) {
1454 default: assert(0 && "Cannot handle this action for SETCC yet!");
1455 case TargetLowering::Custom:
1456 isCustom = true;
1457 // FALLTHROUGH.
1458 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001459 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001460 if (isCustom) {
1461 Tmp3 = TLI.LowerOperation(Result, DAG);
1462 if (Tmp3.Val) Result = Tmp3;
1463 }
Nate Begeman987121a2005-08-23 04:29:48 +00001464 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001465 case TargetLowering::Promote: {
1466 // First step, figure out the appropriate operation to use.
1467 // Allow SETCC to not be supported for all legal data types
1468 // Mostly this targets FP
1469 MVT::ValueType NewInTy = Node->getOperand(0).getValueType();
1470 MVT::ValueType OldVT = NewInTy;
1471
1472 // Scan for the appropriate larger type to use.
1473 while (1) {
1474 NewInTy = (MVT::ValueType)(NewInTy+1);
1475
1476 assert(MVT::isInteger(NewInTy) == MVT::isInteger(OldVT) &&
1477 "Fell off of the edge of the integer world");
1478 assert(MVT::isFloatingPoint(NewInTy) == MVT::isFloatingPoint(OldVT) &&
1479 "Fell off of the edge of the floating point world");
1480
1481 // If the target supports SETCC of this type, use it.
Chris Lattner1408c052005-12-22 05:23:45 +00001482 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001483 break;
1484 }
1485 if (MVT::isInteger(NewInTy))
1486 assert(0 && "Cannot promote Legal Integer SETCC yet");
1487 else {
1488 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
1489 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
1490 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001491 Tmp1 = LegalizeOp(Tmp1);
1492 Tmp2 = LegalizeOp(Tmp2);
1493 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Evan Cheng6f86a7d2006-01-17 19:47:13 +00001494 Result = LegalizeOp(Result);
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001495 break;
Andrew Lenharth6ee85662005-11-30 17:12:26 +00001496 }
Nate Begeman987121a2005-08-23 04:29:48 +00001497 case TargetLowering::Expand:
1498 // Expand a setcc node into a select_cc of the same condition, lhs, and
1499 // rhs that selects between const 1 (true) and const 0 (false).
1500 MVT::ValueType VT = Node->getValueType(0);
1501 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1502 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1503 Node->getOperand(2));
Nate Begeman987121a2005-08-23 04:29:48 +00001504 break;
1505 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001506 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001507 case ISD::MEMSET:
1508 case ISD::MEMCPY:
1509 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001510 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001511 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1512
1513 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1514 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1515 case Expand: assert(0 && "Cannot expand a byte!");
1516 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001517 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001518 break;
1519 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001520 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001521 break;
1522 }
1523 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001524 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001525 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001526
1527 SDOperand Tmp4;
1528 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001529 case Expand: {
1530 // Length is too big, just take the lo-part of the length.
1531 SDOperand HiPart;
1532 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1533 break;
1534 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001535 case Legal:
1536 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001537 break;
1538 case Promote:
1539 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001540 break;
1541 }
1542
1543 SDOperand Tmp5;
1544 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1545 case Expand: assert(0 && "Cannot expand this yet!");
1546 case Legal:
1547 Tmp5 = LegalizeOp(Node->getOperand(4));
1548 break;
1549 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001550 Tmp5 = PromoteOp(Node->getOperand(4));
1551 break;
1552 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001553
1554 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1555 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001556 case TargetLowering::Custom:
1557 isCustom = true;
1558 // FALLTHROUGH
Chris Lattner3c0dd462005-01-16 07:29:19 +00001559 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001560 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001561 if (isCustom) {
1562 Tmp1 = TLI.LowerOperation(Result, DAG);
1563 if (Tmp1.Val) Result = Tmp1;
1564 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001565 break;
1566 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001567 // Otherwise, the target does not support this operation. Lower the
1568 // operation to an explicit libcall as appropriate.
1569 MVT::ValueType IntPtr = TLI.getPointerTy();
1570 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1571 std::vector<std::pair<SDOperand, const Type*> > Args;
1572
Reid Spencer6dced922005-01-12 14:53:45 +00001573 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001574 if (Node->getOpcode() == ISD::MEMSET) {
1575 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1576 // Extend the ubyte argument to be an int value for the call.
1577 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1578 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1579 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1580
1581 FnName = "memset";
1582 } else if (Node->getOpcode() == ISD::MEMCPY ||
1583 Node->getOpcode() == ISD::MEMMOVE) {
1584 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1585 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1586 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1587 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1588 } else {
1589 assert(0 && "Unknown op!");
1590 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001591
Chris Lattner85d70c62005-01-11 05:57:22 +00001592 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001593 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001594 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001595 Result = CallResult.second;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001596 break;
1597 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001598 }
1599 break;
1600 }
Chris Lattner5385db52005-05-09 20:23:03 +00001601
1602 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001603 Tmp1 = LegalizeOp(Node->getOperand(0));
1604 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001606
Chris Lattner5385db52005-05-09 20:23:03 +00001607 // Since these produce two values, make sure to remember that we legalized
1608 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001609 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattner5385db52005-05-09 20:23:03 +00001610 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001611 return Result;
Chris Lattner5385db52005-05-09 20:23:03 +00001612 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001613 Tmp1 = LegalizeOp(Node->getOperand(0));
1614 Tmp2 = LegalizeOp(Node->getOperand(1));
1615 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnerd02b0542006-01-28 10:58:55 +00001616 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner5385db52005-05-09 20:23:03 +00001617 break;
1618
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001619 case ISD::READIO:
1620 Tmp1 = LegalizeOp(Node->getOperand(0));
1621 Tmp2 = LegalizeOp(Node->getOperand(1));
1622
1623 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1624 case TargetLowering::Custom:
1625 default: assert(0 && "This action not implemented for this operation!");
1626 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001628 break;
1629 case TargetLowering::Expand:
1630 // Replace this with a load from memory.
1631 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1632 Node->getOperand(1), DAG.getSrcValue(NULL));
1633 Result = LegalizeOp(Result);
1634 break;
1635 }
1636
1637 // Since these produce two values, make sure to remember that we legalized
1638 // both of them.
Chris Lattnerd02b0542006-01-28 10:58:55 +00001639 AddLegalizedOperand(SDOperand(Node, 0), Result.getValue(0));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001640 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1641 return Result.getValue(Op.ResNo);
1642
1643 case ISD::WRITEIO:
1644 Tmp1 = LegalizeOp(Node->getOperand(0));
1645 Tmp2 = LegalizeOp(Node->getOperand(1));
1646 Tmp3 = LegalizeOp(Node->getOperand(2));
1647
1648 switch (TLI.getOperationAction(Node->getOpcode(),
1649 Node->getOperand(1).getValueType())) {
1650 case TargetLowering::Custom:
1651 default: assert(0 && "This action not implemented for this operation!");
1652 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001653 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001654 break;
1655 case TargetLowering::Expand:
1656 // Replace this with a store to memory.
1657 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1658 Node->getOperand(1), Node->getOperand(2),
1659 DAG.getSrcValue(NULL));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001660 break;
1661 }
1662 break;
1663
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001664 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001665 case ISD::SUB_PARTS:
1666 case ISD::SHL_PARTS:
1667 case ISD::SRA_PARTS:
1668 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001669 std::vector<SDOperand> Ops;
1670 bool Changed = false;
1671 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1672 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1673 Changed |= Ops.back() != Node->getOperand(i);
1674 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001675 if (Changed)
1676 Result = DAG.UpdateNodeOperands(Result, Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001677
Evan Cheng870e4f82006-01-09 18:31:59 +00001678 switch (TLI.getOperationAction(Node->getOpcode(),
1679 Node->getValueType(0))) {
1680 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001681 case TargetLowering::Legal: break;
1682 case TargetLowering::Custom:
1683 Tmp1 = TLI.LowerOperation(Result, DAG);
1684 if (Tmp1.Val) {
1685 SDOperand Tmp2, RetVal(0, 0);
Evan Cheng870e4f82006-01-09 18:31:59 +00001686 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001687 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Evan Cheng870e4f82006-01-09 18:31:59 +00001688 AddLegalizedOperand(SDOperand(Node, i), Tmp2);
1689 if (i == Op.ResNo)
Evan Cheng13e8c9d2006-01-19 04:54:52 +00001690 RetVal = Tmp2;
Evan Cheng870e4f82006-01-09 18:31:59 +00001691 }
Chris Lattnerfb5f4652006-01-10 19:43:26 +00001692 assert(RetVal.Val && "Illegal result number");
Evan Cheng870e4f82006-01-09 18:31:59 +00001693 return RetVal;
1694 }
Evan Cheng870e4f82006-01-09 18:31:59 +00001695 break;
1696 }
1697
Chris Lattner13fe99c2005-04-02 05:00:07 +00001698 // Since these produce multiple values, make sure to remember that we
1699 // legalized all of them.
1700 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1701 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1702 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001703 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001704
1705 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001706 case ISD::ADD:
1707 case ISD::SUB:
1708 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001709 case ISD::MULHS:
1710 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001711 case ISD::UDIV:
1712 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001713 case ISD::AND:
1714 case ISD::OR:
1715 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001716 case ISD::SHL:
1717 case ISD::SRL:
1718 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001719 case ISD::FADD:
1720 case ISD::FSUB:
1721 case ISD::FMUL:
1722 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001723 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001724 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1725 case Expand: assert(0 && "Not possible");
1726 case Legal:
1727 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1728 break;
1729 case Promote:
1730 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1731 break;
1732 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00001733
1734 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001735
Andrew Lenharth72594262005-12-24 23:42:32 +00001736 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001737 default: assert(0 && "Operation not supported");
1738 case TargetLowering::Legal: break;
1739 case TargetLowering::Custom:
1740 Tmp1 = TLI.LowerOperation(Result, DAG);
1741 if (Tmp1.Val) Result = Tmp1;
Andrew Lenharth30db2ec2005-12-25 01:07:37 +00001742 break;
Andrew Lenharth72594262005-12-24 23:42:32 +00001743 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001744 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001745
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001746 case ISD::BUILD_PAIR: {
1747 MVT::ValueType PairTy = Node->getValueType(0);
1748 // TODO: handle the case where the Lo and Hi operands are not of legal type
1749 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1750 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1751 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001752 case TargetLowering::Promote:
1753 case TargetLowering::Custom:
1754 assert(0 && "Cannot promote/custom this yet!");
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001755 case TargetLowering::Legal:
1756 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1757 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1758 break;
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001759 case TargetLowering::Expand:
1760 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1761 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1762 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1763 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1764 TLI.getShiftAmountTy()));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001765 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001766 break;
1767 }
1768 break;
1769 }
1770
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001771 case ISD::UREM:
1772 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001773 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001774 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1775 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001776
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001777 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001778 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
1779 case TargetLowering::Custom:
1780 isCustom = true;
1781 // FALLTHROUGH
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001782 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001783 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001784 if (isCustom) {
1785 Tmp1 = TLI.LowerOperation(Result, DAG);
1786 if (Tmp1.Val) Result = Tmp1;
1787 }
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001788 break;
Chris Lattner81914422005-08-03 20:31:37 +00001789 case TargetLowering::Expand:
1790 if (MVT::isInteger(Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001791 // X % Y -> X-X/Y*Y
Chris Lattner81914422005-08-03 20:31:37 +00001792 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001793 unsigned Opc = Node->getOpcode() == ISD::UREM ? ISD::UDIV : ISD::SDIV;
Chris Lattner81914422005-08-03 20:31:37 +00001794 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1795 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1796 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1797 } else {
1798 // Floating point mod -> fmod libcall.
1799 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1800 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00001801 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001802 }
1803 break;
1804 }
1805 break;
Nate Begemane74795c2006-01-25 18:21:52 +00001806 case ISD::VAARG: {
1807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1808 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1809
Chris Lattner364b89a2006-01-28 07:42:08 +00001810 MVT::ValueType VT = Node->getValueType(0);
Nate Begemane74795c2006-01-25 18:21:52 +00001811 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1812 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001813 case TargetLowering::Custom:
1814 isCustom = true;
1815 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001816 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001817 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1818 Result = Result.getValue(0);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001819 Tmp1 = Result.getValue(1);
1820
1821 if (isCustom) {
1822 Tmp2 = TLI.LowerOperation(Result, DAG);
1823 if (Tmp2.Val) {
1824 Result = LegalizeOp(Tmp2);
1825 Tmp1 = LegalizeOp(Tmp2.getValue(1));
1826 }
1827 }
Nate Begemane74795c2006-01-25 18:21:52 +00001828 break;
1829 case TargetLowering::Expand: {
1830 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
1831 Node->getOperand(2));
1832 // Increment the pointer, VAList, to the next vaarg
1833 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
1834 DAG.getConstant(MVT::getSizeInBits(VT)/8,
1835 TLI.getPointerTy()));
1836 // Store the incremented VAList to the legalized pointer
1837 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
1838 Node->getOperand(2));
1839 // Load the actual argument out of the pointer VAList
1840 Result = DAG.getLoad(VT, Tmp3, VAList, DAG.getSrcValue(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001841 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemane74795c2006-01-25 18:21:52 +00001842 Result = LegalizeOp(Result);
1843 break;
1844 }
1845 }
1846 // Since VAARG produces two values, make sure to remember that we
1847 // legalized both of them.
1848 AddLegalizedOperand(SDOperand(Node, 0), Result);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001849 AddLegalizedOperand(SDOperand(Node, 1), Tmp1);
1850 return Op.ResNo ? Tmp1 : Result;
Nate Begemane74795c2006-01-25 18:21:52 +00001851 }
1852
1853 case ISD::VACOPY:
1854 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1855 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
1856 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
1857
1858 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
1859 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001860 case TargetLowering::Custom:
1861 isCustom = true;
1862 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001863 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001864 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
1865 Node->getOperand(3), Node->getOperand(4));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001866 if (isCustom) {
1867 Tmp1 = TLI.LowerOperation(Result, DAG);
1868 if (Tmp1.Val) Result = Tmp1;
1869 }
Nate Begemane74795c2006-01-25 18:21:52 +00001870 break;
1871 case TargetLowering::Expand:
1872 // This defaults to loading a pointer from the input and storing it to the
1873 // output, returning the chain.
1874 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, Node->getOperand(3));
1875 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp4.getValue(1), Tmp4, Tmp2,
1876 Node->getOperand(4));
Nate Begemane74795c2006-01-25 18:21:52 +00001877 break;
1878 }
1879 break;
1880
1881 case ISD::VAEND:
1882 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1883 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1884
1885 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
1886 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001887 case TargetLowering::Custom:
1888 isCustom = true;
1889 // FALLTHROUGH
Nate Begemane74795c2006-01-25 18:21:52 +00001890 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001891 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001892 if (isCustom) {
1893 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
1894 if (Tmp1.Val) Result = Tmp1;
1895 }
Nate Begemane74795c2006-01-25 18:21:52 +00001896 break;
1897 case TargetLowering::Expand:
1898 Result = Tmp1; // Default to a no-op, return the chain
1899 break;
1900 }
1901 break;
1902
1903 case ISD::VASTART:
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1905 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1906
Chris Lattnerd02b0542006-01-28 10:58:55 +00001907 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1908
Nate Begemane74795c2006-01-25 18:21:52 +00001909 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
1910 default: assert(0 && "This action is not supported yet!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001911 case TargetLowering::Legal: break;
1912 case TargetLowering::Custom:
1913 Tmp1 = TLI.LowerOperation(Result, DAG);
1914 if (Tmp1.Val) Result = Tmp1;
Nate Begemane74795c2006-01-25 18:21:52 +00001915 break;
1916 }
1917 break;
1918
Nate Begeman1b8121b2006-01-11 21:21:00 +00001919 case ISD::ROTL:
1920 case ISD::ROTR:
1921 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001923
1924 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
1925 "Cannot handle this yet!");
Chris Lattnerd02b0542006-01-28 10:58:55 +00001926 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Nate Begeman1b8121b2006-01-11 21:21:00 +00001927 break;
1928
Nate Begeman2fba8a32006-01-14 03:14:10 +00001929 case ISD::BSWAP:
1930 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1931 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001932 case TargetLowering::Custom:
1933 assert(0 && "Cannot custom legalize this yet!");
1934 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001935 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001936 break;
1937 case TargetLowering::Promote: {
1938 MVT::ValueType OVT = Tmp1.getValueType();
1939 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1940 unsigned DiffBits = getSizeInBits(NVT) - getSizeInBits(OVT);
Nate Begeman2fba8a32006-01-14 03:14:10 +00001941
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001942 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1943 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
1944 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
1945 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
1946 break;
1947 }
1948 case TargetLowering::Expand:
1949 Result = ExpandBSWAP(Tmp1);
1950 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00001951 }
1952 break;
1953
Andrew Lenharth5e177822005-05-03 17:19:30 +00001954 case ISD::CTPOP:
1955 case ISD::CTTZ:
1956 case ISD::CTLZ:
1957 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1958 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001959 case TargetLowering::Custom: assert(0 && "Cannot custom handle this yet!");
Andrew Lenharth5e177822005-05-03 17:19:30 +00001960 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00001961 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00001962 break;
1963 case TargetLowering::Promote: {
1964 MVT::ValueType OVT = Tmp1.getValueType();
1965 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001966
1967 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001968 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1969 // Perform the larger operation, then subtract if needed.
1970 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001971 switch (Node->getOpcode()) {
Andrew Lenharth5e177822005-05-03 17:19:30 +00001972 case ISD::CTPOP:
1973 Result = Tmp1;
1974 break;
1975 case ISD::CTTZ:
1976 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001977 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1978 DAG.getConstant(getSizeInBits(NVT), NVT),
1979 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001980 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001981 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1982 break;
1983 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001984 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001985 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1986 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001987 getSizeInBits(OVT), NVT));
1988 break;
1989 }
1990 break;
1991 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001992 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00001993 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth5e177822005-05-03 17:19:30 +00001994 break;
1995 }
1996 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001997
Chris Lattner13fe99c2005-04-02 05:00:07 +00001998 // Unary operators
1999 case ISD::FABS:
2000 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00002001 case ISD::FSQRT:
2002 case ISD::FSIN:
2003 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002004 Tmp1 = LegalizeOp(Node->getOperand(0));
2005 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002006 case TargetLowering::Promote:
2007 case TargetLowering::Custom:
2008 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00002009 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002010 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner13fe99c2005-04-02 05:00:07 +00002011 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002012 case TargetLowering::Expand:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002013 switch (Node->getOpcode()) {
2014 default: assert(0 && "Unreachable!");
2015 case ISD::FNEG:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002016 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
2017 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002018 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattner80026402005-04-30 04:43:14 +00002019 break;
Chris Lattner80026402005-04-30 04:43:14 +00002020 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002021 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
2022 MVT::ValueType VT = Node->getValueType(0);
2023 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00002024 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00002025 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
2026 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattner80026402005-04-30 04:43:14 +00002027 break;
2028 }
2029 case ISD::FSQRT:
2030 case ISD::FSIN:
2031 case ISD::FCOS: {
2032 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00002033 const char *FnName = 0;
2034 switch(Node->getOpcode()) {
2035 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
2036 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
2037 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
2038 default: assert(0 && "Unreachable!");
2039 }
Nate Begeman77558da2005-08-04 21:43:28 +00002040 SDOperand Dummy;
Chris Lattner10f67752006-01-28 04:28:26 +00002041 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00002042 break;
2043 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00002044 }
2045 break;
2046 }
2047 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002048
2049 case ISD::BIT_CONVERT:
Chris Lattner763dfd72006-01-23 07:30:46 +00002050 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner36e663d2005-12-23 00:16:34 +00002051 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
Chris Lattner763dfd72006-01-23 07:30:46 +00002052 } else {
Chris Lattner36e663d2005-12-23 00:16:34 +00002053 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
2054 Node->getOperand(0).getValueType())) {
2055 default: assert(0 && "Unknown operation action!");
2056 case TargetLowering::Expand:
2057 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2058 break;
2059 case TargetLowering::Legal:
2060 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner36e663d2005-12-23 00:16:34 +00002062 break;
2063 }
2064 }
2065 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00002066 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00002067 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002068 case ISD::UINT_TO_FP: {
2069 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00002070 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2071 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00002072 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002073 Node->getOperand(0).getValueType())) {
2074 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002075 case TargetLowering::Custom:
2076 isCustom = true;
2077 // FALLTHROUGH
2078 case TargetLowering::Legal:
2079 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002080 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002081 if (isCustom) {
2082 Tmp1 = TLI.LowerOperation(Result, DAG);
2083 if (Tmp1.Val) Result = Tmp1;
2084 }
2085 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002086 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00002087 Result = ExpandLegalINT_TO_FP(isSigned,
2088 LegalizeOp(Node->getOperand(0)),
2089 Node->getValueType(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002090 break;
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002091 case TargetLowering::Promote:
2092 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
2093 Node->getValueType(0),
2094 isSigned);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002095 break;
Andrew Lenharth8d17c702005-11-30 06:43:03 +00002096 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002097 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00002098 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002099 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
2100 Node->getValueType(0), Node->getOperand(0));
2101 break;
2102 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002103 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002104 if (isSigned) {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002105 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
2106 Tmp1, DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002107 } else {
Chris Lattnerd02b0542006-01-28 10:58:55 +00002108 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
2109 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002110 }
Chris Lattnerd02b0542006-01-28 10:58:55 +00002111 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2112 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002113 break;
2114 }
2115 break;
2116 }
2117 case ISD::TRUNCATE:
2118 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2119 case Legal:
2120 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002121 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002122 break;
2123 case Expand:
2124 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2125
2126 // Since the result is legal, we should just be able to truncate the low
2127 // part of the source.
2128 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2129 break;
2130 case Promote:
2131 Result = PromoteOp(Node->getOperand(0));
2132 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2133 break;
2134 }
2135 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002136
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002137 case ISD::FP_TO_SINT:
2138 case ISD::FP_TO_UINT:
2139 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2140 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002141 Tmp1 = LegalizeOp(Node->getOperand(0));
2142
Chris Lattner44fe26f2005-07-29 00:11:56 +00002143 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2144 default: assert(0 && "Unknown operation action!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002145 case TargetLowering::Custom:
2146 isCustom = true;
2147 // FALLTHROUGH
2148 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002149 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002150 if (isCustom) {
2151 Tmp1 = TLI.LowerOperation(Result, DAG);
2152 if (Tmp1.Val) Result = Tmp1;
2153 }
2154 break;
2155 case TargetLowering::Promote:
2156 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
2157 Node->getOpcode() == ISD::FP_TO_SINT);
2158 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002159 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002160 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2161 SDOperand True, False;
2162 MVT::ValueType VT = Node->getOperand(0).getValueType();
2163 MVT::ValueType NVT = Node->getValueType(0);
2164 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2165 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2166 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2167 Node->getOperand(0), Tmp2, ISD::SETLT);
2168 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2169 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002170 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002171 Tmp2));
2172 False = DAG.getNode(ISD::XOR, NVT, False,
2173 DAG.getConstant(1ULL << ShiftAmt, NVT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002174 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
2175 break;
Nate Begeman36853ee2005-08-14 01:20:53 +00002176 } else {
2177 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2178 }
2179 break;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002180 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002181 break;
2182 case Expand:
2183 assert(0 && "Shouldn't need to expand other operators here!");
2184 case Promote:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002185 Tmp1 = PromoteOp(Node->getOperand(0));
2186 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
2187 Result = LegalizeOp(Result);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002188 break;
2189 }
2190 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002191
Chris Lattner7753f172005-09-02 00:18:10 +00002192 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002193 case ISD::ZERO_EXTEND:
2194 case ISD::SIGN_EXTEND:
2195 case ISD::FP_EXTEND:
2196 case ISD::FP_ROUND:
2197 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002198 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002199 case Legal:
2200 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerd02b0542006-01-28 10:58:55 +00002201 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002202 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002203 case Promote:
2204 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002205 case ISD::ANY_EXTEND:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002206 Tmp1 = PromoteOp(Node->getOperand(0));
2207 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner7753f172005-09-02 00:18:10 +00002208 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002209 case ISD::ZERO_EXTEND:
2210 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002211 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002212 Result = DAG.getZeroExtendInReg(Result,
2213 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002214 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002215 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002216 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002217 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002218 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002219 Result,
2220 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002221 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002222 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002223 Result = PromoteOp(Node->getOperand(0));
2224 if (Result.getValueType() != Op.getValueType())
2225 // Dynamically dead while we have only 2 FP types.
2226 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2227 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002228 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002229 Result = PromoteOp(Node->getOperand(0));
2230 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2231 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002232 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002233 }
2234 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002235 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002236 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002237 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002238 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002239
2240 // If this operation is not supported, convert it to a shl/shr or load/store
2241 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002242 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2243 default: assert(0 && "This action not supported for this op yet!");
2244 case TargetLowering::Legal:
Chris Lattnerd02b0542006-01-28 10:58:55 +00002245 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002246 break;
2247 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002248 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002249 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002250 // NOTE: we could fall back on load/store here too for targets without
2251 // SAR. However, it is doubtful that any exist.
2252 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2253 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002254 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002255 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2256 Node->getOperand(0), ShiftCst);
2257 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2258 Result, ShiftCst);
2259 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2260 // The only way we can lower this is to turn it into a STORETRUNC,
2261 // EXTLOAD pair, targetting a temporary location (a stack slot).
2262
2263 // NOTE: there is a choice here between constantly creating new stack
2264 // slots and always reusing the same one. We currently always create
2265 // new ones, as reuse may inhibit scheduling.
2266 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2267 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2268 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2269 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002270 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002271 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2272 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2273 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002274 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002275 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002276 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2277 Result, StackSlot, DAG.getSrcValue(NULL),
2278 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002279 } else {
2280 assert(0 && "Unknown op");
2281 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00002282 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002283 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002284 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002285 }
Chris Lattner99222f72005-01-15 07:15:18 +00002286 }
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002287
2288 // Make sure that the generated code is itself legal.
2289 if (Result != Op)
2290 Result = LegalizeOp(Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002291
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002292 // Note that LegalizeOp may be reentered even from single-use nodes, which
2293 // means that we always must cache transformed nodes.
2294 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002295 return Result;
2296}
2297
Chris Lattner4d978642005-01-15 22:16:26 +00002298/// PromoteOp - Given an operation that produces a value in an invalid type,
2299/// promote it to compute the value into a larger type. The produced value will
2300/// have the correct bits for the low portion of the register, but no guarantee
2301/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002302SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2303 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002304 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002305 assert(getTypeAction(VT) == Promote &&
2306 "Caller should expand or legalize operands that are not promotable!");
2307 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2308 "Cannot promote to smaller type!");
2309
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002310 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002311 SDOperand Result;
2312 SDNode *Node = Op.Val;
2313
Chris Lattner1a570f12005-09-02 20:32:45 +00002314 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2315 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002316
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002317 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002318 case ISD::CopyFromReg:
2319 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002320 default:
2321 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2322 assert(0 && "Do not know how to promote this operator!");
2323 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002324 case ISD::UNDEF:
2325 Result = DAG.getNode(ISD::UNDEF, NVT);
2326 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002327 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002328 if (VT != MVT::i1)
2329 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2330 else
2331 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002332 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2333 break;
2334 case ISD::ConstantFP:
2335 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2336 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2337 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002338
Chris Lattner2cb338d2005-01-18 02:59:52 +00002339 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002340 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002341 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2342 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002343 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002344
2345 case ISD::TRUNCATE:
2346 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2347 case Legal:
2348 Result = LegalizeOp(Node->getOperand(0));
2349 assert(Result.getValueType() >= NVT &&
2350 "This truncation doesn't make sense!");
2351 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2352 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2353 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002354 case Promote:
2355 // The truncation is not required, because we don't guarantee anything
2356 // about high bits anyway.
2357 Result = PromoteOp(Node->getOperand(0));
2358 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002359 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002360 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2361 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002362 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002363 }
2364 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002365 case ISD::SIGN_EXTEND:
2366 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002367 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002368 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2369 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2370 case Legal:
2371 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002372 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002373 break;
2374 case Promote:
2375 // Promote the reg if it's smaller.
2376 Result = PromoteOp(Node->getOperand(0));
2377 // The high bits are not guaranteed to be anything. Insert an extend.
2378 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002379 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002380 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002381 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002382 Result = DAG.getZeroExtendInReg(Result,
2383 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002384 break;
2385 }
2386 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00002387 case ISD::BIT_CONVERT:
2388 Result = ExpandBIT_CONVERT(Node->getValueType(0), Node->getOperand(0));
2389 Result = PromoteOp(Result);
2390 break;
2391
Chris Lattner4d978642005-01-15 22:16:26 +00002392 case ISD::FP_EXTEND:
2393 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2394 case ISD::FP_ROUND:
2395 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2396 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2397 case Promote: assert(0 && "Unreachable with 2 FP types!");
2398 case Legal:
2399 // Input is legal? Do an FP_ROUND_INREG.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002400 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002401 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002402 break;
2403 }
2404 break;
2405
2406 case ISD::SINT_TO_FP:
2407 case ISD::UINT_TO_FP:
2408 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2409 case Legal:
Chris Lattneraac464e2005-01-21 06:05:23 +00002410 // No extra round required here.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002411 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner4d978642005-01-15 22:16:26 +00002412 break;
2413
2414 case Promote:
2415 Result = PromoteOp(Node->getOperand(0));
2416 if (Node->getOpcode() == ISD::SINT_TO_FP)
2417 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002418 Result,
2419 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002420 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002421 Result = DAG.getZeroExtendInReg(Result,
2422 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002423 // No extra round required here.
2424 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002425 break;
2426 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002427 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2428 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002429 // Round if we cannot tolerate excess precision.
2430 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002431 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2432 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002433 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002434 }
Chris Lattner4d978642005-01-15 22:16:26 +00002435 break;
2436
Chris Lattner268d4572005-12-09 17:32:47 +00002437 case ISD::SIGN_EXTEND_INREG:
2438 Result = PromoteOp(Node->getOperand(0));
2439 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
2440 Node->getOperand(1));
2441 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002442 case ISD::FP_TO_SINT:
2443 case ISD::FP_TO_UINT:
2444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2445 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002446 Tmp1 = Node->getOperand(0);
Chris Lattner4d978642005-01-15 22:16:26 +00002447 break;
2448 case Promote:
2449 // The input result is prerounded, so we don't have to do anything
2450 // special.
2451 Tmp1 = PromoteOp(Node->getOperand(0));
2452 break;
2453 case Expand:
2454 assert(0 && "not implemented");
2455 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002456 // If we're promoting a UINT to a larger size, check to see if the new node
2457 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2458 // we can use that instead. This allows us to generate better code for
2459 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2460 // legal, such as PowerPC.
2461 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002462 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002463 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2464 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002465 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2466 } else {
2467 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2468 }
Chris Lattner4d978642005-01-15 22:16:26 +00002469 break;
2470
Chris Lattner13fe99c2005-04-02 05:00:07 +00002471 case ISD::FABS:
2472 case ISD::FNEG:
2473 Tmp1 = PromoteOp(Node->getOperand(0));
2474 assert(Tmp1.getValueType() == NVT);
2475 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2476 // NOTE: we do not have to do any extra rounding here for
2477 // NoExcessFPPrecision, because we know the input will have the appropriate
2478 // precision, and these operations don't modify precision at all.
2479 break;
2480
Chris Lattner9d6fa982005-04-28 21:44:33 +00002481 case ISD::FSQRT:
2482 case ISD::FSIN:
2483 case ISD::FCOS:
2484 Tmp1 = PromoteOp(Node->getOperand(0));
2485 assert(Tmp1.getValueType() == NVT);
2486 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002487 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002488 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2489 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002490 break;
2491
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002492 case ISD::AND:
2493 case ISD::OR:
2494 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002495 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002496 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002497 case ISD::MUL:
2498 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002499 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002500 // that too is okay if they are integer operations.
2501 Tmp1 = PromoteOp(Node->getOperand(0));
2502 Tmp2 = PromoteOp(Node->getOperand(1));
2503 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2504 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002505 break;
2506 case ISD::FADD:
2507 case ISD::FSUB:
2508 case ISD::FMUL:
Chris Lattner6f3b5772005-09-28 22:28:18 +00002509 Tmp1 = PromoteOp(Node->getOperand(0));
2510 Tmp2 = PromoteOp(Node->getOperand(1));
2511 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2512 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2513
2514 // Floating point operations will give excess precision that we may not be
2515 // able to tolerate. If we DO allow excess precision, just leave it,
2516 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002517 // FIXME: Why would we need to round FP ops more than integer ones?
2518 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002519 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002520 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2521 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002522 break;
2523
Chris Lattner4d978642005-01-15 22:16:26 +00002524 case ISD::SDIV:
2525 case ISD::SREM:
2526 // These operators require that their input be sign extended.
2527 Tmp1 = PromoteOp(Node->getOperand(0));
2528 Tmp2 = PromoteOp(Node->getOperand(1));
2529 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002530 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2531 DAG.getValueType(VT));
2532 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2533 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002534 }
2535 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2536
2537 // Perform FP_ROUND: this is probably overly pessimistic.
2538 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002539 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2540 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002541 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002542 case ISD::FDIV:
2543 case ISD::FREM:
2544 // These operators require that their input be fp extended.
2545 Tmp1 = PromoteOp(Node->getOperand(0));
2546 Tmp2 = PromoteOp(Node->getOperand(1));
2547 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2548
2549 // Perform FP_ROUND: this is probably overly pessimistic.
2550 if (NoExcessFPPrecision)
2551 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2552 DAG.getValueType(VT));
2553 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002554
2555 case ISD::UDIV:
2556 case ISD::UREM:
2557 // These operators require that their input be zero extended.
2558 Tmp1 = PromoteOp(Node->getOperand(0));
2559 Tmp2 = PromoteOp(Node->getOperand(1));
2560 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002561 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2562 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002563 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2564 break;
2565
2566 case ISD::SHL:
2567 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002568 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002569 break;
2570 case ISD::SRA:
2571 // The input value must be properly sign extended.
2572 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002573 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2574 DAG.getValueType(VT));
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002575 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002576 break;
2577 case ISD::SRL:
2578 // The input value must be properly zero extended.
2579 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002580 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002581 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner4d978642005-01-15 22:16:26 +00002582 break;
Nate Begeman595ec732006-01-28 03:14:31 +00002583
2584 case ISD::VAARG:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002585 Tmp1 = Node->getOperand(0); // Get the chain.
2586 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman595ec732006-01-28 03:14:31 +00002587 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
2588 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
2589 Result = TLI.CustomPromoteOperation(Tmp3, DAG);
2590 } else {
2591 SDOperand VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2,
2592 Node->getOperand(2));
2593 // Increment the pointer, VAList, to the next vaarg
2594 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
2595 DAG.getConstant(MVT::getSizeInBits(VT)/8,
2596 TLI.getPointerTy()));
2597 // Store the incremented VAList to the legalized pointer
2598 Tmp3 = DAG.getNode(ISD::STORE, MVT::Other, VAList.getValue(1), Tmp3, Tmp2,
2599 Node->getOperand(2));
2600 // Load the actual argument out of the pointer VAList
2601 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList,
2602 DAG.getSrcValue(0), VT);
2603 }
2604 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002605 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman595ec732006-01-28 03:14:31 +00002606 break;
2607
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002608 case ISD::LOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002609 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Node->getOperand(0),
2610 Node->getOperand(1), Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002611 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002612 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002613 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002614 case ISD::SEXTLOAD:
2615 case ISD::ZEXTLOAD:
2616 case ISD::EXTLOAD:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002617 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Node->getOperand(0),
2618 Node->getOperand(1), Node->getOperand(2),
Chris Lattnerb986f472005-10-15 20:24:07 +00002619 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002620 // Remember that we legalized the chain.
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002621 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002622 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002623 case ISD::SELECT:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002624 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2625 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002626 Result = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002627 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002628 case ISD::SELECT_CC:
2629 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2630 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2631 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002632 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begemane5b86d72005-08-10 20:51:12 +00002633 break;
Nate Begeman2fba8a32006-01-14 03:14:10 +00002634 case ISD::BSWAP:
2635 Tmp1 = Node->getOperand(0);
2636 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2637 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
2638 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
2639 DAG.getConstant(getSizeInBits(NVT) - getSizeInBits(VT),
2640 TLI.getShiftAmountTy()));
2641 break;
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002642 case ISD::CTPOP:
2643 case ISD::CTTZ:
2644 case ISD::CTLZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002645 // Zero extend the argument
2646 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002647 // Perform the larger operation, then subtract if needed.
2648 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002649 switch(Node->getOpcode()) {
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002650 case ISD::CTPOP:
2651 Result = Tmp1;
2652 break;
2653 case ISD::CTTZ:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002654 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002655 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002656 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002657 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002658 DAG.getConstant(getSizeInBits(VT), NVT), Tmp1);
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002659 break;
2660 case ISD::CTLZ:
2661 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002662 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2663 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002664 getSizeInBits(VT), NVT));
2665 break;
2666 }
2667 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002668 }
2669
2670 assert(Result.Val && "Didn't set a result!");
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002671
2672 // Make sure the result is itself legal.
2673 Result = LegalizeOp(Result);
2674
2675 // Remember that we promoted this!
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002676 AddPromotedOperand(Op, Result);
2677 return Result;
2678}
Chris Lattnerdc750592005-01-07 07:47:09 +00002679
Chris Lattner36e663d2005-12-23 00:16:34 +00002680/// ExpandBIT_CONVERT - Expand a BIT_CONVERT node into a store/load combination.
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002681/// The resultant code need not be legal. Note that SrcOp is the input operand
2682/// to the BIT_CONVERT, not the BIT_CONVERT node itself.
Chris Lattner36e663d2005-12-23 00:16:34 +00002683SDOperand SelectionDAGLegalize::ExpandBIT_CONVERT(MVT::ValueType DestVT,
2684 SDOperand SrcOp) {
2685 // Create the stack frame object.
2686 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
2687 unsigned ByteSize = MVT::getSizeInBits(DestVT)/8;
Chris Lattner884eb3ad2005-12-23 00:52:30 +00002688 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, ByteSize);
Chris Lattner36e663d2005-12-23 00:16:34 +00002689 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
2690
2691 // Emit a store to the stack slot.
2692 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner9eae8d52005-12-23 00:50:25 +00002693 SrcOp, FIPtr, DAG.getSrcValue(NULL));
Chris Lattner36e663d2005-12-23 00:16:34 +00002694 // Result is a load from the stack slot.
2695 return DAG.getLoad(DestVT, Store, FIPtr, DAG.getSrcValue(0));
2696}
2697
Chris Lattner4157c412005-04-02 04:00:59 +00002698void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2699 SDOperand Op, SDOperand Amt,
2700 SDOperand &Lo, SDOperand &Hi) {
2701 // Expand the subcomponents.
2702 SDOperand LHSL, LHSH;
2703 ExpandOp(Op, LHSL, LHSH);
2704
2705 std::vector<SDOperand> Ops;
2706 Ops.push_back(LHSL);
2707 Ops.push_back(LHSH);
2708 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002709 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002710 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002711 Hi = Lo.getValue(1);
2712}
2713
2714
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002715/// ExpandShift - Try to find a clever way to expand this shift operation out to
2716/// smaller elements. If we can't find a way that is more efficient than a
2717/// libcall on this target, return false. Otherwise, return true with the
2718/// low-parts expanded into Lo and Hi.
2719bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2720 SDOperand &Lo, SDOperand &Hi) {
2721 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2722 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002723
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002724 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002725 SDOperand ShAmt = LegalizeOp(Amt);
2726 MVT::ValueType ShTy = ShAmt.getValueType();
2727 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2728 unsigned NVTBits = MVT::getSizeInBits(NVT);
2729
2730 // Handle the case when Amt is an immediate. Other cases are currently broken
2731 // and are disabled.
2732 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2733 unsigned Cst = CN->getValue();
2734 // Expand the incoming operand to be shifted, so that we have its parts
2735 SDOperand InL, InH;
2736 ExpandOp(Op, InL, InH);
2737 switch(Opc) {
2738 case ISD::SHL:
2739 if (Cst > VTBits) {
2740 Lo = DAG.getConstant(0, NVT);
2741 Hi = DAG.getConstant(0, NVT);
2742 } else if (Cst > NVTBits) {
2743 Lo = DAG.getConstant(0, NVT);
2744 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002745 } else if (Cst == NVTBits) {
2746 Lo = DAG.getConstant(0, NVT);
2747 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002748 } else {
2749 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2750 Hi = DAG.getNode(ISD::OR, NVT,
2751 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2752 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2753 }
2754 return true;
2755 case ISD::SRL:
2756 if (Cst > VTBits) {
2757 Lo = DAG.getConstant(0, NVT);
2758 Hi = DAG.getConstant(0, NVT);
2759 } else if (Cst > NVTBits) {
2760 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2761 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002762 } else if (Cst == NVTBits) {
2763 Lo = InH;
2764 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002765 } else {
2766 Lo = DAG.getNode(ISD::OR, NVT,
2767 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2768 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2769 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2770 }
2771 return true;
2772 case ISD::SRA:
2773 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002774 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002775 DAG.getConstant(NVTBits-1, ShTy));
2776 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002777 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002778 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002779 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002780 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002781 } else if (Cst == NVTBits) {
2782 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002783 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002784 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002785 } else {
2786 Lo = DAG.getNode(ISD::OR, NVT,
2787 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2788 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2789 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2790 }
2791 return true;
2792 }
2793 }
Nate Begemanb0674922005-04-06 21:13:14 +00002794 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002795}
Chris Lattneraac464e2005-01-21 06:05:23 +00002796
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002797/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2798/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002799/// Found.
Chris Lattner90ba5442006-01-09 23:21:49 +00002800static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found,
2801 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002802 if (Node->getNodeDepth() <= Found->getNodeDepth() ||
Chris Lattner90ba5442006-01-09 23:21:49 +00002803 !Visited.insert(Node).second) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002804
Chris Lattner2dce7032005-05-12 23:24:06 +00002805 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002806 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002807 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002808 Found = Node;
2809 return;
2810 }
2811
2812 // Otherwise, scan the operands of Node to see if any of them is a call.
2813 assert(Node->getNumOperands() != 0 &&
2814 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002815 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner90ba5442006-01-09 23:21:49 +00002816 FindLatestCallSeqStart(Node->getOperand(i).Val, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002817
2818 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002819 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner90ba5442006-01-09 23:21:49 +00002820 Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002821}
2822
2823
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002824/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2825/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002826/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002827static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2828 std::set<SDNode*> &Visited) {
Chris Lattner15afe462006-01-20 18:40:10 +00002829 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
Chris Lattner96ad3132005-08-05 18:10:27 +00002830 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002831
Chris Lattner2dce7032005-05-12 23:24:06 +00002832 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002833 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002834 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002835 Found = Node;
2836 return;
2837 }
2838
2839 // Otherwise, scan the operands of Node to see if any of them is a call.
2840 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2841 if (UI == E) return;
2842 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002843 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002844
2845 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002846 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002847}
2848
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002849/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002850/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002851static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002852 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002853 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002854 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002855 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002856
Chris Lattner02011c92006-01-14 22:41:46 +00002857 // The chain is usually at the end.
Chris Lattner4add7e32005-01-23 04:42:50 +00002858 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner02011c92006-01-14 22:41:46 +00002859 if (TheChain.getValueType() != MVT::Other) {
2860 // Sometimes it's at the beginning.
Chris Lattner3268f242005-05-14 08:34:53 +00002861 TheChain = SDOperand(Node, 0);
Chris Lattner02011c92006-01-14 22:41:46 +00002862 if (TheChain.getValueType() != MVT::Other) {
2863 // Otherwise, hunt for it.
2864 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
2865 if (Node->getValueType(i) == MVT::Other) {
2866 TheChain = SDOperand(Node, i);
2867 break;
2868 }
2869
2870 // Otherwise, we walked into a node without a chain.
2871 if (TheChain.getValueType() != MVT::Other)
2872 return 0;
2873 }
2874 }
Misha Brukman835702a2005-04-21 22:36:52 +00002875
2876 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002877 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002878
Chris Lattner4add7e32005-01-23 04:42:50 +00002879 // Make sure to only follow users of our token chain.
2880 SDNode *User = *UI;
2881 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2882 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002883 if (SDNode *Result = FindCallSeqEnd(User))
2884 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002885 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002886 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002887}
2888
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002889/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002890/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002891static SDNode *FindCallSeqStart(SDNode *Node) {
2892 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002893 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002894
2895 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2896 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002897 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002898}
2899
2900
Chris Lattner4add7e32005-01-23 04:42:50 +00002901/// FindInputOutputChains - If we are replacing an operation with a call we need
2902/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002903/// properly serialize the calls in the block. The returned operand is the
2904/// input chain value for the new call (e.g. the entry node or the previous
2905/// call), and OutChain is set to be the chain node to update to point to the
2906/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002907static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2908 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002909 SDNode *LatestCallSeqStart = Entry.Val;
2910 SDNode *LatestCallSeqEnd = 0;
Chris Lattner90ba5442006-01-09 23:21:49 +00002911 std::set<SDNode*> Visited;
2912 FindLatestCallSeqStart(OpNode, LatestCallSeqStart, Visited);
2913 Visited.clear();
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002914 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002915
Chris Lattner2dce7032005-05-12 23:24:06 +00002916 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002917 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002918 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2919 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002920 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002921 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002922 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2923 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002924 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002925 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002926 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002927
Chris Lattner06bbeb62005-05-11 19:02:11 +00002928 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002929 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002930 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002931 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner90ba5442006-01-09 23:21:49 +00002932 Visited.clear();
Chris Lattner4add7e32005-01-23 04:42:50 +00002933
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002934 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002935 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002936 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002937
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002938 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002939}
2940
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002941/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002942void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2943 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002944 // Nothing to splice it into?
2945 if (OutChain == 0) return;
2946
2947 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2948 //OutChain->dump();
2949
2950 // Form a token factor node merging the old inval and the new inval.
2951 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2952 OutChain->getOperand(0));
2953 // Change the node to refer to the new token.
2954 OutChain->setAdjCallChain(InToken);
2955}
Chris Lattner4add7e32005-01-23 04:42:50 +00002956
2957
Chris Lattneraac464e2005-01-21 06:05:23 +00002958// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2959// does not fit into a register, return the lo part and set the hi part to the
2960// by-reg argument. If it does fit into a single register, return the result
2961// and leave the Hi part unset.
2962SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2963 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002964 SDNode *OutChain;
2965 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2966 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002967 if (InChain.Val == 0)
2968 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002969
Chris Lattneraac464e2005-01-21 06:05:23 +00002970 TargetLowering::ArgListTy Args;
2971 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2972 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2973 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2974 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2975 }
2976 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002977
Chris Lattner06bbeb62005-05-11 19:02:11 +00002978 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002979 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002980 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002981 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2982 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002983
Chris Lattner63022662005-09-02 20:26:58 +00002984 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002985 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002986 default: assert(0 && "Unknown thing");
2987 case Legal:
Chris Lattner9dcce6d2006-01-28 07:39:30 +00002988 Result = CallInfo.first;
Chris Lattner63022662005-09-02 20:26:58 +00002989 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002990 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00002991 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner63022662005-09-02 20:26:58 +00002992 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002993 }
Chris Lattner10f67752006-01-28 04:28:26 +00002994
2995 CallInfo.second = LegalizeOp(CallInfo.second);
Chris Lattner63022662005-09-02 20:26:58 +00002996 SpliceCallInto(CallInfo.second, OutChain);
Chris Lattner63022662005-09-02 20:26:58 +00002997 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00002998}
2999
Chris Lattner4add7e32005-01-23 04:42:50 +00003000
Chris Lattneraac464e2005-01-21 06:05:23 +00003001/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
3002/// destination type is legal.
3003SDOperand SelectionDAGLegalize::
3004ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003005 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00003006 assert(getTypeAction(Source.getValueType()) == Expand &&
3007 "This is not an expansion!");
3008 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
3009
Chris Lattner06bbeb62005-05-11 19:02:11 +00003010 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003011 assert(Source.getValueType() == MVT::i64 &&
3012 "This only works for 64-bit -> FP");
3013 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
3014 // incoming integer is set. To handle this, we dynamically test to see if
3015 // it is set, and, if so, add a fudge factor.
3016 SDOperand Lo, Hi;
3017 ExpandOp(Source, Lo, Hi);
3018
Chris Lattner2a4f7312005-05-13 04:45:13 +00003019 // If this is unsigned, and not supported, first perform the conversion to
3020 // signed, then adjust the result if the sign bit is set.
3021 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
3022 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
3023
Chris Lattnerd47675e2005-08-09 20:20:18 +00003024 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
3025 DAG.getConstant(0, Hi.getValueType()),
3026 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003027 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3028 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3029 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00003030 uint64_t FF = 0x5f800000ULL;
3031 if (TLI.isLittleEndian()) FF <<= 32;
3032 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003033
Chris Lattnerc30405e2005-08-26 17:15:30 +00003034 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003035 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3036 SDOperand FudgeInReg;
3037 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00003038 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3039 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003040 else {
3041 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00003042 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
3043 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00003044 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00003045 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00003046 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00003047
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003048 // Check to see if the target has a custom way to lower this. If so, use it.
3049 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
3050 default: assert(0 && "This action not implemented for this operation!");
3051 case TargetLowering::Legal:
3052 case TargetLowering::Expand:
3053 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003054 case TargetLowering::Custom: {
3055 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
3056 Source), DAG);
3057 if (NV.Val)
3058 return LegalizeOp(NV);
3059 break; // The target decided this was legal after all
3060 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00003061 }
3062
Chris Lattner153587e2005-05-12 07:00:44 +00003063 // Expand the source, then glue it back together for the call. We must expand
3064 // the source in case it is shared (this pass of legalize must traverse it).
3065 SDOperand SrcLo, SrcHi;
3066 ExpandOp(Source, SrcLo, SrcHi);
3067 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3068
Chris Lattner06bbeb62005-05-11 19:02:11 +00003069 SDNode *OutChain = 0;
3070 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3071 DAG.getEntryNode());
3072 const char *FnName = 0;
3073 if (DestTy == MVT::f32)
3074 FnName = "__floatdisf";
3075 else {
3076 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3077 FnName = "__floatdidf";
3078 }
3079
Chris Lattneraac464e2005-01-21 06:05:23 +00003080 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3081
3082 TargetLowering::ArgListTy Args;
3083 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003084
Chris Lattneraac464e2005-01-21 06:05:23 +00003085 Args.push_back(std::make_pair(Source, ArgTy));
3086
3087 // We don't care about token chains for libcalls. We just use the entry
3088 // node as our input and ignore the output chain. This allows us to place
3089 // calls wherever we need them to satisfy data dependences.
3090 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003091
3092 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003093 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3094 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003095
Chris Lattnera5bf1032005-05-12 04:49:08 +00003096 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003097 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003098}
Misha Brukman835702a2005-04-21 22:36:52 +00003099
Chris Lattner689bdcc2006-01-28 08:25:58 +00003100/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
3101/// INT_TO_FP operation of the specified operand when the target requests that
3102/// we expand it. At this point, we know that the result and operand types are
3103/// legal for the target.
3104SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
3105 SDOperand Op0,
3106 MVT::ValueType DestVT) {
3107 if (Op0.getValueType() == MVT::i32) {
3108 // simple 32-bit [signed|unsigned] integer to float/double expansion
3109
3110 // get the stack frame index of a 8 byte buffer
3111 MachineFunction &MF = DAG.getMachineFunction();
3112 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
3113 // get address of 8 byte buffer
3114 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
3115 // word offset constant for Hi/Lo address computation
3116 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
3117 // set up Hi and Lo (into buffer) address based on endian
3118 SDOperand Hi, Lo;
3119 if (TLI.isLittleEndian()) {
3120 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3121 Lo = StackSlot;
3122 } else {
3123 Hi = StackSlot;
3124 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
3125 }
3126 // if signed map to unsigned space
3127 SDOperand Op0Mapped;
3128 if (isSigned) {
3129 // constant used to invert sign bit (signed to unsigned mapping)
3130 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
3131 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
3132 } else {
3133 Op0Mapped = Op0;
3134 }
3135 // store the lo of the constructed double - based on integer input
3136 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
3137 Op0Mapped, Lo, DAG.getSrcValue(NULL));
3138 // initial hi portion of constructed double
3139 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
3140 // store the hi of the constructed double - biased exponent
3141 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
3142 InitialHi, Hi, DAG.getSrcValue(NULL));
3143 // load the constructed double
3144 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
3145 DAG.getSrcValue(NULL));
3146 // FP constant to bias correct the final result
3147 SDOperand Bias = DAG.getConstantFP(isSigned ?
3148 BitsToDouble(0x4330000080000000ULL)
3149 : BitsToDouble(0x4330000000000000ULL),
3150 MVT::f64);
3151 // subtract the bias
3152 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
3153 // final result
3154 SDOperand Result;
3155 // handle final rounding
3156 if (DestVT == MVT::f64) {
3157 // do nothing
3158 Result = Sub;
3159 } else {
3160 // if f32 then cast to f32
3161 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
3162 }
3163 return Result;
3164 }
3165 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
3166 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
3167
3168 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
3169 DAG.getConstant(0, Op0.getValueType()),
3170 ISD::SETLT);
3171 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
3172 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
3173 SignSet, Four, Zero);
3174
3175 // If the sign bit of the integer is set, the large number will be treated
3176 // as a negative number. To counteract this, the dynamic code adds an
3177 // offset depending on the data type.
3178 uint64_t FF;
3179 switch (Op0.getValueType()) {
3180 default: assert(0 && "Unsupported integer type!");
3181 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
3182 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
3183 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
3184 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
3185 }
3186 if (TLI.isLittleEndian()) FF <<= 32;
3187 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
3188
3189 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
3190 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
3191 SDOperand FudgeInReg;
3192 if (DestVT == MVT::f32)
3193 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
3194 DAG.getSrcValue(NULL));
3195 else {
3196 assert(DestVT == MVT::f64 && "Unexpected conversion");
3197 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
3198 DAG.getEntryNode(), CPIdx,
3199 DAG.getSrcValue(NULL), MVT::f32));
3200 }
3201
3202 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
3203}
3204
3205/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
3206/// *INT_TO_FP operation of the specified operand when the target requests that
3207/// we promote it. At this point, we know that the result and operand types are
3208/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3209/// operation that takes a larger input.
3210SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
3211 MVT::ValueType DestVT,
3212 bool isSigned) {
3213 // First step, figure out the appropriate *INT_TO_FP operation to use.
3214 MVT::ValueType NewInTy = LegalOp.getValueType();
3215
3216 unsigned OpToUse = 0;
3217
3218 // Scan for the appropriate larger type to use.
3219 while (1) {
3220 NewInTy = (MVT::ValueType)(NewInTy+1);
3221 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
3222
3223 // If the target supports SINT_TO_FP of this type, use it.
3224 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
3225 default: break;
3226 case TargetLowering::Legal:
3227 if (!TLI.isTypeLegal(NewInTy))
3228 break; // Can't use this datatype.
3229 // FALL THROUGH.
3230 case TargetLowering::Custom:
3231 OpToUse = ISD::SINT_TO_FP;
3232 break;
3233 }
3234 if (OpToUse) break;
3235 if (isSigned) continue;
3236
3237 // If the target supports UINT_TO_FP of this type, use it.
3238 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
3239 default: break;
3240 case TargetLowering::Legal:
3241 if (!TLI.isTypeLegal(NewInTy))
3242 break; // Can't use this datatype.
3243 // FALL THROUGH.
3244 case TargetLowering::Custom:
3245 OpToUse = ISD::UINT_TO_FP;
3246 break;
3247 }
3248 if (OpToUse) break;
3249
3250 // Otherwise, try a larger type.
3251 }
3252
3253 // Okay, we found the operation and type to use. Zero extend our input to the
3254 // desired type then run the operation on it.
3255 return DAG.getNode(OpToUse, DestVT,
3256 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3257 NewInTy, LegalOp));
3258}
3259
3260/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
3261/// FP_TO_*INT operation of the specified operand when the target requests that
3262/// we promote it. At this point, we know that the result and operand types are
3263/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3264/// operation that returns a larger result.
3265SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
3266 MVT::ValueType DestVT,
3267 bool isSigned) {
3268 // First step, figure out the appropriate FP_TO*INT operation to use.
3269 MVT::ValueType NewOutTy = DestVT;
3270
3271 unsigned OpToUse = 0;
3272
3273 // Scan for the appropriate larger type to use.
3274 while (1) {
3275 NewOutTy = (MVT::ValueType)(NewOutTy+1);
3276 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
3277
3278 // If the target supports FP_TO_SINT returning this type, use it.
3279 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
3280 default: break;
3281 case TargetLowering::Legal:
3282 if (!TLI.isTypeLegal(NewOutTy))
3283 break; // Can't use this datatype.
3284 // FALL THROUGH.
3285 case TargetLowering::Custom:
3286 OpToUse = ISD::FP_TO_SINT;
3287 break;
3288 }
3289 if (OpToUse) break;
3290
3291 // If the target supports FP_TO_UINT of this type, use it.
3292 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
3293 default: break;
3294 case TargetLowering::Legal:
3295 if (!TLI.isTypeLegal(NewOutTy))
3296 break; // Can't use this datatype.
3297 // FALL THROUGH.
3298 case TargetLowering::Custom:
3299 OpToUse = ISD::FP_TO_UINT;
3300 break;
3301 }
3302 if (OpToUse) break;
3303
3304 // Otherwise, try a larger type.
3305 }
3306
3307 // Okay, we found the operation and type to use. Truncate the result of the
3308 // extended FP_TO_*INT operation to the desired size.
3309 return DAG.getNode(ISD::TRUNCATE, DestVT,
3310 DAG.getNode(OpToUse, NewOutTy, LegalOp));
3311}
3312
3313/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
3314///
3315SDOperand SelectionDAGLegalize::ExpandBSWAP(SDOperand Op) {
3316 MVT::ValueType VT = Op.getValueType();
3317 MVT::ValueType SHVT = TLI.getShiftAmountTy();
3318 SDOperand Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
3319 switch (VT) {
3320 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
3321 case MVT::i16:
3322 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3323 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3324 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
3325 case MVT::i32:
3326 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3327 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3328 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3329 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3330 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
3331 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
3332 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3333 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3334 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3335 case MVT::i64:
3336 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
3337 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
3338 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
3339 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
3340 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
3341 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
3342 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
3343 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
3344 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
3345 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
3346 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
3347 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
3348 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
3349 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
3350 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
3351 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
3352 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
3353 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
3354 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
3355 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
3356 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
3357 }
3358}
3359
3360/// ExpandBitCount - Expand the specified bitcount instruction into operations.
3361///
3362SDOperand SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDOperand Op) {
3363 switch (Opc) {
3364 default: assert(0 && "Cannot expand this yet!");
3365 case ISD::CTPOP: {
3366 static const uint64_t mask[6] = {
3367 0x5555555555555555ULL, 0x3333333333333333ULL,
3368 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
3369 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
3370 };
3371 MVT::ValueType VT = Op.getValueType();
3372 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3373 unsigned len = getSizeInBits(VT);
3374 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3375 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
3376 SDOperand Tmp2 = DAG.getConstant(mask[i], VT);
3377 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3378 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
3379 DAG.getNode(ISD::AND, VT,
3380 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
3381 }
3382 return Op;
3383 }
3384 case ISD::CTLZ: {
3385 // for now, we do this:
3386 // x = x | (x >> 1);
3387 // x = x | (x >> 2);
3388 // ...
3389 // x = x | (x >>16);
3390 // x = x | (x >>32); // for 64-bit input
3391 // return popcount(~x);
3392 //
3393 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
3394 MVT::ValueType VT = Op.getValueType();
3395 MVT::ValueType ShVT = TLI.getShiftAmountTy();
3396 unsigned len = getSizeInBits(VT);
3397 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
3398 SDOperand Tmp3 = DAG.getConstant(1ULL << i, ShVT);
3399 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
3400 }
3401 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
3402 return DAG.getNode(ISD::CTPOP, VT, Op);
3403 }
3404 case ISD::CTTZ: {
3405 // for now, we use: { return popcount(~x & (x - 1)); }
3406 // unless the target has ctlz but not ctpop, in which case we use:
3407 // { return 32 - nlz(~x & (x-1)); }
3408 // see also http://www.hackersdelight.org/HDcode/ntz.cc
3409 MVT::ValueType VT = Op.getValueType();
3410 SDOperand Tmp2 = DAG.getConstant(~0ULL, VT);
3411 SDOperand Tmp3 = DAG.getNode(ISD::AND, VT,
3412 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
3413 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
3414 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
3415 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
3416 TLI.isOperationLegal(ISD::CTLZ, VT))
3417 return DAG.getNode(ISD::SUB, VT,
3418 DAG.getConstant(getSizeInBits(VT), VT),
3419 DAG.getNode(ISD::CTLZ, VT, Tmp3));
3420 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
3421 }
3422 }
3423}
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003424
3425
Chris Lattnerdc750592005-01-07 07:47:09 +00003426/// ExpandOp - Expand the specified SDOperand into its two component pieces
3427/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3428/// LegalizeNodes map is filled in for any results that are not expanded, the
3429/// ExpandedNodes map is filled in for any results that are expanded, and the
3430/// Lo/Hi values are returned.
3431void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3432 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003433 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003434 SDNode *Node = Op.Val;
3435 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Nate Begemand37c1312005-11-22 18:16:00 +00003436 assert((MVT::isInteger(VT) || VT == MVT::Vector) &&
3437 "Cannot expand FP values!");
3438 assert(((MVT::isInteger(NVT) && NVT < VT) || VT == MVT::Vector) &&
Chris Lattnerdc750592005-01-07 07:47:09 +00003439 "Cannot expand to FP value or to larger int value!");
3440
Chris Lattner1a570f12005-09-02 20:32:45 +00003441 // See if we already expanded it.
3442 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3443 = ExpandedNodes.find(Op);
3444 if (I != ExpandedNodes.end()) {
3445 Lo = I->second.first;
3446 Hi = I->second.second;
3447 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003448 }
3449
Chris Lattnerdc750592005-01-07 07:47:09 +00003450 switch (Node->getOpcode()) {
Chris Lattner44cab002006-01-21 04:27:00 +00003451 case ISD::CopyFromReg:
3452 assert(0 && "CopyFromReg must be legal!");
3453 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003454 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3455 assert(0 && "Do not know how to expand this operator!");
3456 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003457 case ISD::UNDEF:
3458 Lo = DAG.getNode(ISD::UNDEF, NVT);
3459 Hi = DAG.getNode(ISD::UNDEF, NVT);
3460 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003461 case ISD::Constant: {
3462 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3463 Lo = DAG.getConstant(Cst, NVT);
3464 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3465 break;
3466 }
Nate Begemanae89d862005-12-07 19:48:11 +00003467 case ISD::ConstantVec: {
3468 unsigned NumElements = Node->getNumOperands();
3469 // If we only have two elements left in the constant vector, just break it
3470 // apart into the two scalar constants it contains. Otherwise, bisect the
3471 // ConstantVec, and return each half as a new ConstantVec.
3472 // FIXME: this is hard coded as big endian, it may have to change to support
3473 // SSE and Alpha MVI
3474 if (NumElements == 2) {
3475 Hi = Node->getOperand(0);
3476 Lo = Node->getOperand(1);
3477 } else {
3478 NumElements /= 2;
3479 std::vector<SDOperand> LoOps, HiOps;
3480 for (unsigned I = 0, E = NumElements; I < E; ++I) {
3481 HiOps.push_back(Node->getOperand(I));
3482 LoOps.push_back(Node->getOperand(I+NumElements));
3483 }
3484 Lo = DAG.getNode(ISD::ConstantVec, MVT::Vector, LoOps);
3485 Hi = DAG.getNode(ISD::ConstantVec, MVT::Vector, HiOps);
3486 }
3487 break;
3488 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003489
Chris Lattner32e08b72005-03-28 22:03:13 +00003490 case ISD::BUILD_PAIR:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003491 // Return the operands.
3492 Lo = Node->getOperand(0);
3493 Hi = Node->getOperand(1);
Chris Lattner32e08b72005-03-28 22:03:13 +00003494 break;
Chris Lattnerb42ce7c2005-12-12 22:27:43 +00003495
3496 case ISD::SIGN_EXTEND_INREG:
3497 ExpandOp(Node->getOperand(0), Lo, Hi);
3498 // Sign extend the lo-part.
3499 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3500 DAG.getConstant(MVT::getSizeInBits(NVT)-1,
3501 TLI.getShiftAmountTy()));
3502 // sext_inreg the low part if needed.
3503 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
3504 break;
Chris Lattner32e08b72005-03-28 22:03:13 +00003505
Nate Begeman2fba8a32006-01-14 03:14:10 +00003506 case ISD::BSWAP: {
3507 ExpandOp(Node->getOperand(0), Lo, Hi);
3508 SDOperand TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
3509 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
3510 Lo = TempLo;
3511 break;
3512 }
3513
Chris Lattner55e9cde2005-05-11 04:51:16 +00003514 case ISD::CTPOP:
3515 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003516 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3517 DAG.getNode(ISD::CTPOP, NVT, Lo),
3518 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003519 Hi = DAG.getConstant(0, NVT);
3520 break;
3521
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003522 case ISD::CTLZ: {
3523 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003524 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003525 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3526 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003527 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3528 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003529 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3530 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3531
3532 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3533 Hi = DAG.getConstant(0, NVT);
3534 break;
3535 }
3536
3537 case ISD::CTTZ: {
3538 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003539 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003540 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3541 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003542 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3543 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003544 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3545 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3546
3547 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3548 Hi = DAG.getConstant(0, NVT);
3549 break;
3550 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003551
Nate Begemane74795c2006-01-25 18:21:52 +00003552 case ISD::VAARG: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003553 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3554 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemane74795c2006-01-25 18:21:52 +00003555 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
3556 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
3557
3558 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003559 Hi = LegalizeOp(Hi);
Nate Begemane74795c2006-01-25 18:21:52 +00003560 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
3561 if (!TLI.isLittleEndian())
3562 std::swap(Lo, Hi);
3563 break;
3564 }
3565
Chris Lattnerdc750592005-01-07 07:47:09 +00003566 case ISD::LOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003567 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3568 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003569 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003570
3571 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003572 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003573 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3574 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003575 // FIXME: This creates a bogus srcvalue!
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003576 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003577
3578 // Build a factor node to remember that this load is independent of the
3579 // other one.
3580 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3581 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003582
Chris Lattnerdc750592005-01-07 07:47:09 +00003583 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003584 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerdc750592005-01-07 07:47:09 +00003585 if (!TLI.isLittleEndian())
3586 std::swap(Lo, Hi);
3587 break;
3588 }
Nate Begemand37c1312005-11-22 18:16:00 +00003589 case ISD::VLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003590 SDOperand Ch = Node->getOperand(0); // Legalize the chain.
3591 SDOperand Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemand37c1312005-11-22 18:16:00 +00003592 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3593 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3594
3595 // If we only have two elements, turn into a pair of scalar loads.
3596 // FIXME: handle case where a vector of two elements is fine, such as
3597 // 2 x double on SSE2.
3598 if (NumElements == 2) {
3599 Lo = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3600 // Increment the pointer to the other half.
3601 unsigned IncrementSize = MVT::getSizeInBits(EVT)/8;
3602 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3603 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003604 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003605 Hi = DAG.getLoad(EVT, Ch, Ptr, Node->getOperand(4));
3606 } else {
3607 NumElements /= 2; // Split the vector in half
3608 Lo = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3609 unsigned IncrementSize = NumElements * MVT::getSizeInBits(EVT)/8;
3610 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3611 getIntPtrConstant(IncrementSize));
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003612 // FIXME: This creates a bogus srcvalue!
Nate Begemand37c1312005-11-22 18:16:00 +00003613 Hi = DAG.getVecLoad(NumElements, EVT, Ch, Ptr, Node->getOperand(4));
3614 }
3615
3616 // Build a factor node to remember that this load is independent of the
3617 // other one.
3618 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3619 Hi.getValue(1));
3620
3621 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003622 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Nate Begemand37c1312005-11-22 18:16:00 +00003623 if (!TLI.isLittleEndian())
3624 std::swap(Lo, Hi);
3625 break;
3626 }
3627 case ISD::VADD:
3628 case ISD::VSUB:
3629 case ISD::VMUL: {
3630 unsigned NumElements =cast<ConstantSDNode>(Node->getOperand(2))->getValue();
3631 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3632 SDOperand LL, LH, RL, RH;
3633
3634 ExpandOp(Node->getOperand(0), LL, LH);
3635 ExpandOp(Node->getOperand(1), RL, RH);
3636
3637 // If we only have two elements, turn into a pair of scalar loads.
3638 // FIXME: handle case where a vector of two elements is fine, such as
3639 // 2 x double on SSE2.
3640 if (NumElements == 2) {
3641 unsigned Opc = getScalarizedOpcode(Node->getOpcode(), EVT);
3642 Lo = DAG.getNode(Opc, EVT, LL, RL);
3643 Hi = DAG.getNode(Opc, EVT, LH, RH);
3644 } else {
3645 Lo = DAG.getNode(Node->getOpcode(), MVT::Vector, LL, RL, LL.getOperand(2),
3646 LL.getOperand(3));
3647 Hi = DAG.getNode(Node->getOpcode(), MVT::Vector, LH, RH, LH.getOperand(2),
3648 LH.getOperand(3));
3649 }
3650 break;
3651 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003652 case ISD::AND:
3653 case ISD::OR:
3654 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3655 SDOperand LL, LH, RL, RH;
3656 ExpandOp(Node->getOperand(0), LL, LH);
3657 ExpandOp(Node->getOperand(1), RL, RH);
3658 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3659 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3660 break;
3661 }
3662 case ISD::SELECT: {
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003663 SDOperand LL, LH, RL, RH;
Chris Lattnerdc750592005-01-07 07:47:09 +00003664 ExpandOp(Node->getOperand(1), LL, LH);
3665 ExpandOp(Node->getOperand(2), RL, RH);
Chris Lattner9dcce6d2006-01-28 07:39:30 +00003666 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
3667 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattnerdc750592005-01-07 07:47:09 +00003668 break;
3669 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003670 case ISD::SELECT_CC: {
3671 SDOperand TL, TH, FL, FH;
3672 ExpandOp(Node->getOperand(2), TL, TH);
3673 ExpandOp(Node->getOperand(3), FL, FH);
3674 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3675 Node->getOperand(1), TL, FL, Node->getOperand(4));
3676 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3677 Node->getOperand(1), TH, FH, Node->getOperand(4));
3678 break;
3679 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003680 case ISD::SEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003681 SDOperand Chain = Node->getOperand(0);
3682 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003683 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3684
3685 if (EVT == NVT)
3686 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3687 else
3688 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3689 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003690
3691 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003692 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003693
Nate Begemanc3a89c52005-10-13 17:15:37 +00003694 // The high part is obtained by SRA'ing all but one of the bits of the lo
3695 // part.
3696 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3697 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3698 TLI.getShiftAmountTy()));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003699 break;
3700 }
3701 case ISD::ZEXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003702 SDOperand Chain = Node->getOperand(0);
3703 SDOperand Ptr = Node->getOperand(1);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003704 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3705
3706 if (EVT == NVT)
3707 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3708 else
3709 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3710 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003711
3712 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003713 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003714
Nate Begemanc3a89c52005-10-13 17:15:37 +00003715 // The high part is just a zero.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003716 Hi = DAG.getConstant(0, NVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003717 break;
3718 }
3719 case ISD::EXTLOAD: {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003720 SDOperand Chain = Node->getOperand(0);
3721 SDOperand Ptr = Node->getOperand(1);
Chris Lattner258521d2005-10-13 21:44:47 +00003722 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3723
3724 if (EVT == NVT)
3725 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3726 else
3727 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3728 EVT);
3729
3730 // Remember that we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003731 AddLegalizedOperand(SDOperand(Node, 1), LegalizeOp(Lo.getValue(1)));
Chris Lattner258521d2005-10-13 21:44:47 +00003732
3733 // The high part is undefined.
Chris Lattner7753f172005-09-02 00:18:10 +00003734 Hi = DAG.getNode(ISD::UNDEF, NVT);
3735 break;
3736 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003737 case ISD::ANY_EXTEND:
3738 // The low part is any extension of the input (which degenerates to a copy).
3739 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
3740 // The high part is undefined.
3741 Hi = DAG.getNode(ISD::UNDEF, NVT);
3742 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003743 case ISD::SIGN_EXTEND: {
3744 // The low part is just a sign extension of the input (which degenerates to
3745 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003746 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003747
Chris Lattnerdc750592005-01-07 07:47:09 +00003748 // The high part is obtained by SRA'ing all but one of the bits of the lo
3749 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003750 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003751 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
3752 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003753 break;
3754 }
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003755 case ISD::ZERO_EXTEND:
Chris Lattnerdc750592005-01-07 07:47:09 +00003756 // The low part is just a zero extension of the input (which degenerates to
3757 // a copy).
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003758 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +00003759
Chris Lattnerdc750592005-01-07 07:47:09 +00003760 // The high part is just a zero.
3761 Hi = DAG.getConstant(0, NVT);
3762 break;
Chris Lattner36e663d2005-12-23 00:16:34 +00003763
3764 case ISD::BIT_CONVERT: {
3765 SDOperand Tmp = ExpandBIT_CONVERT(Node->getValueType(0),
3766 Node->getOperand(0));
3767 ExpandOp(Tmp, Lo, Hi);
3768 break;
3769 }
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003770
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003771 case ISD::READCYCLECOUNTER:
Chris Lattner44c28c22005-11-20 22:56:56 +00003772 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3773 TargetLowering::Custom &&
3774 "Must custom expand ReadCycleCounter");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003775 Lo = TLI.LowerOperation(Op, DAG);
3776 assert(Lo.Val && "Node must be custom expanded!");
3777 Hi = Lo.getValue(1);
Chris Lattner44c28c22005-11-20 22:56:56 +00003778 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003779 LegalizeOp(Lo.getValue(2)));
Andrew Lenharth627cbd42005-11-20 21:32:07 +00003780 break;
3781
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003782 // These operators cannot be expanded directly, emit them as calls to
3783 // library functions.
3784 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003785 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003786 SDOperand Op;
3787 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3788 case Expand: assert(0 && "cannot expand FP!");
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003789 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3790 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattner941d84a2005-07-30 01:40:57 +00003791 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003792
Chris Lattner941d84a2005-07-30 01:40:57 +00003793 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3794
Chris Lattnerfe68d752005-07-29 00:33:32 +00003795 // Now that the custom expander is done, expand the result, which is still
3796 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003797 if (Op.Val) {
3798 ExpandOp(Op, Lo, Hi);
3799 break;
3800 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003801 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003802
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003803 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003804 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003805 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003806 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003807 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003808
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003809 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003810 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003811 SDOperand Op;
3812 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3813 case Expand: assert(0 && "cannot expand FP!");
3814 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3815 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
3816 }
3817
3818 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
3819
3820 // Now that the custom expander is done, expand the result.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003821 if (Op.Val) {
3822 ExpandOp(Op, Lo, Hi);
3823 break;
3824 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003825 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003826
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003827 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003828 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003829 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003830 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003831 break;
3832
Evan Cheng870e4f82006-01-09 18:31:59 +00003833 case ISD::SHL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003834 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003835 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003836 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003837 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003838 Op = TLI.LowerOperation(Op, DAG);
3839 if (Op.Val) {
3840 // Now that the custom expander is done, expand the result, which is
3841 // still VT.
3842 ExpandOp(Op, Lo, Hi);
3843 break;
3844 }
3845 }
3846
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003847 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003848 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003849 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003850
3851 // If this target supports SHL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003852 TargetLowering::LegalizeAction Action =
3853 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
3854 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3855 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003856 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003857 break;
3858 }
3859
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003860 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003861 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003862 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003863 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003864
Evan Cheng870e4f82006-01-09 18:31:59 +00003865 case ISD::SRA: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003866 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003867 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003868 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003869 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003870 Op = TLI.LowerOperation(Op, DAG);
3871 if (Op.Val) {
3872 // Now that the custom expander is done, expand the result, which is
3873 // still VT.
3874 ExpandOp(Op, Lo, Hi);
3875 break;
3876 }
3877 }
3878
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003879 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003880 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003881 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003882
3883 // If this target supports SRA_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003884 TargetLowering::LegalizeAction Action =
3885 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
3886 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3887 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003888 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003889 break;
3890 }
3891
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003892 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003893 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003894 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003895 }
3896
3897 case ISD::SRL: {
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003898 // If the target wants custom lowering, do so.
Chris Lattner44cab002006-01-21 04:27:00 +00003899 SDOperand ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003900 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003901 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003902 Op = TLI.LowerOperation(Op, DAG);
3903 if (Op.Val) {
3904 // Now that the custom expander is done, expand the result, which is
3905 // still VT.
3906 ExpandOp(Op, Lo, Hi);
3907 break;
3908 }
3909 }
3910
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003911 // If we can emit an efficient shift operation, do so now.
Chris Lattner44cab002006-01-21 04:27:00 +00003912 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003913 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003914
3915 // If this target supports SRL_PARTS, use it.
Evan Cheng870e4f82006-01-09 18:31:59 +00003916 TargetLowering::LegalizeAction Action =
3917 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
3918 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3919 Action == TargetLowering::Custom) {
Chris Lattner44cab002006-01-21 04:27:00 +00003920 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003921 break;
3922 }
3923
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003924 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003925 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003926 break;
Evan Cheng870e4f82006-01-09 18:31:59 +00003927 }
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003928
Misha Brukman835702a2005-04-21 22:36:52 +00003929 case ISD::ADD:
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003930 case ISD::SUB: {
3931 // If the target wants to custom expand this, let them.
3932 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
3933 TargetLowering::Custom) {
3934 Op = TLI.LowerOperation(Op, DAG);
3935 if (Op.Val) {
3936 ExpandOp(Op, Lo, Hi);
3937 break;
3938 }
3939 }
3940
3941 // Expand the subcomponents.
3942 SDOperand LHSL, LHSH, RHSL, RHSH;
3943 ExpandOp(Node->getOperand(0), LHSL, LHSH);
3944 ExpandOp(Node->getOperand(1), RHSL, RHSH);
3945
3946 std::vector<SDOperand> Ops;
3947 Ops.push_back(LHSL);
3948 Ops.push_back(LHSH);
3949 Ops.push_back(RHSL);
3950 Ops.push_back(RHSH);
3951 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
3952 unsigned Opc =
3953 Node->getOpcode() == ISD::ADD ? ISD::ADD_PARTS : ISD::SUB_PARTS;
3954 Lo = DAG.getNode(Opc, VTs, Ops);
3955 Hi = Lo.getValue(1);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003956 break;
Chris Lattnerfd4a7f72006-01-28 05:07:51 +00003957 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003958 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003959 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003960 SDOperand LL, LH, RL, RH;
3961 ExpandOp(Node->getOperand(0), LL, LH);
3962 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003963 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3964 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3965 // extended the sign bit of the low half through the upper half, and if so
3966 // emit a MULHS instead of the alternate sequence that is valid for any
3967 // i64 x i64 multiply.
3968 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3969 // is RH an extension of the sign bit of RL?
3970 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3971 RH.getOperand(1).getOpcode() == ISD::Constant &&
3972 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3973 // is LH an extension of the sign bit of LL?
3974 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3975 LH.getOperand(1).getOpcode() == ISD::Constant &&
3976 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3977 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3978 } else {
3979 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3980 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3981 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3982 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3983 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3984 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003985 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3986 } else {
Chris Lattner10f67752006-01-28 04:28:26 +00003987 Lo = ExpandLibCall("__muldi3" , Node, Hi);
Nate Begemanadd0c632005-04-11 03:01:51 +00003988 }
3989 break;
3990 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003991 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3992 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3993 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3994 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003995 }
3996
Chris Lattnerac12f682005-12-21 18:02:52 +00003997 // Make sure the resultant values have been legalized themselves, unless this
3998 // is a type that requires multi-step expansion.
3999 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
4000 Lo = LegalizeOp(Lo);
4001 Hi = LegalizeOp(Hi);
4002 }
Evan Cheng870e4f82006-01-09 18:31:59 +00004003
4004 // Remember in a map if the values will be reused later.
4005 bool isNew =
4006 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
4007 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00004008}
4009
4010
4011// SelectionDAG::Legalize - This is the entry point for the file.
4012//
Chris Lattner4add7e32005-01-23 04:42:50 +00004013void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00004014 /// run - This is the main entry point to this class.
4015 ///
Chris Lattner9dcce6d2006-01-28 07:39:30 +00004016 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattnerdc750592005-01-07 07:47:09 +00004017}
4018