blob: 9441078d10353ef49781caf8cd34e6cb5f71b528 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-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 Lattner1f2c9d82005-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 Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000121
Chris Lattneraac464e2005-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 Lattner2a7f8a92005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-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 Lattner2e5872c2005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner4add7e32005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000145}
146
Chris Lattnerdc750592005-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 Lattner87a769c2005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattnerdc750592005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattner85d70c62005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000187
Chris Lattnerec26b482005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-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 Lattner32f20bf2005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner3b8e7192005-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 Lattnereb6614d2005-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 Lattnere727af02005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000223 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemancda9aa72005-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 Begeman69d39432005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattnerdc750592005-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 Lattnerbc7497d2005-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 Lattnerdc750592005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
288
289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
293 MVT::f32);
294 } else {
295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
296 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000297 }
298 break;
299 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
307 Changed = true;
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
310 } else {
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
313 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000314 }
315 if (Changed)
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
317 break;
318 }
319
Chris Lattnerdc750592005-01-07 07:47:09 +0000320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
327 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
335 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000336 else
337 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000338
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
344
Chris Lattner3d95c142005-01-19 20:24:35 +0000345 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000348
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
354 }
355
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000360 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000362 } else {
363 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000364 }
Chris Lattner9242c502005-01-09 19:43:23 +0000365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000370 }
Chris Lattner68a12142005-01-07 22:12:08 +0000371 case ISD::BR:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
375 break;
376
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000377 case ISD::BRCOND:
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000379
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
382 case Legal:
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
384 break;
385 case Promote:
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
387 break;
388 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
393 break;
394
Chris Lattnerdc750592005-01-07 07:47:09 +0000395 case ISD::LOAD:
396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
397 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
398 if (Tmp1 != Node->getOperand(0) ||
399 Tmp2 != Node->getOperand(1))
400 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000401 else
402 Result = SDOperand(Node, 0);
403
404 // Since loads produce two values, make sure to remember that we legalized
405 // both of them.
406 AddLegalizedOperand(SDOperand(Node, 0), Result);
407 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
408 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000409
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000410 case ISD::EXTLOAD:
411 case ISD::SEXTLOAD:
412 case ISD::ZEXTLOAD:
413 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
414 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
415 if (Tmp1 != Node->getOperand(0) ||
416 Tmp2 != Node->getOperand(1))
417 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
418 cast<MVTSDNode>(Node)->getExtraValueType());
419 else
420 Result = SDOperand(Node, 0);
421
422 // Since loads produce two values, make sure to remember that we legalized
423 // both of them.
424 AddLegalizedOperand(SDOperand(Node, 0), Result);
425 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
426 return Result.getValue(Op.ResNo);
427
Chris Lattnerdc750592005-01-07 07:47:09 +0000428 case ISD::EXTRACT_ELEMENT:
429 // Get both the low and high parts.
430 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
431 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
432 Result = Tmp2; // 1 -> Hi
433 else
434 Result = Tmp1; // 0 -> Lo
435 break;
436
437 case ISD::CopyToReg:
438 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
439
440 switch (getTypeAction(Node->getOperand(1).getValueType())) {
441 case Legal:
442 // Legalize the incoming value (must be legal).
443 Tmp2 = LegalizeOp(Node->getOperand(1));
444 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000445 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000446 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000447 case Promote:
448 Tmp2 = PromoteOp(Node->getOperand(1));
449 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
450 break;
451 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000452 SDOperand Lo, Hi;
453 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000454 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000455 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
456 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
457 // Note that the copytoreg nodes are independent of each other.
458 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000459 assert(isTypeLegal(Result.getValueType()) &&
460 "Cannot expand multiple times yet (i64 -> i16)");
461 break;
462 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000463 break;
464
465 case ISD::RET:
466 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
467 switch (Node->getNumOperands()) {
468 case 2: // ret val
469 switch (getTypeAction(Node->getOperand(1).getValueType())) {
470 case Legal:
471 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000472 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000473 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
474 break;
475 case Expand: {
476 SDOperand Lo, Hi;
477 ExpandOp(Node->getOperand(1), Lo, Hi);
478 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
479 break;
480 }
481 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000482 Tmp2 = PromoteOp(Node->getOperand(1));
483 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
484 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000485 }
486 break;
487 case 1: // ret void
488 if (Tmp1 != Node->getOperand(0))
489 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
490 break;
491 default: { // ret <values>
492 std::vector<SDOperand> NewValues;
493 NewValues.push_back(Tmp1);
494 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
495 switch (getTypeAction(Node->getOperand(i).getValueType())) {
496 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000497 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000498 break;
499 case Expand: {
500 SDOperand Lo, Hi;
501 ExpandOp(Node->getOperand(i), Lo, Hi);
502 NewValues.push_back(Lo);
503 NewValues.push_back(Hi);
504 break;
505 }
506 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000507 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000508 }
509 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
510 break;
511 }
512 }
513 break;
514 case ISD::STORE:
515 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
516 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
517
Chris Lattnere69daaf2005-01-08 06:25:56 +0000518 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000519 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000520 if (CFP->getValueType(0) == MVT::f32) {
521 union {
522 unsigned I;
523 float F;
524 } V;
525 V.F = CFP->getValue();
526 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
527 DAG.getConstant(V.I, MVT::i32), Tmp2);
528 } else {
529 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
530 union {
531 uint64_t I;
532 double F;
533 } V;
534 V.F = CFP->getValue();
535 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
536 DAG.getConstant(V.I, MVT::i64), Tmp2);
537 }
Chris Lattnera4743132005-02-22 07:23:39 +0000538 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000539 }
540
Chris Lattnerdc750592005-01-07 07:47:09 +0000541 switch (getTypeAction(Node->getOperand(1).getValueType())) {
542 case Legal: {
543 SDOperand Val = LegalizeOp(Node->getOperand(1));
544 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
545 Tmp2 != Node->getOperand(2))
546 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
547 break;
548 }
549 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000550 // Truncate the value and store the result.
551 Tmp3 = PromoteOp(Node->getOperand(1));
552 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
553 Node->getOperand(1).getValueType());
554 break;
555
Chris Lattnerdc750592005-01-07 07:47:09 +0000556 case Expand:
557 SDOperand Lo, Hi;
558 ExpandOp(Node->getOperand(1), Lo, Hi);
559
560 if (!TLI.isLittleEndian())
561 std::swap(Lo, Hi);
562
Chris Lattner0d03eb42005-01-19 18:02:17 +0000563 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000564
Chris Lattner0d03eb42005-01-19 18:02:17 +0000565 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000566 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
567 getIntPtrConstant(IncrementSize));
568 assert(isTypeLegal(Tmp2.getValueType()) &&
569 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000570 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
571 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
572 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000573 }
574 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000575 case ISD::PCMARKER:
576 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000577 if (Tmp1 != Node->getOperand(0))
578 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000579 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000580 case ISD::TRUNCSTORE:
581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
582 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
583
584 switch (getTypeAction(Node->getOperand(1).getValueType())) {
585 case Legal:
586 Tmp2 = LegalizeOp(Node->getOperand(1));
587 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
588 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000589 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000590 cast<MVTSDNode>(Node)->getExtraValueType());
591 break;
592 case Promote:
593 case Expand:
594 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
595 }
596 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000597 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000598 switch (getTypeAction(Node->getOperand(0).getValueType())) {
599 case Expand: assert(0 && "It's impossible to expand bools");
600 case Legal:
601 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
602 break;
603 case Promote:
604 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
605 break;
606 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000607 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000608 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000609
610 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
611 default: assert(0 && "This action is not supported yet!");
612 case TargetLowering::Legal:
613 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
614 Tmp3 != Node->getOperand(2))
615 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
616 Tmp1, Tmp2, Tmp3);
617 break;
618 case TargetLowering::Promote: {
619 MVT::ValueType NVT =
620 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
621 unsigned ExtOp, TruncOp;
622 if (MVT::isInteger(Tmp2.getValueType())) {
623 ExtOp = ISD::ZERO_EXTEND;
624 TruncOp = ISD::TRUNCATE;
625 } else {
626 ExtOp = ISD::FP_EXTEND;
627 TruncOp = ISD::FP_ROUND;
628 }
629 // Promote each of the values to the new type.
630 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
631 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
632 // Perform the larger operation, then round down.
633 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
634 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
635 break;
636 }
637 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000638 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000639 case ISD::SETCC:
640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
641 case Legal:
642 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
643 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
644 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
645 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000646 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000647 break;
648 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000649 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
650 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
651
652 // If this is an FP compare, the operands have already been extended.
653 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
654 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000655 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000656
657 // Otherwise, we have to insert explicit sign or zero extends. Note
658 // that we could insert sign extends for ALL conditions, but zero extend
659 // is cheaper on many machines (an AND instead of two shifts), so prefer
660 // it.
661 switch (cast<SetCCSDNode>(Node)->getCondition()) {
662 default: assert(0 && "Unknown integer comparison!");
663 case ISD::SETEQ:
664 case ISD::SETNE:
665 case ISD::SETUGE:
666 case ISD::SETUGT:
667 case ISD::SETULE:
668 case ISD::SETULT:
669 // ALL of these operations will work if we either sign or zero extend
670 // the operands (including the unsigned comparisons!). Zero extend is
671 // usually a simpler/cheaper operation, so prefer it.
672 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
673 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
674 break;
675 case ISD::SETGE:
676 case ISD::SETGT:
677 case ISD::SETLT:
678 case ISD::SETLE:
679 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
680 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
681 break;
682 }
683
684 }
685 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000686 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000687 break;
688 case Expand:
689 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
690 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
691 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
692 switch (cast<SetCCSDNode>(Node)->getCondition()) {
693 case ISD::SETEQ:
694 case ISD::SETNE:
695 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
696 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
697 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000698 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
699 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000700 DAG.getConstant(0, Tmp1.getValueType()));
701 break;
702 default:
703 // FIXME: This generated code sucks.
704 ISD::CondCode LowCC;
705 switch (cast<SetCCSDNode>(Node)->getCondition()) {
706 default: assert(0 && "Unknown integer setcc!");
707 case ISD::SETLT:
708 case ISD::SETULT: LowCC = ISD::SETULT; break;
709 case ISD::SETGT:
710 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
711 case ISD::SETLE:
712 case ISD::SETULE: LowCC = ISD::SETULE; break;
713 case ISD::SETGE:
714 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
715 }
716
717 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
718 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
719 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
720
721 // NOTE: on targets without efficient SELECT of bools, we can always use
722 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000723 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000724 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000725 Node->getValueType(0), LHSHi, RHSHi);
726 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
727 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
728 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000729 break;
730 }
731 }
732 break;
733
Chris Lattner85d70c62005-01-11 05:57:22 +0000734 case ISD::MEMSET:
735 case ISD::MEMCPY:
736 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000737 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000738 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
739
740 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
741 switch (getTypeAction(Node->getOperand(2).getValueType())) {
742 case Expand: assert(0 && "Cannot expand a byte!");
743 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000744 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000745 break;
746 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000747 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000748 break;
749 }
750 } else {
751 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
752 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000753
754 SDOperand Tmp4;
755 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000756 case Expand: assert(0 && "Cannot expand this yet!");
757 case Legal:
758 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000759 break;
760 case Promote:
761 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000762 break;
763 }
764
765 SDOperand Tmp5;
766 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
767 case Expand: assert(0 && "Cannot expand this yet!");
768 case Legal:
769 Tmp5 = LegalizeOp(Node->getOperand(4));
770 break;
771 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000772 Tmp5 = PromoteOp(Node->getOperand(4));
773 break;
774 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000775
776 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
777 default: assert(0 && "This action not implemented for this operation!");
778 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000779 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
780 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
781 Tmp5 != Node->getOperand(4)) {
782 std::vector<SDOperand> Ops;
783 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
784 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
785 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
786 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000787 break;
788 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000789 // Otherwise, the target does not support this operation. Lower the
790 // operation to an explicit libcall as appropriate.
791 MVT::ValueType IntPtr = TLI.getPointerTy();
792 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
793 std::vector<std::pair<SDOperand, const Type*> > Args;
794
Reid Spencer6dced922005-01-12 14:53:45 +0000795 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000796 if (Node->getOpcode() == ISD::MEMSET) {
797 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
798 // Extend the ubyte argument to be an int value for the call.
799 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
800 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
801 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
802
803 FnName = "memset";
804 } else if (Node->getOpcode() == ISD::MEMCPY ||
805 Node->getOpcode() == ISD::MEMMOVE) {
806 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
807 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
808 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
809 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
810 } else {
811 assert(0 && "Unknown op!");
812 }
813 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000814 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000815 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
816 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000817 break;
818 }
819 case TargetLowering::Custom:
820 std::vector<SDOperand> Ops;
821 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
822 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
823 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
824 Result = TLI.LowerOperation(Result);
825 Result = LegalizeOp(Result);
826 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000827 }
828 break;
829 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000830 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +0000831 case ISD::SUB_PARTS:
832 case ISD::SHL_PARTS:
833 case ISD::SRA_PARTS:
834 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000835 std::vector<SDOperand> Ops;
836 bool Changed = false;
837 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
838 Ops.push_back(LegalizeOp(Node->getOperand(i)));
839 Changed |= Ops.back() != Node->getOperand(i);
840 }
841 if (Changed)
842 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000843
844 // Since these produce multiple values, make sure to remember that we
845 // legalized all of them.
846 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
847 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
848 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000849 }
Chris Lattner13fe99c2005-04-02 05:00:07 +0000850
851 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +0000852 case ISD::ADD:
853 case ISD::SUB:
854 case ISD::MUL:
855 case ISD::UDIV:
856 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +0000857 case ISD::AND:
858 case ISD::OR:
859 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000860 case ISD::SHL:
861 case ISD::SRL:
862 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000863 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
864 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
865 if (Tmp1 != Node->getOperand(0) ||
866 Tmp2 != Node->getOperand(1))
867 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
868 break;
Nate Begeman20b7d2a2005-04-06 00:23:54 +0000869
870 case ISD::UREM:
871 case ISD::SREM:
872 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
873 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
874 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
875 case TargetLowering::Legal:
876 if (Tmp1 != Node->getOperand(0) ||
877 Tmp2 != Node->getOperand(1))
878 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
879 Tmp2);
880 break;
881 case TargetLowering::Promote:
882 case TargetLowering::Custom:
883 assert(0 && "Cannot promote/custom handle this yet!");
884 case TargetLowering::Expand: {
885 MVT::ValueType VT = Node->getValueType(0);
886 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
887 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
888 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
889 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
890 }
891 break;
892 }
893 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +0000894
895 // Unary operators
896 case ISD::FABS:
897 case ISD::FNEG:
898 Tmp1 = LegalizeOp(Node->getOperand(0));
899 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
900 case TargetLowering::Legal:
901 if (Tmp1 != Node->getOperand(0))
902 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
903 break;
904 case TargetLowering::Promote:
905 case TargetLowering::Custom:
906 assert(0 && "Cannot promote/custom handle this yet!");
907 case TargetLowering::Expand:
908 if (Node->getOpcode() == ISD::FNEG) {
909 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
910 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
911 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
912 Tmp2, Tmp1));
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000913 } else if (Node->getOpcode() == ISD::FABS) {
914 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
915 MVT::ValueType VT = Node->getValueType(0);
916 Tmp2 = DAG.getConstantFP(0.0, VT);
917 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
918 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
919 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
920 Result = LegalizeOp(Result);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000921 } else {
Chris Lattnera0c72cf2005-04-02 05:26:37 +0000922 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +0000923 }
924 break;
925 }
926 break;
927
928 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +0000929 case ISD::ZERO_EXTEND:
930 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000931 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000932 case ISD::FP_EXTEND:
933 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000934 case ISD::FP_TO_SINT:
935 case ISD::FP_TO_UINT:
936 case ISD::SINT_TO_FP:
937 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000938 switch (getTypeAction(Node->getOperand(0).getValueType())) {
939 case Legal:
940 Tmp1 = LegalizeOp(Node->getOperand(0));
941 if (Tmp1 != Node->getOperand(0))
942 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
943 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000944 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +0000945 if (Node->getOpcode() == ISD::SINT_TO_FP ||
946 Node->getOpcode() == ISD::UINT_TO_FP) {
947 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
948 Node->getValueType(0), Node->getOperand(0));
949 Result = LegalizeOp(Result);
950 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +0000951 } else if (Node->getOpcode() == ISD::TRUNCATE) {
952 // In the expand case, we must be dealing with a truncate, because
953 // otherwise the result would be larger than the source.
954 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
955
956 // Since the result is legal, we should just be able to truncate the low
957 // part of the source.
958 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
959 break;
Chris Lattneraac464e2005-01-21 06:05:23 +0000960 }
Chris Lattner13fe99c2005-04-02 05:00:07 +0000961 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +0000962
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000963 case Promote:
964 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000965 case ISD::ZERO_EXTEND:
966 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000967 // NOTE: Any extend would work here...
968 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
969 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000970 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000971 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000972 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000973 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000974 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000975 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000976 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
977 Result, Node->getOperand(0).getValueType());
978 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000979 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000980 Result = PromoteOp(Node->getOperand(0));
981 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
982 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000983 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000984 Result = PromoteOp(Node->getOperand(0));
985 if (Result.getValueType() != Op.getValueType())
986 // Dynamically dead while we have only 2 FP types.
987 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
988 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000989 case ISD::FP_ROUND:
990 case ISD::FP_TO_SINT:
991 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000992 Result = PromoteOp(Node->getOperand(0));
993 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
994 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000995 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000996 Result = PromoteOp(Node->getOperand(0));
997 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
998 Result, Node->getOperand(0).getValueType());
999 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1000 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001001 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001002 Result = PromoteOp(Node->getOperand(0));
1003 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1004 Result, Node->getOperand(0).getValueType());
1005 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1006 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001007 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001008 }
1009 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001010 case ISD::FP_ROUND_INREG:
1011 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +00001012 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001013 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001014 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1015
1016 // If this operation is not supported, convert it to a shl/shr or load/store
1017 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001018 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1019 default: assert(0 && "This action not supported for this op yet!");
1020 case TargetLowering::Legal:
1021 if (Tmp1 != Node->getOperand(0))
1022 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1023 ExtraVT);
1024 break;
1025 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001026 // If this is an integer extend and shifts are supported, do that.
1027 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
1028 // NOTE: we could fall back on load/store here too for targets without
1029 // AND. However, it is doubtful that any exist.
1030 // AND out the appropriate bits.
1031 SDOperand Mask =
1032 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
1033 Node->getValueType(0));
1034 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
1035 Node->getOperand(0), Mask);
1036 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
1037 // NOTE: we could fall back on load/store here too for targets without
1038 // SAR. However, it is doubtful that any exist.
1039 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1040 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001041 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001042 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1043 Node->getOperand(0), ShiftCst);
1044 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1045 Result, ShiftCst);
1046 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1047 // The only way we can lower this is to turn it into a STORETRUNC,
1048 // EXTLOAD pair, targetting a temporary location (a stack slot).
1049
1050 // NOTE: there is a choice here between constantly creating new stack
1051 // slots and always reusing the same one. We currently always create
1052 // new ones, as reuse may inhibit scheduling.
1053 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1054 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1055 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1056 MachineFunction &MF = DAG.getMachineFunction();
1057 int SSFI =
1058 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1059 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1060 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1061 Node->getOperand(0), StackSlot, ExtraVT);
1062 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1063 Result, StackSlot, ExtraVT);
1064 } else {
1065 assert(0 && "Unknown op");
1066 }
1067 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001068 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001069 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001070 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001071 }
Chris Lattner99222f72005-01-15 07:15:18 +00001072 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001073
Chris Lattnerea4ca942005-01-07 22:28:47 +00001074 if (!Op.Val->hasOneUse())
1075 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001076
1077 return Result;
1078}
1079
Chris Lattner4d978642005-01-15 22:16:26 +00001080/// PromoteOp - Given an operation that produces a value in an invalid type,
1081/// promote it to compute the value into a larger type. The produced value will
1082/// have the correct bits for the low portion of the register, but no guarantee
1083/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001084SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1085 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001086 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001087 assert(getTypeAction(VT) == Promote &&
1088 "Caller should expand or legalize operands that are not promotable!");
1089 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1090 "Cannot promote to smaller type!");
1091
1092 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1093 if (I != PromotedNodes.end()) return I->second;
1094
1095 SDOperand Tmp1, Tmp2, Tmp3;
1096
1097 SDOperand Result;
1098 SDNode *Node = Op.Val;
1099
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001100 // Promotion needs an optimization step to clean up after it, and is not
1101 // careful to avoid operations the target does not support. Make sure that
1102 // all generated operations are legalized in the next iteration.
1103 NeedsAnotherIteration = true;
1104
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001105 switch (Node->getOpcode()) {
1106 default:
1107 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1108 assert(0 && "Do not know how to promote this operator!");
1109 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001110 case ISD::UNDEF:
1111 Result = DAG.getNode(ISD::UNDEF, NVT);
1112 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001113 case ISD::Constant:
1114 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1115 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1116 break;
1117 case ISD::ConstantFP:
1118 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1119 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1120 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001121 case ISD::CopyFromReg:
1122 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1123 Node->getOperand(0));
1124 // Remember that we legalized the chain.
1125 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1126 break;
1127
Chris Lattner2cb338d2005-01-18 02:59:52 +00001128 case ISD::SETCC:
1129 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1130 "SetCC type is not legal??");
1131 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1132 TLI.getSetCCResultTy(), Node->getOperand(0),
1133 Node->getOperand(1));
1134 Result = LegalizeOp(Result);
1135 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001136
1137 case ISD::TRUNCATE:
1138 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1139 case Legal:
1140 Result = LegalizeOp(Node->getOperand(0));
1141 assert(Result.getValueType() >= NVT &&
1142 "This truncation doesn't make sense!");
1143 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1144 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1145 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001146 case Promote:
1147 // The truncation is not required, because we don't guarantee anything
1148 // about high bits anyway.
1149 Result = PromoteOp(Node->getOperand(0));
1150 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001151 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001152 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1153 // Truncate the low part of the expanded value to the result type
1154 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001155 }
1156 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001157 case ISD::SIGN_EXTEND:
1158 case ISD::ZERO_EXTEND:
1159 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1160 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1161 case Legal:
1162 // Input is legal? Just do extend all the way to the larger type.
1163 Result = LegalizeOp(Node->getOperand(0));
1164 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1165 break;
1166 case Promote:
1167 // Promote the reg if it's smaller.
1168 Result = PromoteOp(Node->getOperand(0));
1169 // The high bits are not guaranteed to be anything. Insert an extend.
1170 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001171 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1172 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001173 else
Chris Lattner05596912005-02-04 18:39:19 +00001174 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1175 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001176 break;
1177 }
1178 break;
1179
1180 case ISD::FP_EXTEND:
1181 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1182 case ISD::FP_ROUND:
1183 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1184 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1185 case Promote: assert(0 && "Unreachable with 2 FP types!");
1186 case Legal:
1187 // Input is legal? Do an FP_ROUND_INREG.
1188 Result = LegalizeOp(Node->getOperand(0));
1189 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1190 break;
1191 }
1192 break;
1193
1194 case ISD::SINT_TO_FP:
1195 case ISD::UINT_TO_FP:
1196 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1197 case Legal:
1198 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001199 // No extra round required here.
1200 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001201 break;
1202
1203 case Promote:
1204 Result = PromoteOp(Node->getOperand(0));
1205 if (Node->getOpcode() == ISD::SINT_TO_FP)
1206 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1207 Result, Node->getOperand(0).getValueType());
1208 else
1209 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1210 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001211 // No extra round required here.
1212 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001213 break;
1214 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001215 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1216 Node->getOperand(0));
1217 Result = LegalizeOp(Result);
1218
1219 // Round if we cannot tolerate excess precision.
1220 if (NoExcessFPPrecision)
1221 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1222 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001223 }
Chris Lattner4d978642005-01-15 22:16:26 +00001224 break;
1225
1226 case ISD::FP_TO_SINT:
1227 case ISD::FP_TO_UINT:
1228 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1229 case Legal:
1230 Tmp1 = LegalizeOp(Node->getOperand(0));
1231 break;
1232 case Promote:
1233 // The input result is prerounded, so we don't have to do anything
1234 // special.
1235 Tmp1 = PromoteOp(Node->getOperand(0));
1236 break;
1237 case Expand:
1238 assert(0 && "not implemented");
1239 }
1240 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1241 break;
1242
Chris Lattner13fe99c2005-04-02 05:00:07 +00001243 case ISD::FABS:
1244 case ISD::FNEG:
1245 Tmp1 = PromoteOp(Node->getOperand(0));
1246 assert(Tmp1.getValueType() == NVT);
1247 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1248 // NOTE: we do not have to do any extra rounding here for
1249 // NoExcessFPPrecision, because we know the input will have the appropriate
1250 // precision, and these operations don't modify precision at all.
1251 break;
1252
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001253 case ISD::AND:
1254 case ISD::OR:
1255 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001256 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001257 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001258 case ISD::MUL:
1259 // The input may have strange things in the top bits of the registers, but
1260 // these operations don't care. They may have wierd bits going out, but
1261 // that too is okay if they are integer operations.
1262 Tmp1 = PromoteOp(Node->getOperand(0));
1263 Tmp2 = PromoteOp(Node->getOperand(1));
1264 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1265 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1266
1267 // However, if this is a floating point operation, they will give excess
1268 // precision that we may not be able to tolerate. If we DO allow excess
1269 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001270 // FIXME: Why would we need to round FP ops more than integer ones?
1271 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001272 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1273 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1274 break;
1275
Chris Lattner4d978642005-01-15 22:16:26 +00001276 case ISD::SDIV:
1277 case ISD::SREM:
1278 // These operators require that their input be sign extended.
1279 Tmp1 = PromoteOp(Node->getOperand(0));
1280 Tmp2 = PromoteOp(Node->getOperand(1));
1281 if (MVT::isInteger(NVT)) {
1282 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001283 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001284 }
1285 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1286
1287 // Perform FP_ROUND: this is probably overly pessimistic.
1288 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1289 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1290 break;
1291
1292 case ISD::UDIV:
1293 case ISD::UREM:
1294 // These operators require that their input be zero extended.
1295 Tmp1 = PromoteOp(Node->getOperand(0));
1296 Tmp2 = PromoteOp(Node->getOperand(1));
1297 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1298 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001299 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001300 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1301 break;
1302
1303 case ISD::SHL:
1304 Tmp1 = PromoteOp(Node->getOperand(0));
1305 Tmp2 = LegalizeOp(Node->getOperand(1));
1306 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1307 break;
1308 case ISD::SRA:
1309 // The input value must be properly sign extended.
1310 Tmp1 = PromoteOp(Node->getOperand(0));
1311 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1312 Tmp2 = LegalizeOp(Node->getOperand(1));
1313 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1314 break;
1315 case ISD::SRL:
1316 // The input value must be properly zero extended.
1317 Tmp1 = PromoteOp(Node->getOperand(0));
1318 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1319 Tmp2 = LegalizeOp(Node->getOperand(1));
1320 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1321 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001322 case ISD::LOAD:
1323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1324 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1325 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1326
1327 // Remember that we legalized the chain.
1328 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1329 break;
1330 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001331 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1332 case Expand: assert(0 && "It's impossible to expand bools");
1333 case Legal:
1334 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1335 break;
1336 case Promote:
1337 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1338 break;
1339 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001340 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1341 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1342 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1343 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001344 case ISD::CALL: {
1345 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1347
Chris Lattner3d95c142005-01-19 20:24:35 +00001348 std::vector<SDOperand> Ops;
1349 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1350 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1351
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001352 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1353 "Can only promote single result calls");
1354 std::vector<MVT::ValueType> RetTyVTs;
1355 RetTyVTs.reserve(2);
1356 RetTyVTs.push_back(NVT);
1357 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001358 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001359 Result = SDOperand(NC, 0);
1360
1361 // Insert the new chain mapping.
1362 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1363 break;
1364 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001365 }
1366
1367 assert(Result.Val && "Didn't set a result!");
1368 AddPromotedOperand(Op, Result);
1369 return Result;
1370}
Chris Lattnerdc750592005-01-07 07:47:09 +00001371
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001372/// ExpandAddSub - Find a clever way to expand this add operation into
1373/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001374void SelectionDAGLegalize::
1375ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1376 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001377 // Expand the subcomponents.
1378 SDOperand LHSL, LHSH, RHSL, RHSH;
1379 ExpandOp(LHS, LHSL, LHSH);
1380 ExpandOp(RHS, RHSL, RHSH);
1381
1382 // Convert this add to the appropriate ADDC pair. The low part has no carry
1383 // in.
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001384 std::vector<SDOperand> Ops;
1385 Ops.push_back(LHSL);
1386 Ops.push_back(LHSH);
1387 Ops.push_back(RHSL);
1388 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001389 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001390 Hi = Lo.getValue(1);
1391}
1392
Chris Lattner4157c412005-04-02 04:00:59 +00001393void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1394 SDOperand Op, SDOperand Amt,
1395 SDOperand &Lo, SDOperand &Hi) {
1396 // Expand the subcomponents.
1397 SDOperand LHSL, LHSH;
1398 ExpandOp(Op, LHSL, LHSH);
1399
1400 std::vector<SDOperand> Ops;
1401 Ops.push_back(LHSL);
1402 Ops.push_back(LHSH);
1403 Ops.push_back(Amt);
1404 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1405 Hi = Lo.getValue(1);
1406}
1407
1408
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001409/// ExpandShift - Try to find a clever way to expand this shift operation out to
1410/// smaller elements. If we can't find a way that is more efficient than a
1411/// libcall on this target, return false. Otherwise, return true with the
1412/// low-parts expanded into Lo and Hi.
1413bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1414 SDOperand &Lo, SDOperand &Hi) {
1415 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1416 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001417
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001418 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001419 SDOperand ShAmt = LegalizeOp(Amt);
1420 MVT::ValueType ShTy = ShAmt.getValueType();
1421 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1422 unsigned NVTBits = MVT::getSizeInBits(NVT);
1423
1424 // Handle the case when Amt is an immediate. Other cases are currently broken
1425 // and are disabled.
1426 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1427 unsigned Cst = CN->getValue();
1428 // Expand the incoming operand to be shifted, so that we have its parts
1429 SDOperand InL, InH;
1430 ExpandOp(Op, InL, InH);
1431 switch(Opc) {
1432 case ISD::SHL:
1433 if (Cst > VTBits) {
1434 Lo = DAG.getConstant(0, NVT);
1435 Hi = DAG.getConstant(0, NVT);
1436 } else if (Cst > NVTBits) {
1437 Lo = DAG.getConstant(0, NVT);
1438 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
1439 } else {
1440 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1441 Hi = DAG.getNode(ISD::OR, NVT,
1442 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1443 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1444 }
1445 return true;
1446 case ISD::SRL:
1447 if (Cst > VTBits) {
1448 Lo = DAG.getConstant(0, NVT);
1449 Hi = DAG.getConstant(0, NVT);
1450 } else if (Cst > NVTBits) {
1451 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1452 Hi = DAG.getConstant(0, NVT);
1453 } else {
1454 Lo = DAG.getNode(ISD::OR, NVT,
1455 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1456 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1457 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1458 }
1459 return true;
1460 case ISD::SRA:
1461 if (Cst > VTBits) {
1462 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1463 DAG.getConstant(NVTBits-1, ShTy));
1464 } else if (Cst > NVTBits) {
1465 Lo = DAG.getNode(ISD::SRA, NVT, InH,
1466 DAG.getConstant(Cst-NVTBits, ShTy));
1467 Hi = DAG.getNode(ISD::SRA, NVT, InH,
1468 DAG.getConstant(NVTBits-1, ShTy));
1469 } else {
1470 Lo = DAG.getNode(ISD::OR, NVT,
1471 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1472 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1473 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1474 }
1475 return true;
1476 }
1477 }
1478 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1479 // so disable it for now. Currently targets are handling this via SHL_PARTS
1480 // and friends.
1481 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001482
1483 // If we have an efficient select operation (or if the selects will all fold
1484 // away), lower to some complex code, otherwise just emit the libcall.
1485 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1486 !isa<ConstantSDNode>(Amt))
1487 return false;
1488
1489 SDOperand InL, InH;
1490 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001491 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1492 DAG.getConstant(NVTBits, ShTy), ShAmt);
1493
Chris Lattner4d25c042005-01-20 20:29:23 +00001494 // Compare the unmasked shift amount against 32.
1495 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1496 DAG.getConstant(NVTBits, ShTy));
1497
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001498 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1499 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1500 DAG.getConstant(NVTBits-1, ShTy));
1501 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1502 DAG.getConstant(NVTBits-1, ShTy));
1503 }
1504
1505 if (Opc == ISD::SHL) {
1506 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1507 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1508 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001509 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001510
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001511 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1512 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1513 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001514 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1515 DAG.getSetCC(ISD::SETEQ,
1516 TLI.getSetCCResultTy(), NAmt,
1517 DAG.getConstant(32, ShTy)),
1518 DAG.getConstant(0, NVT),
1519 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001520 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001521 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001522 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001523 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001524
1525 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001526 if (Opc == ISD::SRA)
1527 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1528 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001529 else
1530 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001531 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001532 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001533 }
1534 return true;
1535}
Chris Lattneraac464e2005-01-21 06:05:23 +00001536
Chris Lattner4add7e32005-01-23 04:42:50 +00001537/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1538/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1539/// Found.
1540static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1541 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1542
1543 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1544 // than the Found node. Just remember this node and return.
1545 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1546 Found = Node;
1547 return;
1548 }
1549
1550 // Otherwise, scan the operands of Node to see if any of them is a call.
1551 assert(Node->getNumOperands() != 0 &&
1552 "All leaves should have depth equal to the entry node!");
1553 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1554 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1555
1556 // Tail recurse for the last iteration.
1557 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1558 Found);
1559}
1560
1561
1562/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1563/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1564/// than Found.
1565static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1566 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1567
1568 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1569 // than the Found node. Just remember this node and return.
1570 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1571 Found = Node;
1572 return;
1573 }
1574
1575 // Otherwise, scan the operands of Node to see if any of them is a call.
1576 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1577 if (UI == E) return;
1578 for (--E; UI != E; ++UI)
1579 FindEarliestAdjCallStackUp(*UI, Found);
1580
1581 // Tail recurse for the last iteration.
1582 FindEarliestAdjCallStackUp(*UI, Found);
1583}
1584
1585/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1586/// find the ADJCALLSTACKUP node that terminates the call sequence.
1587static SDNode *FindAdjCallStackUp(SDNode *Node) {
1588 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1589 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001590 if (Node->use_empty())
1591 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001592
1593 if (Node->hasOneUse()) // Simple case, only has one user to check.
1594 return FindAdjCallStackUp(*Node->use_begin());
1595
1596 SDOperand TheChain(Node, Node->getNumValues()-1);
1597 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1598
1599 for (SDNode::use_iterator UI = Node->use_begin(),
1600 E = Node->use_end(); ; ++UI) {
1601 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1602
1603 // Make sure to only follow users of our token chain.
1604 SDNode *User = *UI;
1605 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1606 if (User->getOperand(i) == TheChain)
1607 return FindAdjCallStackUp(User);
1608 }
1609 assert(0 && "Unreachable");
1610 abort();
1611}
1612
1613/// FindInputOutputChains - If we are replacing an operation with a call we need
1614/// to find the call that occurs before and the call that occurs after it to
1615/// properly serialize the calls in the block.
1616static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1617 SDOperand Entry) {
1618 SDNode *LatestAdjCallStackDown = Entry.Val;
1619 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1620 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1621
1622 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1623
1624
1625 SDNode *EarliestAdjCallStackUp = 0;
1626 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1627
1628 if (EarliestAdjCallStackUp) {
1629 //std::cerr << "Found node: ";
1630 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1631 }
1632
1633 return SDOperand(LatestAdjCallStackUp, 0);
1634}
1635
1636
1637
Chris Lattneraac464e2005-01-21 06:05:23 +00001638// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1639// does not fit into a register, return the lo part and set the hi part to the
1640// by-reg argument. If it does fit into a single register, return the result
1641// and leave the Hi part unset.
1642SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1643 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001644 SDNode *OutChain;
1645 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1646 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00001647 if (InChain.Val == 0)
1648 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00001649
Chris Lattneraac464e2005-01-21 06:05:23 +00001650 TargetLowering::ArgListTy Args;
1651 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1652 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1653 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1654 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1655 }
1656 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1657
1658 // We don't care about token chains for libcalls. We just use the entry
1659 // node as our input and ignore the output chain. This allows us to place
1660 // calls wherever we need them to satisfy data dependences.
1661 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begemanf6565252005-03-26 01:29:23 +00001662 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001663 Args, DAG).first;
1664 switch (getTypeAction(Result.getValueType())) {
1665 default: assert(0 && "Unknown thing");
1666 case Legal:
1667 return Result;
1668 case Promote:
1669 assert(0 && "Cannot promote this yet!");
1670 case Expand:
1671 SDOperand Lo;
1672 ExpandOp(Result, Lo, Hi);
1673 return Lo;
1674 }
1675}
1676
Chris Lattner4add7e32005-01-23 04:42:50 +00001677
Chris Lattneraac464e2005-01-21 06:05:23 +00001678/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1679/// destination type is legal.
1680SDOperand SelectionDAGLegalize::
1681ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1682 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1683 assert(getTypeAction(Source.getValueType()) == Expand &&
1684 "This is not an expansion!");
1685 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1686
Chris Lattner4add7e32005-01-23 04:42:50 +00001687 SDNode *OutChain;
1688 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1689 DAG.getEntryNode());
1690
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001691 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001692 if (isSigned) {
1693 if (DestTy == MVT::f32)
1694 FnName = "__floatdisf";
1695 else {
1696 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1697 FnName = "__floatdidf";
1698 }
1699 } else {
1700 // If this is unsigned, and not supported, first perform the conversion to
1701 // signed, then adjust the result if the sign bit is set.
1702 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1703
1704 assert(0 && "Unsigned casts not supported yet!");
1705 }
1706 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1707
1708 TargetLowering::ArgListTy Args;
1709 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1710 Args.push_back(std::make_pair(Source, ArgTy));
1711
1712 // We don't care about token chains for libcalls. We just use the entry
1713 // node as our input and ignore the output chain. This allows us to place
1714 // calls wherever we need them to satisfy data dependences.
1715 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begemanf6565252005-03-26 01:29:23 +00001716 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner4add7e32005-01-23 04:42:50 +00001717
Chris Lattneraac464e2005-01-21 06:05:23 +00001718}
1719
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001720
1721
Chris Lattnerdc750592005-01-07 07:47:09 +00001722/// ExpandOp - Expand the specified SDOperand into its two component pieces
1723/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1724/// LegalizeNodes map is filled in for any results that are not expanded, the
1725/// ExpandedNodes map is filled in for any results that are expanded, and the
1726/// Lo/Hi values are returned.
1727void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1728 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001729 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001730 SDNode *Node = Op.Val;
1731 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1732 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1733 assert(MVT::isInteger(NVT) && NVT < VT &&
1734 "Cannot expand to FP value or to larger int value!");
1735
1736 // If there is more than one use of this, see if we already expanded it.
1737 // There is no use remembering values that only have a single use, as the map
1738 // entries will never be reused.
1739 if (!Node->hasOneUse()) {
1740 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1741 = ExpandedNodes.find(Op);
1742 if (I != ExpandedNodes.end()) {
1743 Lo = I->second.first;
1744 Hi = I->second.second;
1745 return;
1746 }
1747 }
1748
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001749 // Expanding to multiple registers needs to perform an optimization step, and
1750 // is not careful to avoid operations the target does not support. Make sure
1751 // that all generated operations are legalized in the next iteration.
1752 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001753
Chris Lattnerdc750592005-01-07 07:47:09 +00001754 switch (Node->getOpcode()) {
1755 default:
1756 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1757 assert(0 && "Do not know how to expand this operator!");
1758 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001759 case ISD::UNDEF:
1760 Lo = DAG.getNode(ISD::UNDEF, NVT);
1761 Hi = DAG.getNode(ISD::UNDEF, NVT);
1762 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001763 case ISD::Constant: {
1764 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1765 Lo = DAG.getConstant(Cst, NVT);
1766 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1767 break;
1768 }
1769
1770 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001771 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001772 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001773 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1774 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1775
1776 // Remember that we legalized the chain.
1777 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1778
Chris Lattnerdc750592005-01-07 07:47:09 +00001779 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1780 break;
1781 }
1782
Chris Lattner32e08b72005-03-28 22:03:13 +00001783 case ISD::BUILD_PAIR:
1784 // Legalize both operands. FIXME: in the future we should handle the case
1785 // where the two elements are not legal.
1786 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1787 Lo = LegalizeOp(Node->getOperand(0));
1788 Hi = LegalizeOp(Node->getOperand(1));
1789 break;
1790
Chris Lattnerdc750592005-01-07 07:47:09 +00001791 case ISD::LOAD: {
1792 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1793 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1794 Lo = DAG.getLoad(NVT, Ch, Ptr);
1795
1796 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001797 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001798 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1799 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001800 Hi = DAG.getLoad(NVT, Ch, Ptr);
1801
1802 // Build a factor node to remember that this load is independent of the
1803 // other one.
1804 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1805 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001806
1807 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001808 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001809 if (!TLI.isLittleEndian())
1810 std::swap(Lo, Hi);
1811 break;
1812 }
1813 case ISD::CALL: {
1814 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1815 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1816
Chris Lattner3d95c142005-01-19 20:24:35 +00001817 bool Changed = false;
1818 std::vector<SDOperand> Ops;
1819 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1820 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1821 Changed |= Ops.back() != Node->getOperand(i);
1822 }
1823
Chris Lattnerdc750592005-01-07 07:47:09 +00001824 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1825 "Can only expand a call once so far, not i64 -> i16!");
1826
1827 std::vector<MVT::ValueType> RetTyVTs;
1828 RetTyVTs.reserve(3);
1829 RetTyVTs.push_back(NVT);
1830 RetTyVTs.push_back(NVT);
1831 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001832 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001833 Lo = SDOperand(NC, 0);
1834 Hi = SDOperand(NC, 1);
1835
1836 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001837 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001838 break;
1839 }
1840 case ISD::AND:
1841 case ISD::OR:
1842 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1843 SDOperand LL, LH, RL, RH;
1844 ExpandOp(Node->getOperand(0), LL, LH);
1845 ExpandOp(Node->getOperand(1), RL, RH);
1846 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1847 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1848 break;
1849 }
1850 case ISD::SELECT: {
1851 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001852
1853 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1854 case Expand: assert(0 && "It's impossible to expand bools");
1855 case Legal:
1856 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1857 break;
1858 case Promote:
1859 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1860 break;
1861 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001862 ExpandOp(Node->getOperand(1), LL, LH);
1863 ExpandOp(Node->getOperand(2), RL, RH);
1864 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1865 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1866 break;
1867 }
1868 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00001869 SDOperand In;
1870 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1871 case Expand: assert(0 && "expand-expand not implemented yet!");
1872 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1873 case Promote:
1874 In = PromoteOp(Node->getOperand(0));
1875 // Emit the appropriate sign_extend_inreg to get the value we want.
1876 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
1877 Node->getOperand(0).getValueType());
1878 break;
1879 }
1880
Chris Lattnerdc750592005-01-07 07:47:09 +00001881 // The low part is just a sign extension of the input (which degenerates to
1882 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00001883 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Chris Lattnerdc750592005-01-07 07:47:09 +00001884
1885 // The high part is obtained by SRA'ing all but one of the bits of the lo
1886 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001887 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001888 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1889 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001890 break;
1891 }
Chris Lattner47844892005-04-03 23:41:52 +00001892 case ISD::ZERO_EXTEND: {
1893 SDOperand In;
1894 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1895 case Expand: assert(0 && "expand-expand not implemented yet!");
1896 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
1897 case Promote:
1898 In = PromoteOp(Node->getOperand(0));
1899 // Emit the appropriate zero_extend_inreg to get the value we want.
1900 In = DAG.getNode(ISD::ZERO_EXTEND_INREG, In.getValueType(), In,
1901 Node->getOperand(0).getValueType());
1902 break;
1903 }
1904
Chris Lattnerdc750592005-01-07 07:47:09 +00001905 // The low part is just a zero extension of the input (which degenerates to
1906 // a copy).
1907 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1908
1909 // The high part is just a zero.
1910 Hi = DAG.getConstant(0, NVT);
1911 break;
Chris Lattner47844892005-04-03 23:41:52 +00001912 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001913 // These operators cannot be expanded directly, emit them as calls to
1914 // library functions.
1915 case ISD::FP_TO_SINT:
1916 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001917 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001918 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001919 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001920 break;
1921 case ISD::FP_TO_UINT:
1922 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001923 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001924 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001925 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001926 break;
1927
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001928 case ISD::SHL:
1929 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001930 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001931 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00001932
1933 // If this target supports SHL_PARTS, use it.
1934 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00001935 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
1936 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001937 break;
1938 }
1939
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001940 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001941 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001942 break;
1943
1944 case ISD::SRA:
1945 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001946 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001947 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00001948
1949 // If this target supports SRA_PARTS, use it.
1950 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00001951 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
1952 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001953 break;
1954 }
1955
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001956 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001957 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001958 break;
1959 case ISD::SRL:
1960 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001961 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001962 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00001963
1964 // If this target supports SRL_PARTS, use it.
1965 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00001966 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
1967 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001968 break;
1969 }
1970
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001971 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001972 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001973 break;
1974
Chris Lattner2e5872c2005-04-02 03:38:53 +00001975 case ISD::ADD:
1976 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
1977 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001978 break;
1979 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00001980 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
1981 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001982 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001983 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1984 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1985 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1986 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1987 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001988 }
1989
1990 // Remember in a map if the values will be reused later.
1991 if (!Node->hasOneUse()) {
1992 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1993 std::make_pair(Lo, Hi))).second;
1994 assert(isNew && "Value already expanded?!?");
1995 }
1996}
1997
1998
1999// SelectionDAG::Legalize - This is the entry point for the file.
2000//
Chris Lattner4add7e32005-01-23 04:42:50 +00002001void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002002 /// run - This is the main entry point to this class.
2003 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002004 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002005}
2006