blob: a9329cf876946ec0bc3b4ffc35a9e1eedb56ee51 [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:
753 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
754 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
755 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000756 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
757 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000758 DAG.getConstant(0, Tmp1.getValueType()));
759 break;
760 default:
761 // FIXME: This generated code sucks.
762 ISD::CondCode LowCC;
763 switch (cast<SetCCSDNode>(Node)->getCondition()) {
764 default: assert(0 && "Unknown integer setcc!");
765 case ISD::SETLT:
766 case ISD::SETULT: LowCC = ISD::SETULT; break;
767 case ISD::SETGT:
768 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
769 case ISD::SETLE:
770 case ISD::SETULE: LowCC = ISD::SETULE; break;
771 case ISD::SETGE:
772 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
773 }
774
775 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
776 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
777 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
778
779 // NOTE: on targets without efficient SELECT of bools, we can always use
780 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000781 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000782 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000783 Node->getValueType(0), LHSHi, RHSHi);
784 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
785 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
786 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 break;
788 }
789 }
790 break;
791
Chris Lattner85d70c62005-01-11 05:57:22 +0000792 case ISD::MEMSET:
793 case ISD::MEMCPY:
794 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000795 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000796 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
797
798 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
799 switch (getTypeAction(Node->getOperand(2).getValueType())) {
800 case Expand: assert(0 && "Cannot expand a byte!");
801 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000802 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000803 break;
804 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000805 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000806 break;
807 }
808 } else {
809 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
810 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000811
812 SDOperand Tmp4;
813 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000814 case Expand: assert(0 && "Cannot expand this yet!");
815 case Legal:
816 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000817 break;
818 case Promote:
819 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000820 break;
821 }
822
823 SDOperand Tmp5;
824 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
825 case Expand: assert(0 && "Cannot expand this yet!");
826 case Legal:
827 Tmp5 = LegalizeOp(Node->getOperand(4));
828 break;
829 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000830 Tmp5 = PromoteOp(Node->getOperand(4));
831 break;
832 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000833
834 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
835 default: assert(0 && "This action not implemented for this operation!");
836 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000837 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
838 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
839 Tmp5 != Node->getOperand(4)) {
840 std::vector<SDOperand> Ops;
841 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
842 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
843 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
844 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000845 break;
846 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000847 // Otherwise, the target does not support this operation. Lower the
848 // operation to an explicit libcall as appropriate.
849 MVT::ValueType IntPtr = TLI.getPointerTy();
850 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
851 std::vector<std::pair<SDOperand, const Type*> > Args;
852
Reid Spencer6dced922005-01-12 14:53:45 +0000853 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000854 if (Node->getOpcode() == ISD::MEMSET) {
855 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
856 // Extend the ubyte argument to be an int value for the call.
857 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
858 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
859 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
860
861 FnName = "memset";
862 } else if (Node->getOpcode() == ISD::MEMCPY ||
863 Node->getOpcode() == ISD::MEMMOVE) {
864 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
865 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
866 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
867 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
868 } else {
869 assert(0 && "Unknown op!");
870 }
871 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000872 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000873 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
874 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000875 break;
876 }
877 case TargetLowering::Custom:
878 std::vector<SDOperand> Ops;
879 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
880 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
881 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
882 Result = TLI.LowerOperation(Result);
883 Result = LegalizeOp(Result);
884 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000885 }
886 break;
887 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000888 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +0000889 case ISD::SUB_PARTS:
890 case ISD::SHL_PARTS:
891 case ISD::SRA_PARTS:
892 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000893 std::vector<SDOperand> Ops;
894 bool Changed = false;
895 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
896 Ops.push_back(LegalizeOp(Node->getOperand(i)));
897 Changed |= Ops.back() != Node->getOperand(i);
898 }
899 if (Changed)
900 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000901
902 // Since these produce multiple values, make sure to remember that we
903 // legalized all of them.
904 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
905 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
906 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000907 }
Chris Lattner13fe99c2005-04-02 05:00:07 +0000908
909 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +0000910 case ISD::ADD:
911 case ISD::SUB:
912 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +0000913 case ISD::MULHS:
914 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +0000915 case ISD::UDIV:
916 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +0000917 case ISD::AND:
918 case ISD::OR:
919 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000920 case ISD::SHL:
921 case ISD::SRL:
922 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000923 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
924 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
925 if (Tmp1 != Node->getOperand(0) ||
926 Tmp2 != Node->getOperand(1))
927 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
928 break;
Nate Begeman20b7d2a2005-04-06 00:23:54 +0000929
930 case ISD::UREM:
931 case ISD::SREM:
932 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
933 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
934 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
935 case TargetLowering::Legal:
936 if (Tmp1 != Node->getOperand(0) ||
937 Tmp2 != Node->getOperand(1))
938 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
939 Tmp2);
940 break;
941 case TargetLowering::Promote:
942 case TargetLowering::Custom:
943 assert(0 && "Cannot promote/custom handle this yet!");
944 case TargetLowering::Expand: {
945 MVT::ValueType VT = Node->getValueType(0);
946 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
947 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
948 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
949 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
950 }
951 break;
952 }
953 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +0000954
955 // Unary operators
956 case ISD::FABS:
957 case ISD::FNEG:
958 Tmp1 = LegalizeOp(Node->getOperand(0));
959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
960 case TargetLowering::Legal:
961 if (Tmp1 != Node->getOperand(0))
962 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
963 break;
964 case TargetLowering::Promote:
965 case TargetLowering::Custom:
966 assert(0 && "Cannot promote/custom handle this yet!");
967 case TargetLowering::Expand:
968 if (Node->getOpcode() == ISD::FNEG) {
969 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
970 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
971 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
972 Tmp2, Tmp1));
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000973 } else if (Node->getOpcode() == ISD::FABS) {
974 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
975 MVT::ValueType VT = Node->getValueType(0);
976 Tmp2 = DAG.getConstantFP(0.0, VT);
977 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
978 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
979 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
980 Result = LegalizeOp(Result);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000981 } else {
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000982 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +0000983 }
984 break;
985 }
986 break;
987
988 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +0000989 case ISD::ZERO_EXTEND:
990 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000991 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000992 case ISD::FP_EXTEND:
993 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000994 case ISD::FP_TO_SINT:
995 case ISD::FP_TO_UINT:
996 case ISD::SINT_TO_FP:
997 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000998 switch (getTypeAction(Node->getOperand(0).getValueType())) {
999 case Legal:
1000 Tmp1 = LegalizeOp(Node->getOperand(0));
1001 if (Tmp1 != Node->getOperand(0))
1002 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1003 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001004 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001005 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1006 Node->getOpcode() == ISD::UINT_TO_FP) {
1007 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1008 Node->getValueType(0), Node->getOperand(0));
1009 Result = LegalizeOp(Result);
1010 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001011 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1012 // In the expand case, we must be dealing with a truncate, because
1013 // otherwise the result would be larger than the source.
1014 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1015
1016 // Since the result is legal, we should just be able to truncate the low
1017 // part of the source.
1018 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1019 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001020 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001021 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001022
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001023 case Promote:
1024 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001025 case ISD::ZERO_EXTEND:
1026 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001027 // NOTE: Any extend would work here...
1028 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
1029 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001030 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001031 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001032 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001033 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001034 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001035 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001036 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1037 Result, Node->getOperand(0).getValueType());
1038 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001039 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001040 Result = PromoteOp(Node->getOperand(0));
1041 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1042 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001043 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001044 Result = PromoteOp(Node->getOperand(0));
1045 if (Result.getValueType() != Op.getValueType())
1046 // Dynamically dead while we have only 2 FP types.
1047 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1048 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001049 case ISD::FP_ROUND:
1050 case ISD::FP_TO_SINT:
1051 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001052 Result = PromoteOp(Node->getOperand(0));
1053 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1054 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001055 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001056 Result = PromoteOp(Node->getOperand(0));
1057 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1058 Result, Node->getOperand(0).getValueType());
1059 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1060 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001061 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001062 Result = PromoteOp(Node->getOperand(0));
1063 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1064 Result, Node->getOperand(0).getValueType());
1065 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1066 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001067 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001068 }
1069 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001070 case ISD::FP_ROUND_INREG:
1071 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +00001072 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001073 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001074 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1075
1076 // If this operation is not supported, convert it to a shl/shr or load/store
1077 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001078 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1079 default: assert(0 && "This action not supported for this op yet!");
1080 case TargetLowering::Legal:
1081 if (Tmp1 != Node->getOperand(0))
1082 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1083 ExtraVT);
1084 break;
1085 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001086 // If this is an integer extend and shifts are supported, do that.
1087 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
1088 // NOTE: we could fall back on load/store here too for targets without
1089 // AND. However, it is doubtful that any exist.
1090 // AND out the appropriate bits.
1091 SDOperand Mask =
1092 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
1093 Node->getValueType(0));
1094 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
1095 Node->getOperand(0), Mask);
1096 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
1097 // NOTE: we could fall back on load/store here too for targets without
1098 // SAR. However, it is doubtful that any exist.
1099 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1100 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001101 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001102 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1103 Node->getOperand(0), ShiftCst);
1104 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1105 Result, ShiftCst);
1106 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1107 // The only way we can lower this is to turn it into a STORETRUNC,
1108 // EXTLOAD pair, targetting a temporary location (a stack slot).
1109
1110 // NOTE: there is a choice here between constantly creating new stack
1111 // slots and always reusing the same one. We currently always create
1112 // new ones, as reuse may inhibit scheduling.
1113 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1114 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1115 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1116 MachineFunction &MF = DAG.getMachineFunction();
1117 int SSFI =
1118 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1119 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1120 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1121 Node->getOperand(0), StackSlot, ExtraVT);
1122 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1123 Result, StackSlot, ExtraVT);
1124 } else {
1125 assert(0 && "Unknown op");
1126 }
1127 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001128 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001129 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001130 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001131 }
Chris Lattner99222f72005-01-15 07:15:18 +00001132 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001133
Chris Lattnerea4ca942005-01-07 22:28:47 +00001134 if (!Op.Val->hasOneUse())
1135 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001136
1137 return Result;
1138}
1139
Chris Lattner4d978642005-01-15 22:16:26 +00001140/// PromoteOp - Given an operation that produces a value in an invalid type,
1141/// promote it to compute the value into a larger type. The produced value will
1142/// have the correct bits for the low portion of the register, but no guarantee
1143/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001144SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1145 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001146 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001147 assert(getTypeAction(VT) == Promote &&
1148 "Caller should expand or legalize operands that are not promotable!");
1149 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1150 "Cannot promote to smaller type!");
1151
1152 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1153 if (I != PromotedNodes.end()) return I->second;
1154
1155 SDOperand Tmp1, Tmp2, Tmp3;
1156
1157 SDOperand Result;
1158 SDNode *Node = Op.Val;
1159
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001160 // Promotion needs an optimization step to clean up after it, and is not
1161 // careful to avoid operations the target does not support. Make sure that
1162 // all generated operations are legalized in the next iteration.
1163 NeedsAnotherIteration = true;
1164
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001165 switch (Node->getOpcode()) {
1166 default:
1167 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1168 assert(0 && "Do not know how to promote this operator!");
1169 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001170 case ISD::UNDEF:
1171 Result = DAG.getNode(ISD::UNDEF, NVT);
1172 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001173 case ISD::Constant:
1174 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1175 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1176 break;
1177 case ISD::ConstantFP:
1178 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1179 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1180 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001181 case ISD::CopyFromReg:
1182 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1183 Node->getOperand(0));
1184 // Remember that we legalized the chain.
1185 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1186 break;
1187
Chris Lattner2cb338d2005-01-18 02:59:52 +00001188 case ISD::SETCC:
1189 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1190 "SetCC type is not legal??");
1191 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1192 TLI.getSetCCResultTy(), Node->getOperand(0),
1193 Node->getOperand(1));
1194 Result = LegalizeOp(Result);
1195 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001196
1197 case ISD::TRUNCATE:
1198 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1199 case Legal:
1200 Result = LegalizeOp(Node->getOperand(0));
1201 assert(Result.getValueType() >= NVT &&
1202 "This truncation doesn't make sense!");
1203 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1204 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1205 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001206 case Promote:
1207 // The truncation is not required, because we don't guarantee anything
1208 // about high bits anyway.
1209 Result = PromoteOp(Node->getOperand(0));
1210 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001211 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001212 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1213 // Truncate the low part of the expanded value to the result type
1214 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001215 }
1216 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001217 case ISD::SIGN_EXTEND:
1218 case ISD::ZERO_EXTEND:
1219 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1220 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1221 case Legal:
1222 // Input is legal? Just do extend all the way to the larger type.
1223 Result = LegalizeOp(Node->getOperand(0));
1224 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1225 break;
1226 case Promote:
1227 // Promote the reg if it's smaller.
1228 Result = PromoteOp(Node->getOperand(0));
1229 // The high bits are not guaranteed to be anything. Insert an extend.
1230 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001231 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1232 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001233 else
Chris Lattner05596912005-02-04 18:39:19 +00001234 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1235 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001236 break;
1237 }
1238 break;
1239
1240 case ISD::FP_EXTEND:
1241 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1242 case ISD::FP_ROUND:
1243 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1244 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1245 case Promote: assert(0 && "Unreachable with 2 FP types!");
1246 case Legal:
1247 // Input is legal? Do an FP_ROUND_INREG.
1248 Result = LegalizeOp(Node->getOperand(0));
1249 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1250 break;
1251 }
1252 break;
1253
1254 case ISD::SINT_TO_FP:
1255 case ISD::UINT_TO_FP:
1256 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1257 case Legal:
1258 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001259 // No extra round required here.
1260 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001261 break;
1262
1263 case Promote:
1264 Result = PromoteOp(Node->getOperand(0));
1265 if (Node->getOpcode() == ISD::SINT_TO_FP)
1266 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1267 Result, Node->getOperand(0).getValueType());
1268 else
1269 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1270 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001271 // No extra round required here.
1272 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001273 break;
1274 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001275 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1276 Node->getOperand(0));
1277 Result = LegalizeOp(Result);
1278
1279 // Round if we cannot tolerate excess precision.
1280 if (NoExcessFPPrecision)
1281 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1282 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001283 }
Chris Lattner4d978642005-01-15 22:16:26 +00001284 break;
1285
1286 case ISD::FP_TO_SINT:
1287 case ISD::FP_TO_UINT:
1288 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1289 case Legal:
1290 Tmp1 = LegalizeOp(Node->getOperand(0));
1291 break;
1292 case Promote:
1293 // The input result is prerounded, so we don't have to do anything
1294 // special.
1295 Tmp1 = PromoteOp(Node->getOperand(0));
1296 break;
1297 case Expand:
1298 assert(0 && "not implemented");
1299 }
1300 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1301 break;
1302
Chris Lattner13fe99c2005-04-02 05:00:07 +00001303 case ISD::FABS:
1304 case ISD::FNEG:
1305 Tmp1 = PromoteOp(Node->getOperand(0));
1306 assert(Tmp1.getValueType() == NVT);
1307 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1308 // NOTE: we do not have to do any extra rounding here for
1309 // NoExcessFPPrecision, because we know the input will have the appropriate
1310 // precision, and these operations don't modify precision at all.
1311 break;
1312
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001313 case ISD::AND:
1314 case ISD::OR:
1315 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001316 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001317 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001318 case ISD::MUL:
1319 // The input may have strange things in the top bits of the registers, but
1320 // these operations don't care. They may have wierd bits going out, but
1321 // that too is okay if they are integer operations.
1322 Tmp1 = PromoteOp(Node->getOperand(0));
1323 Tmp2 = PromoteOp(Node->getOperand(1));
1324 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1325 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1326
1327 // However, if this is a floating point operation, they will give excess
1328 // precision that we may not be able to tolerate. If we DO allow excess
1329 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001330 // FIXME: Why would we need to round FP ops more than integer ones?
1331 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001332 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1333 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1334 break;
1335
Chris Lattner4d978642005-01-15 22:16:26 +00001336 case ISD::SDIV:
1337 case ISD::SREM:
1338 // These operators require that their input be sign extended.
1339 Tmp1 = PromoteOp(Node->getOperand(0));
1340 Tmp2 = PromoteOp(Node->getOperand(1));
1341 if (MVT::isInteger(NVT)) {
1342 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001343 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001344 }
1345 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1346
1347 // Perform FP_ROUND: this is probably overly pessimistic.
1348 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1349 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1350 break;
1351
1352 case ISD::UDIV:
1353 case ISD::UREM:
1354 // These operators require that their input be zero extended.
1355 Tmp1 = PromoteOp(Node->getOperand(0));
1356 Tmp2 = PromoteOp(Node->getOperand(1));
1357 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1358 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001359 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001360 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1361 break;
1362
1363 case ISD::SHL:
1364 Tmp1 = PromoteOp(Node->getOperand(0));
1365 Tmp2 = LegalizeOp(Node->getOperand(1));
1366 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1367 break;
1368 case ISD::SRA:
1369 // The input value must be properly sign extended.
1370 Tmp1 = PromoteOp(Node->getOperand(0));
1371 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1372 Tmp2 = LegalizeOp(Node->getOperand(1));
1373 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1374 break;
1375 case ISD::SRL:
1376 // The input value must be properly zero extended.
1377 Tmp1 = PromoteOp(Node->getOperand(0));
1378 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1379 Tmp2 = LegalizeOp(Node->getOperand(1));
1380 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1381 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001382 case ISD::LOAD:
1383 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1384 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001385 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001386 if (MVT::isInteger(NVT))
1387 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, VT);
1388 else
1389 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001390
1391 // Remember that we legalized the chain.
1392 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1393 break;
1394 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001395 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1396 case Expand: assert(0 && "It's impossible to expand bools");
1397 case Legal:
1398 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1399 break;
1400 case Promote:
1401 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1402 break;
1403 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001404 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1405 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1406 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1407 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001408 case ISD::CALL: {
1409 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1410 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1411
Chris Lattner3d95c142005-01-19 20:24:35 +00001412 std::vector<SDOperand> Ops;
1413 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1414 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1415
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001416 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1417 "Can only promote single result calls");
1418 std::vector<MVT::ValueType> RetTyVTs;
1419 RetTyVTs.reserve(2);
1420 RetTyVTs.push_back(NVT);
1421 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001422 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001423 Result = SDOperand(NC, 0);
1424
1425 // Insert the new chain mapping.
1426 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1427 break;
1428 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001429 }
1430
1431 assert(Result.Val && "Didn't set a result!");
1432 AddPromotedOperand(Op, Result);
1433 return Result;
1434}
Chris Lattnerdc750592005-01-07 07:47:09 +00001435
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001436/// ExpandAddSub - Find a clever way to expand this add operation into
1437/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001438void SelectionDAGLegalize::
1439ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1440 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001441 // Expand the subcomponents.
1442 SDOperand LHSL, LHSH, RHSL, RHSH;
1443 ExpandOp(LHS, LHSL, LHSH);
1444 ExpandOp(RHS, RHSL, RHSH);
1445
Chris Lattner8ffd0042005-04-11 20:29:59 +00001446 // FIXME: this should be moved to the dag combiner someday.
1447 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1448 if (LHSL.getValueType() == MVT::i32) {
1449 SDOperand LowEl;
1450 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1451 if (C->getValue() == 0)
1452 LowEl = RHSL;
1453 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1454 if (C->getValue() == 0)
1455 LowEl = LHSL;
1456 if (LowEl.Val) {
1457 // Turn this into an add/sub of the high part only.
1458 SDOperand HiEl =
1459 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1460 LowEl.getValueType(), LHSH, RHSH);
1461 Lo = LowEl;
1462 Hi = HiEl;
1463 return;
1464 }
1465 }
1466
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001467 std::vector<SDOperand> Ops;
1468 Ops.push_back(LHSL);
1469 Ops.push_back(LHSH);
1470 Ops.push_back(RHSL);
1471 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001472 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001473 Hi = Lo.getValue(1);
1474}
1475
Chris Lattner4157c412005-04-02 04:00:59 +00001476void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1477 SDOperand Op, SDOperand Amt,
1478 SDOperand &Lo, SDOperand &Hi) {
1479 // Expand the subcomponents.
1480 SDOperand LHSL, LHSH;
1481 ExpandOp(Op, LHSL, LHSH);
1482
1483 std::vector<SDOperand> Ops;
1484 Ops.push_back(LHSL);
1485 Ops.push_back(LHSH);
1486 Ops.push_back(Amt);
1487 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1488 Hi = Lo.getValue(1);
1489}
1490
1491
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001492/// ExpandShift - Try to find a clever way to expand this shift operation out to
1493/// smaller elements. If we can't find a way that is more efficient than a
1494/// libcall on this target, return false. Otherwise, return true with the
1495/// low-parts expanded into Lo and Hi.
1496bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1497 SDOperand &Lo, SDOperand &Hi) {
1498 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1499 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001500
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001501 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001502 SDOperand ShAmt = LegalizeOp(Amt);
1503 MVT::ValueType ShTy = ShAmt.getValueType();
1504 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1505 unsigned NVTBits = MVT::getSizeInBits(NVT);
1506
1507 // Handle the case when Amt is an immediate. Other cases are currently broken
1508 // and are disabled.
1509 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1510 unsigned Cst = CN->getValue();
1511 // Expand the incoming operand to be shifted, so that we have its parts
1512 SDOperand InL, InH;
1513 ExpandOp(Op, InL, InH);
1514 switch(Opc) {
1515 case ISD::SHL:
1516 if (Cst > VTBits) {
1517 Lo = DAG.getConstant(0, NVT);
1518 Hi = DAG.getConstant(0, NVT);
1519 } else if (Cst > NVTBits) {
1520 Lo = DAG.getConstant(0, NVT);
1521 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001522 } else if (Cst == NVTBits) {
1523 Lo = DAG.getConstant(0, NVT);
1524 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001525 } else {
1526 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1527 Hi = DAG.getNode(ISD::OR, NVT,
1528 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1529 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1530 }
1531 return true;
1532 case ISD::SRL:
1533 if (Cst > VTBits) {
1534 Lo = DAG.getConstant(0, NVT);
1535 Hi = DAG.getConstant(0, NVT);
1536 } else if (Cst > NVTBits) {
1537 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1538 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001539 } else if (Cst == NVTBits) {
1540 Lo = InH;
1541 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001542 } else {
1543 Lo = DAG.getNode(ISD::OR, NVT,
1544 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1545 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1546 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1547 }
1548 return true;
1549 case ISD::SRA:
1550 if (Cst > VTBits) {
1551 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1552 DAG.getConstant(NVTBits-1, ShTy));
1553 } else if (Cst > NVTBits) {
1554 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1555 DAG.getConstant(Cst-NVTBits, ShTy));
1556 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1557 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001558 } else if (Cst == NVTBits) {
1559 Lo = InH;
1560 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1561 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001562 } else {
1563 Lo = DAG.getNode(ISD::OR, NVT,
1564 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1565 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1566 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1567 }
1568 return true;
1569 }
1570 }
1571 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1572 // so disable it for now. Currently targets are handling this via SHL_PARTS
1573 // and friends.
1574 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001575
1576 // If we have an efficient select operation (or if the selects will all fold
1577 // away), lower to some complex code, otherwise just emit the libcall.
1578 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1579 !isa<ConstantSDNode>(Amt))
1580 return false;
1581
1582 SDOperand InL, InH;
1583 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001584 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1585 DAG.getConstant(NVTBits, ShTy), ShAmt);
1586
Chris Lattner4d25c042005-01-20 20:29:23 +00001587 // Compare the unmasked shift amount against 32.
1588 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1589 DAG.getConstant(NVTBits, ShTy));
1590
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001591 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1592 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1593 DAG.getConstant(NVTBits-1, ShTy));
1594 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1595 DAG.getConstant(NVTBits-1, ShTy));
1596 }
1597
1598 if (Opc == ISD::SHL) {
1599 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1600 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1601 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001602 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001603
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001604 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1605 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1606 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001607 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1608 DAG.getSetCC(ISD::SETEQ,
1609 TLI.getSetCCResultTy(), NAmt,
1610 DAG.getConstant(32, ShTy)),
1611 DAG.getConstant(0, NVT),
1612 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001613 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001614 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001615 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001616 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001617
1618 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001619 if (Opc == ISD::SRA)
1620 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1621 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001622 else
1623 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001624 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001625 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001626 }
1627 return true;
1628}
Chris Lattneraac464e2005-01-21 06:05:23 +00001629
Chris Lattner4add7e32005-01-23 04:42:50 +00001630/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1631/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1632/// Found.
1633static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1634 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1635
1636 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1637 // than the Found node. Just remember this node and return.
1638 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1639 Found = Node;
1640 return;
1641 }
1642
1643 // Otherwise, scan the operands of Node to see if any of them is a call.
1644 assert(Node->getNumOperands() != 0 &&
1645 "All leaves should have depth equal to the entry node!");
1646 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1647 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1648
1649 // Tail recurse for the last iteration.
1650 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1651 Found);
1652}
1653
1654
1655/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1656/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1657/// than Found.
1658static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1659 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1660
1661 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1662 // than the Found node. Just remember this node and return.
1663 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1664 Found = Node;
1665 return;
1666 }
1667
1668 // Otherwise, scan the operands of Node to see if any of them is a call.
1669 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1670 if (UI == E) return;
1671 for (--E; UI != E; ++UI)
1672 FindEarliestAdjCallStackUp(*UI, Found);
1673
1674 // Tail recurse for the last iteration.
1675 FindEarliestAdjCallStackUp(*UI, Found);
1676}
1677
1678/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1679/// find the ADJCALLSTACKUP node that terminates the call sequence.
1680static SDNode *FindAdjCallStackUp(SDNode *Node) {
1681 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1682 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001683 if (Node->use_empty())
1684 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001685
1686 if (Node->hasOneUse()) // Simple case, only has one user to check.
1687 return FindAdjCallStackUp(*Node->use_begin());
1688
1689 SDOperand TheChain(Node, Node->getNumValues()-1);
1690 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1691
1692 for (SDNode::use_iterator UI = Node->use_begin(),
1693 E = Node->use_end(); ; ++UI) {
1694 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1695
1696 // Make sure to only follow users of our token chain.
1697 SDNode *User = *UI;
1698 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1699 if (User->getOperand(i) == TheChain)
1700 return FindAdjCallStackUp(User);
1701 }
1702 assert(0 && "Unreachable");
1703 abort();
1704}
1705
1706/// FindInputOutputChains - If we are replacing an operation with a call we need
1707/// to find the call that occurs before and the call that occurs after it to
1708/// properly serialize the calls in the block.
1709static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1710 SDOperand Entry) {
1711 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanadd0c632005-04-11 03:01:51 +00001712 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00001713 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1714 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Nate Begemanadd0c632005-04-11 03:01:51 +00001715
1716 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1717 // previous call in the function. LatestCallStackDown may in that case be
1718 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1719 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1720 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1721 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1722 else
1723 LatestAdjCallStackUp = Entry.Val;
1724 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
1725
Chris Lattner4add7e32005-01-23 04:42:50 +00001726 SDNode *EarliestAdjCallStackUp = 0;
1727 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1728
1729 if (EarliestAdjCallStackUp) {
1730 //std::cerr << "Found node: ";
1731 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1732 }
1733
1734 return SDOperand(LatestAdjCallStackUp, 0);
1735}
1736
1737
1738
Chris Lattneraac464e2005-01-21 06:05:23 +00001739// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1740// does not fit into a register, return the lo part and set the hi part to the
1741// by-reg argument. If it does fit into a single register, return the result
1742// and leave the Hi part unset.
1743SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1744 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001745 SDNode *OutChain;
1746 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1747 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00001748 if (InChain.Val == 0)
1749 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00001750
Chris Lattneraac464e2005-01-21 06:05:23 +00001751 TargetLowering::ArgListTy Args;
1752 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1753 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1754 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1755 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1756 }
1757 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1758
1759 // We don't care about token chains for libcalls. We just use the entry
1760 // node as our input and ignore the output chain. This allows us to place
1761 // calls wherever we need them to satisfy data dependences.
1762 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begemanf6565252005-03-26 01:29:23 +00001763 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001764 Args, DAG).first;
1765 switch (getTypeAction(Result.getValueType())) {
1766 default: assert(0 && "Unknown thing");
1767 case Legal:
1768 return Result;
1769 case Promote:
1770 assert(0 && "Cannot promote this yet!");
1771 case Expand:
1772 SDOperand Lo;
1773 ExpandOp(Result, Lo, Hi);
1774 return Lo;
1775 }
1776}
1777
Chris Lattner4add7e32005-01-23 04:42:50 +00001778
Chris Lattneraac464e2005-01-21 06:05:23 +00001779/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1780/// destination type is legal.
1781SDOperand SelectionDAGLegalize::
1782ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1783 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1784 assert(getTypeAction(Source.getValueType()) == Expand &&
1785 "This is not an expansion!");
1786 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1787
Chris Lattner4add7e32005-01-23 04:42:50 +00001788 SDNode *OutChain;
1789 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1790 DAG.getEntryNode());
1791
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001792 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001793 if (isSigned) {
1794 if (DestTy == MVT::f32)
1795 FnName = "__floatdisf";
1796 else {
1797 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1798 FnName = "__floatdidf";
1799 }
1800 } else {
1801 // If this is unsigned, and not supported, first perform the conversion to
1802 // signed, then adjust the result if the sign bit is set.
1803 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1804
1805 assert(0 && "Unsigned casts not supported yet!");
1806 }
1807 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1808
1809 TargetLowering::ArgListTy Args;
1810 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1811 Args.push_back(std::make_pair(Source, ArgTy));
1812
1813 // We don't care about token chains for libcalls. We just use the entry
1814 // node as our input and ignore the output chain. This allows us to place
1815 // calls wherever we need them to satisfy data dependences.
1816 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begemanf6565252005-03-26 01:29:23 +00001817 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner4add7e32005-01-23 04:42:50 +00001818
Chris Lattneraac464e2005-01-21 06:05:23 +00001819}
1820
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001821
1822
Chris Lattnerdc750592005-01-07 07:47:09 +00001823/// ExpandOp - Expand the specified SDOperand into its two component pieces
1824/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1825/// LegalizeNodes map is filled in for any results that are not expanded, the
1826/// ExpandedNodes map is filled in for any results that are expanded, and the
1827/// Lo/Hi values are returned.
1828void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1829 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001830 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001831 SDNode *Node = Op.Val;
1832 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1833 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1834 assert(MVT::isInteger(NVT) && NVT < VT &&
1835 "Cannot expand to FP value or to larger int value!");
1836
1837 // If there is more than one use of this, see if we already expanded it.
1838 // There is no use remembering values that only have a single use, as the map
1839 // entries will never be reused.
1840 if (!Node->hasOneUse()) {
1841 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1842 = ExpandedNodes.find(Op);
1843 if (I != ExpandedNodes.end()) {
1844 Lo = I->second.first;
1845 Hi = I->second.second;
1846 return;
1847 }
1848 }
1849
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001850 // Expanding to multiple registers needs to perform an optimization step, and
1851 // is not careful to avoid operations the target does not support. Make sure
1852 // that all generated operations are legalized in the next iteration.
1853 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001854
Chris Lattnerdc750592005-01-07 07:47:09 +00001855 switch (Node->getOpcode()) {
1856 default:
1857 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1858 assert(0 && "Do not know how to expand this operator!");
1859 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001860 case ISD::UNDEF:
1861 Lo = DAG.getNode(ISD::UNDEF, NVT);
1862 Hi = DAG.getNode(ISD::UNDEF, NVT);
1863 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001864 case ISD::Constant: {
1865 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1866 Lo = DAG.getConstant(Cst, NVT);
1867 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1868 break;
1869 }
1870
1871 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001872 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001873 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001874 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1875 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1876
1877 // Remember that we legalized the chain.
1878 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1879
Chris Lattnerdc750592005-01-07 07:47:09 +00001880 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1881 break;
1882 }
1883
Chris Lattner32e08b72005-03-28 22:03:13 +00001884 case ISD::BUILD_PAIR:
1885 // Legalize both operands. FIXME: in the future we should handle the case
1886 // where the two elements are not legal.
1887 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1888 Lo = LegalizeOp(Node->getOperand(0));
1889 Hi = LegalizeOp(Node->getOperand(1));
1890 break;
1891
Chris Lattnerdc750592005-01-07 07:47:09 +00001892 case ISD::LOAD: {
1893 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1894 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1895 Lo = DAG.getLoad(NVT, Ch, Ptr);
1896
1897 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001898 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001899 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1900 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001901 Hi = DAG.getLoad(NVT, Ch, Ptr);
1902
1903 // Build a factor node to remember that this load is independent of the
1904 // other one.
1905 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1906 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001907
1908 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001909 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001910 if (!TLI.isLittleEndian())
1911 std::swap(Lo, Hi);
1912 break;
1913 }
1914 case ISD::CALL: {
1915 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1916 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1917
Chris Lattner3d95c142005-01-19 20:24:35 +00001918 bool Changed = false;
1919 std::vector<SDOperand> Ops;
1920 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1921 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1922 Changed |= Ops.back() != Node->getOperand(i);
1923 }
1924
Chris Lattnerdc750592005-01-07 07:47:09 +00001925 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1926 "Can only expand a call once so far, not i64 -> i16!");
1927
1928 std::vector<MVT::ValueType> RetTyVTs;
1929 RetTyVTs.reserve(3);
1930 RetTyVTs.push_back(NVT);
1931 RetTyVTs.push_back(NVT);
1932 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001933 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001934 Lo = SDOperand(NC, 0);
1935 Hi = SDOperand(NC, 1);
1936
1937 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001938 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001939 break;
1940 }
1941 case ISD::AND:
1942 case ISD::OR:
1943 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1944 SDOperand LL, LH, RL, RH;
1945 ExpandOp(Node->getOperand(0), LL, LH);
1946 ExpandOp(Node->getOperand(1), RL, RH);
1947 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1948 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1949 break;
1950 }
1951 case ISD::SELECT: {
1952 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001953
1954 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1955 case Expand: assert(0 && "It's impossible to expand bools");
1956 case Legal:
1957 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1958 break;
1959 case Promote:
1960 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1961 break;
1962 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001963 ExpandOp(Node->getOperand(1), LL, LH);
1964 ExpandOp(Node->getOperand(2), RL, RH);
1965 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1966 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1967 break;
1968 }
1969 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00001970 SDOperand In;
1971 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1972 case Expand: assert(0 && "expand-expand not implemented yet!");
1973 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1974 case Promote:
1975 In = PromoteOp(Node->getOperand(0));
1976 // Emit the appropriate sign_extend_inreg to get the value we want.
1977 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
1978 Node->getOperand(0).getValueType());
1979 break;
1980 }
1981
Chris Lattnerdc750592005-01-07 07:47:09 +00001982 // The low part is just a sign extension of the input (which degenerates to
1983 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00001984 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Chris Lattnerdc750592005-01-07 07:47:09 +00001985
1986 // The high part is obtained by SRA'ing all but one of the bits of the lo
1987 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001988 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001989 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1990 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001991 break;
1992 }
Chris Lattner47844892005-04-03 23:41:52 +00001993 case ISD::ZERO_EXTEND: {
1994 SDOperand In;
1995 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1996 case Expand: assert(0 && "expand-expand not implemented yet!");
1997 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1998 case Promote:
1999 In = PromoteOp(Node->getOperand(0));
2000 // Emit the appropriate zero_extend_inreg to get the value we want.
2001 In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In,
2002 Node->getOperand(0).getValueType());
2003 break;
2004 }
2005
Chris Lattnerdc750592005-01-07 07:47:09 +00002006 // The low part is just a zero extension of the input (which degenerates to
2007 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002008 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Chris Lattnerdc750592005-01-07 07:47:09 +00002009
2010 // The high part is just a zero.
2011 Hi = DAG.getConstant(0, NVT);
2012 break;
Chris Lattner47844892005-04-03 23:41:52 +00002013 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002014 // These operators cannot be expanded directly, emit them as calls to
2015 // library functions.
2016 case ISD::FP_TO_SINT:
2017 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002018 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002019 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002020 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002021 break;
2022 case ISD::FP_TO_UINT:
2023 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002024 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002025 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002026 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002027 break;
2028
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002029 case ISD::SHL:
2030 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002031 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002032 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002033
2034 // If this target supports SHL_PARTS, use it.
2035 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002036 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2037 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002038 break;
2039 }
2040
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002041 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002042 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002043 break;
2044
2045 case ISD::SRA:
2046 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002047 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002048 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002049
2050 // If this target supports SRA_PARTS, use it.
2051 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002052 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2053 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002054 break;
2055 }
2056
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002057 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002058 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002059 break;
2060 case ISD::SRL:
2061 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002062 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002063 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002064
2065 // If this target supports SRL_PARTS, use it.
2066 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002067 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2068 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002069 break;
2070 }
2071
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002072 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002073 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002074 break;
2075
Chris Lattner2e5872c2005-04-02 03:38:53 +00002076 case ISD::ADD:
2077 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2078 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002079 break;
2080 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002081 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2082 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002083 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002084 case ISD::MUL: {
2085 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2086 SDOperand LL, LH, RL, RH;
2087 ExpandOp(Node->getOperand(0), LL, LH);
2088 ExpandOp(Node->getOperand(1), RL, RH);
2089 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2090 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2091 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2092 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2093 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2094 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2095 } else {
2096 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2097 }
2098 break;
2099 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002100 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2101 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2102 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2103 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002104 }
2105
2106 // Remember in a map if the values will be reused later.
2107 if (!Node->hasOneUse()) {
2108 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2109 std::make_pair(Lo, Hi))).second;
2110 assert(isNew && "Value already expanded?!?");
2111 }
2112}
2113
2114
2115// SelectionDAG::Legalize - This is the entry point for the file.
2116//
Chris Lattner4add7e32005-01-23 04:42:50 +00002117void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002118 /// run - This is the main entry point to this class.
2119 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002120 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002121}
2122