blob: 69cd725cafb49a00f18c3eedefb3c17a2c13ead0 [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>
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
28/// hacks on it until the target machine can handle it. This involves
29/// eliminating value sizes the machine cannot handle (promoting small sizes to
30/// large sizes or splitting up large values into small values) as well as
31/// eliminating operations the machine cannot handle.
32///
33/// This code also does a small amount of optimization and recognition of idioms
34/// as part of its processing. For example, if a target does not support a
35/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
36/// will attempt merge setcc and brc instructions into brcc's.
37///
38namespace {
39class SelectionDAGLegalize {
40 TargetLowering &TLI;
41 SelectionDAG &DAG;
42
43 /// LegalizeAction - This enum indicates what action we should take for each
44 /// value type the can occur in the program.
45 enum LegalizeAction {
46 Legal, // The target natively supports this value type.
47 Promote, // This should be promoted to the next larger type.
48 Expand, // This integer type should be broken into smaller pieces.
49 };
50
Chris Lattnerdc750592005-01-07 07:47:09 +000051 /// ValueTypeActions - This is a bitvector that contains two bits for each
52 /// value type, where the two bits correspond to the LegalizeAction enum.
53 /// This can be queried with "getTypeAction(VT)".
54 unsigned ValueTypeActions;
55
56 /// NeedsAnotherIteration - This is set when we expand a large integer
57 /// operation into smaller integer operations, but the smaller operations are
58 /// not set. This occurs only rarely in practice, for targets that don't have
59 /// 32-bit or larger integer registers.
60 bool NeedsAnotherIteration;
61
62 /// LegalizedNodes - For nodes that are of legal width, and that have more
63 /// than one use, this map indicates what regularized operand to use. This
64 /// allows us to avoid legalizing the same thing more than once.
65 std::map<SDOperand, SDOperand> LegalizedNodes;
66
Chris Lattner1f2c9d82005-01-15 05:21:40 +000067 /// PromotedNodes - For nodes that are below legal width, and that have more
68 /// than one use, this map indicates what promoted value to use. This allows
69 /// us to avoid promoting the same thing more than once.
70 std::map<SDOperand, SDOperand> PromotedNodes;
71
Chris Lattnerdc750592005-01-07 07:47:09 +000072 /// ExpandedNodes - For nodes that need to be expanded, and which have more
73 /// than one use, this map indicates which which operands are the expanded
74 /// version of the input. This allows us to avoid expanding the same node
75 /// more than once.
76 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
77
Chris Lattnerea4ca942005-01-07 22:28:47 +000078 void AddLegalizedOperand(SDOperand From, SDOperand To) {
79 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
80 assert(isNew && "Got into the map somehow?");
81 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000082 void AddPromotedOperand(SDOperand From, SDOperand To) {
83 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
84 assert(isNew && "Got into the map somehow?");
85 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000086
Chris Lattnerdc750592005-01-07 07:47:09 +000087public:
88
Chris Lattner4add7e32005-01-23 04:42:50 +000089 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000090
91 /// Run - While there is still lowering to do, perform a pass over the DAG.
92 /// Most regularization can be done in a single pass, but targets that require
93 /// large values to be split into registers multiple times (e.g. i64 -> 4x
94 /// i16) require iteration for these values (the first iteration will demote
95 /// to i32, the second will demote to i16).
96 void Run() {
97 do {
98 NeedsAnotherIteration = false;
99 LegalizeDAG();
100 } while (NeedsAnotherIteration);
101 }
102
103 /// getTypeAction - Return how we should legalize values of this type, either
104 /// it is already legal or we need to expand it into multiple registers of
105 /// smaller integer type, or we need to promote it to a larger type.
106 LegalizeAction getTypeAction(MVT::ValueType VT) const {
107 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
108 }
109
110 /// isTypeLegal - Return true if this type is legal on this target.
111 ///
112 bool isTypeLegal(MVT::ValueType VT) const {
113 return getTypeAction(VT) == Legal;
114 }
115
116private:
117 void LegalizeDAG();
118
119 SDOperand LegalizeOp(SDOperand O);
120 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000121 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000122
Chris Lattneraac464e2005-01-21 06:05:23 +0000123 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
124 SDOperand &Hi);
125 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
126 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000127
128 SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000129 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
130 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000131 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
132 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000133
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000134 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
135 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000136 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
137 SDOperand &Lo, SDOperand &Hi);
138 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000139 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000140
Chris Lattnera5bf1032005-05-12 04:49:08 +0000141 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
142
Chris Lattnerdc750592005-01-07 07:47:09 +0000143 SDOperand getIntPtrConstant(uint64_t Val) {
144 return DAG.getConstant(Val, TLI.getPointerTy());
145 }
146};
147}
148
149
Chris Lattner4add7e32005-01-23 04:42:50 +0000150SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
151 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
152 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000153 assert(MVT::LAST_VALUETYPE <= 16 &&
154 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000155}
156
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000157/// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattnere3e847b2005-07-16 00:19:57 +0000158/// UINT_TO_FP operation of the specified operand when the target requests that
159/// we expand it. At this point, we know that the result and operand types are
160/// legal for the target.
161SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0,
162 MVT::ValueType DestVT) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000163 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000164
165 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(),
Chris Lattnere3e847b2005-07-16 00:19:57 +0000166 Op0,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000167 DAG.getConstant(0,
Chris Lattnere3e847b2005-07-16 00:19:57 +0000168 Op0.getValueType()));
169 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
170 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
171 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000172
Chris Lattnerb35912e2005-07-18 04:31:14 +0000173 // If the sign bit of the integer is set, the large number will be treated as
174 // a negative number. To counteract this, the dynamic code adds an offset
175 // depending on the data type.
176 uint64_t FF;
177 switch (Op0.getValueType()) {
178 default: assert(0 && "Unsupported integer type!");
179 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
180 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
181 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
182 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
183 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000184 if (TLI.isLittleEndian()) FF <<= 32;
185 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000186
Chris Lattnere3e847b2005-07-16 00:19:57 +0000187 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
188 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
189 TLI.getPointerTy());
190 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
191 SDOperand FudgeInReg;
192 if (DestVT == MVT::f32)
193 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
194 DAG.getSrcValue(NULL));
195 else {
196 assert(DestVT == MVT::f64 && "Unexpected conversion");
197 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
198 DAG.getEntryNode(), CPIdx,
199 DAG.getSrcValue(NULL), MVT::f32));
200 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000201
Chris Lattnere3e847b2005-07-16 00:19:57 +0000202 NeedsAnotherIteration = true;
203 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
204}
205
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000206/// PromoteLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000207/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-07-16 00:19:57 +0000208/// we promote it. At this point, we know that the result and operand types are
209/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
210/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000211SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
212 MVT::ValueType DestVT,
213 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000214 // First step, figure out the appropriate *INT_TO_FP operation to use.
215 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000216
Chris Lattnere3e847b2005-07-16 00:19:57 +0000217 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000218
Chris Lattnere3e847b2005-07-16 00:19:57 +0000219 // Scan for the appropriate larger type to use.
220 while (1) {
221 NewInTy = (MVT::ValueType)(NewInTy+1);
222 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000223
Chris Lattnere3e847b2005-07-16 00:19:57 +0000224 // If the target supports SINT_TO_FP of this type, use it.
225 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
226 default: break;
227 case TargetLowering::Legal:
228 if (!TLI.hasNativeSupportFor(NewInTy))
229 break; // Can't use this datatype.
230 // FALL THROUGH.
231 case TargetLowering::Custom:
232 OpToUse = ISD::SINT_TO_FP;
233 break;
234 }
235 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000236 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000237
Chris Lattnere3e847b2005-07-16 00:19:57 +0000238 // If the target supports UINT_TO_FP of this type, use it.
239 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
240 default: break;
241 case TargetLowering::Legal:
242 if (!TLI.hasNativeSupportFor(NewInTy))
243 break; // Can't use this datatype.
244 // FALL THROUGH.
245 case TargetLowering::Custom:
246 OpToUse = ISD::UINT_TO_FP;
247 break;
248 }
249 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000250
Chris Lattnere3e847b2005-07-16 00:19:57 +0000251 // Otherwise, try a larger type.
252 }
253
254 // Make sure to legalize any nodes we create here in the next pass.
255 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000256
Chris Lattnere3e847b2005-07-16 00:19:57 +0000257 // Okay, we found the operation and type to use. Zero extend our input to the
258 // desired type then run the operation on it.
259 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000260 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
261 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000262}
263
Chris Lattner44fe26f2005-07-29 00:11:56 +0000264/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
265/// FP_TO_*INT operation of the specified operand when the target requests that
266/// we promote it. At this point, we know that the result and operand types are
267/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
268/// operation that returns a larger result.
269SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
270 MVT::ValueType DestVT,
271 bool isSigned) {
272 // First step, figure out the appropriate FP_TO*INT operation to use.
273 MVT::ValueType NewOutTy = DestVT;
Jeff Cohen546fd592005-07-30 18:33:25 +0000274
Chris Lattner44fe26f2005-07-29 00:11:56 +0000275 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000276
Chris Lattner44fe26f2005-07-29 00:11:56 +0000277 // Scan for the appropriate larger type to use.
278 while (1) {
279 NewOutTy = (MVT::ValueType)(NewOutTy+1);
280 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohen546fd592005-07-30 18:33:25 +0000281
Chris Lattner44fe26f2005-07-29 00:11:56 +0000282 // If the target supports FP_TO_SINT returning this type, use it.
283 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
284 default: break;
285 case TargetLowering::Legal:
286 if (!TLI.hasNativeSupportFor(NewOutTy))
287 break; // Can't use this datatype.
288 // FALL THROUGH.
289 case TargetLowering::Custom:
290 OpToUse = ISD::FP_TO_SINT;
291 break;
292 }
293 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000294
Chris Lattner44fe26f2005-07-29 00:11:56 +0000295 // If the target supports FP_TO_UINT of this type, use it.
296 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
297 default: break;
298 case TargetLowering::Legal:
299 if (!TLI.hasNativeSupportFor(NewOutTy))
300 break; // Can't use this datatype.
301 // FALL THROUGH.
302 case TargetLowering::Custom:
303 OpToUse = ISD::FP_TO_UINT;
304 break;
305 }
306 if (OpToUse) break;
Jeff Cohen546fd592005-07-30 18:33:25 +0000307
Chris Lattner44fe26f2005-07-29 00:11:56 +0000308 // Otherwise, try a larger type.
309 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000310
Chris Lattner44fe26f2005-07-29 00:11:56 +0000311 // Make sure to legalize any nodes we create here in the next pass.
312 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000313
Chris Lattner44fe26f2005-07-29 00:11:56 +0000314 // Okay, we found the operation and type to use. Truncate the result of the
315 // extended FP_TO_*INT operation to the desired size.
316 return DAG.getNode(ISD::TRUNCATE, DestVT,
317 DAG.getNode(OpToUse, NewOutTy, LegalOp));
318}
319
320
Chris Lattnerdc750592005-01-07 07:47:09 +0000321void SelectionDAGLegalize::LegalizeDAG() {
322 SDOperand OldRoot = DAG.getRoot();
323 SDOperand NewRoot = LegalizeOp(OldRoot);
324 DAG.setRoot(NewRoot);
325
326 ExpandedNodes.clear();
327 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000328 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000329
330 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000331 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000332}
333
334SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000335 assert(getTypeAction(Op.getValueType()) == Legal &&
336 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000337 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000338
Chris Lattnerdc750592005-01-07 07:47:09 +0000339 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000340 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000341 if (Node->getNumValues() > 1) {
342 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
343 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000344 case Legal: break; // Nothing to do.
345 case Expand: {
346 SDOperand T1, T2;
347 ExpandOp(Op.getValue(i), T1, T2);
348 assert(LegalizedNodes.count(Op) &&
349 "Expansion didn't add legal operands!");
350 return LegalizedNodes[Op];
351 }
352 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000353 PromoteOp(Op.getValue(i));
354 assert(LegalizedNodes.count(Op) &&
355 "Expansion didn't add legal operands!");
356 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000357 }
358 }
359
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000360 // Note that LegalizeOp may be reentered even from single-use nodes, which
361 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000362 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
363 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000364
Chris Lattnerec26b482005-01-09 19:03:49 +0000365 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000366
367 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000368
369 switch (Node->getOpcode()) {
370 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000371 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
372 // If this is a target node, legalize it by legalizing the operands then
373 // passing it through.
374 std::vector<SDOperand> Ops;
375 bool Changed = false;
376 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
377 Ops.push_back(LegalizeOp(Node->getOperand(i)));
378 Changed = Changed || Node->getOperand(i) != Ops.back();
379 }
380 if (Changed)
381 if (Node->getNumValues() == 1)
382 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
383 else {
384 std::vector<MVT::ValueType> VTs(Node->value_begin(),
385 Node->value_end());
386 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
387 }
388
389 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
390 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
391 return Result.getValue(Op.ResNo);
392 }
393 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000394 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
395 assert(0 && "Do not know how to legalize this operator!");
396 abort();
397 case ISD::EntryToken:
398 case ISD::FrameIndex:
399 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000400 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000401 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000402 assert(getTypeAction(Node->getValueType(0)) == Legal &&
403 "This must be legal!");
404 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000405 case ISD::CopyFromReg:
406 Tmp1 = LegalizeOp(Node->getOperand(0));
407 if (Tmp1 != Node->getOperand(0))
408 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
409 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000410 else
411 Result = Op.getValue(0);
412
413 // Since CopyFromReg produces two values, make sure to remember that we
414 // legalized both of them.
415 AddLegalizedOperand(Op.getValue(0), Result);
416 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
417 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000418 case ISD::ImplicitDef:
419 Tmp1 = LegalizeOp(Node->getOperand(0));
420 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000421 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000422 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000423 case ISD::UNDEF: {
424 MVT::ValueType VT = Op.getValueType();
425 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000426 default: assert(0 && "This action is not supported yet!");
427 case TargetLowering::Expand:
428 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000429 if (MVT::isInteger(VT))
430 Result = DAG.getConstant(0, VT);
431 else if (MVT::isFloatingPoint(VT))
432 Result = DAG.getConstantFP(0, VT);
433 else
434 assert(0 && "Unknown value type!");
435 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000436 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000437 break;
438 }
439 break;
440 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000441 case ISD::Constant:
442 // We know we don't need to expand constants here, constants only have one
443 // value and we check that it is fine above.
444
445 // FIXME: Maybe we should handle things like targets that don't support full
446 // 32-bit immediates?
447 break;
448 case ISD::ConstantFP: {
449 // Spill FP immediates to the constant pool if the target cannot directly
450 // codegen them. Targets often have some immediate values that can be
451 // efficiently generated into an FP register without a load. We explicitly
452 // leave these constants as ConstantFP nodes for the target to deal with.
453
454 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
455
456 // Check to see if this FP immediate is already legal.
457 bool isLegal = false;
458 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
459 E = TLI.legal_fpimm_end(); I != E; ++I)
460 if (CFP->isExactlyValue(*I)) {
461 isLegal = true;
462 break;
463 }
464
465 if (!isLegal) {
466 // Otherwise we need to spill the constant to memory.
467 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
468
469 bool Extend = false;
470
471 // If a FP immediate is precise when represented as a float, we put it
472 // into the constant pool as a float, even if it's is statically typed
473 // as a double.
474 MVT::ValueType VT = CFP->getValueType(0);
475 bool isDouble = VT == MVT::f64;
476 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
477 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000478 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
479 // Only do this if the target has a native EXTLOAD instruction from
480 // f32.
481 TLI.getOperationAction(ISD::EXTLOAD,
482 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000483 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
484 VT = MVT::f32;
485 Extend = true;
486 }
Misha Brukman835702a2005-04-21 22:36:52 +0000487
Chris Lattnerdc750592005-01-07 07:47:09 +0000488 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
489 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000490 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000491 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
492 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000493 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000494 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
495 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000496 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000497 }
498 break;
499 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000500 case ISD::TokenFactor: {
501 std::vector<SDOperand> Ops;
502 bool Changed = false;
503 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000504 SDOperand Op = Node->getOperand(i);
505 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000506 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000507 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
508 Changed = true;
509 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
510 Ops.push_back(LegalizeOp(Op.getOperand(j)));
511 } else {
512 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
513 Changed |= Ops[i] != Op;
514 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000515 }
516 if (Changed)
517 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
518 break;
519 }
520
Chris Lattner2dce7032005-05-12 23:24:06 +0000521 case ISD::CALLSEQ_START:
522 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000523 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000524 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000525 Tmp2 = Node->getOperand(0);
526 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000527 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000528
529 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
530 // this will cause the maps used to memoize results to get confused.
531 // Create and add a dummy use, just to increase its use count. This will
532 // be removed at the end of legalize when dead nodes are removed.
533 if (Tmp2.Val->hasOneUse())
534 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
535 DAG.getConstant(0, MVT::i32));
536 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000537 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000538 // nodes are treated specially and are mutated in place. This makes the dag
539 // legalization process more efficient and also makes libcall insertion
540 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000541 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000542 case ISD::DYNAMIC_STACKALLOC:
543 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
544 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
545 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
546 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000547 Tmp3 != Node->getOperand(2)) {
548 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
549 std::vector<SDOperand> Ops;
550 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
551 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
552 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000553 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000554
555 // Since this op produces two values, make sure to remember that we
556 // legalized both of them.
557 AddLegalizedOperand(SDOperand(Node, 0), Result);
558 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
559 return Result.getValue(Op.ResNo);
560
Chris Lattnerd0feb642005-05-13 18:43:43 +0000561 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000562 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000563 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
564 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000565
566 bool Changed = false;
567 std::vector<SDOperand> Ops;
568 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
569 Ops.push_back(LegalizeOp(Node->getOperand(i)));
570 Changed |= Ops.back() != Node->getOperand(i);
571 }
572
573 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000574 std::vector<MVT::ValueType> RetTyVTs;
575 RetTyVTs.reserve(Node->getNumValues());
576 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000577 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000578 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
579 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000580 } else {
581 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000582 }
Chris Lattner9242c502005-01-09 19:43:23 +0000583 // Since calls produce multiple values, make sure to remember that we
584 // legalized all of them.
585 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
586 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
587 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000588 }
Chris Lattner68a12142005-01-07 22:12:08 +0000589 case ISD::BR:
590 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
591 if (Tmp1 != Node->getOperand(0))
592 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
593 break;
594
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000595 case ISD::BRCOND:
596 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000597
598 switch (getTypeAction(Node->getOperand(1).getValueType())) {
599 case Expand: assert(0 && "It's impossible to expand bools");
600 case Legal:
601 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
602 break;
603 case Promote:
604 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
605 break;
606 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000607 // Basic block destination (Op#2) is always legal.
608 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
609 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
610 Node->getOperand(2));
611 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000612 case ISD::BRCONDTWOWAY:
613 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
614 switch (getTypeAction(Node->getOperand(1).getValueType())) {
615 case Expand: assert(0 && "It's impossible to expand bools");
616 case Legal:
617 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
618 break;
619 case Promote:
620 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
621 break;
622 }
623 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
624 // pair.
625 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
626 case TargetLowering::Promote:
627 default: assert(0 && "This action is not supported yet!");
628 case TargetLowering::Legal:
629 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
630 std::vector<SDOperand> Ops;
631 Ops.push_back(Tmp1);
632 Ops.push_back(Tmp2);
633 Ops.push_back(Node->getOperand(2));
634 Ops.push_back(Node->getOperand(3));
635 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
636 }
637 break;
638 case TargetLowering::Expand:
639 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
640 Node->getOperand(2));
641 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
642 break;
643 }
644 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000645
Chris Lattnerdc750592005-01-07 07:47:09 +0000646 case ISD::LOAD:
647 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
648 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000649
Chris Lattnerdc750592005-01-07 07:47:09 +0000650 if (Tmp1 != Node->getOperand(0) ||
651 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000652 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
653 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000654 else
655 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000656
Chris Lattnerea4ca942005-01-07 22:28:47 +0000657 // Since loads produce two values, make sure to remember that we legalized
658 // both of them.
659 AddLegalizedOperand(SDOperand(Node, 0), Result);
660 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
661 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000662
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000663 case ISD::EXTLOAD:
664 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000665 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000666 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
667 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000668
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000669 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000670 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000671 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000672 case TargetLowering::Promote:
673 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000674 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
675 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000676 // Since loads produce two values, make sure to remember that we legalized
677 // both of them.
678 AddLegalizedOperand(SDOperand(Node, 0), Result);
679 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
680 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000681
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000682 case TargetLowering::Legal:
683 if (Tmp1 != Node->getOperand(0) ||
684 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000685 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
686 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000687 else
688 Result = SDOperand(Node, 0);
689
690 // Since loads produce two values, make sure to remember that we legalized
691 // both of them.
692 AddLegalizedOperand(SDOperand(Node, 0), Result);
693 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
694 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000695 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000696 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
697 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
698 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000699 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000700 if (Op.ResNo)
701 return Load.getValue(1);
702 return Result;
703 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000704 assert(Node->getOpcode() != ISD::EXTLOAD &&
705 "EXTLOAD should always be supported!");
706 // Turn the unsupported load into an EXTLOAD followed by an explicit
707 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000708 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
709 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000710 SDOperand ValRes;
711 if (Node->getOpcode() == ISD::SEXTLOAD)
712 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000713 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000714 else
715 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000716 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
717 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
718 if (Op.ResNo)
719 return Result.getValue(1);
720 return ValRes;
721 }
722 assert(0 && "Unreachable");
723 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000724 case ISD::EXTRACT_ELEMENT:
725 // Get both the low and high parts.
726 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
727 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
728 Result = Tmp2; // 1 -> Hi
729 else
730 Result = Tmp1; // 0 -> Lo
731 break;
732
733 case ISD::CopyToReg:
734 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000735
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 switch (getTypeAction(Node->getOperand(1).getValueType())) {
737 case Legal:
738 // Legalize the incoming value (must be legal).
739 Tmp2 = LegalizeOp(Node->getOperand(1));
740 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000741 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000742 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000743 case Promote:
744 Tmp2 = PromoteOp(Node->getOperand(1));
745 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
746 break;
747 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000748 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000749 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000750 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000751 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
752 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
753 // Note that the copytoreg nodes are independent of each other.
754 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000755 assert(isTypeLegal(Result.getValueType()) &&
756 "Cannot expand multiple times yet (i64 -> i16)");
757 break;
758 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000759 break;
760
761 case ISD::RET:
762 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
763 switch (Node->getNumOperands()) {
764 case 2: // ret val
765 switch (getTypeAction(Node->getOperand(1).getValueType())) {
766 case Legal:
767 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000768 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000769 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
770 break;
771 case Expand: {
772 SDOperand Lo, Hi;
773 ExpandOp(Node->getOperand(1), Lo, Hi);
774 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000775 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000776 }
777 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000778 Tmp2 = PromoteOp(Node->getOperand(1));
779 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
780 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000781 }
782 break;
783 case 1: // ret void
784 if (Tmp1 != Node->getOperand(0))
785 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
786 break;
787 default: { // ret <values>
788 std::vector<SDOperand> NewValues;
789 NewValues.push_back(Tmp1);
790 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
791 switch (getTypeAction(Node->getOperand(i).getValueType())) {
792 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000793 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000794 break;
795 case Expand: {
796 SDOperand Lo, Hi;
797 ExpandOp(Node->getOperand(i), Lo, Hi);
798 NewValues.push_back(Lo);
799 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000800 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000801 }
802 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000803 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000804 }
805 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
806 break;
807 }
808 }
809 break;
810 case ISD::STORE:
811 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
812 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
813
Chris Lattnere69daaf2005-01-08 06:25:56 +0000814 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000815 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000816 if (CFP->getValueType(0) == MVT::f32) {
817 union {
818 unsigned I;
819 float F;
820 } V;
821 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000822 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000823 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000824 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000825 } else {
826 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
827 union {
828 uint64_t I;
829 double F;
830 } V;
831 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000832 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000833 DAG.getConstant(V.I, MVT::i64), Tmp2,
834 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000835 }
Chris Lattnera4743132005-02-22 07:23:39 +0000836 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000837 }
838
Chris Lattnerdc750592005-01-07 07:47:09 +0000839 switch (getTypeAction(Node->getOperand(1).getValueType())) {
840 case Legal: {
841 SDOperand Val = LegalizeOp(Node->getOperand(1));
842 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
843 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000844 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
845 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000846 break;
847 }
848 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000849 // Truncate the value and store the result.
850 Tmp3 = PromoteOp(Node->getOperand(1));
851 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000852 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +0000853 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000854 break;
855
Chris Lattnerdc750592005-01-07 07:47:09 +0000856 case Expand:
857 SDOperand Lo, Hi;
858 ExpandOp(Node->getOperand(1), Lo, Hi);
859
860 if (!TLI.isLittleEndian())
861 std::swap(Lo, Hi);
862
Chris Lattner55e9cde2005-05-11 04:51:16 +0000863 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
864 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000865 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000866 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
867 getIntPtrConstant(IncrementSize));
868 assert(isTypeLegal(Tmp2.getValueType()) &&
869 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000870 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000871 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
872 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000873 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
874 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000875 }
876 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000877 case ISD::PCMARKER:
878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000879 if (Tmp1 != Node->getOperand(0))
880 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000881 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000882 case ISD::TRUNCSTORE:
883 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
884 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
885
886 switch (getTypeAction(Node->getOperand(1).getValueType())) {
887 case Legal:
888 Tmp2 = LegalizeOp(Node->getOperand(1));
889 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
890 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000891 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +0000892 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000893 break;
894 case Promote:
895 case Expand:
896 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
897 }
898 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000899 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000900 switch (getTypeAction(Node->getOperand(0).getValueType())) {
901 case Expand: assert(0 && "It's impossible to expand bools");
902 case Legal:
903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
904 break;
905 case Promote:
906 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
907 break;
908 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000909 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000910 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000911
912 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
913 default: assert(0 && "This action is not supported yet!");
914 case TargetLowering::Legal:
915 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
916 Tmp3 != Node->getOperand(2))
917 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
918 Tmp1, Tmp2, Tmp3);
919 break;
920 case TargetLowering::Promote: {
921 MVT::ValueType NVT =
922 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
923 unsigned ExtOp, TruncOp;
924 if (MVT::isInteger(Tmp2.getValueType())) {
925 ExtOp = ISD::ZERO_EXTEND;
926 TruncOp = ISD::TRUNCATE;
927 } else {
928 ExtOp = ISD::FP_EXTEND;
929 TruncOp = ISD::FP_ROUND;
930 }
931 // Promote each of the values to the new type.
932 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
933 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
934 // Perform the larger operation, then round down.
935 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
936 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
937 break;
938 }
939 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000940 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000941 case ISD::SETCC:
942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
943 case Legal:
944 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
945 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
946 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
947 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000948 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000949 break;
950 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000951 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
952 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
953
954 // If this is an FP compare, the operands have already been extended.
955 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
956 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000957 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000958
959 // Otherwise, we have to insert explicit sign or zero extends. Note
960 // that we could insert sign extends for ALL conditions, but zero extend
961 // is cheaper on many machines (an AND instead of two shifts), so prefer
962 // it.
963 switch (cast<SetCCSDNode>(Node)->getCondition()) {
964 default: assert(0 && "Unknown integer comparison!");
965 case ISD::SETEQ:
966 case ISD::SETNE:
967 case ISD::SETUGE:
968 case ISD::SETUGT:
969 case ISD::SETULE:
970 case ISD::SETULT:
971 // ALL of these operations will work if we either sign or zero extend
972 // the operands (including the unsigned comparisons!). Zero extend is
973 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000974 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
975 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000976 break;
977 case ISD::SETGE:
978 case ISD::SETGT:
979 case ISD::SETLT:
980 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +0000981 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
982 DAG.getValueType(VT));
983 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
984 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +0000985 break;
986 }
987
988 }
989 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000990 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000991 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000992 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000993 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
994 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
995 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
996 switch (cast<SetCCSDNode>(Node)->getCondition()) {
997 case ISD::SETEQ:
998 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000999 if (RHSLo == RHSHi)
1000 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1001 if (RHSCST->isAllOnesValue()) {
1002 // Comparison to -1.
1003 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +00001004 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +00001005 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +00001006 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001007 }
1008
Chris Lattnerdc750592005-01-07 07:47:09 +00001009 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1010 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1011 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001012 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001013 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +00001014 DAG.getConstant(0, Tmp1.getValueType()));
1015 break;
1016 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001017 // If this is a comparison of the sign bit, just look at the top part.
1018 // X > -1, x < 0
1019 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +00001020 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001021 CST->getValue() == 0) || // X < 0
1022 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
1023 (CST->isAllOnesValue()))) // X > -1
1024 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1025 Node->getValueType(0), LHSHi, RHSHi);
1026
Chris Lattnerdc750592005-01-07 07:47:09 +00001027 // FIXME: This generated code sucks.
1028 ISD::CondCode LowCC;
1029 switch (cast<SetCCSDNode>(Node)->getCondition()) {
1030 default: assert(0 && "Unknown integer setcc!");
1031 case ISD::SETLT:
1032 case ISD::SETULT: LowCC = ISD::SETULT; break;
1033 case ISD::SETGT:
1034 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1035 case ISD::SETLE:
1036 case ISD::SETULE: LowCC = ISD::SETULE; break;
1037 case ISD::SETGE:
1038 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1039 }
Misha Brukman835702a2005-04-21 22:36:52 +00001040
Chris Lattnerdc750592005-01-07 07:47:09 +00001041 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1042 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1043 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1044
1045 // NOTE: on targets without efficient SELECT of bools, we can always use
1046 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001047 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +00001048 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001049 Node->getValueType(0), LHSHi, RHSHi);
1050 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
1051 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1052 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001053 break;
1054 }
1055 }
1056 break;
1057
Chris Lattner85d70c62005-01-11 05:57:22 +00001058 case ISD::MEMSET:
1059 case ISD::MEMCPY:
1060 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001061 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001062 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1063
1064 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1065 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1066 case Expand: assert(0 && "Cannot expand a byte!");
1067 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001068 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001069 break;
1070 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001071 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001072 break;
1073 }
1074 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001075 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001076 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001077
1078 SDOperand Tmp4;
1079 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001080 case Expand: {
1081 // Length is too big, just take the lo-part of the length.
1082 SDOperand HiPart;
1083 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1084 break;
1085 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001086 case Legal:
1087 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001088 break;
1089 case Promote:
1090 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001091 break;
1092 }
1093
1094 SDOperand Tmp5;
1095 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1096 case Expand: assert(0 && "Cannot expand this yet!");
1097 case Legal:
1098 Tmp5 = LegalizeOp(Node->getOperand(4));
1099 break;
1100 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001101 Tmp5 = PromoteOp(Node->getOperand(4));
1102 break;
1103 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001104
1105 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1106 default: assert(0 && "This action not implemented for this operation!");
1107 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001108 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1109 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1110 Tmp5 != Node->getOperand(4)) {
1111 std::vector<SDOperand> Ops;
1112 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1113 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1114 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1115 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001116 break;
1117 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001118 // Otherwise, the target does not support this operation. Lower the
1119 // operation to an explicit libcall as appropriate.
1120 MVT::ValueType IntPtr = TLI.getPointerTy();
1121 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1122 std::vector<std::pair<SDOperand, const Type*> > Args;
1123
Reid Spencer6dced922005-01-12 14:53:45 +00001124 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001125 if (Node->getOpcode() == ISD::MEMSET) {
1126 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1127 // Extend the ubyte argument to be an int value for the call.
1128 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1129 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1130 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1131
1132 FnName = "memset";
1133 } else if (Node->getOpcode() == ISD::MEMCPY ||
1134 Node->getOpcode() == ISD::MEMMOVE) {
1135 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1136 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1137 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1138 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1139 } else {
1140 assert(0 && "Unknown op!");
1141 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001142
Chris Lattner85d70c62005-01-11 05:57:22 +00001143 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001144 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001145 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001146 Result = CallResult.second;
1147 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001148 break;
1149 }
1150 case TargetLowering::Custom:
1151 std::vector<SDOperand> Ops;
1152 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1153 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1154 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001155 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001156 Result = LegalizeOp(Result);
1157 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001158 }
1159 break;
1160 }
Chris Lattner5385db52005-05-09 20:23:03 +00001161
1162 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001163 Tmp1 = LegalizeOp(Node->getOperand(0));
1164 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001165
Chris Lattner86535992005-05-14 07:45:46 +00001166 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1167 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1168 std::vector<SDOperand> Ops;
1169 Ops.push_back(Tmp1);
1170 Ops.push_back(Tmp2);
1171 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1172 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001173 Result = SDOperand(Node, 0);
1174 // Since these produce two values, make sure to remember that we legalized
1175 // both of them.
1176 AddLegalizedOperand(SDOperand(Node, 0), Result);
1177 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1178 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001179 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0));
1181 Tmp2 = LegalizeOp(Node->getOperand(1));
1182 Tmp3 = LegalizeOp(Node->getOperand(2));
1183 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1184 Tmp3 != Node->getOperand(2))
1185 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1186 break;
1187
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001188 case ISD::READIO:
1189 Tmp1 = LegalizeOp(Node->getOperand(0));
1190 Tmp2 = LegalizeOp(Node->getOperand(1));
1191
1192 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1193 case TargetLowering::Custom:
1194 default: assert(0 && "This action not implemented for this operation!");
1195 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001196 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1197 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1198 std::vector<SDOperand> Ops;
1199 Ops.push_back(Tmp1);
1200 Ops.push_back(Tmp2);
1201 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1202 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001203 Result = SDOperand(Node, 0);
1204 break;
1205 case TargetLowering::Expand:
1206 // Replace this with a load from memory.
1207 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1208 Node->getOperand(1), DAG.getSrcValue(NULL));
1209 Result = LegalizeOp(Result);
1210 break;
1211 }
1212
1213 // Since these produce two values, make sure to remember that we legalized
1214 // both of them.
1215 AddLegalizedOperand(SDOperand(Node, 0), Result);
1216 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1217 return Result.getValue(Op.ResNo);
1218
1219 case ISD::WRITEIO:
1220 Tmp1 = LegalizeOp(Node->getOperand(0));
1221 Tmp2 = LegalizeOp(Node->getOperand(1));
1222 Tmp3 = LegalizeOp(Node->getOperand(2));
1223
1224 switch (TLI.getOperationAction(Node->getOpcode(),
1225 Node->getOperand(1).getValueType())) {
1226 case TargetLowering::Custom:
1227 default: assert(0 && "This action not implemented for this operation!");
1228 case TargetLowering::Legal:
1229 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1230 Tmp3 != Node->getOperand(2))
1231 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1232 break;
1233 case TargetLowering::Expand:
1234 // Replace this with a store to memory.
1235 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1236 Node->getOperand(1), Node->getOperand(2),
1237 DAG.getSrcValue(NULL));
1238 Result = LegalizeOp(Result);
1239 break;
1240 }
1241 break;
1242
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001243 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001244 case ISD::SUB_PARTS:
1245 case ISD::SHL_PARTS:
1246 case ISD::SRA_PARTS:
1247 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001248 std::vector<SDOperand> Ops;
1249 bool Changed = false;
1250 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1251 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1252 Changed |= Ops.back() != Node->getOperand(i);
1253 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001254 if (Changed) {
1255 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1256 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1257 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001258
1259 // Since these produce multiple values, make sure to remember that we
1260 // legalized all of them.
1261 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1262 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1263 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001264 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001265
1266 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001267 case ISD::ADD:
1268 case ISD::SUB:
1269 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001270 case ISD::MULHS:
1271 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001272 case ISD::UDIV:
1273 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001274 case ISD::AND:
1275 case ISD::OR:
1276 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001277 case ISD::SHL:
1278 case ISD::SRL:
1279 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001280 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001281 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1282 case Expand: assert(0 && "Not possible");
1283 case Legal:
1284 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1285 break;
1286 case Promote:
1287 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1288 break;
1289 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001290 if (Tmp1 != Node->getOperand(0) ||
1291 Tmp2 != Node->getOperand(1))
1292 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1293 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001294
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001295 case ISD::UREM:
1296 case ISD::SREM:
1297 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1298 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1299 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1300 case TargetLowering::Legal:
1301 if (Tmp1 != Node->getOperand(0) ||
1302 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001303 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001304 Tmp2);
1305 break;
1306 case TargetLowering::Promote:
1307 case TargetLowering::Custom:
1308 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001309 case TargetLowering::Expand:
1310 if (MVT::isInteger(Node->getValueType(0))) {
1311 MVT::ValueType VT = Node->getValueType(0);
1312 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1313 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1314 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1315 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1316 } else {
1317 // Floating point mod -> fmod libcall.
1318 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1319 SDOperand Dummy;
1320 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001321 }
1322 break;
1323 }
1324 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001325
Andrew Lenharth5e177822005-05-03 17:19:30 +00001326 case ISD::CTPOP:
1327 case ISD::CTTZ:
1328 case ISD::CTLZ:
1329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1330 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1331 case TargetLowering::Legal:
1332 if (Tmp1 != Node->getOperand(0))
1333 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1334 break;
1335 case TargetLowering::Promote: {
1336 MVT::ValueType OVT = Tmp1.getValueType();
1337 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001338
1339 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001340 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1341 // Perform the larger operation, then subtract if needed.
1342 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1343 switch(Node->getOpcode())
1344 {
1345 case ISD::CTPOP:
1346 Result = Tmp1;
1347 break;
1348 case ISD::CTTZ:
1349 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001350 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001351 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001352 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001353 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1354 break;
1355 case ISD::CTLZ:
1356 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001357 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1358 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001359 getSizeInBits(OVT), NVT));
1360 break;
1361 }
1362 break;
1363 }
1364 case TargetLowering::Custom:
1365 assert(0 && "Cannot custom handle this yet!");
1366 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001367 switch(Node->getOpcode())
1368 {
1369 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001370 static const uint64_t mask[6] = {
1371 0x5555555555555555ULL, 0x3333333333333333ULL,
1372 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1373 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1374 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001375 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001376 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1377 unsigned len = getSizeInBits(VT);
1378 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001379 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001380 Tmp2 = DAG.getConstant(mask[i], VT);
1381 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001382 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001383 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1384 DAG.getNode(ISD::AND, VT,
1385 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1386 Tmp2));
1387 }
1388 Result = Tmp1;
1389 break;
1390 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001391 case ISD::CTLZ: {
1392 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001393 x = x | (x >> 1);
1394 x = x | (x >> 2);
1395 ...
1396 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001397 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001398 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001399
Chris Lattner56add052005-05-11 18:35:21 +00001400 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1401 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001402 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1403 unsigned len = getSizeInBits(VT);
1404 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1405 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001406 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001407 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1408 }
1409 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001410 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001411 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001412 }
1413 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001414 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001415 // unless the target has ctlz but not ctpop, in which case we use:
1416 // { return 32 - nlz(~x & (x-1)); }
1417 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001418 MVT::ValueType VT = Tmp1.getValueType();
1419 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001420 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001421 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1422 DAG.getNode(ISD::SUB, VT, Tmp1,
1423 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001424 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1425 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1426 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001427 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001428 DAG.getConstant(getSizeInBits(VT), VT),
1429 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1430 } else {
1431 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1432 }
Chris Lattner72473242005-05-11 05:27:09 +00001433 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001434 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001435 default:
1436 assert(0 && "Cannot expand this yet!");
1437 break;
1438 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001439 break;
1440 }
1441 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001442
Chris Lattner13fe99c2005-04-02 05:00:07 +00001443 // Unary operators
1444 case ISD::FABS:
1445 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001446 case ISD::FSQRT:
1447 case ISD::FSIN:
1448 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001449 Tmp1 = LegalizeOp(Node->getOperand(0));
1450 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1451 case TargetLowering::Legal:
1452 if (Tmp1 != Node->getOperand(0))
1453 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1454 break;
1455 case TargetLowering::Promote:
1456 case TargetLowering::Custom:
1457 assert(0 && "Cannot promote/custom handle this yet!");
1458 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001459 switch(Node->getOpcode()) {
1460 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001461 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1462 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1463 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1464 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001465 break;
1466 }
1467 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001468 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1469 MVT::ValueType VT = Node->getValueType(0);
1470 Tmp2 = DAG.getConstantFP(0.0, VT);
1471 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1472 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1473 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1474 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001475 break;
1476 }
1477 case ISD::FSQRT:
1478 case ISD::FSIN:
1479 case ISD::FCOS: {
1480 MVT::ValueType VT = Node->getValueType(0);
1481 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1482 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 }
1489 std::vector<std::pair<SDOperand, const Type*> > Args;
1490 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001491 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001492 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001493 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattner80026402005-04-30 04:43:14 +00001494 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1495 Result = LegalizeOp(CallResult.first);
1496 break;
1497 }
1498 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001499 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001500 }
1501 break;
1502 }
1503 break;
1504
1505 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001506 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001507 case ISD::UINT_TO_FP: {
1508 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1510 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001511 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001512 Node->getOperand(0).getValueType())) {
1513 default: assert(0 && "Unknown operation action!");
1514 case TargetLowering::Expand:
1515 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP yet");
1516 Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1517 Node->getValueType(0));
1518 AddLegalizedOperand(Op, Result);
1519 return Result;
1520 case TargetLowering::Promote:
1521 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1522 Node->getValueType(0),
1523 isSigned);
1524 AddLegalizedOperand(Op, Result);
1525 return Result;
1526 case TargetLowering::Legal:
1527 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001528 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001529
Chris Lattnerdc750592005-01-07 07:47:09 +00001530 Tmp1 = LegalizeOp(Node->getOperand(0));
1531 if (Tmp1 != Node->getOperand(0))
1532 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1533 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001534 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001535 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1536 Node->getValueType(0), Node->getOperand(0));
1537 break;
1538 case Promote:
1539 if (isSigned) {
1540 Result = PromoteOp(Node->getOperand(0));
1541 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1542 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1543 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1544 } else {
1545 Result = PromoteOp(Node->getOperand(0));
1546 Result = DAG.getZeroExtendInReg(Result,
1547 Node->getOperand(0).getValueType());
1548 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001549 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001550 break;
1551 }
1552 break;
1553 }
1554 case ISD::TRUNCATE:
1555 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1556 case Legal:
1557 Tmp1 = LegalizeOp(Node->getOperand(0));
1558 if (Tmp1 != Node->getOperand(0))
1559 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1560 break;
1561 case Expand:
1562 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1563
1564 // Since the result is legal, we should just be able to truncate the low
1565 // part of the source.
1566 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1567 break;
1568 case Promote:
1569 Result = PromoteOp(Node->getOperand(0));
1570 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1571 break;
1572 }
1573 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001574
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001575 case ISD::FP_TO_SINT:
1576 case ISD::FP_TO_UINT:
1577 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1578 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001579 Tmp1 = LegalizeOp(Node->getOperand(0));
1580
Chris Lattner44fe26f2005-07-29 00:11:56 +00001581 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1582 default: assert(0 && "Unknown operation action!");
1583 case TargetLowering::Expand:
1584 assert(0 && "Cannot expand FP_TO*INT yet");
1585 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001586 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00001587 Node->getOpcode() == ISD::FP_TO_SINT);
1588 AddLegalizedOperand(Op, Result);
1589 return Result;
1590 case TargetLowering::Legal:
1591 break;
Chris Lattnerf59b2da2005-07-30 00:04:12 +00001592 case TargetLowering::Custom:
1593 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1594 Result = TLI.LowerOperation(Result, DAG);
1595 AddLegalizedOperand(Op, Result);
1596 NeedsAnotherIteration = true;
1597 return Result;
Chris Lattner44fe26f2005-07-29 00:11:56 +00001598 }
Jeff Cohen546fd592005-07-30 18:33:25 +00001599
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001600 if (Tmp1 != Node->getOperand(0))
1601 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1602 break;
1603 case Expand:
1604 assert(0 && "Shouldn't need to expand other operators here!");
1605 case Promote:
1606 Result = PromoteOp(Node->getOperand(0));
1607 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1608 break;
1609 }
1610 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00001611
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001612 case ISD::ZERO_EXTEND:
1613 case ISD::SIGN_EXTEND:
1614 case ISD::FP_EXTEND:
1615 case ISD::FP_ROUND:
1616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1617 case Legal:
1618 Tmp1 = LegalizeOp(Node->getOperand(0));
1619 if (Tmp1 != Node->getOperand(0))
1620 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1621 break;
1622 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001623 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001624
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001625 case Promote:
1626 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001627 case ISD::ZERO_EXTEND:
1628 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001629 // NOTE: Any extend would work here...
1630 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001631 Result = DAG.getZeroExtendInReg(Result,
1632 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001633 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001634 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001635 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001636 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001637 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001638 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001639 Result,
1640 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001641 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001642 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001643 Result = PromoteOp(Node->getOperand(0));
1644 if (Result.getValueType() != Op.getValueType())
1645 // Dynamically dead while we have only 2 FP types.
1646 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1647 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001648 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001649 Result = PromoteOp(Node->getOperand(0));
1650 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1651 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001652 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001653 }
1654 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001655 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001656 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001657 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001658 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001659
1660 // If this operation is not supported, convert it to a shl/shr or load/store
1661 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001662 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1663 default: assert(0 && "This action not supported for this op yet!");
1664 case TargetLowering::Legal:
1665 if (Tmp1 != Node->getOperand(0))
1666 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001667 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001668 break;
1669 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001670 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001671 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001672 // NOTE: we could fall back on load/store here too for targets without
1673 // SAR. However, it is doubtful that any exist.
1674 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1675 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001676 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001677 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1678 Node->getOperand(0), ShiftCst);
1679 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1680 Result, ShiftCst);
1681 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1682 // The only way we can lower this is to turn it into a STORETRUNC,
1683 // EXTLOAD pair, targetting a temporary location (a stack slot).
1684
1685 // NOTE: there is a choice here between constantly creating new stack
1686 // slots and always reusing the same one. We currently always create
1687 // new ones, as reuse may inhibit scheduling.
1688 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1689 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1690 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1691 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001692 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001693 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1694 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1695 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001696 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001697 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001698 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1699 Result, StackSlot, DAG.getSrcValue(NULL),
1700 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001701 } else {
1702 assert(0 && "Unknown op");
1703 }
1704 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001705 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001706 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001707 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001708 }
Chris Lattner99222f72005-01-15 07:15:18 +00001709 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001710
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001711 // Note that LegalizeOp may be reentered even from single-use nodes, which
1712 // means that we always must cache transformed nodes.
1713 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001714 return Result;
1715}
1716
Chris Lattner4d978642005-01-15 22:16:26 +00001717/// PromoteOp - Given an operation that produces a value in an invalid type,
1718/// promote it to compute the value into a larger type. The produced value will
1719/// have the correct bits for the low portion of the register, but no guarantee
1720/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001721SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1722 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001723 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001724 assert(getTypeAction(VT) == Promote &&
1725 "Caller should expand or legalize operands that are not promotable!");
1726 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1727 "Cannot promote to smaller type!");
1728
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001729 SDOperand Tmp1, Tmp2, Tmp3;
1730
1731 SDOperand Result;
1732 SDNode *Node = Op.Val;
1733
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001734 if (!Node->hasOneUse()) {
1735 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1736 if (I != PromotedNodes.end()) return I->second;
1737 } else {
1738 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1739 }
1740
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001741 // Promotion needs an optimization step to clean up after it, and is not
1742 // careful to avoid operations the target does not support. Make sure that
1743 // all generated operations are legalized in the next iteration.
1744 NeedsAnotherIteration = true;
1745
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001746 switch (Node->getOpcode()) {
1747 default:
1748 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1749 assert(0 && "Do not know how to promote this operator!");
1750 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001751 case ISD::UNDEF:
1752 Result = DAG.getNode(ISD::UNDEF, NVT);
1753 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001754 case ISD::Constant:
1755 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1756 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1757 break;
1758 case ISD::ConstantFP:
1759 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1760 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1761 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001762 case ISD::CopyFromReg:
1763 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1764 Node->getOperand(0));
1765 // Remember that we legalized the chain.
1766 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1767 break;
1768
Chris Lattner2cb338d2005-01-18 02:59:52 +00001769 case ISD::SETCC:
1770 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1771 "SetCC type is not legal??");
1772 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1773 TLI.getSetCCResultTy(), Node->getOperand(0),
1774 Node->getOperand(1));
1775 Result = LegalizeOp(Result);
1776 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001777
1778 case ISD::TRUNCATE:
1779 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1780 case Legal:
1781 Result = LegalizeOp(Node->getOperand(0));
1782 assert(Result.getValueType() >= NVT &&
1783 "This truncation doesn't make sense!");
1784 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1785 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1786 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001787 case Promote:
1788 // The truncation is not required, because we don't guarantee anything
1789 // about high bits anyway.
1790 Result = PromoteOp(Node->getOperand(0));
1791 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001792 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001793 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1794 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00001795 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001796 }
1797 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001798 case ISD::SIGN_EXTEND:
1799 case ISD::ZERO_EXTEND:
1800 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1801 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1802 case Legal:
1803 // Input is legal? Just do extend all the way to the larger type.
1804 Result = LegalizeOp(Node->getOperand(0));
1805 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1806 break;
1807 case Promote:
1808 // Promote the reg if it's smaller.
1809 Result = PromoteOp(Node->getOperand(0));
1810 // The high bits are not guaranteed to be anything. Insert an extend.
1811 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001812 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00001813 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001814 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001815 Result = DAG.getZeroExtendInReg(Result,
1816 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001817 break;
1818 }
1819 break;
1820
1821 case ISD::FP_EXTEND:
1822 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1823 case ISD::FP_ROUND:
1824 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1825 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1826 case Promote: assert(0 && "Unreachable with 2 FP types!");
1827 case Legal:
1828 // Input is legal? Do an FP_ROUND_INREG.
1829 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001830 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1831 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001832 break;
1833 }
1834 break;
1835
1836 case ISD::SINT_TO_FP:
1837 case ISD::UINT_TO_FP:
1838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1839 case Legal:
1840 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001841 // No extra round required here.
1842 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001843 break;
1844
1845 case Promote:
1846 Result = PromoteOp(Node->getOperand(0));
1847 if (Node->getOpcode() == ISD::SINT_TO_FP)
1848 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001849 Result,
1850 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001851 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001852 Result = DAG.getZeroExtendInReg(Result,
1853 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001854 // No extra round required here.
1855 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001856 break;
1857 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001858 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1859 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001860 // Round if we cannot tolerate excess precision.
1861 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001862 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1863 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00001864 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001865 }
Chris Lattner4d978642005-01-15 22:16:26 +00001866 break;
1867
1868 case ISD::FP_TO_SINT:
1869 case ISD::FP_TO_UINT:
1870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1871 case Legal:
1872 Tmp1 = LegalizeOp(Node->getOperand(0));
1873 break;
1874 case Promote:
1875 // The input result is prerounded, so we don't have to do anything
1876 // special.
1877 Tmp1 = PromoteOp(Node->getOperand(0));
1878 break;
1879 case Expand:
1880 assert(0 && "not implemented");
1881 }
1882 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1883 break;
1884
Chris Lattner13fe99c2005-04-02 05:00:07 +00001885 case ISD::FABS:
1886 case ISD::FNEG:
1887 Tmp1 = PromoteOp(Node->getOperand(0));
1888 assert(Tmp1.getValueType() == NVT);
1889 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1890 // NOTE: we do not have to do any extra rounding here for
1891 // NoExcessFPPrecision, because we know the input will have the appropriate
1892 // precision, and these operations don't modify precision at all.
1893 break;
1894
Chris Lattner9d6fa982005-04-28 21:44:33 +00001895 case ISD::FSQRT:
1896 case ISD::FSIN:
1897 case ISD::FCOS:
1898 Tmp1 = PromoteOp(Node->getOperand(0));
1899 assert(Tmp1.getValueType() == NVT);
1900 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1901 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001902 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1903 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00001904 break;
1905
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001906 case ISD::AND:
1907 case ISD::OR:
1908 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001909 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001910 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001911 case ISD::MUL:
1912 // The input may have strange things in the top bits of the registers, but
1913 // these operations don't care. They may have wierd bits going out, but
1914 // that too is okay if they are integer operations.
1915 Tmp1 = PromoteOp(Node->getOperand(0));
1916 Tmp2 = PromoteOp(Node->getOperand(1));
1917 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1918 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1919
1920 // However, if this is a floating point operation, they will give excess
1921 // precision that we may not be able to tolerate. If we DO allow excess
1922 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001923 // FIXME: Why would we need to round FP ops more than integer ones?
1924 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001925 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001926 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1927 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001928 break;
1929
Chris Lattner4d978642005-01-15 22:16:26 +00001930 case ISD::SDIV:
1931 case ISD::SREM:
1932 // These operators require that their input be sign extended.
1933 Tmp1 = PromoteOp(Node->getOperand(0));
1934 Tmp2 = PromoteOp(Node->getOperand(1));
1935 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00001936 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1937 DAG.getValueType(VT));
1938 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1939 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001940 }
1941 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1942
1943 // Perform FP_ROUND: this is probably overly pessimistic.
1944 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001945 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1946 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001947 break;
1948
1949 case ISD::UDIV:
1950 case ISD::UREM:
1951 // These operators require that their input be zero extended.
1952 Tmp1 = PromoteOp(Node->getOperand(0));
1953 Tmp2 = PromoteOp(Node->getOperand(1));
1954 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001955 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1956 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001957 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1958 break;
1959
1960 case ISD::SHL:
1961 Tmp1 = PromoteOp(Node->getOperand(0));
1962 Tmp2 = LegalizeOp(Node->getOperand(1));
1963 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1964 break;
1965 case ISD::SRA:
1966 // The input value must be properly sign extended.
1967 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001968 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1969 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001970 Tmp2 = LegalizeOp(Node->getOperand(1));
1971 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1972 break;
1973 case ISD::SRL:
1974 // The input value must be properly zero extended.
1975 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001976 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001977 Tmp2 = LegalizeOp(Node->getOperand(1));
1978 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1979 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001980 case ISD::LOAD:
1981 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1982 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001983 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001984 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001985 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
1986 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001987 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001988 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
1989 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001990
1991 // Remember that we legalized the chain.
1992 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1993 break;
1994 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001995 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1996 case Expand: assert(0 && "It's impossible to expand bools");
1997 case Legal:
1998 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1999 break;
2000 case Promote:
2001 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2002 break;
2003 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002004 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2005 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2006 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2007 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002008 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002009 case ISD::CALL: {
2010 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2011 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2012
Chris Lattner3d95c142005-01-19 20:24:35 +00002013 std::vector<SDOperand> Ops;
2014 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2015 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2016
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002017 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2018 "Can only promote single result calls");
2019 std::vector<MVT::ValueType> RetTyVTs;
2020 RetTyVTs.reserve(2);
2021 RetTyVTs.push_back(NVT);
2022 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002023 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2024 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002025 Result = SDOperand(NC, 0);
2026
2027 // Insert the new chain mapping.
2028 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2029 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002030 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002031 case ISD::CTPOP:
2032 case ISD::CTTZ:
2033 case ISD::CTLZ:
2034 Tmp1 = Node->getOperand(0);
2035 //Zero extend the argument
2036 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2037 // Perform the larger operation, then subtract if needed.
2038 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2039 switch(Node->getOpcode())
2040 {
2041 case ISD::CTPOP:
2042 Result = Tmp1;
2043 break;
2044 case ISD::CTTZ:
2045 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002046 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002047 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002048 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002049 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2050 break;
2051 case ISD::CTLZ:
2052 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002053 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2054 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002055 getSizeInBits(VT), NVT));
2056 break;
2057 }
2058 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002059 }
2060
2061 assert(Result.Val && "Didn't set a result!");
2062 AddPromotedOperand(Op, Result);
2063 return Result;
2064}
Chris Lattnerdc750592005-01-07 07:47:09 +00002065
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002066/// ExpandAddSub - Find a clever way to expand this add operation into
2067/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002068void SelectionDAGLegalize::
2069ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2070 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002071 // Expand the subcomponents.
2072 SDOperand LHSL, LHSH, RHSL, RHSH;
2073 ExpandOp(LHS, LHSL, LHSH);
2074 ExpandOp(RHS, RHSL, RHSH);
2075
Chris Lattner8ffd0042005-04-11 20:29:59 +00002076 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00002077 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2078 if (LHSL.getValueType() == MVT::i32) {
2079 SDOperand LowEl;
2080 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2081 if (C->getValue() == 0)
2082 LowEl = RHSL;
2083 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2084 if (C->getValue() == 0)
2085 LowEl = LHSL;
2086 if (LowEl.Val) {
2087 // Turn this into an add/sub of the high part only.
2088 SDOperand HiEl =
2089 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2090 LowEl.getValueType(), LHSH, RHSH);
2091 Lo = LowEl;
2092 Hi = HiEl;
2093 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002094 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002095 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002096
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002097 std::vector<SDOperand> Ops;
2098 Ops.push_back(LHSL);
2099 Ops.push_back(LHSH);
2100 Ops.push_back(RHSL);
2101 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002102
2103 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2104 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002105 Hi = Lo.getValue(1);
2106}
2107
Chris Lattner4157c412005-04-02 04:00:59 +00002108void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2109 SDOperand Op, SDOperand Amt,
2110 SDOperand &Lo, SDOperand &Hi) {
2111 // Expand the subcomponents.
2112 SDOperand LHSL, LHSH;
2113 ExpandOp(Op, LHSL, LHSH);
2114
2115 std::vector<SDOperand> Ops;
2116 Ops.push_back(LHSL);
2117 Ops.push_back(LHSH);
2118 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002119 std::vector<MVT::ValueType> VTs;
2120 VTs.push_back(LHSL.getValueType());
2121 VTs.push_back(LHSH.getValueType());
2122 VTs.push_back(Amt.getValueType());
2123 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002124 Hi = Lo.getValue(1);
2125}
2126
2127
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002128/// ExpandShift - Try to find a clever way to expand this shift operation out to
2129/// smaller elements. If we can't find a way that is more efficient than a
2130/// libcall on this target, return false. Otherwise, return true with the
2131/// low-parts expanded into Lo and Hi.
2132bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2133 SDOperand &Lo, SDOperand &Hi) {
2134 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2135 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002136
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002137 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002138 SDOperand ShAmt = LegalizeOp(Amt);
2139 MVT::ValueType ShTy = ShAmt.getValueType();
2140 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2141 unsigned NVTBits = MVT::getSizeInBits(NVT);
2142
2143 // Handle the case when Amt is an immediate. Other cases are currently broken
2144 // and are disabled.
2145 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2146 unsigned Cst = CN->getValue();
2147 // Expand the incoming operand to be shifted, so that we have its parts
2148 SDOperand InL, InH;
2149 ExpandOp(Op, InL, InH);
2150 switch(Opc) {
2151 case ISD::SHL:
2152 if (Cst > VTBits) {
2153 Lo = DAG.getConstant(0, NVT);
2154 Hi = DAG.getConstant(0, NVT);
2155 } else if (Cst > NVTBits) {
2156 Lo = DAG.getConstant(0, NVT);
2157 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002158 } else if (Cst == NVTBits) {
2159 Lo = DAG.getConstant(0, NVT);
2160 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002161 } else {
2162 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2163 Hi = DAG.getNode(ISD::OR, NVT,
2164 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2165 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2166 }
2167 return true;
2168 case ISD::SRL:
2169 if (Cst > VTBits) {
2170 Lo = DAG.getConstant(0, NVT);
2171 Hi = DAG.getConstant(0, NVT);
2172 } else if (Cst > NVTBits) {
2173 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2174 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002175 } else if (Cst == NVTBits) {
2176 Lo = InH;
2177 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002178 } else {
2179 Lo = DAG.getNode(ISD::OR, NVT,
2180 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2181 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2182 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2183 }
2184 return true;
2185 case ISD::SRA:
2186 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002187 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002188 DAG.getConstant(NVTBits-1, ShTy));
2189 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002190 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002191 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002192 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002193 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002194 } else if (Cst == NVTBits) {
2195 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002196 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002197 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002198 } else {
2199 Lo = DAG.getNode(ISD::OR, NVT,
2200 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2201 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2202 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2203 }
2204 return true;
2205 }
2206 }
2207 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2208 // so disable it for now. Currently targets are handling this via SHL_PARTS
2209 // and friends.
2210 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002211
2212 // If we have an efficient select operation (or if the selects will all fold
2213 // away), lower to some complex code, otherwise just emit the libcall.
2214 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2215 !isa<ConstantSDNode>(Amt))
2216 return false;
2217
2218 SDOperand InL, InH;
2219 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002220 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2221 DAG.getConstant(NVTBits, ShTy), ShAmt);
2222
Chris Lattner4d25c042005-01-20 20:29:23 +00002223 // Compare the unmasked shift amount against 32.
2224 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
2225 DAG.getConstant(NVTBits, ShTy));
2226
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002227 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2228 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2229 DAG.getConstant(NVTBits-1, ShTy));
2230 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2231 DAG.getConstant(NVTBits-1, ShTy));
2232 }
2233
2234 if (Opc == ISD::SHL) {
2235 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2236 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2237 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002238 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002239
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002240 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2241 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2242 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002243 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
2244 DAG.getSetCC(ISD::SETEQ,
2245 TLI.getSetCCResultTy(), NAmt,
2246 DAG.getConstant(32, ShTy)),
2247 DAG.getConstant(0, NVT),
2248 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002249 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002250 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002251 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002252 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002253
2254 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002255 if (Opc == ISD::SRA)
2256 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2257 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002258 else
2259 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002260 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002261 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002262 }
2263 return true;
2264}
Chris Lattneraac464e2005-01-21 06:05:23 +00002265
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002266/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2267/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002268/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002269static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002270 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
2271
Chris Lattner2dce7032005-05-12 23:24:06 +00002272 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002273 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002274 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002275 Found = Node;
2276 return;
2277 }
2278
2279 // Otherwise, scan the operands of Node to see if any of them is a call.
2280 assert(Node->getNumOperands() != 0 &&
2281 "All leaves should have depth equal to the entry node!");
2282 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002283 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002284
2285 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002286 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002287 Found);
2288}
2289
2290
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002291/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2292/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002293/// than Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002294static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002295 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2296
Chris Lattner2dce7032005-05-12 23:24:06 +00002297 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002298 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002299 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002300 Found = Node;
2301 return;
2302 }
2303
2304 // Otherwise, scan the operands of Node to see if any of them is a call.
2305 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2306 if (UI == E) return;
2307 for (--E; UI != E; ++UI)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002308 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002309
2310 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002311 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002312}
2313
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002314/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002315/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002316static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002317 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002318 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002319 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002320 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002321
2322 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002323 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002324
Chris Lattner4add7e32005-01-23 04:42:50 +00002325 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002326 if (TheChain.getValueType() != MVT::Other)
2327 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002328 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002329
2330 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00002331 E = Node->use_end(); ; ++UI) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002332 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukman835702a2005-04-21 22:36:52 +00002333
Chris Lattner4add7e32005-01-23 04:42:50 +00002334 // Make sure to only follow users of our token chain.
2335 SDNode *User = *UI;
2336 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2337 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002338 if (SDNode *Result = FindCallSeqEnd(User))
2339 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002340 }
2341 assert(0 && "Unreachable");
2342 abort();
2343}
2344
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002345/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002346/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002347static SDNode *FindCallSeqStart(SDNode *Node) {
2348 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002349 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002350
2351 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2352 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002353 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002354}
2355
2356
Chris Lattner4add7e32005-01-23 04:42:50 +00002357/// FindInputOutputChains - If we are replacing an operation with a call we need
2358/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002359/// properly serialize the calls in the block. The returned operand is the
2360/// input chain value for the new call (e.g. the entry node or the previous
2361/// call), and OutChain is set to be the chain node to update to point to the
2362/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002363static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2364 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002365 SDNode *LatestCallSeqStart = Entry.Val;
2366 SDNode *LatestCallSeqEnd = 0;
2367 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2368 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002369
Chris Lattner2dce7032005-05-12 23:24:06 +00002370 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002371 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002372 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2373 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002374 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2375 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002376 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002377 LatestCallSeqEnd = Entry.Val;
2378 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002379
Chris Lattner06bbeb62005-05-11 19:02:11 +00002380 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002381 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002382 OutChain = 0;
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002383 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002384
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002385 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002386 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002387 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002388
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002389 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002390}
2391
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002392/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002393void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2394 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002395 // Nothing to splice it into?
2396 if (OutChain == 0) return;
2397
2398 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2399 //OutChain->dump();
2400
2401 // Form a token factor node merging the old inval and the new inval.
2402 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2403 OutChain->getOperand(0));
2404 // Change the node to refer to the new token.
2405 OutChain->setAdjCallChain(InToken);
2406}
Chris Lattner4add7e32005-01-23 04:42:50 +00002407
2408
Chris Lattneraac464e2005-01-21 06:05:23 +00002409// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2410// does not fit into a register, return the lo part and set the hi part to the
2411// by-reg argument. If it does fit into a single register, return the result
2412// and leave the Hi part unset.
2413SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2414 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002415 SDNode *OutChain;
2416 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2417 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002418 if (InChain.Val == 0)
2419 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002420
Chris Lattneraac464e2005-01-21 06:05:23 +00002421 TargetLowering::ArgListTy Args;
2422 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2423 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2424 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2425 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2426 }
2427 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002428
Chris Lattner06bbeb62005-05-11 19:02:11 +00002429 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002430 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002431 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002432 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2433 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002434 SpliceCallInto(CallInfo.second, OutChain);
2435
2436 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002437
2438 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002439 default: assert(0 && "Unknown thing");
2440 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002441 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002442 case Promote:
2443 assert(0 && "Cannot promote this yet!");
2444 case Expand:
2445 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002446 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002447 return Lo;
2448 }
2449}
2450
Chris Lattner4add7e32005-01-23 04:42:50 +00002451
Chris Lattneraac464e2005-01-21 06:05:23 +00002452/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2453/// destination type is legal.
2454SDOperand SelectionDAGLegalize::
2455ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2456 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2457 assert(getTypeAction(Source.getValueType()) == Expand &&
2458 "This is not an expansion!");
2459 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2460
Chris Lattner06bbeb62005-05-11 19:02:11 +00002461 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002462 assert(Source.getValueType() == MVT::i64 &&
2463 "This only works for 64-bit -> FP");
2464 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2465 // incoming integer is set. To handle this, we dynamically test to see if
2466 // it is set, and, if so, add a fudge factor.
2467 SDOperand Lo, Hi;
2468 ExpandOp(Source, Lo, Hi);
2469
Chris Lattner2a4f7312005-05-13 04:45:13 +00002470 // If this is unsigned, and not supported, first perform the conversion to
2471 // signed, then adjust the result if the sign bit is set.
2472 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2473 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2474
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002475 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2476 DAG.getConstant(0, Hi.getValueType()));
2477 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2478 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2479 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002480 uint64_t FF = 0x5f800000ULL;
2481 if (TLI.isLittleEndian()) FF <<= 32;
2482 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002483
2484 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2485 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2486 TLI.getPointerTy());
2487 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2488 SDOperand FudgeInReg;
2489 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002490 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2491 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002492 else {
2493 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002494 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2495 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002496 }
2497 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002498 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002499
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002500 // Check to see if the target has a custom way to lower this. If so, use it.
2501 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2502 default: assert(0 && "This action not implemented for this operation!");
2503 case TargetLowering::Legal:
2504 case TargetLowering::Expand:
2505 break; // This case is handled below.
2506 case TargetLowering::Custom:
2507 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002508 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002509 }
2510
Chris Lattner153587e2005-05-12 07:00:44 +00002511 // Expand the source, then glue it back together for the call. We must expand
2512 // the source in case it is shared (this pass of legalize must traverse it).
2513 SDOperand SrcLo, SrcHi;
2514 ExpandOp(Source, SrcLo, SrcHi);
2515 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2516
Chris Lattner06bbeb62005-05-11 19:02:11 +00002517 SDNode *OutChain = 0;
2518 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2519 DAG.getEntryNode());
2520 const char *FnName = 0;
2521 if (DestTy == MVT::f32)
2522 FnName = "__floatdisf";
2523 else {
2524 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2525 FnName = "__floatdidf";
2526 }
2527
Chris Lattneraac464e2005-01-21 06:05:23 +00002528 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2529
2530 TargetLowering::ArgListTy Args;
2531 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002532
Chris Lattneraac464e2005-01-21 06:05:23 +00002533 Args.push_back(std::make_pair(Source, ArgTy));
2534
2535 // We don't care about token chains for libcalls. We just use the entry
2536 // node as our input and ignore the output chain. This allows us to place
2537 // calls wherever we need them to satisfy data dependences.
2538 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002539
2540 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002541 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2542 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002543
Chris Lattnera5bf1032005-05-12 04:49:08 +00002544 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002545 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002546}
Misha Brukman835702a2005-04-21 22:36:52 +00002547
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002548
2549
Chris Lattnerdc750592005-01-07 07:47:09 +00002550/// ExpandOp - Expand the specified SDOperand into its two component pieces
2551/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2552/// LegalizeNodes map is filled in for any results that are not expanded, the
2553/// ExpandedNodes map is filled in for any results that are expanded, and the
2554/// Lo/Hi values are returned.
2555void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2556 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002557 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002558 SDNode *Node = Op.Val;
2559 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2560 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2561 assert(MVT::isInteger(NVT) && NVT < VT &&
2562 "Cannot expand to FP value or to larger int value!");
2563
2564 // If there is more than one use of this, see if we already expanded it.
2565 // There is no use remembering values that only have a single use, as the map
2566 // entries will never be reused.
2567 if (!Node->hasOneUse()) {
2568 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2569 = ExpandedNodes.find(Op);
2570 if (I != ExpandedNodes.end()) {
2571 Lo = I->second.first;
2572 Hi = I->second.second;
2573 return;
2574 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002575 } else {
2576 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002577 }
2578
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002579 // Expanding to multiple registers needs to perform an optimization step, and
2580 // is not careful to avoid operations the target does not support. Make sure
2581 // that all generated operations are legalized in the next iteration.
2582 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002583
Chris Lattnerdc750592005-01-07 07:47:09 +00002584 switch (Node->getOpcode()) {
2585 default:
2586 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2587 assert(0 && "Do not know how to expand this operator!");
2588 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002589 case ISD::UNDEF:
2590 Lo = DAG.getNode(ISD::UNDEF, NVT);
2591 Hi = DAG.getNode(ISD::UNDEF, NVT);
2592 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002593 case ISD::Constant: {
2594 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2595 Lo = DAG.getConstant(Cst, NVT);
2596 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2597 break;
2598 }
2599
2600 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002601 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002602 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002603 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2604 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002605
Chris Lattner3b8e7192005-01-14 22:38:01 +00002606 // Remember that we legalized the chain.
2607 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2608
Chris Lattnerdc750592005-01-07 07:47:09 +00002609 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2610 break;
2611 }
2612
Chris Lattner32e08b72005-03-28 22:03:13 +00002613 case ISD::BUILD_PAIR:
2614 // Legalize both operands. FIXME: in the future we should handle the case
2615 // where the two elements are not legal.
2616 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2617 Lo = LegalizeOp(Node->getOperand(0));
2618 Hi = LegalizeOp(Node->getOperand(1));
2619 break;
2620
Chris Lattner55e9cde2005-05-11 04:51:16 +00002621 case ISD::CTPOP:
2622 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002623 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2624 DAG.getNode(ISD::CTPOP, NVT, Lo),
2625 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002626 Hi = DAG.getConstant(0, NVT);
2627 break;
2628
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002629 case ISD::CTLZ: {
2630 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002631 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002632 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2633 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2634 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2635 HLZ, BitsC);
2636 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2637 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2638
2639 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2640 Hi = DAG.getConstant(0, NVT);
2641 break;
2642 }
2643
2644 case ISD::CTTZ: {
2645 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002646 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002647 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2648 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2649 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2650 LTZ, BitsC);
2651 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2652 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2653
2654 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2655 Hi = DAG.getConstant(0, NVT);
2656 break;
2657 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002658
Chris Lattnerdc750592005-01-07 07:47:09 +00002659 case ISD::LOAD: {
2660 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2661 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002662 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002663
2664 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002665 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002666 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2667 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002668 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002669 //are from the same instruction?
2670 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002671
2672 // Build a factor node to remember that this load is independent of the
2673 // other one.
2674 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2675 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002676
Chris Lattnerdc750592005-01-07 07:47:09 +00002677 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002678 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002679 if (!TLI.isLittleEndian())
2680 std::swap(Lo, Hi);
2681 break;
2682 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002683 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002684 case ISD::CALL: {
2685 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2686 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2687
Chris Lattner3d95c142005-01-19 20:24:35 +00002688 bool Changed = false;
2689 std::vector<SDOperand> Ops;
2690 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2691 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2692 Changed |= Ops.back() != Node->getOperand(i);
2693 }
2694
Chris Lattnerdc750592005-01-07 07:47:09 +00002695 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2696 "Can only expand a call once so far, not i64 -> i16!");
2697
2698 std::vector<MVT::ValueType> RetTyVTs;
2699 RetTyVTs.reserve(3);
2700 RetTyVTs.push_back(NVT);
2701 RetTyVTs.push_back(NVT);
2702 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002703 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2704 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002705 Lo = SDOperand(NC, 0);
2706 Hi = SDOperand(NC, 1);
2707
2708 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002709 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002710 break;
2711 }
2712 case ISD::AND:
2713 case ISD::OR:
2714 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2715 SDOperand LL, LH, RL, RH;
2716 ExpandOp(Node->getOperand(0), LL, LH);
2717 ExpandOp(Node->getOperand(1), RL, RH);
2718 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2719 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2720 break;
2721 }
2722 case ISD::SELECT: {
2723 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002724
2725 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2726 case Expand: assert(0 && "It's impossible to expand bools");
2727 case Legal:
2728 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2729 break;
2730 case Promote:
2731 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2732 break;
2733 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002734 ExpandOp(Node->getOperand(1), LL, LH);
2735 ExpandOp(Node->getOperand(2), RL, RH);
2736 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2737 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2738 break;
2739 }
2740 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002741 SDOperand In;
2742 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2743 case Expand: assert(0 && "expand-expand not implemented yet!");
2744 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2745 case Promote:
2746 In = PromoteOp(Node->getOperand(0));
2747 // Emit the appropriate sign_extend_inreg to get the value we want.
2748 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002749 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002750 break;
2751 }
2752
Chris Lattnerdc750592005-01-07 07:47:09 +00002753 // The low part is just a sign extension of the input (which degenerates to
2754 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002755 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002756
Chris Lattnerdc750592005-01-07 07:47:09 +00002757 // The high part is obtained by SRA'ing all but one of the bits of the lo
2758 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002759 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002760 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2761 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002762 break;
2763 }
Chris Lattner47844892005-04-03 23:41:52 +00002764 case ISD::ZERO_EXTEND: {
2765 SDOperand In;
2766 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2767 case Expand: assert(0 && "expand-expand not implemented yet!");
2768 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2769 case Promote:
2770 In = PromoteOp(Node->getOperand(0));
2771 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002772 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002773 break;
2774 }
2775
Chris Lattnerdc750592005-01-07 07:47:09 +00002776 // The low part is just a zero extension of the input (which degenerates to
2777 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002778 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002779
Chris Lattnerdc750592005-01-07 07:47:09 +00002780 // The high part is just a zero.
2781 Hi = DAG.getConstant(0, NVT);
2782 break;
Chris Lattner47844892005-04-03 23:41:52 +00002783 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002784 // These operators cannot be expanded directly, emit them as calls to
2785 // library functions.
2786 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00002787 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00002788 SDOperand Op;
2789 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2790 case Expand: assert(0 && "cannot expand FP!");
2791 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
2792 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
2793 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002794
Chris Lattner941d84a2005-07-30 01:40:57 +00002795 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
2796
Chris Lattnerfe68d752005-07-29 00:33:32 +00002797 // Now that the custom expander is done, expand the result, which is still
2798 // VT.
Chris Lattner941d84a2005-07-30 01:40:57 +00002799 ExpandOp(Op, Lo, Hi);
Chris Lattnerfe68d752005-07-29 00:33:32 +00002800 break;
2801 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002802
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002803 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002804 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002805 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002806 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002807 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002808
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002809 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00002810 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
2811 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
2812 LegalizeOp(Node->getOperand(0)));
2813 // Now that the custom expander is done, expand the result, which is still
2814 // VT.
2815 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
2816 break;
2817 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002818
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002819 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002820 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002821 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002822 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002823 break;
2824
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002825 case ISD::SHL:
2826 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002827 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002828 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002829
2830 // If this target supports SHL_PARTS, use it.
2831 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002832 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2833 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002834 break;
2835 }
2836
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002837 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002838 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002839 break;
2840
2841 case ISD::SRA:
2842 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002843 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002844 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002845
2846 // If this target supports SRA_PARTS, use it.
2847 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002848 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2849 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002850 break;
2851 }
2852
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002853 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002854 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002855 break;
2856 case ISD::SRL:
2857 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002858 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002859 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002860
2861 // If this target supports SRL_PARTS, use it.
2862 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002863 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2864 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002865 break;
2866 }
2867
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002868 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002869 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002870 break;
2871
Misha Brukman835702a2005-04-21 22:36:52 +00002872 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002873 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2874 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002875 break;
2876 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002877 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2878 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002879 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002880 case ISD::MUL: {
2881 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2882 SDOperand LL, LH, RL, RH;
2883 ExpandOp(Node->getOperand(0), LL, LH);
2884 ExpandOp(Node->getOperand(1), RL, RH);
2885 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2886 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2887 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2888 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2889 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2890 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2891 } else {
2892 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2893 }
2894 break;
2895 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002896 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2897 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2898 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2899 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002900 }
2901
2902 // Remember in a map if the values will be reused later.
2903 if (!Node->hasOneUse()) {
2904 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2905 std::make_pair(Lo, Hi))).second;
2906 assert(isNew && "Value already expanded?!?");
2907 }
2908}
2909
2910
2911// SelectionDAG::Legalize - This is the entry point for the file.
2912//
Chris Lattner4add7e32005-01-23 04:42:50 +00002913void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002914 /// run - This is the main entry point to this class.
2915 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002916 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002917}
2918