blob: 3bd2494ceaaa502a8eede32dc8c06a6881839e63 [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
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000089
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 Lattneraac464e2005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner4add7e32005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000145}
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattnerdc750592005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattner85d70c62005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000187
Chris Lattnerec26b482005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000223 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
288
289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
293 MVT::f32);
294 } else {
295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
296 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000297 }
298 break;
299 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
307 Changed = true;
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
310 } else {
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
313 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000314 }
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 Lattner3d95c142005-01-19 20:24:35 +0000345 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000348
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
354 }
355
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000360 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000362 } else {
363 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000364 }
Chris Lattner9242c502005-01-09 19:43:23 +0000365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000370 }
Chris Lattner68a12142005-01-07 22:12:08 +0000371 case ISD::BR:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
375 break;
376
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000377 case ISD::BRCOND:
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000379
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
382 case Legal:
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
384 break;
385 case Promote:
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
387 break;
388 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
393 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000394 case ISD::BRCONDTWOWAY:
395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
397 case Expand: assert(0 && "It's impossible to expand bools");
398 case Legal:
399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
400 break;
401 case Promote:
402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
403 break;
404 }
405 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
406 // pair.
407 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
408 case TargetLowering::Promote:
409 default: assert(0 && "This action is not supported yet!");
410 case TargetLowering::Legal:
411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
412 std::vector<SDOperand> Ops;
413 Ops.push_back(Tmp1);
414 Ops.push_back(Tmp2);
415 Ops.push_back(Node->getOperand(2));
416 Ops.push_back(Node->getOperand(3));
417 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
418 }
419 break;
420 case TargetLowering::Expand:
421 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
422 Node->getOperand(2));
423 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
424 break;
425 }
426 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000427
Chris Lattnerdc750592005-01-07 07:47:09 +0000428 case ISD::LOAD:
429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
431 if (Tmp1 != Node->getOperand(0) ||
432 Tmp2 != Node->getOperand(1))
433 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000434 else
435 Result = SDOperand(Node, 0);
436
437 // Since loads produce two values, make sure to remember that we legalized
438 // both of them.
439 AddLegalizedOperand(SDOperand(Node, 0), Result);
440 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
441 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000442
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000443 case ISD::EXTLOAD:
444 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000445 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000448
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000449 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
450 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
451 case TargetLowering::Promote:
452 default: assert(0 && "This action is not supported yet!");
453 case TargetLowering::Legal:
454 if (Tmp1 != Node->getOperand(0) ||
455 Tmp2 != Node->getOperand(1))
456 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
457 Tmp1, Tmp2, SrcVT);
458 else
459 Result = SDOperand(Node, 0);
460
461 // Since loads produce two values, make sure to remember that we legalized
462 // both of them.
463 AddLegalizedOperand(SDOperand(Node, 0), Result);
464 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
465 return Result.getValue(Op.ResNo);
466 break;
467 case TargetLowering::Expand:
468 assert(Node->getOpcode() != ISD::EXTLOAD &&
469 "EXTLOAD should always be supported!");
470 // Turn the unsupported load into an EXTLOAD followed by an explicit
471 // zero/sign extend inreg.
472 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
473 Tmp1, Tmp2, SrcVT);
474 unsigned ExtOp = Node->getOpcode() == ISD::SEXTLOAD ?
475 ISD::SIGN_EXTEND_INREG : ISD::ZERO_EXTEND_INREG;
476 SDOperand ValRes = DAG.getNode(ExtOp, Result.getValueType(),
477 Result, SrcVT);
478 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
479 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
480 if (Op.ResNo)
481 return Result.getValue(1);
482 return ValRes;
483 }
484 assert(0 && "Unreachable");
485 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000486 case ISD::EXTRACT_ELEMENT:
487 // Get both the low and high parts.
488 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
489 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
490 Result = Tmp2; // 1 -> Hi
491 else
492 Result = Tmp1; // 0 -> Lo
493 break;
494
495 case ISD::CopyToReg:
496 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
497
498 switch (getTypeAction(Node->getOperand(1).getValueType())) {
499 case Legal:
500 // Legalize the incoming value (must be legal).
501 Tmp2 = LegalizeOp(Node->getOperand(1));
502 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000503 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000504 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000505 case Promote:
506 Tmp2 = PromoteOp(Node->getOperand(1));
507 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
508 break;
509 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000510 SDOperand Lo, Hi;
511 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000512 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000513 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
514 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
515 // Note that the copytoreg nodes are independent of each other.
516 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000517 assert(isTypeLegal(Result.getValueType()) &&
518 "Cannot expand multiple times yet (i64 -> i16)");
519 break;
520 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000521 break;
522
523 case ISD::RET:
524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
525 switch (Node->getNumOperands()) {
526 case 2: // ret val
527 switch (getTypeAction(Node->getOperand(1).getValueType())) {
528 case Legal:
529 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000530 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000531 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
532 break;
533 case Expand: {
534 SDOperand Lo, Hi;
535 ExpandOp(Node->getOperand(1), Lo, Hi);
536 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
537 break;
538 }
539 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000540 Tmp2 = PromoteOp(Node->getOperand(1));
541 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
542 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000543 }
544 break;
545 case 1: // ret void
546 if (Tmp1 != Node->getOperand(0))
547 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
548 break;
549 default: { // ret <values>
550 std::vector<SDOperand> NewValues;
551 NewValues.push_back(Tmp1);
552 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
553 switch (getTypeAction(Node->getOperand(i).getValueType())) {
554 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000555 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000556 break;
557 case Expand: {
558 SDOperand Lo, Hi;
559 ExpandOp(Node->getOperand(i), Lo, Hi);
560 NewValues.push_back(Lo);
561 NewValues.push_back(Hi);
562 break;
563 }
564 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000565 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000566 }
567 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
568 break;
569 }
570 }
571 break;
572 case ISD::STORE:
573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
574 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
575
Chris Lattnere69daaf2005-01-08 06:25:56 +0000576 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000577 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000578 if (CFP->getValueType(0) == MVT::f32) {
579 union {
580 unsigned I;
581 float F;
582 } V;
583 V.F = CFP->getValue();
584 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
585 DAG.getConstant(V.I, MVT::i32), Tmp2);
586 } else {
587 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
588 union {
589 uint64_t I;
590 double F;
591 } V;
592 V.F = CFP->getValue();
593 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
594 DAG.getConstant(V.I, MVT::i64), Tmp2);
595 }
Chris Lattnera4743132005-02-22 07:23:39 +0000596 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000597 }
598
Chris Lattnerdc750592005-01-07 07:47:09 +0000599 switch (getTypeAction(Node->getOperand(1).getValueType())) {
600 case Legal: {
601 SDOperand Val = LegalizeOp(Node->getOperand(1));
602 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
603 Tmp2 != Node->getOperand(2))
604 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
605 break;
606 }
607 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000608 // Truncate the value and store the result.
609 Tmp3 = PromoteOp(Node->getOperand(1));
610 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
611 Node->getOperand(1).getValueType());
612 break;
613
Chris Lattnerdc750592005-01-07 07:47:09 +0000614 case Expand:
615 SDOperand Lo, Hi;
616 ExpandOp(Node->getOperand(1), Lo, Hi);
617
618 if (!TLI.isLittleEndian())
619 std::swap(Lo, Hi);
620
Chris Lattner0d03eb42005-01-19 18:02:17 +0000621 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000622
Chris Lattner0d03eb42005-01-19 18:02:17 +0000623 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000624 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
625 getIntPtrConstant(IncrementSize));
626 assert(isTypeLegal(Tmp2.getValueType()) &&
627 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000628 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
629 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
630 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000631 }
632 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000633 case ISD::PCMARKER:
634 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000635 if (Tmp1 != Node->getOperand(0))
636 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000637 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000638 case ISD::TRUNCSTORE:
639 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
640 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
641
642 switch (getTypeAction(Node->getOperand(1).getValueType())) {
643 case Legal:
644 Tmp2 = LegalizeOp(Node->getOperand(1));
645 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
646 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000647 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000648 cast<MVTSDNode>(Node)->getExtraValueType());
649 break;
650 case Promote:
651 case Expand:
652 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
653 }
654 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000655 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000656 switch (getTypeAction(Node->getOperand(0).getValueType())) {
657 case Expand: assert(0 && "It's impossible to expand bools");
658 case Legal:
659 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
660 break;
661 case Promote:
662 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
663 break;
664 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000665 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000666 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000667
668 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
669 default: assert(0 && "This action is not supported yet!");
670 case TargetLowering::Legal:
671 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
672 Tmp3 != Node->getOperand(2))
673 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
674 Tmp1, Tmp2, Tmp3);
675 break;
676 case TargetLowering::Promote: {
677 MVT::ValueType NVT =
678 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
679 unsigned ExtOp, TruncOp;
680 if (MVT::isInteger(Tmp2.getValueType())) {
681 ExtOp = ISD::ZERO_EXTEND;
682 TruncOp = ISD::TRUNCATE;
683 } else {
684 ExtOp = ISD::FP_EXTEND;
685 TruncOp = ISD::FP_ROUND;
686 }
687 // Promote each of the values to the new type.
688 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
689 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
690 // Perform the larger operation, then round down.
691 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
692 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
693 break;
694 }
695 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000696 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000697 case ISD::SETCC:
698 switch (getTypeAction(Node->getOperand(0).getValueType())) {
699 case Legal:
700 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
701 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
702 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
703 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000704 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000705 break;
706 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000707 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
708 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
709
710 // If this is an FP compare, the operands have already been extended.
711 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
712 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000713 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000714
715 // Otherwise, we have to insert explicit sign or zero extends. Note
716 // that we could insert sign extends for ALL conditions, but zero extend
717 // is cheaper on many machines (an AND instead of two shifts), so prefer
718 // it.
719 switch (cast<SetCCSDNode>(Node)->getCondition()) {
720 default: assert(0 && "Unknown integer comparison!");
721 case ISD::SETEQ:
722 case ISD::SETNE:
723 case ISD::SETUGE:
724 case ISD::SETUGT:
725 case ISD::SETULE:
726 case ISD::SETULT:
727 // ALL of these operations will work if we either sign or zero extend
728 // the operands (including the unsigned comparisons!). Zero extend is
729 // usually a simpler/cheaper operation, so prefer it.
730 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
731 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
732 break;
733 case ISD::SETGE:
734 case ISD::SETGT:
735 case ISD::SETLT:
736 case ISD::SETLE:
737 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
738 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
739 break;
740 }
741
742 }
743 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000744 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000745 break;
746 case Expand:
747 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
748 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
749 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
750 switch (cast<SetCCSDNode>(Node)->getCondition()) {
751 case ISD::SETEQ:
752 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000753 if (RHSLo == RHSHi)
754 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
755 if (RHSCST->isAllOnesValue()) {
756 // Comparison to -1.
757 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
758 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
759 Node->getValueType(0), Tmp1, RHSLo);
760 break;
761 }
762
Chris Lattnerdc750592005-01-07 07:47:09 +0000763 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
764 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
765 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000766 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
767 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000768 DAG.getConstant(0, Tmp1.getValueType()));
769 break;
770 default:
771 // FIXME: This generated code sucks.
772 ISD::CondCode LowCC;
773 switch (cast<SetCCSDNode>(Node)->getCondition()) {
774 default: assert(0 && "Unknown integer setcc!");
775 case ISD::SETLT:
776 case ISD::SETULT: LowCC = ISD::SETULT; break;
777 case ISD::SETGT:
778 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
779 case ISD::SETLE:
780 case ISD::SETULE: LowCC = ISD::SETULE; break;
781 case ISD::SETGE:
782 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
783 }
784
785 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
786 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
787 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
788
789 // NOTE: on targets without efficient SELECT of bools, we can always use
790 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000791 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000792 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000793 Node->getValueType(0), LHSHi, RHSHi);
794 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
795 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
796 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000797 break;
798 }
799 }
800 break;
801
Chris Lattner85d70c62005-01-11 05:57:22 +0000802 case ISD::MEMSET:
803 case ISD::MEMCPY:
804 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000805 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000806 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
807
808 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
809 switch (getTypeAction(Node->getOperand(2).getValueType())) {
810 case Expand: assert(0 && "Cannot expand a byte!");
811 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000812 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000813 break;
814 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000815 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000816 break;
817 }
818 } else {
819 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
820 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000821
822 SDOperand Tmp4;
823 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000824 case Expand: assert(0 && "Cannot expand this yet!");
825 case Legal:
826 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000827 break;
828 case Promote:
829 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000830 break;
831 }
832
833 SDOperand Tmp5;
834 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
835 case Expand: assert(0 && "Cannot expand this yet!");
836 case Legal:
837 Tmp5 = LegalizeOp(Node->getOperand(4));
838 break;
839 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000840 Tmp5 = PromoteOp(Node->getOperand(4));
841 break;
842 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000843
844 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
845 default: assert(0 && "This action not implemented for this operation!");
846 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000847 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
848 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
849 Tmp5 != Node->getOperand(4)) {
850 std::vector<SDOperand> Ops;
851 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
852 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
853 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
854 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000855 break;
856 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000857 // Otherwise, the target does not support this operation. Lower the
858 // operation to an explicit libcall as appropriate.
859 MVT::ValueType IntPtr = TLI.getPointerTy();
860 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
861 std::vector<std::pair<SDOperand, const Type*> > Args;
862
Reid Spencer6dced922005-01-12 14:53:45 +0000863 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000864 if (Node->getOpcode() == ISD::MEMSET) {
865 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
866 // Extend the ubyte argument to be an int value for the call.
867 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
868 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
869 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
870
871 FnName = "memset";
872 } else if (Node->getOpcode() == ISD::MEMCPY ||
873 Node->getOpcode() == ISD::MEMMOVE) {
874 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
875 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
876 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
877 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
878 } else {
879 assert(0 && "Unknown op!");
880 }
881 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000882 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000883 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
884 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000885 break;
886 }
887 case TargetLowering::Custom:
888 std::vector<SDOperand> Ops;
889 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
890 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
891 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
892 Result = TLI.LowerOperation(Result);
893 Result = LegalizeOp(Result);
894 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000895 }
896 break;
897 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000898 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +0000899 case ISD::SUB_PARTS:
900 case ISD::SHL_PARTS:
901 case ISD::SRA_PARTS:
902 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000903 std::vector<SDOperand> Ops;
904 bool Changed = false;
905 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
906 Ops.push_back(LegalizeOp(Node->getOperand(i)));
907 Changed |= Ops.back() != Node->getOperand(i);
908 }
909 if (Changed)
910 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000911
912 // Since these produce multiple values, make sure to remember that we
913 // legalized all of them.
914 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
915 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
916 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000917 }
Chris Lattner13fe99c2005-04-02 05:00:07 +0000918
919 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +0000920 case ISD::ADD:
921 case ISD::SUB:
922 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +0000923 case ISD::MULHS:
924 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +0000925 case ISD::UDIV:
926 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +0000927 case ISD::AND:
928 case ISD::OR:
929 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000930 case ISD::SHL:
931 case ISD::SRL:
932 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000933 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
934 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
935 if (Tmp1 != Node->getOperand(0) ||
936 Tmp2 != Node->getOperand(1))
937 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
938 break;
Nate Begeman20b7d2a2005-04-06 00:23:54 +0000939
940 case ISD::UREM:
941 case ISD::SREM:
942 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
943 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
944 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
945 case TargetLowering::Legal:
946 if (Tmp1 != Node->getOperand(0) ||
947 Tmp2 != Node->getOperand(1))
948 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
949 Tmp2);
950 break;
951 case TargetLowering::Promote:
952 case TargetLowering::Custom:
953 assert(0 && "Cannot promote/custom handle this yet!");
954 case TargetLowering::Expand: {
955 MVT::ValueType VT = Node->getValueType(0);
956 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
957 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
958 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
959 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
960 }
961 break;
962 }
963 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +0000964
965 // Unary operators
966 case ISD::FABS:
967 case ISD::FNEG:
968 Tmp1 = LegalizeOp(Node->getOperand(0));
969 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
970 case TargetLowering::Legal:
971 if (Tmp1 != Node->getOperand(0))
972 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
973 break;
974 case TargetLowering::Promote:
975 case TargetLowering::Custom:
976 assert(0 && "Cannot promote/custom handle this yet!");
977 case TargetLowering::Expand:
978 if (Node->getOpcode() == ISD::FNEG) {
979 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
980 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
981 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
982 Tmp2, Tmp1));
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000983 } else if (Node->getOpcode() == ISD::FABS) {
984 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
985 MVT::ValueType VT = Node->getValueType(0);
986 Tmp2 = DAG.getConstantFP(0.0, VT);
987 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
988 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
989 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
990 Result = LegalizeOp(Result);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000991 } else {
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000992 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +0000993 }
994 break;
995 }
996 break;
997
998 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +0000999 case ISD::ZERO_EXTEND:
1000 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001001 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001002 case ISD::FP_EXTEND:
1003 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001004 case ISD::FP_TO_SINT:
1005 case ISD::FP_TO_UINT:
1006 case ISD::SINT_TO_FP:
1007 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001008 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1009 case Legal:
1010 Tmp1 = LegalizeOp(Node->getOperand(0));
1011 if (Tmp1 != Node->getOperand(0))
1012 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1013 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001014 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001015 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1016 Node->getOpcode() == ISD::UINT_TO_FP) {
1017 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1018 Node->getValueType(0), Node->getOperand(0));
1019 Result = LegalizeOp(Result);
1020 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001021 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1022 // In the expand case, we must be dealing with a truncate, because
1023 // otherwise the result would be larger than the source.
1024 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1025
1026 // Since the result is legal, we should just be able to truncate the low
1027 // part of the source.
1028 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1029 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001030 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001031 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001032
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001033 case Promote:
1034 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001035 case ISD::ZERO_EXTEND:
1036 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001037 // NOTE: Any extend would work here...
1038 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
1039 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001040 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001041 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001042 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001043 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001044 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001045 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001046 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1047 Result, Node->getOperand(0).getValueType());
1048 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001049 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001050 Result = PromoteOp(Node->getOperand(0));
1051 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1052 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001053 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001054 Result = PromoteOp(Node->getOperand(0));
1055 if (Result.getValueType() != Op.getValueType())
1056 // Dynamically dead while we have only 2 FP types.
1057 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1058 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001059 case ISD::FP_ROUND:
1060 case ISD::FP_TO_SINT:
1061 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001062 Result = PromoteOp(Node->getOperand(0));
1063 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1064 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001065 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001066 Result = PromoteOp(Node->getOperand(0));
1067 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1068 Result, Node->getOperand(0).getValueType());
1069 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1070 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001071 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001072 Result = PromoteOp(Node->getOperand(0));
1073 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1074 Result, Node->getOperand(0).getValueType());
1075 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1076 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001077 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001078 }
1079 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001080 case ISD::FP_ROUND_INREG:
1081 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +00001082 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001083 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001084 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1085
1086 // If this operation is not supported, convert it to a shl/shr or load/store
1087 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001088 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1089 default: assert(0 && "This action not supported for this op yet!");
1090 case TargetLowering::Legal:
1091 if (Tmp1 != Node->getOperand(0))
1092 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1093 ExtraVT);
1094 break;
1095 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001096 // If this is an integer extend and shifts are supported, do that.
1097 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
1098 // NOTE: we could fall back on load/store here too for targets without
1099 // AND. However, it is doubtful that any exist.
1100 // AND out the appropriate bits.
1101 SDOperand Mask =
1102 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
1103 Node->getValueType(0));
1104 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
1105 Node->getOperand(0), Mask);
1106 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
1107 // NOTE: we could fall back on load/store here too for targets without
1108 // SAR. However, it is doubtful that any exist.
1109 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1110 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001111 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001112 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1113 Node->getOperand(0), ShiftCst);
1114 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1115 Result, ShiftCst);
1116 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1117 // The only way we can lower this is to turn it into a STORETRUNC,
1118 // EXTLOAD pair, targetting a temporary location (a stack slot).
1119
1120 // NOTE: there is a choice here between constantly creating new stack
1121 // slots and always reusing the same one. We currently always create
1122 // new ones, as reuse may inhibit scheduling.
1123 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1124 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1125 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1126 MachineFunction &MF = DAG.getMachineFunction();
1127 int SSFI =
1128 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1129 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1130 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1131 Node->getOperand(0), StackSlot, ExtraVT);
1132 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1133 Result, StackSlot, ExtraVT);
1134 } else {
1135 assert(0 && "Unknown op");
1136 }
1137 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001138 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001139 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001140 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001141 }
Chris Lattner99222f72005-01-15 07:15:18 +00001142 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001143
Chris Lattnerea4ca942005-01-07 22:28:47 +00001144 if (!Op.Val->hasOneUse())
1145 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001146
1147 return Result;
1148}
1149
Chris Lattner4d978642005-01-15 22:16:26 +00001150/// PromoteOp - Given an operation that produces a value in an invalid type,
1151/// promote it to compute the value into a larger type. The produced value will
1152/// have the correct bits for the low portion of the register, but no guarantee
1153/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001154SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1155 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001156 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001157 assert(getTypeAction(VT) == Promote &&
1158 "Caller should expand or legalize operands that are not promotable!");
1159 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1160 "Cannot promote to smaller type!");
1161
1162 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1163 if (I != PromotedNodes.end()) return I->second;
1164
1165 SDOperand Tmp1, Tmp2, Tmp3;
1166
1167 SDOperand Result;
1168 SDNode *Node = Op.Val;
1169
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001170 // Promotion needs an optimization step to clean up after it, and is not
1171 // careful to avoid operations the target does not support. Make sure that
1172 // all generated operations are legalized in the next iteration.
1173 NeedsAnotherIteration = true;
1174
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001175 switch (Node->getOpcode()) {
1176 default:
1177 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1178 assert(0 && "Do not know how to promote this operator!");
1179 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001180 case ISD::UNDEF:
1181 Result = DAG.getNode(ISD::UNDEF, NVT);
1182 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001183 case ISD::Constant:
1184 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1185 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1186 break;
1187 case ISD::ConstantFP:
1188 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1189 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1190 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001191 case ISD::CopyFromReg:
1192 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1193 Node->getOperand(0));
1194 // Remember that we legalized the chain.
1195 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1196 break;
1197
Chris Lattner2cb338d2005-01-18 02:59:52 +00001198 case ISD::SETCC:
1199 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1200 "SetCC type is not legal??");
1201 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1202 TLI.getSetCCResultTy(), Node->getOperand(0),
1203 Node->getOperand(1));
1204 Result = LegalizeOp(Result);
1205 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001206
1207 case ISD::TRUNCATE:
1208 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1209 case Legal:
1210 Result = LegalizeOp(Node->getOperand(0));
1211 assert(Result.getValueType() >= NVT &&
1212 "This truncation doesn't make sense!");
1213 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1214 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1215 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001216 case Promote:
1217 // The truncation is not required, because we don't guarantee anything
1218 // about high bits anyway.
1219 Result = PromoteOp(Node->getOperand(0));
1220 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001221 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001222 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1223 // Truncate the low part of the expanded value to the result type
1224 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001225 }
1226 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001227 case ISD::SIGN_EXTEND:
1228 case ISD::ZERO_EXTEND:
1229 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1230 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1231 case Legal:
1232 // Input is legal? Just do extend all the way to the larger type.
1233 Result = LegalizeOp(Node->getOperand(0));
1234 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1235 break;
1236 case Promote:
1237 // Promote the reg if it's smaller.
1238 Result = PromoteOp(Node->getOperand(0));
1239 // The high bits are not guaranteed to be anything. Insert an extend.
1240 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001241 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1242 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001243 else
Chris Lattner05596912005-02-04 18:39:19 +00001244 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1245 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001246 break;
1247 }
1248 break;
1249
1250 case ISD::FP_EXTEND:
1251 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1252 case ISD::FP_ROUND:
1253 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1254 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1255 case Promote: assert(0 && "Unreachable with 2 FP types!");
1256 case Legal:
1257 // Input is legal? Do an FP_ROUND_INREG.
1258 Result = LegalizeOp(Node->getOperand(0));
1259 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1260 break;
1261 }
1262 break;
1263
1264 case ISD::SINT_TO_FP:
1265 case ISD::UINT_TO_FP:
1266 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1267 case Legal:
1268 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001269 // No extra round required here.
1270 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001271 break;
1272
1273 case Promote:
1274 Result = PromoteOp(Node->getOperand(0));
1275 if (Node->getOpcode() == ISD::SINT_TO_FP)
1276 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1277 Result, Node->getOperand(0).getValueType());
1278 else
1279 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1280 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001281 // No extra round required here.
1282 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001283 break;
1284 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001285 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1286 Node->getOperand(0));
1287 Result = LegalizeOp(Result);
1288
1289 // Round if we cannot tolerate excess precision.
1290 if (NoExcessFPPrecision)
1291 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1292 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001293 }
Chris Lattner4d978642005-01-15 22:16:26 +00001294 break;
1295
1296 case ISD::FP_TO_SINT:
1297 case ISD::FP_TO_UINT:
1298 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1299 case Legal:
1300 Tmp1 = LegalizeOp(Node->getOperand(0));
1301 break;
1302 case Promote:
1303 // The input result is prerounded, so we don't have to do anything
1304 // special.
1305 Tmp1 = PromoteOp(Node->getOperand(0));
1306 break;
1307 case Expand:
1308 assert(0 && "not implemented");
1309 }
1310 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1311 break;
1312
Chris Lattner13fe99c2005-04-02 05:00:07 +00001313 case ISD::FABS:
1314 case ISD::FNEG:
1315 Tmp1 = PromoteOp(Node->getOperand(0));
1316 assert(Tmp1.getValueType() == NVT);
1317 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1318 // NOTE: we do not have to do any extra rounding here for
1319 // NoExcessFPPrecision, because we know the input will have the appropriate
1320 // precision, and these operations don't modify precision at all.
1321 break;
1322
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001323 case ISD::AND:
1324 case ISD::OR:
1325 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001326 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001327 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001328 case ISD::MUL:
1329 // The input may have strange things in the top bits of the registers, but
1330 // these operations don't care. They may have wierd bits going out, but
1331 // that too is okay if they are integer operations.
1332 Tmp1 = PromoteOp(Node->getOperand(0));
1333 Tmp2 = PromoteOp(Node->getOperand(1));
1334 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1335 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1336
1337 // However, if this is a floating point operation, they will give excess
1338 // precision that we may not be able to tolerate. If we DO allow excess
1339 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001340 // FIXME: Why would we need to round FP ops more than integer ones?
1341 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001342 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1343 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1344 break;
1345
Chris Lattner4d978642005-01-15 22:16:26 +00001346 case ISD::SDIV:
1347 case ISD::SREM:
1348 // These operators require that their input be sign extended.
1349 Tmp1 = PromoteOp(Node->getOperand(0));
1350 Tmp2 = PromoteOp(Node->getOperand(1));
1351 if (MVT::isInteger(NVT)) {
1352 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001353 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001354 }
1355 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1356
1357 // Perform FP_ROUND: this is probably overly pessimistic.
1358 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1359 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1360 break;
1361
1362 case ISD::UDIV:
1363 case ISD::UREM:
1364 // These operators require that their input be zero extended.
1365 Tmp1 = PromoteOp(Node->getOperand(0));
1366 Tmp2 = PromoteOp(Node->getOperand(1));
1367 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1368 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001369 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001370 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1371 break;
1372
1373 case ISD::SHL:
1374 Tmp1 = PromoteOp(Node->getOperand(0));
1375 Tmp2 = LegalizeOp(Node->getOperand(1));
1376 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1377 break;
1378 case ISD::SRA:
1379 // The input value must be properly sign extended.
1380 Tmp1 = PromoteOp(Node->getOperand(0));
1381 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1382 Tmp2 = LegalizeOp(Node->getOperand(1));
1383 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1384 break;
1385 case ISD::SRL:
1386 // The input value must be properly zero extended.
1387 Tmp1 = PromoteOp(Node->getOperand(0));
1388 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1389 Tmp2 = LegalizeOp(Node->getOperand(1));
1390 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1391 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001392 case ISD::LOAD:
1393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001395 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001396 if (MVT::isInteger(NVT))
1397 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, VT);
1398 else
1399 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001400
1401 // Remember that we legalized the chain.
1402 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1403 break;
1404 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001405 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1406 case Expand: assert(0 && "It's impossible to expand bools");
1407 case Legal:
1408 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1409 break;
1410 case Promote:
1411 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1412 break;
1413 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001414 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1415 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1416 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1417 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001418 case ISD::CALL: {
1419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1420 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1421
Chris Lattner3d95c142005-01-19 20:24:35 +00001422 std::vector<SDOperand> Ops;
1423 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1424 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1425
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001426 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1427 "Can only promote single result calls");
1428 std::vector<MVT::ValueType> RetTyVTs;
1429 RetTyVTs.reserve(2);
1430 RetTyVTs.push_back(NVT);
1431 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001432 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001433 Result = SDOperand(NC, 0);
1434
1435 // Insert the new chain mapping.
1436 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1437 break;
1438 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001439 }
1440
1441 assert(Result.Val && "Didn't set a result!");
1442 AddPromotedOperand(Op, Result);
1443 return Result;
1444}
Chris Lattnerdc750592005-01-07 07:47:09 +00001445
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001446/// ExpandAddSub - Find a clever way to expand this add operation into
1447/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001448void SelectionDAGLegalize::
1449ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1450 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001451 // Expand the subcomponents.
1452 SDOperand LHSL, LHSH, RHSL, RHSH;
1453 ExpandOp(LHS, LHSL, LHSH);
1454 ExpandOp(RHS, RHSL, RHSH);
1455
Chris Lattner8ffd0042005-04-11 20:29:59 +00001456 // FIXME: this should be moved to the dag combiner someday.
1457 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1458 if (LHSL.getValueType() == MVT::i32) {
1459 SDOperand LowEl;
1460 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1461 if (C->getValue() == 0)
1462 LowEl = RHSL;
1463 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1464 if (C->getValue() == 0)
1465 LowEl = LHSL;
1466 if (LowEl.Val) {
1467 // Turn this into an add/sub of the high part only.
1468 SDOperand HiEl =
1469 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1470 LowEl.getValueType(), LHSH, RHSH);
1471 Lo = LowEl;
1472 Hi = HiEl;
1473 return;
1474 }
1475 }
1476
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001477 std::vector<SDOperand> Ops;
1478 Ops.push_back(LHSL);
1479 Ops.push_back(LHSH);
1480 Ops.push_back(RHSL);
1481 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001482 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001483 Hi = Lo.getValue(1);
1484}
1485
Chris Lattner4157c412005-04-02 04:00:59 +00001486void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1487 SDOperand Op, SDOperand Amt,
1488 SDOperand &Lo, SDOperand &Hi) {
1489 // Expand the subcomponents.
1490 SDOperand LHSL, LHSH;
1491 ExpandOp(Op, LHSL, LHSH);
1492
1493 std::vector<SDOperand> Ops;
1494 Ops.push_back(LHSL);
1495 Ops.push_back(LHSH);
1496 Ops.push_back(Amt);
1497 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1498 Hi = Lo.getValue(1);
1499}
1500
1501
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001502/// ExpandShift - Try to find a clever way to expand this shift operation out to
1503/// smaller elements. If we can't find a way that is more efficient than a
1504/// libcall on this target, return false. Otherwise, return true with the
1505/// low-parts expanded into Lo and Hi.
1506bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1507 SDOperand &Lo, SDOperand &Hi) {
1508 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1509 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001510
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001511 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001512 SDOperand ShAmt = LegalizeOp(Amt);
1513 MVT::ValueType ShTy = ShAmt.getValueType();
1514 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1515 unsigned NVTBits = MVT::getSizeInBits(NVT);
1516
1517 // Handle the case when Amt is an immediate. Other cases are currently broken
1518 // and are disabled.
1519 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1520 unsigned Cst = CN->getValue();
1521 // Expand the incoming operand to be shifted, so that we have its parts
1522 SDOperand InL, InH;
1523 ExpandOp(Op, InL, InH);
1524 switch(Opc) {
1525 case ISD::SHL:
1526 if (Cst > VTBits) {
1527 Lo = DAG.getConstant(0, NVT);
1528 Hi = DAG.getConstant(0, NVT);
1529 } else if (Cst > NVTBits) {
1530 Lo = DAG.getConstant(0, NVT);
1531 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001532 } else if (Cst == NVTBits) {
1533 Lo = DAG.getConstant(0, NVT);
1534 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001535 } else {
1536 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1537 Hi = DAG.getNode(ISD::OR, NVT,
1538 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1539 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1540 }
1541 return true;
1542 case ISD::SRL:
1543 if (Cst > VTBits) {
1544 Lo = DAG.getConstant(0, NVT);
1545 Hi = DAG.getConstant(0, NVT);
1546 } else if (Cst > NVTBits) {
1547 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1548 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001549 } else if (Cst == NVTBits) {
1550 Lo = InH;
1551 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001552 } else {
1553 Lo = DAG.getNode(ISD::OR, NVT,
1554 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1555 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1556 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1557 }
1558 return true;
1559 case ISD::SRA:
1560 if (Cst > VTBits) {
1561 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1562 DAG.getConstant(NVTBits-1, ShTy));
1563 } else if (Cst > NVTBits) {
1564 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1565 DAG.getConstant(Cst-NVTBits, ShTy));
1566 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1567 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001568 } else if (Cst == NVTBits) {
1569 Lo = InH;
1570 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1571 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001572 } else {
1573 Lo = DAG.getNode(ISD::OR, NVT,
1574 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1575 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1576 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1577 }
1578 return true;
1579 }
1580 }
1581 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1582 // so disable it for now. Currently targets are handling this via SHL_PARTS
1583 // and friends.
1584 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001585
1586 // If we have an efficient select operation (or if the selects will all fold
1587 // away), lower to some complex code, otherwise just emit the libcall.
1588 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1589 !isa<ConstantSDNode>(Amt))
1590 return false;
1591
1592 SDOperand InL, InH;
1593 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001594 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1595 DAG.getConstant(NVTBits, ShTy), ShAmt);
1596
Chris Lattner4d25c042005-01-20 20:29:23 +00001597 // Compare the unmasked shift amount against 32.
1598 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1599 DAG.getConstant(NVTBits, ShTy));
1600
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001601 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1602 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1603 DAG.getConstant(NVTBits-1, ShTy));
1604 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1605 DAG.getConstant(NVTBits-1, ShTy));
1606 }
1607
1608 if (Opc == ISD::SHL) {
1609 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1610 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1611 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001612 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001613
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001614 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1615 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1616 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001617 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1618 DAG.getSetCC(ISD::SETEQ,
1619 TLI.getSetCCResultTy(), NAmt,
1620 DAG.getConstant(32, ShTy)),
1621 DAG.getConstant(0, NVT),
1622 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001623 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001624 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001625 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001626 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001627
1628 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001629 if (Opc == ISD::SRA)
1630 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1631 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001632 else
1633 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001634 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001635 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001636 }
1637 return true;
1638}
Chris Lattneraac464e2005-01-21 06:05:23 +00001639
Chris Lattner4add7e32005-01-23 04:42:50 +00001640/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1641/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1642/// Found.
1643static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1644 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1645
1646 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1647 // than the Found node. Just remember this node and return.
1648 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1649 Found = Node;
1650 return;
1651 }
1652
1653 // Otherwise, scan the operands of Node to see if any of them is a call.
1654 assert(Node->getNumOperands() != 0 &&
1655 "All leaves should have depth equal to the entry node!");
1656 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1657 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1658
1659 // Tail recurse for the last iteration.
1660 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1661 Found);
1662}
1663
1664
1665/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1666/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1667/// than Found.
1668static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1669 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1670
1671 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1672 // than the Found node. Just remember this node and return.
1673 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1674 Found = Node;
1675 return;
1676 }
1677
1678 // Otherwise, scan the operands of Node to see if any of them is a call.
1679 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1680 if (UI == E) return;
1681 for (--E; UI != E; ++UI)
1682 FindEarliestAdjCallStackUp(*UI, Found);
1683
1684 // Tail recurse for the last iteration.
1685 FindEarliestAdjCallStackUp(*UI, Found);
1686}
1687
1688/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1689/// find the ADJCALLSTACKUP node that terminates the call sequence.
1690static SDNode *FindAdjCallStackUp(SDNode *Node) {
1691 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1692 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001693 if (Node->use_empty())
1694 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001695
1696 if (Node->hasOneUse()) // Simple case, only has one user to check.
1697 return FindAdjCallStackUp(*Node->use_begin());
1698
1699 SDOperand TheChain(Node, Node->getNumValues()-1);
1700 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1701
1702 for (SDNode::use_iterator UI = Node->use_begin(),
1703 E = Node->use_end(); ; ++UI) {
1704 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1705
1706 // Make sure to only follow users of our token chain.
1707 SDNode *User = *UI;
1708 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1709 if (User->getOperand(i) == TheChain)
1710 return FindAdjCallStackUp(User);
1711 }
1712 assert(0 && "Unreachable");
1713 abort();
1714}
1715
1716/// FindInputOutputChains - If we are replacing an operation with a call we need
1717/// to find the call that occurs before and the call that occurs after it to
1718/// properly serialize the calls in the block.
1719static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1720 SDOperand Entry) {
1721 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanadd0c632005-04-11 03:01:51 +00001722 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00001723 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1724 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Nate Begemanadd0c632005-04-11 03:01:51 +00001725
1726 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1727 // previous call in the function. LatestCallStackDown may in that case be
1728 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1729 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1730 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1731 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1732 else
1733 LatestAdjCallStackUp = Entry.Val;
1734 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
1735
Chris Lattner4add7e32005-01-23 04:42:50 +00001736 SDNode *EarliestAdjCallStackUp = 0;
1737 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1738
1739 if (EarliestAdjCallStackUp) {
1740 //std::cerr << "Found node: ";
1741 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1742 }
1743
1744 return SDOperand(LatestAdjCallStackUp, 0);
1745}
1746
1747
1748
Chris Lattneraac464e2005-01-21 06:05:23 +00001749// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1750// does not fit into a register, return the lo part and set the hi part to the
1751// by-reg argument. If it does fit into a single register, return the result
1752// and leave the Hi part unset.
1753SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1754 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001755 SDNode *OutChain;
1756 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1757 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00001758 if (InChain.Val == 0)
1759 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00001760
Chris Lattneraac464e2005-01-21 06:05:23 +00001761 TargetLowering::ArgListTy Args;
1762 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1763 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1764 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1765 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1766 }
1767 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1768
1769 // We don't care about token chains for libcalls. We just use the entry
1770 // node as our input and ignore the output chain. This allows us to place
1771 // calls wherever we need them to satisfy data dependences.
1772 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begemanf6565252005-03-26 01:29:23 +00001773 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001774 Args, DAG).first;
1775 switch (getTypeAction(Result.getValueType())) {
1776 default: assert(0 && "Unknown thing");
1777 case Legal:
1778 return Result;
1779 case Promote:
1780 assert(0 && "Cannot promote this yet!");
1781 case Expand:
1782 SDOperand Lo;
1783 ExpandOp(Result, Lo, Hi);
1784 return Lo;
1785 }
1786}
1787
Chris Lattner4add7e32005-01-23 04:42:50 +00001788
Chris Lattneraac464e2005-01-21 06:05:23 +00001789/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1790/// destination type is legal.
1791SDOperand SelectionDAGLegalize::
1792ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1793 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1794 assert(getTypeAction(Source.getValueType()) == Expand &&
1795 "This is not an expansion!");
1796 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1797
Chris Lattner4add7e32005-01-23 04:42:50 +00001798 SDNode *OutChain;
1799 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1800 DAG.getEntryNode());
1801
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001802 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001803 if (isSigned) {
1804 if (DestTy == MVT::f32)
1805 FnName = "__floatdisf";
1806 else {
1807 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1808 FnName = "__floatdidf";
1809 }
1810 } else {
1811 // If this is unsigned, and not supported, first perform the conversion to
1812 // signed, then adjust the result if the sign bit is set.
1813 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1814
1815 assert(0 && "Unsigned casts not supported yet!");
1816 }
1817 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1818
1819 TargetLowering::ArgListTy Args;
1820 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1821 Args.push_back(std::make_pair(Source, ArgTy));
1822
1823 // We don't care about token chains for libcalls. We just use the entry
1824 // node as our input and ignore the output chain. This allows us to place
1825 // calls wherever we need them to satisfy data dependences.
1826 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begemanf6565252005-03-26 01:29:23 +00001827 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner4add7e32005-01-23 04:42:50 +00001828
Chris Lattneraac464e2005-01-21 06:05:23 +00001829}
1830
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001831
1832
Chris Lattnerdc750592005-01-07 07:47:09 +00001833/// ExpandOp - Expand the specified SDOperand into its two component pieces
1834/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1835/// LegalizeNodes map is filled in for any results that are not expanded, the
1836/// ExpandedNodes map is filled in for any results that are expanded, and the
1837/// Lo/Hi values are returned.
1838void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1839 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001840 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001841 SDNode *Node = Op.Val;
1842 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1843 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1844 assert(MVT::isInteger(NVT) && NVT < VT &&
1845 "Cannot expand to FP value or to larger int value!");
1846
1847 // If there is more than one use of this, see if we already expanded it.
1848 // There is no use remembering values that only have a single use, as the map
1849 // entries will never be reused.
1850 if (!Node->hasOneUse()) {
1851 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1852 = ExpandedNodes.find(Op);
1853 if (I != ExpandedNodes.end()) {
1854 Lo = I->second.first;
1855 Hi = I->second.second;
1856 return;
1857 }
1858 }
1859
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001860 // Expanding to multiple registers needs to perform an optimization step, and
1861 // is not careful to avoid operations the target does not support. Make sure
1862 // that all generated operations are legalized in the next iteration.
1863 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001864
Chris Lattnerdc750592005-01-07 07:47:09 +00001865 switch (Node->getOpcode()) {
1866 default:
1867 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1868 assert(0 && "Do not know how to expand this operator!");
1869 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001870 case ISD::UNDEF:
1871 Lo = DAG.getNode(ISD::UNDEF, NVT);
1872 Hi = DAG.getNode(ISD::UNDEF, NVT);
1873 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001874 case ISD::Constant: {
1875 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1876 Lo = DAG.getConstant(Cst, NVT);
1877 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1878 break;
1879 }
1880
1881 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001882 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001883 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001884 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1885 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1886
1887 // Remember that we legalized the chain.
1888 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1889
Chris Lattnerdc750592005-01-07 07:47:09 +00001890 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1891 break;
1892 }
1893
Chris Lattner32e08b72005-03-28 22:03:13 +00001894 case ISD::BUILD_PAIR:
1895 // Legalize both operands. FIXME: in the future we should handle the case
1896 // where the two elements are not legal.
1897 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1898 Lo = LegalizeOp(Node->getOperand(0));
1899 Hi = LegalizeOp(Node->getOperand(1));
1900 break;
1901
Chris Lattnerdc750592005-01-07 07:47:09 +00001902 case ISD::LOAD: {
1903 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1904 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1905 Lo = DAG.getLoad(NVT, Ch, Ptr);
1906
1907 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001908 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001909 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1910 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001911 Hi = DAG.getLoad(NVT, Ch, Ptr);
1912
1913 // Build a factor node to remember that this load is independent of the
1914 // other one.
1915 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1916 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001917
1918 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001919 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001920 if (!TLI.isLittleEndian())
1921 std::swap(Lo, Hi);
1922 break;
1923 }
1924 case ISD::CALL: {
1925 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1926 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1927
Chris Lattner3d95c142005-01-19 20:24:35 +00001928 bool Changed = false;
1929 std::vector<SDOperand> Ops;
1930 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1931 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1932 Changed |= Ops.back() != Node->getOperand(i);
1933 }
1934
Chris Lattnerdc750592005-01-07 07:47:09 +00001935 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1936 "Can only expand a call once so far, not i64 -> i16!");
1937
1938 std::vector<MVT::ValueType> RetTyVTs;
1939 RetTyVTs.reserve(3);
1940 RetTyVTs.push_back(NVT);
1941 RetTyVTs.push_back(NVT);
1942 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001943 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001944 Lo = SDOperand(NC, 0);
1945 Hi = SDOperand(NC, 1);
1946
1947 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001948 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001949 break;
1950 }
1951 case ISD::AND:
1952 case ISD::OR:
1953 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1954 SDOperand LL, LH, RL, RH;
1955 ExpandOp(Node->getOperand(0), LL, LH);
1956 ExpandOp(Node->getOperand(1), RL, RH);
1957 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1958 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1959 break;
1960 }
1961 case ISD::SELECT: {
1962 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001963
1964 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1965 case Expand: assert(0 && "It's impossible to expand bools");
1966 case Legal:
1967 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1968 break;
1969 case Promote:
1970 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1971 break;
1972 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001973 ExpandOp(Node->getOperand(1), LL, LH);
1974 ExpandOp(Node->getOperand(2), RL, RH);
1975 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1976 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1977 break;
1978 }
1979 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00001980 SDOperand In;
1981 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1982 case Expand: assert(0 && "expand-expand not implemented yet!");
1983 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1984 case Promote:
1985 In = PromoteOp(Node->getOperand(0));
1986 // Emit the appropriate sign_extend_inreg to get the value we want.
1987 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
1988 Node->getOperand(0).getValueType());
1989 break;
1990 }
1991
Chris Lattnerdc750592005-01-07 07:47:09 +00001992 // The low part is just a sign extension of the input (which degenerates to
1993 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00001994 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Chris Lattnerdc750592005-01-07 07:47:09 +00001995
1996 // The high part is obtained by SRA'ing all but one of the bits of the lo
1997 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001998 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001999 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2000 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002001 break;
2002 }
Chris Lattner47844892005-04-03 23:41:52 +00002003 case ISD::ZERO_EXTEND: {
2004 SDOperand In;
2005 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2006 case Expand: assert(0 && "expand-expand not implemented yet!");
2007 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2008 case Promote:
2009 In = PromoteOp(Node->getOperand(0));
2010 // Emit the appropriate zero_extend_inreg to get the value we want.
2011 In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In,
2012 Node->getOperand(0).getValueType());
2013 break;
2014 }
2015
Chris Lattnerdc750592005-01-07 07:47:09 +00002016 // The low part is just a zero extension of the input (which degenerates to
2017 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002018 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Chris Lattnerdc750592005-01-07 07:47:09 +00002019
2020 // The high part is just a zero.
2021 Hi = DAG.getConstant(0, NVT);
2022 break;
Chris Lattner47844892005-04-03 23:41:52 +00002023 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002024 // These operators cannot be expanded directly, emit them as calls to
2025 // library functions.
2026 case ISD::FP_TO_SINT:
2027 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002028 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002029 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002030 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002031 break;
2032 case ISD::FP_TO_UINT:
2033 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002034 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002035 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002036 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002037 break;
2038
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002039 case ISD::SHL:
2040 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002041 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002042 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002043
2044 // If this target supports SHL_PARTS, use it.
2045 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002046 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2047 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002048 break;
2049 }
2050
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002051 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002052 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002053 break;
2054
2055 case ISD::SRA:
2056 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002057 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002058 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002059
2060 // If this target supports SRA_PARTS, use it.
2061 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002062 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2063 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002064 break;
2065 }
2066
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002067 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002068 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002069 break;
2070 case ISD::SRL:
2071 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002072 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002073 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002074
2075 // If this target supports SRL_PARTS, use it.
2076 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002077 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2078 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002079 break;
2080 }
2081
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002082 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002083 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002084 break;
2085
Chris Lattner2e5872c2005-04-02 03:38:53 +00002086 case ISD::ADD:
2087 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2088 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002089 break;
2090 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002091 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2092 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002093 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002094 case ISD::MUL: {
2095 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2096 SDOperand LL, LH, RL, RH;
2097 ExpandOp(Node->getOperand(0), LL, LH);
2098 ExpandOp(Node->getOperand(1), RL, RH);
2099 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2100 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2101 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2102 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2103 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2104 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2105 } else {
2106 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2107 }
2108 break;
2109 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002110 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2111 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2112 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2113 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002114 }
2115
2116 // Remember in a map if the values will be reused later.
2117 if (!Node->hasOneUse()) {
2118 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2119 std::make_pair(Lo, Hi))).second;
2120 assert(isNew && "Value already expanded?!?");
2121 }
2122}
2123
2124
2125// SelectionDAG::Legalize - This is the entry point for the file.
2126//
Chris Lattner4add7e32005-01-23 04:42:50 +00002127void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002128 /// run - This is the main entry point to this class.
2129 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002130 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002131}
2132