blob: e3f8e0911e9ae4fc784f663faa1e4399ee09a901 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// 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.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
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 Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattnerdc750592005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
55 unsigned ValueTypeActions;
56
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner1f2c9d82005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattnerdc750592005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattnerea4ca942005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000128
129 SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000130 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
131 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000132 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000134
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000135 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
136 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000137 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
139 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000140 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000141
Chris Lattnera5bf1032005-05-12 04:49:08 +0000142 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
143
Chris Lattnerdc750592005-01-07 07:47:09 +0000144 SDOperand getIntPtrConstant(uint64_t Val) {
145 return DAG.getConstant(Val, TLI.getPointerTy());
146 }
147};
148}
149
150
Chris Lattner4add7e32005-01-23 04:42:50 +0000151SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
152 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
153 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000154 assert(MVT::LAST_VALUETYPE <= 16 &&
155 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000156}
157
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000158/// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattnere3e847b2005-07-16 00:19:57 +0000159/// UINT_TO_FP operation of the specified operand when the target requests that
160/// we expand it. At this point, we know that the result and operand types are
161/// legal for the target.
162SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0,
163 MVT::ValueType DestVT) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000164 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000165
166 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(),
Chris Lattnere3e847b2005-07-16 00:19:57 +0000167 Op0,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000168 DAG.getConstant(0,
Chris Lattnere3e847b2005-07-16 00:19:57 +0000169 Op0.getValueType()));
170 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
171 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
172 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000173
Chris Lattnerb35912e2005-07-18 04:31:14 +0000174 // If the sign bit of the integer is set, the large number will be treated as
175 // a negative number. To counteract this, the dynamic code adds an offset
176 // depending on the data type.
177 uint64_t FF;
178 switch (Op0.getValueType()) {
179 default: assert(0 && "Unsupported integer type!");
180 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
181 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
182 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
183 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
184 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000185 if (TLI.isLittleEndian()) FF <<= 32;
186 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000187
Chris Lattnere3e847b2005-07-16 00:19:57 +0000188 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
189 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
190 TLI.getPointerTy());
191 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
192 SDOperand FudgeInReg;
193 if (DestVT == MVT::f32)
194 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
195 DAG.getSrcValue(NULL));
196 else {
197 assert(DestVT == MVT::f64 && "Unexpected conversion");
198 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
199 DAG.getEntryNode(), CPIdx,
200 DAG.getSrcValue(NULL), MVT::f32));
201 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000202
Chris Lattnere3e847b2005-07-16 00:19:57 +0000203 NeedsAnotherIteration = true;
204 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
205}
206
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000207/// PromoteLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000208/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000209/// we promote it. At this point, we know that the result and operand types are
210/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
211/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000212SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
213 MVT::ValueType DestVT,
214 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000215 // First step, figure out the appropriate *INT_TO_FP operation to use.
216 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000217
Chris Lattnere3e847b2005-07-16 00:19:57 +0000218 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000219
Chris Lattnere3e847b2005-07-16 00:19:57 +0000220 // Scan for the appropriate larger type to use.
221 while (1) {
222 NewInTy = (MVT::ValueType)(NewInTy+1);
223 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000224
Chris Lattnere3e847b2005-07-16 00:19:57 +0000225 // If the target supports SINT_TO_FP of this type, use it.
226 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
227 default: break;
228 case TargetLowering::Legal:
229 if (!TLI.hasNativeSupportFor(NewInTy))
230 break; // Can't use this datatype.
231 // FALL THROUGH.
232 case TargetLowering::Custom:
233 OpToUse = ISD::SINT_TO_FP;
234 break;
235 }
236 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000237 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000238
Chris Lattnere3e847b2005-07-16 00:19:57 +0000239 // If the target supports UINT_TO_FP of this type, use it.
240 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
241 default: break;
242 case TargetLowering::Legal:
243 if (!TLI.hasNativeSupportFor(NewInTy))
244 break; // Can't use this datatype.
245 // FALL THROUGH.
246 case TargetLowering::Custom:
247 OpToUse = ISD::UINT_TO_FP;
248 break;
249 }
250 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000251
Chris Lattnere3e847b2005-07-16 00:19:57 +0000252 // Otherwise, try a larger type.
253 }
254
255 // Make sure to legalize any nodes we create here in the next pass.
256 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnere3e847b2005-07-16 00:19:57 +0000258 // Okay, we found the operation and type to use. Zero extend our input to the
259 // desired type then run the operation on it.
260 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000261 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
262 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000263}
264
Chris Lattner44fe26f2005-07-29 00:11:56 +0000265/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
266/// FP_TO_*INT operation of the specified operand when the target requests that
267/// we promote it. At this point, we know that the result and operand types are
268/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
269/// operation that returns a larger result.
270SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
271 MVT::ValueType DestVT,
272 bool isSigned) {
273 // First step, figure out the appropriate FP_TO*INT operation to use.
274 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000275
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000277
Chris Lattner44fe26f2005-07-29 00:11:56 +0000278 // Scan for the appropriate larger type to use.
279 while (1) {
280 NewOutTy = (MVT::ValueType)(NewOutTy+1);
281 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000282
Chris Lattner44fe26f2005-07-29 00:11:56 +0000283 // If the target supports FP_TO_SINT returning this type, use it.
284 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
285 default: break;
286 case TargetLowering::Legal:
287 if (!TLI.hasNativeSupportFor(NewOutTy))
288 break; // Can't use this datatype.
289 // FALL THROUGH.
290 case TargetLowering::Custom:
291 OpToUse = ISD::FP_TO_SINT;
292 break;
293 }
294 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000295
Chris Lattner44fe26f2005-07-29 00:11:56 +0000296 // If the target supports FP_TO_UINT of this type, use it.
297 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
298 default: break;
299 case TargetLowering::Legal:
300 if (!TLI.hasNativeSupportFor(NewOutTy))
301 break; // Can't use this datatype.
302 // FALL THROUGH.
303 case TargetLowering::Custom:
304 OpToUse = ISD::FP_TO_UINT;
305 break;
306 }
307 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000308
Chris Lattner44fe26f2005-07-29 00:11:56 +0000309 // Otherwise, try a larger type.
310 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000311
Chris Lattner44fe26f2005-07-29 00:11:56 +0000312 // Make sure to legalize any nodes we create here in the next pass.
313 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000314
Chris Lattner44fe26f2005-07-29 00:11:56 +0000315 // Okay, we found the operation and type to use. Truncate the result of the
316 // extended FP_TO_*INT operation to the desired size.
317 return DAG.getNode(ISD::TRUNCATE, DestVT,
318 DAG.getNode(OpToUse, NewOutTy, LegalOp));
319}
320
321
Chris Lattnerdc750592005-01-07 07:47:09 +0000322void SelectionDAGLegalize::LegalizeDAG() {
323 SDOperand OldRoot = DAG.getRoot();
324 SDOperand NewRoot = LegalizeOp(OldRoot);
325 DAG.setRoot(NewRoot);
326
327 ExpandedNodes.clear();
328 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000329 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000330
331 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000332 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000333}
334
335SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000336 assert(getTypeAction(Op.getValueType()) == Legal &&
337 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000338 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000339
Chris Lattnerdc750592005-01-07 07:47:09 +0000340 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000341 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000342 if (Node->getNumValues() > 1) {
343 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
344 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000345 case Legal: break; // Nothing to do.
346 case Expand: {
347 SDOperand T1, T2;
348 ExpandOp(Op.getValue(i), T1, T2);
349 assert(LegalizedNodes.count(Op) &&
350 "Expansion didn't add legal operands!");
351 return LegalizedNodes[Op];
352 }
353 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000354 PromoteOp(Op.getValue(i));
355 assert(LegalizedNodes.count(Op) &&
356 "Expansion didn't add legal operands!");
357 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000358 }
359 }
360
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000361 // Note that LegalizeOp may be reentered even from single-use nodes, which
362 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000363 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
364 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000365
Chris Lattnerec26b482005-01-09 19:03:49 +0000366 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000367
368 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000369
370 switch (Node->getOpcode()) {
371 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000372 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
373 // If this is a target node, legalize it by legalizing the operands then
374 // passing it through.
375 std::vector<SDOperand> Ops;
376 bool Changed = false;
377 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
378 Ops.push_back(LegalizeOp(Node->getOperand(i)));
379 Changed = Changed || Node->getOperand(i) != Ops.back();
380 }
381 if (Changed)
382 if (Node->getNumValues() == 1)
383 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
384 else {
385 std::vector<MVT::ValueType> VTs(Node->value_begin(),
386 Node->value_end());
387 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
388 }
389
390 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
391 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
392 return Result.getValue(Op.ResNo);
393 }
394 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000395 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
396 assert(0 && "Do not know how to legalize this operator!");
397 abort();
398 case ISD::EntryToken:
399 case ISD::FrameIndex:
400 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000401 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000402 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000403 assert(getTypeAction(Node->getValueType(0)) == Legal &&
404 "This must be legal!");
405 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000406 case ISD::CopyFromReg:
407 Tmp1 = LegalizeOp(Node->getOperand(0));
408 if (Tmp1 != Node->getOperand(0))
409 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
410 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000411 else
412 Result = Op.getValue(0);
413
414 // Since CopyFromReg produces two values, make sure to remember that we
415 // legalized both of them.
416 AddLegalizedOperand(Op.getValue(0), Result);
417 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
418 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000419 case ISD::ImplicitDef:
420 Tmp1 = LegalizeOp(Node->getOperand(0));
421 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000422 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000423 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000424 case ISD::UNDEF: {
425 MVT::ValueType VT = Op.getValueType();
426 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000427 default: assert(0 && "This action is not supported yet!");
428 case TargetLowering::Expand:
429 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000430 if (MVT::isInteger(VT))
431 Result = DAG.getConstant(0, VT);
432 else if (MVT::isFloatingPoint(VT))
433 Result = DAG.getConstantFP(0, VT);
434 else
435 assert(0 && "Unknown value type!");
436 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000437 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000438 break;
439 }
440 break;
441 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000442 case ISD::Constant:
443 // We know we don't need to expand constants here, constants only have one
444 // value and we check that it is fine above.
445
446 // FIXME: Maybe we should handle things like targets that don't support full
447 // 32-bit immediates?
448 break;
449 case ISD::ConstantFP: {
450 // Spill FP immediates to the constant pool if the target cannot directly
451 // codegen them. Targets often have some immediate values that can be
452 // efficiently generated into an FP register without a load. We explicitly
453 // leave these constants as ConstantFP nodes for the target to deal with.
454
455 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
456
457 // Check to see if this FP immediate is already legal.
458 bool isLegal = false;
459 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
460 E = TLI.legal_fpimm_end(); I != E; ++I)
461 if (CFP->isExactlyValue(*I)) {
462 isLegal = true;
463 break;
464 }
465
466 if (!isLegal) {
467 // Otherwise we need to spill the constant to memory.
468 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
469
470 bool Extend = false;
471
472 // If a FP immediate is precise when represented as a float, we put it
473 // into the constant pool as a float, even if it's is statically typed
474 // as a double.
475 MVT::ValueType VT = CFP->getValueType(0);
476 bool isDouble = VT == MVT::f64;
477 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
478 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000479 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
480 // Only do this if the target has a native EXTLOAD instruction from
481 // f32.
482 TLI.getOperationAction(ISD::EXTLOAD,
483 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000484 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
485 VT = MVT::f32;
486 Extend = true;
487 }
Misha Brukman835702a2005-04-21 22:36:52 +0000488
Chris Lattnerdc750592005-01-07 07:47:09 +0000489 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
490 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000491 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000492 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
493 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000494 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000495 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
496 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000497 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000498 }
499 break;
500 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000501 case ISD::TokenFactor: {
502 std::vector<SDOperand> Ops;
503 bool Changed = false;
504 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000505 SDOperand Op = Node->getOperand(i);
506 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000507 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000508 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
509 Changed = true;
510 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
511 Ops.push_back(LegalizeOp(Op.getOperand(j)));
512 } else {
513 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
514 Changed |= Ops[i] != Op;
515 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000516 }
517 if (Changed)
518 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
519 break;
520 }
521
Chris Lattner2dce7032005-05-12 23:24:06 +0000522 case ISD::CALLSEQ_START:
523 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000524 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000525 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000526 Tmp2 = Node->getOperand(0);
527 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000528 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000529
530 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
531 // this will cause the maps used to memoize results to get confused.
532 // Create and add a dummy use, just to increase its use count. This will
533 // be removed at the end of legalize when dead nodes are removed.
534 if (Tmp2.Val->hasOneUse())
535 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
536 DAG.getConstant(0, MVT::i32));
537 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000538 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000539 // nodes are treated specially and are mutated in place. This makes the dag
540 // legalization process more efficient and also makes libcall insertion
541 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000542 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000543 case ISD::DYNAMIC_STACKALLOC:
544 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
545 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
546 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
547 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000548 Tmp3 != Node->getOperand(2)) {
549 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
550 std::vector<SDOperand> Ops;
551 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
552 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
553 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000554 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000555
556 // Since this op produces two values, make sure to remember that we
557 // legalized both of them.
558 AddLegalizedOperand(SDOperand(Node, 0), Result);
559 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
560 return Result.getValue(Op.ResNo);
561
Chris Lattnerd0feb642005-05-13 18:43:43 +0000562 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000563 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000564 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
565 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000566
567 bool Changed = false;
568 std::vector<SDOperand> Ops;
569 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
570 Ops.push_back(LegalizeOp(Node->getOperand(i)));
571 Changed |= Ops.back() != Node->getOperand(i);
572 }
573
574 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000575 std::vector<MVT::ValueType> RetTyVTs;
576 RetTyVTs.reserve(Node->getNumValues());
577 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000578 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000579 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
580 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000581 } else {
582 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000583 }
Chris Lattner9242c502005-01-09 19:43:23 +0000584 // Since calls produce multiple values, make sure to remember that we
585 // legalized all of them.
586 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
587 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
588 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000589 }
Chris Lattner68a12142005-01-07 22:12:08 +0000590 case ISD::BR:
591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
592 if (Tmp1 != Node->getOperand(0))
593 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
594 break;
595
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000596 case ISD::BRCOND:
597 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000598
599 switch (getTypeAction(Node->getOperand(1).getValueType())) {
600 case Expand: assert(0 && "It's impossible to expand bools");
601 case Legal:
602 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
603 break;
604 case Promote:
605 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
606 break;
607 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000608 // Basic block destination (Op#2) is always legal.
609 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
610 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
611 Node->getOperand(2));
612 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000613 case ISD::BRCONDTWOWAY:
614 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
615 switch (getTypeAction(Node->getOperand(1).getValueType())) {
616 case Expand: assert(0 && "It's impossible to expand bools");
617 case Legal:
618 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
619 break;
620 case Promote:
621 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
622 break;
623 }
624 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
625 // pair.
626 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
627 case TargetLowering::Promote:
628 default: assert(0 && "This action is not supported yet!");
629 case TargetLowering::Legal:
630 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
631 std::vector<SDOperand> Ops;
632 Ops.push_back(Tmp1);
633 Ops.push_back(Tmp2);
634 Ops.push_back(Node->getOperand(2));
635 Ops.push_back(Node->getOperand(3));
636 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
637 }
638 break;
639 case TargetLowering::Expand:
640 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
641 Node->getOperand(2));
642 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
643 break;
644 }
645 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000646
Chris Lattnerdc750592005-01-07 07:47:09 +0000647 case ISD::LOAD:
648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
649 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000650
Chris Lattnerdc750592005-01-07 07:47:09 +0000651 if (Tmp1 != Node->getOperand(0) ||
652 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000653 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
654 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000655 else
656 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000657
Chris Lattnerea4ca942005-01-07 22:28:47 +0000658 // Since loads produce two values, make sure to remember that we legalized
659 // both of them.
660 AddLegalizedOperand(SDOperand(Node, 0), Result);
661 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
662 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000663
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000664 case ISD::EXTLOAD:
665 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000666 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000667 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
668 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000669
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000670 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000671 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000672 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000673 case TargetLowering::Promote:
674 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000675 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
676 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000677 // Since loads produce two values, make sure to remember that we legalized
678 // both of them.
679 AddLegalizedOperand(SDOperand(Node, 0), Result);
680 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
681 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000682
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000683 case TargetLowering::Legal:
684 if (Tmp1 != Node->getOperand(0) ||
685 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000686 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
687 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000688 else
689 Result = SDOperand(Node, 0);
690
691 // Since loads produce two values, make sure to remember that we legalized
692 // both of them.
693 AddLegalizedOperand(SDOperand(Node, 0), Result);
694 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
695 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000696 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000697 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
698 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
699 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000700 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000701 if (Op.ResNo)
702 return Load.getValue(1);
703 return Result;
704 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000705 assert(Node->getOpcode() != ISD::EXTLOAD &&
706 "EXTLOAD should always be supported!");
707 // Turn the unsupported load into an EXTLOAD followed by an explicit
708 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000709 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
710 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000711 SDOperand ValRes;
712 if (Node->getOpcode() == ISD::SEXTLOAD)
713 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000714 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000715 else
716 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000717 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
718 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
719 if (Op.ResNo)
720 return Result.getValue(1);
721 return ValRes;
722 }
723 assert(0 && "Unreachable");
724 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000725 case ISD::EXTRACT_ELEMENT:
726 // Get both the low and high parts.
727 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
728 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
729 Result = Tmp2; // 1 -> Hi
730 else
731 Result = Tmp1; // 0 -> Lo
732 break;
733
734 case ISD::CopyToReg:
735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000736
Chris Lattnerdc750592005-01-07 07:47:09 +0000737 switch (getTypeAction(Node->getOperand(1).getValueType())) {
738 case Legal:
739 // Legalize the incoming value (must be legal).
740 Tmp2 = LegalizeOp(Node->getOperand(1));
741 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000742 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000743 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000744 case Promote:
745 Tmp2 = PromoteOp(Node->getOperand(1));
746 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
747 break;
748 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000749 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000750 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000751 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000752 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
753 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
754 // Note that the copytoreg nodes are independent of each other.
755 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000756 assert(isTypeLegal(Result.getValueType()) &&
757 "Cannot expand multiple times yet (i64 -> i16)");
758 break;
759 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000760 break;
761
762 case ISD::RET:
763 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
764 switch (Node->getNumOperands()) {
765 case 2: // ret val
766 switch (getTypeAction(Node->getOperand(1).getValueType())) {
767 case Legal:
768 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000769 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000770 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
771 break;
772 case Expand: {
773 SDOperand Lo, Hi;
774 ExpandOp(Node->getOperand(1), Lo, Hi);
775 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000776 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000777 }
778 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000779 Tmp2 = PromoteOp(Node->getOperand(1));
780 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
781 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000782 }
783 break;
784 case 1: // ret void
785 if (Tmp1 != Node->getOperand(0))
786 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
787 break;
788 default: { // ret <values>
789 std::vector<SDOperand> NewValues;
790 NewValues.push_back(Tmp1);
791 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
792 switch (getTypeAction(Node->getOperand(i).getValueType())) {
793 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000794 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000795 break;
796 case Expand: {
797 SDOperand Lo, Hi;
798 ExpandOp(Node->getOperand(i), Lo, Hi);
799 NewValues.push_back(Lo);
800 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000801 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000802 }
803 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000804 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000805 }
806 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
807 break;
808 }
809 }
810 break;
811 case ISD::STORE:
812 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
813 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
814
Chris Lattnere69daaf2005-01-08 06:25:56 +0000815 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000816 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000817 if (CFP->getValueType(0) == MVT::f32) {
818 union {
819 unsigned I;
820 float F;
821 } V;
822 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000823 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000824 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000825 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000826 } else {
827 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
828 union {
829 uint64_t I;
830 double F;
831 } V;
832 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000833 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000834 DAG.getConstant(V.I, MVT::i64), Tmp2,
835 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000836 }
Chris Lattnera4743132005-02-22 07:23:39 +0000837 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000838 }
839
Chris Lattnerdc750592005-01-07 07:47:09 +0000840 switch (getTypeAction(Node->getOperand(1).getValueType())) {
841 case Legal: {
842 SDOperand Val = LegalizeOp(Node->getOperand(1));
843 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
844 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000845 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
846 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000847 break;
848 }
849 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000850 // Truncate the value and store the result.
851 Tmp3 = PromoteOp(Node->getOperand(1));
852 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000853 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +0000854 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000855 break;
856
Chris Lattnerdc750592005-01-07 07:47:09 +0000857 case Expand:
858 SDOperand Lo, Hi;
859 ExpandOp(Node->getOperand(1), Lo, Hi);
860
861 if (!TLI.isLittleEndian())
862 std::swap(Lo, Hi);
863
Chris Lattner55e9cde2005-05-11 04:51:16 +0000864 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
865 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000866 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000867 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
868 getIntPtrConstant(IncrementSize));
869 assert(isTypeLegal(Tmp2.getValueType()) &&
870 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000871 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000872 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
873 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000874 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
875 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000876 }
877 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000878 case ISD::PCMARKER:
879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000880 if (Tmp1 != Node->getOperand(0))
881 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000882 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000883 case ISD::TRUNCSTORE:
884 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
885 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
886
887 switch (getTypeAction(Node->getOperand(1).getValueType())) {
888 case Legal:
889 Tmp2 = LegalizeOp(Node->getOperand(1));
890 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
891 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000892 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +0000893 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000894 break;
895 case Promote:
896 case Expand:
897 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
898 }
899 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000900 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000901 switch (getTypeAction(Node->getOperand(0).getValueType())) {
902 case Expand: assert(0 && "It's impossible to expand bools");
903 case Legal:
904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
905 break;
906 case Promote:
907 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
908 break;
909 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000910 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000911 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000912
913 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
914 default: assert(0 && "This action is not supported yet!");
915 case TargetLowering::Legal:
916 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
917 Tmp3 != Node->getOperand(2))
918 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
919 Tmp1, Tmp2, Tmp3);
920 break;
921 case TargetLowering::Promote: {
922 MVT::ValueType NVT =
923 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
924 unsigned ExtOp, TruncOp;
925 if (MVT::isInteger(Tmp2.getValueType())) {
926 ExtOp = ISD::ZERO_EXTEND;
927 TruncOp = ISD::TRUNCATE;
928 } else {
929 ExtOp = ISD::FP_EXTEND;
930 TruncOp = ISD::FP_ROUND;
931 }
932 // Promote each of the values to the new type.
933 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
934 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
935 // Perform the larger operation, then round down.
936 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
937 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
938 break;
939 }
940 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000941 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000942 case ISD::SETCC:
943 switch (getTypeAction(Node->getOperand(0).getValueType())) {
944 case Legal:
945 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
946 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
947 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
948 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000949 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000950 break;
951 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000952 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
953 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
954
955 // If this is an FP compare, the operands have already been extended.
956 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
957 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000958 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000959
960 // Otherwise, we have to insert explicit sign or zero extends. Note
961 // that we could insert sign extends for ALL conditions, but zero extend
962 // is cheaper on many machines (an AND instead of two shifts), so prefer
963 // it.
964 switch (cast<SetCCSDNode>(Node)->getCondition()) {
965 default: assert(0 && "Unknown integer comparison!");
966 case ISD::SETEQ:
967 case ISD::SETNE:
968 case ISD::SETUGE:
969 case ISD::SETUGT:
970 case ISD::SETULE:
971 case ISD::SETULT:
972 // ALL of these operations will work if we either sign or zero extend
973 // the operands (including the unsigned comparisons!). Zero extend is
974 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000975 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
976 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000977 break;
978 case ISD::SETGE:
979 case ISD::SETGT:
980 case ISD::SETLT:
981 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +0000982 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
983 DAG.getValueType(VT));
984 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
985 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +0000986 break;
987 }
988
989 }
990 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000991 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000992 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000993 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000994 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
995 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
996 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
997 switch (cast<SetCCSDNode>(Node)->getCondition()) {
998 case ISD::SETEQ:
999 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001000 if (RHSLo == RHSHi)
1001 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1002 if (RHSCST->isAllOnesValue()) {
1003 // Comparison to -1.
1004 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +00001005 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +00001006 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +00001007 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001008 }
1009
Chris Lattnerdc750592005-01-07 07:47:09 +00001010 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1011 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1012 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001013 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001014 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +00001015 DAG.getConstant(0, Tmp1.getValueType()));
1016 break;
1017 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001018 // If this is a comparison of the sign bit, just look at the top part.
1019 // X > -1, x < 0
1020 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +00001021 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001022 CST->getValue() == 0) || // X < 0
1023 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
1024 (CST->isAllOnesValue()))) // X > -1
1025 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1026 Node->getValueType(0), LHSHi, RHSHi);
1027
Chris Lattnerdc750592005-01-07 07:47:09 +00001028 // FIXME: This generated code sucks.
1029 ISD::CondCode LowCC;
1030 switch (cast<SetCCSDNode>(Node)->getCondition()) {
1031 default: assert(0 && "Unknown integer setcc!");
1032 case ISD::SETLT:
1033 case ISD::SETULT: LowCC = ISD::SETULT; break;
1034 case ISD::SETGT:
1035 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1036 case ISD::SETLE:
1037 case ISD::SETULE: LowCC = ISD::SETULE; break;
1038 case ISD::SETGE:
1039 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1040 }
Misha Brukman835702a2005-04-21 22:36:52 +00001041
Chris Lattnerdc750592005-01-07 07:47:09 +00001042 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1043 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1044 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1045
1046 // NOTE: on targets without efficient SELECT of bools, we can always use
1047 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001048 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +00001049 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001050 Node->getValueType(0), LHSHi, RHSHi);
1051 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
1052 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1053 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001054 break;
1055 }
1056 }
1057 break;
1058
Chris Lattner85d70c62005-01-11 05:57:22 +00001059 case ISD::MEMSET:
1060 case ISD::MEMCPY:
1061 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001062 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001063 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1064
1065 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1066 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1067 case Expand: assert(0 && "Cannot expand a byte!");
1068 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001069 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001070 break;
1071 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001072 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001073 break;
1074 }
1075 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001076 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001077 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001078
1079 SDOperand Tmp4;
1080 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001081 case Expand: {
1082 // Length is too big, just take the lo-part of the length.
1083 SDOperand HiPart;
1084 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1085 break;
1086 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001087 case Legal:
1088 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001089 break;
1090 case Promote:
1091 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001092 break;
1093 }
1094
1095 SDOperand Tmp5;
1096 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1097 case Expand: assert(0 && "Cannot expand this yet!");
1098 case Legal:
1099 Tmp5 = LegalizeOp(Node->getOperand(4));
1100 break;
1101 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001102 Tmp5 = PromoteOp(Node->getOperand(4));
1103 break;
1104 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001105
1106 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1107 default: assert(0 && "This action not implemented for this operation!");
1108 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001109 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1110 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1111 Tmp5 != Node->getOperand(4)) {
1112 std::vector<SDOperand> Ops;
1113 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1114 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1115 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1116 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001117 break;
1118 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001119 // Otherwise, the target does not support this operation. Lower the
1120 // operation to an explicit libcall as appropriate.
1121 MVT::ValueType IntPtr = TLI.getPointerTy();
1122 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1123 std::vector<std::pair<SDOperand, const Type*> > Args;
1124
Reid Spencer6dced922005-01-12 14:53:45 +00001125 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001126 if (Node->getOpcode() == ISD::MEMSET) {
1127 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1128 // Extend the ubyte argument to be an int value for the call.
1129 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1130 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1131 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1132
1133 FnName = "memset";
1134 } else if (Node->getOpcode() == ISD::MEMCPY ||
1135 Node->getOpcode() == ISD::MEMMOVE) {
1136 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1137 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1138 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1139 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1140 } else {
1141 assert(0 && "Unknown op!");
1142 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001143
Chris Lattner85d70c62005-01-11 05:57:22 +00001144 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001145 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001146 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001147 Result = CallResult.second;
1148 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001149 break;
1150 }
1151 case TargetLowering::Custom:
1152 std::vector<SDOperand> Ops;
1153 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1154 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1155 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001156 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001157 Result = LegalizeOp(Result);
1158 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001159 }
1160 break;
1161 }
Chris Lattner5385db52005-05-09 20:23:03 +00001162
1163 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001164 Tmp1 = LegalizeOp(Node->getOperand(0));
1165 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001166
Chris Lattner86535992005-05-14 07:45:46 +00001167 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1168 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1169 std::vector<SDOperand> Ops;
1170 Ops.push_back(Tmp1);
1171 Ops.push_back(Tmp2);
1172 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1173 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001174 Result = SDOperand(Node, 0);
1175 // Since these produce two values, make sure to remember that we legalized
1176 // both of them.
1177 AddLegalizedOperand(SDOperand(Node, 0), Result);
1178 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1179 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001180 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001181 Tmp1 = LegalizeOp(Node->getOperand(0));
1182 Tmp2 = LegalizeOp(Node->getOperand(1));
1183 Tmp3 = LegalizeOp(Node->getOperand(2));
1184 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1185 Tmp3 != Node->getOperand(2))
1186 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1187 break;
1188
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001189 case ISD::READIO:
1190 Tmp1 = LegalizeOp(Node->getOperand(0));
1191 Tmp2 = LegalizeOp(Node->getOperand(1));
1192
1193 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1194 case TargetLowering::Custom:
1195 default: assert(0 && "This action not implemented for this operation!");
1196 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001197 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1198 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1199 std::vector<SDOperand> Ops;
1200 Ops.push_back(Tmp1);
1201 Ops.push_back(Tmp2);
1202 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1203 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001204 Result = SDOperand(Node, 0);
1205 break;
1206 case TargetLowering::Expand:
1207 // Replace this with a load from memory.
1208 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1209 Node->getOperand(1), DAG.getSrcValue(NULL));
1210 Result = LegalizeOp(Result);
1211 break;
1212 }
1213
1214 // Since these produce two values, make sure to remember that we legalized
1215 // both of them.
1216 AddLegalizedOperand(SDOperand(Node, 0), Result);
1217 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1218 return Result.getValue(Op.ResNo);
1219
1220 case ISD::WRITEIO:
1221 Tmp1 = LegalizeOp(Node->getOperand(0));
1222 Tmp2 = LegalizeOp(Node->getOperand(1));
1223 Tmp3 = LegalizeOp(Node->getOperand(2));
1224
1225 switch (TLI.getOperationAction(Node->getOpcode(),
1226 Node->getOperand(1).getValueType())) {
1227 case TargetLowering::Custom:
1228 default: assert(0 && "This action not implemented for this operation!");
1229 case TargetLowering::Legal:
1230 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1231 Tmp3 != Node->getOperand(2))
1232 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1233 break;
1234 case TargetLowering::Expand:
1235 // Replace this with a store to memory.
1236 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1237 Node->getOperand(1), Node->getOperand(2),
1238 DAG.getSrcValue(NULL));
1239 Result = LegalizeOp(Result);
1240 break;
1241 }
1242 break;
1243
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001244 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001245 case ISD::SUB_PARTS:
1246 case ISD::SHL_PARTS:
1247 case ISD::SRA_PARTS:
1248 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001249 std::vector<SDOperand> Ops;
1250 bool Changed = false;
1251 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1252 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1253 Changed |= Ops.back() != Node->getOperand(i);
1254 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001255 if (Changed) {
1256 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1257 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1258 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001259
1260 // Since these produce multiple values, make sure to remember that we
1261 // legalized all of them.
1262 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1263 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1264 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001265 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001266
1267 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001268 case ISD::ADD:
1269 case ISD::SUB:
1270 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001271 case ISD::MULHS:
1272 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001273 case ISD::UDIV:
1274 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001275 case ISD::AND:
1276 case ISD::OR:
1277 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001278 case ISD::SHL:
1279 case ISD::SRL:
1280 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001281 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001282 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1283 case Expand: assert(0 && "Not possible");
1284 case Legal:
1285 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1286 break;
1287 case Promote:
1288 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1289 break;
1290 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001291 if (Tmp1 != Node->getOperand(0) ||
1292 Tmp2 != Node->getOperand(1))
1293 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1294 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001295
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001296 case ISD::UREM:
1297 case ISD::SREM:
1298 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1299 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1300 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1301 case TargetLowering::Legal:
1302 if (Tmp1 != Node->getOperand(0) ||
1303 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001304 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001305 Tmp2);
1306 break;
1307 case TargetLowering::Promote:
1308 case TargetLowering::Custom:
1309 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001310 case TargetLowering::Expand:
1311 if (MVT::isInteger(Node->getValueType(0))) {
1312 MVT::ValueType VT = Node->getValueType(0);
1313 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1314 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1315 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1316 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1317 } else {
1318 // Floating point mod -> fmod libcall.
1319 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1320 SDOperand Dummy;
1321 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001322 }
1323 break;
1324 }
1325 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001326
Andrew Lenharth5e177822005-05-03 17:19:30 +00001327 case ISD::CTPOP:
1328 case ISD::CTTZ:
1329 case ISD::CTLZ:
1330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1331 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1332 case TargetLowering::Legal:
1333 if (Tmp1 != Node->getOperand(0))
1334 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1335 break;
1336 case TargetLowering::Promote: {
1337 MVT::ValueType OVT = Tmp1.getValueType();
1338 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001339
1340 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001341 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1342 // Perform the larger operation, then subtract if needed.
1343 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1344 switch(Node->getOpcode())
1345 {
1346 case ISD::CTPOP:
1347 Result = Tmp1;
1348 break;
1349 case ISD::CTTZ:
1350 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001351 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001352 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001353 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001354 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1355 break;
1356 case ISD::CTLZ:
1357 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001358 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1359 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001360 getSizeInBits(OVT), NVT));
1361 break;
1362 }
1363 break;
1364 }
1365 case TargetLowering::Custom:
1366 assert(0 && "Cannot custom handle this yet!");
1367 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001368 switch(Node->getOpcode())
1369 {
1370 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001371 static const uint64_t mask[6] = {
1372 0x5555555555555555ULL, 0x3333333333333333ULL,
1373 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1374 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1375 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001376 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001377 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1378 unsigned len = getSizeInBits(VT);
1379 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001380 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001381 Tmp2 = DAG.getConstant(mask[i], VT);
1382 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001383 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001384 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1385 DAG.getNode(ISD::AND, VT,
1386 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1387 Tmp2));
1388 }
1389 Result = Tmp1;
1390 break;
1391 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001392 case ISD::CTLZ: {
1393 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001394 x = x | (x >> 1);
1395 x = x | (x >> 2);
1396 ...
1397 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001398 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001399 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001400
Chris Lattner56add052005-05-11 18:35:21 +00001401 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1402 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001403 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1404 unsigned len = getSizeInBits(VT);
1405 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1406 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001407 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001408 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1409 }
1410 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001411 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001412 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001413 }
1414 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001415 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001416 // unless the target has ctlz but not ctpop, in which case we use:
1417 // { return 32 - nlz(~x & (x-1)); }
1418 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001419 MVT::ValueType VT = Tmp1.getValueType();
1420 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001421 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001422 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1423 DAG.getNode(ISD::SUB, VT, Tmp1,
1424 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001425 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1426 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1427 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001428 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001429 DAG.getConstant(getSizeInBits(VT), VT),
1430 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1431 } else {
1432 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1433 }
Chris Lattner72473242005-05-11 05:27:09 +00001434 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001435 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001436 default:
1437 assert(0 && "Cannot expand this yet!");
1438 break;
1439 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001440 break;
1441 }
1442 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001443
Chris Lattner13fe99c2005-04-02 05:00:07 +00001444 // Unary operators
1445 case ISD::FABS:
1446 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001447 case ISD::FSQRT:
1448 case ISD::FSIN:
1449 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001450 Tmp1 = LegalizeOp(Node->getOperand(0));
1451 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1452 case TargetLowering::Legal:
1453 if (Tmp1 != Node->getOperand(0))
1454 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1455 break;
1456 case TargetLowering::Promote:
1457 case TargetLowering::Custom:
1458 assert(0 && "Cannot promote/custom handle this yet!");
1459 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001460 switch(Node->getOpcode()) {
1461 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001462 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1463 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1464 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1465 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001466 break;
1467 }
1468 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001469 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1470 MVT::ValueType VT = Node->getValueType(0);
1471 Tmp2 = DAG.getConstantFP(0.0, VT);
1472 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1473 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1474 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1475 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001476 break;
1477 }
1478 case ISD::FSQRT:
1479 case ISD::FSIN:
1480 case ISD::FCOS: {
1481 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001482 const char *FnName = 0;
1483 switch(Node->getOpcode()) {
1484 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1485 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1486 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1487 default: assert(0 && "Unreachable!");
1488 }
Nate Begeman77558da2005-08-04 21:43:28 +00001489 SDOperand Dummy;
1490 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001491 break;
1492 }
1493 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001494 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001495 }
1496 break;
1497 }
1498 break;
1499
1500 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001501 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001502 case ISD::UINT_TO_FP: {
1503 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001504 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1505 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001506 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001507 Node->getOperand(0).getValueType())) {
1508 default: assert(0 && "Unknown operation action!");
1509 case TargetLowering::Expand:
1510 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP yet");
1511 Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1512 Node->getValueType(0));
1513 AddLegalizedOperand(Op, Result);
1514 return Result;
1515 case TargetLowering::Promote:
1516 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1517 Node->getValueType(0),
1518 isSigned);
1519 AddLegalizedOperand(Op, Result);
1520 return Result;
1521 case TargetLowering::Legal:
1522 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001523 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001524
Chris Lattnerdc750592005-01-07 07:47:09 +00001525 Tmp1 = LegalizeOp(Node->getOperand(0));
1526 if (Tmp1 != Node->getOperand(0))
1527 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1528 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001529 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001530 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1531 Node->getValueType(0), Node->getOperand(0));
1532 break;
1533 case Promote:
1534 if (isSigned) {
1535 Result = PromoteOp(Node->getOperand(0));
1536 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1537 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1538 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1539 } else {
1540 Result = PromoteOp(Node->getOperand(0));
1541 Result = DAG.getZeroExtendInReg(Result,
1542 Node->getOperand(0).getValueType());
1543 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001544 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001545 break;
1546 }
1547 break;
1548 }
1549 case ISD::TRUNCATE:
1550 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1551 case Legal:
1552 Tmp1 = LegalizeOp(Node->getOperand(0));
1553 if (Tmp1 != Node->getOperand(0))
1554 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1555 break;
1556 case Expand:
1557 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1558
1559 // Since the result is legal, we should just be able to truncate the low
1560 // part of the source.
1561 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1562 break;
1563 case Promote:
1564 Result = PromoteOp(Node->getOperand(0));
1565 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1566 break;
1567 }
1568 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001569
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001570 case ISD::FP_TO_SINT:
1571 case ISD::FP_TO_UINT:
1572 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1573 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001574 Tmp1 = LegalizeOp(Node->getOperand(0));
1575
Chris Lattner44fe26f2005-07-29 00:11:56 +00001576 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1577 default: assert(0 && "Unknown operation action!");
1578 case TargetLowering::Expand:
1579 assert(0 && "Cannot expand FP_TO*INT yet");
1580 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001581 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001582 Node->getOpcode() == ISD::FP_TO_SINT);
1583 AddLegalizedOperand(Op, Result);
1584 return Result;
1585 case TargetLowering::Legal:
1586 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001587 case TargetLowering::Custom:
1588 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1589 Result = TLI.LowerOperation(Result, DAG);
1590 AddLegalizedOperand(Op, Result);
1591 NeedsAnotherIteration = true;
1592 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001593 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001594
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001595 if (Tmp1 != Node->getOperand(0))
1596 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1597 break;
1598 case Expand:
1599 assert(0 && "Shouldn't need to expand other operators here!");
1600 case Promote:
1601 Result = PromoteOp(Node->getOperand(0));
1602 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1603 break;
1604 }
1605 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001606
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001607 case ISD::ZERO_EXTEND:
1608 case ISD::SIGN_EXTEND:
1609 case ISD::FP_EXTEND:
1610 case ISD::FP_ROUND:
1611 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1612 case Legal:
1613 Tmp1 = LegalizeOp(Node->getOperand(0));
1614 if (Tmp1 != Node->getOperand(0))
1615 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1616 break;
1617 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001618 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001619
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001620 case Promote:
1621 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001622 case ISD::ZERO_EXTEND:
1623 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001624 // NOTE: Any extend would work here...
1625 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001626 Result = DAG.getZeroExtendInReg(Result,
1627 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001628 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001629 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001630 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001631 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001632 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001633 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001634 Result,
1635 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001636 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001637 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001638 Result = PromoteOp(Node->getOperand(0));
1639 if (Result.getValueType() != Op.getValueType())
1640 // Dynamically dead while we have only 2 FP types.
1641 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1642 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001643 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001644 Result = PromoteOp(Node->getOperand(0));
1645 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1646 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001647 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001648 }
1649 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001650 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001651 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001652 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001653 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001654
1655 // If this operation is not supported, convert it to a shl/shr or load/store
1656 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001657 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1658 default: assert(0 && "This action not supported for this op yet!");
1659 case TargetLowering::Legal:
1660 if (Tmp1 != Node->getOperand(0))
1661 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001662 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001663 break;
1664 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001665 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001666 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001667 // NOTE: we could fall back on load/store here too for targets without
1668 // SAR. However, it is doubtful that any exist.
1669 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1670 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001671 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001672 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1673 Node->getOperand(0), ShiftCst);
1674 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1675 Result, ShiftCst);
1676 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1677 // The only way we can lower this is to turn it into a STORETRUNC,
1678 // EXTLOAD pair, targetting a temporary location (a stack slot).
1679
1680 // NOTE: there is a choice here between constantly creating new stack
1681 // slots and always reusing the same one. We currently always create
1682 // new ones, as reuse may inhibit scheduling.
1683 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1684 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1685 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1686 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001687 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001688 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1689 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1690 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001691 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001692 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001693 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1694 Result, StackSlot, DAG.getSrcValue(NULL),
1695 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001696 } else {
1697 assert(0 && "Unknown op");
1698 }
1699 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001700 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001701 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001702 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001703 }
Chris Lattner99222f72005-01-15 07:15:18 +00001704 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001705
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001706 // Note that LegalizeOp may be reentered even from single-use nodes, which
1707 // means that we always must cache transformed nodes.
1708 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001709 return Result;
1710}
1711
Chris Lattner4d978642005-01-15 22:16:26 +00001712/// PromoteOp - Given an operation that produces a value in an invalid type,
1713/// promote it to compute the value into a larger type. The produced value will
1714/// have the correct bits for the low portion of the register, but no guarantee
1715/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001716SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1717 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001718 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001719 assert(getTypeAction(VT) == Promote &&
1720 "Caller should expand or legalize operands that are not promotable!");
1721 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1722 "Cannot promote to smaller type!");
1723
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001724 SDOperand Tmp1, Tmp2, Tmp3;
1725
1726 SDOperand Result;
1727 SDNode *Node = Op.Val;
1728
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001729 if (!Node->hasOneUse()) {
1730 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1731 if (I != PromotedNodes.end()) return I->second;
1732 } else {
1733 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1734 }
1735
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001736 // Promotion needs an optimization step to clean up after it, and is not
1737 // careful to avoid operations the target does not support. Make sure that
1738 // all generated operations are legalized in the next iteration.
1739 NeedsAnotherIteration = true;
1740
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001741 switch (Node->getOpcode()) {
1742 default:
1743 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1744 assert(0 && "Do not know how to promote this operator!");
1745 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001746 case ISD::UNDEF:
1747 Result = DAG.getNode(ISD::UNDEF, NVT);
1748 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001749 case ISD::Constant:
1750 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1751 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1752 break;
1753 case ISD::ConstantFP:
1754 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1755 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1756 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001757 case ISD::CopyFromReg:
1758 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1759 Node->getOperand(0));
1760 // Remember that we legalized the chain.
1761 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1762 break;
1763
Chris Lattner2cb338d2005-01-18 02:59:52 +00001764 case ISD::SETCC:
1765 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1766 "SetCC type is not legal??");
1767 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1768 TLI.getSetCCResultTy(), Node->getOperand(0),
1769 Node->getOperand(1));
1770 Result = LegalizeOp(Result);
1771 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001772
1773 case ISD::TRUNCATE:
1774 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1775 case Legal:
1776 Result = LegalizeOp(Node->getOperand(0));
1777 assert(Result.getValueType() >= NVT &&
1778 "This truncation doesn't make sense!");
1779 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1780 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1781 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001782 case Promote:
1783 // The truncation is not required, because we don't guarantee anything
1784 // about high bits anyway.
1785 Result = PromoteOp(Node->getOperand(0));
1786 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001787 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001788 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1789 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00001790 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001791 }
1792 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001793 case ISD::SIGN_EXTEND:
1794 case ISD::ZERO_EXTEND:
1795 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1796 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1797 case Legal:
1798 // Input is legal? Just do extend all the way to the larger type.
1799 Result = LegalizeOp(Node->getOperand(0));
1800 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1801 break;
1802 case Promote:
1803 // Promote the reg if it's smaller.
1804 Result = PromoteOp(Node->getOperand(0));
1805 // The high bits are not guaranteed to be anything. Insert an extend.
1806 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001807 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00001808 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001809 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001810 Result = DAG.getZeroExtendInReg(Result,
1811 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001812 break;
1813 }
1814 break;
1815
1816 case ISD::FP_EXTEND:
1817 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1818 case ISD::FP_ROUND:
1819 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1820 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1821 case Promote: assert(0 && "Unreachable with 2 FP types!");
1822 case Legal:
1823 // Input is legal? Do an FP_ROUND_INREG.
1824 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001825 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1826 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001827 break;
1828 }
1829 break;
1830
1831 case ISD::SINT_TO_FP:
1832 case ISD::UINT_TO_FP:
1833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1834 case Legal:
1835 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001836 // No extra round required here.
1837 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001838 break;
1839
1840 case Promote:
1841 Result = PromoteOp(Node->getOperand(0));
1842 if (Node->getOpcode() == ISD::SINT_TO_FP)
1843 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001844 Result,
1845 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001846 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001847 Result = DAG.getZeroExtendInReg(Result,
1848 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001849 // No extra round required here.
1850 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001851 break;
1852 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001853 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1854 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001855 // Round if we cannot tolerate excess precision.
1856 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001857 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1858 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00001859 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001860 }
Chris Lattner4d978642005-01-15 22:16:26 +00001861 break;
1862
1863 case ISD::FP_TO_SINT:
1864 case ISD::FP_TO_UINT:
1865 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1866 case Legal:
1867 Tmp1 = LegalizeOp(Node->getOperand(0));
1868 break;
1869 case Promote:
1870 // The input result is prerounded, so we don't have to do anything
1871 // special.
1872 Tmp1 = PromoteOp(Node->getOperand(0));
1873 break;
1874 case Expand:
1875 assert(0 && "not implemented");
1876 }
1877 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1878 break;
1879
Chris Lattner13fe99c2005-04-02 05:00:07 +00001880 case ISD::FABS:
1881 case ISD::FNEG:
1882 Tmp1 = PromoteOp(Node->getOperand(0));
1883 assert(Tmp1.getValueType() == NVT);
1884 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1885 // NOTE: we do not have to do any extra rounding here for
1886 // NoExcessFPPrecision, because we know the input will have the appropriate
1887 // precision, and these operations don't modify precision at all.
1888 break;
1889
Chris Lattner9d6fa982005-04-28 21:44:33 +00001890 case ISD::FSQRT:
1891 case ISD::FSIN:
1892 case ISD::FCOS:
1893 Tmp1 = PromoteOp(Node->getOperand(0));
1894 assert(Tmp1.getValueType() == NVT);
1895 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1896 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001897 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1898 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00001899 break;
1900
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001901 case ISD::AND:
1902 case ISD::OR:
1903 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001904 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001905 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001906 case ISD::MUL:
1907 // The input may have strange things in the top bits of the registers, but
1908 // these operations don't care. They may have wierd bits going out, but
1909 // that too is okay if they are integer operations.
1910 Tmp1 = PromoteOp(Node->getOperand(0));
1911 Tmp2 = PromoteOp(Node->getOperand(1));
1912 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1913 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1914
1915 // However, if this is a floating point operation, they will give excess
1916 // precision that we may not be able to tolerate. If we DO allow excess
1917 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001918 // FIXME: Why would we need to round FP ops more than integer ones?
1919 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001920 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001921 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1922 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001923 break;
1924
Chris Lattner4d978642005-01-15 22:16:26 +00001925 case ISD::SDIV:
1926 case ISD::SREM:
1927 // These operators require that their input be sign extended.
1928 Tmp1 = PromoteOp(Node->getOperand(0));
1929 Tmp2 = PromoteOp(Node->getOperand(1));
1930 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00001931 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1932 DAG.getValueType(VT));
1933 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1934 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001935 }
1936 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1937
1938 // Perform FP_ROUND: this is probably overly pessimistic.
1939 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001940 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1941 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001942 break;
1943
1944 case ISD::UDIV:
1945 case ISD::UREM:
1946 // These operators require that their input be zero extended.
1947 Tmp1 = PromoteOp(Node->getOperand(0));
1948 Tmp2 = PromoteOp(Node->getOperand(1));
1949 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001950 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1951 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001952 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1953 break;
1954
1955 case ISD::SHL:
1956 Tmp1 = PromoteOp(Node->getOperand(0));
1957 Tmp2 = LegalizeOp(Node->getOperand(1));
1958 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1959 break;
1960 case ISD::SRA:
1961 // The input value must be properly sign extended.
1962 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001963 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1964 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001965 Tmp2 = LegalizeOp(Node->getOperand(1));
1966 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1967 break;
1968 case ISD::SRL:
1969 // The input value must be properly zero extended.
1970 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001971 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001972 Tmp2 = LegalizeOp(Node->getOperand(1));
1973 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1974 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001975 case ISD::LOAD:
1976 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1977 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001978 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001979 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001980 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
1981 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001982 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001983 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
1984 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001985
1986 // Remember that we legalized the chain.
1987 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1988 break;
1989 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001990 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1991 case Expand: assert(0 && "It's impossible to expand bools");
1992 case Legal:
1993 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1994 break;
1995 case Promote:
1996 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1997 break;
1998 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001999 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2000 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2001 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2002 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002003 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002004 case ISD::CALL: {
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2006 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2007
Chris Lattner3d95c142005-01-19 20:24:35 +00002008 std::vector<SDOperand> Ops;
2009 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2010 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2011
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002012 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2013 "Can only promote single result calls");
2014 std::vector<MVT::ValueType> RetTyVTs;
2015 RetTyVTs.reserve(2);
2016 RetTyVTs.push_back(NVT);
2017 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002018 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2019 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002020 Result = SDOperand(NC, 0);
2021
2022 // Insert the new chain mapping.
2023 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2024 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002025 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002026 case ISD::CTPOP:
2027 case ISD::CTTZ:
2028 case ISD::CTLZ:
2029 Tmp1 = Node->getOperand(0);
2030 //Zero extend the argument
2031 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2032 // Perform the larger operation, then subtract if needed.
2033 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2034 switch(Node->getOpcode())
2035 {
2036 case ISD::CTPOP:
2037 Result = Tmp1;
2038 break;
2039 case ISD::CTTZ:
2040 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002041 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002042 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002043 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002044 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2045 break;
2046 case ISD::CTLZ:
2047 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002048 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2049 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002050 getSizeInBits(VT), NVT));
2051 break;
2052 }
2053 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002054 }
2055
2056 assert(Result.Val && "Didn't set a result!");
2057 AddPromotedOperand(Op, Result);
2058 return Result;
2059}
Chris Lattnerdc750592005-01-07 07:47:09 +00002060
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002061/// ExpandAddSub - Find a clever way to expand this add operation into
2062/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002063void SelectionDAGLegalize::
2064ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2065 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002066 // Expand the subcomponents.
2067 SDOperand LHSL, LHSH, RHSL, RHSH;
2068 ExpandOp(LHS, LHSL, LHSH);
2069 ExpandOp(RHS, RHSL, RHSH);
2070
Chris Lattner8ffd0042005-04-11 20:29:59 +00002071 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002072 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2073 if (LHSL.getValueType() == MVT::i32) {
2074 SDOperand LowEl;
2075 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2076 if (C->getValue() == 0)
2077 LowEl = RHSL;
2078 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2079 if (C->getValue() == 0)
2080 LowEl = LHSL;
2081 if (LowEl.Val) {
2082 // Turn this into an add/sub of the high part only.
2083 SDOperand HiEl =
2084 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2085 LowEl.getValueType(), LHSH, RHSH);
2086 Lo = LowEl;
2087 Hi = HiEl;
2088 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002089 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002090 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002091
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002092 std::vector<SDOperand> Ops;
2093 Ops.push_back(LHSL);
2094 Ops.push_back(LHSH);
2095 Ops.push_back(RHSL);
2096 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002097
2098 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2099 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002100 Hi = Lo.getValue(1);
2101}
2102
Chris Lattner4157c412005-04-02 04:00:59 +00002103void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2104 SDOperand Op, SDOperand Amt,
2105 SDOperand &Lo, SDOperand &Hi) {
2106 // Expand the subcomponents.
2107 SDOperand LHSL, LHSH;
2108 ExpandOp(Op, LHSL, LHSH);
2109
2110 std::vector<SDOperand> Ops;
2111 Ops.push_back(LHSL);
2112 Ops.push_back(LHSH);
2113 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002114 std::vector<MVT::ValueType> VTs;
2115 VTs.push_back(LHSL.getValueType());
2116 VTs.push_back(LHSH.getValueType());
2117 VTs.push_back(Amt.getValueType());
2118 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002119 Hi = Lo.getValue(1);
2120}
2121
2122
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002123/// ExpandShift - Try to find a clever way to expand this shift operation out to
2124/// smaller elements. If we can't find a way that is more efficient than a
2125/// libcall on this target, return false. Otherwise, return true with the
2126/// low-parts expanded into Lo and Hi.
2127bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2128 SDOperand &Lo, SDOperand &Hi) {
2129 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2130 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002131
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002132 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002133 SDOperand ShAmt = LegalizeOp(Amt);
2134 MVT::ValueType ShTy = ShAmt.getValueType();
2135 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2136 unsigned NVTBits = MVT::getSizeInBits(NVT);
2137
2138 // Handle the case when Amt is an immediate. Other cases are currently broken
2139 // and are disabled.
2140 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2141 unsigned Cst = CN->getValue();
2142 // Expand the incoming operand to be shifted, so that we have its parts
2143 SDOperand InL, InH;
2144 ExpandOp(Op, InL, InH);
2145 switch(Opc) {
2146 case ISD::SHL:
2147 if (Cst > VTBits) {
2148 Lo = DAG.getConstant(0, NVT);
2149 Hi = DAG.getConstant(0, NVT);
2150 } else if (Cst > NVTBits) {
2151 Lo = DAG.getConstant(0, NVT);
2152 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002153 } else if (Cst == NVTBits) {
2154 Lo = DAG.getConstant(0, NVT);
2155 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002156 } else {
2157 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2158 Hi = DAG.getNode(ISD::OR, NVT,
2159 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2160 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2161 }
2162 return true;
2163 case ISD::SRL:
2164 if (Cst > VTBits) {
2165 Lo = DAG.getConstant(0, NVT);
2166 Hi = DAG.getConstant(0, NVT);
2167 } else if (Cst > NVTBits) {
2168 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2169 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002170 } else if (Cst == NVTBits) {
2171 Lo = InH;
2172 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002173 } else {
2174 Lo = DAG.getNode(ISD::OR, NVT,
2175 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2176 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2177 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2178 }
2179 return true;
2180 case ISD::SRA:
2181 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002182 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002183 DAG.getConstant(NVTBits-1, ShTy));
2184 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002185 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002186 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002187 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002188 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002189 } else if (Cst == NVTBits) {
2190 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002191 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002192 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002193 } else {
2194 Lo = DAG.getNode(ISD::OR, NVT,
2195 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2196 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2197 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2198 }
2199 return true;
2200 }
2201 }
2202 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2203 // so disable it for now. Currently targets are handling this via SHL_PARTS
2204 // and friends.
2205 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002206
2207 // If we have an efficient select operation (or if the selects will all fold
2208 // away), lower to some complex code, otherwise just emit the libcall.
2209 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2210 !isa<ConstantSDNode>(Amt))
2211 return false;
2212
2213 SDOperand InL, InH;
2214 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002215 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2216 DAG.getConstant(NVTBits, ShTy), ShAmt);
2217
Chris Lattner4d25c042005-01-20 20:29:23 +00002218 // Compare the unmasked shift amount against 32.
2219 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
2220 DAG.getConstant(NVTBits, ShTy));
2221
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002222 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2223 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2224 DAG.getConstant(NVTBits-1, ShTy));
2225 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2226 DAG.getConstant(NVTBits-1, ShTy));
2227 }
2228
2229 if (Opc == ISD::SHL) {
2230 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2231 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2232 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002233 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002234
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002235 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2236 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2237 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002238 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
2239 DAG.getSetCC(ISD::SETEQ,
2240 TLI.getSetCCResultTy(), NAmt,
2241 DAG.getConstant(32, ShTy)),
2242 DAG.getConstant(0, NVT),
2243 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002244 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002245 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002246 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002247 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002248
2249 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002250 if (Opc == ISD::SRA)
2251 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2252 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002253 else
2254 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002255 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002256 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002257 }
2258 return true;
2259}
Chris Lattneraac464e2005-01-21 06:05:23 +00002260
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002261/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2262/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002263/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002264static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002265 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002266
Chris Lattner2dce7032005-05-12 23:24:06 +00002267 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002268 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002269 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002270 Found = Node;
2271 return;
2272 }
2273
2274 // Otherwise, scan the operands of Node to see if any of them is a call.
2275 assert(Node->getNumOperands() != 0 &&
2276 "All leaves should have depth equal to the entry node!");
2277 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002278 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002279
2280 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002281 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002282 Found);
2283}
2284
2285
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002286/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2287/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002288/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002289static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2290 std::set<SDNode*> &Visited) {
2291 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2292 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002293
Chris Lattner2dce7032005-05-12 23:24:06 +00002294 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002295 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002296 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002297 Found = Node;
2298 return;
2299 }
2300
2301 // Otherwise, scan the operands of Node to see if any of them is a call.
2302 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2303 if (UI == E) return;
2304 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002305 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002306
2307 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002308 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002309}
2310
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002311/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002312/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002313static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002314 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002315 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002316 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002317 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002318
2319 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002320 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002321
Chris Lattner4add7e32005-01-23 04:42:50 +00002322 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002323 if (TheChain.getValueType() != MVT::Other)
2324 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002325 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002326
2327 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002328 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002329
Chris Lattner4add7e32005-01-23 04:42:50 +00002330 // Make sure to only follow users of our token chain.
2331 SDNode *User = *UI;
2332 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2333 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002334 if (SDNode *Result = FindCallSeqEnd(User))
2335 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002336 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002337 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002338}
2339
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002340/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002341/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002342static SDNode *FindCallSeqStart(SDNode *Node) {
2343 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002344 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002345
2346 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2347 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002348 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002349}
2350
2351
Chris Lattner4add7e32005-01-23 04:42:50 +00002352/// FindInputOutputChains - If we are replacing an operation with a call we need
2353/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002354/// properly serialize the calls in the block. The returned operand is the
2355/// input chain value for the new call (e.g. the entry node or the previous
2356/// call), and OutChain is set to be the chain node to update to point to the
2357/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002358static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2359 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002360 SDNode *LatestCallSeqStart = Entry.Val;
2361 SDNode *LatestCallSeqEnd = 0;
2362 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2363 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002364
Chris Lattner2dce7032005-05-12 23:24:06 +00002365 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002366 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002367 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2368 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002369 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2370 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002371 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002372 LatestCallSeqEnd = Entry.Val;
2373 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002374
Chris Lattner06bbeb62005-05-11 19:02:11 +00002375 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002376 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002377 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002378 std::set<SDNode*> Visited;
2379 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002380
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002381 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002382 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002383 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002384
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002385 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002386}
2387
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002388/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002389void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2390 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002391 // Nothing to splice it into?
2392 if (OutChain == 0) return;
2393
2394 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2395 //OutChain->dump();
2396
2397 // Form a token factor node merging the old inval and the new inval.
2398 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2399 OutChain->getOperand(0));
2400 // Change the node to refer to the new token.
2401 OutChain->setAdjCallChain(InToken);
2402}
Chris Lattner4add7e32005-01-23 04:42:50 +00002403
2404
Chris Lattneraac464e2005-01-21 06:05:23 +00002405// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2406// does not fit into a register, return the lo part and set the hi part to the
2407// by-reg argument. If it does fit into a single register, return the result
2408// and leave the Hi part unset.
2409SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2410 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002411 SDNode *OutChain;
2412 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2413 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002414 if (InChain.Val == 0)
2415 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002416
Chris Lattneraac464e2005-01-21 06:05:23 +00002417 TargetLowering::ArgListTy Args;
2418 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2419 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2420 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2421 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2422 }
2423 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002424
Chris Lattner06bbeb62005-05-11 19:02:11 +00002425 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002426 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002427 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002428 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2429 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002430 SpliceCallInto(CallInfo.second, OutChain);
2431
2432 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002433
2434 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002435 default: assert(0 && "Unknown thing");
2436 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002437 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002438 case Promote:
2439 assert(0 && "Cannot promote this yet!");
2440 case Expand:
2441 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002442 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002443 return Lo;
2444 }
2445}
2446
Chris Lattner4add7e32005-01-23 04:42:50 +00002447
Chris Lattneraac464e2005-01-21 06:05:23 +00002448/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2449/// destination type is legal.
2450SDOperand SelectionDAGLegalize::
2451ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2452 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2453 assert(getTypeAction(Source.getValueType()) == Expand &&
2454 "This is not an expansion!");
2455 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2456
Chris Lattner06bbeb62005-05-11 19:02:11 +00002457 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002458 assert(Source.getValueType() == MVT::i64 &&
2459 "This only works for 64-bit -> FP");
2460 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2461 // incoming integer is set. To handle this, we dynamically test to see if
2462 // it is set, and, if so, add a fudge factor.
2463 SDOperand Lo, Hi;
2464 ExpandOp(Source, Lo, Hi);
2465
Chris Lattner2a4f7312005-05-13 04:45:13 +00002466 // If this is unsigned, and not supported, first perform the conversion to
2467 // signed, then adjust the result if the sign bit is set.
2468 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2469 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2470
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002471 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2472 DAG.getConstant(0, Hi.getValueType()));
2473 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2474 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2475 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002476 uint64_t FF = 0x5f800000ULL;
2477 if (TLI.isLittleEndian()) FF <<= 32;
2478 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002479
2480 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2481 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2482 TLI.getPointerTy());
2483 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2484 SDOperand FudgeInReg;
2485 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002486 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2487 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002488 else {
2489 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002490 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2491 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002492 }
2493 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002494 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002495
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002496 // Check to see if the target has a custom way to lower this. If so, use it.
2497 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2498 default: assert(0 && "This action not implemented for this operation!");
2499 case TargetLowering::Legal:
2500 case TargetLowering::Expand:
2501 break; // This case is handled below.
2502 case TargetLowering::Custom:
2503 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002504 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002505 }
2506
Chris Lattner153587e2005-05-12 07:00:44 +00002507 // Expand the source, then glue it back together for the call. We must expand
2508 // the source in case it is shared (this pass of legalize must traverse it).
2509 SDOperand SrcLo, SrcHi;
2510 ExpandOp(Source, SrcLo, SrcHi);
2511 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2512
Chris Lattner06bbeb62005-05-11 19:02:11 +00002513 SDNode *OutChain = 0;
2514 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2515 DAG.getEntryNode());
2516 const char *FnName = 0;
2517 if (DestTy == MVT::f32)
2518 FnName = "__floatdisf";
2519 else {
2520 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2521 FnName = "__floatdidf";
2522 }
2523
Chris Lattneraac464e2005-01-21 06:05:23 +00002524 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2525
2526 TargetLowering::ArgListTy Args;
2527 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002528
Chris Lattneraac464e2005-01-21 06:05:23 +00002529 Args.push_back(std::make_pair(Source, ArgTy));
2530
2531 // We don't care about token chains for libcalls. We just use the entry
2532 // node as our input and ignore the output chain. This allows us to place
2533 // calls wherever we need them to satisfy data dependences.
2534 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002535
2536 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002537 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2538 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002539
Chris Lattnera5bf1032005-05-12 04:49:08 +00002540 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002541 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002542}
Misha Brukman835702a2005-04-21 22:36:52 +00002543
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002544
2545
Chris Lattnerdc750592005-01-07 07:47:09 +00002546/// ExpandOp - Expand the specified SDOperand into its two component pieces
2547/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2548/// LegalizeNodes map is filled in for any results that are not expanded, the
2549/// ExpandedNodes map is filled in for any results that are expanded, and the
2550/// Lo/Hi values are returned.
2551void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2552 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002553 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002554 SDNode *Node = Op.Val;
2555 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2556 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2557 assert(MVT::isInteger(NVT) && NVT < VT &&
2558 "Cannot expand to FP value or to larger int value!");
2559
2560 // If there is more than one use of this, see if we already expanded it.
2561 // There is no use remembering values that only have a single use, as the map
2562 // entries will never be reused.
2563 if (!Node->hasOneUse()) {
2564 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2565 = ExpandedNodes.find(Op);
2566 if (I != ExpandedNodes.end()) {
2567 Lo = I->second.first;
2568 Hi = I->second.second;
2569 return;
2570 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002571 } else {
2572 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002573 }
2574
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002575 // Expanding to multiple registers needs to perform an optimization step, and
2576 // is not careful to avoid operations the target does not support. Make sure
2577 // that all generated operations are legalized in the next iteration.
2578 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002579
Chris Lattnerdc750592005-01-07 07:47:09 +00002580 switch (Node->getOpcode()) {
2581 default:
2582 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2583 assert(0 && "Do not know how to expand this operator!");
2584 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002585 case ISD::UNDEF:
2586 Lo = DAG.getNode(ISD::UNDEF, NVT);
2587 Hi = DAG.getNode(ISD::UNDEF, NVT);
2588 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002589 case ISD::Constant: {
2590 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2591 Lo = DAG.getConstant(Cst, NVT);
2592 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2593 break;
2594 }
2595
2596 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002597 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002598 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002599 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2600 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002601
Chris Lattner3b8e7192005-01-14 22:38:01 +00002602 // Remember that we legalized the chain.
2603 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2604
Chris Lattnerdc750592005-01-07 07:47:09 +00002605 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2606 break;
2607 }
2608
Chris Lattner32e08b72005-03-28 22:03:13 +00002609 case ISD::BUILD_PAIR:
2610 // Legalize both operands. FIXME: in the future we should handle the case
2611 // where the two elements are not legal.
2612 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2613 Lo = LegalizeOp(Node->getOperand(0));
2614 Hi = LegalizeOp(Node->getOperand(1));
2615 break;
2616
Chris Lattner55e9cde2005-05-11 04:51:16 +00002617 case ISD::CTPOP:
2618 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002619 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2620 DAG.getNode(ISD::CTPOP, NVT, Lo),
2621 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002622 Hi = DAG.getConstant(0, NVT);
2623 break;
2624
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002625 case ISD::CTLZ: {
2626 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002627 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002628 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2629 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2630 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2631 HLZ, BitsC);
2632 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2633 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2634
2635 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2636 Hi = DAG.getConstant(0, NVT);
2637 break;
2638 }
2639
2640 case ISD::CTTZ: {
2641 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002642 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002643 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2644 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2645 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2646 LTZ, BitsC);
2647 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2648 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2649
2650 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2651 Hi = DAG.getConstant(0, NVT);
2652 break;
2653 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002654
Chris Lattnerdc750592005-01-07 07:47:09 +00002655 case ISD::LOAD: {
2656 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2657 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002658 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002659
2660 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002661 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002662 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2663 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002664 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002665 //are from the same instruction?
2666 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002667
2668 // Build a factor node to remember that this load is independent of the
2669 // other one.
2670 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2671 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002672
Chris Lattnerdc750592005-01-07 07:47:09 +00002673 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002674 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002675 if (!TLI.isLittleEndian())
2676 std::swap(Lo, Hi);
2677 break;
2678 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002679 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002680 case ISD::CALL: {
2681 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2682 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2683
Chris Lattner3d95c142005-01-19 20:24:35 +00002684 bool Changed = false;
2685 std::vector<SDOperand> Ops;
2686 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2687 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2688 Changed |= Ops.back() != Node->getOperand(i);
2689 }
2690
Chris Lattnerdc750592005-01-07 07:47:09 +00002691 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2692 "Can only expand a call once so far, not i64 -> i16!");
2693
2694 std::vector<MVT::ValueType> RetTyVTs;
2695 RetTyVTs.reserve(3);
2696 RetTyVTs.push_back(NVT);
2697 RetTyVTs.push_back(NVT);
2698 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002699 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2700 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002701 Lo = SDOperand(NC, 0);
2702 Hi = SDOperand(NC, 1);
2703
2704 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002705 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002706 break;
2707 }
2708 case ISD::AND:
2709 case ISD::OR:
2710 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2711 SDOperand LL, LH, RL, RH;
2712 ExpandOp(Node->getOperand(0), LL, LH);
2713 ExpandOp(Node->getOperand(1), RL, RH);
2714 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2715 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2716 break;
2717 }
2718 case ISD::SELECT: {
2719 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002720
2721 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2722 case Expand: assert(0 && "It's impossible to expand bools");
2723 case Legal:
2724 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2725 break;
2726 case Promote:
2727 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2728 break;
2729 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002730 ExpandOp(Node->getOperand(1), LL, LH);
2731 ExpandOp(Node->getOperand(2), RL, RH);
2732 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2733 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2734 break;
2735 }
2736 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002737 SDOperand In;
2738 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2739 case Expand: assert(0 && "expand-expand not implemented yet!");
2740 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2741 case Promote:
2742 In = PromoteOp(Node->getOperand(0));
2743 // Emit the appropriate sign_extend_inreg to get the value we want.
2744 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002745 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002746 break;
2747 }
2748
Chris Lattnerdc750592005-01-07 07:47:09 +00002749 // The low part is just a sign extension of the input (which degenerates to
2750 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002751 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002752
Chris Lattnerdc750592005-01-07 07:47:09 +00002753 // The high part is obtained by SRA'ing all but one of the bits of the lo
2754 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002755 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002756 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2757 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002758 break;
2759 }
Chris Lattner47844892005-04-03 23:41:52 +00002760 case ISD::ZERO_EXTEND: {
2761 SDOperand In;
2762 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2763 case Expand: assert(0 && "expand-expand not implemented yet!");
2764 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2765 case Promote:
2766 In = PromoteOp(Node->getOperand(0));
2767 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002768 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002769 break;
2770 }
2771
Chris Lattnerdc750592005-01-07 07:47:09 +00002772 // The low part is just a zero extension of the input (which degenerates to
2773 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002774 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002775
Chris Lattnerdc750592005-01-07 07:47:09 +00002776 // The high part is just a zero.
2777 Hi = DAG.getConstant(0, NVT);
2778 break;
Chris Lattner47844892005-04-03 23:41:52 +00002779 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002780 // These operators cannot be expanded directly, emit them as calls to
2781 // library functions.
2782 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00002783 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00002784 SDOperand Op;
2785 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2786 case Expand: assert(0 && "cannot expand FP!");
2787 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
2788 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
2789 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002790
Chris Lattner941d84a2005-07-30 01:40:57 +00002791 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
2792
Chris Lattnerfe68d752005-07-29 00:33:32 +00002793 // Now that the custom expander is done, expand the result, which is still
2794 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00002795 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00002796 break;
2797 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002798
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002799 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002800 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002801 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002802 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002803 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002804
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002805 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00002806 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
2807 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
2808 LegalizeOp(Node->getOperand(0)));
2809 // Now that the custom expander is done, expand the result, which is still
2810 // VT.
2811 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
2812 break;
2813 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002814
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002815 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002816 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002817 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002818 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002819 break;
2820
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002821 case ISD::SHL:
2822 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002823 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002824 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002825
2826 // If this target supports SHL_PARTS, use it.
2827 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002828 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2829 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002830 break;
2831 }
2832
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002833 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002834 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002835 break;
2836
2837 case ISD::SRA:
2838 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002839 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002840 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002841
2842 // If this target supports SRA_PARTS, use it.
2843 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002844 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2845 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002846 break;
2847 }
2848
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002849 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002850 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002851 break;
2852 case ISD::SRL:
2853 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002854 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002855 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002856
2857 // If this target supports SRL_PARTS, use it.
2858 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002859 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2860 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002861 break;
2862 }
2863
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002864 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002865 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002866 break;
2867
Misha Brukman835702a2005-04-21 22:36:52 +00002868 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002869 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2870 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002871 break;
2872 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002873 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2874 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002875 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002876 case ISD::MUL: {
2877 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2878 SDOperand LL, LH, RL, RH;
2879 ExpandOp(Node->getOperand(0), LL, LH);
2880 ExpandOp(Node->getOperand(1), RL, RH);
2881 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2882 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2883 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2884 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2885 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2886 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2887 } else {
2888 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2889 }
2890 break;
2891 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002892 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2893 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2894 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2895 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002896 }
2897
2898 // Remember in a map if the values will be reused later.
2899 if (!Node->hasOneUse()) {
2900 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2901 std::make_pair(Lo, Hi))).second;
2902 assert(isNew && "Value already expanded?!?");
2903 }
2904}
2905
2906
2907// SelectionDAG::Legalize - This is the entry point for the file.
2908//
Chris Lattner4add7e32005-01-23 04:42:50 +00002909void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002910 /// run - This is the main entry point to this class.
2911 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002912 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002913}
2914