blob: 91badeb2bdf080372e56cd24746d2ce1c77eb219 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
28/// hacks on it until the target machine can handle it. This involves
29/// eliminating value sizes the machine cannot handle (promoting small sizes to
30/// large sizes or splitting up large values into small values) as well as
31/// eliminating operations the machine cannot handle.
32///
33/// This code also does a small amount of optimization and recognition of idioms
34/// as part of its processing. For example, if a target does not support a
35/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
36/// will attempt merge setcc and brc instructions into brcc's.
37///
38namespace {
39class SelectionDAGLegalize {
40 TargetLowering &TLI;
41 SelectionDAG &DAG;
42
43 /// LegalizeAction - This enum indicates what action we should take for each
44 /// value type the can occur in the program.
45 enum LegalizeAction {
46 Legal, // The target natively supports this value type.
47 Promote, // This should be promoted to the next larger type.
48 Expand, // This integer type should be broken into smaller pieces.
49 };
50
Chris Lattner3e928bb2005-01-07 07:47:09 +000051 /// ValueTypeActions - This is a bitvector that contains two bits for each
52 /// value type, where the two bits correspond to the LegalizeAction enum.
53 /// This can be queried with "getTypeAction(VT)".
54 unsigned ValueTypeActions;
55
56 /// NeedsAnotherIteration - This is set when we expand a large integer
57 /// operation into smaller integer operations, but the smaller operations are
58 /// not set. This occurs only rarely in practice, for targets that don't have
59 /// 32-bit or larger integer registers.
60 bool NeedsAnotherIteration;
61
62 /// LegalizedNodes - For nodes that are of legal width, and that have more
63 /// than one use, this map indicates what regularized operand to use. This
64 /// allows us to avoid legalizing the same thing more than once.
65 std::map<SDOperand, SDOperand> LegalizedNodes;
66
Chris Lattner03c85462005-01-15 05:21:40 +000067 /// PromotedNodes - For nodes that are below legal width, and that have more
68 /// than one use, this map indicates what promoted value to use. This allows
69 /// us to avoid promoting the same thing more than once.
70 std::map<SDOperand, SDOperand> PromotedNodes;
71
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 /// ExpandedNodes - For nodes that need to be expanded, and which have more
73 /// than one use, this map indicates which which operands are the expanded
74 /// version of the input. This allows us to avoid expanding the same node
75 /// more than once.
76 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
77
Chris Lattner8afc48e2005-01-07 22:28:47 +000078 void AddLegalizedOperand(SDOperand From, SDOperand To) {
79 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
80 assert(isNew && "Got into the map somehow?");
81 }
Chris Lattner03c85462005-01-15 05:21:40 +000082 void AddPromotedOperand(SDOperand From, SDOperand To) {
83 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
84 assert(isNew && "Got into the map somehow?");
85 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000086
Chris Lattner3e928bb2005-01-07 07:47:09 +000087public:
88
Chris Lattner9c32d3b2005-01-23 04:42:50 +000089 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000090
91 /// Run - While there is still lowering to do, perform a pass over the DAG.
92 /// Most regularization can be done in a single pass, but targets that require
93 /// large values to be split into registers multiple times (e.g. i64 -> 4x
94 /// i16) require iteration for these values (the first iteration will demote
95 /// to i32, the second will demote to i16).
96 void Run() {
97 do {
98 NeedsAnotherIteration = false;
99 LegalizeDAG();
100 } while (NeedsAnotherIteration);
101 }
102
103 /// getTypeAction - Return how we should legalize values of this type, either
104 /// it is already legal or we need to expand it into multiple registers of
105 /// smaller integer type, or we need to promote it to a larger type.
106 LegalizeAction getTypeAction(MVT::ValueType VT) const {
107 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
108 }
109
110 /// isTypeLegal - Return true if this type is legal on this target.
111 ///
112 bool isTypeLegal(MVT::ValueType VT) const {
113 return getTypeAction(VT) == Legal;
114 }
115
116private:
117 void LegalizeDAG();
118
119 SDOperand LegalizeOp(SDOperand O);
120 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000121 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000122
Chris Lattner77e77a62005-01-21 06:05:23 +0000123 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
124 SDOperand &Hi);
125 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
126 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
131 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000132 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000133
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000134 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
135
Chris Lattner3e928bb2005-01-07 07:47:09 +0000136 SDOperand getIntPtrConstant(uint64_t Val) {
137 return DAG.getConstant(Val, TLI.getPointerTy());
138 }
139};
140}
141
142
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000143SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
144 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
145 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 assert(MVT::LAST_VALUETYPE <= 16 &&
147 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000148}
149
Chris Lattner3e928bb2005-01-07 07:47:09 +0000150void SelectionDAGLegalize::LegalizeDAG() {
151 SDOperand OldRoot = DAG.getRoot();
152 SDOperand NewRoot = LegalizeOp(OldRoot);
153 DAG.setRoot(NewRoot);
154
155 ExpandedNodes.clear();
156 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000157 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158
159 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000160 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000161}
162
163SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000164 assert(getTypeAction(Op.getValueType()) == Legal &&
165 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000166 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000167
Chris Lattner3e928bb2005-01-07 07:47:09 +0000168 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000169 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000170 if (Node->getNumValues() > 1) {
171 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
172 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000173 case Legal: break; // Nothing to do.
174 case Expand: {
175 SDOperand T1, T2;
176 ExpandOp(Op.getValue(i), T1, T2);
177 assert(LegalizedNodes.count(Op) &&
178 "Expansion didn't add legal operands!");
179 return LegalizedNodes[Op];
180 }
181 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000182 PromoteOp(Op.getValue(i));
183 assert(LegalizedNodes.count(Op) &&
184 "Expansion didn't add legal operands!");
185 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000186 }
187 }
188
Chris Lattner45982da2005-05-12 16:53:42 +0000189 // Note that LegalizeOp may be reentered even from single-use nodes, which
190 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000191 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
192 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000193
Chris Lattnerfa404e82005-01-09 19:03:49 +0000194 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000195
196 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000197
198 switch (Node->getOpcode()) {
199 default:
200 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
201 assert(0 && "Do not know how to legalize this operator!");
202 abort();
203 case ISD::EntryToken:
204 case ISD::FrameIndex:
205 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000206 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000207 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000208 assert(getTypeAction(Node->getValueType(0)) == Legal &&
209 "This must be legal!");
210 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000211 case ISD::CopyFromReg:
212 Tmp1 = LegalizeOp(Node->getOperand(0));
213 if (Tmp1 != Node->getOperand(0))
214 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
215 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000216 else
217 Result = Op.getValue(0);
218
219 // Since CopyFromReg produces two values, make sure to remember that we
220 // legalized both of them.
221 AddLegalizedOperand(Op.getValue(0), Result);
222 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
223 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000224 case ISD::ImplicitDef:
225 Tmp1 = LegalizeOp(Node->getOperand(0));
226 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000227 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000228 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000229 case ISD::UNDEF: {
230 MVT::ValueType VT = Op.getValueType();
231 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000232 default: assert(0 && "This action is not supported yet!");
233 case TargetLowering::Expand:
234 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000235 if (MVT::isInteger(VT))
236 Result = DAG.getConstant(0, VT);
237 else if (MVT::isFloatingPoint(VT))
238 Result = DAG.getConstantFP(0, VT);
239 else
240 assert(0 && "Unknown value type!");
241 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000242 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000243 break;
244 }
245 break;
246 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000247 case ISD::Constant:
248 // We know we don't need to expand constants here, constants only have one
249 // value and we check that it is fine above.
250
251 // FIXME: Maybe we should handle things like targets that don't support full
252 // 32-bit immediates?
253 break;
254 case ISD::ConstantFP: {
255 // Spill FP immediates to the constant pool if the target cannot directly
256 // codegen them. Targets often have some immediate values that can be
257 // efficiently generated into an FP register without a load. We explicitly
258 // leave these constants as ConstantFP nodes for the target to deal with.
259
260 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
261
262 // Check to see if this FP immediate is already legal.
263 bool isLegal = false;
264 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
265 E = TLI.legal_fpimm_end(); I != E; ++I)
266 if (CFP->isExactlyValue(*I)) {
267 isLegal = true;
268 break;
269 }
270
271 if (!isLegal) {
272 // Otherwise we need to spill the constant to memory.
273 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
274
275 bool Extend = false;
276
277 // If a FP immediate is precise when represented as a float, we put it
278 // into the constant pool as a float, even if it's is statically typed
279 // as a double.
280 MVT::ValueType VT = CFP->getValueType(0);
281 bool isDouble = VT == MVT::f64;
282 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
283 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000284 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
285 // Only do this if the target has a native EXTLOAD instruction from
286 // f32.
287 TLI.getOperationAction(ISD::EXTLOAD,
288 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
290 VT = MVT::f32;
291 Extend = true;
292 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000293
Chris Lattner3e928bb2005-01-07 07:47:09 +0000294 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
295 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000296 if (Extend) {
297 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000298 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000299 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000300 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
301 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000302 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000303 }
304 break;
305 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000306 case ISD::TokenFactor: {
307 std::vector<SDOperand> Ops;
308 bool Changed = false;
309 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000310 SDOperand Op = Node->getOperand(i);
311 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000312 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000313 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
314 Changed = true;
315 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
316 Ops.push_back(LegalizeOp(Op.getOperand(j)));
317 } else {
318 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
319 Changed |= Ops[i] != Op;
320 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000321 }
322 if (Changed)
323 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
324 break;
325 }
326
Chris Lattner16cd04d2005-05-12 23:24:06 +0000327 case ISD::CALLSEQ_START:
328 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000330 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000331 Tmp2 = Node->getOperand(0);
332 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000333 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000334
335 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
336 // this will cause the maps used to memoize results to get confused.
337 // Create and add a dummy use, just to increase its use count. This will
338 // be removed at the end of legalize when dead nodes are removed.
339 if (Tmp2.Val->hasOneUse())
340 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
341 DAG.getConstant(0, MVT::i32));
342 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000343 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000344 // nodes are treated specially and are mutated in place. This makes the dag
345 // legalization process more efficient and also makes libcall insertion
346 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000348 case ISD::DYNAMIC_STACKALLOC:
349 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
350 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
351 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
352 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
353 Tmp3 != Node->getOperand(2))
354 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
355 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000356 else
357 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000358
359 // Since this op produces two values, make sure to remember that we
360 // legalized both of them.
361 AddLegalizedOperand(SDOperand(Node, 0), Result);
362 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
363 return Result.getValue(Op.ResNo);
364
Chris Lattnerd71c0412005-05-13 18:43:43 +0000365 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000366 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000367 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
368 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000369
370 bool Changed = false;
371 std::vector<SDOperand> Ops;
372 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
373 Ops.push_back(LegalizeOp(Node->getOperand(i)));
374 Changed |= Ops.back() != Node->getOperand(i);
375 }
376
377 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000378 std::vector<MVT::ValueType> RetTyVTs;
379 RetTyVTs.reserve(Node->getNumValues());
380 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000381 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000382 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
383 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000384 } else {
385 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000386 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000387 // Since calls produce multiple values, make sure to remember that we
388 // legalized all of them.
389 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
390 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
391 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000392 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000393 case ISD::BR:
394 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
395 if (Tmp1 != Node->getOperand(0))
396 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
397 break;
398
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000399 case ISD::BRCOND:
400 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000401
402 switch (getTypeAction(Node->getOperand(1).getValueType())) {
403 case Expand: assert(0 && "It's impossible to expand bools");
404 case Legal:
405 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
406 break;
407 case Promote:
408 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
409 break;
410 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000411 // Basic block destination (Op#2) is always legal.
412 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
413 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
414 Node->getOperand(2));
415 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000416 case ISD::BRCONDTWOWAY:
417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
418 switch (getTypeAction(Node->getOperand(1).getValueType())) {
419 case Expand: assert(0 && "It's impossible to expand bools");
420 case Legal:
421 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
422 break;
423 case Promote:
424 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
425 break;
426 }
427 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
428 // pair.
429 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
430 case TargetLowering::Promote:
431 default: assert(0 && "This action is not supported yet!");
432 case TargetLowering::Legal:
433 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
434 std::vector<SDOperand> Ops;
435 Ops.push_back(Tmp1);
436 Ops.push_back(Tmp2);
437 Ops.push_back(Node->getOperand(2));
438 Ops.push_back(Node->getOperand(3));
439 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
440 }
441 break;
442 case TargetLowering::Expand:
443 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
444 Node->getOperand(2));
445 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
446 break;
447 }
448 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000449
Chris Lattner3e928bb2005-01-07 07:47:09 +0000450 case ISD::LOAD:
451 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
452 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000453
Chris Lattner3e928bb2005-01-07 07:47:09 +0000454 if (Tmp1 != Node->getOperand(0) ||
455 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000456 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
457 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000458 else
459 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000460
Chris Lattner8afc48e2005-01-07 22:28:47 +0000461 // Since loads produce two values, make sure to remember that we legalized
462 // both of them.
463 AddLegalizedOperand(SDOperand(Node, 0), Result);
464 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
465 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000466
Chris Lattner0f69b292005-01-15 06:18:18 +0000467 case ISD::EXTLOAD:
468 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000469 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
471 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000472
Chris Lattner01ff7212005-04-10 22:54:25 +0000473 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
474 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000475 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000476 case TargetLowering::Promote:
477 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
478 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000479 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000480 // Since loads produce two values, make sure to remember that we legalized
481 // both of them.
482 AddLegalizedOperand(SDOperand(Node, 0), Result);
483 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
484 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000485
Chris Lattner01ff7212005-04-10 22:54:25 +0000486 case TargetLowering::Legal:
487 if (Tmp1 != Node->getOperand(0) ||
488 Tmp2 != Node->getOperand(1))
489 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000490 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000491 else
492 Result = SDOperand(Node, 0);
493
494 // Since loads produce two values, make sure to remember that we legalized
495 // both of them.
496 AddLegalizedOperand(SDOperand(Node, 0), Result);
497 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
498 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000499 case TargetLowering::Expand:
500 assert(Node->getOpcode() != ISD::EXTLOAD &&
501 "EXTLOAD should always be supported!");
502 // Turn the unsupported load into an EXTLOAD followed by an explicit
503 // zero/sign extend inreg.
504 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000505 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000506 SDOperand ValRes;
507 if (Node->getOpcode() == ISD::SEXTLOAD)
508 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
509 Result, SrcVT);
510 else
511 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000512 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
513 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
514 if (Op.ResNo)
515 return Result.getValue(1);
516 return ValRes;
517 }
518 assert(0 && "Unreachable");
519 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000520 case ISD::EXTRACT_ELEMENT:
521 // Get both the low and high parts.
522 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
523 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
524 Result = Tmp2; // 1 -> Hi
525 else
526 Result = Tmp1; // 0 -> Lo
527 break;
528
529 case ISD::CopyToReg:
530 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000531
Chris Lattner3e928bb2005-01-07 07:47:09 +0000532 switch (getTypeAction(Node->getOperand(1).getValueType())) {
533 case Legal:
534 // Legalize the incoming value (must be legal).
535 Tmp2 = LegalizeOp(Node->getOperand(1));
536 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000537 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000538 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000539 case Promote:
540 Tmp2 = PromoteOp(Node->getOperand(1));
541 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
542 break;
543 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000544 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000545 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000546 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000547 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
548 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
549 // Note that the copytoreg nodes are independent of each other.
550 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 assert(isTypeLegal(Result.getValueType()) &&
552 "Cannot expand multiple times yet (i64 -> i16)");
553 break;
554 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000555 break;
556
557 case ISD::RET:
558 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
559 switch (Node->getNumOperands()) {
560 case 2: // ret val
561 switch (getTypeAction(Node->getOperand(1).getValueType())) {
562 case Legal:
563 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000564 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000565 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
566 break;
567 case Expand: {
568 SDOperand Lo, Hi;
569 ExpandOp(Node->getOperand(1), Lo, Hi);
570 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000571 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000572 }
573 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000574 Tmp2 = PromoteOp(Node->getOperand(1));
575 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
576 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000577 }
578 break;
579 case 1: // ret void
580 if (Tmp1 != Node->getOperand(0))
581 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
582 break;
583 default: { // ret <values>
584 std::vector<SDOperand> NewValues;
585 NewValues.push_back(Tmp1);
586 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
587 switch (getTypeAction(Node->getOperand(i).getValueType())) {
588 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000589 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000590 break;
591 case Expand: {
592 SDOperand Lo, Hi;
593 ExpandOp(Node->getOperand(i), Lo, Hi);
594 NewValues.push_back(Lo);
595 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000596 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000597 }
598 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000599 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000600 }
601 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
602 break;
603 }
604 }
605 break;
606 case ISD::STORE:
607 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
608 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
609
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000610 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000611 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000612 if (CFP->getValueType(0) == MVT::f32) {
613 union {
614 unsigned I;
615 float F;
616 } V;
617 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000618 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000619 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000620 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000621 } else {
622 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
623 union {
624 uint64_t I;
625 double F;
626 } V;
627 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000628 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000629 DAG.getConstant(V.I, MVT::i64), Tmp2,
630 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000631 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000632 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000633 }
634
Chris Lattner3e928bb2005-01-07 07:47:09 +0000635 switch (getTypeAction(Node->getOperand(1).getValueType())) {
636 case Legal: {
637 SDOperand Val = LegalizeOp(Node->getOperand(1));
638 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
639 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000640 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
641 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000642 break;
643 }
644 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000645 // Truncate the value and store the result.
646 Tmp3 = PromoteOp(Node->getOperand(1));
647 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000648 Node->getOperand(3),
649 Node->getOperand(1).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000650 break;
651
Chris Lattner3e928bb2005-01-07 07:47:09 +0000652 case Expand:
653 SDOperand Lo, Hi;
654 ExpandOp(Node->getOperand(1), Lo, Hi);
655
656 if (!TLI.isLittleEndian())
657 std::swap(Lo, Hi);
658
Chris Lattneredb1add2005-05-11 04:51:16 +0000659 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
660 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000661 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000662 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
663 getIntPtrConstant(IncrementSize));
664 assert(isTypeLegal(Tmp2.getValueType()) &&
665 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000666 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000667 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
668 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000669 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
670 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000671 }
672 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000673 case ISD::PCMARKER:
674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000675 if (Tmp1 != Node->getOperand(0))
676 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000677 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000678 case ISD::TRUNCSTORE:
679 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
680 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
681
682 switch (getTypeAction(Node->getOperand(1).getValueType())) {
683 case Legal:
684 Tmp2 = LegalizeOp(Node->getOperand(1));
685 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
686 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000687 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000688 Node->getOperand(3),
Chris Lattner0f69b292005-01-15 06:18:18 +0000689 cast<MVTSDNode>(Node)->getExtraValueType());
690 break;
691 case Promote:
692 case Expand:
693 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
694 }
695 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000696 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000697 switch (getTypeAction(Node->getOperand(0).getValueType())) {
698 case Expand: assert(0 && "It's impossible to expand bools");
699 case Legal:
700 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
701 break;
702 case Promote:
703 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
704 break;
705 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000706 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000707 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000708
709 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
710 default: assert(0 && "This action is not supported yet!");
711 case TargetLowering::Legal:
712 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
713 Tmp3 != Node->getOperand(2))
714 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
715 Tmp1, Tmp2, Tmp3);
716 break;
717 case TargetLowering::Promote: {
718 MVT::ValueType NVT =
719 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
720 unsigned ExtOp, TruncOp;
721 if (MVT::isInteger(Tmp2.getValueType())) {
722 ExtOp = ISD::ZERO_EXTEND;
723 TruncOp = ISD::TRUNCATE;
724 } else {
725 ExtOp = ISD::FP_EXTEND;
726 TruncOp = ISD::FP_ROUND;
727 }
728 // Promote each of the values to the new type.
729 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
730 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
731 // Perform the larger operation, then round down.
732 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
733 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
734 break;
735 }
736 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000737 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000738 case ISD::SETCC:
739 switch (getTypeAction(Node->getOperand(0).getValueType())) {
740 case Legal:
741 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
742 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
743 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
744 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000745 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000746 break;
747 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000748 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
749 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
750
751 // If this is an FP compare, the operands have already been extended.
752 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
753 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000754 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000755
756 // Otherwise, we have to insert explicit sign or zero extends. Note
757 // that we could insert sign extends for ALL conditions, but zero extend
758 // is cheaper on many machines (an AND instead of two shifts), so prefer
759 // it.
760 switch (cast<SetCCSDNode>(Node)->getCondition()) {
761 default: assert(0 && "Unknown integer comparison!");
762 case ISD::SETEQ:
763 case ISD::SETNE:
764 case ISD::SETUGE:
765 case ISD::SETUGT:
766 case ISD::SETULE:
767 case ISD::SETULT:
768 // ALL of these operations will work if we either sign or zero extend
769 // the operands (including the unsigned comparisons!). Zero extend is
770 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000771 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
772 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000773 break;
774 case ISD::SETGE:
775 case ISD::SETGT:
776 case ISD::SETLT:
777 case ISD::SETLE:
778 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
779 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
780 break;
781 }
782
783 }
784 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000785 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000786 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000787 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
789 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
790 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
791 switch (cast<SetCCSDNode>(Node)->getCondition()) {
792 case ISD::SETEQ:
793 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000794 if (RHSLo == RHSHi)
795 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
796 if (RHSCST->isAllOnesValue()) {
797 // Comparison to -1.
798 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000799 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000800 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000801 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000802 }
803
Chris Lattner3e928bb2005-01-07 07:47:09 +0000804 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
805 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
806 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000807 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000808 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000809 DAG.getConstant(0, Tmp1.getValueType()));
810 break;
811 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000812 // If this is a comparison of the sign bit, just look at the top part.
813 // X > -1, x < 0
814 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000815 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000816 CST->getValue() == 0) || // X < 0
817 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
818 (CST->isAllOnesValue()))) // X > -1
819 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
820 Node->getValueType(0), LHSHi, RHSHi);
821
Chris Lattner3e928bb2005-01-07 07:47:09 +0000822 // FIXME: This generated code sucks.
823 ISD::CondCode LowCC;
824 switch (cast<SetCCSDNode>(Node)->getCondition()) {
825 default: assert(0 && "Unknown integer setcc!");
826 case ISD::SETLT:
827 case ISD::SETULT: LowCC = ISD::SETULT; break;
828 case ISD::SETGT:
829 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
830 case ISD::SETLE:
831 case ISD::SETULE: LowCC = ISD::SETULE; break;
832 case ISD::SETGE:
833 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
834 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000835
Chris Lattner3e928bb2005-01-07 07:47:09 +0000836 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
837 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
838 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
839
840 // NOTE: on targets without efficient SELECT of bools, we can always use
841 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000842 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000843 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000844 Node->getValueType(0), LHSHi, RHSHi);
845 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
846 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
847 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000848 break;
849 }
850 }
851 break;
852
Chris Lattnere1bd8222005-01-11 05:57:22 +0000853 case ISD::MEMSET:
854 case ISD::MEMCPY:
855 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000856 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000857 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
858
859 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
860 switch (getTypeAction(Node->getOperand(2).getValueType())) {
861 case Expand: assert(0 && "Cannot expand a byte!");
862 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000863 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000864 break;
865 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000866 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000867 break;
868 }
869 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000870 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000871 }
Chris Lattner272455b2005-02-02 03:44:41 +0000872
873 SDOperand Tmp4;
874 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000875 case Expand: assert(0 && "Cannot expand this yet!");
876 case Legal:
877 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000878 break;
879 case Promote:
880 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000881 break;
882 }
883
884 SDOperand Tmp5;
885 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
886 case Expand: assert(0 && "Cannot expand this yet!");
887 case Legal:
888 Tmp5 = LegalizeOp(Node->getOperand(4));
889 break;
890 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000891 Tmp5 = PromoteOp(Node->getOperand(4));
892 break;
893 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000894
895 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
896 default: assert(0 && "This action not implemented for this operation!");
897 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000898 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
899 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
900 Tmp5 != Node->getOperand(4)) {
901 std::vector<SDOperand> Ops;
902 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
903 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
904 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
905 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000906 break;
907 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000908 // Otherwise, the target does not support this operation. Lower the
909 // operation to an explicit libcall as appropriate.
910 MVT::ValueType IntPtr = TLI.getPointerTy();
911 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
912 std::vector<std::pair<SDOperand, const Type*> > Args;
913
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000914 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000915 if (Node->getOpcode() == ISD::MEMSET) {
916 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
917 // Extend the ubyte argument to be an int value for the call.
918 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
919 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
920 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
921
922 FnName = "memset";
923 } else if (Node->getOpcode() == ISD::MEMCPY ||
924 Node->getOpcode() == ISD::MEMMOVE) {
925 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
926 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
927 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
928 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
929 } else {
930 assert(0 && "Unknown op!");
931 }
Chris Lattner45982da2005-05-12 16:53:42 +0000932
Chris Lattnere1bd8222005-01-11 05:57:22 +0000933 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +0000934 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000935 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
936 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000937 break;
938 }
939 case TargetLowering::Custom:
940 std::vector<SDOperand> Ops;
941 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
942 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
943 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
944 Result = TLI.LowerOperation(Result);
945 Result = LegalizeOp(Result);
946 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000947 }
948 break;
949 }
Chris Lattner52d08bd2005-05-09 20:23:03 +0000950
951 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000952 Tmp1 = LegalizeOp(Node->getOperand(0));
953 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000954
Chris Lattner52d08bd2005-05-09 20:23:03 +0000955 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000956 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000957 else
958 Result = SDOperand(Node, 0);
959 // Since these produce two values, make sure to remember that we legalized
960 // both of them.
961 AddLegalizedOperand(SDOperand(Node, 0), Result);
962 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
963 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000964 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000965 Tmp1 = LegalizeOp(Node->getOperand(0));
966 Tmp2 = LegalizeOp(Node->getOperand(1));
967 Tmp3 = LegalizeOp(Node->getOperand(2));
968 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
969 Tmp3 != Node->getOperand(2))
970 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
971 break;
972
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000973 case ISD::READIO:
974 Tmp1 = LegalizeOp(Node->getOperand(0));
975 Tmp2 = LegalizeOp(Node->getOperand(1));
976
977 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
978 case TargetLowering::Custom:
979 default: assert(0 && "This action not implemented for this operation!");
980 case TargetLowering::Legal:
981 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
982 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
983 Tmp1, Tmp2);
984 else
985 Result = SDOperand(Node, 0);
986 break;
987 case TargetLowering::Expand:
988 // Replace this with a load from memory.
989 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
990 Node->getOperand(1), DAG.getSrcValue(NULL));
991 Result = LegalizeOp(Result);
992 break;
993 }
994
995 // Since these produce two values, make sure to remember that we legalized
996 // both of them.
997 AddLegalizedOperand(SDOperand(Node, 0), Result);
998 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
999 return Result.getValue(Op.ResNo);
1000
1001 case ISD::WRITEIO:
1002 Tmp1 = LegalizeOp(Node->getOperand(0));
1003 Tmp2 = LegalizeOp(Node->getOperand(1));
1004 Tmp3 = LegalizeOp(Node->getOperand(2));
1005
1006 switch (TLI.getOperationAction(Node->getOpcode(),
1007 Node->getOperand(1).getValueType())) {
1008 case TargetLowering::Custom:
1009 default: assert(0 && "This action not implemented for this operation!");
1010 case TargetLowering::Legal:
1011 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1012 Tmp3 != Node->getOperand(2))
1013 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1014 break;
1015 case TargetLowering::Expand:
1016 // Replace this with a store to memory.
1017 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1018 Node->getOperand(1), Node->getOperand(2),
1019 DAG.getSrcValue(NULL));
1020 Result = LegalizeOp(Result);
1021 break;
1022 }
1023 break;
1024
Chris Lattner84f67882005-01-20 18:52:28 +00001025 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001026 case ISD::SUB_PARTS:
1027 case ISD::SHL_PARTS:
1028 case ISD::SRA_PARTS:
1029 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001030 std::vector<SDOperand> Ops;
1031 bool Changed = false;
1032 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1033 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1034 Changed |= Ops.back() != Node->getOperand(i);
1035 }
1036 if (Changed)
1037 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001038
1039 // Since these produce multiple values, make sure to remember that we
1040 // legalized all of them.
1041 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1042 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1043 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001044 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001045
1046 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001047 case ISD::ADD:
1048 case ISD::SUB:
1049 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001050 case ISD::MULHS:
1051 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001052 case ISD::UDIV:
1053 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001054 case ISD::AND:
1055 case ISD::OR:
1056 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001057 case ISD::SHL:
1058 case ISD::SRL:
1059 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001060 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1061 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1062 if (Tmp1 != Node->getOperand(0) ||
1063 Tmp2 != Node->getOperand(1))
1064 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1065 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001066
Nate Begemanc105e192005-04-06 00:23:54 +00001067 case ISD::UREM:
1068 case ISD::SREM:
1069 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1070 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1071 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1072 case TargetLowering::Legal:
1073 if (Tmp1 != Node->getOperand(0) ||
1074 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001075 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001076 Tmp2);
1077 break;
1078 case TargetLowering::Promote:
1079 case TargetLowering::Custom:
1080 assert(0 && "Cannot promote/custom handle this yet!");
1081 case TargetLowering::Expand: {
1082 MVT::ValueType VT = Node->getValueType(0);
1083 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1084 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1085 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1086 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1087 }
1088 break;
1089 }
1090 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001091
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001092 case ISD::CTPOP:
1093 case ISD::CTTZ:
1094 case ISD::CTLZ:
1095 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1096 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1097 case TargetLowering::Legal:
1098 if (Tmp1 != Node->getOperand(0))
1099 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1100 break;
1101 case TargetLowering::Promote: {
1102 MVT::ValueType OVT = Tmp1.getValueType();
1103 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001104
1105 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001106 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1107 // Perform the larger operation, then subtract if needed.
1108 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1109 switch(Node->getOpcode())
1110 {
1111 case ISD::CTPOP:
1112 Result = Tmp1;
1113 break;
1114 case ISD::CTTZ:
1115 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner39a8f332005-05-12 19:05:01 +00001116 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001117 DAG.getConstant(getSizeInBits(NVT), NVT));
1118 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1119 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1120 break;
1121 case ISD::CTLZ:
1122 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1123 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1124 DAG.getConstant(getSizeInBits(NVT) -
1125 getSizeInBits(OVT), NVT));
1126 break;
1127 }
1128 break;
1129 }
1130 case TargetLowering::Custom:
1131 assert(0 && "Cannot custom handle this yet!");
1132 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001133 switch(Node->getOpcode())
1134 {
1135 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001136 static const uint64_t mask[6] = {
1137 0x5555555555555555ULL, 0x3333333333333333ULL,
1138 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1139 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1140 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001141 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001142 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1143 unsigned len = getSizeInBits(VT);
1144 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001145 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001146 Tmp2 = DAG.getConstant(mask[i], VT);
1147 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001148 Tmp1 = DAG.getNode(ISD::ADD, VT,
1149 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1150 DAG.getNode(ISD::AND, VT,
1151 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1152 Tmp2));
1153 }
1154 Result = Tmp1;
1155 break;
1156 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001157 case ISD::CTLZ: {
1158 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001159 x = x | (x >> 1);
1160 x = x | (x >> 2);
1161 ...
1162 x = x | (x >>16);
1163 x = x | (x >>32); // for 64-bit input
1164 return popcount(~x);
Duraid Madina57ff7e52005-05-11 08:45:08 +00001165
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001166 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1167 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001168 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1169 unsigned len = getSizeInBits(VT);
1170 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1171 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1172 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1173 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1174 }
1175 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001176 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001177 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001178 }
1179 case ISD::CTTZ: {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001180 // for now, we use: { return popcount(~x & (x - 1)); }
1181 // unless the target has ctlz but not ctpop, in which case we use:
1182 // { return 32 - nlz(~x & (x-1)); }
1183 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001184 MVT::ValueType VT = Tmp1.getValueType();
1185 Tmp2 = DAG.getConstant(~0ULL, VT);
1186 Tmp3 = DAG.getNode(ISD::AND, VT,
1187 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1188 DAG.getNode(ISD::SUB, VT, Tmp1,
1189 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001190 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1191 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1192 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1193 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1194 DAG.getConstant(getSizeInBits(VT), VT),
1195 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1196 } else {
1197 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1198 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001199 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001200 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001201 default:
1202 assert(0 && "Cannot expand this yet!");
1203 break;
1204 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001205 break;
1206 }
1207 break;
1208
Chris Lattner2c8086f2005-04-02 05:00:07 +00001209 // Unary operators
1210 case ISD::FABS:
1211 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001212 case ISD::FSQRT:
1213 case ISD::FSIN:
1214 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001215 Tmp1 = LegalizeOp(Node->getOperand(0));
1216 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1217 case TargetLowering::Legal:
1218 if (Tmp1 != Node->getOperand(0))
1219 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1220 break;
1221 case TargetLowering::Promote:
1222 case TargetLowering::Custom:
1223 assert(0 && "Cannot promote/custom handle this yet!");
1224 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001225 switch(Node->getOpcode()) {
1226 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001227 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1228 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1229 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1230 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001231 break;
1232 }
1233 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001234 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1235 MVT::ValueType VT = Node->getValueType(0);
1236 Tmp2 = DAG.getConstantFP(0.0, VT);
1237 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1238 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1239 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1240 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001241 break;
1242 }
1243 case ISD::FSQRT:
1244 case ISD::FSIN:
1245 case ISD::FCOS: {
1246 MVT::ValueType VT = Node->getValueType(0);
1247 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1248 const char *FnName = 0;
1249 switch(Node->getOpcode()) {
1250 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1251 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1252 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1253 default: assert(0 && "Unreachable!");
1254 }
1255 std::vector<std::pair<SDOperand, const Type*> > Args;
1256 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001257 // FIXME: should use ExpandLibCall!
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001258 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001259 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001260 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1261 Result = LegalizeOp(CallResult.first);
1262 break;
1263 }
1264 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001265 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001266 }
1267 break;
1268 }
1269 break;
1270
1271 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001272 case ISD::ZERO_EXTEND:
1273 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001274 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001275 case ISD::FP_EXTEND:
1276 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001277 case ISD::FP_TO_SINT:
1278 case ISD::FP_TO_UINT:
1279 case ISD::SINT_TO_FP:
1280 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001281 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1282 case Legal:
1283 Tmp1 = LegalizeOp(Node->getOperand(0));
1284 if (Tmp1 != Node->getOperand(0))
1285 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1286 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001287 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001288 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1289 Node->getOpcode() == ISD::UINT_TO_FP) {
1290 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1291 Node->getValueType(0), Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001292 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001293 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1294 // In the expand case, we must be dealing with a truncate, because
1295 // otherwise the result would be larger than the source.
1296 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001297
Chris Lattner2c8086f2005-04-02 05:00:07 +00001298 // Since the result is legal, we should just be able to truncate the low
1299 // part of the source.
1300 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1301 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001302 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001303 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001304
Chris Lattner03c85462005-01-15 05:21:40 +00001305 case Promote:
1306 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001307 case ISD::ZERO_EXTEND:
1308 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001309 // NOTE: Any extend would work here...
1310 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001311 Result = DAG.getZeroExtendInReg(Result,
1312 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001313 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001314 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001315 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001316 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001317 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001318 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1319 Result, Node->getOperand(0).getValueType());
1320 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001321 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001322 Result = PromoteOp(Node->getOperand(0));
1323 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1324 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001325 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001326 Result = PromoteOp(Node->getOperand(0));
1327 if (Result.getValueType() != Op.getValueType())
1328 // Dynamically dead while we have only 2 FP types.
1329 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1330 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001331 case ISD::FP_ROUND:
1332 case ISD::FP_TO_SINT:
1333 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001334 Result = PromoteOp(Node->getOperand(0));
1335 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1336 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001337 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001338 Result = PromoteOp(Node->getOperand(0));
1339 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1340 Result, Node->getOperand(0).getValueType());
1341 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1342 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001343 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001344 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001345 Result = DAG.getZeroExtendInReg(Result,
1346 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001347 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1348 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001349 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001350 }
1351 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001352 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001353 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001354 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001355 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1356
1357 // If this operation is not supported, convert it to a shl/shr or load/store
1358 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001359 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1360 default: assert(0 && "This action not supported for this op yet!");
1361 case TargetLowering::Legal:
1362 if (Tmp1 != Node->getOperand(0))
1363 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1364 ExtraVT);
1365 break;
1366 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001367 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001368 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001369 // NOTE: we could fall back on load/store here too for targets without
1370 // SAR. However, it is doubtful that any exist.
1371 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1372 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001373 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001374 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1375 Node->getOperand(0), ShiftCst);
1376 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1377 Result, ShiftCst);
1378 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1379 // The only way we can lower this is to turn it into a STORETRUNC,
1380 // EXTLOAD pair, targetting a temporary location (a stack slot).
1381
1382 // NOTE: there is a choice here between constantly creating new stack
1383 // slots and always reusing the same one. We currently always create
1384 // new ones, as reuse may inhibit scheduling.
1385 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1386 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1387 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1388 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001389 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001390 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1391 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1392 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001393 Node->getOperand(0), StackSlot,
1394 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001395 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001396 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001397 } else {
1398 assert(0 && "Unknown op");
1399 }
1400 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001401 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001402 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001403 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001405 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001406
Chris Lattner45982da2005-05-12 16:53:42 +00001407 // Note that LegalizeOp may be reentered even from single-use nodes, which
1408 // means that we always must cache transformed nodes.
1409 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001410 return Result;
1411}
1412
Chris Lattner8b6fa222005-01-15 22:16:26 +00001413/// PromoteOp - Given an operation that produces a value in an invalid type,
1414/// promote it to compute the value into a larger type. The produced value will
1415/// have the correct bits for the low portion of the register, but no guarantee
1416/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001417SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1418 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001419 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001420 assert(getTypeAction(VT) == Promote &&
1421 "Caller should expand or legalize operands that are not promotable!");
1422 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1423 "Cannot promote to smaller type!");
1424
Chris Lattner03c85462005-01-15 05:21:40 +00001425 SDOperand Tmp1, Tmp2, Tmp3;
1426
1427 SDOperand Result;
1428 SDNode *Node = Op.Val;
1429
Chris Lattner45982da2005-05-12 16:53:42 +00001430 if (!Node->hasOneUse()) {
1431 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1432 if (I != PromotedNodes.end()) return I->second;
1433 } else {
1434 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1435 }
1436
Chris Lattner0f69b292005-01-15 06:18:18 +00001437 // Promotion needs an optimization step to clean up after it, and is not
1438 // careful to avoid operations the target does not support. Make sure that
1439 // all generated operations are legalized in the next iteration.
1440 NeedsAnotherIteration = true;
1441
Chris Lattner03c85462005-01-15 05:21:40 +00001442 switch (Node->getOpcode()) {
1443 default:
1444 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1445 assert(0 && "Do not know how to promote this operator!");
1446 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001447 case ISD::UNDEF:
1448 Result = DAG.getNode(ISD::UNDEF, NVT);
1449 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001450 case ISD::Constant:
1451 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1452 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1453 break;
1454 case ISD::ConstantFP:
1455 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1456 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1457 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001458 case ISD::CopyFromReg:
1459 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1460 Node->getOperand(0));
1461 // Remember that we legalized the chain.
1462 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1463 break;
1464
Chris Lattner82fbfb62005-01-18 02:59:52 +00001465 case ISD::SETCC:
1466 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1467 "SetCC type is not legal??");
1468 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1469 TLI.getSetCCResultTy(), Node->getOperand(0),
1470 Node->getOperand(1));
1471 Result = LegalizeOp(Result);
1472 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001473
1474 case ISD::TRUNCATE:
1475 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1476 case Legal:
1477 Result = LegalizeOp(Node->getOperand(0));
1478 assert(Result.getValueType() >= NVT &&
1479 "This truncation doesn't make sense!");
1480 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1481 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1482 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001483 case Promote:
1484 // The truncation is not required, because we don't guarantee anything
1485 // about high bits anyway.
1486 Result = PromoteOp(Node->getOperand(0));
1487 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001488 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001489 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1490 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001491 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001492 }
1493 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001494 case ISD::SIGN_EXTEND:
1495 case ISD::ZERO_EXTEND:
1496 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1497 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1498 case Legal:
1499 // Input is legal? Just do extend all the way to the larger type.
1500 Result = LegalizeOp(Node->getOperand(0));
1501 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1502 break;
1503 case Promote:
1504 // Promote the reg if it's smaller.
1505 Result = PromoteOp(Node->getOperand(0));
1506 // The high bits are not guaranteed to be anything. Insert an extend.
1507 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001508 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1509 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001510 else
Chris Lattner23993562005-04-13 02:38:47 +00001511 Result = DAG.getZeroExtendInReg(Result,
1512 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001513 break;
1514 }
1515 break;
1516
1517 case ISD::FP_EXTEND:
1518 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1519 case ISD::FP_ROUND:
1520 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1521 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1522 case Promote: assert(0 && "Unreachable with 2 FP types!");
1523 case Legal:
1524 // Input is legal? Do an FP_ROUND_INREG.
1525 Result = LegalizeOp(Node->getOperand(0));
1526 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1527 break;
1528 }
1529 break;
1530
1531 case ISD::SINT_TO_FP:
1532 case ISD::UINT_TO_FP:
1533 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1534 case Legal:
1535 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001536 // No extra round required here.
1537 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001538 break;
1539
1540 case Promote:
1541 Result = PromoteOp(Node->getOperand(0));
1542 if (Node->getOpcode() == ISD::SINT_TO_FP)
1543 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1544 Result, Node->getOperand(0).getValueType());
1545 else
Chris Lattner23993562005-04-13 02:38:47 +00001546 Result = DAG.getZeroExtendInReg(Result,
1547 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001548 // No extra round required here.
1549 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001550 break;
1551 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001552 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1553 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001554 // Round if we cannot tolerate excess precision.
1555 if (NoExcessFPPrecision)
1556 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1557 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001558 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001559 break;
1560
1561 case ISD::FP_TO_SINT:
1562 case ISD::FP_TO_UINT:
1563 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1564 case Legal:
1565 Tmp1 = LegalizeOp(Node->getOperand(0));
1566 break;
1567 case Promote:
1568 // The input result is prerounded, so we don't have to do anything
1569 // special.
1570 Tmp1 = PromoteOp(Node->getOperand(0));
1571 break;
1572 case Expand:
1573 assert(0 && "not implemented");
1574 }
1575 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1576 break;
1577
Chris Lattner2c8086f2005-04-02 05:00:07 +00001578 case ISD::FABS:
1579 case ISD::FNEG:
1580 Tmp1 = PromoteOp(Node->getOperand(0));
1581 assert(Tmp1.getValueType() == NVT);
1582 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1583 // NOTE: we do not have to do any extra rounding here for
1584 // NoExcessFPPrecision, because we know the input will have the appropriate
1585 // precision, and these operations don't modify precision at all.
1586 break;
1587
Chris Lattnerda6ba872005-04-28 21:44:33 +00001588 case ISD::FSQRT:
1589 case ISD::FSIN:
1590 case ISD::FCOS:
1591 Tmp1 = PromoteOp(Node->getOperand(0));
1592 assert(Tmp1.getValueType() == NVT);
1593 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1594 if(NoExcessFPPrecision)
1595 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1596 break;
1597
Chris Lattner03c85462005-01-15 05:21:40 +00001598 case ISD::AND:
1599 case ISD::OR:
1600 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001601 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001602 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001603 case ISD::MUL:
1604 // The input may have strange things in the top bits of the registers, but
1605 // these operations don't care. They may have wierd bits going out, but
1606 // that too is okay if they are integer operations.
1607 Tmp1 = PromoteOp(Node->getOperand(0));
1608 Tmp2 = PromoteOp(Node->getOperand(1));
1609 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1610 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1611
1612 // However, if this is a floating point operation, they will give excess
1613 // precision that we may not be able to tolerate. If we DO allow excess
1614 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001615 // FIXME: Why would we need to round FP ops more than integer ones?
1616 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001617 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1618 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1619 break;
1620
Chris Lattner8b6fa222005-01-15 22:16:26 +00001621 case ISD::SDIV:
1622 case ISD::SREM:
1623 // These operators require that their input be sign extended.
1624 Tmp1 = PromoteOp(Node->getOperand(0));
1625 Tmp2 = PromoteOp(Node->getOperand(1));
1626 if (MVT::isInteger(NVT)) {
1627 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001628 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001629 }
1630 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1631
1632 // Perform FP_ROUND: this is probably overly pessimistic.
1633 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1634 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1635 break;
1636
1637 case ISD::UDIV:
1638 case ISD::UREM:
1639 // These operators require that their input be zero extended.
1640 Tmp1 = PromoteOp(Node->getOperand(0));
1641 Tmp2 = PromoteOp(Node->getOperand(1));
1642 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001643 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1644 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001645 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1646 break;
1647
1648 case ISD::SHL:
1649 Tmp1 = PromoteOp(Node->getOperand(0));
1650 Tmp2 = LegalizeOp(Node->getOperand(1));
1651 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1652 break;
1653 case ISD::SRA:
1654 // The input value must be properly sign extended.
1655 Tmp1 = PromoteOp(Node->getOperand(0));
1656 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1657 Tmp2 = LegalizeOp(Node->getOperand(1));
1658 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1659 break;
1660 case ISD::SRL:
1661 // The input value must be properly zero extended.
1662 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001663 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001664 Tmp2 = LegalizeOp(Node->getOperand(1));
1665 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1666 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001667 case ISD::LOAD:
1668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1669 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001670 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001671 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001672 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1673 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001674 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001675 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1676 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001677
1678 // Remember that we legalized the chain.
1679 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1680 break;
1681 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001682 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1683 case Expand: assert(0 && "It's impossible to expand bools");
1684 case Legal:
1685 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1686 break;
1687 case Promote:
1688 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1689 break;
1690 }
Chris Lattner03c85462005-01-15 05:21:40 +00001691 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1692 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1693 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1694 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00001695 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00001696 case ISD::CALL: {
1697 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1698 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1699
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001700 std::vector<SDOperand> Ops;
1701 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1702 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1703
Chris Lattner8ac532c2005-01-16 19:46:48 +00001704 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1705 "Can only promote single result calls");
1706 std::vector<MVT::ValueType> RetTyVTs;
1707 RetTyVTs.reserve(2);
1708 RetTyVTs.push_back(NVT);
1709 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00001710 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1711 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001712 Result = SDOperand(NC, 0);
1713
1714 // Insert the new chain mapping.
1715 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1716 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001717 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001718 case ISD::CTPOP:
1719 case ISD::CTTZ:
1720 case ISD::CTLZ:
1721 Tmp1 = Node->getOperand(0);
1722 //Zero extend the argument
1723 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1724 // Perform the larger operation, then subtract if needed.
1725 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1726 switch(Node->getOpcode())
1727 {
1728 case ISD::CTPOP:
1729 Result = Tmp1;
1730 break;
1731 case ISD::CTTZ:
1732 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1733 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1734 DAG.getConstant(getSizeInBits(NVT), NVT));
1735 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1736 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1737 break;
1738 case ISD::CTLZ:
1739 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1740 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1741 DAG.getConstant(getSizeInBits(NVT) -
1742 getSizeInBits(VT), NVT));
1743 break;
1744 }
1745 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001746 }
1747
1748 assert(Result.Val && "Didn't set a result!");
1749 AddPromotedOperand(Op, Result);
1750 return Result;
1751}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001752
Chris Lattner84f67882005-01-20 18:52:28 +00001753/// ExpandAddSub - Find a clever way to expand this add operation into
1754/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001755void SelectionDAGLegalize::
1756ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1757 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001758 // Expand the subcomponents.
1759 SDOperand LHSL, LHSH, RHSL, RHSH;
1760 ExpandOp(LHS, LHSL, LHSH);
1761 ExpandOp(RHS, RHSL, RHSH);
1762
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001763 // FIXME: this should be moved to the dag combiner someday.
1764 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1765 if (LHSL.getValueType() == MVT::i32) {
1766 SDOperand LowEl;
1767 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1768 if (C->getValue() == 0)
1769 LowEl = RHSL;
1770 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1771 if (C->getValue() == 0)
1772 LowEl = LHSL;
1773 if (LowEl.Val) {
1774 // Turn this into an add/sub of the high part only.
1775 SDOperand HiEl =
1776 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1777 LowEl.getValueType(), LHSH, RHSH);
1778 Lo = LowEl;
1779 Hi = HiEl;
1780 return;
1781 }
1782 }
1783
Chris Lattner84f67882005-01-20 18:52:28 +00001784 std::vector<SDOperand> Ops;
1785 Ops.push_back(LHSL);
1786 Ops.push_back(LHSH);
1787 Ops.push_back(RHSL);
1788 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001789 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001790 Hi = Lo.getValue(1);
1791}
1792
Chris Lattner5b359c62005-04-02 04:00:59 +00001793void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1794 SDOperand Op, SDOperand Amt,
1795 SDOperand &Lo, SDOperand &Hi) {
1796 // Expand the subcomponents.
1797 SDOperand LHSL, LHSH;
1798 ExpandOp(Op, LHSL, LHSH);
1799
1800 std::vector<SDOperand> Ops;
1801 Ops.push_back(LHSL);
1802 Ops.push_back(LHSH);
1803 Ops.push_back(Amt);
1804 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1805 Hi = Lo.getValue(1);
1806}
1807
1808
Chris Lattnere34b3962005-01-19 04:19:40 +00001809/// ExpandShift - Try to find a clever way to expand this shift operation out to
1810/// smaller elements. If we can't find a way that is more efficient than a
1811/// libcall on this target, return false. Otherwise, return true with the
1812/// low-parts expanded into Lo and Hi.
1813bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1814 SDOperand &Lo, SDOperand &Hi) {
1815 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1816 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001817
Chris Lattnere34b3962005-01-19 04:19:40 +00001818 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001819 SDOperand ShAmt = LegalizeOp(Amt);
1820 MVT::ValueType ShTy = ShAmt.getValueType();
1821 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1822 unsigned NVTBits = MVT::getSizeInBits(NVT);
1823
1824 // Handle the case when Amt is an immediate. Other cases are currently broken
1825 // and are disabled.
1826 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1827 unsigned Cst = CN->getValue();
1828 // Expand the incoming operand to be shifted, so that we have its parts
1829 SDOperand InL, InH;
1830 ExpandOp(Op, InL, InH);
1831 switch(Opc) {
1832 case ISD::SHL:
1833 if (Cst > VTBits) {
1834 Lo = DAG.getConstant(0, NVT);
1835 Hi = DAG.getConstant(0, NVT);
1836 } else if (Cst > NVTBits) {
1837 Lo = DAG.getConstant(0, NVT);
1838 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001839 } else if (Cst == NVTBits) {
1840 Lo = DAG.getConstant(0, NVT);
1841 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001842 } else {
1843 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1844 Hi = DAG.getNode(ISD::OR, NVT,
1845 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1846 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1847 }
1848 return true;
1849 case ISD::SRL:
1850 if (Cst > VTBits) {
1851 Lo = DAG.getConstant(0, NVT);
1852 Hi = DAG.getConstant(0, NVT);
1853 } else if (Cst > NVTBits) {
1854 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1855 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001856 } else if (Cst == NVTBits) {
1857 Lo = InH;
1858 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001859 } else {
1860 Lo = DAG.getNode(ISD::OR, NVT,
1861 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1862 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1863 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1864 }
1865 return true;
1866 case ISD::SRA:
1867 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001868 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001869 DAG.getConstant(NVTBits-1, ShTy));
1870 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001871 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001872 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001873 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001874 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001875 } else if (Cst == NVTBits) {
1876 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001877 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001878 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001879 } else {
1880 Lo = DAG.getNode(ISD::OR, NVT,
1881 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1882 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1883 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1884 }
1885 return true;
1886 }
1887 }
1888 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1889 // so disable it for now. Currently targets are handling this via SHL_PARTS
1890 // and friends.
1891 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001892
1893 // If we have an efficient select operation (or if the selects will all fold
1894 // away), lower to some complex code, otherwise just emit the libcall.
1895 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1896 !isa<ConstantSDNode>(Amt))
1897 return false;
1898
1899 SDOperand InL, InH;
1900 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001901 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1902 DAG.getConstant(NVTBits, ShTy), ShAmt);
1903
Chris Lattnere5544f82005-01-20 20:29:23 +00001904 // Compare the unmasked shift amount against 32.
1905 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1906 DAG.getConstant(NVTBits, ShTy));
1907
Chris Lattnere34b3962005-01-19 04:19:40 +00001908 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1909 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1910 DAG.getConstant(NVTBits-1, ShTy));
1911 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1912 DAG.getConstant(NVTBits-1, ShTy));
1913 }
1914
1915 if (Opc == ISD::SHL) {
1916 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1917 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1918 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001919 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001920
Chris Lattnere34b3962005-01-19 04:19:40 +00001921 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1922 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1923 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001924 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1925 DAG.getSetCC(ISD::SETEQ,
1926 TLI.getSetCCResultTy(), NAmt,
1927 DAG.getConstant(32, ShTy)),
1928 DAG.getConstant(0, NVT),
1929 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001930 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001931 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001932 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001933 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001934
1935 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001936 if (Opc == ISD::SRA)
1937 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1938 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001939 else
1940 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001941 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001942 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001943 }
1944 return true;
1945}
Chris Lattner77e77a62005-01-21 06:05:23 +00001946
Chris Lattner9530ddc2005-05-13 05:09:11 +00001947/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
1948/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001949/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001950static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001951 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1952
Chris Lattner16cd04d2005-05-12 23:24:06 +00001953 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001954 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00001955 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001956 Found = Node;
1957 return;
1958 }
1959
1960 // Otherwise, scan the operands of Node to see if any of them is a call.
1961 assert(Node->getNumOperands() != 0 &&
1962 "All leaves should have depth equal to the entry node!");
1963 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00001964 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001965
1966 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001967 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001968 Found);
1969}
1970
1971
Chris Lattner9530ddc2005-05-13 05:09:11 +00001972/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
1973/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001974/// than Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001975static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001976 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1977
Chris Lattner16cd04d2005-05-12 23:24:06 +00001978 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001979 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00001980 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001981 Found = Node;
1982 return;
1983 }
1984
1985 // Otherwise, scan the operands of Node to see if any of them is a call.
1986 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1987 if (UI == E) return;
1988 for (--E; UI != E; ++UI)
Chris Lattner9530ddc2005-05-13 05:09:11 +00001989 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001990
1991 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001992 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001993}
1994
Chris Lattner9530ddc2005-05-13 05:09:11 +00001995/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00001996/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001997static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00001998 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001999 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002000 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002001 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002002
2003 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002004 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002005
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002006 SDOperand TheChain(Node, Node->getNumValues()-1);
2007 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002008
2009 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002010 E = Node->use_end(); ; ++UI) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002011 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002012
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002013 // Make sure to only follow users of our token chain.
2014 SDNode *User = *UI;
2015 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2016 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002017 if (SDNode *Result = FindCallSeqEnd(User))
2018 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002019 }
2020 assert(0 && "Unreachable");
2021 abort();
2022}
2023
Chris Lattner9530ddc2005-05-13 05:09:11 +00002024/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002025/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002026static SDNode *FindCallSeqStart(SDNode *Node) {
2027 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002028 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002029
2030 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2031 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002032 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002033}
2034
2035
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002036/// FindInputOutputChains - If we are replacing an operation with a call we need
2037/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002038/// properly serialize the calls in the block. The returned operand is the
2039/// input chain value for the new call (e.g. the entry node or the previous
2040/// call), and OutChain is set to be the chain node to update to point to the
2041/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002042static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2043 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002044 SDNode *LatestCallSeqStart = Entry.Val;
2045 SDNode *LatestCallSeqEnd = 0;
2046 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2047 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002048
Chris Lattner16cd04d2005-05-12 23:24:06 +00002049 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002050 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002051 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2052 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002053 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2054 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002055 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002056 LatestCallSeqEnd = Entry.Val;
2057 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002058
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002059 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002060 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002061 OutChain = 0;
Chris Lattner9530ddc2005-05-13 05:09:11 +00002062 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002063
Chris Lattner9530ddc2005-05-13 05:09:11 +00002064 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002065 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002066 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002067
Chris Lattner9530ddc2005-05-13 05:09:11 +00002068 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002069}
2070
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002071/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002072void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2073 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002074 // Nothing to splice it into?
2075 if (OutChain == 0) return;
2076
2077 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2078 //OutChain->dump();
2079
2080 // Form a token factor node merging the old inval and the new inval.
2081 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2082 OutChain->getOperand(0));
2083 // Change the node to refer to the new token.
2084 OutChain->setAdjCallChain(InToken);
2085}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002086
2087
Chris Lattner77e77a62005-01-21 06:05:23 +00002088// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2089// does not fit into a register, return the lo part and set the hi part to the
2090// by-reg argument. If it does fit into a single register, return the result
2091// and leave the Hi part unset.
2092SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2093 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002094 SDNode *OutChain;
2095 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2096 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002097 if (InChain.Val == 0)
2098 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002099
Chris Lattner77e77a62005-01-21 06:05:23 +00002100 TargetLowering::ArgListTy Args;
2101 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2102 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2103 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2104 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2105 }
2106 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002107
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002108 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002109 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002110 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002111 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2112 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002113 SpliceCallInto(CallInfo.second, OutChain);
2114
2115 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002116
2117 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002118 default: assert(0 && "Unknown thing");
2119 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002120 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002121 case Promote:
2122 assert(0 && "Cannot promote this yet!");
2123 case Expand:
2124 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002125 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002126 return Lo;
2127 }
2128}
2129
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002130
Chris Lattner77e77a62005-01-21 06:05:23 +00002131/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2132/// destination type is legal.
2133SDOperand SelectionDAGLegalize::
2134ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2135 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2136 assert(getTypeAction(Source.getValueType()) == Expand &&
2137 "This is not an expansion!");
2138 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2139
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002140 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002141 assert(Source.getValueType() == MVT::i64 &&
2142 "This only works for 64-bit -> FP");
2143 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2144 // incoming integer is set. To handle this, we dynamically test to see if
2145 // it is set, and, if so, add a fudge factor.
2146 SDOperand Lo, Hi;
2147 ExpandOp(Source, Lo, Hi);
2148
Chris Lattner66de05b2005-05-13 04:45:13 +00002149 // If this is unsigned, and not supported, first perform the conversion to
2150 // signed, then adjust the result if the sign bit is set.
2151 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2152 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2153
Chris Lattnere9c35e72005-04-13 05:09:42 +00002154 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2155 DAG.getConstant(0, Hi.getValueType()));
2156 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2157 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2158 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002159 uint64_t FF = 0x5f800000ULL;
2160 if (TLI.isLittleEndian()) FF <<= 32;
2161 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002162
2163 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2164 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2165 TLI.getPointerTy());
2166 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2167 SDOperand FudgeInReg;
2168 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002169 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2170 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002171 else {
2172 assert(DestTy == MVT::f64 && "Unexpected conversion");
2173 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002174 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002175 }
2176 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002177 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002178
Chris Lattner13689e22005-05-12 07:00:44 +00002179 // Expand the source, then glue it back together for the call. We must expand
2180 // the source in case it is shared (this pass of legalize must traverse it).
2181 SDOperand SrcLo, SrcHi;
2182 ExpandOp(Source, SrcLo, SrcHi);
2183 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2184
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002185 SDNode *OutChain = 0;
2186 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2187 DAG.getEntryNode());
2188 const char *FnName = 0;
2189 if (DestTy == MVT::f32)
2190 FnName = "__floatdisf";
2191 else {
2192 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2193 FnName = "__floatdidf";
2194 }
2195
Chris Lattner77e77a62005-01-21 06:05:23 +00002196 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2197
2198 TargetLowering::ArgListTy Args;
2199 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002200
Chris Lattner77e77a62005-01-21 06:05:23 +00002201 Args.push_back(std::make_pair(Source, ArgTy));
2202
2203 // We don't care about token chains for libcalls. We just use the entry
2204 // node as our input and ignore the output chain. This allows us to place
2205 // calls wherever we need them to satisfy data dependences.
2206 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002207
2208 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002209 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2210 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002211
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002212 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002213 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002214}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002215
Chris Lattnere34b3962005-01-19 04:19:40 +00002216
2217
Chris Lattner3e928bb2005-01-07 07:47:09 +00002218/// ExpandOp - Expand the specified SDOperand into its two component pieces
2219/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2220/// LegalizeNodes map is filled in for any results that are not expanded, the
2221/// ExpandedNodes map is filled in for any results that are expanded, and the
2222/// Lo/Hi values are returned.
2223void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2224 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002225 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002226 SDNode *Node = Op.Val;
2227 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2228 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2229 assert(MVT::isInteger(NVT) && NVT < VT &&
2230 "Cannot expand to FP value or to larger int value!");
2231
2232 // If there is more than one use of this, see if we already expanded it.
2233 // There is no use remembering values that only have a single use, as the map
2234 // entries will never be reused.
2235 if (!Node->hasOneUse()) {
2236 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2237 = ExpandedNodes.find(Op);
2238 if (I != ExpandedNodes.end()) {
2239 Lo = I->second.first;
2240 Hi = I->second.second;
2241 return;
2242 }
Chris Lattner45982da2005-05-12 16:53:42 +00002243 } else {
2244 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002245 }
2246
Chris Lattner4e6c7462005-01-08 19:27:05 +00002247 // Expanding to multiple registers needs to perform an optimization step, and
2248 // is not careful to avoid operations the target does not support. Make sure
2249 // that all generated operations are legalized in the next iteration.
2250 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002251
Chris Lattner3e928bb2005-01-07 07:47:09 +00002252 switch (Node->getOpcode()) {
2253 default:
2254 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2255 assert(0 && "Do not know how to expand this operator!");
2256 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002257 case ISD::UNDEF:
2258 Lo = DAG.getNode(ISD::UNDEF, NVT);
2259 Hi = DAG.getNode(ISD::UNDEF, NVT);
2260 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002261 case ISD::Constant: {
2262 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2263 Lo = DAG.getConstant(Cst, NVT);
2264 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2265 break;
2266 }
2267
2268 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002269 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002270 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002271 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2272 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002273
Chris Lattner69a52152005-01-14 22:38:01 +00002274 // Remember that we legalized the chain.
2275 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2276
Chris Lattner3e928bb2005-01-07 07:47:09 +00002277 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2278 break;
2279 }
2280
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002281 case ISD::BUILD_PAIR:
2282 // Legalize both operands. FIXME: in the future we should handle the case
2283 // where the two elements are not legal.
2284 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2285 Lo = LegalizeOp(Node->getOperand(0));
2286 Hi = LegalizeOp(Node->getOperand(1));
2287 break;
2288
Chris Lattneredb1add2005-05-11 04:51:16 +00002289 case ISD::CTPOP:
2290 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002291 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2292 DAG.getNode(ISD::CTPOP, NVT, Lo),
2293 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002294 Hi = DAG.getConstant(0, NVT);
2295 break;
2296
Chris Lattner39a8f332005-05-12 19:05:01 +00002297 case ISD::CTLZ: {
2298 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002299 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002300 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2301 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2302 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2303 HLZ, BitsC);
2304 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2305 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2306
2307 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2308 Hi = DAG.getConstant(0, NVT);
2309 break;
2310 }
2311
2312 case ISD::CTTZ: {
2313 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002314 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002315 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2316 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2317 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2318 LTZ, BitsC);
2319 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2320 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2321
2322 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2323 Hi = DAG.getConstant(0, NVT);
2324 break;
2325 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002326
Chris Lattner3e928bb2005-01-07 07:47:09 +00002327 case ISD::LOAD: {
2328 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2329 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002330 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002331
2332 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002333 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002334 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2335 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002336 //Is this safe? declaring that the two parts of the split load
2337 //are from the same instruction?
2338 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002339
2340 // Build a factor node to remember that this load is independent of the
2341 // other one.
2342 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2343 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002344
Chris Lattner3e928bb2005-01-07 07:47:09 +00002345 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002346 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002347 if (!TLI.isLittleEndian())
2348 std::swap(Lo, Hi);
2349 break;
2350 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002351 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002352 case ISD::CALL: {
2353 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2354 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2355
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002356 bool Changed = false;
2357 std::vector<SDOperand> Ops;
2358 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2359 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2360 Changed |= Ops.back() != Node->getOperand(i);
2361 }
2362
Chris Lattner3e928bb2005-01-07 07:47:09 +00002363 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2364 "Can only expand a call once so far, not i64 -> i16!");
2365
2366 std::vector<MVT::ValueType> RetTyVTs;
2367 RetTyVTs.reserve(3);
2368 RetTyVTs.push_back(NVT);
2369 RetTyVTs.push_back(NVT);
2370 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002371 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2372 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002373 Lo = SDOperand(NC, 0);
2374 Hi = SDOperand(NC, 1);
2375
2376 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002377 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002378 break;
2379 }
2380 case ISD::AND:
2381 case ISD::OR:
2382 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2383 SDOperand LL, LH, RL, RH;
2384 ExpandOp(Node->getOperand(0), LL, LH);
2385 ExpandOp(Node->getOperand(1), RL, RH);
2386 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2387 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2388 break;
2389 }
2390 case ISD::SELECT: {
2391 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002392
2393 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2394 case Expand: assert(0 && "It's impossible to expand bools");
2395 case Legal:
2396 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2397 break;
2398 case Promote:
2399 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2400 break;
2401 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002402 ExpandOp(Node->getOperand(1), LL, LH);
2403 ExpandOp(Node->getOperand(2), RL, RH);
2404 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2405 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2406 break;
2407 }
2408 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002409 SDOperand In;
2410 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2411 case Expand: assert(0 && "expand-expand not implemented yet!");
2412 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2413 case Promote:
2414 In = PromoteOp(Node->getOperand(0));
2415 // Emit the appropriate sign_extend_inreg to get the value we want.
2416 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2417 Node->getOperand(0).getValueType());
2418 break;
2419 }
2420
Chris Lattner3e928bb2005-01-07 07:47:09 +00002421 // The low part is just a sign extension of the input (which degenerates to
2422 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002423 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002424
Chris Lattner3e928bb2005-01-07 07:47:09 +00002425 // The high part is obtained by SRA'ing all but one of the bits of the lo
2426 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002427 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002428 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2429 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002430 break;
2431 }
Chris Lattner06098e02005-04-03 23:41:52 +00002432 case ISD::ZERO_EXTEND: {
2433 SDOperand In;
2434 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2435 case Expand: assert(0 && "expand-expand not implemented yet!");
2436 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2437 case Promote:
2438 In = PromoteOp(Node->getOperand(0));
2439 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002440 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002441 break;
2442 }
2443
Chris Lattner3e928bb2005-01-07 07:47:09 +00002444 // The low part is just a zero extension of the input (which degenerates to
2445 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002446 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002447
Chris Lattner3e928bb2005-01-07 07:47:09 +00002448 // The high part is just a zero.
2449 Hi = DAG.getConstant(0, NVT);
2450 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002451 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002452 // These operators cannot be expanded directly, emit them as calls to
2453 // library functions.
2454 case ISD::FP_TO_SINT:
2455 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002456 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002457 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002458 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002459 break;
2460 case ISD::FP_TO_UINT:
2461 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002462 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002463 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002464 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002465 break;
2466
Chris Lattnere34b3962005-01-19 04:19:40 +00002467 case ISD::SHL:
2468 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002469 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002470 break;
Chris Lattner47599822005-04-02 03:38:53 +00002471
2472 // If this target supports SHL_PARTS, use it.
2473 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002474 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2475 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002476 break;
2477 }
2478
Chris Lattnere34b3962005-01-19 04:19:40 +00002479 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002480 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002481 break;
2482
2483 case ISD::SRA:
2484 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002485 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002486 break;
Chris Lattner47599822005-04-02 03:38:53 +00002487
2488 // If this target supports SRA_PARTS, use it.
2489 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002490 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2491 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002492 break;
2493 }
2494
Chris Lattnere34b3962005-01-19 04:19:40 +00002495 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002496 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002497 break;
2498 case ISD::SRL:
2499 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002500 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002501 break;
Chris Lattner47599822005-04-02 03:38:53 +00002502
2503 // If this target supports SRL_PARTS, use it.
2504 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002505 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2506 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002507 break;
2508 }
2509
Chris Lattnere34b3962005-01-19 04:19:40 +00002510 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002511 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002512 break;
2513
Misha Brukmanedf128a2005-04-21 22:36:52 +00002514 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002515 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2516 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002517 break;
2518 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002519 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2520 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002521 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002522 case ISD::MUL: {
2523 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2524 SDOperand LL, LH, RL, RH;
2525 ExpandOp(Node->getOperand(0), LL, LH);
2526 ExpandOp(Node->getOperand(1), RL, RH);
2527 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2528 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2529 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2530 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2531 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2532 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2533 } else {
2534 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2535 }
2536 break;
2537 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002538 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2539 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2540 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2541 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002542 }
2543
2544 // Remember in a map if the values will be reused later.
2545 if (!Node->hasOneUse()) {
2546 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2547 std::make_pair(Lo, Hi))).second;
2548 assert(isNew && "Value already expanded?!?");
2549 }
2550}
2551
2552
2553// SelectionDAG::Legalize - This is the entry point for the file.
2554//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002555void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002556 /// run - This is the main entry point to this class.
2557 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002558 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002559}
2560