blob: b9245a5d2ae6d3b465147fd71736102c68ed343e [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 Lattner52d08bd2005-05-09 20:23:03 +0000598 DAG.getConstant(V.I, MVT::i32), Tmp2,
599 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:
930 case ISD::READIO:
931 Tmp1 = LegalizeOp(Node->getOperand(0));
932 Tmp2 = LegalizeOp(Node->getOperand(1));
933 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
934 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),Tmp1, Tmp2);
935 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);
942
943 case ISD::WRITEPORT:
944 case ISD::WRITEIO:
945 Tmp1 = LegalizeOp(Node->getOperand(0));
946 Tmp2 = LegalizeOp(Node->getOperand(1));
947 Tmp3 = LegalizeOp(Node->getOperand(2));
948 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
949 Tmp3 != Node->getOperand(2))
950 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
951 break;
952
Chris Lattner84f67882005-01-20 18:52:28 +0000953 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +0000954 case ISD::SUB_PARTS:
955 case ISD::SHL_PARTS:
956 case ISD::SRA_PARTS:
957 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +0000958 std::vector<SDOperand> Ops;
959 bool Changed = false;
960 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
961 Ops.push_back(LegalizeOp(Node->getOperand(i)));
962 Changed |= Ops.back() != Node->getOperand(i);
963 }
964 if (Changed)
965 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +0000966
967 // Since these produce multiple values, make sure to remember that we
968 // legalized all of them.
969 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
970 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
971 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +0000972 }
Chris Lattner2c8086f2005-04-02 05:00:07 +0000973
974 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +0000975 case ISD::ADD:
976 case ISD::SUB:
977 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +0000978 case ISD::MULHS:
979 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000980 case ISD::UDIV:
981 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000982 case ISD::AND:
983 case ISD::OR:
984 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000985 case ISD::SHL:
986 case ISD::SRL:
987 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000988 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
989 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
990 if (Tmp1 != Node->getOperand(0) ||
991 Tmp2 != Node->getOperand(1))
992 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
993 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000994
Nate Begemanc105e192005-04-06 00:23:54 +0000995 case ISD::UREM:
996 case ISD::SREM:
997 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
998 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
999 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1000 case TargetLowering::Legal:
1001 if (Tmp1 != Node->getOperand(0) ||
1002 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001003 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001004 Tmp2);
1005 break;
1006 case TargetLowering::Promote:
1007 case TargetLowering::Custom:
1008 assert(0 && "Cannot promote/custom handle this yet!");
1009 case TargetLowering::Expand: {
1010 MVT::ValueType VT = Node->getValueType(0);
1011 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1012 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1013 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1014 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1015 }
1016 break;
1017 }
1018 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001019
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001020 case ISD::CTPOP:
1021 case ISD::CTTZ:
1022 case ISD::CTLZ:
1023 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1024 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1025 case TargetLowering::Legal:
1026 if (Tmp1 != Node->getOperand(0))
1027 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1028 break;
1029 case TargetLowering::Promote: {
1030 MVT::ValueType OVT = Tmp1.getValueType();
1031 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1032 //Zero extend the argument
1033 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1034 // Perform the larger operation, then subtract if needed.
1035 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1036 switch(Node->getOpcode())
1037 {
1038 case ISD::CTPOP:
1039 Result = Tmp1;
1040 break;
1041 case ISD::CTTZ:
1042 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1043 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1044 DAG.getConstant(getSizeInBits(NVT), NVT));
1045 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1046 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1047 break;
1048 case ISD::CTLZ:
1049 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1050 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1051 DAG.getConstant(getSizeInBits(NVT) -
1052 getSizeInBits(OVT), NVT));
1053 break;
1054 }
1055 break;
1056 }
1057 case TargetLowering::Custom:
1058 assert(0 && "Cannot custom handle this yet!");
1059 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001060 switch(Node->getOpcode())
1061 {
1062 case ISD::CTPOP: {
1063 static const uint64_t mask[6][9] = {
1064 {0, 0x55, 0x5555, 0, 0x55555555, 0, 0, 0, 0x5555555555555555ULL},
1065 {0, 0x33, 0x3333, 0, 0x33333333, 0, 0, 0, 0x3333333333333333ULL},
1066 {0, 0x0F, 0x0F0F, 0, 0x0F0F0F0F, 0, 0, 0, 0x0F0F0F0F0F0F0F0FULL},
1067 {0, 0, 0x00FF, 0, 0x00FF00FF, 0, 0, 0, 0x00FF00FF00FF00FFULL},
1068 {0, 0, 0, 0, 0x0000FFFF, 0, 0, 0, 0x0000FFFF0000FFFFULL},
1069 {0, 0, 0, 0, 0, 0, 0, 0, 0x00000000FFFFFFFFULL}};
1070 MVT::ValueType VT = Tmp1.getValueType();
1071 int len = getSizeInBits(VT);
1072 for (int i = 0; (1 << i) <= (len / 2); ++i) {
1073 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
1074 Tmp2 = DAG.getConstant(mask[i][len/8], VT);
1075 Tmp3 = DAG.getConstant(1 << i, VT);
1076 Tmp1 = DAG.getNode(ISD::ADD, VT,
1077 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1078 DAG.getNode(ISD::AND, VT,
1079 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1080 Tmp2));
1081 }
1082 Result = Tmp1;
1083 break;
1084 }
1085// case ISD::CTTZ:
1086// break;
1087// case ISD::CTLZ:
1088// break;
1089 default:
1090 assert(0 && "Cannot expand this yet!");
1091 break;
1092 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001093 break;
1094 }
1095 break;
1096
Chris Lattner2c8086f2005-04-02 05:00:07 +00001097 // Unary operators
1098 case ISD::FABS:
1099 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001100 case ISD::FSQRT:
1101 case ISD::FSIN:
1102 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001103 Tmp1 = LegalizeOp(Node->getOperand(0));
1104 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1105 case TargetLowering::Legal:
1106 if (Tmp1 != Node->getOperand(0))
1107 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1108 break;
1109 case TargetLowering::Promote:
1110 case TargetLowering::Custom:
1111 assert(0 && "Cannot promote/custom handle this yet!");
1112 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001113 switch(Node->getOpcode()) {
1114 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001115 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1116 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1117 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1118 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001119 break;
1120 }
1121 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001122 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1123 MVT::ValueType VT = Node->getValueType(0);
1124 Tmp2 = DAG.getConstantFP(0.0, VT);
1125 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1126 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1127 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1128 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001129 break;
1130 }
1131 case ISD::FSQRT:
1132 case ISD::FSIN:
1133 case ISD::FCOS: {
1134 MVT::ValueType VT = Node->getValueType(0);
1135 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1136 const char *FnName = 0;
1137 switch(Node->getOpcode()) {
1138 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1139 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1140 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1141 default: assert(0 && "Unreachable!");
1142 }
1143 std::vector<std::pair<SDOperand, const Type*> > Args;
1144 Args.push_back(std::make_pair(Tmp1, T));
1145 std::pair<SDOperand,SDOperand> CallResult =
1146 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1147 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1148 Result = LegalizeOp(CallResult.first);
1149 break;
1150 }
1151 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001152 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001153 }
1154 break;
1155 }
1156 break;
1157
1158 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001159 case ISD::ZERO_EXTEND:
1160 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001161 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001162 case ISD::FP_EXTEND:
1163 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001164 case ISD::FP_TO_SINT:
1165 case ISD::FP_TO_UINT:
1166 case ISD::SINT_TO_FP:
1167 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001168 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1169 case Legal:
1170 Tmp1 = LegalizeOp(Node->getOperand(0));
1171 if (Tmp1 != Node->getOperand(0))
1172 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1173 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001174 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001175 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1176 Node->getOpcode() == ISD::UINT_TO_FP) {
1177 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1178 Node->getValueType(0), Node->getOperand(0));
1179 Result = LegalizeOp(Result);
1180 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001181 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1182 // In the expand case, we must be dealing with a truncate, because
1183 // otherwise the result would be larger than the source.
1184 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001185
Chris Lattner2c8086f2005-04-02 05:00:07 +00001186 // Since the result is legal, we should just be able to truncate the low
1187 // part of the source.
1188 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1189 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001190 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001191 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001192
Chris Lattner03c85462005-01-15 05:21:40 +00001193 case Promote:
1194 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001195 case ISD::ZERO_EXTEND:
1196 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001197 // NOTE: Any extend would work here...
1198 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001199 Result = DAG.getZeroExtendInReg(Result,
1200 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001201 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001202 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001203 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001204 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001205 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001206 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1207 Result, Node->getOperand(0).getValueType());
1208 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001209 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001210 Result = PromoteOp(Node->getOperand(0));
1211 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1212 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001213 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001214 Result = PromoteOp(Node->getOperand(0));
1215 if (Result.getValueType() != Op.getValueType())
1216 // Dynamically dead while we have only 2 FP types.
1217 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1218 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001219 case ISD::FP_ROUND:
1220 case ISD::FP_TO_SINT:
1221 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001222 Result = PromoteOp(Node->getOperand(0));
1223 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1224 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001225 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001226 Result = PromoteOp(Node->getOperand(0));
1227 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1228 Result, Node->getOperand(0).getValueType());
1229 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1230 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001231 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001232 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001233 Result = DAG.getZeroExtendInReg(Result,
1234 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001235 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1236 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001237 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001238 }
1239 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001240 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001241 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001242 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001243 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1244
1245 // If this operation is not supported, convert it to a shl/shr or load/store
1246 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001247 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1248 default: assert(0 && "This action not supported for this op yet!");
1249 case TargetLowering::Legal:
1250 if (Tmp1 != Node->getOperand(0))
1251 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1252 ExtraVT);
1253 break;
1254 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001255 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001256 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001257 // NOTE: we could fall back on load/store here too for targets without
1258 // SAR. However, it is doubtful that any exist.
1259 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1260 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001261 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001262 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1263 Node->getOperand(0), ShiftCst);
1264 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1265 Result, ShiftCst);
1266 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1267 // The only way we can lower this is to turn it into a STORETRUNC,
1268 // EXTLOAD pair, targetting a temporary location (a stack slot).
1269
1270 // NOTE: there is a choice here between constantly creating new stack
1271 // slots and always reusing the same one. We currently always create
1272 // new ones, as reuse may inhibit scheduling.
1273 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1274 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1275 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1276 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001277 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001278 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1279 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1280 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001281 Node->getOperand(0), StackSlot,
1282 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001283 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001284 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001285 } else {
1286 assert(0 && "Unknown op");
1287 }
1288 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001289 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001290 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001291 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001292 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001293 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001294
Chris Lattner8afc48e2005-01-07 22:28:47 +00001295 if (!Op.Val->hasOneUse())
1296 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001297
1298 return Result;
1299}
1300
Chris Lattner8b6fa222005-01-15 22:16:26 +00001301/// PromoteOp - Given an operation that produces a value in an invalid type,
1302/// promote it to compute the value into a larger type. The produced value will
1303/// have the correct bits for the low portion of the register, but no guarantee
1304/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001305SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1306 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001307 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001308 assert(getTypeAction(VT) == Promote &&
1309 "Caller should expand or legalize operands that are not promotable!");
1310 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1311 "Cannot promote to smaller type!");
1312
1313 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1314 if (I != PromotedNodes.end()) return I->second;
1315
1316 SDOperand Tmp1, Tmp2, Tmp3;
1317
1318 SDOperand Result;
1319 SDNode *Node = Op.Val;
1320
Chris Lattner0f69b292005-01-15 06:18:18 +00001321 // Promotion needs an optimization step to clean up after it, and is not
1322 // careful to avoid operations the target does not support. Make sure that
1323 // all generated operations are legalized in the next iteration.
1324 NeedsAnotherIteration = true;
1325
Chris Lattner03c85462005-01-15 05:21:40 +00001326 switch (Node->getOpcode()) {
1327 default:
1328 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1329 assert(0 && "Do not know how to promote this operator!");
1330 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001331 case ISD::UNDEF:
1332 Result = DAG.getNode(ISD::UNDEF, NVT);
1333 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001334 case ISD::Constant:
1335 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1336 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1337 break;
1338 case ISD::ConstantFP:
1339 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1340 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1341 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001342 case ISD::CopyFromReg:
1343 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1344 Node->getOperand(0));
1345 // Remember that we legalized the chain.
1346 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1347 break;
1348
Chris Lattner82fbfb62005-01-18 02:59:52 +00001349 case ISD::SETCC:
1350 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1351 "SetCC type is not legal??");
1352 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1353 TLI.getSetCCResultTy(), Node->getOperand(0),
1354 Node->getOperand(1));
1355 Result = LegalizeOp(Result);
1356 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001357
1358 case ISD::TRUNCATE:
1359 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1360 case Legal:
1361 Result = LegalizeOp(Node->getOperand(0));
1362 assert(Result.getValueType() >= NVT &&
1363 "This truncation doesn't make sense!");
1364 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1365 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1366 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001367 case Promote:
1368 // The truncation is not required, because we don't guarantee anything
1369 // about high bits anyway.
1370 Result = PromoteOp(Node->getOperand(0));
1371 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001372 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001373 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1374 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001375 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001376 }
1377 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001378 case ISD::SIGN_EXTEND:
1379 case ISD::ZERO_EXTEND:
1380 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1381 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1382 case Legal:
1383 // Input is legal? Just do extend all the way to the larger type.
1384 Result = LegalizeOp(Node->getOperand(0));
1385 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1386 break;
1387 case Promote:
1388 // Promote the reg if it's smaller.
1389 Result = PromoteOp(Node->getOperand(0));
1390 // The high bits are not guaranteed to be anything. Insert an extend.
1391 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001392 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1393 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001394 else
Chris Lattner23993562005-04-13 02:38:47 +00001395 Result = DAG.getZeroExtendInReg(Result,
1396 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001397 break;
1398 }
1399 break;
1400
1401 case ISD::FP_EXTEND:
1402 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1403 case ISD::FP_ROUND:
1404 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1405 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1406 case Promote: assert(0 && "Unreachable with 2 FP types!");
1407 case Legal:
1408 // Input is legal? Do an FP_ROUND_INREG.
1409 Result = LegalizeOp(Node->getOperand(0));
1410 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1411 break;
1412 }
1413 break;
1414
1415 case ISD::SINT_TO_FP:
1416 case ISD::UINT_TO_FP:
1417 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1418 case Legal:
1419 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001420 // No extra round required here.
1421 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001422 break;
1423
1424 case Promote:
1425 Result = PromoteOp(Node->getOperand(0));
1426 if (Node->getOpcode() == ISD::SINT_TO_FP)
1427 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1428 Result, Node->getOperand(0).getValueType());
1429 else
Chris Lattner23993562005-04-13 02:38:47 +00001430 Result = DAG.getZeroExtendInReg(Result,
1431 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001432 // No extra round required here.
1433 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001434 break;
1435 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001436 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1437 Node->getOperand(0));
1438 Result = LegalizeOp(Result);
1439
1440 // Round if we cannot tolerate excess precision.
1441 if (NoExcessFPPrecision)
1442 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1443 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001444 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001445 break;
1446
1447 case ISD::FP_TO_SINT:
1448 case ISD::FP_TO_UINT:
1449 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1450 case Legal:
1451 Tmp1 = LegalizeOp(Node->getOperand(0));
1452 break;
1453 case Promote:
1454 // The input result is prerounded, so we don't have to do anything
1455 // special.
1456 Tmp1 = PromoteOp(Node->getOperand(0));
1457 break;
1458 case Expand:
1459 assert(0 && "not implemented");
1460 }
1461 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1462 break;
1463
Chris Lattner2c8086f2005-04-02 05:00:07 +00001464 case ISD::FABS:
1465 case ISD::FNEG:
1466 Tmp1 = PromoteOp(Node->getOperand(0));
1467 assert(Tmp1.getValueType() == NVT);
1468 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1469 // NOTE: we do not have to do any extra rounding here for
1470 // NoExcessFPPrecision, because we know the input will have the appropriate
1471 // precision, and these operations don't modify precision at all.
1472 break;
1473
Chris Lattnerda6ba872005-04-28 21:44:33 +00001474 case ISD::FSQRT:
1475 case ISD::FSIN:
1476 case ISD::FCOS:
1477 Tmp1 = PromoteOp(Node->getOperand(0));
1478 assert(Tmp1.getValueType() == NVT);
1479 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1480 if(NoExcessFPPrecision)
1481 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1482 break;
1483
Chris Lattner03c85462005-01-15 05:21:40 +00001484 case ISD::AND:
1485 case ISD::OR:
1486 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001487 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001488 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001489 case ISD::MUL:
1490 // The input may have strange things in the top bits of the registers, but
1491 // these operations don't care. They may have wierd bits going out, but
1492 // that too is okay if they are integer operations.
1493 Tmp1 = PromoteOp(Node->getOperand(0));
1494 Tmp2 = PromoteOp(Node->getOperand(1));
1495 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1496 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1497
1498 // However, if this is a floating point operation, they will give excess
1499 // precision that we may not be able to tolerate. If we DO allow excess
1500 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001501 // FIXME: Why would we need to round FP ops more than integer ones?
1502 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001503 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1504 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1505 break;
1506
Chris Lattner8b6fa222005-01-15 22:16:26 +00001507 case ISD::SDIV:
1508 case ISD::SREM:
1509 // These operators require that their input be sign extended.
1510 Tmp1 = PromoteOp(Node->getOperand(0));
1511 Tmp2 = PromoteOp(Node->getOperand(1));
1512 if (MVT::isInteger(NVT)) {
1513 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001514 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001515 }
1516 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1517
1518 // Perform FP_ROUND: this is probably overly pessimistic.
1519 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1520 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1521 break;
1522
1523 case ISD::UDIV:
1524 case ISD::UREM:
1525 // These operators require that their input be zero extended.
1526 Tmp1 = PromoteOp(Node->getOperand(0));
1527 Tmp2 = PromoteOp(Node->getOperand(1));
1528 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001529 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1530 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001531 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1532 break;
1533
1534 case ISD::SHL:
1535 Tmp1 = PromoteOp(Node->getOperand(0));
1536 Tmp2 = LegalizeOp(Node->getOperand(1));
1537 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1538 break;
1539 case ISD::SRA:
1540 // The input value must be properly sign extended.
1541 Tmp1 = PromoteOp(Node->getOperand(0));
1542 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1543 Tmp2 = LegalizeOp(Node->getOperand(1));
1544 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1545 break;
1546 case ISD::SRL:
1547 // The input value must be properly zero extended.
1548 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001549 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001550 Tmp2 = LegalizeOp(Node->getOperand(1));
1551 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1552 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001553 case ISD::LOAD:
1554 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1555 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001556 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001557 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001558 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1559 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001560 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001561 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1562 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001563
1564 // Remember that we legalized the chain.
1565 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1566 break;
1567 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001568 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1569 case Expand: assert(0 && "It's impossible to expand bools");
1570 case Legal:
1571 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1572 break;
1573 case Promote:
1574 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1575 break;
1576 }
Chris Lattner03c85462005-01-15 05:21:40 +00001577 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1578 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1579 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1580 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001581 case ISD::CALL: {
1582 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1583 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1584
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001585 std::vector<SDOperand> Ops;
1586 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1587 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1588
Chris Lattner8ac532c2005-01-16 19:46:48 +00001589 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1590 "Can only promote single result calls");
1591 std::vector<MVT::ValueType> RetTyVTs;
1592 RetTyVTs.reserve(2);
1593 RetTyVTs.push_back(NVT);
1594 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001595 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001596 Result = SDOperand(NC, 0);
1597
1598 // Insert the new chain mapping.
1599 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1600 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001601 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001602 case ISD::CTPOP:
1603 case ISD::CTTZ:
1604 case ISD::CTLZ:
1605 Tmp1 = Node->getOperand(0);
1606 //Zero extend the argument
1607 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1608 // Perform the larger operation, then subtract if needed.
1609 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1610 switch(Node->getOpcode())
1611 {
1612 case ISD::CTPOP:
1613 Result = Tmp1;
1614 break;
1615 case ISD::CTTZ:
1616 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1617 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1618 DAG.getConstant(getSizeInBits(NVT), NVT));
1619 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1620 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1621 break;
1622 case ISD::CTLZ:
1623 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1624 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1625 DAG.getConstant(getSizeInBits(NVT) -
1626 getSizeInBits(VT), NVT));
1627 break;
1628 }
1629 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001630 }
1631
1632 assert(Result.Val && "Didn't set a result!");
1633 AddPromotedOperand(Op, Result);
1634 return Result;
1635}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001636
Chris Lattner84f67882005-01-20 18:52:28 +00001637/// ExpandAddSub - Find a clever way to expand this add operation into
1638/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001639void SelectionDAGLegalize::
1640ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1641 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001642 // Expand the subcomponents.
1643 SDOperand LHSL, LHSH, RHSL, RHSH;
1644 ExpandOp(LHS, LHSL, LHSH);
1645 ExpandOp(RHS, RHSL, RHSH);
1646
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001647 // FIXME: this should be moved to the dag combiner someday.
1648 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1649 if (LHSL.getValueType() == MVT::i32) {
1650 SDOperand LowEl;
1651 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1652 if (C->getValue() == 0)
1653 LowEl = RHSL;
1654 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1655 if (C->getValue() == 0)
1656 LowEl = LHSL;
1657 if (LowEl.Val) {
1658 // Turn this into an add/sub of the high part only.
1659 SDOperand HiEl =
1660 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1661 LowEl.getValueType(), LHSH, RHSH);
1662 Lo = LowEl;
1663 Hi = HiEl;
1664 return;
1665 }
1666 }
1667
Chris Lattner84f67882005-01-20 18:52:28 +00001668 std::vector<SDOperand> Ops;
1669 Ops.push_back(LHSL);
1670 Ops.push_back(LHSH);
1671 Ops.push_back(RHSL);
1672 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001673 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001674 Hi = Lo.getValue(1);
1675}
1676
Chris Lattner5b359c62005-04-02 04:00:59 +00001677void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1678 SDOperand Op, SDOperand Amt,
1679 SDOperand &Lo, SDOperand &Hi) {
1680 // Expand the subcomponents.
1681 SDOperand LHSL, LHSH;
1682 ExpandOp(Op, LHSL, LHSH);
1683
1684 std::vector<SDOperand> Ops;
1685 Ops.push_back(LHSL);
1686 Ops.push_back(LHSH);
1687 Ops.push_back(Amt);
1688 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1689 Hi = Lo.getValue(1);
1690}
1691
1692
Chris Lattnere34b3962005-01-19 04:19:40 +00001693/// ExpandShift - Try to find a clever way to expand this shift operation out to
1694/// smaller elements. If we can't find a way that is more efficient than a
1695/// libcall on this target, return false. Otherwise, return true with the
1696/// low-parts expanded into Lo and Hi.
1697bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1698 SDOperand &Lo, SDOperand &Hi) {
1699 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1700 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001701
Chris Lattnere34b3962005-01-19 04:19:40 +00001702 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001703 SDOperand ShAmt = LegalizeOp(Amt);
1704 MVT::ValueType ShTy = ShAmt.getValueType();
1705 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1706 unsigned NVTBits = MVT::getSizeInBits(NVT);
1707
1708 // Handle the case when Amt is an immediate. Other cases are currently broken
1709 // and are disabled.
1710 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1711 unsigned Cst = CN->getValue();
1712 // Expand the incoming operand to be shifted, so that we have its parts
1713 SDOperand InL, InH;
1714 ExpandOp(Op, InL, InH);
1715 switch(Opc) {
1716 case ISD::SHL:
1717 if (Cst > VTBits) {
1718 Lo = DAG.getConstant(0, NVT);
1719 Hi = DAG.getConstant(0, NVT);
1720 } else if (Cst > NVTBits) {
1721 Lo = DAG.getConstant(0, NVT);
1722 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001723 } else if (Cst == NVTBits) {
1724 Lo = DAG.getConstant(0, NVT);
1725 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001726 } else {
1727 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1728 Hi = DAG.getNode(ISD::OR, NVT,
1729 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1730 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1731 }
1732 return true;
1733 case ISD::SRL:
1734 if (Cst > VTBits) {
1735 Lo = DAG.getConstant(0, NVT);
1736 Hi = DAG.getConstant(0, NVT);
1737 } else if (Cst > NVTBits) {
1738 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1739 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001740 } else if (Cst == NVTBits) {
1741 Lo = InH;
1742 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001743 } else {
1744 Lo = DAG.getNode(ISD::OR, NVT,
1745 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1746 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1747 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1748 }
1749 return true;
1750 case ISD::SRA:
1751 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001752 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001753 DAG.getConstant(NVTBits-1, ShTy));
1754 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001755 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001756 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001757 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001758 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001759 } else if (Cst == NVTBits) {
1760 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001761 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001762 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001763 } else {
1764 Lo = DAG.getNode(ISD::OR, NVT,
1765 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1766 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1767 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1768 }
1769 return true;
1770 }
1771 }
1772 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1773 // so disable it for now. Currently targets are handling this via SHL_PARTS
1774 // and friends.
1775 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001776
1777 // If we have an efficient select operation (or if the selects will all fold
1778 // away), lower to some complex code, otherwise just emit the libcall.
1779 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1780 !isa<ConstantSDNode>(Amt))
1781 return false;
1782
1783 SDOperand InL, InH;
1784 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001785 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1786 DAG.getConstant(NVTBits, ShTy), ShAmt);
1787
Chris Lattnere5544f82005-01-20 20:29:23 +00001788 // Compare the unmasked shift amount against 32.
1789 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1790 DAG.getConstant(NVTBits, ShTy));
1791
Chris Lattnere34b3962005-01-19 04:19:40 +00001792 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1793 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1794 DAG.getConstant(NVTBits-1, ShTy));
1795 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1796 DAG.getConstant(NVTBits-1, ShTy));
1797 }
1798
1799 if (Opc == ISD::SHL) {
1800 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1801 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1802 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001803 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001804
Chris Lattnere34b3962005-01-19 04:19:40 +00001805 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1806 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1807 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001808 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1809 DAG.getSetCC(ISD::SETEQ,
1810 TLI.getSetCCResultTy(), NAmt,
1811 DAG.getConstant(32, ShTy)),
1812 DAG.getConstant(0, NVT),
1813 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001814 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001815 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001816 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001817 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001818
1819 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001820 if (Opc == ISD::SRA)
1821 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1822 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001823 else
1824 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001825 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001826 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001827 }
1828 return true;
1829}
Chris Lattner77e77a62005-01-21 06:05:23 +00001830
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001831/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1832/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1833/// Found.
1834static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1835 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1836
1837 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1838 // than the Found node. Just remember this node and return.
1839 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1840 Found = Node;
1841 return;
1842 }
1843
1844 // Otherwise, scan the operands of Node to see if any of them is a call.
1845 assert(Node->getNumOperands() != 0 &&
1846 "All leaves should have depth equal to the entry node!");
1847 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1848 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1849
1850 // Tail recurse for the last iteration.
1851 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1852 Found);
1853}
1854
1855
1856/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1857/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1858/// than Found.
1859static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1860 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1861
1862 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1863 // than the Found node. Just remember this node and return.
1864 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1865 Found = Node;
1866 return;
1867 }
1868
1869 // Otherwise, scan the operands of Node to see if any of them is a call.
1870 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1871 if (UI == E) return;
1872 for (--E; UI != E; ++UI)
1873 FindEarliestAdjCallStackUp(*UI, Found);
1874
1875 // Tail recurse for the last iteration.
1876 FindEarliestAdjCallStackUp(*UI, Found);
1877}
1878
1879/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1880/// find the ADJCALLSTACKUP node that terminates the call sequence.
1881static SDNode *FindAdjCallStackUp(SDNode *Node) {
1882 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1883 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00001884 if (Node->use_empty())
1885 return 0; // No adjcallstackup
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001886
1887 if (Node->hasOneUse()) // Simple case, only has one user to check.
1888 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001889
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001890 SDOperand TheChain(Node, Node->getNumValues()-1);
1891 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001892
1893 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001894 E = Node->use_end(); ; ++UI) {
1895 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001896
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001897 // Make sure to only follow users of our token chain.
1898 SDNode *User = *UI;
1899 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1900 if (User->getOperand(i) == TheChain)
1901 return FindAdjCallStackUp(User);
1902 }
1903 assert(0 && "Unreachable");
1904 abort();
1905}
1906
1907/// FindInputOutputChains - If we are replacing an operation with a call we need
1908/// to find the call that occurs before and the call that occurs after it to
1909/// properly serialize the calls in the block.
1910static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1911 SDOperand Entry) {
1912 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanc7c16572005-04-11 03:01:51 +00001913 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001914 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1915 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001916
Nate Begemanc7c16572005-04-11 03:01:51 +00001917 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1918 // previous call in the function. LatestCallStackDown may in that case be
1919 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1920 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1921 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1922 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1923 else
1924 LatestAdjCallStackUp = Entry.Val;
1925 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001926
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001927 SDNode *EarliestAdjCallStackUp = 0;
1928 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1929
1930 if (EarliestAdjCallStackUp) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001931 //std::cerr << "Found node: ";
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001932 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1933 }
1934
1935 return SDOperand(LatestAdjCallStackUp, 0);
1936}
1937
1938
1939
Chris Lattner77e77a62005-01-21 06:05:23 +00001940// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1941// does not fit into a register, return the lo part and set the hi part to the
1942// by-reg argument. If it does fit into a single register, return the result
1943// and leave the Hi part unset.
1944SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1945 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001946 SDNode *OutChain;
1947 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1948 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00001949 if (InChain.Val == 0)
1950 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001951
Chris Lattner77e77a62005-01-21 06:05:23 +00001952 TargetLowering::ArgListTy Args;
1953 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1954 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1955 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1956 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1957 }
1958 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001959
Chris Lattner77e77a62005-01-21 06:05:23 +00001960 // We don't care about token chains for libcalls. We just use the entry
1961 // node as our input and ignore the output chain. This allows us to place
1962 // calls wherever we need them to satisfy data dependences.
1963 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begeman8e21e712005-03-26 01:29:23 +00001964 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattner77e77a62005-01-21 06:05:23 +00001965 Args, DAG).first;
1966 switch (getTypeAction(Result.getValueType())) {
1967 default: assert(0 && "Unknown thing");
1968 case Legal:
1969 return Result;
1970 case Promote:
1971 assert(0 && "Cannot promote this yet!");
1972 case Expand:
1973 SDOperand Lo;
1974 ExpandOp(Result, Lo, Hi);
1975 return Lo;
1976 }
1977}
1978
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001979
Chris Lattner77e77a62005-01-21 06:05:23 +00001980/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1981/// destination type is legal.
1982SDOperand SelectionDAGLegalize::
1983ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1984 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1985 assert(getTypeAction(Source.getValueType()) == Expand &&
1986 "This is not an expansion!");
1987 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1988
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001989 SDNode *OutChain;
1990 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1991 DAG.getEntryNode());
1992
Chris Lattnerfed55772005-01-23 23:19:44 +00001993 const char *FnName = 0;
Chris Lattner77e77a62005-01-21 06:05:23 +00001994 if (isSigned) {
1995 if (DestTy == MVT::f32)
1996 FnName = "__floatdisf";
1997 else {
1998 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1999 FnName = "__floatdidf";
2000 }
2001 } else {
2002 // If this is unsigned, and not supported, first perform the conversion to
2003 // signed, then adjust the result if the sign bit is set.
Chris Lattnerffe284c2005-04-13 03:42:14 +00002004 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner77e77a62005-01-21 06:05:23 +00002005
Chris Lattnere9c35e72005-04-13 05:09:42 +00002006 assert(Source.getValueType() == MVT::i64 &&
2007 "This only works for 64-bit -> FP");
2008 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2009 // incoming integer is set. To handle this, we dynamically test to see if
2010 // it is set, and, if so, add a fudge factor.
2011 SDOperand Lo, Hi;
2012 ExpandOp(Source, Lo, Hi);
2013
2014 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2015 DAG.getConstant(0, Hi.getValueType()));
2016 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2017 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2018 SignSet, Four, Zero);
2019 // FIXME: This is almost certainly broken for big-endian systems. Should
2020 // this just put the fudge factor in the low bits of the uint64 constant or?
2021 static Constant *FudgeFactor =
2022 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
2023
2024 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2025 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2026 TLI.getPointerTy());
2027 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2028 SDOperand FudgeInReg;
2029 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002030 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2031 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002032 else {
2033 assert(DestTy == MVT::f64 && "Unexpected conversion");
2034 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002035 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002036 }
2037 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002038 }
2039 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2040
2041 TargetLowering::ArgListTy Args;
2042 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
2043 Args.push_back(std::make_pair(Source, ArgTy));
2044
2045 // We don't care about token chains for libcalls. We just use the entry
2046 // node as our input and ignore the output chain. This allows us to place
2047 // calls wherever we need them to satisfy data dependences.
2048 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begeman8e21e712005-03-26 01:29:23 +00002049 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002050}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002051
Chris Lattnere34b3962005-01-19 04:19:40 +00002052
2053
Chris Lattner3e928bb2005-01-07 07:47:09 +00002054/// ExpandOp - Expand the specified SDOperand into its two component pieces
2055/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2056/// LegalizeNodes map is filled in for any results that are not expanded, the
2057/// ExpandedNodes map is filled in for any results that are expanded, and the
2058/// Lo/Hi values are returned.
2059void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2060 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002061 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002062 SDNode *Node = Op.Val;
2063 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2064 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2065 assert(MVT::isInteger(NVT) && NVT < VT &&
2066 "Cannot expand to FP value or to larger int value!");
2067
2068 // If there is more than one use of this, see if we already expanded it.
2069 // There is no use remembering values that only have a single use, as the map
2070 // entries will never be reused.
2071 if (!Node->hasOneUse()) {
2072 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2073 = ExpandedNodes.find(Op);
2074 if (I != ExpandedNodes.end()) {
2075 Lo = I->second.first;
2076 Hi = I->second.second;
2077 return;
2078 }
2079 }
2080
Chris Lattner4e6c7462005-01-08 19:27:05 +00002081 // Expanding to multiple registers needs to perform an optimization step, and
2082 // is not careful to avoid operations the target does not support. Make sure
2083 // that all generated operations are legalized in the next iteration.
2084 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002085
Chris Lattner3e928bb2005-01-07 07:47:09 +00002086 switch (Node->getOpcode()) {
2087 default:
2088 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2089 assert(0 && "Do not know how to expand this operator!");
2090 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002091 case ISD::UNDEF:
2092 Lo = DAG.getNode(ISD::UNDEF, NVT);
2093 Hi = DAG.getNode(ISD::UNDEF, NVT);
2094 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002095 case ISD::Constant: {
2096 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2097 Lo = DAG.getConstant(Cst, NVT);
2098 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2099 break;
2100 }
2101
2102 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002103 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002104 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002105 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2106 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002107
Chris Lattner69a52152005-01-14 22:38:01 +00002108 // Remember that we legalized the chain.
2109 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2110
Chris Lattner3e928bb2005-01-07 07:47:09 +00002111 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2112 break;
2113 }
2114
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002115 case ISD::BUILD_PAIR:
2116 // Legalize both operands. FIXME: in the future we should handle the case
2117 // where the two elements are not legal.
2118 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2119 Lo = LegalizeOp(Node->getOperand(0));
2120 Hi = LegalizeOp(Node->getOperand(1));
2121 break;
2122
Chris Lattner3e928bb2005-01-07 07:47:09 +00002123 case ISD::LOAD: {
2124 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2125 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002126 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002127
2128 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002129 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002130 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2131 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002132 //Is this safe? declaring that the two parts of the split load
2133 //are from the same instruction?
2134 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002135
2136 // Build a factor node to remember that this load is independent of the
2137 // other one.
2138 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2139 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002140
Chris Lattner3e928bb2005-01-07 07:47:09 +00002141 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002142 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002143 if (!TLI.isLittleEndian())
2144 std::swap(Lo, Hi);
2145 break;
2146 }
2147 case ISD::CALL: {
2148 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2149 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2150
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002151 bool Changed = false;
2152 std::vector<SDOperand> Ops;
2153 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2154 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2155 Changed |= Ops.back() != Node->getOperand(i);
2156 }
2157
Chris Lattner3e928bb2005-01-07 07:47:09 +00002158 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2159 "Can only expand a call once so far, not i64 -> i16!");
2160
2161 std::vector<MVT::ValueType> RetTyVTs;
2162 RetTyVTs.reserve(3);
2163 RetTyVTs.push_back(NVT);
2164 RetTyVTs.push_back(NVT);
2165 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002166 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002167 Lo = SDOperand(NC, 0);
2168 Hi = SDOperand(NC, 1);
2169
2170 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002171 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002172 break;
2173 }
2174 case ISD::AND:
2175 case ISD::OR:
2176 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2177 SDOperand LL, LH, RL, RH;
2178 ExpandOp(Node->getOperand(0), LL, LH);
2179 ExpandOp(Node->getOperand(1), RL, RH);
2180 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2181 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2182 break;
2183 }
2184 case ISD::SELECT: {
2185 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002186
2187 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2188 case Expand: assert(0 && "It's impossible to expand bools");
2189 case Legal:
2190 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2191 break;
2192 case Promote:
2193 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2194 break;
2195 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002196 ExpandOp(Node->getOperand(1), LL, LH);
2197 ExpandOp(Node->getOperand(2), RL, RH);
2198 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2199 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2200 break;
2201 }
2202 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002203 SDOperand In;
2204 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2205 case Expand: assert(0 && "expand-expand not implemented yet!");
2206 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2207 case Promote:
2208 In = PromoteOp(Node->getOperand(0));
2209 // Emit the appropriate sign_extend_inreg to get the value we want.
2210 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2211 Node->getOperand(0).getValueType());
2212 break;
2213 }
2214
Chris Lattner3e928bb2005-01-07 07:47:09 +00002215 // The low part is just a sign extension of the input (which degenerates to
2216 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002217 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002218
Chris Lattner3e928bb2005-01-07 07:47:09 +00002219 // The high part is obtained by SRA'ing all but one of the bits of the lo
2220 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002221 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002222 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2223 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002224 break;
2225 }
Chris Lattner06098e02005-04-03 23:41:52 +00002226 case ISD::ZERO_EXTEND: {
2227 SDOperand In;
2228 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2229 case Expand: assert(0 && "expand-expand not implemented yet!");
2230 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2231 case Promote:
2232 In = PromoteOp(Node->getOperand(0));
2233 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002234 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002235 break;
2236 }
2237
Chris Lattner3e928bb2005-01-07 07:47:09 +00002238 // The low part is just a zero extension of the input (which degenerates to
2239 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002240 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002241
Chris Lattner3e928bb2005-01-07 07:47:09 +00002242 // The high part is just a zero.
2243 Hi = DAG.getConstant(0, NVT);
2244 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002245 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002246 // These operators cannot be expanded directly, emit them as calls to
2247 // library functions.
2248 case ISD::FP_TO_SINT:
2249 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002250 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002251 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002252 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002253 break;
2254 case ISD::FP_TO_UINT:
2255 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002256 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002257 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002258 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002259 break;
2260
Chris Lattnere34b3962005-01-19 04:19:40 +00002261 case ISD::SHL:
2262 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002263 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002264 break;
Chris Lattner47599822005-04-02 03:38:53 +00002265
2266 // If this target supports SHL_PARTS, use it.
2267 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002268 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2269 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002270 break;
2271 }
2272
Chris Lattnere34b3962005-01-19 04:19:40 +00002273 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002274 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002275 break;
2276
2277 case ISD::SRA:
2278 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002279 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002280 break;
Chris Lattner47599822005-04-02 03:38:53 +00002281
2282 // If this target supports SRA_PARTS, use it.
2283 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002284 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2285 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002286 break;
2287 }
2288
Chris Lattnere34b3962005-01-19 04:19:40 +00002289 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002290 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002291 break;
2292 case ISD::SRL:
2293 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002294 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002295 break;
Chris Lattner47599822005-04-02 03:38:53 +00002296
2297 // If this target supports SRL_PARTS, use it.
2298 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002299 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2300 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002301 break;
2302 }
2303
Chris Lattnere34b3962005-01-19 04:19:40 +00002304 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002305 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002306 break;
2307
Misha Brukmanedf128a2005-04-21 22:36:52 +00002308 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002309 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2310 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002311 break;
2312 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002313 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2314 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002315 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002316 case ISD::MUL: {
2317 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2318 SDOperand LL, LH, RL, RH;
2319 ExpandOp(Node->getOperand(0), LL, LH);
2320 ExpandOp(Node->getOperand(1), RL, RH);
2321 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2322 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2323 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2324 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2325 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2326 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2327 } else {
2328 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2329 }
2330 break;
2331 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002332 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2333 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2334 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2335 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002336 }
2337
2338 // Remember in a map if the values will be reused later.
2339 if (!Node->hasOneUse()) {
2340 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2341 std::make_pair(Lo, Hi))).second;
2342 assert(isNew && "Value already expanded?!?");
2343 }
2344}
2345
2346
2347// SelectionDAG::Legalize - This is the entry point for the file.
2348//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002349void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002350 /// run - This is the main entry point to this class.
2351 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002352 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002353}
2354