blob: 12938e6e57bf3d8d1cf087a30d4ccb9eb64d8814 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattner3e928bb2005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner03c85462005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattner8afc48e2005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner03c85462005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
Chris Lattner9c32d3b2005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner77e77a62005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145}
146
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattnere1bd8222005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187
Chris Lattnerfa404e82005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000223 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000288
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
293 MVT::f32);
294 } else {
295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
296 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000297 }
298 break;
299 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
307 Changed = true;
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
310 } else {
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
313 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000314 }
315 if (Changed)
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
317 break;
318 }
319
Chris Lattner3e928bb2005-01-07 07:47:09 +0000320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
327 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
335 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000336 else
337 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000338
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
344
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000345 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000348
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
354 }
355
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000360 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000362 } else {
363 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000364 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000370 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000371 case ISD::BR:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
375 break;
376
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000377 case ISD::BRCOND:
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000379
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
382 case Legal:
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
384 break;
385 case Promote:
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
387 break;
388 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
393 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000394 case ISD::BRCONDTWOWAY:
395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
397 case Expand: assert(0 && "It's impossible to expand bools");
398 case Legal:
399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
400 break;
401 case Promote:
402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
403 break;
404 }
405 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
406 // pair.
407 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
408 case TargetLowering::Promote:
409 default: assert(0 && "This action is not supported yet!");
410 case TargetLowering::Legal:
411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
412 std::vector<SDOperand> Ops;
413 Ops.push_back(Tmp1);
414 Ops.push_back(Tmp2);
415 Ops.push_back(Node->getOperand(2));
416 Ops.push_back(Node->getOperand(3));
417 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
418 }
419 break;
420 case TargetLowering::Expand:
421 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
422 Node->getOperand(2));
423 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
424 break;
425 }
426 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000427
Chris Lattner3e928bb2005-01-07 07:47:09 +0000428 case ISD::LOAD:
429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
431 if (Tmp1 != Node->getOperand(0) ||
432 Tmp2 != Node->getOperand(1))
433 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner8afc48e2005-01-07 22:28:47 +0000434 else
435 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000436
Chris Lattner8afc48e2005-01-07 22:28:47 +0000437 // Since loads produce two values, make sure to remember that we legalized
438 // both of them.
439 AddLegalizedOperand(SDOperand(Node, 0), Result);
440 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
441 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000442
Chris Lattner0f69b292005-01-15 06:18:18 +0000443 case ISD::EXTLOAD:
444 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000445 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000448
Chris Lattner01ff7212005-04-10 22:54:25 +0000449 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
450 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000451 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000452 case TargetLowering::Promote:
453 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
454 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
455 Tmp1, Tmp2, MVT::i8);
456 // Since loads produce two values, make sure to remember that we legalized
457 // both of them.
458 AddLegalizedOperand(SDOperand(Node, 0), Result);
459 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
460 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000461
Chris Lattner01ff7212005-04-10 22:54:25 +0000462 case TargetLowering::Legal:
463 if (Tmp1 != Node->getOperand(0) ||
464 Tmp2 != Node->getOperand(1))
465 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
466 Tmp1, Tmp2, SrcVT);
467 else
468 Result = SDOperand(Node, 0);
469
470 // Since loads produce two values, make sure to remember that we legalized
471 // both of them.
472 AddLegalizedOperand(SDOperand(Node, 0), Result);
473 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
474 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000475 case TargetLowering::Expand:
476 assert(Node->getOpcode() != ISD::EXTLOAD &&
477 "EXTLOAD should always be supported!");
478 // Turn the unsupported load into an EXTLOAD followed by an explicit
479 // zero/sign extend inreg.
480 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
481 Tmp1, Tmp2, SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000482 SDOperand ValRes;
483 if (Node->getOpcode() == ISD::SEXTLOAD)
484 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
485 Result, SrcVT);
486 else
487 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000488 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
489 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
490 if (Op.ResNo)
491 return Result.getValue(1);
492 return ValRes;
493 }
494 assert(0 && "Unreachable");
495 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000496 case ISD::EXTRACT_ELEMENT:
497 // Get both the low and high parts.
498 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
499 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
500 Result = Tmp2; // 1 -> Hi
501 else
502 Result = Tmp1; // 0 -> Lo
503 break;
504
505 case ISD::CopyToReg:
506 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000507
Chris Lattner3e928bb2005-01-07 07:47:09 +0000508 switch (getTypeAction(Node->getOperand(1).getValueType())) {
509 case Legal:
510 // Legalize the incoming value (must be legal).
511 Tmp2 = LegalizeOp(Node->getOperand(1));
512 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000513 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000514 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000515 case Promote:
516 Tmp2 = PromoteOp(Node->getOperand(1));
517 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
518 break;
519 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000520 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000521 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000522 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000523 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
524 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
525 // Note that the copytoreg nodes are independent of each other.
526 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 assert(isTypeLegal(Result.getValueType()) &&
528 "Cannot expand multiple times yet (i64 -> i16)");
529 break;
530 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000531 break;
532
533 case ISD::RET:
534 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
535 switch (Node->getNumOperands()) {
536 case 2: // ret val
537 switch (getTypeAction(Node->getOperand(1).getValueType())) {
538 case Legal:
539 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000540 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000541 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
542 break;
543 case Expand: {
544 SDOperand Lo, Hi;
545 ExpandOp(Node->getOperand(1), Lo, Hi);
546 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000547 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000548 }
549 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000550 Tmp2 = PromoteOp(Node->getOperand(1));
551 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
552 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000553 }
554 break;
555 case 1: // ret void
556 if (Tmp1 != Node->getOperand(0))
557 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
558 break;
559 default: { // ret <values>
560 std::vector<SDOperand> NewValues;
561 NewValues.push_back(Tmp1);
562 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
563 switch (getTypeAction(Node->getOperand(i).getValueType())) {
564 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000565 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000566 break;
567 case Expand: {
568 SDOperand Lo, Hi;
569 ExpandOp(Node->getOperand(i), Lo, Hi);
570 NewValues.push_back(Lo);
571 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000572 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000573 }
574 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000575 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576 }
577 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
578 break;
579 }
580 }
581 break;
582 case ISD::STORE:
583 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
584 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
585
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000586 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000587 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000588 if (CFP->getValueType(0) == MVT::f32) {
589 union {
590 unsigned I;
591 float F;
592 } V;
593 V.F = CFP->getValue();
594 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
595 DAG.getConstant(V.I, MVT::i32), Tmp2);
596 } else {
597 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
598 union {
599 uint64_t I;
600 double F;
601 } V;
602 V.F = CFP->getValue();
603 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
604 DAG.getConstant(V.I, MVT::i64), Tmp2);
605 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000606 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000607 }
608
Chris Lattner3e928bb2005-01-07 07:47:09 +0000609 switch (getTypeAction(Node->getOperand(1).getValueType())) {
610 case Legal: {
611 SDOperand Val = LegalizeOp(Node->getOperand(1));
612 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
613 Tmp2 != Node->getOperand(2))
614 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
615 break;
616 }
617 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000618 // Truncate the value and store the result.
619 Tmp3 = PromoteOp(Node->getOperand(1));
620 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
621 Node->getOperand(1).getValueType());
622 break;
623
Chris Lattner3e928bb2005-01-07 07:47:09 +0000624 case Expand:
625 SDOperand Lo, Hi;
626 ExpandOp(Node->getOperand(1), Lo, Hi);
627
628 if (!TLI.isLittleEndian())
629 std::swap(Lo, Hi);
630
Chris Lattnerec39a452005-01-19 18:02:17 +0000631 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000632
Chris Lattnerec39a452005-01-19 18:02:17 +0000633 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000634 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
635 getIntPtrConstant(IncrementSize));
636 assert(isTypeLegal(Tmp2.getValueType()) &&
637 "Pointers must be legal!");
Chris Lattnerec39a452005-01-19 18:02:17 +0000638 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
639 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
640 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 }
642 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000643 case ISD::PCMARKER:
644 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000645 if (Tmp1 != Node->getOperand(0))
646 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000647 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000648 case ISD::TRUNCSTORE:
649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
650 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
651
652 switch (getTypeAction(Node->getOperand(1).getValueType())) {
653 case Legal:
654 Tmp2 = LegalizeOp(Node->getOperand(1));
655 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
656 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000657 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner0f69b292005-01-15 06:18:18 +0000658 cast<MVTSDNode>(Node)->getExtraValueType());
659 break;
660 case Promote:
661 case Expand:
662 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
663 }
664 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000665 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000666 switch (getTypeAction(Node->getOperand(0).getValueType())) {
667 case Expand: assert(0 && "It's impossible to expand bools");
668 case Legal:
669 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
670 break;
671 case Promote:
672 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
673 break;
674 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000675 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000676 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000677
678 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
679 default: assert(0 && "This action is not supported yet!");
680 case TargetLowering::Legal:
681 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
682 Tmp3 != Node->getOperand(2))
683 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
684 Tmp1, Tmp2, Tmp3);
685 break;
686 case TargetLowering::Promote: {
687 MVT::ValueType NVT =
688 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
689 unsigned ExtOp, TruncOp;
690 if (MVT::isInteger(Tmp2.getValueType())) {
691 ExtOp = ISD::ZERO_EXTEND;
692 TruncOp = ISD::TRUNCATE;
693 } else {
694 ExtOp = ISD::FP_EXTEND;
695 TruncOp = ISD::FP_ROUND;
696 }
697 // Promote each of the values to the new type.
698 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
699 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
700 // Perform the larger operation, then round down.
701 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
702 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
703 break;
704 }
705 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000706 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000707 case ISD::SETCC:
708 switch (getTypeAction(Node->getOperand(0).getValueType())) {
709 case Legal:
710 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
711 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
712 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
713 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000714 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000715 break;
716 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000717 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
718 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
719
720 // If this is an FP compare, the operands have already been extended.
721 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
722 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000723 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000724
725 // Otherwise, we have to insert explicit sign or zero extends. Note
726 // that we could insert sign extends for ALL conditions, but zero extend
727 // is cheaper on many machines (an AND instead of two shifts), so prefer
728 // it.
729 switch (cast<SetCCSDNode>(Node)->getCondition()) {
730 default: assert(0 && "Unknown integer comparison!");
731 case ISD::SETEQ:
732 case ISD::SETNE:
733 case ISD::SETUGE:
734 case ISD::SETUGT:
735 case ISD::SETULE:
736 case ISD::SETULT:
737 // ALL of these operations will work if we either sign or zero extend
738 // the operands (including the unsigned comparisons!). Zero extend is
739 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000740 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
741 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000742 break;
743 case ISD::SETGE:
744 case ISD::SETGT:
745 case ISD::SETLT:
746 case ISD::SETLE:
747 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
748 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
749 break;
750 }
751
752 }
753 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000754 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000755 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000756 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000757 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
758 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
759 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
760 switch (cast<SetCCSDNode>(Node)->getCondition()) {
761 case ISD::SETEQ:
762 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000763 if (RHSLo == RHSHi)
764 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
765 if (RHSCST->isAllOnesValue()) {
766 // Comparison to -1.
767 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000768 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000769 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000770 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000771 }
772
Chris Lattner3e928bb2005-01-07 07:47:09 +0000773 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
774 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
775 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000776 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000777 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000778 DAG.getConstant(0, Tmp1.getValueType()));
779 break;
780 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000781 // If this is a comparison of the sign bit, just look at the top part.
782 // X > -1, x < 0
783 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000784 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000785 CST->getValue() == 0) || // X < 0
786 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
787 (CST->isAllOnesValue()))) // X > -1
788 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
789 Node->getValueType(0), LHSHi, RHSHi);
790
Chris Lattner3e928bb2005-01-07 07:47:09 +0000791 // FIXME: This generated code sucks.
792 ISD::CondCode LowCC;
793 switch (cast<SetCCSDNode>(Node)->getCondition()) {
794 default: assert(0 && "Unknown integer setcc!");
795 case ISD::SETLT:
796 case ISD::SETULT: LowCC = ISD::SETULT; break;
797 case ISD::SETGT:
798 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
799 case ISD::SETLE:
800 case ISD::SETULE: LowCC = ISD::SETULE; break;
801 case ISD::SETGE:
802 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
803 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000804
Chris Lattner3e928bb2005-01-07 07:47:09 +0000805 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
806 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
807 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
808
809 // NOTE: on targets without efficient SELECT of bools, we can always use
810 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000811 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000812 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000813 Node->getValueType(0), LHSHi, RHSHi);
814 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
815 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
816 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000817 break;
818 }
819 }
820 break;
821
Chris Lattnere1bd8222005-01-11 05:57:22 +0000822 case ISD::MEMSET:
823 case ISD::MEMCPY:
824 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000825 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000826 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
827
828 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
829 switch (getTypeAction(Node->getOperand(2).getValueType())) {
830 case Expand: assert(0 && "Cannot expand a byte!");
831 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000832 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000833 break;
834 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000835 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000836 break;
837 }
838 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000839 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000840 }
Chris Lattner272455b2005-02-02 03:44:41 +0000841
842 SDOperand Tmp4;
843 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000844 case Expand: assert(0 && "Cannot expand this yet!");
845 case Legal:
846 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000847 break;
848 case Promote:
849 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000850 break;
851 }
852
853 SDOperand Tmp5;
854 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
855 case Expand: assert(0 && "Cannot expand this yet!");
856 case Legal:
857 Tmp5 = LegalizeOp(Node->getOperand(4));
858 break;
859 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000860 Tmp5 = PromoteOp(Node->getOperand(4));
861 break;
862 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000863
864 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
865 default: assert(0 && "This action not implemented for this operation!");
866 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000867 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
868 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
869 Tmp5 != Node->getOperand(4)) {
870 std::vector<SDOperand> Ops;
871 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
872 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
873 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
874 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000875 break;
876 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000877 // Otherwise, the target does not support this operation. Lower the
878 // operation to an explicit libcall as appropriate.
879 MVT::ValueType IntPtr = TLI.getPointerTy();
880 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
881 std::vector<std::pair<SDOperand, const Type*> > Args;
882
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000883 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000884 if (Node->getOpcode() == ISD::MEMSET) {
885 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
886 // Extend the ubyte argument to be an int value for the call.
887 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
888 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
889 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
890
891 FnName = "memset";
892 } else if (Node->getOpcode() == ISD::MEMCPY ||
893 Node->getOpcode() == ISD::MEMMOVE) {
894 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
895 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
896 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
897 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
898 } else {
899 assert(0 && "Unknown op!");
900 }
901 std::pair<SDOperand,SDOperand> CallResult =
Nate Begeman8e21e712005-03-26 01:29:23 +0000902 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000903 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
904 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000905 break;
906 }
907 case TargetLowering::Custom:
908 std::vector<SDOperand> Ops;
909 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
910 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
911 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
912 Result = TLI.LowerOperation(Result);
913 Result = LegalizeOp(Result);
914 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000915 }
916 break;
917 }
Chris Lattner84f67882005-01-20 18:52:28 +0000918 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +0000919 case ISD::SUB_PARTS:
920 case ISD::SHL_PARTS:
921 case ISD::SRA_PARTS:
922 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +0000923 std::vector<SDOperand> Ops;
924 bool Changed = false;
925 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
926 Ops.push_back(LegalizeOp(Node->getOperand(i)));
927 Changed |= Ops.back() != Node->getOperand(i);
928 }
929 if (Changed)
930 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +0000931
932 // Since these produce multiple values, make sure to remember that we
933 // legalized all of them.
934 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
935 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
936 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +0000937 }
Chris Lattner2c8086f2005-04-02 05:00:07 +0000938
939 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +0000940 case ISD::ADD:
941 case ISD::SUB:
942 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +0000943 case ISD::MULHS:
944 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000945 case ISD::UDIV:
946 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000947 case ISD::AND:
948 case ISD::OR:
949 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000950 case ISD::SHL:
951 case ISD::SRL:
952 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000953 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
954 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
955 if (Tmp1 != Node->getOperand(0) ||
956 Tmp2 != Node->getOperand(1))
957 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
958 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000959
Nate Begemanc105e192005-04-06 00:23:54 +0000960 case ISD::UREM:
961 case ISD::SREM:
962 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
963 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
964 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
965 case TargetLowering::Legal:
966 if (Tmp1 != Node->getOperand(0) ||
967 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000968 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +0000969 Tmp2);
970 break;
971 case TargetLowering::Promote:
972 case TargetLowering::Custom:
973 assert(0 && "Cannot promote/custom handle this yet!");
974 case TargetLowering::Expand: {
975 MVT::ValueType VT = Node->getValueType(0);
976 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
977 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
978 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
979 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
980 }
981 break;
982 }
983 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +0000984
985 // Unary operators
986 case ISD::FABS:
987 case ISD::FNEG:
988 Tmp1 = LegalizeOp(Node->getOperand(0));
989 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
990 case TargetLowering::Legal:
991 if (Tmp1 != Node->getOperand(0))
992 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
993 break;
994 case TargetLowering::Promote:
995 case TargetLowering::Custom:
996 assert(0 && "Cannot promote/custom handle this yet!");
997 case TargetLowering::Expand:
998 if (Node->getOpcode() == ISD::FNEG) {
999 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1000 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1001 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1002 Tmp2, Tmp1));
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001003 } else if (Node->getOpcode() == ISD::FABS) {
1004 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1005 MVT::ValueType VT = Node->getValueType(0);
1006 Tmp2 = DAG.getConstantFP(0.0, VT);
1007 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1008 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1009 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1010 Result = LegalizeOp(Result);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001011 } else {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001012 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001013 }
1014 break;
1015 }
1016 break;
1017
1018 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001019 case ISD::ZERO_EXTEND:
1020 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001021 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001022 case ISD::FP_EXTEND:
1023 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001024 case ISD::FP_TO_SINT:
1025 case ISD::FP_TO_UINT:
1026 case ISD::SINT_TO_FP:
1027 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001028 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1029 case Legal:
1030 Tmp1 = LegalizeOp(Node->getOperand(0));
1031 if (Tmp1 != Node->getOperand(0))
1032 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1033 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001034 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001035 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1036 Node->getOpcode() == ISD::UINT_TO_FP) {
1037 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1038 Node->getValueType(0), Node->getOperand(0));
1039 Result = LegalizeOp(Result);
1040 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001041 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1042 // In the expand case, we must be dealing with a truncate, because
1043 // otherwise the result would be larger than the source.
1044 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001045
Chris Lattner2c8086f2005-04-02 05:00:07 +00001046 // Since the result is legal, we should just be able to truncate the low
1047 // part of the source.
1048 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1049 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001050 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001051 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001052
Chris Lattner03c85462005-01-15 05:21:40 +00001053 case Promote:
1054 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001055 case ISD::ZERO_EXTEND:
1056 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001057 // NOTE: Any extend would work here...
1058 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001059 Result = DAG.getZeroExtendInReg(Result,
1060 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001061 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001062 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001063 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001064 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001065 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001066 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1067 Result, Node->getOperand(0).getValueType());
1068 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001069 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001070 Result = PromoteOp(Node->getOperand(0));
1071 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1072 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001073 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001074 Result = PromoteOp(Node->getOperand(0));
1075 if (Result.getValueType() != Op.getValueType())
1076 // Dynamically dead while we have only 2 FP types.
1077 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1078 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001079 case ISD::FP_ROUND:
1080 case ISD::FP_TO_SINT:
1081 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001082 Result = PromoteOp(Node->getOperand(0));
1083 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1084 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001085 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001086 Result = PromoteOp(Node->getOperand(0));
1087 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1088 Result, Node->getOperand(0).getValueType());
1089 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1090 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001091 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001092 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001093 Result = DAG.getZeroExtendInReg(Result,
1094 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001095 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1096 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001097 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001098 }
1099 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001100 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001101 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001102 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001103 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1104
1105 // If this operation is not supported, convert it to a shl/shr or load/store
1106 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001107 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1108 default: assert(0 && "This action not supported for this op yet!");
1109 case TargetLowering::Legal:
1110 if (Tmp1 != Node->getOperand(0))
1111 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1112 ExtraVT);
1113 break;
1114 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001115 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001116 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001117 // NOTE: we could fall back on load/store here too for targets without
1118 // SAR. However, it is doubtful that any exist.
1119 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1120 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001121 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001122 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1123 Node->getOperand(0), ShiftCst);
1124 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1125 Result, ShiftCst);
1126 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1127 // The only way we can lower this is to turn it into a STORETRUNC,
1128 // EXTLOAD pair, targetting a temporary location (a stack slot).
1129
1130 // NOTE: there is a choice here between constantly creating new stack
1131 // slots and always reusing the same one. We currently always create
1132 // new ones, as reuse may inhibit scheduling.
1133 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1134 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1135 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1136 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001137 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001138 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1139 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1140 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
1141 Node->getOperand(0), StackSlot, ExtraVT);
1142 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
1143 Result, StackSlot, ExtraVT);
1144 } else {
1145 assert(0 && "Unknown op");
1146 }
1147 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001148 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001149 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001150 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001151 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001152 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001153
Chris Lattner8afc48e2005-01-07 22:28:47 +00001154 if (!Op.Val->hasOneUse())
1155 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001156
1157 return Result;
1158}
1159
Chris Lattner8b6fa222005-01-15 22:16:26 +00001160/// PromoteOp - Given an operation that produces a value in an invalid type,
1161/// promote it to compute the value into a larger type. The produced value will
1162/// have the correct bits for the low portion of the register, but no guarantee
1163/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001164SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1165 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001166 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001167 assert(getTypeAction(VT) == Promote &&
1168 "Caller should expand or legalize operands that are not promotable!");
1169 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1170 "Cannot promote to smaller type!");
1171
1172 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1173 if (I != PromotedNodes.end()) return I->second;
1174
1175 SDOperand Tmp1, Tmp2, Tmp3;
1176
1177 SDOperand Result;
1178 SDNode *Node = Op.Val;
1179
Chris Lattner0f69b292005-01-15 06:18:18 +00001180 // Promotion needs an optimization step to clean up after it, and is not
1181 // careful to avoid operations the target does not support. Make sure that
1182 // all generated operations are legalized in the next iteration.
1183 NeedsAnotherIteration = true;
1184
Chris Lattner03c85462005-01-15 05:21:40 +00001185 switch (Node->getOpcode()) {
1186 default:
1187 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1188 assert(0 && "Do not know how to promote this operator!");
1189 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001190 case ISD::UNDEF:
1191 Result = DAG.getNode(ISD::UNDEF, NVT);
1192 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001193 case ISD::Constant:
1194 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1195 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1196 break;
1197 case ISD::ConstantFP:
1198 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1199 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1200 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001201 case ISD::CopyFromReg:
1202 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1203 Node->getOperand(0));
1204 // Remember that we legalized the chain.
1205 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1206 break;
1207
Chris Lattner82fbfb62005-01-18 02:59:52 +00001208 case ISD::SETCC:
1209 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1210 "SetCC type is not legal??");
1211 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1212 TLI.getSetCCResultTy(), Node->getOperand(0),
1213 Node->getOperand(1));
1214 Result = LegalizeOp(Result);
1215 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001216
1217 case ISD::TRUNCATE:
1218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1219 case Legal:
1220 Result = LegalizeOp(Node->getOperand(0));
1221 assert(Result.getValueType() >= NVT &&
1222 "This truncation doesn't make sense!");
1223 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1224 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1225 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001226 case Promote:
1227 // The truncation is not required, because we don't guarantee anything
1228 // about high bits anyway.
1229 Result = PromoteOp(Node->getOperand(0));
1230 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001231 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001232 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1233 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001234 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001235 }
1236 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001237 case ISD::SIGN_EXTEND:
1238 case ISD::ZERO_EXTEND:
1239 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1240 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1241 case Legal:
1242 // Input is legal? Just do extend all the way to the larger type.
1243 Result = LegalizeOp(Node->getOperand(0));
1244 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1245 break;
1246 case Promote:
1247 // Promote the reg if it's smaller.
1248 Result = PromoteOp(Node->getOperand(0));
1249 // The high bits are not guaranteed to be anything. Insert an extend.
1250 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001251 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1252 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001253 else
Chris Lattner23993562005-04-13 02:38:47 +00001254 Result = DAG.getZeroExtendInReg(Result,
1255 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001256 break;
1257 }
1258 break;
1259
1260 case ISD::FP_EXTEND:
1261 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1262 case ISD::FP_ROUND:
1263 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1264 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1265 case Promote: assert(0 && "Unreachable with 2 FP types!");
1266 case Legal:
1267 // Input is legal? Do an FP_ROUND_INREG.
1268 Result = LegalizeOp(Node->getOperand(0));
1269 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1270 break;
1271 }
1272 break;
1273
1274 case ISD::SINT_TO_FP:
1275 case ISD::UINT_TO_FP:
1276 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1277 case Legal:
1278 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001279 // No extra round required here.
1280 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001281 break;
1282
1283 case Promote:
1284 Result = PromoteOp(Node->getOperand(0));
1285 if (Node->getOpcode() == ISD::SINT_TO_FP)
1286 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1287 Result, Node->getOperand(0).getValueType());
1288 else
Chris Lattner23993562005-04-13 02:38:47 +00001289 Result = DAG.getZeroExtendInReg(Result,
1290 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001291 // No extra round required here.
1292 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001293 break;
1294 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001295 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1296 Node->getOperand(0));
1297 Result = LegalizeOp(Result);
1298
1299 // Round if we cannot tolerate excess precision.
1300 if (NoExcessFPPrecision)
1301 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1302 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001303 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001304 break;
1305
1306 case ISD::FP_TO_SINT:
1307 case ISD::FP_TO_UINT:
1308 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1309 case Legal:
1310 Tmp1 = LegalizeOp(Node->getOperand(0));
1311 break;
1312 case Promote:
1313 // The input result is prerounded, so we don't have to do anything
1314 // special.
1315 Tmp1 = PromoteOp(Node->getOperand(0));
1316 break;
1317 case Expand:
1318 assert(0 && "not implemented");
1319 }
1320 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1321 break;
1322
Chris Lattner2c8086f2005-04-02 05:00:07 +00001323 case ISD::FABS:
1324 case ISD::FNEG:
1325 Tmp1 = PromoteOp(Node->getOperand(0));
1326 assert(Tmp1.getValueType() == NVT);
1327 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1328 // NOTE: we do not have to do any extra rounding here for
1329 // NoExcessFPPrecision, because we know the input will have the appropriate
1330 // precision, and these operations don't modify precision at all.
1331 break;
1332
Chris Lattner03c85462005-01-15 05:21:40 +00001333 case ISD::AND:
1334 case ISD::OR:
1335 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001336 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001337 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001338 case ISD::MUL:
1339 // The input may have strange things in the top bits of the registers, but
1340 // these operations don't care. They may have wierd bits going out, but
1341 // that too is okay if they are integer operations.
1342 Tmp1 = PromoteOp(Node->getOperand(0));
1343 Tmp2 = PromoteOp(Node->getOperand(1));
1344 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1345 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1346
1347 // However, if this is a floating point operation, they will give excess
1348 // precision that we may not be able to tolerate. If we DO allow excess
1349 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001350 // FIXME: Why would we need to round FP ops more than integer ones?
1351 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001352 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1353 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1354 break;
1355
Chris Lattner8b6fa222005-01-15 22:16:26 +00001356 case ISD::SDIV:
1357 case ISD::SREM:
1358 // These operators require that their input be sign extended.
1359 Tmp1 = PromoteOp(Node->getOperand(0));
1360 Tmp2 = PromoteOp(Node->getOperand(1));
1361 if (MVT::isInteger(NVT)) {
1362 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001363 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001364 }
1365 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1366
1367 // Perform FP_ROUND: this is probably overly pessimistic.
1368 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1369 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1370 break;
1371
1372 case ISD::UDIV:
1373 case ISD::UREM:
1374 // These operators require that their input be zero extended.
1375 Tmp1 = PromoteOp(Node->getOperand(0));
1376 Tmp2 = PromoteOp(Node->getOperand(1));
1377 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001378 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1379 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001380 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1381 break;
1382
1383 case ISD::SHL:
1384 Tmp1 = PromoteOp(Node->getOperand(0));
1385 Tmp2 = LegalizeOp(Node->getOperand(1));
1386 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1387 break;
1388 case ISD::SRA:
1389 // The input value must be properly sign extended.
1390 Tmp1 = PromoteOp(Node->getOperand(0));
1391 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1392 Tmp2 = LegalizeOp(Node->getOperand(1));
1393 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1394 break;
1395 case ISD::SRL:
1396 // The input value must be properly zero extended.
1397 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001398 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001399 Tmp2 = LegalizeOp(Node->getOperand(1));
1400 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1401 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001402 case ISD::LOAD:
1403 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001405 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001406 if (MVT::isInteger(NVT))
1407 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, VT);
1408 else
1409 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001410
1411 // Remember that we legalized the chain.
1412 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1413 break;
1414 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001415 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1416 case Expand: assert(0 && "It's impossible to expand bools");
1417 case Legal:
1418 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1419 break;
1420 case Promote:
1421 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1422 break;
1423 }
Chris Lattner03c85462005-01-15 05:21:40 +00001424 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1425 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1426 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1427 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001428 case ISD::CALL: {
1429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1431
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001432 std::vector<SDOperand> Ops;
1433 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1434 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1435
Chris Lattner8ac532c2005-01-16 19:46:48 +00001436 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1437 "Can only promote single result calls");
1438 std::vector<MVT::ValueType> RetTyVTs;
1439 RetTyVTs.reserve(2);
1440 RetTyVTs.push_back(NVT);
1441 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001442 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001443 Result = SDOperand(NC, 0);
1444
1445 // Insert the new chain mapping.
1446 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1447 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001448 }
Chris Lattner03c85462005-01-15 05:21:40 +00001449 }
1450
1451 assert(Result.Val && "Didn't set a result!");
1452 AddPromotedOperand(Op, Result);
1453 return Result;
1454}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001455
Chris Lattner84f67882005-01-20 18:52:28 +00001456/// ExpandAddSub - Find a clever way to expand this add operation into
1457/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001458void SelectionDAGLegalize::
1459ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1460 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001461 // Expand the subcomponents.
1462 SDOperand LHSL, LHSH, RHSL, RHSH;
1463 ExpandOp(LHS, LHSL, LHSH);
1464 ExpandOp(RHS, RHSL, RHSH);
1465
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001466 // FIXME: this should be moved to the dag combiner someday.
1467 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1468 if (LHSL.getValueType() == MVT::i32) {
1469 SDOperand LowEl;
1470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1471 if (C->getValue() == 0)
1472 LowEl = RHSL;
1473 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1474 if (C->getValue() == 0)
1475 LowEl = LHSL;
1476 if (LowEl.Val) {
1477 // Turn this into an add/sub of the high part only.
1478 SDOperand HiEl =
1479 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1480 LowEl.getValueType(), LHSH, RHSH);
1481 Lo = LowEl;
1482 Hi = HiEl;
1483 return;
1484 }
1485 }
1486
Chris Lattner84f67882005-01-20 18:52:28 +00001487 std::vector<SDOperand> Ops;
1488 Ops.push_back(LHSL);
1489 Ops.push_back(LHSH);
1490 Ops.push_back(RHSL);
1491 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001492 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001493 Hi = Lo.getValue(1);
1494}
1495
Chris Lattner5b359c62005-04-02 04:00:59 +00001496void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1497 SDOperand Op, SDOperand Amt,
1498 SDOperand &Lo, SDOperand &Hi) {
1499 // Expand the subcomponents.
1500 SDOperand LHSL, LHSH;
1501 ExpandOp(Op, LHSL, LHSH);
1502
1503 std::vector<SDOperand> Ops;
1504 Ops.push_back(LHSL);
1505 Ops.push_back(LHSH);
1506 Ops.push_back(Amt);
1507 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1508 Hi = Lo.getValue(1);
1509}
1510
1511
Chris Lattnere34b3962005-01-19 04:19:40 +00001512/// ExpandShift - Try to find a clever way to expand this shift operation out to
1513/// smaller elements. If we can't find a way that is more efficient than a
1514/// libcall on this target, return false. Otherwise, return true with the
1515/// low-parts expanded into Lo and Hi.
1516bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1517 SDOperand &Lo, SDOperand &Hi) {
1518 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1519 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001520
Chris Lattnere34b3962005-01-19 04:19:40 +00001521 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001522 SDOperand ShAmt = LegalizeOp(Amt);
1523 MVT::ValueType ShTy = ShAmt.getValueType();
1524 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1525 unsigned NVTBits = MVT::getSizeInBits(NVT);
1526
1527 // Handle the case when Amt is an immediate. Other cases are currently broken
1528 // and are disabled.
1529 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1530 unsigned Cst = CN->getValue();
1531 // Expand the incoming operand to be shifted, so that we have its parts
1532 SDOperand InL, InH;
1533 ExpandOp(Op, InL, InH);
1534 switch(Opc) {
1535 case ISD::SHL:
1536 if (Cst > VTBits) {
1537 Lo = DAG.getConstant(0, NVT);
1538 Hi = DAG.getConstant(0, NVT);
1539 } else if (Cst > NVTBits) {
1540 Lo = DAG.getConstant(0, NVT);
1541 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001542 } else if (Cst == NVTBits) {
1543 Lo = DAG.getConstant(0, NVT);
1544 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001545 } else {
1546 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1547 Hi = DAG.getNode(ISD::OR, NVT,
1548 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1549 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1550 }
1551 return true;
1552 case ISD::SRL:
1553 if (Cst > VTBits) {
1554 Lo = DAG.getConstant(0, NVT);
1555 Hi = DAG.getConstant(0, NVT);
1556 } else if (Cst > NVTBits) {
1557 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1558 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001559 } else if (Cst == NVTBits) {
1560 Lo = InH;
1561 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001562 } else {
1563 Lo = DAG.getNode(ISD::OR, NVT,
1564 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1565 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1566 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1567 }
1568 return true;
1569 case ISD::SRA:
1570 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001571 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001572 DAG.getConstant(NVTBits-1, ShTy));
1573 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001574 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001575 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001576 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001577 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001578 } else if (Cst == NVTBits) {
1579 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001580 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001581 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001582 } else {
1583 Lo = DAG.getNode(ISD::OR, NVT,
1584 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1585 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1586 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1587 }
1588 return true;
1589 }
1590 }
1591 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1592 // so disable it for now. Currently targets are handling this via SHL_PARTS
1593 // and friends.
1594 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001595
1596 // If we have an efficient select operation (or if the selects will all fold
1597 // away), lower to some complex code, otherwise just emit the libcall.
1598 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1599 !isa<ConstantSDNode>(Amt))
1600 return false;
1601
1602 SDOperand InL, InH;
1603 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001604 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1605 DAG.getConstant(NVTBits, ShTy), ShAmt);
1606
Chris Lattnere5544f82005-01-20 20:29:23 +00001607 // Compare the unmasked shift amount against 32.
1608 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1609 DAG.getConstant(NVTBits, ShTy));
1610
Chris Lattnere34b3962005-01-19 04:19:40 +00001611 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1612 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1613 DAG.getConstant(NVTBits-1, ShTy));
1614 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1615 DAG.getConstant(NVTBits-1, ShTy));
1616 }
1617
1618 if (Opc == ISD::SHL) {
1619 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1620 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1621 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001622 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001623
Chris Lattnere34b3962005-01-19 04:19:40 +00001624 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1625 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1626 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001627 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1628 DAG.getSetCC(ISD::SETEQ,
1629 TLI.getSetCCResultTy(), NAmt,
1630 DAG.getConstant(32, ShTy)),
1631 DAG.getConstant(0, NVT),
1632 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001633 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001634 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001635 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001636 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001637
1638 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001639 if (Opc == ISD::SRA)
1640 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1641 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001642 else
1643 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001644 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001645 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001646 }
1647 return true;
1648}
Chris Lattner77e77a62005-01-21 06:05:23 +00001649
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001650/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1651/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1652/// Found.
1653static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1654 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1655
1656 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1657 // than the Found node. Just remember this node and return.
1658 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1659 Found = Node;
1660 return;
1661 }
1662
1663 // Otherwise, scan the operands of Node to see if any of them is a call.
1664 assert(Node->getNumOperands() != 0 &&
1665 "All leaves should have depth equal to the entry node!");
1666 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1667 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1668
1669 // Tail recurse for the last iteration.
1670 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1671 Found);
1672}
1673
1674
1675/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1676/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1677/// than Found.
1678static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1679 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1680
1681 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1682 // than the Found node. Just remember this node and return.
1683 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1684 Found = Node;
1685 return;
1686 }
1687
1688 // Otherwise, scan the operands of Node to see if any of them is a call.
1689 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1690 if (UI == E) return;
1691 for (--E; UI != E; ++UI)
1692 FindEarliestAdjCallStackUp(*UI, Found);
1693
1694 // Tail recurse for the last iteration.
1695 FindEarliestAdjCallStackUp(*UI, Found);
1696}
1697
1698/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1699/// find the ADJCALLSTACKUP node that terminates the call sequence.
1700static SDNode *FindAdjCallStackUp(SDNode *Node) {
1701 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1702 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00001703 if (Node->use_empty())
1704 return 0; // No adjcallstackup
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001705
1706 if (Node->hasOneUse()) // Simple case, only has one user to check.
1707 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001708
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001709 SDOperand TheChain(Node, Node->getNumValues()-1);
1710 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001711
1712 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001713 E = Node->use_end(); ; ++UI) {
1714 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001715
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001716 // Make sure to only follow users of our token chain.
1717 SDNode *User = *UI;
1718 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1719 if (User->getOperand(i) == TheChain)
1720 return FindAdjCallStackUp(User);
1721 }
1722 assert(0 && "Unreachable");
1723 abort();
1724}
1725
1726/// FindInputOutputChains - If we are replacing an operation with a call we need
1727/// to find the call that occurs before and the call that occurs after it to
1728/// properly serialize the calls in the block.
1729static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1730 SDOperand Entry) {
1731 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanc7c16572005-04-11 03:01:51 +00001732 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001733 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1734 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001735
Nate Begemanc7c16572005-04-11 03:01:51 +00001736 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1737 // previous call in the function. LatestCallStackDown may in that case be
1738 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1739 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1740 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1741 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1742 else
1743 LatestAdjCallStackUp = Entry.Val;
1744 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001745
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001746 SDNode *EarliestAdjCallStackUp = 0;
1747 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1748
1749 if (EarliestAdjCallStackUp) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001750 //std::cerr << "Found node: ";
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001751 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1752 }
1753
1754 return SDOperand(LatestAdjCallStackUp, 0);
1755}
1756
1757
1758
Chris Lattner77e77a62005-01-21 06:05:23 +00001759// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1760// does not fit into a register, return the lo part and set the hi part to the
1761// by-reg argument. If it does fit into a single register, return the result
1762// and leave the Hi part unset.
1763SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1764 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001765 SDNode *OutChain;
1766 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1767 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00001768 if (InChain.Val == 0)
1769 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001770
Chris Lattner77e77a62005-01-21 06:05:23 +00001771 TargetLowering::ArgListTy Args;
1772 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1773 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1774 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1775 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1776 }
1777 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001778
Chris Lattner77e77a62005-01-21 06:05:23 +00001779 // We don't care about token chains for libcalls. We just use the entry
1780 // node as our input and ignore the output chain. This allows us to place
1781 // calls wherever we need them to satisfy data dependences.
1782 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begeman8e21e712005-03-26 01:29:23 +00001783 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattner77e77a62005-01-21 06:05:23 +00001784 Args, DAG).first;
1785 switch (getTypeAction(Result.getValueType())) {
1786 default: assert(0 && "Unknown thing");
1787 case Legal:
1788 return Result;
1789 case Promote:
1790 assert(0 && "Cannot promote this yet!");
1791 case Expand:
1792 SDOperand Lo;
1793 ExpandOp(Result, Lo, Hi);
1794 return Lo;
1795 }
1796}
1797
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001798
Chris Lattner77e77a62005-01-21 06:05:23 +00001799/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1800/// destination type is legal.
1801SDOperand SelectionDAGLegalize::
1802ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1803 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1804 assert(getTypeAction(Source.getValueType()) == Expand &&
1805 "This is not an expansion!");
1806 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1807
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001808 SDNode *OutChain;
1809 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1810 DAG.getEntryNode());
1811
Chris Lattnerfed55772005-01-23 23:19:44 +00001812 const char *FnName = 0;
Chris Lattner77e77a62005-01-21 06:05:23 +00001813 if (isSigned) {
1814 if (DestTy == MVT::f32)
1815 FnName = "__floatdisf";
1816 else {
1817 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1818 FnName = "__floatdidf";
1819 }
1820 } else {
1821 // If this is unsigned, and not supported, first perform the conversion to
1822 // signed, then adjust the result if the sign bit is set.
Chris Lattnerffe284c2005-04-13 03:42:14 +00001823 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner77e77a62005-01-21 06:05:23 +00001824
Chris Lattnere9c35e72005-04-13 05:09:42 +00001825 assert(Source.getValueType() == MVT::i64 &&
1826 "This only works for 64-bit -> FP");
1827 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1828 // incoming integer is set. To handle this, we dynamically test to see if
1829 // it is set, and, if so, add a fudge factor.
1830 SDOperand Lo, Hi;
1831 ExpandOp(Source, Lo, Hi);
1832
1833 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
1834 DAG.getConstant(0, Hi.getValueType()));
1835 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1836 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1837 SignSet, Four, Zero);
1838 // FIXME: This is almost certainly broken for big-endian systems. Should
1839 // this just put the fudge factor in the low bits of the uint64 constant or?
1840 static Constant *FudgeFactor =
1841 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
1842
1843 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
1844 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
1845 TLI.getPointerTy());
1846 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1847 SDOperand FudgeInReg;
1848 if (DestTy == MVT::f32)
1849 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx);
1850 else {
1851 assert(DestTy == MVT::f64 && "Unexpected conversion");
1852 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
1853 CPIdx, MVT::f32);
1854 }
1855 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00001856 }
1857 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1858
1859 TargetLowering::ArgListTy Args;
1860 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1861 Args.push_back(std::make_pair(Source, ArgTy));
1862
1863 // We don't care about token chains for libcalls. We just use the entry
1864 // node as our input and ignore the output chain. This allows us to place
1865 // calls wherever we need them to satisfy data dependences.
1866 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begeman8e21e712005-03-26 01:29:23 +00001867 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner77e77a62005-01-21 06:05:23 +00001868}
Misha Brukmanedf128a2005-04-21 22:36:52 +00001869
Chris Lattnere34b3962005-01-19 04:19:40 +00001870
1871
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872/// ExpandOp - Expand the specified SDOperand into its two component pieces
1873/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1874/// LegalizeNodes map is filled in for any results that are not expanded, the
1875/// ExpandedNodes map is filled in for any results that are expanded, and the
1876/// Lo/Hi values are returned.
1877void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1878 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001879 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001880 SDNode *Node = Op.Val;
1881 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1882 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1883 assert(MVT::isInteger(NVT) && NVT < VT &&
1884 "Cannot expand to FP value or to larger int value!");
1885
1886 // If there is more than one use of this, see if we already expanded it.
1887 // There is no use remembering values that only have a single use, as the map
1888 // entries will never be reused.
1889 if (!Node->hasOneUse()) {
1890 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1891 = ExpandedNodes.find(Op);
1892 if (I != ExpandedNodes.end()) {
1893 Lo = I->second.first;
1894 Hi = I->second.second;
1895 return;
1896 }
1897 }
1898
Chris Lattner4e6c7462005-01-08 19:27:05 +00001899 // Expanding to multiple registers needs to perform an optimization step, and
1900 // is not careful to avoid operations the target does not support. Make sure
1901 // that all generated operations are legalized in the next iteration.
1902 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001903
Chris Lattner3e928bb2005-01-07 07:47:09 +00001904 switch (Node->getOpcode()) {
1905 default:
1906 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1907 assert(0 && "Do not know how to expand this operator!");
1908 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001909 case ISD::UNDEF:
1910 Lo = DAG.getNode(ISD::UNDEF, NVT);
1911 Hi = DAG.getNode(ISD::UNDEF, NVT);
1912 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001913 case ISD::Constant: {
1914 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1915 Lo = DAG.getConstant(Cst, NVT);
1916 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1917 break;
1918 }
1919
1920 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00001921 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001922 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00001923 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1924 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001925
Chris Lattner69a52152005-01-14 22:38:01 +00001926 // Remember that we legalized the chain.
1927 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1928
Chris Lattner3e928bb2005-01-07 07:47:09 +00001929 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1930 break;
1931 }
1932
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00001933 case ISD::BUILD_PAIR:
1934 // Legalize both operands. FIXME: in the future we should handle the case
1935 // where the two elements are not legal.
1936 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1937 Lo = LegalizeOp(Node->getOperand(0));
1938 Hi = LegalizeOp(Node->getOperand(1));
1939 break;
1940
Chris Lattner3e928bb2005-01-07 07:47:09 +00001941 case ISD::LOAD: {
1942 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1943 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1944 Lo = DAG.getLoad(NVT, Ch, Ptr);
1945
1946 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00001947 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001948 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1949 getIntPtrConstant(IncrementSize));
Chris Lattnerec39a452005-01-19 18:02:17 +00001950 Hi = DAG.getLoad(NVT, Ch, Ptr);
1951
1952 // Build a factor node to remember that this load is independent of the
1953 // other one.
1954 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1955 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001956
Chris Lattner3e928bb2005-01-07 07:47:09 +00001957 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00001958 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001959 if (!TLI.isLittleEndian())
1960 std::swap(Lo, Hi);
1961 break;
1962 }
1963 case ISD::CALL: {
1964 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1965 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1966
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001967 bool Changed = false;
1968 std::vector<SDOperand> Ops;
1969 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1970 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1971 Changed |= Ops.back() != Node->getOperand(i);
1972 }
1973
Chris Lattner3e928bb2005-01-07 07:47:09 +00001974 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1975 "Can only expand a call once so far, not i64 -> i16!");
1976
1977 std::vector<MVT::ValueType> RetTyVTs;
1978 RetTyVTs.reserve(3);
1979 RetTyVTs.push_back(NVT);
1980 RetTyVTs.push_back(NVT);
1981 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001982 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001983 Lo = SDOperand(NC, 0);
1984 Hi = SDOperand(NC, 1);
1985
1986 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00001987 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001988 break;
1989 }
1990 case ISD::AND:
1991 case ISD::OR:
1992 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1993 SDOperand LL, LH, RL, RH;
1994 ExpandOp(Node->getOperand(0), LL, LH);
1995 ExpandOp(Node->getOperand(1), RL, RH);
1996 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1997 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1998 break;
1999 }
2000 case ISD::SELECT: {
2001 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002002
2003 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2004 case Expand: assert(0 && "It's impossible to expand bools");
2005 case Legal:
2006 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2007 break;
2008 case Promote:
2009 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2010 break;
2011 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002012 ExpandOp(Node->getOperand(1), LL, LH);
2013 ExpandOp(Node->getOperand(2), RL, RH);
2014 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2015 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2016 break;
2017 }
2018 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002019 SDOperand In;
2020 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2021 case Expand: assert(0 && "expand-expand not implemented yet!");
2022 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2023 case Promote:
2024 In = PromoteOp(Node->getOperand(0));
2025 // Emit the appropriate sign_extend_inreg to get the value we want.
2026 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2027 Node->getOperand(0).getValueType());
2028 break;
2029 }
2030
Chris Lattner3e928bb2005-01-07 07:47:09 +00002031 // The low part is just a sign extension of the input (which degenerates to
2032 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002033 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002034
Chris Lattner3e928bb2005-01-07 07:47:09 +00002035 // The high part is obtained by SRA'ing all but one of the bits of the lo
2036 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002037 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002038 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2039 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002040 break;
2041 }
Chris Lattner06098e02005-04-03 23:41:52 +00002042 case ISD::ZERO_EXTEND: {
2043 SDOperand In;
2044 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2045 case Expand: assert(0 && "expand-expand not implemented yet!");
2046 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2047 case Promote:
2048 In = PromoteOp(Node->getOperand(0));
2049 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002050 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002051 break;
2052 }
2053
Chris Lattner3e928bb2005-01-07 07:47:09 +00002054 // The low part is just a zero extension of the input (which degenerates to
2055 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002056 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002057
Chris Lattner3e928bb2005-01-07 07:47:09 +00002058 // The high part is just a zero.
2059 Hi = DAG.getConstant(0, NVT);
2060 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002061 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002062 // These operators cannot be expanded directly, emit them as calls to
2063 // library functions.
2064 case ISD::FP_TO_SINT:
2065 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002066 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002067 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002068 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002069 break;
2070 case ISD::FP_TO_UINT:
2071 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002072 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002073 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002074 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002075 break;
2076
Chris Lattnere34b3962005-01-19 04:19:40 +00002077 case ISD::SHL:
2078 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002079 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002080 break;
Chris Lattner47599822005-04-02 03:38:53 +00002081
2082 // If this target supports SHL_PARTS, use it.
2083 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002084 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2085 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002086 break;
2087 }
2088
Chris Lattnere34b3962005-01-19 04:19:40 +00002089 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002090 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002091 break;
2092
2093 case ISD::SRA:
2094 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002095 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002096 break;
Chris Lattner47599822005-04-02 03:38:53 +00002097
2098 // If this target supports SRA_PARTS, use it.
2099 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002100 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2101 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002102 break;
2103 }
2104
Chris Lattnere34b3962005-01-19 04:19:40 +00002105 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002106 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002107 break;
2108 case ISD::SRL:
2109 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002110 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002111 break;
Chris Lattner47599822005-04-02 03:38:53 +00002112
2113 // If this target supports SRL_PARTS, use it.
2114 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002115 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2116 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002117 break;
2118 }
2119
Chris Lattnere34b3962005-01-19 04:19:40 +00002120 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002121 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002122 break;
2123
Misha Brukmanedf128a2005-04-21 22:36:52 +00002124 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002125 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2126 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002127 break;
2128 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002129 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2130 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002131 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002132 case ISD::MUL: {
2133 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2134 SDOperand LL, LH, RL, RH;
2135 ExpandOp(Node->getOperand(0), LL, LH);
2136 ExpandOp(Node->getOperand(1), RL, RH);
2137 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2138 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2139 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2140 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2141 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2142 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2143 } else {
2144 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2145 }
2146 break;
2147 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002148 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2149 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2150 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2151 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002152 }
2153
2154 // Remember in a map if the values will be reused later.
2155 if (!Node->hasOneUse()) {
2156 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2157 std::make_pair(Lo, Hi))).second;
2158 assert(isNew && "Value already expanded?!?");
2159 }
2160}
2161
2162
2163// SelectionDAG::Legalize - This is the entry point for the file.
2164//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002165void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002166 /// run - This is the main entry point to this class.
2167 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002168 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002169}
2170