blob: 680b2ce9bca95a87b5aa7a70376d0d2f283af2be [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) ||
376 Tmp3 != Node->getOperand(2))
377 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
378 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000379 else
380 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000381
382 // Since this op produces two values, make sure to remember that we
383 // legalized both of them.
384 AddLegalizedOperand(SDOperand(Node, 0), Result);
385 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
386 return Result.getValue(Op.ResNo);
387
Chris Lattnerd71c0412005-05-13 18:43:43 +0000388 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000389 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000390 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
391 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000392
393 bool Changed = false;
394 std::vector<SDOperand> Ops;
395 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
396 Ops.push_back(LegalizeOp(Node->getOperand(i)));
397 Changed |= Ops.back() != Node->getOperand(i);
398 }
399
400 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000401 std::vector<MVT::ValueType> RetTyVTs;
402 RetTyVTs.reserve(Node->getNumValues());
403 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000404 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000405 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
406 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000407 } else {
408 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000409 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000410 // Since calls produce multiple values, make sure to remember that we
411 // legalized all of them.
412 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
413 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
414 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000415 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000416 case ISD::BR:
417 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
418 if (Tmp1 != Node->getOperand(0))
419 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
420 break;
421
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000422 case ISD::BRCOND:
423 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000424
425 switch (getTypeAction(Node->getOperand(1).getValueType())) {
426 case Expand: assert(0 && "It's impossible to expand bools");
427 case Legal:
428 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
429 break;
430 case Promote:
431 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
432 break;
433 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000434 // Basic block destination (Op#2) is always legal.
435 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
436 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
437 Node->getOperand(2));
438 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000439 case ISD::BRCONDTWOWAY:
440 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
441 switch (getTypeAction(Node->getOperand(1).getValueType())) {
442 case Expand: assert(0 && "It's impossible to expand bools");
443 case Legal:
444 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
445 break;
446 case Promote:
447 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
448 break;
449 }
450 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
451 // pair.
452 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
453 case TargetLowering::Promote:
454 default: assert(0 && "This action is not supported yet!");
455 case TargetLowering::Legal:
456 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
457 std::vector<SDOperand> Ops;
458 Ops.push_back(Tmp1);
459 Ops.push_back(Tmp2);
460 Ops.push_back(Node->getOperand(2));
461 Ops.push_back(Node->getOperand(3));
462 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
463 }
464 break;
465 case TargetLowering::Expand:
466 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
467 Node->getOperand(2));
468 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
469 break;
470 }
471 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000472
Chris Lattner3e928bb2005-01-07 07:47:09 +0000473 case ISD::LOAD:
474 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
475 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000476
Chris Lattner3e928bb2005-01-07 07:47:09 +0000477 if (Tmp1 != Node->getOperand(0) ||
478 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000479 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
480 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000481 else
482 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000483
Chris Lattner8afc48e2005-01-07 22:28:47 +0000484 // Since loads produce two values, make sure to remember that we legalized
485 // both of them.
486 AddLegalizedOperand(SDOperand(Node, 0), Result);
487 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
488 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000489
Chris Lattner0f69b292005-01-15 06:18:18 +0000490 case ISD::EXTLOAD:
491 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000492 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000493 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
494 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000495
Chris Lattner01ff7212005-04-10 22:54:25 +0000496 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
497 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000498 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000499 case TargetLowering::Promote:
500 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
501 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000502 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000503 // Since loads produce two values, make sure to remember that we legalized
504 // both of them.
505 AddLegalizedOperand(SDOperand(Node, 0), Result);
506 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
507 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000508
Chris Lattner01ff7212005-04-10 22:54:25 +0000509 case TargetLowering::Legal:
510 if (Tmp1 != Node->getOperand(0) ||
511 Tmp2 != Node->getOperand(1))
512 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000513 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000514 else
515 Result = SDOperand(Node, 0);
516
517 // Since loads produce two values, make sure to remember that we legalized
518 // both of them.
519 AddLegalizedOperand(SDOperand(Node, 0), Result);
520 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
521 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000522 case TargetLowering::Expand:
523 assert(Node->getOpcode() != ISD::EXTLOAD &&
524 "EXTLOAD should always be supported!");
525 // Turn the unsupported load into an EXTLOAD followed by an explicit
526 // zero/sign extend inreg.
527 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000528 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000529 SDOperand ValRes;
530 if (Node->getOpcode() == ISD::SEXTLOAD)
531 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
532 Result, SrcVT);
533 else
534 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000535 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
536 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
537 if (Op.ResNo)
538 return Result.getValue(1);
539 return ValRes;
540 }
541 assert(0 && "Unreachable");
542 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000543 case ISD::EXTRACT_ELEMENT:
544 // Get both the low and high parts.
545 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
546 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
547 Result = Tmp2; // 1 -> Hi
548 else
549 Result = Tmp1; // 0 -> Lo
550 break;
551
552 case ISD::CopyToReg:
553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000554
Chris Lattner3e928bb2005-01-07 07:47:09 +0000555 switch (getTypeAction(Node->getOperand(1).getValueType())) {
556 case Legal:
557 // Legalize the incoming value (must be legal).
558 Tmp2 = LegalizeOp(Node->getOperand(1));
559 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000560 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000561 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000562 case Promote:
563 Tmp2 = PromoteOp(Node->getOperand(1));
564 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
565 break;
566 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000567 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000568 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000569 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000570 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
571 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
572 // Note that the copytoreg nodes are independent of each other.
573 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000574 assert(isTypeLegal(Result.getValueType()) &&
575 "Cannot expand multiple times yet (i64 -> i16)");
576 break;
577 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000578 break;
579
580 case ISD::RET:
581 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
582 switch (Node->getNumOperands()) {
583 case 2: // ret val
584 switch (getTypeAction(Node->getOperand(1).getValueType())) {
585 case Legal:
586 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000587 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000588 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
589 break;
590 case Expand: {
591 SDOperand Lo, Hi;
592 ExpandOp(Node->getOperand(1), Lo, Hi);
593 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000594 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000595 }
596 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000597 Tmp2 = PromoteOp(Node->getOperand(1));
598 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
599 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000600 }
601 break;
602 case 1: // ret void
603 if (Tmp1 != Node->getOperand(0))
604 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
605 break;
606 default: { // ret <values>
607 std::vector<SDOperand> NewValues;
608 NewValues.push_back(Tmp1);
609 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
610 switch (getTypeAction(Node->getOperand(i).getValueType())) {
611 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000612 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000613 break;
614 case Expand: {
615 SDOperand Lo, Hi;
616 ExpandOp(Node->getOperand(i), Lo, Hi);
617 NewValues.push_back(Lo);
618 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000619 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000620 }
621 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000622 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000623 }
624 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
625 break;
626 }
627 }
628 break;
629 case ISD::STORE:
630 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
631 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
632
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000633 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000634 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000635 if (CFP->getValueType(0) == MVT::f32) {
636 union {
637 unsigned I;
638 float F;
639 } V;
640 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000641 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000642 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000643 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000644 } else {
645 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
646 union {
647 uint64_t I;
648 double F;
649 } V;
650 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000651 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000652 DAG.getConstant(V.I, MVT::i64), Tmp2,
653 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000654 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000655 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000656 }
657
Chris Lattner3e928bb2005-01-07 07:47:09 +0000658 switch (getTypeAction(Node->getOperand(1).getValueType())) {
659 case Legal: {
660 SDOperand Val = LegalizeOp(Node->getOperand(1));
661 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
662 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000663 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
664 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000665 break;
666 }
667 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000668 // Truncate the value and store the result.
669 Tmp3 = PromoteOp(Node->getOperand(1));
670 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000671 Node->getOperand(3),
672 Node->getOperand(1).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000673 break;
674
Chris Lattner3e928bb2005-01-07 07:47:09 +0000675 case Expand:
676 SDOperand Lo, Hi;
677 ExpandOp(Node->getOperand(1), Lo, Hi);
678
679 if (!TLI.isLittleEndian())
680 std::swap(Lo, Hi);
681
Chris Lattneredb1add2005-05-11 04:51:16 +0000682 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
683 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000684 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
686 getIntPtrConstant(IncrementSize));
687 assert(isTypeLegal(Tmp2.getValueType()) &&
688 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000689 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000690 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
691 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000692 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
693 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000694 }
695 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000696 case ISD::PCMARKER:
697 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000698 if (Tmp1 != Node->getOperand(0))
699 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000700 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000701 case ISD::TRUNCSTORE:
702 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
703 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
704
705 switch (getTypeAction(Node->getOperand(1).getValueType())) {
706 case Legal:
707 Tmp2 = LegalizeOp(Node->getOperand(1));
708 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
709 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000710 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000711 Node->getOperand(3),
Chris Lattner0f69b292005-01-15 06:18:18 +0000712 cast<MVTSDNode>(Node)->getExtraValueType());
713 break;
714 case Promote:
715 case Expand:
716 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
717 }
718 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000719 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000720 switch (getTypeAction(Node->getOperand(0).getValueType())) {
721 case Expand: assert(0 && "It's impossible to expand bools");
722 case Legal:
723 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
724 break;
725 case Promote:
726 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
727 break;
728 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000729 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000730 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000731
732 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
733 default: assert(0 && "This action is not supported yet!");
734 case TargetLowering::Legal:
735 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
736 Tmp3 != Node->getOperand(2))
737 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
738 Tmp1, Tmp2, Tmp3);
739 break;
740 case TargetLowering::Promote: {
741 MVT::ValueType NVT =
742 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
743 unsigned ExtOp, TruncOp;
744 if (MVT::isInteger(Tmp2.getValueType())) {
745 ExtOp = ISD::ZERO_EXTEND;
746 TruncOp = ISD::TRUNCATE;
747 } else {
748 ExtOp = ISD::FP_EXTEND;
749 TruncOp = ISD::FP_ROUND;
750 }
751 // Promote each of the values to the new type.
752 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
753 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
754 // Perform the larger operation, then round down.
755 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
756 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
757 break;
758 }
759 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000760 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000761 case ISD::SETCC:
762 switch (getTypeAction(Node->getOperand(0).getValueType())) {
763 case Legal:
764 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
765 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
766 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
767 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000768 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000769 break;
770 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000771 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
772 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
773
774 // If this is an FP compare, the operands have already been extended.
775 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
776 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000777 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000778
779 // Otherwise, we have to insert explicit sign or zero extends. Note
780 // that we could insert sign extends for ALL conditions, but zero extend
781 // is cheaper on many machines (an AND instead of two shifts), so prefer
782 // it.
783 switch (cast<SetCCSDNode>(Node)->getCondition()) {
784 default: assert(0 && "Unknown integer comparison!");
785 case ISD::SETEQ:
786 case ISD::SETNE:
787 case ISD::SETUGE:
788 case ISD::SETUGT:
789 case ISD::SETULE:
790 case ISD::SETULT:
791 // ALL of these operations will work if we either sign or zero extend
792 // the operands (including the unsigned comparisons!). Zero extend is
793 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000794 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
795 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000796 break;
797 case ISD::SETGE:
798 case ISD::SETGT:
799 case ISD::SETLT:
800 case ISD::SETLE:
801 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
802 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
803 break;
804 }
805
806 }
807 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000808 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000809 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000810 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000811 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
812 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
813 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
814 switch (cast<SetCCSDNode>(Node)->getCondition()) {
815 case ISD::SETEQ:
816 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000817 if (RHSLo == RHSHi)
818 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
819 if (RHSCST->isAllOnesValue()) {
820 // Comparison to -1.
821 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000822 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000823 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000824 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000825 }
826
Chris Lattner3e928bb2005-01-07 07:47:09 +0000827 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
828 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
829 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000830 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000831 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000832 DAG.getConstant(0, Tmp1.getValueType()));
833 break;
834 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000835 // If this is a comparison of the sign bit, just look at the top part.
836 // X > -1, x < 0
837 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000838 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000839 CST->getValue() == 0) || // X < 0
840 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
841 (CST->isAllOnesValue()))) // X > -1
842 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
843 Node->getValueType(0), LHSHi, RHSHi);
844
Chris Lattner3e928bb2005-01-07 07:47:09 +0000845 // FIXME: This generated code sucks.
846 ISD::CondCode LowCC;
847 switch (cast<SetCCSDNode>(Node)->getCondition()) {
848 default: assert(0 && "Unknown integer setcc!");
849 case ISD::SETLT:
850 case ISD::SETULT: LowCC = ISD::SETULT; break;
851 case ISD::SETGT:
852 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
853 case ISD::SETLE:
854 case ISD::SETULE: LowCC = ISD::SETULE; break;
855 case ISD::SETGE:
856 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
857 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000858
Chris Lattner3e928bb2005-01-07 07:47:09 +0000859 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
860 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
861 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
862
863 // NOTE: on targets without efficient SELECT of bools, we can always use
864 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000865 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000866 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000867 Node->getValueType(0), LHSHi, RHSHi);
868 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
869 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
870 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000871 break;
872 }
873 }
874 break;
875
Chris Lattnere1bd8222005-01-11 05:57:22 +0000876 case ISD::MEMSET:
877 case ISD::MEMCPY:
878 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000880 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
881
882 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
883 switch (getTypeAction(Node->getOperand(2).getValueType())) {
884 case Expand: assert(0 && "Cannot expand a byte!");
885 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000886 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000887 break;
888 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000889 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000890 break;
891 }
892 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000893 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000894 }
Chris Lattner272455b2005-02-02 03:44:41 +0000895
896 SDOperand Tmp4;
897 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000898 case Expand: assert(0 && "Cannot expand this yet!");
899 case Legal:
900 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000901 break;
902 case Promote:
903 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000904 break;
905 }
906
907 SDOperand Tmp5;
908 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
909 case Expand: assert(0 && "Cannot expand this yet!");
910 case Legal:
911 Tmp5 = LegalizeOp(Node->getOperand(4));
912 break;
913 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000914 Tmp5 = PromoteOp(Node->getOperand(4));
915 break;
916 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000917
918 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
919 default: assert(0 && "This action not implemented for this operation!");
920 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000921 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
922 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
923 Tmp5 != Node->getOperand(4)) {
924 std::vector<SDOperand> Ops;
925 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
926 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
927 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
928 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000929 break;
930 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000931 // Otherwise, the target does not support this operation. Lower the
932 // operation to an explicit libcall as appropriate.
933 MVT::ValueType IntPtr = TLI.getPointerTy();
934 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
935 std::vector<std::pair<SDOperand, const Type*> > Args;
936
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000937 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000938 if (Node->getOpcode() == ISD::MEMSET) {
939 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
940 // Extend the ubyte argument to be an int value for the call.
941 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
942 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
943 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
944
945 FnName = "memset";
946 } else if (Node->getOpcode() == ISD::MEMCPY ||
947 Node->getOpcode() == ISD::MEMMOVE) {
948 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
949 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
950 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
951 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
952 } else {
953 assert(0 && "Unknown op!");
954 }
Chris Lattner45982da2005-05-12 16:53:42 +0000955
Chris Lattnere1bd8222005-01-11 05:57:22 +0000956 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +0000957 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000958 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
959 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000960 break;
961 }
962 case TargetLowering::Custom:
963 std::vector<SDOperand> Ops;
964 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
965 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
966 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +0000967 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000968 Result = LegalizeOp(Result);
969 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000970 }
971 break;
972 }
Chris Lattner52d08bd2005-05-09 20:23:03 +0000973
974 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000975 Tmp1 = LegalizeOp(Node->getOperand(0));
976 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000977
Chris Lattner52d08bd2005-05-09 20:23:03 +0000978 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000979 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000980 else
981 Result = SDOperand(Node, 0);
982 // Since these produce two values, make sure to remember that we legalized
983 // both of them.
984 AddLegalizedOperand(SDOperand(Node, 0), Result);
985 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
986 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000987 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000988 Tmp1 = LegalizeOp(Node->getOperand(0));
989 Tmp2 = LegalizeOp(Node->getOperand(1));
990 Tmp3 = LegalizeOp(Node->getOperand(2));
991 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
992 Tmp3 != Node->getOperand(2))
993 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
994 break;
995
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000996 case ISD::READIO:
997 Tmp1 = LegalizeOp(Node->getOperand(0));
998 Tmp2 = LegalizeOp(Node->getOperand(1));
999
1000 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1001 case TargetLowering::Custom:
1002 default: assert(0 && "This action not implemented for this operation!");
1003 case TargetLowering::Legal:
1004 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1005 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
1006 Tmp1, Tmp2);
1007 else
1008 Result = SDOperand(Node, 0);
1009 break;
1010 case TargetLowering::Expand:
1011 // Replace this with a load from memory.
1012 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1013 Node->getOperand(1), DAG.getSrcValue(NULL));
1014 Result = LegalizeOp(Result);
1015 break;
1016 }
1017
1018 // Since these produce two values, make sure to remember that we legalized
1019 // both of them.
1020 AddLegalizedOperand(SDOperand(Node, 0), Result);
1021 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1022 return Result.getValue(Op.ResNo);
1023
1024 case ISD::WRITEIO:
1025 Tmp1 = LegalizeOp(Node->getOperand(0));
1026 Tmp2 = LegalizeOp(Node->getOperand(1));
1027 Tmp3 = LegalizeOp(Node->getOperand(2));
1028
1029 switch (TLI.getOperationAction(Node->getOpcode(),
1030 Node->getOperand(1).getValueType())) {
1031 case TargetLowering::Custom:
1032 default: assert(0 && "This action not implemented for this operation!");
1033 case TargetLowering::Legal:
1034 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1035 Tmp3 != Node->getOperand(2))
1036 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1037 break;
1038 case TargetLowering::Expand:
1039 // Replace this with a store to memory.
1040 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1041 Node->getOperand(1), Node->getOperand(2),
1042 DAG.getSrcValue(NULL));
1043 Result = LegalizeOp(Result);
1044 break;
1045 }
1046 break;
1047
Chris Lattner84f67882005-01-20 18:52:28 +00001048 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001049 case ISD::SUB_PARTS:
1050 case ISD::SHL_PARTS:
1051 case ISD::SRA_PARTS:
1052 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001053 std::vector<SDOperand> Ops;
1054 bool Changed = false;
1055 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1056 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1057 Changed |= Ops.back() != Node->getOperand(i);
1058 }
1059 if (Changed)
1060 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001061
1062 // Since these produce multiple values, make sure to remember that we
1063 // legalized all of them.
1064 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1065 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1066 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001067 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001068
1069 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001070 case ISD::ADD:
1071 case ISD::SUB:
1072 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001073 case ISD::MULHS:
1074 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001075 case ISD::UDIV:
1076 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001077 case ISD::AND:
1078 case ISD::OR:
1079 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001080 case ISD::SHL:
1081 case ISD::SRL:
1082 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001083 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1084 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1085 if (Tmp1 != Node->getOperand(0) ||
1086 Tmp2 != Node->getOperand(1))
1087 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1088 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001089
Nate Begemanc105e192005-04-06 00:23:54 +00001090 case ISD::UREM:
1091 case ISD::SREM:
1092 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1093 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1094 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1095 case TargetLowering::Legal:
1096 if (Tmp1 != Node->getOperand(0) ||
1097 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001098 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001099 Tmp2);
1100 break;
1101 case TargetLowering::Promote:
1102 case TargetLowering::Custom:
1103 assert(0 && "Cannot promote/custom handle this yet!");
1104 case TargetLowering::Expand: {
1105 MVT::ValueType VT = Node->getValueType(0);
1106 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1107 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1108 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1109 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1110 }
1111 break;
1112 }
1113 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001114
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001115 case ISD::CTPOP:
1116 case ISD::CTTZ:
1117 case ISD::CTLZ:
1118 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1119 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1120 case TargetLowering::Legal:
1121 if (Tmp1 != Node->getOperand(0))
1122 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1123 break;
1124 case TargetLowering::Promote: {
1125 MVT::ValueType OVT = Tmp1.getValueType();
1126 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001127
1128 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001129 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1130 // Perform the larger operation, then subtract if needed.
1131 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1132 switch(Node->getOpcode())
1133 {
1134 case ISD::CTPOP:
1135 Result = Tmp1;
1136 break;
1137 case ISD::CTTZ:
1138 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner39a8f332005-05-12 19:05:01 +00001139 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001140 DAG.getConstant(getSizeInBits(NVT), NVT));
1141 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1142 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1143 break;
1144 case ISD::CTLZ:
1145 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1146 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1147 DAG.getConstant(getSizeInBits(NVT) -
1148 getSizeInBits(OVT), NVT));
1149 break;
1150 }
1151 break;
1152 }
1153 case TargetLowering::Custom:
1154 assert(0 && "Cannot custom handle this yet!");
1155 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001156 switch(Node->getOpcode())
1157 {
1158 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001159 static const uint64_t mask[6] = {
1160 0x5555555555555555ULL, 0x3333333333333333ULL,
1161 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1162 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1163 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001164 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001165 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1166 unsigned len = getSizeInBits(VT);
1167 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001168 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001169 Tmp2 = DAG.getConstant(mask[i], VT);
1170 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001171 Tmp1 = DAG.getNode(ISD::ADD, VT,
1172 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1173 DAG.getNode(ISD::AND, VT,
1174 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1175 Tmp2));
1176 }
1177 Result = Tmp1;
1178 break;
1179 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001180 case ISD::CTLZ: {
1181 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001182 x = x | (x >> 1);
1183 x = x | (x >> 2);
1184 ...
1185 x = x | (x >>16);
1186 x = x | (x >>32); // for 64-bit input
1187 return popcount(~x);
Duraid Madina57ff7e52005-05-11 08:45:08 +00001188
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001189 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1190 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001191 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1192 unsigned len = getSizeInBits(VT);
1193 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1194 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1195 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1196 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1197 }
1198 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001199 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001200 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001201 }
1202 case ISD::CTTZ: {
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001203 // for now, we use: { return popcount(~x & (x - 1)); }
1204 // unless the target has ctlz but not ctpop, in which case we use:
1205 // { return 32 - nlz(~x & (x-1)); }
1206 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001207 MVT::ValueType VT = Tmp1.getValueType();
1208 Tmp2 = DAG.getConstant(~0ULL, VT);
1209 Tmp3 = DAG.getNode(ISD::AND, VT,
1210 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1211 DAG.getNode(ISD::SUB, VT, Tmp1,
1212 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001213 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1214 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1215 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1216 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1217 DAG.getConstant(getSizeInBits(VT), VT),
1218 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1219 } else {
1220 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1221 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001222 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001223 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001224 default:
1225 assert(0 && "Cannot expand this yet!");
1226 break;
1227 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001228 break;
1229 }
1230 break;
1231
Chris Lattner2c8086f2005-04-02 05:00:07 +00001232 // Unary operators
1233 case ISD::FABS:
1234 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001235 case ISD::FSQRT:
1236 case ISD::FSIN:
1237 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001238 Tmp1 = LegalizeOp(Node->getOperand(0));
1239 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1240 case TargetLowering::Legal:
1241 if (Tmp1 != Node->getOperand(0))
1242 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1243 break;
1244 case TargetLowering::Promote:
1245 case TargetLowering::Custom:
1246 assert(0 && "Cannot promote/custom handle this yet!");
1247 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001248 switch(Node->getOpcode()) {
1249 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001250 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1251 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1252 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1253 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001254 break;
1255 }
1256 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001257 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1258 MVT::ValueType VT = Node->getValueType(0);
1259 Tmp2 = DAG.getConstantFP(0.0, VT);
1260 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1261 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1262 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1263 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001264 break;
1265 }
1266 case ISD::FSQRT:
1267 case ISD::FSIN:
1268 case ISD::FCOS: {
1269 MVT::ValueType VT = Node->getValueType(0);
1270 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1271 const char *FnName = 0;
1272 switch(Node->getOpcode()) {
1273 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1274 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1275 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1276 default: assert(0 && "Unreachable!");
1277 }
1278 std::vector<std::pair<SDOperand, const Type*> > Args;
1279 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001280 // FIXME: should use ExpandLibCall!
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001281 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001282 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001283 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1284 Result = LegalizeOp(CallResult.first);
1285 break;
1286 }
1287 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001288 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001289 }
1290 break;
1291 }
1292 break;
1293
1294 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001295 case ISD::ZERO_EXTEND:
1296 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001297 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001298 case ISD::FP_EXTEND:
1299 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001300 case ISD::FP_TO_SINT:
1301 case ISD::FP_TO_UINT:
1302 case ISD::SINT_TO_FP:
1303 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001304 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1305 case Legal:
1306 Tmp1 = LegalizeOp(Node->getOperand(0));
1307 if (Tmp1 != Node->getOperand(0))
1308 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1309 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001310 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001311 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1312 Node->getOpcode() == ISD::UINT_TO_FP) {
1313 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1314 Node->getValueType(0), Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001315 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001316 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1317 // In the expand case, we must be dealing with a truncate, because
1318 // otherwise the result would be larger than the source.
1319 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001320
Chris Lattner2c8086f2005-04-02 05:00:07 +00001321 // Since the result is legal, we should just be able to truncate the low
1322 // part of the source.
1323 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1324 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001325 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001326 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001327
Chris Lattner03c85462005-01-15 05:21:40 +00001328 case Promote:
1329 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001330 case ISD::ZERO_EXTEND:
1331 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001332 // NOTE: Any extend would work here...
1333 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001334 Result = DAG.getZeroExtendInReg(Result,
1335 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001336 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001337 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001338 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001339 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001340 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001341 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1342 Result, Node->getOperand(0).getValueType());
1343 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001344 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001345 Result = PromoteOp(Node->getOperand(0));
1346 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1347 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001348 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001349 Result = PromoteOp(Node->getOperand(0));
1350 if (Result.getValueType() != Op.getValueType())
1351 // Dynamically dead while we have only 2 FP types.
1352 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1353 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001354 case ISD::FP_ROUND:
1355 case ISD::FP_TO_SINT:
1356 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001357 Result = PromoteOp(Node->getOperand(0));
1358 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1359 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001360 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001361 Result = PromoteOp(Node->getOperand(0));
1362 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1363 Result, Node->getOperand(0).getValueType());
1364 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1365 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001366 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001367 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001368 Result = DAG.getZeroExtendInReg(Result,
1369 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001370 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1371 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001372 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001373 }
1374 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001375 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001376 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001377 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001378 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1379
1380 // If this operation is not supported, convert it to a shl/shr or load/store
1381 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001382 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1383 default: assert(0 && "This action not supported for this op yet!");
1384 case TargetLowering::Legal:
1385 if (Tmp1 != Node->getOperand(0))
1386 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1387 ExtraVT);
1388 break;
1389 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001390 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001391 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001392 // NOTE: we could fall back on load/store here too for targets without
1393 // SAR. However, it is doubtful that any exist.
1394 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1395 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001396 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001397 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1398 Node->getOperand(0), ShiftCst);
1399 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1400 Result, ShiftCst);
1401 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1402 // The only way we can lower this is to turn it into a STORETRUNC,
1403 // EXTLOAD pair, targetting a temporary location (a stack slot).
1404
1405 // NOTE: there is a choice here between constantly creating new stack
1406 // slots and always reusing the same one. We currently always create
1407 // new ones, as reuse may inhibit scheduling.
1408 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1409 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1410 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1411 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001412 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001413 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1414 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1415 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001416 Node->getOperand(0), StackSlot,
1417 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001418 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001419 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001420 } else {
1421 assert(0 && "Unknown op");
1422 }
1423 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001424 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001425 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001426 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001427 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001428 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001429
Chris Lattner45982da2005-05-12 16:53:42 +00001430 // Note that LegalizeOp may be reentered even from single-use nodes, which
1431 // means that we always must cache transformed nodes.
1432 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001433 return Result;
1434}
1435
Chris Lattner8b6fa222005-01-15 22:16:26 +00001436/// PromoteOp - Given an operation that produces a value in an invalid type,
1437/// promote it to compute the value into a larger type. The produced value will
1438/// have the correct bits for the low portion of the register, but no guarantee
1439/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001440SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1441 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001442 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001443 assert(getTypeAction(VT) == Promote &&
1444 "Caller should expand or legalize operands that are not promotable!");
1445 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1446 "Cannot promote to smaller type!");
1447
Chris Lattner03c85462005-01-15 05:21:40 +00001448 SDOperand Tmp1, Tmp2, Tmp3;
1449
1450 SDOperand Result;
1451 SDNode *Node = Op.Val;
1452
Chris Lattner45982da2005-05-12 16:53:42 +00001453 if (!Node->hasOneUse()) {
1454 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1455 if (I != PromotedNodes.end()) return I->second;
1456 } else {
1457 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1458 }
1459
Chris Lattner0f69b292005-01-15 06:18:18 +00001460 // Promotion needs an optimization step to clean up after it, and is not
1461 // careful to avoid operations the target does not support. Make sure that
1462 // all generated operations are legalized in the next iteration.
1463 NeedsAnotherIteration = true;
1464
Chris Lattner03c85462005-01-15 05:21:40 +00001465 switch (Node->getOpcode()) {
1466 default:
1467 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1468 assert(0 && "Do not know how to promote this operator!");
1469 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001470 case ISD::UNDEF:
1471 Result = DAG.getNode(ISD::UNDEF, NVT);
1472 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001473 case ISD::Constant:
1474 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1475 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1476 break;
1477 case ISD::ConstantFP:
1478 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1479 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1480 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001481 case ISD::CopyFromReg:
1482 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1483 Node->getOperand(0));
1484 // Remember that we legalized the chain.
1485 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1486 break;
1487
Chris Lattner82fbfb62005-01-18 02:59:52 +00001488 case ISD::SETCC:
1489 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1490 "SetCC type is not legal??");
1491 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1492 TLI.getSetCCResultTy(), Node->getOperand(0),
1493 Node->getOperand(1));
1494 Result = LegalizeOp(Result);
1495 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001496
1497 case ISD::TRUNCATE:
1498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1499 case Legal:
1500 Result = LegalizeOp(Node->getOperand(0));
1501 assert(Result.getValueType() >= NVT &&
1502 "This truncation doesn't make sense!");
1503 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1504 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1505 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001506 case Promote:
1507 // The truncation is not required, because we don't guarantee anything
1508 // about high bits anyway.
1509 Result = PromoteOp(Node->getOperand(0));
1510 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001511 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001512 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1513 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001514 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001515 }
1516 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001517 case ISD::SIGN_EXTEND:
1518 case ISD::ZERO_EXTEND:
1519 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1520 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1521 case Legal:
1522 // Input is legal? Just do extend all the way to the larger type.
1523 Result = LegalizeOp(Node->getOperand(0));
1524 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1525 break;
1526 case Promote:
1527 // Promote the reg if it's smaller.
1528 Result = PromoteOp(Node->getOperand(0));
1529 // The high bits are not guaranteed to be anything. Insert an extend.
1530 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001531 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1532 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001533 else
Chris Lattner23993562005-04-13 02:38:47 +00001534 Result = DAG.getZeroExtendInReg(Result,
1535 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001536 break;
1537 }
1538 break;
1539
1540 case ISD::FP_EXTEND:
1541 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1542 case ISD::FP_ROUND:
1543 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1544 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1545 case Promote: assert(0 && "Unreachable with 2 FP types!");
1546 case Legal:
1547 // Input is legal? Do an FP_ROUND_INREG.
1548 Result = LegalizeOp(Node->getOperand(0));
1549 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1550 break;
1551 }
1552 break;
1553
1554 case ISD::SINT_TO_FP:
1555 case ISD::UINT_TO_FP:
1556 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1557 case Legal:
1558 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001559 // No extra round required here.
1560 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001561 break;
1562
1563 case Promote:
1564 Result = PromoteOp(Node->getOperand(0));
1565 if (Node->getOpcode() == ISD::SINT_TO_FP)
1566 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1567 Result, Node->getOperand(0).getValueType());
1568 else
Chris Lattner23993562005-04-13 02:38:47 +00001569 Result = DAG.getZeroExtendInReg(Result,
1570 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001571 // No extra round required here.
1572 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001573 break;
1574 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001575 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1576 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001577 // Round if we cannot tolerate excess precision.
1578 if (NoExcessFPPrecision)
1579 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1580 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001581 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001582 break;
1583
1584 case ISD::FP_TO_SINT:
1585 case ISD::FP_TO_UINT:
1586 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1587 case Legal:
1588 Tmp1 = LegalizeOp(Node->getOperand(0));
1589 break;
1590 case Promote:
1591 // The input result is prerounded, so we don't have to do anything
1592 // special.
1593 Tmp1 = PromoteOp(Node->getOperand(0));
1594 break;
1595 case Expand:
1596 assert(0 && "not implemented");
1597 }
1598 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1599 break;
1600
Chris Lattner2c8086f2005-04-02 05:00:07 +00001601 case ISD::FABS:
1602 case ISD::FNEG:
1603 Tmp1 = PromoteOp(Node->getOperand(0));
1604 assert(Tmp1.getValueType() == NVT);
1605 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1606 // NOTE: we do not have to do any extra rounding here for
1607 // NoExcessFPPrecision, because we know the input will have the appropriate
1608 // precision, and these operations don't modify precision at all.
1609 break;
1610
Chris Lattnerda6ba872005-04-28 21:44:33 +00001611 case ISD::FSQRT:
1612 case ISD::FSIN:
1613 case ISD::FCOS:
1614 Tmp1 = PromoteOp(Node->getOperand(0));
1615 assert(Tmp1.getValueType() == NVT);
1616 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1617 if(NoExcessFPPrecision)
1618 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1619 break;
1620
Chris Lattner03c85462005-01-15 05:21:40 +00001621 case ISD::AND:
1622 case ISD::OR:
1623 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001624 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001625 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001626 case ISD::MUL:
1627 // The input may have strange things in the top bits of the registers, but
1628 // these operations don't care. They may have wierd bits going out, but
1629 // that too is okay if they are integer operations.
1630 Tmp1 = PromoteOp(Node->getOperand(0));
1631 Tmp2 = PromoteOp(Node->getOperand(1));
1632 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1633 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1634
1635 // However, if this is a floating point operation, they will give excess
1636 // precision that we may not be able to tolerate. If we DO allow excess
1637 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001638 // FIXME: Why would we need to round FP ops more than integer ones?
1639 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001640 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1641 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1642 break;
1643
Chris Lattner8b6fa222005-01-15 22:16:26 +00001644 case ISD::SDIV:
1645 case ISD::SREM:
1646 // These operators require that their input be sign extended.
1647 Tmp1 = PromoteOp(Node->getOperand(0));
1648 Tmp2 = PromoteOp(Node->getOperand(1));
1649 if (MVT::isInteger(NVT)) {
1650 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001651 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001652 }
1653 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1654
1655 // Perform FP_ROUND: this is probably overly pessimistic.
1656 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1657 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1658 break;
1659
1660 case ISD::UDIV:
1661 case ISD::UREM:
1662 // These operators require that their input be zero extended.
1663 Tmp1 = PromoteOp(Node->getOperand(0));
1664 Tmp2 = PromoteOp(Node->getOperand(1));
1665 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001666 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1667 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001668 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1669 break;
1670
1671 case ISD::SHL:
1672 Tmp1 = PromoteOp(Node->getOperand(0));
1673 Tmp2 = LegalizeOp(Node->getOperand(1));
1674 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1675 break;
1676 case ISD::SRA:
1677 // The input value must be properly sign extended.
1678 Tmp1 = PromoteOp(Node->getOperand(0));
1679 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1680 Tmp2 = LegalizeOp(Node->getOperand(1));
1681 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1682 break;
1683 case ISD::SRL:
1684 // The input value must be properly zero extended.
1685 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001686 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001687 Tmp2 = LegalizeOp(Node->getOperand(1));
1688 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1689 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001690 case ISD::LOAD:
1691 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1692 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001693 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001694 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001695 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1696 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001697 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001698 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1699 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001700
1701 // Remember that we legalized the chain.
1702 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1703 break;
1704 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001705 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1706 case Expand: assert(0 && "It's impossible to expand bools");
1707 case Legal:
1708 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1709 break;
1710 case Promote:
1711 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1712 break;
1713 }
Chris Lattner03c85462005-01-15 05:21:40 +00001714 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1715 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1716 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1717 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00001718 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00001719 case ISD::CALL: {
1720 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1721 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1722
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001723 std::vector<SDOperand> Ops;
1724 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1725 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1726
Chris Lattner8ac532c2005-01-16 19:46:48 +00001727 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1728 "Can only promote single result calls");
1729 std::vector<MVT::ValueType> RetTyVTs;
1730 RetTyVTs.reserve(2);
1731 RetTyVTs.push_back(NVT);
1732 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00001733 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1734 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001735 Result = SDOperand(NC, 0);
1736
1737 // Insert the new chain mapping.
1738 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1739 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001740 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001741 case ISD::CTPOP:
1742 case ISD::CTTZ:
1743 case ISD::CTLZ:
1744 Tmp1 = Node->getOperand(0);
1745 //Zero extend the argument
1746 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1747 // Perform the larger operation, then subtract if needed.
1748 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1749 switch(Node->getOpcode())
1750 {
1751 case ISD::CTPOP:
1752 Result = Tmp1;
1753 break;
1754 case ISD::CTTZ:
1755 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1756 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1757 DAG.getConstant(getSizeInBits(NVT), NVT));
1758 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1759 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1760 break;
1761 case ISD::CTLZ:
1762 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1763 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1764 DAG.getConstant(getSizeInBits(NVT) -
1765 getSizeInBits(VT), NVT));
1766 break;
1767 }
1768 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001769 }
1770
1771 assert(Result.Val && "Didn't set a result!");
1772 AddPromotedOperand(Op, Result);
1773 return Result;
1774}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001775
Chris Lattner84f67882005-01-20 18:52:28 +00001776/// ExpandAddSub - Find a clever way to expand this add operation into
1777/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001778void SelectionDAGLegalize::
1779ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1780 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001781 // Expand the subcomponents.
1782 SDOperand LHSL, LHSH, RHSL, RHSH;
1783 ExpandOp(LHS, LHSL, LHSH);
1784 ExpandOp(RHS, RHSL, RHSH);
1785
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001786 // FIXME: this should be moved to the dag combiner someday.
1787 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1788 if (LHSL.getValueType() == MVT::i32) {
1789 SDOperand LowEl;
1790 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1791 if (C->getValue() == 0)
1792 LowEl = RHSL;
1793 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1794 if (C->getValue() == 0)
1795 LowEl = LHSL;
1796 if (LowEl.Val) {
1797 // Turn this into an add/sub of the high part only.
1798 SDOperand HiEl =
1799 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1800 LowEl.getValueType(), LHSH, RHSH);
1801 Lo = LowEl;
1802 Hi = HiEl;
1803 return;
1804 }
1805 }
1806
Chris Lattner84f67882005-01-20 18:52:28 +00001807 std::vector<SDOperand> Ops;
1808 Ops.push_back(LHSL);
1809 Ops.push_back(LHSH);
1810 Ops.push_back(RHSL);
1811 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001812 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001813 Hi = Lo.getValue(1);
1814}
1815
Chris Lattner5b359c62005-04-02 04:00:59 +00001816void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1817 SDOperand Op, SDOperand Amt,
1818 SDOperand &Lo, SDOperand &Hi) {
1819 // Expand the subcomponents.
1820 SDOperand LHSL, LHSH;
1821 ExpandOp(Op, LHSL, LHSH);
1822
1823 std::vector<SDOperand> Ops;
1824 Ops.push_back(LHSL);
1825 Ops.push_back(LHSH);
1826 Ops.push_back(Amt);
1827 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1828 Hi = Lo.getValue(1);
1829}
1830
1831
Chris Lattnere34b3962005-01-19 04:19:40 +00001832/// ExpandShift - Try to find a clever way to expand this shift operation out to
1833/// smaller elements. If we can't find a way that is more efficient than a
1834/// libcall on this target, return false. Otherwise, return true with the
1835/// low-parts expanded into Lo and Hi.
1836bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1837 SDOperand &Lo, SDOperand &Hi) {
1838 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1839 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001840
Chris Lattnere34b3962005-01-19 04:19:40 +00001841 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001842 SDOperand ShAmt = LegalizeOp(Amt);
1843 MVT::ValueType ShTy = ShAmt.getValueType();
1844 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1845 unsigned NVTBits = MVT::getSizeInBits(NVT);
1846
1847 // Handle the case when Amt is an immediate. Other cases are currently broken
1848 // and are disabled.
1849 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1850 unsigned Cst = CN->getValue();
1851 // Expand the incoming operand to be shifted, so that we have its parts
1852 SDOperand InL, InH;
1853 ExpandOp(Op, InL, InH);
1854 switch(Opc) {
1855 case ISD::SHL:
1856 if (Cst > VTBits) {
1857 Lo = DAG.getConstant(0, NVT);
1858 Hi = DAG.getConstant(0, NVT);
1859 } else if (Cst > NVTBits) {
1860 Lo = DAG.getConstant(0, NVT);
1861 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001862 } else if (Cst == NVTBits) {
1863 Lo = DAG.getConstant(0, NVT);
1864 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001865 } else {
1866 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1867 Hi = DAG.getNode(ISD::OR, NVT,
1868 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1869 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1870 }
1871 return true;
1872 case ISD::SRL:
1873 if (Cst > VTBits) {
1874 Lo = DAG.getConstant(0, NVT);
1875 Hi = DAG.getConstant(0, NVT);
1876 } else if (Cst > NVTBits) {
1877 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1878 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001879 } else if (Cst == NVTBits) {
1880 Lo = InH;
1881 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001882 } else {
1883 Lo = DAG.getNode(ISD::OR, NVT,
1884 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1885 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1886 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1887 }
1888 return true;
1889 case ISD::SRA:
1890 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001891 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001892 DAG.getConstant(NVTBits-1, ShTy));
1893 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001894 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001895 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001896 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001897 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001898 } else if (Cst == NVTBits) {
1899 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001900 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001901 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001902 } else {
1903 Lo = DAG.getNode(ISD::OR, NVT,
1904 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1905 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1906 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1907 }
1908 return true;
1909 }
1910 }
1911 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1912 // so disable it for now. Currently targets are handling this via SHL_PARTS
1913 // and friends.
1914 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001915
1916 // If we have an efficient select operation (or if the selects will all fold
1917 // away), lower to some complex code, otherwise just emit the libcall.
1918 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1919 !isa<ConstantSDNode>(Amt))
1920 return false;
1921
1922 SDOperand InL, InH;
1923 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001924 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1925 DAG.getConstant(NVTBits, ShTy), ShAmt);
1926
Chris Lattnere5544f82005-01-20 20:29:23 +00001927 // Compare the unmasked shift amount against 32.
1928 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1929 DAG.getConstant(NVTBits, ShTy));
1930
Chris Lattnere34b3962005-01-19 04:19:40 +00001931 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1932 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1933 DAG.getConstant(NVTBits-1, ShTy));
1934 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1935 DAG.getConstant(NVTBits-1, ShTy));
1936 }
1937
1938 if (Opc == ISD::SHL) {
1939 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1940 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1941 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001942 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001943
Chris Lattnere34b3962005-01-19 04:19:40 +00001944 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1945 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1946 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001947 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1948 DAG.getSetCC(ISD::SETEQ,
1949 TLI.getSetCCResultTy(), NAmt,
1950 DAG.getConstant(32, ShTy)),
1951 DAG.getConstant(0, NVT),
1952 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001953 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001954 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001955 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001956 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001957
1958 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001959 if (Opc == ISD::SRA)
1960 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1961 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001962 else
1963 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001964 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001965 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001966 }
1967 return true;
1968}
Chris Lattner77e77a62005-01-21 06:05:23 +00001969
Chris Lattner9530ddc2005-05-13 05:09:11 +00001970/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
1971/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001972/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001973static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001974 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1975
Chris Lattner16cd04d2005-05-12 23:24:06 +00001976 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001977 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00001978 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001979 Found = Node;
1980 return;
1981 }
1982
1983 // Otherwise, scan the operands of Node to see if any of them is a call.
1984 assert(Node->getNumOperands() != 0 &&
1985 "All leaves should have depth equal to the entry node!");
1986 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00001987 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001988
1989 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001990 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001991 Found);
1992}
1993
1994
Chris Lattner9530ddc2005-05-13 05:09:11 +00001995/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
1996/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001997/// than Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00001998static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001999 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2000
Chris Lattner16cd04d2005-05-12 23:24:06 +00002001 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002002 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002003 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002004 Found = Node;
2005 return;
2006 }
2007
2008 // Otherwise, scan the operands of Node to see if any of them is a call.
2009 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2010 if (UI == E) return;
2011 for (--E; UI != E; ++UI)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002012 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002013
2014 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002015 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002016}
2017
Chris Lattner9530ddc2005-05-13 05:09:11 +00002018/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002019/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002020static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002021 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002022 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002023 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002024 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002025
2026 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002027 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002028
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002029 SDOperand TheChain(Node, Node->getNumValues()-1);
2030 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002031
2032 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002033 E = Node->use_end(); ; ++UI) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002034 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002035
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002036 // Make sure to only follow users of our token chain.
2037 SDNode *User = *UI;
2038 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2039 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002040 if (SDNode *Result = FindCallSeqEnd(User))
2041 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002042 }
2043 assert(0 && "Unreachable");
2044 abort();
2045}
2046
Chris Lattner9530ddc2005-05-13 05:09:11 +00002047/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002048/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002049static SDNode *FindCallSeqStart(SDNode *Node) {
2050 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002051 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002052
2053 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2054 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002055 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002056}
2057
2058
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002059/// FindInputOutputChains - If we are replacing an operation with a call we need
2060/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002061/// properly serialize the calls in the block. The returned operand is the
2062/// input chain value for the new call (e.g. the entry node or the previous
2063/// call), and OutChain is set to be the chain node to update to point to the
2064/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002065static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2066 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002067 SDNode *LatestCallSeqStart = Entry.Val;
2068 SDNode *LatestCallSeqEnd = 0;
2069 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2070 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002071
Chris Lattner16cd04d2005-05-12 23:24:06 +00002072 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002073 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002074 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2075 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002076 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2077 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002078 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002079 LatestCallSeqEnd = Entry.Val;
2080 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002081
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002082 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002083 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002084 OutChain = 0;
Chris Lattner9530ddc2005-05-13 05:09:11 +00002085 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002086
Chris Lattner9530ddc2005-05-13 05:09:11 +00002087 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002088 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002089 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002090
Chris Lattner9530ddc2005-05-13 05:09:11 +00002091 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002092}
2093
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002094/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002095void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2096 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002097 // Nothing to splice it into?
2098 if (OutChain == 0) return;
2099
2100 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2101 //OutChain->dump();
2102
2103 // Form a token factor node merging the old inval and the new inval.
2104 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2105 OutChain->getOperand(0));
2106 // Change the node to refer to the new token.
2107 OutChain->setAdjCallChain(InToken);
2108}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002109
2110
Chris Lattner77e77a62005-01-21 06:05:23 +00002111// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2112// does not fit into a register, return the lo part and set the hi part to the
2113// by-reg argument. If it does fit into a single register, return the result
2114// and leave the Hi part unset.
2115SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2116 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002117 SDNode *OutChain;
2118 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2119 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002120 if (InChain.Val == 0)
2121 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002122
Chris Lattner77e77a62005-01-21 06:05:23 +00002123 TargetLowering::ArgListTy Args;
2124 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2125 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2126 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2127 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2128 }
2129 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002130
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002131 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002132 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002133 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002134 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2135 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002136 SpliceCallInto(CallInfo.second, OutChain);
2137
2138 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002139
2140 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002141 default: assert(0 && "Unknown thing");
2142 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002143 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002144 case Promote:
2145 assert(0 && "Cannot promote this yet!");
2146 case Expand:
2147 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002148 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002149 return Lo;
2150 }
2151}
2152
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002153
Chris Lattner77e77a62005-01-21 06:05:23 +00002154/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2155/// destination type is legal.
2156SDOperand SelectionDAGLegalize::
2157ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2158 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2159 assert(getTypeAction(Source.getValueType()) == Expand &&
2160 "This is not an expansion!");
2161 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2162
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002163 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002164 assert(Source.getValueType() == MVT::i64 &&
2165 "This only works for 64-bit -> FP");
2166 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2167 // incoming integer is set. To handle this, we dynamically test to see if
2168 // it is set, and, if so, add a fudge factor.
2169 SDOperand Lo, Hi;
2170 ExpandOp(Source, Lo, Hi);
2171
Chris Lattner66de05b2005-05-13 04:45:13 +00002172 // If this is unsigned, and not supported, first perform the conversion to
2173 // signed, then adjust the result if the sign bit is set.
2174 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2175 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2176
Chris Lattnere9c35e72005-04-13 05:09:42 +00002177 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2178 DAG.getConstant(0, Hi.getValueType()));
2179 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2180 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2181 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002182 uint64_t FF = 0x5f800000ULL;
2183 if (TLI.isLittleEndian()) FF <<= 32;
2184 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002185
2186 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2187 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2188 TLI.getPointerTy());
2189 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2190 SDOperand FudgeInReg;
2191 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002192 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2193 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002194 else {
2195 assert(DestTy == MVT::f64 && "Unexpected conversion");
2196 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002197 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002198 }
2199 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002200 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002201
Chris Lattnera88a2602005-05-14 05:33:54 +00002202 // Check to see if the target has a custom way to lower this. If so, use it.
2203 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2204 default: assert(0 && "This action not implemented for this operation!");
2205 case TargetLowering::Legal:
2206 case TargetLowering::Expand:
2207 break; // This case is handled below.
2208 case TargetLowering::Custom:
2209 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002210 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002211 }
2212
Chris Lattner13689e22005-05-12 07:00:44 +00002213 // Expand the source, then glue it back together for the call. We must expand
2214 // the source in case it is shared (this pass of legalize must traverse it).
2215 SDOperand SrcLo, SrcHi;
2216 ExpandOp(Source, SrcLo, SrcHi);
2217 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2218
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002219 SDNode *OutChain = 0;
2220 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2221 DAG.getEntryNode());
2222 const char *FnName = 0;
2223 if (DestTy == MVT::f32)
2224 FnName = "__floatdisf";
2225 else {
2226 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2227 FnName = "__floatdidf";
2228 }
2229
Chris Lattner77e77a62005-01-21 06:05:23 +00002230 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2231
2232 TargetLowering::ArgListTy Args;
2233 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002234
Chris Lattner77e77a62005-01-21 06:05:23 +00002235 Args.push_back(std::make_pair(Source, ArgTy));
2236
2237 // We don't care about token chains for libcalls. We just use the entry
2238 // node as our input and ignore the output chain. This allows us to place
2239 // calls wherever we need them to satisfy data dependences.
2240 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002241
2242 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002243 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2244 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002245
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002246 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002247 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002248}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002249
Chris Lattnere34b3962005-01-19 04:19:40 +00002250
2251
Chris Lattner3e928bb2005-01-07 07:47:09 +00002252/// ExpandOp - Expand the specified SDOperand into its two component pieces
2253/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2254/// LegalizeNodes map is filled in for any results that are not expanded, the
2255/// ExpandedNodes map is filled in for any results that are expanded, and the
2256/// Lo/Hi values are returned.
2257void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2258 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002259 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002260 SDNode *Node = Op.Val;
2261 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2262 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2263 assert(MVT::isInteger(NVT) && NVT < VT &&
2264 "Cannot expand to FP value or to larger int value!");
2265
2266 // If there is more than one use of this, see if we already expanded it.
2267 // There is no use remembering values that only have a single use, as the map
2268 // entries will never be reused.
2269 if (!Node->hasOneUse()) {
2270 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2271 = ExpandedNodes.find(Op);
2272 if (I != ExpandedNodes.end()) {
2273 Lo = I->second.first;
2274 Hi = I->second.second;
2275 return;
2276 }
Chris Lattner45982da2005-05-12 16:53:42 +00002277 } else {
2278 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002279 }
2280
Chris Lattner4e6c7462005-01-08 19:27:05 +00002281 // Expanding to multiple registers needs to perform an optimization step, and
2282 // is not careful to avoid operations the target does not support. Make sure
2283 // that all generated operations are legalized in the next iteration.
2284 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002285
Chris Lattner3e928bb2005-01-07 07:47:09 +00002286 switch (Node->getOpcode()) {
2287 default:
2288 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2289 assert(0 && "Do not know how to expand this operator!");
2290 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002291 case ISD::UNDEF:
2292 Lo = DAG.getNode(ISD::UNDEF, NVT);
2293 Hi = DAG.getNode(ISD::UNDEF, NVT);
2294 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002295 case ISD::Constant: {
2296 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2297 Lo = DAG.getConstant(Cst, NVT);
2298 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2299 break;
2300 }
2301
2302 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002303 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002304 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002305 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2306 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002307
Chris Lattner69a52152005-01-14 22:38:01 +00002308 // Remember that we legalized the chain.
2309 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2310
Chris Lattner3e928bb2005-01-07 07:47:09 +00002311 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2312 break;
2313 }
2314
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002315 case ISD::BUILD_PAIR:
2316 // Legalize both operands. FIXME: in the future we should handle the case
2317 // where the two elements are not legal.
2318 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2319 Lo = LegalizeOp(Node->getOperand(0));
2320 Hi = LegalizeOp(Node->getOperand(1));
2321 break;
2322
Chris Lattneredb1add2005-05-11 04:51:16 +00002323 case ISD::CTPOP:
2324 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002325 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2326 DAG.getNode(ISD::CTPOP, NVT, Lo),
2327 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002328 Hi = DAG.getConstant(0, NVT);
2329 break;
2330
Chris Lattner39a8f332005-05-12 19:05:01 +00002331 case ISD::CTLZ: {
2332 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002333 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002334 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2335 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2336 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2337 HLZ, BitsC);
2338 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2339 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2340
2341 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2342 Hi = DAG.getConstant(0, NVT);
2343 break;
2344 }
2345
2346 case ISD::CTTZ: {
2347 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002348 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002349 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2350 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2351 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2352 LTZ, BitsC);
2353 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2354 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2355
2356 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2357 Hi = DAG.getConstant(0, NVT);
2358 break;
2359 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002360
Chris Lattner3e928bb2005-01-07 07:47:09 +00002361 case ISD::LOAD: {
2362 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2363 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002364 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002365
2366 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002367 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002368 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2369 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002370 //Is this safe? declaring that the two parts of the split load
2371 //are from the same instruction?
2372 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002373
2374 // Build a factor node to remember that this load is independent of the
2375 // other one.
2376 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2377 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002378
Chris Lattner3e928bb2005-01-07 07:47:09 +00002379 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002380 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002381 if (!TLI.isLittleEndian())
2382 std::swap(Lo, Hi);
2383 break;
2384 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002385 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002386 case ISD::CALL: {
2387 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2388 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2389
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002390 bool Changed = false;
2391 std::vector<SDOperand> Ops;
2392 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2393 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2394 Changed |= Ops.back() != Node->getOperand(i);
2395 }
2396
Chris Lattner3e928bb2005-01-07 07:47:09 +00002397 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2398 "Can only expand a call once so far, not i64 -> i16!");
2399
2400 std::vector<MVT::ValueType> RetTyVTs;
2401 RetTyVTs.reserve(3);
2402 RetTyVTs.push_back(NVT);
2403 RetTyVTs.push_back(NVT);
2404 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002405 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2406 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002407 Lo = SDOperand(NC, 0);
2408 Hi = SDOperand(NC, 1);
2409
2410 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002411 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002412 break;
2413 }
2414 case ISD::AND:
2415 case ISD::OR:
2416 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2417 SDOperand LL, LH, RL, RH;
2418 ExpandOp(Node->getOperand(0), LL, LH);
2419 ExpandOp(Node->getOperand(1), RL, RH);
2420 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2421 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2422 break;
2423 }
2424 case ISD::SELECT: {
2425 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002426
2427 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2428 case Expand: assert(0 && "It's impossible to expand bools");
2429 case Legal:
2430 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2431 break;
2432 case Promote:
2433 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2434 break;
2435 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002436 ExpandOp(Node->getOperand(1), LL, LH);
2437 ExpandOp(Node->getOperand(2), RL, RH);
2438 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2439 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2440 break;
2441 }
2442 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002443 SDOperand In;
2444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2445 case Expand: assert(0 && "expand-expand not implemented yet!");
2446 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2447 case Promote:
2448 In = PromoteOp(Node->getOperand(0));
2449 // Emit the appropriate sign_extend_inreg to get the value we want.
2450 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2451 Node->getOperand(0).getValueType());
2452 break;
2453 }
2454
Chris Lattner3e928bb2005-01-07 07:47:09 +00002455 // The low part is just a sign extension of the input (which degenerates to
2456 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002457 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002458
Chris Lattner3e928bb2005-01-07 07:47:09 +00002459 // The high part is obtained by SRA'ing all but one of the bits of the lo
2460 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002461 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002462 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2463 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002464 break;
2465 }
Chris Lattner06098e02005-04-03 23:41:52 +00002466 case ISD::ZERO_EXTEND: {
2467 SDOperand In;
2468 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2469 case Expand: assert(0 && "expand-expand not implemented yet!");
2470 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2471 case Promote:
2472 In = PromoteOp(Node->getOperand(0));
2473 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002474 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002475 break;
2476 }
2477
Chris Lattner3e928bb2005-01-07 07:47:09 +00002478 // The low part is just a zero extension of the input (which degenerates to
2479 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002480 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002481
Chris Lattner3e928bb2005-01-07 07:47:09 +00002482 // The high part is just a zero.
2483 Hi = DAG.getConstant(0, NVT);
2484 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002485 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002486 // These operators cannot be expanded directly, emit them as calls to
2487 // library functions.
2488 case ISD::FP_TO_SINT:
2489 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002490 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002491 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002492 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002493 break;
2494 case ISD::FP_TO_UINT:
2495 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002496 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002497 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002498 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002499 break;
2500
Chris Lattnere34b3962005-01-19 04:19:40 +00002501 case ISD::SHL:
2502 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002503 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002504 break;
Chris Lattner47599822005-04-02 03:38:53 +00002505
2506 // If this target supports SHL_PARTS, use it.
2507 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002508 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2509 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002510 break;
2511 }
2512
Chris Lattnere34b3962005-01-19 04:19:40 +00002513 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002514 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002515 break;
2516
2517 case ISD::SRA:
2518 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002519 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002520 break;
Chris Lattner47599822005-04-02 03:38:53 +00002521
2522 // If this target supports SRA_PARTS, use it.
2523 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002524 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2525 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002526 break;
2527 }
2528
Chris Lattnere34b3962005-01-19 04:19:40 +00002529 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002530 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002531 break;
2532 case ISD::SRL:
2533 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002534 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002535 break;
Chris Lattner47599822005-04-02 03:38:53 +00002536
2537 // If this target supports SRL_PARTS, use it.
2538 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002539 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2540 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002541 break;
2542 }
2543
Chris Lattnere34b3962005-01-19 04:19:40 +00002544 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002545 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002546 break;
2547
Misha Brukmanedf128a2005-04-21 22:36:52 +00002548 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002549 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2550 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002551 break;
2552 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002553 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2554 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002555 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002556 case ISD::MUL: {
2557 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2558 SDOperand LL, LH, RL, RH;
2559 ExpandOp(Node->getOperand(0), LL, LH);
2560 ExpandOp(Node->getOperand(1), RL, RH);
2561 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2562 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2563 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2564 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2565 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2566 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2567 } else {
2568 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2569 }
2570 break;
2571 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002572 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2573 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2574 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2575 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002576 }
2577
2578 // Remember in a map if the values will be reused later.
2579 if (!Node->hasOneUse()) {
2580 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2581 std::make_pair(Lo, Hi))).second;
2582 assert(isNew && "Value already expanded?!?");
2583 }
2584}
2585
2586
2587// SelectionDAG::Legalize - This is the entry point for the file.
2588//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002589void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002590 /// run - This is the main entry point to this class.
2591 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002592 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002593}
2594