blob: f1fd69eca857c88d6f9ada0af0dd9e3bdd87299a [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:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000200 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
201 // If this is a target node, legalize it by legalizing the operands then
202 // passing it through.
203 std::vector<SDOperand> Ops;
204 bool Changed = false;
205 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
206 Ops.push_back(LegalizeOp(Node->getOperand(i)));
207 Changed = Changed || Node->getOperand(i) != Ops.back();
208 }
209 if (Changed)
210 if (Node->getNumValues() == 1)
211 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
212 else {
213 std::vector<MVT::ValueType> VTs(Node->value_begin(),
214 Node->value_end());
215 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
216 }
217
218 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
219 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
220 return Result.getValue(Op.ResNo);
221 }
222 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000223 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
224 assert(0 && "Do not know how to legalize this operator!");
225 abort();
226 case ISD::EntryToken:
227 case ISD::FrameIndex:
228 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000229 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000230 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000231 assert(getTypeAction(Node->getValueType(0)) == Legal &&
232 "This must be legal!");
233 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000234 case ISD::CopyFromReg:
235 Tmp1 = LegalizeOp(Node->getOperand(0));
236 if (Tmp1 != Node->getOperand(0))
237 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
238 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000239 else
240 Result = Op.getValue(0);
241
242 // Since CopyFromReg produces two values, make sure to remember that we
243 // legalized both of them.
244 AddLegalizedOperand(Op.getValue(0), Result);
245 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
246 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000247 case ISD::ImplicitDef:
248 Tmp1 = LegalizeOp(Node->getOperand(0));
249 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000250 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000251 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000252 case ISD::UNDEF: {
253 MVT::ValueType VT = Op.getValueType();
254 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000255 default: assert(0 && "This action is not supported yet!");
256 case TargetLowering::Expand:
257 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000258 if (MVT::isInteger(VT))
259 Result = DAG.getConstant(0, VT);
260 else if (MVT::isFloatingPoint(VT))
261 Result = DAG.getConstantFP(0, VT);
262 else
263 assert(0 && "Unknown value type!");
264 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000265 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000266 break;
267 }
268 break;
269 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000270 case ISD::Constant:
271 // We know we don't need to expand constants here, constants only have one
272 // value and we check that it is fine above.
273
274 // FIXME: Maybe we should handle things like targets that don't support full
275 // 32-bit immediates?
276 break;
277 case ISD::ConstantFP: {
278 // Spill FP immediates to the constant pool if the target cannot directly
279 // codegen them. Targets often have some immediate values that can be
280 // efficiently generated into an FP register without a load. We explicitly
281 // leave these constants as ConstantFP nodes for the target to deal with.
282
283 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
284
285 // Check to see if this FP immediate is already legal.
286 bool isLegal = false;
287 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
288 E = TLI.legal_fpimm_end(); I != E; ++I)
289 if (CFP->isExactlyValue(*I)) {
290 isLegal = true;
291 break;
292 }
293
294 if (!isLegal) {
295 // Otherwise we need to spill the constant to memory.
296 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
297
298 bool Extend = false;
299
300 // If a FP immediate is precise when represented as a float, we put it
301 // into the constant pool as a float, even if it's is statically typed
302 // as a double.
303 MVT::ValueType VT = CFP->getValueType(0);
304 bool isDouble = VT == MVT::f64;
305 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
306 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000307 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
308 // Only do this if the target has a native EXTLOAD instruction from
309 // f32.
310 TLI.getOperationAction(ISD::EXTLOAD,
311 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000312 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
313 VT = MVT::f32;
314 Extend = true;
315 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000316
Chris Lattner3e928bb2005-01-07 07:47:09 +0000317 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
318 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000319 if (Extend) {
320 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000321 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000322 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000323 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
324 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000325 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000326 }
327 break;
328 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000329 case ISD::TokenFactor: {
330 std::vector<SDOperand> Ops;
331 bool Changed = false;
332 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000333 SDOperand Op = Node->getOperand(i);
334 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000335 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000336 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
337 Changed = true;
338 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
339 Ops.push_back(LegalizeOp(Op.getOperand(j)));
340 } else {
341 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
342 Changed |= Ops[i] != Op;
343 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000344 }
345 if (Changed)
346 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
347 break;
348 }
349
Chris Lattner16cd04d2005-05-12 23:24:06 +0000350 case ISD::CALLSEQ_START:
351 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000353 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000354 Tmp2 = Node->getOperand(0);
355 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000356 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000357
358 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
359 // this will cause the maps used to memoize results to get confused.
360 // Create and add a dummy use, just to increase its use count. This will
361 // be removed at the end of legalize when dead nodes are removed.
362 if (Tmp2.Val->hasOneUse())
363 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
364 DAG.getConstant(0, MVT::i32));
365 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000366 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000367 // nodes are treated specially and are mutated in place. This makes the dag
368 // legalization process more efficient and also makes libcall insertion
369 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000370 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000371 case ISD::DYNAMIC_STACKALLOC:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
374 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
375 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000376 Tmp3 != Node->getOperand(2)) {
377 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
378 std::vector<SDOperand> Ops;
379 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
380 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
381 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000382 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000383
384 // Since this op produces two values, make sure to remember that we
385 // legalized both of them.
386 AddLegalizedOperand(SDOperand(Node, 0), Result);
387 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
388 return Result.getValue(Op.ResNo);
389
Chris Lattnerd71c0412005-05-13 18:43:43 +0000390 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000391 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000392 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
393 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000394
395 bool Changed = false;
396 std::vector<SDOperand> Ops;
397 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
398 Ops.push_back(LegalizeOp(Node->getOperand(i)));
399 Changed |= Ops.back() != Node->getOperand(i);
400 }
401
402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000403 std::vector<MVT::ValueType> RetTyVTs;
404 RetTyVTs.reserve(Node->getNumValues());
405 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000406 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000407 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
408 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000409 } else {
410 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000411 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000412 // Since calls produce multiple values, make sure to remember that we
413 // legalized all of them.
414 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
415 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
416 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000417 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000418 case ISD::BR:
419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
420 if (Tmp1 != Node->getOperand(0))
421 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
422 break;
423
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000424 case ISD::BRCOND:
425 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000426
427 switch (getTypeAction(Node->getOperand(1).getValueType())) {
428 case Expand: assert(0 && "It's impossible to expand bools");
429 case Legal:
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
431 break;
432 case Promote:
433 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
434 break;
435 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000436 // Basic block destination (Op#2) is always legal.
437 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
438 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
439 Node->getOperand(2));
440 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000441 case ISD::BRCONDTWOWAY:
442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
443 switch (getTypeAction(Node->getOperand(1).getValueType())) {
444 case Expand: assert(0 && "It's impossible to expand bools");
445 case Legal:
446 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
447 break;
448 case Promote:
449 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
450 break;
451 }
452 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
453 // pair.
454 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
455 case TargetLowering::Promote:
456 default: assert(0 && "This action is not supported yet!");
457 case TargetLowering::Legal:
458 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
459 std::vector<SDOperand> Ops;
460 Ops.push_back(Tmp1);
461 Ops.push_back(Tmp2);
462 Ops.push_back(Node->getOperand(2));
463 Ops.push_back(Node->getOperand(3));
464 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
465 }
466 break;
467 case TargetLowering::Expand:
468 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
469 Node->getOperand(2));
470 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
471 break;
472 }
473 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000474
Chris Lattner3e928bb2005-01-07 07:47:09 +0000475 case ISD::LOAD:
476 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
477 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000478
Chris Lattner3e928bb2005-01-07 07:47:09 +0000479 if (Tmp1 != Node->getOperand(0) ||
480 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000481 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
482 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000483 else
484 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000485
Chris Lattner8afc48e2005-01-07 22:28:47 +0000486 // Since loads produce two values, make sure to remember that we legalized
487 // both of them.
488 AddLegalizedOperand(SDOperand(Node, 0), Result);
489 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
490 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000491
Chris Lattner0f69b292005-01-15 06:18:18 +0000492 case ISD::EXTLOAD:
493 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000494 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000495 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
496 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000497
Chris Lattner01ff7212005-04-10 22:54:25 +0000498 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
499 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000500 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000501 case TargetLowering::Promote:
502 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
503 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000504 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000505 // Since loads produce two values, make sure to remember that we legalized
506 // both of them.
507 AddLegalizedOperand(SDOperand(Node, 0), Result);
508 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
509 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000510
Chris Lattner01ff7212005-04-10 22:54:25 +0000511 case TargetLowering::Legal:
512 if (Tmp1 != Node->getOperand(0) ||
513 Tmp2 != Node->getOperand(1))
514 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000515 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000516 else
517 Result = SDOperand(Node, 0);
518
519 // Since loads produce two values, make sure to remember that we legalized
520 // both of them.
521 AddLegalizedOperand(SDOperand(Node, 0), Result);
522 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
523 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000524 case TargetLowering::Expand:
525 assert(Node->getOpcode() != ISD::EXTLOAD &&
526 "EXTLOAD should always be supported!");
527 // Turn the unsupported load into an EXTLOAD followed by an explicit
528 // zero/sign extend inreg.
529 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000530 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000531 SDOperand ValRes;
532 if (Node->getOpcode() == ISD::SEXTLOAD)
533 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
534 Result, SrcVT);
535 else
536 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000537 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
538 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
539 if (Op.ResNo)
540 return Result.getValue(1);
541 return ValRes;
542 }
543 assert(0 && "Unreachable");
544 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000545 case ISD::EXTRACT_ELEMENT:
546 // Get both the low and high parts.
547 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
548 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
549 Result = Tmp2; // 1 -> Hi
550 else
551 Result = Tmp1; // 0 -> Lo
552 break;
553
554 case ISD::CopyToReg:
555 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000556
Chris Lattner3e928bb2005-01-07 07:47:09 +0000557 switch (getTypeAction(Node->getOperand(1).getValueType())) {
558 case Legal:
559 // Legalize the incoming value (must be legal).
560 Tmp2 = LegalizeOp(Node->getOperand(1));
561 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000562 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000563 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000564 case Promote:
565 Tmp2 = PromoteOp(Node->getOperand(1));
566 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
567 break;
568 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000570 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000571 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000572 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
573 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
574 // Note that the copytoreg nodes are independent of each other.
575 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576 assert(isTypeLegal(Result.getValueType()) &&
577 "Cannot expand multiple times yet (i64 -> i16)");
578 break;
579 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000580 break;
581
582 case ISD::RET:
583 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
584 switch (Node->getNumOperands()) {
585 case 2: // ret val
586 switch (getTypeAction(Node->getOperand(1).getValueType())) {
587 case Legal:
588 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000589 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000590 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
591 break;
592 case Expand: {
593 SDOperand Lo, Hi;
594 ExpandOp(Node->getOperand(1), Lo, Hi);
595 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, 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 Tmp2 = PromoteOp(Node->getOperand(1));
600 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
601 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000602 }
603 break;
604 case 1: // ret void
605 if (Tmp1 != Node->getOperand(0))
606 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
607 break;
608 default: { // ret <values>
609 std::vector<SDOperand> NewValues;
610 NewValues.push_back(Tmp1);
611 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
612 switch (getTypeAction(Node->getOperand(i).getValueType())) {
613 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000614 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000615 break;
616 case Expand: {
617 SDOperand Lo, Hi;
618 ExpandOp(Node->getOperand(i), Lo, Hi);
619 NewValues.push_back(Lo);
620 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000621 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000622 }
623 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000624 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000625 }
626 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
627 break;
628 }
629 }
630 break;
631 case ISD::STORE:
632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
633 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
634
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000635 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000636 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000637 if (CFP->getValueType(0) == MVT::f32) {
638 union {
639 unsigned I;
640 float F;
641 } V;
642 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000643 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000644 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000645 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000646 } else {
647 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
648 union {
649 uint64_t I;
650 double F;
651 } V;
652 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000653 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000654 DAG.getConstant(V.I, MVT::i64), Tmp2,
655 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000656 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000657 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000658 }
659
Chris Lattner3e928bb2005-01-07 07:47:09 +0000660 switch (getTypeAction(Node->getOperand(1).getValueType())) {
661 case Legal: {
662 SDOperand Val = LegalizeOp(Node->getOperand(1));
663 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
664 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000665 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
666 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000667 break;
668 }
669 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000670 // Truncate the value and store the result.
671 Tmp3 = PromoteOp(Node->getOperand(1));
672 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000673 Node->getOperand(3),
674 Node->getOperand(1).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000675 break;
676
Chris Lattner3e928bb2005-01-07 07:47:09 +0000677 case Expand:
678 SDOperand Lo, Hi;
679 ExpandOp(Node->getOperand(1), Lo, Hi);
680
681 if (!TLI.isLittleEndian())
682 std::swap(Lo, Hi);
683
Chris Lattneredb1add2005-05-11 04:51:16 +0000684 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
685 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000686 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000687 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
688 getIntPtrConstant(IncrementSize));
689 assert(isTypeLegal(Tmp2.getValueType()) &&
690 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000691 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000692 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
693 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000694 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
695 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000696 }
697 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000698 case ISD::PCMARKER:
699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000700 if (Tmp1 != Node->getOperand(0))
701 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000702 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000703 case ISD::TRUNCSTORE:
704 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
705 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
706
707 switch (getTypeAction(Node->getOperand(1).getValueType())) {
708 case Legal:
709 Tmp2 = LegalizeOp(Node->getOperand(1));
710 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
711 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000712 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000713 Node->getOperand(3),
Chris Lattner0f69b292005-01-15 06:18:18 +0000714 cast<MVTSDNode>(Node)->getExtraValueType());
715 break;
716 case Promote:
717 case Expand:
718 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
719 }
720 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000721 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000722 switch (getTypeAction(Node->getOperand(0).getValueType())) {
723 case Expand: assert(0 && "It's impossible to expand bools");
724 case Legal:
725 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
726 break;
727 case Promote:
728 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
729 break;
730 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000731 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000732 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000733
734 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
735 default: assert(0 && "This action is not supported yet!");
736 case TargetLowering::Legal:
737 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
738 Tmp3 != Node->getOperand(2))
739 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
740 Tmp1, Tmp2, Tmp3);
741 break;
742 case TargetLowering::Promote: {
743 MVT::ValueType NVT =
744 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
745 unsigned ExtOp, TruncOp;
746 if (MVT::isInteger(Tmp2.getValueType())) {
747 ExtOp = ISD::ZERO_EXTEND;
748 TruncOp = ISD::TRUNCATE;
749 } else {
750 ExtOp = ISD::FP_EXTEND;
751 TruncOp = ISD::FP_ROUND;
752 }
753 // Promote each of the values to the new type.
754 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
755 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
756 // Perform the larger operation, then round down.
757 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
758 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
759 break;
760 }
761 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000762 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000763 case ISD::SETCC:
764 switch (getTypeAction(Node->getOperand(0).getValueType())) {
765 case Legal:
766 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
767 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
768 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
769 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000770 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000771 break;
772 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000773 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
774 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
775
776 // If this is an FP compare, the operands have already been extended.
777 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
778 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000779 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000780
781 // Otherwise, we have to insert explicit sign or zero extends. Note
782 // that we could insert sign extends for ALL conditions, but zero extend
783 // is cheaper on many machines (an AND instead of two shifts), so prefer
784 // it.
785 switch (cast<SetCCSDNode>(Node)->getCondition()) {
786 default: assert(0 && "Unknown integer comparison!");
787 case ISD::SETEQ:
788 case ISD::SETNE:
789 case ISD::SETUGE:
790 case ISD::SETUGT:
791 case ISD::SETULE:
792 case ISD::SETULT:
793 // ALL of these operations will work if we either sign or zero extend
794 // the operands (including the unsigned comparisons!). Zero extend is
795 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000796 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
797 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000798 break;
799 case ISD::SETGE:
800 case ISD::SETGT:
801 case ISD::SETLT:
802 case ISD::SETLE:
803 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
804 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
805 break;
806 }
807
808 }
809 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000810 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000812 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000813 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
814 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
815 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
816 switch (cast<SetCCSDNode>(Node)->getCondition()) {
817 case ISD::SETEQ:
818 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000819 if (RHSLo == RHSHi)
820 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
821 if (RHSCST->isAllOnesValue()) {
822 // Comparison to -1.
823 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000824 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000825 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000826 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000827 }
828
Chris Lattner3e928bb2005-01-07 07:47:09 +0000829 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
830 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
831 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000832 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000833 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000834 DAG.getConstant(0, Tmp1.getValueType()));
835 break;
836 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000837 // If this is a comparison of the sign bit, just look at the top part.
838 // X > -1, x < 0
839 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000840 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000841 CST->getValue() == 0) || // X < 0
842 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
843 (CST->isAllOnesValue()))) // X > -1
844 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
845 Node->getValueType(0), LHSHi, RHSHi);
846
Chris Lattner3e928bb2005-01-07 07:47:09 +0000847 // FIXME: This generated code sucks.
848 ISD::CondCode LowCC;
849 switch (cast<SetCCSDNode>(Node)->getCondition()) {
850 default: assert(0 && "Unknown integer setcc!");
851 case ISD::SETLT:
852 case ISD::SETULT: LowCC = ISD::SETULT; break;
853 case ISD::SETGT:
854 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
855 case ISD::SETLE:
856 case ISD::SETULE: LowCC = ISD::SETULE; break;
857 case ISD::SETGE:
858 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
859 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000860
Chris Lattner3e928bb2005-01-07 07:47:09 +0000861 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
862 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
863 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
864
865 // NOTE: on targets without efficient SELECT of bools, we can always use
866 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000867 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000868 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000869 Node->getValueType(0), LHSHi, RHSHi);
870 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
871 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
872 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000873 break;
874 }
875 }
876 break;
877
Chris Lattnere1bd8222005-01-11 05:57:22 +0000878 case ISD::MEMSET:
879 case ISD::MEMCPY:
880 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000881 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000882 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
883
884 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
885 switch (getTypeAction(Node->getOperand(2).getValueType())) {
886 case Expand: assert(0 && "Cannot expand a byte!");
887 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000888 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000889 break;
890 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000891 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000892 break;
893 }
894 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000895 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000896 }
Chris Lattner272455b2005-02-02 03:44:41 +0000897
898 SDOperand Tmp4;
899 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000900 case Expand: assert(0 && "Cannot expand this yet!");
901 case Legal:
902 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000903 break;
904 case Promote:
905 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000906 break;
907 }
908
909 SDOperand Tmp5;
910 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
911 case Expand: assert(0 && "Cannot expand this yet!");
912 case Legal:
913 Tmp5 = LegalizeOp(Node->getOperand(4));
914 break;
915 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000916 Tmp5 = PromoteOp(Node->getOperand(4));
917 break;
918 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000919
920 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
921 default: assert(0 && "This action not implemented for this operation!");
922 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000923 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
924 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
925 Tmp5 != Node->getOperand(4)) {
926 std::vector<SDOperand> Ops;
927 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
928 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
929 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
930 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000931 break;
932 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000933 // Otherwise, the target does not support this operation. Lower the
934 // operation to an explicit libcall as appropriate.
935 MVT::ValueType IntPtr = TLI.getPointerTy();
936 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
937 std::vector<std::pair<SDOperand, const Type*> > Args;
938
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000939 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000940 if (Node->getOpcode() == ISD::MEMSET) {
941 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
942 // Extend the ubyte argument to be an int value for the call.
943 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
944 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
945 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
946
947 FnName = "memset";
948 } else if (Node->getOpcode() == ISD::MEMCPY ||
949 Node->getOpcode() == ISD::MEMMOVE) {
950 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
951 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
952 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
953 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
954 } else {
955 assert(0 && "Unknown op!");
956 }
Chris Lattner45982da2005-05-12 16:53:42 +0000957
Chris Lattnere1bd8222005-01-11 05:57:22 +0000958 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +0000959 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000960 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
961 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000962 break;
963 }
964 case TargetLowering::Custom:
965 std::vector<SDOperand> Ops;
966 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
967 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
968 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +0000969 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000970 Result = LegalizeOp(Result);
971 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000972 }
973 break;
974 }
Chris Lattner52d08bd2005-05-09 20:23:03 +0000975
976 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000977 Tmp1 = LegalizeOp(Node->getOperand(0));
978 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000979
Chris Lattner3e011362005-05-14 07:45:46 +0000980 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
981 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
982 std::vector<SDOperand> Ops;
983 Ops.push_back(Tmp1);
984 Ops.push_back(Tmp2);
985 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
986 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +0000987 Result = SDOperand(Node, 0);
988 // Since these produce two values, make sure to remember that we legalized
989 // both of them.
990 AddLegalizedOperand(SDOperand(Node, 0), Result);
991 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
992 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000993 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000994 Tmp1 = LegalizeOp(Node->getOperand(0));
995 Tmp2 = LegalizeOp(Node->getOperand(1));
996 Tmp3 = LegalizeOp(Node->getOperand(2));
997 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
998 Tmp3 != Node->getOperand(2))
999 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1000 break;
1001
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001002 case ISD::READIO:
1003 Tmp1 = LegalizeOp(Node->getOperand(0));
1004 Tmp2 = LegalizeOp(Node->getOperand(1));
1005
1006 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1007 case TargetLowering::Custom:
1008 default: assert(0 && "This action not implemented for this operation!");
1009 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001010 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1011 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1012 std::vector<SDOperand> Ops;
1013 Ops.push_back(Tmp1);
1014 Ops.push_back(Tmp2);
1015 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1016 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001017 Result = SDOperand(Node, 0);
1018 break;
1019 case TargetLowering::Expand:
1020 // Replace this with a load from memory.
1021 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1022 Node->getOperand(1), DAG.getSrcValue(NULL));
1023 Result = LegalizeOp(Result);
1024 break;
1025 }
1026
1027 // Since these produce two values, make sure to remember that we legalized
1028 // both of them.
1029 AddLegalizedOperand(SDOperand(Node, 0), Result);
1030 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1031 return Result.getValue(Op.ResNo);
1032
1033 case ISD::WRITEIO:
1034 Tmp1 = LegalizeOp(Node->getOperand(0));
1035 Tmp2 = LegalizeOp(Node->getOperand(1));
1036 Tmp3 = LegalizeOp(Node->getOperand(2));
1037
1038 switch (TLI.getOperationAction(Node->getOpcode(),
1039 Node->getOperand(1).getValueType())) {
1040 case TargetLowering::Custom:
1041 default: assert(0 && "This action not implemented for this operation!");
1042 case TargetLowering::Legal:
1043 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1044 Tmp3 != Node->getOperand(2))
1045 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1046 break;
1047 case TargetLowering::Expand:
1048 // Replace this with a store to memory.
1049 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1050 Node->getOperand(1), Node->getOperand(2),
1051 DAG.getSrcValue(NULL));
1052 Result = LegalizeOp(Result);
1053 break;
1054 }
1055 break;
1056
Chris Lattner84f67882005-01-20 18:52:28 +00001057 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001058 case ISD::SUB_PARTS:
1059 case ISD::SHL_PARTS:
1060 case ISD::SRA_PARTS:
1061 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001062 std::vector<SDOperand> Ops;
1063 bool Changed = false;
1064 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1065 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1066 Changed |= Ops.back() != Node->getOperand(i);
1067 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001068 if (Changed) {
1069 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1070 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1071 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001072
1073 // Since these produce multiple values, make sure to remember that we
1074 // legalized all of them.
1075 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1076 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1077 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001078 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001079
1080 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001081 case ISD::ADD:
1082 case ISD::SUB:
1083 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001084 case ISD::MULHS:
1085 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001086 case ISD::UDIV:
1087 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001088 case ISD::AND:
1089 case ISD::OR:
1090 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001091 case ISD::SHL:
1092 case ISD::SRL:
1093 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001094 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1095 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1096 if (Tmp1 != Node->getOperand(0) ||
1097 Tmp2 != Node->getOperand(1))
1098 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1099 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001100
Nate Begemanc105e192005-04-06 00:23:54 +00001101 case ISD::UREM:
1102 case ISD::SREM:
1103 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1104 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1105 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1106 case TargetLowering::Legal:
1107 if (Tmp1 != Node->getOperand(0) ||
1108 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001109 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001110 Tmp2);
1111 break;
1112 case TargetLowering::Promote:
1113 case TargetLowering::Custom:
1114 assert(0 && "Cannot promote/custom handle this yet!");
1115 case TargetLowering::Expand: {
1116 MVT::ValueType VT = Node->getValueType(0);
1117 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1118 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1119 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1120 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1121 }
1122 break;
1123 }
1124 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001125
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001126 case ISD::CTPOP:
1127 case ISD::CTTZ:
1128 case ISD::CTLZ:
1129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1130 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1131 case TargetLowering::Legal:
1132 if (Tmp1 != Node->getOperand(0))
1133 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1134 break;
1135 case TargetLowering::Promote: {
1136 MVT::ValueType OVT = Tmp1.getValueType();
1137 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001138
1139 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001140 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1141 // Perform the larger operation, then subtract if needed.
1142 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1143 switch(Node->getOpcode())
1144 {
1145 case ISD::CTPOP:
1146 Result = Tmp1;
1147 break;
1148 case ISD::CTTZ:
1149 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner39a8f332005-05-12 19:05:01 +00001150 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001151 DAG.getConstant(getSizeInBits(NVT), NVT));
1152 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1153 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1154 break;
1155 case ISD::CTLZ:
1156 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1157 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1158 DAG.getConstant(getSizeInBits(NVT) -
1159 getSizeInBits(OVT), NVT));
1160 break;
1161 }
1162 break;
1163 }
1164 case TargetLowering::Custom:
1165 assert(0 && "Cannot custom handle this yet!");
1166 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001167 switch(Node->getOpcode())
1168 {
1169 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001170 static const uint64_t mask[6] = {
1171 0x5555555555555555ULL, 0x3333333333333333ULL,
1172 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1173 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1174 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001175 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001176 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1177 unsigned len = getSizeInBits(VT);
1178 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001179 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001180 Tmp2 = DAG.getConstant(mask[i], VT);
1181 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001182 Tmp1 = DAG.getNode(ISD::ADD, VT,
1183 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1184 DAG.getNode(ISD::AND, VT,
1185 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1186 Tmp2));
1187 }
1188 Result = Tmp1;
1189 break;
1190 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001191 case ISD::CTLZ: {
1192 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001193 x = x | (x >> 1);
1194 x = x | (x >> 2);
1195 ...
1196 x = x | (x >>16);
1197 x = x | (x >>32); // for 64-bit input
1198 return popcount(~x);
Duraid Madina57ff7e52005-05-11 08:45:08 +00001199
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001200 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1201 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001202 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1203 unsigned len = getSizeInBits(VT);
1204 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1205 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1206 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1207 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1208 }
1209 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001210 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001211 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001212 }
1213 case ISD::CTTZ: {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001214 // for now, we use: { return popcount(~x & (x - 1)); }
1215 // unless the target has ctlz but not ctpop, in which case we use:
1216 // { return 32 - nlz(~x & (x-1)); }
1217 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001218 MVT::ValueType VT = Tmp1.getValueType();
1219 Tmp2 = DAG.getConstant(~0ULL, VT);
1220 Tmp3 = DAG.getNode(ISD::AND, VT,
1221 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1222 DAG.getNode(ISD::SUB, VT, Tmp1,
1223 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001224 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1225 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1226 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1227 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1228 DAG.getConstant(getSizeInBits(VT), VT),
1229 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1230 } else {
1231 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1232 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001233 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001234 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001235 default:
1236 assert(0 && "Cannot expand this yet!");
1237 break;
1238 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001239 break;
1240 }
1241 break;
1242
Chris Lattner2c8086f2005-04-02 05:00:07 +00001243 // Unary operators
1244 case ISD::FABS:
1245 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001246 case ISD::FSQRT:
1247 case ISD::FSIN:
1248 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001249 Tmp1 = LegalizeOp(Node->getOperand(0));
1250 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1251 case TargetLowering::Legal:
1252 if (Tmp1 != Node->getOperand(0))
1253 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1254 break;
1255 case TargetLowering::Promote:
1256 case TargetLowering::Custom:
1257 assert(0 && "Cannot promote/custom handle this yet!");
1258 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001259 switch(Node->getOpcode()) {
1260 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001261 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1262 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1263 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1264 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001265 break;
1266 }
1267 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001268 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1269 MVT::ValueType VT = Node->getValueType(0);
1270 Tmp2 = DAG.getConstantFP(0.0, VT);
1271 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1272 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1273 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1274 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001275 break;
1276 }
1277 case ISD::FSQRT:
1278 case ISD::FSIN:
1279 case ISD::FCOS: {
1280 MVT::ValueType VT = Node->getValueType(0);
1281 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1282 const char *FnName = 0;
1283 switch(Node->getOpcode()) {
1284 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1285 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1286 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1287 default: assert(0 && "Unreachable!");
1288 }
1289 std::vector<std::pair<SDOperand, const Type*> > Args;
1290 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001291 // FIXME: should use ExpandLibCall!
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001292 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001293 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001294 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1295 Result = LegalizeOp(CallResult.first);
1296 break;
1297 }
1298 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001299 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001300 }
1301 break;
1302 }
1303 break;
1304
1305 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001306 case ISD::ZERO_EXTEND:
1307 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001308 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001309 case ISD::FP_EXTEND:
1310 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001311 case ISD::FP_TO_SINT:
1312 case ISD::FP_TO_UINT:
1313 case ISD::SINT_TO_FP:
1314 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001315 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1316 case Legal:
1317 Tmp1 = LegalizeOp(Node->getOperand(0));
1318 if (Tmp1 != Node->getOperand(0))
1319 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1320 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001321 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001322 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1323 Node->getOpcode() == ISD::UINT_TO_FP) {
1324 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1325 Node->getValueType(0), Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001326 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001327 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1328 // In the expand case, we must be dealing with a truncate, because
1329 // otherwise the result would be larger than the source.
1330 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001331
Chris Lattner2c8086f2005-04-02 05:00:07 +00001332 // Since the result is legal, we should just be able to truncate the low
1333 // part of the source.
1334 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1335 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001336 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001337 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001338
Chris Lattner03c85462005-01-15 05:21:40 +00001339 case Promote:
1340 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001341 case ISD::ZERO_EXTEND:
1342 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001343 // NOTE: Any extend would work here...
1344 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001345 Result = DAG.getZeroExtendInReg(Result,
1346 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001347 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001348 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001349 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001350 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001351 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001352 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1353 Result, Node->getOperand(0).getValueType());
1354 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001355 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001356 Result = PromoteOp(Node->getOperand(0));
1357 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1358 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001359 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001360 Result = PromoteOp(Node->getOperand(0));
1361 if (Result.getValueType() != Op.getValueType())
1362 // Dynamically dead while we have only 2 FP types.
1363 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1364 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001365 case ISD::FP_ROUND:
1366 case ISD::FP_TO_SINT:
1367 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001368 Result = PromoteOp(Node->getOperand(0));
1369 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1370 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001371 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001372 Result = PromoteOp(Node->getOperand(0));
1373 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1374 Result, Node->getOperand(0).getValueType());
1375 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1376 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001377 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001378 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001379 Result = DAG.getZeroExtendInReg(Result,
1380 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001381 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1382 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001383 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001384 }
1385 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001386 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001387 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001388 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001389 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1390
1391 // If this operation is not supported, convert it to a shl/shr or load/store
1392 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001393 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1394 default: assert(0 && "This action not supported for this op yet!");
1395 case TargetLowering::Legal:
1396 if (Tmp1 != Node->getOperand(0))
1397 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1398 ExtraVT);
1399 break;
1400 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001401 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001402 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001403 // NOTE: we could fall back on load/store here too for targets without
1404 // SAR. However, it is doubtful that any exist.
1405 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1406 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001407 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001408 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1409 Node->getOperand(0), ShiftCst);
1410 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1411 Result, ShiftCst);
1412 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1413 // The only way we can lower this is to turn it into a STORETRUNC,
1414 // EXTLOAD pair, targetting a temporary location (a stack slot).
1415
1416 // NOTE: there is a choice here between constantly creating new stack
1417 // slots and always reusing the same one. We currently always create
1418 // new ones, as reuse may inhibit scheduling.
1419 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1420 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1421 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1422 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001423 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001424 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1425 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1426 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001427 Node->getOperand(0), StackSlot,
1428 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001429 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001430 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001431 } else {
1432 assert(0 && "Unknown op");
1433 }
1434 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001435 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001436 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001437 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001439 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001440
Chris Lattner45982da2005-05-12 16:53:42 +00001441 // Note that LegalizeOp may be reentered even from single-use nodes, which
1442 // means that we always must cache transformed nodes.
1443 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001444 return Result;
1445}
1446
Chris Lattner8b6fa222005-01-15 22:16:26 +00001447/// PromoteOp - Given an operation that produces a value in an invalid type,
1448/// promote it to compute the value into a larger type. The produced value will
1449/// have the correct bits for the low portion of the register, but no guarantee
1450/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001451SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1452 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001453 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001454 assert(getTypeAction(VT) == Promote &&
1455 "Caller should expand or legalize operands that are not promotable!");
1456 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1457 "Cannot promote to smaller type!");
1458
Chris Lattner03c85462005-01-15 05:21:40 +00001459 SDOperand Tmp1, Tmp2, Tmp3;
1460
1461 SDOperand Result;
1462 SDNode *Node = Op.Val;
1463
Chris Lattner45982da2005-05-12 16:53:42 +00001464 if (!Node->hasOneUse()) {
1465 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1466 if (I != PromotedNodes.end()) return I->second;
1467 } else {
1468 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1469 }
1470
Chris Lattner0f69b292005-01-15 06:18:18 +00001471 // Promotion needs an optimization step to clean up after it, and is not
1472 // careful to avoid operations the target does not support. Make sure that
1473 // all generated operations are legalized in the next iteration.
1474 NeedsAnotherIteration = true;
1475
Chris Lattner03c85462005-01-15 05:21:40 +00001476 switch (Node->getOpcode()) {
1477 default:
1478 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1479 assert(0 && "Do not know how to promote this operator!");
1480 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001481 case ISD::UNDEF:
1482 Result = DAG.getNode(ISD::UNDEF, NVT);
1483 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001484 case ISD::Constant:
1485 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1486 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1487 break;
1488 case ISD::ConstantFP:
1489 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1490 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1491 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001492 case ISD::CopyFromReg:
1493 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1494 Node->getOperand(0));
1495 // Remember that we legalized the chain.
1496 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1497 break;
1498
Chris Lattner82fbfb62005-01-18 02:59:52 +00001499 case ISD::SETCC:
1500 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1501 "SetCC type is not legal??");
1502 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1503 TLI.getSetCCResultTy(), Node->getOperand(0),
1504 Node->getOperand(1));
1505 Result = LegalizeOp(Result);
1506 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001507
1508 case ISD::TRUNCATE:
1509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1510 case Legal:
1511 Result = LegalizeOp(Node->getOperand(0));
1512 assert(Result.getValueType() >= NVT &&
1513 "This truncation doesn't make sense!");
1514 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1515 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1516 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001517 case Promote:
1518 // The truncation is not required, because we don't guarantee anything
1519 // about high bits anyway.
1520 Result = PromoteOp(Node->getOperand(0));
1521 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001522 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001523 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1524 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001525 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001526 }
1527 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001528 case ISD::SIGN_EXTEND:
1529 case ISD::ZERO_EXTEND:
1530 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1531 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1532 case Legal:
1533 // Input is legal? Just do extend all the way to the larger type.
1534 Result = LegalizeOp(Node->getOperand(0));
1535 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1536 break;
1537 case Promote:
1538 // Promote the reg if it's smaller.
1539 Result = PromoteOp(Node->getOperand(0));
1540 // The high bits are not guaranteed to be anything. Insert an extend.
1541 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001542 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1543 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001544 else
Chris Lattner23993562005-04-13 02:38:47 +00001545 Result = DAG.getZeroExtendInReg(Result,
1546 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001547 break;
1548 }
1549 break;
1550
1551 case ISD::FP_EXTEND:
1552 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1553 case ISD::FP_ROUND:
1554 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1555 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1556 case Promote: assert(0 && "Unreachable with 2 FP types!");
1557 case Legal:
1558 // Input is legal? Do an FP_ROUND_INREG.
1559 Result = LegalizeOp(Node->getOperand(0));
1560 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1561 break;
1562 }
1563 break;
1564
1565 case ISD::SINT_TO_FP:
1566 case ISD::UINT_TO_FP:
1567 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1568 case Legal:
1569 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001570 // No extra round required here.
1571 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001572 break;
1573
1574 case Promote:
1575 Result = PromoteOp(Node->getOperand(0));
1576 if (Node->getOpcode() == ISD::SINT_TO_FP)
1577 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1578 Result, Node->getOperand(0).getValueType());
1579 else
Chris Lattner23993562005-04-13 02:38:47 +00001580 Result = DAG.getZeroExtendInReg(Result,
1581 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001582 // No extra round required here.
1583 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001584 break;
1585 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001586 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1587 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001588 // Round if we cannot tolerate excess precision.
1589 if (NoExcessFPPrecision)
1590 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1591 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001592 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001593 break;
1594
1595 case ISD::FP_TO_SINT:
1596 case ISD::FP_TO_UINT:
1597 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1598 case Legal:
1599 Tmp1 = LegalizeOp(Node->getOperand(0));
1600 break;
1601 case Promote:
1602 // The input result is prerounded, so we don't have to do anything
1603 // special.
1604 Tmp1 = PromoteOp(Node->getOperand(0));
1605 break;
1606 case Expand:
1607 assert(0 && "not implemented");
1608 }
1609 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1610 break;
1611
Chris Lattner2c8086f2005-04-02 05:00:07 +00001612 case ISD::FABS:
1613 case ISD::FNEG:
1614 Tmp1 = PromoteOp(Node->getOperand(0));
1615 assert(Tmp1.getValueType() == NVT);
1616 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1617 // NOTE: we do not have to do any extra rounding here for
1618 // NoExcessFPPrecision, because we know the input will have the appropriate
1619 // precision, and these operations don't modify precision at all.
1620 break;
1621
Chris Lattnerda6ba872005-04-28 21:44:33 +00001622 case ISD::FSQRT:
1623 case ISD::FSIN:
1624 case ISD::FCOS:
1625 Tmp1 = PromoteOp(Node->getOperand(0));
1626 assert(Tmp1.getValueType() == NVT);
1627 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1628 if(NoExcessFPPrecision)
1629 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1630 break;
1631
Chris Lattner03c85462005-01-15 05:21:40 +00001632 case ISD::AND:
1633 case ISD::OR:
1634 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001635 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001636 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001637 case ISD::MUL:
1638 // The input may have strange things in the top bits of the registers, but
1639 // these operations don't care. They may have wierd bits going out, but
1640 // that too is okay if they are integer operations.
1641 Tmp1 = PromoteOp(Node->getOperand(0));
1642 Tmp2 = PromoteOp(Node->getOperand(1));
1643 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1644 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1645
1646 // However, if this is a floating point operation, they will give excess
1647 // precision that we may not be able to tolerate. If we DO allow excess
1648 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001649 // FIXME: Why would we need to round FP ops more than integer ones?
1650 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001651 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1652 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1653 break;
1654
Chris Lattner8b6fa222005-01-15 22:16:26 +00001655 case ISD::SDIV:
1656 case ISD::SREM:
1657 // These operators require that their input be sign extended.
1658 Tmp1 = PromoteOp(Node->getOperand(0));
1659 Tmp2 = PromoteOp(Node->getOperand(1));
1660 if (MVT::isInteger(NVT)) {
1661 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001662 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001663 }
1664 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1665
1666 // Perform FP_ROUND: this is probably overly pessimistic.
1667 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1668 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1669 break;
1670
1671 case ISD::UDIV:
1672 case ISD::UREM:
1673 // These operators require that their input be zero extended.
1674 Tmp1 = PromoteOp(Node->getOperand(0));
1675 Tmp2 = PromoteOp(Node->getOperand(1));
1676 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001677 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1678 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001679 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1680 break;
1681
1682 case ISD::SHL:
1683 Tmp1 = PromoteOp(Node->getOperand(0));
1684 Tmp2 = LegalizeOp(Node->getOperand(1));
1685 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1686 break;
1687 case ISD::SRA:
1688 // The input value must be properly sign extended.
1689 Tmp1 = PromoteOp(Node->getOperand(0));
1690 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1691 Tmp2 = LegalizeOp(Node->getOperand(1));
1692 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1693 break;
1694 case ISD::SRL:
1695 // The input value must be properly zero extended.
1696 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001697 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001698 Tmp2 = LegalizeOp(Node->getOperand(1));
1699 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1700 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001701 case ISD::LOAD:
1702 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1703 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001704 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001705 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001706 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1707 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001708 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001709 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1710 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001711
1712 // Remember that we legalized the chain.
1713 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1714 break;
1715 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001716 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1717 case Expand: assert(0 && "It's impossible to expand bools");
1718 case Legal:
1719 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1720 break;
1721 case Promote:
1722 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1723 break;
1724 }
Chris Lattner03c85462005-01-15 05:21:40 +00001725 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1726 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1727 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1728 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00001729 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00001730 case ISD::CALL: {
1731 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1732 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1733
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001734 std::vector<SDOperand> Ops;
1735 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1736 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1737
Chris Lattner8ac532c2005-01-16 19:46:48 +00001738 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1739 "Can only promote single result calls");
1740 std::vector<MVT::ValueType> RetTyVTs;
1741 RetTyVTs.reserve(2);
1742 RetTyVTs.push_back(NVT);
1743 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00001744 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1745 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001746 Result = SDOperand(NC, 0);
1747
1748 // Insert the new chain mapping.
1749 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1750 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001751 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001752 case ISD::CTPOP:
1753 case ISD::CTTZ:
1754 case ISD::CTLZ:
1755 Tmp1 = Node->getOperand(0);
1756 //Zero extend the argument
1757 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1758 // Perform the larger operation, then subtract if needed.
1759 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1760 switch(Node->getOpcode())
1761 {
1762 case ISD::CTPOP:
1763 Result = Tmp1;
1764 break;
1765 case ISD::CTTZ:
1766 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1767 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1768 DAG.getConstant(getSizeInBits(NVT), NVT));
1769 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1770 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1771 break;
1772 case ISD::CTLZ:
1773 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1774 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1775 DAG.getConstant(getSizeInBits(NVT) -
1776 getSizeInBits(VT), NVT));
1777 break;
1778 }
1779 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001780 }
1781
1782 assert(Result.Val && "Didn't set a result!");
1783 AddPromotedOperand(Op, Result);
1784 return Result;
1785}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001786
Chris Lattner84f67882005-01-20 18:52:28 +00001787/// ExpandAddSub - Find a clever way to expand this add operation into
1788/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001789void SelectionDAGLegalize::
1790ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1791 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001792 // Expand the subcomponents.
1793 SDOperand LHSL, LHSH, RHSL, RHSH;
1794 ExpandOp(LHS, LHSL, LHSH);
1795 ExpandOp(RHS, RHSL, RHSH);
1796
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001797 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00001798 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
1799 if (LHSL.getValueType() == MVT::i32) {
1800 SDOperand LowEl;
1801 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1802 if (C->getValue() == 0)
1803 LowEl = RHSL;
1804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1805 if (C->getValue() == 0)
1806 LowEl = LHSL;
1807 if (LowEl.Val) {
1808 // Turn this into an add/sub of the high part only.
1809 SDOperand HiEl =
1810 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1811 LowEl.getValueType(), LHSH, RHSH);
1812 Lo = LowEl;
1813 Hi = HiEl;
1814 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001815 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001816 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001817
Chris Lattner84f67882005-01-20 18:52:28 +00001818 std::vector<SDOperand> Ops;
1819 Ops.push_back(LHSL);
1820 Ops.push_back(LHSH);
1821 Ops.push_back(RHSL);
1822 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00001823
1824 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
1825 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001826 Hi = Lo.getValue(1);
1827}
1828
Chris Lattner5b359c62005-04-02 04:00:59 +00001829void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1830 SDOperand Op, SDOperand Amt,
1831 SDOperand &Lo, SDOperand &Hi) {
1832 // Expand the subcomponents.
1833 SDOperand LHSL, LHSH;
1834 ExpandOp(Op, LHSL, LHSH);
1835
1836 std::vector<SDOperand> Ops;
1837 Ops.push_back(LHSL);
1838 Ops.push_back(LHSH);
1839 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00001840 std::vector<MVT::ValueType> VTs;
1841 VTs.push_back(LHSL.getValueType());
1842 VTs.push_back(LHSH.getValueType());
1843 VTs.push_back(Amt.getValueType());
1844 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00001845 Hi = Lo.getValue(1);
1846}
1847
1848
Chris Lattnere34b3962005-01-19 04:19:40 +00001849/// ExpandShift - Try to find a clever way to expand this shift operation out to
1850/// smaller elements. If we can't find a way that is more efficient than a
1851/// libcall on this target, return false. Otherwise, return true with the
1852/// low-parts expanded into Lo and Hi.
1853bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1854 SDOperand &Lo, SDOperand &Hi) {
1855 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1856 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001857
Chris Lattnere34b3962005-01-19 04:19:40 +00001858 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001859 SDOperand ShAmt = LegalizeOp(Amt);
1860 MVT::ValueType ShTy = ShAmt.getValueType();
1861 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1862 unsigned NVTBits = MVT::getSizeInBits(NVT);
1863
1864 // Handle the case when Amt is an immediate. Other cases are currently broken
1865 // and are disabled.
1866 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1867 unsigned Cst = CN->getValue();
1868 // Expand the incoming operand to be shifted, so that we have its parts
1869 SDOperand InL, InH;
1870 ExpandOp(Op, InL, InH);
1871 switch(Opc) {
1872 case ISD::SHL:
1873 if (Cst > VTBits) {
1874 Lo = DAG.getConstant(0, NVT);
1875 Hi = DAG.getConstant(0, NVT);
1876 } else if (Cst > NVTBits) {
1877 Lo = DAG.getConstant(0, NVT);
1878 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001879 } else if (Cst == NVTBits) {
1880 Lo = DAG.getConstant(0, NVT);
1881 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001882 } else {
1883 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1884 Hi = DAG.getNode(ISD::OR, NVT,
1885 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1886 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1887 }
1888 return true;
1889 case ISD::SRL:
1890 if (Cst > VTBits) {
1891 Lo = DAG.getConstant(0, NVT);
1892 Hi = DAG.getConstant(0, NVT);
1893 } else if (Cst > NVTBits) {
1894 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1895 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001896 } else if (Cst == NVTBits) {
1897 Lo = InH;
1898 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001899 } else {
1900 Lo = DAG.getNode(ISD::OR, NVT,
1901 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1902 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1903 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1904 }
1905 return true;
1906 case ISD::SRA:
1907 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001908 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001909 DAG.getConstant(NVTBits-1, ShTy));
1910 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001911 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001912 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001913 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001914 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001915 } else if (Cst == NVTBits) {
1916 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001917 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001918 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001919 } else {
1920 Lo = DAG.getNode(ISD::OR, NVT,
1921 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1922 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1923 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1924 }
1925 return true;
1926 }
1927 }
1928 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1929 // so disable it for now. Currently targets are handling this via SHL_PARTS
1930 // and friends.
1931 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001932
1933 // If we have an efficient select operation (or if the selects will all fold
1934 // away), lower to some complex code, otherwise just emit the libcall.
1935 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1936 !isa<ConstantSDNode>(Amt))
1937 return false;
1938
1939 SDOperand InL, InH;
1940 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001941 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1942 DAG.getConstant(NVTBits, ShTy), ShAmt);
1943
Chris Lattnere5544f82005-01-20 20:29:23 +00001944 // Compare the unmasked shift amount against 32.
1945 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1946 DAG.getConstant(NVTBits, ShTy));
1947
Chris Lattnere34b3962005-01-19 04:19:40 +00001948 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1949 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1950 DAG.getConstant(NVTBits-1, ShTy));
1951 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1952 DAG.getConstant(NVTBits-1, ShTy));
1953 }
1954
1955 if (Opc == ISD::SHL) {
1956 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1957 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1958 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001959 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001960
Chris Lattnere34b3962005-01-19 04:19:40 +00001961 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1962 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1963 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001964 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1965 DAG.getSetCC(ISD::SETEQ,
1966 TLI.getSetCCResultTy(), NAmt,
1967 DAG.getConstant(32, ShTy)),
1968 DAG.getConstant(0, NVT),
1969 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001970 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001971 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001972 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001973 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001974
1975 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001976 if (Opc == ISD::SRA)
1977 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1978 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001979 else
1980 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001981 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001982 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001983 }
1984 return true;
1985}
Chris Lattner77e77a62005-01-21 06:05:23 +00001986
Chris Lattner9530ddc2005-05-13 05:09:11 +00001987/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
1988/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001989/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001990static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001991 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1992
Chris Lattner16cd04d2005-05-12 23:24:06 +00001993 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001994 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00001995 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001996 Found = Node;
1997 return;
1998 }
1999
2000 // Otherwise, scan the operands of Node to see if any of them is a call.
2001 assert(Node->getNumOperands() != 0 &&
2002 "All leaves should have depth equal to the entry node!");
2003 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002004 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002005
2006 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002007 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002008 Found);
2009}
2010
2011
Chris Lattner9530ddc2005-05-13 05:09:11 +00002012/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2013/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002014/// than Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002015static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002016 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2017
Chris Lattner16cd04d2005-05-12 23:24:06 +00002018 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002019 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002020 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002021 Found = Node;
2022 return;
2023 }
2024
2025 // Otherwise, scan the operands of Node to see if any of them is a call.
2026 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2027 if (UI == E) return;
2028 for (--E; UI != E; ++UI)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002029 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002030
2031 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002032 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002033}
2034
Chris Lattner9530ddc2005-05-13 05:09:11 +00002035/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002036/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002037static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002038 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002039 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002040 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002041 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002042
2043 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002044 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002045
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002046 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002047 if (TheChain.getValueType() != MVT::Other)
2048 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002049 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002050
2051 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002052 E = Node->use_end(); ; ++UI) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002053 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002054
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002055 // Make sure to only follow users of our token chain.
2056 SDNode *User = *UI;
2057 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2058 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002059 if (SDNode *Result = FindCallSeqEnd(User))
2060 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002061 }
2062 assert(0 && "Unreachable");
2063 abort();
2064}
2065
Chris Lattner9530ddc2005-05-13 05:09:11 +00002066/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002067/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002068static SDNode *FindCallSeqStart(SDNode *Node) {
2069 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002070 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002071
2072 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2073 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002074 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002075}
2076
2077
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002078/// FindInputOutputChains - If we are replacing an operation with a call we need
2079/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002080/// properly serialize the calls in the block. The returned operand is the
2081/// input chain value for the new call (e.g. the entry node or the previous
2082/// call), and OutChain is set to be the chain node to update to point to the
2083/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002084static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2085 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002086 SDNode *LatestCallSeqStart = Entry.Val;
2087 SDNode *LatestCallSeqEnd = 0;
2088 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2089 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002090
Chris Lattner16cd04d2005-05-12 23:24:06 +00002091 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002092 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002093 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2094 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002095 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2096 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002097 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002098 LatestCallSeqEnd = Entry.Val;
2099 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002100
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002101 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002102 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002103 OutChain = 0;
Chris Lattner9530ddc2005-05-13 05:09:11 +00002104 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002105
Chris Lattner9530ddc2005-05-13 05:09:11 +00002106 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002107 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002108 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002109
Chris Lattner9530ddc2005-05-13 05:09:11 +00002110 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002111}
2112
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002113/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002114void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2115 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002116 // Nothing to splice it into?
2117 if (OutChain == 0) return;
2118
2119 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2120 //OutChain->dump();
2121
2122 // Form a token factor node merging the old inval and the new inval.
2123 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2124 OutChain->getOperand(0));
2125 // Change the node to refer to the new token.
2126 OutChain->setAdjCallChain(InToken);
2127}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002128
2129
Chris Lattner77e77a62005-01-21 06:05:23 +00002130// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2131// does not fit into a register, return the lo part and set the hi part to the
2132// by-reg argument. If it does fit into a single register, return the result
2133// and leave the Hi part unset.
2134SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2135 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002136 SDNode *OutChain;
2137 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2138 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002139 if (InChain.Val == 0)
2140 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002141
Chris Lattner77e77a62005-01-21 06:05:23 +00002142 TargetLowering::ArgListTy Args;
2143 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2144 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2145 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2146 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2147 }
2148 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002149
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002150 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002151 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002152 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002153 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2154 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002155 SpliceCallInto(CallInfo.second, OutChain);
2156
2157 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002158
2159 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002160 default: assert(0 && "Unknown thing");
2161 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002162 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002163 case Promote:
2164 assert(0 && "Cannot promote this yet!");
2165 case Expand:
2166 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002167 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002168 return Lo;
2169 }
2170}
2171
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002172
Chris Lattner77e77a62005-01-21 06:05:23 +00002173/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2174/// destination type is legal.
2175SDOperand SelectionDAGLegalize::
2176ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2177 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2178 assert(getTypeAction(Source.getValueType()) == Expand &&
2179 "This is not an expansion!");
2180 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2181
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002182 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002183 assert(Source.getValueType() == MVT::i64 &&
2184 "This only works for 64-bit -> FP");
2185 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2186 // incoming integer is set. To handle this, we dynamically test to see if
2187 // it is set, and, if so, add a fudge factor.
2188 SDOperand Lo, Hi;
2189 ExpandOp(Source, Lo, Hi);
2190
Chris Lattner66de05b2005-05-13 04:45:13 +00002191 // If this is unsigned, and not supported, first perform the conversion to
2192 // signed, then adjust the result if the sign bit is set.
2193 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2194 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2195
Chris Lattnere9c35e72005-04-13 05:09:42 +00002196 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2197 DAG.getConstant(0, Hi.getValueType()));
2198 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2199 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2200 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002201 uint64_t FF = 0x5f800000ULL;
2202 if (TLI.isLittleEndian()) FF <<= 32;
2203 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002204
2205 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2206 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2207 TLI.getPointerTy());
2208 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2209 SDOperand FudgeInReg;
2210 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002211 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2212 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002213 else {
2214 assert(DestTy == MVT::f64 && "Unexpected conversion");
2215 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002216 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002217 }
2218 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002219 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002220
Chris Lattnera88a2602005-05-14 05:33:54 +00002221 // Check to see if the target has a custom way to lower this. If so, use it.
2222 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2223 default: assert(0 && "This action not implemented for this operation!");
2224 case TargetLowering::Legal:
2225 case TargetLowering::Expand:
2226 break; // This case is handled below.
2227 case TargetLowering::Custom:
2228 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002229 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002230 }
2231
Chris Lattner13689e22005-05-12 07:00:44 +00002232 // Expand the source, then glue it back together for the call. We must expand
2233 // the source in case it is shared (this pass of legalize must traverse it).
2234 SDOperand SrcLo, SrcHi;
2235 ExpandOp(Source, SrcLo, SrcHi);
2236 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2237
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002238 SDNode *OutChain = 0;
2239 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2240 DAG.getEntryNode());
2241 const char *FnName = 0;
2242 if (DestTy == MVT::f32)
2243 FnName = "__floatdisf";
2244 else {
2245 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2246 FnName = "__floatdidf";
2247 }
2248
Chris Lattner77e77a62005-01-21 06:05:23 +00002249 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2250
2251 TargetLowering::ArgListTy Args;
2252 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002253
Chris Lattner77e77a62005-01-21 06:05:23 +00002254 Args.push_back(std::make_pair(Source, ArgTy));
2255
2256 // We don't care about token chains for libcalls. We just use the entry
2257 // node as our input and ignore the output chain. This allows us to place
2258 // calls wherever we need them to satisfy data dependences.
2259 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002260
2261 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002262 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2263 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002264
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002265 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002266 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002267}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002268
Chris Lattnere34b3962005-01-19 04:19:40 +00002269
2270
Chris Lattner3e928bb2005-01-07 07:47:09 +00002271/// ExpandOp - Expand the specified SDOperand into its two component pieces
2272/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2273/// LegalizeNodes map is filled in for any results that are not expanded, the
2274/// ExpandedNodes map is filled in for any results that are expanded, and the
2275/// Lo/Hi values are returned.
2276void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2277 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002278 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002279 SDNode *Node = Op.Val;
2280 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2281 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2282 assert(MVT::isInteger(NVT) && NVT < VT &&
2283 "Cannot expand to FP value or to larger int value!");
2284
2285 // If there is more than one use of this, see if we already expanded it.
2286 // There is no use remembering values that only have a single use, as the map
2287 // entries will never be reused.
2288 if (!Node->hasOneUse()) {
2289 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2290 = ExpandedNodes.find(Op);
2291 if (I != ExpandedNodes.end()) {
2292 Lo = I->second.first;
2293 Hi = I->second.second;
2294 return;
2295 }
Chris Lattner45982da2005-05-12 16:53:42 +00002296 } else {
2297 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002298 }
2299
Chris Lattner4e6c7462005-01-08 19:27:05 +00002300 // Expanding to multiple registers needs to perform an optimization step, and
2301 // is not careful to avoid operations the target does not support. Make sure
2302 // that all generated operations are legalized in the next iteration.
2303 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002304
Chris Lattner3e928bb2005-01-07 07:47:09 +00002305 switch (Node->getOpcode()) {
2306 default:
2307 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2308 assert(0 && "Do not know how to expand this operator!");
2309 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002310 case ISD::UNDEF:
2311 Lo = DAG.getNode(ISD::UNDEF, NVT);
2312 Hi = DAG.getNode(ISD::UNDEF, NVT);
2313 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002314 case ISD::Constant: {
2315 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2316 Lo = DAG.getConstant(Cst, NVT);
2317 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2318 break;
2319 }
2320
2321 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002322 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002323 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002324 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2325 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002326
Chris Lattner69a52152005-01-14 22:38:01 +00002327 // Remember that we legalized the chain.
2328 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2329
Chris Lattner3e928bb2005-01-07 07:47:09 +00002330 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2331 break;
2332 }
2333
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002334 case ISD::BUILD_PAIR:
2335 // Legalize both operands. FIXME: in the future we should handle the case
2336 // where the two elements are not legal.
2337 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2338 Lo = LegalizeOp(Node->getOperand(0));
2339 Hi = LegalizeOp(Node->getOperand(1));
2340 break;
2341
Chris Lattneredb1add2005-05-11 04:51:16 +00002342 case ISD::CTPOP:
2343 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002344 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2345 DAG.getNode(ISD::CTPOP, NVT, Lo),
2346 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002347 Hi = DAG.getConstant(0, NVT);
2348 break;
2349
Chris Lattner39a8f332005-05-12 19:05:01 +00002350 case ISD::CTLZ: {
2351 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002352 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002353 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2354 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2355 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2356 HLZ, BitsC);
2357 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2358 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2359
2360 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2361 Hi = DAG.getConstant(0, NVT);
2362 break;
2363 }
2364
2365 case ISD::CTTZ: {
2366 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002367 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002368 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2369 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2370 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2371 LTZ, BitsC);
2372 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2373 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2374
2375 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2376 Hi = DAG.getConstant(0, NVT);
2377 break;
2378 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002379
Chris Lattner3e928bb2005-01-07 07:47:09 +00002380 case ISD::LOAD: {
2381 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2382 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002383 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002384
2385 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002386 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002387 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2388 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002389 //Is this safe? declaring that the two parts of the split load
2390 //are from the same instruction?
2391 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002392
2393 // Build a factor node to remember that this load is independent of the
2394 // other one.
2395 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2396 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002397
Chris Lattner3e928bb2005-01-07 07:47:09 +00002398 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002399 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002400 if (!TLI.isLittleEndian())
2401 std::swap(Lo, Hi);
2402 break;
2403 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002404 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002405 case ISD::CALL: {
2406 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2407 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2408
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002409 bool Changed = false;
2410 std::vector<SDOperand> Ops;
2411 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2412 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2413 Changed |= Ops.back() != Node->getOperand(i);
2414 }
2415
Chris Lattner3e928bb2005-01-07 07:47:09 +00002416 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2417 "Can only expand a call once so far, not i64 -> i16!");
2418
2419 std::vector<MVT::ValueType> RetTyVTs;
2420 RetTyVTs.reserve(3);
2421 RetTyVTs.push_back(NVT);
2422 RetTyVTs.push_back(NVT);
2423 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002424 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2425 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002426 Lo = SDOperand(NC, 0);
2427 Hi = SDOperand(NC, 1);
2428
2429 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002430 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002431 break;
2432 }
2433 case ISD::AND:
2434 case ISD::OR:
2435 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2436 SDOperand LL, LH, RL, RH;
2437 ExpandOp(Node->getOperand(0), LL, LH);
2438 ExpandOp(Node->getOperand(1), RL, RH);
2439 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2440 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2441 break;
2442 }
2443 case ISD::SELECT: {
2444 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002445
2446 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2447 case Expand: assert(0 && "It's impossible to expand bools");
2448 case Legal:
2449 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2450 break;
2451 case Promote:
2452 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2453 break;
2454 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002455 ExpandOp(Node->getOperand(1), LL, LH);
2456 ExpandOp(Node->getOperand(2), RL, RH);
2457 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2458 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2459 break;
2460 }
2461 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002462 SDOperand In;
2463 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2464 case Expand: assert(0 && "expand-expand not implemented yet!");
2465 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2466 case Promote:
2467 In = PromoteOp(Node->getOperand(0));
2468 // Emit the appropriate sign_extend_inreg to get the value we want.
2469 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2470 Node->getOperand(0).getValueType());
2471 break;
2472 }
2473
Chris Lattner3e928bb2005-01-07 07:47:09 +00002474 // The low part is just a sign extension of the input (which degenerates to
2475 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002476 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002477
Chris Lattner3e928bb2005-01-07 07:47:09 +00002478 // The high part is obtained by SRA'ing all but one of the bits of the lo
2479 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002480 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002481 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2482 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002483 break;
2484 }
Chris Lattner06098e02005-04-03 23:41:52 +00002485 case ISD::ZERO_EXTEND: {
2486 SDOperand In;
2487 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2488 case Expand: assert(0 && "expand-expand not implemented yet!");
2489 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2490 case Promote:
2491 In = PromoteOp(Node->getOperand(0));
2492 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002493 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002494 break;
2495 }
2496
Chris Lattner3e928bb2005-01-07 07:47:09 +00002497 // The low part is just a zero extension of the input (which degenerates to
2498 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002499 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002500
Chris Lattner3e928bb2005-01-07 07:47:09 +00002501 // The high part is just a zero.
2502 Hi = DAG.getConstant(0, NVT);
2503 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002504 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002505 // These operators cannot be expanded directly, emit them as calls to
2506 // library functions.
2507 case ISD::FP_TO_SINT:
2508 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002509 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002510 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002511 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002512 break;
2513 case ISD::FP_TO_UINT:
2514 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002515 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002516 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002517 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002518 break;
2519
Chris Lattnere34b3962005-01-19 04:19:40 +00002520 case ISD::SHL:
2521 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002522 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002523 break;
Chris Lattner47599822005-04-02 03:38:53 +00002524
2525 // If this target supports SHL_PARTS, use it.
2526 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002527 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2528 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002529 break;
2530 }
2531
Chris Lattnere34b3962005-01-19 04:19:40 +00002532 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002533 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002534 break;
2535
2536 case ISD::SRA:
2537 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002538 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002539 break;
Chris Lattner47599822005-04-02 03:38:53 +00002540
2541 // If this target supports SRA_PARTS, use it.
2542 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002543 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2544 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002545 break;
2546 }
2547
Chris Lattnere34b3962005-01-19 04:19:40 +00002548 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002549 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002550 break;
2551 case ISD::SRL:
2552 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002553 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002554 break;
Chris Lattner47599822005-04-02 03:38:53 +00002555
2556 // If this target supports SRL_PARTS, use it.
2557 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002558 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2559 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002560 break;
2561 }
2562
Chris Lattnere34b3962005-01-19 04:19:40 +00002563 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002564 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002565 break;
2566
Misha Brukmanedf128a2005-04-21 22:36:52 +00002567 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002568 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2569 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002570 break;
2571 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002572 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2573 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002574 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002575 case ISD::MUL: {
2576 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2577 SDOperand LL, LH, RL, RH;
2578 ExpandOp(Node->getOperand(0), LL, LH);
2579 ExpandOp(Node->getOperand(1), RL, RH);
2580 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2581 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2582 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2583 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2584 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2585 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2586 } else {
2587 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2588 }
2589 break;
2590 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002591 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2592 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2593 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2594 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002595 }
2596
2597 // Remember in a map if the values will be reused later.
2598 if (!Node->hasOneUse()) {
2599 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2600 std::make_pair(Lo, Hi))).second;
2601 assert(isNew && "Value already expanded?!?");
2602 }
2603}
2604
2605
2606// SelectionDAG::Legalize - This is the entry point for the file.
2607//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002608void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002609 /// run - This is the main entry point to this class.
2610 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002611 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002612}
2613