blob: 282dfd11ca9ce6166d4ee13136977cd97f614555 [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 Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattner3e928bb2005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner03c85462005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattner8afc48e2005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner03c85462005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
Chris Lattner9c32d3b2005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner77e77a62005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145}
146
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattnere1bd8222005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187
Chris Lattnerfa404e82005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000223 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000288
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000293 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000294 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
296 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000298 }
299 break;
300 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000301 case ISD::TokenFactor: {
302 std::vector<SDOperand> Ops;
303 bool Changed = false;
304 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000305 SDOperand Op = Node->getOperand(i);
306 // Fold single-use TokenFactor nodes into this token factor as we go.
307 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
308 Changed = true;
309 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
310 Ops.push_back(LegalizeOp(Op.getOperand(j)));
311 } else {
312 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
313 Changed |= Ops[i] != Op;
314 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000315 }
316 if (Changed)
317 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
318 break;
319 }
320
Chris Lattner3e928bb2005-01-07 07:47:09 +0000321 case ISD::ADJCALLSTACKDOWN:
322 case ISD::ADJCALLSTACKUP:
323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
324 // There is no need to legalize the size argument (Operand #1)
325 if (Tmp1 != Node->getOperand(0))
326 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
327 Node->getOperand(1));
328 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000329 case ISD::DYNAMIC_STACKALLOC:
330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
332 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
333 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
334 Tmp3 != Node->getOperand(2))
335 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
336 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000337 else
338 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000339
340 // Since this op produces two values, make sure to remember that we
341 // legalized both of them.
342 AddLegalizedOperand(SDOperand(Node, 0), Result);
343 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
344 return Result.getValue(Op.ResNo);
345
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000346 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
348 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000349
350 bool Changed = false;
351 std::vector<SDOperand> Ops;
352 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
353 Ops.push_back(LegalizeOp(Node->getOperand(i)));
354 Changed |= Ops.back() != Node->getOperand(i);
355 }
356
357 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358 std::vector<MVT::ValueType> RetTyVTs;
359 RetTyVTs.reserve(Node->getNumValues());
360 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000361 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000362 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000363 } else {
364 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000365 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000366 // Since calls produce multiple values, make sure to remember that we
367 // legalized all of them.
368 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
369 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
370 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000371 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000372 case ISD::BR:
373 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
374 if (Tmp1 != Node->getOperand(0))
375 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
376 break;
377
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000378 case ISD::BRCOND:
379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000380
381 switch (getTypeAction(Node->getOperand(1).getValueType())) {
382 case Expand: assert(0 && "It's impossible to expand bools");
383 case Legal:
384 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
385 break;
386 case Promote:
387 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
388 break;
389 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000390 // Basic block destination (Op#2) is always legal.
391 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
392 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
393 Node->getOperand(2));
394 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000395 case ISD::BRCONDTWOWAY:
396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
397 switch (getTypeAction(Node->getOperand(1).getValueType())) {
398 case Expand: assert(0 && "It's impossible to expand bools");
399 case Legal:
400 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
401 break;
402 case Promote:
403 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
404 break;
405 }
406 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
407 // pair.
408 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
409 case TargetLowering::Promote:
410 default: assert(0 && "This action is not supported yet!");
411 case TargetLowering::Legal:
412 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
413 std::vector<SDOperand> Ops;
414 Ops.push_back(Tmp1);
415 Ops.push_back(Tmp2);
416 Ops.push_back(Node->getOperand(2));
417 Ops.push_back(Node->getOperand(3));
418 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
419 }
420 break;
421 case TargetLowering::Expand:
422 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
423 Node->getOperand(2));
424 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
425 break;
426 }
427 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000428
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429 case ISD::LOAD:
430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
431 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000432
Chris Lattner3e928bb2005-01-07 07:47:09 +0000433 if (Tmp1 != Node->getOperand(0) ||
434 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000435 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
436 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000437 else
438 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000439
Chris Lattner8afc48e2005-01-07 22:28:47 +0000440 // Since loads produce two values, make sure to remember that we legalized
441 // both of them.
442 AddLegalizedOperand(SDOperand(Node, 0), Result);
443 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
444 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000445
Chris Lattner0f69b292005-01-15 06:18:18 +0000446 case ISD::EXTLOAD:
447 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000448 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
450 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000451
Chris Lattner01ff7212005-04-10 22:54:25 +0000452 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
453 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000454 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000455 case TargetLowering::Promote:
456 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
457 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000458 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000459 // Since loads produce two values, make sure to remember that we legalized
460 // both of them.
461 AddLegalizedOperand(SDOperand(Node, 0), Result);
462 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
463 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000464
Chris Lattner01ff7212005-04-10 22:54:25 +0000465 case TargetLowering::Legal:
466 if (Tmp1 != Node->getOperand(0) ||
467 Tmp2 != Node->getOperand(1))
468 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000469 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000470 else
471 Result = SDOperand(Node, 0);
472
473 // Since loads produce two values, make sure to remember that we legalized
474 // both of them.
475 AddLegalizedOperand(SDOperand(Node, 0), Result);
476 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
477 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000478 case TargetLowering::Expand:
479 assert(Node->getOpcode() != ISD::EXTLOAD &&
480 "EXTLOAD should always be supported!");
481 // Turn the unsupported load into an EXTLOAD followed by an explicit
482 // zero/sign extend inreg.
483 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000484 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000485 SDOperand ValRes;
486 if (Node->getOpcode() == ISD::SEXTLOAD)
487 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
488 Result, SrcVT);
489 else
490 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000491 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
492 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
493 if (Op.ResNo)
494 return Result.getValue(1);
495 return ValRes;
496 }
497 assert(0 && "Unreachable");
498 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499 case ISD::EXTRACT_ELEMENT:
500 // Get both the low and high parts.
501 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
502 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
503 Result = Tmp2; // 1 -> Hi
504 else
505 Result = Tmp1; // 0 -> Lo
506 break;
507
508 case ISD::CopyToReg:
509 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000510
Chris Lattner3e928bb2005-01-07 07:47:09 +0000511 switch (getTypeAction(Node->getOperand(1).getValueType())) {
512 case Legal:
513 // Legalize the incoming value (must be legal).
514 Tmp2 = LegalizeOp(Node->getOperand(1));
515 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000516 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000517 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000518 case Promote:
519 Tmp2 = PromoteOp(Node->getOperand(1));
520 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
521 break;
522 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000523 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000524 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000525 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000526 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
527 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
528 // Note that the copytoreg nodes are independent of each other.
529 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000530 assert(isTypeLegal(Result.getValueType()) &&
531 "Cannot expand multiple times yet (i64 -> i16)");
532 break;
533 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534 break;
535
536 case ISD::RET:
537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
538 switch (Node->getNumOperands()) {
539 case 2: // ret val
540 switch (getTypeAction(Node->getOperand(1).getValueType())) {
541 case Legal:
542 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000543 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000544 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
545 break;
546 case Expand: {
547 SDOperand Lo, Hi;
548 ExpandOp(Node->getOperand(1), Lo, Hi);
549 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000550 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 }
552 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000553 Tmp2 = PromoteOp(Node->getOperand(1));
554 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
555 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000556 }
557 break;
558 case 1: // ret void
559 if (Tmp1 != Node->getOperand(0))
560 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
561 break;
562 default: { // ret <values>
563 std::vector<SDOperand> NewValues;
564 NewValues.push_back(Tmp1);
565 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
566 switch (getTypeAction(Node->getOperand(i).getValueType())) {
567 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000568 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569 break;
570 case Expand: {
571 SDOperand Lo, Hi;
572 ExpandOp(Node->getOperand(i), Lo, Hi);
573 NewValues.push_back(Lo);
574 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000575 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576 }
577 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000578 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579 }
580 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
581 break;
582 }
583 }
584 break;
585 case ISD::STORE:
586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
587 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
588
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000589 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000590 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000591 if (CFP->getValueType(0) == MVT::f32) {
592 union {
593 unsigned I;
594 float F;
595 } V;
596 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000597 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000598 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000599 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000600 } else {
601 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
602 union {
603 uint64_t I;
604 double F;
605 } V;
606 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000607 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000608 DAG.getConstant(V.I, MVT::i64), Tmp2,
609 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000610 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000611 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000612 }
613
Chris Lattner3e928bb2005-01-07 07:47:09 +0000614 switch (getTypeAction(Node->getOperand(1).getValueType())) {
615 case Legal: {
616 SDOperand Val = LegalizeOp(Node->getOperand(1));
617 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
618 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000619 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
620 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000621 break;
622 }
623 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000624 // Truncate the value and store the result.
625 Tmp3 = PromoteOp(Node->getOperand(1));
626 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000627 Node->getOperand(3),
628 Node->getOperand(1).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000629 break;
630
Chris Lattner3e928bb2005-01-07 07:47:09 +0000631 case Expand:
632 SDOperand Lo, Hi;
633 ExpandOp(Node->getOperand(1), Lo, Hi);
634
635 if (!TLI.isLittleEndian())
636 std::swap(Lo, Hi);
637
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000638 Lo = DAG.getNode(ISD::STORE, MVT::Other,Tmp1, Lo, Tmp2,Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000639
Chris Lattnerec39a452005-01-19 18:02:17 +0000640 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
642 getIntPtrConstant(IncrementSize));
643 assert(isTypeLegal(Tmp2.getValueType()) &&
644 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000645 //Again, claiming both parts of the store came form the same Instr
646 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, Node->getOperand(3));
647
Chris Lattnerec39a452005-01-19 18:02:17 +0000648 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
649 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000650 }
651 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000652 case ISD::PCMARKER:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000654 if (Tmp1 != Node->getOperand(0))
655 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000656 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000657 case ISD::TRUNCSTORE:
658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
659 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
660
661 switch (getTypeAction(Node->getOperand(1).getValueType())) {
662 case Legal:
663 Tmp2 = LegalizeOp(Node->getOperand(1));
664 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
665 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000666 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000667 Node->getOperand(3),
Chris Lattner0f69b292005-01-15 06:18:18 +0000668 cast<MVTSDNode>(Node)->getExtraValueType());
669 break;
670 case Promote:
671 case Expand:
672 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
673 }
674 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000675 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000676 switch (getTypeAction(Node->getOperand(0).getValueType())) {
677 case Expand: assert(0 && "It's impossible to expand bools");
678 case Legal:
679 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
680 break;
681 case Promote:
682 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
683 break;
684 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000686 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000687
688 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
689 default: assert(0 && "This action is not supported yet!");
690 case TargetLowering::Legal:
691 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
692 Tmp3 != Node->getOperand(2))
693 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
694 Tmp1, Tmp2, Tmp3);
695 break;
696 case TargetLowering::Promote: {
697 MVT::ValueType NVT =
698 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
699 unsigned ExtOp, TruncOp;
700 if (MVT::isInteger(Tmp2.getValueType())) {
701 ExtOp = ISD::ZERO_EXTEND;
702 TruncOp = ISD::TRUNCATE;
703 } else {
704 ExtOp = ISD::FP_EXTEND;
705 TruncOp = ISD::FP_ROUND;
706 }
707 // Promote each of the values to the new type.
708 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
709 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
710 // Perform the larger operation, then round down.
711 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
712 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
713 break;
714 }
715 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000716 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000717 case ISD::SETCC:
718 switch (getTypeAction(Node->getOperand(0).getValueType())) {
719 case Legal:
720 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
722 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
723 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000724 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725 break;
726 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000727 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
728 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
729
730 // If this is an FP compare, the operands have already been extended.
731 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
732 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000733 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000734
735 // Otherwise, we have to insert explicit sign or zero extends. Note
736 // that we could insert sign extends for ALL conditions, but zero extend
737 // is cheaper on many machines (an AND instead of two shifts), so prefer
738 // it.
739 switch (cast<SetCCSDNode>(Node)->getCondition()) {
740 default: assert(0 && "Unknown integer comparison!");
741 case ISD::SETEQ:
742 case ISD::SETNE:
743 case ISD::SETUGE:
744 case ISD::SETUGT:
745 case ISD::SETULE:
746 case ISD::SETULT:
747 // ALL of these operations will work if we either sign or zero extend
748 // the operands (including the unsigned comparisons!). Zero extend is
749 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000750 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
751 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000752 break;
753 case ISD::SETGE:
754 case ISD::SETGT:
755 case ISD::SETLT:
756 case ISD::SETLE:
757 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
758 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
759 break;
760 }
761
762 }
763 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000764 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000765 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000766 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000767 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
768 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
769 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
770 switch (cast<SetCCSDNode>(Node)->getCondition()) {
771 case ISD::SETEQ:
772 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000773 if (RHSLo == RHSHi)
774 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
775 if (RHSCST->isAllOnesValue()) {
776 // Comparison to -1.
777 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000778 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000779 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000780 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000781 }
782
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
784 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
785 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000786 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000787 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788 DAG.getConstant(0, Tmp1.getValueType()));
789 break;
790 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000791 // If this is a comparison of the sign bit, just look at the top part.
792 // X > -1, x < 0
793 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000794 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000795 CST->getValue() == 0) || // X < 0
796 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
797 (CST->isAllOnesValue()))) // X > -1
798 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
799 Node->getValueType(0), LHSHi, RHSHi);
800
Chris Lattner3e928bb2005-01-07 07:47:09 +0000801 // FIXME: This generated code sucks.
802 ISD::CondCode LowCC;
803 switch (cast<SetCCSDNode>(Node)->getCondition()) {
804 default: assert(0 && "Unknown integer setcc!");
805 case ISD::SETLT:
806 case ISD::SETULT: LowCC = ISD::SETULT; break;
807 case ISD::SETGT:
808 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
809 case ISD::SETLE:
810 case ISD::SETULE: LowCC = ISD::SETULE; break;
811 case ISD::SETGE:
812 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
813 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000814
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
816 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
817 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
818
819 // NOTE: on targets without efficient SELECT of bools, we can always use
820 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000821 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000822 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000823 Node->getValueType(0), LHSHi, RHSHi);
824 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
825 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
826 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000827 break;
828 }
829 }
830 break;
831
Chris Lattnere1bd8222005-01-11 05:57:22 +0000832 case ISD::MEMSET:
833 case ISD::MEMCPY:
834 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000836 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
837
838 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
839 switch (getTypeAction(Node->getOperand(2).getValueType())) {
840 case Expand: assert(0 && "Cannot expand a byte!");
841 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000842 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000843 break;
844 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000845 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000846 break;
847 }
848 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000849 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000850 }
Chris Lattner272455b2005-02-02 03:44:41 +0000851
852 SDOperand Tmp4;
853 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000854 case Expand: assert(0 && "Cannot expand this yet!");
855 case Legal:
856 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000857 break;
858 case Promote:
859 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000860 break;
861 }
862
863 SDOperand Tmp5;
864 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
865 case Expand: assert(0 && "Cannot expand this yet!");
866 case Legal:
867 Tmp5 = LegalizeOp(Node->getOperand(4));
868 break;
869 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000870 Tmp5 = PromoteOp(Node->getOperand(4));
871 break;
872 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000873
874 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
875 default: assert(0 && "This action not implemented for this operation!");
876 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000877 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
878 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
879 Tmp5 != Node->getOperand(4)) {
880 std::vector<SDOperand> Ops;
881 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
882 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
883 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
884 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000885 break;
886 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000887 // Otherwise, the target does not support this operation. Lower the
888 // operation to an explicit libcall as appropriate.
889 MVT::ValueType IntPtr = TLI.getPointerTy();
890 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
891 std::vector<std::pair<SDOperand, const Type*> > Args;
892
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000893 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000894 if (Node->getOpcode() == ISD::MEMSET) {
895 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
896 // Extend the ubyte argument to be an int value for the call.
897 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
898 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
899 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
900
901 FnName = "memset";
902 } else if (Node->getOpcode() == ISD::MEMCPY ||
903 Node->getOpcode() == ISD::MEMMOVE) {
904 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
905 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
906 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
907 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
908 } else {
909 assert(0 && "Unknown op!");
910 }
911 std::pair<SDOperand,SDOperand> CallResult =
Nate Begeman8e21e712005-03-26 01:29:23 +0000912 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000913 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
914 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000915 break;
916 }
917 case TargetLowering::Custom:
918 std::vector<SDOperand> Ops;
919 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
920 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
921 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
922 Result = TLI.LowerOperation(Result);
923 Result = LegalizeOp(Result);
924 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000925 }
926 break;
927 }
Chris Lattner52d08bd2005-05-09 20:23:03 +0000928
929 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000930 Tmp1 = LegalizeOp(Node->getOperand(0));
931 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000932
Chris Lattner52d08bd2005-05-09 20:23:03 +0000933 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000934 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000935 else
936 Result = SDOperand(Node, 0);
937 // Since these produce two values, make sure to remember that we legalized
938 // both of them.
939 AddLegalizedOperand(SDOperand(Node, 0), Result);
940 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
941 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000942 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000943 Tmp1 = LegalizeOp(Node->getOperand(0));
944 Tmp2 = LegalizeOp(Node->getOperand(1));
945 Tmp3 = LegalizeOp(Node->getOperand(2));
946 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
947 Tmp3 != Node->getOperand(2))
948 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
949 break;
950
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000951 case ISD::READIO:
952 Tmp1 = LegalizeOp(Node->getOperand(0));
953 Tmp2 = LegalizeOp(Node->getOperand(1));
954
955 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
956 case TargetLowering::Custom:
957 default: assert(0 && "This action not implemented for this operation!");
958 case TargetLowering::Legal:
959 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
960 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
961 Tmp1, Tmp2);
962 else
963 Result = SDOperand(Node, 0);
964 break;
965 case TargetLowering::Expand:
966 // Replace this with a load from memory.
967 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
968 Node->getOperand(1), DAG.getSrcValue(NULL));
969 Result = LegalizeOp(Result);
970 break;
971 }
972
973 // Since these produce two values, make sure to remember that we legalized
974 // both of them.
975 AddLegalizedOperand(SDOperand(Node, 0), Result);
976 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
977 return Result.getValue(Op.ResNo);
978
979 case ISD::WRITEIO:
980 Tmp1 = LegalizeOp(Node->getOperand(0));
981 Tmp2 = LegalizeOp(Node->getOperand(1));
982 Tmp3 = LegalizeOp(Node->getOperand(2));
983
984 switch (TLI.getOperationAction(Node->getOpcode(),
985 Node->getOperand(1).getValueType())) {
986 case TargetLowering::Custom:
987 default: assert(0 && "This action not implemented for this operation!");
988 case TargetLowering::Legal:
989 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
990 Tmp3 != Node->getOperand(2))
991 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
992 break;
993 case TargetLowering::Expand:
994 // Replace this with a store to memory.
995 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
996 Node->getOperand(1), Node->getOperand(2),
997 DAG.getSrcValue(NULL));
998 Result = LegalizeOp(Result);
999 break;
1000 }
1001 break;
1002
Chris Lattner84f67882005-01-20 18:52:28 +00001003 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001004 case ISD::SUB_PARTS:
1005 case ISD::SHL_PARTS:
1006 case ISD::SRA_PARTS:
1007 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001008 std::vector<SDOperand> Ops;
1009 bool Changed = false;
1010 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1011 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1012 Changed |= Ops.back() != Node->getOperand(i);
1013 }
1014 if (Changed)
1015 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001016
1017 // Since these produce multiple values, make sure to remember that we
1018 // legalized all of them.
1019 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1020 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1021 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001022 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001023
1024 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001025 case ISD::ADD:
1026 case ISD::SUB:
1027 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001028 case ISD::MULHS:
1029 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001030 case ISD::UDIV:
1031 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001032 case ISD::AND:
1033 case ISD::OR:
1034 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001035 case ISD::SHL:
1036 case ISD::SRL:
1037 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001038 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1039 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1040 if (Tmp1 != Node->getOperand(0) ||
1041 Tmp2 != Node->getOperand(1))
1042 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1043 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001044
Nate Begemanc105e192005-04-06 00:23:54 +00001045 case ISD::UREM:
1046 case ISD::SREM:
1047 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1048 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1049 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1050 case TargetLowering::Legal:
1051 if (Tmp1 != Node->getOperand(0) ||
1052 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001053 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001054 Tmp2);
1055 break;
1056 case TargetLowering::Promote:
1057 case TargetLowering::Custom:
1058 assert(0 && "Cannot promote/custom handle this yet!");
1059 case TargetLowering::Expand: {
1060 MVT::ValueType VT = Node->getValueType(0);
1061 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1062 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1063 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1064 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1065 }
1066 break;
1067 }
1068 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001069
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001070 case ISD::CTPOP:
1071 case ISD::CTTZ:
1072 case ISD::CTLZ:
1073 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1074 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1075 case TargetLowering::Legal:
1076 if (Tmp1 != Node->getOperand(0))
1077 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1078 break;
1079 case TargetLowering::Promote: {
1080 MVT::ValueType OVT = Tmp1.getValueType();
1081 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1082 //Zero extend the argument
1083 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1084 // Perform the larger operation, then subtract if needed.
1085 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1086 switch(Node->getOpcode())
1087 {
1088 case ISD::CTPOP:
1089 Result = Tmp1;
1090 break;
1091 case ISD::CTTZ:
1092 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1093 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1094 DAG.getConstant(getSizeInBits(NVT), NVT));
1095 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1096 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1097 break;
1098 case ISD::CTLZ:
1099 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1100 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1101 DAG.getConstant(getSizeInBits(NVT) -
1102 getSizeInBits(OVT), NVT));
1103 break;
1104 }
1105 break;
1106 }
1107 case TargetLowering::Custom:
1108 assert(0 && "Cannot custom handle this yet!");
1109 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001110 switch(Node->getOpcode())
1111 {
1112 case ISD::CTPOP: {
1113 static const uint64_t mask[6][9] = {
1114 {0, 0x55, 0x5555, 0, 0x55555555, 0, 0, 0, 0x5555555555555555ULL},
1115 {0, 0x33, 0x3333, 0, 0x33333333, 0, 0, 0, 0x3333333333333333ULL},
1116 {0, 0x0F, 0x0F0F, 0, 0x0F0F0F0F, 0, 0, 0, 0x0F0F0F0F0F0F0F0FULL},
1117 {0, 0, 0x00FF, 0, 0x00FF00FF, 0, 0, 0, 0x00FF00FF00FF00FFULL},
1118 {0, 0, 0, 0, 0x0000FFFF, 0, 0, 0, 0x0000FFFF0000FFFFULL},
1119 {0, 0, 0, 0, 0, 0, 0, 0, 0x00000000FFFFFFFFULL}};
1120 MVT::ValueType VT = Tmp1.getValueType();
1121 int len = getSizeInBits(VT);
1122 for (int i = 0; (1 << i) <= (len / 2); ++i) {
1123 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
1124 Tmp2 = DAG.getConstant(mask[i][len/8], VT);
1125 Tmp3 = DAG.getConstant(1 << i, VT);
1126 Tmp1 = DAG.getNode(ISD::ADD, VT,
1127 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1128 DAG.getNode(ISD::AND, VT,
1129 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1130 Tmp2));
1131 }
1132 Result = Tmp1;
1133 break;
1134 }
1135// case ISD::CTTZ:
1136// break;
1137// case ISD::CTLZ:
1138// break;
1139 default:
1140 assert(0 && "Cannot expand this yet!");
1141 break;
1142 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001143 break;
1144 }
1145 break;
1146
Chris Lattner2c8086f2005-04-02 05:00:07 +00001147 // Unary operators
1148 case ISD::FABS:
1149 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001150 case ISD::FSQRT:
1151 case ISD::FSIN:
1152 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001153 Tmp1 = LegalizeOp(Node->getOperand(0));
1154 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1155 case TargetLowering::Legal:
1156 if (Tmp1 != Node->getOperand(0))
1157 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1158 break;
1159 case TargetLowering::Promote:
1160 case TargetLowering::Custom:
1161 assert(0 && "Cannot promote/custom handle this yet!");
1162 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001163 switch(Node->getOpcode()) {
1164 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001165 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1166 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1167 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1168 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001169 break;
1170 }
1171 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001172 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1173 MVT::ValueType VT = Node->getValueType(0);
1174 Tmp2 = DAG.getConstantFP(0.0, VT);
1175 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1176 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1177 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1178 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001179 break;
1180 }
1181 case ISD::FSQRT:
1182 case ISD::FSIN:
1183 case ISD::FCOS: {
1184 MVT::ValueType VT = Node->getValueType(0);
1185 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1186 const char *FnName = 0;
1187 switch(Node->getOpcode()) {
1188 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1189 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1190 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1191 default: assert(0 && "Unreachable!");
1192 }
1193 std::vector<std::pair<SDOperand, const Type*> > Args;
1194 Args.push_back(std::make_pair(Tmp1, T));
1195 std::pair<SDOperand,SDOperand> CallResult =
1196 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1197 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1198 Result = LegalizeOp(CallResult.first);
1199 break;
1200 }
1201 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001202 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001203 }
1204 break;
1205 }
1206 break;
1207
1208 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001209 case ISD::ZERO_EXTEND:
1210 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001211 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001212 case ISD::FP_EXTEND:
1213 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001214 case ISD::FP_TO_SINT:
1215 case ISD::FP_TO_UINT:
1216 case ISD::SINT_TO_FP:
1217 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1219 case Legal:
1220 Tmp1 = LegalizeOp(Node->getOperand(0));
1221 if (Tmp1 != Node->getOperand(0))
1222 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1223 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001224 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001225 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1226 Node->getOpcode() == ISD::UINT_TO_FP) {
1227 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1228 Node->getValueType(0), Node->getOperand(0));
1229 Result = LegalizeOp(Result);
1230 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001231 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1232 // In the expand case, we must be dealing with a truncate, because
1233 // otherwise the result would be larger than the source.
1234 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001235
Chris Lattner2c8086f2005-04-02 05:00:07 +00001236 // Since the result is legal, we should just be able to truncate the low
1237 // part of the source.
1238 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1239 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001240 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001241 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001242
Chris Lattner03c85462005-01-15 05:21:40 +00001243 case Promote:
1244 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001245 case ISD::ZERO_EXTEND:
1246 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001247 // NOTE: Any extend would work here...
1248 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001249 Result = DAG.getZeroExtendInReg(Result,
1250 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001251 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001252 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001253 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001254 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001255 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001256 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1257 Result, Node->getOperand(0).getValueType());
1258 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001259 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001260 Result = PromoteOp(Node->getOperand(0));
1261 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1262 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001263 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001264 Result = PromoteOp(Node->getOperand(0));
1265 if (Result.getValueType() != Op.getValueType())
1266 // Dynamically dead while we have only 2 FP types.
1267 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1268 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001269 case ISD::FP_ROUND:
1270 case ISD::FP_TO_SINT:
1271 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001272 Result = PromoteOp(Node->getOperand(0));
1273 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1274 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001275 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001276 Result = PromoteOp(Node->getOperand(0));
1277 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1278 Result, Node->getOperand(0).getValueType());
1279 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1280 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001281 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001282 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001283 Result = DAG.getZeroExtendInReg(Result,
1284 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001285 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1286 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001287 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001288 }
1289 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001290 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001291 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001292 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001293 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1294
1295 // If this operation is not supported, convert it to a shl/shr or load/store
1296 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001297 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1298 default: assert(0 && "This action not supported for this op yet!");
1299 case TargetLowering::Legal:
1300 if (Tmp1 != Node->getOperand(0))
1301 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1302 ExtraVT);
1303 break;
1304 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001305 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001306 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001307 // NOTE: we could fall back on load/store here too for targets without
1308 // SAR. However, it is doubtful that any exist.
1309 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1310 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001311 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001312 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1313 Node->getOperand(0), ShiftCst);
1314 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1315 Result, ShiftCst);
1316 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1317 // The only way we can lower this is to turn it into a STORETRUNC,
1318 // EXTLOAD pair, targetting a temporary location (a stack slot).
1319
1320 // NOTE: there is a choice here between constantly creating new stack
1321 // slots and always reusing the same one. We currently always create
1322 // new ones, as reuse may inhibit scheduling.
1323 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1324 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1325 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1326 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001327 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001328 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1329 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1330 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001331 Node->getOperand(0), StackSlot,
1332 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001333 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001334 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001335 } else {
1336 assert(0 && "Unknown op");
1337 }
1338 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001339 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001340 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001341 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001342 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001343 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001344
Chris Lattner8afc48e2005-01-07 22:28:47 +00001345 if (!Op.Val->hasOneUse())
1346 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001347
1348 return Result;
1349}
1350
Chris Lattner8b6fa222005-01-15 22:16:26 +00001351/// PromoteOp - Given an operation that produces a value in an invalid type,
1352/// promote it to compute the value into a larger type. The produced value will
1353/// have the correct bits for the low portion of the register, but no guarantee
1354/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001355SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1356 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001357 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001358 assert(getTypeAction(VT) == Promote &&
1359 "Caller should expand or legalize operands that are not promotable!");
1360 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1361 "Cannot promote to smaller type!");
1362
1363 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1364 if (I != PromotedNodes.end()) return I->second;
1365
1366 SDOperand Tmp1, Tmp2, Tmp3;
1367
1368 SDOperand Result;
1369 SDNode *Node = Op.Val;
1370
Chris Lattner0f69b292005-01-15 06:18:18 +00001371 // Promotion needs an optimization step to clean up after it, and is not
1372 // careful to avoid operations the target does not support. Make sure that
1373 // all generated operations are legalized in the next iteration.
1374 NeedsAnotherIteration = true;
1375
Chris Lattner03c85462005-01-15 05:21:40 +00001376 switch (Node->getOpcode()) {
1377 default:
1378 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1379 assert(0 && "Do not know how to promote this operator!");
1380 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001381 case ISD::UNDEF:
1382 Result = DAG.getNode(ISD::UNDEF, NVT);
1383 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001384 case ISD::Constant:
1385 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1386 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1387 break;
1388 case ISD::ConstantFP:
1389 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1390 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1391 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001392 case ISD::CopyFromReg:
1393 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1394 Node->getOperand(0));
1395 // Remember that we legalized the chain.
1396 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1397 break;
1398
Chris Lattner82fbfb62005-01-18 02:59:52 +00001399 case ISD::SETCC:
1400 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1401 "SetCC type is not legal??");
1402 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1403 TLI.getSetCCResultTy(), Node->getOperand(0),
1404 Node->getOperand(1));
1405 Result = LegalizeOp(Result);
1406 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001407
1408 case ISD::TRUNCATE:
1409 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1410 case Legal:
1411 Result = LegalizeOp(Node->getOperand(0));
1412 assert(Result.getValueType() >= NVT &&
1413 "This truncation doesn't make sense!");
1414 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1415 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1416 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001417 case Promote:
1418 // The truncation is not required, because we don't guarantee anything
1419 // about high bits anyway.
1420 Result = PromoteOp(Node->getOperand(0));
1421 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001422 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001423 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1424 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001425 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001426 }
1427 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001428 case ISD::SIGN_EXTEND:
1429 case ISD::ZERO_EXTEND:
1430 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1431 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1432 case Legal:
1433 // Input is legal? Just do extend all the way to the larger type.
1434 Result = LegalizeOp(Node->getOperand(0));
1435 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1436 break;
1437 case Promote:
1438 // Promote the reg if it's smaller.
1439 Result = PromoteOp(Node->getOperand(0));
1440 // The high bits are not guaranteed to be anything. Insert an extend.
1441 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001442 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1443 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001444 else
Chris Lattner23993562005-04-13 02:38:47 +00001445 Result = DAG.getZeroExtendInReg(Result,
1446 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001447 break;
1448 }
1449 break;
1450
1451 case ISD::FP_EXTEND:
1452 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1453 case ISD::FP_ROUND:
1454 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1455 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1456 case Promote: assert(0 && "Unreachable with 2 FP types!");
1457 case Legal:
1458 // Input is legal? Do an FP_ROUND_INREG.
1459 Result = LegalizeOp(Node->getOperand(0));
1460 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1461 break;
1462 }
1463 break;
1464
1465 case ISD::SINT_TO_FP:
1466 case ISD::UINT_TO_FP:
1467 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1468 case Legal:
1469 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001470 // No extra round required here.
1471 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001472 break;
1473
1474 case Promote:
1475 Result = PromoteOp(Node->getOperand(0));
1476 if (Node->getOpcode() == ISD::SINT_TO_FP)
1477 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1478 Result, Node->getOperand(0).getValueType());
1479 else
Chris Lattner23993562005-04-13 02:38:47 +00001480 Result = DAG.getZeroExtendInReg(Result,
1481 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001482 // No extra round required here.
1483 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001484 break;
1485 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001486 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1487 Node->getOperand(0));
1488 Result = LegalizeOp(Result);
1489
1490 // Round if we cannot tolerate excess precision.
1491 if (NoExcessFPPrecision)
1492 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1493 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001494 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001495 break;
1496
1497 case ISD::FP_TO_SINT:
1498 case ISD::FP_TO_UINT:
1499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1500 case Legal:
1501 Tmp1 = LegalizeOp(Node->getOperand(0));
1502 break;
1503 case Promote:
1504 // The input result is prerounded, so we don't have to do anything
1505 // special.
1506 Tmp1 = PromoteOp(Node->getOperand(0));
1507 break;
1508 case Expand:
1509 assert(0 && "not implemented");
1510 }
1511 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1512 break;
1513
Chris Lattner2c8086f2005-04-02 05:00:07 +00001514 case ISD::FABS:
1515 case ISD::FNEG:
1516 Tmp1 = PromoteOp(Node->getOperand(0));
1517 assert(Tmp1.getValueType() == NVT);
1518 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1519 // NOTE: we do not have to do any extra rounding here for
1520 // NoExcessFPPrecision, because we know the input will have the appropriate
1521 // precision, and these operations don't modify precision at all.
1522 break;
1523
Chris Lattnerda6ba872005-04-28 21:44:33 +00001524 case ISD::FSQRT:
1525 case ISD::FSIN:
1526 case ISD::FCOS:
1527 Tmp1 = PromoteOp(Node->getOperand(0));
1528 assert(Tmp1.getValueType() == NVT);
1529 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1530 if(NoExcessFPPrecision)
1531 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1532 break;
1533
Chris Lattner03c85462005-01-15 05:21:40 +00001534 case ISD::AND:
1535 case ISD::OR:
1536 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001537 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001538 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001539 case ISD::MUL:
1540 // The input may have strange things in the top bits of the registers, but
1541 // these operations don't care. They may have wierd bits going out, but
1542 // that too is okay if they are integer operations.
1543 Tmp1 = PromoteOp(Node->getOperand(0));
1544 Tmp2 = PromoteOp(Node->getOperand(1));
1545 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1546 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1547
1548 // However, if this is a floating point operation, they will give excess
1549 // precision that we may not be able to tolerate. If we DO allow excess
1550 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001551 // FIXME: Why would we need to round FP ops more than integer ones?
1552 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001553 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1554 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1555 break;
1556
Chris Lattner8b6fa222005-01-15 22:16:26 +00001557 case ISD::SDIV:
1558 case ISD::SREM:
1559 // These operators require that their input be sign extended.
1560 Tmp1 = PromoteOp(Node->getOperand(0));
1561 Tmp2 = PromoteOp(Node->getOperand(1));
1562 if (MVT::isInteger(NVT)) {
1563 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001564 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001565 }
1566 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1567
1568 // Perform FP_ROUND: this is probably overly pessimistic.
1569 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1570 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1571 break;
1572
1573 case ISD::UDIV:
1574 case ISD::UREM:
1575 // These operators require that their input be zero extended.
1576 Tmp1 = PromoteOp(Node->getOperand(0));
1577 Tmp2 = PromoteOp(Node->getOperand(1));
1578 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001579 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1580 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001581 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1582 break;
1583
1584 case ISD::SHL:
1585 Tmp1 = PromoteOp(Node->getOperand(0));
1586 Tmp2 = LegalizeOp(Node->getOperand(1));
1587 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1588 break;
1589 case ISD::SRA:
1590 // The input value must be properly sign extended.
1591 Tmp1 = PromoteOp(Node->getOperand(0));
1592 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1593 Tmp2 = LegalizeOp(Node->getOperand(1));
1594 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1595 break;
1596 case ISD::SRL:
1597 // The input value must be properly zero extended.
1598 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001599 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001600 Tmp2 = LegalizeOp(Node->getOperand(1));
1601 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1602 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001603 case ISD::LOAD:
1604 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1605 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001606 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001607 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001608 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1609 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001610 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001611 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1612 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001613
1614 // Remember that we legalized the chain.
1615 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1616 break;
1617 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001618 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1619 case Expand: assert(0 && "It's impossible to expand bools");
1620 case Legal:
1621 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1622 break;
1623 case Promote:
1624 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1625 break;
1626 }
Chris Lattner03c85462005-01-15 05:21:40 +00001627 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1628 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1629 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1630 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001631 case ISD::CALL: {
1632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1633 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1634
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001635 std::vector<SDOperand> Ops;
1636 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1637 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1638
Chris Lattner8ac532c2005-01-16 19:46:48 +00001639 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1640 "Can only promote single result calls");
1641 std::vector<MVT::ValueType> RetTyVTs;
1642 RetTyVTs.reserve(2);
1643 RetTyVTs.push_back(NVT);
1644 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001645 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001646 Result = SDOperand(NC, 0);
1647
1648 // Insert the new chain mapping.
1649 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1650 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001651 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001652 case ISD::CTPOP:
1653 case ISD::CTTZ:
1654 case ISD::CTLZ:
1655 Tmp1 = Node->getOperand(0);
1656 //Zero extend the argument
1657 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1658 // Perform the larger operation, then subtract if needed.
1659 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1660 switch(Node->getOpcode())
1661 {
1662 case ISD::CTPOP:
1663 Result = Tmp1;
1664 break;
1665 case ISD::CTTZ:
1666 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1667 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1668 DAG.getConstant(getSizeInBits(NVT), NVT));
1669 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1670 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1671 break;
1672 case ISD::CTLZ:
1673 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1674 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1675 DAG.getConstant(getSizeInBits(NVT) -
1676 getSizeInBits(VT), NVT));
1677 break;
1678 }
1679 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001680 }
1681
1682 assert(Result.Val && "Didn't set a result!");
1683 AddPromotedOperand(Op, Result);
1684 return Result;
1685}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001686
Chris Lattner84f67882005-01-20 18:52:28 +00001687/// ExpandAddSub - Find a clever way to expand this add operation into
1688/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001689void SelectionDAGLegalize::
1690ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1691 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001692 // Expand the subcomponents.
1693 SDOperand LHSL, LHSH, RHSL, RHSH;
1694 ExpandOp(LHS, LHSL, LHSH);
1695 ExpandOp(RHS, RHSL, RHSH);
1696
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001697 // FIXME: this should be moved to the dag combiner someday.
1698 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1699 if (LHSL.getValueType() == MVT::i32) {
1700 SDOperand LowEl;
1701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1702 if (C->getValue() == 0)
1703 LowEl = RHSL;
1704 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1705 if (C->getValue() == 0)
1706 LowEl = LHSL;
1707 if (LowEl.Val) {
1708 // Turn this into an add/sub of the high part only.
1709 SDOperand HiEl =
1710 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1711 LowEl.getValueType(), LHSH, RHSH);
1712 Lo = LowEl;
1713 Hi = HiEl;
1714 return;
1715 }
1716 }
1717
Chris Lattner84f67882005-01-20 18:52:28 +00001718 std::vector<SDOperand> Ops;
1719 Ops.push_back(LHSL);
1720 Ops.push_back(LHSH);
1721 Ops.push_back(RHSL);
1722 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001723 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001724 Hi = Lo.getValue(1);
1725}
1726
Chris Lattner5b359c62005-04-02 04:00:59 +00001727void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1728 SDOperand Op, SDOperand Amt,
1729 SDOperand &Lo, SDOperand &Hi) {
1730 // Expand the subcomponents.
1731 SDOperand LHSL, LHSH;
1732 ExpandOp(Op, LHSL, LHSH);
1733
1734 std::vector<SDOperand> Ops;
1735 Ops.push_back(LHSL);
1736 Ops.push_back(LHSH);
1737 Ops.push_back(Amt);
1738 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1739 Hi = Lo.getValue(1);
1740}
1741
1742
Chris Lattnere34b3962005-01-19 04:19:40 +00001743/// ExpandShift - Try to find a clever way to expand this shift operation out to
1744/// smaller elements. If we can't find a way that is more efficient than a
1745/// libcall on this target, return false. Otherwise, return true with the
1746/// low-parts expanded into Lo and Hi.
1747bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1748 SDOperand &Lo, SDOperand &Hi) {
1749 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1750 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001751
Chris Lattnere34b3962005-01-19 04:19:40 +00001752 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001753 SDOperand ShAmt = LegalizeOp(Amt);
1754 MVT::ValueType ShTy = ShAmt.getValueType();
1755 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1756 unsigned NVTBits = MVT::getSizeInBits(NVT);
1757
1758 // Handle the case when Amt is an immediate. Other cases are currently broken
1759 // and are disabled.
1760 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1761 unsigned Cst = CN->getValue();
1762 // Expand the incoming operand to be shifted, so that we have its parts
1763 SDOperand InL, InH;
1764 ExpandOp(Op, InL, InH);
1765 switch(Opc) {
1766 case ISD::SHL:
1767 if (Cst > VTBits) {
1768 Lo = DAG.getConstant(0, NVT);
1769 Hi = DAG.getConstant(0, NVT);
1770 } else if (Cst > NVTBits) {
1771 Lo = DAG.getConstant(0, NVT);
1772 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001773 } else if (Cst == NVTBits) {
1774 Lo = DAG.getConstant(0, NVT);
1775 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001776 } else {
1777 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1778 Hi = DAG.getNode(ISD::OR, NVT,
1779 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1780 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1781 }
1782 return true;
1783 case ISD::SRL:
1784 if (Cst > VTBits) {
1785 Lo = DAG.getConstant(0, NVT);
1786 Hi = DAG.getConstant(0, NVT);
1787 } else if (Cst > NVTBits) {
1788 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1789 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001790 } else if (Cst == NVTBits) {
1791 Lo = InH;
1792 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001793 } else {
1794 Lo = DAG.getNode(ISD::OR, NVT,
1795 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1796 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1797 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1798 }
1799 return true;
1800 case ISD::SRA:
1801 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001802 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001803 DAG.getConstant(NVTBits-1, ShTy));
1804 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001805 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001806 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001807 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001808 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001809 } else if (Cst == NVTBits) {
1810 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001811 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001812 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001813 } else {
1814 Lo = DAG.getNode(ISD::OR, NVT,
1815 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1816 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1817 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1818 }
1819 return true;
1820 }
1821 }
1822 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1823 // so disable it for now. Currently targets are handling this via SHL_PARTS
1824 // and friends.
1825 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001826
1827 // If we have an efficient select operation (or if the selects will all fold
1828 // away), lower to some complex code, otherwise just emit the libcall.
1829 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1830 !isa<ConstantSDNode>(Amt))
1831 return false;
1832
1833 SDOperand InL, InH;
1834 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001835 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1836 DAG.getConstant(NVTBits, ShTy), ShAmt);
1837
Chris Lattnere5544f82005-01-20 20:29:23 +00001838 // Compare the unmasked shift amount against 32.
1839 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1840 DAG.getConstant(NVTBits, ShTy));
1841
Chris Lattnere34b3962005-01-19 04:19:40 +00001842 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1843 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1844 DAG.getConstant(NVTBits-1, ShTy));
1845 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1846 DAG.getConstant(NVTBits-1, ShTy));
1847 }
1848
1849 if (Opc == ISD::SHL) {
1850 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1851 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1852 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001853 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001854
Chris Lattnere34b3962005-01-19 04:19:40 +00001855 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1856 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1857 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001858 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1859 DAG.getSetCC(ISD::SETEQ,
1860 TLI.getSetCCResultTy(), NAmt,
1861 DAG.getConstant(32, ShTy)),
1862 DAG.getConstant(0, NVT),
1863 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001864 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001865 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001866 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001867 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001868
1869 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001870 if (Opc == ISD::SRA)
1871 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1872 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001873 else
1874 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001875 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001876 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001877 }
1878 return true;
1879}
Chris Lattner77e77a62005-01-21 06:05:23 +00001880
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001881/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1882/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1883/// Found.
1884static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1885 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1886
1887 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1888 // than the Found node. Just remember this node and return.
1889 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1890 Found = Node;
1891 return;
1892 }
1893
1894 // Otherwise, scan the operands of Node to see if any of them is a call.
1895 assert(Node->getNumOperands() != 0 &&
1896 "All leaves should have depth equal to the entry node!");
1897 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1898 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1899
1900 // Tail recurse for the last iteration.
1901 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1902 Found);
1903}
1904
1905
1906/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1907/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1908/// than Found.
1909static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1910 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1911
1912 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1913 // than the Found node. Just remember this node and return.
1914 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1915 Found = Node;
1916 return;
1917 }
1918
1919 // Otherwise, scan the operands of Node to see if any of them is a call.
1920 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1921 if (UI == E) return;
1922 for (--E; UI != E; ++UI)
1923 FindEarliestAdjCallStackUp(*UI, Found);
1924
1925 // Tail recurse for the last iteration.
1926 FindEarliestAdjCallStackUp(*UI, Found);
1927}
1928
1929/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1930/// find the ADJCALLSTACKUP node that terminates the call sequence.
1931static SDNode *FindAdjCallStackUp(SDNode *Node) {
1932 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1933 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00001934 if (Node->use_empty())
1935 return 0; // No adjcallstackup
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001936
1937 if (Node->hasOneUse()) // Simple case, only has one user to check.
1938 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001939
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001940 SDOperand TheChain(Node, Node->getNumValues()-1);
1941 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001942
1943 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001944 E = Node->use_end(); ; ++UI) {
1945 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001946
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001947 // Make sure to only follow users of our token chain.
1948 SDNode *User = *UI;
1949 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1950 if (User->getOperand(i) == TheChain)
1951 return FindAdjCallStackUp(User);
1952 }
1953 assert(0 && "Unreachable");
1954 abort();
1955}
1956
1957/// FindInputOutputChains - If we are replacing an operation with a call we need
1958/// to find the call that occurs before and the call that occurs after it to
1959/// properly serialize the calls in the block.
1960static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1961 SDOperand Entry) {
1962 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanc7c16572005-04-11 03:01:51 +00001963 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001964 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1965 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001966
Nate Begemanc7c16572005-04-11 03:01:51 +00001967 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1968 // previous call in the function. LatestCallStackDown may in that case be
1969 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1970 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1971 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1972 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1973 else
1974 LatestAdjCallStackUp = Entry.Val;
1975 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001976
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001977 SDNode *EarliestAdjCallStackUp = 0;
1978 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1979
1980 if (EarliestAdjCallStackUp) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001981 //std::cerr << "Found node: ";
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001982 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1983 }
1984
1985 return SDOperand(LatestAdjCallStackUp, 0);
1986}
1987
1988
1989
Chris Lattner77e77a62005-01-21 06:05:23 +00001990// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1991// does not fit into a register, return the lo part and set the hi part to the
1992// by-reg argument. If it does fit into a single register, return the result
1993// and leave the Hi part unset.
1994SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1995 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001996 SDNode *OutChain;
1997 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1998 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00001999 if (InChain.Val == 0)
2000 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002001
Chris Lattner77e77a62005-01-21 06:05:23 +00002002 TargetLowering::ArgListTy Args;
2003 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2004 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2005 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2006 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2007 }
2008 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002009
Chris Lattner77e77a62005-01-21 06:05:23 +00002010 // We don't care about token chains for libcalls. We just use the entry
2011 // node as our input and ignore the output chain. This allows us to place
2012 // calls wherever we need them to satisfy data dependences.
2013 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begeman8e21e712005-03-26 01:29:23 +00002014 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattner77e77a62005-01-21 06:05:23 +00002015 Args, DAG).first;
2016 switch (getTypeAction(Result.getValueType())) {
2017 default: assert(0 && "Unknown thing");
2018 case Legal:
2019 return Result;
2020 case Promote:
2021 assert(0 && "Cannot promote this yet!");
2022 case Expand:
2023 SDOperand Lo;
2024 ExpandOp(Result, Lo, Hi);
2025 return Lo;
2026 }
2027}
2028
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002029
Chris Lattner77e77a62005-01-21 06:05:23 +00002030/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2031/// destination type is legal.
2032SDOperand SelectionDAGLegalize::
2033ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2034 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2035 assert(getTypeAction(Source.getValueType()) == Expand &&
2036 "This is not an expansion!");
2037 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2038
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002039 SDNode *OutChain;
2040 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2041 DAG.getEntryNode());
2042
Chris Lattnerfed55772005-01-23 23:19:44 +00002043 const char *FnName = 0;
Chris Lattner77e77a62005-01-21 06:05:23 +00002044 if (isSigned) {
2045 if (DestTy == MVT::f32)
2046 FnName = "__floatdisf";
2047 else {
2048 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2049 FnName = "__floatdidf";
2050 }
2051 } else {
2052 // If this is unsigned, and not supported, first perform the conversion to
2053 // signed, then adjust the result if the sign bit is set.
Chris Lattnerffe284c2005-04-13 03:42:14 +00002054 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner77e77a62005-01-21 06:05:23 +00002055
Chris Lattnere9c35e72005-04-13 05:09:42 +00002056 assert(Source.getValueType() == MVT::i64 &&
2057 "This only works for 64-bit -> FP");
2058 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2059 // incoming integer is set. To handle this, we dynamically test to see if
2060 // it is set, and, if so, add a fudge factor.
2061 SDOperand Lo, Hi;
2062 ExpandOp(Source, Lo, Hi);
2063
2064 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2065 DAG.getConstant(0, Hi.getValueType()));
2066 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2067 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2068 SignSet, Four, Zero);
2069 // FIXME: This is almost certainly broken for big-endian systems. Should
2070 // this just put the fudge factor in the low bits of the uint64 constant or?
2071 static Constant *FudgeFactor =
2072 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
2073
2074 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2075 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2076 TLI.getPointerTy());
2077 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2078 SDOperand FudgeInReg;
2079 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002080 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2081 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002082 else {
2083 assert(DestTy == MVT::f64 && "Unexpected conversion");
2084 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002085 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002086 }
2087 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002088 }
2089 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2090
2091 TargetLowering::ArgListTy Args;
2092 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
2093 Args.push_back(std::make_pair(Source, ArgTy));
2094
2095 // We don't care about token chains for libcalls. We just use the entry
2096 // node as our input and ignore the output chain. This allows us to place
2097 // calls wherever we need them to satisfy data dependences.
2098 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begeman8e21e712005-03-26 01:29:23 +00002099 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002100}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002101
Chris Lattnere34b3962005-01-19 04:19:40 +00002102
2103
Chris Lattner3e928bb2005-01-07 07:47:09 +00002104/// ExpandOp - Expand the specified SDOperand into its two component pieces
2105/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2106/// LegalizeNodes map is filled in for any results that are not expanded, the
2107/// ExpandedNodes map is filled in for any results that are expanded, and the
2108/// Lo/Hi values are returned.
2109void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2110 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002111 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002112 SDNode *Node = Op.Val;
2113 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2114 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2115 assert(MVT::isInteger(NVT) && NVT < VT &&
2116 "Cannot expand to FP value or to larger int value!");
2117
2118 // If there is more than one use of this, see if we already expanded it.
2119 // There is no use remembering values that only have a single use, as the map
2120 // entries will never be reused.
2121 if (!Node->hasOneUse()) {
2122 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2123 = ExpandedNodes.find(Op);
2124 if (I != ExpandedNodes.end()) {
2125 Lo = I->second.first;
2126 Hi = I->second.second;
2127 return;
2128 }
2129 }
2130
Chris Lattner4e6c7462005-01-08 19:27:05 +00002131 // Expanding to multiple registers needs to perform an optimization step, and
2132 // is not careful to avoid operations the target does not support. Make sure
2133 // that all generated operations are legalized in the next iteration.
2134 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002135
Chris Lattner3e928bb2005-01-07 07:47:09 +00002136 switch (Node->getOpcode()) {
2137 default:
2138 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2139 assert(0 && "Do not know how to expand this operator!");
2140 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002141 case ISD::UNDEF:
2142 Lo = DAG.getNode(ISD::UNDEF, NVT);
2143 Hi = DAG.getNode(ISD::UNDEF, NVT);
2144 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002145 case ISD::Constant: {
2146 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2147 Lo = DAG.getConstant(Cst, NVT);
2148 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2149 break;
2150 }
2151
2152 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002153 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002154 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002155 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2156 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002157
Chris Lattner69a52152005-01-14 22:38:01 +00002158 // Remember that we legalized the chain.
2159 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2160
Chris Lattner3e928bb2005-01-07 07:47:09 +00002161 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2162 break;
2163 }
2164
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002165 case ISD::BUILD_PAIR:
2166 // Legalize both operands. FIXME: in the future we should handle the case
2167 // where the two elements are not legal.
2168 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2169 Lo = LegalizeOp(Node->getOperand(0));
2170 Hi = LegalizeOp(Node->getOperand(1));
2171 break;
2172
Chris Lattner3e928bb2005-01-07 07:47:09 +00002173 case ISD::LOAD: {
2174 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2175 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002176 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002177
2178 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002179 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002180 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2181 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002182 //Is this safe? declaring that the two parts of the split load
2183 //are from the same instruction?
2184 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002185
2186 // Build a factor node to remember that this load is independent of the
2187 // other one.
2188 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2189 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002190
Chris Lattner3e928bb2005-01-07 07:47:09 +00002191 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002192 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002193 if (!TLI.isLittleEndian())
2194 std::swap(Lo, Hi);
2195 break;
2196 }
2197 case ISD::CALL: {
2198 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2199 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2200
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002201 bool Changed = false;
2202 std::vector<SDOperand> Ops;
2203 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2204 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2205 Changed |= Ops.back() != Node->getOperand(i);
2206 }
2207
Chris Lattner3e928bb2005-01-07 07:47:09 +00002208 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2209 "Can only expand a call once so far, not i64 -> i16!");
2210
2211 std::vector<MVT::ValueType> RetTyVTs;
2212 RetTyVTs.reserve(3);
2213 RetTyVTs.push_back(NVT);
2214 RetTyVTs.push_back(NVT);
2215 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002216 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002217 Lo = SDOperand(NC, 0);
2218 Hi = SDOperand(NC, 1);
2219
2220 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002221 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002222 break;
2223 }
2224 case ISD::AND:
2225 case ISD::OR:
2226 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2227 SDOperand LL, LH, RL, RH;
2228 ExpandOp(Node->getOperand(0), LL, LH);
2229 ExpandOp(Node->getOperand(1), RL, RH);
2230 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2231 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2232 break;
2233 }
2234 case ISD::SELECT: {
2235 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002236
2237 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2238 case Expand: assert(0 && "It's impossible to expand bools");
2239 case Legal:
2240 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2241 break;
2242 case Promote:
2243 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2244 break;
2245 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002246 ExpandOp(Node->getOperand(1), LL, LH);
2247 ExpandOp(Node->getOperand(2), RL, RH);
2248 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2249 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2250 break;
2251 }
2252 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002253 SDOperand In;
2254 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2255 case Expand: assert(0 && "expand-expand not implemented yet!");
2256 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2257 case Promote:
2258 In = PromoteOp(Node->getOperand(0));
2259 // Emit the appropriate sign_extend_inreg to get the value we want.
2260 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2261 Node->getOperand(0).getValueType());
2262 break;
2263 }
2264
Chris Lattner3e928bb2005-01-07 07:47:09 +00002265 // The low part is just a sign extension of the input (which degenerates to
2266 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002267 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002268
Chris Lattner3e928bb2005-01-07 07:47:09 +00002269 // The high part is obtained by SRA'ing all but one of the bits of the lo
2270 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002271 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002272 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2273 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002274 break;
2275 }
Chris Lattner06098e02005-04-03 23:41:52 +00002276 case ISD::ZERO_EXTEND: {
2277 SDOperand In;
2278 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2279 case Expand: assert(0 && "expand-expand not implemented yet!");
2280 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2281 case Promote:
2282 In = PromoteOp(Node->getOperand(0));
2283 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002284 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002285 break;
2286 }
2287
Chris Lattner3e928bb2005-01-07 07:47:09 +00002288 // The low part is just a zero extension of the input (which degenerates to
2289 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002290 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002291
Chris Lattner3e928bb2005-01-07 07:47:09 +00002292 // The high part is just a zero.
2293 Hi = DAG.getConstant(0, NVT);
2294 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002295 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002296 // These operators cannot be expanded directly, emit them as calls to
2297 // library functions.
2298 case ISD::FP_TO_SINT:
2299 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002300 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002301 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002302 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002303 break;
2304 case ISD::FP_TO_UINT:
2305 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002306 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002307 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002308 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002309 break;
2310
Chris Lattnere34b3962005-01-19 04:19:40 +00002311 case ISD::SHL:
2312 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002313 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002314 break;
Chris Lattner47599822005-04-02 03:38:53 +00002315
2316 // If this target supports SHL_PARTS, use it.
2317 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002318 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2319 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002320 break;
2321 }
2322
Chris Lattnere34b3962005-01-19 04:19:40 +00002323 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002324 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002325 break;
2326
2327 case ISD::SRA:
2328 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002329 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002330 break;
Chris Lattner47599822005-04-02 03:38:53 +00002331
2332 // If this target supports SRA_PARTS, use it.
2333 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002334 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2335 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002336 break;
2337 }
2338
Chris Lattnere34b3962005-01-19 04:19:40 +00002339 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002340 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002341 break;
2342 case ISD::SRL:
2343 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002344 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002345 break;
Chris Lattner47599822005-04-02 03:38:53 +00002346
2347 // If this target supports SRL_PARTS, use it.
2348 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002349 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2350 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002351 break;
2352 }
2353
Chris Lattnere34b3962005-01-19 04:19:40 +00002354 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002355 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002356 break;
2357
Misha Brukmanedf128a2005-04-21 22:36:52 +00002358 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002359 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2360 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002361 break;
2362 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002363 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2364 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002365 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002366 case ISD::MUL: {
2367 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2368 SDOperand LL, LH, RL, RH;
2369 ExpandOp(Node->getOperand(0), LL, LH);
2370 ExpandOp(Node->getOperand(1), RL, RH);
2371 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2372 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2373 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2374 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2375 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2376 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2377 } else {
2378 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2379 }
2380 break;
2381 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002382 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2383 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2384 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2385 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002386 }
2387
2388 // Remember in a map if the values will be reused later.
2389 if (!Node->hasOneUse()) {
2390 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2391 std::make_pair(Lo, Hi))).second;
2392 assert(isNew && "Value already expanded?!?");
2393 }
2394}
2395
2396
2397// SelectionDAG::Legalize - This is the entry point for the file.
2398//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002399void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002400 /// run - This is the main entry point to this class.
2401 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002402 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002403}
2404