blob: b975e5859ecec9fa868d091fc764928e694912e1 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.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 Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattnerdc750592005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner1f2c9d82005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattnerdc750592005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattnerea4ca942005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
88 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
89
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000121
122 SDOperand getIntPtrConstant(uint64_t Val) {
123 return DAG.getConstant(Val, TLI.getPointerTy());
124 }
125};
126}
127
128
129SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
130 SelectionDAG &dag)
Chris Lattner87a769c2005-01-16 01:11:45 +0000131 : TLI(tli), DAG(dag), ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000132 assert(MVT::LAST_VALUETYPE <= 16 &&
133 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000134}
135
Chris Lattnerdc750592005-01-07 07:47:09 +0000136void SelectionDAGLegalize::LegalizeDAG() {
137 SDOperand OldRoot = DAG.getRoot();
138 SDOperand NewRoot = LegalizeOp(OldRoot);
139 DAG.setRoot(NewRoot);
140
141 ExpandedNodes.clear();
142 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000143 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000144
145 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000146 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000147}
148
149SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000150 assert(getTypeAction(Op.getValueType()) == Legal &&
151 "Caller should expand or promote operands that are not legal!");
152
Chris Lattnerdc750592005-01-07 07:47:09 +0000153 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000154 // register on this target, make sure to expand or promote them.
155 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000156 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
157 switch (getTypeAction(Op.Val->getValueType(i))) {
158 case Legal: break; // Nothing to do.
159 case Expand: {
160 SDOperand T1, T2;
161 ExpandOp(Op.getValue(i), T1, T2);
162 assert(LegalizedNodes.count(Op) &&
163 "Expansion didn't add legal operands!");
164 return LegalizedNodes[Op];
165 }
166 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000167 PromoteOp(Op.getValue(i));
168 assert(LegalizedNodes.count(Op) &&
169 "Expansion didn't add legal operands!");
170 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000171 }
172 }
173
Chris Lattner85d70c62005-01-11 05:57:22 +0000174 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
175 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000176
Chris Lattnerec26b482005-01-09 19:03:49 +0000177 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000178
179 SDOperand Result = Op;
180 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000181
182 switch (Node->getOpcode()) {
183 default:
184 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
185 assert(0 && "Do not know how to legalize this operator!");
186 abort();
187 case ISD::EntryToken:
188 case ISD::FrameIndex:
189 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000190 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000191 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000192 assert(getTypeAction(Node->getValueType(0)) == Legal &&
193 "This must be legal!");
194 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000195 case ISD::CopyFromReg:
196 Tmp1 = LegalizeOp(Node->getOperand(0));
197 if (Tmp1 != Node->getOperand(0))
198 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
199 Node->getValueType(0), Tmp1);
200 break;
Chris Lattnere727af02005-01-13 20:50:02 +0000201 case ISD::ImplicitDef:
202 Tmp1 = LegalizeOp(Node->getOperand(0));
203 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000204 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000205 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000206 case ISD::Constant:
207 // We know we don't need to expand constants here, constants only have one
208 // value and we check that it is fine above.
209
210 // FIXME: Maybe we should handle things like targets that don't support full
211 // 32-bit immediates?
212 break;
213 case ISD::ConstantFP: {
214 // Spill FP immediates to the constant pool if the target cannot directly
215 // codegen them. Targets often have some immediate values that can be
216 // efficiently generated into an FP register without a load. We explicitly
217 // leave these constants as ConstantFP nodes for the target to deal with.
218
219 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
220
221 // Check to see if this FP immediate is already legal.
222 bool isLegal = false;
223 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
224 E = TLI.legal_fpimm_end(); I != E; ++I)
225 if (CFP->isExactlyValue(*I)) {
226 isLegal = true;
227 break;
228 }
229
230 if (!isLegal) {
231 // Otherwise we need to spill the constant to memory.
232 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
233
234 bool Extend = false;
235
236 // If a FP immediate is precise when represented as a float, we put it
237 // into the constant pool as a float, even if it's is statically typed
238 // as a double.
239 MVT::ValueType VT = CFP->getValueType(0);
240 bool isDouble = VT == MVT::f64;
241 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
242 Type::FloatTy, CFP->getValue());
243 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
244 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
245 VT = MVT::f32;
246 Extend = true;
247 }
248
249 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
250 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000251 if (Extend) {
252 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
253 MVT::f32);
254 } else {
255 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
256 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000257 }
258 break;
259 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000260 case ISD::TokenFactor: {
261 std::vector<SDOperand> Ops;
262 bool Changed = false;
263 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
264 Ops.push_back(LegalizeOp(Node->getOperand(i))); // Legalize the operands
265 Changed |= Ops[i] != Node->getOperand(i);
266 }
267 if (Changed)
268 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
269 break;
270 }
271
Chris Lattnerdc750592005-01-07 07:47:09 +0000272 case ISD::ADJCALLSTACKDOWN:
273 case ISD::ADJCALLSTACKUP:
274 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
275 // There is no need to legalize the size argument (Operand #1)
276 if (Tmp1 != Node->getOperand(0))
277 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
278 Node->getOperand(1));
279 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000280 case ISD::DYNAMIC_STACKALLOC:
281 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
282 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
283 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
284 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
285 Tmp3 != Node->getOperand(2))
286 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
287 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000288 else
289 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000290
291 // Since this op produces two values, make sure to remember that we
292 // legalized both of them.
293 AddLegalizedOperand(SDOperand(Node, 0), Result);
294 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
295 return Result.getValue(Op.ResNo);
296
Chris Lattnerdc750592005-01-07 07:47:09 +0000297 case ISD::CALL:
298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
299 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfa854eb2005-01-07 21:35:32 +0000300 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000301 std::vector<MVT::ValueType> RetTyVTs;
302 RetTyVTs.reserve(Node->getNumValues());
303 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000304 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner9242c502005-01-09 19:43:23 +0000305 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0);
306 } else {
307 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000308 }
Chris Lattner9242c502005-01-09 19:43:23 +0000309 // Since calls produce multiple values, make sure to remember that we
310 // legalized all of them.
311 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
312 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
313 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000314
Chris Lattner68a12142005-01-07 22:12:08 +0000315 case ISD::BR:
316 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
317 if (Tmp1 != Node->getOperand(0))
318 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
319 break;
320
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000321 case ISD::BRCOND:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // FIXME: booleans might not be legal!
324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
325 // Basic block destination (Op#2) is always legal.
326 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
327 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
328 Node->getOperand(2));
329 break;
330
Chris Lattnerdc750592005-01-07 07:47:09 +0000331 case ISD::LOAD:
332 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
333 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
334 if (Tmp1 != Node->getOperand(0) ||
335 Tmp2 != Node->getOperand(1))
336 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000337 else
338 Result = SDOperand(Node, 0);
339
340 // Since loads produce two values, make sure to remember that we legalized
341 // both of them.
342 AddLegalizedOperand(SDOperand(Node, 0), Result);
343 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
344 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000345
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000346 case ISD::EXTLOAD:
347 case ISD::SEXTLOAD:
348 case ISD::ZEXTLOAD:
349 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
350 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
351 if (Tmp1 != Node->getOperand(0) ||
352 Tmp2 != Node->getOperand(1))
353 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
354 cast<MVTSDNode>(Node)->getExtraValueType());
355 else
356 Result = SDOperand(Node, 0);
357
358 // Since loads produce two values, make sure to remember that we legalized
359 // both of them.
360 AddLegalizedOperand(SDOperand(Node, 0), Result);
361 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
362 return Result.getValue(Op.ResNo);
363
Chris Lattnerdc750592005-01-07 07:47:09 +0000364 case ISD::EXTRACT_ELEMENT:
365 // Get both the low and high parts.
366 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
367 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
368 Result = Tmp2; // 1 -> Hi
369 else
370 Result = Tmp1; // 0 -> Lo
371 break;
372
373 case ISD::CopyToReg:
374 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
375
376 switch (getTypeAction(Node->getOperand(1).getValueType())) {
377 case Legal:
378 // Legalize the incoming value (must be legal).
379 Tmp2 = LegalizeOp(Node->getOperand(1));
380 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000381 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000382 break;
383 case Expand: {
384 SDOperand Lo, Hi;
385 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000386 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +0000387 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
388 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
389 assert(isTypeLegal(Result.getValueType()) &&
390 "Cannot expand multiple times yet (i64 -> i16)");
391 break;
392 }
393 case Promote:
Chris Lattner73b69772005-01-16 02:23:34 +0000394 assert(0 && "CopyToReg should not require promotion!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000395 abort();
396 }
397 break;
398
399 case ISD::RET:
400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
401 switch (Node->getNumOperands()) {
402 case 2: // ret val
403 switch (getTypeAction(Node->getOperand(1).getValueType())) {
404 case Legal:
405 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000406 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000407 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
408 break;
409 case Expand: {
410 SDOperand Lo, Hi;
411 ExpandOp(Node->getOperand(1), Lo, Hi);
412 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
413 break;
414 }
415 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000416 Tmp2 = PromoteOp(Node->getOperand(1));
417 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
418 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000419 }
420 break;
421 case 1: // ret void
422 if (Tmp1 != Node->getOperand(0))
423 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
424 break;
425 default: { // ret <values>
426 std::vector<SDOperand> NewValues;
427 NewValues.push_back(Tmp1);
428 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
429 switch (getTypeAction(Node->getOperand(i).getValueType())) {
430 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000431 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 break;
433 case Expand: {
434 SDOperand Lo, Hi;
435 ExpandOp(Node->getOperand(i), Lo, Hi);
436 NewValues.push_back(Lo);
437 NewValues.push_back(Hi);
438 break;
439 }
440 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000441 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000442 }
443 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
444 break;
445 }
446 }
447 break;
448 case ISD::STORE:
449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
450 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
451
Chris Lattnere69daaf2005-01-08 06:25:56 +0000452 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000453 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000454 if (CFP->getValueType(0) == MVT::f32) {
455 union {
456 unsigned I;
457 float F;
458 } V;
459 V.F = CFP->getValue();
460 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
461 DAG.getConstant(V.I, MVT::i32), Tmp2);
462 } else {
463 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
464 union {
465 uint64_t I;
466 double F;
467 } V;
468 V.F = CFP->getValue();
469 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
470 DAG.getConstant(V.I, MVT::i64), Tmp2);
471 }
472 Op = Result;
473 Node = Op.Val;
474 }
475
Chris Lattnerdc750592005-01-07 07:47:09 +0000476 switch (getTypeAction(Node->getOperand(1).getValueType())) {
477 case Legal: {
478 SDOperand Val = LegalizeOp(Node->getOperand(1));
479 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
480 Tmp2 != Node->getOperand(2))
481 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
482 break;
483 }
484 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000485 // Truncate the value and store the result.
486 Tmp3 = PromoteOp(Node->getOperand(1));
487 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
488 Node->getOperand(1).getValueType());
489 break;
490
Chris Lattnerdc750592005-01-07 07:47:09 +0000491 case Expand:
492 SDOperand Lo, Hi;
493 ExpandOp(Node->getOperand(1), Lo, Hi);
494
495 if (!TLI.isLittleEndian())
496 std::swap(Lo, Hi);
497
498 // FIXME: These two stores are independent of each other!
499 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
500
Chris Lattner9242c502005-01-09 19:43:23 +0000501 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000502 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
503 getIntPtrConstant(IncrementSize));
504 assert(isTypeLegal(Tmp2.getValueType()) &&
505 "Pointers must be legal!");
506 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
507 }
508 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000509 case ISD::TRUNCSTORE:
510 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
511 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
512
513 switch (getTypeAction(Node->getOperand(1).getValueType())) {
514 case Legal:
515 Tmp2 = LegalizeOp(Node->getOperand(1));
516 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
517 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000518 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000519 cast<MVTSDNode>(Node)->getExtraValueType());
520 break;
521 case Promote:
522 case Expand:
523 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
524 }
525 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000526 case ISD::SELECT:
Chris Lattnerdc750592005-01-07 07:47:09 +0000527 // FIXME: BOOLS MAY REQUIRE PROMOTION!
528 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
529 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000530 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000531
532 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
533 default: assert(0 && "This action is not supported yet!");
534 case TargetLowering::Legal:
535 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
536 Tmp3 != Node->getOperand(2))
537 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
538 Tmp1, Tmp2, Tmp3);
539 break;
540 case TargetLowering::Promote: {
541 MVT::ValueType NVT =
542 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
543 unsigned ExtOp, TruncOp;
544 if (MVT::isInteger(Tmp2.getValueType())) {
545 ExtOp = ISD::ZERO_EXTEND;
546 TruncOp = ISD::TRUNCATE;
547 } else {
548 ExtOp = ISD::FP_EXTEND;
549 TruncOp = ISD::FP_ROUND;
550 }
551 // Promote each of the values to the new type.
552 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
553 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
554 // Perform the larger operation, then round down.
555 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
556 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
557 break;
558 }
559 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000560 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000561 case ISD::SETCC:
562 switch (getTypeAction(Node->getOperand(0).getValueType())) {
563 case Legal:
564 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
565 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
566 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
567 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000568 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000569 break;
570 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000571 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
572 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
573
574 // If this is an FP compare, the operands have already been extended.
575 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
576 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000577 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000578
579 // Otherwise, we have to insert explicit sign or zero extends. Note
580 // that we could insert sign extends for ALL conditions, but zero extend
581 // is cheaper on many machines (an AND instead of two shifts), so prefer
582 // it.
583 switch (cast<SetCCSDNode>(Node)->getCondition()) {
584 default: assert(0 && "Unknown integer comparison!");
585 case ISD::SETEQ:
586 case ISD::SETNE:
587 case ISD::SETUGE:
588 case ISD::SETUGT:
589 case ISD::SETULE:
590 case ISD::SETULT:
591 // ALL of these operations will work if we either sign or zero extend
592 // the operands (including the unsigned comparisons!). Zero extend is
593 // usually a simpler/cheaper operation, so prefer it.
594 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
595 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
596 break;
597 case ISD::SETGE:
598 case ISD::SETGT:
599 case ISD::SETLT:
600 case ISD::SETLE:
601 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
602 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
603 break;
604 }
605
606 }
607 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000608 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000609 break;
610 case Expand:
611 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
612 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
613 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
614 switch (cast<SetCCSDNode>(Node)->getCondition()) {
615 case ISD::SETEQ:
616 case ISD::SETNE:
617 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
618 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
619 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000620 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
621 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000622 DAG.getConstant(0, Tmp1.getValueType()));
623 break;
624 default:
625 // FIXME: This generated code sucks.
626 ISD::CondCode LowCC;
627 switch (cast<SetCCSDNode>(Node)->getCondition()) {
628 default: assert(0 && "Unknown integer setcc!");
629 case ISD::SETLT:
630 case ISD::SETULT: LowCC = ISD::SETULT; break;
631 case ISD::SETGT:
632 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
633 case ISD::SETLE:
634 case ISD::SETULE: LowCC = ISD::SETULE; break;
635 case ISD::SETGE:
636 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
637 }
638
639 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
640 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
641 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
642
643 // NOTE: on targets without efficient SELECT of bools, we can always use
644 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000645 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000646 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000647 Node->getValueType(0), LHSHi, RHSHi);
648 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
649 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
650 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000651 break;
652 }
653 }
654 break;
655
Chris Lattner85d70c62005-01-11 05:57:22 +0000656 case ISD::MEMSET:
657 case ISD::MEMCPY:
658 case ISD::MEMMOVE: {
659 Tmp1 = LegalizeOp(Node->getOperand(0));
660 Tmp2 = LegalizeOp(Node->getOperand(1));
661 Tmp3 = LegalizeOp(Node->getOperand(2));
662 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
663 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
Chris Lattner3c0dd462005-01-16 07:29:19 +0000664
665 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
666 default: assert(0 && "This action not implemented for this operation!");
667 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000668 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
669 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
670 Tmp5 != Node->getOperand(4)) {
671 std::vector<SDOperand> Ops;
672 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
673 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
674 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
675 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000676 break;
677 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000678 // Otherwise, the target does not support this operation. Lower the
679 // operation to an explicit libcall as appropriate.
680 MVT::ValueType IntPtr = TLI.getPointerTy();
681 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
682 std::vector<std::pair<SDOperand, const Type*> > Args;
683
Reid Spencer6dced922005-01-12 14:53:45 +0000684 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000685 if (Node->getOpcode() == ISD::MEMSET) {
686 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
687 // Extend the ubyte argument to be an int value for the call.
688 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
689 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
690 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
691
692 FnName = "memset";
693 } else if (Node->getOpcode() == ISD::MEMCPY ||
694 Node->getOpcode() == ISD::MEMMOVE) {
695 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
696 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
697 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
698 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
699 } else {
700 assert(0 && "Unknown op!");
701 }
702 std::pair<SDOperand,SDOperand> CallResult =
703 TLI.LowerCallTo(Tmp1, Type::VoidTy,
704 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
705 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000706 break;
707 }
708 case TargetLowering::Custom:
709 std::vector<SDOperand> Ops;
710 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
711 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
712 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
713 Result = TLI.LowerOperation(Result);
714 Result = LegalizeOp(Result);
715 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000716 }
717 break;
718 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000719 case ISD::ADD:
720 case ISD::SUB:
721 case ISD::MUL:
722 case ISD::UDIV:
723 case ISD::SDIV:
724 case ISD::UREM:
725 case ISD::SREM:
726 case ISD::AND:
727 case ISD::OR:
728 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000729 case ISD::SHL:
730 case ISD::SRL:
731 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000732 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
733 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
734 if (Tmp1 != Node->getOperand(0) ||
735 Tmp2 != Node->getOperand(1))
736 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
737 break;
738 case ISD::ZERO_EXTEND:
739 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000740 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000741 case ISD::FP_EXTEND:
742 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000743 case ISD::FP_TO_SINT:
744 case ISD::FP_TO_UINT:
745 case ISD::SINT_TO_FP:
746 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000747 switch (getTypeAction(Node->getOperand(0).getValueType())) {
748 case Legal:
749 Tmp1 = LegalizeOp(Node->getOperand(0));
750 if (Tmp1 != Node->getOperand(0))
751 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
752 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000753 case Expand:
Chris Lattner05b4e372005-01-13 17:59:25 +0000754 assert(Node->getOpcode() != ISD::SINT_TO_FP &&
755 Node->getOpcode() != ISD::UINT_TO_FP &&
756 "Cannot lower Xint_to_fp to a call yet!");
757
Chris Lattnera65a2f02005-01-07 22:37:48 +0000758 // In the expand case, we must be dealing with a truncate, because
759 // otherwise the result would be larger than the source.
760 assert(Node->getOpcode() == ISD::TRUNCATE &&
761 "Shouldn't need to expand other operators here!");
762 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
763
764 // Since the result is legal, we should just be able to truncate the low
765 // part of the source.
766 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
767 break;
768
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000769 case Promote:
770 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000771 case ISD::ZERO_EXTEND:
772 Result = PromoteOp(Node->getOperand(0));
773 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
774 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000775 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000776 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000777 Result = PromoteOp(Node->getOperand(0));
778 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
779 Result, Node->getOperand(0).getValueType());
780 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000781 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000782 Result = PromoteOp(Node->getOperand(0));
783 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
784 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000785 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000786 Result = PromoteOp(Node->getOperand(0));
787 if (Result.getValueType() != Op.getValueType())
788 // Dynamically dead while we have only 2 FP types.
789 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
790 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000791 case ISD::FP_ROUND:
792 case ISD::FP_TO_SINT:
793 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000794 Result = PromoteOp(Node->getOperand(0));
795 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
796 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000797 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000798 Result = PromoteOp(Node->getOperand(0));
799 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
800 Result, Node->getOperand(0).getValueType());
801 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
802 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000803 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000804 Result = PromoteOp(Node->getOperand(0));
805 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
806 Result, Node->getOperand(0).getValueType());
807 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
808 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000809 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000810 }
811 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000812 case ISD::FP_ROUND_INREG:
813 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000814 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000815 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000816 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
817
818 // If this operation is not supported, convert it to a shl/shr or load/store
819 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000820 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
821 default: assert(0 && "This action not supported for this op yet!");
822 case TargetLowering::Legal:
823 if (Tmp1 != Node->getOperand(0))
824 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
825 ExtraVT);
826 break;
827 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000828 // If this is an integer extend and shifts are supported, do that.
829 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
830 // NOTE: we could fall back on load/store here too for targets without
831 // AND. However, it is doubtful that any exist.
832 // AND out the appropriate bits.
833 SDOperand Mask =
834 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
835 Node->getValueType(0));
836 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
837 Node->getOperand(0), Mask);
838 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
839 // NOTE: we could fall back on load/store here too for targets without
840 // SAR. However, it is doubtful that any exist.
841 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
842 MVT::getSizeInBits(ExtraVT);
843 SDOperand ShiftCst = DAG.getConstant(BitsDiff, MVT::i8);
844 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
845 Node->getOperand(0), ShiftCst);
846 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
847 Result, ShiftCst);
848 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
849 // The only way we can lower this is to turn it into a STORETRUNC,
850 // EXTLOAD pair, targetting a temporary location (a stack slot).
851
852 // NOTE: there is a choice here between constantly creating new stack
853 // slots and always reusing the same one. We currently always create
854 // new ones, as reuse may inhibit scheduling.
855 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
856 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
857 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
858 MachineFunction &MF = DAG.getMachineFunction();
859 int SSFI =
860 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
861 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
862 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
863 Node->getOperand(0), StackSlot, ExtraVT);
864 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
865 Result, StackSlot, ExtraVT);
866 } else {
867 assert(0 && "Unknown op");
868 }
869 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000870 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000871 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000872 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000873 }
Chris Lattner99222f72005-01-15 07:15:18 +0000874 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000875
Chris Lattnerea4ca942005-01-07 22:28:47 +0000876 if (!Op.Val->hasOneUse())
877 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000878
879 return Result;
880}
881
Chris Lattner4d978642005-01-15 22:16:26 +0000882/// PromoteOp - Given an operation that produces a value in an invalid type,
883/// promote it to compute the value into a larger type. The produced value will
884/// have the correct bits for the low portion of the register, but no guarantee
885/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000886SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
887 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000888 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000889 assert(getTypeAction(VT) == Promote &&
890 "Caller should expand or legalize operands that are not promotable!");
891 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
892 "Cannot promote to smaller type!");
893
894 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
895 if (I != PromotedNodes.end()) return I->second;
896
897 SDOperand Tmp1, Tmp2, Tmp3;
898
899 SDOperand Result;
900 SDNode *Node = Op.Val;
901
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000902 // Promotion needs an optimization step to clean up after it, and is not
903 // careful to avoid operations the target does not support. Make sure that
904 // all generated operations are legalized in the next iteration.
905 NeedsAnotherIteration = true;
906
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000907 switch (Node->getOpcode()) {
908 default:
909 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
910 assert(0 && "Do not know how to promote this operator!");
911 abort();
912 case ISD::Constant:
913 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
914 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
915 break;
916 case ISD::ConstantFP:
917 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
918 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
919 break;
920
921 case ISD::TRUNCATE:
922 switch (getTypeAction(Node->getOperand(0).getValueType())) {
923 case Legal:
924 Result = LegalizeOp(Node->getOperand(0));
925 assert(Result.getValueType() >= NVT &&
926 "This truncation doesn't make sense!");
927 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
928 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
929 break;
930 case Expand:
931 assert(0 && "Cannot handle expand yet");
932 case Promote:
933 assert(0 && "Cannot handle promote-promote yet");
934 }
935 break;
Chris Lattner4d978642005-01-15 22:16:26 +0000936 case ISD::SIGN_EXTEND:
937 case ISD::ZERO_EXTEND:
938 switch (getTypeAction(Node->getOperand(0).getValueType())) {
939 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
940 case Legal:
941 // Input is legal? Just do extend all the way to the larger type.
942 Result = LegalizeOp(Node->getOperand(0));
943 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
944 break;
945 case Promote:
946 // Promote the reg if it's smaller.
947 Result = PromoteOp(Node->getOperand(0));
948 // The high bits are not guaranteed to be anything. Insert an extend.
949 if (Node->getOpcode() == ISD::SIGN_EXTEND)
950 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
951 else
952 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
953 break;
954 }
955 break;
956
957 case ISD::FP_EXTEND:
958 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
959 case ISD::FP_ROUND:
960 switch (getTypeAction(Node->getOperand(0).getValueType())) {
961 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
962 case Promote: assert(0 && "Unreachable with 2 FP types!");
963 case Legal:
964 // Input is legal? Do an FP_ROUND_INREG.
965 Result = LegalizeOp(Node->getOperand(0));
966 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
967 break;
968 }
969 break;
970
971 case ISD::SINT_TO_FP:
972 case ISD::UINT_TO_FP:
973 switch (getTypeAction(Node->getOperand(0).getValueType())) {
974 case Legal:
975 Result = LegalizeOp(Node->getOperand(0));
976 break;
977
978 case Promote:
979 Result = PromoteOp(Node->getOperand(0));
980 if (Node->getOpcode() == ISD::SINT_TO_FP)
981 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
982 Result, Node->getOperand(0).getValueType());
983 else
984 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
985 Result, Node->getOperand(0).getValueType());
986 break;
987 case Expand:
988 assert(0 && "Unimplemented");
989 }
990 // No extra round required here.
991 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
992 break;
993
994 case ISD::FP_TO_SINT:
995 case ISD::FP_TO_UINT:
996 switch (getTypeAction(Node->getOperand(0).getValueType())) {
997 case Legal:
998 Tmp1 = LegalizeOp(Node->getOperand(0));
999 break;
1000 case Promote:
1001 // The input result is prerounded, so we don't have to do anything
1002 // special.
1003 Tmp1 = PromoteOp(Node->getOperand(0));
1004 break;
1005 case Expand:
1006 assert(0 && "not implemented");
1007 }
1008 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1009 break;
1010
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001011 case ISD::AND:
1012 case ISD::OR:
1013 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001014 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001015 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001016 case ISD::MUL:
1017 // The input may have strange things in the top bits of the registers, but
1018 // these operations don't care. They may have wierd bits going out, but
1019 // that too is okay if they are integer operations.
1020 Tmp1 = PromoteOp(Node->getOperand(0));
1021 Tmp2 = PromoteOp(Node->getOperand(1));
1022 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1023 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1024
1025 // However, if this is a floating point operation, they will give excess
1026 // precision that we may not be able to tolerate. If we DO allow excess
1027 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001028 // FIXME: Why would we need to round FP ops more than integer ones?
1029 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001030 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1031 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1032 break;
1033
Chris Lattner4d978642005-01-15 22:16:26 +00001034 case ISD::SDIV:
1035 case ISD::SREM:
1036 // These operators require that their input be sign extended.
1037 Tmp1 = PromoteOp(Node->getOperand(0));
1038 Tmp2 = PromoteOp(Node->getOperand(1));
1039 if (MVT::isInteger(NVT)) {
1040 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001041 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001042 }
1043 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1044
1045 // Perform FP_ROUND: this is probably overly pessimistic.
1046 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1047 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1048 break;
1049
1050 case ISD::UDIV:
1051 case ISD::UREM:
1052 // These operators require that their input be zero extended.
1053 Tmp1 = PromoteOp(Node->getOperand(0));
1054 Tmp2 = PromoteOp(Node->getOperand(1));
1055 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1056 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001057 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001058 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1059 break;
1060
1061 case ISD::SHL:
1062 Tmp1 = PromoteOp(Node->getOperand(0));
1063 Tmp2 = LegalizeOp(Node->getOperand(1));
1064 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1065 break;
1066 case ISD::SRA:
1067 // The input value must be properly sign extended.
1068 Tmp1 = PromoteOp(Node->getOperand(0));
1069 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1070 Tmp2 = LegalizeOp(Node->getOperand(1));
1071 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1072 break;
1073 case ISD::SRL:
1074 // The input value must be properly zero extended.
1075 Tmp1 = PromoteOp(Node->getOperand(0));
1076 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1077 Tmp2 = LegalizeOp(Node->getOperand(1));
1078 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1079 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001080 case ISD::LOAD:
1081 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1082 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1083 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1084
1085 // Remember that we legalized the chain.
1086 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1087 break;
1088 case ISD::SELECT:
Chris Lattner4d978642005-01-15 22:16:26 +00001089 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001090 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1091 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1092 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1093 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001094 case ISD::CALL: {
1095 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1096 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1097
1098 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1099 "Can only promote single result calls");
1100 std::vector<MVT::ValueType> RetTyVTs;
1101 RetTyVTs.reserve(2);
1102 RetTyVTs.push_back(NVT);
1103 RetTyVTs.push_back(MVT::Other);
1104 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2);
1105 Result = SDOperand(NC, 0);
1106
1107 // Insert the new chain mapping.
1108 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1109 break;
1110 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001111 }
1112
1113 assert(Result.Val && "Didn't set a result!");
1114 AddPromotedOperand(Op, Result);
1115 return Result;
1116}
Chris Lattnerdc750592005-01-07 07:47:09 +00001117
1118/// ExpandOp - Expand the specified SDOperand into its two component pieces
1119/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1120/// LegalizeNodes map is filled in for any results that are not expanded, the
1121/// ExpandedNodes map is filled in for any results that are expanded, and the
1122/// Lo/Hi values are returned.
1123void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1124 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001125 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001126 SDNode *Node = Op.Val;
1127 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1128 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1129 assert(MVT::isInteger(NVT) && NVT < VT &&
1130 "Cannot expand to FP value or to larger int value!");
1131
1132 // If there is more than one use of this, see if we already expanded it.
1133 // There is no use remembering values that only have a single use, as the map
1134 // entries will never be reused.
1135 if (!Node->hasOneUse()) {
1136 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1137 = ExpandedNodes.find(Op);
1138 if (I != ExpandedNodes.end()) {
1139 Lo = I->second.first;
1140 Hi = I->second.second;
1141 return;
1142 }
1143 }
1144
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001145 // Expanding to multiple registers needs to perform an optimization step, and
1146 // is not careful to avoid operations the target does not support. Make sure
1147 // that all generated operations are legalized in the next iteration.
1148 NeedsAnotherIteration = true;
1149 const char *LibCallName = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001150
Chris Lattnerdc750592005-01-07 07:47:09 +00001151 switch (Node->getOpcode()) {
1152 default:
1153 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1154 assert(0 && "Do not know how to expand this operator!");
1155 abort();
1156 case ISD::Constant: {
1157 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1158 Lo = DAG.getConstant(Cst, NVT);
1159 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1160 break;
1161 }
1162
1163 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001164 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001165 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001166 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1167 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1168
1169 // Remember that we legalized the chain.
1170 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1171
Chris Lattnerdc750592005-01-07 07:47:09 +00001172 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1173 break;
1174 }
1175
1176 case ISD::LOAD: {
1177 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1178 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1179 Lo = DAG.getLoad(NVT, Ch, Ptr);
1180
1181 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001182 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001183 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1184 getIntPtrConstant(IncrementSize));
1185 // FIXME: This load is independent of the first one.
1186 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
1187
1188 // Remember that we legalized the chain.
Chris Lattnerea4ca942005-01-07 22:28:47 +00001189 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001190 if (!TLI.isLittleEndian())
1191 std::swap(Lo, Hi);
1192 break;
1193 }
1194 case ISD::CALL: {
1195 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1196 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1197
1198 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1199 "Can only expand a call once so far, not i64 -> i16!");
1200
1201 std::vector<MVT::ValueType> RetTyVTs;
1202 RetTyVTs.reserve(3);
1203 RetTyVTs.push_back(NVT);
1204 RetTyVTs.push_back(NVT);
1205 RetTyVTs.push_back(MVT::Other);
1206 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
1207 Lo = SDOperand(NC, 0);
1208 Hi = SDOperand(NC, 1);
1209
1210 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001211 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001212 break;
1213 }
1214 case ISD::AND:
1215 case ISD::OR:
1216 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1217 SDOperand LL, LH, RL, RH;
1218 ExpandOp(Node->getOperand(0), LL, LH);
1219 ExpandOp(Node->getOperand(1), RL, RH);
1220 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1221 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1222 break;
1223 }
1224 case ISD::SELECT: {
1225 SDOperand C, LL, LH, RL, RH;
1226 // FIXME: BOOLS MAY REQUIRE PROMOTION!
1227 C = LegalizeOp(Node->getOperand(0));
1228 ExpandOp(Node->getOperand(1), LL, LH);
1229 ExpandOp(Node->getOperand(2), RL, RH);
1230 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1231 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1232 break;
1233 }
1234 case ISD::SIGN_EXTEND: {
1235 // The low part is just a sign extension of the input (which degenerates to
1236 // a copy).
1237 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1238
1239 // The high part is obtained by SRA'ing all but one of the bits of the lo
1240 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001241 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
1242 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8));
Chris Lattnerdc750592005-01-07 07:47:09 +00001243 break;
1244 }
1245 case ISD::ZERO_EXTEND:
1246 // The low part is just a zero extension of the input (which degenerates to
1247 // a copy).
1248 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1249
1250 // The high part is just a zero.
1251 Hi = DAG.getConstant(0, NVT);
1252 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001253
1254 // These operators cannot be expanded directly, emit them as calls to
1255 // library functions.
1256 case ISD::FP_TO_SINT:
1257 if (Node->getOperand(0).getValueType() == MVT::f32)
1258 LibCallName = "__fixsfdi";
1259 else
1260 LibCallName = "__fixdfdi";
1261 break;
1262 case ISD::FP_TO_UINT:
1263 if (Node->getOperand(0).getValueType() == MVT::f32)
1264 LibCallName = "__fixunssfdi";
1265 else
1266 LibCallName = "__fixunsdfdi";
1267 break;
1268
1269 case ISD::ADD: LibCallName = "__adddi3"; break;
1270 case ISD::SUB: LibCallName = "__subdi3"; break;
1271 case ISD::MUL: LibCallName = "__muldi3"; break;
1272 case ISD::SDIV: LibCallName = "__divdi3"; break;
1273 case ISD::UDIV: LibCallName = "__udivdi3"; break;
1274 case ISD::SREM: LibCallName = "__moddi3"; break;
1275 case ISD::UREM: LibCallName = "__umoddi3"; break;
Chris Lattnerbe02d432005-01-10 21:02:37 +00001276 case ISD::SHL: LibCallName = "__ashldi3"; break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001277 case ISD::SRA: LibCallName = "__ashrdi3"; break;
Chris Lattnerbe02d432005-01-10 21:02:37 +00001278 case ISD::SRL: LibCallName = "__lshrdi3"; break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001279 }
1280
1281 // Int2FP -> __floatdisf/__floatdidf
1282
1283 // If this is to be expanded into a libcall... do so now.
1284 if (LibCallName) {
1285 TargetLowering::ArgListTy Args;
1286 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1287 Args.push_back(std::make_pair(Node->getOperand(i),
Chris Lattner99222f72005-01-15 07:15:18 +00001288 MVT::getTypeForValueType(Node->getOperand(i).getValueType())));
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001289 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
1290
1291 // We don't care about token chains for libcalls. We just use the entry
1292 // node as our input and ignore the output chain. This allows us to place
1293 // calls wherever we need them to satisfy data dependences.
1294 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
Chris Lattner99222f72005-01-15 07:15:18 +00001295 MVT::getTypeForValueType(Op.getValueType()), Callee,
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001296 Args, DAG).first;
1297 ExpandOp(Result, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +00001298 }
1299
1300 // Remember in a map if the values will be reused later.
1301 if (!Node->hasOneUse()) {
1302 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1303 std::make_pair(Lo, Hi))).second;
1304 assert(isNew && "Value already expanded?!?");
1305 }
1306}
1307
1308
1309// SelectionDAG::Legalize - This is the entry point for the file.
1310//
1311void SelectionDAG::Legalize(TargetLowering &TLI) {
1312 /// run - This is the main entry point to this class.
1313 ///
1314 SelectionDAGLegalize(TLI, *this).Run();
1315}
1316