blob: 2994d4812b8c0bdd104199e8709ba02f351fa627 [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
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000122 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
123 SDOperand &Lo, SDOperand &Hi);
124
Chris Lattnerdc750592005-01-07 07:47:09 +0000125 SDOperand getIntPtrConstant(uint64_t Val) {
126 return DAG.getConstant(Val, TLI.getPointerTy());
127 }
128};
129}
130
131
132SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
133 SelectionDAG &dag)
Chris Lattner87a769c2005-01-16 01:11:45 +0000134 : TLI(tli), DAG(dag), ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000135 assert(MVT::LAST_VALUETYPE <= 16 &&
136 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000137}
138
Chris Lattnerdc750592005-01-07 07:47:09 +0000139void SelectionDAGLegalize::LegalizeDAG() {
140 SDOperand OldRoot = DAG.getRoot();
141 SDOperand NewRoot = LegalizeOp(OldRoot);
142 DAG.setRoot(NewRoot);
143
144 ExpandedNodes.clear();
145 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000146 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000147
148 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000149 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000150}
151
152SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000153 assert(getTypeAction(Op.getValueType()) == Legal &&
154 "Caller should expand or promote operands that are not legal!");
155
Chris Lattnerdc750592005-01-07 07:47:09 +0000156 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000157 // register on this target, make sure to expand or promote them.
158 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000159 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
160 switch (getTypeAction(Op.Val->getValueType(i))) {
161 case Legal: break; // Nothing to do.
162 case Expand: {
163 SDOperand T1, T2;
164 ExpandOp(Op.getValue(i), T1, T2);
165 assert(LegalizedNodes.count(Op) &&
166 "Expansion didn't add legal operands!");
167 return LegalizedNodes[Op];
168 }
169 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000170 PromoteOp(Op.getValue(i));
171 assert(LegalizedNodes.count(Op) &&
172 "Expansion didn't add legal operands!");
173 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000174 }
175 }
176
Chris Lattner85d70c62005-01-11 05:57:22 +0000177 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
178 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000179
Chris Lattnerec26b482005-01-09 19:03:49 +0000180 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000181
182 SDOperand Result = Op;
183 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000184
185 switch (Node->getOpcode()) {
186 default:
187 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
188 assert(0 && "Do not know how to legalize this operator!");
189 abort();
190 case ISD::EntryToken:
191 case ISD::FrameIndex:
192 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000193 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000194 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000195 assert(getTypeAction(Node->getValueType(0)) == Legal &&
196 "This must be legal!");
197 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000198 case ISD::CopyFromReg:
199 Tmp1 = LegalizeOp(Node->getOperand(0));
200 if (Tmp1 != Node->getOperand(0))
201 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
202 Node->getValueType(0), Tmp1);
203 break;
Chris Lattnere727af02005-01-13 20:50:02 +0000204 case ISD::ImplicitDef:
205 Tmp1 = LegalizeOp(Node->getOperand(0));
206 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000207 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000208 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000209 case ISD::Constant:
210 // We know we don't need to expand constants here, constants only have one
211 // value and we check that it is fine above.
212
213 // FIXME: Maybe we should handle things like targets that don't support full
214 // 32-bit immediates?
215 break;
216 case ISD::ConstantFP: {
217 // Spill FP immediates to the constant pool if the target cannot directly
218 // codegen them. Targets often have some immediate values that can be
219 // efficiently generated into an FP register without a load. We explicitly
220 // leave these constants as ConstantFP nodes for the target to deal with.
221
222 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
223
224 // Check to see if this FP immediate is already legal.
225 bool isLegal = false;
226 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
227 E = TLI.legal_fpimm_end(); I != E; ++I)
228 if (CFP->isExactlyValue(*I)) {
229 isLegal = true;
230 break;
231 }
232
233 if (!isLegal) {
234 // Otherwise we need to spill the constant to memory.
235 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
236
237 bool Extend = false;
238
239 // If a FP immediate is precise when represented as a float, we put it
240 // into the constant pool as a float, even if it's is statically typed
241 // as a double.
242 MVT::ValueType VT = CFP->getValueType(0);
243 bool isDouble = VT == MVT::f64;
244 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
245 Type::FloatTy, CFP->getValue());
246 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
247 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
248 VT = MVT::f32;
249 Extend = true;
250 }
251
252 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
253 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000254 if (Extend) {
255 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
256 MVT::f32);
257 } else {
258 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
259 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000260 }
261 break;
262 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000263 case ISD::TokenFactor: {
264 std::vector<SDOperand> Ops;
265 bool Changed = false;
266 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
267 Ops.push_back(LegalizeOp(Node->getOperand(i))); // Legalize the operands
268 Changed |= Ops[i] != Node->getOperand(i);
269 }
270 if (Changed)
271 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
272 break;
273 }
274
Chris Lattnerdc750592005-01-07 07:47:09 +0000275 case ISD::ADJCALLSTACKDOWN:
276 case ISD::ADJCALLSTACKUP:
277 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
278 // There is no need to legalize the size argument (Operand #1)
279 if (Tmp1 != Node->getOperand(0))
280 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
281 Node->getOperand(1));
282 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000283 case ISD::DYNAMIC_STACKALLOC:
284 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
285 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
286 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
287 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
288 Tmp3 != Node->getOperand(2))
289 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
290 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000291 else
292 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000293
294 // Since this op produces two values, make sure to remember that we
295 // legalized both of them.
296 AddLegalizedOperand(SDOperand(Node, 0), Result);
297 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
298 return Result.getValue(Op.ResNo);
299
Chris Lattnerdc750592005-01-07 07:47:09 +0000300 case ISD::CALL:
301 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
302 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattnerfa854eb2005-01-07 21:35:32 +0000303 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000304 std::vector<MVT::ValueType> RetTyVTs;
305 RetTyVTs.reserve(Node->getNumValues());
306 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000307 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner9242c502005-01-09 19:43:23 +0000308 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2), 0);
309 } else {
310 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000311 }
Chris Lattner9242c502005-01-09 19:43:23 +0000312 // Since calls produce multiple values, make sure to remember that we
313 // legalized all of them.
314 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
315 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
316 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000317
Chris Lattner68a12142005-01-07 22:12:08 +0000318 case ISD::BR:
319 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
320 if (Tmp1 != Node->getOperand(0))
321 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
322 break;
323
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000324 case ISD::BRCOND:
325 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000326
327 switch (getTypeAction(Node->getOperand(1).getValueType())) {
328 case Expand: assert(0 && "It's impossible to expand bools");
329 case Legal:
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
331 break;
332 case Promote:
333 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
334 break;
335 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000336 // Basic block destination (Op#2) is always legal.
337 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
338 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
339 Node->getOperand(2));
340 break;
341
Chris Lattnerdc750592005-01-07 07:47:09 +0000342 case ISD::LOAD:
343 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
344 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
345 if (Tmp1 != Node->getOperand(0) ||
346 Tmp2 != Node->getOperand(1))
347 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000348 else
349 Result = SDOperand(Node, 0);
350
351 // Since loads produce two values, make sure to remember that we legalized
352 // both of them.
353 AddLegalizedOperand(SDOperand(Node, 0), Result);
354 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
355 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000356
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000357 case ISD::EXTLOAD:
358 case ISD::SEXTLOAD:
359 case ISD::ZEXTLOAD:
360 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
361 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
362 if (Tmp1 != Node->getOperand(0) ||
363 Tmp2 != Node->getOperand(1))
364 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
365 cast<MVTSDNode>(Node)->getExtraValueType());
366 else
367 Result = SDOperand(Node, 0);
368
369 // Since loads produce two values, make sure to remember that we legalized
370 // both of them.
371 AddLegalizedOperand(SDOperand(Node, 0), Result);
372 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
373 return Result.getValue(Op.ResNo);
374
Chris Lattnerdc750592005-01-07 07:47:09 +0000375 case ISD::EXTRACT_ELEMENT:
376 // Get both the low and high parts.
377 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
378 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
379 Result = Tmp2; // 1 -> Hi
380 else
381 Result = Tmp1; // 0 -> Lo
382 break;
383
384 case ISD::CopyToReg:
385 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
386
387 switch (getTypeAction(Node->getOperand(1).getValueType())) {
388 case Legal:
389 // Legalize the incoming value (must be legal).
390 Tmp2 = LegalizeOp(Node->getOperand(1));
391 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000392 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000393 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000394 case Promote:
395 Tmp2 = PromoteOp(Node->getOperand(1));
396 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
397 break;
398 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000399 SDOperand Lo, Hi;
400 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000401 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000402 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
403 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
404 // Note that the copytoreg nodes are independent of each other.
405 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000406 assert(isTypeLegal(Result.getValueType()) &&
407 "Cannot expand multiple times yet (i64 -> i16)");
408 break;
409 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000410 break;
411
412 case ISD::RET:
413 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
414 switch (Node->getNumOperands()) {
415 case 2: // ret val
416 switch (getTypeAction(Node->getOperand(1).getValueType())) {
417 case Legal:
418 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000419 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000420 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
421 break;
422 case Expand: {
423 SDOperand Lo, Hi;
424 ExpandOp(Node->getOperand(1), Lo, Hi);
425 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
426 break;
427 }
428 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000429 Tmp2 = PromoteOp(Node->getOperand(1));
430 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
431 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 }
433 break;
434 case 1: // ret void
435 if (Tmp1 != Node->getOperand(0))
436 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
437 break;
438 default: { // ret <values>
439 std::vector<SDOperand> NewValues;
440 NewValues.push_back(Tmp1);
441 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
442 switch (getTypeAction(Node->getOperand(i).getValueType())) {
443 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000444 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000445 break;
446 case Expand: {
447 SDOperand Lo, Hi;
448 ExpandOp(Node->getOperand(i), Lo, Hi);
449 NewValues.push_back(Lo);
450 NewValues.push_back(Hi);
451 break;
452 }
453 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000454 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000455 }
456 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
457 break;
458 }
459 }
460 break;
461 case ISD::STORE:
462 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
463 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
464
Chris Lattnere69daaf2005-01-08 06:25:56 +0000465 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000466 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000467 if (CFP->getValueType(0) == MVT::f32) {
468 union {
469 unsigned I;
470 float F;
471 } V;
472 V.F = CFP->getValue();
473 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
474 DAG.getConstant(V.I, MVT::i32), Tmp2);
475 } else {
476 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
477 union {
478 uint64_t I;
479 double F;
480 } V;
481 V.F = CFP->getValue();
482 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
483 DAG.getConstant(V.I, MVT::i64), Tmp2);
484 }
485 Op = Result;
486 Node = Op.Val;
487 }
488
Chris Lattnerdc750592005-01-07 07:47:09 +0000489 switch (getTypeAction(Node->getOperand(1).getValueType())) {
490 case Legal: {
491 SDOperand Val = LegalizeOp(Node->getOperand(1));
492 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
493 Tmp2 != Node->getOperand(2))
494 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
495 break;
496 }
497 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000498 // Truncate the value and store the result.
499 Tmp3 = PromoteOp(Node->getOperand(1));
500 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
501 Node->getOperand(1).getValueType());
502 break;
503
Chris Lattnerdc750592005-01-07 07:47:09 +0000504 case Expand:
505 SDOperand Lo, Hi;
506 ExpandOp(Node->getOperand(1), Lo, Hi);
507
508 if (!TLI.isLittleEndian())
509 std::swap(Lo, Hi);
510
Chris Lattner0d03eb42005-01-19 18:02:17 +0000511 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000512
Chris Lattner0d03eb42005-01-19 18:02:17 +0000513 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000514 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
515 getIntPtrConstant(IncrementSize));
516 assert(isTypeLegal(Tmp2.getValueType()) &&
517 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000518 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
519 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
520 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000521 }
522 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000523 case ISD::TRUNCSTORE:
524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
525 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
526
527 switch (getTypeAction(Node->getOperand(1).getValueType())) {
528 case Legal:
529 Tmp2 = LegalizeOp(Node->getOperand(1));
530 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
531 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000532 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000533 cast<MVTSDNode>(Node)->getExtraValueType());
534 break;
535 case Promote:
536 case Expand:
537 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
538 }
539 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000540 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
542 case Expand: assert(0 && "It's impossible to expand bools");
543 case Legal:
544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
545 break;
546 case Promote:
547 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
548 break;
549 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000550 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000551 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000552
553 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
554 default: assert(0 && "This action is not supported yet!");
555 case TargetLowering::Legal:
556 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
557 Tmp3 != Node->getOperand(2))
558 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
559 Tmp1, Tmp2, Tmp3);
560 break;
561 case TargetLowering::Promote: {
562 MVT::ValueType NVT =
563 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
564 unsigned ExtOp, TruncOp;
565 if (MVT::isInteger(Tmp2.getValueType())) {
566 ExtOp = ISD::ZERO_EXTEND;
567 TruncOp = ISD::TRUNCATE;
568 } else {
569 ExtOp = ISD::FP_EXTEND;
570 TruncOp = ISD::FP_ROUND;
571 }
572 // Promote each of the values to the new type.
573 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
574 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
575 // Perform the larger operation, then round down.
576 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
577 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
578 break;
579 }
580 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000581 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000582 case ISD::SETCC:
583 switch (getTypeAction(Node->getOperand(0).getValueType())) {
584 case Legal:
585 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
586 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
587 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
588 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000589 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000590 break;
591 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000592 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
593 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
594
595 // If this is an FP compare, the operands have already been extended.
596 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
597 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000598 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000599
600 // Otherwise, we have to insert explicit sign or zero extends. Note
601 // that we could insert sign extends for ALL conditions, but zero extend
602 // is cheaper on many machines (an AND instead of two shifts), so prefer
603 // it.
604 switch (cast<SetCCSDNode>(Node)->getCondition()) {
605 default: assert(0 && "Unknown integer comparison!");
606 case ISD::SETEQ:
607 case ISD::SETNE:
608 case ISD::SETUGE:
609 case ISD::SETUGT:
610 case ISD::SETULE:
611 case ISD::SETULT:
612 // ALL of these operations will work if we either sign or zero extend
613 // the operands (including the unsigned comparisons!). Zero extend is
614 // usually a simpler/cheaper operation, so prefer it.
615 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
616 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
617 break;
618 case ISD::SETGE:
619 case ISD::SETGT:
620 case ISD::SETLT:
621 case ISD::SETLE:
622 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
623 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
624 break;
625 }
626
627 }
628 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000629 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000630 break;
631 case Expand:
632 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
633 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
634 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
635 switch (cast<SetCCSDNode>(Node)->getCondition()) {
636 case ISD::SETEQ:
637 case ISD::SETNE:
638 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
639 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
640 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000641 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
642 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000643 DAG.getConstant(0, Tmp1.getValueType()));
644 break;
645 default:
646 // FIXME: This generated code sucks.
647 ISD::CondCode LowCC;
648 switch (cast<SetCCSDNode>(Node)->getCondition()) {
649 default: assert(0 && "Unknown integer setcc!");
650 case ISD::SETLT:
651 case ISD::SETULT: LowCC = ISD::SETULT; break;
652 case ISD::SETGT:
653 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
654 case ISD::SETLE:
655 case ISD::SETULE: LowCC = ISD::SETULE; break;
656 case ISD::SETGE:
657 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
658 }
659
660 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
661 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
662 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
663
664 // NOTE: on targets without efficient SELECT of bools, we can always use
665 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000666 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000667 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000668 Node->getValueType(0), LHSHi, RHSHi);
669 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
670 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
671 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000672 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));
Chris Lattner3c0dd462005-01-16 07:29:19 +0000685
686 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
687 default: assert(0 && "This action not implemented for this operation!");
688 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000689 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
690 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
691 Tmp5 != Node->getOperand(4)) {
692 std::vector<SDOperand> Ops;
693 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
694 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
695 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
696 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000697 break;
698 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000699 // Otherwise, the target does not support this operation. Lower the
700 // operation to an explicit libcall as appropriate.
701 MVT::ValueType IntPtr = TLI.getPointerTy();
702 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
703 std::vector<std::pair<SDOperand, const Type*> > Args;
704
Reid Spencer6dced922005-01-12 14:53:45 +0000705 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000706 if (Node->getOpcode() == ISD::MEMSET) {
707 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
708 // Extend the ubyte argument to be an int value for the call.
709 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
710 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
711 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
712
713 FnName = "memset";
714 } else if (Node->getOpcode() == ISD::MEMCPY ||
715 Node->getOpcode() == ISD::MEMMOVE) {
716 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
717 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
718 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
719 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
720 } else {
721 assert(0 && "Unknown op!");
722 }
723 std::pair<SDOperand,SDOperand> CallResult =
724 TLI.LowerCallTo(Tmp1, Type::VoidTy,
725 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
726 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000727 break;
728 }
729 case TargetLowering::Custom:
730 std::vector<SDOperand> Ops;
731 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
732 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
733 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
734 Result = TLI.LowerOperation(Result);
735 Result = LegalizeOp(Result);
736 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000737 }
738 break;
739 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000740 case ISD::ADD:
741 case ISD::SUB:
742 case ISD::MUL:
743 case ISD::UDIV:
744 case ISD::SDIV:
745 case ISD::UREM:
746 case ISD::SREM:
747 case ISD::AND:
748 case ISD::OR:
749 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000750 case ISD::SHL:
751 case ISD::SRL:
752 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000753 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
754 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
755 if (Tmp1 != Node->getOperand(0) ||
756 Tmp2 != Node->getOperand(1))
757 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
758 break;
759 case ISD::ZERO_EXTEND:
760 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000761 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000762 case ISD::FP_EXTEND:
763 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000764 case ISD::FP_TO_SINT:
765 case ISD::FP_TO_UINT:
766 case ISD::SINT_TO_FP:
767 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000768 switch (getTypeAction(Node->getOperand(0).getValueType())) {
769 case Legal:
770 Tmp1 = LegalizeOp(Node->getOperand(0));
771 if (Tmp1 != Node->getOperand(0))
772 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
773 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000774 case Expand:
Chris Lattner05b4e372005-01-13 17:59:25 +0000775 assert(Node->getOpcode() != ISD::SINT_TO_FP &&
776 Node->getOpcode() != ISD::UINT_TO_FP &&
777 "Cannot lower Xint_to_fp to a call yet!");
778
Chris Lattnera65a2f02005-01-07 22:37:48 +0000779 // In the expand case, we must be dealing with a truncate, because
780 // otherwise the result would be larger than the source.
781 assert(Node->getOpcode() == ISD::TRUNCATE &&
782 "Shouldn't need to expand other operators here!");
783 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
784
785 // Since the result is legal, we should just be able to truncate the low
786 // part of the source.
787 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
788 break;
789
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000790 case Promote:
791 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000792 case ISD::ZERO_EXTEND:
793 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000794 // NOTE: Any extend would work here...
795 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
796 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000797 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000798 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000799 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000800 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000801 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000802 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000803 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
804 Result, Node->getOperand(0).getValueType());
805 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000806 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000807 Result = PromoteOp(Node->getOperand(0));
808 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
809 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000810 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000811 Result = PromoteOp(Node->getOperand(0));
812 if (Result.getValueType() != Op.getValueType())
813 // Dynamically dead while we have only 2 FP types.
814 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
815 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000816 case ISD::FP_ROUND:
817 case ISD::FP_TO_SINT:
818 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000819 Result = PromoteOp(Node->getOperand(0));
820 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
821 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000822 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000823 Result = PromoteOp(Node->getOperand(0));
824 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
825 Result, Node->getOperand(0).getValueType());
826 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
827 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000828 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000829 Result = PromoteOp(Node->getOperand(0));
830 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
831 Result, Node->getOperand(0).getValueType());
832 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
833 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000834 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000835 }
836 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000837 case ISD::FP_ROUND_INREG:
838 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000839 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000840 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000841 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
842
843 // If this operation is not supported, convert it to a shl/shr or load/store
844 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000845 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
846 default: assert(0 && "This action not supported for this op yet!");
847 case TargetLowering::Legal:
848 if (Tmp1 != Node->getOperand(0))
849 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
850 ExtraVT);
851 break;
852 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000853 // If this is an integer extend and shifts are supported, do that.
854 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
855 // NOTE: we could fall back on load/store here too for targets without
856 // AND. However, it is doubtful that any exist.
857 // AND out the appropriate bits.
858 SDOperand Mask =
859 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
860 Node->getValueType(0));
861 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
862 Node->getOperand(0), Mask);
863 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
864 // NOTE: we could fall back on load/store here too for targets without
865 // SAR. However, it is doubtful that any exist.
866 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
867 MVT::getSizeInBits(ExtraVT);
868 SDOperand ShiftCst = DAG.getConstant(BitsDiff, MVT::i8);
869 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
870 Node->getOperand(0), ShiftCst);
871 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
872 Result, ShiftCst);
873 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
874 // The only way we can lower this is to turn it into a STORETRUNC,
875 // EXTLOAD pair, targetting a temporary location (a stack slot).
876
877 // NOTE: there is a choice here between constantly creating new stack
878 // slots and always reusing the same one. We currently always create
879 // new ones, as reuse may inhibit scheduling.
880 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
881 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
882 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
883 MachineFunction &MF = DAG.getMachineFunction();
884 int SSFI =
885 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
886 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
887 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
888 Node->getOperand(0), StackSlot, ExtraVT);
889 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
890 Result, StackSlot, ExtraVT);
891 } else {
892 assert(0 && "Unknown op");
893 }
894 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000895 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000896 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000897 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000898 }
Chris Lattner99222f72005-01-15 07:15:18 +0000899 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000900
Chris Lattnerea4ca942005-01-07 22:28:47 +0000901 if (!Op.Val->hasOneUse())
902 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000903
904 return Result;
905}
906
Chris Lattner4d978642005-01-15 22:16:26 +0000907/// PromoteOp - Given an operation that produces a value in an invalid type,
908/// promote it to compute the value into a larger type. The produced value will
909/// have the correct bits for the low portion of the register, but no guarantee
910/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000911SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
912 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000913 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000914 assert(getTypeAction(VT) == Promote &&
915 "Caller should expand or legalize operands that are not promotable!");
916 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
917 "Cannot promote to smaller type!");
918
919 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
920 if (I != PromotedNodes.end()) return I->second;
921
922 SDOperand Tmp1, Tmp2, Tmp3;
923
924 SDOperand Result;
925 SDNode *Node = Op.Val;
926
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000927 // Promotion needs an optimization step to clean up after it, and is not
928 // careful to avoid operations the target does not support. Make sure that
929 // all generated operations are legalized in the next iteration.
930 NeedsAnotherIteration = true;
931
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000932 switch (Node->getOpcode()) {
933 default:
934 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
935 assert(0 && "Do not know how to promote this operator!");
936 abort();
937 case ISD::Constant:
938 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
939 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
940 break;
941 case ISD::ConstantFP:
942 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
943 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
944 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000945 case ISD::CopyFromReg:
946 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
947 Node->getOperand(0));
948 // Remember that we legalized the chain.
949 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
950 break;
951
Chris Lattner2cb338d2005-01-18 02:59:52 +0000952 case ISD::SETCC:
953 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
954 "SetCC type is not legal??");
955 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
956 TLI.getSetCCResultTy(), Node->getOperand(0),
957 Node->getOperand(1));
958 Result = LegalizeOp(Result);
959 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000960
961 case ISD::TRUNCATE:
962 switch (getTypeAction(Node->getOperand(0).getValueType())) {
963 case Legal:
964 Result = LegalizeOp(Node->getOperand(0));
965 assert(Result.getValueType() >= NVT &&
966 "This truncation doesn't make sense!");
967 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
968 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
969 break;
970 case Expand:
971 assert(0 && "Cannot handle expand yet");
972 case Promote:
973 assert(0 && "Cannot handle promote-promote yet");
974 }
975 break;
Chris Lattner4d978642005-01-15 22:16:26 +0000976 case ISD::SIGN_EXTEND:
977 case ISD::ZERO_EXTEND:
978 switch (getTypeAction(Node->getOperand(0).getValueType())) {
979 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
980 case Legal:
981 // Input is legal? Just do extend all the way to the larger type.
982 Result = LegalizeOp(Node->getOperand(0));
983 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
984 break;
985 case Promote:
986 // Promote the reg if it's smaller.
987 Result = PromoteOp(Node->getOperand(0));
988 // The high bits are not guaranteed to be anything. Insert an extend.
989 if (Node->getOpcode() == ISD::SIGN_EXTEND)
990 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
991 else
992 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
993 break;
994 }
995 break;
996
997 case ISD::FP_EXTEND:
998 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
999 case ISD::FP_ROUND:
1000 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1001 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1002 case Promote: assert(0 && "Unreachable with 2 FP types!");
1003 case Legal:
1004 // Input is legal? Do an FP_ROUND_INREG.
1005 Result = LegalizeOp(Node->getOperand(0));
1006 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1007 break;
1008 }
1009 break;
1010
1011 case ISD::SINT_TO_FP:
1012 case ISD::UINT_TO_FP:
1013 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1014 case Legal:
1015 Result = LegalizeOp(Node->getOperand(0));
1016 break;
1017
1018 case Promote:
1019 Result = PromoteOp(Node->getOperand(0));
1020 if (Node->getOpcode() == ISD::SINT_TO_FP)
1021 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1022 Result, Node->getOperand(0).getValueType());
1023 else
1024 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1025 Result, Node->getOperand(0).getValueType());
1026 break;
1027 case Expand:
1028 assert(0 && "Unimplemented");
1029 }
1030 // No extra round required here.
1031 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1032 break;
1033
1034 case ISD::FP_TO_SINT:
1035 case ISD::FP_TO_UINT:
1036 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1037 case Legal:
1038 Tmp1 = LegalizeOp(Node->getOperand(0));
1039 break;
1040 case Promote:
1041 // The input result is prerounded, so we don't have to do anything
1042 // special.
1043 Tmp1 = PromoteOp(Node->getOperand(0));
1044 break;
1045 case Expand:
1046 assert(0 && "not implemented");
1047 }
1048 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1049 break;
1050
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001051 case ISD::AND:
1052 case ISD::OR:
1053 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001054 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001055 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001056 case ISD::MUL:
1057 // The input may have strange things in the top bits of the registers, but
1058 // these operations don't care. They may have wierd bits going out, but
1059 // that too is okay if they are integer operations.
1060 Tmp1 = PromoteOp(Node->getOperand(0));
1061 Tmp2 = PromoteOp(Node->getOperand(1));
1062 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1063 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1064
1065 // However, if this is a floating point operation, they will give excess
1066 // precision that we may not be able to tolerate. If we DO allow excess
1067 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001068 // FIXME: Why would we need to round FP ops more than integer ones?
1069 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001070 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1071 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1072 break;
1073
Chris Lattner4d978642005-01-15 22:16:26 +00001074 case ISD::SDIV:
1075 case ISD::SREM:
1076 // These operators require that their input be sign extended.
1077 Tmp1 = PromoteOp(Node->getOperand(0));
1078 Tmp2 = PromoteOp(Node->getOperand(1));
1079 if (MVT::isInteger(NVT)) {
1080 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001081 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001082 }
1083 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1084
1085 // Perform FP_ROUND: this is probably overly pessimistic.
1086 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1087 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1088 break;
1089
1090 case ISD::UDIV:
1091 case ISD::UREM:
1092 // These operators require that their input be zero extended.
1093 Tmp1 = PromoteOp(Node->getOperand(0));
1094 Tmp2 = PromoteOp(Node->getOperand(1));
1095 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1096 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001097 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001098 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1099 break;
1100
1101 case ISD::SHL:
1102 Tmp1 = PromoteOp(Node->getOperand(0));
1103 Tmp2 = LegalizeOp(Node->getOperand(1));
1104 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1105 break;
1106 case ISD::SRA:
1107 // The input value must be properly sign extended.
1108 Tmp1 = PromoteOp(Node->getOperand(0));
1109 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1110 Tmp2 = LegalizeOp(Node->getOperand(1));
1111 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1112 break;
1113 case ISD::SRL:
1114 // The input value must be properly zero extended.
1115 Tmp1 = PromoteOp(Node->getOperand(0));
1116 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1117 Tmp2 = LegalizeOp(Node->getOperand(1));
1118 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1119 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001120 case ISD::LOAD:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1123 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1124
1125 // Remember that we legalized the chain.
1126 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1127 break;
1128 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001129 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1130 case Expand: assert(0 && "It's impossible to expand bools");
1131 case Legal:
1132 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1133 break;
1134 case Promote:
1135 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1136 break;
1137 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001138 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1139 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1140 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1141 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001142 case ISD::CALL: {
1143 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1144 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1145
1146 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1147 "Can only promote single result calls");
1148 std::vector<MVT::ValueType> RetTyVTs;
1149 RetTyVTs.reserve(2);
1150 RetTyVTs.push_back(NVT);
1151 RetTyVTs.push_back(MVT::Other);
1152 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2);
1153 Result = SDOperand(NC, 0);
1154
1155 // Insert the new chain mapping.
1156 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1157 break;
1158 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001159 }
1160
1161 assert(Result.Val && "Didn't set a result!");
1162 AddPromotedOperand(Op, Result);
1163 return Result;
1164}
Chris Lattnerdc750592005-01-07 07:47:09 +00001165
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001166/// ExpandShift - Try to find a clever way to expand this shift operation out to
1167/// smaller elements. If we can't find a way that is more efficient than a
1168/// libcall on this target, return false. Otherwise, return true with the
1169/// low-parts expanded into Lo and Hi.
1170bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1171 SDOperand &Lo, SDOperand &Hi) {
1172 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1173 "This is not a shift!");
1174 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1175
1176 // If we have an efficient select operation (or if the selects will all fold
1177 // away), lower to some complex code, otherwise just emit the libcall.
1178 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1179 !isa<ConstantSDNode>(Amt))
1180 return false;
1181
1182 SDOperand InL, InH;
1183 ExpandOp(Op, InL, InH);
1184 SDOperand ShAmt = LegalizeOp(Amt);
1185 SDOperand OShAmt = ShAmt; // Unmasked shift amount.
1186 MVT::ValueType ShTy = ShAmt.getValueType();
1187
1188 unsigned NVTBits = MVT::getSizeInBits(NVT);
1189 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1190 DAG.getConstant(NVTBits, ShTy), ShAmt);
1191
1192 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1193 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1194 DAG.getConstant(NVTBits-1, ShTy));
1195 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1196 DAG.getConstant(NVTBits-1, ShTy));
1197 }
1198
1199 if (Opc == ISD::SHL) {
1200 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1201 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1202 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
1203 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt
1204
1205 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), OShAmt,
1206 DAG.getConstant(NVTBits, ShTy));
1207 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1208 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1209 } else {
1210 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
1211 DAG.getNode(ISD::SHL, NVT, InH, NAmt),
1212 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
1213 bool isSign = Opc == ISD::SRA;
1214 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt);
1215
1216 SDOperand HiPart;
1217 if (isSign)
1218 HiPart = DAG.getNode(Opc, NVT, InH, DAG.getConstant(NVTBits-1, ShTy));
1219 else
1220 HiPart = DAG.getConstant(0, NVT);
1221 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), OShAmt,
1222 DAG.getConstant(NVTBits, ShTy));
1223 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1224 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart,T2);
1225 }
1226 return true;
1227}
1228
1229
1230
Chris Lattnerdc750592005-01-07 07:47:09 +00001231/// ExpandOp - Expand the specified SDOperand into its two component pieces
1232/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1233/// LegalizeNodes map is filled in for any results that are not expanded, the
1234/// ExpandedNodes map is filled in for any results that are expanded, and the
1235/// Lo/Hi values are returned.
1236void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1237 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001238 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001239 SDNode *Node = Op.Val;
1240 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1241 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1242 assert(MVT::isInteger(NVT) && NVT < VT &&
1243 "Cannot expand to FP value or to larger int value!");
1244
1245 // If there is more than one use of this, see if we already expanded it.
1246 // There is no use remembering values that only have a single use, as the map
1247 // entries will never be reused.
1248 if (!Node->hasOneUse()) {
1249 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1250 = ExpandedNodes.find(Op);
1251 if (I != ExpandedNodes.end()) {
1252 Lo = I->second.first;
1253 Hi = I->second.second;
1254 return;
1255 }
1256 }
1257
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001258 // Expanding to multiple registers needs to perform an optimization step, and
1259 // is not careful to avoid operations the target does not support. Make sure
1260 // that all generated operations are legalized in the next iteration.
1261 NeedsAnotherIteration = true;
1262 const char *LibCallName = 0;
Chris Lattnerdc750592005-01-07 07:47:09 +00001263
Chris Lattnerdc750592005-01-07 07:47:09 +00001264 switch (Node->getOpcode()) {
1265 default:
1266 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1267 assert(0 && "Do not know how to expand this operator!");
1268 abort();
1269 case ISD::Constant: {
1270 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1271 Lo = DAG.getConstant(Cst, NVT);
1272 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1273 break;
1274 }
1275
1276 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001277 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001278 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001279 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1280 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1281
1282 // Remember that we legalized the chain.
1283 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1284
Chris Lattnerdc750592005-01-07 07:47:09 +00001285 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1286 break;
1287 }
1288
1289 case ISD::LOAD: {
1290 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1291 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1292 Lo = DAG.getLoad(NVT, Ch, Ptr);
1293
1294 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001295 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001296 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1297 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001298 Hi = DAG.getLoad(NVT, Ch, Ptr);
1299
1300 // Build a factor node to remember that this load is independent of the
1301 // other one.
1302 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1303 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001304
1305 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001306 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001307 if (!TLI.isLittleEndian())
1308 std::swap(Lo, Hi);
1309 break;
1310 }
1311 case ISD::CALL: {
1312 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1313 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1314
1315 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1316 "Can only expand a call once so far, not i64 -> i16!");
1317
1318 std::vector<MVT::ValueType> RetTyVTs;
1319 RetTyVTs.reserve(3);
1320 RetTyVTs.push_back(NVT);
1321 RetTyVTs.push_back(NVT);
1322 RetTyVTs.push_back(MVT::Other);
1323 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee);
1324 Lo = SDOperand(NC, 0);
1325 Hi = SDOperand(NC, 1);
1326
1327 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001328 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001329 break;
1330 }
1331 case ISD::AND:
1332 case ISD::OR:
1333 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1334 SDOperand LL, LH, RL, RH;
1335 ExpandOp(Node->getOperand(0), LL, LH);
1336 ExpandOp(Node->getOperand(1), RL, RH);
1337 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1338 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1339 break;
1340 }
1341 case ISD::SELECT: {
1342 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001343
1344 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1345 case Expand: assert(0 && "It's impossible to expand bools");
1346 case Legal:
1347 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1348 break;
1349 case Promote:
1350 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1351 break;
1352 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001353 ExpandOp(Node->getOperand(1), LL, LH);
1354 ExpandOp(Node->getOperand(2), RL, RH);
1355 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1356 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1357 break;
1358 }
1359 case ISD::SIGN_EXTEND: {
1360 // The low part is just a sign extension of the input (which degenerates to
1361 // a copy).
1362 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1363
1364 // The high part is obtained by SRA'ing all but one of the bits of the lo
1365 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001366 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
1367 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8));
Chris Lattnerdc750592005-01-07 07:47:09 +00001368 break;
1369 }
1370 case ISD::ZERO_EXTEND:
1371 // The low part is just a zero extension of the input (which degenerates to
1372 // a copy).
1373 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1374
1375 // The high part is just a zero.
1376 Hi = DAG.getConstant(0, NVT);
1377 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001378
1379 // These operators cannot be expanded directly, emit them as calls to
1380 // library functions.
1381 case ISD::FP_TO_SINT:
1382 if (Node->getOperand(0).getValueType() == MVT::f32)
1383 LibCallName = "__fixsfdi";
1384 else
1385 LibCallName = "__fixdfdi";
1386 break;
1387 case ISD::FP_TO_UINT:
1388 if (Node->getOperand(0).getValueType() == MVT::f32)
1389 LibCallName = "__fixunssfdi";
1390 else
1391 LibCallName = "__fixunsdfdi";
1392 break;
1393
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001394 case ISD::SHL:
1395 // If we can emit an efficient shift operation, do so now.
1396 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1),
1397 Lo, Hi))
1398 break;
1399 // Otherwise, emit a libcall.
1400 LibCallName = "__ashldi3";
1401 break;
1402
1403 case ISD::SRA:
1404 // If we can emit an efficient shift operation, do so now.
1405 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1),
1406 Lo, Hi))
1407 break;
1408 // Otherwise, emit a libcall.
1409 LibCallName = "__ashrdi3";
1410 break;
1411 case ISD::SRL:
1412 // If we can emit an efficient shift operation, do so now.
1413 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1),
1414 Lo, Hi))
1415 break;
1416 // Otherwise, emit a libcall.
1417 LibCallName = "__lshrdi3";
1418 break;
1419
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001420 case ISD::ADD: LibCallName = "__adddi3"; break;
1421 case ISD::SUB: LibCallName = "__subdi3"; break;
1422 case ISD::MUL: LibCallName = "__muldi3"; break;
1423 case ISD::SDIV: LibCallName = "__divdi3"; break;
1424 case ISD::UDIV: LibCallName = "__udivdi3"; break;
1425 case ISD::SREM: LibCallName = "__moddi3"; break;
1426 case ISD::UREM: LibCallName = "__umoddi3"; break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001427 }
1428
1429 // Int2FP -> __floatdisf/__floatdidf
1430
1431 // If this is to be expanded into a libcall... do so now.
1432 if (LibCallName) {
1433 TargetLowering::ArgListTy Args;
1434 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1435 Args.push_back(std::make_pair(Node->getOperand(i),
Chris Lattner99222f72005-01-15 07:15:18 +00001436 MVT::getTypeForValueType(Node->getOperand(i).getValueType())));
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001437 SDOperand Callee = DAG.getExternalSymbol(LibCallName, TLI.getPointerTy());
1438
1439 // We don't care about token chains for libcalls. We just use the entry
1440 // node as our input and ignore the output chain. This allows us to place
1441 // calls wherever we need them to satisfy data dependences.
1442 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(),
Chris Lattner99222f72005-01-15 07:15:18 +00001443 MVT::getTypeForValueType(Op.getValueType()), Callee,
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001444 Args, DAG).first;
1445 ExpandOp(Result, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +00001446 }
1447
1448 // Remember in a map if the values will be reused later.
1449 if (!Node->hasOneUse()) {
1450 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1451 std::make_pair(Lo, Hi))).second;
1452 assert(isNew && "Value already expanded?!?");
1453 }
1454}
1455
1456
1457// SelectionDAG::Legalize - This is the entry point for the file.
1458//
1459void SelectionDAG::Legalize(TargetLowering &TLI) {
1460 /// run - This is the main entry point to this class.
1461 ///
1462 SelectionDAGLegalize(TLI, *this).Run();
1463}
1464