blob: bcbaae825ab7483c442a361e9f1152a969e939bf [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
50 /// TransformToType - For any value types we are promoting or expanding, this
51 /// contains the value type that we are changing to. For Expanded types, this
52 /// contains one step of the expand (e.g. i64 -> i32), even if there are
53 /// multiple steps required (e.g. i64 -> i16)
54 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
55
56 /// ValueTypeActions - This is a bitvector that contains two bits for each
57 /// value type, where the two bits correspond to the LegalizeAction enum.
58 /// This can be queried with "getTypeAction(VT)".
59 unsigned ValueTypeActions;
60
61 /// NeedsAnotherIteration - This is set when we expand a large integer
62 /// operation into smaller integer operations, but the smaller operations are
63 /// not set. This occurs only rarely in practice, for targets that don't have
64 /// 32-bit or larger integer registers.
65 bool NeedsAnotherIteration;
66
67 /// LegalizedNodes - For nodes that are of legal width, and that have more
68 /// than one use, this map indicates what regularized operand to use. This
69 /// allows us to avoid legalizing the same thing more than once.
70 std::map<SDOperand, SDOperand> LegalizedNodes;
71
Chris Lattner1f2c9d82005-01-15 05:21:40 +000072 /// PromotedNodes - For nodes that are below legal width, and that have more
73 /// than one use, this map indicates what promoted value to use. This allows
74 /// us to avoid promoting the same thing more than once.
75 std::map<SDOperand, SDOperand> PromotedNodes;
76
Chris Lattnerdc750592005-01-07 07:47:09 +000077 /// ExpandedNodes - For nodes that need to be expanded, and which have more
78 /// than one use, this map indicates which which operands are the expanded
79 /// version of the input. This allows us to avoid expanding the same node
80 /// more than once.
81 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
82
Chris Lattnerea4ca942005-01-07 22:28:47 +000083 void AddLegalizedOperand(SDOperand From, SDOperand To) {
84 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000087 void AddPromotedOperand(SDOperand From, SDOperand To) {
88 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
89 assert(isNew && "Got into the map somehow?");
90 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000091
Chris Lattnerdc750592005-01-07 07:47:09 +000092 /// setValueTypeAction - Set the action for a particular value type. This
93 /// assumes an action has not already been set for this value type.
94 void setValueTypeAction(MVT::ValueType VT, LegalizeAction A) {
95 ValueTypeActions |= A << (VT*2);
96 if (A == Promote) {
97 MVT::ValueType PromoteTo;
98 if (VT == MVT::f32)
99 PromoteTo = MVT::f64;
100 else {
101 unsigned LargerReg = VT+1;
102 while (!TLI.hasNativeSupportFor((MVT::ValueType)LargerReg)) {
103 ++LargerReg;
104 assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
105 "Nothing to promote to??");
106 }
107 PromoteTo = (MVT::ValueType)LargerReg;
108 }
109
110 assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
111 MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
112 "Can only promote from int->int or fp->fp!");
113 assert(VT < PromoteTo && "Must promote to a larger type!");
114 TransformToType[VT] = PromoteTo;
115 } else if (A == Expand) {
116 assert(MVT::isInteger(VT) && VT > MVT::i8 &&
117 "Cannot expand this type: target must support SOME integer reg!");
118 // Expand to the next smaller integer type!
119 TransformToType[VT] = (MVT::ValueType)(VT-1);
120 }
121 }
122
123public:
124
125 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
126
127 /// Run - While there is still lowering to do, perform a pass over the DAG.
128 /// Most regularization can be done in a single pass, but targets that require
129 /// large values to be split into registers multiple times (e.g. i64 -> 4x
130 /// i16) require iteration for these values (the first iteration will demote
131 /// to i32, the second will demote to i16).
132 void Run() {
133 do {
134 NeedsAnotherIteration = false;
135 LegalizeDAG();
136 } while (NeedsAnotherIteration);
137 }
138
139 /// getTypeAction - Return how we should legalize values of this type, either
140 /// it is already legal or we need to expand it into multiple registers of
141 /// smaller integer type, or we need to promote it to a larger type.
142 LegalizeAction getTypeAction(MVT::ValueType VT) const {
143 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
144 }
145
146 /// isTypeLegal - Return true if this type is legal on this target.
147 ///
148 bool isTypeLegal(MVT::ValueType VT) const {
149 return getTypeAction(VT) == Legal;
150 }
151
152private:
153 void LegalizeDAG();
154
155 SDOperand LegalizeOp(SDOperand O);
156 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000157 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000158
159 SDOperand getIntPtrConstant(uint64_t Val) {
160 return DAG.getConstant(Val, TLI.getPointerTy());
161 }
162};
163}
164
165
166SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
167 SelectionDAG &dag)
168 : TLI(tli), DAG(dag), ValueTypeActions(0) {
169
170 assert(MVT::LAST_VALUETYPE <= 16 &&
171 "Too many value types for ValueTypeActions to hold!");
172
173 // Inspect all of the ValueType's possible, deciding how to process them.
174 for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
175 // If TLI says we are expanding this type, expand it!
176 if (TLI.getNumElements((MVT::ValueType)IntReg) != 1)
177 setValueTypeAction((MVT::ValueType)IntReg, Expand);
178 else if (!TLI.hasNativeSupportFor((MVT::ValueType)IntReg))
179 // Otherwise, if we don't have native support, we must promote to a
180 // larger type.
181 setValueTypeAction((MVT::ValueType)IntReg, Promote);
182
183 // If the target does not have native support for F32, promote it to F64.
184 if (!TLI.hasNativeSupportFor(MVT::f32))
185 setValueTypeAction(MVT::f32, Promote);
186}
187
Chris Lattnerdc750592005-01-07 07:47:09 +0000188void SelectionDAGLegalize::LegalizeDAG() {
189 SDOperand OldRoot = DAG.getRoot();
190 SDOperand NewRoot = LegalizeOp(OldRoot);
191 DAG.setRoot(NewRoot);
192
193 ExpandedNodes.clear();
194 LegalizedNodes.clear();
195
196 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000197 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000198}
199
200SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000201 assert(getTypeAction(Op.getValueType()) == Legal &&
202 "Caller should expand or promote operands that are not legal!");
203
Chris Lattnerdc750592005-01-07 07:47:09 +0000204 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000205 // register on this target, make sure to expand or promote them.
206 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000207 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
208 switch (getTypeAction(Op.Val->getValueType(i))) {
209 case Legal: break; // Nothing to do.
210 case Expand: {
211 SDOperand T1, T2;
212 ExpandOp(Op.getValue(i), T1, T2);
213 assert(LegalizedNodes.count(Op) &&
214 "Expansion didn't add legal operands!");
215 return LegalizedNodes[Op];
216 }
217 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000218 PromoteOp(Op.getValue(i));
219 assert(LegalizedNodes.count(Op) &&
220 "Expansion didn't add legal operands!");
221 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000222 }
223 }
224
Chris Lattner85d70c62005-01-11 05:57:22 +0000225 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
226 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000227
Chris Lattnerec26b482005-01-09 19:03:49 +0000228 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000229
230 SDOperand Result = Op;
231 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000232
233 switch (Node->getOpcode()) {
234 default:
235 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
236 assert(0 && "Do not know how to legalize this operator!");
237 abort();
238 case ISD::EntryToken:
239 case ISD::FrameIndex:
240 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000241 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000242 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000243 assert(getTypeAction(Node->getValueType(0)) == Legal &&
244 "This must be legal!");
245 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000246 case ISD::CopyFromReg:
247 Tmp1 = LegalizeOp(Node->getOperand(0));
248 if (Tmp1 != Node->getOperand(0))
249 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
250 Node->getValueType(0), Tmp1);
251 break;
Chris Lattnere727af02005-01-13 20:50:02 +0000252 case ISD::ImplicitDef:
253 Tmp1 = LegalizeOp(Node->getOperand(0));
254 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000255 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000256 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000257 case ISD::Constant:
258 // We know we don't need to expand constants here, constants only have one
259 // value and we check that it is fine above.
260
261 // FIXME: Maybe we should handle things like targets that don't support full
262 // 32-bit immediates?
263 break;
264 case ISD::ConstantFP: {
265 // Spill FP immediates to the constant pool if the target cannot directly
266 // codegen them. Targets often have some immediate values that can be
267 // efficiently generated into an FP register without a load. We explicitly
268 // leave these constants as ConstantFP nodes for the target to deal with.
269
270 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
271
272 // Check to see if this FP immediate is already legal.
273 bool isLegal = false;
274 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
275 E = TLI.legal_fpimm_end(); I != E; ++I)
276 if (CFP->isExactlyValue(*I)) {
277 isLegal = true;
278 break;
279 }
280
281 if (!isLegal) {
282 // Otherwise we need to spill the constant to memory.
283 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
284
285 bool Extend = false;
286
287 // If a FP immediate is precise when represented as a float, we put it
288 // into the constant pool as a float, even if it's is statically typed
289 // as a double.
290 MVT::ValueType VT = CFP->getValueType(0);
291 bool isDouble = VT == MVT::f64;
292 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
293 Type::FloatTy, CFP->getValue());
294 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
295 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
296 VT = MVT::f32;
297 Extend = true;
298 }
299
300 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
301 TLI.getPointerTy());
302 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
303
304 if (Extend) Result = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Result);
305 }
306 break;
307 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000308 case ISD::TokenFactor: {
309 std::vector<SDOperand> Ops;
310 bool Changed = false;
311 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
312 Ops.push_back(LegalizeOp(Node->getOperand(i))); // Legalize the operands
313 Changed |= Ops[i] != Node->getOperand(i);
314 }
315 if (Changed)
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
317 break;
318 }
319
Chris Lattnerdc750592005-01-07 07:47:09 +0000320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
327 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
335 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000336 else
337 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000338
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
344
Chris Lattnerdc750592005-01-07 07:47:09 +0000345 case ISD::CALL:
346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfa854eb2005-01-07 21:35:32 +0000348 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000349 std::vector<MVT::ValueType> RetTyVTs;
350 RetTyVTs.reserve(Node->getNumValues());
351 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000352 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner9242c502005-01-09 19:43:23 +0000353 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0);
354 } else {
355 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000356 }
Chris Lattner9242c502005-01-09 19:43:23 +0000357 // Since calls produce multiple values, make sure to remember that we
358 // legalized all of them.
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
360 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
361 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000362
Chris Lattner68a12142005-01-07 22:12:08 +0000363 case ISD::BR:
364 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
365 if (Tmp1 != Node->getOperand(0))
366 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
367 break;
368
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000369 case ISD::BRCOND:
370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
371 // FIXME: booleans might not be legal!
372 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
373 // Basic block destination (Op#2) is always legal.
374 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
375 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
376 Node->getOperand(2));
377 break;
378
Chris Lattnerdc750592005-01-07 07:47:09 +0000379 case ISD::LOAD:
380 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
381 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
382 if (Tmp1 != Node->getOperand(0) ||
383 Tmp2 != Node->getOperand(1))
384 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000385 else
386 Result = SDOperand(Node, 0);
387
388 // Since loads produce two values, make sure to remember that we legalized
389 // both of them.
390 AddLegalizedOperand(SDOperand(Node, 0), Result);
391 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
392 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000393
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000394 case ISD::EXTLOAD:
395 case ISD::SEXTLOAD:
396 case ISD::ZEXTLOAD:
397 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
398 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
399 if (Tmp1 != Node->getOperand(0) ||
400 Tmp2 != Node->getOperand(1))
401 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
402 cast<MVTSDNode>(Node)->getExtraValueType());
403 else
404 Result = SDOperand(Node, 0);
405
406 // Since loads produce two values, make sure to remember that we legalized
407 // both of them.
408 AddLegalizedOperand(SDOperand(Node, 0), Result);
409 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
410 return Result.getValue(Op.ResNo);
411
Chris Lattnerdc750592005-01-07 07:47:09 +0000412 case ISD::EXTRACT_ELEMENT:
413 // Get both the low and high parts.
414 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
415 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
416 Result = Tmp2; // 1 -> Hi
417 else
418 Result = Tmp1; // 0 -> Lo
419 break;
420
421 case ISD::CopyToReg:
422 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
423
424 switch (getTypeAction(Node->getOperand(1).getValueType())) {
425 case Legal:
426 // Legalize the incoming value (must be legal).
427 Tmp2 = LegalizeOp(Node->getOperand(1));
428 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000429 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000430 break;
431 case Expand: {
432 SDOperand Lo, Hi;
433 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000434 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +0000435 Result = DAG.getCopyToReg(Tmp1, Lo, Reg);
436 Result = DAG.getCopyToReg(Result, Hi, Reg+1);
437 assert(isTypeLegal(Result.getValueType()) &&
438 "Cannot expand multiple times yet (i64 -> i16)");
439 break;
440 }
441 case Promote:
442 assert(0 && "Don't know what it means to promote this!");
443 abort();
444 }
445 break;
446
447 case ISD::RET:
448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
449 switch (Node->getNumOperands()) {
450 case 2: // ret val
451 switch (getTypeAction(Node->getOperand(1).getValueType())) {
452 case Legal:
453 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000454 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
456 break;
457 case Expand: {
458 SDOperand Lo, Hi;
459 ExpandOp(Node->getOperand(1), Lo, Hi);
460 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
461 break;
462 }
463 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000464 Tmp2 = PromoteOp(Node->getOperand(1));
465 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
466 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000467 }
468 break;
469 case 1: // ret void
470 if (Tmp1 != Node->getOperand(0))
471 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
472 break;
473 default: { // ret <values>
474 std::vector<SDOperand> NewValues;
475 NewValues.push_back(Tmp1);
476 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
477 switch (getTypeAction(Node->getOperand(i).getValueType())) {
478 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000479 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000480 break;
481 case Expand: {
482 SDOperand Lo, Hi;
483 ExpandOp(Node->getOperand(i), Lo, Hi);
484 NewValues.push_back(Lo);
485 NewValues.push_back(Hi);
486 break;
487 }
488 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000489 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
492 break;
493 }
494 }
495 break;
496 case ISD::STORE:
497 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
498 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
499
Chris Lattnere69daaf2005-01-08 06:25:56 +0000500 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000501 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000502 if (CFP->getValueType(0) == MVT::f32) {
503 union {
504 unsigned I;
505 float F;
506 } V;
507 V.F = CFP->getValue();
508 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
509 DAG.getConstant(V.I, MVT::i32), Tmp2);
510 } else {
511 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
512 union {
513 uint64_t I;
514 double F;
515 } V;
516 V.F = CFP->getValue();
517 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
518 DAG.getConstant(V.I, MVT::i64), Tmp2);
519 }
520 Op = Result;
521 Node = Op.Val;
522 }
523
Chris Lattnerdc750592005-01-07 07:47:09 +0000524 switch (getTypeAction(Node->getOperand(1).getValueType())) {
525 case Legal: {
526 SDOperand Val = LegalizeOp(Node->getOperand(1));
527 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
528 Tmp2 != Node->getOperand(2))
529 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
530 break;
531 }
532 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000533 // Truncate the value and store the result.
534 Tmp3 = PromoteOp(Node->getOperand(1));
535 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
536 Node->getOperand(1).getValueType());
537 break;
538
Chris Lattnerdc750592005-01-07 07:47:09 +0000539 case Expand:
540 SDOperand Lo, Hi;
541 ExpandOp(Node->getOperand(1), Lo, Hi);
542
543 if (!TLI.isLittleEndian())
544 std::swap(Lo, Hi);
545
546 // FIXME: These two stores are independent of each other!
547 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
548
Chris Lattner9242c502005-01-09 19:43:23 +0000549 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000550 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
551 getIntPtrConstant(IncrementSize));
552 assert(isTypeLegal(Tmp2.getValueType()) &&
553 "Pointers must be legal!");
554 Result = DAG.getNode(ISD::STORE, MVT::Other, Result, Hi, Tmp2);
555 }
556 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000557 case ISD::TRUNCSTORE:
558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
559 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
560
561 switch (getTypeAction(Node->getOperand(1).getValueType())) {
562 case Legal:
563 Tmp2 = LegalizeOp(Node->getOperand(1));
564 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
565 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000566 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000567 cast<MVTSDNode>(Node)->getExtraValueType());
568 break;
569 case Promote:
570 case Expand:
571 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
572 }
573 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000574 case ISD::SELECT:
Chris Lattnerdc750592005-01-07 07:47:09 +0000575 // FIXME: BOOLS MAY REQUIRE PROMOTION!
576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Cond
577 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000578 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
579
580 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattnerdc750592005-01-07 07:47:09 +0000581 Tmp3 != Node->getOperand(2))
582 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0), Tmp1, Tmp2,Tmp3);
583 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000584 case ISD::SETCC:
585 switch (getTypeAction(Node->getOperand(0).getValueType())) {
586 case Legal:
587 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
588 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
589 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
590 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
591 Tmp1, Tmp2);
592 break;
593 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000594 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
595 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
596
597 // If this is an FP compare, the operands have already been extended.
598 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
599 MVT::ValueType VT = Node->getOperand(0).getValueType();
600 MVT::ValueType NVT = TransformToType[VT];
601
602 // Otherwise, we have to insert explicit sign or zero extends. Note
603 // that we could insert sign extends for ALL conditions, but zero extend
604 // is cheaper on many machines (an AND instead of two shifts), so prefer
605 // it.
606 switch (cast<SetCCSDNode>(Node)->getCondition()) {
607 default: assert(0 && "Unknown integer comparison!");
608 case ISD::SETEQ:
609 case ISD::SETNE:
610 case ISD::SETUGE:
611 case ISD::SETUGT:
612 case ISD::SETULE:
613 case ISD::SETULT:
614 // ALL of these operations will work if we either sign or zero extend
615 // the operands (including the unsigned comparisons!). Zero extend is
616 // usually a simpler/cheaper operation, so prefer it.
617 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
618 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
619 break;
620 case ISD::SETGE:
621 case ISD::SETGT:
622 case ISD::SETLT:
623 case ISD::SETLE:
624 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
625 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
626 break;
627 }
628
629 }
630 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
631 Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000632 break;
633 case Expand:
634 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
635 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
636 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
637 switch (cast<SetCCSDNode>(Node)->getCondition()) {
638 case ISD::SETEQ:
639 case ISD::SETNE:
640 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
641 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
642 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
643 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(), Tmp1,
644 DAG.getConstant(0, Tmp1.getValueType()));
645 break;
646 default:
647 // FIXME: This generated code sucks.
648 ISD::CondCode LowCC;
649 switch (cast<SetCCSDNode>(Node)->getCondition()) {
650 default: assert(0 && "Unknown integer setcc!");
651 case ISD::SETLT:
652 case ISD::SETULT: LowCC = ISD::SETULT; break;
653 case ISD::SETGT:
654 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
655 case ISD::SETLE:
656 case ISD::SETULE: LowCC = ISD::SETULE; break;
657 case ISD::SETGE:
658 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
659 }
660
661 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
662 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
663 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
664
665 // NOTE: on targets without efficient SELECT of bools, we can always use
666 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
667 Tmp1 = DAG.getSetCC(LowCC, LHSLo, RHSLo);
668 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
669 LHSHi, RHSHi);
670 Result = DAG.getSetCC(ISD::SETEQ, LHSHi, RHSHi);
671 Result = DAG.getNode(ISD::SELECT, MVT::i1, Result, Tmp1, Tmp2);
672 break;
673 }
674 }
675 break;
676
Chris Lattner85d70c62005-01-11 05:57:22 +0000677 case ISD::MEMSET:
678 case ISD::MEMCPY:
679 case ISD::MEMMOVE: {
680 Tmp1 = LegalizeOp(Node->getOperand(0));
681 Tmp2 = LegalizeOp(Node->getOperand(1));
682 Tmp3 = LegalizeOp(Node->getOperand(2));
683 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
684 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
685 if (TLI.isOperationSupported(Node->getOpcode(), MVT::Other)) {
686 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
687 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
688 Tmp5 != Node->getOperand(4)) {
689 std::vector<SDOperand> Ops;
690 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
691 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
692 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
693 }
694 } else {
695 // Otherwise, the target does not support this operation. Lower the
696 // operation to an explicit libcall as appropriate.
697 MVT::ValueType IntPtr = TLI.getPointerTy();
698 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
699 std::vector<std::pair<SDOperand, const Type*> > Args;
700
Reid Spencer6dced922005-01-12 14:53:45 +0000701 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000702 if (Node->getOpcode() == ISD::MEMSET) {
703 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
704 // Extend the ubyte argument to be an int value for the call.
705 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
706 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
707 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
708
709 FnName = "memset";
710 } else if (Node->getOpcode() == ISD::MEMCPY ||
711 Node->getOpcode() == ISD::MEMMOVE) {
712 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
713 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
714 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
715 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
716 } else {
717 assert(0 && "Unknown op!");
718 }
719 std::pair<SDOperand,SDOperand> CallResult =
720 TLI.LowerCallTo(Tmp1, Type::VoidTy,
721 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
722 Result = LegalizeOp(CallResult.second);
723 }
724 break;
725 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000726 case ISD::ADD:
727 case ISD::SUB:
728 case ISD::MUL:
729 case ISD::UDIV:
730 case ISD::SDIV:
731 case ISD::UREM:
732 case ISD::SREM:
733 case ISD::AND:
734 case ISD::OR:
735 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000736 case ISD::SHL:
737 case ISD::SRL:
738 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000739 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
740 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
741 if (Tmp1 != Node->getOperand(0) ||
742 Tmp2 != Node->getOperand(1))
743 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
744 break;
745 case ISD::ZERO_EXTEND:
746 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000747 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000748 case ISD::FP_EXTEND:
749 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000750 case ISD::FP_TO_SINT:
751 case ISD::FP_TO_UINT:
752 case ISD::SINT_TO_FP:
753 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000754 switch (getTypeAction(Node->getOperand(0).getValueType())) {
755 case Legal:
756 Tmp1 = LegalizeOp(Node->getOperand(0));
757 if (Tmp1 != Node->getOperand(0))
758 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
759 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000760 case Expand:
Chris Lattner05b4e372005-01-13 17:59:25 +0000761 assert(Node->getOpcode() != ISD::SINT_TO_FP &&
762 Node->getOpcode() != ISD::UINT_TO_FP &&
763 "Cannot lower Xint_to_fp to a call yet!");
764
Chris Lattnera65a2f02005-01-07 22:37:48 +0000765 // In the expand case, we must be dealing with a truncate, because
766 // otherwise the result would be larger than the source.
767 assert(Node->getOpcode() == ISD::TRUNCATE &&
768 "Shouldn't need to expand other operators here!");
769 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
770
771 // Since the result is legal, we should just be able to truncate the low
772 // part of the source.
773 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
774 break;
775
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000776 case Promote:
777 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000778 case ISD::ZERO_EXTEND:
779 Result = PromoteOp(Node->getOperand(0));
780 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
781 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000782 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000783 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000784 Result = PromoteOp(Node->getOperand(0));
785 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
786 Result, Node->getOperand(0).getValueType());
787 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000788 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000789 Result = PromoteOp(Node->getOperand(0));
790 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
791 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000792 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000793 Result = PromoteOp(Node->getOperand(0));
794 if (Result.getValueType() != Op.getValueType())
795 // Dynamically dead while we have only 2 FP types.
796 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
797 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000798 case ISD::FP_ROUND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000799 Result = PromoteOp(Node->getOperand(0));
800 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Result);
801 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000802 case ISD::FP_TO_SINT:
803 case ISD::FP_TO_UINT:
804 case ISD::SINT_TO_FP:
805 case ISD::UINT_TO_FP:
806 Node->dump();
807 assert(0 && "Do not know how to promote this yet!");
808 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000809 }
810 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000811 case ISD::FP_ROUND_INREG:
812 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000813 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000814 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000815 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
816
817 // If this operation is not supported, convert it to a shl/shr or load/store
818 // pair.
819 if (!TLI.isOperationSupported(Node->getOpcode(), ExtraVT)) {
820 // If this is an integer extend and shifts are supported, do that.
821 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
822 // NOTE: we could fall back on load/store here too for targets without
823 // AND. However, it is doubtful that any exist.
824 // AND out the appropriate bits.
825 SDOperand Mask =
826 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
827 Node->getValueType(0));
828 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
829 Node->getOperand(0), Mask);
830 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
831 // NOTE: we could fall back on load/store here too for targets without
832 // SAR. However, it is doubtful that any exist.
833 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
834 MVT::getSizeInBits(ExtraVT);
835 SDOperand ShiftCst = DAG.getConstant(BitsDiff, MVT::i8);
836 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
837 Node->getOperand(0), ShiftCst);
838 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
839 Result, ShiftCst);
840 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
841 // The only way we can lower this is to turn it into a STORETRUNC,
842 // EXTLOAD pair, targetting a temporary location (a stack slot).
843
844 // NOTE: there is a choice here between constantly creating new stack
845 // slots and always reusing the same one. We currently always create
846 // new ones, as reuse may inhibit scheduling.
847 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
848 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
849 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
850 MachineFunction &MF = DAG.getMachineFunction();
851 int SSFI =
852 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
853 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
854 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
855 Node->getOperand(0), StackSlot, ExtraVT);
856 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
857 Result, StackSlot, ExtraVT);
858 } else {
859 assert(0 && "Unknown op");
860 }
861 Result = LegalizeOp(Result);
862 } else {
863 if (Tmp1 != Node->getOperand(0))
864 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
865 ExtraVT);
866 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000867 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000868 }
Chris Lattner99222f72005-01-15 07:15:18 +0000869 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000870
Chris Lattnerea4ca942005-01-07 22:28:47 +0000871 if (!Op.Val->hasOneUse())
872 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000873
874 return Result;
875}
876
Chris Lattner4d978642005-01-15 22:16:26 +0000877/// PromoteOp - Given an operation that produces a value in an invalid type,
878/// promote it to compute the value into a larger type. The produced value will
879/// have the correct bits for the low portion of the register, but no guarantee
880/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000881SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
882 MVT::ValueType VT = Op.getValueType();
883 MVT::ValueType NVT = TransformToType[VT];
884 assert(getTypeAction(VT) == Promote &&
885 "Caller should expand or legalize operands that are not promotable!");
886 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
887 "Cannot promote to smaller type!");
888
889 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
890 if (I != PromotedNodes.end()) return I->second;
891
892 SDOperand Tmp1, Tmp2, Tmp3;
893
894 SDOperand Result;
895 SDNode *Node = Op.Val;
896
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000897 // Promotion needs an optimization step to clean up after it, and is not
898 // careful to avoid operations the target does not support. Make sure that
899 // all generated operations are legalized in the next iteration.
900 NeedsAnotherIteration = true;
901
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000902 switch (Node->getOpcode()) {
903 default:
904 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
905 assert(0 && "Do not know how to promote this operator!");
906 abort();
Chris Lattner4d978642005-01-15 22:16:26 +0000907 case ISD::CALL:
908 assert(0 && "Target's LowerCallTo implementation is buggy, returning value"
909 " types that are not supported by the target!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000910 case ISD::Constant:
911 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
912 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
913 break;
914 case ISD::ConstantFP:
915 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
916 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
917 break;
918
919 case ISD::TRUNCATE:
920 switch (getTypeAction(Node->getOperand(0).getValueType())) {
921 case Legal:
922 Result = LegalizeOp(Node->getOperand(0));
923 assert(Result.getValueType() >= NVT &&
924 "This truncation doesn't make sense!");
925 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
926 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
927 break;
928 case Expand:
929 assert(0 && "Cannot handle expand yet");
930 case Promote:
931 assert(0 && "Cannot handle promote-promote yet");
932 }
933 break;
Chris Lattner4d978642005-01-15 22:16:26 +0000934 case ISD::SIGN_EXTEND:
935 case ISD::ZERO_EXTEND:
936 switch (getTypeAction(Node->getOperand(0).getValueType())) {
937 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
938 case Legal:
939 // Input is legal? Just do extend all the way to the larger type.
940 Result = LegalizeOp(Node->getOperand(0));
941 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
942 break;
943 case Promote:
944 // Promote the reg if it's smaller.
945 Result = PromoteOp(Node->getOperand(0));
946 // The high bits are not guaranteed to be anything. Insert an extend.
947 if (Node->getOpcode() == ISD::SIGN_EXTEND)
948 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
949 else
950 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
951 break;
952 }
953 break;
954
955 case ISD::FP_EXTEND:
956 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
957 case ISD::FP_ROUND:
958 switch (getTypeAction(Node->getOperand(0).getValueType())) {
959 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
960 case Promote: assert(0 && "Unreachable with 2 FP types!");
961 case Legal:
962 // Input is legal? Do an FP_ROUND_INREG.
963 Result = LegalizeOp(Node->getOperand(0));
964 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
965 break;
966 }
967 break;
968
969 case ISD::SINT_TO_FP:
970 case ISD::UINT_TO_FP:
971 switch (getTypeAction(Node->getOperand(0).getValueType())) {
972 case Legal:
973 Result = LegalizeOp(Node->getOperand(0));
974 break;
975
976 case Promote:
977 Result = PromoteOp(Node->getOperand(0));
978 if (Node->getOpcode() == ISD::SINT_TO_FP)
979 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
980 Result, Node->getOperand(0).getValueType());
981 else
982 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
983 Result, Node->getOperand(0).getValueType());
984 break;
985 case Expand:
986 assert(0 && "Unimplemented");
987 }
988 // No extra round required here.
989 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
990 break;
991
992 case ISD::FP_TO_SINT:
993 case ISD::FP_TO_UINT:
994 switch (getTypeAction(Node->getOperand(0).getValueType())) {
995 case Legal:
996 Tmp1 = LegalizeOp(Node->getOperand(0));
997 break;
998 case Promote:
999 // The input result is prerounded, so we don't have to do anything
1000 // special.
1001 Tmp1 = PromoteOp(Node->getOperand(0));
1002 break;
1003 case Expand:
1004 assert(0 && "not implemented");
1005 }
1006 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1007 break;
1008
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001009 case ISD::AND:
1010 case ISD::OR:
1011 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001012 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001013 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001014 case ISD::MUL:
1015 // The input may have strange things in the top bits of the registers, but
1016 // these operations don't care. They may have wierd bits going out, but
1017 // that too is okay if they are integer operations.
1018 Tmp1 = PromoteOp(Node->getOperand(0));
1019 Tmp2 = PromoteOp(Node->getOperand(1));
1020 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1021 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1022
1023 // However, if this is a floating point operation, they will give excess
1024 // precision that we may not be able to tolerate. If we DO allow excess
1025 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001026 // FIXME: Why would we need to round FP ops more than integer ones?
1027 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001028 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1029 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1030 break;
1031
Chris Lattner4d978642005-01-15 22:16:26 +00001032 case ISD::SDIV:
1033 case ISD::SREM:
1034 // These operators require that their input be sign extended.
1035 Tmp1 = PromoteOp(Node->getOperand(0));
1036 Tmp2 = PromoteOp(Node->getOperand(1));
1037 if (MVT::isInteger(NVT)) {
1038 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001039 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001040 }
1041 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1042
1043 // Perform FP_ROUND: this is probably overly pessimistic.
1044 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1045 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1046 break;
1047
1048 case ISD::UDIV:
1049 case ISD::UREM:
1050 // These operators require that their input be zero extended.
1051 Tmp1 = PromoteOp(Node->getOperand(0));
1052 Tmp2 = PromoteOp(Node->getOperand(1));
1053 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1054 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001055 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001056 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1057 break;
1058
1059 case ISD::SHL:
1060 Tmp1 = PromoteOp(Node->getOperand(0));
1061 Tmp2 = LegalizeOp(Node->getOperand(1));
1062 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1063 break;
1064 case ISD::SRA:
1065 // The input value must be properly sign extended.
1066 Tmp1 = PromoteOp(Node->getOperand(0));
1067 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1068 Tmp2 = LegalizeOp(Node->getOperand(1));
1069 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1070 break;
1071 case ISD::SRL:
1072 // The input value must be properly zero extended.
1073 Tmp1 = PromoteOp(Node->getOperand(0));
1074 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1075 Tmp2 = LegalizeOp(Node->getOperand(1));
1076 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1077 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001078 case ISD::LOAD:
1079 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1080 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1081 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1082
1083 // Remember that we legalized the chain.
1084 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1085 break;
1086 case ISD::SELECT:
Chris Lattner4d978642005-01-15 22:16:26 +00001087 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001088 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1089 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1090 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1091 break;
1092 }
1093
1094 assert(Result.Val && "Didn't set a result!");
1095 AddPromotedOperand(Op, Result);
1096 return Result;
1097}
Chris Lattnerdc750592005-01-07 07:47:09 +00001098
1099/// ExpandOp - Expand the specified SDOperand into its two component pieces
1100/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1101/// LegalizeNodes map is filled in for any results that are not expanded, the
1102/// ExpandedNodes map is filled in for any results that are expanded, and the
1103/// Lo/Hi values are returned.
1104void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1105 MVT::ValueType VT = Op.getValueType();
1106 MVT::ValueType NVT = TransformToType[VT];
1107 SDNode *Node = Op.Val;
1108 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1109 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1110 assert(MVT::isInteger(NVT) && NVT < VT &&
1111 "Cannot expand to FP value or to larger int value!");
1112
1113 // If there is more than one use of this, see if we already expanded it.
1114 // There is no use remembering values that only have a single use, as the map
1115 // entries will never be reused.
1116 if (!Node->hasOneUse()) {
1117 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1118 = ExpandedNodes.find(Op);
1119 if (I != ExpandedNodes.end()) {
1120 Lo = I->second.first;
1121 Hi = I->second.second;
1122 return;
1123 }
1124 }
1125
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001126 // Expanding to multiple registers needs to perform an optimization step, and
1127 // is not careful to avoid operations the target does not support. Make sure
1128 // that all generated operations are legalized in the next iteration.
1129 NeedsAnotherIteration = true;
1130 const char *LibCallName = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001131
Chris Lattnerdc750592005-01-07 07:47:09 +00001132 switch (Node->getOpcode()) {
1133 default:
1134 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1135 assert(0 && "Do not know how to expand this operator!");
1136 abort();
1137 case ISD::Constant: {
1138 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1139 Lo = DAG.getConstant(Cst, NVT);
1140 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1141 break;
1142 }
1143
1144 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001145 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001146 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001147 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1148 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1149
1150 // Remember that we legalized the chain.
1151 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1152
Chris Lattnerdc750592005-01-07 07:47:09 +00001153 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1154 break;
1155 }
1156
1157 case ISD::LOAD: {
1158 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1159 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1160 Lo = DAG.getLoad(NVT, Ch, Ptr);
1161
1162 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001163 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001164 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1165 getIntPtrConstant(IncrementSize));
1166 // FIXME: This load is independent of the first one.
1167 Hi = DAG.getLoad(NVT, Lo.getValue(1), Ptr);
1168
1169 // Remember that we legalized the chain.
Chris Lattnerea4ca942005-01-07 22:28:47 +00001170 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001171 if (!TLI.isLittleEndian())
1172 std::swap(Lo, Hi);
1173 break;
1174 }
1175 case ISD::CALL: {
1176 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1177 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1178
1179 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1180 "Can only expand a call once so far, not i64 -> i16!");
1181
1182 std::vector<MVT::ValueType> RetTyVTs;
1183 RetTyVTs.reserve(3);
1184 RetTyVTs.push_back(NVT);
1185 RetTyVTs.push_back(NVT);
1186 RetTyVTs.push_back(MVT::Other);
1187 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
1188 Lo = SDOperand(NC, 0);
1189 Hi = SDOperand(NC, 1);
1190
1191 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001192 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001193 break;
1194 }
1195 case ISD::AND:
1196 case ISD::OR:
1197 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1198 SDOperand LL, LH, RL, RH;
1199 ExpandOp(Node->getOperand(0), LL, LH);
1200 ExpandOp(Node->getOperand(1), RL, RH);
1201 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1202 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1203 break;
1204 }
1205 case ISD::SELECT: {
1206 SDOperand C, LL, LH, RL, RH;
1207 // FIXME: BOOLS MAY REQUIRE PROMOTION!
1208 C = LegalizeOp(Node->getOperand(0));
1209 ExpandOp(Node->getOperand(1), LL, LH);
1210 ExpandOp(Node->getOperand(2), RL, RH);
1211 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1212 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1213 break;
1214 }
1215 case ISD::SIGN_EXTEND: {
1216 // The low part is just a sign extension of the input (which degenerates to
1217 // a copy).
1218 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1219
1220 // The high part is obtained by SRA'ing all but one of the bits of the lo
1221 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001222 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
1223 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8));
Chris Lattnerdc750592005-01-07 07:47:09 +00001224 break;
1225 }
1226 case ISD::ZERO_EXTEND:
1227 // The low part is just a zero extension of the input (which degenerates to
1228 // a copy).
1229 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1230
1231 // The high part is just a zero.
1232 Hi = DAG.getConstant(0, NVT);
1233 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001234
1235 // These operators cannot be expanded directly, emit them as calls to
1236 // library functions.
1237 case ISD::FP_TO_SINT:
1238 if (Node->getOperand(0).getValueType() == MVT::f32)
1239 LibCallName = "__fixsfdi";
1240 else
1241 LibCallName = "__fixdfdi";
1242 break;
1243 case ISD::FP_TO_UINT:
1244 if (Node->getOperand(0).getValueType() == MVT::f32)
1245 LibCallName = "__fixunssfdi";
1246 else
1247 LibCallName = "__fixunsdfdi";
1248 break;
1249
1250 case ISD::ADD: LibCallName = "__adddi3"; break;
1251 case ISD::SUB: LibCallName = "__subdi3"; break;
1252 case ISD::MUL: LibCallName = "__muldi3"; break;
1253 case ISD::SDIV: LibCallName = "__divdi3"; break;
1254 case ISD::UDIV: LibCallName = "__udivdi3"; break;
1255 case ISD::SREM: LibCallName = "__moddi3"; break;
1256 case ISD::UREM: LibCallName = "__umoddi3"; break;
Chris Lattnerbe02d432005-01-10 21:02:37 +00001257 case ISD::SHL: LibCallName = "__ashldi3"; break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001258 case ISD::SRA: LibCallName = "__ashrdi3"; break;
Chris Lattnerbe02d432005-01-10 21:02:37 +00001259 case ISD::SRL: LibCallName = "__lshrdi3"; break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001260 }
1261
1262 // Int2FP -> __floatdisf/__floatdidf
1263
1264 // If this is to be expanded into a libcall... do so now.
1265 if (LibCallName) {
1266 TargetLowering::ArgListTy Args;
1267 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1268 Args.push_back(std::make_pair(Node->getOperand(i),
Chris Lattner99222f72005-01-15 07:15:18 +00001269 MVT::getTypeForValueType(Node->getOperand(i).getValueType())));
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001270 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
1271
1272 // We don't care about token chains for libcalls. We just use the entry
1273 // node as our input and ignore the output chain. This allows us to place
1274 // calls wherever we need them to satisfy data dependences.
1275 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
Chris Lattner99222f72005-01-15 07:15:18 +00001276 MVT::getTypeForValueType(Op.getValueType()), Callee,
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001277 Args, DAG).first;
1278 ExpandOp(Result, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +00001279 }
1280
1281 // Remember in a map if the values will be reused later.
1282 if (!Node->hasOneUse()) {
1283 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1284 std::make_pair(Lo, Hi))).second;
1285 assert(isNew && "Value already expanded?!?");
1286 }
1287}
1288
1289
1290// SelectionDAG::Legalize - This is the entry point for the file.
1291//
1292void SelectionDAG::Legalize(TargetLowering &TLI) {
1293 /// run - This is the main entry point to this class.
1294 ///
1295 SelectionDAGLegalize(TLI, *this).Run();
1296}
1297