blob: 5f8a21aca0f73d58378dbb61143789f0316261ff [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattnerdc750592005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner1f2c9d82005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattnerdc750592005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattnerea4ca942005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000089
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000121
Chris Lattneraac464e2005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000128 void ExpandAddSub(bool isAdd, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000130
Chris Lattnerdc750592005-01-07 07:47:09 +0000131 SDOperand getIntPtrConstant(uint64_t Val) {
132 return DAG.getConstant(Val, TLI.getPointerTy());
133 }
134};
135}
136
137
Chris Lattner4add7e32005-01-23 04:42:50 +0000138SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
139 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
140 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 assert(MVT::LAST_VALUETYPE <= 16 &&
142 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000143}
144
Chris Lattnerdc750592005-01-07 07:47:09 +0000145void SelectionDAGLegalize::LegalizeDAG() {
146 SDOperand OldRoot = DAG.getRoot();
147 SDOperand NewRoot = LegalizeOp(OldRoot);
148 DAG.setRoot(NewRoot);
149
150 ExpandedNodes.clear();
151 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000152 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000153
154 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000155 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000156}
157
158SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000159 assert(getTypeAction(Op.getValueType()) == Legal &&
160 "Caller should expand or promote operands that are not legal!");
161
Chris Lattnerdc750592005-01-07 07:47:09 +0000162 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000163 // register on this target, make sure to expand or promote them.
164 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000165 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
166 switch (getTypeAction(Op.Val->getValueType(i))) {
167 case Legal: break; // Nothing to do.
168 case Expand: {
169 SDOperand T1, T2;
170 ExpandOp(Op.getValue(i), T1, T2);
171 assert(LegalizedNodes.count(Op) &&
172 "Expansion didn't add legal operands!");
173 return LegalizedNodes[Op];
174 }
175 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000176 PromoteOp(Op.getValue(i));
177 assert(LegalizedNodes.count(Op) &&
178 "Expansion didn't add legal operands!");
179 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000180 }
181 }
182
Chris Lattner85d70c62005-01-11 05:57:22 +0000183 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
184 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000185
Chris Lattnerec26b482005-01-09 19:03:49 +0000186 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000187
188 SDOperand Result = Op;
189 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000190
191 switch (Node->getOpcode()) {
192 default:
193 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
194 assert(0 && "Do not know how to legalize this operator!");
195 abort();
196 case ISD::EntryToken:
197 case ISD::FrameIndex:
198 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000199 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000200 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000201 assert(getTypeAction(Node->getValueType(0)) == Legal &&
202 "This must be legal!");
203 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000204 case ISD::CopyFromReg:
205 Tmp1 = LegalizeOp(Node->getOperand(0));
206 if (Tmp1 != Node->getOperand(0))
207 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
208 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000209 else
210 Result = Op.getValue(0);
211
212 // Since CopyFromReg produces two values, make sure to remember that we
213 // legalized both of them.
214 AddLegalizedOperand(Op.getValue(0), Result);
215 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
216 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000217 case ISD::ImplicitDef:
218 Tmp1 = LegalizeOp(Node->getOperand(0));
219 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000220 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000221 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000222 case ISD::Constant:
223 // We know we don't need to expand constants here, constants only have one
224 // value and we check that it is fine above.
225
226 // FIXME: Maybe we should handle things like targets that don't support full
227 // 32-bit immediates?
228 break;
229 case ISD::ConstantFP: {
230 // Spill FP immediates to the constant pool if the target cannot directly
231 // codegen them. Targets often have some immediate values that can be
232 // efficiently generated into an FP register without a load. We explicitly
233 // leave these constants as ConstantFP nodes for the target to deal with.
234
235 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
236
237 // Check to see if this FP immediate is already legal.
238 bool isLegal = false;
239 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
240 E = TLI.legal_fpimm_end(); I != E; ++I)
241 if (CFP->isExactlyValue(*I)) {
242 isLegal = true;
243 break;
244 }
245
246 if (!isLegal) {
247 // Otherwise we need to spill the constant to memory.
248 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
249
250 bool Extend = false;
251
252 // If a FP immediate is precise when represented as a float, we put it
253 // into the constant pool as a float, even if it's is statically typed
254 // as a double.
255 MVT::ValueType VT = CFP->getValueType(0);
256 bool isDouble = VT == MVT::f64;
257 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
258 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000259 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
260 // Only do this if the target has a native EXTLOAD instruction from
261 // f32.
262 TLI.getOperationAction(ISD::EXTLOAD,
263 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000264 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
265 VT = MVT::f32;
266 Extend = true;
267 }
268
269 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
270 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000271 if (Extend) {
272 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
273 MVT::f32);
274 } else {
275 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
276 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000277 }
278 break;
279 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000280 case ISD::TokenFactor: {
281 std::vector<SDOperand> Ops;
282 bool Changed = false;
283 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000284 SDOperand Op = Node->getOperand(i);
285 // Fold single-use TokenFactor nodes into this token factor as we go.
286 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
287 Changed = true;
288 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
289 Ops.push_back(LegalizeOp(Op.getOperand(j)));
290 } else {
291 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
292 Changed |= Ops[i] != Op;
293 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000294 }
295 if (Changed)
296 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
297 break;
298 }
299
Chris Lattnerdc750592005-01-07 07:47:09 +0000300 case ISD::ADJCALLSTACKDOWN:
301 case ISD::ADJCALLSTACKUP:
302 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
303 // There is no need to legalize the size argument (Operand #1)
304 if (Tmp1 != Node->getOperand(0))
305 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
306 Node->getOperand(1));
307 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000308 case ISD::DYNAMIC_STACKALLOC:
309 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
310 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
311 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
312 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
313 Tmp3 != Node->getOperand(2))
314 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
315 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000316 else
317 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000318
319 // Since this op produces two values, make sure to remember that we
320 // legalized both of them.
321 AddLegalizedOperand(SDOperand(Node, 0), Result);
322 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
323 return Result.getValue(Op.ResNo);
324
Chris Lattner3d95c142005-01-19 20:24:35 +0000325 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000326 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
327 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000328
329 bool Changed = false;
330 std::vector<SDOperand> Ops;
331 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
332 Ops.push_back(LegalizeOp(Node->getOperand(i)));
333 Changed |= Ops.back() != Node->getOperand(i);
334 }
335
336 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000337 std::vector<MVT::ValueType> RetTyVTs;
338 RetTyVTs.reserve(Node->getNumValues());
339 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000340 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000341 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000342 } else {
343 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000344 }
Chris Lattner9242c502005-01-09 19:43:23 +0000345 // Since calls produce multiple values, make sure to remember that we
346 // legalized all of them.
347 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
348 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
349 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000350 }
Chris Lattner68a12142005-01-07 22:12:08 +0000351 case ISD::BR:
352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
353 if (Tmp1 != Node->getOperand(0))
354 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
355 break;
356
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000357 case ISD::BRCOND:
358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000359
360 switch (getTypeAction(Node->getOperand(1).getValueType())) {
361 case Expand: assert(0 && "It's impossible to expand bools");
362 case Legal:
363 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
364 break;
365 case Promote:
366 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
367 break;
368 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000369 // Basic block destination (Op#2) is always legal.
370 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
371 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
372 Node->getOperand(2));
373 break;
374
Chris Lattnerdc750592005-01-07 07:47:09 +0000375 case ISD::LOAD:
376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
377 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
378 if (Tmp1 != Node->getOperand(0) ||
379 Tmp2 != Node->getOperand(1))
380 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000381 else
382 Result = SDOperand(Node, 0);
383
384 // Since loads produce two values, make sure to remember that we legalized
385 // both of them.
386 AddLegalizedOperand(SDOperand(Node, 0), Result);
387 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
388 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000389
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000390 case ISD::EXTLOAD:
391 case ISD::SEXTLOAD:
392 case ISD::ZEXTLOAD:
393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
394 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
395 if (Tmp1 != Node->getOperand(0) ||
396 Tmp2 != Node->getOperand(1))
397 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
398 cast<MVTSDNode>(Node)->getExtraValueType());
399 else
400 Result = SDOperand(Node, 0);
401
402 // Since loads produce two values, make sure to remember that we legalized
403 // both of them.
404 AddLegalizedOperand(SDOperand(Node, 0), Result);
405 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
406 return Result.getValue(Op.ResNo);
407
Chris Lattnerdc750592005-01-07 07:47:09 +0000408 case ISD::EXTRACT_ELEMENT:
409 // Get both the low and high parts.
410 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
411 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
412 Result = Tmp2; // 1 -> Hi
413 else
414 Result = Tmp1; // 0 -> Lo
415 break;
416
417 case ISD::CopyToReg:
418 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
419
420 switch (getTypeAction(Node->getOperand(1).getValueType())) {
421 case Legal:
422 // Legalize the incoming value (must be legal).
423 Tmp2 = LegalizeOp(Node->getOperand(1));
424 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000425 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000426 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000427 case Promote:
428 Tmp2 = PromoteOp(Node->getOperand(1));
429 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
430 break;
431 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 SDOperand Lo, Hi;
433 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000434 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000435 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
436 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
437 // Note that the copytoreg nodes are independent of each other.
438 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000439 assert(isTypeLegal(Result.getValueType()) &&
440 "Cannot expand multiple times yet (i64 -> i16)");
441 break;
442 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000443 break;
444
445 case ISD::RET:
446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
447 switch (Node->getNumOperands()) {
448 case 2: // ret val
449 switch (getTypeAction(Node->getOperand(1).getValueType())) {
450 case Legal:
451 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000452 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000453 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
454 break;
455 case Expand: {
456 SDOperand Lo, Hi;
457 ExpandOp(Node->getOperand(1), Lo, Hi);
458 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
459 break;
460 }
461 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000462 Tmp2 = PromoteOp(Node->getOperand(1));
463 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
464 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000465 }
466 break;
467 case 1: // ret void
468 if (Tmp1 != Node->getOperand(0))
469 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
470 break;
471 default: { // ret <values>
472 std::vector<SDOperand> NewValues;
473 NewValues.push_back(Tmp1);
474 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
475 switch (getTypeAction(Node->getOperand(i).getValueType())) {
476 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000477 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000478 break;
479 case Expand: {
480 SDOperand Lo, Hi;
481 ExpandOp(Node->getOperand(i), Lo, Hi);
482 NewValues.push_back(Lo);
483 NewValues.push_back(Hi);
484 break;
485 }
486 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000487 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000488 }
489 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
490 break;
491 }
492 }
493 break;
494 case ISD::STORE:
495 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
496 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
497
Chris Lattnere69daaf2005-01-08 06:25:56 +0000498 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000499 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000500 if (CFP->getValueType(0) == MVT::f32) {
501 union {
502 unsigned I;
503 float F;
504 } V;
505 V.F = CFP->getValue();
506 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
507 DAG.getConstant(V.I, MVT::i32), Tmp2);
508 } else {
509 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
510 union {
511 uint64_t I;
512 double F;
513 } V;
514 V.F = CFP->getValue();
515 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
516 DAG.getConstant(V.I, MVT::i64), Tmp2);
517 }
Chris Lattnera4743132005-02-22 07:23:39 +0000518 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000519 }
520
Chris Lattnerdc750592005-01-07 07:47:09 +0000521 switch (getTypeAction(Node->getOperand(1).getValueType())) {
522 case Legal: {
523 SDOperand Val = LegalizeOp(Node->getOperand(1));
524 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
525 Tmp2 != Node->getOperand(2))
526 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
527 break;
528 }
529 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000530 // Truncate the value and store the result.
531 Tmp3 = PromoteOp(Node->getOperand(1));
532 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
533 Node->getOperand(1).getValueType());
534 break;
535
Chris Lattnerdc750592005-01-07 07:47:09 +0000536 case Expand:
537 SDOperand Lo, Hi;
538 ExpandOp(Node->getOperand(1), Lo, Hi);
539
540 if (!TLI.isLittleEndian())
541 std::swap(Lo, Hi);
542
Chris Lattner0d03eb42005-01-19 18:02:17 +0000543 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000544
Chris Lattner0d03eb42005-01-19 18:02:17 +0000545 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000546 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
547 getIntPtrConstant(IncrementSize));
548 assert(isTypeLegal(Tmp2.getValueType()) &&
549 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000550 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
551 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
552 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000553 }
554 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000555 case ISD::PCMARKER:
556 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
557 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1, Node->getOperand(1));
558 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000559 case ISD::TRUNCSTORE:
560 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
561 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
562
563 switch (getTypeAction(Node->getOperand(1).getValueType())) {
564 case Legal:
565 Tmp2 = LegalizeOp(Node->getOperand(1));
566 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
567 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000568 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000569 cast<MVTSDNode>(Node)->getExtraValueType());
570 break;
571 case Promote:
572 case Expand:
573 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
574 }
575 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000576 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000577 switch (getTypeAction(Node->getOperand(0).getValueType())) {
578 case Expand: assert(0 && "It's impossible to expand bools");
579 case Legal:
580 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
581 break;
582 case Promote:
583 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
584 break;
585 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000586 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000587 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000588
589 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
590 default: assert(0 && "This action is not supported yet!");
591 case TargetLowering::Legal:
592 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
593 Tmp3 != Node->getOperand(2))
594 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
595 Tmp1, Tmp2, Tmp3);
596 break;
597 case TargetLowering::Promote: {
598 MVT::ValueType NVT =
599 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
600 unsigned ExtOp, TruncOp;
601 if (MVT::isInteger(Tmp2.getValueType())) {
602 ExtOp = ISD::ZERO_EXTEND;
603 TruncOp = ISD::TRUNCATE;
604 } else {
605 ExtOp = ISD::FP_EXTEND;
606 TruncOp = ISD::FP_ROUND;
607 }
608 // Promote each of the values to the new type.
609 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
610 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
611 // Perform the larger operation, then round down.
612 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
613 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
614 break;
615 }
616 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000617 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000618 case ISD::SETCC:
619 switch (getTypeAction(Node->getOperand(0).getValueType())) {
620 case Legal:
621 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
622 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
623 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
624 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000625 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000626 break;
627 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000628 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
629 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
630
631 // If this is an FP compare, the operands have already been extended.
632 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
633 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000634 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000635
636 // Otherwise, we have to insert explicit sign or zero extends. Note
637 // that we could insert sign extends for ALL conditions, but zero extend
638 // is cheaper on many machines (an AND instead of two shifts), so prefer
639 // it.
640 switch (cast<SetCCSDNode>(Node)->getCondition()) {
641 default: assert(0 && "Unknown integer comparison!");
642 case ISD::SETEQ:
643 case ISD::SETNE:
644 case ISD::SETUGE:
645 case ISD::SETUGT:
646 case ISD::SETULE:
647 case ISD::SETULT:
648 // ALL of these operations will work if we either sign or zero extend
649 // the operands (including the unsigned comparisons!). Zero extend is
650 // usually a simpler/cheaper operation, so prefer it.
651 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
652 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
653 break;
654 case ISD::SETGE:
655 case ISD::SETGT:
656 case ISD::SETLT:
657 case ISD::SETLE:
658 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
659 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
660 break;
661 }
662
663 }
664 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000665 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000666 break;
667 case Expand:
668 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
669 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
670 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
671 switch (cast<SetCCSDNode>(Node)->getCondition()) {
672 case ISD::SETEQ:
673 case ISD::SETNE:
674 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
675 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
676 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000677 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
678 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000679 DAG.getConstant(0, Tmp1.getValueType()));
680 break;
681 default:
682 // FIXME: This generated code sucks.
683 ISD::CondCode LowCC;
684 switch (cast<SetCCSDNode>(Node)->getCondition()) {
685 default: assert(0 && "Unknown integer setcc!");
686 case ISD::SETLT:
687 case ISD::SETULT: LowCC = ISD::SETULT; break;
688 case ISD::SETGT:
689 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
690 case ISD::SETLE:
691 case ISD::SETULE: LowCC = ISD::SETULE; break;
692 case ISD::SETGE:
693 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
694 }
695
696 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
697 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
698 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
699
700 // NOTE: on targets without efficient SELECT of bools, we can always use
701 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000702 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000703 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000704 Node->getValueType(0), LHSHi, RHSHi);
705 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
706 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
707 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000708 break;
709 }
710 }
711 break;
712
Chris Lattner85d70c62005-01-11 05:57:22 +0000713 case ISD::MEMSET:
714 case ISD::MEMCPY:
715 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000716 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000717 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
718
719 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
720 switch (getTypeAction(Node->getOperand(2).getValueType())) {
721 case Expand: assert(0 && "Cannot expand a byte!");
722 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000723 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000724 break;
725 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000726 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000727 break;
728 }
729 } else {
730 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
731 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000732
733 SDOperand Tmp4;
734 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000735 case Expand: assert(0 && "Cannot expand this yet!");
736 case Legal:
737 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000738 break;
739 case Promote:
740 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000741 break;
742 }
743
744 SDOperand Tmp5;
745 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
746 case Expand: assert(0 && "Cannot expand this yet!");
747 case Legal:
748 Tmp5 = LegalizeOp(Node->getOperand(4));
749 break;
750 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000751 Tmp5 = PromoteOp(Node->getOperand(4));
752 break;
753 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000754
755 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
756 default: assert(0 && "This action not implemented for this operation!");
757 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000758 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
759 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
760 Tmp5 != Node->getOperand(4)) {
761 std::vector<SDOperand> Ops;
762 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
763 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
764 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
765 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000766 break;
767 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000768 // Otherwise, the target does not support this operation. Lower the
769 // operation to an explicit libcall as appropriate.
770 MVT::ValueType IntPtr = TLI.getPointerTy();
771 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
772 std::vector<std::pair<SDOperand, const Type*> > Args;
773
Reid Spencer6dced922005-01-12 14:53:45 +0000774 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000775 if (Node->getOpcode() == ISD::MEMSET) {
776 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
777 // Extend the ubyte argument to be an int value for the call.
778 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
779 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
780 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
781
782 FnName = "memset";
783 } else if (Node->getOpcode() == ISD::MEMCPY ||
784 Node->getOpcode() == ISD::MEMMOVE) {
785 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
786 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
787 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
788 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
789 } else {
790 assert(0 && "Unknown op!");
791 }
792 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000793 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000794 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
795 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000796 break;
797 }
798 case TargetLowering::Custom:
799 std::vector<SDOperand> Ops;
800 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
801 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
802 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
803 Result = TLI.LowerOperation(Result);
804 Result = LegalizeOp(Result);
805 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000806 }
807 break;
808 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000809 case ISD::ADD_PARTS:
810 case ISD::SUB_PARTS: {
811 std::vector<SDOperand> Ops;
812 bool Changed = false;
813 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
814 Ops.push_back(LegalizeOp(Node->getOperand(i)));
815 Changed |= Ops.back() != Node->getOperand(i);
816 }
817 if (Changed)
818 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
819 break;
820 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000821 case ISD::ADD:
822 case ISD::SUB:
823 case ISD::MUL:
824 case ISD::UDIV:
825 case ISD::SDIV:
826 case ISD::UREM:
827 case ISD::SREM:
828 case ISD::AND:
829 case ISD::OR:
830 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000831 case ISD::SHL:
832 case ISD::SRL:
833 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000834 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
835 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
836 if (Tmp1 != Node->getOperand(0) ||
837 Tmp2 != Node->getOperand(1))
838 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
839 break;
840 case ISD::ZERO_EXTEND:
841 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000842 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000843 case ISD::FP_EXTEND:
844 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000845 case ISD::FP_TO_SINT:
846 case ISD::FP_TO_UINT:
847 case ISD::SINT_TO_FP:
848 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000849 switch (getTypeAction(Node->getOperand(0).getValueType())) {
850 case Legal:
851 Tmp1 = LegalizeOp(Node->getOperand(0));
852 if (Tmp1 != Node->getOperand(0))
853 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
854 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000855 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +0000856 if (Node->getOpcode() == ISD::SINT_TO_FP ||
857 Node->getOpcode() == ISD::UINT_TO_FP) {
858 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
859 Node->getValueType(0), Node->getOperand(0));
860 Result = LegalizeOp(Result);
861 break;
862 }
Chris Lattnera65a2f02005-01-07 22:37:48 +0000863 // In the expand case, we must be dealing with a truncate, because
864 // otherwise the result would be larger than the source.
865 assert(Node->getOpcode() == ISD::TRUNCATE &&
866 "Shouldn't need to expand other operators here!");
867 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
868
869 // Since the result is legal, we should just be able to truncate the low
870 // part of the source.
871 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
872 break;
873
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000874 case Promote:
875 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000876 case ISD::ZERO_EXTEND:
877 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000878 // NOTE: Any extend would work here...
879 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
880 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000881 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000882 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000883 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000884 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000885 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000886 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000887 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
888 Result, Node->getOperand(0).getValueType());
889 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000890 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000891 Result = PromoteOp(Node->getOperand(0));
892 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
893 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000894 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000895 Result = PromoteOp(Node->getOperand(0));
896 if (Result.getValueType() != Op.getValueType())
897 // Dynamically dead while we have only 2 FP types.
898 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
899 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000900 case ISD::FP_ROUND:
901 case ISD::FP_TO_SINT:
902 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000903 Result = PromoteOp(Node->getOperand(0));
904 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
905 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000906 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000907 Result = PromoteOp(Node->getOperand(0));
908 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
909 Result, Node->getOperand(0).getValueType());
910 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
911 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000912 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000913 Result = PromoteOp(Node->getOperand(0));
914 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
915 Result, Node->getOperand(0).getValueType());
916 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
917 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000918 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000919 }
920 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000921 case ISD::FP_ROUND_INREG:
922 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000923 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000924 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000925 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
926
927 // If this operation is not supported, convert it to a shl/shr or load/store
928 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000929 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
930 default: assert(0 && "This action not supported for this op yet!");
931 case TargetLowering::Legal:
932 if (Tmp1 != Node->getOperand(0))
933 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
934 ExtraVT);
935 break;
936 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000937 // If this is an integer extend and shifts are supported, do that.
938 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
939 // NOTE: we could fall back on load/store here too for targets without
940 // AND. However, it is doubtful that any exist.
941 // AND out the appropriate bits.
942 SDOperand Mask =
943 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
944 Node->getValueType(0));
945 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
946 Node->getOperand(0), Mask);
947 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
948 // NOTE: we could fall back on load/store here too for targets without
949 // SAR. However, it is doubtful that any exist.
950 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
951 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +0000952 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +0000953 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
954 Node->getOperand(0), ShiftCst);
955 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
956 Result, ShiftCst);
957 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
958 // The only way we can lower this is to turn it into a STORETRUNC,
959 // EXTLOAD pair, targetting a temporary location (a stack slot).
960
961 // NOTE: there is a choice here between constantly creating new stack
962 // slots and always reusing the same one. We currently always create
963 // new ones, as reuse may inhibit scheduling.
964 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
965 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
966 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
967 MachineFunction &MF = DAG.getMachineFunction();
968 int SSFI =
969 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
970 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
971 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
972 Node->getOperand(0), StackSlot, ExtraVT);
973 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
974 Result, StackSlot, ExtraVT);
975 } else {
976 assert(0 && "Unknown op");
977 }
978 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000979 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000980 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000981 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000982 }
Chris Lattner99222f72005-01-15 07:15:18 +0000983 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000984
Chris Lattnerea4ca942005-01-07 22:28:47 +0000985 if (!Op.Val->hasOneUse())
986 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000987
988 return Result;
989}
990
Chris Lattner4d978642005-01-15 22:16:26 +0000991/// PromoteOp - Given an operation that produces a value in an invalid type,
992/// promote it to compute the value into a larger type. The produced value will
993/// have the correct bits for the low portion of the register, but no guarantee
994/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000995SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
996 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000997 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000998 assert(getTypeAction(VT) == Promote &&
999 "Caller should expand or legalize operands that are not promotable!");
1000 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1001 "Cannot promote to smaller type!");
1002
1003 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1004 if (I != PromotedNodes.end()) return I->second;
1005
1006 SDOperand Tmp1, Tmp2, Tmp3;
1007
1008 SDOperand Result;
1009 SDNode *Node = Op.Val;
1010
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001011 // Promotion needs an optimization step to clean up after it, and is not
1012 // careful to avoid operations the target does not support. Make sure that
1013 // all generated operations are legalized in the next iteration.
1014 NeedsAnotherIteration = true;
1015
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001016 switch (Node->getOpcode()) {
1017 default:
1018 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1019 assert(0 && "Do not know how to promote this operator!");
1020 abort();
1021 case ISD::Constant:
1022 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1023 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1024 break;
1025 case ISD::ConstantFP:
1026 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1027 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1028 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001029 case ISD::CopyFromReg:
1030 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1031 Node->getOperand(0));
1032 // Remember that we legalized the chain.
1033 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1034 break;
1035
Chris Lattner2cb338d2005-01-18 02:59:52 +00001036 case ISD::SETCC:
1037 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1038 "SetCC type is not legal??");
1039 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1040 TLI.getSetCCResultTy(), Node->getOperand(0),
1041 Node->getOperand(1));
1042 Result = LegalizeOp(Result);
1043 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001044
1045 case ISD::TRUNCATE:
1046 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1047 case Legal:
1048 Result = LegalizeOp(Node->getOperand(0));
1049 assert(Result.getValueType() >= NVT &&
1050 "This truncation doesn't make sense!");
1051 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1052 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1053 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001054 case Promote:
1055 // The truncation is not required, because we don't guarantee anything
1056 // about high bits anyway.
1057 Result = PromoteOp(Node->getOperand(0));
1058 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001059 case Expand:
1060 assert(0 && "Cannot handle expand yet");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001061 }
1062 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001063 case ISD::SIGN_EXTEND:
1064 case ISD::ZERO_EXTEND:
1065 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1066 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1067 case Legal:
1068 // Input is legal? Just do extend all the way to the larger type.
1069 Result = LegalizeOp(Node->getOperand(0));
1070 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1071 break;
1072 case Promote:
1073 // Promote the reg if it's smaller.
1074 Result = PromoteOp(Node->getOperand(0));
1075 // The high bits are not guaranteed to be anything. Insert an extend.
1076 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001077 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1078 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001079 else
Chris Lattner05596912005-02-04 18:39:19 +00001080 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1081 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001082 break;
1083 }
1084 break;
1085
1086 case ISD::FP_EXTEND:
1087 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1088 case ISD::FP_ROUND:
1089 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1090 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1091 case Promote: assert(0 && "Unreachable with 2 FP types!");
1092 case Legal:
1093 // Input is legal? Do an FP_ROUND_INREG.
1094 Result = LegalizeOp(Node->getOperand(0));
1095 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1096 break;
1097 }
1098 break;
1099
1100 case ISD::SINT_TO_FP:
1101 case ISD::UINT_TO_FP:
1102 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1103 case Legal:
1104 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001105 // No extra round required here.
1106 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001107 break;
1108
1109 case Promote:
1110 Result = PromoteOp(Node->getOperand(0));
1111 if (Node->getOpcode() == ISD::SINT_TO_FP)
1112 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1113 Result, Node->getOperand(0).getValueType());
1114 else
1115 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1116 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001117 // No extra round required here.
1118 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001119 break;
1120 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001121 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1122 Node->getOperand(0));
1123 Result = LegalizeOp(Result);
1124
1125 // Round if we cannot tolerate excess precision.
1126 if (NoExcessFPPrecision)
1127 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1128 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001129 }
Chris Lattner4d978642005-01-15 22:16:26 +00001130 break;
1131
1132 case ISD::FP_TO_SINT:
1133 case ISD::FP_TO_UINT:
1134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1135 case Legal:
1136 Tmp1 = LegalizeOp(Node->getOperand(0));
1137 break;
1138 case Promote:
1139 // The input result is prerounded, so we don't have to do anything
1140 // special.
1141 Tmp1 = PromoteOp(Node->getOperand(0));
1142 break;
1143 case Expand:
1144 assert(0 && "not implemented");
1145 }
1146 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1147 break;
1148
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001149 case ISD::AND:
1150 case ISD::OR:
1151 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001152 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001153 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001154 case ISD::MUL:
1155 // The input may have strange things in the top bits of the registers, but
1156 // these operations don't care. They may have wierd bits going out, but
1157 // that too is okay if they are integer operations.
1158 Tmp1 = PromoteOp(Node->getOperand(0));
1159 Tmp2 = PromoteOp(Node->getOperand(1));
1160 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1161 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1162
1163 // However, if this is a floating point operation, they will give excess
1164 // precision that we may not be able to tolerate. If we DO allow excess
1165 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001166 // FIXME: Why would we need to round FP ops more than integer ones?
1167 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001168 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1169 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1170 break;
1171
Chris Lattner4d978642005-01-15 22:16:26 +00001172 case ISD::SDIV:
1173 case ISD::SREM:
1174 // These operators require that their input be sign extended.
1175 Tmp1 = PromoteOp(Node->getOperand(0));
1176 Tmp2 = PromoteOp(Node->getOperand(1));
1177 if (MVT::isInteger(NVT)) {
1178 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001179 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001180 }
1181 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1182
1183 // Perform FP_ROUND: this is probably overly pessimistic.
1184 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1185 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1186 break;
1187
1188 case ISD::UDIV:
1189 case ISD::UREM:
1190 // These operators require that their input be zero extended.
1191 Tmp1 = PromoteOp(Node->getOperand(0));
1192 Tmp2 = PromoteOp(Node->getOperand(1));
1193 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1194 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001195 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001196 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1197 break;
1198
1199 case ISD::SHL:
1200 Tmp1 = PromoteOp(Node->getOperand(0));
1201 Tmp2 = LegalizeOp(Node->getOperand(1));
1202 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1203 break;
1204 case ISD::SRA:
1205 // The input value must be properly sign extended.
1206 Tmp1 = PromoteOp(Node->getOperand(0));
1207 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1208 Tmp2 = LegalizeOp(Node->getOperand(1));
1209 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1210 break;
1211 case ISD::SRL:
1212 // The input value must be properly zero extended.
1213 Tmp1 = PromoteOp(Node->getOperand(0));
1214 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1215 Tmp2 = LegalizeOp(Node->getOperand(1));
1216 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1217 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001218 case ISD::LOAD:
1219 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1220 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1221 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1222
1223 // Remember that we legalized the chain.
1224 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1225 break;
1226 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001227 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1228 case Expand: assert(0 && "It's impossible to expand bools");
1229 case Legal:
1230 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1231 break;
1232 case Promote:
1233 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1234 break;
1235 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001236 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1237 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1238 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1239 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001240 case ISD::CALL: {
1241 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1242 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1243
Chris Lattner3d95c142005-01-19 20:24:35 +00001244 std::vector<SDOperand> Ops;
1245 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1246 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1247
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001248 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1249 "Can only promote single result calls");
1250 std::vector<MVT::ValueType> RetTyVTs;
1251 RetTyVTs.reserve(2);
1252 RetTyVTs.push_back(NVT);
1253 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001254 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001255 Result = SDOperand(NC, 0);
1256
1257 // Insert the new chain mapping.
1258 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1259 break;
1260 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001261 }
1262
1263 assert(Result.Val && "Didn't set a result!");
1264 AddPromotedOperand(Op, Result);
1265 return Result;
1266}
Chris Lattnerdc750592005-01-07 07:47:09 +00001267
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001268/// ExpandAddSub - Find a clever way to expand this add operation into
1269/// subcomponents.
1270void SelectionDAGLegalize::ExpandAddSub(bool isAdd, SDOperand LHS,SDOperand RHS,
1271 SDOperand &Lo, SDOperand &Hi) {
1272 // Expand the subcomponents.
1273 SDOperand LHSL, LHSH, RHSL, RHSH;
1274 ExpandOp(LHS, LHSL, LHSH);
1275 ExpandOp(RHS, RHSL, RHSH);
1276
1277 // Convert this add to the appropriate ADDC pair. The low part has no carry
1278 // in.
1279 unsigned Opc = isAdd ? ISD::ADD_PARTS : ISD::SUB_PARTS;
1280 std::vector<SDOperand> Ops;
1281 Ops.push_back(LHSL);
1282 Ops.push_back(LHSH);
1283 Ops.push_back(RHSL);
1284 Ops.push_back(RHSH);
1285 Lo = DAG.getNode(Opc, LHSL.getValueType(), Ops);
1286 Hi = Lo.getValue(1);
1287}
1288
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001289/// ExpandShift - Try to find a clever way to expand this shift operation out to
1290/// smaller elements. If we can't find a way that is more efficient than a
1291/// libcall on this target, return false. Otherwise, return true with the
1292/// low-parts expanded into Lo and Hi.
1293bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1294 SDOperand &Lo, SDOperand &Hi) {
1295 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1296 "This is not a shift!");
1297 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1298
1299 // If we have an efficient select operation (or if the selects will all fold
1300 // away), lower to some complex code, otherwise just emit the libcall.
1301 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1302 !isa<ConstantSDNode>(Amt))
1303 return false;
1304
1305 SDOperand InL, InH;
1306 ExpandOp(Op, InL, InH);
1307 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001308 MVT::ValueType ShTy = ShAmt.getValueType();
1309
1310 unsigned NVTBits = MVT::getSizeInBits(NVT);
1311 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1312 DAG.getConstant(NVTBits, ShTy), ShAmt);
1313
Chris Lattner4d25c042005-01-20 20:29:23 +00001314 // Compare the unmasked shift amount against 32.
1315 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1316 DAG.getConstant(NVTBits, ShTy));
1317
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001318 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1319 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1320 DAG.getConstant(NVTBits-1, ShTy));
1321 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1322 DAG.getConstant(NVTBits-1, ShTy));
1323 }
1324
1325 if (Opc == ISD::SHL) {
1326 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1327 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1328 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001329 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001330
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001331 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1332 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1333 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001334 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1335 DAG.getSetCC(ISD::SETEQ,
1336 TLI.getSetCCResultTy(), NAmt,
1337 DAG.getConstant(32, ShTy)),
1338 DAG.getConstant(0, NVT),
1339 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001340 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001341 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001342 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001343 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001344
1345 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001346 if (Opc == ISD::SRA)
1347 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1348 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001349 else
1350 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001351 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001352 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001353 }
1354 return true;
1355}
Chris Lattneraac464e2005-01-21 06:05:23 +00001356
Chris Lattner4add7e32005-01-23 04:42:50 +00001357/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1358/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1359/// Found.
1360static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1361 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1362
1363 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1364 // than the Found node. Just remember this node and return.
1365 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1366 Found = Node;
1367 return;
1368 }
1369
1370 // Otherwise, scan the operands of Node to see if any of them is a call.
1371 assert(Node->getNumOperands() != 0 &&
1372 "All leaves should have depth equal to the entry node!");
1373 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1374 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1375
1376 // Tail recurse for the last iteration.
1377 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1378 Found);
1379}
1380
1381
1382/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1383/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1384/// than Found.
1385static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1386 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1387
1388 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1389 // than the Found node. Just remember this node and return.
1390 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1391 Found = Node;
1392 return;
1393 }
1394
1395 // Otherwise, scan the operands of Node to see if any of them is a call.
1396 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1397 if (UI == E) return;
1398 for (--E; UI != E; ++UI)
1399 FindEarliestAdjCallStackUp(*UI, Found);
1400
1401 // Tail recurse for the last iteration.
1402 FindEarliestAdjCallStackUp(*UI, Found);
1403}
1404
1405/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1406/// find the ADJCALLSTACKUP node that terminates the call sequence.
1407static SDNode *FindAdjCallStackUp(SDNode *Node) {
1408 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1409 return Node;
1410 assert(!Node->use_empty() && "Could not find ADJCALLSTACKUP!");
1411
1412 if (Node->hasOneUse()) // Simple case, only has one user to check.
1413 return FindAdjCallStackUp(*Node->use_begin());
1414
1415 SDOperand TheChain(Node, Node->getNumValues()-1);
1416 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1417
1418 for (SDNode::use_iterator UI = Node->use_begin(),
1419 E = Node->use_end(); ; ++UI) {
1420 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1421
1422 // Make sure to only follow users of our token chain.
1423 SDNode *User = *UI;
1424 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1425 if (User->getOperand(i) == TheChain)
1426 return FindAdjCallStackUp(User);
1427 }
1428 assert(0 && "Unreachable");
1429 abort();
1430}
1431
1432/// FindInputOutputChains - If we are replacing an operation with a call we need
1433/// to find the call that occurs before and the call that occurs after it to
1434/// properly serialize the calls in the block.
1435static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1436 SDOperand Entry) {
1437 SDNode *LatestAdjCallStackDown = Entry.Val;
1438 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1439 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1440
1441 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1442
1443
1444 SDNode *EarliestAdjCallStackUp = 0;
1445 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1446
1447 if (EarliestAdjCallStackUp) {
1448 //std::cerr << "Found node: ";
1449 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1450 }
1451
1452 return SDOperand(LatestAdjCallStackUp, 0);
1453}
1454
1455
1456
Chris Lattneraac464e2005-01-21 06:05:23 +00001457// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1458// does not fit into a register, return the lo part and set the hi part to the
1459// by-reg argument. If it does fit into a single register, return the result
1460// and leave the Hi part unset.
1461SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1462 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001463 SDNode *OutChain;
1464 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1465 DAG.getEntryNode());
1466 // TODO. Link in chains.
1467
Chris Lattneraac464e2005-01-21 06:05:23 +00001468 TargetLowering::ArgListTy Args;
1469 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1470 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1471 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1472 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1473 }
1474 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1475
1476 // We don't care about token chains for libcalls. We just use the entry
1477 // node as our input and ignore the output chain. This allows us to place
1478 // calls wherever we need them to satisfy data dependences.
1479 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begemanf6565252005-03-26 01:29:23 +00001480 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001481 Args, DAG).first;
1482 switch (getTypeAction(Result.getValueType())) {
1483 default: assert(0 && "Unknown thing");
1484 case Legal:
1485 return Result;
1486 case Promote:
1487 assert(0 && "Cannot promote this yet!");
1488 case Expand:
1489 SDOperand Lo;
1490 ExpandOp(Result, Lo, Hi);
1491 return Lo;
1492 }
1493}
1494
Chris Lattner4add7e32005-01-23 04:42:50 +00001495
Chris Lattneraac464e2005-01-21 06:05:23 +00001496/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1497/// destination type is legal.
1498SDOperand SelectionDAGLegalize::
1499ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1500 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1501 assert(getTypeAction(Source.getValueType()) == Expand &&
1502 "This is not an expansion!");
1503 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1504
Chris Lattner4add7e32005-01-23 04:42:50 +00001505 SDNode *OutChain;
1506 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1507 DAG.getEntryNode());
1508
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001509 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001510 if (isSigned) {
1511 if (DestTy == MVT::f32)
1512 FnName = "__floatdisf";
1513 else {
1514 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1515 FnName = "__floatdidf";
1516 }
1517 } else {
1518 // If this is unsigned, and not supported, first perform the conversion to
1519 // signed, then adjust the result if the sign bit is set.
1520 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1521
1522 assert(0 && "Unsigned casts not supported yet!");
1523 }
1524 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1525
1526 TargetLowering::ArgListTy Args;
1527 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1528 Args.push_back(std::make_pair(Source, ArgTy));
1529
1530 // We don't care about token chains for libcalls. We just use the entry
1531 // node as our input and ignore the output chain. This allows us to place
1532 // calls wherever we need them to satisfy data dependences.
1533 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begemanf6565252005-03-26 01:29:23 +00001534 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattner4add7e32005-01-23 04:42:50 +00001535
Chris Lattneraac464e2005-01-21 06:05:23 +00001536}
1537
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001538
1539
Chris Lattnerdc750592005-01-07 07:47:09 +00001540/// ExpandOp - Expand the specified SDOperand into its two component pieces
1541/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1542/// LegalizeNodes map is filled in for any results that are not expanded, the
1543/// ExpandedNodes map is filled in for any results that are expanded, and the
1544/// Lo/Hi values are returned.
1545void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1546 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001547 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001548 SDNode *Node = Op.Val;
1549 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1550 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1551 assert(MVT::isInteger(NVT) && NVT < VT &&
1552 "Cannot expand to FP value or to larger int value!");
1553
1554 // If there is more than one use of this, see if we already expanded it.
1555 // There is no use remembering values that only have a single use, as the map
1556 // entries will never be reused.
1557 if (!Node->hasOneUse()) {
1558 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1559 = ExpandedNodes.find(Op);
1560 if (I != ExpandedNodes.end()) {
1561 Lo = I->second.first;
1562 Hi = I->second.second;
1563 return;
1564 }
1565 }
1566
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001567 // Expanding to multiple registers needs to perform an optimization step, and
1568 // is not careful to avoid operations the target does not support. Make sure
1569 // that all generated operations are legalized in the next iteration.
1570 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001571
Chris Lattnerdc750592005-01-07 07:47:09 +00001572 switch (Node->getOpcode()) {
1573 default:
1574 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1575 assert(0 && "Do not know how to expand this operator!");
1576 abort();
1577 case ISD::Constant: {
1578 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1579 Lo = DAG.getConstant(Cst, NVT);
1580 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1581 break;
1582 }
1583
1584 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001585 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001586 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001587 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1588 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1589
1590 // Remember that we legalized the chain.
1591 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1592
Chris Lattnerdc750592005-01-07 07:47:09 +00001593 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1594 break;
1595 }
1596
Chris Lattner32e08b72005-03-28 22:03:13 +00001597 case ISD::BUILD_PAIR:
1598 // Legalize both operands. FIXME: in the future we should handle the case
1599 // where the two elements are not legal.
1600 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1601 Lo = LegalizeOp(Node->getOperand(0));
1602 Hi = LegalizeOp(Node->getOperand(1));
1603 break;
1604
Chris Lattnerdc750592005-01-07 07:47:09 +00001605 case ISD::LOAD: {
1606 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1607 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1608 Lo = DAG.getLoad(NVT, Ch, Ptr);
1609
1610 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001611 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001612 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1613 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001614 Hi = DAG.getLoad(NVT, Ch, Ptr);
1615
1616 // Build a factor node to remember that this load is independent of the
1617 // other one.
1618 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1619 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001620
1621 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001622 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001623 if (!TLI.isLittleEndian())
1624 std::swap(Lo, Hi);
1625 break;
1626 }
1627 case ISD::CALL: {
1628 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1629 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1630
Chris Lattner3d95c142005-01-19 20:24:35 +00001631 bool Changed = false;
1632 std::vector<SDOperand> Ops;
1633 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1634 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1635 Changed |= Ops.back() != Node->getOperand(i);
1636 }
1637
Chris Lattnerdc750592005-01-07 07:47:09 +00001638 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1639 "Can only expand a call once so far, not i64 -> i16!");
1640
1641 std::vector<MVT::ValueType> RetTyVTs;
1642 RetTyVTs.reserve(3);
1643 RetTyVTs.push_back(NVT);
1644 RetTyVTs.push_back(NVT);
1645 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001646 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001647 Lo = SDOperand(NC, 0);
1648 Hi = SDOperand(NC, 1);
1649
1650 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001651 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001652 break;
1653 }
1654 case ISD::AND:
1655 case ISD::OR:
1656 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1657 SDOperand LL, LH, RL, RH;
1658 ExpandOp(Node->getOperand(0), LL, LH);
1659 ExpandOp(Node->getOperand(1), RL, RH);
1660 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1661 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1662 break;
1663 }
1664 case ISD::SELECT: {
1665 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001666
1667 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1668 case Expand: assert(0 && "It's impossible to expand bools");
1669 case Legal:
1670 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1671 break;
1672 case Promote:
1673 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1674 break;
1675 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001676 ExpandOp(Node->getOperand(1), LL, LH);
1677 ExpandOp(Node->getOperand(2), RL, RH);
1678 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1679 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1680 break;
1681 }
1682 case ISD::SIGN_EXTEND: {
1683 // The low part is just a sign extension of the input (which degenerates to
1684 // a copy).
1685 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1686
1687 // The high part is obtained by SRA'ing all but one of the bits of the lo
1688 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001689 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001690 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1691 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001692 break;
1693 }
1694 case ISD::ZERO_EXTEND:
1695 // The low part is just a zero extension of the input (which degenerates to
1696 // a copy).
1697 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1698
1699 // The high part is just a zero.
1700 Hi = DAG.getConstant(0, NVT);
1701 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001702
1703 // These operators cannot be expanded directly, emit them as calls to
1704 // library functions.
1705 case ISD::FP_TO_SINT:
1706 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001707 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001708 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001709 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001710 break;
1711 case ISD::FP_TO_UINT:
1712 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001713 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001714 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001715 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001716 break;
1717
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001718 case ISD::SHL:
1719 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001720 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001721 break;
1722 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001723 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001724 break;
1725
1726 case ISD::SRA:
1727 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001728 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001729 break;
1730 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001731 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001732 break;
1733 case ISD::SRL:
1734 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001735 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001736 break;
1737 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001738 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001739 break;
1740
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001741 case ISD::ADD:
1742 ExpandAddSub(true, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1743 break;
1744 case ISD::SUB:
1745 ExpandAddSub(false, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1746 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001747 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1748 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1749 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1750 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1751 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001752 }
1753
1754 // Remember in a map if the values will be reused later.
1755 if (!Node->hasOneUse()) {
1756 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1757 std::make_pair(Lo, Hi))).second;
1758 assert(isNew && "Value already expanded?!?");
1759 }
1760}
1761
1762
1763// SelectionDAG::Legalize - This is the entry point for the file.
1764//
Chris Lattner4add7e32005-01-23 04:42:50 +00001765void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00001766 /// run - This is the main entry point to this class.
1767 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00001768 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00001769}
1770