blob: ee515fd39001a7c18d18f8e6054b780f95aaea9b [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-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 Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-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 Lattner3e928bb2005-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 Lattner03c85462005-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 Lattner3e928bb2005-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 Lattner8afc48e2005-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 Lattner03c85462005-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 Lattner8afc48e2005-01-07 22:28:47 +000086
Chris Lattner3e928bb2005-01-07 07:47:09 +000087public:
88
Chris Lattner9c32d3b2005-01-23 04:42:50 +000089 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-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 Lattner03c85462005-01-15 05:21:40 +0000121 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122
Chris Lattner77e77a62005-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 Lattnercad063f2005-07-16 00:19:57 +0000127
128 SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
129 SDOperand PromoteLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
130
Chris Lattnere34b3962005-01-19 04:19:40 +0000131 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
132 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000133 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
134 SDOperand &Lo, SDOperand &Hi);
135 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000136 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000137
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000138 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
139
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140 SDOperand getIntPtrConstant(uint64_t Val) {
141 return DAG.getConstant(Val, TLI.getPointerTy());
142 }
143};
144}
145
146
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000147SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
148 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
149 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150 assert(MVT::LAST_VALUETYPE <= 16 &&
151 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000152}
153
Chris Lattnercad063f2005-07-16 00:19:57 +0000154/// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a
155/// UINT_TO_FP operation of the specified operand when the target requests that
156/// we expand it. At this point, we know that the result and operand types are
157/// legal for the target.
158SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0,
159 MVT::ValueType DestVT) {
160 assert(Op0.getValueType() == MVT::i32 &&
161 "This code only works for i32 input: extend in the future");
162 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
163
164 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(),
165 Op0,
166 DAG.getConstant(0,
167 Op0.getValueType()));
168 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
169 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
170 SignSet, Four, Zero);
171
172 uint64_t FF = 0x5f800000ULL;
173 if (TLI.isLittleEndian()) FF <<= 32;
174 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
175
176 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
177 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
178 TLI.getPointerTy());
179 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
180 SDOperand FudgeInReg;
181 if (DestVT == MVT::f32)
182 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
183 DAG.getSrcValue(NULL));
184 else {
185 assert(DestVT == MVT::f64 && "Unexpected conversion");
186 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
187 DAG.getEntryNode(), CPIdx,
188 DAG.getSrcValue(NULL), MVT::f32));
189 }
190
191 NeedsAnotherIteration = true;
192 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
193}
194
195/// PromoteLegalUINT_TO_FP - This function is responsible for legalizing a
196/// UINT_TO_FP operation of the specified operand when the target requests that
197/// we promote it. At this point, we know that the result and operand types are
198/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
199/// operation that takes a larger input.
200SDOperand SelectionDAGLegalize::PromoteLegalUINT_TO_FP(SDOperand LegalOp,
201 MVT::ValueType DestVT) {
202 // First step, figure out the appropriate *INT_TO_FP operation to use.
203 MVT::ValueType NewInTy = LegalOp.getValueType();
204
205 unsigned OpToUse = 0;
206
207 // Scan for the appropriate larger type to use.
208 while (1) {
209 NewInTy = (MVT::ValueType)(NewInTy+1);
210 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
211
212 // If the target supports SINT_TO_FP of this type, use it.
213 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
214 default: break;
215 case TargetLowering::Legal:
216 if (!TLI.hasNativeSupportFor(NewInTy))
217 break; // Can't use this datatype.
218 // FALL THROUGH.
219 case TargetLowering::Custom:
220 OpToUse = ISD::SINT_TO_FP;
221 break;
222 }
223 if (OpToUse) break;
224
225 // If the target supports UINT_TO_FP of this type, use it.
226 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
227 default: break;
228 case TargetLowering::Legal:
229 if (!TLI.hasNativeSupportFor(NewInTy))
230 break; // Can't use this datatype.
231 // FALL THROUGH.
232 case TargetLowering::Custom:
233 OpToUse = ISD::UINT_TO_FP;
234 break;
235 }
236 if (OpToUse) break;
237
238 // Otherwise, try a larger type.
239 }
240
241 // Make sure to legalize any nodes we create here in the next pass.
242 NeedsAnotherIteration = true;
243
244 // Okay, we found the operation and type to use. Zero extend our input to the
245 // desired type then run the operation on it.
246 return DAG.getNode(OpToUse, DestVT,
247 DAG.getNode(ISD::ZERO_EXTEND, NewInTy, LegalOp));
248}
249
Chris Lattner3e928bb2005-01-07 07:47:09 +0000250void SelectionDAGLegalize::LegalizeDAG() {
251 SDOperand OldRoot = DAG.getRoot();
252 SDOperand NewRoot = LegalizeOp(OldRoot);
253 DAG.setRoot(NewRoot);
254
255 ExpandedNodes.clear();
256 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000257 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000258
259 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000260 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000261}
262
263SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000264 assert(getTypeAction(Op.getValueType()) == Legal &&
265 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000266 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000267
Chris Lattner3e928bb2005-01-07 07:47:09 +0000268 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000269 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000270 if (Node->getNumValues() > 1) {
271 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
272 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000273 case Legal: break; // Nothing to do.
274 case Expand: {
275 SDOperand T1, T2;
276 ExpandOp(Op.getValue(i), T1, T2);
277 assert(LegalizedNodes.count(Op) &&
278 "Expansion didn't add legal operands!");
279 return LegalizedNodes[Op];
280 }
281 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000282 PromoteOp(Op.getValue(i));
283 assert(LegalizedNodes.count(Op) &&
284 "Expansion didn't add legal operands!");
285 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000286 }
287 }
288
Chris Lattner45982da2005-05-12 16:53:42 +0000289 // Note that LegalizeOp may be reentered even from single-use nodes, which
290 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000291 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
292 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000293
Chris Lattnerfa404e82005-01-09 19:03:49 +0000294 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000295
296 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000297
298 switch (Node->getOpcode()) {
299 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000300 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
301 // If this is a target node, legalize it by legalizing the operands then
302 // passing it through.
303 std::vector<SDOperand> Ops;
304 bool Changed = false;
305 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
306 Ops.push_back(LegalizeOp(Node->getOperand(i)));
307 Changed = Changed || Node->getOperand(i) != Ops.back();
308 }
309 if (Changed)
310 if (Node->getNumValues() == 1)
311 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
312 else {
313 std::vector<MVT::ValueType> VTs(Node->value_begin(),
314 Node->value_end());
315 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
316 }
317
318 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
319 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
320 return Result.getValue(Op.ResNo);
321 }
322 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000323 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
324 assert(0 && "Do not know how to legalize this operator!");
325 abort();
326 case ISD::EntryToken:
327 case ISD::FrameIndex:
328 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000329 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000330 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000331 assert(getTypeAction(Node->getValueType(0)) == Legal &&
332 "This must be legal!");
333 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000334 case ISD::CopyFromReg:
335 Tmp1 = LegalizeOp(Node->getOperand(0));
336 if (Tmp1 != Node->getOperand(0))
337 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
338 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000339 else
340 Result = Op.getValue(0);
341
342 // Since CopyFromReg produces two values, make sure to remember that we
343 // legalized both of them.
344 AddLegalizedOperand(Op.getValue(0), Result);
345 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
346 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000347 case ISD::ImplicitDef:
348 Tmp1 = LegalizeOp(Node->getOperand(0));
349 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000350 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000351 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000352 case ISD::UNDEF: {
353 MVT::ValueType VT = Op.getValueType();
354 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000355 default: assert(0 && "This action is not supported yet!");
356 case TargetLowering::Expand:
357 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000358 if (MVT::isInteger(VT))
359 Result = DAG.getConstant(0, VT);
360 else if (MVT::isFloatingPoint(VT))
361 Result = DAG.getConstantFP(0, VT);
362 else
363 assert(0 && "Unknown value type!");
364 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000365 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000366 break;
367 }
368 break;
369 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000370 case ISD::Constant:
371 // We know we don't need to expand constants here, constants only have one
372 // value and we check that it is fine above.
373
374 // FIXME: Maybe we should handle things like targets that don't support full
375 // 32-bit immediates?
376 break;
377 case ISD::ConstantFP: {
378 // Spill FP immediates to the constant pool if the target cannot directly
379 // codegen them. Targets often have some immediate values that can be
380 // efficiently generated into an FP register without a load. We explicitly
381 // leave these constants as ConstantFP nodes for the target to deal with.
382
383 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
384
385 // Check to see if this FP immediate is already legal.
386 bool isLegal = false;
387 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
388 E = TLI.legal_fpimm_end(); I != E; ++I)
389 if (CFP->isExactlyValue(*I)) {
390 isLegal = true;
391 break;
392 }
393
394 if (!isLegal) {
395 // Otherwise we need to spill the constant to memory.
396 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
397
398 bool Extend = false;
399
400 // If a FP immediate is precise when represented as a float, we put it
401 // into the constant pool as a float, even if it's is statically typed
402 // as a double.
403 MVT::ValueType VT = CFP->getValueType(0);
404 bool isDouble = VT == MVT::f64;
405 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
406 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000407 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
408 // Only do this if the target has a native EXTLOAD instruction from
409 // f32.
410 TLI.getOperationAction(ISD::EXTLOAD,
411 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000412 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
413 VT = MVT::f32;
414 Extend = true;
415 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000416
Chris Lattner3e928bb2005-01-07 07:47:09 +0000417 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
418 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000419 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000420 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
421 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000422 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000423 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
424 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000425 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000426 }
427 break;
428 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000429 case ISD::TokenFactor: {
430 std::vector<SDOperand> Ops;
431 bool Changed = false;
432 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000433 SDOperand Op = Node->getOperand(i);
434 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000435 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000436 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
437 Changed = true;
438 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
439 Ops.push_back(LegalizeOp(Op.getOperand(j)));
440 } else {
441 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
442 Changed |= Ops[i] != Op;
443 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000444 }
445 if (Changed)
446 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
447 break;
448 }
449
Chris Lattner16cd04d2005-05-12 23:24:06 +0000450 case ISD::CALLSEQ_START:
451 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000452 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000453 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000454 Tmp2 = Node->getOperand(0);
455 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000456 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000457
458 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
459 // this will cause the maps used to memoize results to get confused.
460 // Create and add a dummy use, just to increase its use count. This will
461 // be removed at the end of legalize when dead nodes are removed.
462 if (Tmp2.Val->hasOneUse())
463 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
464 DAG.getConstant(0, MVT::i32));
465 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000466 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000467 // nodes are treated specially and are mutated in place. This makes the dag
468 // legalization process more efficient and also makes libcall insertion
469 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000470 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000471 case ISD::DYNAMIC_STACKALLOC:
472 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
473 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
474 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
475 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000476 Tmp3 != Node->getOperand(2)) {
477 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
478 std::vector<SDOperand> Ops;
479 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
480 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
481 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000482 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000483
484 // Since this op produces two values, make sure to remember that we
485 // legalized both of them.
486 AddLegalizedOperand(SDOperand(Node, 0), Result);
487 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
488 return Result.getValue(Op.ResNo);
489
Chris Lattnerd71c0412005-05-13 18:43:43 +0000490 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000491 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000492 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
493 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000494
495 bool Changed = false;
496 std::vector<SDOperand> Ops;
497 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
498 Ops.push_back(LegalizeOp(Node->getOperand(i)));
499 Changed |= Ops.back() != Node->getOperand(i);
500 }
501
502 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000503 std::vector<MVT::ValueType> RetTyVTs;
504 RetTyVTs.reserve(Node->getNumValues());
505 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000506 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000507 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
508 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000509 } else {
510 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000511 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000512 // Since calls produce multiple values, make sure to remember that we
513 // legalized all of them.
514 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
515 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
516 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000517 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000518 case ISD::BR:
519 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
520 if (Tmp1 != Node->getOperand(0))
521 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
522 break;
523
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000524 case ISD::BRCOND:
525 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000526
527 switch (getTypeAction(Node->getOperand(1).getValueType())) {
528 case Expand: assert(0 && "It's impossible to expand bools");
529 case Legal:
530 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
531 break;
532 case Promote:
533 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
534 break;
535 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000536 // Basic block destination (Op#2) is always legal.
537 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
538 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
539 Node->getOperand(2));
540 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000541 case ISD::BRCONDTWOWAY:
542 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
543 switch (getTypeAction(Node->getOperand(1).getValueType())) {
544 case Expand: assert(0 && "It's impossible to expand bools");
545 case Legal:
546 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
547 break;
548 case Promote:
549 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
550 break;
551 }
552 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
553 // pair.
554 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
555 case TargetLowering::Promote:
556 default: assert(0 && "This action is not supported yet!");
557 case TargetLowering::Legal:
558 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
559 std::vector<SDOperand> Ops;
560 Ops.push_back(Tmp1);
561 Ops.push_back(Tmp2);
562 Ops.push_back(Node->getOperand(2));
563 Ops.push_back(Node->getOperand(3));
564 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
565 }
566 break;
567 case TargetLowering::Expand:
568 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
569 Node->getOperand(2));
570 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
571 break;
572 }
573 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000574
Chris Lattner3e928bb2005-01-07 07:47:09 +0000575 case ISD::LOAD:
576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
577 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000578
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579 if (Tmp1 != Node->getOperand(0) ||
580 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000581 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
582 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000583 else
584 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000585
Chris Lattner8afc48e2005-01-07 22:28:47 +0000586 // Since loads produce two values, make sure to remember that we legalized
587 // both of them.
588 AddLegalizedOperand(SDOperand(Node, 0), Result);
589 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
590 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000591
Chris Lattner0f69b292005-01-15 06:18:18 +0000592 case ISD::EXTLOAD:
593 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000594 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000595 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
596 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000597
Chris Lattner5f056bf2005-07-10 01:55:33 +0000598 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000599 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000600 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000601 case TargetLowering::Promote:
602 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000603 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
604 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000605 // Since loads produce two values, make sure to remember that we legalized
606 // both of them.
607 AddLegalizedOperand(SDOperand(Node, 0), Result);
608 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
609 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000610
Chris Lattner01ff7212005-04-10 22:54:25 +0000611 case TargetLowering::Legal:
612 if (Tmp1 != Node->getOperand(0) ||
613 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000614 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
615 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000616 else
617 Result = SDOperand(Node, 0);
618
619 // Since loads produce two values, make sure to remember that we legalized
620 // both of them.
621 AddLegalizedOperand(SDOperand(Node, 0), Result);
622 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
623 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000624 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000625 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
626 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
627 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000628 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000629 if (Op.ResNo)
630 return Load.getValue(1);
631 return Result;
632 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000633 assert(Node->getOpcode() != ISD::EXTLOAD &&
634 "EXTLOAD should always be supported!");
635 // Turn the unsupported load into an EXTLOAD followed by an explicit
636 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000637 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
638 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000639 SDOperand ValRes;
640 if (Node->getOpcode() == ISD::SEXTLOAD)
641 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000642 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000643 else
644 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000645 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
646 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
647 if (Op.ResNo)
648 return Result.getValue(1);
649 return ValRes;
650 }
651 assert(0 && "Unreachable");
652 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000653 case ISD::EXTRACT_ELEMENT:
654 // Get both the low and high parts.
655 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
656 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
657 Result = Tmp2; // 1 -> Hi
658 else
659 Result = Tmp1; // 0 -> Lo
660 break;
661
662 case ISD::CopyToReg:
663 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000664
Chris Lattner3e928bb2005-01-07 07:47:09 +0000665 switch (getTypeAction(Node->getOperand(1).getValueType())) {
666 case Legal:
667 // Legalize the incoming value (must be legal).
668 Tmp2 = LegalizeOp(Node->getOperand(1));
669 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000670 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000671 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000672 case Promote:
673 Tmp2 = PromoteOp(Node->getOperand(1));
674 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
675 break;
676 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000677 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000678 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000679 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000680 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
681 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
682 // Note that the copytoreg nodes are independent of each other.
683 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000684 assert(isTypeLegal(Result.getValueType()) &&
685 "Cannot expand multiple times yet (i64 -> i16)");
686 break;
687 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000688 break;
689
690 case ISD::RET:
691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
692 switch (Node->getNumOperands()) {
693 case 2: // ret val
694 switch (getTypeAction(Node->getOperand(1).getValueType())) {
695 case Legal:
696 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000697 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000698 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
699 break;
700 case Expand: {
701 SDOperand Lo, Hi;
702 ExpandOp(Node->getOperand(1), Lo, Hi);
703 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000704 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000705 }
706 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000707 Tmp2 = PromoteOp(Node->getOperand(1));
708 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
709 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000710 }
711 break;
712 case 1: // ret void
713 if (Tmp1 != Node->getOperand(0))
714 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
715 break;
716 default: { // ret <values>
717 std::vector<SDOperand> NewValues;
718 NewValues.push_back(Tmp1);
719 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
720 switch (getTypeAction(Node->getOperand(i).getValueType())) {
721 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000722 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000723 break;
724 case Expand: {
725 SDOperand Lo, Hi;
726 ExpandOp(Node->getOperand(i), Lo, Hi);
727 NewValues.push_back(Lo);
728 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000729 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000730 }
731 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000732 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000733 }
734 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
735 break;
736 }
737 }
738 break;
739 case ISD::STORE:
740 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
741 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
742
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000743 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000744 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000745 if (CFP->getValueType(0) == MVT::f32) {
746 union {
747 unsigned I;
748 float F;
749 } V;
750 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000751 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000752 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000753 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000754 } else {
755 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
756 union {
757 uint64_t I;
758 double F;
759 } V;
760 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000761 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000762 DAG.getConstant(V.I, MVT::i64), Tmp2,
763 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000764 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000765 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000766 }
767
Chris Lattner3e928bb2005-01-07 07:47:09 +0000768 switch (getTypeAction(Node->getOperand(1).getValueType())) {
769 case Legal: {
770 SDOperand Val = LegalizeOp(Node->getOperand(1));
771 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
772 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000773 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
774 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000775 break;
776 }
777 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000778 // Truncate the value and store the result.
779 Tmp3 = PromoteOp(Node->getOperand(1));
780 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000781 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000782 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +0000783 break;
784
Chris Lattner3e928bb2005-01-07 07:47:09 +0000785 case Expand:
786 SDOperand Lo, Hi;
787 ExpandOp(Node->getOperand(1), Lo, Hi);
788
789 if (!TLI.isLittleEndian())
790 std::swap(Lo, Hi);
791
Chris Lattneredb1add2005-05-11 04:51:16 +0000792 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
793 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000794 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000795 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
796 getIntPtrConstant(IncrementSize));
797 assert(isTypeLegal(Tmp2.getValueType()) &&
798 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000799 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000800 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
801 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000802 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
803 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000804 }
805 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000806 case ISD::PCMARKER:
807 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000808 if (Tmp1 != Node->getOperand(0))
809 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000810 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000811 case ISD::TRUNCSTORE:
812 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
813 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
814
815 switch (getTypeAction(Node->getOperand(1).getValueType())) {
816 case Legal:
817 Tmp2 = LegalizeOp(Node->getOperand(1));
818 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
819 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000820 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000821 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +0000822 break;
823 case Promote:
824 case Expand:
825 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
826 }
827 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000828 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000829 switch (getTypeAction(Node->getOperand(0).getValueType())) {
830 case Expand: assert(0 && "It's impossible to expand bools");
831 case Legal:
832 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
833 break;
834 case Promote:
835 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
836 break;
837 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000838 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000839 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000840
841 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
842 default: assert(0 && "This action is not supported yet!");
843 case TargetLowering::Legal:
844 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
845 Tmp3 != Node->getOperand(2))
846 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
847 Tmp1, Tmp2, Tmp3);
848 break;
849 case TargetLowering::Promote: {
850 MVT::ValueType NVT =
851 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
852 unsigned ExtOp, TruncOp;
853 if (MVT::isInteger(Tmp2.getValueType())) {
854 ExtOp = ISD::ZERO_EXTEND;
855 TruncOp = ISD::TRUNCATE;
856 } else {
857 ExtOp = ISD::FP_EXTEND;
858 TruncOp = ISD::FP_ROUND;
859 }
860 // Promote each of the values to the new type.
861 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
862 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
863 // Perform the larger operation, then round down.
864 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
865 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
866 break;
867 }
868 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000869 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000870 case ISD::SETCC:
871 switch (getTypeAction(Node->getOperand(0).getValueType())) {
872 case Legal:
873 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
874 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
875 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
876 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000877 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000878 break;
879 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000880 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
881 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
882
883 // If this is an FP compare, the operands have already been extended.
884 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
885 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000886 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000887
888 // Otherwise, we have to insert explicit sign or zero extends. Note
889 // that we could insert sign extends for ALL conditions, but zero extend
890 // is cheaper on many machines (an AND instead of two shifts), so prefer
891 // it.
892 switch (cast<SetCCSDNode>(Node)->getCondition()) {
893 default: assert(0 && "Unknown integer comparison!");
894 case ISD::SETEQ:
895 case ISD::SETNE:
896 case ISD::SETUGE:
897 case ISD::SETUGT:
898 case ISD::SETULE:
899 case ISD::SETULT:
900 // ALL of these operations will work if we either sign or zero extend
901 // the operands (including the unsigned comparisons!). Zero extend is
902 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000903 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
904 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000905 break;
906 case ISD::SETGE:
907 case ISD::SETGT:
908 case ISD::SETLT:
909 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +0000910 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
911 DAG.getValueType(VT));
912 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
913 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +0000914 break;
915 }
916
917 }
918 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000919 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000920 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000921 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000922 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
923 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
924 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
925 switch (cast<SetCCSDNode>(Node)->getCondition()) {
926 case ISD::SETEQ:
927 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000928 if (RHSLo == RHSHi)
929 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
930 if (RHSCST->isAllOnesValue()) {
931 // Comparison to -1.
932 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000933 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000934 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000935 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000936 }
937
Chris Lattner3e928bb2005-01-07 07:47:09 +0000938 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
939 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
940 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000941 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000942 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000943 DAG.getConstant(0, Tmp1.getValueType()));
944 break;
945 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000946 // If this is a comparison of the sign bit, just look at the top part.
947 // X > -1, x < 0
948 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000949 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000950 CST->getValue() == 0) || // X < 0
951 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
952 (CST->isAllOnesValue()))) // X > -1
953 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
954 Node->getValueType(0), LHSHi, RHSHi);
955
Chris Lattner3e928bb2005-01-07 07:47:09 +0000956 // FIXME: This generated code sucks.
957 ISD::CondCode LowCC;
958 switch (cast<SetCCSDNode>(Node)->getCondition()) {
959 default: assert(0 && "Unknown integer setcc!");
960 case ISD::SETLT:
961 case ISD::SETULT: LowCC = ISD::SETULT; break;
962 case ISD::SETGT:
963 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
964 case ISD::SETLE:
965 case ISD::SETULE: LowCC = ISD::SETULE; break;
966 case ISD::SETGE:
967 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
968 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000969
Chris Lattner3e928bb2005-01-07 07:47:09 +0000970 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
971 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
972 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
973
974 // NOTE: on targets without efficient SELECT of bools, we can always use
975 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000976 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000977 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000978 Node->getValueType(0), LHSHi, RHSHi);
979 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
980 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
981 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000982 break;
983 }
984 }
985 break;
986
Chris Lattnere1bd8222005-01-11 05:57:22 +0000987 case ISD::MEMSET:
988 case ISD::MEMCPY:
989 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000990 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000991 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
992
993 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
994 switch (getTypeAction(Node->getOperand(2).getValueType())) {
995 case Expand: assert(0 && "Cannot expand a byte!");
996 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000997 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000998 break;
999 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001000 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001001 break;
1002 }
1003 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001004 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001005 }
Chris Lattner272455b2005-02-02 03:44:41 +00001006
1007 SDOperand Tmp4;
1008 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001009 case Expand: {
1010 // Length is too big, just take the lo-part of the length.
1011 SDOperand HiPart;
1012 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1013 break;
1014 }
Chris Lattnere5605212005-01-28 22:29:18 +00001015 case Legal:
1016 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001017 break;
1018 case Promote:
1019 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001020 break;
1021 }
1022
1023 SDOperand Tmp5;
1024 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1025 case Expand: assert(0 && "Cannot expand this yet!");
1026 case Legal:
1027 Tmp5 = LegalizeOp(Node->getOperand(4));
1028 break;
1029 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001030 Tmp5 = PromoteOp(Node->getOperand(4));
1031 break;
1032 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001033
1034 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1035 default: assert(0 && "This action not implemented for this operation!");
1036 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001037 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1038 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1039 Tmp5 != Node->getOperand(4)) {
1040 std::vector<SDOperand> Ops;
1041 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1042 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1043 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1044 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001045 break;
1046 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001047 // Otherwise, the target does not support this operation. Lower the
1048 // operation to an explicit libcall as appropriate.
1049 MVT::ValueType IntPtr = TLI.getPointerTy();
1050 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1051 std::vector<std::pair<SDOperand, const Type*> > Args;
1052
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001053 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001054 if (Node->getOpcode() == ISD::MEMSET) {
1055 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1056 // Extend the ubyte argument to be an int value for the call.
1057 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1058 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1059 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1060
1061 FnName = "memset";
1062 } else if (Node->getOpcode() == ISD::MEMCPY ||
1063 Node->getOpcode() == ISD::MEMMOVE) {
1064 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1065 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1066 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1067 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1068 } else {
1069 assert(0 && "Unknown op!");
1070 }
Chris Lattner45982da2005-05-12 16:53:42 +00001071
Chris Lattnere1bd8222005-01-11 05:57:22 +00001072 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001073 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001074 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001075 Result = CallResult.second;
1076 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001077 break;
1078 }
1079 case TargetLowering::Custom:
1080 std::vector<SDOperand> Ops;
1081 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1082 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1083 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +00001084 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001085 Result = LegalizeOp(Result);
1086 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001087 }
1088 break;
1089 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001090
1091 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001092 Tmp1 = LegalizeOp(Node->getOperand(0));
1093 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001094
Chris Lattner3e011362005-05-14 07:45:46 +00001095 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1096 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1097 std::vector<SDOperand> Ops;
1098 Ops.push_back(Tmp1);
1099 Ops.push_back(Tmp2);
1100 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1101 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001102 Result = SDOperand(Node, 0);
1103 // Since these produce two values, make sure to remember that we legalized
1104 // both of them.
1105 AddLegalizedOperand(SDOperand(Node, 0), Result);
1106 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1107 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001108 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001109 Tmp1 = LegalizeOp(Node->getOperand(0));
1110 Tmp2 = LegalizeOp(Node->getOperand(1));
1111 Tmp3 = LegalizeOp(Node->getOperand(2));
1112 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1113 Tmp3 != Node->getOperand(2))
1114 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1115 break;
1116
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001117 case ISD::READIO:
1118 Tmp1 = LegalizeOp(Node->getOperand(0));
1119 Tmp2 = LegalizeOp(Node->getOperand(1));
1120
1121 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1122 case TargetLowering::Custom:
1123 default: assert(0 && "This action not implemented for this operation!");
1124 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001125 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1126 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1127 std::vector<SDOperand> Ops;
1128 Ops.push_back(Tmp1);
1129 Ops.push_back(Tmp2);
1130 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1131 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001132 Result = SDOperand(Node, 0);
1133 break;
1134 case TargetLowering::Expand:
1135 // Replace this with a load from memory.
1136 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1137 Node->getOperand(1), DAG.getSrcValue(NULL));
1138 Result = LegalizeOp(Result);
1139 break;
1140 }
1141
1142 // Since these produce two values, make sure to remember that we legalized
1143 // both of them.
1144 AddLegalizedOperand(SDOperand(Node, 0), Result);
1145 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1146 return Result.getValue(Op.ResNo);
1147
1148 case ISD::WRITEIO:
1149 Tmp1 = LegalizeOp(Node->getOperand(0));
1150 Tmp2 = LegalizeOp(Node->getOperand(1));
1151 Tmp3 = LegalizeOp(Node->getOperand(2));
1152
1153 switch (TLI.getOperationAction(Node->getOpcode(),
1154 Node->getOperand(1).getValueType())) {
1155 case TargetLowering::Custom:
1156 default: assert(0 && "This action not implemented for this operation!");
1157 case TargetLowering::Legal:
1158 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1159 Tmp3 != Node->getOperand(2))
1160 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1161 break;
1162 case TargetLowering::Expand:
1163 // Replace this with a store to memory.
1164 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1165 Node->getOperand(1), Node->getOperand(2),
1166 DAG.getSrcValue(NULL));
1167 Result = LegalizeOp(Result);
1168 break;
1169 }
1170 break;
1171
Chris Lattner84f67882005-01-20 18:52:28 +00001172 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001173 case ISD::SUB_PARTS:
1174 case ISD::SHL_PARTS:
1175 case ISD::SRA_PARTS:
1176 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001177 std::vector<SDOperand> Ops;
1178 bool Changed = false;
1179 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1180 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1181 Changed |= Ops.back() != Node->getOperand(i);
1182 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001183 if (Changed) {
1184 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1185 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1186 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001187
1188 // Since these produce multiple values, make sure to remember that we
1189 // legalized all of them.
1190 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1191 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1192 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001193 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001194
1195 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001196 case ISD::ADD:
1197 case ISD::SUB:
1198 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001199 case ISD::MULHS:
1200 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001201 case ISD::UDIV:
1202 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001203 case ISD::AND:
1204 case ISD::OR:
1205 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001206 case ISD::SHL:
1207 case ISD::SRL:
1208 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001210 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1211 case Expand: assert(0 && "Not possible");
1212 case Legal:
1213 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1214 break;
1215 case Promote:
1216 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1217 break;
1218 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001219 if (Tmp1 != Node->getOperand(0) ||
1220 Tmp2 != Node->getOperand(1))
1221 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1222 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001223
Nate Begemanc105e192005-04-06 00:23:54 +00001224 case ISD::UREM:
1225 case ISD::SREM:
1226 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1227 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1228 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1229 case TargetLowering::Legal:
1230 if (Tmp1 != Node->getOperand(0) ||
1231 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001232 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001233 Tmp2);
1234 break;
1235 case TargetLowering::Promote:
1236 case TargetLowering::Custom:
1237 assert(0 && "Cannot promote/custom handle this yet!");
1238 case TargetLowering::Expand: {
1239 MVT::ValueType VT = Node->getValueType(0);
1240 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1241 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1242 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1243 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1244 }
1245 break;
1246 }
1247 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001248
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001249 case ISD::CTPOP:
1250 case ISD::CTTZ:
1251 case ISD::CTLZ:
1252 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1253 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1254 case TargetLowering::Legal:
1255 if (Tmp1 != Node->getOperand(0))
1256 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1257 break;
1258 case TargetLowering::Promote: {
1259 MVT::ValueType OVT = Tmp1.getValueType();
1260 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001261
1262 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001263 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1264 // Perform the larger operation, then subtract if needed.
1265 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1266 switch(Node->getOpcode())
1267 {
1268 case ISD::CTPOP:
1269 Result = Tmp1;
1270 break;
1271 case ISD::CTTZ:
1272 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner39a8f332005-05-12 19:05:01 +00001273 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001274 DAG.getConstant(getSizeInBits(NVT), NVT));
1275 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1276 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1277 break;
1278 case ISD::CTLZ:
1279 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1280 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1281 DAG.getConstant(getSizeInBits(NVT) -
1282 getSizeInBits(OVT), NVT));
1283 break;
1284 }
1285 break;
1286 }
1287 case TargetLowering::Custom:
1288 assert(0 && "Cannot custom handle this yet!");
1289 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001290 switch(Node->getOpcode())
1291 {
1292 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001293 static const uint64_t mask[6] = {
1294 0x5555555555555555ULL, 0x3333333333333333ULL,
1295 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1296 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1297 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001298 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001299 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1300 unsigned len = getSizeInBits(VT);
1301 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001302 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001303 Tmp2 = DAG.getConstant(mask[i], VT);
1304 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001305 Tmp1 = DAG.getNode(ISD::ADD, VT,
1306 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1307 DAG.getNode(ISD::AND, VT,
1308 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1309 Tmp2));
1310 }
1311 Result = Tmp1;
1312 break;
1313 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001314 case ISD::CTLZ: {
1315 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001316 x = x | (x >> 1);
1317 x = x | (x >> 2);
1318 ...
1319 x = x | (x >>16);
1320 x = x | (x >>32); // for 64-bit input
1321 return popcount(~x);
Duraid Madina57ff7e52005-05-11 08:45:08 +00001322
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001323 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1324 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001325 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1326 unsigned len = getSizeInBits(VT);
1327 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1328 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1329 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1330 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1331 }
1332 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001333 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001334 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001335 }
1336 case ISD::CTTZ: {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001337 // for now, we use: { return popcount(~x & (x - 1)); }
1338 // unless the target has ctlz but not ctpop, in which case we use:
1339 // { return 32 - nlz(~x & (x-1)); }
1340 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001341 MVT::ValueType VT = Tmp1.getValueType();
1342 Tmp2 = DAG.getConstant(~0ULL, VT);
1343 Tmp3 = DAG.getNode(ISD::AND, VT,
1344 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1345 DAG.getNode(ISD::SUB, VT, Tmp1,
1346 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001347 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1348 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1349 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1350 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1351 DAG.getConstant(getSizeInBits(VT), VT),
1352 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1353 } else {
1354 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1355 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001356 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001357 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001358 default:
1359 assert(0 && "Cannot expand this yet!");
1360 break;
1361 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001362 break;
1363 }
1364 break;
1365
Chris Lattner2c8086f2005-04-02 05:00:07 +00001366 // Unary operators
1367 case ISD::FABS:
1368 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001369 case ISD::FSQRT:
1370 case ISD::FSIN:
1371 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001372 Tmp1 = LegalizeOp(Node->getOperand(0));
1373 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1374 case TargetLowering::Legal:
1375 if (Tmp1 != Node->getOperand(0))
1376 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1377 break;
1378 case TargetLowering::Promote:
1379 case TargetLowering::Custom:
1380 assert(0 && "Cannot promote/custom handle this yet!");
1381 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001382 switch(Node->getOpcode()) {
1383 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001384 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1385 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1386 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1387 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001388 break;
1389 }
1390 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001391 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1392 MVT::ValueType VT = Node->getValueType(0);
1393 Tmp2 = DAG.getConstantFP(0.0, VT);
1394 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1395 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1396 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1397 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001398 break;
1399 }
1400 case ISD::FSQRT:
1401 case ISD::FSIN:
1402 case ISD::FCOS: {
1403 MVT::ValueType VT = Node->getValueType(0);
1404 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1405 const char *FnName = 0;
1406 switch(Node->getOpcode()) {
1407 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1408 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1409 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1410 default: assert(0 && "Unreachable!");
1411 }
1412 std::vector<std::pair<SDOperand, const Type*> > Args;
1413 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001414 // FIXME: should use ExpandLibCall!
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001415 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001416 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001417 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1418 Result = LegalizeOp(CallResult.first);
1419 break;
1420 }
1421 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001422 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001423 }
1424 break;
1425 }
1426 break;
1427
1428 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001429 case ISD::ZERO_EXTEND:
1430 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001431 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001432 case ISD::FP_EXTEND:
1433 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001434 case ISD::FP_TO_SINT:
1435 case ISD::FP_TO_UINT:
1436 case ISD::SINT_TO_FP:
1437 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1439 case Legal:
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001440 //still made need to expand if the op is illegal, but the types are legal
Chris Lattnercad063f2005-07-16 00:19:57 +00001441 if (Node->getOpcode() == ISD::UINT_TO_FP) {
1442 switch (TLI.getOperationAction(Node->getOpcode(),
1443 Node->getOperand(0).getValueType())) {
1444 default: assert(0 && "Unknown operation action!");
1445 case TargetLowering::Expand:
1446 Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1447 Node->getValueType(0));
1448 AddLegalizedOperand(Op, Result);
1449 return Result;
1450 case TargetLowering::Promote:
1451 Result = PromoteLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1452 Node->getValueType(0));
1453 AddLegalizedOperand(Op, Result);
1454 return Result;
1455 case TargetLowering::Legal:
1456 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001457 }
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001458 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001459 Tmp1 = LegalizeOp(Node->getOperand(0));
1460 if (Tmp1 != Node->getOperand(0))
1461 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1462 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001463 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001464 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1465 Node->getOpcode() == ISD::UINT_TO_FP) {
1466 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1467 Node->getValueType(0), Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001468 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001469 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1470 // In the expand case, we must be dealing with a truncate, because
1471 // otherwise the result would be larger than the source.
1472 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001473
Chris Lattner2c8086f2005-04-02 05:00:07 +00001474 // Since the result is legal, we should just be able to truncate the low
1475 // part of the source.
1476 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1477 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001478 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001479 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001480
Chris Lattner03c85462005-01-15 05:21:40 +00001481 case Promote:
1482 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001483 case ISD::ZERO_EXTEND:
1484 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001485 // NOTE: Any extend would work here...
1486 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001487 Result = DAG.getZeroExtendInReg(Result,
1488 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001489 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001490 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001491 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001492 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001493 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001494 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001495 Result,
1496 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001497 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001498 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001499 Result = PromoteOp(Node->getOperand(0));
1500 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1501 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001502 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001503 Result = PromoteOp(Node->getOperand(0));
1504 if (Result.getValueType() != Op.getValueType())
1505 // Dynamically dead while we have only 2 FP types.
1506 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1507 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001508 case ISD::FP_ROUND:
1509 case ISD::FP_TO_SINT:
1510 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001511 Result = PromoteOp(Node->getOperand(0));
1512 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1513 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001514 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001515 Result = PromoteOp(Node->getOperand(0));
1516 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001517 Result,
1518 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattnerf8161d82005-01-16 05:06:12 +00001519 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1520 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001521 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001522 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001523 Result = DAG.getZeroExtendInReg(Result,
1524 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001525 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1526 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001527 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001528 }
1529 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001530 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001531 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001532 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001533 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001534
1535 // If this operation is not supported, convert it to a shl/shr or load/store
1536 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001537 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1538 default: assert(0 && "This action not supported for this op yet!");
1539 case TargetLowering::Legal:
1540 if (Tmp1 != Node->getOperand(0))
1541 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001542 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001543 break;
1544 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001545 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001546 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001547 // NOTE: we could fall back on load/store here too for targets without
1548 // SAR. However, it is doubtful that any exist.
1549 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1550 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001551 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001552 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1553 Node->getOperand(0), ShiftCst);
1554 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1555 Result, ShiftCst);
1556 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1557 // The only way we can lower this is to turn it into a STORETRUNC,
1558 // EXTLOAD pair, targetting a temporary location (a stack slot).
1559
1560 // NOTE: there is a choice here between constantly creating new stack
1561 // slots and always reusing the same one. We currently always create
1562 // new ones, as reuse may inhibit scheduling.
1563 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1564 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1565 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1566 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001567 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001568 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1569 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1570 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001571 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001572 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001573 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1574 Result, StackSlot, DAG.getSrcValue(NULL),
1575 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001576 } else {
1577 assert(0 && "Unknown op");
1578 }
1579 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001580 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001581 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001582 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001583 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001584 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001585
Chris Lattner45982da2005-05-12 16:53:42 +00001586 // Note that LegalizeOp may be reentered even from single-use nodes, which
1587 // means that we always must cache transformed nodes.
1588 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001589 return Result;
1590}
1591
Chris Lattner8b6fa222005-01-15 22:16:26 +00001592/// PromoteOp - Given an operation that produces a value in an invalid type,
1593/// promote it to compute the value into a larger type. The produced value will
1594/// have the correct bits for the low portion of the register, but no guarantee
1595/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001596SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1597 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001598 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001599 assert(getTypeAction(VT) == Promote &&
1600 "Caller should expand or legalize operands that are not promotable!");
1601 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1602 "Cannot promote to smaller type!");
1603
Chris Lattner03c85462005-01-15 05:21:40 +00001604 SDOperand Tmp1, Tmp2, Tmp3;
1605
1606 SDOperand Result;
1607 SDNode *Node = Op.Val;
1608
Chris Lattner45982da2005-05-12 16:53:42 +00001609 if (!Node->hasOneUse()) {
1610 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1611 if (I != PromotedNodes.end()) return I->second;
1612 } else {
1613 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1614 }
1615
Chris Lattner0f69b292005-01-15 06:18:18 +00001616 // Promotion needs an optimization step to clean up after it, and is not
1617 // careful to avoid operations the target does not support. Make sure that
1618 // all generated operations are legalized in the next iteration.
1619 NeedsAnotherIteration = true;
1620
Chris Lattner03c85462005-01-15 05:21:40 +00001621 switch (Node->getOpcode()) {
1622 default:
1623 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1624 assert(0 && "Do not know how to promote this operator!");
1625 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001626 case ISD::UNDEF:
1627 Result = DAG.getNode(ISD::UNDEF, NVT);
1628 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001629 case ISD::Constant:
1630 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1631 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1632 break;
1633 case ISD::ConstantFP:
1634 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1635 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1636 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001637 case ISD::CopyFromReg:
1638 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1639 Node->getOperand(0));
1640 // Remember that we legalized the chain.
1641 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1642 break;
1643
Chris Lattner82fbfb62005-01-18 02:59:52 +00001644 case ISD::SETCC:
1645 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1646 "SetCC type is not legal??");
1647 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1648 TLI.getSetCCResultTy(), Node->getOperand(0),
1649 Node->getOperand(1));
1650 Result = LegalizeOp(Result);
1651 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001652
1653 case ISD::TRUNCATE:
1654 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1655 case Legal:
1656 Result = LegalizeOp(Node->getOperand(0));
1657 assert(Result.getValueType() >= NVT &&
1658 "This truncation doesn't make sense!");
1659 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1660 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1661 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001662 case Promote:
1663 // The truncation is not required, because we don't guarantee anything
1664 // about high bits anyway.
1665 Result = PromoteOp(Node->getOperand(0));
1666 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001667 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001668 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1669 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001670 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001671 }
1672 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001673 case ISD::SIGN_EXTEND:
1674 case ISD::ZERO_EXTEND:
1675 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1676 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1677 case Legal:
1678 // Input is legal? Just do extend all the way to the larger type.
1679 Result = LegalizeOp(Node->getOperand(0));
1680 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1681 break;
1682 case Promote:
1683 // Promote the reg if it's smaller.
1684 Result = PromoteOp(Node->getOperand(0));
1685 // The high bits are not guaranteed to be anything. Insert an extend.
1686 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001687 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00001688 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001689 else
Chris Lattner23993562005-04-13 02:38:47 +00001690 Result = DAG.getZeroExtendInReg(Result,
1691 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001692 break;
1693 }
1694 break;
1695
1696 case ISD::FP_EXTEND:
1697 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1698 case ISD::FP_ROUND:
1699 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1700 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1701 case Promote: assert(0 && "Unreachable with 2 FP types!");
1702 case Legal:
1703 // Input is legal? Do an FP_ROUND_INREG.
1704 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001705 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1706 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001707 break;
1708 }
1709 break;
1710
1711 case ISD::SINT_TO_FP:
1712 case ISD::UINT_TO_FP:
1713 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1714 case Legal:
1715 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001716 // No extra round required here.
1717 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001718 break;
1719
1720 case Promote:
1721 Result = PromoteOp(Node->getOperand(0));
1722 if (Node->getOpcode() == ISD::SINT_TO_FP)
1723 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001724 Result,
1725 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001726 else
Chris Lattner23993562005-04-13 02:38:47 +00001727 Result = DAG.getZeroExtendInReg(Result,
1728 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001729 // No extra round required here.
1730 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001731 break;
1732 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001733 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1734 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001735 // Round if we cannot tolerate excess precision.
1736 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00001737 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1738 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00001739 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001740 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001741 break;
1742
1743 case ISD::FP_TO_SINT:
1744 case ISD::FP_TO_UINT:
1745 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1746 case Legal:
1747 Tmp1 = LegalizeOp(Node->getOperand(0));
1748 break;
1749 case Promote:
1750 // The input result is prerounded, so we don't have to do anything
1751 // special.
1752 Tmp1 = PromoteOp(Node->getOperand(0));
1753 break;
1754 case Expand:
1755 assert(0 && "not implemented");
1756 }
1757 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1758 break;
1759
Chris Lattner2c8086f2005-04-02 05:00:07 +00001760 case ISD::FABS:
1761 case ISD::FNEG:
1762 Tmp1 = PromoteOp(Node->getOperand(0));
1763 assert(Tmp1.getValueType() == NVT);
1764 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1765 // NOTE: we do not have to do any extra rounding here for
1766 // NoExcessFPPrecision, because we know the input will have the appropriate
1767 // precision, and these operations don't modify precision at all.
1768 break;
1769
Chris Lattnerda6ba872005-04-28 21:44:33 +00001770 case ISD::FSQRT:
1771 case ISD::FSIN:
1772 case ISD::FCOS:
1773 Tmp1 = PromoteOp(Node->getOperand(0));
1774 assert(Tmp1.getValueType() == NVT);
1775 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1776 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00001777 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1778 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00001779 break;
1780
Chris Lattner03c85462005-01-15 05:21:40 +00001781 case ISD::AND:
1782 case ISD::OR:
1783 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001784 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001785 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001786 case ISD::MUL:
1787 // The input may have strange things in the top bits of the registers, but
1788 // these operations don't care. They may have wierd bits going out, but
1789 // that too is okay if they are integer operations.
1790 Tmp1 = PromoteOp(Node->getOperand(0));
1791 Tmp2 = PromoteOp(Node->getOperand(1));
1792 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1793 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1794
1795 // However, if this is a floating point operation, they will give excess
1796 // precision that we may not be able to tolerate. If we DO allow excess
1797 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001798 // FIXME: Why would we need to round FP ops more than integer ones?
1799 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001800 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00001801 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1802 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00001803 break;
1804
Chris Lattner8b6fa222005-01-15 22:16:26 +00001805 case ISD::SDIV:
1806 case ISD::SREM:
1807 // These operators require that their input be sign extended.
1808 Tmp1 = PromoteOp(Node->getOperand(0));
1809 Tmp2 = PromoteOp(Node->getOperand(1));
1810 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001811 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1812 DAG.getValueType(VT));
1813 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1814 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001815 }
1816 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1817
1818 // Perform FP_ROUND: this is probably overly pessimistic.
1819 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00001820 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1821 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001822 break;
1823
1824 case ISD::UDIV:
1825 case ISD::UREM:
1826 // These operators require that their input be zero extended.
1827 Tmp1 = PromoteOp(Node->getOperand(0));
1828 Tmp2 = PromoteOp(Node->getOperand(1));
1829 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001830 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1831 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001832 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1833 break;
1834
1835 case ISD::SHL:
1836 Tmp1 = PromoteOp(Node->getOperand(0));
1837 Tmp2 = LegalizeOp(Node->getOperand(1));
1838 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1839 break;
1840 case ISD::SRA:
1841 // The input value must be properly sign extended.
1842 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001843 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1844 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001845 Tmp2 = LegalizeOp(Node->getOperand(1));
1846 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1847 break;
1848 case ISD::SRL:
1849 // The input value must be properly zero extended.
1850 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001851 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001852 Tmp2 = LegalizeOp(Node->getOperand(1));
1853 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1854 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001855 case ISD::LOAD:
1856 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1857 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001858 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001859 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00001860 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
1861 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001862 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00001863 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
1864 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001865
1866 // Remember that we legalized the chain.
1867 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1868 break;
1869 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1871 case Expand: assert(0 && "It's impossible to expand bools");
1872 case Legal:
1873 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1874 break;
1875 case Promote:
1876 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1877 break;
1878 }
Chris Lattner03c85462005-01-15 05:21:40 +00001879 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1880 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1881 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1882 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00001883 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00001884 case ISD::CALL: {
1885 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1886 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1887
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001888 std::vector<SDOperand> Ops;
1889 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1890 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1891
Chris Lattner8ac532c2005-01-16 19:46:48 +00001892 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1893 "Can only promote single result calls");
1894 std::vector<MVT::ValueType> RetTyVTs;
1895 RetTyVTs.reserve(2);
1896 RetTyVTs.push_back(NVT);
1897 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00001898 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1899 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001900 Result = SDOperand(NC, 0);
1901
1902 // Insert the new chain mapping.
1903 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1904 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001905 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001906 case ISD::CTPOP:
1907 case ISD::CTTZ:
1908 case ISD::CTLZ:
1909 Tmp1 = Node->getOperand(0);
1910 //Zero extend the argument
1911 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1912 // Perform the larger operation, then subtract if needed.
1913 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1914 switch(Node->getOpcode())
1915 {
1916 case ISD::CTPOP:
1917 Result = Tmp1;
1918 break;
1919 case ISD::CTTZ:
1920 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1921 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1922 DAG.getConstant(getSizeInBits(NVT), NVT));
1923 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1924 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1925 break;
1926 case ISD::CTLZ:
1927 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1928 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1929 DAG.getConstant(getSizeInBits(NVT) -
1930 getSizeInBits(VT), NVT));
1931 break;
1932 }
1933 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001934 }
1935
1936 assert(Result.Val && "Didn't set a result!");
1937 AddPromotedOperand(Op, Result);
1938 return Result;
1939}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001940
Chris Lattner84f67882005-01-20 18:52:28 +00001941/// ExpandAddSub - Find a clever way to expand this add operation into
1942/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001943void SelectionDAGLegalize::
1944ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1945 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001946 // Expand the subcomponents.
1947 SDOperand LHSL, LHSH, RHSL, RHSH;
1948 ExpandOp(LHS, LHSL, LHSH);
1949 ExpandOp(RHS, RHSL, RHSH);
1950
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001951 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00001952 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
1953 if (LHSL.getValueType() == MVT::i32) {
1954 SDOperand LowEl;
1955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1956 if (C->getValue() == 0)
1957 LowEl = RHSL;
1958 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1959 if (C->getValue() == 0)
1960 LowEl = LHSL;
1961 if (LowEl.Val) {
1962 // Turn this into an add/sub of the high part only.
1963 SDOperand HiEl =
1964 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1965 LowEl.getValueType(), LHSH, RHSH);
1966 Lo = LowEl;
1967 Hi = HiEl;
1968 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001969 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001970 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001971
Chris Lattner84f67882005-01-20 18:52:28 +00001972 std::vector<SDOperand> Ops;
1973 Ops.push_back(LHSL);
1974 Ops.push_back(LHSH);
1975 Ops.push_back(RHSL);
1976 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00001977
1978 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
1979 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001980 Hi = Lo.getValue(1);
1981}
1982
Chris Lattner5b359c62005-04-02 04:00:59 +00001983void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1984 SDOperand Op, SDOperand Amt,
1985 SDOperand &Lo, SDOperand &Hi) {
1986 // Expand the subcomponents.
1987 SDOperand LHSL, LHSH;
1988 ExpandOp(Op, LHSL, LHSH);
1989
1990 std::vector<SDOperand> Ops;
1991 Ops.push_back(LHSL);
1992 Ops.push_back(LHSH);
1993 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00001994 std::vector<MVT::ValueType> VTs;
1995 VTs.push_back(LHSL.getValueType());
1996 VTs.push_back(LHSH.getValueType());
1997 VTs.push_back(Amt.getValueType());
1998 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00001999 Hi = Lo.getValue(1);
2000}
2001
2002
Chris Lattnere34b3962005-01-19 04:19:40 +00002003/// ExpandShift - Try to find a clever way to expand this shift operation out to
2004/// smaller elements. If we can't find a way that is more efficient than a
2005/// libcall on this target, return false. Otherwise, return true with the
2006/// low-parts expanded into Lo and Hi.
2007bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2008 SDOperand &Lo, SDOperand &Hi) {
2009 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2010 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002011
Chris Lattnere34b3962005-01-19 04:19:40 +00002012 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002013 SDOperand ShAmt = LegalizeOp(Amt);
2014 MVT::ValueType ShTy = ShAmt.getValueType();
2015 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2016 unsigned NVTBits = MVT::getSizeInBits(NVT);
2017
2018 // Handle the case when Amt is an immediate. Other cases are currently broken
2019 // and are disabled.
2020 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2021 unsigned Cst = CN->getValue();
2022 // Expand the incoming operand to be shifted, so that we have its parts
2023 SDOperand InL, InH;
2024 ExpandOp(Op, InL, InH);
2025 switch(Opc) {
2026 case ISD::SHL:
2027 if (Cst > VTBits) {
2028 Lo = DAG.getConstant(0, NVT);
2029 Hi = DAG.getConstant(0, NVT);
2030 } else if (Cst > NVTBits) {
2031 Lo = DAG.getConstant(0, NVT);
2032 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002033 } else if (Cst == NVTBits) {
2034 Lo = DAG.getConstant(0, NVT);
2035 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002036 } else {
2037 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2038 Hi = DAG.getNode(ISD::OR, NVT,
2039 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2040 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2041 }
2042 return true;
2043 case ISD::SRL:
2044 if (Cst > VTBits) {
2045 Lo = DAG.getConstant(0, NVT);
2046 Hi = DAG.getConstant(0, NVT);
2047 } else if (Cst > NVTBits) {
2048 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2049 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002050 } else if (Cst == NVTBits) {
2051 Lo = InH;
2052 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002053 } else {
2054 Lo = DAG.getNode(ISD::OR, NVT,
2055 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2056 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2057 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2058 }
2059 return true;
2060 case ISD::SRA:
2061 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002062 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002063 DAG.getConstant(NVTBits-1, ShTy));
2064 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002065 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002066 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002067 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002068 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002069 } else if (Cst == NVTBits) {
2070 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002071 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002072 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002073 } else {
2074 Lo = DAG.getNode(ISD::OR, NVT,
2075 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2076 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2077 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2078 }
2079 return true;
2080 }
2081 }
2082 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2083 // so disable it for now. Currently targets are handling this via SHL_PARTS
2084 // and friends.
2085 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002086
2087 // If we have an efficient select operation (or if the selects will all fold
2088 // away), lower to some complex code, otherwise just emit the libcall.
2089 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2090 !isa<ConstantSDNode>(Amt))
2091 return false;
2092
2093 SDOperand InL, InH;
2094 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002095 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2096 DAG.getConstant(NVTBits, ShTy), ShAmt);
2097
Chris Lattnere5544f82005-01-20 20:29:23 +00002098 // Compare the unmasked shift amount against 32.
2099 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
2100 DAG.getConstant(NVTBits, ShTy));
2101
Chris Lattnere34b3962005-01-19 04:19:40 +00002102 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2103 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2104 DAG.getConstant(NVTBits-1, ShTy));
2105 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2106 DAG.getConstant(NVTBits-1, ShTy));
2107 }
2108
2109 if (Opc == ISD::SHL) {
2110 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2111 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2112 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002113 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002114
Chris Lattnere34b3962005-01-19 04:19:40 +00002115 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2116 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2117 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002118 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
2119 DAG.getSetCC(ISD::SETEQ,
2120 TLI.getSetCCResultTy(), NAmt,
2121 DAG.getConstant(32, ShTy)),
2122 DAG.getConstant(0, NVT),
2123 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002124 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002125 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002126 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002127 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002128
2129 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002130 if (Opc == ISD::SRA)
2131 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2132 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002133 else
2134 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002135 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002136 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002137 }
2138 return true;
2139}
Chris Lattner77e77a62005-01-21 06:05:23 +00002140
Chris Lattner9530ddc2005-05-13 05:09:11 +00002141/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2142/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002143/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002144static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002145 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
2146
Chris Lattner16cd04d2005-05-12 23:24:06 +00002147 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002148 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002149 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002150 Found = Node;
2151 return;
2152 }
2153
2154 // Otherwise, scan the operands of Node to see if any of them is a call.
2155 assert(Node->getNumOperands() != 0 &&
2156 "All leaves should have depth equal to the entry node!");
2157 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002158 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002159
2160 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002161 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002162 Found);
2163}
2164
2165
Chris Lattner9530ddc2005-05-13 05:09:11 +00002166/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2167/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002168/// than Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002169static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002170 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2171
Chris Lattner16cd04d2005-05-12 23:24:06 +00002172 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002173 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002174 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002175 Found = Node;
2176 return;
2177 }
2178
2179 // Otherwise, scan the operands of Node to see if any of them is a call.
2180 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2181 if (UI == E) return;
2182 for (--E; UI != E; ++UI)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002183 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002184
2185 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002186 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002187}
2188
Chris Lattner9530ddc2005-05-13 05:09:11 +00002189/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002190/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002191static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002192 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002193 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002194 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002195 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002196
2197 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002198 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002199
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002200 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002201 if (TheChain.getValueType() != MVT::Other)
2202 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002203 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002204
2205 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002206 E = Node->use_end(); ; ++UI) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002207 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002208
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002209 // Make sure to only follow users of our token chain.
2210 SDNode *User = *UI;
2211 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2212 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002213 if (SDNode *Result = FindCallSeqEnd(User))
2214 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002215 }
2216 assert(0 && "Unreachable");
2217 abort();
2218}
2219
Chris Lattner9530ddc2005-05-13 05:09:11 +00002220/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002221/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002222static SDNode *FindCallSeqStart(SDNode *Node) {
2223 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002224 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002225
2226 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2227 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002228 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002229}
2230
2231
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002232/// FindInputOutputChains - If we are replacing an operation with a call we need
2233/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002234/// properly serialize the calls in the block. The returned operand is the
2235/// input chain value for the new call (e.g. the entry node or the previous
2236/// call), and OutChain is set to be the chain node to update to point to the
2237/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002238static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2239 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002240 SDNode *LatestCallSeqStart = Entry.Val;
2241 SDNode *LatestCallSeqEnd = 0;
2242 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2243 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002244
Chris Lattner16cd04d2005-05-12 23:24:06 +00002245 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002246 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002247 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2248 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002249 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2250 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002251 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002252 LatestCallSeqEnd = Entry.Val;
2253 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002254
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002255 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002256 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002257 OutChain = 0;
Chris Lattner9530ddc2005-05-13 05:09:11 +00002258 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002259
Chris Lattner9530ddc2005-05-13 05:09:11 +00002260 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002261 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002262 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002263
Chris Lattner9530ddc2005-05-13 05:09:11 +00002264 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002265}
2266
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002267/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002268void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2269 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002270 // Nothing to splice it into?
2271 if (OutChain == 0) return;
2272
2273 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2274 //OutChain->dump();
2275
2276 // Form a token factor node merging the old inval and the new inval.
2277 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2278 OutChain->getOperand(0));
2279 // Change the node to refer to the new token.
2280 OutChain->setAdjCallChain(InToken);
2281}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002282
2283
Chris Lattner77e77a62005-01-21 06:05:23 +00002284// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2285// does not fit into a register, return the lo part and set the hi part to the
2286// by-reg argument. If it does fit into a single register, return the result
2287// and leave the Hi part unset.
2288SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2289 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002290 SDNode *OutChain;
2291 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2292 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002293 if (InChain.Val == 0)
2294 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002295
Chris Lattner77e77a62005-01-21 06:05:23 +00002296 TargetLowering::ArgListTy Args;
2297 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2298 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2299 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2300 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2301 }
2302 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002303
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002304 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002305 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002306 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002307 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2308 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002309 SpliceCallInto(CallInfo.second, OutChain);
2310
2311 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002312
2313 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002314 default: assert(0 && "Unknown thing");
2315 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002316 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002317 case Promote:
2318 assert(0 && "Cannot promote this yet!");
2319 case Expand:
2320 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002321 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002322 return Lo;
2323 }
2324}
2325
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002326
Chris Lattner77e77a62005-01-21 06:05:23 +00002327/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2328/// destination type is legal.
2329SDOperand SelectionDAGLegalize::
2330ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2331 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2332 assert(getTypeAction(Source.getValueType()) == Expand &&
2333 "This is not an expansion!");
2334 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2335
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002336 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002337 assert(Source.getValueType() == MVT::i64 &&
2338 "This only works for 64-bit -> FP");
2339 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2340 // incoming integer is set. To handle this, we dynamically test to see if
2341 // it is set, and, if so, add a fudge factor.
2342 SDOperand Lo, Hi;
2343 ExpandOp(Source, Lo, Hi);
2344
Chris Lattner66de05b2005-05-13 04:45:13 +00002345 // If this is unsigned, and not supported, first perform the conversion to
2346 // signed, then adjust the result if the sign bit is set.
2347 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2348 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2349
Chris Lattnere9c35e72005-04-13 05:09:42 +00002350 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2351 DAG.getConstant(0, Hi.getValueType()));
2352 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2353 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2354 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002355 uint64_t FF = 0x5f800000ULL;
2356 if (TLI.isLittleEndian()) FF <<= 32;
2357 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002358
2359 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2360 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2361 TLI.getPointerTy());
2362 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2363 SDOperand FudgeInReg;
2364 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002365 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2366 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002367 else {
2368 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002369 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2370 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002371 }
2372 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002373 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002374
Chris Lattnera88a2602005-05-14 05:33:54 +00002375 // Check to see if the target has a custom way to lower this. If so, use it.
2376 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2377 default: assert(0 && "This action not implemented for this operation!");
2378 case TargetLowering::Legal:
2379 case TargetLowering::Expand:
2380 break; // This case is handled below.
2381 case TargetLowering::Custom:
2382 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002383 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002384 }
2385
Chris Lattner13689e22005-05-12 07:00:44 +00002386 // Expand the source, then glue it back together for the call. We must expand
2387 // the source in case it is shared (this pass of legalize must traverse it).
2388 SDOperand SrcLo, SrcHi;
2389 ExpandOp(Source, SrcLo, SrcHi);
2390 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2391
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002392 SDNode *OutChain = 0;
2393 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2394 DAG.getEntryNode());
2395 const char *FnName = 0;
2396 if (DestTy == MVT::f32)
2397 FnName = "__floatdisf";
2398 else {
2399 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2400 FnName = "__floatdidf";
2401 }
2402
Chris Lattner77e77a62005-01-21 06:05:23 +00002403 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2404
2405 TargetLowering::ArgListTy Args;
2406 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002407
Chris Lattner77e77a62005-01-21 06:05:23 +00002408 Args.push_back(std::make_pair(Source, ArgTy));
2409
2410 // We don't care about token chains for libcalls. We just use the entry
2411 // node as our input and ignore the output chain. This allows us to place
2412 // calls wherever we need them to satisfy data dependences.
2413 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002414
2415 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002416 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2417 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002418
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002419 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002420 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002421}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002422
Chris Lattnere34b3962005-01-19 04:19:40 +00002423
2424
Chris Lattner3e928bb2005-01-07 07:47:09 +00002425/// ExpandOp - Expand the specified SDOperand into its two component pieces
2426/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2427/// LegalizeNodes map is filled in for any results that are not expanded, the
2428/// ExpandedNodes map is filled in for any results that are expanded, and the
2429/// Lo/Hi values are returned.
2430void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2431 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002432 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002433 SDNode *Node = Op.Val;
2434 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2435 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2436 assert(MVT::isInteger(NVT) && NVT < VT &&
2437 "Cannot expand to FP value or to larger int value!");
2438
2439 // If there is more than one use of this, see if we already expanded it.
2440 // There is no use remembering values that only have a single use, as the map
2441 // entries will never be reused.
2442 if (!Node->hasOneUse()) {
2443 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2444 = ExpandedNodes.find(Op);
2445 if (I != ExpandedNodes.end()) {
2446 Lo = I->second.first;
2447 Hi = I->second.second;
2448 return;
2449 }
Chris Lattner45982da2005-05-12 16:53:42 +00002450 } else {
2451 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002452 }
2453
Chris Lattner4e6c7462005-01-08 19:27:05 +00002454 // Expanding to multiple registers needs to perform an optimization step, and
2455 // is not careful to avoid operations the target does not support. Make sure
2456 // that all generated operations are legalized in the next iteration.
2457 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002458
Chris Lattner3e928bb2005-01-07 07:47:09 +00002459 switch (Node->getOpcode()) {
2460 default:
2461 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2462 assert(0 && "Do not know how to expand this operator!");
2463 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002464 case ISD::UNDEF:
2465 Lo = DAG.getNode(ISD::UNDEF, NVT);
2466 Hi = DAG.getNode(ISD::UNDEF, NVT);
2467 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002468 case ISD::Constant: {
2469 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2470 Lo = DAG.getConstant(Cst, NVT);
2471 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2472 break;
2473 }
2474
2475 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002476 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002477 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002478 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2479 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002480
Chris Lattner69a52152005-01-14 22:38:01 +00002481 // Remember that we legalized the chain.
2482 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2483
Chris Lattner3e928bb2005-01-07 07:47:09 +00002484 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2485 break;
2486 }
2487
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002488 case ISD::BUILD_PAIR:
2489 // Legalize both operands. FIXME: in the future we should handle the case
2490 // where the two elements are not legal.
2491 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2492 Lo = LegalizeOp(Node->getOperand(0));
2493 Hi = LegalizeOp(Node->getOperand(1));
2494 break;
2495
Chris Lattneredb1add2005-05-11 04:51:16 +00002496 case ISD::CTPOP:
2497 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002498 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2499 DAG.getNode(ISD::CTPOP, NVT, Lo),
2500 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002501 Hi = DAG.getConstant(0, NVT);
2502 break;
2503
Chris Lattner39a8f332005-05-12 19:05:01 +00002504 case ISD::CTLZ: {
2505 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002506 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002507 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2508 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2509 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2510 HLZ, BitsC);
2511 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2512 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2513
2514 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2515 Hi = DAG.getConstant(0, NVT);
2516 break;
2517 }
2518
2519 case ISD::CTTZ: {
2520 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002521 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002522 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2523 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2524 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2525 LTZ, BitsC);
2526 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2527 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2528
2529 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2530 Hi = DAG.getConstant(0, NVT);
2531 break;
2532 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002533
Chris Lattner3e928bb2005-01-07 07:47:09 +00002534 case ISD::LOAD: {
2535 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2536 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002537 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002538
2539 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002540 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002541 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2542 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002543 //Is this safe? declaring that the two parts of the split load
2544 //are from the same instruction?
2545 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002546
2547 // Build a factor node to remember that this load is independent of the
2548 // other one.
2549 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2550 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002551
Chris Lattner3e928bb2005-01-07 07:47:09 +00002552 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002553 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002554 if (!TLI.isLittleEndian())
2555 std::swap(Lo, Hi);
2556 break;
2557 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002558 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002559 case ISD::CALL: {
2560 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2561 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2562
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002563 bool Changed = false;
2564 std::vector<SDOperand> Ops;
2565 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2566 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2567 Changed |= Ops.back() != Node->getOperand(i);
2568 }
2569
Chris Lattner3e928bb2005-01-07 07:47:09 +00002570 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2571 "Can only expand a call once so far, not i64 -> i16!");
2572
2573 std::vector<MVT::ValueType> RetTyVTs;
2574 RetTyVTs.reserve(3);
2575 RetTyVTs.push_back(NVT);
2576 RetTyVTs.push_back(NVT);
2577 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002578 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2579 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002580 Lo = SDOperand(NC, 0);
2581 Hi = SDOperand(NC, 1);
2582
2583 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002584 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002585 break;
2586 }
2587 case ISD::AND:
2588 case ISD::OR:
2589 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2590 SDOperand LL, LH, RL, RH;
2591 ExpandOp(Node->getOperand(0), LL, LH);
2592 ExpandOp(Node->getOperand(1), RL, RH);
2593 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2594 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2595 break;
2596 }
2597 case ISD::SELECT: {
2598 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002599
2600 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2601 case Expand: assert(0 && "It's impossible to expand bools");
2602 case Legal:
2603 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2604 break;
2605 case Promote:
2606 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2607 break;
2608 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002609 ExpandOp(Node->getOperand(1), LL, LH);
2610 ExpandOp(Node->getOperand(2), RL, RH);
2611 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2612 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2613 break;
2614 }
2615 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002616 SDOperand In;
2617 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2618 case Expand: assert(0 && "expand-expand not implemented yet!");
2619 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2620 case Promote:
2621 In = PromoteOp(Node->getOperand(0));
2622 // Emit the appropriate sign_extend_inreg to get the value we want.
2623 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00002624 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00002625 break;
2626 }
2627
Chris Lattner3e928bb2005-01-07 07:47:09 +00002628 // The low part is just a sign extension of the input (which degenerates to
2629 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002630 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002631
Chris Lattner3e928bb2005-01-07 07:47:09 +00002632 // The high part is obtained by SRA'ing all but one of the bits of the lo
2633 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002634 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002635 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2636 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002637 break;
2638 }
Chris Lattner06098e02005-04-03 23:41:52 +00002639 case ISD::ZERO_EXTEND: {
2640 SDOperand In;
2641 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2642 case Expand: assert(0 && "expand-expand not implemented yet!");
2643 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2644 case Promote:
2645 In = PromoteOp(Node->getOperand(0));
2646 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002647 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002648 break;
2649 }
2650
Chris Lattner3e928bb2005-01-07 07:47:09 +00002651 // The low part is just a zero extension of the input (which degenerates to
2652 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002653 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002654
Chris Lattner3e928bb2005-01-07 07:47:09 +00002655 // The high part is just a zero.
2656 Hi = DAG.getConstant(0, NVT);
2657 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002658 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002659 // These operators cannot be expanded directly, emit them as calls to
2660 // library functions.
2661 case ISD::FP_TO_SINT:
2662 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002663 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002664 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002665 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002666 break;
2667 case ISD::FP_TO_UINT:
2668 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002669 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002670 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002671 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002672 break;
2673
Chris Lattnere34b3962005-01-19 04:19:40 +00002674 case ISD::SHL:
2675 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002676 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002677 break;
Chris Lattner47599822005-04-02 03:38:53 +00002678
2679 // If this target supports SHL_PARTS, use it.
2680 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002681 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2682 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002683 break;
2684 }
2685
Chris Lattnere34b3962005-01-19 04:19:40 +00002686 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002687 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002688 break;
2689
2690 case ISD::SRA:
2691 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002692 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002693 break;
Chris Lattner47599822005-04-02 03:38:53 +00002694
2695 // If this target supports SRA_PARTS, use it.
2696 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002697 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2698 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002699 break;
2700 }
2701
Chris Lattnere34b3962005-01-19 04:19:40 +00002702 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002703 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002704 break;
2705 case ISD::SRL:
2706 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002707 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002708 break;
Chris Lattner47599822005-04-02 03:38:53 +00002709
2710 // If this target supports SRL_PARTS, use it.
2711 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002712 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2713 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002714 break;
2715 }
2716
Chris Lattnere34b3962005-01-19 04:19:40 +00002717 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002718 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002719 break;
2720
Misha Brukmanedf128a2005-04-21 22:36:52 +00002721 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002722 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2723 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002724 break;
2725 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002726 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2727 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002728 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002729 case ISD::MUL: {
2730 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2731 SDOperand LL, LH, RL, RH;
2732 ExpandOp(Node->getOperand(0), LL, LH);
2733 ExpandOp(Node->getOperand(1), RL, RH);
2734 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2735 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2736 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2737 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2738 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2739 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2740 } else {
2741 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2742 }
2743 break;
2744 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002745 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2746 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2747 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2748 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002749 }
2750
2751 // Remember in a map if the values will be reused later.
2752 if (!Node->hasOneUse()) {
2753 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2754 std::make_pair(Lo, Hi))).second;
2755 assert(isNew && "Value already expanded?!?");
2756 }
2757}
2758
2759
2760// SelectionDAG::Legalize - This is the entry point for the file.
2761//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002762void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002763 /// run - This is the main entry point to this class.
2764 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002765 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002766}
2767